usb: dwc3: gadget: add a 'restore' argument to set_ep_config
[linux-2.6-block.git] / drivers / usb / dwc3 / core.c
CommitLineData
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 *
5945f789
FB
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
72246da4 12 *
5945f789
FB
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
72246da4 17 *
5945f789
FB
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
72246da4
FB
20 */
21
a72e658b 22#include <linux/module.h>
72246da4
FB
23#include <linux/kernel.h>
24#include <linux/slab.h>
25#include <linux/spinlock.h>
26#include <linux/platform_device.h>
27#include <linux/pm_runtime.h>
28#include <linux/interrupt.h>
29#include <linux/ioport.h>
30#include <linux/io.h>
31#include <linux/list.h>
32#include <linux/delay.h>
33#include <linux/dma-mapping.h>
457e84b6 34#include <linux/of.h>
72246da4
FB
35
36#include <linux/usb/ch9.h>
37#include <linux/usb/gadget.h>
f7e846f0 38#include <linux/usb/of.h>
a45c82b8 39#include <linux/usb/otg.h>
72246da4 40
6462cbd5 41#include "platform_data.h"
72246da4
FB
42#include "core.h"
43#include "gadget.h"
44#include "io.h"
45
46#include "debug.h"
47
8300dd23
FB
48/* -------------------------------------------------------------------------- */
49
3140e8cb
SAS
50void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
51{
52 u32 reg;
53
54 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
55 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
56 reg |= DWC3_GCTL_PRTCAPDIR(mode);
57 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
58}
8300dd23 59
72246da4
FB
60/**
61 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
62 * @dwc: pointer to our context structure
63 */
64static void dwc3_core_soft_reset(struct dwc3 *dwc)
65{
66 u32 reg;
67
68 /* Before Resetting PHY, put Core in Reset */
69 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
70 reg |= DWC3_GCTL_CORESOFTRESET;
71 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
72
73 /* Assert USB3 PHY reset */
74 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
75 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
76 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
77
78 /* Assert USB2 PHY reset */
79 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
80 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
81 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
82
51e1e7bc
FB
83 usb_phy_init(dwc->usb2_phy);
84 usb_phy_init(dwc->usb3_phy);
72246da4
FB
85 mdelay(100);
86
87 /* Clear USB3 PHY reset */
88 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
89 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
90 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
91
92 /* Clear USB2 PHY reset */
93 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
94 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
95 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
96
45627ac6
PA
97 mdelay(100);
98
72246da4
FB
99 /* After PHYs are stable we can take Core out of reset state */
100 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
101 reg &= ~DWC3_GCTL_CORESOFTRESET;
102 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
103}
104
105/**
106 * dwc3_free_one_event_buffer - Frees one event buffer
107 * @dwc: Pointer to our controller context structure
108 * @evt: Pointer to event buffer to be freed
109 */
110static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
111 struct dwc3_event_buffer *evt)
112{
113 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
72246da4
FB
114}
115
116/**
1d046793 117 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
72246da4
FB
118 * @dwc: Pointer to our controller context structure
119 * @length: size of the event buffer
120 *
1d046793 121 * Returns a pointer to the allocated event buffer structure on success
72246da4
FB
122 * otherwise ERR_PTR(errno).
123 */
67d0b500
FB
124static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
125 unsigned length)
72246da4
FB
126{
127 struct dwc3_event_buffer *evt;
128
380f0d28 129 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
72246da4
FB
130 if (!evt)
131 return ERR_PTR(-ENOMEM);
132
133 evt->dwc = dwc;
134 evt->length = length;
135 evt->buf = dma_alloc_coherent(dwc->dev, length,
136 &evt->dma, GFP_KERNEL);
e32672f0 137 if (!evt->buf)
72246da4 138 return ERR_PTR(-ENOMEM);
72246da4
FB
139
140 return evt;
141}
142
143/**
144 * dwc3_free_event_buffers - frees all allocated event buffers
145 * @dwc: Pointer to our controller context structure
146 */
147static void dwc3_free_event_buffers(struct dwc3 *dwc)
148{
149 struct dwc3_event_buffer *evt;
150 int i;
151
9f622b2a 152 for (i = 0; i < dwc->num_event_buffers; i++) {
72246da4 153 evt = dwc->ev_buffs[i];
64b6c8a7 154 if (evt)
72246da4 155 dwc3_free_one_event_buffer(dwc, evt);
72246da4
FB
156 }
157}
158
159/**
160 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
1d046793 161 * @dwc: pointer to our controller context structure
72246da4
FB
162 * @length: size of event buffer
163 *
1d046793 164 * Returns 0 on success otherwise negative errno. In the error case, dwc
72246da4
FB
165 * may contain some buffers allocated but not all which were requested.
166 */
41ac7b3a 167static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
72246da4 168{
9f622b2a 169 int num;
72246da4
FB
170 int i;
171
9f622b2a
FB
172 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
173 dwc->num_event_buffers = num;
174
380f0d28
FB
175 dwc->ev_buffs = devm_kzalloc(dwc->dev, sizeof(*dwc->ev_buffs) * num,
176 GFP_KERNEL);
457d3f21
FB
177 if (!dwc->ev_buffs) {
178 dev_err(dwc->dev, "can't allocate event buffers array\n");
179 return -ENOMEM;
180 }
181
72246da4
FB
182 for (i = 0; i < num; i++) {
183 struct dwc3_event_buffer *evt;
184
185 evt = dwc3_alloc_one_event_buffer(dwc, length);
186 if (IS_ERR(evt)) {
187 dev_err(dwc->dev, "can't allocate event buffer\n");
188 return PTR_ERR(evt);
189 }
190 dwc->ev_buffs[i] = evt;
191 }
192
193 return 0;
194}
195
196/**
197 * dwc3_event_buffers_setup - setup our allocated event buffers
1d046793 198 * @dwc: pointer to our controller context structure
72246da4
FB
199 *
200 * Returns 0 on success otherwise negative errno.
201 */
7acd85e0 202static int dwc3_event_buffers_setup(struct dwc3 *dwc)
72246da4
FB
203{
204 struct dwc3_event_buffer *evt;
205 int n;
206
9f622b2a 207 for (n = 0; n < dwc->num_event_buffers; n++) {
72246da4
FB
208 evt = dwc->ev_buffs[n];
209 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
210 evt->buf, (unsigned long long) evt->dma,
211 evt->length);
212
7acd85e0
PZ
213 evt->lpos = 0;
214
72246da4
FB
215 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
216 lower_32_bits(evt->dma));
217 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
218 upper_32_bits(evt->dma));
219 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
68d6a01b 220 DWC3_GEVNTSIZ_SIZE(evt->length));
72246da4
FB
221 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
222 }
223
224 return 0;
225}
226
227static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
228{
229 struct dwc3_event_buffer *evt;
230 int n;
231
9f622b2a 232 for (n = 0; n < dwc->num_event_buffers; n++) {
72246da4 233 evt = dwc->ev_buffs[n];
7acd85e0
PZ
234
235 evt->lpos = 0;
236
72246da4
FB
237 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
238 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
68d6a01b
FB
239 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK
240 | DWC3_GEVNTSIZ_SIZE(0));
72246da4
FB
241 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
242 }
243}
244
789451f6
FB
245static void dwc3_core_num_eps(struct dwc3 *dwc)
246{
247 struct dwc3_hwparams *parms = &dwc->hwparams;
248
249 dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
250 dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
251
252 dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n",
253 dwc->num_in_eps, dwc->num_out_eps);
254}
255
41ac7b3a 256static void dwc3_cache_hwparams(struct dwc3 *dwc)
26ceca97
FB
257{
258 struct dwc3_hwparams *parms = &dwc->hwparams;
259
260 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
261 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
262 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
263 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
264 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
265 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
266 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
267 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
268 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
269}
270
72246da4
FB
271/**
272 * dwc3_core_init - Low-level initialization of DWC3 Core
273 * @dwc: Pointer to our controller context structure
274 *
275 * Returns 0 on success otherwise negative errno.
276 */
41ac7b3a 277static int dwc3_core_init(struct dwc3 *dwc)
72246da4
FB
278{
279 unsigned long timeout;
280 u32 reg;
281 int ret;
282
7650bd74
SAS
283 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
284 /* This should read as U3 followed by revision number */
285 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
286 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
287 ret = -ENODEV;
288 goto err0;
289 }
248b122b 290 dwc->revision = reg;
7650bd74 291
72246da4
FB
292 /* issue device SoftReset too */
293 timeout = jiffies + msecs_to_jiffies(500);
294 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
295 do {
296 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
297 if (!(reg & DWC3_DCTL_CSFTRST))
298 break;
299
300 if (time_after(jiffies, timeout)) {
301 dev_err(dwc->dev, "Reset Timed Out\n");
302 ret = -ETIMEDOUT;
303 goto err0;
304 }
305
306 cpu_relax();
307 } while (true);
308
58a0f23f
PA
309 dwc3_core_soft_reset(dwc);
310
4878a028 311 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
3e87c42a 312 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
4878a028
SAS
313 reg &= ~DWC3_GCTL_DISSCRAMBLE;
314
164d7731 315 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
4878a028 316 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
32a4a135
FB
317 /**
318 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
319 * issue which would cause xHCI compliance tests to fail.
320 *
321 * Because of that we cannot enable clock gating on such
322 * configurations.
323 *
324 * Refers to:
325 *
326 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
327 * SOF/ITP Mode Used
328 */
329 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
330 dwc->dr_mode == USB_DR_MODE_OTG) &&
331 (dwc->revision >= DWC3_REVISION_210A &&
332 dwc->revision <= DWC3_REVISION_250A))
333 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
334 else
335 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
4878a028
SAS
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
356err0:
357 return ret;
358}
359
360static 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 368static int dwc3_probe(struct platform_device *pdev)
72246da4 369{
941ea361
FB
370 struct device *dev = &pdev->dev;
371 struct dwc3_platform_data *pdata = dev_get_platdata(dev);
372 struct device_node *node = dev->of_node;
72246da4
FB
373 struct resource *res;
374 struct dwc3 *dwc;
0949e99b 375
72246da4 376 int ret = -ENOMEM;
0949e99b
FB
377
378 void __iomem *regs;
72246da4
FB
379 void *mem;
380
802ca850 381 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
72246da4 382 if (!mem) {
802ca850
CP
383 dev_err(dev, "not enough memory\n");
384 return -ENOMEM;
72246da4
FB
385 }
386 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
387 dwc->mem = mem;
388
51249dca 389 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
72246da4 390 if (!res) {
51249dca 391 dev_err(dev, "missing IRQ\n");
802ca850 392 return -ENODEV;
72246da4 393 }
066618bc
KVA
394 dwc->xhci_resources[1].start = res->start;
395 dwc->xhci_resources[1].end = res->end;
396 dwc->xhci_resources[1].flags = res->flags;
397 dwc->xhci_resources[1].name = res->name;
72246da4 398
51249dca
IS
399 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
400 if (!res) {
401 dev_err(dev, "missing memory resource\n");
402 return -ENODEV;
403 }
72246da4 404
5088b6f5 405 if (node) {
f7e846f0
FB
406 dwc->maximum_speed = of_usb_get_maximum_speed(node);
407
5088b6f5
KVA
408 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
409 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
6462cbd5
FB
410
411 dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize");
a45c82b8 412 dwc->dr_mode = of_usb_get_dr_mode(node);
bb674907 413 } else if (pdata) {
f7e846f0
FB
414 dwc->maximum_speed = pdata->maximum_speed;
415
5088b6f5
KVA
416 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
417 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
6462cbd5
FB
418
419 dwc->needs_fifo_resize = pdata->tx_fifo_resize;
a45c82b8 420 dwc->dr_mode = pdata->dr_mode;
bb674907
FB
421 } else {
422 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
423 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
5088b6f5
KVA
424 }
425
f7e846f0
FB
426 /* default to superspeed if no maximum_speed passed */
427 if (dwc->maximum_speed == USB_SPEED_UNKNOWN)
428 dwc->maximum_speed = USB_SPEED_SUPER;
429
d105e7f8
FB
430 if (IS_ERR(dwc->usb2_phy)) {
431 ret = PTR_ERR(dwc->usb2_phy);
432
433 /*
434 * if -ENXIO is returned, it means PHY layer wasn't
435 * enabled, so it makes no sense to return -EPROBE_DEFER
436 * in that case, since no PHY driver will ever probe.
437 */
438 if (ret == -ENXIO)
439 return ret;
440
51e1e7bc
FB
441 dev_err(dev, "no usb2 phy configured\n");
442 return -EPROBE_DEFER;
443 }
444
d105e7f8 445 if (IS_ERR(dwc->usb3_phy)) {
315955d7 446 ret = PTR_ERR(dwc->usb3_phy);
d105e7f8
FB
447
448 /*
449 * if -ENXIO is returned, it means PHY layer wasn't
450 * enabled, so it makes no sense to return -EPROBE_DEFER
451 * in that case, since no PHY driver will ever probe.
452 */
453 if (ret == -ENXIO)
454 return ret;
455
51e1e7bc
FB
456 dev_err(dev, "no usb3 phy configured\n");
457 return -EPROBE_DEFER;
458 }
459
2e112345
II
460 dwc->xhci_resources[0].start = res->start;
461 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
462 DWC3_XHCI_REGS_END;
463 dwc->xhci_resources[0].flags = res->flags;
464 dwc->xhci_resources[0].name = res->name;
465
466 res->start += DWC3_GLOBALS_REGS_START;
467
468 /*
469 * Request memory region but exclude xHCI regs,
470 * since it will be requested by the xhci-plat driver.
471 */
472 regs = devm_ioremap_resource(dev, res);
473 if (IS_ERR(regs))
474 return PTR_ERR(regs);
475
72246da4
FB
476 spin_lock_init(&dwc->lock);
477 platform_set_drvdata(pdev, dwc);
478
479 dwc->regs = regs;
480 dwc->regs_size = resource_size(res);
802ca850 481 dwc->dev = dev;
72246da4 482
ddff14f1
KVA
483 dev->dma_mask = dev->parent->dma_mask;
484 dev->dma_parms = dev->parent->dma_parms;
485 dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
486
802ca850
CP
487 pm_runtime_enable(dev);
488 pm_runtime_get_sync(dev);
489 pm_runtime_forbid(dev);
72246da4 490
4fd24483
KVA
491 dwc3_cache_hwparams(dwc);
492
3921426b
FB
493 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
494 if (ret) {
495 dev_err(dwc->dev, "failed to allocate event buffers\n");
496 ret = -ENOMEM;
497 goto err0;
498 }
499
32a4a135
FB
500 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
501 dwc->dr_mode = USB_DR_MODE_HOST;
502 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
503 dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
504
505 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
506 dwc->dr_mode = USB_DR_MODE_OTG;
507
72246da4
FB
508 ret = dwc3_core_init(dwc);
509 if (ret) {
802ca850 510 dev_err(dev, "failed to initialize core\n");
3921426b 511 goto err0;
72246da4
FB
512 }
513
3088f108
KVA
514 usb_phy_set_suspend(dwc->usb2_phy, 0);
515 usb_phy_set_suspend(dwc->usb3_phy, 0);
516
f122d33e
FB
517 ret = dwc3_event_buffers_setup(dwc);
518 if (ret) {
519 dev_err(dwc->dev, "failed to setup event buffers\n");
520 goto err1;
521 }
522
a45c82b8
RK
523 switch (dwc->dr_mode) {
524 case USB_DR_MODE_PERIPHERAL:
3140e8cb 525 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
72246da4
FB
526 ret = dwc3_gadget_init(dwc);
527 if (ret) {
802ca850 528 dev_err(dev, "failed to initialize gadget\n");
f122d33e 529 goto err2;
72246da4 530 }
d07e8819 531 break;
a45c82b8 532 case USB_DR_MODE_HOST:
3140e8cb 533 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
d07e8819
FB
534 ret = dwc3_host_init(dwc);
535 if (ret) {
802ca850 536 dev_err(dev, "failed to initialize host\n");
f122d33e 537 goto err2;
d07e8819
FB
538 }
539 break;
a45c82b8 540 case USB_DR_MODE_OTG:
3140e8cb 541 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
d07e8819
FB
542 ret = dwc3_host_init(dwc);
543 if (ret) {
802ca850 544 dev_err(dev, "failed to initialize host\n");
f122d33e 545 goto err2;
d07e8819
FB
546 }
547
72246da4
FB
548 ret = dwc3_gadget_init(dwc);
549 if (ret) {
802ca850 550 dev_err(dev, "failed to initialize gadget\n");
f122d33e 551 goto err2;
72246da4 552 }
0949e99b
FB
553 break;
554 default:
a45c82b8 555 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
f122d33e 556 goto err2;
72246da4
FB
557 }
558
559 ret = dwc3_debugfs_init(dwc);
560 if (ret) {
802ca850 561 dev_err(dev, "failed to initialize debugfs\n");
f122d33e 562 goto err3;
72246da4
FB
563 }
564
802ca850 565 pm_runtime_allow(dev);
72246da4
FB
566
567 return 0;
568
f122d33e 569err3:
a45c82b8
RK
570 switch (dwc->dr_mode) {
571 case USB_DR_MODE_PERIPHERAL:
72246da4 572 dwc3_gadget_exit(dwc);
0949e99b 573 break;
a45c82b8 574 case USB_DR_MODE_HOST:
d07e8819
FB
575 dwc3_host_exit(dwc);
576 break;
a45c82b8 577 case USB_DR_MODE_OTG:
d07e8819 578 dwc3_host_exit(dwc);
72246da4 579 dwc3_gadget_exit(dwc);
d07e8819 580 break;
0949e99b
FB
581 default:
582 /* do nothing */
583 break;
584 }
72246da4 585
f122d33e
FB
586err2:
587 dwc3_event_buffers_cleanup(dwc);
588
72246da4 589err1:
501fae51
KVA
590 usb_phy_set_suspend(dwc->usb2_phy, 1);
591 usb_phy_set_suspend(dwc->usb3_phy, 1);
802ca850 592 dwc3_core_exit(dwc);
72246da4 593
3921426b
FB
594err0:
595 dwc3_free_event_buffers(dwc);
596
72246da4
FB
597 return ret;
598}
599
fb4e98ab 600static int dwc3_remove(struct platform_device *pdev)
72246da4 601{
72246da4 602 struct dwc3 *dwc = platform_get_drvdata(pdev);
72246da4 603
8ba007a9
KVA
604 usb_phy_set_suspend(dwc->usb2_phy, 1);
605 usb_phy_set_suspend(dwc->usb3_phy, 1);
606
16b972a5 607 pm_runtime_put_sync(&pdev->dev);
72246da4
FB
608 pm_runtime_disable(&pdev->dev);
609
610 dwc3_debugfs_exit(dwc);
611
a45c82b8
RK
612 switch (dwc->dr_mode) {
613 case USB_DR_MODE_PERIPHERAL:
72246da4 614 dwc3_gadget_exit(dwc);
0949e99b 615 break;
a45c82b8 616 case USB_DR_MODE_HOST:
d07e8819
FB
617 dwc3_host_exit(dwc);
618 break;
a45c82b8 619 case USB_DR_MODE_OTG:
d07e8819 620 dwc3_host_exit(dwc);
72246da4 621 dwc3_gadget_exit(dwc);
d07e8819 622 break;
0949e99b
FB
623 default:
624 /* do nothing */
625 break;
626 }
72246da4 627
f122d33e 628 dwc3_event_buffers_cleanup(dwc);
d9b4330a 629 dwc3_free_event_buffers(dwc);
72246da4 630 dwc3_core_exit(dwc);
72246da4
FB
631
632 return 0;
633}
634
19fda7cd 635#ifdef CONFIG_PM_SLEEP
7415f17c
FB
636static int dwc3_prepare(struct device *dev)
637{
638 struct dwc3 *dwc = dev_get_drvdata(dev);
639 unsigned long flags;
640
641 spin_lock_irqsave(&dwc->lock, flags);
642
a45c82b8
RK
643 switch (dwc->dr_mode) {
644 case USB_DR_MODE_PERIPHERAL:
645 case USB_DR_MODE_OTG:
7415f17c
FB
646 dwc3_gadget_prepare(dwc);
647 /* FALLTHROUGH */
a45c82b8 648 case USB_DR_MODE_HOST:
7415f17c
FB
649 default:
650 dwc3_event_buffers_cleanup(dwc);
651 break;
652 }
653
654 spin_unlock_irqrestore(&dwc->lock, flags);
655
656 return 0;
657}
658
659static void dwc3_complete(struct device *dev)
660{
661 struct dwc3 *dwc = dev_get_drvdata(dev);
662 unsigned long flags;
663
664 spin_lock_irqsave(&dwc->lock, flags);
665
a45c82b8
RK
666 switch (dwc->dr_mode) {
667 case USB_DR_MODE_PERIPHERAL:
668 case USB_DR_MODE_OTG:
7415f17c
FB
669 dwc3_gadget_complete(dwc);
670 /* FALLTHROUGH */
a45c82b8 671 case USB_DR_MODE_HOST:
7415f17c
FB
672 default:
673 dwc3_event_buffers_setup(dwc);
674 break;
675 }
676
677 spin_unlock_irqrestore(&dwc->lock, flags);
678}
679
680static int dwc3_suspend(struct device *dev)
681{
682 struct dwc3 *dwc = dev_get_drvdata(dev);
683 unsigned long flags;
684
685 spin_lock_irqsave(&dwc->lock, flags);
686
a45c82b8
RK
687 switch (dwc->dr_mode) {
688 case USB_DR_MODE_PERIPHERAL:
689 case USB_DR_MODE_OTG:
7415f17c
FB
690 dwc3_gadget_suspend(dwc);
691 /* FALLTHROUGH */
a45c82b8 692 case USB_DR_MODE_HOST:
7415f17c
FB
693 default:
694 /* do nothing */
695 break;
696 }
697
698 dwc->gctl = dwc3_readl(dwc->regs, DWC3_GCTL);
699 spin_unlock_irqrestore(&dwc->lock, flags);
700
701 usb_phy_shutdown(dwc->usb3_phy);
702 usb_phy_shutdown(dwc->usb2_phy);
703
704 return 0;
705}
706
707static int dwc3_resume(struct device *dev)
708{
709 struct dwc3 *dwc = dev_get_drvdata(dev);
710 unsigned long flags;
711
712 usb_phy_init(dwc->usb3_phy);
713 usb_phy_init(dwc->usb2_phy);
7415f17c
FB
714
715 spin_lock_irqsave(&dwc->lock, flags);
716
717 dwc3_writel(dwc->regs, DWC3_GCTL, dwc->gctl);
718
a45c82b8
RK
719 switch (dwc->dr_mode) {
720 case USB_DR_MODE_PERIPHERAL:
721 case USB_DR_MODE_OTG:
7415f17c
FB
722 dwc3_gadget_resume(dwc);
723 /* FALLTHROUGH */
a45c82b8 724 case USB_DR_MODE_HOST:
7415f17c
FB
725 default:
726 /* do nothing */
727 break;
728 }
729
730 spin_unlock_irqrestore(&dwc->lock, flags);
731
732 pm_runtime_disable(dev);
733 pm_runtime_set_active(dev);
734 pm_runtime_enable(dev);
735
736 return 0;
737}
738
739static const struct dev_pm_ops dwc3_dev_pm_ops = {
740 .prepare = dwc3_prepare,
741 .complete = dwc3_complete,
742
743 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
744};
745
746#define DWC3_PM_OPS &(dwc3_dev_pm_ops)
747#else
748#define DWC3_PM_OPS NULL
749#endif
750
5088b6f5
KVA
751#ifdef CONFIG_OF
752static const struct of_device_id of_dwc3_match[] = {
22a5aa17
FB
753 {
754 .compatible = "snps,dwc3"
755 },
5088b6f5
KVA
756 {
757 .compatible = "synopsys,dwc3"
758 },
759 { },
760};
761MODULE_DEVICE_TABLE(of, of_dwc3_match);
762#endif
763
72246da4
FB
764static struct platform_driver dwc3_driver = {
765 .probe = dwc3_probe,
7690417d 766 .remove = dwc3_remove,
72246da4
FB
767 .driver = {
768 .name = "dwc3",
5088b6f5 769 .of_match_table = of_match_ptr(of_dwc3_match),
7415f17c 770 .pm = DWC3_PM_OPS,
72246da4 771 },
72246da4
FB
772};
773
b1116dcc
TK
774module_platform_driver(dwc3_driver);
775
7ae4fc4d 776MODULE_ALIAS("platform:dwc3");
72246da4 777MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
5945f789 778MODULE_LICENSE("GPL v2");
72246da4 779MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");