2 * Rockchip PCIe PHY driver
4 * Copyright (C) 2016 Shawn Lin <shawn.lin@rock-chips.com>
5 * Copyright (C) 2016 ROCKCHIP, Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/clk.h>
18 #include <linux/delay.h>
20 #include <linux/mfd/syscon.h>
21 #include <linux/module.h>
23 #include <linux/of_address.h>
24 #include <linux/of_platform.h>
25 #include <linux/phy/phy.h>
26 #include <linux/platform_device.h>
27 #include <linux/regmap.h>
28 #include <linux/reset.h>
31 * The higher 16-bit of this register is used for write protection
32 * only if BIT(x + 16) set to 1 the BIT(x) can be written.
34 #define HIWORD_UPDATE(val, mask, shift) \
35 ((val) << (shift) | (mask) << ((shift) + 16))
37 #define PHY_MAX_LANE_NUM 4
38 #define PHY_CFG_DATA_SHIFT 7
39 #define PHY_CFG_ADDR_SHIFT 1
40 #define PHY_CFG_DATA_MASK 0xf
41 #define PHY_CFG_ADDR_MASK 0x3f
42 #define PHY_CFG_RD_MASK 0x3ff
43 #define PHY_CFG_WR_ENABLE 1
44 #define PHY_CFG_WR_DISABLE 1
45 #define PHY_CFG_WR_SHIFT 0
46 #define PHY_CFG_WR_MASK 1
47 #define PHY_CFG_PLL_LOCK 0x10
48 #define PHY_CFG_CLK_TEST 0x10
49 #define PHY_CFG_CLK_SCC 0x12
50 #define PHY_CFG_SEPE_RATE BIT(3)
51 #define PHY_CFG_PLL_100M BIT(3)
52 #define PHY_PLL_LOCKED BIT(9)
53 #define PHY_PLL_OUTPUT BIT(10)
54 #define PHY_LANE_A_STATUS 0x30
55 #define PHY_LANE_B_STATUS 0x31
56 #define PHY_LANE_C_STATUS 0x32
57 #define PHY_LANE_D_STATUS 0x33
58 #define PHY_LANE_RX_DET_SHIFT 11
59 #define PHY_LANE_RX_DET_TH 0x1
60 #define PHY_LANE_IDLE_OFF 0x1
61 #define PHY_LANE_IDLE_MASK 0x1
62 #define PHY_LANE_IDLE_A_SHIFT 3
63 #define PHY_LANE_IDLE_B_SHIFT 4
64 #define PHY_LANE_IDLE_C_SHIFT 5
65 #define PHY_LANE_IDLE_D_SHIFT 6
67 struct rockchip_pcie_data
{
68 unsigned int pcie_conf
;
69 unsigned int pcie_status
;
70 unsigned int pcie_laneoff
;
73 struct rockchip_pcie_phy
{
74 struct rockchip_pcie_data
*phy_data
;
75 struct regmap
*reg_base
;
76 struct reset_control
*phy_rst
;
77 struct clk
*clk_pciephy_ref
;
80 static inline void phy_wr_cfg(struct rockchip_pcie_phy
*rk_phy
,
83 regmap_write(rk_phy
->reg_base
, rk_phy
->phy_data
->pcie_conf
,
91 regmap_write(rk_phy
->reg_base
, rk_phy
->phy_data
->pcie_conf
,
92 HIWORD_UPDATE(PHY_CFG_WR_ENABLE
,
96 regmap_write(rk_phy
->reg_base
, rk_phy
->phy_data
->pcie_conf
,
97 HIWORD_UPDATE(PHY_CFG_WR_DISABLE
,
102 static inline u32
phy_rd_cfg(struct rockchip_pcie_phy
*rk_phy
,
107 regmap_write(rk_phy
->reg_base
, rk_phy
->phy_data
->pcie_conf
,
110 PHY_CFG_ADDR_SHIFT
));
111 regmap_read(rk_phy
->reg_base
,
112 rk_phy
->phy_data
->pcie_status
,
117 static int rockchip_pcie_phy_power_off(struct phy
*phy
)
119 struct rockchip_pcie_phy
*rk_phy
= phy_get_drvdata(phy
);
122 err
= reset_control_assert(rk_phy
->phy_rst
);
124 dev_err(&phy
->dev
, "assert phy_rst err %d\n", err
);
131 static int rockchip_pcie_phy_power_on(struct phy
*phy
)
133 struct rockchip_pcie_phy
*rk_phy
= phy_get_drvdata(phy
);
136 unsigned long timeout
;
138 err
= reset_control_deassert(rk_phy
->phy_rst
);
140 dev_err(&phy
->dev
, "deassert phy_rst err %d\n", err
);
144 regmap_write(rk_phy
->reg_base
, rk_phy
->phy_data
->pcie_conf
,
145 HIWORD_UPDATE(PHY_CFG_PLL_LOCK
,
147 PHY_CFG_ADDR_SHIFT
));
150 * No documented timeout value for phy operation below,
151 * so we make it large enough here. And we use loop-break
152 * method which should not be harmful.
154 timeout
= jiffies
+ msecs_to_jiffies(1000);
157 while (time_before(jiffies
, timeout
)) {
158 regmap_read(rk_phy
->reg_base
,
159 rk_phy
->phy_data
->pcie_status
,
161 if (status
& PHY_PLL_LOCKED
) {
162 dev_dbg(&phy
->dev
, "pll locked!\n");
170 dev_err(&phy
->dev
, "pll lock timeout!\n");
174 phy_wr_cfg(rk_phy
, PHY_CFG_CLK_TEST
, PHY_CFG_SEPE_RATE
);
175 phy_wr_cfg(rk_phy
, PHY_CFG_CLK_SCC
, PHY_CFG_PLL_100M
);
178 while (time_before(jiffies
, timeout
)) {
179 regmap_read(rk_phy
->reg_base
,
180 rk_phy
->phy_data
->pcie_status
,
182 if (!(status
& PHY_PLL_OUTPUT
)) {
183 dev_dbg(&phy
->dev
, "pll output enable done!\n");
191 dev_err(&phy
->dev
, "pll output enable timeout!\n");
195 regmap_write(rk_phy
->reg_base
, rk_phy
->phy_data
->pcie_conf
,
196 HIWORD_UPDATE(PHY_CFG_PLL_LOCK
,
198 PHY_CFG_ADDR_SHIFT
));
200 while (time_before(jiffies
, timeout
)) {
201 regmap_read(rk_phy
->reg_base
,
202 rk_phy
->phy_data
->pcie_status
,
204 if (status
& PHY_PLL_LOCKED
) {
205 dev_dbg(&phy
->dev
, "pll relocked!\n");
213 dev_err(&phy
->dev
, "pll relock timeout!\n");
220 reset_control_assert(rk_phy
->phy_rst
);
224 static int rockchip_pcie_phy_init(struct phy
*phy
)
226 struct rockchip_pcie_phy
*rk_phy
= phy_get_drvdata(phy
);
229 err
= clk_prepare_enable(rk_phy
->clk_pciephy_ref
);
231 dev_err(&phy
->dev
, "Fail to enable pcie ref clock.\n");
235 err
= reset_control_assert(rk_phy
->phy_rst
);
237 dev_err(&phy
->dev
, "assert phy_rst err %d\n", err
);
244 clk_disable_unprepare(rk_phy
->clk_pciephy_ref
);
249 static int rockchip_pcie_phy_exit(struct phy
*phy
)
251 struct rockchip_pcie_phy
*rk_phy
= phy_get_drvdata(phy
);
253 clk_disable_unprepare(rk_phy
->clk_pciephy_ref
);
258 static const struct phy_ops ops
= {
259 .init
= rockchip_pcie_phy_init
,
260 .exit
= rockchip_pcie_phy_exit
,
261 .power_on
= rockchip_pcie_phy_power_on
,
262 .power_off
= rockchip_pcie_phy_power_off
,
263 .owner
= THIS_MODULE
,
266 static const struct rockchip_pcie_data rk3399_pcie_data
= {
268 .pcie_status
= 0xe2a4,
269 .pcie_laneoff
= 0xe214,
272 static const struct of_device_id rockchip_pcie_phy_dt_ids
[] = {
274 .compatible
= "rockchip,rk3399-pcie-phy",
275 .data
= &rk3399_pcie_data
,
280 MODULE_DEVICE_TABLE(of
, rockchip_pcie_phy_dt_ids
);
282 static int rockchip_pcie_phy_probe(struct platform_device
*pdev
)
284 struct device
*dev
= &pdev
->dev
;
285 struct rockchip_pcie_phy
*rk_phy
;
286 struct phy
*generic_phy
;
287 struct phy_provider
*phy_provider
;
289 const struct of_device_id
*of_id
;
291 grf
= syscon_node_to_regmap(dev
->parent
->of_node
);
293 dev_err(dev
, "Cannot find GRF syscon\n");
297 rk_phy
= devm_kzalloc(dev
, sizeof(*rk_phy
), GFP_KERNEL
);
301 of_id
= of_match_device(rockchip_pcie_phy_dt_ids
, &pdev
->dev
);
305 rk_phy
->phy_data
= (struct rockchip_pcie_data
*)of_id
->data
;
306 rk_phy
->reg_base
= grf
;
308 rk_phy
->phy_rst
= devm_reset_control_get(dev
, "phy");
309 if (IS_ERR(rk_phy
->phy_rst
)) {
310 if (PTR_ERR(rk_phy
->phy_rst
) != -EPROBE_DEFER
)
312 "missing phy property for reset controller\n");
313 return PTR_ERR(rk_phy
->phy_rst
);
316 rk_phy
->clk_pciephy_ref
= devm_clk_get(dev
, "refclk");
317 if (IS_ERR(rk_phy
->clk_pciephy_ref
)) {
318 dev_err(dev
, "refclk not found.\n");
319 return PTR_ERR(rk_phy
->clk_pciephy_ref
);
322 generic_phy
= devm_phy_create(dev
, dev
->of_node
, &ops
);
323 if (IS_ERR(generic_phy
)) {
324 dev_err(dev
, "failed to create PHY\n");
325 return PTR_ERR(generic_phy
);
328 phy_set_drvdata(generic_phy
, rk_phy
);
329 phy_provider
= devm_of_phy_provider_register(dev
, of_phy_simple_xlate
);
331 return PTR_ERR_OR_ZERO(phy_provider
);
334 static struct platform_driver rockchip_pcie_driver
= {
335 .probe
= rockchip_pcie_phy_probe
,
337 .name
= "rockchip-pcie-phy",
338 .of_match_table
= rockchip_pcie_phy_dt_ids
,
342 module_platform_driver(rockchip_pcie_driver
);
344 MODULE_AUTHOR("Shawn Lin <shawn.lin@rock-chips.com>");
345 MODULE_DESCRIPTION("Rockchip PCIe PHY driver");
346 MODULE_LICENSE("GPL v2");