sdhci-sirf.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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_CLK_DELAY_SETTING 0x4C
  17. #define SDHCI_SIRF_8BITBUS BIT(3)
  18. #define SIRF_TUNING_COUNT 128
  19. struct sdhci_sirf_priv {
  20. int gpio_cd;
  21. };
  22. static void sdhci_sirf_set_bus_width(struct sdhci_host *host, int width)
  23. {
  24. u8 ctrl;
  25. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  26. ctrl &= ~(SDHCI_CTRL_4BITBUS | SDHCI_SIRF_8BITBUS);
  27. /*
  28. * CSR atlas7 and prima2 SD host version is not 3.0
  29. * 8bit-width enable bit of CSR SD hosts is 3,
  30. * while stardard hosts use bit 5
  31. */
  32. if (width == MMC_BUS_WIDTH_8)
  33. ctrl |= SDHCI_SIRF_8BITBUS;
  34. else if (width == MMC_BUS_WIDTH_4)
  35. ctrl |= SDHCI_CTRL_4BITBUS;
  36. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  37. }
  38. static int sdhci_sirf_execute_tuning(struct sdhci_host *host, u32 opcode)
  39. {
  40. int tuning_seq_cnt = 3;
  41. u8 phase, tuned_phases[SIRF_TUNING_COUNT];
  42. u8 tuned_phase_cnt = 0;
  43. int rc = 0, longest_range = 0;
  44. int start = -1, end = 0, tuning_value = -1, range = 0;
  45. u16 clock_setting;
  46. struct mmc_host *mmc = host->mmc;
  47. clock_setting = sdhci_readw(host, SDHCI_CLK_DELAY_SETTING);
  48. clock_setting &= ~0x3fff;
  49. retry:
  50. phase = 0;
  51. do {
  52. sdhci_writel(host,
  53. clock_setting | phase,
  54. SDHCI_CLK_DELAY_SETTING);
  55. if (!mmc_send_tuning(mmc)) {
  56. /* Tuning is successful at this tuning point */
  57. tuned_phases[tuned_phase_cnt++] = phase;
  58. dev_dbg(mmc_dev(mmc), "%s: Found good phase = %d\n",
  59. mmc_hostname(mmc), phase);
  60. if (start == -1)
  61. start = phase;
  62. end = phase;
  63. range++;
  64. if (phase == (SIRF_TUNING_COUNT - 1)
  65. && range > longest_range)
  66. tuning_value = (start + end) / 2;
  67. } else {
  68. dev_dbg(mmc_dev(mmc), "%s: Found bad phase = %d\n",
  69. mmc_hostname(mmc), phase);
  70. if (range > longest_range) {
  71. tuning_value = (start + end) / 2;
  72. longest_range = range;
  73. }
  74. start = -1;
  75. end = range = 0;
  76. }
  77. } while (++phase < ARRAY_SIZE(tuned_phases));
  78. if (tuned_phase_cnt && tuning_value > 0) {
  79. /*
  80. * Finally set the selected phase in delay
  81. * line hw block.
  82. */
  83. phase = tuning_value;
  84. sdhci_writel(host,
  85. clock_setting | phase,
  86. SDHCI_CLK_DELAY_SETTING);
  87. dev_dbg(mmc_dev(mmc), "%s: Setting the tuning phase to %d\n",
  88. mmc_hostname(mmc), phase);
  89. } else {
  90. if (--tuning_seq_cnt)
  91. goto retry;
  92. /* Tuning failed */
  93. dev_dbg(mmc_dev(mmc), "%s: No tuning point found\n",
  94. mmc_hostname(mmc));
  95. rc = -EIO;
  96. }
  97. return rc;
  98. }
  99. static struct sdhci_ops sdhci_sirf_ops = {
  100. .platform_execute_tuning = sdhci_sirf_execute_tuning,
  101. .set_clock = sdhci_set_clock,
  102. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  103. .set_bus_width = sdhci_sirf_set_bus_width,
  104. .reset = sdhci_reset,
  105. .set_uhs_signaling = sdhci_set_uhs_signaling,
  106. };
  107. static struct sdhci_pltfm_data sdhci_sirf_pdata = {
  108. .ops = &sdhci_sirf_ops,
  109. .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  110. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  111. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
  112. SDHCI_QUIRK_INVERTED_WRITE_PROTECT |
  113. SDHCI_QUIRK_DELAY_AFTER_POWER,
  114. };
  115. static int sdhci_sirf_probe(struct platform_device *pdev)
  116. {
  117. struct sdhci_host *host;
  118. struct sdhci_pltfm_host *pltfm_host;
  119. struct sdhci_sirf_priv *priv;
  120. struct clk *clk;
  121. int gpio_cd;
  122. int ret;
  123. clk = devm_clk_get(&pdev->dev, NULL);
  124. if (IS_ERR(clk)) {
  125. dev_err(&pdev->dev, "unable to get clock");
  126. return PTR_ERR(clk);
  127. }
  128. if (pdev->dev.of_node)
  129. gpio_cd = of_get_named_gpio(pdev->dev.of_node, "cd-gpios", 0);
  130. else
  131. gpio_cd = -EINVAL;
  132. host = sdhci_pltfm_init(pdev, &sdhci_sirf_pdata, sizeof(struct sdhci_sirf_priv));
  133. if (IS_ERR(host))
  134. return PTR_ERR(host);
  135. pltfm_host = sdhci_priv(host);
  136. pltfm_host->clk = clk;
  137. priv = sdhci_pltfm_priv(pltfm_host);
  138. priv->gpio_cd = gpio_cd;
  139. sdhci_get_of_property(pdev);
  140. ret = clk_prepare_enable(pltfm_host->clk);
  141. if (ret)
  142. goto err_clk_prepare;
  143. ret = sdhci_add_host(host);
  144. if (ret)
  145. goto err_sdhci_add;
  146. /*
  147. * We must request the IRQ after sdhci_add_host(), as the tasklet only
  148. * gets setup in sdhci_add_host() and we oops.
  149. */
  150. if (gpio_is_valid(priv->gpio_cd)) {
  151. ret = mmc_gpio_request_cd(host->mmc, priv->gpio_cd, 0);
  152. if (ret) {
  153. dev_err(&pdev->dev, "card detect irq request failed: %d\n",
  154. ret);
  155. goto err_request_cd;
  156. }
  157. mmc_gpiod_request_cd_irq(host->mmc);
  158. }
  159. return 0;
  160. err_request_cd:
  161. sdhci_remove_host(host, 0);
  162. err_sdhci_add:
  163. clk_disable_unprepare(pltfm_host->clk);
  164. err_clk_prepare:
  165. sdhci_pltfm_free(pdev);
  166. return ret;
  167. }
  168. #ifdef CONFIG_PM_SLEEP
  169. static int sdhci_sirf_suspend(struct device *dev)
  170. {
  171. struct sdhci_host *host = dev_get_drvdata(dev);
  172. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  173. int ret;
  174. ret = sdhci_suspend_host(host);
  175. if (ret)
  176. return ret;
  177. clk_disable(pltfm_host->clk);
  178. return 0;
  179. }
  180. static int sdhci_sirf_resume(struct device *dev)
  181. {
  182. struct sdhci_host *host = dev_get_drvdata(dev);
  183. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  184. int ret;
  185. ret = clk_enable(pltfm_host->clk);
  186. if (ret) {
  187. dev_dbg(dev, "Resume: Error enabling clock\n");
  188. return ret;
  189. }
  190. return sdhci_resume_host(host);
  191. }
  192. static SIMPLE_DEV_PM_OPS(sdhci_sirf_pm_ops, sdhci_sirf_suspend, sdhci_sirf_resume);
  193. #endif
  194. static const struct of_device_id sdhci_sirf_of_match[] = {
  195. { .compatible = "sirf,prima2-sdhc" },
  196. { }
  197. };
  198. MODULE_DEVICE_TABLE(of, sdhci_sirf_of_match);
  199. static struct platform_driver sdhci_sirf_driver = {
  200. .driver = {
  201. .name = "sdhci-sirf",
  202. .of_match_table = sdhci_sirf_of_match,
  203. #ifdef CONFIG_PM_SLEEP
  204. .pm = &sdhci_sirf_pm_ops,
  205. #endif
  206. },
  207. .probe = sdhci_sirf_probe,
  208. .remove = sdhci_pltfm_unregister,
  209. };
  210. module_platform_driver(sdhci_sirf_driver);
  211. MODULE_DESCRIPTION("SDHCI driver for SiRFprimaII/SiRFmarco");
  212. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  213. MODULE_LICENSE("GPL v2");