]> Git Repo - linux.git/blame - drivers/usb/host/xhci-plat.c
Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / drivers / usb / host / xhci-plat.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0
3429e91a
SAS
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 <[email protected]>
7 *
8 * A lot of code borrowed from the Linux xHCI driver.
3429e91a
SAS
9 */
10
4718c177 11#include <linux/clk.h>
48157bb9 12#include <linux/dma-mapping.h>
3429e91a 13#include <linux/module.h>
4c39d4b9 14#include <linux/pci.h>
1fe6c452 15#include <linux/of.h>
2847d242 16#include <linux/of_device.h>
48157bb9 17#include <linux/platform_device.h>
7b8ef22e 18#include <linux/usb/phy.h>
48157bb9 19#include <linux/slab.h>
579085b5 20#include <linux/acpi.h>
c94d41e9 21#include <linux/usb/of.h>
3429e91a
SAS
22
23#include "xhci.h"
4efb2f69 24#include "xhci-plat.h"
97374792 25#include "xhci-mvebu.h"
4ac8918f 26#include "xhci-rcar.h"
3429e91a 27
1885d9a3
AB
28static struct hc_driver __read_mostly xhci_plat_hc_driver;
29
cd33a321
RQ
30static int xhci_plat_setup(struct usb_hcd *hcd);
31static int xhci_plat_start(struct usb_hcd *hcd);
32
33static const struct xhci_driver_overrides xhci_plat_overrides __initconst = {
4efb2f69 34 .extra_priv_size = sizeof(struct xhci_plat_priv),
cd33a321
RQ
35 .reset = xhci_plat_setup,
36 .start = xhci_plat_start,
37};
38
b1c127ae 39static void xhci_priv_plat_start(struct usb_hcd *hcd)
3429e91a 40{
b1c127ae
FB
41 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
42
43 if (priv->plat_start)
44 priv->plat_start(hcd);
45}
46
47static 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}
5ad3b03e 56
98c0a3ff
YS
57static 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
b1c127ae
FB
67static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
68{
3429e91a
SAS
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 */
52fb6125 74 xhci->quirks |= XHCI_PLAT;
3429e91a
SAS
75}
76
77/* called during probe() after chip reset completes */
78static int xhci_plat_setup(struct usb_hcd *hcd)
79{
4ac8918f
YS
80 int ret;
81
4ac8918f 82
b1c127ae
FB
83 ret = xhci_priv_init_quirk(hcd);
84 if (ret)
85 return ret;
3bdb263d 86
3429e91a
SAS
87 return xhci_gen_setup(hcd, xhci_plat_quirks);
88}
89
94adcdce
YS
90static int xhci_plat_start(struct usb_hcd *hcd)
91{
b1c127ae 92 xhci_priv_plat_start(hcd);
94adcdce
YS
93 return xhci_run(hcd);
94}
95
4efb2f69
YS
96#ifdef CONFIG_OF
97static const struct xhci_plat_priv xhci_plat_marvell_armada = {
b1c127ae 98 .init_quirk = xhci_mvebu_mbus_init_quirk,
4efb2f69
YS
99};
100
101static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen2 = {
9bf9d9d6 102 .firmware_name = XHCI_RCAR_FIRMWARE_NAME_V1,
b1c127ae
FB
103 .init_quirk = xhci_rcar_init_quirk,
104 .plat_start = xhci_rcar_start,
435cc113 105 .resume_quirk = xhci_rcar_resume_quirk,
4efb2f69
YS
106};
107
526a240f 108static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen3 = {
fc72aa83 109 .firmware_name = XHCI_RCAR_FIRMWARE_NAME_V3,
3f1dae6b
YS
110 .init_quirk = xhci_rcar_init_quirk,
111 .plat_start = xhci_rcar_start,
435cc113 112 .resume_quirk = xhci_rcar_resume_quirk,
3f1dae6b
YS
113};
114
4efb2f69
YS
115static const struct of_device_id usb_xhci_of_match[] = {
116 {
117 .compatible = "generic-xhci",
118 }, {
119 .compatible = "xhci-platform",
120 }, {
121 .compatible = "marvell,armada-375-xhci",
122 .data = &xhci_plat_marvell_armada,
123 }, {
124 .compatible = "marvell,armada-380-xhci",
125 .data = &xhci_plat_marvell_armada,
126 }, {
127 .compatible = "renesas,xhci-r8a7790",
128 .data = &xhci_plat_renesas_rcar_gen2,
129 }, {
130 .compatible = "renesas,xhci-r8a7791",
131 .data = &xhci_plat_renesas_rcar_gen2,
82487b71
YS
132 }, {
133 .compatible = "renesas,xhci-r8a7793",
134 .data = &xhci_plat_renesas_rcar_gen2,
526a240f
YS
135 }, {
136 .compatible = "renesas,xhci-r8a7795",
137 .data = &xhci_plat_renesas_rcar_gen3,
3f1dae6b
YS
138 }, {
139 .compatible = "renesas,xhci-r8a7796",
4dd51864 140 .data = &xhci_plat_renesas_rcar_gen3,
4efb2f69 141 }, {
f1bbdc3b
SH
142 .compatible = "renesas,rcar-gen2-xhci",
143 .data = &xhci_plat_renesas_rcar_gen2,
144 }, {
145 .compatible = "renesas,rcar-gen3-xhci",
146 .data = &xhci_plat_renesas_rcar_gen3,
4efb2f69 147 },
ff075d67 148 {},
4efb2f69
YS
149};
150MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
151#endif
152
3429e91a
SAS
153static int xhci_plat_probe(struct platform_device *pdev)
154{
2847d242 155 const struct xhci_plat_priv *priv_match;
3429e91a 156 const struct hc_driver *driver;
222471f7 157 struct device *sysdev, *tmpdev;
3429e91a
SAS
158 struct xhci_hcd *xhci;
159 struct resource *res;
160 struct usb_hcd *hcd;
4718c177 161 struct clk *clk;
3ae2da7b 162 struct clk *reg_clk;
3429e91a
SAS
163 int ret;
164 int irq;
165
166 if (usb_disabled())
167 return -ENODEV;
168
1885d9a3 169 driver = &xhci_plat_hc_driver;
3429e91a
SAS
170
171 irq = platform_get_irq(pdev, 0);
172 if (irq < 0)
4b148d51 173 return irq;
3429e91a 174
4c39d4b9
AB
175 /*
176 * sysdev must point to a device that is known to the system firmware
177 * or PCI hardware. We handle these three cases here:
178 * 1. xhci_plat comes from firmware
179 * 2. xhci_plat is child of a device from firmware (dwc3-plat)
180 * 3. xhci_plat is grandchild of a pci device (dwc3-pci)
181 */
c6b8e793
AW
182 for (sysdev = &pdev->dev; sysdev; sysdev = sysdev->parent) {
183 if (is_of_node(sysdev->fwnode) ||
184 is_acpi_device_node(sysdev->fwnode))
185 break;
4c39d4b9 186#ifdef CONFIG_PCI
c6b8e793
AW
187 else if (sysdev->bus == &pci_bus_type)
188 break;
4c39d4b9 189#endif
c6b8e793
AW
190 }
191
192 if (!sysdev)
193 sysdev = &pdev->dev;
4c39d4b9 194
0ebbe398 195 /* Try to set 64-bit DMA first */
4c39d4b9 196 if (WARN_ON(!sysdev->dma_mask))
0ebbe398 197 /* Platform did not initialize dma_mask */
4c39d4b9 198 ret = dma_coerce_mask_and_coherent(sysdev,
0ebbe398 199 DMA_BIT_MASK(64));
c10cf118 200 else
4c39d4b9 201 ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
0ebbe398
DD
202
203 /* If seting 64-bit DMA mask fails, fall back to 32-bit DMA mask */
204 if (ret) {
4c39d4b9 205 ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(32));
0ebbe398
DD
206 if (ret)
207 return ret;
208 }
c10cf118 209
b0c69b4b
BW
210 pm_runtime_set_active(&pdev->dev);
211 pm_runtime_enable(&pdev->dev);
212 pm_runtime_get_noresume(&pdev->dev);
213
4c39d4b9
AB
214 hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
215 dev_name(&pdev->dev), NULL);
b0c69b4b
BW
216 if (!hcd) {
217 ret = -ENOMEM;
218 goto disable_runtime;
219 }
3429e91a 220
e7020f19 221 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
fd666348
HS
222 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
223 if (IS_ERR(hcd->regs)) {
224 ret = PTR_ERR(hcd->regs);
3429e91a
SAS
225 goto put_hcd;
226 }
227
e7020f19
VB
228 hcd->rsrc_start = res->start;
229 hcd->rsrc_len = resource_size(res);
230
4718c177 231 /*
3ae2da7b
GC
232 * Not all platforms have clks so it is not an error if the
233 * clock do not exist.
4718c177 234 */
3ae2da7b
GC
235 reg_clk = devm_clk_get(&pdev->dev, "reg");
236 if (!IS_ERR(reg_clk)) {
237 ret = clk_prepare_enable(reg_clk);
238 if (ret)
239 goto put_hcd;
240 } else if (PTR_ERR(reg_clk) == -EPROBE_DEFER) {
241 ret = -EPROBE_DEFER;
242 goto put_hcd;
243 }
244
4718c177
GC
245 clk = devm_clk_get(&pdev->dev, NULL);
246 if (!IS_ERR(clk)) {
247 ret = clk_prepare_enable(clk);
248 if (ret)
3ae2da7b 249 goto disable_reg_clk;
de95c40d
TP
250 } else if (PTR_ERR(clk) == -EPROBE_DEFER) {
251 ret = -EPROBE_DEFER;
3ae2da7b 252 goto disable_reg_clk;
4718c177
GC
253 }
254
4efb2f69 255 xhci = hcd_to_xhci(hcd);
2847d242
GU
256 priv_match = of_device_get_match_data(&pdev->dev);
257 if (priv_match) {
4efb2f69
YS
258 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
259
260 /* Just copy data for now */
2ad294d5
GC
261 if (priv_match)
262 *priv = *priv_match;
4efb2f69
YS
263 }
264
3c9740a1 265 device_wakeup_enable(hcd->self.controller);
3429e91a 266
4718c177 267 xhci->clk = clk;
3ae2da7b 268 xhci->reg_clk = reg_clk;
9fa733f2 269 xhci->main_hcd = hcd;
4c39d4b9 270 xhci->shared_hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
3429e91a
SAS
271 dev_name(&pdev->dev), hcd);
272 if (!xhci->shared_hcd) {
273 ret = -ENOMEM;
4ac53087 274 goto disable_clk;
3429e91a
SAS
275 }
276
222471f7
AKV
277 /* imod_interval is the interrupt moderation value in nanoseconds. */
278 xhci->imod_interval = 40000;
4750bc78 279
222471f7
AKV
280 /* Iterate over all parent nodes for finding quirks */
281 for (tmpdev = &pdev->dev; tmpdev; tmpdev = tmpdev->parent) {
3429e91a 282
222471f7
AKV
283 if (device_property_read_bool(tmpdev, "usb2-lpm-disable"))
284 xhci->quirks |= XHCI_HW_LPM_DISABLE;
21939f00 285
222471f7
AKV
286 if (device_property_read_bool(tmpdev, "usb3-lpm-capable"))
287 xhci->quirks |= XHCI_LPM_SUPPORT;
288
289 if (device_property_read_bool(tmpdev, "quirk-broken-port-ped"))
290 xhci->quirks |= XHCI_BROKEN_PORT_PED;
291
292 device_property_read_u32(tmpdev, "imod-interval-ns",
293 &xhci->imod_interval);
294 }
ab725cbe 295
4c39d4b9 296 hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev, "usb-phy", 0);
7b8ef22e
MR
297 if (IS_ERR(hcd->usb_phy)) {
298 ret = PTR_ERR(hcd->usb_phy);
299 if (ret == -EPROBE_DEFER)
300 goto put_usb3_hcd;
301 hcd->usb_phy = NULL;
302 } else {
303 ret = usb_phy_init(hcd->usb_phy);
304 if (ret)
305 goto put_usb3_hcd;
4e88d4c0 306 hcd->skip_phy_initialization = 1;
7b8ef22e
MR
307 }
308
c94d41e9
PC
309 hcd->tpl_support = of_usb_host_tpl_support(sysdev->of_node);
310 xhci->shared_hcd->tpl_support = hcd->tpl_support;
4ac53087 311 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
3429e91a 312 if (ret)
7b8ef22e 313 goto disable_usb_phy;
3429e91a 314
5de4e1ea
W
315 if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
316 xhci->shared_hcd->can_do_streams = 1;
317
4ac53087
RQ
318 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
319 if (ret)
320 goto dealloc_usb2_hcd;
321
c70a1529 322 device_enable_async_suspend(&pdev->dev);
b0c69b4b
BW
323 pm_runtime_put_noidle(&pdev->dev);
324
325 /*
326 * Prevent runtime pm from being on as default, users should enable
327 * runtime pm using power/control in sysfs.
328 */
329 pm_runtime_forbid(&pdev->dev);
c70a1529 330
3429e91a
SAS
331 return 0;
332
4ac53087
RQ
333
334dealloc_usb2_hcd:
335 usb_remove_hcd(hcd);
336
7b8ef22e
MR
337disable_usb_phy:
338 usb_phy_shutdown(hcd->usb_phy);
339
3429e91a
SAS
340put_usb3_hcd:
341 usb_put_hcd(xhci->shared_hcd);
342
4718c177 343disable_clk:
2d79609b 344 clk_disable_unprepare(clk);
4718c177 345
3ae2da7b
GC
346disable_reg_clk:
347 clk_disable_unprepare(reg_clk);
348
3429e91a
SAS
349put_hcd:
350 usb_put_hcd(hcd);
351
b0c69b4b
BW
352disable_runtime:
353 pm_runtime_put_noidle(&pdev->dev);
354 pm_runtime_disable(&pdev->dev);
355
3429e91a
SAS
356 return ret;
357}
358
359static int xhci_plat_remove(struct platform_device *dev)
360{
361 struct usb_hcd *hcd = platform_get_drvdata(dev);
362 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4718c177 363 struct clk *clk = xhci->clk;
3ae2da7b 364 struct clk *reg_clk = xhci->reg_clk;
f0680904 365 struct usb_hcd *shared_hcd = xhci->shared_hcd;
3429e91a 366
dcc7620c
GR
367 xhci->xhc_state |= XHCI_STATE_REMOVING;
368
f0680904
MN
369 usb_remove_hcd(shared_hcd);
370 xhci->shared_hcd = NULL;
7b8ef22e 371 usb_phy_shutdown(hcd->usb_phy);
3429e91a
SAS
372
373 usb_remove_hcd(hcd);
f0680904 374 usb_put_hcd(shared_hcd);
4ac53087 375
2d79609b 376 clk_disable_unprepare(clk);
3ae2da7b 377 clk_disable_unprepare(reg_clk);
3429e91a 378 usb_put_hcd(hcd);
3429e91a 379
b0c69b4b
BW
380 pm_runtime_set_suspended(&dev->dev);
381 pm_runtime_disable(&dev->dev);
382
3429e91a
SAS
383 return 0;
384}
385
d852ed98 386static int __maybe_unused xhci_plat_suspend(struct device *dev)
57d04eb1
VS
387{
388 struct usb_hcd *hcd = dev_get_drvdata(dev);
389 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
390
a1377e53
LB
391 /*
392 * xhci_suspend() needs `do_wakeup` to know whether host is allowed
393 * to do wakeup during suspend. Since xhci_plat_suspend is currently
394 * only designed for system suspend, device_may_wakeup() is enough
395 * to dertermine whether host is allowed to do wakeup. Need to
396 * reconsider this when xhci_plat_suspend enlarges its scope, e.g.,
397 * also applies to runtime suspend.
398 */
d56e57ca 399 return xhci_suspend(xhci, device_may_wakeup(dev));
57d04eb1
VS
400}
401
d852ed98 402static int __maybe_unused xhci_plat_resume(struct device *dev)
57d04eb1
VS
403{
404 struct usb_hcd *hcd = dev_get_drvdata(dev);
405 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
98c0a3ff 406 int ret;
57d04eb1 407
98c0a3ff
YS
408 ret = xhci_priv_resume_quirk(hcd);
409 if (ret)
410 return ret;
411
57d04eb1
VS
412 return xhci_resume(xhci, 0);
413}
b0c69b4b 414
d852ed98 415static int __maybe_unused xhci_plat_runtime_suspend(struct device *dev)
b0c69b4b
BW
416{
417 struct usb_hcd *hcd = dev_get_drvdata(dev);
418 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
419
420 return xhci_suspend(xhci, true);
421}
422
d852ed98 423static int __maybe_unused xhci_plat_runtime_resume(struct device *dev)
b0c69b4b
BW
424{
425 struct usb_hcd *hcd = dev_get_drvdata(dev);
426 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
427
428 return xhci_resume(xhci, 0);
429}
57d04eb1
VS
430
431static const struct dev_pm_ops xhci_plat_pm_ops = {
432 SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
b0c69b4b
BW
433
434 SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend,
435 xhci_plat_runtime_resume,
436 NULL)
57d04eb1 437};
57d04eb1 438
579085b5
DD
439static const struct acpi_device_id usb_xhci_acpi_match[] = {
440 /* XHCI-compliant USB Controller */
441 { "PNP0D10", },
442 { }
443};
444MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
445
3429e91a
SAS
446static struct platform_driver usb_xhci_driver = {
447 .probe = xhci_plat_probe,
448 .remove = xhci_plat_remove,
449 .driver = {
450 .name = "xhci-hcd",
b0c69b4b 451 .pm = &xhci_plat_pm_ops,
1fe6c452 452 .of_match_table = of_match_ptr(usb_xhci_of_match),
579085b5 453 .acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
3429e91a
SAS
454 },
455};
456MODULE_ALIAS("platform:xhci-hcd");
457
29e409f0 458static int __init xhci_plat_init(void)
3429e91a 459{
cd33a321 460 xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
3429e91a
SAS
461 return platform_driver_register(&usb_xhci_driver);
462}
29e409f0 463module_init(xhci_plat_init);
3429e91a 464
29e409f0 465static void __exit xhci_plat_exit(void)
3429e91a
SAS
466{
467 platform_driver_unregister(&usb_xhci_driver);
468}
29e409f0
AB
469module_exit(xhci_plat_exit);
470
471MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
472MODULE_LICENSE("GPL");
This page took 0.519281 seconds and 4 git commands to generate.