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