blob: 62b42d1805bca89572200f81a409257e8d7b5585 [file] [log] [blame]
Shawn Linaa2bd2e2021-01-15 18:01:21 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Rockchip PCIE3.0 phy driver
4 *
5 * Copyright (C) 2021 Rockchip Electronics Co., Ltd.
6 */
7
Shawn Linaa2bd2e2021-01-15 18:01:21 +08008#include <clk.h>
9#include <dm.h>
10#include <generic-phy.h>
11#include <regmap.h>
12#include <reset-uclass.h>
13#include <syscon.h>
14#include <asm/io.h>
15#include <dm/device_compat.h>
16#include <dm/lists.h>
17
Jonas Karlmanb7ca8842023-08-02 19:28:33 +000018/* Register for RK3568 */
19#define GRF_PCIE30PHY_CON1 0x4
20#define GRF_PCIE30PHY_CON6 0x18
21#define GRF_PCIE30PHY_CON9 0x24
22#define GRF_PCIE30PHY_DA_OCM (BIT(15) | BIT(31))
23#define GRF_PCIE30PHY_STATUS0 0x80
24#define GRF_PCIE30PHY_WR_EN (0xf << 16)
25#define SRAM_INIT_DONE(reg) (reg & BIT(14))
26
27#define RK3568_BIFURCATION_LANE_0_1 BIT(0)
Shawn Linaa2bd2e2021-01-15 18:01:21 +080028
Jonas Karlman3f24f8b2023-08-02 19:04:32 +000029/* 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_SRAM_INIT_DONE(reg) (reg & BIT(0))
35
36#define RK3588_BIFURCATION_LANE_0_1 BIT(0)
37#define RK3588_BIFURCATION_LANE_2_3 BIT(1)
38#define RK3588_LANE_AGGREGATION BIT(2)
Sebastian Kropatschea40d552024-07-14 23:23:43 +020039#define RK3588_PCIE1LN_SEL_EN (GENMASK(1, 0) << 16)
40#define RK3588_PCIE30_PHY_MODE_EN (GENMASK(2, 0) << 16)
Jonas Karlman3f24f8b2023-08-02 19:04:32 +000041
Shawn Linaa2bd2e2021-01-15 18:01:21 +080042/**
43 * struct rockchip_p3phy_priv - RK DW PCIe PHY state
44 *
45 * @mmio: The base address of PHY internal registers
46 * @phy_grf: The regmap for controlling pipe signal
47 * @p30phy: The reset signal for PHY
Jonas Karlman6153aed2023-08-02 19:04:29 +000048 * @clks: The clocks for PHY
Jonas Karlmanb7ca8842023-08-02 19:28:33 +000049 * @num_lanes: The number of lane to controller mappings
50 * @lanes: The lane to controller mapping
Shawn Linaa2bd2e2021-01-15 18:01:21 +080051 */
52struct rockchip_p3phy_priv {
53 void __iomem *mmio;
54 struct regmap *phy_grf;
Jonas Karlman3f24f8b2023-08-02 19:04:32 +000055 struct regmap *pipe_grf;
Shawn Linaa2bd2e2021-01-15 18:01:21 +080056 struct reset_ctl p30phy;
Jonas Karlman6153aed2023-08-02 19:04:29 +000057 struct clk_bulk clks;
Jonas Karlmanb7ca8842023-08-02 19:28:33 +000058 int num_lanes;
59 u32 lanes[4];
Shawn Linaa2bd2e2021-01-15 18:01:21 +080060};
61
Jonas Karlmana10080c2023-08-02 19:04:30 +000062struct rockchip_p3phy_ops {
63 int (*phy_init)(struct phy *phy);
64};
65
66static int rockchip_p3phy_rk3568_init(struct phy *phy)
67{
68 struct rockchip_p3phy_priv *priv = dev_get_priv(phy->dev);
Jonas Karlmanb7ca8842023-08-02 19:28:33 +000069 bool bifurcation = false;
70 int ret;
71 u32 reg;
Jonas Karlmana10080c2023-08-02 19:04:30 +000072
73 /* Deassert PCIe PMA output clamp mode */
Jonas Karlmanb7ca8842023-08-02 19:28:33 +000074 regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON9, GRF_PCIE30PHY_DA_OCM);
75
76 for (int i = 0; i < priv->num_lanes; i++) {
77 if (priv->lanes[i] > 1)
78 bifurcation = true;
79 }
80
81 /* Set bifurcation if needed, and it doesn't care RC/EP */
82 if (bifurcation) {
83 regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON6,
84 GRF_PCIE30PHY_WR_EN | RK3568_BIFURCATION_LANE_0_1);
85 regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON1,
86 GRF_PCIE30PHY_DA_OCM);
87 } else {
88 regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON6,
89 GRF_PCIE30PHY_WR_EN & ~RK3568_BIFURCATION_LANE_0_1);
90 }
Jonas Karlmana10080c2023-08-02 19:04:30 +000091
92 reset_deassert(&priv->p30phy);
93 udelay(1);
94
Jonas Karlmanb7ca8842023-08-02 19:28:33 +000095 ret = regmap_read_poll_timeout(priv->phy_grf,
96 GRF_PCIE30PHY_STATUS0,
97 reg, SRAM_INIT_DONE(reg),
98 0, 500);
99 if (ret)
100 dev_err(phy->dev, "lock failed 0x%x\n", reg);
101
102 return ret;
Jonas Karlmana10080c2023-08-02 19:04:30 +0000103}
104
105static const struct rockchip_p3phy_ops rk3568_ops = {
106 .phy_init = rockchip_p3phy_rk3568_init,
107};
108
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000109static int rockchip_p3phy_rk3588_init(struct phy *phy)
110{
111 struct rockchip_p3phy_priv *priv = dev_get_priv(phy->dev);
112 u32 reg = 0;
Sebastian Kropatschefa02142024-07-14 23:23:42 +0200113 u8 mode = RK3588_LANE_AGGREGATION; /* Lane aggregation by default */
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000114 int ret;
115
116 /* Deassert PCIe PMA output clamp mode */
117 regmap_write(priv->phy_grf, RK3588_PCIE3PHY_GRF_CMN_CON0,
118 BIT(8) | BIT(24));
119
120 /* Set bifurcation if needed */
121 for (int i = 0; i < priv->num_lanes; i++) {
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000122 if (priv->lanes[i] > 1)
Sebastian Kropatschefa02142024-07-14 23:23:42 +0200123 mode &= ~RK3588_LANE_AGGREGATION;
124 if (priv->lanes[i] == 3)
125 mode |= RK3588_BIFURCATION_LANE_0_1;
126 if (priv->lanes[i] == 4)
127 mode |= RK3588_BIFURCATION_LANE_2_3;
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000128 }
129
Sebastian Kropatschefa02142024-07-14 23:23:42 +0200130 reg = mode;
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000131 regmap_write(priv->phy_grf, RK3588_PCIE3PHY_GRF_CMN_CON0,
Sebastian Kropatschea40d552024-07-14 23:23:43 +0200132 RK3588_PCIE30_PHY_MODE_EN | reg);
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000133
134 /* Set pcie1ln_sel in PHP_GRF_PCIESEL_CON */
Sebastian Kropatschea40d552024-07-14 23:23:43 +0200135 reg = mode & (RK3588_BIFURCATION_LANE_0_1 | RK3588_BIFURCATION_LANE_2_3);
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000136 if (reg)
137 regmap_write(priv->pipe_grf, PHP_GRF_PCIESEL_CON,
Sebastian Kropatschea40d552024-07-14 23:23:43 +0200138 RK3588_PCIE1LN_SEL_EN | reg);
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000139
140 reset_deassert(&priv->p30phy);
141 udelay(1);
142
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(phy->dev, "lock failed 0x%x\n", reg);
153
154 return ret;
155}
156
157static const struct rockchip_p3phy_ops rk3588_ops = {
158 .phy_init = rockchip_p3phy_rk3588_init,
159};
160
Sebastian Kropatschb4ab3e52024-07-14 23:23:41 +0200161static int rockchip_p3phy_init(struct phy *phy)
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800162{
Jonas Karlmana10080c2023-08-02 19:04:30 +0000163 struct rockchip_p3phy_ops *ops =
164 (struct rockchip_p3phy_ops *)dev_get_driver_data(phy->dev);
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800165 struct rockchip_p3phy_priv *priv = dev_get_priv(phy->dev);
166 int ret;
167
Jonas Karlman6153aed2023-08-02 19:04:29 +0000168 ret = clk_enable_bulk(&priv->clks);
169 if (ret)
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800170 return ret;
171
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800172 reset_assert(&priv->p30phy);
173 udelay(1);
174
Jonas Karlmana10080c2023-08-02 19:04:30 +0000175 ret = ops->phy_init(phy);
176 if (ret)
177 clk_disable_bulk(&priv->clks);
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800178
Jonas Karlmana10080c2023-08-02 19:04:30 +0000179 return ret;
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800180}
181
Sebastian Kropatschb4ab3e52024-07-14 23:23:41 +0200182static int rockchip_p3phy_exit(struct phy *phy)
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800183{
184 struct rockchip_p3phy_priv *priv = dev_get_priv(phy->dev);
185
Jonas Karlman6153aed2023-08-02 19:04:29 +0000186 clk_disable_bulk(&priv->clks);
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800187 reset_assert(&priv->p30phy);
188
189 return 0;
190}
191
192static int rockchip_p3phy_probe(struct udevice *dev)
193{
194 struct rockchip_p3phy_priv *priv = dev_get_priv(dev);
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800195 int ret;
196
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100197 priv->mmio = dev_read_addr_ptr(dev);
198 if (!priv->mmio)
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800199 return -EINVAL;
200
Jonas Karlman6153aed2023-08-02 19:04:29 +0000201 priv->phy_grf = syscon_regmap_lookup_by_phandle(dev, "rockchip,phy-grf");
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800202 if (IS_ERR(priv->phy_grf)) {
203 dev_err(dev, "failed to find rockchip,phy_grf regmap\n");
204 return PTR_ERR(priv->phy_grf);
205 }
206
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000207 if (device_is_compatible(dev, "rockchip,rk3588-pcie3-phy")) {
208 priv->pipe_grf =
209 syscon_regmap_lookup_by_phandle(dev, "rockchip,pipe-grf");
210 if (IS_ERR(priv->pipe_grf)) {
211 dev_err(dev, "failed to find rockchip,pipe_grf regmap\n");
212 return PTR_ERR(priv->pipe_grf);
213 }
214 }
215
Jonas Karlmanb7ca8842023-08-02 19:28:33 +0000216 ret = dev_read_size(dev, "data-lanes");
217 if (ret > 0) {
218 priv->num_lanes = ret / sizeof(u32);
219 if (priv->num_lanes < 2 ||
220 priv->num_lanes > ARRAY_SIZE(priv->lanes)) {
221 dev_err(dev, "unsupported data-lanes property size\n");
222 return -EINVAL;
223 }
224
225 ret = dev_read_u32_array(dev, "data-lanes", priv->lanes,
226 priv->num_lanes);
227 if (ret) {
228 dev_err(dev, "failed to read data-lanes property\n");
229 return ret;
230 }
231 }
232
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800233 ret = reset_get_by_name(dev, "phy", &priv->p30phy);
234 if (ret) {
235 dev_err(dev, "no phy reset control specified\n");
236 return ret;
237 }
238
Jonas Karlman6153aed2023-08-02 19:04:29 +0000239 ret = clk_get_bulk(dev, &priv->clks);
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800240 if (ret) {
Jonas Karlman6153aed2023-08-02 19:04:29 +0000241 dev_err(dev, "failed to get clocks\n");
242 return ret;
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800243 }
244
245 return 0;
246}
247
Sebastian Kropatschb4ab3e52024-07-14 23:23:41 +0200248static struct phy_ops rockchip_p3phy_ops = {
249 .init = rockchip_p3phy_init,
250 .exit = rockchip_p3phy_exit,
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800251};
252
253static const struct udevice_id rockchip_p3phy_of_match[] = {
Jonas Karlmana10080c2023-08-02 19:04:30 +0000254 {
255 .compatible = "rockchip,rk3568-pcie3-phy",
256 .data = (ulong)&rk3568_ops,
257 },
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000258 {
259 .compatible = "rockchip,rk3588-pcie3-phy",
260 .data = (ulong)&rk3588_ops,
261 },
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800262 { },
263};
264
265U_BOOT_DRIVER(rockchip_pcie3phy) = {
266 .name = "rockchip_pcie3phy",
267 .id = UCLASS_PHY,
268 .of_match = rockchip_p3phy_of_match,
Sebastian Kropatschb4ab3e52024-07-14 23:23:41 +0200269 .ops = &rockchip_p3phy_ops,
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800270 .probe = rockchip_p3phy_probe,
271 .priv_auto = sizeof(struct rockchip_p3phy_priv),
272};