sdhci-sirf.c 5.6 KB

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