1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/version.h>
23#include <linux/module.h>
24#include <linux/kernel.h>
25#include <linux/slab.h>
26#include <linux/spinlock.h>
27#include <linux/platform_device.h>
28#include <linux/pm_runtime.h>
29#include <linux/interrupt.h>
30#include <linux/ioport.h>
31#include <linux/io.h>
32#include <linux/list.h>
33#include <linux/delay.h>
34#include <linux/dma-mapping.h>
35#include <linux/of.h>
36#include <linux/acpi.h>
37#include <linux/pinctrl/consumer.h>
38#include <linux/of_address.h>
39
40#include <linux/usb/ch9.h>
41#include <linux/usb/gadget.h>
42#include <linux/usb/of.h>
43#include <linux/usb/otg.h>
44
45#include "core.h"
46#include "gadget.h"
47#include "io.h"
48
49#include "debug.h"
50
51#define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000
52
53
54
55
56
57static int dwc3_get_dr_mode(struct dwc3 *dwc)
58{
59 enum usb_dr_mode mode;
60 struct device *dev = dwc->dev;
61 unsigned int hw_mode;
62
63 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
64 dwc->dr_mode = USB_DR_MODE_OTG;
65
66 mode = dwc->dr_mode;
67 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
68
69 switch (hw_mode) {
70 case DWC3_GHWPARAMS0_MODE_GADGET:
71 if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) {
72 dev_err(dev,
73 "Controller does not support host mode.\n");
74 return -EINVAL;
75 }
76 mode = USB_DR_MODE_PERIPHERAL;
77 break;
78 case DWC3_GHWPARAMS0_MODE_HOST:
79 if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
80 dev_err(dev,
81 "Controller does not support device mode.\n");
82 return -EINVAL;
83 }
84 mode = USB_DR_MODE_HOST;
85 break;
86 default:
87 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
88 mode = USB_DR_MODE_HOST;
89 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
90 mode = USB_DR_MODE_PERIPHERAL;
91 }
92
93 if (mode != dwc->dr_mode) {
94 dev_warn(dev,
95 "Configuration mismatch. dr_mode forced to %s\n",
96 mode == USB_DR_MODE_HOST ? "host" : "gadget");
97
98 dwc->dr_mode = mode;
99 }
100
101 return 0;
102}
103
104static void dwc3_event_buffers_cleanup(struct dwc3 *dwc);
105
106static void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
107{
108 u32 reg;
109
110 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
111 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
112 reg |= DWC3_GCTL_PRTCAPDIR(mode);
113 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
114}
115
116static void __dwc3_set_mode(struct work_struct *work)
117{
118 struct dwc3 *dwc = work_to_dwc(work);
119 unsigned long flags;
120 int ret;
121
122 if (!dwc->desired_dr_role)
123 return;
124
125 if (dwc->desired_dr_role == dwc->current_dr_role)
126 return;
127
128 if (dwc->dr_mode != USB_DR_MODE_OTG)
129 return;
130
131 switch (dwc->current_dr_role) {
132 case DWC3_GCTL_PRTCAP_HOST:
133 dwc3_host_exit(dwc);
134 break;
135 case DWC3_GCTL_PRTCAP_DEVICE:
136 dwc3_gadget_exit(dwc);
137 dwc3_event_buffers_cleanup(dwc);
138 break;
139 default:
140 break;
141 }
142
143 spin_lock_irqsave(&dwc->lock, flags);
144
145 dwc3_set_prtcap(dwc, dwc->desired_dr_role);
146
147 dwc->current_dr_role = dwc->desired_dr_role;
148
149 spin_unlock_irqrestore(&dwc->lock, flags);
150
151 switch (dwc->desired_dr_role) {
152 case DWC3_GCTL_PRTCAP_HOST:
153 ret = dwc3_host_init(dwc);
154 if (ret) {
155 dev_err(dwc->dev, "failed to initialize host\n");
156 } else {
157 if (dwc->usb2_phy)
158 otg_set_vbus(dwc->usb2_phy->otg, true);
159 if (dwc->usb2_generic_phy)
160 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
161
162 }
163 break;
164 case DWC3_GCTL_PRTCAP_DEVICE:
165 dwc3_event_buffers_setup(dwc);
166
167 if (dwc->usb2_phy)
168 otg_set_vbus(dwc->usb2_phy->otg, false);
169 if (dwc->usb2_generic_phy)
170 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
171
172 ret = dwc3_gadget_init(dwc);
173 if (ret)
174 dev_err(dwc->dev, "failed to initialize peripheral\n");
175 break;
176 default:
177 break;
178 }
179}
180
181void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
182{
183 unsigned long flags;
184
185 spin_lock_irqsave(&dwc->lock, flags);
186 dwc->desired_dr_role = mode;
187 spin_unlock_irqrestore(&dwc->lock, flags);
188
189 queue_work(system_power_efficient_wq, &dwc->drd_work);
190}
191
192u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
193{
194 struct dwc3 *dwc = dep->dwc;
195 u32 reg;
196
197 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
198 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
199 DWC3_GDBGFIFOSPACE_TYPE(type));
200
201 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
202
203 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
204}
205
206
207
208
209
210static int dwc3_core_soft_reset(struct dwc3 *dwc)
211{
212 u32 reg;
213 int retries = 1000;
214 int ret;
215
216 usb_phy_init(dwc->usb2_phy);
217 usb_phy_init(dwc->usb3_phy);
218 ret = phy_init(dwc->usb2_generic_phy);
219 if (ret < 0)
220 return ret;
221
222 ret = phy_init(dwc->usb3_generic_phy);
223 if (ret < 0) {
224 phy_exit(dwc->usb2_generic_phy);
225 return ret;
226 }
227
228
229
230
231
232
233 if (dwc->dr_mode == USB_DR_MODE_HOST || dwc->is_hibernated == true)
234 return 0;
235
236 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
237 reg |= DWC3_DCTL_CSFTRST;
238 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
239
240 do {
241 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
242 if (!(reg & DWC3_DCTL_CSFTRST))
243 return 0;
244
245 udelay(1);
246 } while (--retries);
247
248 return -ETIMEDOUT;
249}
250
251
252
253
254
255static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
256{
257 u32 reg, gfladj;
258 u32 dft;
259
260 if (dwc->revision < DWC3_REVISION_250A)
261 return;
262
263 if (dwc->fladj == 0)
264 return;
265
266
267 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
268 gfladj = reg;
269
270 if (dwc->refclk_fladj) {
271 if ((reg & DWC3_GFLADJ_REFCLK_FLADJ) !=
272 (dwc->fladj & DWC3_GFLADJ_REFCLK_FLADJ)) {
273 reg &= ~DWC3_GFLADJ_REFCLK_FLADJ;
274 reg |= (dwc->fladj & DWC3_GFLADJ_REFCLK_FLADJ);
275 }
276 }
277
278 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
279 if (dft != dwc->fladj) {
280 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
281 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
282 }
283
284
285 if (reg != gfladj)
286 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
287}
288
289
290
291
292
293
294static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
295 struct dwc3_event_buffer *evt)
296{
297 dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
298}
299
300
301
302
303
304
305
306
307
308static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
309 unsigned length)
310{
311 struct dwc3_event_buffer *evt;
312
313 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
314 if (!evt)
315 return ERR_PTR(-ENOMEM);
316
317 evt->dwc = dwc;
318 evt->length = length;
319 evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
320 if (!evt->cache)
321 return ERR_PTR(-ENOMEM);
322
323 evt->buf = dma_alloc_coherent(dwc->sysdev, length,
324 &evt->dma, GFP_KERNEL);
325 if (!evt->buf)
326 return ERR_PTR(-ENOMEM);
327
328 return evt;
329}
330
331
332
333
334
335void dwc3_free_event_buffers(struct dwc3 *dwc)
336{
337 struct dwc3_event_buffer *evt;
338
339 evt = dwc->ev_buf;
340 if (evt)
341 dwc3_free_one_event_buffer(dwc, evt);
342}
343
344
345
346
347
348
349
350
351
352int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
353{
354 struct dwc3_event_buffer *evt;
355
356 evt = dwc3_alloc_one_event_buffer(dwc, length);
357 if (IS_ERR(evt)) {
358 dev_err(dwc->dev, "can't allocate event buffer\n");
359 return PTR_ERR(evt);
360 }
361 dwc->ev_buf = evt;
362
363 return 0;
364}
365
366
367
368
369
370
371
372int dwc3_event_buffers_setup(struct dwc3 *dwc)
373{
374 struct dwc3_event_buffer *evt;
375
376 if (dwc->dr_mode == USB_DR_MODE_HOST)
377 return 0;
378
379 evt = dwc->ev_buf;
380 evt->lpos = 0;
381 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
382 lower_32_bits(evt->dma));
383 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
384 upper_32_bits(evt->dma));
385 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
386 DWC3_GEVNTSIZ_SIZE(evt->length));
387 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
388
389 return 0;
390}
391
392static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
393{
394 struct dwc3_event_buffer *evt;
395
396 evt = dwc->ev_buf;
397
398 evt->lpos = 0;
399
400 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
401 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
402 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
403 | DWC3_GEVNTSIZ_SIZE(0));
404 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
405}
406
407static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
408{
409 u32 size;
410
411 if (dwc->dr_mode == USB_DR_MODE_HOST)
412 return 0;
413
414 if (!dwc->has_hibernation)
415 return 0;
416
417 if (!dwc->nr_scratch)
418 return 0;
419
420
421 if (dwc->scratchbuf)
422 return 0;
423
424 size = dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE;
425
426 dwc->scratchbuf = kzalloc(size, GFP_KERNEL);
427
428 if (!dwc->scratchbuf)
429 return -ENOMEM;
430
431 dwc->scratch_addr = dma_map_single(dwc->dev, dwc->scratchbuf, size,
432 DMA_BIDIRECTIONAL);
433 if (dma_mapping_error(dwc->dev, dwc->scratch_addr)) {
434 dev_err(dwc->dev, "failed to map scratch buffer\n");
435 return -EFAULT;
436 }
437
438 return 0;
439}
440
441static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
442{
443 u32 param;
444 int ret;
445
446 if (dwc->dr_mode == USB_DR_MODE_HOST)
447 return 0;
448
449 if (!dwc->has_hibernation)
450 return 0;
451
452 if (!dwc->nr_scratch)
453 return 0;
454
455
456 if (WARN_ON(!dwc->scratchbuf))
457 return 0;
458
459 param = lower_32_bits(dwc->scratch_addr);
460
461 ret = dwc3_send_gadget_generic_command(dwc,
462 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
463 if (ret < 0)
464 goto err1;
465
466 param = upper_32_bits(dwc->scratch_addr);
467
468 ret = dwc3_send_gadget_generic_command(dwc,
469 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
470 if (ret < 0)
471 goto err1;
472
473 return 0;
474
475err1:
476 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
477 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
478
479 return ret;
480}
481
482static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
483{
484 if (!dwc->has_hibernation)
485 return;
486
487 if (!dwc->nr_scratch)
488 return;
489
490
491 if (WARN_ON(!dwc->scratchbuf))
492 return;
493
494 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
495 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
496 kfree(dwc->scratchbuf);
497}
498
499static void dwc3_core_num_eps(struct dwc3 *dwc)
500{
501 struct dwc3_hwparams *parms = &dwc->hwparams;
502
503 dwc->num_eps = DWC3_NUM_EPS(parms);
504}
505
506static void dwc3_cache_hwparams(struct dwc3 *dwc)
507{
508 struct dwc3_hwparams *parms = &dwc->hwparams;
509
510 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
511 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
512 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
513 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
514 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
515 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
516 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
517 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
518 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
519}
520
521static int dwc3_config_soc_bus(struct dwc3 *dwc)
522{
523 int ret;
524
525
526
527
528
529
530 if (of_dma_is_coherent(dwc->dev->of_node)) {
531 u32 reg;
532
533 reg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
534 reg |= DWC3_GSBUSCFG0_DATRDREQINFO |
535 DWC3_GSBUSCFG0_DESRDREQINFO |
536 DWC3_GSBUSCFG0_DATWRREQINFO |
537 DWC3_GSBUSCFG0_DESWRREQINFO;
538 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, reg);
539
540 ret = dwc3_enable_hw_coherency(dwc->dev);
541 if (ret)
542 return ret;
543 }
544
545
546
547
548 dwc3_set_simple_data(dwc);
549
550 return 0;
551}
552
553
554
555
556
557
558
559
560
561static int dwc3_phy_setup(struct dwc3 *dwc)
562{
563 u32 reg;
564 int ret;
565
566 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
567
568
569
570
571
572 reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
573
574
575
576
577
578
579
580 if (dwc->revision > DWC3_REVISION_194A)
581 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
582
583 if (dwc->u2ss_inp3_quirk)
584 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
585
586 if (dwc->dis_rxdet_inp3_quirk)
587 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
588
589 if (dwc->req_p1p2p3_quirk)
590 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
591
592 if (dwc->del_p1p2p3_quirk)
593 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
594
595 if (dwc->del_phy_power_chg_quirk)
596 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
597
598 if (dwc->lfps_filter_quirk)
599 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
600
601 if (dwc->rx_detect_poll_quirk)
602 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
603
604 if (dwc->tx_de_emphasis_quirk)
605 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
606
607 if (dwc->dis_u3_susphy_quirk)
608 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
609
610 if (dwc->dis_del_phy_power_chg_quirk)
611 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
612
613 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
614
615 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
616
617
618 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
619 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
620 if (dwc->hsphy_interface &&
621 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
622 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
623 break;
624 } else if (dwc->hsphy_interface &&
625 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
626 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
627 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
628 } else {
629
630 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
631 break;
632 }
633
634 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
635 ret = dwc3_ulpi_init(dwc);
636 if (ret)
637 return ret;
638
639 default:
640 break;
641 }
642
643 switch (dwc->hsphy_mode) {
644 case USBPHY_INTERFACE_MODE_UTMI:
645 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
646 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
647 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
648 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
649 break;
650 case USBPHY_INTERFACE_MODE_UTMIW:
651 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
652 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
653 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
654 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
655 break;
656 default:
657 break;
658 }
659
660
661
662
663
664
665
666 if (dwc->revision > DWC3_REVISION_194A)
667 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
668
669 if (dwc->dis_u2_susphy_quirk)
670 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
671
672 if (dwc->dis_enblslpm_quirk)
673 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
674
675 if (dwc->dis_u2_freeclk_exists_quirk)
676 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
677
678 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
679
680 return 0;
681}
682
683static void dwc3_core_exit(struct dwc3 *dwc)
684{
685 usb_phy_shutdown(dwc->usb2_phy);
686 usb_phy_shutdown(dwc->usb3_phy);
687 phy_exit(dwc->usb2_generic_phy);
688 phy_exit(dwc->usb3_generic_phy);
689
690 usb_phy_set_suspend(dwc->usb2_phy, 1);
691 usb_phy_set_suspend(dwc->usb3_phy, 1);
692 phy_power_off(dwc->usb2_generic_phy);
693 phy_power_off(dwc->usb3_generic_phy);
694}
695
696static bool dwc3_core_is_valid(struct dwc3 *dwc)
697{
698 u32 reg;
699
700 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
701
702
703 if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
704
705 dwc->revision = reg;
706 } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
707
708 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
709 dwc->revision |= DWC3_REVISION_IS_DWC31;
710 } else {
711 return false;
712 }
713
714 return true;
715}
716
717static void dwc3_core_setup_global_control(struct dwc3 *dwc)
718{
719 u32 hwparams4 = dwc->hwparams.hwparams4;
720 u32 reg;
721
722 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
723 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
724
725 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
726 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
727
728
729
730
731
732
733
734
735
736
737
738
739 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
740 dwc->dr_mode == USB_DR_MODE_OTG) &&
741 (dwc->revision >= DWC3_REVISION_210A &&
742 dwc->revision <= DWC3_REVISION_250A))
743 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
744 else
745 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
746 break;
747 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
748 if (!device_property_read_bool(dwc->dev,
749 "snps,enable-hibernation")) {
750 dev_dbg(dwc->dev, "Hibernation not enabled\n");
751 } else {
752
753 dwc->nr_scratch =
754 DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
755 dwc->has_hibernation = 1;
756 }
757
758
759
760
761
762 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
763 break;
764 default:
765
766 break;
767 }
768
769
770 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
771 dev_info(dwc->dev, "Running with FPGA optmizations\n");
772 dwc->is_fpga = true;
773 }
774
775 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
776 "disable_scramble cannot be used on non-FPGA builds\n");
777
778 if (dwc->disable_scramble_quirk && dwc->is_fpga)
779 reg |= DWC3_GCTL_DISSCRAMBLE;
780 else
781 reg &= ~DWC3_GCTL_DISSCRAMBLE;
782
783 if (dwc->u2exit_lfps_quirk)
784 reg |= DWC3_GCTL_U2EXIT_LFPS;
785
786
787
788
789
790
791
792 if (dwc->revision < DWC3_REVISION_190A)
793 reg |= DWC3_GCTL_U2RSTECN;
794
795 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
796}
797
798static int dwc3_core_get_phy(struct dwc3 *dwc);
799
800
801
802
803
804
805
806int dwc3_core_init(struct dwc3 *dwc)
807{
808 u32 reg;
809 int ret;
810
811 if (!dwc3_core_is_valid(dwc)) {
812 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
813 ret = -ENODEV;
814 goto err0;
815 }
816
817
818
819
820
821 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
822
823
824 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
825 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
826 if (dwc->maximum_speed == USB_SPEED_SUPER)
827 dwc->maximum_speed = USB_SPEED_HIGH;
828 }
829
830 ret = dwc3_core_get_phy(dwc);
831 if (ret)
832 goto err0;
833
834 ret = dwc3_core_soft_reset(dwc);
835 if (ret)
836 goto err0;
837
838 ret = dwc3_config_soc_bus(dwc);
839 if (ret)
840 goto err0;
841
842 ret = dwc3_phy_setup(dwc);
843 if (ret)
844 goto err0;
845
846 dwc3_core_setup_global_control(dwc);
847 dwc3_core_num_eps(dwc);
848
849 if (dwc->scratchbuf == NULL) {
850 ret = dwc3_alloc_scratch_buffers(dwc);
851 if (ret) {
852 dev_err(dwc->dev,
853 "Not enough memory for scratch buffers\n");
854 goto err1;
855 }
856 }
857
858 ret = dwc3_setup_scratch_buffers(dwc);
859 if (ret) {
860 dev_err(dwc->dev, "Failed to setup scratch buffers: %d\n", ret);
861 goto err1;
862 }
863
864
865 dwc3_frame_length_adjustment(dwc);
866
867 usb_phy_set_suspend(dwc->usb2_phy, 0);
868 usb_phy_set_suspend(dwc->usb3_phy, 0);
869 ret = phy_power_on(dwc->usb2_generic_phy);
870 if (ret < 0)
871 goto err2;
872
873 ret = phy_power_on(dwc->usb3_generic_phy);
874 if (ret < 0)
875 goto err3;
876
877 ret = dwc3_event_buffers_setup(dwc);
878 if (ret) {
879 dev_err(dwc->dev, "failed to setup event buffers\n");
880 goto err4;
881 }
882
883 switch (dwc->dr_mode) {
884 case USB_DR_MODE_PERIPHERAL:
885 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
886 break;
887 case USB_DR_MODE_HOST:
888 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
889 break;
890 case USB_DR_MODE_OTG:
891 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_OTG);
892 break;
893 default:
894 dev_warn(dwc->dev, "Unsupported mode %d\n", dwc->dr_mode);
895 break;
896 }
897
898
899
900
901
902
903 if (!dwc3_is_usb31(dwc) && dwc->revision >= DWC3_REVISION_310A) {
904 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
905 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
906 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
907 }
908
909
910
911
912
913
914
915 if (dwc->enable_guctl1_resume_quirk) {
916 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
917 reg |= DWC3_GUCTL1_RESUME_QUIRK;
918 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
919 }
920
921
922
923
924
925
926
927
928
929 if (dwc->enable_guctl1_ipd_quirk) {
930 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
931 reg |= DWC3_GUCTL1_IPD_QUIRK;
932 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
933 }
934
935 if (dwc->revision >= DWC3_REVISION_250A) {
936 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
937
938
939
940
941
942 if (dwc->revision >= DWC3_REVISION_290A)
943 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
944
945 if (dwc->dis_tx_ipgap_linecheck_quirk)
946 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
947
948 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
949 }
950
951 return 0;
952
953err4:
954 phy_power_off(dwc->usb3_generic_phy);
955
956err3:
957 phy_power_off(dwc->usb2_generic_phy);
958
959err2:
960 usb_phy_set_suspend(dwc->usb2_phy, 1);
961 usb_phy_set_suspend(dwc->usb3_phy, 1);
962
963err1:
964 usb_phy_shutdown(dwc->usb2_phy);
965 usb_phy_shutdown(dwc->usb3_phy);
966 phy_exit(dwc->usb2_generic_phy);
967 phy_exit(dwc->usb3_generic_phy);
968
969err0:
970 return ret;
971}
972
973static int dwc3_core_get_phy(struct dwc3 *dwc)
974{
975 struct device *dev = dwc->dev;
976 struct device_node *node = dev->of_node;
977 int ret;
978
979 if (node) {
980 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
981 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
982 } else {
983 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
984 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
985 }
986
987 if (IS_ERR(dwc->usb2_phy)) {
988 ret = PTR_ERR(dwc->usb2_phy);
989 if (ret == -ENXIO || ret == -ENODEV) {
990 dwc->usb2_phy = NULL;
991 } else if (ret == -EPROBE_DEFER) {
992 return ret;
993 } else {
994 dev_err(dev, "no usb2 phy configured\n");
995 return ret;
996 }
997 }
998
999 if (IS_ERR(dwc->usb3_phy)) {
1000 ret = PTR_ERR(dwc->usb3_phy);
1001 if (ret == -ENXIO || ret == -ENODEV) {
1002 dwc->usb3_phy = NULL;
1003 } else if (ret == -EPROBE_DEFER) {
1004 return ret;
1005 } else {
1006 dev_err(dev, "no usb3 phy configured\n");
1007 return ret;
1008 }
1009 }
1010
1011 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
1012 if (IS_ERR(dwc->usb2_generic_phy)) {
1013 ret = PTR_ERR(dwc->usb2_generic_phy);
1014 if (ret == -ENOSYS || ret == -ENODEV) {
1015 dwc->usb2_generic_phy = NULL;
1016 } else if (ret == -EPROBE_DEFER) {
1017 return ret;
1018 } else {
1019 dev_err(dev, "no usb2 phy configured\n");
1020 return ret;
1021 }
1022 }
1023
1024 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
1025 if (IS_ERR(dwc->usb3_generic_phy)) {
1026 ret = PTR_ERR(dwc->usb3_generic_phy);
1027 if (ret == -ENOSYS || ret == -ENODEV) {
1028 dwc->usb3_generic_phy = NULL;
1029 } else if (ret == -EPROBE_DEFER) {
1030 return ret;
1031 } else {
1032 dev_err(dev, "no usb3 phy configured\n");
1033 return ret;
1034 }
1035 }
1036
1037 return 0;
1038}
1039
1040static int dwc3_core_init_mode(struct dwc3 *dwc)
1041{
1042 struct device *dev = dwc->dev;
1043 int ret;
1044
1045 switch (dwc->dr_mode) {
1046 case USB_DR_MODE_PERIPHERAL:
1047 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1048
1049 if (dwc->usb2_phy)
1050 otg_set_vbus(dwc->usb2_phy->otg, false);
1051 if (dwc->usb2_generic_phy)
1052 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
1053
1054 ret = dwc3_gadget_init(dwc);
1055 if (ret) {
1056 if (ret != -EPROBE_DEFER)
1057 dev_err(dev, "failed to initialize gadget\n");
1058 return ret;
1059 }
1060 break;
1061 case USB_DR_MODE_HOST:
1062 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1063
1064 if (dwc->usb2_phy)
1065 otg_set_vbus(dwc->usb2_phy->otg, true);
1066 if (dwc->usb2_generic_phy)
1067 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
1068
1069 ret = dwc3_host_init(dwc);
1070 if (ret) {
1071 if (ret != -EPROBE_DEFER)
1072 dev_err(dev, "failed to initialize host\n");
1073 return ret;
1074 }
1075 break;
1076 case USB_DR_MODE_OTG:
1077 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_OTG);
1078
1079 ret = dwc3_otg_init(dwc);
1080 if (ret) {
1081 if (ret != -EPROBE_DEFER)
1082 dev_err(dev, "failed to initialize otg\n");
1083 return ret;
1084 }
1085
1086 ret = dwc3_gadget_init(dwc);
1087 if (ret) {
1088 if (ret != -EPROBE_DEFER)
1089 dev_err(dev, "failed to initialize gadget\n");
1090 return ret;
1091 }
1092
1093 ret = dwc3_host_init(dwc);
1094 if (ret) {
1095 if (ret != -EPROBE_DEFER)
1096 dev_err(dev, "failed to initialize host\n");
1097 return ret;
1098 }
1099
1100 INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
1101 ret = dwc3_drd_init(dwc);
1102 if (ret) {
1103 if (ret != -EPROBE_DEFER)
1104 dev_err(dev, "failed to initialize dual-role\n");
1105 return ret;
1106 }
1107 break;
1108 default:
1109 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1110 return -EINVAL;
1111 }
1112
1113 return 0;
1114}
1115
1116static void dwc3_core_exit_mode(struct dwc3 *dwc)
1117{
1118 switch (dwc->dr_mode) {
1119 case USB_DR_MODE_PERIPHERAL:
1120 dwc3_gadget_exit(dwc);
1121 break;
1122 case USB_DR_MODE_HOST:
1123 dwc3_host_exit(dwc);
1124 break;
1125 case USB_DR_MODE_OTG:
1126 dwc3_drd_exit(dwc);
1127 break;
1128 default:
1129
1130 break;
1131 }
1132}
1133
1134static void dwc3_get_properties(struct dwc3 *dwc)
1135{
1136 struct device *dev = dwc->dev;
1137 u8 lpm_nyet_threshold;
1138 u8 tx_de_emphasis;
1139 u8 hird_threshold;
1140
1141
1142 lpm_nyet_threshold = 0xff;
1143
1144
1145 tx_de_emphasis = 1;
1146
1147
1148
1149
1150
1151 hird_threshold = 12;
1152
1153 dwc->maximum_speed = usb_get_maximum_speed(dev);
1154 dwc->dr_mode = usb_get_dr_mode(dev);
1155 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
1156
1157 dwc->sysdev_is_parent = device_property_read_bool(dev,
1158 "linux,sysdev_is_parent");
1159 if (dwc->sysdev_is_parent)
1160 dwc->sysdev = dwc->dev->parent;
1161 else
1162 dwc->sysdev = dwc->dev;
1163
1164 dwc->has_lpm_erratum = device_property_read_bool(dev,
1165 "snps,has-lpm-erratum");
1166 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
1167 &lpm_nyet_threshold);
1168 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
1169 "snps,is-utmi-l1-suspend");
1170 device_property_read_u8(dev, "snps,hird-threshold",
1171 &hird_threshold);
1172 dwc->usb3_lpm_capable = device_property_read_bool(dev,
1173 "snps,usb3_lpm_capable");
1174
1175 dwc->disable_scramble_quirk = device_property_read_bool(dev,
1176 "snps,disable_scramble_quirk");
1177 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
1178 "snps,u2exit_lfps_quirk");
1179 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
1180 "snps,u2ss_inp3_quirk");
1181 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
1182 "snps,req_p1p2p3_quirk");
1183 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
1184 "snps,del_p1p2p3_quirk");
1185 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
1186 "snps,del_phy_power_chg_quirk");
1187 dwc->lfps_filter_quirk = device_property_read_bool(dev,
1188 "snps,lfps_filter_quirk");
1189 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
1190 "snps,rx_detect_poll_quirk");
1191 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
1192 "snps,dis_u3_susphy_quirk");
1193 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
1194 "snps,dis_u2_susphy_quirk");
1195 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1196 "snps,dis_enblslpm_quirk");
1197 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1198 "snps,dis_rxdet_inp3_quirk");
1199 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1200 "snps,dis-u2-freeclk-exists-quirk");
1201 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1202 "snps,dis-del-phy-power-chg-quirk");
1203 dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1204 "snps,dis-tx-ipgap-linecheck-quirk");
1205
1206 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
1207 "snps,tx_de_emphasis_quirk");
1208 device_property_read_u8(dev, "snps,tx_de_emphasis",
1209 &tx_de_emphasis);
1210 device_property_read_string(dev, "snps,hsphy_interface",
1211 &dwc->hsphy_interface);
1212 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
1213 &dwc->fladj);
1214
1215 dwc->refclk_fladj = device_property_read_bool(dev,
1216 "snps,refclk_fladj");
1217 dwc->enable_guctl1_resume_quirk = device_property_read_bool(dev,
1218 "snps,enable_guctl1_resume_quirk");
1219 dwc->enable_guctl1_ipd_quirk = device_property_read_bool(dev,
1220 "snps,enable_guctl1_ipd_quirk");
1221
1222 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1223 dwc->tx_de_emphasis = tx_de_emphasis;
1224
1225 dwc->hird_threshold = hird_threshold
1226 | (dwc->is_utmi_l1_suspend << 4);
1227
1228
1229 dwc3_simple_check_quirks(dwc);
1230
1231 dwc->imod_interval = 0;
1232}
1233
1234
1235bool dwc3_has_imod(struct dwc3 *dwc)
1236{
1237 return ((dwc3_is_usb3(dwc) &&
1238 dwc->revision >= DWC3_REVISION_300A) ||
1239 (dwc3_is_usb31(dwc) &&
1240 dwc->revision >= DWC3_USB31_REVISION_120A));
1241}
1242
1243static void dwc3_check_params(struct dwc3 *dwc)
1244{
1245 struct device *dev = dwc->dev;
1246
1247
1248 if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
1249 dev_warn(dwc->dev, "Interrupt moderation not supported\n");
1250 dwc->imod_interval = 0;
1251 }
1252
1253
1254
1255
1256
1257
1258
1259
1260 if (!dwc->imod_interval &&
1261 (dwc->revision == DWC3_REVISION_300A))
1262 dwc->imod_interval = 1;
1263
1264
1265 switch (dwc->maximum_speed) {
1266 case USB_SPEED_LOW:
1267 case USB_SPEED_FULL:
1268 case USB_SPEED_HIGH:
1269 case USB_SPEED_SUPER:
1270 case USB_SPEED_SUPER_PLUS:
1271 break;
1272 default:
1273 dev_err(dev, "invalid maximum_speed parameter %d\n",
1274 dwc->maximum_speed);
1275
1276 case USB_SPEED_UNKNOWN:
1277
1278 dwc->maximum_speed = USB_SPEED_SUPER;
1279
1280
1281
1282
1283 if (dwc3_is_usb31(dwc) &&
1284 (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
1285 DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1286 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1287
1288 break;
1289 }
1290}
1291
1292static int dwc3_probe(struct platform_device *pdev)
1293{
1294 struct device *dev = &pdev->dev;
1295 struct resource *res;
1296 struct dwc3 *dwc;
1297 int ret;
1298 u32 mdwidth;
1299 void __iomem *regs;
1300
1301 dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
1302 if (!dwc)
1303 return -ENOMEM;
1304
1305 dwc->dev = dev;
1306
1307 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1308 if (!res) {
1309 dev_err(dev, "missing memory resource\n");
1310 return -ENODEV;
1311 }
1312
1313 dwc->xhci_resources[0].start = res->start;
1314 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1315 DWC3_XHCI_REGS_END;
1316 dwc->xhci_resources[0].flags = res->flags;
1317 dwc->xhci_resources[0].name = res->name;
1318
1319 res->start += DWC3_GLOBALS_REGS_START;
1320
1321
1322
1323
1324
1325 regs = devm_ioremap_resource(dev, res);
1326 if (IS_ERR(regs)) {
1327 ret = PTR_ERR(regs);
1328 goto err0;
1329 }
1330
1331 dwc->regs = regs;
1332 dwc->regs_size = resource_size(res);
1333
1334 dwc3_get_properties(dwc);
1335
1336 platform_set_drvdata(pdev, dwc);
1337 dwc3_cache_hwparams(dwc);
1338
1339 spin_lock_init(&dwc->lock);
1340
1341
1342 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1343 dev_dbg(dev, "Enabling %d-bit DMA addresses.\n", mdwidth);
1344 dma_set_coherent_mask(dev, DMA_BIT_MASK(mdwidth));
1345
1346 pm_runtime_set_active(dev);
1347 pm_runtime_use_autosuspend(dev);
1348 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
1349 pm_runtime_enable(dev);
1350 ret = pm_runtime_get_sync(dev);
1351 if (ret < 0)
1352 goto err1;
1353
1354 pm_runtime_forbid(dev);
1355
1356 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1357 if (ret) {
1358 dev_err(dwc->dev, "failed to allocate event buffers\n");
1359 ret = -ENOMEM;
1360 goto err2;
1361 }
1362
1363 ret = dwc3_get_dr_mode(dwc);
1364 if (ret)
1365 goto err3;
1366
1367 ret = dwc3_core_init(dwc);
1368 if (ret) {
1369 dev_err(dev, "failed to initialize core\n");
1370 goto err4;
1371 }
1372
1373 dwc3_check_params(dwc);
1374
1375 ret = dwc3_core_init_mode(dwc);
1376 if (ret)
1377 goto err5;
1378
1379 dwc3_debugfs_init(dwc);
1380 pm_runtime_put(dev);
1381
1382 return 0;
1383
1384err5:
1385 dwc3_event_buffers_cleanup(dwc);
1386
1387err4:
1388 dwc3_free_scratch_buffers(dwc);
1389
1390err3:
1391 dwc3_free_event_buffers(dwc);
1392 dwc3_ulpi_exit(dwc);
1393
1394err2:
1395 pm_runtime_allow(&pdev->dev);
1396
1397err1:
1398 pm_runtime_put_sync(&pdev->dev);
1399 pm_runtime_disable(&pdev->dev);
1400
1401err0:
1402
1403
1404
1405
1406
1407 res->start -= DWC3_GLOBALS_REGS_START;
1408
1409 return ret;
1410}
1411
1412static int dwc3_remove(struct platform_device *pdev)
1413{
1414 struct dwc3 *dwc = platform_get_drvdata(pdev);
1415 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1416
1417 pm_runtime_get_sync(&pdev->dev);
1418
1419
1420
1421
1422
1423 res->start -= DWC3_GLOBALS_REGS_START;
1424
1425 dwc3_debugfs_exit(dwc);
1426 dwc3_core_exit_mode(dwc);
1427
1428 dwc3_event_buffers_cleanup(dwc);
1429 dwc3_core_exit(dwc);
1430 dwc3_ulpi_exit(dwc);
1431
1432 pm_runtime_put_sync(&pdev->dev);
1433 pm_runtime_allow(&pdev->dev);
1434 pm_runtime_disable(&pdev->dev);
1435
1436 dwc3_free_event_buffers(dwc);
1437 dwc3_free_scratch_buffers(dwc);
1438
1439 return 0;
1440}
1441
1442#ifdef CONFIG_PM
1443static int dwc3_suspend_common(struct dwc3 *dwc)
1444{
1445 unsigned long flags;
1446
1447 switch (dwc->dr_mode) {
1448 case USB_DR_MODE_PERIPHERAL:
1449 case USB_DR_MODE_OTG:
1450 spin_lock_irqsave(&dwc->lock, flags);
1451 dwc3_gadget_suspend(dwc);
1452 spin_unlock_irqrestore(&dwc->lock, flags);
1453 break;
1454 case USB_DR_MODE_HOST:
1455 default:
1456
1457 break;
1458 }
1459
1460 dwc3_event_buffers_cleanup(dwc);
1461
1462
1463 dwc3_set_usb_core_power(dwc, false);
1464
1465
1466
1467
1468
1469 if (!dwc->is_d3)
1470 dwc3_core_exit(dwc);
1471
1472 return 0;
1473}
1474
1475static int dwc3_resume_common(struct dwc3 *dwc)
1476{
1477 unsigned long flags;
1478 int ret;
1479
1480
1481 dwc3_set_usb_core_power(dwc, true);
1482
1483 ret = dwc3_core_init(dwc);
1484 if (ret)
1485 return ret;
1486
1487 switch (dwc->dr_mode) {
1488 case USB_DR_MODE_PERIPHERAL:
1489 case USB_DR_MODE_OTG:
1490 spin_lock_irqsave(&dwc->lock, flags);
1491 dwc3_gadget_resume(dwc);
1492 spin_unlock_irqrestore(&dwc->lock, flags);
1493
1494 case USB_DR_MODE_HOST:
1495 default:
1496
1497 break;
1498 }
1499
1500 return 0;
1501}
1502
1503static int dwc3_runtime_checks(struct dwc3 *dwc)
1504{
1505 switch (dwc->dr_mode) {
1506 case USB_DR_MODE_PERIPHERAL:
1507 case USB_DR_MODE_OTG:
1508 if (dwc->connected)
1509 return -EBUSY;
1510 break;
1511 case USB_DR_MODE_HOST:
1512 default:
1513
1514 break;
1515 }
1516
1517 return 0;
1518}
1519
1520static int dwc3_runtime_suspend(struct device *dev)
1521{
1522 struct dwc3 *dwc = dev_get_drvdata(dev);
1523 int ret;
1524
1525 if (dwc3_runtime_checks(dwc))
1526 return -EBUSY;
1527
1528 ret = dwc3_suspend_common(dwc);
1529 if (ret)
1530 return ret;
1531
1532 device_init_wakeup(dev, true);
1533
1534 return 0;
1535}
1536
1537static int dwc3_runtime_resume(struct device *dev)
1538{
1539 struct dwc3 *dwc = dev_get_drvdata(dev);
1540 int ret;
1541
1542 device_init_wakeup(dev, false);
1543
1544 ret = dwc3_resume_common(dwc);
1545 if (ret)
1546 return ret;
1547
1548 switch (dwc->dr_mode) {
1549 case USB_DR_MODE_PERIPHERAL:
1550 case USB_DR_MODE_OTG:
1551 dwc3_gadget_process_pending_events(dwc);
1552 break;
1553 case USB_DR_MODE_HOST:
1554 default:
1555
1556 break;
1557 }
1558
1559 pm_runtime_mark_last_busy(dev);
1560 pm_runtime_put(dev);
1561
1562 return 0;
1563}
1564
1565static int dwc3_runtime_idle(struct device *dev)
1566{
1567 struct dwc3 *dwc = dev_get_drvdata(dev);
1568
1569 switch (dwc->dr_mode) {
1570 case USB_DR_MODE_PERIPHERAL:
1571 case USB_DR_MODE_OTG:
1572 if (dwc3_runtime_checks(dwc))
1573 return -EBUSY;
1574 break;
1575 case USB_DR_MODE_HOST:
1576 default:
1577
1578 break;
1579 }
1580
1581 pm_runtime_mark_last_busy(dev);
1582 pm_runtime_autosuspend(dev);
1583
1584 return 0;
1585}
1586#endif
1587
1588#ifdef CONFIG_PM_SLEEP
1589static int dwc3_suspend(struct device *dev)
1590{
1591 struct dwc3 *dwc = dev_get_drvdata(dev);
1592 int ret;
1593
1594 ret = dwc3_suspend_common(dwc);
1595 if (ret)
1596 return ret;
1597
1598 pinctrl_pm_select_sleep_state(dev);
1599
1600 return 0;
1601}
1602
1603static int dwc3_resume(struct device *dev)
1604{
1605 struct dwc3 *dwc = dev_get_drvdata(dev);
1606 int ret;
1607
1608 pinctrl_pm_select_default_state(dev);
1609
1610 ret = dwc3_resume_common(dwc);
1611 if (ret)
1612 return ret;
1613
1614 pm_runtime_disable(dev);
1615 pm_runtime_set_active(dev);
1616 pm_runtime_enable(dev);
1617
1618 return 0;
1619}
1620#endif
1621
1622static const struct dev_pm_ops dwc3_dev_pm_ops = {
1623 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
1624 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
1625 dwc3_runtime_idle)
1626};
1627
1628#ifdef CONFIG_OF
1629static const struct of_device_id of_dwc3_match[] = {
1630 {
1631 .compatible = "snps,dwc3"
1632 },
1633 {
1634 .compatible = "synopsys,dwc3"
1635 },
1636 { },
1637};
1638MODULE_DEVICE_TABLE(of, of_dwc3_match);
1639#endif
1640
1641#ifdef CONFIG_ACPI
1642
1643#define ACPI_ID_INTEL_BSW "808622B7"
1644
1645static const struct acpi_device_id dwc3_acpi_match[] = {
1646 { ACPI_ID_INTEL_BSW, 0 },
1647 { },
1648};
1649MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
1650#endif
1651
1652static struct platform_driver dwc3_driver = {
1653 .probe = dwc3_probe,
1654 .remove = dwc3_remove,
1655 .driver = {
1656 .name = "dwc3",
1657 .of_match_table = of_match_ptr(of_dwc3_match),
1658 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
1659 .pm = &dwc3_dev_pm_ops,
1660 },
1661};
1662
1663module_platform_driver(dwc3_driver);
1664
1665MODULE_ALIAS("platform:dwc3");
1666MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1667MODULE_LICENSE("GPL v2");
1668MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");
1669