sdhci-sirf.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 16384
  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 u32 sdhci_sirf_readl_le(struct sdhci_host *host, int reg)
  39. {
  40. u32 val = readl(host->ioaddr + reg);
  41. if (unlikely((reg == SDHCI_CAPABILITIES_1) &&
  42. (host->mmc->caps & MMC_CAP_UHS_SDR50))) {
  43. /* fake CAP_1 register */
  44. val = SDHCI_SUPPORT_SDR50 | SDHCI_USE_SDR50_TUNING;
  45. }
  46. if (unlikely(reg == SDHCI_SLOT_INT_STATUS)) {
  47. u32 prss = val;
  48. /* fake chips as V3.0 host conreoller */
  49. prss &= ~(0xFF << 16);
  50. val = prss | (SDHCI_SPEC_300 << 16);
  51. }
  52. return val;
  53. }
  54. static u16 sdhci_sirf_readw_le(struct sdhci_host *host, int reg)
  55. {
  56. u16 ret = 0;
  57. ret = readw(host->ioaddr + reg);
  58. if (unlikely(reg == SDHCI_HOST_VERSION)) {
  59. ret = readw(host->ioaddr + SDHCI_HOST_VERSION);
  60. ret |= SDHCI_SPEC_300;
  61. }
  62. return ret;
  63. }
  64. static int sdhci_sirf_execute_tuning(struct sdhci_host *host, u32 opcode)
  65. {
  66. int tuning_seq_cnt = 3;
  67. int phase;
  68. u8 tuned_phase_cnt = 0;
  69. int rc = 0, longest_range = 0;
  70. int start = -1, end = 0, tuning_value = -1, range = 0;
  71. u16 clock_setting;
  72. struct mmc_host *mmc = host->mmc;
  73. clock_setting = sdhci_readw(host, SDHCI_CLK_DELAY_SETTING);
  74. clock_setting &= ~0x3fff;
  75. retry:
  76. phase = 0;
  77. tuned_phase_cnt = 0;
  78. do {
  79. sdhci_writel(host,
  80. clock_setting | phase,
  81. SDHCI_CLK_DELAY_SETTING);
  82. if (!mmc_send_tuning(mmc)) {
  83. /* Tuning is successful at this tuning point */
  84. tuned_phase_cnt++;
  85. dev_dbg(mmc_dev(mmc), "%s: Found good phase = %d\n",
  86. mmc_hostname(mmc), phase);
  87. if (start == -1)
  88. start = phase;
  89. end = phase;
  90. range++;
  91. if (phase == (SIRF_TUNING_COUNT - 1)
  92. && range > longest_range)
  93. tuning_value = (start + end) / 2;
  94. } else {
  95. dev_dbg(mmc_dev(mmc), "%s: Found bad phase = %d\n",
  96. mmc_hostname(mmc), phase);
  97. if (range > longest_range) {
  98. tuning_value = (start + end) / 2;
  99. longest_range = range;
  100. }
  101. start = -1;
  102. end = range = 0;
  103. }
  104. } while (++phase < SIRF_TUNING_COUNT);
  105. if (tuned_phase_cnt && tuning_value > 0) {
  106. /*
  107. * Finally set the selected phase in delay
  108. * line hw block.
  109. */
  110. phase = tuning_value;
  111. sdhci_writel(host,
  112. clock_setting | phase,
  113. SDHCI_CLK_DELAY_SETTING);
  114. dev_dbg(mmc_dev(mmc), "%s: Setting the tuning phase to %d\n",
  115. mmc_hostname(mmc), phase);
  116. } else {
  117. if (--tuning_seq_cnt)
  118. goto retry;
  119. /* Tuning failed */
  120. dev_dbg(mmc_dev(mmc), "%s: No tuning point found\n",
  121. mmc_hostname(mmc));
  122. rc = -EIO;
  123. }
  124. return rc;
  125. }
  126. static struct sdhci_ops sdhci_sirf_ops = {
  127. .read_l = sdhci_sirf_readl_le,
  128. .read_w = sdhci_sirf_readw_le,
  129. .platform_execute_tuning = sdhci_sirf_execute_tuning,
  130. .set_clock = sdhci_set_clock,
  131. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  132. .set_bus_width = sdhci_sirf_set_bus_width,
  133. .reset = sdhci_reset,
  134. .set_uhs_signaling = sdhci_set_uhs_signaling,
  135. };
  136. static struct sdhci_pltfm_data sdhci_sirf_pdata = {
  137. .ops = &sdhci_sirf_ops,
  138. .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  139. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  140. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
  141. SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS,
  142. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
  143. };
  144. static int sdhci_sirf_probe(struct platform_device *pdev)
  145. {
  146. struct sdhci_host *host;
  147. struct sdhci_pltfm_host *pltfm_host;
  148. struct sdhci_sirf_priv *priv;
  149. struct clk *clk;
  150. int gpio_cd;
  151. int ret;
  152. clk = devm_clk_get(&pdev->dev, NULL);
  153. if (IS_ERR(clk)) {
  154. dev_err(&pdev->dev, "unable to get clock");
  155. return PTR_ERR(clk);
  156. }
  157. if (pdev->dev.of_node)
  158. gpio_cd = of_get_named_gpio(pdev->dev.of_node, "cd-gpios", 0);
  159. else
  160. gpio_cd = -EINVAL;
  161. host = sdhci_pltfm_init(pdev, &sdhci_sirf_pdata, sizeof(struct sdhci_sirf_priv));
  162. if (IS_ERR(host))
  163. return PTR_ERR(host);
  164. pltfm_host = sdhci_priv(host);
  165. pltfm_host->clk = clk;
  166. priv = sdhci_pltfm_priv(pltfm_host);
  167. priv->gpio_cd = gpio_cd;
  168. sdhci_get_of_property(pdev);
  169. ret = clk_prepare_enable(pltfm_host->clk);
  170. if (ret)
  171. goto err_clk_prepare;
  172. ret = sdhci_add_host(host);
  173. if (ret)
  174. goto err_sdhci_add;
  175. /*
  176. * We must request the IRQ after sdhci_add_host(), as the tasklet only
  177. * gets setup in sdhci_add_host() and we oops.
  178. */
  179. if (gpio_is_valid(priv->gpio_cd)) {
  180. ret = mmc_gpio_request_cd(host->mmc, priv->gpio_cd, 0);
  181. if (ret) {
  182. dev_err(&pdev->dev, "card detect irq request failed: %d\n",
  183. ret);
  184. goto err_request_cd;
  185. }
  186. mmc_gpiod_request_cd_irq(host->mmc);
  187. }
  188. return 0;
  189. err_request_cd:
  190. sdhci_remove_host(host, 0);
  191. err_sdhci_add:
  192. clk_disable_unprepare(pltfm_host->clk);
  193. err_clk_prepare:
  194. sdhci_pltfm_free(pdev);
  195. return ret;
  196. }
  197. #ifdef CONFIG_PM_SLEEP
  198. static int sdhci_sirf_suspend(struct device *dev)
  199. {
  200. struct sdhci_host *host = dev_get_drvdata(dev);
  201. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  202. int ret;
  203. ret = sdhci_suspend_host(host);
  204. if (ret)
  205. return ret;
  206. clk_disable(pltfm_host->clk);
  207. return 0;
  208. }
  209. static int sdhci_sirf_resume(struct device *dev)
  210. {
  211. struct sdhci_host *host = dev_get_drvdata(dev);
  212. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  213. int ret;
  214. ret = clk_enable(pltfm_host->clk);
  215. if (ret) {
  216. dev_dbg(dev, "Resume: Error enabling clock\n");
  217. return ret;
  218. }
  219. return sdhci_resume_host(host);
  220. }
  221. static SIMPLE_DEV_PM_OPS(sdhci_sirf_pm_ops, sdhci_sirf_suspend, sdhci_sirf_resume);
  222. #endif
  223. static const struct of_device_id sdhci_sirf_of_match[] = {
  224. { .compatible = "sirf,prima2-sdhc" },
  225. { }
  226. };
  227. MODULE_DEVICE_TABLE(of, sdhci_sirf_of_match);
  228. static struct platform_driver sdhci_sirf_driver = {
  229. .driver = {
  230. .name = "sdhci-sirf",
  231. .of_match_table = sdhci_sirf_of_match,
  232. #ifdef CONFIG_PM_SLEEP
  233. .pm = &sdhci_sirf_pm_ops,
  234. #endif
  235. },
  236. .probe = sdhci_sirf_probe,
  237. .remove = sdhci_pltfm_unregister,
  238. };
  239. module_platform_driver(sdhci_sirf_driver);
  240. MODULE_DESCRIPTION("SDHCI driver for SiRFprimaII/SiRFmarco");
  241. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  242. MODULE_LICENSE("GPL v2");