phy-intel-lgm-emmc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel eMMC PHY driver
  4. * Copyright (C) 2019 Intel, Corp.
  5. */
  6. #include <linux/bits.h>
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/phy/phy.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regmap.h>
  16. /* eMMC phy register definitions */
  17. #define EMMC_PHYCTRL0_REG 0xa8
  18. #define DR_TY_MASK GENMASK(30, 28)
  19. #define DR_TY_SHIFT(x) (((x) << 28) & DR_TY_MASK)
  20. #define OTAPDLYENA BIT(14)
  21. #define OTAPDLYSEL_MASK GENMASK(13, 10)
  22. #define OTAPDLYSEL_SHIFT(x) (((x) << 10) & OTAPDLYSEL_MASK)
  23. #define EMMC_PHYCTRL1_REG 0xac
  24. #define PDB_MASK BIT(0)
  25. #define PDB_SHIFT(x) (((x) << 0) & PDB_MASK)
  26. #define ENDLL_MASK BIT(7)
  27. #define ENDLL_SHIFT(x) (((x) << 7) & ENDLL_MASK)
  28. #define EMMC_PHYCTRL2_REG 0xb0
  29. #define FRQSEL_25M 0
  30. #define FRQSEL_50M 1
  31. #define FRQSEL_100M 2
  32. #define FRQSEL_150M 3
  33. #define FRQSEL_MASK GENMASK(24, 22)
  34. #define FRQSEL_SHIFT(x) (((x) << 22) & FRQSEL_MASK)
  35. #define EMMC_PHYSTAT_REG 0xbc
  36. #define CALDONE_MASK BIT(9)
  37. #define DLLRDY_MASK BIT(8)
  38. #define IS_CALDONE(x) ((x) & CALDONE_MASK)
  39. #define IS_DLLRDY(x) ((x) & DLLRDY_MASK)
  40. struct intel_emmc_phy {
  41. struct regmap *syscfg;
  42. struct clk *emmcclk;
  43. };
  44. static int intel_emmc_phy_power(struct phy *phy, bool on_off)
  45. {
  46. struct intel_emmc_phy *priv = phy_get_drvdata(phy);
  47. unsigned int caldone;
  48. unsigned int dllrdy;
  49. unsigned int freqsel;
  50. unsigned long rate;
  51. int ret, quot;
  52. /*
  53. * Keep phyctrl_pdb and phyctrl_endll low to allow
  54. * initialization of CALIO state M/C DFFs
  55. */
  56. ret = regmap_update_bits(priv->syscfg, EMMC_PHYCTRL1_REG, PDB_MASK,
  57. PDB_SHIFT(0));
  58. if (ret) {
  59. dev_err(&phy->dev, "CALIO power down bar failed: %d\n", ret);
  60. return ret;
  61. }
  62. /* Already finish power_off above */
  63. if (!on_off)
  64. return 0;
  65. rate = clk_get_rate(priv->emmcclk);
  66. quot = DIV_ROUND_CLOSEST(rate, 50000000);
  67. if (quot > FRQSEL_150M)
  68. dev_warn(&phy->dev, "Unsupported rate: %lu\n", rate);
  69. freqsel = clamp_t(int, quot, FRQSEL_25M, FRQSEL_150M);
  70. /*
  71. * According to the user manual, calpad calibration
  72. * cycle takes more than 2us without the minimal recommended
  73. * value, so we may need a little margin here
  74. */
  75. udelay(5);
  76. ret = regmap_update_bits(priv->syscfg, EMMC_PHYCTRL1_REG, PDB_MASK,
  77. PDB_SHIFT(1));
  78. if (ret) {
  79. dev_err(&phy->dev, "CALIO power down bar failed: %d\n", ret);
  80. return ret;
  81. }
  82. /*
  83. * According to the user manual, it asks driver to wait 5us for
  84. * calpad busy trimming. However it is documented that this value is
  85. * PVT(A.K.A process,voltage and temperature) relevant, so some
  86. * failure cases are found which indicates we should be more tolerant
  87. * to calpad busy trimming.
  88. */
  89. ret = regmap_read_poll_timeout(priv->syscfg, EMMC_PHYSTAT_REG,
  90. caldone, IS_CALDONE(caldone),
  91. 0, 50);
  92. if (ret) {
  93. dev_err(&phy->dev, "caldone failed, ret=%d\n", ret);
  94. return ret;
  95. }
  96. /* Set the frequency of the DLL operation */
  97. ret = regmap_update_bits(priv->syscfg, EMMC_PHYCTRL2_REG, FRQSEL_MASK,
  98. FRQSEL_SHIFT(freqsel));
  99. if (ret) {
  100. dev_err(&phy->dev, "set the frequency of dll failed:%d\n", ret);
  101. return ret;
  102. }
  103. /* Turn on the DLL */
  104. ret = regmap_update_bits(priv->syscfg, EMMC_PHYCTRL1_REG, ENDLL_MASK,
  105. ENDLL_SHIFT(1));
  106. if (ret) {
  107. dev_err(&phy->dev, "turn on the dll failed: %d\n", ret);
  108. return ret;
  109. }
  110. /*
  111. * After enabling analog DLL circuits docs say that we need 10.2 us if
  112. * our source clock is at 50 MHz and that lock time scales linearly
  113. * with clock speed. If we are powering on the PHY and the card clock
  114. * is super slow (like 100 kHZ) this could take as long as 5.1 ms as
  115. * per the math: 10.2 us * (50000000 Hz / 100000 Hz) => 5.1 ms
  116. * Hopefully we won't be running at 100 kHz, but we should still make
  117. * sure we wait long enough.
  118. *
  119. * NOTE: There appear to be corner cases where the DLL seems to take
  120. * extra long to lock for reasons that aren't understood. In some
  121. * extreme cases we've seen it take up to over 10ms (!). We'll be
  122. * generous and give it 50ms.
  123. */
  124. ret = regmap_read_poll_timeout(priv->syscfg,
  125. EMMC_PHYSTAT_REG,
  126. dllrdy, IS_DLLRDY(dllrdy),
  127. 0, 50 * USEC_PER_MSEC);
  128. if (ret) {
  129. dev_err(&phy->dev, "dllrdy failed. ret=%d\n", ret);
  130. return ret;
  131. }
  132. return 0;
  133. }
  134. static int intel_emmc_phy_init(struct phy *phy)
  135. {
  136. struct intel_emmc_phy *priv = phy_get_drvdata(phy);
  137. /*
  138. * We purposely get the clock here and not in probe to avoid the
  139. * circular dependency problem. We expect:
  140. * - PHY driver to probe
  141. * - SDHCI driver to start probe
  142. * - SDHCI driver to register it's clock
  143. * - SDHCI driver to get the PHY
  144. * - SDHCI driver to init the PHY
  145. *
  146. * The clock is optional, so upon any error just return it like
  147. * any other error to user.
  148. *
  149. */
  150. priv->emmcclk = clk_get_optional(&phy->dev, "emmcclk");
  151. if (IS_ERR(priv->emmcclk)) {
  152. dev_err(&phy->dev, "ERROR: getting emmcclk\n");
  153. return PTR_ERR(priv->emmcclk);
  154. }
  155. return 0;
  156. }
  157. static int intel_emmc_phy_exit(struct phy *phy)
  158. {
  159. struct intel_emmc_phy *priv = phy_get_drvdata(phy);
  160. clk_put(priv->emmcclk);
  161. return 0;
  162. }
  163. static int intel_emmc_phy_power_on(struct phy *phy)
  164. {
  165. struct intel_emmc_phy *priv = phy_get_drvdata(phy);
  166. int ret;
  167. /* Drive impedance: 50 Ohm */
  168. ret = regmap_update_bits(priv->syscfg, EMMC_PHYCTRL0_REG, DR_TY_MASK,
  169. DR_TY_SHIFT(6));
  170. if (ret) {
  171. dev_err(&phy->dev, "ERROR set drive-impednce-50ohm: %d\n", ret);
  172. return ret;
  173. }
  174. /* Output tap delay: disable */
  175. ret = regmap_update_bits(priv->syscfg, EMMC_PHYCTRL0_REG, OTAPDLYENA,
  176. 0);
  177. if (ret) {
  178. dev_err(&phy->dev, "ERROR Set output tap delay : %d\n", ret);
  179. return ret;
  180. }
  181. /* Output tap delay */
  182. ret = regmap_update_bits(priv->syscfg, EMMC_PHYCTRL0_REG,
  183. OTAPDLYSEL_MASK, OTAPDLYSEL_SHIFT(4));
  184. if (ret) {
  185. dev_err(&phy->dev, "ERROR: output tap dly select: %d\n", ret);
  186. return ret;
  187. }
  188. /* Power up eMMC phy analog blocks */
  189. return intel_emmc_phy_power(phy, true);
  190. }
  191. static int intel_emmc_phy_power_off(struct phy *phy)
  192. {
  193. /* Power down eMMC phy analog blocks */
  194. return intel_emmc_phy_power(phy, false);
  195. }
  196. static const struct phy_ops ops = {
  197. .init = intel_emmc_phy_init,
  198. .exit = intel_emmc_phy_exit,
  199. .power_on = intel_emmc_phy_power_on,
  200. .power_off = intel_emmc_phy_power_off,
  201. .owner = THIS_MODULE,
  202. };
  203. static int intel_emmc_phy_probe(struct platform_device *pdev)
  204. {
  205. struct device *dev = &pdev->dev;
  206. struct device_node *np = dev->of_node;
  207. struct intel_emmc_phy *priv;
  208. struct phy *generic_phy;
  209. struct phy_provider *phy_provider;
  210. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  211. if (!priv)
  212. return -ENOMEM;
  213. /* Get eMMC phy (accessed via chiptop) regmap */
  214. priv->syscfg = syscon_regmap_lookup_by_phandle(np, "intel,syscon");
  215. if (IS_ERR(priv->syscfg)) {
  216. dev_err(dev, "failed to find syscon\n");
  217. return PTR_ERR(priv->syscfg);
  218. }
  219. generic_phy = devm_phy_create(dev, np, &ops);
  220. if (IS_ERR(generic_phy)) {
  221. dev_err(dev, "failed to create PHY\n");
  222. return PTR_ERR(generic_phy);
  223. }
  224. phy_set_drvdata(generic_phy, priv);
  225. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  226. return PTR_ERR_OR_ZERO(phy_provider);
  227. }
  228. static const struct of_device_id intel_emmc_phy_dt_ids[] = {
  229. { .compatible = "intel,lgm-emmc-phy" },
  230. {}
  231. };
  232. MODULE_DEVICE_TABLE(of, intel_emmc_phy_dt_ids);
  233. static struct platform_driver intel_emmc_driver = {
  234. .probe = intel_emmc_phy_probe,
  235. .driver = {
  236. .name = "intel-emmc-phy",
  237. .of_match_table = intel_emmc_phy_dt_ids,
  238. },
  239. };
  240. module_platform_driver(intel_emmc_driver);
  241. MODULE_AUTHOR("Peter Harliman Liem <[email protected]>");
  242. MODULE_DESCRIPTION("Intel eMMC PHY driver");
  243. MODULE_LICENSE("GPL v2");