xref: /openbmc/linux/drivers/usb/host/xhci-plat.c (revision 4fc4dca8)
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 - http://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 
23 #include "xhci.h"
24 #include "xhci-plat.h"
25 #include "xhci-mvebu.h"
26 #include "xhci-rcar.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_resume_quirk(struct usb_hcd *hcd)
58 {
59 	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
60 
61 	if (!priv->resume_quirk)
62 		return 0;
63 
64 	return priv->resume_quirk(hcd);
65 }
66 
67 static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
68 {
69 	/*
70 	 * As of now platform drivers don't provide MSI support so we ensure
71 	 * here that the generic code does not try to make a pci_dev from our
72 	 * dev struct in order to setup MSI
73 	 */
74 	xhci->quirks |= XHCI_PLAT;
75 }
76 
77 /* called during probe() after chip reset completes */
78 static int xhci_plat_setup(struct usb_hcd *hcd)
79 {
80 	int ret;
81 
82 
83 	ret = xhci_priv_init_quirk(hcd);
84 	if (ret)
85 		return ret;
86 
87 	return xhci_gen_setup(hcd, xhci_plat_quirks);
88 }
89 
90 static int xhci_plat_start(struct usb_hcd *hcd)
91 {
92 	xhci_priv_plat_start(hcd);
93 	return xhci_run(hcd);
94 }
95 
96 #ifdef CONFIG_OF
97 static const struct xhci_plat_priv xhci_plat_marvell_armada = {
98 	.init_quirk = xhci_mvebu_mbus_init_quirk,
99 };
100 
101 static const struct xhci_plat_priv xhci_plat_marvell_armada3700 = {
102 	.init_quirk = xhci_mvebu_a3700_init_quirk,
103 };
104 
105 static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen2 = {
106 	.firmware_name = XHCI_RCAR_FIRMWARE_NAME_V1,
107 	.init_quirk = xhci_rcar_init_quirk,
108 	.plat_start = xhci_rcar_start,
109 	.resume_quirk = xhci_rcar_resume_quirk,
110 };
111 
112 static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen3 = {
113 	.firmware_name = XHCI_RCAR_FIRMWARE_NAME_V3,
114 	.init_quirk = xhci_rcar_init_quirk,
115 	.plat_start = xhci_rcar_start,
116 	.resume_quirk = xhci_rcar_resume_quirk,
117 };
118 
119 static const struct of_device_id usb_xhci_of_match[] = {
120 	{
121 		.compatible = "generic-xhci",
122 	}, {
123 		.compatible = "xhci-platform",
124 	}, {
125 		.compatible = "marvell,armada-375-xhci",
126 		.data = &xhci_plat_marvell_armada,
127 	}, {
128 		.compatible = "marvell,armada-380-xhci",
129 		.data = &xhci_plat_marvell_armada,
130 	}, {
131 		.compatible = "marvell,armada3700-xhci",
132 		.data = &xhci_plat_marvell_armada3700,
133 	}, {
134 		.compatible = "renesas,xhci-r8a7790",
135 		.data = &xhci_plat_renesas_rcar_gen2,
136 	}, {
137 		.compatible = "renesas,xhci-r8a7791",
138 		.data = &xhci_plat_renesas_rcar_gen2,
139 	}, {
140 		.compatible = "renesas,xhci-r8a7793",
141 		.data = &xhci_plat_renesas_rcar_gen2,
142 	}, {
143 		.compatible = "renesas,xhci-r8a7795",
144 		.data = &xhci_plat_renesas_rcar_gen3,
145 	}, {
146 		.compatible = "renesas,xhci-r8a7796",
147 		.data = &xhci_plat_renesas_rcar_gen3,
148 	}, {
149 		.compatible = "renesas,rcar-gen2-xhci",
150 		.data = &xhci_plat_renesas_rcar_gen2,
151 	}, {
152 		.compatible = "renesas,rcar-gen3-xhci",
153 		.data = &xhci_plat_renesas_rcar_gen3,
154 	},
155 	{},
156 };
157 MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
158 #endif
159 
160 static int xhci_plat_probe(struct platform_device *pdev)
161 {
162 	const struct xhci_plat_priv *priv_match;
163 	const struct hc_driver	*driver;
164 	struct device		*sysdev, *tmpdev;
165 	struct xhci_hcd		*xhci;
166 	struct resource         *res;
167 	struct usb_hcd		*hcd;
168 	int			ret;
169 	int			irq;
170 
171 	if (usb_disabled())
172 		return -ENODEV;
173 
174 	driver = &xhci_plat_hc_driver;
175 
176 	irq = platform_get_irq(pdev, 0);
177 	if (irq < 0)
178 		return irq;
179 
180 	/*
181 	 * sysdev must point to a device that is known to the system firmware
182 	 * or PCI hardware. We handle these three cases here:
183 	 * 1. xhci_plat comes from firmware
184 	 * 2. xhci_plat is child of a device from firmware (dwc3-plat)
185 	 * 3. xhci_plat is grandchild of a pci device (dwc3-pci)
186 	 */
187 	for (sysdev = &pdev->dev; sysdev; sysdev = sysdev->parent) {
188 		if (is_of_node(sysdev->fwnode) ||
189 			is_acpi_device_node(sysdev->fwnode))
190 			break;
191 #ifdef CONFIG_PCI
192 		else if (sysdev->bus == &pci_bus_type)
193 			break;
194 #endif
195 	}
196 
197 	if (!sysdev)
198 		sysdev = &pdev->dev;
199 
200 	/* Try to set 64-bit DMA first */
201 	if (WARN_ON(!sysdev->dma_mask))
202 		/* Platform did not initialize dma_mask */
203 		ret = dma_coerce_mask_and_coherent(sysdev,
204 						   DMA_BIT_MASK(64));
205 	else
206 		ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
207 
208 	/* If seting 64-bit DMA mask fails, fall back to 32-bit DMA mask */
209 	if (ret) {
210 		ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(32));
211 		if (ret)
212 			return ret;
213 	}
214 
215 	pm_runtime_set_active(&pdev->dev);
216 	pm_runtime_enable(&pdev->dev);
217 	pm_runtime_get_noresume(&pdev->dev);
218 
219 	hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
220 			       dev_name(&pdev->dev), NULL);
221 	if (!hcd) {
222 		ret = -ENOMEM;
223 		goto disable_runtime;
224 	}
225 
226 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
227 	hcd->regs = devm_ioremap_resource(&pdev->dev, res);
228 	if (IS_ERR(hcd->regs)) {
229 		ret = PTR_ERR(hcd->regs);
230 		goto put_hcd;
231 	}
232 
233 	hcd->rsrc_start = res->start;
234 	hcd->rsrc_len = resource_size(res);
235 
236 	xhci = hcd_to_xhci(hcd);
237 
238 	/*
239 	 * Not all platforms have clks so it is not an error if the
240 	 * clock do not exist.
241 	 */
242 	xhci->reg_clk = devm_clk_get_optional(&pdev->dev, "reg");
243 	if (IS_ERR(xhci->reg_clk)) {
244 		ret = PTR_ERR(xhci->reg_clk);
245 		goto put_hcd;
246 	}
247 
248 	ret = clk_prepare_enable(xhci->reg_clk);
249 	if (ret)
250 		goto put_hcd;
251 
252 	xhci->clk = devm_clk_get_optional(&pdev->dev, NULL);
253 	if (IS_ERR(xhci->clk)) {
254 		ret = PTR_ERR(xhci->clk);
255 		goto disable_reg_clk;
256 	}
257 
258 	ret = clk_prepare_enable(xhci->clk);
259 	if (ret)
260 		goto disable_reg_clk;
261 
262 	priv_match = of_device_get_match_data(&pdev->dev);
263 	if (priv_match) {
264 		struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
265 
266 		/* Just copy data for now */
267 		if (priv_match)
268 			*priv = *priv_match;
269 	}
270 
271 	device_wakeup_enable(hcd->self.controller);
272 
273 	xhci->main_hcd = hcd;
274 	xhci->shared_hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
275 			dev_name(&pdev->dev), hcd);
276 	if (!xhci->shared_hcd) {
277 		ret = -ENOMEM;
278 		goto disable_clk;
279 	}
280 
281 	/* imod_interval is the interrupt moderation value in nanoseconds. */
282 	xhci->imod_interval = 40000;
283 
284 	/* Iterate over all parent nodes for finding quirks */
285 	for (tmpdev = &pdev->dev; tmpdev; tmpdev = tmpdev->parent) {
286 
287 		if (device_property_read_bool(tmpdev, "usb2-lpm-disable"))
288 			xhci->quirks |= XHCI_HW_LPM_DISABLE;
289 
290 		if (device_property_read_bool(tmpdev, "usb3-lpm-capable"))
291 			xhci->quirks |= XHCI_LPM_SUPPORT;
292 
293 		if (device_property_read_bool(tmpdev, "quirk-broken-port-ped"))
294 			xhci->quirks |= XHCI_BROKEN_PORT_PED;
295 
296 		device_property_read_u32(tmpdev, "imod-interval-ns",
297 					 &xhci->imod_interval);
298 	}
299 
300 	hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev, "usb-phy", 0);
301 	if (IS_ERR(hcd->usb_phy)) {
302 		ret = PTR_ERR(hcd->usb_phy);
303 		if (ret == -EPROBE_DEFER)
304 			goto put_usb3_hcd;
305 		hcd->usb_phy = NULL;
306 	} else {
307 		ret = usb_phy_init(hcd->usb_phy);
308 		if (ret)
309 			goto put_usb3_hcd;
310 		hcd->skip_phy_initialization = 1;
311 	}
312 
313 	hcd->tpl_support = of_usb_host_tpl_support(sysdev->of_node);
314 	xhci->shared_hcd->tpl_support = hcd->tpl_support;
315 	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
316 	if (ret)
317 		goto disable_usb_phy;
318 
319 	if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
320 		xhci->shared_hcd->can_do_streams = 1;
321 
322 	ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
323 	if (ret)
324 		goto dealloc_usb2_hcd;
325 
326 	device_enable_async_suspend(&pdev->dev);
327 	pm_runtime_put_noidle(&pdev->dev);
328 
329 	/*
330 	 * Prevent runtime pm from being on as default, users should enable
331 	 * runtime pm using power/control in sysfs.
332 	 */
333 	pm_runtime_forbid(&pdev->dev);
334 
335 	return 0;
336 
337 
338 dealloc_usb2_hcd:
339 	usb_remove_hcd(hcd);
340 
341 disable_usb_phy:
342 	usb_phy_shutdown(hcd->usb_phy);
343 
344 put_usb3_hcd:
345 	usb_put_hcd(xhci->shared_hcd);
346 
347 disable_clk:
348 	clk_disable_unprepare(xhci->clk);
349 
350 disable_reg_clk:
351 	clk_disable_unprepare(xhci->reg_clk);
352 
353 put_hcd:
354 	usb_put_hcd(hcd);
355 
356 disable_runtime:
357 	pm_runtime_put_noidle(&pdev->dev);
358 	pm_runtime_disable(&pdev->dev);
359 
360 	return ret;
361 }
362 
363 static int xhci_plat_remove(struct platform_device *dev)
364 {
365 	struct usb_hcd	*hcd = platform_get_drvdata(dev);
366 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
367 	struct clk *clk = xhci->clk;
368 	struct clk *reg_clk = xhci->reg_clk;
369 	struct usb_hcd *shared_hcd = xhci->shared_hcd;
370 
371 	xhci->xhc_state |= XHCI_STATE_REMOVING;
372 
373 	usb_remove_hcd(shared_hcd);
374 	xhci->shared_hcd = NULL;
375 	usb_phy_shutdown(hcd->usb_phy);
376 
377 	usb_remove_hcd(hcd);
378 	usb_put_hcd(shared_hcd);
379 
380 	clk_disable_unprepare(clk);
381 	clk_disable_unprepare(reg_clk);
382 	usb_put_hcd(hcd);
383 
384 	pm_runtime_set_suspended(&dev->dev);
385 	pm_runtime_disable(&dev->dev);
386 
387 	return 0;
388 }
389 
390 static int __maybe_unused xhci_plat_suspend(struct device *dev)
391 {
392 	struct usb_hcd	*hcd = dev_get_drvdata(dev);
393 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
394 
395 	/*
396 	 * xhci_suspend() needs `do_wakeup` to know whether host is allowed
397 	 * to do wakeup during suspend. Since xhci_plat_suspend is currently
398 	 * only designed for system suspend, device_may_wakeup() is enough
399 	 * to dertermine whether host is allowed to do wakeup. Need to
400 	 * reconsider this when xhci_plat_suspend enlarges its scope, e.g.,
401 	 * also applies to runtime suspend.
402 	 */
403 	return xhci_suspend(xhci, device_may_wakeup(dev));
404 }
405 
406 static int __maybe_unused xhci_plat_resume(struct device *dev)
407 {
408 	struct usb_hcd	*hcd = dev_get_drvdata(dev);
409 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
410 	int ret;
411 
412 	ret = xhci_priv_resume_quirk(hcd);
413 	if (ret)
414 		return ret;
415 
416 	return xhci_resume(xhci, 0);
417 }
418 
419 static int __maybe_unused xhci_plat_runtime_suspend(struct device *dev)
420 {
421 	struct usb_hcd  *hcd = dev_get_drvdata(dev);
422 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
423 
424 	return xhci_suspend(xhci, true);
425 }
426 
427 static int __maybe_unused xhci_plat_runtime_resume(struct device *dev)
428 {
429 	struct usb_hcd  *hcd = dev_get_drvdata(dev);
430 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
431 
432 	return xhci_resume(xhci, 0);
433 }
434 
435 static const struct dev_pm_ops xhci_plat_pm_ops = {
436 	SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
437 
438 	SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend,
439 			   xhci_plat_runtime_resume,
440 			   NULL)
441 };
442 
443 static const struct acpi_device_id usb_xhci_acpi_match[] = {
444 	/* XHCI-compliant USB Controller */
445 	{ "PNP0D10", },
446 	{ }
447 };
448 MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
449 
450 static struct platform_driver usb_xhci_driver = {
451 	.probe	= xhci_plat_probe,
452 	.remove	= xhci_plat_remove,
453 	.driver	= {
454 		.name = "xhci-hcd",
455 		.pm = &xhci_plat_pm_ops,
456 		.of_match_table = of_match_ptr(usb_xhci_of_match),
457 		.acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
458 	},
459 };
460 MODULE_ALIAS("platform:xhci-hcd");
461 
462 static int __init xhci_plat_init(void)
463 {
464 	xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
465 	return platform_driver_register(&usb_xhci_driver);
466 }
467 module_init(xhci_plat_init);
468 
469 static void __exit xhci_plat_exit(void)
470 {
471 	platform_driver_unregister(&usb_xhci_driver);
472 }
473 module_exit(xhci_plat_exit);
474 
475 MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
476 MODULE_LICENSE("GPL");
477