Commit | Line | Data |
---|---|---|
72246da4 FB |
1 | /** |
2 | * dwc3-pci.c - PCI Specific glue layer | |
3 | * | |
4 | * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com | |
72246da4 FB |
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> | |
46a57283 | 40 | #include <linux/module.h> |
72246da4 FB |
41 | #include <linux/slab.h> |
42 | #include <linux/pci.h> | |
43 | #include <linux/platform_device.h> | |
44 | ||
e3ec3eb7 FB |
45 | #include <linux/usb/otg.h> |
46 | #include <linux/usb/nop-usb-xceiv.h> | |
47 | ||
72246da4 FB |
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 | ||
72246da4 FB |
52 | struct dwc3_pci { |
53 | struct device *dev; | |
54 | struct platform_device *dwc3; | |
e3ec3eb7 FB |
55 | struct platform_device *usb2_phy; |
56 | struct platform_device *usb3_phy; | |
72246da4 FB |
57 | }; |
58 | ||
41ac7b3a | 59 | static int dwc3_pci_register_phys(struct dwc3_pci *glue) |
e3ec3eb7 FB |
60 | { |
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 | ||
101 | err3: | |
102 | platform_device_del(glue->usb2_phy); | |
103 | ||
104 | err2: | |
105 | platform_device_put(glue->usb3_phy); | |
106 | ||
107 | err1: | |
108 | platform_device_put(glue->usb2_phy); | |
109 | ||
110 | return ret; | |
111 | } | |
112 | ||
41ac7b3a | 113 | static int dwc3_pci_probe(struct pci_dev *pci, |
72246da4 FB |
114 | 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; | |
802ca850 | 120 | struct device *dev = &pci->dev; |
72246da4 | 121 | |
802ca850 | 122 | glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL); |
72246da4 | 123 | if (!glue) { |
802ca850 CP |
124 | dev_err(dev, "not enough memory\n"); |
125 | return -ENOMEM; | |
72246da4 FB |
126 | } |
127 | ||
1d046793 | 128 | glue->dev = dev; |
72246da4 FB |
129 | |
130 | ret = pci_enable_device(pci); | |
131 | if (ret) { | |
802ca850 CP |
132 | dev_err(dev, "failed to enable pci device\n"); |
133 | return -ENODEV; | |
72246da4 FB |
134 | } |
135 | ||
136 | pci_set_power_state(pci, PCI_D0); | |
137 | pci_set_master(pci); | |
138 | ||
e3ec3eb7 FB |
139 | ret = dwc3_pci_register_phys(glue); |
140 | if (ret) { | |
141 | dev_err(dev, "couldn't register PHYs\n"); | |
142 | return ret; | |
143 | } | |
144 | ||
124dafde | 145 | dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO); |
72246da4 | 146 | if (!dwc3) { |
802ca850 | 147 | dev_err(dev, "couldn't allocate dwc3 device\n"); |
28f1a0d9 | 148 | ret = -ENOMEM; |
802ca850 | 149 | goto err1; |
72246da4 FB |
150 | } |
151 | ||
152 | memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res)); | |
153 | ||
154 | res[0].start = pci_resource_start(pci, 0); | |
155 | res[0].end = pci_resource_end(pci, 0); | |
156 | res[0].name = "dwc_usb3"; | |
157 | res[0].flags = IORESOURCE_MEM; | |
158 | ||
159 | res[1].start = pci->irq; | |
160 | res[1].name = "dwc_usb3"; | |
161 | res[1].flags = IORESOURCE_IRQ; | |
162 | ||
163 | ret = platform_device_add_resources(dwc3, res, ARRAY_SIZE(res)); | |
164 | if (ret) { | |
802ca850 | 165 | dev_err(dev, "couldn't add resources to dwc3 device\n"); |
124dafde | 166 | goto err1; |
72246da4 FB |
167 | } |
168 | ||
169 | pci_set_drvdata(pci, glue); | |
170 | ||
802ca850 | 171 | dma_set_coherent_mask(&dwc3->dev, dev->coherent_dma_mask); |
72246da4 | 172 | |
802ca850 CP |
173 | dwc3->dev.dma_mask = dev->dma_mask; |
174 | dwc3->dev.dma_parms = dev->dma_parms; | |
175 | dwc3->dev.parent = dev; | |
1d046793 | 176 | glue->dwc3 = dwc3; |
72246da4 FB |
177 | |
178 | ret = platform_device_add(dwc3); | |
179 | if (ret) { | |
802ca850 CP |
180 | dev_err(dev, "failed to register dwc3 device\n"); |
181 | goto err3; | |
72246da4 FB |
182 | } |
183 | ||
184 | return 0; | |
185 | ||
802ca850 | 186 | err3: |
72246da4 FB |
187 | pci_set_drvdata(pci, NULL); |
188 | platform_device_put(dwc3); | |
72246da4 | 189 | err1: |
802ca850 | 190 | pci_disable_device(pci); |
72246da4 | 191 | |
72246da4 FB |
192 | return ret; |
193 | } | |
194 | ||
fb4e98ab | 195 | static void dwc3_pci_remove(struct pci_dev *pci) |
72246da4 FB |
196 | { |
197 | struct dwc3_pci *glue = pci_get_drvdata(pci); | |
198 | ||
e3ec3eb7 FB |
199 | platform_device_unregister(glue->usb2_phy); |
200 | platform_device_unregister(glue->usb3_phy); | |
72246da4 FB |
201 | platform_device_unregister(glue->dwc3); |
202 | pci_set_drvdata(pci, NULL); | |
203 | pci_disable_device(pci); | |
72246da4 FB |
204 | } |
205 | ||
206 | static DEFINE_PCI_DEVICE_TABLE(dwc3_pci_id_table) = { | |
207 | { | |
208 | PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS, | |
209 | PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3), | |
210 | }, | |
211 | { } /* Terminating Entry */ | |
212 | }; | |
213 | MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table); | |
214 | ||
215 | static struct pci_driver dwc3_pci_driver = { | |
0949e99b | 216 | .name = "dwc3-pci", |
72246da4 FB |
217 | .id_table = dwc3_pci_id_table, |
218 | .probe = dwc3_pci_probe, | |
7690417d | 219 | .remove = dwc3_pci_remove, |
72246da4 FB |
220 | }; |
221 | ||
222 | MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); | |
223 | MODULE_LICENSE("Dual BSD/GPL"); | |
224 | MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer"); | |
225 | ||
95656336 | 226 | module_pci_driver(dwc3_pci_driver); |