phy-qcom-pcie2.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2019, Linaro Ltd.
  5. */
  6. #include <linux/clk-provider.h>
  7. #include <linux/clk.h>
  8. #include <linux/iopoll.h>
  9. #include <linux/module.h>
  10. #include <linux/phy/phy.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/reset.h>
  13. #include <linux/slab.h>
  14. #include <dt-bindings/phy/phy.h>
  15. #define PCIE20_PARF_PHY_STTS 0x3c
  16. #define PCIE2_PHY_RESET_CTRL 0x44
  17. #define PCIE20_PARF_PHY_REFCLK_CTRL2 0xa0
  18. #define PCIE20_PARF_PHY_REFCLK_CTRL3 0xa4
  19. #define PCIE20_PARF_PCS_SWING_CTRL1 0x88
  20. #define PCIE20_PARF_PCS_SWING_CTRL2 0x8c
  21. #define PCIE20_PARF_PCS_DEEMPH1 0x74
  22. #define PCIE20_PARF_PCS_DEEMPH2 0x78
  23. #define PCIE20_PARF_PCS_DEEMPH3 0x7c
  24. #define PCIE20_PARF_CONFIGBITS 0x84
  25. #define PCIE20_PARF_PHY_CTRL3 0x94
  26. #define PCIE20_PARF_PCS_CTRL 0x80
  27. #define TX_AMP_VAL 120
  28. #define PHY_RX0_EQ_GEN1_VAL 0
  29. #define PHY_RX0_EQ_GEN2_VAL 4
  30. #define TX_DEEMPH_GEN1_VAL 24
  31. #define TX_DEEMPH_GEN2_3_5DB_VAL 26
  32. #define TX_DEEMPH_GEN2_6DB_VAL 36
  33. #define PHY_TX0_TERM_OFFST_VAL 0
  34. struct qcom_phy {
  35. struct device *dev;
  36. void __iomem *base;
  37. struct regulator_bulk_data vregs[2];
  38. struct reset_control *phy_reset;
  39. struct reset_control *pipe_reset;
  40. struct clk *pipe_clk;
  41. };
  42. static int qcom_pcie2_phy_init(struct phy *phy)
  43. {
  44. struct qcom_phy *qphy = phy_get_drvdata(phy);
  45. int ret;
  46. ret = reset_control_deassert(qphy->phy_reset);
  47. if (ret) {
  48. dev_err(qphy->dev, "cannot deassert pipe reset\n");
  49. return ret;
  50. }
  51. ret = regulator_bulk_enable(ARRAY_SIZE(qphy->vregs), qphy->vregs);
  52. if (ret)
  53. reset_control_assert(qphy->phy_reset);
  54. return ret;
  55. }
  56. static int qcom_pcie2_phy_power_on(struct phy *phy)
  57. {
  58. struct qcom_phy *qphy = phy_get_drvdata(phy);
  59. int ret;
  60. u32 val;
  61. /* Program REF_CLK source */
  62. val = readl(qphy->base + PCIE20_PARF_PHY_REFCLK_CTRL2);
  63. val &= ~BIT(1);
  64. writel(val, qphy->base + PCIE20_PARF_PHY_REFCLK_CTRL2);
  65. usleep_range(1000, 2000);
  66. /* Don't use PAD for refclock */
  67. val = readl(qphy->base + PCIE20_PARF_PHY_REFCLK_CTRL2);
  68. val &= ~BIT(0);
  69. writel(val, qphy->base + PCIE20_PARF_PHY_REFCLK_CTRL2);
  70. /* Program SSP ENABLE */
  71. val = readl(qphy->base + PCIE20_PARF_PHY_REFCLK_CTRL3);
  72. val |= BIT(0);
  73. writel(val, qphy->base + PCIE20_PARF_PHY_REFCLK_CTRL3);
  74. usleep_range(1000, 2000);
  75. /* Assert Phy SW Reset */
  76. val = readl(qphy->base + PCIE2_PHY_RESET_CTRL);
  77. val |= BIT(0);
  78. writel(val, qphy->base + PCIE2_PHY_RESET_CTRL);
  79. /* Program Tx Amplitude */
  80. val = readl(qphy->base + PCIE20_PARF_PCS_SWING_CTRL1);
  81. val &= ~0x7f;
  82. val |= TX_AMP_VAL;
  83. writel(val, qphy->base + PCIE20_PARF_PCS_SWING_CTRL1);
  84. val = readl(qphy->base + PCIE20_PARF_PCS_SWING_CTRL2);
  85. val &= ~0x7f;
  86. val |= TX_AMP_VAL;
  87. writel(val, qphy->base + PCIE20_PARF_PCS_SWING_CTRL2);
  88. /* Program De-Emphasis */
  89. val = readl(qphy->base + PCIE20_PARF_PCS_DEEMPH1);
  90. val &= ~0x3f;
  91. val |= TX_DEEMPH_GEN2_6DB_VAL;
  92. writel(val, qphy->base + PCIE20_PARF_PCS_DEEMPH1);
  93. val = readl(qphy->base + PCIE20_PARF_PCS_DEEMPH2);
  94. val &= ~0x3f;
  95. val |= TX_DEEMPH_GEN2_3_5DB_VAL;
  96. writel(val, qphy->base + PCIE20_PARF_PCS_DEEMPH2);
  97. val = readl(qphy->base + PCIE20_PARF_PCS_DEEMPH3);
  98. val &= ~0x3f;
  99. val |= TX_DEEMPH_GEN1_VAL;
  100. writel(val, qphy->base + PCIE20_PARF_PCS_DEEMPH3);
  101. /* Program Rx_Eq */
  102. val = readl(qphy->base + PCIE20_PARF_CONFIGBITS);
  103. val &= ~0x7;
  104. val |= PHY_RX0_EQ_GEN2_VAL;
  105. writel(val, qphy->base + PCIE20_PARF_CONFIGBITS);
  106. /* Program Tx0_term_offset */
  107. val = readl(qphy->base + PCIE20_PARF_PHY_CTRL3);
  108. val &= ~0x1f;
  109. val |= PHY_TX0_TERM_OFFST_VAL;
  110. writel(val, qphy->base + PCIE20_PARF_PHY_CTRL3);
  111. /* disable Tx2Rx Loopback */
  112. val = readl(qphy->base + PCIE20_PARF_PCS_CTRL);
  113. val &= ~BIT(1);
  114. writel(val, qphy->base + PCIE20_PARF_PCS_CTRL);
  115. /* De-assert Phy SW Reset */
  116. val = readl(qphy->base + PCIE2_PHY_RESET_CTRL);
  117. val &= ~BIT(0);
  118. writel(val, qphy->base + PCIE2_PHY_RESET_CTRL);
  119. usleep_range(1000, 2000);
  120. ret = reset_control_deassert(qphy->pipe_reset);
  121. if (ret) {
  122. dev_err(qphy->dev, "cannot deassert pipe reset\n");
  123. goto out;
  124. }
  125. clk_set_rate(qphy->pipe_clk, 250000000);
  126. ret = clk_prepare_enable(qphy->pipe_clk);
  127. if (ret) {
  128. dev_err(qphy->dev, "failed to enable pipe clock\n");
  129. goto out;
  130. }
  131. ret = readl_poll_timeout(qphy->base + PCIE20_PARF_PHY_STTS, val,
  132. !(val & BIT(0)), 1000, 10);
  133. if (ret)
  134. dev_err(qphy->dev, "phy initialization failed\n");
  135. out:
  136. return ret;
  137. }
  138. static int qcom_pcie2_phy_power_off(struct phy *phy)
  139. {
  140. struct qcom_phy *qphy = phy_get_drvdata(phy);
  141. u32 val;
  142. val = readl(qphy->base + PCIE2_PHY_RESET_CTRL);
  143. val |= BIT(0);
  144. writel(val, qphy->base + PCIE2_PHY_RESET_CTRL);
  145. clk_disable_unprepare(qphy->pipe_clk);
  146. reset_control_assert(qphy->pipe_reset);
  147. return 0;
  148. }
  149. static int qcom_pcie2_phy_exit(struct phy *phy)
  150. {
  151. struct qcom_phy *qphy = phy_get_drvdata(phy);
  152. regulator_bulk_disable(ARRAY_SIZE(qphy->vregs), qphy->vregs);
  153. reset_control_assert(qphy->phy_reset);
  154. return 0;
  155. }
  156. static const struct phy_ops qcom_pcie2_ops = {
  157. .init = qcom_pcie2_phy_init,
  158. .power_on = qcom_pcie2_phy_power_on,
  159. .power_off = qcom_pcie2_phy_power_off,
  160. .exit = qcom_pcie2_phy_exit,
  161. .owner = THIS_MODULE,
  162. };
  163. /*
  164. * Register a fixed rate pipe clock.
  165. *
  166. * The <s>_pipe_clksrc generated by PHY goes to the GCC that gate
  167. * controls it. The <s>_pipe_clk coming out of the GCC is requested
  168. * by the PHY driver for its operations.
  169. * We register the <s>_pipe_clksrc here. The gcc driver takes care
  170. * of assigning this <s>_pipe_clksrc as parent to <s>_pipe_clk.
  171. * Below picture shows this relationship.
  172. *
  173. * +---------------+
  174. * | PHY block |<<---------------------------------------+
  175. * | | |
  176. * | +-------+ | +-----+ |
  177. * I/P---^-->| PLL |---^--->pipe_clksrc--->| GCC |--->pipe_clk---+
  178. * clk | +-------+ | +-----+
  179. * +---------------+
  180. */
  181. static int phy_pipe_clksrc_register(struct qcom_phy *qphy)
  182. {
  183. struct device_node *np = qphy->dev->of_node;
  184. struct clk_fixed_rate *fixed;
  185. struct clk_init_data init = { };
  186. int ret;
  187. ret = of_property_read_string(np, "clock-output-names", &init.name);
  188. if (ret) {
  189. dev_err(qphy->dev, "%s: No clock-output-names\n", np->name);
  190. return ret;
  191. }
  192. fixed = devm_kzalloc(qphy->dev, sizeof(*fixed), GFP_KERNEL);
  193. if (!fixed)
  194. return -ENOMEM;
  195. init.ops = &clk_fixed_rate_ops;
  196. /* controllers using QMP phys use 250MHz pipe clock interface */
  197. fixed->fixed_rate = 250000000;
  198. fixed->hw.init = &init;
  199. return devm_clk_hw_register(qphy->dev, &fixed->hw);
  200. }
  201. static int qcom_pcie2_phy_probe(struct platform_device *pdev)
  202. {
  203. struct phy_provider *phy_provider;
  204. struct qcom_phy *qphy;
  205. struct device *dev = &pdev->dev;
  206. struct phy *phy;
  207. int ret;
  208. qphy = devm_kzalloc(dev, sizeof(*qphy), GFP_KERNEL);
  209. if (!qphy)
  210. return -ENOMEM;
  211. qphy->dev = dev;
  212. qphy->base = devm_platform_ioremap_resource(pdev, 0);
  213. if (IS_ERR(qphy->base))
  214. return PTR_ERR(qphy->base);
  215. ret = phy_pipe_clksrc_register(qphy);
  216. if (ret) {
  217. dev_err(dev, "failed to register pipe_clk\n");
  218. return ret;
  219. }
  220. qphy->vregs[0].supply = "vdda-vp";
  221. qphy->vregs[1].supply = "vdda-vph";
  222. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(qphy->vregs), qphy->vregs);
  223. if (ret < 0)
  224. return ret;
  225. qphy->pipe_clk = devm_clk_get(dev, NULL);
  226. if (IS_ERR(qphy->pipe_clk)) {
  227. dev_err(dev, "failed to acquire pipe clock\n");
  228. return PTR_ERR(qphy->pipe_clk);
  229. }
  230. qphy->phy_reset = devm_reset_control_get_exclusive(dev, "phy");
  231. if (IS_ERR(qphy->phy_reset)) {
  232. dev_err(dev, "failed to acquire phy reset\n");
  233. return PTR_ERR(qphy->phy_reset);
  234. }
  235. qphy->pipe_reset = devm_reset_control_get_exclusive(dev, "pipe");
  236. if (IS_ERR(qphy->pipe_reset)) {
  237. dev_err(dev, "failed to acquire pipe reset\n");
  238. return PTR_ERR(qphy->pipe_reset);
  239. }
  240. phy = devm_phy_create(dev, dev->of_node, &qcom_pcie2_ops);
  241. if (IS_ERR(phy)) {
  242. dev_err(dev, "failed to create phy\n");
  243. return PTR_ERR(phy);
  244. }
  245. phy_set_drvdata(phy, qphy);
  246. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  247. if (IS_ERR(phy_provider))
  248. dev_err(dev, "failed to register phy provider\n");
  249. return PTR_ERR_OR_ZERO(phy_provider);
  250. }
  251. static const struct of_device_id qcom_pcie2_phy_match_table[] = {
  252. { .compatible = "qcom,pcie2-phy" },
  253. {}
  254. };
  255. MODULE_DEVICE_TABLE(of, qcom_pcie2_phy_match_table);
  256. static struct platform_driver qcom_pcie2_phy_driver = {
  257. .probe = qcom_pcie2_phy_probe,
  258. .driver = {
  259. .name = "phy-qcom-pcie2",
  260. .of_match_table = qcom_pcie2_phy_match_table,
  261. },
  262. };
  263. module_platform_driver(qcom_pcie2_phy_driver);
  264. MODULE_DESCRIPTION("Qualcomm PCIe PHY driver");
  265. MODULE_LICENSE("GPL v2");