phy-rockchip-snps-pcie3.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Rockchip PCIE3.0 phy driver
  4. *
  5. * Copyright (C) 2022 Rockchip Electronics Co., Ltd.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/module.h>
  14. #include <linux/of_device.h>
  15. #include <linux/phy/pcie.h>
  16. #include <linux/phy/phy.h>
  17. #include <linux/regmap.h>
  18. #include <linux/reset.h>
  19. /* Register for RK3568 */
  20. #define GRF_PCIE30PHY_CON1 0x4
  21. #define GRF_PCIE30PHY_CON6 0x18
  22. #define GRF_PCIE30PHY_CON9 0x24
  23. #define GRF_PCIE30PHY_DA_OCM (BIT(15) | BIT(31))
  24. #define GRF_PCIE30PHY_STATUS0 0x80
  25. #define GRF_PCIE30PHY_WR_EN (0xf << 16)
  26. #define SRAM_INIT_DONE(reg) (reg & BIT(14))
  27. #define RK3568_BIFURCATION_LANE_0_1 BIT(0)
  28. /* Register for RK3588 */
  29. #define PHP_GRF_PCIESEL_CON 0x100
  30. #define RK3588_PCIE3PHY_GRF_CMN_CON0 0x0
  31. #define RK3588_PCIE3PHY_GRF_PHY0_STATUS1 0x904
  32. #define RK3588_PCIE3PHY_GRF_PHY1_STATUS1 0xa04
  33. #define RK3588_SRAM_INIT_DONE(reg) (reg & BIT(0))
  34. #define RK3588_BIFURCATION_LANE_0_1 BIT(0)
  35. #define RK3588_BIFURCATION_LANE_2_3 BIT(1)
  36. #define RK3588_LANE_AGGREGATION BIT(2)
  37. struct rockchip_p3phy_ops;
  38. struct rockchip_p3phy_priv {
  39. const struct rockchip_p3phy_ops *ops;
  40. void __iomem *mmio;
  41. /* mode: RC, EP */
  42. int mode;
  43. /* pcie30_phymode: Aggregation, Bifurcation */
  44. int pcie30_phymode;
  45. struct regmap *phy_grf;
  46. struct regmap *pipe_grf;
  47. struct reset_control *p30phy;
  48. struct phy *phy;
  49. struct clk_bulk_data *clks;
  50. int num_clks;
  51. int num_lanes;
  52. u32 lanes[4];
  53. };
  54. struct rockchip_p3phy_ops {
  55. int (*phy_init)(struct rockchip_p3phy_priv *priv);
  56. };
  57. static int rockchip_p3phy_set_mode(struct phy *phy, enum phy_mode mode, int submode)
  58. {
  59. struct rockchip_p3phy_priv *priv = phy_get_drvdata(phy);
  60. /* Actually We don't care EP/RC mode, but just record it */
  61. switch (submode) {
  62. case PHY_MODE_PCIE_RC:
  63. priv->mode = PHY_MODE_PCIE_RC;
  64. break;
  65. case PHY_MODE_PCIE_EP:
  66. priv->mode = PHY_MODE_PCIE_EP;
  67. break;
  68. default:
  69. dev_err(&phy->dev, "%s, invalid mode\n", __func__);
  70. return -EINVAL;
  71. }
  72. return 0;
  73. }
  74. static int rockchip_p3phy_rk3568_init(struct rockchip_p3phy_priv *priv)
  75. {
  76. struct phy *phy = priv->phy;
  77. bool bifurcation = false;
  78. int ret;
  79. u32 reg;
  80. /* Deassert PCIe PMA output clamp mode */
  81. regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON9, GRF_PCIE30PHY_DA_OCM);
  82. for (int i = 0; i < priv->num_lanes; i++) {
  83. dev_info(&phy->dev, "lane number %d, val %d\n", i, priv->lanes[i]);
  84. if (priv->lanes[i] > 1)
  85. bifurcation = true;
  86. }
  87. /* Set bifurcation if needed, and it doesn't care RC/EP */
  88. if (bifurcation) {
  89. dev_info(&phy->dev, "bifurcation enabled\n");
  90. regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON6,
  91. GRF_PCIE30PHY_WR_EN | RK3568_BIFURCATION_LANE_0_1);
  92. regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON1,
  93. GRF_PCIE30PHY_DA_OCM);
  94. } else {
  95. dev_dbg(&phy->dev, "bifurcation disabled\n");
  96. regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON6,
  97. GRF_PCIE30PHY_WR_EN & ~RK3568_BIFURCATION_LANE_0_1);
  98. }
  99. reset_control_deassert(priv->p30phy);
  100. ret = regmap_read_poll_timeout(priv->phy_grf,
  101. GRF_PCIE30PHY_STATUS0,
  102. reg, SRAM_INIT_DONE(reg),
  103. 0, 500);
  104. if (ret)
  105. dev_err(&priv->phy->dev, "%s: lock failed 0x%x, check input refclk and power supply\n",
  106. __func__, reg);
  107. return ret;
  108. }
  109. static const struct rockchip_p3phy_ops rk3568_ops = {
  110. .phy_init = rockchip_p3phy_rk3568_init,
  111. };
  112. static int rockchip_p3phy_rk3588_init(struct rockchip_p3phy_priv *priv)
  113. {
  114. u32 reg = 0;
  115. u8 mode = 0;
  116. int ret;
  117. /* Deassert PCIe PMA output clamp mode */
  118. regmap_write(priv->phy_grf, RK3588_PCIE3PHY_GRF_CMN_CON0, BIT(8) | BIT(24));
  119. /* Set bifurcation if needed */
  120. for (int i = 0; i < priv->num_lanes; i++) {
  121. if (!priv->lanes[i])
  122. mode |= (BIT(i) << 3);
  123. if (priv->lanes[i] > 1)
  124. mode |= (BIT(i) >> 1);
  125. }
  126. if (!mode)
  127. reg = RK3588_LANE_AGGREGATION;
  128. else {
  129. if (mode & (BIT(0) | BIT(1)))
  130. reg |= RK3588_BIFURCATION_LANE_0_1;
  131. if (mode & (BIT(2) | BIT(3)))
  132. reg |= RK3588_BIFURCATION_LANE_2_3;
  133. }
  134. regmap_write(priv->phy_grf, RK3588_PCIE3PHY_GRF_CMN_CON0, (0x7<<16) | reg);
  135. /* Set pcie1ln_sel in PHP_GRF_PCIESEL_CON */
  136. if (!IS_ERR(priv->pipe_grf)) {
  137. reg = (mode & (BIT(6) | BIT(7))) >> 6;
  138. if (reg)
  139. regmap_write(priv->pipe_grf, PHP_GRF_PCIESEL_CON,
  140. (reg << 16) | reg);
  141. }
  142. reset_control_deassert(priv->p30phy);
  143. ret = regmap_read_poll_timeout(priv->phy_grf,
  144. RK3588_PCIE3PHY_GRF_PHY0_STATUS1,
  145. reg, RK3588_SRAM_INIT_DONE(reg),
  146. 0, 500);
  147. ret |= regmap_read_poll_timeout(priv->phy_grf,
  148. RK3588_PCIE3PHY_GRF_PHY1_STATUS1,
  149. reg, RK3588_SRAM_INIT_DONE(reg),
  150. 0, 500);
  151. if (ret)
  152. dev_err(&priv->phy->dev, "lock failed 0x%x, check input refclk and power supply\n",
  153. reg);
  154. return ret;
  155. }
  156. static const struct rockchip_p3phy_ops rk3588_ops = {
  157. .phy_init = rockchip_p3phy_rk3588_init,
  158. };
  159. static int rochchip_p3phy_init(struct phy *phy)
  160. {
  161. struct rockchip_p3phy_priv *priv = phy_get_drvdata(phy);
  162. int ret;
  163. ret = clk_bulk_prepare_enable(priv->num_clks, priv->clks);
  164. if (ret) {
  165. dev_err(&priv->phy->dev, "failed to enable PCIe bulk clks %d\n", ret);
  166. return ret;
  167. }
  168. reset_control_assert(priv->p30phy);
  169. udelay(1);
  170. if (priv->ops->phy_init) {
  171. ret = priv->ops->phy_init(priv);
  172. if (ret)
  173. clk_bulk_disable_unprepare(priv->num_clks, priv->clks);
  174. }
  175. return ret;
  176. }
  177. static int rochchip_p3phy_exit(struct phy *phy)
  178. {
  179. struct rockchip_p3phy_priv *priv = phy_get_drvdata(phy);
  180. clk_bulk_disable_unprepare(priv->num_clks, priv->clks);
  181. reset_control_assert(priv->p30phy);
  182. return 0;
  183. }
  184. static const struct phy_ops rochchip_p3phy_ops = {
  185. .init = rochchip_p3phy_init,
  186. .exit = rochchip_p3phy_exit,
  187. .set_mode = rockchip_p3phy_set_mode,
  188. .owner = THIS_MODULE,
  189. };
  190. static int rockchip_p3phy_probe(struct platform_device *pdev)
  191. {
  192. struct phy_provider *phy_provider;
  193. struct device *dev = &pdev->dev;
  194. struct rockchip_p3phy_priv *priv;
  195. struct device_node *np = dev->of_node;
  196. int ret;
  197. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  198. if (!priv)
  199. return -ENOMEM;
  200. priv->mmio = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  201. if (IS_ERR(priv->mmio)) {
  202. ret = PTR_ERR(priv->mmio);
  203. return ret;
  204. }
  205. priv->ops = of_device_get_match_data(&pdev->dev);
  206. if (!priv->ops) {
  207. dev_err(dev, "no of match data provided\n");
  208. return -EINVAL;
  209. }
  210. priv->phy_grf = syscon_regmap_lookup_by_phandle(np, "rockchip,phy-grf");
  211. if (IS_ERR(priv->phy_grf)) {
  212. dev_err(dev, "failed to find rockchip,phy_grf regmap\n");
  213. return PTR_ERR(priv->phy_grf);
  214. }
  215. if (of_device_is_compatible(np, "rockchip,rk3588-pcie3-phy")) {
  216. priv->pipe_grf =
  217. syscon_regmap_lookup_by_phandle(dev->of_node,
  218. "rockchip,pipe-grf");
  219. if (IS_ERR(priv->pipe_grf))
  220. dev_info(dev, "failed to find rockchip,pipe_grf regmap\n");
  221. } else {
  222. priv->pipe_grf = NULL;
  223. }
  224. priv->num_lanes = of_property_read_variable_u32_array(dev->of_node, "data-lanes",
  225. priv->lanes, 2,
  226. ARRAY_SIZE(priv->lanes));
  227. /* if no data-lanes assume aggregation */
  228. if (priv->num_lanes == -EINVAL) {
  229. dev_dbg(dev, "no data-lanes property found\n");
  230. priv->num_lanes = 1;
  231. priv->lanes[0] = 1;
  232. } else if (priv->num_lanes < 0) {
  233. dev_err(dev, "failed to read data-lanes property %d\n", priv->num_lanes);
  234. return priv->num_lanes;
  235. }
  236. priv->phy = devm_phy_create(dev, NULL, &rochchip_p3phy_ops);
  237. if (IS_ERR(priv->phy)) {
  238. dev_err(dev, "failed to create combphy\n");
  239. return PTR_ERR(priv->phy);
  240. }
  241. priv->p30phy = devm_reset_control_get_optional_exclusive(dev, "phy");
  242. if (IS_ERR(priv->p30phy)) {
  243. return dev_err_probe(dev, PTR_ERR(priv->p30phy),
  244. "failed to get phy reset control\n");
  245. }
  246. if (!priv->p30phy)
  247. dev_info(dev, "no phy reset control specified\n");
  248. priv->num_clks = devm_clk_bulk_get_all(dev, &priv->clks);
  249. if (priv->num_clks < 1)
  250. return -ENODEV;
  251. dev_set_drvdata(dev, priv);
  252. phy_set_drvdata(priv->phy, priv);
  253. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  254. return PTR_ERR_OR_ZERO(phy_provider);
  255. }
  256. static const struct of_device_id rockchip_p3phy_of_match[] = {
  257. { .compatible = "rockchip,rk3568-pcie3-phy", .data = &rk3568_ops },
  258. { .compatible = "rockchip,rk3588-pcie3-phy", .data = &rk3588_ops },
  259. { },
  260. };
  261. MODULE_DEVICE_TABLE(of, rockchip_p3phy_of_match);
  262. static struct platform_driver rockchip_p3phy_driver = {
  263. .probe = rockchip_p3phy_probe,
  264. .driver = {
  265. .name = "rockchip-snps-pcie3-phy",
  266. .of_match_table = rockchip_p3phy_of_match,
  267. },
  268. };
  269. module_platform_driver(rockchip_p3phy_driver);
  270. MODULE_DESCRIPTION("Rockchip Synopsys PCIe 3.0 PHY driver");
  271. MODULE_LICENSE("GPL");