1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Rockchip PCIe PHY driver
*
* Copyright (c) 2016 Rockchip, Inc.
* Copyright (c) 2020 Amarula Solutions(India)
*/
#include <common.h>
#include <clk.h>
#include <dm.h>
#include <dm/device_compat.h>
#include <reset.h>
#include <syscon.h>
#include <asm/gpio.h>
#include <asm/io.h>
#include <linux/iopoll.h>
#include <asm/arch-rockchip/clock.h>
#include "pcie_rockchip.h"
DECLARE_GLOBAL_DATA_PTR;
static void phy_wr_cfg(struct rockchip_pcie_phy *phy, u32 addr, u32 data)
{
u32 reg;
reg = HIWORD_UPDATE_MASK(data, PHY_CFG_DATA_MASK, PHY_CFG_DATA_SHIFT);
reg |= HIWORD_UPDATE_MASK(addr, PHY_CFG_ADDR_MASK, PHY_CFG_ADDR_SHIFT);
writel(reg, phy->reg_base + PCIE_PHY_CONF);
udelay(1);
reg = HIWORD_UPDATE_MASK(PHY_CFG_WR_ENABLE,
PHY_CFG_WR_MASK,
PHY_CFG_WR_SHIFT);
writel(reg, phy->reg_base + PCIE_PHY_CONF);
udelay(1);
reg = HIWORD_UPDATE_MASK(PHY_CFG_WR_DISABLE,
PHY_CFG_WR_MASK,
PHY_CFG_WR_SHIFT);
writel(reg, phy->reg_base + PCIE_PHY_CONF);
}
static int rockchip_pcie_phy_power_on(struct rockchip_pcie_phy *phy)
{
int ret = 0;
u32 reg, status;
ret = reset_deassert(&phy->phy_rst);
if (ret) {
dev_err(dev, "failed to assert phy reset\n");
return ret;
}
reg = HIWORD_UPDATE_MASK(PHY_CFG_PLL_LOCK,
PHY_CFG_ADDR_MASK,
PHY_CFG_ADDR_SHIFT);
writel(reg, phy->reg_base + PCIE_PHY_CONF);
reg = HIWORD_UPDATE_MASK(!PHY_LANE_IDLE_OFF,
PHY_LANE_IDLE_MASK,
PHY_LANE_IDLE_A_SHIFT);
writel(reg, phy->reg_base + PCIE_PHY_LANEOFF);
ret = -EINVAL;
ret = readl_poll_sleep_timeout(phy->reg_base + PCIE_PHY_STATUS,
status,
status & PHY_PLL_LOCKED,
20 * 1000,
50);
if (ret) {
dev_err(&phy->dev, "pll lock timeout!\n");
goto err_pll_lock;
}
phy_wr_cfg(phy, PHY_CFG_CLK_TEST, PHY_CFG_SEPE_RATE);
phy_wr_cfg(phy, PHY_CFG_CLK_SCC, PHY_CFG_PLL_100M);
ret = -ETIMEDOUT;
ret = readl_poll_sleep_timeout(phy->reg_base + PCIE_PHY_STATUS,
status,
!(status & PHY_PLL_OUTPUT),
20 * 1000,
50);
if (ret) {
dev_err(&phy->dev, "pll output enable timeout!\n");
goto err_pll_lock;
}
reg = HIWORD_UPDATE_MASK(PHY_CFG_PLL_LOCK,
PHY_CFG_ADDR_MASK,
PHY_CFG_ADDR_SHIFT);
writel(reg, phy->reg_base + PCIE_PHY_CONF);
ret = -EINVAL;
ret = readl_poll_sleep_timeout(phy->reg_base + PCIE_PHY_STATUS,
status,
status & PHY_PLL_LOCKED,
20 * 1000,
50);
if (ret) {
dev_err(&phy->dev, "pll relock timeout!\n");
goto err_pll_lock;
}
return 0;
err_pll_lock:
reset_assert(&phy->phy_rst);
return ret;
}
static int rockchip_pcie_phy_power_off(struct rockchip_pcie_phy *phy)
{
int ret;
u32 reg;
reg = HIWORD_UPDATE_MASK(PHY_LANE_IDLE_OFF,
PHY_LANE_IDLE_MASK,
PHY_LANE_IDLE_A_SHIFT);
writel(reg, phy->reg_base + PCIE_PHY_LANEOFF);
ret = reset_assert(&phy->phy_rst);
if (ret) {
dev_err(dev, "failed to assert phy reset\n");
return ret;
}
return 0;
}
static int rockchip_pcie_phy_init(struct rockchip_pcie_phy *phy)
{
int ret;
ret = clk_enable(&phy->refclk);
if (ret) {
dev_err(dev, "failed to enable refclk clock\n");
return ret;
}
ret = reset_assert(&phy->phy_rst);
if (ret) {
dev_err(dev, "failed to assert phy reset\n");
goto err_reset;
}
return 0;
err_reset:
clk_disable(&phy->refclk);
return ret;
}
static int rockchip_pcie_phy_exit(struct rockchip_pcie_phy *phy)
{
clk_disable(&phy->refclk);
return 0;
}
static struct rockchip_pcie_phy_ops pcie_phy_ops = {
.init = rockchip_pcie_phy_init,
.power_on = rockchip_pcie_phy_power_on,
.power_off = rockchip_pcie_phy_power_off,
.exit = rockchip_pcie_phy_exit,
};
int rockchip_pcie_phy_get(struct udevice *dev)
{
struct rockchip_pcie *priv = dev_get_priv(dev);
struct rockchip_pcie_phy *phy_priv = &priv->rk_phy;
ofnode phy_node;
u32 phandle;
int ret;
phandle = dev_read_u32_default(dev, "phys", 0);
phy_node = ofnode_get_by_phandle(phandle);
if (!ofnode_valid(phy_node)) {
dev_err(dev, "failed to found pcie-phy\n");
return -ENODEV;
}
phy_priv->reg_base = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
ret = clk_get_by_index_nodev(phy_node, 0, &phy_priv->refclk);
if (ret) {
dev_err(dev, "failed to get refclk clock phandle\n");
return ret;
}
ret = reset_get_by_index_nodev(phy_node, 0, &phy_priv->phy_rst);
if (ret) {
dev_err(dev, "failed to get phy reset phandle\n");
return ret;
}
phy_priv->ops = &pcie_phy_ops;
priv->phy = phy_priv;
return 0;
}
|