xref: /OK3568_Linux_fs/kernel/drivers/phy/rockchip/phy-rockchip-snps-pcie3.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Rockchip PCIE3.0 phy driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2020 Rockchip Electronics Co., Ltd.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/clk.h>
9*4882a593Smuzhiyun #include <linux/delay.h>
10*4882a593Smuzhiyun #include <linux/io.h>
11*4882a593Smuzhiyun #include <linux/iopoll.h>
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/mfd/syscon.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/of_device.h>
16*4882a593Smuzhiyun #include <linux/phy/pcie.h>
17*4882a593Smuzhiyun #include <linux/phy/phy.h>
18*4882a593Smuzhiyun #include <linux/regmap.h>
19*4882a593Smuzhiyun #include <linux/reset.h>
20*4882a593Smuzhiyun #include <dt-bindings/phy/phy-snps-pcie3.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun /* Register for RK3568 */
23*4882a593Smuzhiyun #define GRF_PCIE30PHY_CON1 0x4
24*4882a593Smuzhiyun #define GRF_PCIE30PHY_CON4 0x10
25*4882a593Smuzhiyun #define GRF_PCIE30PHY_CON6 0x18
26*4882a593Smuzhiyun #define GRF_PCIE30PHY_CON9 0x24
27*4882a593Smuzhiyun #define GRF_PCIE30PHY_STATUS0 0x80
28*4882a593Smuzhiyun #define SRAM_INIT_DONE(reg) (reg & BIT(14))
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /* Register for RK3588 */
31*4882a593Smuzhiyun #define PHP_GRF_PCIESEL_CON 0x100
32*4882a593Smuzhiyun #define RK3588_PCIE3PHY_GRF_CMN_CON0 0x0
33*4882a593Smuzhiyun #define RK3588_PCIE3PHY_GRF_PHY0_STATUS1 0x904
34*4882a593Smuzhiyun #define RK3588_PCIE3PHY_GRF_PHY1_STATUS1 0xa04
35*4882a593Smuzhiyun #define RK3588_SRAM_INIT_DONE(reg) (reg & BIT(0))
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun struct rockchip_p3phy_ops;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun struct rockchip_p3phy_priv {
40*4882a593Smuzhiyun 	const struct rockchip_p3phy_ops *ops;
41*4882a593Smuzhiyun 	void __iomem *mmio;
42*4882a593Smuzhiyun 	/* mode: RC, EP */
43*4882a593Smuzhiyun 	int mode;
44*4882a593Smuzhiyun 	/* pcie30_phymode: Aggregation, Bifurcation */
45*4882a593Smuzhiyun 	int pcie30_phymode;
46*4882a593Smuzhiyun 	struct regmap *phy_grf;
47*4882a593Smuzhiyun 	struct regmap *pipe_grf;
48*4882a593Smuzhiyun 	struct reset_control *p30phy;
49*4882a593Smuzhiyun 	struct phy *phy;
50*4882a593Smuzhiyun 	struct clk_bulk_data *clks;
51*4882a593Smuzhiyun 	int num_clks;
52*4882a593Smuzhiyun 	bool is_bifurcation;
53*4882a593Smuzhiyun };
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun struct rockchip_p3phy_ops {
56*4882a593Smuzhiyun 	int (*phy_init)(struct rockchip_p3phy_priv *priv);
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun 
rockchip_p3phy_set_mode(struct phy * phy,enum phy_mode mode,int submode)59*4882a593Smuzhiyun static int rockchip_p3phy_set_mode(struct phy *phy, enum phy_mode mode, int submode)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun 	struct rockchip_p3phy_priv *priv = phy_get_drvdata(phy);
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	/* Acutally We don't care EP/RC mode, but just record it */
64*4882a593Smuzhiyun 	switch (submode) {
65*4882a593Smuzhiyun 	case PHY_MODE_PCIE_RC:
66*4882a593Smuzhiyun 		priv->mode = PHY_MODE_PCIE_RC;
67*4882a593Smuzhiyun 		break;
68*4882a593Smuzhiyun 	case PHY_MODE_PCIE_EP:
69*4882a593Smuzhiyun 		priv->mode = PHY_MODE_PCIE_EP;
70*4882a593Smuzhiyun 		break;
71*4882a593Smuzhiyun 	case PHY_MODE_PCIE_BIFURCATION:
72*4882a593Smuzhiyun 		priv->is_bifurcation = true;
73*4882a593Smuzhiyun 		break;
74*4882a593Smuzhiyun 	default:
75*4882a593Smuzhiyun 		pr_info("%s, invalid mode\n", __func__);
76*4882a593Smuzhiyun 		return -EINVAL;
77*4882a593Smuzhiyun 	}
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	return 0;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun static const u16 phy_fw[] = {
83*4882a593Smuzhiyun 	#include "phy-rockchip-snps-pcie3.fw"
84*4882a593Smuzhiyun };
85*4882a593Smuzhiyun 
rockchip_p3phy_rk3568_init(struct rockchip_p3phy_priv * priv)86*4882a593Smuzhiyun static int rockchip_p3phy_rk3568_init(struct rockchip_p3phy_priv *priv)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	int i;
89*4882a593Smuzhiyun 	int ret = 0;
90*4882a593Smuzhiyun 	u32 reg;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	/* Deassert PCIe PMA output clamp mode */
93*4882a593Smuzhiyun 	regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON9,
94*4882a593Smuzhiyun 		     (0x1 << 15) | (0x1 << 31));
95*4882a593Smuzhiyun 	/* Set bifurcation if needed, and it doesn't care RC/EP */
96*4882a593Smuzhiyun 	if (priv->is_bifurcation) {
97*4882a593Smuzhiyun 		regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON6,
98*4882a593Smuzhiyun 			     0x1 | (0xf << 16));
99*4882a593Smuzhiyun 		regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON1,
100*4882a593Smuzhiyun 			     (0x1 << 15) | (0x1 << 31));
101*4882a593Smuzhiyun 	}
102*4882a593Smuzhiyun 	regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON4,
103*4882a593Smuzhiyun 		     (0x0 << 14) | (0x1 << (14 + 16))); //sdram_ld_done
104*4882a593Smuzhiyun 	regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON4,
105*4882a593Smuzhiyun 		     (0x0 << 13) | (0x1 << (13 + 16))); //sdram_bypass
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	reset_control_deassert(priv->p30phy);
108*4882a593Smuzhiyun 	ret = regmap_read_poll_timeout(priv->phy_grf,
109*4882a593Smuzhiyun 				       GRF_PCIE30PHY_STATUS0,
110*4882a593Smuzhiyun 				       reg, SRAM_INIT_DONE(reg),
111*4882a593Smuzhiyun 				       0, 500);
112*4882a593Smuzhiyun 	if (ret) {
113*4882a593Smuzhiyun 		pr_err("%s: lock failed 0x%x, check input refclk and power supply\n",
114*4882a593Smuzhiyun 		       __func__, reg);
115*4882a593Smuzhiyun 		goto out;
116*4882a593Smuzhiyun 	}
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON9,
119*4882a593Smuzhiyun 		     (0x3 << 8) | (0x3 << (8 + 16))); //map to access sram
120*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(phy_fw); i++)
121*4882a593Smuzhiyun 		writel(phy_fw[i], priv->mmio + (i<<2));
122*4882a593Smuzhiyun 	pr_info("snps pcie3phy FW update! size %ld\n", ARRAY_SIZE(phy_fw));
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON9,
125*4882a593Smuzhiyun 		     (0x0 << 8) | (0x3 << (8 + 16)));
126*4882a593Smuzhiyun 	regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON4,
127*4882a593Smuzhiyun 		     (0x1 << 14) | (0x1 << (14 + 16))); //sdram_ld_done
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun out:
130*4882a593Smuzhiyun 	return ret;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun static const struct rockchip_p3phy_ops rk3568_ops = {
134*4882a593Smuzhiyun 	.phy_init = rockchip_p3phy_rk3568_init,
135*4882a593Smuzhiyun };
136*4882a593Smuzhiyun 
rockchip_p3phy_rk3588_init(struct rockchip_p3phy_priv * priv)137*4882a593Smuzhiyun static int rockchip_p3phy_rk3588_init(struct rockchip_p3phy_priv *priv)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun 	int ret = 0;
140*4882a593Smuzhiyun 	u32 reg;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	/* Deassert PCIe PMA output clamp mode */
143*4882a593Smuzhiyun 	regmap_write(priv->phy_grf, RK3588_PCIE3PHY_GRF_CMN_CON0,
144*4882a593Smuzhiyun 		     (0x1 << 8) | (0x1 << 24));
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	reset_control_deassert(priv->p30phy);
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	ret = regmap_read_poll_timeout(priv->phy_grf,
149*4882a593Smuzhiyun 				       RK3588_PCIE3PHY_GRF_PHY0_STATUS1,
150*4882a593Smuzhiyun 				       reg, RK3588_SRAM_INIT_DONE(reg),
151*4882a593Smuzhiyun 				       0, 500);
152*4882a593Smuzhiyun 	ret |= regmap_read_poll_timeout(priv->phy_grf,
153*4882a593Smuzhiyun 					RK3588_PCIE3PHY_GRF_PHY1_STATUS1,
154*4882a593Smuzhiyun 					reg, RK3588_SRAM_INIT_DONE(reg),
155*4882a593Smuzhiyun 					0, 500);
156*4882a593Smuzhiyun 	if (ret)
157*4882a593Smuzhiyun 		pr_err("%s: lock failed 0x%x, check input refclk and power supply\n",
158*4882a593Smuzhiyun 		       __func__, reg);
159*4882a593Smuzhiyun 	return ret;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun static const struct rockchip_p3phy_ops rk3588_ops = {
163*4882a593Smuzhiyun 	.phy_init = rockchip_p3phy_rk3588_init,
164*4882a593Smuzhiyun };
165*4882a593Smuzhiyun 
rochchip_p3phy_init(struct phy * phy)166*4882a593Smuzhiyun static int rochchip_p3phy_init(struct phy *phy)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	struct rockchip_p3phy_priv *priv = phy_get_drvdata(phy);
169*4882a593Smuzhiyun 	int ret;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	ret = clk_bulk_prepare_enable(priv->num_clks, priv->clks);
172*4882a593Smuzhiyun 	if (ret) {
173*4882a593Smuzhiyun 		pr_err("failed to enable PCIe bulk clks %d\n", ret);
174*4882a593Smuzhiyun 		return ret;
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	reset_control_assert(priv->p30phy);
178*4882a593Smuzhiyun 	udelay(1);
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	if (priv->ops->phy_init) {
181*4882a593Smuzhiyun 		ret = priv->ops->phy_init(priv);
182*4882a593Smuzhiyun 		if (ret)
183*4882a593Smuzhiyun 			clk_bulk_disable_unprepare(priv->num_clks, priv->clks);
184*4882a593Smuzhiyun 	};
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	return ret;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
rochchip_p3phy_exit(struct phy * phy)189*4882a593Smuzhiyun static int rochchip_p3phy_exit(struct phy *phy)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun 	struct rockchip_p3phy_priv *priv = phy_get_drvdata(phy);
192*4882a593Smuzhiyun 	clk_bulk_disable_unprepare(priv->num_clks, priv->clks);
193*4882a593Smuzhiyun 	reset_control_assert(priv->p30phy);
194*4882a593Smuzhiyun 	return 0;
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun static const struct phy_ops rochchip_p3phy_ops = {
198*4882a593Smuzhiyun 	.init = rochchip_p3phy_init,
199*4882a593Smuzhiyun 	.exit = rochchip_p3phy_exit,
200*4882a593Smuzhiyun 	.set_mode = rockchip_p3phy_set_mode,
201*4882a593Smuzhiyun 	.owner = THIS_MODULE,
202*4882a593Smuzhiyun };
203*4882a593Smuzhiyun 
rockchip_p3phy_probe(struct platform_device * pdev)204*4882a593Smuzhiyun static int rockchip_p3phy_probe(struct platform_device *pdev)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	struct phy_provider *phy_provider;
207*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
208*4882a593Smuzhiyun 	struct rockchip_p3phy_priv *priv;
209*4882a593Smuzhiyun 	struct device_node *np = dev->of_node;
210*4882a593Smuzhiyun 	struct resource *res;
211*4882a593Smuzhiyun 	int ret;
212*4882a593Smuzhiyun 	u32 val, reg;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
215*4882a593Smuzhiyun 	if (!priv)
216*4882a593Smuzhiyun 		return -ENOMEM;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
219*4882a593Smuzhiyun 	priv->mmio = devm_ioremap_resource(dev, res);
220*4882a593Smuzhiyun 	if (IS_ERR(priv->mmio)) {
221*4882a593Smuzhiyun 		ret = PTR_ERR(priv->mmio);
222*4882a593Smuzhiyun 		return ret;
223*4882a593Smuzhiyun 	}
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	priv->ops = of_device_get_match_data(&pdev->dev);
226*4882a593Smuzhiyun 	if (!priv->ops) {
227*4882a593Smuzhiyun 		dev_err(&pdev->dev, "no of match data provided\n");
228*4882a593Smuzhiyun 		return -EINVAL;
229*4882a593Smuzhiyun 	}
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	priv->phy_grf = syscon_regmap_lookup_by_phandle(np, "rockchip,phy-grf");
232*4882a593Smuzhiyun 	if (IS_ERR(priv->phy_grf)) {
233*4882a593Smuzhiyun 		dev_err(dev, "failed to find rockchip,phy_grf regmap\n");
234*4882a593Smuzhiyun 		return PTR_ERR(priv->phy_grf);
235*4882a593Smuzhiyun 	}
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	priv->pipe_grf = syscon_regmap_lookup_by_phandle(dev->of_node,
238*4882a593Smuzhiyun 							 "rockchip,pipe-grf");
239*4882a593Smuzhiyun 	if (IS_ERR(priv->pipe_grf))
240*4882a593Smuzhiyun 		dev_info(dev, "failed to find rockchip,pipe_grf regmap\n");
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	ret = device_property_read_u32(dev, "rockchip,pcie30-phymode", &val);
243*4882a593Smuzhiyun 	if (!ret)
244*4882a593Smuzhiyun 		priv->pcie30_phymode = val;
245*4882a593Smuzhiyun 	else
246*4882a593Smuzhiyun 		priv->pcie30_phymode = PHY_MODE_PCIE_AGGREGATION;
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	/* Select correct pcie30_phymode */
249*4882a593Smuzhiyun 	if (priv->pcie30_phymode > 4)
250*4882a593Smuzhiyun 		priv->pcie30_phymode = PHY_MODE_PCIE_AGGREGATION;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	regmap_write(priv->phy_grf, RK3588_PCIE3PHY_GRF_CMN_CON0,
253*4882a593Smuzhiyun 		     (0x7<<16) | priv->pcie30_phymode);
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	/* Set pcie1ln_sel in PHP_GRF_PCIESEL_CON */
256*4882a593Smuzhiyun 	if (!IS_ERR(priv->pipe_grf)) {
257*4882a593Smuzhiyun 		reg = priv->pcie30_phymode & 3;
258*4882a593Smuzhiyun 		if (reg)
259*4882a593Smuzhiyun 			regmap_write(priv->pipe_grf, PHP_GRF_PCIESEL_CON,
260*4882a593Smuzhiyun 				     (reg << 16) | reg);
261*4882a593Smuzhiyun 	};
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	priv->phy = devm_phy_create(dev, NULL, &rochchip_p3phy_ops);
264*4882a593Smuzhiyun 	if (IS_ERR(priv->phy)) {
265*4882a593Smuzhiyun 		dev_err(dev, "failed to create combphy\n");
266*4882a593Smuzhiyun 		return PTR_ERR(priv->phy);
267*4882a593Smuzhiyun 	}
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	priv->p30phy = devm_reset_control_get(dev, "phy");
270*4882a593Smuzhiyun 	if (IS_ERR(priv->p30phy)) {
271*4882a593Smuzhiyun 		dev_warn(dev, "no phy reset control specified\n");
272*4882a593Smuzhiyun 		priv->p30phy = NULL;
273*4882a593Smuzhiyun 	}
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	priv->num_clks = devm_clk_bulk_get_all(dev, &priv->clks);
276*4882a593Smuzhiyun 	if (priv->num_clks < 1)
277*4882a593Smuzhiyun 		return -ENODEV;
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	dev_set_drvdata(dev, priv);
280*4882a593Smuzhiyun 	phy_set_drvdata(priv->phy, priv);
281*4882a593Smuzhiyun 	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
282*4882a593Smuzhiyun 	return PTR_ERR_OR_ZERO(phy_provider);
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun static const struct of_device_id rockchip_p3phy_of_match[] = {
286*4882a593Smuzhiyun 	{ .compatible = "rockchip,rk3568-pcie3-phy", .data = &rk3568_ops },
287*4882a593Smuzhiyun 	{ .compatible = "rockchip,rk3588-pcie3-phy", .data = &rk3588_ops },
288*4882a593Smuzhiyun 	{ },
289*4882a593Smuzhiyun };
290*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, rockchip_p3phy_of_match);
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun static struct platform_driver rockchip_p3phy_driver = {
293*4882a593Smuzhiyun 	.probe	= rockchip_p3phy_probe,
294*4882a593Smuzhiyun 	.driver = {
295*4882a593Smuzhiyun 		.name = "rockchip-snps-pcie3-phy",
296*4882a593Smuzhiyun 		.of_match_table = rockchip_p3phy_of_match,
297*4882a593Smuzhiyun 	},
298*4882a593Smuzhiyun };
299*4882a593Smuzhiyun module_platform_driver(rockchip_p3phy_driver);
300*4882a593Smuzhiyun MODULE_DESCRIPTION("Rockchip Synopsys PCIe 3.0 PHY driver");
301*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
302