xref: /linux/drivers/usb/host/xhci-plat.c (revision be5ae730ffa6fd774a00a4705c1e11e078b08ca1)
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 #include <linux/usb/xhci-sideband.h>
24 
25 #include "xhci.h"
26 #include "xhci-plat.h"
27 #include "xhci-mvebu.h"
28 
29 static struct hc_driver __read_mostly xhci_plat_hc_driver;
30 
31 static int xhci_plat_setup(struct usb_hcd *hcd);
32 static int xhci_plat_start(struct usb_hcd *hcd);
33 
34 static const struct xhci_driver_overrides xhci_plat_overrides __initconst = {
35 	.extra_priv_size = sizeof(struct xhci_plat_priv),
36 	.reset = xhci_plat_setup,
37 	.start = xhci_plat_start,
38 };
39 
40 static void xhci_priv_plat_start(struct usb_hcd *hcd)
41 {
42 	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
43 
44 	if (priv->plat_start)
45 		priv->plat_start(hcd);
46 }
47 
48 static int xhci_priv_init_quirk(struct usb_hcd *hcd)
49 {
50 	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
51 
52 	if (!priv->init_quirk)
53 		return 0;
54 
55 	return priv->init_quirk(hcd);
56 }
57 
58 static int xhci_priv_suspend_quirk(struct usb_hcd *hcd)
59 {
60 	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
61 
62 	if (!priv->suspend_quirk)
63 		return 0;
64 
65 	return priv->suspend_quirk(hcd);
66 }
67 
68 static int xhci_priv_resume_quirk(struct usb_hcd *hcd)
69 {
70 	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
71 
72 	if (!priv->resume_quirk)
73 		return 0;
74 
75 	return priv->resume_quirk(hcd);
76 }
77 
78 static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
79 {
80 	struct xhci_plat_priv *priv = xhci_to_priv(xhci);
81 
82 	xhci->quirks |= priv->quirks;
83 }
84 
85 /* called during probe() after chip reset completes */
86 static int xhci_plat_setup(struct usb_hcd *hcd)
87 {
88 	int ret;
89 
90 
91 	ret = xhci_priv_init_quirk(hcd);
92 	if (ret)
93 		return ret;
94 
95 	return xhci_gen_setup(hcd, xhci_plat_quirks);
96 }
97 
98 static int xhci_plat_start(struct usb_hcd *hcd)
99 {
100 	xhci_priv_plat_start(hcd);
101 	return xhci_run(hcd);
102 }
103 
104 #ifdef CONFIG_OF
105 static const struct xhci_plat_priv xhci_plat_marvell_armada = {
106 	.init_quirk = xhci_mvebu_mbus_init_quirk,
107 };
108 
109 static const struct xhci_plat_priv xhci_plat_marvell_armada3700 = {
110 	.quirks = XHCI_RESET_ON_RESUME,
111 };
112 
113 static const struct xhci_plat_priv xhci_plat_brcm = {
114 	.quirks = XHCI_RESET_ON_RESUME | XHCI_SUSPEND_RESUME_CLKS,
115 };
116 
117 static const struct of_device_id usb_xhci_of_match[] = {
118 	{
119 		.compatible = "generic-xhci",
120 	}, {
121 		.compatible = "xhci-platform",
122 	}, {
123 		.compatible = "marvell,armada-375-xhci",
124 		.data = &xhci_plat_marvell_armada,
125 	}, {
126 		.compatible = "marvell,armada-380-xhci",
127 		.data = &xhci_plat_marvell_armada,
128 	}, {
129 		.compatible = "marvell,armada3700-xhci",
130 		.data = &xhci_plat_marvell_armada3700,
131 	}, {
132 		.compatible = "brcm,xhci-brcm-v2",
133 		.data = &xhci_plat_brcm,
134 	}, {
135 		.compatible = "brcm,bcm2711-xhci",
136 		.data = &xhci_plat_brcm,
137 	}, {
138 		.compatible = "brcm,bcm7445-xhci",
139 		.data = &xhci_plat_brcm,
140 	},
141 	{},
142 };
143 MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
144 #endif
145 
146 int xhci_plat_probe(struct platform_device *pdev, struct device *sysdev, const struct xhci_plat_priv *priv_match)
147 {
148 	const struct hc_driver	*driver;
149 	struct device		*tmpdev;
150 	struct xhci_hcd		*xhci;
151 	struct resource         *res;
152 	struct usb_hcd		*hcd, *usb3_hcd;
153 	int			ret;
154 	int			irq;
155 	struct xhci_plat_priv	*priv = NULL;
156 	const struct of_device_id *of_match;
157 
158 	if (usb_disabled())
159 		return -ENODEV;
160 
161 	driver = &xhci_plat_hc_driver;
162 
163 	irq = platform_get_irq(pdev, 0);
164 	if (irq < 0)
165 		return irq;
166 
167 	if (!sysdev)
168 		sysdev = &pdev->dev;
169 
170 	ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
171 	if (ret)
172 		return ret;
173 
174 	pm_runtime_set_active(&pdev->dev);
175 	pm_runtime_enable(&pdev->dev);
176 	pm_runtime_get_noresume(&pdev->dev);
177 
178 	hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
179 			       dev_name(&pdev->dev), NULL);
180 	if (!hcd) {
181 		ret = -ENOMEM;
182 		goto disable_runtime;
183 	}
184 
185 	hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
186 	if (IS_ERR(hcd->regs)) {
187 		ret = PTR_ERR(hcd->regs);
188 		goto put_hcd;
189 	}
190 
191 	hcd->rsrc_start = res->start;
192 	hcd->rsrc_len = resource_size(res);
193 
194 	xhci = hcd_to_xhci(hcd);
195 
196 	xhci->allow_single_roothub = 1;
197 
198 	/*
199 	 * Not all platforms have clks so it is not an error if the
200 	 * clock do not exist.
201 	 */
202 	xhci->reg_clk = devm_clk_get_optional(&pdev->dev, "reg");
203 	if (IS_ERR(xhci->reg_clk)) {
204 		ret = PTR_ERR(xhci->reg_clk);
205 		goto put_hcd;
206 	}
207 
208 	xhci->clk = devm_clk_get_optional(&pdev->dev, NULL);
209 	if (IS_ERR(xhci->clk)) {
210 		ret = PTR_ERR(xhci->clk);
211 		goto put_hcd;
212 	}
213 
214 	xhci->reset = devm_reset_control_array_get_optional_shared(&pdev->dev);
215 	if (IS_ERR(xhci->reset)) {
216 		ret = PTR_ERR(xhci->reset);
217 		goto put_hcd;
218 	}
219 
220 	ret = reset_control_deassert(xhci->reset);
221 	if (ret)
222 		goto put_hcd;
223 
224 	ret = clk_prepare_enable(xhci->reg_clk);
225 	if (ret)
226 		goto err_reset;
227 
228 	ret = clk_prepare_enable(xhci->clk);
229 	if (ret)
230 		goto disable_reg_clk;
231 
232 	if (priv_match) {
233 		priv = hcd_to_xhci_priv(hcd);
234 		/* Just copy data for now */
235 		*priv = *priv_match;
236 	}
237 
238 	device_set_wakeup_capable(&pdev->dev, true);
239 
240 	xhci->main_hcd = hcd;
241 
242 	/* imod_interval is the interrupt moderation value in nanoseconds. */
243 	xhci->imod_interval = 40000;
244 
245 	/* Iterate over all parent nodes for finding quirks */
246 	for (tmpdev = &pdev->dev; tmpdev; tmpdev = tmpdev->parent) {
247 
248 		if (device_property_read_bool(tmpdev, "usb2-lpm-disable"))
249 			xhci->quirks |= XHCI_HW_LPM_DISABLE;
250 
251 		if (device_property_read_bool(tmpdev, "usb3-lpm-capable"))
252 			xhci->quirks |= XHCI_LPM_SUPPORT;
253 
254 		if (device_property_read_bool(tmpdev, "quirk-broken-port-ped"))
255 			xhci->quirks |= XHCI_BROKEN_PORT_PED;
256 
257 		if (device_property_read_bool(tmpdev, "xhci-sg-trb-cache-size-quirk"))
258 			xhci->quirks |= XHCI_SG_TRB_CACHE_SIZE_QUIRK;
259 
260 		if (device_property_read_bool(tmpdev, "write-64-hi-lo-quirk"))
261 			xhci->quirks |= XHCI_WRITE_64_HI_LO;
262 
263 		if (device_property_read_bool(tmpdev, "xhci-missing-cas-quirk"))
264 			xhci->quirks |= XHCI_MISSING_CAS;
265 
266 		if (device_property_read_bool(tmpdev, "xhci-skip-phy-init-quirk"))
267 			xhci->quirks |= XHCI_SKIP_PHY_INIT;
268 
269 		device_property_read_u32(tmpdev, "imod-interval-ns",
270 					 &xhci->imod_interval);
271 		device_property_read_u16(tmpdev, "num-hc-interrupters",
272 					 &xhci->max_interrupters);
273 	}
274 
275 	/*
276 	 * Drivers such as dwc3 manages PHYs themself (and rely on driver name
277 	 * matching for the xhci platform device).
278 	 */
279 	of_match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev);
280 	if (of_match) {
281 		hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev, "usb-phy", 0);
282 		if (IS_ERR(hcd->usb_phy)) {
283 			ret = PTR_ERR(hcd->usb_phy);
284 			if (ret == -EPROBE_DEFER)
285 				goto disable_clk;
286 			hcd->usb_phy = NULL;
287 		} else {
288 			ret = usb_phy_init(hcd->usb_phy);
289 			if (ret)
290 				goto disable_clk;
291 		}
292 	}
293 
294 	hcd->tpl_support = of_usb_host_tpl_support(sysdev->of_node);
295 
296 	if ((priv && (priv->quirks & XHCI_SKIP_PHY_INIT)) ||
297 	    (xhci->quirks & XHCI_SKIP_PHY_INIT))
298 		hcd->skip_phy_initialization = 1;
299 
300 	if (priv && (priv->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK))
301 		xhci->quirks |= XHCI_SG_TRB_CACHE_SIZE_QUIRK;
302 
303 	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
304 	if (ret)
305 		goto disable_usb_phy;
306 
307 	if (!xhci_has_one_roothub(xhci)) {
308 		xhci->shared_hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
309 						    dev_name(&pdev->dev), hcd);
310 		if (!xhci->shared_hcd) {
311 			ret = -ENOMEM;
312 			goto dealloc_usb2_hcd;
313 		}
314 
315 		if (of_match) {
316 			xhci->shared_hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev,
317 										"usb-phy", 1);
318 			if (IS_ERR(xhci->shared_hcd->usb_phy)) {
319 				xhci->shared_hcd->usb_phy = NULL;
320 			} else {
321 				ret = usb_phy_init(xhci->shared_hcd->usb_phy);
322 				if (ret)
323 					dev_err(sysdev, "%s init usb3phy fail (ret=%d)\n",
324 						__func__, ret);
325 			}
326 		}
327 
328 		xhci->shared_hcd->tpl_support = hcd->tpl_support;
329 	}
330 
331 	usb3_hcd = xhci_get_usb3_hcd(xhci);
332 	if (usb3_hcd && HCC_MAX_PSA(xhci->hcc_params) >= 4 &&
333 	    !(xhci->quirks & XHCI_BROKEN_STREAMS))
334 		usb3_hcd->can_do_streams = 1;
335 
336 	if (xhci->shared_hcd) {
337 		xhci->shared_hcd->rsrc_start = hcd->rsrc_start;
338 		xhci->shared_hcd->rsrc_len = hcd->rsrc_len;
339 		ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
340 		if (ret)
341 			goto put_usb3_hcd;
342 	}
343 
344 	device_enable_async_suspend(&pdev->dev);
345 	pm_runtime_put_noidle(&pdev->dev);
346 
347 	/*
348 	 * Prevent runtime pm from being on as default, users should enable
349 	 * runtime pm using power/control in sysfs.
350 	 */
351 	pm_runtime_forbid(&pdev->dev);
352 
353 	return 0;
354 
355 
356 put_usb3_hcd:
357 	usb_put_hcd(xhci->shared_hcd);
358 
359 dealloc_usb2_hcd:
360 	usb_remove_hcd(hcd);
361 
362 disable_usb_phy:
363 	usb_phy_shutdown(hcd->usb_phy);
364 
365 disable_clk:
366 	clk_disable_unprepare(xhci->clk);
367 
368 disable_reg_clk:
369 	clk_disable_unprepare(xhci->reg_clk);
370 
371 err_reset:
372 	reset_control_assert(xhci->reset);
373 
374 put_hcd:
375 	usb_put_hcd(hcd);
376 
377 disable_runtime:
378 	pm_runtime_put_noidle(&pdev->dev);
379 	pm_runtime_disable(&pdev->dev);
380 
381 	return ret;
382 }
383 EXPORT_SYMBOL_GPL(xhci_plat_probe);
384 
385 static int xhci_generic_plat_probe(struct platform_device *pdev)
386 {
387 	const struct xhci_plat_priv *priv_match;
388 	struct device *sysdev;
389 	int ret;
390 
391 	/*
392 	 * sysdev must point to a device that is known to the system firmware
393 	 * or PCI hardware. We handle these three cases here:
394 	 * 1. xhci_plat comes from firmware
395 	 * 2. xhci_plat is child of a device from firmware (dwc3-plat)
396 	 * 3. xhci_plat is grandchild of a pci device (dwc3-pci)
397 	 */
398 	for (sysdev = &pdev->dev; sysdev; sysdev = sysdev->parent) {
399 		if (is_of_node(sysdev->fwnode) ||
400 			is_acpi_device_node(sysdev->fwnode))
401 			break;
402 		else if (dev_is_pci(sysdev))
403 			break;
404 	}
405 
406 	if (!sysdev)
407 		sysdev = &pdev->dev;
408 
409 	if (WARN_ON(!sysdev->dma_mask)) {
410 		/* Platform did not initialize dma_mask */
411 		ret = dma_coerce_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
412 		if (ret)
413 			return ret;
414 	}
415 
416 	if (pdev->dev.of_node)
417 		priv_match = of_device_get_match_data(&pdev->dev);
418 	else
419 		priv_match = dev_get_platdata(&pdev->dev);
420 
421 	return xhci_plat_probe(pdev, sysdev, priv_match);
422 }
423 
424 void xhci_plat_remove(struct platform_device *dev)
425 {
426 	struct usb_hcd	*hcd = platform_get_drvdata(dev);
427 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
428 	struct clk *clk = xhci->clk;
429 	struct clk *reg_clk = xhci->reg_clk;
430 	struct usb_hcd *shared_hcd = xhci->shared_hcd;
431 
432 	xhci->xhc_state |= XHCI_STATE_REMOVING;
433 	pm_runtime_get_sync(&dev->dev);
434 
435 	if (shared_hcd) {
436 		usb_remove_hcd(shared_hcd);
437 		xhci->shared_hcd = NULL;
438 	}
439 
440 	usb_phy_shutdown(hcd->usb_phy);
441 
442 	usb_remove_hcd(hcd);
443 
444 	if (shared_hcd)
445 		usb_put_hcd(shared_hcd);
446 
447 	clk_disable_unprepare(clk);
448 	clk_disable_unprepare(reg_clk);
449 	reset_control_assert(xhci->reset);
450 	usb_put_hcd(hcd);
451 
452 	pm_runtime_disable(&dev->dev);
453 	pm_runtime_put_noidle(&dev->dev);
454 	pm_runtime_set_suspended(&dev->dev);
455 }
456 EXPORT_SYMBOL_GPL(xhci_plat_remove);
457 
458 static int xhci_plat_suspend_common(struct device *dev)
459 {
460 	struct usb_hcd	*hcd = dev_get_drvdata(dev);
461 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
462 	int ret;
463 
464 	if (pm_runtime_suspended(dev))
465 		pm_runtime_resume(dev);
466 
467 	ret = xhci_priv_suspend_quirk(hcd);
468 	if (ret)
469 		return ret;
470 	/*
471 	 * xhci_suspend() needs `do_wakeup` to know whether host is allowed
472 	 * to do wakeup during suspend.
473 	 */
474 	ret = xhci_suspend(xhci, device_may_wakeup(dev));
475 	if (ret)
476 		return ret;
477 
478 	if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
479 		clk_disable_unprepare(xhci->clk);
480 		clk_disable_unprepare(xhci->reg_clk);
481 	}
482 
483 	return 0;
484 }
485 
486 static int xhci_plat_suspend(struct device *dev)
487 {
488 	struct usb_hcd	*hcd = dev_get_drvdata(dev);
489 	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
490 
491 	if (xhci_sideband_check(hcd)) {
492 		priv->sideband_at_suspend = 1;
493 		dev_dbg(dev, "sideband instance active, skip suspend.\n");
494 		return 0;
495 	}
496 
497 	return xhci_plat_suspend_common(dev);
498 }
499 
500 static int xhci_plat_freeze(struct device *dev)
501 {
502 	return xhci_plat_suspend_common(dev);
503 }
504 
505 static int xhci_plat_resume_common(struct device *dev, bool power_lost)
506 {
507 	struct usb_hcd	*hcd = dev_get_drvdata(dev);
508 	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
509 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
510 	int ret;
511 
512 	if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
513 		ret = clk_prepare_enable(xhci->clk);
514 		if (ret)
515 			return ret;
516 
517 		ret = clk_prepare_enable(xhci->reg_clk);
518 		if (ret) {
519 			clk_disable_unprepare(xhci->clk);
520 			return ret;
521 		}
522 	}
523 
524 	ret = xhci_priv_resume_quirk(hcd);
525 	if (ret)
526 		goto disable_clks;
527 
528 	ret = xhci_resume(xhci, power_lost || priv->power_lost, false);
529 	if (ret)
530 		goto disable_clks;
531 
532 	pm_runtime_disable(dev);
533 	pm_runtime_set_active(dev);
534 	pm_runtime_enable(dev);
535 
536 	return 0;
537 
538 disable_clks:
539 	if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
540 		clk_disable_unprepare(xhci->clk);
541 		clk_disable_unprepare(xhci->reg_clk);
542 	}
543 
544 	return ret;
545 }
546 
547 static int xhci_plat_resume(struct device *dev)
548 {
549 	struct usb_hcd	*hcd = dev_get_drvdata(dev);
550 	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
551 
552 	if (priv->sideband_at_suspend) {
553 		priv->sideband_at_suspend = 0;
554 		dev_dbg(dev, "sideband instance active, skip resume.\n");
555 		return 0;
556 	}
557 
558 	return xhci_plat_resume_common(dev, false);
559 }
560 
561 static int xhci_plat_thaw(struct device *dev)
562 {
563 	return xhci_plat_resume_common(dev, false);
564 }
565 
566 static int xhci_plat_restore(struct device *dev)
567 {
568 	return xhci_plat_resume_common(dev, true);
569 }
570 
571 static int __maybe_unused xhci_plat_runtime_suspend(struct device *dev)
572 {
573 	struct usb_hcd  *hcd = dev_get_drvdata(dev);
574 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
575 	int ret;
576 
577 	ret = xhci_priv_suspend_quirk(hcd);
578 	if (ret)
579 		return ret;
580 
581 	return xhci_suspend(xhci, true);
582 }
583 
584 static int __maybe_unused xhci_plat_runtime_resume(struct device *dev)
585 {
586 	struct usb_hcd  *hcd = dev_get_drvdata(dev);
587 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
588 
589 	return xhci_resume(xhci, false, true);
590 }
591 
592 const struct dev_pm_ops xhci_plat_pm_ops = {
593 	.suspend = pm_sleep_ptr(xhci_plat_suspend),
594 	.resume = pm_sleep_ptr(xhci_plat_resume),
595 	.freeze = pm_sleep_ptr(xhci_plat_freeze),
596 	.thaw = pm_sleep_ptr(xhci_plat_thaw),
597 	.poweroff = pm_sleep_ptr(xhci_plat_freeze),
598 	.restore = pm_sleep_ptr(xhci_plat_restore),
599 
600 	SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend,
601 			   xhci_plat_runtime_resume,
602 			   NULL)
603 };
604 EXPORT_SYMBOL_GPL(xhci_plat_pm_ops);
605 
606 #ifdef CONFIG_ACPI
607 static const struct acpi_device_id usb_xhci_acpi_match[] = {
608 	/* XHCI-compliant USB Controller */
609 	{ "PNP0D10", },
610 	{ "PNP0D15", },
611 	{ }
612 };
613 MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
614 #endif
615 
616 static struct platform_driver usb_generic_xhci_driver = {
617 	.probe	= xhci_generic_plat_probe,
618 	.remove = xhci_plat_remove,
619 	.shutdown = usb_hcd_platform_shutdown,
620 	.driver	= {
621 		.name = "xhci-hcd",
622 		.pm = &xhci_plat_pm_ops,
623 		.of_match_table = of_match_ptr(usb_xhci_of_match),
624 		.acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
625 	},
626 };
627 MODULE_ALIAS("platform:xhci-hcd");
628 
629 static int __init xhci_plat_init(void)
630 {
631 	xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
632 	return platform_driver_register(&usb_generic_xhci_driver);
633 }
634 module_init(xhci_plat_init);
635 
636 static void __exit xhci_plat_exit(void)
637 {
638 	platform_driver_unregister(&usb_generic_xhci_driver);
639 }
640 module_exit(xhci_plat_exit);
641 
642 MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
643 MODULE_LICENSE("GPL");
644