This source file includes following definitions.
- pipemux_strap_is_valid
- pipemux_strap_read
- pcie_core_is_for_rc
- sr_pcie_phy_init
- sr_paxc_phy_init
- sr_pcie_phy_xlate
- sr_pcie_phy_probe
1
2
3
4
5
6 #include <linux/clk.h>
7 #include <linux/delay.h>
8 #include <linux/io.h>
9 #include <linux/module.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/of.h>
12 #include <linux/phy/phy.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
15
16
17 #define SR_NR_PCIE_PHYS 9
18 #define SR_PAXC_PHY_IDX (SR_NR_PCIE_PHYS - 1)
19
20 #define PCIE_PIPEMUX_CFG_OFFSET 0x10c
21 #define PCIE_PIPEMUX_SELECT_STRAP 0xf
22
23 #define CDRU_STRAP_DATA_LSW_OFFSET 0x5c
24 #define PCIE_PIPEMUX_SHIFT 19
25 #define PCIE_PIPEMUX_MASK 0xf
26
27 #define MHB_MEM_PW_PAXC_OFFSET 0x1c0
28 #define MHB_PWR_ARR_POWERON 0x8
29 #define MHB_PWR_ARR_POWEROK 0x4
30 #define MHB_PWR_POWERON 0x2
31 #define MHB_PWR_POWEROK 0x1
32 #define MHB_PWR_STATUS_MASK (MHB_PWR_ARR_POWERON | \
33 MHB_PWR_ARR_POWEROK | \
34 MHB_PWR_POWERON | \
35 MHB_PWR_POWEROK)
36
37 struct sr_pcie_phy_core;
38
39
40
41
42
43
44
45
46 struct sr_pcie_phy {
47 struct sr_pcie_phy_core *core;
48 unsigned int index;
49 struct phy *phy;
50 };
51
52
53
54
55
56
57
58
59
60
61
62 struct sr_pcie_phy_core {
63 struct device *dev;
64 void __iomem *base;
65 struct regmap *cdru;
66 struct regmap *mhb;
67 u32 pipemux;
68 struct sr_pcie_phy phys[SR_NR_PCIE_PHYS];
69 };
70
71
72
73
74
75
76
77
78 static const u8 pipemux_table[] = {
79
80 0x00,
81
82 0x80,
83
84 0x00,
85
86 0x81,
87
88 0xc3,
89
90 0xff,
91
92 0xcd,
93
94 0xfd,
95
96 0xf0,
97
98 0xc0,
99
100 0x42,
101
102 0x3c,
103
104 0xfc,
105
106 0x4c,
107 };
108
109
110
111
112 static bool pipemux_strap_is_valid(u32 pipemux)
113 {
114 return !!(pipemux < ARRAY_SIZE(pipemux_table));
115 }
116
117
118
119
120 static u32 pipemux_strap_read(struct sr_pcie_phy_core *core)
121 {
122 u32 pipemux;
123
124
125
126
127
128
129
130 pipemux = readl(core->base + PCIE_PIPEMUX_CFG_OFFSET);
131 pipemux &= PCIE_PIPEMUX_MASK;
132 if (pipemux == PCIE_PIPEMUX_SELECT_STRAP) {
133 regmap_read(core->cdru, CDRU_STRAP_DATA_LSW_OFFSET, &pipemux);
134 pipemux >>= PCIE_PIPEMUX_SHIFT;
135 pipemux &= PCIE_PIPEMUX_MASK;
136 }
137
138 return pipemux;
139 }
140
141
142
143
144
145 static bool pcie_core_is_for_rc(struct sr_pcie_phy *phy)
146 {
147 struct sr_pcie_phy_core *core = phy->core;
148 unsigned int core_idx = phy->index;
149
150 return !!((pipemux_table[core->pipemux] >> core_idx) & 0x1);
151 }
152
153 static int sr_pcie_phy_init(struct phy *p)
154 {
155 struct sr_pcie_phy *phy = phy_get_drvdata(p);
156
157
158
159
160
161
162 if (pcie_core_is_for_rc(phy))
163 return 0;
164
165 return -ENODEV;
166 }
167
168 static int sr_paxc_phy_init(struct phy *p)
169 {
170 struct sr_pcie_phy *phy = phy_get_drvdata(p);
171 struct sr_pcie_phy_core *core = phy->core;
172 unsigned int core_idx = phy->index;
173 u32 val;
174
175 if (core_idx != SR_PAXC_PHY_IDX)
176 return -EINVAL;
177
178 regmap_read(core->mhb, MHB_MEM_PW_PAXC_OFFSET, &val);
179 if ((val & MHB_PWR_STATUS_MASK) != MHB_PWR_STATUS_MASK) {
180 dev_err(core->dev, "PAXC is not powered up\n");
181 return -ENODEV;
182 }
183
184 return 0;
185 }
186
187 static const struct phy_ops sr_pcie_phy_ops = {
188 .init = sr_pcie_phy_init,
189 .owner = THIS_MODULE,
190 };
191
192 static const struct phy_ops sr_paxc_phy_ops = {
193 .init = sr_paxc_phy_init,
194 .owner = THIS_MODULE,
195 };
196
197 static struct phy *sr_pcie_phy_xlate(struct device *dev,
198 struct of_phandle_args *args)
199 {
200 struct sr_pcie_phy_core *core;
201 int phy_idx;
202
203 core = dev_get_drvdata(dev);
204 if (!core)
205 return ERR_PTR(-EINVAL);
206
207 phy_idx = args->args[0];
208
209 if (WARN_ON(phy_idx >= SR_NR_PCIE_PHYS))
210 return ERR_PTR(-ENODEV);
211
212 return core->phys[phy_idx].phy;
213 }
214
215 static int sr_pcie_phy_probe(struct platform_device *pdev)
216 {
217 struct device *dev = &pdev->dev;
218 struct device_node *node = dev->of_node;
219 struct sr_pcie_phy_core *core;
220 struct resource *res;
221 struct phy_provider *provider;
222 unsigned int phy_idx = 0;
223
224 core = devm_kzalloc(dev, sizeof(*core), GFP_KERNEL);
225 if (!core)
226 return -ENOMEM;
227
228 core->dev = dev;
229
230 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
231 core->base = devm_ioremap_resource(core->dev, res);
232 if (IS_ERR(core->base))
233 return PTR_ERR(core->base);
234
235 core->cdru = syscon_regmap_lookup_by_phandle(node, "brcm,sr-cdru");
236 if (IS_ERR(core->cdru)) {
237 dev_err(core->dev, "unable to find CDRU device\n");
238 return PTR_ERR(core->cdru);
239 }
240
241 core->mhb = syscon_regmap_lookup_by_phandle(node, "brcm,sr-mhb");
242 if (IS_ERR(core->mhb)) {
243 dev_err(core->dev, "unable to find MHB device\n");
244 return PTR_ERR(core->mhb);
245 }
246
247
248 core->pipemux = pipemux_strap_read(core);
249 if (!pipemux_strap_is_valid(core->pipemux)) {
250 dev_err(core->dev, "invalid PCIe PIPEMUX strap %u\n",
251 core->pipemux);
252 return -EIO;
253 }
254
255 for (phy_idx = 0; phy_idx < SR_NR_PCIE_PHYS; phy_idx++) {
256 struct sr_pcie_phy *p = &core->phys[phy_idx];
257 const struct phy_ops *ops;
258
259 if (phy_idx == SR_PAXC_PHY_IDX)
260 ops = &sr_paxc_phy_ops;
261 else
262 ops = &sr_pcie_phy_ops;
263
264 p->phy = devm_phy_create(dev, NULL, ops);
265 if (IS_ERR(p->phy)) {
266 dev_err(dev, "failed to create PCIe PHY\n");
267 return PTR_ERR(p->phy);
268 }
269
270 p->core = core;
271 p->index = phy_idx;
272 phy_set_drvdata(p->phy, p);
273 }
274
275 dev_set_drvdata(dev, core);
276
277 provider = devm_of_phy_provider_register(dev, sr_pcie_phy_xlate);
278 if (IS_ERR(provider)) {
279 dev_err(dev, "failed to register PHY provider\n");
280 return PTR_ERR(provider);
281 }
282
283 dev_info(dev, "Stingray PCIe PHY driver initialized\n");
284
285 return 0;
286 }
287
288 static const struct of_device_id sr_pcie_phy_match_table[] = {
289 { .compatible = "brcm,sr-pcie-phy" },
290 { }
291 };
292 MODULE_DEVICE_TABLE(of, sr_pcie_phy_match_table);
293
294 static struct platform_driver sr_pcie_phy_driver = {
295 .driver = {
296 .name = "sr-pcie-phy",
297 .of_match_table = sr_pcie_phy_match_table,
298 },
299 .probe = sr_pcie_phy_probe,
300 };
301 module_platform_driver(sr_pcie_phy_driver);
302
303 MODULE_AUTHOR("Ray Jui <ray.jui@broadcom.com>");
304 MODULE_DESCRIPTION("Broadcom Stingray PCIe PHY driver");
305 MODULE_LICENSE("GPL v2");