sdhci-sirf.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * SDHCI support for SiRF primaII and marco SoCs
  3. *
  4. * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
  5. *
  6. * Licensed under GPLv2 or later.
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/device.h>
  10. #include <linux/mmc/host.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_gpio.h>
  14. #include <linux/mmc/slot-gpio.h>
  15. #include "sdhci-pltfm.h"
  16. #define SDHCI_SIRF_8BITBUS BIT(3)
  17. struct sdhci_sirf_priv {
  18. struct clk *clk;
  19. int gpio_cd;
  20. };
  21. static unsigned int sdhci_sirf_get_max_clk(struct sdhci_host *host)
  22. {
  23. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  24. struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host);
  25. return clk_get_rate(priv->clk);
  26. }
  27. static void sdhci_sirf_set_bus_width(struct sdhci_host *host, int width)
  28. {
  29. u8 ctrl;
  30. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  31. ctrl &= ~(SDHCI_CTRL_4BITBUS | SDHCI_SIRF_8BITBUS);
  32. /*
  33. * CSR atlas7 and prima2 SD host version is not 3.0
  34. * 8bit-width enable bit of CSR SD hosts is 3,
  35. * while stardard hosts use bit 5
  36. */
  37. if (width == MMC_BUS_WIDTH_8)
  38. ctrl |= SDHCI_SIRF_8BITBUS;
  39. else if (width == MMC_BUS_WIDTH_4)
  40. ctrl |= SDHCI_CTRL_4BITBUS;
  41. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  42. }
  43. static struct sdhci_ops sdhci_sirf_ops = {
  44. .set_clock = sdhci_set_clock,
  45. .get_max_clock = sdhci_sirf_get_max_clk,
  46. .set_bus_width = sdhci_sirf_set_bus_width,
  47. .reset = sdhci_reset,
  48. .set_uhs_signaling = sdhci_set_uhs_signaling,
  49. };
  50. static struct sdhci_pltfm_data sdhci_sirf_pdata = {
  51. .ops = &sdhci_sirf_ops,
  52. .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  53. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  54. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
  55. SDHCI_QUIRK_INVERTED_WRITE_PROTECT |
  56. SDHCI_QUIRK_DELAY_AFTER_POWER,
  57. };
  58. static int sdhci_sirf_probe(struct platform_device *pdev)
  59. {
  60. struct sdhci_host *host;
  61. struct sdhci_pltfm_host *pltfm_host;
  62. struct sdhci_sirf_priv *priv;
  63. struct clk *clk;
  64. int gpio_cd;
  65. int ret;
  66. clk = devm_clk_get(&pdev->dev, NULL);
  67. if (IS_ERR(clk)) {
  68. dev_err(&pdev->dev, "unable to get clock");
  69. return PTR_ERR(clk);
  70. }
  71. if (pdev->dev.of_node)
  72. gpio_cd = of_get_named_gpio(pdev->dev.of_node, "cd-gpios", 0);
  73. else
  74. gpio_cd = -EINVAL;
  75. host = sdhci_pltfm_init(pdev, &sdhci_sirf_pdata, sizeof(struct sdhci_sirf_priv));
  76. if (IS_ERR(host))
  77. return PTR_ERR(host);
  78. pltfm_host = sdhci_priv(host);
  79. priv = sdhci_pltfm_priv(pltfm_host);
  80. priv->clk = clk;
  81. priv->gpio_cd = gpio_cd;
  82. sdhci_get_of_property(pdev);
  83. ret = clk_prepare_enable(priv->clk);
  84. if (ret)
  85. goto err_clk_prepare;
  86. ret = sdhci_add_host(host);
  87. if (ret)
  88. goto err_sdhci_add;
  89. /*
  90. * We must request the IRQ after sdhci_add_host(), as the tasklet only
  91. * gets setup in sdhci_add_host() and we oops.
  92. */
  93. if (gpio_is_valid(priv->gpio_cd)) {
  94. ret = mmc_gpio_request_cd(host->mmc, priv->gpio_cd, 0);
  95. if (ret) {
  96. dev_err(&pdev->dev, "card detect irq request failed: %d\n",
  97. ret);
  98. goto err_request_cd;
  99. }
  100. mmc_gpiod_request_cd_irq(host->mmc);
  101. }
  102. return 0;
  103. err_request_cd:
  104. sdhci_remove_host(host, 0);
  105. err_sdhci_add:
  106. clk_disable_unprepare(priv->clk);
  107. err_clk_prepare:
  108. sdhci_pltfm_free(pdev);
  109. return ret;
  110. }
  111. static int sdhci_sirf_remove(struct platform_device *pdev)
  112. {
  113. struct sdhci_host *host = platform_get_drvdata(pdev);
  114. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  115. struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host);
  116. sdhci_pltfm_unregister(pdev);
  117. if (gpio_is_valid(priv->gpio_cd))
  118. mmc_gpio_free_cd(host->mmc);
  119. clk_disable_unprepare(priv->clk);
  120. return 0;
  121. }
  122. #ifdef CONFIG_PM_SLEEP
  123. static int sdhci_sirf_suspend(struct device *dev)
  124. {
  125. struct sdhci_host *host = dev_get_drvdata(dev);
  126. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  127. struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host);
  128. int ret;
  129. ret = sdhci_suspend_host(host);
  130. if (ret)
  131. return ret;
  132. clk_disable(priv->clk);
  133. return 0;
  134. }
  135. static int sdhci_sirf_resume(struct device *dev)
  136. {
  137. struct sdhci_host *host = dev_get_drvdata(dev);
  138. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  139. struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host);
  140. int ret;
  141. ret = clk_enable(priv->clk);
  142. if (ret) {
  143. dev_dbg(dev, "Resume: Error enabling clock\n");
  144. return ret;
  145. }
  146. return sdhci_resume_host(host);
  147. }
  148. static SIMPLE_DEV_PM_OPS(sdhci_sirf_pm_ops, sdhci_sirf_suspend, sdhci_sirf_resume);
  149. #endif
  150. static const struct of_device_id sdhci_sirf_of_match[] = {
  151. { .compatible = "sirf,prima2-sdhc" },
  152. { }
  153. };
  154. MODULE_DEVICE_TABLE(of, sdhci_sirf_of_match);
  155. static struct platform_driver sdhci_sirf_driver = {
  156. .driver = {
  157. .name = "sdhci-sirf",
  158. .of_match_table = sdhci_sirf_of_match,
  159. #ifdef CONFIG_PM_SLEEP
  160. .pm = &sdhci_sirf_pm_ops,
  161. #endif
  162. },
  163. .probe = sdhci_sirf_probe,
  164. .remove = sdhci_sirf_remove,
  165. };
  166. module_platform_driver(sdhci_sirf_driver);
  167. MODULE_DESCRIPTION("SDHCI driver for SiRFprimaII/SiRFmarco");
  168. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  169. MODULE_LICENSE("GPL v2");