phy-qcom-apq8064-sata.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/io.h>
  6. #include <linux/iopoll.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/time.h>
  11. #include <linux/delay.h>
  12. #include <linux/clk.h>
  13. #include <linux/slab.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/phy/phy.h>
  16. /* PHY registers */
  17. #define UNIPHY_PLL_REFCLK_CFG 0x000
  18. #define UNIPHY_PLL_PWRGEN_CFG 0x014
  19. #define UNIPHY_PLL_GLB_CFG 0x020
  20. #define UNIPHY_PLL_SDM_CFG0 0x038
  21. #define UNIPHY_PLL_SDM_CFG1 0x03C
  22. #define UNIPHY_PLL_SDM_CFG2 0x040
  23. #define UNIPHY_PLL_SDM_CFG3 0x044
  24. #define UNIPHY_PLL_SDM_CFG4 0x048
  25. #define UNIPHY_PLL_SSC_CFG0 0x04C
  26. #define UNIPHY_PLL_SSC_CFG1 0x050
  27. #define UNIPHY_PLL_SSC_CFG2 0x054
  28. #define UNIPHY_PLL_SSC_CFG3 0x058
  29. #define UNIPHY_PLL_LKDET_CFG0 0x05C
  30. #define UNIPHY_PLL_LKDET_CFG1 0x060
  31. #define UNIPHY_PLL_LKDET_CFG2 0x064
  32. #define UNIPHY_PLL_CAL_CFG0 0x06C
  33. #define UNIPHY_PLL_CAL_CFG8 0x08C
  34. #define UNIPHY_PLL_CAL_CFG9 0x090
  35. #define UNIPHY_PLL_CAL_CFG10 0x094
  36. #define UNIPHY_PLL_CAL_CFG11 0x098
  37. #define UNIPHY_PLL_STATUS 0x0C0
  38. #define SATA_PHY_SER_CTRL 0x100
  39. #define SATA_PHY_TX_DRIV_CTRL0 0x104
  40. #define SATA_PHY_TX_DRIV_CTRL1 0x108
  41. #define SATA_PHY_TX_IMCAL0 0x11C
  42. #define SATA_PHY_TX_IMCAL2 0x124
  43. #define SATA_PHY_RX_IMCAL0 0x128
  44. #define SATA_PHY_EQUAL 0x13C
  45. #define SATA_PHY_OOB_TERM 0x144
  46. #define SATA_PHY_CDR_CTRL0 0x148
  47. #define SATA_PHY_CDR_CTRL1 0x14C
  48. #define SATA_PHY_CDR_CTRL2 0x150
  49. #define SATA_PHY_CDR_CTRL3 0x154
  50. #define SATA_PHY_PI_CTRL0 0x168
  51. #define SATA_PHY_POW_DWN_CTRL0 0x180
  52. #define SATA_PHY_POW_DWN_CTRL1 0x184
  53. #define SATA_PHY_TX_DATA_CTRL 0x188
  54. #define SATA_PHY_ALIGNP 0x1A4
  55. #define SATA_PHY_TX_IMCAL_STAT 0x1E4
  56. #define SATA_PHY_RX_IMCAL_STAT 0x1E8
  57. #define UNIPHY_PLL_LOCK BIT(0)
  58. #define SATA_PHY_TX_CAL BIT(0)
  59. #define SATA_PHY_RX_CAL BIT(0)
  60. /* default timeout set to 1 sec */
  61. #define TIMEOUT_MS 10000
  62. #define DELAY_INTERVAL_US 100
  63. struct qcom_apq8064_sata_phy {
  64. void __iomem *mmio;
  65. struct clk *cfg_clk;
  66. struct device *dev;
  67. };
  68. /* Helper function to do poll and timeout */
  69. static int poll_timeout(void __iomem *addr, u32 mask)
  70. {
  71. u32 val;
  72. return readl_relaxed_poll_timeout(addr, val, (val & mask),
  73. DELAY_INTERVAL_US, TIMEOUT_MS * 1000);
  74. }
  75. static int qcom_apq8064_sata_phy_init(struct phy *generic_phy)
  76. {
  77. struct qcom_apq8064_sata_phy *phy = phy_get_drvdata(generic_phy);
  78. void __iomem *base = phy->mmio;
  79. int ret = 0;
  80. /* SATA phy initialization */
  81. writel_relaxed(0x01, base + SATA_PHY_SER_CTRL);
  82. writel_relaxed(0xB1, base + SATA_PHY_POW_DWN_CTRL0);
  83. /* Make sure the power down happens before power up */
  84. mb();
  85. usleep_range(10, 60);
  86. writel_relaxed(0x01, base + SATA_PHY_POW_DWN_CTRL0);
  87. writel_relaxed(0x3E, base + SATA_PHY_POW_DWN_CTRL1);
  88. writel_relaxed(0x01, base + SATA_PHY_RX_IMCAL0);
  89. writel_relaxed(0x01, base + SATA_PHY_TX_IMCAL0);
  90. writel_relaxed(0x02, base + SATA_PHY_TX_IMCAL2);
  91. /* Write UNIPHYPLL registers to configure PLL */
  92. writel_relaxed(0x04, base + UNIPHY_PLL_REFCLK_CFG);
  93. writel_relaxed(0x00, base + UNIPHY_PLL_PWRGEN_CFG);
  94. writel_relaxed(0x0A, base + UNIPHY_PLL_CAL_CFG0);
  95. writel_relaxed(0xF3, base + UNIPHY_PLL_CAL_CFG8);
  96. writel_relaxed(0x01, base + UNIPHY_PLL_CAL_CFG9);
  97. writel_relaxed(0xED, base + UNIPHY_PLL_CAL_CFG10);
  98. writel_relaxed(0x02, base + UNIPHY_PLL_CAL_CFG11);
  99. writel_relaxed(0x36, base + UNIPHY_PLL_SDM_CFG0);
  100. writel_relaxed(0x0D, base + UNIPHY_PLL_SDM_CFG1);
  101. writel_relaxed(0xA3, base + UNIPHY_PLL_SDM_CFG2);
  102. writel_relaxed(0xF0, base + UNIPHY_PLL_SDM_CFG3);
  103. writel_relaxed(0x00, base + UNIPHY_PLL_SDM_CFG4);
  104. writel_relaxed(0x19, base + UNIPHY_PLL_SSC_CFG0);
  105. writel_relaxed(0xE1, base + UNIPHY_PLL_SSC_CFG1);
  106. writel_relaxed(0x00, base + UNIPHY_PLL_SSC_CFG2);
  107. writel_relaxed(0x11, base + UNIPHY_PLL_SSC_CFG3);
  108. writel_relaxed(0x04, base + UNIPHY_PLL_LKDET_CFG0);
  109. writel_relaxed(0xFF, base + UNIPHY_PLL_LKDET_CFG1);
  110. writel_relaxed(0x02, base + UNIPHY_PLL_GLB_CFG);
  111. /* make sure global config LDO power down happens before power up */
  112. mb();
  113. writel_relaxed(0x03, base + UNIPHY_PLL_GLB_CFG);
  114. writel_relaxed(0x05, base + UNIPHY_PLL_LKDET_CFG2);
  115. /* PLL Lock wait */
  116. ret = poll_timeout(base + UNIPHY_PLL_STATUS, UNIPHY_PLL_LOCK);
  117. if (ret) {
  118. dev_err(phy->dev, "poll timeout UNIPHY_PLL_STATUS\n");
  119. return ret;
  120. }
  121. /* TX Calibration */
  122. ret = poll_timeout(base + SATA_PHY_TX_IMCAL_STAT, SATA_PHY_TX_CAL);
  123. if (ret) {
  124. dev_err(phy->dev, "poll timeout SATA_PHY_TX_IMCAL_STAT\n");
  125. return ret;
  126. }
  127. /* RX Calibration */
  128. ret = poll_timeout(base + SATA_PHY_RX_IMCAL_STAT, SATA_PHY_RX_CAL);
  129. if (ret) {
  130. dev_err(phy->dev, "poll timeout SATA_PHY_RX_IMCAL_STAT\n");
  131. return ret;
  132. }
  133. /* SATA phy calibrated succesfully, power up to functional mode */
  134. writel_relaxed(0x3E, base + SATA_PHY_POW_DWN_CTRL1);
  135. writel_relaxed(0x01, base + SATA_PHY_RX_IMCAL0);
  136. writel_relaxed(0x01, base + SATA_PHY_TX_IMCAL0);
  137. writel_relaxed(0x00, base + SATA_PHY_POW_DWN_CTRL1);
  138. writel_relaxed(0x59, base + SATA_PHY_CDR_CTRL0);
  139. writel_relaxed(0x04, base + SATA_PHY_CDR_CTRL1);
  140. writel_relaxed(0x00, base + SATA_PHY_CDR_CTRL2);
  141. writel_relaxed(0x00, base + SATA_PHY_PI_CTRL0);
  142. writel_relaxed(0x00, base + SATA_PHY_CDR_CTRL3);
  143. writel_relaxed(0x01, base + SATA_PHY_POW_DWN_CTRL0);
  144. writel_relaxed(0x11, base + SATA_PHY_TX_DATA_CTRL);
  145. writel_relaxed(0x43, base + SATA_PHY_ALIGNP);
  146. writel_relaxed(0x04, base + SATA_PHY_OOB_TERM);
  147. writel_relaxed(0x01, base + SATA_PHY_EQUAL);
  148. writel_relaxed(0x09, base + SATA_PHY_TX_DRIV_CTRL0);
  149. writel_relaxed(0x09, base + SATA_PHY_TX_DRIV_CTRL1);
  150. return 0;
  151. }
  152. static int qcom_apq8064_sata_phy_exit(struct phy *generic_phy)
  153. {
  154. struct qcom_apq8064_sata_phy *phy = phy_get_drvdata(generic_phy);
  155. void __iomem *base = phy->mmio;
  156. /* Power down PHY */
  157. writel_relaxed(0xF8, base + SATA_PHY_POW_DWN_CTRL0);
  158. writel_relaxed(0xFE, base + SATA_PHY_POW_DWN_CTRL1);
  159. /* Power down PLL block */
  160. writel_relaxed(0x00, base + UNIPHY_PLL_GLB_CFG);
  161. return 0;
  162. }
  163. static const struct phy_ops qcom_apq8064_sata_phy_ops = {
  164. .init = qcom_apq8064_sata_phy_init,
  165. .exit = qcom_apq8064_sata_phy_exit,
  166. .owner = THIS_MODULE,
  167. };
  168. static int qcom_apq8064_sata_phy_probe(struct platform_device *pdev)
  169. {
  170. struct qcom_apq8064_sata_phy *phy;
  171. struct device *dev = &pdev->dev;
  172. struct phy_provider *phy_provider;
  173. struct phy *generic_phy;
  174. int ret;
  175. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  176. if (!phy)
  177. return -ENOMEM;
  178. phy->mmio = devm_platform_ioremap_resource(pdev, 0);
  179. if (IS_ERR(phy->mmio))
  180. return PTR_ERR(phy->mmio);
  181. generic_phy = devm_phy_create(dev, NULL, &qcom_apq8064_sata_phy_ops);
  182. if (IS_ERR(generic_phy)) {
  183. dev_err(dev, "%s: failed to create phy\n", __func__);
  184. return PTR_ERR(generic_phy);
  185. }
  186. phy->dev = dev;
  187. phy_set_drvdata(generic_phy, phy);
  188. platform_set_drvdata(pdev, phy);
  189. phy->cfg_clk = devm_clk_get(dev, "cfg");
  190. if (IS_ERR(phy->cfg_clk)) {
  191. dev_err(dev, "Failed to get sata cfg clock\n");
  192. return PTR_ERR(phy->cfg_clk);
  193. }
  194. ret = clk_prepare_enable(phy->cfg_clk);
  195. if (ret)
  196. return ret;
  197. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  198. if (IS_ERR(phy_provider)) {
  199. clk_disable_unprepare(phy->cfg_clk);
  200. dev_err(dev, "%s: failed to register phy\n", __func__);
  201. return PTR_ERR(phy_provider);
  202. }
  203. return 0;
  204. }
  205. static int qcom_apq8064_sata_phy_remove(struct platform_device *pdev)
  206. {
  207. struct qcom_apq8064_sata_phy *phy = platform_get_drvdata(pdev);
  208. clk_disable_unprepare(phy->cfg_clk);
  209. return 0;
  210. }
  211. static const struct of_device_id qcom_apq8064_sata_phy_of_match[] = {
  212. { .compatible = "qcom,apq8064-sata-phy" },
  213. { },
  214. };
  215. MODULE_DEVICE_TABLE(of, qcom_apq8064_sata_phy_of_match);
  216. static struct platform_driver qcom_apq8064_sata_phy_driver = {
  217. .probe = qcom_apq8064_sata_phy_probe,
  218. .remove = qcom_apq8064_sata_phy_remove,
  219. .driver = {
  220. .name = "qcom-apq8064-sata-phy",
  221. .of_match_table = qcom_apq8064_sata_phy_of_match,
  222. }
  223. };
  224. module_platform_driver(qcom_apq8064_sata_phy_driver);
  225. MODULE_DESCRIPTION("QCOM apq8064 SATA PHY driver");
  226. MODULE_LICENSE("GPL v2");