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