blob: 2737bd81dd968227eb071b6929bf0ea9805ad531 [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)
39
Shawn Linaa2bd2e2021-01-15 18:01:21 +080040/**
41 * struct rockchip_p3phy_priv - RK DW PCIe PHY state
42 *
43 * @mmio: The base address of PHY internal registers
44 * @phy_grf: The regmap for controlling pipe signal
45 * @p30phy: The reset signal for PHY
Jonas Karlman6153aed2023-08-02 19:04:29 +000046 * @clks: The clocks for PHY
Jonas Karlmanb7ca8842023-08-02 19:28:33 +000047 * @num_lanes: The number of lane to controller mappings
48 * @lanes: The lane to controller mapping
Shawn Linaa2bd2e2021-01-15 18:01:21 +080049 */
50struct rockchip_p3phy_priv {
51 void __iomem *mmio;
52 struct regmap *phy_grf;
Jonas Karlman3f24f8b2023-08-02 19:04:32 +000053 struct regmap *pipe_grf;
Shawn Linaa2bd2e2021-01-15 18:01:21 +080054 struct reset_ctl p30phy;
Jonas Karlman6153aed2023-08-02 19:04:29 +000055 struct clk_bulk clks;
Jonas Karlmanb7ca8842023-08-02 19:28:33 +000056 int num_lanes;
57 u32 lanes[4];
Shawn Linaa2bd2e2021-01-15 18:01:21 +080058};
59
Jonas Karlmana10080c2023-08-02 19:04:30 +000060struct rockchip_p3phy_ops {
61 int (*phy_init)(struct phy *phy);
62};
63
64static int rockchip_p3phy_rk3568_init(struct phy *phy)
65{
66 struct rockchip_p3phy_priv *priv = dev_get_priv(phy->dev);
Jonas Karlmanb7ca8842023-08-02 19:28:33 +000067 bool bifurcation = false;
68 int ret;
69 u32 reg;
Jonas Karlmana10080c2023-08-02 19:04:30 +000070
71 /* Deassert PCIe PMA output clamp mode */
Jonas Karlmanb7ca8842023-08-02 19:28:33 +000072 regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON9, GRF_PCIE30PHY_DA_OCM);
73
74 for (int i = 0; i < priv->num_lanes; i++) {
75 if (priv->lanes[i] > 1)
76 bifurcation = true;
77 }
78
79 /* Set bifurcation if needed, and it doesn't care RC/EP */
80 if (bifurcation) {
81 regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON6,
82 GRF_PCIE30PHY_WR_EN | RK3568_BIFURCATION_LANE_0_1);
83 regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON1,
84 GRF_PCIE30PHY_DA_OCM);
85 } else {
86 regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON6,
87 GRF_PCIE30PHY_WR_EN & ~RK3568_BIFURCATION_LANE_0_1);
88 }
Jonas Karlmana10080c2023-08-02 19:04:30 +000089
90 reset_deassert(&priv->p30phy);
91 udelay(1);
92
Jonas Karlmanb7ca8842023-08-02 19:28:33 +000093 ret = regmap_read_poll_timeout(priv->phy_grf,
94 GRF_PCIE30PHY_STATUS0,
95 reg, SRAM_INIT_DONE(reg),
96 0, 500);
97 if (ret)
98 dev_err(phy->dev, "lock failed 0x%x\n", reg);
99
100 return ret;
Jonas Karlmana10080c2023-08-02 19:04:30 +0000101}
102
103static const struct rockchip_p3phy_ops rk3568_ops = {
104 .phy_init = rockchip_p3phy_rk3568_init,
105};
106
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000107static int rockchip_p3phy_rk3588_init(struct phy *phy)
108{
109 struct rockchip_p3phy_priv *priv = dev_get_priv(phy->dev);
110 u32 reg = 0;
111 u8 mode = 0;
112 int ret;
113
114 /* Deassert PCIe PMA output clamp mode */
115 regmap_write(priv->phy_grf, RK3588_PCIE3PHY_GRF_CMN_CON0,
116 BIT(8) | BIT(24));
117
118 /* Set bifurcation if needed */
119 for (int i = 0; i < priv->num_lanes; i++) {
120 if (!priv->lanes[i])
121 mode |= (BIT(i) << 3);
122
123 if (priv->lanes[i] > 1)
124 mode |= (BIT(i) >> 1);
125 }
126
127 if (!mode) {
128 reg = RK3588_LANE_AGGREGATION;
129 } else {
130 if (mode & (BIT(0) | BIT(1)))
131 reg |= RK3588_BIFURCATION_LANE_0_1;
132
133 if (mode & (BIT(2) | BIT(3)))
134 reg |= RK3588_BIFURCATION_LANE_2_3;
135 }
136
137 regmap_write(priv->phy_grf, RK3588_PCIE3PHY_GRF_CMN_CON0,
138 (0x7 << 16) | reg);
139
140 /* Set pcie1ln_sel in PHP_GRF_PCIESEL_CON */
141 reg = (mode & (BIT(6) | BIT(7))) >> 6;
142 if (reg)
143 regmap_write(priv->pipe_grf, PHP_GRF_PCIESEL_CON,
144 (reg << 16) | reg);
145
146 reset_deassert(&priv->p30phy);
147 udelay(1);
148
149 ret = regmap_read_poll_timeout(priv->phy_grf,
150 RK3588_PCIE3PHY_GRF_PHY0_STATUS1,
151 reg, RK3588_SRAM_INIT_DONE(reg),
152 0, 500);
153 ret |= regmap_read_poll_timeout(priv->phy_grf,
154 RK3588_PCIE3PHY_GRF_PHY1_STATUS1,
155 reg, RK3588_SRAM_INIT_DONE(reg),
156 0, 500);
157 if (ret)
158 dev_err(phy->dev, "lock failed 0x%x\n", reg);
159
160 return ret;
161}
162
163static const struct rockchip_p3phy_ops rk3588_ops = {
164 .phy_init = rockchip_p3phy_rk3588_init,
165};
166
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800167static int rochchip_p3phy_init(struct phy *phy)
168{
Jonas Karlmana10080c2023-08-02 19:04:30 +0000169 struct rockchip_p3phy_ops *ops =
170 (struct rockchip_p3phy_ops *)dev_get_driver_data(phy->dev);
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800171 struct rockchip_p3phy_priv *priv = dev_get_priv(phy->dev);
172 int ret;
173
Jonas Karlman6153aed2023-08-02 19:04:29 +0000174 ret = clk_enable_bulk(&priv->clks);
175 if (ret)
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800176 return ret;
177
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800178 reset_assert(&priv->p30phy);
179 udelay(1);
180
Jonas Karlmana10080c2023-08-02 19:04:30 +0000181 ret = ops->phy_init(phy);
182 if (ret)
183 clk_disable_bulk(&priv->clks);
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800184
Jonas Karlmana10080c2023-08-02 19:04:30 +0000185 return ret;
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800186}
187
188static int rochchip_p3phy_exit(struct phy *phy)
189{
190 struct rockchip_p3phy_priv *priv = dev_get_priv(phy->dev);
191
Jonas Karlman6153aed2023-08-02 19:04:29 +0000192 clk_disable_bulk(&priv->clks);
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800193 reset_assert(&priv->p30phy);
194
195 return 0;
196}
197
198static int rockchip_p3phy_probe(struct udevice *dev)
199{
200 struct rockchip_p3phy_priv *priv = dev_get_priv(dev);
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800201 int ret;
202
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100203 priv->mmio = dev_read_addr_ptr(dev);
204 if (!priv->mmio)
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800205 return -EINVAL;
206
Jonas Karlman6153aed2023-08-02 19:04:29 +0000207 priv->phy_grf = syscon_regmap_lookup_by_phandle(dev, "rockchip,phy-grf");
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800208 if (IS_ERR(priv->phy_grf)) {
209 dev_err(dev, "failed to find rockchip,phy_grf regmap\n");
210 return PTR_ERR(priv->phy_grf);
211 }
212
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000213 if (device_is_compatible(dev, "rockchip,rk3588-pcie3-phy")) {
214 priv->pipe_grf =
215 syscon_regmap_lookup_by_phandle(dev, "rockchip,pipe-grf");
216 if (IS_ERR(priv->pipe_grf)) {
217 dev_err(dev, "failed to find rockchip,pipe_grf regmap\n");
218 return PTR_ERR(priv->pipe_grf);
219 }
220 }
221
Jonas Karlmanb7ca8842023-08-02 19:28:33 +0000222 ret = dev_read_size(dev, "data-lanes");
223 if (ret > 0) {
224 priv->num_lanes = ret / sizeof(u32);
225 if (priv->num_lanes < 2 ||
226 priv->num_lanes > ARRAY_SIZE(priv->lanes)) {
227 dev_err(dev, "unsupported data-lanes property size\n");
228 return -EINVAL;
229 }
230
231 ret = dev_read_u32_array(dev, "data-lanes", priv->lanes,
232 priv->num_lanes);
233 if (ret) {
234 dev_err(dev, "failed to read data-lanes property\n");
235 return ret;
236 }
237 }
238
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800239 ret = reset_get_by_name(dev, "phy", &priv->p30phy);
240 if (ret) {
241 dev_err(dev, "no phy reset control specified\n");
242 return ret;
243 }
244
Jonas Karlman6153aed2023-08-02 19:04:29 +0000245 ret = clk_get_bulk(dev, &priv->clks);
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800246 if (ret) {
Jonas Karlman6153aed2023-08-02 19:04:29 +0000247 dev_err(dev, "failed to get clocks\n");
248 return ret;
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800249 }
250
251 return 0;
252}
253
254static struct phy_ops rochchip_p3phy_ops = {
255 .init = rochchip_p3phy_init,
256 .exit = rochchip_p3phy_exit,
257};
258
259static const struct udevice_id rockchip_p3phy_of_match[] = {
Jonas Karlmana10080c2023-08-02 19:04:30 +0000260 {
261 .compatible = "rockchip,rk3568-pcie3-phy",
262 .data = (ulong)&rk3568_ops,
263 },
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000264 {
265 .compatible = "rockchip,rk3588-pcie3-phy",
266 .data = (ulong)&rk3588_ops,
267 },
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800268 { },
269};
270
271U_BOOT_DRIVER(rockchip_pcie3phy) = {
272 .name = "rockchip_pcie3phy",
273 .id = UCLASS_PHY,
274 .of_match = rockchip_p3phy_of_match,
275 .ops = &rochchip_p3phy_ops,
276 .probe = rockchip_p3phy_probe,
277 .priv_auto = sizeof(struct rockchip_p3phy_priv),
278};