blob: ed07ec04a9622526b94666acc4f7390bcac56e25 [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * dwc3-pci.c - PCI Specific glue layer
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
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>
Stephen Rothwell46a57282011-08-23 15:08:54 +100040#include <linux/module.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030041#include <linux/slab.h>
42#include <linux/pci.h>
43#include <linux/platform_device.h>
44
Felipe Balbie3ec3eb2012-07-19 13:50:44 +030045#include <linux/usb/otg.h>
46#include <linux/usb/nop-usb-xceiv.h>
47
Felipe Balbi72246da2011-08-19 18:10:58 +030048/* FIXME define these in <linux/pci_ids.h> */
49#define PCI_VENDOR_ID_SYNOPSYS 0x16c3
50#define PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3 0xabcd
51
Felipe Balbi72246da2011-08-19 18:10:58 +030052struct dwc3_pci {
53 struct device *dev;
54 struct platform_device *dwc3;
Felipe Balbie3ec3eb2012-07-19 13:50:44 +030055 struct platform_device *usb2_phy;
56 struct platform_device *usb3_phy;
Felipe Balbi72246da2011-08-19 18:10:58 +030057};
58
Bill Pemberton41ac7b32012-11-19 13:21:48 -050059static int dwc3_pci_register_phys(struct dwc3_pci *glue)
Felipe Balbie3ec3eb2012-07-19 13:50:44 +030060{
61 struct nop_usb_xceiv_platform_data pdata;
62 struct platform_device *pdev;
63 int ret;
64
65 memset(&pdata, 0x00, sizeof(pdata));
66
67 pdev = platform_device_alloc("nop_usb_xceiv", 0);
68 if (!pdev)
69 return -ENOMEM;
70
71 glue->usb2_phy = pdev;
72 pdata.type = USB_PHY_TYPE_USB2;
73
74 ret = platform_device_add_data(glue->usb2_phy, &pdata, sizeof(pdata));
75 if (ret)
76 goto err1;
77
78 pdev = platform_device_alloc("nop_usb_xceiv", 1);
79 if (!pdev) {
80 ret = -ENOMEM;
81 goto err1;
82 }
83
84 glue->usb3_phy = pdev;
85 pdata.type = USB_PHY_TYPE_USB3;
86
87 ret = platform_device_add_data(glue->usb3_phy, &pdata, sizeof(pdata));
88 if (ret)
89 goto err2;
90
91 ret = platform_device_add(glue->usb2_phy);
92 if (ret)
93 goto err2;
94
95 ret = platform_device_add(glue->usb3_phy);
96 if (ret)
97 goto err3;
98
99 return 0;
100
101err3:
102 platform_device_del(glue->usb2_phy);
103
104err2:
105 platform_device_put(glue->usb3_phy);
106
107err1:
108 platform_device_put(glue->usb2_phy);
109
110 return ret;
111}
112
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500113static int dwc3_pci_probe(struct pci_dev *pci,
Felipe Balbi72246da2011-08-19 18:10:58 +0300114 const struct pci_device_id *id)
115{
116 struct resource res[2];
117 struct platform_device *dwc3;
118 struct dwc3_pci *glue;
119 int ret = -ENOMEM;
Chanho Park802ca852012-02-15 18:27:55 +0900120 struct device *dev = &pci->dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300121
Chanho Park802ca852012-02-15 18:27:55 +0900122 glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300123 if (!glue) {
Chanho Park802ca852012-02-15 18:27:55 +0900124 dev_err(dev, "not enough memory\n");
125 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300126 }
127
Paul Zimmerman1d046792012-02-15 18:56:56 -0800128 glue->dev = dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300129
130 ret = pci_enable_device(pci);
131 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900132 dev_err(dev, "failed to enable pci device\n");
133 return -ENODEV;
Felipe Balbi72246da2011-08-19 18:10:58 +0300134 }
135
Felipe Balbi72246da2011-08-19 18:10:58 +0300136 pci_set_master(pci);
137
Felipe Balbie3ec3eb2012-07-19 13:50:44 +0300138 ret = dwc3_pci_register_phys(glue);
139 if (ret) {
140 dev_err(dev, "couldn't register PHYs\n");
141 return ret;
142 }
143
Sebastian Andrzej Siewior124dafd2012-10-29 18:09:53 +0100144 dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO);
Felipe Balbi72246da2011-08-19 18:10:58 +0300145 if (!dwc3) {
Chanho Park802ca852012-02-15 18:27:55 +0900146 dev_err(dev, "couldn't allocate dwc3 device\n");
Felipe Balbi28f1a0d2012-03-12 16:41:19 +0200147 ret = -ENOMEM;
Chanho Park802ca852012-02-15 18:27:55 +0900148 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300149 }
150
151 memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
152
153 res[0].start = pci_resource_start(pci, 0);
154 res[0].end = pci_resource_end(pci, 0);
155 res[0].name = "dwc_usb3";
156 res[0].flags = IORESOURCE_MEM;
157
158 res[1].start = pci->irq;
159 res[1].name = "dwc_usb3";
160 res[1].flags = IORESOURCE_IRQ;
161
162 ret = platform_device_add_resources(dwc3, res, ARRAY_SIZE(res));
163 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900164 dev_err(dev, "couldn't add resources to dwc3 device\n");
Sebastian Andrzej Siewior124dafd2012-10-29 18:09:53 +0100165 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300166 }
167
168 pci_set_drvdata(pci, glue);
169
Chanho Park802ca852012-02-15 18:27:55 +0900170 dma_set_coherent_mask(&dwc3->dev, dev->coherent_dma_mask);
Felipe Balbi72246da2011-08-19 18:10:58 +0300171
Chanho Park802ca852012-02-15 18:27:55 +0900172 dwc3->dev.dma_mask = dev->dma_mask;
173 dwc3->dev.dma_parms = dev->dma_parms;
174 dwc3->dev.parent = dev;
Paul Zimmerman1d046792012-02-15 18:56:56 -0800175 glue->dwc3 = dwc3;
Felipe Balbi72246da2011-08-19 18:10:58 +0300176
177 ret = platform_device_add(dwc3);
178 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900179 dev_err(dev, "failed to register dwc3 device\n");
180 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +0300181 }
182
183 return 0;
184
Chanho Park802ca852012-02-15 18:27:55 +0900185err3:
Felipe Balbi72246da2011-08-19 18:10:58 +0300186 pci_set_drvdata(pci, NULL);
187 platform_device_put(dwc3);
Chanho Park802ca852012-02-15 18:27:55 +0900188err1:
Felipe Balbi72246da2011-08-19 18:10:58 +0300189 pci_disable_device(pci);
190
Felipe Balbi72246da2011-08-19 18:10:58 +0300191 return ret;
192}
193
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500194static void dwc3_pci_remove(struct pci_dev *pci)
Felipe Balbi72246da2011-08-19 18:10:58 +0300195{
196 struct dwc3_pci *glue = pci_get_drvdata(pci);
197
Peter Chenf28c42c2013-05-24 14:29:20 +0800198 platform_device_unregister(glue->dwc3);
Felipe Balbie3ec3eb2012-07-19 13:50:44 +0300199 platform_device_unregister(glue->usb2_phy);
200 platform_device_unregister(glue->usb3_phy);
Felipe Balbi72246da2011-08-19 18:10:58 +0300201 pci_set_drvdata(pci, NULL);
202 pci_disable_device(pci);
Felipe Balbi72246da2011-08-19 18:10:58 +0300203}
204
205static DEFINE_PCI_DEVICE_TABLE(dwc3_pci_id_table) = {
206 {
207 PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
208 PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3),
209 },
210 { } /* Terminating Entry */
211};
212MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
213
Felipe Balbi68907a72013-02-11 11:36:22 +0200214#ifdef CONFIG_PM
215static int dwc3_pci_suspend(struct device *dev)
216{
217 struct pci_dev *pci = to_pci_dev(dev);
218
219 pci_disable_device(pci);
220
221 return 0;
222}
223
224static int dwc3_pci_resume(struct device *dev)
225{
226 struct pci_dev *pci = to_pci_dev(dev);
227 int ret;
228
229 ret = pci_enable_device(pci);
230 if (ret) {
231 dev_err(dev, "can't re-enable device --> %d\n", ret);
232 return ret;
233 }
234
235 pci_set_master(pci);
236
237 return 0;
238}
239
240static const struct dev_pm_ops dwc3_pci_dev_pm_ops = {
241 SET_SYSTEM_SLEEP_PM_OPS(dwc3_pci_suspend, dwc3_pci_resume)
242};
243
244#define DEV_PM_OPS (&dwc3_pci_dev_pm_ops)
245#else
246#define DEV_PM_OPS NULL
247#endif /* CONFIG_PM */
248
Felipe Balbi72246da2011-08-19 18:10:58 +0300249static struct pci_driver dwc3_pci_driver = {
Felipe Balbi0949e992011-10-12 10:44:56 +0300250 .name = "dwc3-pci",
Felipe Balbi72246da2011-08-19 18:10:58 +0300251 .id_table = dwc3_pci_id_table,
252 .probe = dwc3_pci_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500253 .remove = dwc3_pci_remove,
Felipe Balbi68907a72013-02-11 11:36:22 +0200254 .driver = {
255 .pm = DEV_PM_OPS,
256 },
Felipe Balbi72246da2011-08-19 18:10:58 +0300257};
258
259MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
260MODULE_LICENSE("Dual BSD/GPL");
261MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
262
Greg Kroah-Hartman95656332011-11-18 10:14:24 -0800263module_pci_driver(dwc3_pci_driver);