1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Rockchip PCIE3.0 phy driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2021 Rockchip Electronics Co., Ltd.
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <common.h>
9*4882a593Smuzhiyun #include <clk.h>
10*4882a593Smuzhiyun #include <dm.h>
11*4882a593Smuzhiyun #include <dm/lists.h>
12*4882a593Smuzhiyun #include <generic-phy.h>
13*4882a593Smuzhiyun #include <syscon.h>
14*4882a593Smuzhiyun #include <asm/io.h>
15*4882a593Smuzhiyun #include <asm/arch/clock.h>
16*4882a593Smuzhiyun #include <regmap.h>
17*4882a593Smuzhiyun #include <reset-uclass.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun /* Register for RK3568 */
20*4882a593Smuzhiyun #define GRF_PCIE30PHY_RK3568_CON1 0x4
21*4882a593Smuzhiyun #define GRF_PCIE30PHY_RK3568_CON4 0x10
22*4882a593Smuzhiyun #define GRF_PCIE30PHY_RK3568_CON6 0x18
23*4882a593Smuzhiyun #define GRF_PCIE30PHY_RK3568_CON9 0x24
24*4882a593Smuzhiyun #define GRF_PCIE30PHY_RK3568_STATUS0 0x80
25*4882a593Smuzhiyun #define RK3568_SRAM_INIT_DONE(reg) (reg & BIT(14))
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun /* Register for RK3588 */
28*4882a593Smuzhiyun #define PHP_GRF_PCIESEL_CON 0x100
29*4882a593Smuzhiyun #define RK3588_PCIE3PHY_GRF_CMN_CON0 0x0
30*4882a593Smuzhiyun #define RK3588_PCIE3PHY_GRF_PHY0_STATUS1 0x904
31*4882a593Smuzhiyun #define RK3588_PCIE3PHY_GRF_PHY1_STATUS1 0xa04
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun /*
34*4882a593Smuzhiyun * pcie30_phy_mode[2:0]
35*4882a593Smuzhiyun * bit2: aggregation
36*4882a593Smuzhiyun * bit1: bifurcation for port 1
37*4882a593Smuzhiyun * bit0: bifurcation for port 0
38*4882a593Smuzhiyun */
39*4882a593Smuzhiyun #define PHY_MODE_PCIE_AGGREGATION 4 /* PCIe3x4 */
40*4882a593Smuzhiyun #define PHY_MODE_PCIE_NANBNB 0 /* P1:PCIe3x2 + P0:PCIe3x2 */
41*4882a593Smuzhiyun #define PHY_MODE_PCIE_NANBBI 1 /* P1:PCIe3x2 + P0:PCIe3x1*2 */
42*4882a593Smuzhiyun #define PHY_MODE_PCIE_NABINB 2 /* P1:PCIe3x1*2 + P0:PCIe3x2 */
43*4882a593Smuzhiyun #define PHY_MODE_PCIE_NABIBI 3 /* P1:PCIe3x1*2 + P0:PCIe3x1*2 */
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun struct rockchip_p3phy_ops;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun struct rockchip_p3phy_priv {
48*4882a593Smuzhiyun const struct rockchip_p3phy_ops *ops;
49*4882a593Smuzhiyun struct clk_bulk clks;
50*4882a593Smuzhiyun void __iomem *mmio;
51*4882a593Smuzhiyun int mode;
52*4882a593Smuzhiyun struct regmap *phy_grf;
53*4882a593Smuzhiyun struct regmap *pipe_grf;
54*4882a593Smuzhiyun struct reset_ctl p30phy;
55*4882a593Smuzhiyun bool is_bifurcation;
56*4882a593Smuzhiyun /* pcie30_phymode: Aggregation, Bifurcation */
57*4882a593Smuzhiyun int pcie30_phymode;
58*4882a593Smuzhiyun };
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun struct rockchip_p3phy_ops {
61*4882a593Smuzhiyun int (*phy_init)(struct rockchip_p3phy_priv *priv);
62*4882a593Smuzhiyun };
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun static const u16 phy_fw[] = {
65*4882a593Smuzhiyun #include "phy-rockchip-snps-pcie3.fw"
66*4882a593Smuzhiyun };
67*4882a593Smuzhiyun
rockchip_p3phy_rk3568_init(struct rockchip_p3phy_priv * priv)68*4882a593Smuzhiyun static int rockchip_p3phy_rk3568_init(struct rockchip_p3phy_priv *priv)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun int i, ret = 0;
71*4882a593Smuzhiyun u32 reg;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /* Deassert PCIe PMA output clamp mode */
74*4882a593Smuzhiyun regmap_write(priv->phy_grf, GRF_PCIE30PHY_RK3568_CON9,
75*4882a593Smuzhiyun (0x1 << 15) | (0x1 << 31));
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /* Set bifurcation if needed */
78*4882a593Smuzhiyun if (priv->is_bifurcation) {
79*4882a593Smuzhiyun regmap_write(priv->phy_grf, GRF_PCIE30PHY_RK3568_CON6,
80*4882a593Smuzhiyun 0x1 | (0xf << 16));
81*4882a593Smuzhiyun regmap_write(priv->phy_grf, GRF_PCIE30PHY_RK3568_CON1,
82*4882a593Smuzhiyun (0x1 << 15) | (0x1 << 31));
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun regmap_write(priv->phy_grf, GRF_PCIE30PHY_RK3568_CON4,
86*4882a593Smuzhiyun (0x0 << 14) | (0x1 << (14 + 16))); //sdram_ld_done
87*4882a593Smuzhiyun regmap_write(priv->phy_grf, GRF_PCIE30PHY_RK3568_CON4,
88*4882a593Smuzhiyun (0x0 << 13) | (0x1 << (13 + 16))); //sdram_bypass
89*4882a593Smuzhiyun reset_deassert(&priv->p30phy);
90*4882a593Smuzhiyun udelay(5);
91*4882a593Smuzhiyun ret = regmap_read_poll_timeout(priv->phy_grf,
92*4882a593Smuzhiyun GRF_PCIE30PHY_RK3568_STATUS0,
93*4882a593Smuzhiyun reg, RK3568_SRAM_INIT_DONE(reg),
94*4882a593Smuzhiyun 0, 500);
95*4882a593Smuzhiyun if (ret) {
96*4882a593Smuzhiyun pr_err("%s: lock failed 0x%x, check refclk and power\n",
97*4882a593Smuzhiyun __func__, reg);
98*4882a593Smuzhiyun return -ETIMEDOUT;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun regmap_write(priv->phy_grf, GRF_PCIE30PHY_RK3568_CON9,
102*4882a593Smuzhiyun (0x3 << 8) | (0x3 << (8 + 16))); //map to access sram
103*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(phy_fw); i++)
104*4882a593Smuzhiyun writel(phy_fw[i], priv->mmio + (i<<2));
105*4882a593Smuzhiyun printf("snps pcie3phy FW update! size %ld\n", ARRAY_SIZE(phy_fw));
106*4882a593Smuzhiyun regmap_write(priv->phy_grf, GRF_PCIE30PHY_RK3568_CON9,
107*4882a593Smuzhiyun (0x0 << 8) | (0x3 << (8 + 16)));
108*4882a593Smuzhiyun regmap_write(priv->phy_grf, GRF_PCIE30PHY_RK3568_CON4,
109*4882a593Smuzhiyun (0x1 << 14) | (0x1 << (14 + 16))); //sdram_ld_done
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun udelay(10);
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun return 0;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun static const struct rockchip_p3phy_ops rk3568_ops = {
117*4882a593Smuzhiyun .phy_init = &rockchip_p3phy_rk3568_init,
118*4882a593Smuzhiyun };
119*4882a593Smuzhiyun
rockchip_p3phy_rk3588_init(struct rockchip_p3phy_priv * priv)120*4882a593Smuzhiyun static int rockchip_p3phy_rk3588_init(struct rockchip_p3phy_priv *priv)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun u32 reg;
123*4882a593Smuzhiyun u32 timeout;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun /* Deassert PCIe PMA output clamp mode */
126*4882a593Smuzhiyun regmap_write(priv->phy_grf, RK3588_PCIE3PHY_GRF_CMN_CON0,
127*4882a593Smuzhiyun (0x1 << 8) | (0x1 << 24));
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun /* Select correct pcie30_phymode */
130*4882a593Smuzhiyun if (priv->pcie30_phymode > 4)
131*4882a593Smuzhiyun priv->pcie30_phymode = PHY_MODE_PCIE_AGGREGATION;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun regmap_write(priv->phy_grf, RK3588_PCIE3PHY_GRF_CMN_CON0,
134*4882a593Smuzhiyun (0x7<<16) | priv->pcie30_phymode);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /* Set pcie1ln_sel in PHP_GRF_PCIESEL_CON */
137*4882a593Smuzhiyun reg = priv->pcie30_phymode & 3;
138*4882a593Smuzhiyun if (reg)
139*4882a593Smuzhiyun regmap_write(priv->pipe_grf, PHP_GRF_PCIESEL_CON,
140*4882a593Smuzhiyun (reg << 16) | reg);
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun timeout = 500;
143*4882a593Smuzhiyun while (timeout--) {
144*4882a593Smuzhiyun regmap_read(priv->phy_grf, RK3588_PCIE3PHY_GRF_PHY0_STATUS1, ®);
145*4882a593Smuzhiyun if (reg & 0x1)
146*4882a593Smuzhiyun break;
147*4882a593Smuzhiyun udelay(1);
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun if (timeout <= 0) {
151*4882a593Smuzhiyun pr_err("%s: phy0 lock failed, check input refclk and power supply\n", __func__);
152*4882a593Smuzhiyun return -ETIMEDOUT;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun timeout = 500;
156*4882a593Smuzhiyun while (timeout--) {
157*4882a593Smuzhiyun regmap_read(priv->phy_grf, RK3588_PCIE3PHY_GRF_PHY1_STATUS1, ®);
158*4882a593Smuzhiyun if (reg & 0x1)
159*4882a593Smuzhiyun break;
160*4882a593Smuzhiyun udelay(1);
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun if (timeout <= 0) {
164*4882a593Smuzhiyun pr_err("%s: phy1 lock failed, check input refclk and power supply\n", __func__);
165*4882a593Smuzhiyun return -ETIMEDOUT;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun reset_deassert(&priv->p30phy);
169*4882a593Smuzhiyun udelay(5);
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun return 0;
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun static const struct rockchip_p3phy_ops rk3588_ops = {
175*4882a593Smuzhiyun .phy_init = &rockchip_p3phy_rk3588_init,
176*4882a593Smuzhiyun };
177*4882a593Smuzhiyun
rochchip_p3phy_init(struct phy * phy)178*4882a593Smuzhiyun static int rochchip_p3phy_init(struct phy *phy)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun struct rockchip_p3phy_priv *priv = dev_get_priv(phy->dev);
181*4882a593Smuzhiyun int ret;
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun ret = clk_enable_bulk(&priv->clks);
184*4882a593Smuzhiyun if (ret) {
185*4882a593Smuzhiyun pr_err("failed to enable clks (ret=%d)\n", ret);
186*4882a593Smuzhiyun return ret;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun reset_assert(&priv->p30phy);
190*4882a593Smuzhiyun udelay(1);
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun if (priv->ops->phy_init) {
193*4882a593Smuzhiyun ret = priv->ops->phy_init(priv);
194*4882a593Smuzhiyun if (ret) {
195*4882a593Smuzhiyun clk_disable_bulk(&priv->clks);
196*4882a593Smuzhiyun return ret;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun return 0;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun
rochchip_p3phy_exit(struct phy * phy)204*4882a593Smuzhiyun static int rochchip_p3phy_exit(struct phy *phy)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun struct rockchip_p3phy_priv *priv = dev_get_priv(phy->dev);
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun clk_disable_bulk(&priv->clks);
209*4882a593Smuzhiyun reset_assert(&priv->p30phy);
210*4882a593Smuzhiyun return 0;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
rockchip_p3phy_probe(struct udevice * dev)213*4882a593Smuzhiyun static int rockchip_p3phy_probe(struct udevice *dev)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun struct rockchip_p3phy_priv *priv = dev_get_priv(dev);
216*4882a593Smuzhiyun dev_get_driver_data(dev);
217*4882a593Smuzhiyun struct udevice *syscon;
218*4882a593Smuzhiyun int ret;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun priv->mmio = (void __iomem *)dev_read_addr(dev);
221*4882a593Smuzhiyun if ((fdt_addr_t)priv->mmio == FDT_ADDR_T_NONE)
222*4882a593Smuzhiyun return -EINVAL;
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun priv->ops = (struct rockchip_p3phy_ops *)dev_get_driver_data(dev);
225*4882a593Smuzhiyun if (!priv->ops) {
226*4882a593Smuzhiyun dev_err(dev, "no of match data provided\n");
227*4882a593Smuzhiyun return -EINVAL;
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
231*4882a593Smuzhiyun "rockchip,phy-grf", &syscon);
232*4882a593Smuzhiyun if (ret) {
233*4882a593Smuzhiyun pr_err("unable to find syscon device for rockchip,phy-grf\n");
234*4882a593Smuzhiyun return ret;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun priv->phy_grf = syscon_get_regmap(syscon);
238*4882a593Smuzhiyun if (IS_ERR(priv->phy_grf)) {
239*4882a593Smuzhiyun dev_err(dev, "failed to find rockchip,phy_grf regmap\n");
240*4882a593Smuzhiyun return PTR_ERR(priv->phy_grf);
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun dev_dbg(priv->dev, "phy_grf is 0x%llx\n", priv->phy_grf->base);
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
246*4882a593Smuzhiyun "rockchip,pipe-grf", &syscon);
247*4882a593Smuzhiyun if (ret) {
248*4882a593Smuzhiyun /* It's optional, rk3568 doesn't need it */
249*4882a593Smuzhiyun priv->pipe_grf = NULL;
250*4882a593Smuzhiyun pr_err("unable to get syscon device for rockchip,pipe-grf\n");
251*4882a593Smuzhiyun goto skip_pipe_grf;
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun priv->pipe_grf = syscon_get_regmap(syscon);
255*4882a593Smuzhiyun if (IS_ERR(priv->pipe_grf))
256*4882a593Smuzhiyun dev_err(dev, "failed to find rockchip,pipe_grf regmap\n");
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun priv->pcie30_phymode = dev_read_u32_default(dev, "rockchip,pcie30-phymode", PHY_MODE_PCIE_AGGREGATION);
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun skip_pipe_grf:
262*4882a593Smuzhiyun ret = reset_get_by_name(dev, "phy", &priv->p30phy);
263*4882a593Smuzhiyun if (ret) {
264*4882a593Smuzhiyun dev_err(dev, "no phy reset control specified\n");
265*4882a593Smuzhiyun return ret;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun if (ret) {
269*4882a593Smuzhiyun dev_err(dev, "Can't get clock: %d\n", ret);
270*4882a593Smuzhiyun return ret;
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun return 0;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
rockchip_p3phy_configure(struct phy * phy,union phy_configure_opts * opts)276*4882a593Smuzhiyun static int rockchip_p3phy_configure(struct phy *phy, union phy_configure_opts *opts)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun struct rockchip_p3phy_priv *priv = dev_get_priv(phy->dev);
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun priv->is_bifurcation = opts->pcie.is_bifurcation;
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun return 0;
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun static struct phy_ops rochchip_p3phy_ops = {
286*4882a593Smuzhiyun .init = rochchip_p3phy_init,
287*4882a593Smuzhiyun .exit = rochchip_p3phy_exit,
288*4882a593Smuzhiyun .configure = rockchip_p3phy_configure,
289*4882a593Smuzhiyun };
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun static const struct udevice_id rockchip_p3phy_of_match[] = {
292*4882a593Smuzhiyun { .compatible = "rockchip,rk3568-pcie3-phy", .data = (ulong)&rk3568_ops},
293*4882a593Smuzhiyun { .compatible = "rockchip,rk3588-pcie3-phy", .data = (ulong)&rk3588_ops },
294*4882a593Smuzhiyun { },
295*4882a593Smuzhiyun };
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun U_BOOT_DRIVER(rockchip_pcie3phy) = {
298*4882a593Smuzhiyun .name = "rockchip_pcie3phy",
299*4882a593Smuzhiyun .id = UCLASS_PHY,
300*4882a593Smuzhiyun .of_match = rockchip_p3phy_of_match,
301*4882a593Smuzhiyun .ops = &rochchip_p3phy_ops,
302*4882a593Smuzhiyun .probe = rockchip_p3phy_probe,
303*4882a593Smuzhiyun .priv_auto_alloc_size = sizeof(struct rockchip_p3phy_priv),
304*4882a593Smuzhiyun };
305