phy-rockchip-snps-pcie3.c 10 KB

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