xref: /linux/drivers/usb/host/xhci-plat.c (revision e8fa2dd9e2bc12f8386f3d5b897d8ea025ee8b7b)
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->usb_phy = devm_usb_get_phy_by_phandle(sysdev,
295 			    "usb-phy", 1);
296 		if (IS_ERR(xhci->shared_hcd->usb_phy)) {
297 			xhci->shared_hcd->usb_phy = NULL;
298 		} else {
299 			ret = usb_phy_init(xhci->shared_hcd->usb_phy);
300 			if (ret)
301 				dev_err(sysdev, "%s init usb3phy fail (ret=%d)\n",
302 					    __func__, ret);
303 		}
304 
305 		xhci->shared_hcd->tpl_support = hcd->tpl_support;
306 	}
307 
308 	usb3_hcd = xhci_get_usb3_hcd(xhci);
309 	if (usb3_hcd && HCC_MAX_PSA(xhci->hcc_params) >= 4)
310 		usb3_hcd->can_do_streams = 1;
311 
312 	if (xhci->shared_hcd) {
313 		ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
314 		if (ret)
315 			goto put_usb3_hcd;
316 	}
317 
318 	device_enable_async_suspend(&pdev->dev);
319 	pm_runtime_put_noidle(&pdev->dev);
320 
321 	/*
322 	 * Prevent runtime pm from being on as default, users should enable
323 	 * runtime pm using power/control in sysfs.
324 	 */
325 	pm_runtime_forbid(&pdev->dev);
326 
327 	return 0;
328 
329 
330 put_usb3_hcd:
331 	usb_put_hcd(xhci->shared_hcd);
332 
333 dealloc_usb2_hcd:
334 	usb_remove_hcd(hcd);
335 
336 disable_usb_phy:
337 	usb_phy_shutdown(hcd->usb_phy);
338 
339 disable_clk:
340 	clk_disable_unprepare(xhci->clk);
341 
342 disable_reg_clk:
343 	clk_disable_unprepare(xhci->reg_clk);
344 
345 err_reset:
346 	reset_control_assert(xhci->reset);
347 
348 put_hcd:
349 	usb_put_hcd(hcd);
350 
351 disable_runtime:
352 	pm_runtime_put_noidle(&pdev->dev);
353 	pm_runtime_disable(&pdev->dev);
354 
355 	return ret;
356 }
357 EXPORT_SYMBOL_GPL(xhci_plat_probe);
358 
359 static int xhci_generic_plat_probe(struct platform_device *pdev)
360 {
361 	const struct xhci_plat_priv *priv_match;
362 	struct device *sysdev;
363 	int ret;
364 
365 	/*
366 	 * sysdev must point to a device that is known to the system firmware
367 	 * or PCI hardware. We handle these three cases here:
368 	 * 1. xhci_plat comes from firmware
369 	 * 2. xhci_plat is child of a device from firmware (dwc3-plat)
370 	 * 3. xhci_plat is grandchild of a pci device (dwc3-pci)
371 	 */
372 	for (sysdev = &pdev->dev; sysdev; sysdev = sysdev->parent) {
373 		if (is_of_node(sysdev->fwnode) ||
374 			is_acpi_device_node(sysdev->fwnode))
375 			break;
376 		else if (dev_is_pci(sysdev))
377 			break;
378 	}
379 
380 	if (!sysdev)
381 		sysdev = &pdev->dev;
382 
383 	if (WARN_ON(!sysdev->dma_mask)) {
384 		/* Platform did not initialize dma_mask */
385 		ret = dma_coerce_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
386 		if (ret)
387 			return ret;
388 	}
389 
390 	if (pdev->dev.of_node)
391 		priv_match = of_device_get_match_data(&pdev->dev);
392 	else
393 		priv_match = dev_get_platdata(&pdev->dev);
394 
395 	return xhci_plat_probe(pdev, sysdev, priv_match);
396 }
397 
398 int xhci_plat_remove(struct platform_device *dev)
399 {
400 	struct usb_hcd	*hcd = platform_get_drvdata(dev);
401 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
402 	struct clk *clk = xhci->clk;
403 	struct clk *reg_clk = xhci->reg_clk;
404 	struct usb_hcd *shared_hcd = xhci->shared_hcd;
405 
406 	pm_runtime_get_sync(&dev->dev);
407 	xhci->xhc_state |= XHCI_STATE_REMOVING;
408 
409 	if (shared_hcd) {
410 		usb_remove_hcd(shared_hcd);
411 		xhci->shared_hcd = NULL;
412 	}
413 
414 	usb_phy_shutdown(hcd->usb_phy);
415 
416 	usb_remove_hcd(hcd);
417 
418 	if (shared_hcd)
419 		usb_put_hcd(shared_hcd);
420 
421 	clk_disable_unprepare(clk);
422 	clk_disable_unprepare(reg_clk);
423 	reset_control_assert(xhci->reset);
424 	usb_put_hcd(hcd);
425 
426 	pm_runtime_disable(&dev->dev);
427 	pm_runtime_put_noidle(&dev->dev);
428 	pm_runtime_set_suspended(&dev->dev);
429 
430 	return 0;
431 }
432 EXPORT_SYMBOL_GPL(xhci_plat_remove);
433 
434 static int __maybe_unused xhci_plat_suspend(struct device *dev)
435 {
436 	struct usb_hcd	*hcd = dev_get_drvdata(dev);
437 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
438 	int ret;
439 
440 	if (pm_runtime_suspended(dev))
441 		pm_runtime_resume(dev);
442 
443 	ret = xhci_priv_suspend_quirk(hcd);
444 	if (ret)
445 		return ret;
446 	/*
447 	 * xhci_suspend() needs `do_wakeup` to know whether host is allowed
448 	 * to do wakeup during suspend.
449 	 */
450 	ret = xhci_suspend(xhci, device_may_wakeup(dev));
451 	if (ret)
452 		return ret;
453 
454 	if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
455 		clk_disable_unprepare(xhci->clk);
456 		clk_disable_unprepare(xhci->reg_clk);
457 	}
458 
459 	return 0;
460 }
461 
462 static int __maybe_unused xhci_plat_resume(struct device *dev)
463 {
464 	struct usb_hcd	*hcd = dev_get_drvdata(dev);
465 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
466 	int ret;
467 
468 	if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
469 		clk_prepare_enable(xhci->clk);
470 		clk_prepare_enable(xhci->reg_clk);
471 	}
472 
473 	ret = xhci_priv_resume_quirk(hcd);
474 	if (ret)
475 		return ret;
476 
477 	ret = xhci_resume(xhci, PMSG_RESUME);
478 	if (ret)
479 		return ret;
480 
481 	pm_runtime_disable(dev);
482 	pm_runtime_set_active(dev);
483 	pm_runtime_enable(dev);
484 
485 	return 0;
486 }
487 
488 static int __maybe_unused xhci_plat_runtime_suspend(struct device *dev)
489 {
490 	struct usb_hcd  *hcd = dev_get_drvdata(dev);
491 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
492 	int ret;
493 
494 	ret = xhci_priv_suspend_quirk(hcd);
495 	if (ret)
496 		return ret;
497 
498 	return xhci_suspend(xhci, true);
499 }
500 
501 static int __maybe_unused xhci_plat_runtime_resume(struct device *dev)
502 {
503 	struct usb_hcd  *hcd = dev_get_drvdata(dev);
504 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
505 
506 	return xhci_resume(xhci, PMSG_AUTO_RESUME);
507 }
508 
509 const struct dev_pm_ops xhci_plat_pm_ops = {
510 	SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
511 
512 	SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend,
513 			   xhci_plat_runtime_resume,
514 			   NULL)
515 };
516 EXPORT_SYMBOL_GPL(xhci_plat_pm_ops);
517 
518 #ifdef CONFIG_ACPI
519 static const struct acpi_device_id usb_xhci_acpi_match[] = {
520 	/* XHCI-compliant USB Controller */
521 	{ "PNP0D10", },
522 	{ }
523 };
524 MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
525 #endif
526 
527 static struct platform_driver usb_generic_xhci_driver = {
528 	.probe	= xhci_generic_plat_probe,
529 	.remove	= xhci_plat_remove,
530 	.shutdown = usb_hcd_platform_shutdown,
531 	.driver	= {
532 		.name = "xhci-hcd",
533 		.pm = &xhci_plat_pm_ops,
534 		.of_match_table = of_match_ptr(usb_xhci_of_match),
535 		.acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
536 	},
537 };
538 MODULE_ALIAS("platform:xhci-hcd");
539 
540 static int __init xhci_plat_init(void)
541 {
542 	xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
543 	return platform_driver_register(&usb_generic_xhci_driver);
544 }
545 module_init(xhci_plat_init);
546 
547 static void __exit xhci_plat_exit(void)
548 {
549 	platform_driver_unregister(&usb_generic_xhci_driver);
550 }
551 module_exit(xhci_plat_exit);
552 
553 MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
554 MODULE_LICENSE("GPL");
555