blob: 1a0cf9f31e4375b9260cfd149164bc8c29f00c5a [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
Gregory CLEMENT4718c172014-05-15 12:17:32 +020014#include <linux/clk.h>
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +030015#include <linux/dma-mapping.h>
Gregory CLEMENT48157bb2014-05-15 12:17:31 +020016#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/platform_device.h>
19#include <linux/slab.h>
Pratyush Anand20f6fdd2014-07-04 17:01:25 +030020#include <linux/usb/xhci_pdriver.h>
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +020021
22#include "xhci.h"
Gregory CLEMENT97374792014-05-15 12:17:33 +020023#include "xhci-mvebu.h"
Yoshihiro Shimoda4ac89182014-07-09 10:08:52 +090024#include "xhci-rcar.h"
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +020025
26static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
27{
28 /*
29 * As of now platform drivers don't provide MSI support so we ensure
30 * here that the generic code does not try to make a pci_dev from our
31 * dev struct in order to setup MSI
32 */
Sarah Sharp52fb6122013-08-08 10:08:34 -070033 xhci->quirks |= XHCI_PLAT;
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +020034}
35
36/* called during probe() after chip reset completes */
37static int xhci_plat_setup(struct usb_hcd *hcd)
38{
Yoshihiro Shimoda4ac89182014-07-09 10:08:52 +090039 struct device_node *of_node = hcd->self.controller->of_node;
40 int ret;
41
42 if (of_device_is_compatible(of_node, "renesas,xhci-r8a7790") ||
43 of_device_is_compatible(of_node, "renesas,xhci-r8a7791")) {
44 ret = xhci_rcar_init_quirk(hcd);
45 if (ret)
46 return ret;
47 }
48
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +020049 return xhci_gen_setup(hcd, xhci_plat_quirks);
50}
51
Yoshihiro Shimoda94adcdc2014-05-28 20:22:58 +090052static int xhci_plat_start(struct usb_hcd *hcd)
53{
Yoshihiro Shimoda4ac89182014-07-09 10:08:52 +090054 struct device_node *of_node = hcd->self.controller->of_node;
55
56 if (of_device_is_compatible(of_node, "renesas,xhci-r8a7790") ||
57 of_device_is_compatible(of_node, "renesas,xhci-r8a7791"))
58 xhci_rcar_start(hcd);
59
Yoshihiro Shimoda94adcdc2014-05-28 20:22:58 +090060 return xhci_run(hcd);
61}
62
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +020063static const struct hc_driver xhci_plat_xhci_driver = {
64 .description = "xhci-hcd",
65 .product_desc = "xHCI Host Controller",
66 .hcd_priv_size = sizeof(struct xhci_hcd *),
67
68 /*
69 * generic hardware linkage
70 */
71 .irq = xhci_irq,
72 .flags = HCD_MEMORY | HCD_USB3 | HCD_SHARED,
73
74 /*
75 * basic lifecycle operations
76 */
77 .reset = xhci_plat_setup,
Yoshihiro Shimoda94adcdc2014-05-28 20:22:58 +090078 .start = xhci_plat_start,
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +020079 .stop = xhci_stop,
80 .shutdown = xhci_shutdown,
81
82 /*
83 * managing i/o requests and associated device resources
84 */
85 .urb_enqueue = xhci_urb_enqueue,
86 .urb_dequeue = xhci_urb_dequeue,
87 .alloc_dev = xhci_alloc_dev,
88 .free_dev = xhci_free_dev,
89 .alloc_streams = xhci_alloc_streams,
90 .free_streams = xhci_free_streams,
91 .add_endpoint = xhci_add_endpoint,
92 .drop_endpoint = xhci_drop_endpoint,
93 .endpoint_reset = xhci_endpoint_reset,
94 .check_bandwidth = xhci_check_bandwidth,
95 .reset_bandwidth = xhci_reset_bandwidth,
96 .address_device = xhci_address_device,
Dan Williams48fc7db2013-12-05 17:07:27 -080097 .enable_device = xhci_enable_device,
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +020098 .update_hub_device = xhci_update_hub_device,
99 .reset_device = xhci_discover_or_reset_device,
100
101 /*
102 * scheduling support
103 */
104 .get_frame_number = xhci_get_frame,
105
106 /* Root hub support */
107 .hub_control = xhci_hub_control,
108 .hub_status_data = xhci_hub_status_data,
109 .bus_suspend = xhci_bus_suspend,
110 .bus_resume = xhci_bus_resume,
Pratyush Anand94ef3d52014-07-04 17:01:24 +0300111
112 .enable_usb3_lpm_timeout = xhci_enable_usb3_lpm_timeout,
113 .disable_usb3_lpm_timeout = xhci_disable_usb3_lpm_timeout,
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200114};
115
116static int xhci_plat_probe(struct platform_device *pdev)
117{
Pratyush Anand20f6fdd2014-07-04 17:01:25 +0300118 struct device_node *node = pdev->dev.of_node;
119 struct usb_xhci_pdata *pdata = dev_get_platdata(&pdev->dev);
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200120 const struct hc_driver *driver;
121 struct xhci_hcd *xhci;
122 struct resource *res;
123 struct usb_hcd *hcd;
Gregory CLEMENT4718c172014-05-15 12:17:32 +0200124 struct clk *clk;
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200125 int ret;
126 int irq;
127
128 if (usb_disabled())
129 return -ENODEV;
130
131 driver = &xhci_plat_xhci_driver;
132
133 irq = platform_get_irq(pdev, 0);
134 if (irq < 0)
135 return -ENODEV;
136
137 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
138 if (!res)
139 return -ENODEV;
140
Gregory CLEMENT97374792014-05-15 12:17:33 +0200141 if (of_device_is_compatible(pdev->dev.of_node,
142 "marvell,armada-375-xhci") ||
143 of_device_is_compatible(pdev->dev.of_node,
144 "marvell,armada-380-xhci")) {
145 ret = xhci_mvebu_mbus_init_quirk(pdev);
146 if (ret)
147 return ret;
148 }
149
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +0300150 /* Initialize dma_mask and coherent_dma_mask to 32-bits */
151 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
152 if (ret)
153 return ret;
154 if (!pdev->dev.dma_mask)
155 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
156 else
157 dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
158
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200159 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
160 if (!hcd)
161 return -ENOMEM;
162
163 hcd->rsrc_start = res->start;
164 hcd->rsrc_len = resource_size(res);
165
Himangi Saraogifd666342014-06-20 23:11:23 +0530166 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
167 if (IS_ERR(hcd->regs)) {
168 ret = PTR_ERR(hcd->regs);
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200169 goto put_hcd;
170 }
171
Gregory CLEMENT4718c172014-05-15 12:17:32 +0200172 /*
173 * Not all platforms have a clk so it is not an error if the
174 * clock does not exists.
175 */
176 clk = devm_clk_get(&pdev->dev, NULL);
177 if (!IS_ERR(clk)) {
178 ret = clk_prepare_enable(clk);
179 if (ret)
Himangi Saraogifd666342014-06-20 23:11:23 +0530180 goto put_hcd;
Gregory CLEMENT4718c172014-05-15 12:17:32 +0200181 }
182
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200183 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
184 if (ret)
Gregory CLEMENT4718c172014-05-15 12:17:32 +0200185 goto disable_clk;
186
Peter Chen3c9740a2013-11-05 10:46:02 +0800187 device_wakeup_enable(hcd->self.controller);
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200188
189 /* USB 2.0 roothub is stored in the platform_device now. */
Jingoo Han477527b2013-05-23 19:18:39 +0900190 hcd = platform_get_drvdata(pdev);
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200191 xhci = hcd_to_xhci(hcd);
Gregory CLEMENT4718c172014-05-15 12:17:32 +0200192 xhci->clk = clk;
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200193 xhci->shared_hcd = usb_create_shared_hcd(driver, &pdev->dev,
194 dev_name(&pdev->dev), hcd);
195 if (!xhci->shared_hcd) {
196 ret = -ENOMEM;
197 goto dealloc_usb2_hcd;
198 }
199
Pratyush Anand20f6fdd2014-07-04 17:01:25 +0300200 if ((node && of_property_read_bool(node, "usb3-lpm-capable")) ||
201 (pdata && pdata->usb3_lpm_capable))
202 xhci->quirks |= XHCI_LPM_SUPPORT;
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200203 /*
204 * Set the xHCI pointer before xhci_plat_setup() (aka hcd_driver.reset)
205 * is called by usb_add_hcd().
206 */
207 *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci;
208
Oliver Neukum14aec582014-02-11 20:36:04 +0100209 if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
210 xhci->shared_hcd->can_do_streams = 1;
211
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200212 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
213 if (ret)
214 goto put_usb3_hcd;
215
216 return 0;
217
218put_usb3_hcd:
219 usb_put_hcd(xhci->shared_hcd);
220
221dealloc_usb2_hcd:
222 usb_remove_hcd(hcd);
223
Gregory CLEMENT4718c172014-05-15 12:17:32 +0200224disable_clk:
225 if (!IS_ERR(clk))
226 clk_disable_unprepare(clk);
227
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200228put_hcd:
229 usb_put_hcd(hcd);
230
231 return ret;
232}
233
234static int xhci_plat_remove(struct platform_device *dev)
235{
236 struct usb_hcd *hcd = platform_get_drvdata(dev);
237 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Gregory CLEMENT4718c172014-05-15 12:17:32 +0200238 struct clk *clk = xhci->clk;
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200239
240 usb_remove_hcd(xhci->shared_hcd);
241 usb_put_hcd(xhci->shared_hcd);
242
243 usb_remove_hcd(hcd);
Gregory CLEMENT4718c172014-05-15 12:17:32 +0200244 if (!IS_ERR(clk))
245 clk_disable_unprepare(clk);
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200246 usb_put_hcd(hcd);
247 kfree(xhci);
248
249 return 0;
250}
251
Arnd Bergmann274f6af2014-05-08 15:52:19 +0200252#ifdef CONFIG_PM_SLEEP
Vikas Sajjan57d04eb2013-02-11 12:58:00 +0200253static int xhci_plat_suspend(struct device *dev)
254{
255 struct usb_hcd *hcd = dev_get_drvdata(dev);
256 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
257
258 return xhci_suspend(xhci);
259}
260
261static int xhci_plat_resume(struct device *dev)
262{
263 struct usb_hcd *hcd = dev_get_drvdata(dev);
264 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
265
266 return xhci_resume(xhci, 0);
267}
268
269static const struct dev_pm_ops xhci_plat_pm_ops = {
270 SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
271};
272#define DEV_PM_OPS (&xhci_plat_pm_ops)
273#else
274#define DEV_PM_OPS NULL
275#endif /* CONFIG_PM */
276
Al Cooper1fe6c452013-07-25 19:04:44 -0400277#ifdef CONFIG_OF
278static const struct of_device_id usb_xhci_of_match[] = {
Hans de Goede0f943882014-02-11 17:54:46 +0100279 { .compatible = "generic-xhci" },
Al Cooper1fe6c452013-07-25 19:04:44 -0400280 { .compatible = "xhci-platform" },
Gregory CLEMENT97374792014-05-15 12:17:33 +0200281 { .compatible = "marvell,armada-375-xhci"},
282 { .compatible = "marvell,armada-380-xhci"},
Yoshihiro Shimoda4ac89182014-07-09 10:08:52 +0900283 { .compatible = "renesas,xhci-r8a7790"},
284 { .compatible = "renesas,xhci-r8a7791"},
Al Cooper1fe6c452013-07-25 19:04:44 -0400285 { },
286};
287MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
288#endif
289
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200290static struct platform_driver usb_xhci_driver = {
291 .probe = xhci_plat_probe,
292 .remove = xhci_plat_remove,
293 .driver = {
294 .name = "xhci-hcd",
Vikas Sajjan57d04eb2013-02-11 12:58:00 +0200295 .pm = DEV_PM_OPS,
Al Cooper1fe6c452013-07-25 19:04:44 -0400296 .of_match_table = of_match_ptr(usb_xhci_of_match),
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200297 },
298};
299MODULE_ALIAS("platform:xhci-hcd");
300
301int xhci_register_plat(void)
302{
303 return platform_driver_register(&usb_xhci_driver);
304}
305
306void xhci_unregister_plat(void)
307{
308 platform_driver_unregister(&usb_xhci_driver);
309}