blob: 9d29aa1b1bc8f8301b8fb7c7b0f6bc3b18cd27c8 [file] [log] [blame]
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +02001/*
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
14#include <linux/platform_device.h>
15#include <linux/module.h>
16#include <linux/slab.h>
Al Cooper1fe6c452013-07-25 19:04:44 -040017#include <linux/of.h>
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +030018#include <linux/dma-mapping.h>
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +020019
20#include "xhci.h"
21
22static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
23{
24 /*
25 * As of now platform drivers don't provide MSI support so we ensure
26 * here that the generic code does not try to make a pci_dev from our
27 * dev struct in order to setup MSI
28 */
Sarah Sharp52fb6122013-08-08 10:08:34 -070029 xhci->quirks |= XHCI_PLAT;
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +020030}
31
32/* called during probe() after chip reset completes */
33static int xhci_plat_setup(struct usb_hcd *hcd)
34{
35 return xhci_gen_setup(hcd, xhci_plat_quirks);
36}
37
38static const struct hc_driver xhci_plat_xhci_driver = {
39 .description = "xhci-hcd",
40 .product_desc = "xHCI Host Controller",
41 .hcd_priv_size = sizeof(struct xhci_hcd *),
42
43 /*
44 * generic hardware linkage
45 */
46 .irq = xhci_irq,
47 .flags = HCD_MEMORY | HCD_USB3 | HCD_SHARED,
48
49 /*
50 * basic lifecycle operations
51 */
52 .reset = xhci_plat_setup,
53 .start = xhci_run,
54 .stop = xhci_stop,
55 .shutdown = xhci_shutdown,
56
57 /*
58 * managing i/o requests and associated device resources
59 */
60 .urb_enqueue = xhci_urb_enqueue,
61 .urb_dequeue = xhci_urb_dequeue,
62 .alloc_dev = xhci_alloc_dev,
63 .free_dev = xhci_free_dev,
64 .alloc_streams = xhci_alloc_streams,
65 .free_streams = xhci_free_streams,
66 .add_endpoint = xhci_add_endpoint,
67 .drop_endpoint = xhci_drop_endpoint,
68 .endpoint_reset = xhci_endpoint_reset,
69 .check_bandwidth = xhci_check_bandwidth,
70 .reset_bandwidth = xhci_reset_bandwidth,
71 .address_device = xhci_address_device,
72 .update_hub_device = xhci_update_hub_device,
73 .reset_device = xhci_discover_or_reset_device,
74
75 /*
76 * scheduling support
77 */
78 .get_frame_number = xhci_get_frame,
79
80 /* Root hub support */
81 .hub_control = xhci_hub_control,
82 .hub_status_data = xhci_hub_status_data,
83 .bus_suspend = xhci_bus_suspend,
84 .bus_resume = xhci_bus_resume,
85};
86
87static int xhci_plat_probe(struct platform_device *pdev)
88{
89 const struct hc_driver *driver;
90 struct xhci_hcd *xhci;
91 struct resource *res;
92 struct usb_hcd *hcd;
93 int ret;
94 int irq;
95
96 if (usb_disabled())
97 return -ENODEV;
98
99 driver = &xhci_plat_xhci_driver;
100
101 irq = platform_get_irq(pdev, 0);
102 if (irq < 0)
103 return -ENODEV;
104
105 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
106 if (!res)
107 return -ENODEV;
108
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +0300109 /* Initialize dma_mask and coherent_dma_mask to 32-bits */
110 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
111 if (ret)
112 return ret;
113 if (!pdev->dev.dma_mask)
114 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
115 else
116 dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
117
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200118 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
119 if (!hcd)
120 return -ENOMEM;
121
122 hcd->rsrc_start = res->start;
123 hcd->rsrc_len = resource_size(res);
124
125 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
126 driver->description)) {
127 dev_dbg(&pdev->dev, "controller already in use\n");
128 ret = -EBUSY;
129 goto put_hcd;
130 }
131
Ruchika Kharwar319acdf2012-08-10 09:58:30 +0300132 hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len);
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200133 if (!hcd->regs) {
134 dev_dbg(&pdev->dev, "error mapping memory\n");
135 ret = -EFAULT;
136 goto release_mem_region;
137 }
138
139 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
140 if (ret)
141 goto unmap_registers;
Peter Chen3c9740a2013-11-05 10:46:02 +0800142 device_wakeup_enable(hcd->self.controller);
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200143
144 /* USB 2.0 roothub is stored in the platform_device now. */
Jingoo Han477527b2013-05-23 19:18:39 +0900145 hcd = platform_get_drvdata(pdev);
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200146 xhci = hcd_to_xhci(hcd);
147 xhci->shared_hcd = usb_create_shared_hcd(driver, &pdev->dev,
148 dev_name(&pdev->dev), hcd);
149 if (!xhci->shared_hcd) {
150 ret = -ENOMEM;
151 goto dealloc_usb2_hcd;
152 }
153
154 /*
155 * Set the xHCI pointer before xhci_plat_setup() (aka hcd_driver.reset)
156 * is called by usb_add_hcd().
157 */
158 *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci;
159
160 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
161 if (ret)
162 goto put_usb3_hcd;
163
164 return 0;
165
166put_usb3_hcd:
167 usb_put_hcd(xhci->shared_hcd);
168
169dealloc_usb2_hcd:
170 usb_remove_hcd(hcd);
171
172unmap_registers:
173 iounmap(hcd->regs);
174
175release_mem_region:
176 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
177
178put_hcd:
179 usb_put_hcd(hcd);
180
181 return ret;
182}
183
184static int xhci_plat_remove(struct platform_device *dev)
185{
186 struct usb_hcd *hcd = platform_get_drvdata(dev);
187 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
188
189 usb_remove_hcd(xhci->shared_hcd);
190 usb_put_hcd(xhci->shared_hcd);
191
192 usb_remove_hcd(hcd);
193 iounmap(hcd->regs);
George Cherian5388a3a2013-06-21 13:59:08 +0530194 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200195 usb_put_hcd(hcd);
196 kfree(xhci);
197
198 return 0;
199}
200
Vikas Sajjan57d04eb2013-02-11 12:58:00 +0200201#ifdef CONFIG_PM
202static int xhci_plat_suspend(struct device *dev)
203{
204 struct usb_hcd *hcd = dev_get_drvdata(dev);
205 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
206
207 return xhci_suspend(xhci);
208}
209
210static int xhci_plat_resume(struct device *dev)
211{
212 struct usb_hcd *hcd = dev_get_drvdata(dev);
213 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
214
215 return xhci_resume(xhci, 0);
216}
217
218static const struct dev_pm_ops xhci_plat_pm_ops = {
219 SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
220};
221#define DEV_PM_OPS (&xhci_plat_pm_ops)
222#else
223#define DEV_PM_OPS NULL
224#endif /* CONFIG_PM */
225
Al Cooper1fe6c452013-07-25 19:04:44 -0400226#ifdef CONFIG_OF
227static const struct of_device_id usb_xhci_of_match[] = {
228 { .compatible = "xhci-platform" },
229 { },
230};
231MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
232#endif
233
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200234static struct platform_driver usb_xhci_driver = {
235 .probe = xhci_plat_probe,
236 .remove = xhci_plat_remove,
237 .driver = {
238 .name = "xhci-hcd",
Vikas Sajjan57d04eb2013-02-11 12:58:00 +0200239 .pm = DEV_PM_OPS,
Al Cooper1fe6c452013-07-25 19:04:44 -0400240 .of_match_table = of_match_ptr(usb_xhci_of_match),
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200241 },
242};
243MODULE_ALIAS("platform:xhci-hcd");
244
245int xhci_register_plat(void)
246{
247 return platform_driver_register(&usb_xhci_driver);
248}
249
250void xhci_unregister_plat(void)
251{
252 platform_driver_unregister(&usb_xhci_driver);
253}