xref: /openbmc/linux/drivers/usb/host/xhci-plat.c (revision d47a97bd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * xhci-plat.c - xHCI host controller driver platform Bus Glue.
4  *
5  * Copyright (C) 2012 Texas Instruments Incorporated - https://www.ti.com
6  * Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
7  *
8  * A lot of code borrowed from the Linux xHCI driver.
9  */
10 
11 #include <linux/clk.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/module.h>
14 #include <linux/pci.h>
15 #include <linux/of.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/usb/phy.h>
19 #include <linux/slab.h>
20 #include <linux/acpi.h>
21 #include <linux/usb/of.h>
22 #include <linux/reset.h>
23 
24 #include "xhci.h"
25 #include "xhci-plat.h"
26 #include "xhci-mvebu.h"
27 
28 static struct hc_driver __read_mostly xhci_plat_hc_driver;
29 
30 static int xhci_plat_setup(struct usb_hcd *hcd);
31 static int xhci_plat_start(struct usb_hcd *hcd);
32 
33 static const struct xhci_driver_overrides xhci_plat_overrides __initconst = {
34 	.extra_priv_size = sizeof(struct xhci_plat_priv),
35 	.reset = xhci_plat_setup,
36 	.start = xhci_plat_start,
37 };
38 
39 static void xhci_priv_plat_start(struct usb_hcd *hcd)
40 {
41 	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
42 
43 	if (priv->plat_start)
44 		priv->plat_start(hcd);
45 }
46 
47 static int xhci_priv_init_quirk(struct usb_hcd *hcd)
48 {
49 	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
50 
51 	if (!priv->init_quirk)
52 		return 0;
53 
54 	return priv->init_quirk(hcd);
55 }
56 
57 static int xhci_priv_suspend_quirk(struct usb_hcd *hcd)
58 {
59 	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
60 
61 	if (!priv->suspend_quirk)
62 		return 0;
63 
64 	return priv->suspend_quirk(hcd);
65 }
66 
67 static int xhci_priv_resume_quirk(struct usb_hcd *hcd)
68 {
69 	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
70 
71 	if (!priv->resume_quirk)
72 		return 0;
73 
74 	return priv->resume_quirk(hcd);
75 }
76 
77 static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
78 {
79 	struct xhci_plat_priv *priv = xhci_to_priv(xhci);
80 
81 	/*
82 	 * As of now platform drivers don't provide MSI support so we ensure
83 	 * here that the generic code does not try to make a pci_dev from our
84 	 * dev struct in order to setup MSI
85 	 */
86 	xhci->quirks |= XHCI_PLAT | priv->quirks;
87 }
88 
89 /* called during probe() after chip reset completes */
90 static int xhci_plat_setup(struct usb_hcd *hcd)
91 {
92 	int ret;
93 
94 
95 	ret = xhci_priv_init_quirk(hcd);
96 	if (ret)
97 		return ret;
98 
99 	return xhci_gen_setup(hcd, xhci_plat_quirks);
100 }
101 
102 static int xhci_plat_start(struct usb_hcd *hcd)
103 {
104 	xhci_priv_plat_start(hcd);
105 	return xhci_run(hcd);
106 }
107 
108 #ifdef CONFIG_OF
109 static const struct xhci_plat_priv xhci_plat_marvell_armada = {
110 	.init_quirk = xhci_mvebu_mbus_init_quirk,
111 };
112 
113 static const struct xhci_plat_priv xhci_plat_marvell_armada3700 = {
114 	.init_quirk = xhci_mvebu_a3700_init_quirk,
115 };
116 
117 static const struct xhci_plat_priv xhci_plat_brcm = {
118 	.quirks = XHCI_RESET_ON_RESUME | XHCI_SUSPEND_RESUME_CLKS,
119 };
120 
121 static const struct of_device_id usb_xhci_of_match[] = {
122 	{
123 		.compatible = "generic-xhci",
124 	}, {
125 		.compatible = "xhci-platform",
126 	}, {
127 		.compatible = "marvell,armada-375-xhci",
128 		.data = &xhci_plat_marvell_armada,
129 	}, {
130 		.compatible = "marvell,armada-380-xhci",
131 		.data = &xhci_plat_marvell_armada,
132 	}, {
133 		.compatible = "marvell,armada3700-xhci",
134 		.data = &xhci_plat_marvell_armada3700,
135 	}, {
136 		.compatible = "brcm,xhci-brcm-v2",
137 		.data = &xhci_plat_brcm,
138 	}, {
139 		.compatible = "brcm,bcm7445-xhci",
140 		.data = &xhci_plat_brcm,
141 	},
142 	{},
143 };
144 MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
145 #endif
146 
147 int xhci_plat_probe(struct platform_device *pdev, struct device *sysdev, const struct xhci_plat_priv *priv_match)
148 {
149 	const struct hc_driver	*driver;
150 	struct device		*tmpdev;
151 	struct xhci_hcd		*xhci;
152 	struct resource         *res;
153 	struct usb_hcd		*hcd, *usb3_hcd;
154 	int			ret;
155 	int			irq;
156 	struct xhci_plat_priv	*priv = NULL;
157 
158 
159 	if (usb_disabled())
160 		return -ENODEV;
161 
162 	driver = &xhci_plat_hc_driver;
163 
164 	irq = platform_get_irq(pdev, 0);
165 	if (irq < 0)
166 		return irq;
167 
168 	if (!sysdev)
169 		sysdev = &pdev->dev;
170 
171 	ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
172 	if (ret)
173 		return ret;
174 
175 	pm_runtime_set_active(&pdev->dev);
176 	pm_runtime_enable(&pdev->dev);
177 	pm_runtime_get_noresume(&pdev->dev);
178 
179 	hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
180 			       dev_name(&pdev->dev), NULL);
181 	if (!hcd) {
182 		ret = -ENOMEM;
183 		goto disable_runtime;
184 	}
185 
186 	hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
187 	if (IS_ERR(hcd->regs)) {
188 		ret = PTR_ERR(hcd->regs);
189 		goto put_hcd;
190 	}
191 
192 	hcd->rsrc_start = res->start;
193 	hcd->rsrc_len = resource_size(res);
194 
195 	xhci = hcd_to_xhci(hcd);
196 
197 	xhci->allow_single_roothub = 1;
198 
199 	/*
200 	 * Not all platforms have clks so it is not an error if the
201 	 * clock do not exist.
202 	 */
203 	xhci->reg_clk = devm_clk_get_optional(&pdev->dev, "reg");
204 	if (IS_ERR(xhci->reg_clk)) {
205 		ret = PTR_ERR(xhci->reg_clk);
206 		goto put_hcd;
207 	}
208 
209 	xhci->clk = devm_clk_get_optional(&pdev->dev, NULL);
210 	if (IS_ERR(xhci->clk)) {
211 		ret = PTR_ERR(xhci->clk);
212 		goto put_hcd;
213 	}
214 
215 	xhci->reset = devm_reset_control_array_get_optional_shared(&pdev->dev);
216 	if (IS_ERR(xhci->reset)) {
217 		ret = PTR_ERR(xhci->reset);
218 		goto put_hcd;
219 	}
220 
221 	ret = reset_control_deassert(xhci->reset);
222 	if (ret)
223 		goto put_hcd;
224 
225 	ret = clk_prepare_enable(xhci->reg_clk);
226 	if (ret)
227 		goto err_reset;
228 
229 	ret = clk_prepare_enable(xhci->clk);
230 	if (ret)
231 		goto disable_reg_clk;
232 
233 	if (priv_match) {
234 		priv = hcd_to_xhci_priv(hcd);
235 		/* Just copy data for now */
236 		*priv = *priv_match;
237 	}
238 
239 	device_set_wakeup_capable(&pdev->dev, true);
240 
241 	xhci->main_hcd = hcd;
242 
243 	/* imod_interval is the interrupt moderation value in nanoseconds. */
244 	xhci->imod_interval = 40000;
245 
246 	/* Iterate over all parent nodes for finding quirks */
247 	for (tmpdev = &pdev->dev; tmpdev; tmpdev = tmpdev->parent) {
248 
249 		if (device_property_read_bool(tmpdev, "usb2-lpm-disable"))
250 			xhci->quirks |= XHCI_HW_LPM_DISABLE;
251 
252 		if (device_property_read_bool(tmpdev, "usb3-lpm-capable"))
253 			xhci->quirks |= XHCI_LPM_SUPPORT;
254 
255 		if (device_property_read_bool(tmpdev, "quirk-broken-port-ped"))
256 			xhci->quirks |= XHCI_BROKEN_PORT_PED;
257 
258 		device_property_read_u32(tmpdev, "imod-interval-ns",
259 					 &xhci->imod_interval);
260 	}
261 
262 	hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev, "usb-phy", 0);
263 	if (IS_ERR(hcd->usb_phy)) {
264 		ret = PTR_ERR(hcd->usb_phy);
265 		if (ret == -EPROBE_DEFER)
266 			goto disable_clk;
267 		hcd->usb_phy = NULL;
268 	} else {
269 		ret = usb_phy_init(hcd->usb_phy);
270 		if (ret)
271 			goto disable_clk;
272 	}
273 
274 	hcd->tpl_support = of_usb_host_tpl_support(sysdev->of_node);
275 
276 	if (priv && (priv->quirks & XHCI_SKIP_PHY_INIT))
277 		hcd->skip_phy_initialization = 1;
278 
279 	if (priv && (priv->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK))
280 		xhci->quirks |= XHCI_SG_TRB_CACHE_SIZE_QUIRK;
281 
282 	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
283 	if (ret)
284 		goto disable_usb_phy;
285 
286 	if (!xhci_has_one_roothub(xhci)) {
287 		xhci->shared_hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
288 						    dev_name(&pdev->dev), hcd);
289 		if (!xhci->shared_hcd) {
290 			ret = -ENOMEM;
291 			goto dealloc_usb2_hcd;
292 		}
293 
294 		xhci->shared_hcd->tpl_support = hcd->tpl_support;
295 	}
296 
297 	usb3_hcd = xhci_get_usb3_hcd(xhci);
298 	if (usb3_hcd && HCC_MAX_PSA(xhci->hcc_params) >= 4)
299 		usb3_hcd->can_do_streams = 1;
300 
301 	if (xhci->shared_hcd) {
302 		ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
303 		if (ret)
304 			goto put_usb3_hcd;
305 	}
306 
307 	device_enable_async_suspend(&pdev->dev);
308 	pm_runtime_put_noidle(&pdev->dev);
309 
310 	/*
311 	 * Prevent runtime pm from being on as default, users should enable
312 	 * runtime pm using power/control in sysfs.
313 	 */
314 	pm_runtime_forbid(&pdev->dev);
315 
316 	return 0;
317 
318 
319 put_usb3_hcd:
320 	usb_put_hcd(xhci->shared_hcd);
321 
322 dealloc_usb2_hcd:
323 	usb_remove_hcd(hcd);
324 
325 disable_usb_phy:
326 	usb_phy_shutdown(hcd->usb_phy);
327 
328 disable_clk:
329 	clk_disable_unprepare(xhci->clk);
330 
331 disable_reg_clk:
332 	clk_disable_unprepare(xhci->reg_clk);
333 
334 err_reset:
335 	reset_control_assert(xhci->reset);
336 
337 put_hcd:
338 	usb_put_hcd(hcd);
339 
340 disable_runtime:
341 	pm_runtime_put_noidle(&pdev->dev);
342 	pm_runtime_disable(&pdev->dev);
343 
344 	return ret;
345 }
346 EXPORT_SYMBOL_GPL(xhci_plat_probe);
347 
348 static int xhci_generic_plat_probe(struct platform_device *pdev)
349 {
350 	const struct xhci_plat_priv *priv_match;
351 	struct device *sysdev;
352 	int ret;
353 
354 	/*
355 	 * sysdev must point to a device that is known to the system firmware
356 	 * or PCI hardware. We handle these three cases here:
357 	 * 1. xhci_plat comes from firmware
358 	 * 2. xhci_plat is child of a device from firmware (dwc3-plat)
359 	 * 3. xhci_plat is grandchild of a pci device (dwc3-pci)
360 	 */
361 	for (sysdev = &pdev->dev; sysdev; sysdev = sysdev->parent) {
362 		if (is_of_node(sysdev->fwnode) ||
363 			is_acpi_device_node(sysdev->fwnode))
364 			break;
365 #ifdef CONFIG_PCI
366 		else if (sysdev->bus == &pci_bus_type)
367 			break;
368 #endif
369 	}
370 
371 	if (!sysdev)
372 		sysdev = &pdev->dev;
373 
374 	if (WARN_ON(!sysdev->dma_mask)) {
375 		/* Platform did not initialize dma_mask */
376 		ret = dma_coerce_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
377 		if (ret)
378 			return ret;
379 	}
380 
381 	if (pdev->dev.of_node)
382 		priv_match = of_device_get_match_data(&pdev->dev);
383 	else
384 		priv_match = dev_get_platdata(&pdev->dev);
385 
386 	return xhci_plat_probe(pdev, sysdev, priv_match);
387 }
388 
389 int xhci_plat_remove(struct platform_device *dev)
390 {
391 	struct usb_hcd	*hcd = platform_get_drvdata(dev);
392 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
393 	struct clk *clk = xhci->clk;
394 	struct clk *reg_clk = xhci->reg_clk;
395 	struct usb_hcd *shared_hcd = xhci->shared_hcd;
396 
397 	pm_runtime_get_sync(&dev->dev);
398 	xhci->xhc_state |= XHCI_STATE_REMOVING;
399 
400 	if (shared_hcd) {
401 		usb_remove_hcd(shared_hcd);
402 		xhci->shared_hcd = NULL;
403 	}
404 
405 	usb_phy_shutdown(hcd->usb_phy);
406 
407 	usb_remove_hcd(hcd);
408 
409 	if (shared_hcd)
410 		usb_put_hcd(shared_hcd);
411 
412 	clk_disable_unprepare(clk);
413 	clk_disable_unprepare(reg_clk);
414 	reset_control_assert(xhci->reset);
415 	usb_put_hcd(hcd);
416 
417 	pm_runtime_disable(&dev->dev);
418 	pm_runtime_put_noidle(&dev->dev);
419 	pm_runtime_set_suspended(&dev->dev);
420 
421 	return 0;
422 }
423 EXPORT_SYMBOL_GPL(xhci_plat_remove);
424 
425 static int __maybe_unused xhci_plat_suspend(struct device *dev)
426 {
427 	struct usb_hcd	*hcd = dev_get_drvdata(dev);
428 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
429 	int ret;
430 
431 	if (pm_runtime_suspended(dev))
432 		pm_runtime_resume(dev);
433 
434 	ret = xhci_priv_suspend_quirk(hcd);
435 	if (ret)
436 		return ret;
437 	/*
438 	 * xhci_suspend() needs `do_wakeup` to know whether host is allowed
439 	 * to do wakeup during suspend.
440 	 */
441 	ret = xhci_suspend(xhci, device_may_wakeup(dev));
442 	if (ret)
443 		return ret;
444 
445 	if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
446 		clk_disable_unprepare(xhci->clk);
447 		clk_disable_unprepare(xhci->reg_clk);
448 	}
449 
450 	return 0;
451 }
452 
453 static int __maybe_unused xhci_plat_resume(struct device *dev)
454 {
455 	struct usb_hcd	*hcd = dev_get_drvdata(dev);
456 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
457 	int ret;
458 
459 	if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
460 		clk_prepare_enable(xhci->clk);
461 		clk_prepare_enable(xhci->reg_clk);
462 	}
463 
464 	ret = xhci_priv_resume_quirk(hcd);
465 	if (ret)
466 		return ret;
467 
468 	ret = xhci_resume(xhci, 0);
469 	if (ret)
470 		return ret;
471 
472 	pm_runtime_disable(dev);
473 	pm_runtime_set_active(dev);
474 	pm_runtime_enable(dev);
475 
476 	return 0;
477 }
478 
479 static int __maybe_unused xhci_plat_runtime_suspend(struct device *dev)
480 {
481 	struct usb_hcd  *hcd = dev_get_drvdata(dev);
482 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
483 	int ret;
484 
485 	ret = xhci_priv_suspend_quirk(hcd);
486 	if (ret)
487 		return ret;
488 
489 	return xhci_suspend(xhci, true);
490 }
491 
492 static int __maybe_unused xhci_plat_runtime_resume(struct device *dev)
493 {
494 	struct usb_hcd  *hcd = dev_get_drvdata(dev);
495 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
496 
497 	return xhci_resume(xhci, 0);
498 }
499 
500 const struct dev_pm_ops xhci_plat_pm_ops = {
501 	SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
502 
503 	SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend,
504 			   xhci_plat_runtime_resume,
505 			   NULL)
506 };
507 EXPORT_SYMBOL_GPL(xhci_plat_pm_ops);
508 
509 #ifdef CONFIG_ACPI
510 static const struct acpi_device_id usb_xhci_acpi_match[] = {
511 	/* XHCI-compliant USB Controller */
512 	{ "PNP0D10", },
513 	{ }
514 };
515 MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
516 #endif
517 
518 static struct platform_driver usb_generic_xhci_driver = {
519 	.probe	= xhci_generic_plat_probe,
520 	.remove	= xhci_plat_remove,
521 	.shutdown = usb_hcd_platform_shutdown,
522 	.driver	= {
523 		.name = "xhci-hcd",
524 		.pm = &xhci_plat_pm_ops,
525 		.of_match_table = of_match_ptr(usb_xhci_of_match),
526 		.acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
527 	},
528 };
529 MODULE_ALIAS("platform:xhci-hcd");
530 
531 static int __init xhci_plat_init(void)
532 {
533 	xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
534 	return platform_driver_register(&usb_generic_xhci_driver);
535 }
536 module_init(xhci_plat_init);
537 
538 static void __exit xhci_plat_exit(void)
539 {
540 	platform_driver_unregister(&usb_generic_xhci_driver);
541 }
542 module_exit(xhci_plat_exit);
543 
544 MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
545 MODULE_LICENSE("GPL");
546