source: src/linux/universal/linux-3.18/drivers/usb/host/xhci-plat.c @ 31869

Last change on this file since 31869 was 31869, checked in by brainslayer, 6 weeks ago

update

File size: 7.6 KB
Line 
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/usb/phy.h>
20#include <linux/slab.h>
21#include <linux/usb/xhci_pdriver.h>
22
23#include "xhci.h"
24#include "xhci-mvebu.h"
25#include "xhci-rcar.h"
26
27static struct hc_driver __read_mostly xhci_plat_hc_driver;
28
29static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
30{
31        /*
32         * As of now platform drivers don't provide MSI support so we ensure
33         * here that the generic code does not try to make a pci_dev from our
34         * dev struct in order to setup MSI
35         */
36        xhci->quirks |= XHCI_PLAT;
37}
38
39/* called during probe() after chip reset completes */
40static int xhci_plat_setup(struct usb_hcd *hcd)
41{
42        struct device_node *of_node = hcd->self.controller->of_node;
43        int ret;
44
45        if (of_device_is_compatible(of_node, "renesas,xhci-r8a7790") ||
46            of_device_is_compatible(of_node, "renesas,xhci-r8a7791")) {
47                ret = xhci_rcar_init_quirk(hcd);
48                if (ret)
49                        return ret;
50        }
51
52        return xhci_gen_setup(hcd, xhci_plat_quirks);
53}
54
55static int xhci_plat_start(struct usb_hcd *hcd)
56{
57        struct device_node *of_node = hcd->self.controller->of_node;
58
59        if (of_device_is_compatible(of_node, "renesas,xhci-r8a7790") ||
60            of_device_is_compatible(of_node, "renesas,xhci-r8a7791"))
61                xhci_rcar_start(hcd);
62
63        return xhci_run(hcd);
64}
65
66static int xhci_plat_probe(struct platform_device *pdev)
67{
68        struct device_node      *node = pdev->dev.of_node;
69        struct usb_xhci_pdata   *pdata = dev_get_platdata(&pdev->dev);
70        const struct hc_driver  *driver;
71        struct xhci_hcd         *xhci;
72        struct resource         *res;
73        struct usb_hcd          *hcd;
74        struct clk              *clk;
75        int                     ret;
76        int                     irq;
77
78        if (usb_disabled())
79                return -ENODEV;
80
81        driver = &xhci_plat_hc_driver;
82
83        irq = platform_get_irq(pdev, 0);
84        if (irq < 0)
85                return -ENODEV;
86
87        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
88        if (!res)
89                return -ENODEV;
90
91        /* Initialize dma_mask and coherent_dma_mask to 32-bits */
92        ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
93        if (ret)
94                return ret;
95        if (!pdev->dev.dma_mask)
96                pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
97        else
98                dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
99
100        hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
101        if (!hcd)
102                return -ENOMEM;
103
104        hcd->rsrc_start = res->start;
105        hcd->rsrc_len = resource_size(res);
106#if IS_ENABLED(CONFIG_DWMAC_IPQ806X)
107        if (pdata->usb2_susphy_quirk) {
108                hcd->susphy = pdata->susphy;
109        }
110#endif
111        hcd->regs = devm_ioremap_resource(&pdev->dev, res);
112        if (IS_ERR(hcd->regs)) {
113                ret = PTR_ERR(hcd->regs);
114                goto put_hcd;
115        }
116
117        /*
118         * Not all platforms have a clk so it is not an error if the
119         * clock does not exists.
120         */
121        clk = devm_clk_get(&pdev->dev, NULL);
122        if (!IS_ERR(clk)) {
123                ret = clk_prepare_enable(clk);
124                if (ret)
125                        goto put_hcd;
126        } else if (PTR_ERR(clk) == -EPROBE_DEFER) {
127                ret = -EPROBE_DEFER;
128                goto put_hcd;
129        }
130#if IS_ENABLED(CONFIG_USB_XHCI_MVEBU)
131        if (of_device_is_compatible(pdev->dev.of_node,
132                                    "marvell,armada-375-xhci") ||
133            of_device_is_compatible(pdev->dev.of_node,
134                                    "marvell,armada-380-xhci")) {
135                ret = xhci_mvebu_mbus_init_quirk(pdev);
136                if (ret)
137                        goto disable_clk;
138        }
139       
140        if (of_device_is_compatible(pdev->dev.of_node,
141                                     "marvell,armada-380-xhci")) {
142                ret = xhci_mvebu_vbus_init_quirk();
143                if (ret)
144                       goto disable_clk;
145        }
146#endif
147        ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
148        if (ret)
149                goto disable_clk;
150
151        device_wakeup_enable(hcd->self.controller);
152
153        /* USB 2.0 roothub is stored in the platform_device now. */
154        hcd = platform_get_drvdata(pdev);
155        xhci = hcd_to_xhci(hcd);
156        xhci->clk = clk;
157        xhci->shared_hcd = usb_create_shared_hcd(driver, &pdev->dev,
158                        dev_name(&pdev->dev), hcd);
159        if (!xhci->shared_hcd) {
160                ret = -ENOMEM;
161                goto dealloc_usb2_hcd;
162        }
163
164        if ((node && of_property_read_bool(node, "usb3-lpm-capable")) ||
165                        (pdata && pdata->usb3_lpm_capable))
166                xhci->quirks |= XHCI_LPM_SUPPORT;
167        /*
168         * Set the xHCI pointer before xhci_plat_setup() (aka hcd_driver.reset)
169         * is called by usb_add_hcd().
170         */
171        *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci;
172
173        if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
174                xhci->shared_hcd->can_do_streams = 1;
175
176        hcd->usb_phy = devm_usb_get_phy_by_phandle(&pdev->dev, "usb-phy", 0);
177        if (IS_ERR(hcd->usb_phy)) {
178                ret = PTR_ERR(hcd->usb_phy);
179                if (ret == -EPROBE_DEFER)
180                        goto put_usb3_hcd;
181                hcd->usb_phy = NULL;
182        } else {
183                ret = usb_phy_init(hcd->usb_phy);
184                if (ret)
185                        goto put_usb3_hcd;
186        }
187
188        ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
189        if (ret)
190                goto disable_usb_phy;
191
192        return 0;
193
194disable_usb_phy:
195        usb_phy_shutdown(hcd->usb_phy);
196
197put_usb3_hcd:
198        usb_put_hcd(xhci->shared_hcd);
199
200dealloc_usb2_hcd:
201        usb_remove_hcd(hcd);
202
203disable_clk:
204        if (!IS_ERR(clk))
205                clk_disable_unprepare(clk);
206
207put_hcd:
208        usb_put_hcd(hcd);
209
210        return ret;
211}
212
213static int xhci_plat_remove(struct platform_device *dev)
214{
215        struct usb_hcd  *hcd = platform_get_drvdata(dev);
216        struct xhci_hcd *xhci = hcd_to_xhci(hcd);
217        struct clk *clk = xhci->clk;
218
219        xhci->xhc_state |= XHCI_STATE_REMOVING;
220
221        usb_remove_hcd(xhci->shared_hcd);
222        usb_phy_shutdown(hcd->usb_phy);
223        usb_put_hcd(xhci->shared_hcd);
224
225        usb_remove_hcd(hcd);
226        if (!IS_ERR(clk))
227                clk_disable_unprepare(clk);
228        usb_put_hcd(hcd);
229        kfree(xhci);
230
231        return 0;
232}
233
234#ifdef CONFIG_PM_SLEEP
235static int xhci_plat_suspend(struct device *dev)
236{
237        struct usb_hcd  *hcd = dev_get_drvdata(dev);
238        struct xhci_hcd *xhci = hcd_to_xhci(hcd);
239
240        /*
241         * xhci_suspend() needs `do_wakeup` to know whether host is allowed
242         * to do wakeup during suspend. Since xhci_plat_suspend is currently
243         * only designed for system suspend, device_may_wakeup() is enough
244         * to dertermine whether host is allowed to do wakeup. Need to
245         * reconsider this when xhci_plat_suspend enlarges its scope, e.g.,
246         * also applies to runtime suspend.
247         */
248        return xhci_suspend(xhci, device_may_wakeup(dev));
249}
250
251static int xhci_plat_resume(struct device *dev)
252{
253        struct usb_hcd  *hcd = dev_get_drvdata(dev);
254        struct xhci_hcd *xhci = hcd_to_xhci(hcd);
255
256        return xhci_resume(xhci, 0);
257}
258
259static const struct dev_pm_ops xhci_plat_pm_ops = {
260        SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
261};
262#define DEV_PM_OPS      (&xhci_plat_pm_ops)
263#else
264#define DEV_PM_OPS      NULL
265#endif /* CONFIG_PM */
266
267#ifdef CONFIG_OF
268static const struct of_device_id usb_xhci_of_match[] = {
269        { .compatible = "generic-xhci" },
270        { .compatible = "xhci-platform" },
271        { .compatible = "marvell,armada-375-xhci"},
272        { .compatible = "marvell,armada-380-xhci"},
273        { .compatible = "renesas,xhci-r8a7790"},
274        { .compatible = "renesas,xhci-r8a7791"},
275        { },
276};
277MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
278#endif
279
280static struct platform_driver usb_xhci_driver = {
281        .probe  = xhci_plat_probe,
282        .remove = xhci_plat_remove,
283        .driver = {
284                .name = "xhci-hcd",
285                .pm = DEV_PM_OPS,
286                .of_match_table = of_match_ptr(usb_xhci_of_match),
287        },
288};
289MODULE_ALIAS("platform:xhci-hcd");
290
291static int __init xhci_plat_init(void)
292{
293        xhci_init_driver(&xhci_plat_hc_driver, xhci_plat_setup);
294        xhci_plat_hc_driver.start = xhci_plat_start;
295        return platform_driver_register(&usb_xhci_driver);
296}
297module_init(xhci_plat_init);
298
299static void __exit xhci_plat_exit(void)
300{
301        platform_driver_unregister(&usb_xhci_driver);
302}
303module_exit(xhci_plat_exit);
304
305MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
306MODULE_LICENSE("GPL");
Note: See TracBrowser for help on using the repository browser.