]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/usb/host/xhci-plat.c
usb: host: xhci-plat: add clock support
[mirror_ubuntu-bionic-kernel.git] / drivers / usb / host / xhci-plat.c
CommitLineData
3429e91a
SAS
1/*
2 * xhci-plat.c - xHCI host controller driver platform Bus Glue.
3 *
4 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com
5 * Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
6 *
7 * A lot of code borrowed from the Linux xHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
12 */
13
4718c177 14#include <linux/clk.h>
48157bb9 15#include <linux/dma-mapping.h>
3429e91a 16#include <linux/module.h>
1fe6c452 17#include <linux/of.h>
48157bb9
GC
18#include <linux/platform_device.h>
19#include <linux/slab.h>
3429e91a
SAS
20
21#include "xhci.h"
22
23static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
24{
25 /*
26 * As of now platform drivers don't provide MSI support so we ensure
27 * here that the generic code does not try to make a pci_dev from our
28 * dev struct in order to setup MSI
29 */
52fb6125 30 xhci->quirks |= XHCI_PLAT;
3429e91a
SAS
31}
32
33/* called during probe() after chip reset completes */
34static int xhci_plat_setup(struct usb_hcd *hcd)
35{
36 return xhci_gen_setup(hcd, xhci_plat_quirks);
37}
38
39static const struct hc_driver xhci_plat_xhci_driver = {
40 .description = "xhci-hcd",
41 .product_desc = "xHCI Host Controller",
42 .hcd_priv_size = sizeof(struct xhci_hcd *),
43
44 /*
45 * generic hardware linkage
46 */
47 .irq = xhci_irq,
48 .flags = HCD_MEMORY | HCD_USB3 | HCD_SHARED,
49
50 /*
51 * basic lifecycle operations
52 */
53 .reset = xhci_plat_setup,
54 .start = xhci_run,
55 .stop = xhci_stop,
56 .shutdown = xhci_shutdown,
57
58 /*
59 * managing i/o requests and associated device resources
60 */
61 .urb_enqueue = xhci_urb_enqueue,
62 .urb_dequeue = xhci_urb_dequeue,
63 .alloc_dev = xhci_alloc_dev,
64 .free_dev = xhci_free_dev,
65 .alloc_streams = xhci_alloc_streams,
66 .free_streams = xhci_free_streams,
67 .add_endpoint = xhci_add_endpoint,
68 .drop_endpoint = xhci_drop_endpoint,
69 .endpoint_reset = xhci_endpoint_reset,
70 .check_bandwidth = xhci_check_bandwidth,
71 .reset_bandwidth = xhci_reset_bandwidth,
72 .address_device = xhci_address_device,
48fc7dbd 73 .enable_device = xhci_enable_device,
3429e91a
SAS
74 .update_hub_device = xhci_update_hub_device,
75 .reset_device = xhci_discover_or_reset_device,
76
77 /*
78 * scheduling support
79 */
80 .get_frame_number = xhci_get_frame,
81
82 /* Root hub support */
83 .hub_control = xhci_hub_control,
84 .hub_status_data = xhci_hub_status_data,
85 .bus_suspend = xhci_bus_suspend,
86 .bus_resume = xhci_bus_resume,
87};
88
89static int xhci_plat_probe(struct platform_device *pdev)
90{
91 const struct hc_driver *driver;
92 struct xhci_hcd *xhci;
93 struct resource *res;
94 struct usb_hcd *hcd;
4718c177 95 struct clk *clk;
3429e91a
SAS
96 int ret;
97 int irq;
98
99 if (usb_disabled())
100 return -ENODEV;
101
102 driver = &xhci_plat_xhci_driver;
103
104 irq = platform_get_irq(pdev, 0);
105 if (irq < 0)
106 return -ENODEV;
107
108 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
109 if (!res)
110 return -ENODEV;
111
c10cf118
XR
112 /* Initialize dma_mask and coherent_dma_mask to 32-bits */
113 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
114 if (ret)
115 return ret;
116 if (!pdev->dev.dma_mask)
117 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
118 else
119 dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
120
3429e91a
SAS
121 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
122 if (!hcd)
123 return -ENOMEM;
124
125 hcd->rsrc_start = res->start;
126 hcd->rsrc_len = resource_size(res);
127
128 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
129 driver->description)) {
130 dev_dbg(&pdev->dev, "controller already in use\n");
131 ret = -EBUSY;
132 goto put_hcd;
133 }
134
319acdfc 135 hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len);
3429e91a
SAS
136 if (!hcd->regs) {
137 dev_dbg(&pdev->dev, "error mapping memory\n");
138 ret = -EFAULT;
139 goto release_mem_region;
140 }
141
4718c177
GC
142 /*
143 * Not all platforms have a clk so it is not an error if the
144 * clock does not exists.
145 */
146 clk = devm_clk_get(&pdev->dev, NULL);
147 if (!IS_ERR(clk)) {
148 ret = clk_prepare_enable(clk);
149 if (ret)
150 goto unmap_registers;
151 }
152
3429e91a
SAS
153 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
154 if (ret)
4718c177
GC
155 goto disable_clk;
156
3c9740a1 157 device_wakeup_enable(hcd->self.controller);
3429e91a
SAS
158
159 /* USB 2.0 roothub is stored in the platform_device now. */
477527ba 160 hcd = platform_get_drvdata(pdev);
3429e91a 161 xhci = hcd_to_xhci(hcd);
4718c177 162 xhci->clk = clk;
3429e91a
SAS
163 xhci->shared_hcd = usb_create_shared_hcd(driver, &pdev->dev,
164 dev_name(&pdev->dev), hcd);
165 if (!xhci->shared_hcd) {
166 ret = -ENOMEM;
167 goto dealloc_usb2_hcd;
168 }
169
170 /*
171 * Set the xHCI pointer before xhci_plat_setup() (aka hcd_driver.reset)
172 * is called by usb_add_hcd().
173 */
174 *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci;
175
14aec589
ON
176 if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
177 xhci->shared_hcd->can_do_streams = 1;
178
3429e91a
SAS
179 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
180 if (ret)
181 goto put_usb3_hcd;
182
183 return 0;
184
185put_usb3_hcd:
186 usb_put_hcd(xhci->shared_hcd);
187
188dealloc_usb2_hcd:
189 usb_remove_hcd(hcd);
190
4718c177
GC
191disable_clk:
192 if (!IS_ERR(clk))
193 clk_disable_unprepare(clk);
194
3429e91a
SAS
195unmap_registers:
196 iounmap(hcd->regs);
197
198release_mem_region:
199 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
200
201put_hcd:
202 usb_put_hcd(hcd);
203
204 return ret;
205}
206
207static int xhci_plat_remove(struct platform_device *dev)
208{
209 struct usb_hcd *hcd = platform_get_drvdata(dev);
210 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4718c177 211 struct clk *clk = xhci->clk;
3429e91a
SAS
212
213 usb_remove_hcd(xhci->shared_hcd);
214 usb_put_hcd(xhci->shared_hcd);
215
216 usb_remove_hcd(hcd);
4718c177
GC
217 if (!IS_ERR(clk))
218 clk_disable_unprepare(clk);
3429e91a 219 iounmap(hcd->regs);
5388a3a5 220 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
3429e91a
SAS
221 usb_put_hcd(hcd);
222 kfree(xhci);
223
224 return 0;
225}
226
57d04eb1
VS
227#ifdef CONFIG_PM
228static int xhci_plat_suspend(struct device *dev)
229{
230 struct usb_hcd *hcd = dev_get_drvdata(dev);
231 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
232
233 return xhci_suspend(xhci);
234}
235
236static int xhci_plat_resume(struct device *dev)
237{
238 struct usb_hcd *hcd = dev_get_drvdata(dev);
239 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
240
241 return xhci_resume(xhci, 0);
242}
243
244static const struct dev_pm_ops xhci_plat_pm_ops = {
245 SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
246};
247#define DEV_PM_OPS (&xhci_plat_pm_ops)
248#else
249#define DEV_PM_OPS NULL
250#endif /* CONFIG_PM */
251
1fe6c452
AC
252#ifdef CONFIG_OF
253static const struct of_device_id usb_xhci_of_match[] = {
0f94388b 254 { .compatible = "generic-xhci" },
1fe6c452
AC
255 { .compatible = "xhci-platform" },
256 { },
257};
258MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
259#endif
260
3429e91a
SAS
261static struct platform_driver usb_xhci_driver = {
262 .probe = xhci_plat_probe,
263 .remove = xhci_plat_remove,
264 .driver = {
265 .name = "xhci-hcd",
57d04eb1 266 .pm = DEV_PM_OPS,
1fe6c452 267 .of_match_table = of_match_ptr(usb_xhci_of_match),
3429e91a
SAS
268 },
269};
270MODULE_ALIAS("platform:xhci-hcd");
271
272int xhci_register_plat(void)
273{
274 return platform_driver_register(&usb_xhci_driver);
275}
276
277void xhci_unregister_plat(void)
278{
279 platform_driver_unregister(&usb_xhci_driver);
280}