dw_mmc-zx.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * ZX Specific Extensions for Synopsys DW Multimedia Card Interface driver
  3. *
  4. * Copyright (C) 2016, Linaro Ltd.
  5. * Copyright (C) 2016, ZTE Corp.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/mmc/host.h>
  15. #include <linux/mmc/mmc.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #include "dw_mmc.h"
  23. #include "dw_mmc-pltfm.h"
  24. #include "dw_mmc-zx.h"
  25. struct dw_mci_zx_priv_data {
  26. struct regmap *sysc_base;
  27. };
  28. enum delay_type {
  29. DELAY_TYPE_READ, /* read dqs delay */
  30. DELAY_TYPE_CLK, /* clk sample delay */
  31. };
  32. static int dw_mci_zx_emmc_set_delay(struct dw_mci *host, unsigned int delay,
  33. enum delay_type dflag)
  34. {
  35. struct dw_mci_zx_priv_data *priv = host->priv;
  36. struct regmap *sysc_base = priv->sysc_base;
  37. unsigned int clksel;
  38. unsigned int loop = 1000;
  39. int ret;
  40. if (!sysc_base)
  41. return -EINVAL;
  42. ret = regmap_update_bits(sysc_base, LB_AON_EMMC_CFG_REG0,
  43. PARA_HALF_CLK_MODE | PARA_DLL_BYPASS_MODE |
  44. PARA_PHASE_DET_SEL_MASK |
  45. PARA_DLL_LOCK_NUM_MASK |
  46. DLL_REG_SET | PARA_DLL_START_MASK,
  47. PARA_DLL_START(4) | PARA_DLL_LOCK_NUM(4));
  48. if (ret)
  49. return ret;
  50. ret = regmap_read(sysc_base, LB_AON_EMMC_CFG_REG1, &clksel);
  51. if (ret)
  52. return ret;
  53. if (dflag == DELAY_TYPE_CLK) {
  54. clksel &= ~CLK_SAMP_DELAY_MASK;
  55. clksel |= CLK_SAMP_DELAY(delay);
  56. } else {
  57. clksel &= ~READ_DQS_DELAY_MASK;
  58. clksel |= READ_DQS_DELAY(delay);
  59. }
  60. regmap_write(sysc_base, LB_AON_EMMC_CFG_REG1, clksel);
  61. regmap_update_bits(sysc_base, LB_AON_EMMC_CFG_REG0,
  62. PARA_DLL_START_MASK | PARA_DLL_LOCK_NUM_MASK |
  63. DLL_REG_SET,
  64. PARA_DLL_START(4) | PARA_DLL_LOCK_NUM(4) |
  65. DLL_REG_SET);
  66. do {
  67. ret = regmap_read(sysc_base, LB_AON_EMMC_CFG_REG2, &clksel);
  68. if (ret)
  69. return ret;
  70. } while (--loop && !(clksel & ZX_DLL_LOCKED));
  71. if (!loop) {
  72. dev_err(host->dev, "Error: %s dll lock fail\n", __func__);
  73. return -EIO;
  74. }
  75. return 0;
  76. }
  77. static int dw_mci_zx_emmc_execute_tuning(struct dw_mci_slot *slot, u32 opcode)
  78. {
  79. struct dw_mci *host = slot->host;
  80. struct mmc_host *mmc = slot->mmc;
  81. int ret, len = 0, start = 0, end = 0, delay, best = 0;
  82. for (delay = 1; delay < 128; delay++) {
  83. ret = dw_mci_zx_emmc_set_delay(host, delay, DELAY_TYPE_CLK);
  84. if (!ret && mmc_send_tuning(mmc, opcode, NULL)) {
  85. if (start >= 0) {
  86. end = delay - 1;
  87. /* check and update longest good range */
  88. if ((end - start) > len) {
  89. best = (start + end) >> 1;
  90. len = end - start;
  91. }
  92. }
  93. start = -1;
  94. end = 0;
  95. continue;
  96. }
  97. if (start < 0)
  98. start = delay;
  99. }
  100. if (start >= 0) {
  101. end = delay - 1;
  102. if ((end - start) > len) {
  103. best = (start + end) >> 1;
  104. len = end - start;
  105. }
  106. }
  107. if (best < 0)
  108. return -EIO;
  109. dev_info(host->dev, "%s best range: start %d end %d\n", __func__,
  110. start, end);
  111. return dw_mci_zx_emmc_set_delay(host, best, DELAY_TYPE_CLK);
  112. }
  113. static int dw_mci_zx_prepare_hs400_tuning(struct dw_mci *host,
  114. struct mmc_ios *ios)
  115. {
  116. int ret;
  117. /* config phase shift as 90 degree */
  118. ret = dw_mci_zx_emmc_set_delay(host, 32, DELAY_TYPE_READ);
  119. if (ret < 0)
  120. return -EIO;
  121. return 0;
  122. }
  123. static int dw_mci_zx_execute_tuning(struct dw_mci_slot *slot, u32 opcode)
  124. {
  125. struct dw_mci *host = slot->host;
  126. if (host->verid == 0x290a) /* only for emmc */
  127. return dw_mci_zx_emmc_execute_tuning(slot, opcode);
  128. /* TODO: Add 0x210a dedicated tuning for sd/sdio */
  129. return 0;
  130. }
  131. static int dw_mci_zx_parse_dt(struct dw_mci *host)
  132. {
  133. struct device_node *np = host->dev->of_node;
  134. struct device_node *node;
  135. struct dw_mci_zx_priv_data *priv;
  136. struct regmap *sysc_base;
  137. int ret;
  138. /* syscon is needed only by emmc */
  139. node = of_parse_phandle(np, "zte,aon-syscon", 0);
  140. if (node) {
  141. sysc_base = syscon_node_to_regmap(node);
  142. of_node_put(node);
  143. if (IS_ERR(sysc_base)) {
  144. ret = PTR_ERR(sysc_base);
  145. if (ret != -EPROBE_DEFER)
  146. dev_err(host->dev, "Can't get syscon: %d\n",
  147. ret);
  148. return ret;
  149. }
  150. } else {
  151. return 0;
  152. }
  153. priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
  154. if (!priv)
  155. return -ENOMEM;
  156. priv->sysc_base = sysc_base;
  157. host->priv = priv;
  158. return 0;
  159. }
  160. static unsigned long zx_dwmmc_caps[3] = {
  161. MMC_CAP_CMD23,
  162. MMC_CAP_CMD23,
  163. MMC_CAP_CMD23,
  164. };
  165. static const struct dw_mci_drv_data zx_drv_data = {
  166. .caps = zx_dwmmc_caps,
  167. .execute_tuning = dw_mci_zx_execute_tuning,
  168. .prepare_hs400_tuning = dw_mci_zx_prepare_hs400_tuning,
  169. .parse_dt = dw_mci_zx_parse_dt,
  170. };
  171. static const struct of_device_id dw_mci_zx_match[] = {
  172. { .compatible = "zte,zx296718-dw-mshc", .data = &zx_drv_data},
  173. {},
  174. };
  175. MODULE_DEVICE_TABLE(of, dw_mci_zx_match);
  176. static int dw_mci_zx_probe(struct platform_device *pdev)
  177. {
  178. const struct dw_mci_drv_data *drv_data;
  179. const struct of_device_id *match;
  180. match = of_match_node(dw_mci_zx_match, pdev->dev.of_node);
  181. drv_data = match->data;
  182. return dw_mci_pltfm_register(pdev, drv_data);
  183. }
  184. static const struct dev_pm_ops dw_mci_zx_dev_pm_ops = {
  185. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  186. pm_runtime_force_resume)
  187. SET_RUNTIME_PM_OPS(dw_mci_runtime_suspend,
  188. dw_mci_runtime_resume,
  189. NULL)
  190. };
  191. static struct platform_driver dw_mci_zx_pltfm_driver = {
  192. .probe = dw_mci_zx_probe,
  193. .remove = dw_mci_pltfm_remove,
  194. .driver = {
  195. .name = "dwmmc_zx",
  196. .of_match_table = dw_mci_zx_match,
  197. .pm = &dw_mci_zx_dev_pm_ops,
  198. },
  199. };
  200. module_platform_driver(dw_mci_zx_pltfm_driver);
  201. MODULE_DESCRIPTION("ZTE emmc/sd driver");
  202. MODULE_LICENSE("GPL v2");