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 | ||
789451f6 FB |
263 | static void dwc3_core_num_eps(struct dwc3 *dwc) |
264 | { | |
265 | struct dwc3_hwparams *parms = &dwc->hwparams; | |
266 | ||
267 | dwc->num_in_eps = DWC3_NUM_IN_EPS(parms); | |
268 | dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps; | |
269 | ||
270 | dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n", | |
271 | dwc->num_in_eps, dwc->num_out_eps); | |
272 | } | |
273 | ||
41ac7b3a | 274 | static void dwc3_cache_hwparams(struct dwc3 *dwc) |
26ceca97 FB |
275 | { |
276 | struct dwc3_hwparams *parms = &dwc->hwparams; | |
277 | ||
278 | parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0); | |
279 | parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1); | |
280 | parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2); | |
281 | parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3); | |
282 | parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4); | |
283 | parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5); | |
284 | parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6); | |
285 | parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7); | |
286 | parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8); | |
287 | } | |
288 | ||
72246da4 FB |
289 | /** |
290 | * dwc3_core_init - Low-level initialization of DWC3 Core | |
291 | * @dwc: Pointer to our controller context structure | |
292 | * | |
293 | * Returns 0 on success otherwise negative errno. | |
294 | */ | |
41ac7b3a | 295 | static int dwc3_core_init(struct dwc3 *dwc) |
72246da4 FB |
296 | { |
297 | unsigned long timeout; | |
298 | u32 reg; | |
299 | int ret; | |
300 | ||
7650bd74 SAS |
301 | reg = dwc3_readl(dwc->regs, DWC3_GSNPSID); |
302 | /* This should read as U3 followed by revision number */ | |
303 | if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) { | |
304 | dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n"); | |
305 | ret = -ENODEV; | |
306 | goto err0; | |
307 | } | |
248b122b | 308 | dwc->revision = reg; |
7650bd74 | 309 | |
72246da4 FB |
310 | /* issue device SoftReset too */ |
311 | timeout = jiffies + msecs_to_jiffies(500); | |
312 | dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST); | |
313 | do { | |
314 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
315 | if (!(reg & DWC3_DCTL_CSFTRST)) | |
316 | break; | |
317 | ||
318 | if (time_after(jiffies, timeout)) { | |
319 | dev_err(dwc->dev, "Reset Timed Out\n"); | |
320 | ret = -ETIMEDOUT; | |
321 | goto err0; | |
322 | } | |
323 | ||
324 | cpu_relax(); | |
325 | } while (true); | |
326 | ||
58a0f23f PA |
327 | dwc3_core_soft_reset(dwc); |
328 | ||
4878a028 | 329 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
3e87c42a | 330 | reg &= ~DWC3_GCTL_SCALEDOWN_MASK; |
4878a028 SAS |
331 | reg &= ~DWC3_GCTL_DISSCRAMBLE; |
332 | ||
164d7731 | 333 | switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) { |
4878a028 SAS |
334 | case DWC3_GHWPARAMS1_EN_PWROPT_CLK: |
335 | reg &= ~DWC3_GCTL_DSBLCLKGTNG; | |
336 | break; | |
337 | default: | |
338 | dev_dbg(dwc->dev, "No power optimization available\n"); | |
339 | } | |
340 | ||
341 | /* | |
342 | * WORKAROUND: DWC3 revisions <1.90a have a bug | |
1d046793 | 343 | * where the device can fail to connect at SuperSpeed |
4878a028 | 344 | * and falls back to high-speed mode which causes |
1d046793 | 345 | * the device to enter a Connect/Disconnect loop |
4878a028 SAS |
346 | */ |
347 | if (dwc->revision < DWC3_REVISION_190A) | |
348 | reg |= DWC3_GCTL_U2RSTECN; | |
349 | ||
789451f6 FB |
350 | dwc3_core_num_eps(dwc); |
351 | ||
4878a028 SAS |
352 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
353 | ||
72246da4 FB |
354 | return 0; |
355 | ||
72246da4 FB |
356 | err0: |
357 | return ret; | |
358 | } | |
359 | ||
360 | static void dwc3_core_exit(struct dwc3 *dwc) | |
361 | { | |
01b8daf7 VG |
362 | usb_phy_shutdown(dwc->usb2_phy); |
363 | usb_phy_shutdown(dwc->usb3_phy); | |
72246da4 FB |
364 | } |
365 | ||
366 | #define DWC3_ALIGN_MASK (16 - 1) | |
367 | ||
41ac7b3a | 368 | static int dwc3_probe(struct platform_device *pdev) |
72246da4 | 369 | { |
457e84b6 | 370 | struct device_node *node = pdev->dev.of_node; |
72246da4 FB |
371 | struct resource *res; |
372 | struct dwc3 *dwc; | |
802ca850 | 373 | struct device *dev = &pdev->dev; |
0949e99b | 374 | |
72246da4 | 375 | int ret = -ENOMEM; |
0949e99b FB |
376 | |
377 | void __iomem *regs; | |
72246da4 FB |
378 | void *mem; |
379 | ||
0949e99b FB |
380 | u8 mode; |
381 | ||
802ca850 | 382 | mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL); |
72246da4 | 383 | if (!mem) { |
802ca850 CP |
384 | dev_err(dev, "not enough memory\n"); |
385 | return -ENOMEM; | |
72246da4 FB |
386 | } |
387 | dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1); | |
388 | dwc->mem = mem; | |
389 | ||
51249dca | 390 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
72246da4 | 391 | if (!res) { |
51249dca | 392 | dev_err(dev, "missing IRQ\n"); |
802ca850 | 393 | return -ENODEV; |
72246da4 | 394 | } |
066618bc KVA |
395 | dwc->xhci_resources[1].start = res->start; |
396 | dwc->xhci_resources[1].end = res->end; | |
397 | dwc->xhci_resources[1].flags = res->flags; | |
398 | dwc->xhci_resources[1].name = res->name; | |
72246da4 | 399 | |
51249dca IS |
400 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
401 | if (!res) { | |
402 | dev_err(dev, "missing memory resource\n"); | |
403 | return -ENODEV; | |
404 | } | |
066618bc | 405 | dwc->xhci_resources[0].start = res->start; |
51249dca IS |
406 | dwc->xhci_resources[0].end = dwc->xhci_resources[0].start + |
407 | DWC3_XHCI_REGS_END; | |
066618bc KVA |
408 | dwc->xhci_resources[0].flags = res->flags; |
409 | dwc->xhci_resources[0].name = res->name; | |
51249dca IS |
410 | |
411 | /* | |
412 | * Request memory region but exclude xHCI regs, | |
413 | * since it will be requested by the xhci-plat driver. | |
414 | */ | |
415 | res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START, | |
416 | resource_size(res) - DWC3_GLOBALS_REGS_START, | |
802ca850 | 417 | dev_name(dev)); |
72246da4 | 418 | if (!res) { |
802ca850 CP |
419 | dev_err(dev, "can't request mem region\n"); |
420 | return -ENOMEM; | |
72246da4 FB |
421 | } |
422 | ||
b7e38aa6 | 423 | regs = devm_ioremap_nocache(dev, res->start, resource_size(res)); |
72246da4 | 424 | if (!regs) { |
802ca850 CP |
425 | dev_err(dev, "ioremap failed\n"); |
426 | return -ENOMEM; | |
72246da4 FB |
427 | } |
428 | ||
5088b6f5 KVA |
429 | if (node) { |
430 | dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0); | |
431 | dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1); | |
432 | } else { | |
433 | dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); | |
434 | dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3); | |
435 | } | |
436 | ||
51e1e7bc FB |
437 | if (IS_ERR_OR_NULL(dwc->usb2_phy)) { |
438 | dev_err(dev, "no usb2 phy configured\n"); | |
439 | return -EPROBE_DEFER; | |
440 | } | |
441 | ||
51e1e7bc FB |
442 | if (IS_ERR_OR_NULL(dwc->usb3_phy)) { |
443 | dev_err(dev, "no usb3 phy configured\n"); | |
444 | return -EPROBE_DEFER; | |
445 | } | |
446 | ||
8ba007a9 KVA |
447 | usb_phy_set_suspend(dwc->usb2_phy, 0); |
448 | usb_phy_set_suspend(dwc->usb3_phy, 0); | |
449 | ||
72246da4 FB |
450 | spin_lock_init(&dwc->lock); |
451 | platform_set_drvdata(pdev, dwc); | |
452 | ||
453 | dwc->regs = regs; | |
454 | dwc->regs_size = resource_size(res); | |
802ca850 | 455 | dwc->dev = dev; |
72246da4 | 456 | |
6c167fc9 FB |
457 | if (!strncmp("super", maximum_speed, 5)) |
458 | dwc->maximum_speed = DWC3_DCFG_SUPERSPEED; | |
459 | else if (!strncmp("high", maximum_speed, 4)) | |
460 | dwc->maximum_speed = DWC3_DCFG_HIGHSPEED; | |
461 | else if (!strncmp("full", maximum_speed, 4)) | |
462 | dwc->maximum_speed = DWC3_DCFG_FULLSPEED1; | |
463 | else if (!strncmp("low", maximum_speed, 3)) | |
464 | dwc->maximum_speed = DWC3_DCFG_LOWSPEED; | |
465 | else | |
466 | dwc->maximum_speed = DWC3_DCFG_SUPERSPEED; | |
467 | ||
5088b6f5 | 468 | dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize"); |
457e84b6 | 469 | |
802ca850 CP |
470 | pm_runtime_enable(dev); |
471 | pm_runtime_get_sync(dev); | |
472 | pm_runtime_forbid(dev); | |
72246da4 | 473 | |
4fd24483 KVA |
474 | dwc3_cache_hwparams(dwc); |
475 | ||
3921426b FB |
476 | ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE); |
477 | if (ret) { | |
478 | dev_err(dwc->dev, "failed to allocate event buffers\n"); | |
479 | ret = -ENOMEM; | |
480 | goto err0; | |
481 | } | |
482 | ||
72246da4 FB |
483 | ret = dwc3_core_init(dwc); |
484 | if (ret) { | |
802ca850 | 485 | dev_err(dev, "failed to initialize core\n"); |
3921426b | 486 | goto err0; |
72246da4 FB |
487 | } |
488 | ||
f122d33e FB |
489 | ret = dwc3_event_buffers_setup(dwc); |
490 | if (ret) { | |
491 | dev_err(dwc->dev, "failed to setup event buffers\n"); | |
492 | goto err1; | |
493 | } | |
494 | ||
cd051da2 VG |
495 | if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) |
496 | mode = DWC3_MODE_HOST; | |
497 | else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) | |
498 | mode = DWC3_MODE_DEVICE; | |
499 | else | |
500 | mode = DWC3_MODE_DRD; | |
0949e99b FB |
501 | |
502 | switch (mode) { | |
0949e99b | 503 | case DWC3_MODE_DEVICE: |
3140e8cb | 504 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE); |
72246da4 FB |
505 | ret = dwc3_gadget_init(dwc); |
506 | if (ret) { | |
802ca850 | 507 | dev_err(dev, "failed to initialize gadget\n"); |
f122d33e | 508 | goto err2; |
72246da4 | 509 | } |
d07e8819 FB |
510 | break; |
511 | case DWC3_MODE_HOST: | |
3140e8cb | 512 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST); |
d07e8819 FB |
513 | ret = dwc3_host_init(dwc); |
514 | if (ret) { | |
802ca850 | 515 | dev_err(dev, "failed to initialize host\n"); |
f122d33e | 516 | goto err2; |
d07e8819 FB |
517 | } |
518 | break; | |
519 | case DWC3_MODE_DRD: | |
3140e8cb | 520 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG); |
d07e8819 FB |
521 | ret = dwc3_host_init(dwc); |
522 | if (ret) { | |
802ca850 | 523 | dev_err(dev, "failed to initialize host\n"); |
f122d33e | 524 | goto err2; |
d07e8819 FB |
525 | } |
526 | ||
72246da4 FB |
527 | ret = dwc3_gadget_init(dwc); |
528 | if (ret) { | |
802ca850 | 529 | dev_err(dev, "failed to initialize gadget\n"); |
f122d33e | 530 | goto err2; |
72246da4 | 531 | } |
0949e99b FB |
532 | break; |
533 | default: | |
802ca850 | 534 | dev_err(dev, "Unsupported mode of operation %d\n", mode); |
f122d33e | 535 | goto err2; |
72246da4 | 536 | } |
0949e99b | 537 | dwc->mode = mode; |
72246da4 FB |
538 | |
539 | ret = dwc3_debugfs_init(dwc); | |
540 | if (ret) { | |
802ca850 | 541 | dev_err(dev, "failed to initialize debugfs\n"); |
f122d33e | 542 | goto err3; |
72246da4 FB |
543 | } |
544 | ||
802ca850 | 545 | pm_runtime_allow(dev); |
72246da4 FB |
546 | |
547 | return 0; | |
548 | ||
f122d33e | 549 | err3: |
0949e99b | 550 | switch (mode) { |
0949e99b | 551 | case DWC3_MODE_DEVICE: |
72246da4 | 552 | dwc3_gadget_exit(dwc); |
0949e99b | 553 | break; |
d07e8819 FB |
554 | case DWC3_MODE_HOST: |
555 | dwc3_host_exit(dwc); | |
556 | break; | |
557 | case DWC3_MODE_DRD: | |
558 | dwc3_host_exit(dwc); | |
72246da4 | 559 | dwc3_gadget_exit(dwc); |
d07e8819 | 560 | break; |
0949e99b FB |
561 | default: |
562 | /* do nothing */ | |
563 | break; | |
564 | } | |
72246da4 | 565 | |
f122d33e FB |
566 | err2: |
567 | dwc3_event_buffers_cleanup(dwc); | |
568 | ||
72246da4 | 569 | err1: |
802ca850 | 570 | dwc3_core_exit(dwc); |
72246da4 | 571 | |
3921426b FB |
572 | err0: |
573 | dwc3_free_event_buffers(dwc); | |
574 | ||
72246da4 FB |
575 | return ret; |
576 | } | |
577 | ||
fb4e98ab | 578 | static int dwc3_remove(struct platform_device *pdev) |
72246da4 | 579 | { |
72246da4 | 580 | struct dwc3 *dwc = platform_get_drvdata(pdev); |
72246da4 | 581 | |
8ba007a9 KVA |
582 | usb_phy_set_suspend(dwc->usb2_phy, 1); |
583 | usb_phy_set_suspend(dwc->usb3_phy, 1); | |
584 | ||
72246da4 FB |
585 | pm_runtime_put(&pdev->dev); |
586 | pm_runtime_disable(&pdev->dev); | |
587 | ||
588 | dwc3_debugfs_exit(dwc); | |
589 | ||
0949e99b | 590 | switch (dwc->mode) { |
0949e99b | 591 | case DWC3_MODE_DEVICE: |
72246da4 | 592 | dwc3_gadget_exit(dwc); |
0949e99b | 593 | break; |
d07e8819 FB |
594 | case DWC3_MODE_HOST: |
595 | dwc3_host_exit(dwc); | |
596 | break; | |
597 | case DWC3_MODE_DRD: | |
598 | dwc3_host_exit(dwc); | |
72246da4 | 599 | dwc3_gadget_exit(dwc); |
d07e8819 | 600 | break; |
0949e99b FB |
601 | default: |
602 | /* do nothing */ | |
603 | break; | |
604 | } | |
72246da4 | 605 | |
f122d33e | 606 | dwc3_event_buffers_cleanup(dwc); |
d9b4330a | 607 | dwc3_free_event_buffers(dwc); |
72246da4 | 608 | dwc3_core_exit(dwc); |
72246da4 FB |
609 | |
610 | return 0; | |
611 | } | |
612 | ||
7415f17c FB |
613 | #ifdef CONFIG_PM |
614 | static int dwc3_prepare(struct device *dev) | |
615 | { | |
616 | struct dwc3 *dwc = dev_get_drvdata(dev); | |
617 | unsigned long flags; | |
618 | ||
619 | spin_lock_irqsave(&dwc->lock, flags); | |
620 | ||
621 | switch (dwc->mode) { | |
622 | case DWC3_MODE_DEVICE: | |
623 | case DWC3_MODE_DRD: | |
624 | dwc3_gadget_prepare(dwc); | |
625 | /* FALLTHROUGH */ | |
626 | case DWC3_MODE_HOST: | |
627 | default: | |
628 | dwc3_event_buffers_cleanup(dwc); | |
629 | break; | |
630 | } | |
631 | ||
632 | spin_unlock_irqrestore(&dwc->lock, flags); | |
633 | ||
634 | return 0; | |
635 | } | |
636 | ||
637 | static void dwc3_complete(struct device *dev) | |
638 | { | |
639 | struct dwc3 *dwc = dev_get_drvdata(dev); | |
640 | unsigned long flags; | |
641 | ||
642 | spin_lock_irqsave(&dwc->lock, flags); | |
643 | ||
644 | switch (dwc->mode) { | |
645 | case DWC3_MODE_DEVICE: | |
646 | case DWC3_MODE_DRD: | |
647 | dwc3_gadget_complete(dwc); | |
648 | /* FALLTHROUGH */ | |
649 | case DWC3_MODE_HOST: | |
650 | default: | |
651 | dwc3_event_buffers_setup(dwc); | |
652 | break; | |
653 | } | |
654 | ||
655 | spin_unlock_irqrestore(&dwc->lock, flags); | |
656 | } | |
657 | ||
658 | static int dwc3_suspend(struct device *dev) | |
659 | { | |
660 | struct dwc3 *dwc = dev_get_drvdata(dev); | |
661 | unsigned long flags; | |
662 | ||
663 | spin_lock_irqsave(&dwc->lock, flags); | |
664 | ||
665 | switch (dwc->mode) { | |
666 | case DWC3_MODE_DEVICE: | |
667 | case DWC3_MODE_DRD: | |
668 | dwc3_gadget_suspend(dwc); | |
669 | /* FALLTHROUGH */ | |
670 | case DWC3_MODE_HOST: | |
671 | default: | |
672 | /* do nothing */ | |
673 | break; | |
674 | } | |
675 | ||
676 | dwc->gctl = dwc3_readl(dwc->regs, DWC3_GCTL); | |
677 | spin_unlock_irqrestore(&dwc->lock, flags); | |
678 | ||
679 | usb_phy_shutdown(dwc->usb3_phy); | |
680 | usb_phy_shutdown(dwc->usb2_phy); | |
681 | ||
682 | return 0; | |
683 | } | |
684 | ||
685 | static int dwc3_resume(struct device *dev) | |
686 | { | |
687 | struct dwc3 *dwc = dev_get_drvdata(dev); | |
688 | unsigned long flags; | |
689 | ||
690 | usb_phy_init(dwc->usb3_phy); | |
691 | usb_phy_init(dwc->usb2_phy); | |
692 | msleep(100); | |
693 | ||
694 | spin_lock_irqsave(&dwc->lock, flags); | |
695 | ||
696 | dwc3_writel(dwc->regs, DWC3_GCTL, dwc->gctl); | |
697 | ||
698 | switch (dwc->mode) { | |
699 | case DWC3_MODE_DEVICE: | |
700 | case DWC3_MODE_DRD: | |
701 | dwc3_gadget_resume(dwc); | |
702 | /* FALLTHROUGH */ | |
703 | case DWC3_MODE_HOST: | |
704 | default: | |
705 | /* do nothing */ | |
706 | break; | |
707 | } | |
708 | ||
709 | spin_unlock_irqrestore(&dwc->lock, flags); | |
710 | ||
711 | pm_runtime_disable(dev); | |
712 | pm_runtime_set_active(dev); | |
713 | pm_runtime_enable(dev); | |
714 | ||
715 | return 0; | |
716 | } | |
717 | ||
718 | static const struct dev_pm_ops dwc3_dev_pm_ops = { | |
719 | .prepare = dwc3_prepare, | |
720 | .complete = dwc3_complete, | |
721 | ||
722 | SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume) | |
723 | }; | |
724 | ||
725 | #define DWC3_PM_OPS &(dwc3_dev_pm_ops) | |
726 | #else | |
727 | #define DWC3_PM_OPS NULL | |
728 | #endif | |
729 | ||
5088b6f5 KVA |
730 | #ifdef CONFIG_OF |
731 | static const struct of_device_id of_dwc3_match[] = { | |
732 | { | |
733 | .compatible = "synopsys,dwc3" | |
734 | }, | |
735 | { }, | |
736 | }; | |
737 | MODULE_DEVICE_TABLE(of, of_dwc3_match); | |
738 | #endif | |
739 | ||
72246da4 FB |
740 | static struct platform_driver dwc3_driver = { |
741 | .probe = dwc3_probe, | |
7690417d | 742 | .remove = dwc3_remove, |
72246da4 FB |
743 | .driver = { |
744 | .name = "dwc3", | |
5088b6f5 | 745 | .of_match_table = of_match_ptr(of_dwc3_match), |
7415f17c | 746 | .pm = DWC3_PM_OPS, |
72246da4 | 747 | }, |
72246da4 FB |
748 | }; |
749 | ||
b1116dcc TK |
750 | module_platform_driver(dwc3_driver); |
751 | ||
7ae4fc4d | 752 | MODULE_ALIAS("platform:dwc3"); |
72246da4 FB |
753 | MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); |
754 | MODULE_LICENSE("Dual BSD/GPL"); | |
755 | MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver"); |