Merge remote-tracking branch 'lsk/v3.10/topic/arm64-misc' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / usb / dwc3 / dwc3-pci.c
1 /**
2  * dwc3-pci.c - PCI Specific glue layer
3  *
4  * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
5  *
6  * Authors: Felipe Balbi <balbi@ti.com>,
7  *          Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions, and the following disclaimer,
14  *    without modification.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The names of the above-listed copyright holders may not be used
19  *    to endorse or promote products derived from this software without
20  *    specific prior written permission.
21  *
22  * ALTERNATIVELY, this software may be distributed under the terms of the
23  * GNU General Public License ("GPL") version 2, as published by the Free
24  * Software Foundation.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */
38
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/slab.h>
42 #include <linux/pci.h>
43 #include <linux/platform_device.h>
44
45 #include <linux/usb/otg.h>
46 #include <linux/usb/nop-usb-xceiv.h>
47
48 /* FIXME define these in <linux/pci_ids.h> */
49 #define PCI_VENDOR_ID_SYNOPSYS          0x16c3
50 #define PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3 0xabcd
51 #define PCI_DEVICE_ID_INTEL_BYT         0x0f37
52 #define PCI_DEVICE_ID_INTEL_MRFLD       0x119e
53
54 struct dwc3_pci {
55         struct device           *dev;
56         struct platform_device  *dwc3;
57         struct platform_device  *usb2_phy;
58         struct platform_device  *usb3_phy;
59 };
60
61 static int dwc3_pci_register_phys(struct dwc3_pci *glue)
62 {
63         struct nop_usb_xceiv_platform_data pdata;
64         struct platform_device  *pdev;
65         int                     ret;
66
67         memset(&pdata, 0x00, sizeof(pdata));
68
69         pdev = platform_device_alloc("nop_usb_xceiv", 0);
70         if (!pdev)
71                 return -ENOMEM;
72
73         glue->usb2_phy = pdev;
74         pdata.type = USB_PHY_TYPE_USB2;
75
76         ret = platform_device_add_data(glue->usb2_phy, &pdata, sizeof(pdata));
77         if (ret)
78                 goto err1;
79
80         pdev = platform_device_alloc("nop_usb_xceiv", 1);
81         if (!pdev) {
82                 ret = -ENOMEM;
83                 goto err1;
84         }
85
86         glue->usb3_phy = pdev;
87         pdata.type = USB_PHY_TYPE_USB3;
88
89         ret = platform_device_add_data(glue->usb3_phy, &pdata, sizeof(pdata));
90         if (ret)
91                 goto err2;
92
93         ret = platform_device_add(glue->usb2_phy);
94         if (ret)
95                 goto err2;
96
97         ret = platform_device_add(glue->usb3_phy);
98         if (ret)
99                 goto err3;
100
101         return 0;
102
103 err3:
104         platform_device_del(glue->usb2_phy);
105
106 err2:
107         platform_device_put(glue->usb3_phy);
108
109 err1:
110         platform_device_put(glue->usb2_phy);
111
112         return ret;
113 }
114
115 static int dwc3_pci_probe(struct pci_dev *pci,
116                 const struct pci_device_id *id)
117 {
118         struct resource         res[2];
119         struct platform_device  *dwc3;
120         struct dwc3_pci         *glue;
121         int                     ret = -ENOMEM;
122         struct device           *dev = &pci->dev;
123
124         glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
125         if (!glue) {
126                 dev_err(dev, "not enough memory\n");
127                 return -ENOMEM;
128         }
129
130         glue->dev = dev;
131
132         ret = pci_enable_device(pci);
133         if (ret) {
134                 dev_err(dev, "failed to enable pci device\n");
135                 return -ENODEV;
136         }
137
138         pci_set_power_state(pci, PCI_D0);
139         pci_set_master(pci);
140
141         ret = dwc3_pci_register_phys(glue);
142         if (ret) {
143                 dev_err(dev, "couldn't register PHYs\n");
144                 return ret;
145         }
146
147         dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO);
148         if (!dwc3) {
149                 dev_err(dev, "couldn't allocate dwc3 device\n");
150                 ret = -ENOMEM;
151                 goto err1;
152         }
153
154         memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
155
156         res[0].start    = pci_resource_start(pci, 0);
157         res[0].end      = pci_resource_end(pci, 0);
158         res[0].name     = "dwc_usb3";
159         res[0].flags    = IORESOURCE_MEM;
160
161         res[1].start    = pci->irq;
162         res[1].name     = "dwc_usb3";
163         res[1].flags    = IORESOURCE_IRQ;
164
165         ret = platform_device_add_resources(dwc3, res, ARRAY_SIZE(res));
166         if (ret) {
167                 dev_err(dev, "couldn't add resources to dwc3 device\n");
168                 goto err1;
169         }
170
171         pci_set_drvdata(pci, glue);
172
173         dma_set_coherent_mask(&dwc3->dev, dev->coherent_dma_mask);
174
175         dwc3->dev.dma_mask = dev->dma_mask;
176         dwc3->dev.dma_parms = dev->dma_parms;
177         dwc3->dev.parent = dev;
178         glue->dwc3 = dwc3;
179
180         ret = platform_device_add(dwc3);
181         if (ret) {
182                 dev_err(dev, "failed to register dwc3 device\n");
183                 goto err3;
184         }
185
186         return 0;
187
188 err3:
189         pci_set_drvdata(pci, NULL);
190         platform_device_put(dwc3);
191 err1:
192         pci_disable_device(pci);
193
194         return ret;
195 }
196
197 static void dwc3_pci_remove(struct pci_dev *pci)
198 {
199         struct dwc3_pci *glue = pci_get_drvdata(pci);
200
201         platform_device_unregister(glue->dwc3);
202         platform_device_unregister(glue->usb2_phy);
203         platform_device_unregister(glue->usb3_phy);
204         pci_set_drvdata(pci, NULL);
205         pci_disable_device(pci);
206 }
207
208 static DEFINE_PCI_DEVICE_TABLE(dwc3_pci_id_table) = {
209         {
210                 PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
211                                 PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3),
212         },
213         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BYT), },
214         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_MRFLD), },
215         {  }    /* Terminating Entry */
216 };
217 MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
218
219 #ifdef CONFIG_PM
220 static int dwc3_pci_suspend(struct device *dev)
221 {
222         struct pci_dev  *pci = to_pci_dev(dev);
223
224         pci_disable_device(pci);
225
226         return 0;
227 }
228
229 static int dwc3_pci_resume(struct device *dev)
230 {
231         struct pci_dev  *pci = to_pci_dev(dev);
232         int             ret;
233
234         ret = pci_enable_device(pci);
235         if (ret) {
236                 dev_err(dev, "can't re-enable device --> %d\n", ret);
237                 return ret;
238         }
239
240         pci_set_master(pci);
241
242         return 0;
243 }
244
245 static const struct dev_pm_ops dwc3_pci_dev_pm_ops = {
246         SET_SYSTEM_SLEEP_PM_OPS(dwc3_pci_suspend, dwc3_pci_resume)
247 };
248
249 #define DEV_PM_OPS      (&dwc3_pci_dev_pm_ops)
250 #else
251 #define DEV_PM_OPS      NULL
252 #endif /* CONFIG_PM */
253
254 static struct pci_driver dwc3_pci_driver = {
255         .name           = "dwc3-pci",
256         .id_table       = dwc3_pci_id_table,
257         .probe          = dwc3_pci_probe,
258         .remove         = dwc3_pci_remove,
259         .driver         = {
260                 .pm     = DEV_PM_OPS,
261         },
262 };
263
264 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
265 MODULE_LICENSE("Dual BSD/GPL");
266 MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
267
268 module_pci_driver(dwc3_pci_driver);