blob: e03b0bb37910f7082c0180a93bbbe910c5e105e6 [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>
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020017#include <linux/usb/otg.h>
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +020018
19#include "xhci.h"
20
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020021static struct usb_phy *phy;
22
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +020023static 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 */
30 xhci->quirks |= XHCI_BROKEN_MSI;
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,
73 .update_hub_device = xhci_update_hub_device,
74 .reset_device = xhci_discover_or_reset_device,
75
76 /*
77 * scheduling support
78 */
79 .get_frame_number = xhci_get_frame,
80
81 /* Root hub support */
82 .hub_control = xhci_hub_control,
83 .hub_status_data = xhci_hub_status_data,
84 .bus_suspend = xhci_bus_suspend,
85 .bus_resume = xhci_bus_resume,
86};
87
88static int xhci_plat_probe(struct platform_device *pdev)
89{
90 const struct hc_driver *driver;
91 struct xhci_hcd *xhci;
92 struct resource *res;
93 struct usb_hcd *hcd;
94 int ret;
95 int irq;
96
97 if (usb_disabled())
98 return -ENODEV;
99
100 driver = &xhci_plat_xhci_driver;
101
102 irq = platform_get_irq(pdev, 0);
103 if (irq < 0)
104 return -ENODEV;
105
106 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
107 if (!res)
108 return -ENODEV;
109
110 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
111 if (!hcd)
112 return -ENOMEM;
113
114 hcd->rsrc_start = res->start;
115 hcd->rsrc_len = resource_size(res);
116
117 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
118 driver->description)) {
119 dev_dbg(&pdev->dev, "controller already in use\n");
120 ret = -EBUSY;
121 goto put_hcd;
122 }
123
124 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
125 if (!hcd->regs) {
126 dev_dbg(&pdev->dev, "error mapping memory\n");
127 ret = -EFAULT;
128 goto release_mem_region;
129 }
130
131 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
132 if (ret)
133 goto unmap_registers;
134
135 /* USB 2.0 roothub is stored in the platform_device now. */
136 hcd = dev_get_drvdata(&pdev->dev);
137 xhci = hcd_to_xhci(hcd);
138 xhci->shared_hcd = usb_create_shared_hcd(driver, &pdev->dev,
139 dev_name(&pdev->dev), hcd);
140 if (!xhci->shared_hcd) {
141 ret = -ENOMEM;
142 goto dealloc_usb2_hcd;
143 }
144
145 /*
146 * Set the xHCI pointer before xhci_plat_setup() (aka hcd_driver.reset)
147 * is called by usb_add_hcd().
148 */
149 *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci;
150
151 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
152 if (ret)
153 goto put_usb3_hcd;
154
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200155 phy = usb_get_transceiver();
156 if (phy && phy->otg) {
157 dev_dbg(&pdev->dev, "%s otg support available\n", __func__);
158 hcd->driver->stop(hcd);
159 ret = otg_set_host(phy->otg, &hcd->self);
160 if (ret) {
161 dev_err(&pdev->dev, "%s otg_set_host failed\n",
162 __func__);
163 usb_put_transceiver(phy);
164 goto put_usb3_hcd;
165 }
166 }
167
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200168 return 0;
169
170put_usb3_hcd:
171 usb_put_hcd(xhci->shared_hcd);
172
173dealloc_usb2_hcd:
174 usb_remove_hcd(hcd);
175
176unmap_registers:
177 iounmap(hcd->regs);
178
179release_mem_region:
180 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
181
182put_hcd:
183 usb_put_hcd(hcd);
184
185 return ret;
186}
187
188static int xhci_plat_remove(struct platform_device *dev)
189{
190 struct usb_hcd *hcd = platform_get_drvdata(dev);
191 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
192
193 usb_remove_hcd(xhci->shared_hcd);
194 usb_put_hcd(xhci->shared_hcd);
195
196 usb_remove_hcd(hcd);
197 iounmap(hcd->regs);
198 usb_put_hcd(hcd);
199 kfree(xhci);
200
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200201 if (phy && phy->otg) {
202 otg_set_host(phy->otg, NULL);
203 usb_put_transceiver(phy);
204 }
205
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200206 return 0;
207}
208
209static struct platform_driver usb_xhci_driver = {
210 .probe = xhci_plat_probe,
211 .remove = xhci_plat_remove,
212 .driver = {
213 .name = "xhci-hcd",
214 },
215};
216MODULE_ALIAS("platform:xhci-hcd");
217
218int xhci_register_plat(void)
219{
220 return platform_driver_register(&usb_xhci_driver);
221}
222
223void xhci_unregister_plat(void)
224{
225 platform_driver_unregister(&usb_xhci_driver);
226}