blob: fadb77c25cf2a8ac15f5e9794157eab3c1e7b090 [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;
Sebastian Kropatschefa02142024-07-14 23:23:42 +0200111 u8 mode = RK3588_LANE_AGGREGATION; /* Lane aggregation by default */
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000112 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++) {
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000120 if (priv->lanes[i] > 1)
Sebastian Kropatschefa02142024-07-14 23:23:42 +0200121 mode &= ~RK3588_LANE_AGGREGATION;
122 if (priv->lanes[i] == 3)
123 mode |= RK3588_BIFURCATION_LANE_0_1;
124 if (priv->lanes[i] == 4)
125 mode |= RK3588_BIFURCATION_LANE_2_3;
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000126 }
127
Sebastian Kropatschefa02142024-07-14 23:23:42 +0200128 reg = mode;
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000129 regmap_write(priv->phy_grf, RK3588_PCIE3PHY_GRF_CMN_CON0,
130 (0x7 << 16) | reg);
131
132 /* Set pcie1ln_sel in PHP_GRF_PCIESEL_CON */
Sebastian Kropatschefa02142024-07-14 23:23:42 +0200133 reg = mode & 3;
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000134 if (reg)
135 regmap_write(priv->pipe_grf, PHP_GRF_PCIESEL_CON,
136 (reg << 16) | reg);
137
138 reset_deassert(&priv->p30phy);
139 udelay(1);
140
141 ret = regmap_read_poll_timeout(priv->phy_grf,
142 RK3588_PCIE3PHY_GRF_PHY0_STATUS1,
143 reg, RK3588_SRAM_INIT_DONE(reg),
144 0, 500);
145 ret |= regmap_read_poll_timeout(priv->phy_grf,
146 RK3588_PCIE3PHY_GRF_PHY1_STATUS1,
147 reg, RK3588_SRAM_INIT_DONE(reg),
148 0, 500);
149 if (ret)
150 dev_err(phy->dev, "lock failed 0x%x\n", reg);
151
152 return ret;
153}
154
155static const struct rockchip_p3phy_ops rk3588_ops = {
156 .phy_init = rockchip_p3phy_rk3588_init,
157};
158
Sebastian Kropatschb4ab3e52024-07-14 23:23:41 +0200159static int rockchip_p3phy_init(struct phy *phy)
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800160{
Jonas Karlmana10080c2023-08-02 19:04:30 +0000161 struct rockchip_p3phy_ops *ops =
162 (struct rockchip_p3phy_ops *)dev_get_driver_data(phy->dev);
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800163 struct rockchip_p3phy_priv *priv = dev_get_priv(phy->dev);
164 int ret;
165
Jonas Karlman6153aed2023-08-02 19:04:29 +0000166 ret = clk_enable_bulk(&priv->clks);
167 if (ret)
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800168 return ret;
169
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800170 reset_assert(&priv->p30phy);
171 udelay(1);
172
Jonas Karlmana10080c2023-08-02 19:04:30 +0000173 ret = ops->phy_init(phy);
174 if (ret)
175 clk_disable_bulk(&priv->clks);
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800176
Jonas Karlmana10080c2023-08-02 19:04:30 +0000177 return ret;
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800178}
179
Sebastian Kropatschb4ab3e52024-07-14 23:23:41 +0200180static int rockchip_p3phy_exit(struct phy *phy)
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800181{
182 struct rockchip_p3phy_priv *priv = dev_get_priv(phy->dev);
183
Jonas Karlman6153aed2023-08-02 19:04:29 +0000184 clk_disable_bulk(&priv->clks);
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800185 reset_assert(&priv->p30phy);
186
187 return 0;
188}
189
190static int rockchip_p3phy_probe(struct udevice *dev)
191{
192 struct rockchip_p3phy_priv *priv = dev_get_priv(dev);
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800193 int ret;
194
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100195 priv->mmio = dev_read_addr_ptr(dev);
196 if (!priv->mmio)
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800197 return -EINVAL;
198
Jonas Karlman6153aed2023-08-02 19:04:29 +0000199 priv->phy_grf = syscon_regmap_lookup_by_phandle(dev, "rockchip,phy-grf");
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800200 if (IS_ERR(priv->phy_grf)) {
201 dev_err(dev, "failed to find rockchip,phy_grf regmap\n");
202 return PTR_ERR(priv->phy_grf);
203 }
204
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000205 if (device_is_compatible(dev, "rockchip,rk3588-pcie3-phy")) {
206 priv->pipe_grf =
207 syscon_regmap_lookup_by_phandle(dev, "rockchip,pipe-grf");
208 if (IS_ERR(priv->pipe_grf)) {
209 dev_err(dev, "failed to find rockchip,pipe_grf regmap\n");
210 return PTR_ERR(priv->pipe_grf);
211 }
212 }
213
Jonas Karlmanb7ca8842023-08-02 19:28:33 +0000214 ret = dev_read_size(dev, "data-lanes");
215 if (ret > 0) {
216 priv->num_lanes = ret / sizeof(u32);
217 if (priv->num_lanes < 2 ||
218 priv->num_lanes > ARRAY_SIZE(priv->lanes)) {
219 dev_err(dev, "unsupported data-lanes property size\n");
220 return -EINVAL;
221 }
222
223 ret = dev_read_u32_array(dev, "data-lanes", priv->lanes,
224 priv->num_lanes);
225 if (ret) {
226 dev_err(dev, "failed to read data-lanes property\n");
227 return ret;
228 }
229 }
230
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800231 ret = reset_get_by_name(dev, "phy", &priv->p30phy);
232 if (ret) {
233 dev_err(dev, "no phy reset control specified\n");
234 return ret;
235 }
236
Jonas Karlman6153aed2023-08-02 19:04:29 +0000237 ret = clk_get_bulk(dev, &priv->clks);
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800238 if (ret) {
Jonas Karlman6153aed2023-08-02 19:04:29 +0000239 dev_err(dev, "failed to get clocks\n");
240 return ret;
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800241 }
242
243 return 0;
244}
245
Sebastian Kropatschb4ab3e52024-07-14 23:23:41 +0200246static struct phy_ops rockchip_p3phy_ops = {
247 .init = rockchip_p3phy_init,
248 .exit = rockchip_p3phy_exit,
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800249};
250
251static const struct udevice_id rockchip_p3phy_of_match[] = {
Jonas Karlmana10080c2023-08-02 19:04:30 +0000252 {
253 .compatible = "rockchip,rk3568-pcie3-phy",
254 .data = (ulong)&rk3568_ops,
255 },
Jonas Karlman3f24f8b2023-08-02 19:04:32 +0000256 {
257 .compatible = "rockchip,rk3588-pcie3-phy",
258 .data = (ulong)&rk3588_ops,
259 },
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800260 { },
261};
262
263U_BOOT_DRIVER(rockchip_pcie3phy) = {
264 .name = "rockchip_pcie3phy",
265 .id = UCLASS_PHY,
266 .of_match = rockchip_p3phy_of_match,
Sebastian Kropatschb4ab3e52024-07-14 23:23:41 +0200267 .ops = &rockchip_p3phy_ops,
Shawn Linaa2bd2e2021-01-15 18:01:21 +0800268 .probe = rockchip_p3phy_probe,
269 .priv_auto = sizeof(struct rockchip_p3phy_priv),
270};