Debian Codegraph @

drivers/usb/dwc3/dwc3-xilinx.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * dwc3-xilinx.c - Xilinx DWC3 controller specific glue driver
4 *
5 * Authors: Manish Narani <[email protected]>
6 * Anurag Kumar Vulisha <[email protected]>
7 */
8
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/clk.h>
13 #include <linux/of.h>
14 #include <linux/platform_device.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/of_platform.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/reset.h>
20 #include <linux/of_address.h>
21 #include <linux/delay.h>
22 #include <linux/firmware/xlnx-zynqmp.h>
23 #include <linux/io.h>
24
25 #include <linux/phy/phy.h>
26
27 /* USB phy reset mask register */
28 #define XLNX_USB_PHY_RST_EN 0x001C
29 #define XLNX_PHY_RST_MASK 0x1
30
31 /* Xilinx USB 3.0 IP Register */
32 #define XLNX_USB_TRAFFIC_ROUTE_CONFIG 0x005C
33 #define XLNX_USB_TRAFFIC_ROUTE_FPD 0x1
34
35 /* USB 2.0 IP Register */
36 #define XLNX_USB2_TRAFFIC_ROUTE_CONFIG 0x0044
37
38 #define XLNX_USB_FPD_PIPE_CLK 0x7c
39 #define PIPE_CLK_DESELECT 1
40 #define PIPE_CLK_SELECT 0
41 #define XLNX_USB_FPD_POWER_PRSNT 0x80
42 #define FPD_POWER_PRSNT_OPTION BIT(0)
43
44 struct dwc3_xlnx {
45 int num_clocks;
46 struct clk_bulk_data *clks;
47 struct device *dev;
48 void __iomem *regs;
49 int (*pltfm_init)(struct dwc3_xlnx *data);
50 struct phy *usb3_phy;
51 };
52
53 static void dwc3_xlnx_mask_phy_rst(struct dwc3_xlnx *priv_data, bool mask)
54 {
55 u32 reg;
56
57 /*
58 * Enable or disable ULPI PHY reset from USB Controller.
59 * This does not actually reset the phy, but just controls
60 * whether USB controller can or cannot reset ULPI PHY.
61 */
62 reg = readl(priv_data->regs + XLNX_USB_PHY_RST_EN);
63
64 if (mask)
65 reg &= ~XLNX_PHY_RST_MASK;
66 else
67 reg |= XLNX_PHY_RST_MASK;
68
69 writel(reg, priv_data->regs + XLNX_USB_PHY_RST_EN);
70 }
71
72 static void dwc3_xlnx_set_coherency(struct dwc3_xlnx *priv_data, u32 coherency_offset)
73 {
74 struct device *dev = priv_data->dev;
75 u32 reg;
76
77 /*
78 * This routes the USB DMA traffic to go through FPD path instead
79 * of reaching DDR directly. This traffic routing is needed to
80 * make SMMU and CCI work with USB DMA.
81 */
82 if (of_dma_is_coherent(dev->of_node) || device_iommu_mapped(dev)) {
83 reg = readl(priv_data->regs + coherency_offset);
84 reg |= XLNX_USB_TRAFFIC_ROUTE_FPD;
85 writel(reg, priv_data->regs + coherency_offset);
86 }
87 }
88
89 static int dwc3_xlnx_init_versal(struct dwc3_xlnx *priv_data)
90 {
91 struct device *dev = priv_data->dev;
92 struct reset_control *crst;
93 int ret;
94
95 crst = devm_reset_control_get_exclusive(dev, NULL);
96 if (IS_ERR(crst))
97 return dev_err_probe(dev, PTR_ERR(crst), "failed to get reset signal\n");
98
99 dwc3_xlnx_mask_phy_rst(priv_data, false);
100
101 /* Assert and De-assert reset */
102 ret = reset_control_assert(crst);
103 if (ret < 0) {
104 dev_err_probe(dev, ret, "failed to assert Reset\n");
105 return ret;
106 }
107
108 ret = reset_control_deassert(crst);
109 if (ret < 0) {
110 dev_err_probe(dev, ret, "failed to De-assert Reset\n");
111 return ret;
112 }
113
114 dwc3_xlnx_mask_phy_rst(priv_data, true);
115 dwc3_xlnx_set_coherency(priv_data, XLNX_USB2_TRAFFIC_ROUTE_CONFIG);
116
117 return 0;
118 }
119
120 static int dwc3_xlnx_init_zynqmp(struct dwc3_xlnx *priv_data)
121 {
122 struct device *dev = priv_data->dev;
123 struct reset_control *crst, *hibrst, *apbrst;
124 struct gpio_desc *reset_gpio;
125 int ret = 0;
126
127 priv_data->usb3_phy = devm_phy_optional_get(dev, "usb3-phy");
128 if (IS_ERR(priv_data->usb3_phy)) {
129 ret = PTR_ERR(priv_data->usb3_phy);
130 dev_err_probe(dev, ret,
131 "failed to get USB3 PHY\n");
132 goto err;
133 }
134
135 crst = devm_reset_control_get_exclusive(dev, "usb_crst");
136 if (IS_ERR(crst)) {
137 ret = PTR_ERR(crst);
138 dev_err_probe(dev, ret,
139 "failed to get core reset signal\n");
140 goto err;
141 }
142
143 hibrst = devm_reset_control_get_exclusive(dev, "usb_hibrst");
144 if (IS_ERR(hibrst)) {
145 ret = PTR_ERR(hibrst);
146 dev_err_probe(dev, ret,
147 "failed to get hibernation reset signal\n");
148 goto err;
149 }
150
151 apbrst = devm_reset_control_get_exclusive(dev, "usb_apbrst");
152 if (IS_ERR(apbrst)) {
153 ret = PTR_ERR(apbrst);
154 dev_err_probe(dev, ret,
155 "failed to get APB reset signal\n");
156 goto err;
157 }
158
159 /*
160 * Asserting the core resets is not required unless a USB3 PHY is used.
161 * They may also break the configuration if USB3 is actually in use but
162 * the usb3-phy entry is missing from the device tree. Therefore, skip
163 * a full reset cycle and just deassert the resets if the phy is
164 * absent.
165 */
166 if (priv_data->usb3_phy) {
167 ret = reset_control_assert(crst);
168 if (ret < 0) {
169 dev_err(dev, "Failed to assert core reset\n");
170 goto err;
171 }
172
173 ret = reset_control_assert(hibrst);
174 if (ret < 0) {
175 dev_err(dev, "Failed to assert hibernation reset\n");
176 goto err;
177 }
178
179 ret = reset_control_assert(apbrst);
180 if (ret < 0) {
181 dev_err(dev, "Failed to assert APB reset\n");
182 goto err;
183 }
184 }
185
186 ret = phy_init(priv_data->usb3_phy);
187 if (ret < 0) {
188 phy_exit(priv_data->usb3_phy);
189 goto err;
190 }
191
192 ret = reset_control_deassert(apbrst);
193 if (ret < 0) {
194 dev_err(dev, "Failed to release APB reset\n");
195 goto err;
196 }
197
198 if (priv_data->usb3_phy) {
199 /* Set PIPE Power Present signal in FPD Power Present Register*/
200 writel(FPD_POWER_PRSNT_OPTION, priv_data->regs + XLNX_USB_FPD_POWER_PRSNT);
201 /* Set the PIPE Clock Select bit in FPD PIPE Clock register */
202 writel(PIPE_CLK_SELECT, priv_data->regs + XLNX_USB_FPD_PIPE_CLK);
203 } else {
204 /* Deselect the PIPE Clock Select bit in FPD PIPE Clock register */
205 writel(PIPE_CLK_DESELECT, priv_data->regs + XLNX_USB_FPD_PIPE_CLK);
206 }
207
208 ret = reset_control_deassert(crst);
209 if (ret < 0) {
210 dev_err(dev, "Failed to release core reset\n");
211 goto err;
212 }
213
214 ret = reset_control_deassert(hibrst);
215 if (ret < 0) {
216 dev_err(dev, "Failed to release hibernation reset\n");
217 goto err;
218 }
219
220 ret = phy_power_on(priv_data->usb3_phy);
221 if (ret < 0) {
222 phy_exit(priv_data->usb3_phy);
223 goto err;
224 }
225
226 /* ulpi reset via gpio-modepin or gpio-framework driver */
227 reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
228 if (IS_ERR(reset_gpio)) {
229 return dev_err_probe(dev, PTR_ERR(reset_gpio),
230 "Failed to request reset GPIO\n");
231 }
232
233 if (reset_gpio) {
234 usleep_range(5000, 10000);
235 gpiod_set_value_cansleep(reset_gpio, 0);
236 usleep_range(5000, 10000);
237 }
238
239 dwc3_xlnx_set_coherency(priv_data, XLNX_USB_TRAFFIC_ROUTE_CONFIG);
240 err:
241 return ret;
242 }
243
244 static const struct of_device_id dwc3_xlnx_of_match[] = {
245 {
246 .compatible = "xlnx,zynqmp-dwc3",
247 .data = &dwc3_xlnx_init_zynqmp,
248 },
249 {
250 .compatible = "xlnx,versal-dwc3",
251 .data = &dwc3_xlnx_init_versal,
252 },
253 { /* Sentinel */ }
254 };
255 MODULE_DEVICE_TABLE(of, dwc3_xlnx_of_match);
256
257 static int dwc3_set_swnode(struct device *dev)
258 {
259 struct device_node *np = dev->of_node, *dwc3_np;
260 struct property_entry props[2];
261 int prop_idx = 0, ret = 0;
262
263 dwc3_np = of_get_compatible_child(np, "snps,dwc3");
264 if (!dwc3_np) {
265 ret = -ENODEV;
266 dev_err(dev, "failed to find dwc3 core child\n");
267 return ret;
268 }
269
270 memset(props, 0, sizeof(struct property_entry) * ARRAY_SIZE(props));
271 if (of_dma_is_coherent(dwc3_np))
272 props[prop_idx++] = PROPERTY_ENTRY_U16("snps,gsbuscfg0-reqinfo",
273 0xffff);
274 of_node_put(dwc3_np);
275
276 if (prop_idx)
277 ret = device_create_managed_software_node(dev, props, NULL);
278
279 return ret;
280 }
281
282 static int dwc3_xlnx_probe(struct platform_device *pdev)
283 {
284 struct dwc3_xlnx *priv_data;
285 struct device *dev = &pdev->dev;
286 struct device_node *np = dev->of_node;
287 const struct of_device_id *match;
288 void __iomem *regs;
289 int ret;
290
291 priv_data = devm_kzalloc(dev, sizeof(*priv_data), GFP_KERNEL);
292 if (!priv_data)
293 return -ENOMEM;
294
295 regs = devm_platform_ioremap_resource(pdev, 0);
296 if (IS_ERR(regs))
297 return dev_err_probe(dev, PTR_ERR(regs), "failed to map registers\n");
298
299 match = of_match_node(dwc3_xlnx_of_match, pdev->dev.of_node);
300
301 priv_data->pltfm_init = match->data;
302 priv_data->regs = regs;
303 priv_data->dev = dev;
304
305 platform_set_drvdata(pdev, priv_data);
306
307 ret = devm_clk_bulk_get_all(priv_data->dev, &priv_data->clks);
308 if (ret < 0)
309 return ret;
310
311 priv_data->num_clocks = ret;
312
313 ret = clk_bulk_prepare_enable(priv_data->num_clocks, priv_data->clks);
314 if (ret)
315 return ret;
316
317 ret = priv_data->pltfm_init(priv_data);
318 if (ret)
319 goto err_clk_put;
320
321 ret = dwc3_set_swnode(dev);
322 if (ret)
323 goto err_clk_put;
324
325 ret = of_platform_populate(np, NULL, NULL, dev);
326 if (ret)
327 goto err_clk_put;
328
329 pm_runtime_set_active(dev);
330 ret = devm_pm_runtime_enable(dev);
331 if (ret < 0)
332 goto err_pm_set_suspended;
333
334 pm_suspend_ignore_children(dev, false);
335 ret = pm_runtime_resume_and_get(dev);
336 if (ret < 0)
337 goto err_pm_set_suspended;
338
339 return 0;
340
341 err_pm_set_suspended:
342 of_platform_depopulate(dev);
343 pm_runtime_set_suspended(dev);
344
345 err_clk_put:
346 clk_bulk_disable_unprepare(priv_data->num_clocks, priv_data->clks);
347
348 return ret;
349 }
350
351 static void dwc3_xlnx_remove(struct platform_device *pdev)
352 {
353 struct dwc3_xlnx *priv_data = platform_get_drvdata(pdev);
354 struct device *dev = &pdev->dev;
355
356 of_platform_depopulate(dev);
357
358 clk_bulk_disable_unprepare(priv_data->num_clocks, priv_data->clks);
359 priv_data->num_clocks = 0;
360
361 pm_runtime_put_noidle(dev);
362 pm_runtime_set_suspended(dev);
363 }
364
365 static int __maybe_unused dwc3_xlnx_runtime_suspend(struct device *dev)
366 {
367 struct dwc3_xlnx *priv_data = dev_get_drvdata(dev);
368
369 clk_bulk_disable(priv_data->num_clocks, priv_data->clks);
370
371 return 0;
372 }
373
374 static int __maybe_unused dwc3_xlnx_runtime_resume(struct device *dev)
375 {
376 struct dwc3_xlnx *priv_data = dev_get_drvdata(dev);
377
378 return clk_bulk_enable(priv_data->num_clocks, priv_data->clks);
379 }
380
381 static int __maybe_unused dwc3_xlnx_runtime_idle(struct device *dev)
382 {
383 pm_runtime_autosuspend(dev);
384
385 return 0;
386 }
387
388 static int __maybe_unused dwc3_xlnx_suspend(struct device *dev)
389 {
390 struct dwc3_xlnx *priv_data = dev_get_drvdata(dev);
391
392 phy_exit(priv_data->usb3_phy);
393
394 /* Disable the clocks */
395 clk_bulk_disable(priv_data->num_clocks, priv_data->clks);
396
397 return 0;
398 }
399
400 static int __maybe_unused dwc3_xlnx_resume(struct device *dev)
401 {
402 struct dwc3_xlnx *priv_data = dev_get_drvdata(dev);
403 int ret;
404
405 ret = clk_bulk_enable(priv_data->num_clocks, priv_data->clks);
406 if (ret)
407 return ret;
408
409 ret = phy_init(priv_data->usb3_phy);
410 if (ret < 0)
411 return ret;
412
413 ret = phy_power_on(priv_data->usb3_phy);
414 if (ret < 0) {
415 phy_exit(priv_data->usb3_phy);
416 return ret;
417 }
418
419 return 0;
420 }
421
422 static const struct dev_pm_ops dwc3_xlnx_dev_pm_ops = {
423 SET_SYSTEM_SLEEP_PM_OPS(dwc3_xlnx_suspend, dwc3_xlnx_resume)
424 SET_RUNTIME_PM_OPS(dwc3_xlnx_runtime_suspend,
425 dwc3_xlnx_runtime_resume, dwc3_xlnx_runtime_idle)
426 };
427
428 static struct platform_driver dwc3_xlnx_driver = {
429 .probe = dwc3_xlnx_probe,
430 .remove = dwc3_xlnx_remove,
431 .shutdown = dwc3_xlnx_remove,
432 .driver = {
433 .name = "dwc3-xilinx",
434 .of_match_table = dwc3_xlnx_of_match,
435 .pm = &dwc3_xlnx_dev_pm_ops,
436 },
437 };
438
439 module_platform_driver(dwc3_xlnx_driver);
440
441 MODULE_LICENSE("GPL v2");
442 MODULE_DESCRIPTION("Xilinx DWC3 controller specific glue driver");
443 MODULE_AUTHOR("Manish Narani <[email protected]>");
444 MODULE_AUTHOR("Anurag Kumar Vulisha <[email protected]>");