blob: 94f550e37f986950f35c28e1e53d8773b4156031 [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 Balbi8300dd22011-10-18 13:54:01 +030048#include "core.h"
49
Felipe Balbi72246da2011-08-19 18:10:58 +030050/* FIXME define these in <linux/pci_ids.h> */
51#define PCI_VENDOR_ID_SYNOPSYS 0x16c3
52#define PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3 0xabcd
53
Felipe Balbi72246da2011-08-19 18:10:58 +030054struct dwc3_pci {
55 struct device *dev;
56 struct platform_device *dwc3;
Felipe Balbie3ec3eb2012-07-19 13:50:44 +030057 struct platform_device *usb2_phy;
58 struct platform_device *usb3_phy;
Felipe Balbi72246da2011-08-19 18:10:58 +030059};
60
Felipe Balbie3ec3eb2012-07-19 13:50:44 +030061static int __devinit 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
103err3:
104 platform_device_del(glue->usb2_phy);
105
106err2:
107 platform_device_put(glue->usb3_phy);
108
109err1:
110 platform_device_put(glue->usb2_phy);
111
112 return ret;
113}
114
Felipe Balbi72246da2011-08-19 18:10:58 +0300115static int __devinit 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 int devid;
Chanho Park802ca852012-02-15 18:27:55 +0900123 struct device *dev = &pci->dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300124
Chanho Park802ca852012-02-15 18:27:55 +0900125 glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300126 if (!glue) {
Chanho Park802ca852012-02-15 18:27:55 +0900127 dev_err(dev, "not enough memory\n");
128 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300129 }
130
Paul Zimmerman1d046792012-02-15 18:56:56 -0800131 glue->dev = dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300132
133 ret = pci_enable_device(pci);
134 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900135 dev_err(dev, "failed to enable pci device\n");
136 return -ENODEV;
Felipe Balbi72246da2011-08-19 18:10:58 +0300137 }
138
139 pci_set_power_state(pci, PCI_D0);
140 pci_set_master(pci);
141
Felipe Balbie3ec3eb2012-07-19 13:50:44 +0300142 ret = dwc3_pci_register_phys(glue);
143 if (ret) {
144 dev_err(dev, "couldn't register PHYs\n");
145 return ret;
146 }
147
Felipe Balbi8300dd22011-10-18 13:54:01 +0300148 devid = dwc3_get_device_id();
Paul Zimmerman7d26b582012-02-24 17:32:14 -0800149 if (devid < 0) {
150 ret = -ENOMEM;
Chanho Park802ca852012-02-15 18:27:55 +0900151 goto err1;
Paul Zimmerman7d26b582012-02-24 17:32:14 -0800152 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300153
Felipe Balbi0949e992011-10-12 10:44:56 +0300154 dwc3 = platform_device_alloc("dwc3", devid);
Felipe Balbi72246da2011-08-19 18:10:58 +0300155 if (!dwc3) {
Chanho Park802ca852012-02-15 18:27:55 +0900156 dev_err(dev, "couldn't allocate dwc3 device\n");
Felipe Balbi28f1a0d2012-03-12 16:41:19 +0200157 ret = -ENOMEM;
Chanho Park802ca852012-02-15 18:27:55 +0900158 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300159 }
160
161 memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
162
163 res[0].start = pci_resource_start(pci, 0);
164 res[0].end = pci_resource_end(pci, 0);
165 res[0].name = "dwc_usb3";
166 res[0].flags = IORESOURCE_MEM;
167
168 res[1].start = pci->irq;
169 res[1].name = "dwc_usb3";
170 res[1].flags = IORESOURCE_IRQ;
171
172 ret = platform_device_add_resources(dwc3, res, ARRAY_SIZE(res));
173 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900174 dev_err(dev, "couldn't add resources to dwc3 device\n");
175 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +0300176 }
177
178 pci_set_drvdata(pci, glue);
179
Chanho Park802ca852012-02-15 18:27:55 +0900180 dma_set_coherent_mask(&dwc3->dev, dev->coherent_dma_mask);
Felipe Balbi72246da2011-08-19 18:10:58 +0300181
Chanho Park802ca852012-02-15 18:27:55 +0900182 dwc3->dev.dma_mask = dev->dma_mask;
183 dwc3->dev.dma_parms = dev->dma_parms;
184 dwc3->dev.parent = dev;
Paul Zimmerman1d046792012-02-15 18:56:56 -0800185 glue->dwc3 = dwc3;
Felipe Balbi72246da2011-08-19 18:10:58 +0300186
187 ret = platform_device_add(dwc3);
188 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900189 dev_err(dev, "failed to register dwc3 device\n");
190 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +0300191 }
192
193 return 0;
194
Chanho Park802ca852012-02-15 18:27:55 +0900195err3:
Felipe Balbi72246da2011-08-19 18:10:58 +0300196 pci_set_drvdata(pci, NULL);
197 platform_device_put(dwc3);
198
Chanho Park802ca852012-02-15 18:27:55 +0900199err2:
Felipe Balbi8300dd22011-10-18 13:54:01 +0300200 dwc3_put_device_id(devid);
Felipe Balbi72246da2011-08-19 18:10:58 +0300201
Chanho Park802ca852012-02-15 18:27:55 +0900202err1:
Felipe Balbi72246da2011-08-19 18:10:58 +0300203 pci_disable_device(pci);
204
Felipe Balbi72246da2011-08-19 18:10:58 +0300205 return ret;
206}
207
208static void __devexit dwc3_pci_remove(struct pci_dev *pci)
209{
210 struct dwc3_pci *glue = pci_get_drvdata(pci);
211
Felipe Balbie3ec3eb2012-07-19 13:50:44 +0300212 platform_device_unregister(glue->usb2_phy);
213 platform_device_unregister(glue->usb3_phy);
Felipe Balbi8300dd22011-10-18 13:54:01 +0300214 dwc3_put_device_id(glue->dwc3->id);
Felipe Balbi72246da2011-08-19 18:10:58 +0300215 platform_device_unregister(glue->dwc3);
216 pci_set_drvdata(pci, NULL);
217 pci_disable_device(pci);
Felipe Balbi72246da2011-08-19 18:10:58 +0300218}
219
220static DEFINE_PCI_DEVICE_TABLE(dwc3_pci_id_table) = {
221 {
222 PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
223 PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3),
224 },
225 { } /* Terminating Entry */
226};
227MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
228
229static struct pci_driver dwc3_pci_driver = {
Felipe Balbi0949e992011-10-12 10:44:56 +0300230 .name = "dwc3-pci",
Felipe Balbi72246da2011-08-19 18:10:58 +0300231 .id_table = dwc3_pci_id_table,
232 .probe = dwc3_pci_probe,
233 .remove = __devexit_p(dwc3_pci_remove),
234};
235
236MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
237MODULE_LICENSE("Dual BSD/GPL");
238MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
239
Greg Kroah-Hartman95656332011-11-18 10:14:24 -0800240module_pci_driver(dwc3_pci_driver);