Commit | Line | Data |
---|---|---|
72246da4 FB |
1 | /** |
2 | * core.c - DesignWare USB3 DRD Controller Core file | |
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 | ||
a72e658b | 39 | #include <linux/module.h> |
72246da4 FB |
40 | #include <linux/kernel.h> |
41 | #include <linux/slab.h> | |
42 | #include <linux/spinlock.h> | |
43 | #include <linux/platform_device.h> | |
44 | #include <linux/pm_runtime.h> | |
45 | #include <linux/interrupt.h> | |
46 | #include <linux/ioport.h> | |
47 | #include <linux/io.h> | |
48 | #include <linux/list.h> | |
49 | #include <linux/delay.h> | |
50 | #include <linux/dma-mapping.h> | |
457e84b6 | 51 | #include <linux/of.h> |
72246da4 | 52 | |
51e1e7bc | 53 | #include <linux/usb/otg.h> |
72246da4 FB |
54 | #include <linux/usb/ch9.h> |
55 | #include <linux/usb/gadget.h> | |
56 | ||
57 | #include "core.h" | |
58 | #include "gadget.h" | |
59 | #include "io.h" | |
60 | ||
61 | #include "debug.h" | |
62 | ||
6c167fc9 FB |
63 | static char *maximum_speed = "super"; |
64 | module_param(maximum_speed, charp, 0); | |
65 | MODULE_PARM_DESC(maximum_speed, "Maximum supported speed."); | |
66 | ||
8300dd23 FB |
67 | /* -------------------------------------------------------------------------- */ |
68 | ||
3140e8cb SAS |
69 | void dwc3_set_mode(struct dwc3 *dwc, u32 mode) |
70 | { | |
71 | u32 reg; | |
72 | ||
73 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); | |
74 | reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG)); | |
75 | reg |= DWC3_GCTL_PRTCAPDIR(mode); | |
76 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); | |
77 | } | |
8300dd23 | 78 | |
72246da4 FB |
79 | /** |
80 | * dwc3_core_soft_reset - Issues core soft reset and PHY reset | |
81 | * @dwc: pointer to our context structure | |
82 | */ | |
83 | static void dwc3_core_soft_reset(struct dwc3 *dwc) | |
84 | { | |
85 | u32 reg; | |
86 | ||
87 | /* Before Resetting PHY, put Core in Reset */ | |
88 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); | |
89 | reg |= DWC3_GCTL_CORESOFTRESET; | |
90 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); | |
91 | ||
92 | /* Assert USB3 PHY reset */ | |
93 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); | |
94 | reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST; | |
95 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); | |
96 | ||
97 | /* Assert USB2 PHY reset */ | |
98 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); | |
99 | reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST; | |
100 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); | |
101 | ||
51e1e7bc FB |
102 | usb_phy_init(dwc->usb2_phy); |
103 | usb_phy_init(dwc->usb3_phy); | |
72246da4 FB |
104 | mdelay(100); |
105 | ||
106 | /* Clear USB3 PHY reset */ | |
107 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); | |
108 | reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST; | |
109 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); | |
110 | ||
111 | /* Clear USB2 PHY reset */ | |
112 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); | |
113 | reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST; | |
114 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); | |
115 | ||
45627ac6 PA |
116 | mdelay(100); |
117 | ||
72246da4 FB |
118 | /* After PHYs are stable we can take Core out of reset state */ |
119 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); | |
120 | reg &= ~DWC3_GCTL_CORESOFTRESET; | |
121 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); | |
122 | } | |
123 | ||
124 | /** | |
125 | * dwc3_free_one_event_buffer - Frees one event buffer | |
126 | * @dwc: Pointer to our controller context structure | |
127 | * @evt: Pointer to event buffer to be freed | |
128 | */ | |
129 | static void dwc3_free_one_event_buffer(struct dwc3 *dwc, | |
130 | struct dwc3_event_buffer *evt) | |
131 | { | |
132 | dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma); | |
72246da4 FB |
133 | } |
134 | ||
135 | /** | |
1d046793 | 136 | * dwc3_alloc_one_event_buffer - Allocates one event buffer structure |
72246da4 FB |
137 | * @dwc: Pointer to our controller context structure |
138 | * @length: size of the event buffer | |
139 | * | |
1d046793 | 140 | * Returns a pointer to the allocated event buffer structure on success |
72246da4 FB |
141 | * otherwise ERR_PTR(errno). |
142 | */ | |
67d0b500 FB |
143 | static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc, |
144 | unsigned length) | |
72246da4 FB |
145 | { |
146 | struct dwc3_event_buffer *evt; | |
147 | ||
380f0d28 | 148 | evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL); |
72246da4 FB |
149 | if (!evt) |
150 | return ERR_PTR(-ENOMEM); | |
151 | ||
152 | evt->dwc = dwc; | |
153 | evt->length = length; | |
154 | evt->buf = dma_alloc_coherent(dwc->dev, length, | |
155 | &evt->dma, GFP_KERNEL); | |
e32672f0 | 156 | if (!evt->buf) |
72246da4 | 157 | return ERR_PTR(-ENOMEM); |
72246da4 FB |
158 | |
159 | return evt; | |
160 | } | |
161 | ||
162 | /** | |
163 | * dwc3_free_event_buffers - frees all allocated event buffers | |
164 | * @dwc: Pointer to our controller context structure | |
165 | */ | |
166 | static void dwc3_free_event_buffers(struct dwc3 *dwc) | |
167 | { | |
168 | struct dwc3_event_buffer *evt; | |
169 | int i; | |
170 | ||
9f622b2a | 171 | for (i = 0; i < dwc->num_event_buffers; i++) { |
72246da4 | 172 | evt = dwc->ev_buffs[i]; |
64b6c8a7 | 173 | if (evt) |
72246da4 | 174 | dwc3_free_one_event_buffer(dwc, evt); |
72246da4 FB |
175 | } |
176 | } | |
177 | ||
178 | /** | |
179 | * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length | |
1d046793 | 180 | * @dwc: pointer to our controller context structure |
72246da4 FB |
181 | * @length: size of event buffer |
182 | * | |
1d046793 | 183 | * Returns 0 on success otherwise negative errno. In the error case, dwc |
72246da4 FB |
184 | * may contain some buffers allocated but not all which were requested. |
185 | */ | |
41ac7b3a | 186 | static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length) |
72246da4 | 187 | { |
9f622b2a | 188 | int num; |
72246da4 FB |
189 | int i; |
190 | ||
9f622b2a FB |
191 | num = DWC3_NUM_INT(dwc->hwparams.hwparams1); |
192 | dwc->num_event_buffers = num; | |
193 | ||
380f0d28 FB |
194 | dwc->ev_buffs = devm_kzalloc(dwc->dev, sizeof(*dwc->ev_buffs) * num, |
195 | GFP_KERNEL); | |
457d3f21 FB |
196 | if (!dwc->ev_buffs) { |
197 | dev_err(dwc->dev, "can't allocate event buffers array\n"); | |
198 | return -ENOMEM; | |
199 | } | |
200 | ||
72246da4 FB |
201 | for (i = 0; i < num; i++) { |
202 | struct dwc3_event_buffer *evt; | |
203 | ||
204 | evt = dwc3_alloc_one_event_buffer(dwc, length); | |
205 | if (IS_ERR(evt)) { | |
206 | dev_err(dwc->dev, "can't allocate event buffer\n"); | |
207 | return PTR_ERR(evt); | |
208 | } | |
209 | dwc->ev_buffs[i] = evt; | |
210 | } | |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
215 | /** | |
216 | * dwc3_event_buffers_setup - setup our allocated event buffers | |
1d046793 | 217 | * @dwc: pointer to our controller context structure |
72246da4 FB |
218 | * |
219 | * Returns 0 on success otherwise negative errno. | |
220 | */ | |
7acd85e0 | 221 | static int dwc3_event_buffers_setup(struct dwc3 *dwc) |
72246da4 FB |
222 | { |
223 | struct dwc3_event_buffer *evt; | |
224 | int n; | |
225 | ||
9f622b2a | 226 | for (n = 0; n < dwc->num_event_buffers; n++) { |
72246da4 FB |
227 | evt = dwc->ev_buffs[n]; |
228 | dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n", | |
229 | evt->buf, (unsigned long long) evt->dma, | |
230 | evt->length); | |
231 | ||
7acd85e0 PZ |
232 | evt->lpos = 0; |
233 | ||
72246da4 FB |
234 | dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), |
235 | lower_32_bits(evt->dma)); | |
236 | dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), | |
237 | upper_32_bits(evt->dma)); | |
238 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), | |
239 | evt->length & 0xffff); | |
240 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0); | |
241 | } | |
242 | ||
243 | return 0; | |
244 | } | |
245 | ||
246 | static void dwc3_event_buffers_cleanup(struct dwc3 *dwc) | |
247 | { | |
248 | struct dwc3_event_buffer *evt; | |
249 | int n; | |
250 | ||
9f622b2a | 251 | for (n = 0; n < dwc->num_event_buffers; n++) { |
72246da4 | 252 | evt = dwc->ev_buffs[n]; |
7acd85e0 PZ |
253 | |
254 | evt->lpos = 0; | |
255 | ||
72246da4 FB |
256 | dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0); |
257 | dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0); | |
258 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0); | |
259 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0); | |
260 | } | |
261 | } | |
262 | ||
41ac7b3a | 263 | static void dwc3_cache_hwparams(struct dwc3 *dwc) |
26ceca97 FB |
264 | { |
265 | struct dwc3_hwparams *parms = &dwc->hwparams; | |
266 | ||
267 | parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0); | |
268 | parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1); | |
269 | parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2); | |
270 | parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3); | |
271 | parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4); | |
272 | parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5); | |
273 | parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6); | |
274 | parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7); | |
275 | parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8); | |
276 | } | |
277 | ||
72246da4 FB |
278 | /** |
279 | * dwc3_core_init - Low-level initialization of DWC3 Core | |
280 | * @dwc: Pointer to our controller context structure | |
281 | * | |
282 | * Returns 0 on success otherwise negative errno. | |
283 | */ | |
41ac7b3a | 284 | static int dwc3_core_init(struct dwc3 *dwc) |
72246da4 FB |
285 | { |
286 | unsigned long timeout; | |
287 | u32 reg; | |
288 | int ret; | |
289 | ||
7650bd74 SAS |
290 | reg = dwc3_readl(dwc->regs, DWC3_GSNPSID); |
291 | /* This should read as U3 followed by revision number */ | |
292 | if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) { | |
293 | dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n"); | |
294 | ret = -ENODEV; | |
295 | goto err0; | |
296 | } | |
248b122b | 297 | dwc->revision = reg; |
7650bd74 | 298 | |
72246da4 FB |
299 | /* issue device SoftReset too */ |
300 | timeout = jiffies + msecs_to_jiffies(500); | |
301 | dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST); | |
302 | do { | |
303 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
304 | if (!(reg & DWC3_DCTL_CSFTRST)) | |
305 | break; | |
306 | ||
307 | if (time_after(jiffies, timeout)) { | |
308 | dev_err(dwc->dev, "Reset Timed Out\n"); | |
309 | ret = -ETIMEDOUT; | |
310 | goto err0; | |
311 | } | |
312 | ||
313 | cpu_relax(); | |
314 | } while (true); | |
315 | ||
58a0f23f PA |
316 | dwc3_core_soft_reset(dwc); |
317 | ||
4878a028 | 318 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
3e87c42a | 319 | reg &= ~DWC3_GCTL_SCALEDOWN_MASK; |
4878a028 SAS |
320 | reg &= ~DWC3_GCTL_DISSCRAMBLE; |
321 | ||
164d7731 | 322 | switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) { |
4878a028 SAS |
323 | case DWC3_GHWPARAMS1_EN_PWROPT_CLK: |
324 | reg &= ~DWC3_GCTL_DSBLCLKGTNG; | |
325 | break; | |
326 | default: | |
327 | dev_dbg(dwc->dev, "No power optimization available\n"); | |
328 | } | |
329 | ||
330 | /* | |
331 | * WORKAROUND: DWC3 revisions <1.90a have a bug | |
1d046793 | 332 | * where the device can fail to connect at SuperSpeed |
4878a028 | 333 | * and falls back to high-speed mode which causes |
1d046793 | 334 | * the device to enter a Connect/Disconnect loop |
4878a028 SAS |
335 | */ |
336 | if (dwc->revision < DWC3_REVISION_190A) | |
337 | reg |= DWC3_GCTL_U2RSTECN; | |
338 | ||
339 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); | |
340 | ||
72246da4 FB |
341 | return 0; |
342 | ||
72246da4 FB |
343 | err0: |
344 | return ret; | |
345 | } | |
346 | ||
347 | static void dwc3_core_exit(struct dwc3 *dwc) | |
348 | { | |
01b8daf7 VG |
349 | usb_phy_shutdown(dwc->usb2_phy); |
350 | usb_phy_shutdown(dwc->usb3_phy); | |
72246da4 FB |
351 | } |
352 | ||
353 | #define DWC3_ALIGN_MASK (16 - 1) | |
354 | ||
41ac7b3a | 355 | static int dwc3_probe(struct platform_device *pdev) |
72246da4 | 356 | { |
457e84b6 | 357 | struct device_node *node = pdev->dev.of_node; |
72246da4 FB |
358 | struct resource *res; |
359 | struct dwc3 *dwc; | |
802ca850 | 360 | struct device *dev = &pdev->dev; |
0949e99b | 361 | |
72246da4 | 362 | int ret = -ENOMEM; |
0949e99b FB |
363 | |
364 | void __iomem *regs; | |
72246da4 FB |
365 | void *mem; |
366 | ||
0949e99b FB |
367 | u8 mode; |
368 | ||
802ca850 | 369 | mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL); |
72246da4 | 370 | if (!mem) { |
802ca850 CP |
371 | dev_err(dev, "not enough memory\n"); |
372 | return -ENOMEM; | |
72246da4 FB |
373 | } |
374 | dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1); | |
375 | dwc->mem = mem; | |
376 | ||
51249dca | 377 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
72246da4 | 378 | if (!res) { |
51249dca | 379 | dev_err(dev, "missing IRQ\n"); |
802ca850 | 380 | return -ENODEV; |
72246da4 | 381 | } |
066618bc KVA |
382 | dwc->xhci_resources[1].start = res->start; |
383 | dwc->xhci_resources[1].end = res->end; | |
384 | dwc->xhci_resources[1].flags = res->flags; | |
385 | dwc->xhci_resources[1].name = res->name; | |
72246da4 | 386 | |
51249dca IS |
387 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
388 | if (!res) { | |
389 | dev_err(dev, "missing memory resource\n"); | |
390 | return -ENODEV; | |
391 | } | |
066618bc | 392 | dwc->xhci_resources[0].start = res->start; |
51249dca IS |
393 | dwc->xhci_resources[0].end = dwc->xhci_resources[0].start + |
394 | DWC3_XHCI_REGS_END; | |
066618bc KVA |
395 | dwc->xhci_resources[0].flags = res->flags; |
396 | dwc->xhci_resources[0].name = res->name; | |
51249dca IS |
397 | |
398 | /* | |
399 | * Request memory region but exclude xHCI regs, | |
400 | * since it will be requested by the xhci-plat driver. | |
401 | */ | |
402 | res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START, | |
403 | resource_size(res) - DWC3_GLOBALS_REGS_START, | |
802ca850 | 404 | dev_name(dev)); |
72246da4 | 405 | if (!res) { |
802ca850 CP |
406 | dev_err(dev, "can't request mem region\n"); |
407 | return -ENOMEM; | |
72246da4 FB |
408 | } |
409 | ||
b7e38aa6 | 410 | regs = devm_ioremap_nocache(dev, res->start, resource_size(res)); |
72246da4 | 411 | if (!regs) { |
802ca850 CP |
412 | dev_err(dev, "ioremap failed\n"); |
413 | return -ENOMEM; | |
72246da4 FB |
414 | } |
415 | ||
5088b6f5 KVA |
416 | if (node) { |
417 | dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0); | |
418 | dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1); | |
419 | } else { | |
420 | dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); | |
421 | dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3); | |
422 | } | |
423 | ||
51e1e7bc FB |
424 | if (IS_ERR_OR_NULL(dwc->usb2_phy)) { |
425 | dev_err(dev, "no usb2 phy configured\n"); | |
426 | return -EPROBE_DEFER; | |
427 | } | |
428 | ||
51e1e7bc FB |
429 | if (IS_ERR_OR_NULL(dwc->usb3_phy)) { |
430 | dev_err(dev, "no usb3 phy configured\n"); | |
431 | return -EPROBE_DEFER; | |
432 | } | |
433 | ||
8ba007a9 KVA |
434 | usb_phy_set_suspend(dwc->usb2_phy, 0); |
435 | usb_phy_set_suspend(dwc->usb3_phy, 0); | |
436 | ||
72246da4 FB |
437 | spin_lock_init(&dwc->lock); |
438 | platform_set_drvdata(pdev, dwc); | |
439 | ||
440 | dwc->regs = regs; | |
441 | dwc->regs_size = resource_size(res); | |
802ca850 | 442 | dwc->dev = dev; |
72246da4 | 443 | |
6c167fc9 FB |
444 | if (!strncmp("super", maximum_speed, 5)) |
445 | dwc->maximum_speed = DWC3_DCFG_SUPERSPEED; | |
446 | else if (!strncmp("high", maximum_speed, 4)) | |
447 | dwc->maximum_speed = DWC3_DCFG_HIGHSPEED; | |
448 | else if (!strncmp("full", maximum_speed, 4)) | |
449 | dwc->maximum_speed = DWC3_DCFG_FULLSPEED1; | |
450 | else if (!strncmp("low", maximum_speed, 3)) | |
451 | dwc->maximum_speed = DWC3_DCFG_LOWSPEED; | |
452 | else | |
453 | dwc->maximum_speed = DWC3_DCFG_SUPERSPEED; | |
454 | ||
5088b6f5 | 455 | dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize"); |
457e84b6 | 456 | |
802ca850 CP |
457 | pm_runtime_enable(dev); |
458 | pm_runtime_get_sync(dev); | |
459 | pm_runtime_forbid(dev); | |
72246da4 | 460 | |
4fd24483 KVA |
461 | dwc3_cache_hwparams(dwc); |
462 | ||
3921426b FB |
463 | ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE); |
464 | if (ret) { | |
465 | dev_err(dwc->dev, "failed to allocate event buffers\n"); | |
466 | ret = -ENOMEM; | |
467 | goto err0; | |
468 | } | |
469 | ||
72246da4 FB |
470 | ret = dwc3_core_init(dwc); |
471 | if (ret) { | |
802ca850 | 472 | dev_err(dev, "failed to initialize core\n"); |
3921426b | 473 | goto err0; |
72246da4 FB |
474 | } |
475 | ||
f122d33e FB |
476 | ret = dwc3_event_buffers_setup(dwc); |
477 | if (ret) { | |
478 | dev_err(dwc->dev, "failed to setup event buffers\n"); | |
479 | goto err1; | |
480 | } | |
481 | ||
cd051da2 VG |
482 | if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) |
483 | mode = DWC3_MODE_HOST; | |
484 | else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) | |
485 | mode = DWC3_MODE_DEVICE; | |
486 | else | |
487 | mode = DWC3_MODE_DRD; | |
0949e99b FB |
488 | |
489 | switch (mode) { | |
0949e99b | 490 | case DWC3_MODE_DEVICE: |
3140e8cb | 491 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE); |
72246da4 FB |
492 | ret = dwc3_gadget_init(dwc); |
493 | if (ret) { | |
802ca850 | 494 | dev_err(dev, "failed to initialize gadget\n"); |
f122d33e | 495 | goto err2; |
72246da4 | 496 | } |
d07e8819 FB |
497 | break; |
498 | case DWC3_MODE_HOST: | |
3140e8cb | 499 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST); |
d07e8819 FB |
500 | ret = dwc3_host_init(dwc); |
501 | if (ret) { | |
802ca850 | 502 | dev_err(dev, "failed to initialize host\n"); |
f122d33e | 503 | goto err2; |
d07e8819 FB |
504 | } |
505 | break; | |
506 | case DWC3_MODE_DRD: | |
3140e8cb | 507 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG); |
d07e8819 FB |
508 | ret = dwc3_host_init(dwc); |
509 | if (ret) { | |
802ca850 | 510 | dev_err(dev, "failed to initialize host\n"); |
f122d33e | 511 | goto err2; |
d07e8819 FB |
512 | } |
513 | ||
72246da4 FB |
514 | ret = dwc3_gadget_init(dwc); |
515 | if (ret) { | |
802ca850 | 516 | dev_err(dev, "failed to initialize gadget\n"); |
f122d33e | 517 | goto err2; |
72246da4 | 518 | } |
0949e99b FB |
519 | break; |
520 | default: | |
802ca850 | 521 | dev_err(dev, "Unsupported mode of operation %d\n", mode); |
f122d33e | 522 | goto err2; |
72246da4 | 523 | } |
0949e99b | 524 | dwc->mode = mode; |
72246da4 FB |
525 | |
526 | ret = dwc3_debugfs_init(dwc); | |
527 | if (ret) { | |
802ca850 | 528 | dev_err(dev, "failed to initialize debugfs\n"); |
f122d33e | 529 | goto err3; |
72246da4 FB |
530 | } |
531 | ||
802ca850 | 532 | pm_runtime_allow(dev); |
72246da4 FB |
533 | |
534 | return 0; | |
535 | ||
f122d33e | 536 | err3: |
0949e99b | 537 | switch (mode) { |
0949e99b | 538 | case DWC3_MODE_DEVICE: |
72246da4 | 539 | dwc3_gadget_exit(dwc); |
0949e99b | 540 | break; |
d07e8819 FB |
541 | case DWC3_MODE_HOST: |
542 | dwc3_host_exit(dwc); | |
543 | break; | |
544 | case DWC3_MODE_DRD: | |
545 | dwc3_host_exit(dwc); | |
72246da4 | 546 | dwc3_gadget_exit(dwc); |
d07e8819 | 547 | break; |
0949e99b FB |
548 | default: |
549 | /* do nothing */ | |
550 | break; | |
551 | } | |
72246da4 | 552 | |
f122d33e FB |
553 | err2: |
554 | dwc3_event_buffers_cleanup(dwc); | |
555 | ||
72246da4 | 556 | err1: |
802ca850 | 557 | dwc3_core_exit(dwc); |
72246da4 | 558 | |
3921426b FB |
559 | err0: |
560 | dwc3_free_event_buffers(dwc); | |
561 | ||
72246da4 FB |
562 | return ret; |
563 | } | |
564 | ||
fb4e98ab | 565 | static int dwc3_remove(struct platform_device *pdev) |
72246da4 | 566 | { |
72246da4 | 567 | struct dwc3 *dwc = platform_get_drvdata(pdev); |
72246da4 | 568 | |
8ba007a9 KVA |
569 | usb_phy_set_suspend(dwc->usb2_phy, 1); |
570 | usb_phy_set_suspend(dwc->usb3_phy, 1); | |
571 | ||
72246da4 FB |
572 | pm_runtime_put(&pdev->dev); |
573 | pm_runtime_disable(&pdev->dev); | |
574 | ||
575 | dwc3_debugfs_exit(dwc); | |
576 | ||
0949e99b | 577 | switch (dwc->mode) { |
0949e99b | 578 | case DWC3_MODE_DEVICE: |
72246da4 | 579 | dwc3_gadget_exit(dwc); |
0949e99b | 580 | break; |
d07e8819 FB |
581 | case DWC3_MODE_HOST: |
582 | dwc3_host_exit(dwc); | |
583 | break; | |
584 | case DWC3_MODE_DRD: | |
585 | dwc3_host_exit(dwc); | |
72246da4 | 586 | dwc3_gadget_exit(dwc); |
d07e8819 | 587 | break; |
0949e99b FB |
588 | default: |
589 | /* do nothing */ | |
590 | break; | |
591 | } | |
72246da4 | 592 | |
f122d33e | 593 | dwc3_event_buffers_cleanup(dwc); |
d9b4330a | 594 | dwc3_free_event_buffers(dwc); |
72246da4 | 595 | dwc3_core_exit(dwc); |
72246da4 FB |
596 | |
597 | return 0; | |
598 | } | |
599 | ||
7415f17c FB |
600 | #ifdef CONFIG_PM |
601 | static int dwc3_prepare(struct device *dev) | |
602 | { | |
603 | struct dwc3 *dwc = dev_get_drvdata(dev); | |
604 | unsigned long flags; | |
605 | ||
606 | spin_lock_irqsave(&dwc->lock, flags); | |
607 | ||
608 | switch (dwc->mode) { | |
609 | case DWC3_MODE_DEVICE: | |
610 | case DWC3_MODE_DRD: | |
611 | dwc3_gadget_prepare(dwc); | |
612 | /* FALLTHROUGH */ | |
613 | case DWC3_MODE_HOST: | |
614 | default: | |
615 | dwc3_event_buffers_cleanup(dwc); | |
616 | break; | |
617 | } | |
618 | ||
619 | spin_unlock_irqrestore(&dwc->lock, flags); | |
620 | ||
621 | return 0; | |
622 | } | |
623 | ||
624 | static void dwc3_complete(struct device *dev) | |
625 | { | |
626 | struct dwc3 *dwc = dev_get_drvdata(dev); | |
627 | unsigned long flags; | |
628 | ||
629 | spin_lock_irqsave(&dwc->lock, flags); | |
630 | ||
631 | switch (dwc->mode) { | |
632 | case DWC3_MODE_DEVICE: | |
633 | case DWC3_MODE_DRD: | |
634 | dwc3_gadget_complete(dwc); | |
635 | /* FALLTHROUGH */ | |
636 | case DWC3_MODE_HOST: | |
637 | default: | |
638 | dwc3_event_buffers_setup(dwc); | |
639 | break; | |
640 | } | |
641 | ||
642 | spin_unlock_irqrestore(&dwc->lock, flags); | |
643 | } | |
644 | ||
645 | static int dwc3_suspend(struct device *dev) | |
646 | { | |
647 | struct dwc3 *dwc = dev_get_drvdata(dev); | |
648 | unsigned long flags; | |
649 | ||
650 | spin_lock_irqsave(&dwc->lock, flags); | |
651 | ||
652 | switch (dwc->mode) { | |
653 | case DWC3_MODE_DEVICE: | |
654 | case DWC3_MODE_DRD: | |
655 | dwc3_gadget_suspend(dwc); | |
656 | /* FALLTHROUGH */ | |
657 | case DWC3_MODE_HOST: | |
658 | default: | |
659 | /* do nothing */ | |
660 | break; | |
661 | } | |
662 | ||
663 | dwc->gctl = dwc3_readl(dwc->regs, DWC3_GCTL); | |
664 | spin_unlock_irqrestore(&dwc->lock, flags); | |
665 | ||
666 | usb_phy_shutdown(dwc->usb3_phy); | |
667 | usb_phy_shutdown(dwc->usb2_phy); | |
668 | ||
669 | return 0; | |
670 | } | |
671 | ||
672 | static int dwc3_resume(struct device *dev) | |
673 | { | |
674 | struct dwc3 *dwc = dev_get_drvdata(dev); | |
675 | unsigned long flags; | |
676 | ||
677 | usb_phy_init(dwc->usb3_phy); | |
678 | usb_phy_init(dwc->usb2_phy); | |
679 | msleep(100); | |
680 | ||
681 | spin_lock_irqsave(&dwc->lock, flags); | |
682 | ||
683 | dwc3_writel(dwc->regs, DWC3_GCTL, dwc->gctl); | |
684 | ||
685 | switch (dwc->mode) { | |
686 | case DWC3_MODE_DEVICE: | |
687 | case DWC3_MODE_DRD: | |
688 | dwc3_gadget_resume(dwc); | |
689 | /* FALLTHROUGH */ | |
690 | case DWC3_MODE_HOST: | |
691 | default: | |
692 | /* do nothing */ | |
693 | break; | |
694 | } | |
695 | ||
696 | spin_unlock_irqrestore(&dwc->lock, flags); | |
697 | ||
698 | pm_runtime_disable(dev); | |
699 | pm_runtime_set_active(dev); | |
700 | pm_runtime_enable(dev); | |
701 | ||
702 | return 0; | |
703 | } | |
704 | ||
705 | static const struct dev_pm_ops dwc3_dev_pm_ops = { | |
706 | .prepare = dwc3_prepare, | |
707 | .complete = dwc3_complete, | |
708 | ||
709 | SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume) | |
710 | }; | |
711 | ||
712 | #define DWC3_PM_OPS &(dwc3_dev_pm_ops) | |
713 | #else | |
714 | #define DWC3_PM_OPS NULL | |
715 | #endif | |
716 | ||
5088b6f5 KVA |
717 | #ifdef CONFIG_OF |
718 | static const struct of_device_id of_dwc3_match[] = { | |
719 | { | |
720 | .compatible = "synopsys,dwc3" | |
721 | }, | |
722 | { }, | |
723 | }; | |
724 | MODULE_DEVICE_TABLE(of, of_dwc3_match); | |
725 | #endif | |
726 | ||
72246da4 FB |
727 | static struct platform_driver dwc3_driver = { |
728 | .probe = dwc3_probe, | |
7690417d | 729 | .remove = dwc3_remove, |
72246da4 FB |
730 | .driver = { |
731 | .name = "dwc3", | |
5088b6f5 | 732 | .of_match_table = of_match_ptr(of_dwc3_match), |
7415f17c | 733 | .pm = DWC3_PM_OPS, |
72246da4 | 734 | }, |
72246da4 FB |
735 | }; |
736 | ||
b1116dcc TK |
737 | module_platform_driver(dwc3_driver); |
738 | ||
7ae4fc4d | 739 | MODULE_ALIAS("platform:dwc3"); |
72246da4 FB |
740 | MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); |
741 | MODULE_LICENSE("Dual BSD/GPL"); | |
742 | MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver"); |