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