sdhci-spear.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * drivers/mmc/host/sdhci-spear.c
  3. *
  4. * Support of SDHCI platform devices for spear soc family
  5. *
  6. * Copyright (C) 2010 ST Microelectronics
  7. * Viresh Kumar <viresh.linux@gmail.com>
  8. *
  9. * Inspired by sdhci-pltfm.c
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/gpio.h>
  18. #include <linux/highmem.h>
  19. #include <linux/module.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/of.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm.h>
  26. #include <linux/slab.h>
  27. #include <linux/mmc/host.h>
  28. #include <linux/mmc/sdhci-spear.h>
  29. #include <linux/mmc/slot-gpio.h>
  30. #include <linux/io.h>
  31. #include "sdhci.h"
  32. struct spear_sdhci {
  33. struct clk *clk;
  34. struct sdhci_plat_data *data;
  35. };
  36. /* sdhci ops */
  37. static const struct sdhci_ops sdhci_pltfm_ops = {
  38. .set_clock = sdhci_set_clock,
  39. .set_bus_width = sdhci_set_bus_width,
  40. .reset = sdhci_reset,
  41. .set_uhs_signaling = sdhci_set_uhs_signaling,
  42. };
  43. #ifdef CONFIG_OF
  44. static struct sdhci_plat_data *sdhci_probe_config_dt(struct platform_device *pdev)
  45. {
  46. struct device_node *np = pdev->dev.of_node;
  47. struct sdhci_plat_data *pdata = NULL;
  48. int cd_gpio;
  49. cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
  50. if (!gpio_is_valid(cd_gpio))
  51. cd_gpio = -1;
  52. /* If pdata is required */
  53. if (cd_gpio != -1) {
  54. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  55. if (!pdata)
  56. dev_err(&pdev->dev, "DT: kzalloc failed\n");
  57. else
  58. pdata->card_int_gpio = cd_gpio;
  59. }
  60. return pdata;
  61. }
  62. #else
  63. static struct sdhci_plat_data *sdhci_probe_config_dt(struct platform_device *pdev)
  64. {
  65. return ERR_PTR(-ENOSYS);
  66. }
  67. #endif
  68. static int sdhci_probe(struct platform_device *pdev)
  69. {
  70. struct device_node *np = pdev->dev.of_node;
  71. struct sdhci_host *host;
  72. struct resource *iomem;
  73. struct spear_sdhci *sdhci;
  74. struct device *dev;
  75. int ret;
  76. dev = pdev->dev.parent ? pdev->dev.parent : &pdev->dev;
  77. host = sdhci_alloc_host(dev, sizeof(*sdhci));
  78. if (IS_ERR(host)) {
  79. ret = PTR_ERR(host);
  80. dev_dbg(&pdev->dev, "cannot allocate memory for sdhci\n");
  81. goto err;
  82. }
  83. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  84. host->ioaddr = devm_ioremap_resource(&pdev->dev, iomem);
  85. if (IS_ERR(host->ioaddr)) {
  86. ret = PTR_ERR(host->ioaddr);
  87. dev_dbg(&pdev->dev, "unable to map iomem: %d\n", ret);
  88. goto err_host;
  89. }
  90. host->hw_name = "sdhci";
  91. host->ops = &sdhci_pltfm_ops;
  92. host->irq = platform_get_irq(pdev, 0);
  93. host->quirks = SDHCI_QUIRK_BROKEN_ADMA;
  94. sdhci = sdhci_priv(host);
  95. /* clk enable */
  96. sdhci->clk = devm_clk_get(&pdev->dev, NULL);
  97. if (IS_ERR(sdhci->clk)) {
  98. ret = PTR_ERR(sdhci->clk);
  99. dev_dbg(&pdev->dev, "Error getting clock\n");
  100. goto err_host;
  101. }
  102. ret = clk_prepare_enable(sdhci->clk);
  103. if (ret) {
  104. dev_dbg(&pdev->dev, "Error enabling clock\n");
  105. goto err_host;
  106. }
  107. ret = clk_set_rate(sdhci->clk, 50000000);
  108. if (ret)
  109. dev_dbg(&pdev->dev, "Error setting desired clk, clk=%lu\n",
  110. clk_get_rate(sdhci->clk));
  111. if (np) {
  112. sdhci->data = sdhci_probe_config_dt(pdev);
  113. if (IS_ERR(sdhci->data)) {
  114. dev_err(&pdev->dev, "DT: Failed to get pdata\n");
  115. goto disable_clk;
  116. }
  117. } else {
  118. sdhci->data = dev_get_platdata(&pdev->dev);
  119. }
  120. /*
  121. * It is optional to use GPIOs for sdhci card detection. If
  122. * sdhci->data is NULL, then use original sdhci lines otherwise
  123. * GPIO lines. We use the built-in GPIO support for this.
  124. */
  125. if (sdhci->data && sdhci->data->card_int_gpio >= 0) {
  126. ret = mmc_gpio_request_cd(host->mmc,
  127. sdhci->data->card_int_gpio, 0);
  128. if (ret < 0) {
  129. dev_dbg(&pdev->dev,
  130. "failed to request card-detect gpio%d\n",
  131. sdhci->data->card_int_gpio);
  132. goto disable_clk;
  133. }
  134. }
  135. ret = sdhci_add_host(host);
  136. if (ret) {
  137. dev_dbg(&pdev->dev, "error adding host\n");
  138. goto disable_clk;
  139. }
  140. platform_set_drvdata(pdev, host);
  141. return 0;
  142. disable_clk:
  143. clk_disable_unprepare(sdhci->clk);
  144. err_host:
  145. sdhci_free_host(host);
  146. err:
  147. dev_err(&pdev->dev, "spear-sdhci probe failed: %d\n", ret);
  148. return ret;
  149. }
  150. static int sdhci_remove(struct platform_device *pdev)
  151. {
  152. struct sdhci_host *host = platform_get_drvdata(pdev);
  153. struct spear_sdhci *sdhci = sdhci_priv(host);
  154. int dead = 0;
  155. u32 scratch;
  156. scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
  157. if (scratch == (u32)-1)
  158. dead = 1;
  159. sdhci_remove_host(host, dead);
  160. clk_disable_unprepare(sdhci->clk);
  161. sdhci_free_host(host);
  162. return 0;
  163. }
  164. #ifdef CONFIG_PM_SLEEP
  165. static int sdhci_suspend(struct device *dev)
  166. {
  167. struct sdhci_host *host = dev_get_drvdata(dev);
  168. struct spear_sdhci *sdhci = sdhci_priv(host);
  169. int ret;
  170. ret = sdhci_suspend_host(host);
  171. if (!ret)
  172. clk_disable(sdhci->clk);
  173. return ret;
  174. }
  175. static int sdhci_resume(struct device *dev)
  176. {
  177. struct sdhci_host *host = dev_get_drvdata(dev);
  178. struct spear_sdhci *sdhci = sdhci_priv(host);
  179. int ret;
  180. ret = clk_enable(sdhci->clk);
  181. if (ret) {
  182. dev_dbg(dev, "Resume: Error enabling clock\n");
  183. return ret;
  184. }
  185. return sdhci_resume_host(host);
  186. }
  187. #endif
  188. static SIMPLE_DEV_PM_OPS(sdhci_pm_ops, sdhci_suspend, sdhci_resume);
  189. #ifdef CONFIG_OF
  190. static const struct of_device_id sdhci_spear_id_table[] = {
  191. { .compatible = "st,spear300-sdhci" },
  192. {}
  193. };
  194. MODULE_DEVICE_TABLE(of, sdhci_spear_id_table);
  195. #endif
  196. static struct platform_driver sdhci_driver = {
  197. .driver = {
  198. .name = "sdhci",
  199. .owner = THIS_MODULE,
  200. .pm = &sdhci_pm_ops,
  201. .of_match_table = of_match_ptr(sdhci_spear_id_table),
  202. },
  203. .probe = sdhci_probe,
  204. .remove = sdhci_remove,
  205. };
  206. module_platform_driver(sdhci_driver);
  207. MODULE_DESCRIPTION("SPEAr Secure Digital Host Controller Interface driver");
  208. MODULE_AUTHOR("Viresh Kumar <viresh.linux@gmail.com>");
  209. MODULE_LICENSE("GPL v2");