1 // SPDX-License-Identifier: GPL-2.0
3 * core.c - DesignWare USB3 DRD Controller Core file
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
11 #include <linux/clk.h>
12 #include <linux/version.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/interrupt.h>
20 #include <linux/ioport.h>
22 #include <linux/list.h>
23 #include <linux/delay.h>
24 #include <linux/dma-mapping.h>
26 #include <linux/acpi.h>
27 #include <linux/pinctrl/consumer.h>
28 #include <linux/reset.h>
30 #include <linux/usb/ch9.h>
31 #include <linux/usb/gadget.h>
32 #include <linux/usb/of.h>
33 #include <linux/usb/otg.h>
41 #define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
44 * dwc3_get_dr_mode - Validates and sets dr_mode
45 * @dwc: pointer to our context structure
47 static int dwc3_get_dr_mode(struct dwc3 *dwc)
49 enum usb_dr_mode mode;
50 struct device *dev = dwc->dev;
53 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
54 dwc->dr_mode = USB_DR_MODE_OTG;
57 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
60 case DWC3_GHWPARAMS0_MODE_GADGET:
61 if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) {
63 "Controller does not support host mode.\n");
66 mode = USB_DR_MODE_PERIPHERAL;
68 case DWC3_GHWPARAMS0_MODE_HOST:
69 if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
71 "Controller does not support device mode.\n");
74 mode = USB_DR_MODE_HOST;
77 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
78 mode = USB_DR_MODE_HOST;
79 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
80 mode = USB_DR_MODE_PERIPHERAL;
83 if (mode != dwc->dr_mode) {
85 "Configuration mismatch. dr_mode forced to %s\n",
86 mode == USB_DR_MODE_HOST ? "host" : "gadget");
94 void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
98 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
99 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
100 reg |= DWC3_GCTL_PRTCAPDIR(mode);
101 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
103 dwc->current_dr_role = mode;
106 static void __dwc3_set_mode(struct work_struct *work)
108 struct dwc3 *dwc = work_to_dwc(work);
112 if (dwc->dr_mode != USB_DR_MODE_OTG)
115 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
116 dwc3_otg_update(dwc, 0);
118 if (!dwc->desired_dr_role)
121 if (dwc->desired_dr_role == dwc->current_dr_role)
124 if (dwc->desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev)
127 switch (dwc->current_dr_role) {
128 case DWC3_GCTL_PRTCAP_HOST:
131 case DWC3_GCTL_PRTCAP_DEVICE:
132 dwc3_gadget_exit(dwc);
133 dwc3_event_buffers_cleanup(dwc);
135 case DWC3_GCTL_PRTCAP_OTG:
137 spin_lock_irqsave(&dwc->lock, flags);
138 dwc->desired_otg_role = DWC3_OTG_ROLE_IDLE;
139 spin_unlock_irqrestore(&dwc->lock, flags);
140 dwc3_otg_update(dwc, 1);
146 spin_lock_irqsave(&dwc->lock, flags);
148 dwc3_set_prtcap(dwc, dwc->desired_dr_role);
150 spin_unlock_irqrestore(&dwc->lock, flags);
152 switch (dwc->desired_dr_role) {
153 case DWC3_GCTL_PRTCAP_HOST:
154 ret = dwc3_host_init(dwc);
156 dev_err(dwc->dev, "failed to initialize host\n");
159 otg_set_vbus(dwc->usb2_phy->otg, true);
160 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
161 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
162 phy_calibrate(dwc->usb2_generic_phy);
165 case DWC3_GCTL_PRTCAP_DEVICE:
166 dwc3_event_buffers_setup(dwc);
169 otg_set_vbus(dwc->usb2_phy->otg, false);
170 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
171 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
173 ret = dwc3_gadget_init(dwc);
175 dev_err(dwc->dev, "failed to initialize peripheral\n");
177 case DWC3_GCTL_PRTCAP_OTG:
179 dwc3_otg_update(dwc, 0);
187 void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
191 spin_lock_irqsave(&dwc->lock, flags);
192 dwc->desired_dr_role = mode;
193 spin_unlock_irqrestore(&dwc->lock, flags);
195 queue_work(system_freezable_wq, &dwc->drd_work);
198 u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
200 struct dwc3 *dwc = dep->dwc;
203 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
204 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
205 DWC3_GDBGFIFOSPACE_TYPE(type));
207 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
209 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
213 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
214 * @dwc: pointer to our context structure
216 static int dwc3_core_soft_reset(struct dwc3 *dwc)
222 usb_phy_init(dwc->usb2_phy);
223 usb_phy_init(dwc->usb3_phy);
224 ret = phy_init(dwc->usb2_generic_phy);
228 ret = phy_init(dwc->usb3_generic_phy);
230 phy_exit(dwc->usb2_generic_phy);
235 * We're resetting only the device side because, if we're in host mode,
236 * XHCI driver will reset the host block. If dwc3 was configured for
237 * host-only mode, then we can return early.
239 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST)
242 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
243 reg |= DWC3_DCTL_CSFTRST;
244 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
247 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
248 if (!(reg & DWC3_DCTL_CSFTRST))
254 phy_exit(dwc->usb3_generic_phy);
255 phy_exit(dwc->usb2_generic_phy);
261 * For DWC_usb31 controller, once DWC3_DCTL_CSFTRST bit is cleared,
262 * we must wait at least 50ms before accessing the PHY domain
263 * (synchronization delay). DWC_usb31 programming guide section 1.3.2.
265 if (dwc3_is_usb31(dwc))
271 static const struct clk_bulk_data dwc3_core_clks[] = {
273 { .id = "bus_early" },
278 * dwc3_frame_length_adjustment - Adjusts frame length if required
279 * @dwc3: Pointer to our controller context structure
281 static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
286 if (dwc->revision < DWC3_REVISION_250A)
292 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
293 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
294 if (!dev_WARN_ONCE(dwc->dev, dft == dwc->fladj,
295 "request value same as default, ignoring\n")) {
296 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
297 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
298 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
303 * dwc3_free_one_event_buffer - Frees one event buffer
304 * @dwc: Pointer to our controller context structure
305 * @evt: Pointer to event buffer to be freed
307 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
308 struct dwc3_event_buffer *evt)
310 dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
314 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
315 * @dwc: Pointer to our controller context structure
316 * @length: size of the event buffer
318 * Returns a pointer to the allocated event buffer structure on success
319 * otherwise ERR_PTR(errno).
321 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
324 struct dwc3_event_buffer *evt;
326 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
328 return ERR_PTR(-ENOMEM);
331 evt->length = length;
332 evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
334 return ERR_PTR(-ENOMEM);
336 evt->buf = dma_alloc_coherent(dwc->sysdev, length,
337 &evt->dma, GFP_KERNEL);
339 return ERR_PTR(-ENOMEM);
345 * dwc3_free_event_buffers - frees all allocated event buffers
346 * @dwc: Pointer to our controller context structure
348 static void dwc3_free_event_buffers(struct dwc3 *dwc)
350 struct dwc3_event_buffer *evt;
354 dwc3_free_one_event_buffer(dwc, evt);
358 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
359 * @dwc: pointer to our controller context structure
360 * @length: size of event buffer
362 * Returns 0 on success otherwise negative errno. In the error case, dwc
363 * may contain some buffers allocated but not all which were requested.
365 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
367 struct dwc3_event_buffer *evt;
369 evt = dwc3_alloc_one_event_buffer(dwc, length);
371 dev_err(dwc->dev, "can't allocate event buffer\n");
380 * dwc3_event_buffers_setup - setup our allocated event buffers
381 * @dwc: pointer to our controller context structure
383 * Returns 0 on success otherwise negative errno.
385 int dwc3_event_buffers_setup(struct dwc3 *dwc)
387 struct dwc3_event_buffer *evt;
391 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
392 lower_32_bits(evt->dma));
393 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
394 upper_32_bits(evt->dma));
395 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
396 DWC3_GEVNTSIZ_SIZE(evt->length));
397 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
402 void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
404 struct dwc3_event_buffer *evt;
410 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
411 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
412 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
413 | DWC3_GEVNTSIZ_SIZE(0));
414 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
417 static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
419 if (!dwc->has_hibernation)
422 if (!dwc->nr_scratch)
425 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
426 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
427 if (!dwc->scratchbuf)
433 static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
435 dma_addr_t scratch_addr;
439 if (!dwc->has_hibernation)
442 if (!dwc->nr_scratch)
445 /* should never fall here */
446 if (!WARN_ON(dwc->scratchbuf))
449 scratch_addr = dma_map_single(dwc->sysdev, dwc->scratchbuf,
450 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
452 if (dma_mapping_error(dwc->sysdev, scratch_addr)) {
453 dev_err(dwc->sysdev, "failed to map scratch buffer\n");
458 dwc->scratch_addr = scratch_addr;
460 param = lower_32_bits(scratch_addr);
462 ret = dwc3_send_gadget_generic_command(dwc,
463 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
467 param = upper_32_bits(scratch_addr);
469 ret = dwc3_send_gadget_generic_command(dwc,
470 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
477 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
478 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
484 static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
486 if (!dwc->has_hibernation)
489 if (!dwc->nr_scratch)
492 /* should never fall here */
493 if (!WARN_ON(dwc->scratchbuf))
496 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
497 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
498 kfree(dwc->scratchbuf);
501 static void dwc3_core_num_eps(struct dwc3 *dwc)
503 struct dwc3_hwparams *parms = &dwc->hwparams;
505 dwc->num_eps = DWC3_NUM_EPS(parms);
508 static void dwc3_cache_hwparams(struct dwc3 *dwc)
510 struct dwc3_hwparams *parms = &dwc->hwparams;
512 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
513 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
514 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
515 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
516 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
517 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
518 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
519 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
520 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
523 static int dwc3_core_ulpi_init(struct dwc3 *dwc)
528 intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
530 if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
531 (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
532 dwc->hsphy_interface &&
533 !strncmp(dwc->hsphy_interface, "ulpi", 4)))
534 ret = dwc3_ulpi_init(dwc);
540 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
541 * @dwc: Pointer to our controller context structure
543 * Returns 0 on success. The USB PHY interfaces are configured but not
544 * initialized. The PHY interfaces and the PHYs get initialized together with
545 * the core in dwc3_core_init.
547 static int dwc3_phy_setup(struct dwc3 *dwc)
551 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
554 * Make sure UX_EXIT_PX is cleared as that causes issues with some
555 * PHYs. Also, this bit is not supposed to be used in normal operation.
557 reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
560 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
561 * to '0' during coreConsultant configuration. So default value
562 * will be '0' when the core is reset. Application needs to set it
563 * to '1' after the core initialization is completed.
565 if (dwc->revision > DWC3_REVISION_194A)
566 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
568 if (dwc->u2ss_inp3_quirk)
569 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
571 if (dwc->dis_rxdet_inp3_quirk)
572 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
574 if (dwc->req_p1p2p3_quirk)
575 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
577 if (dwc->del_p1p2p3_quirk)
578 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
580 if (dwc->del_phy_power_chg_quirk)
581 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
583 if (dwc->lfps_filter_quirk)
584 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
586 if (dwc->rx_detect_poll_quirk)
587 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
589 if (dwc->tx_de_emphasis_quirk)
590 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
592 if (dwc->dis_u3_susphy_quirk)
593 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
595 if (dwc->dis_del_phy_power_chg_quirk)
596 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
598 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
600 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
602 /* Select the HS PHY interface */
603 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
604 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
605 if (dwc->hsphy_interface &&
606 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
607 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
609 } else if (dwc->hsphy_interface &&
610 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
611 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
612 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
614 /* Relying on default value. */
615 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
619 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
625 switch (dwc->hsphy_mode) {
626 case USBPHY_INTERFACE_MODE_UTMI:
627 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
628 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
629 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
630 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
632 case USBPHY_INTERFACE_MODE_UTMIW:
633 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
634 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
635 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
636 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
643 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
644 * '0' during coreConsultant configuration. So default value will
645 * be '0' when the core is reset. Application needs to set it to
646 * '1' after the core initialization is completed.
648 if (dwc->revision > DWC3_REVISION_194A)
649 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
651 if (dwc->dis_u2_susphy_quirk)
652 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
654 if (dwc->dis_enblslpm_quirk)
655 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
657 if (dwc->dis_u2_freeclk_exists_quirk)
658 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
660 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
665 static void dwc3_core_exit(struct dwc3 *dwc)
667 dwc3_event_buffers_cleanup(dwc);
669 usb_phy_shutdown(dwc->usb2_phy);
670 usb_phy_shutdown(dwc->usb3_phy);
671 phy_exit(dwc->usb2_generic_phy);
672 phy_exit(dwc->usb3_generic_phy);
674 usb_phy_set_suspend(dwc->usb2_phy, 1);
675 usb_phy_set_suspend(dwc->usb3_phy, 1);
676 phy_power_off(dwc->usb2_generic_phy);
677 phy_power_off(dwc->usb3_generic_phy);
678 clk_bulk_disable(dwc->num_clks, dwc->clks);
679 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
680 reset_control_assert(dwc->reset);
683 static bool dwc3_core_is_valid(struct dwc3 *dwc)
687 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
689 /* This should read as U3 followed by revision number */
690 if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
691 /* Detected DWC_usb3 IP */
693 } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
694 /* Detected DWC_usb31 IP */
695 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
696 dwc->revision |= DWC3_REVISION_IS_DWC31;
704 static void dwc3_core_setup_global_control(struct dwc3 *dwc)
706 u32 hwparams4 = dwc->hwparams.hwparams4;
709 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
710 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
712 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
713 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
715 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
716 * issue which would cause xHCI compliance tests to fail.
718 * Because of that we cannot enable clock gating on such
723 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
726 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
727 dwc->dr_mode == USB_DR_MODE_OTG) &&
728 (dwc->revision >= DWC3_REVISION_210A &&
729 dwc->revision <= DWC3_REVISION_250A))
730 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
732 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
734 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
735 /* enable hibernation here */
736 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
739 * REVISIT Enabling this bit so that host-mode hibernation
740 * will work. Device-mode hibernation is not yet implemented.
742 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
749 /* check if current dwc3 is on simulation board */
750 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
751 dev_info(dwc->dev, "Running with FPGA optmizations\n");
755 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
756 "disable_scramble cannot be used on non-FPGA builds\n");
758 if (dwc->disable_scramble_quirk && dwc->is_fpga)
759 reg |= DWC3_GCTL_DISSCRAMBLE;
761 reg &= ~DWC3_GCTL_DISSCRAMBLE;
763 if (dwc->u2exit_lfps_quirk)
764 reg |= DWC3_GCTL_U2EXIT_LFPS;
767 * WORKAROUND: DWC3 revisions <1.90a have a bug
768 * where the device can fail to connect at SuperSpeed
769 * and falls back to high-speed mode which causes
770 * the device to enter a Connect/Disconnect loop
772 if (dwc->revision < DWC3_REVISION_190A)
773 reg |= DWC3_GCTL_U2RSTECN;
775 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
778 static int dwc3_core_get_phy(struct dwc3 *dwc);
779 static int dwc3_core_ulpi_init(struct dwc3 *dwc);
782 * dwc3_core_init - Low-level initialization of DWC3 Core
783 * @dwc: Pointer to our controller context structure
785 * Returns 0 on success otherwise negative errno.
787 static int dwc3_core_init(struct dwc3 *dwc)
792 if (!dwc3_core_is_valid(dwc)) {
793 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
799 * Write Linux Version Code to our GUID register so it's easy to figure
800 * out which kernel version a bug was found.
802 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
804 /* Handle USB2.0-only core configuration */
805 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
806 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
807 if (dwc->maximum_speed == USB_SPEED_SUPER)
808 dwc->maximum_speed = USB_SPEED_HIGH;
811 ret = dwc3_phy_setup(dwc);
815 if (!dwc->ulpi_ready) {
816 ret = dwc3_core_ulpi_init(dwc);
819 dwc->ulpi_ready = true;
822 if (!dwc->phys_ready) {
823 ret = dwc3_core_get_phy(dwc);
826 dwc->phys_ready = true;
829 ret = dwc3_core_soft_reset(dwc);
833 dwc3_core_setup_global_control(dwc);
834 dwc3_core_num_eps(dwc);
836 ret = dwc3_setup_scratch_buffers(dwc);
840 /* Adjust Frame Length */
841 dwc3_frame_length_adjustment(dwc);
843 usb_phy_set_suspend(dwc->usb2_phy, 0);
844 usb_phy_set_suspend(dwc->usb3_phy, 0);
845 ret = phy_power_on(dwc->usb2_generic_phy);
849 ret = phy_power_on(dwc->usb3_generic_phy);
853 ret = dwc3_event_buffers_setup(dwc);
855 dev_err(dwc->dev, "failed to setup event buffers\n");
860 * ENDXFER polling is available on version 3.10a and later of
861 * the DWC_usb3 controller. It is NOT available in the
862 * DWC_usb31 controller.
864 if (!dwc3_is_usb31(dwc) && dwc->revision >= DWC3_REVISION_310A) {
865 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
866 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
867 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
870 if (dwc->revision >= DWC3_REVISION_250A) {
871 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
874 * Enable hardware control of sending remote wakeup
875 * in HS when the device is in the L1 state.
877 if (dwc->revision >= DWC3_REVISION_290A)
878 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
880 if (dwc->dis_tx_ipgap_linecheck_quirk)
881 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
883 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
887 * Must config both number of packets and max burst settings to enable
888 * RX and/or TX threshold.
890 if (dwc3_is_usb31(dwc) && dwc->dr_mode == USB_DR_MODE_HOST) {
891 u8 rx_thr_num = dwc->rx_thr_num_pkt_prd;
892 u8 rx_maxburst = dwc->rx_max_burst_prd;
893 u8 tx_thr_num = dwc->tx_thr_num_pkt_prd;
894 u8 tx_maxburst = dwc->tx_max_burst_prd;
896 if (rx_thr_num && rx_maxburst) {
897 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
898 reg |= DWC31_RXTHRNUMPKTSEL_PRD;
900 reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
901 reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
903 reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
904 reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
906 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
909 if (tx_thr_num && tx_maxburst) {
910 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
911 reg |= DWC31_TXTHRNUMPKTSEL_PRD;
913 reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
914 reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
916 reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
917 reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
919 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
926 phy_power_off(dwc->usb3_generic_phy);
929 phy_power_off(dwc->usb2_generic_phy);
932 usb_phy_set_suspend(dwc->usb2_phy, 1);
933 usb_phy_set_suspend(dwc->usb3_phy, 1);
936 usb_phy_shutdown(dwc->usb2_phy);
937 usb_phy_shutdown(dwc->usb3_phy);
938 phy_exit(dwc->usb2_generic_phy);
939 phy_exit(dwc->usb3_generic_phy);
948 static int dwc3_core_get_phy(struct dwc3 *dwc)
950 struct device *dev = dwc->dev;
951 struct device_node *node = dev->of_node;
955 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
956 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
958 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
959 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
962 if (IS_ERR(dwc->usb2_phy)) {
963 ret = PTR_ERR(dwc->usb2_phy);
964 if (ret == -ENXIO || ret == -ENODEV) {
965 dwc->usb2_phy = NULL;
966 } else if (ret == -EPROBE_DEFER) {
969 dev_err(dev, "no usb2 phy configured\n");
974 if (IS_ERR(dwc->usb3_phy)) {
975 ret = PTR_ERR(dwc->usb3_phy);
976 if (ret == -ENXIO || ret == -ENODEV) {
977 dwc->usb3_phy = NULL;
978 } else if (ret == -EPROBE_DEFER) {
981 dev_err(dev, "no usb3 phy configured\n");
986 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
987 if (IS_ERR(dwc->usb2_generic_phy)) {
988 ret = PTR_ERR(dwc->usb2_generic_phy);
989 if (ret == -ENOSYS || ret == -ENODEV) {
990 dwc->usb2_generic_phy = NULL;
991 } else if (ret == -EPROBE_DEFER) {
994 dev_err(dev, "no usb2 phy configured\n");
999 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
1000 if (IS_ERR(dwc->usb3_generic_phy)) {
1001 ret = PTR_ERR(dwc->usb3_generic_phy);
1002 if (ret == -ENOSYS || ret == -ENODEV) {
1003 dwc->usb3_generic_phy = NULL;
1004 } else if (ret == -EPROBE_DEFER) {
1007 dev_err(dev, "no usb3 phy configured\n");
1015 static int dwc3_core_init_mode(struct dwc3 *dwc)
1017 struct device *dev = dwc->dev;
1020 switch (dwc->dr_mode) {
1021 case USB_DR_MODE_PERIPHERAL:
1022 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1025 otg_set_vbus(dwc->usb2_phy->otg, false);
1026 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
1027 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
1029 ret = dwc3_gadget_init(dwc);
1031 if (ret != -EPROBE_DEFER)
1032 dev_err(dev, "failed to initialize gadget\n");
1036 case USB_DR_MODE_HOST:
1037 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1040 otg_set_vbus(dwc->usb2_phy->otg, true);
1041 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
1042 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
1044 ret = dwc3_host_init(dwc);
1046 if (ret != -EPROBE_DEFER)
1047 dev_err(dev, "failed to initialize host\n");
1050 phy_calibrate(dwc->usb2_generic_phy);
1052 case USB_DR_MODE_OTG:
1053 INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
1054 ret = dwc3_drd_init(dwc);
1056 if (ret != -EPROBE_DEFER)
1057 dev_err(dev, "failed to initialize dual-role\n");
1062 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1069 static void dwc3_core_exit_mode(struct dwc3 *dwc)
1071 switch (dwc->dr_mode) {
1072 case USB_DR_MODE_PERIPHERAL:
1073 dwc3_gadget_exit(dwc);
1075 case USB_DR_MODE_HOST:
1076 dwc3_host_exit(dwc);
1078 case USB_DR_MODE_OTG:
1087 static void dwc3_get_properties(struct dwc3 *dwc)
1089 struct device *dev = dwc->dev;
1090 u8 lpm_nyet_threshold;
1093 u8 rx_thr_num_pkt_prd;
1094 u8 rx_max_burst_prd;
1095 u8 tx_thr_num_pkt_prd;
1096 u8 tx_max_burst_prd;
1098 /* default to highest possible threshold */
1099 lpm_nyet_threshold = 0xff;
1101 /* default to -3.5dB de-emphasis */
1105 * default to assert utmi_sleep_n and use maximum allowed HIRD
1106 * threshold value of 0b1100
1108 hird_threshold = 12;
1110 dwc->maximum_speed = usb_get_maximum_speed(dev);
1111 dwc->dr_mode = usb_get_dr_mode(dev);
1112 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
1114 dwc->sysdev_is_parent = device_property_read_bool(dev,
1115 "linux,sysdev_is_parent");
1116 if (dwc->sysdev_is_parent)
1117 dwc->sysdev = dwc->dev->parent;
1119 dwc->sysdev = dwc->dev;
1121 dwc->has_lpm_erratum = device_property_read_bool(dev,
1122 "snps,has-lpm-erratum");
1123 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
1124 &lpm_nyet_threshold);
1125 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
1126 "snps,is-utmi-l1-suspend");
1127 device_property_read_u8(dev, "snps,hird-threshold",
1129 dwc->usb3_lpm_capable = device_property_read_bool(dev,
1130 "snps,usb3_lpm_capable");
1131 device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
1132 &rx_thr_num_pkt_prd);
1133 device_property_read_u8(dev, "snps,rx-max-burst-prd",
1135 device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
1136 &tx_thr_num_pkt_prd);
1137 device_property_read_u8(dev, "snps,tx-max-burst-prd",
1140 dwc->disable_scramble_quirk = device_property_read_bool(dev,
1141 "snps,disable_scramble_quirk");
1142 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
1143 "snps,u2exit_lfps_quirk");
1144 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
1145 "snps,u2ss_inp3_quirk");
1146 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
1147 "snps,req_p1p2p3_quirk");
1148 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
1149 "snps,del_p1p2p3_quirk");
1150 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
1151 "snps,del_phy_power_chg_quirk");
1152 dwc->lfps_filter_quirk = device_property_read_bool(dev,
1153 "snps,lfps_filter_quirk");
1154 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
1155 "snps,rx_detect_poll_quirk");
1156 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
1157 "snps,dis_u3_susphy_quirk");
1158 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
1159 "snps,dis_u2_susphy_quirk");
1160 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1161 "snps,dis_enblslpm_quirk");
1162 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1163 "snps,dis_rxdet_inp3_quirk");
1164 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1165 "snps,dis-u2-freeclk-exists-quirk");
1166 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1167 "snps,dis-del-phy-power-chg-quirk");
1168 dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1169 "snps,dis-tx-ipgap-linecheck-quirk");
1171 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
1172 "snps,tx_de_emphasis_quirk");
1173 device_property_read_u8(dev, "snps,tx_de_emphasis",
1175 device_property_read_string(dev, "snps,hsphy_interface",
1176 &dwc->hsphy_interface);
1177 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
1180 dwc->dis_metastability_quirk = device_property_read_bool(dev,
1181 "snps,dis_metastability_quirk");
1183 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1184 dwc->tx_de_emphasis = tx_de_emphasis;
1186 dwc->hird_threshold = hird_threshold
1187 | (dwc->is_utmi_l1_suspend << 4);
1189 dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd;
1190 dwc->rx_max_burst_prd = rx_max_burst_prd;
1192 dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd;
1193 dwc->tx_max_burst_prd = tx_max_burst_prd;
1195 dwc->imod_interval = 0;
1198 /* check whether the core supports IMOD */
1199 bool dwc3_has_imod(struct dwc3 *dwc)
1201 return ((dwc3_is_usb3(dwc) &&
1202 dwc->revision >= DWC3_REVISION_300A) ||
1203 (dwc3_is_usb31(dwc) &&
1204 dwc->revision >= DWC3_USB31_REVISION_120A));
1207 static void dwc3_check_params(struct dwc3 *dwc)
1209 struct device *dev = dwc->dev;
1211 /* Check for proper value of imod_interval */
1212 if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
1213 dev_warn(dwc->dev, "Interrupt moderation not supported\n");
1214 dwc->imod_interval = 0;
1218 * Workaround for STAR 9000961433 which affects only version
1219 * 3.00a of the DWC_usb3 core. This prevents the controller
1220 * interrupt from being masked while handling events. IMOD
1221 * allows us to work around this issue. Enable it for the
1224 if (!dwc->imod_interval &&
1225 (dwc->revision == DWC3_REVISION_300A))
1226 dwc->imod_interval = 1;
1228 /* Check the maximum_speed parameter */
1229 switch (dwc->maximum_speed) {
1231 case USB_SPEED_FULL:
1232 case USB_SPEED_HIGH:
1233 case USB_SPEED_SUPER:
1234 case USB_SPEED_SUPER_PLUS:
1237 dev_err(dev, "invalid maximum_speed parameter %d\n",
1238 dwc->maximum_speed);
1240 case USB_SPEED_UNKNOWN:
1241 /* default to superspeed */
1242 dwc->maximum_speed = USB_SPEED_SUPER;
1245 * default to superspeed plus if we are capable.
1247 if (dwc3_is_usb31(dwc) &&
1248 (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
1249 DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1250 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1256 static int dwc3_probe(struct platform_device *pdev)
1258 struct device *dev = &pdev->dev;
1259 struct resource *res, dwc_res;
1266 dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
1270 dwc->clks = devm_kmemdup(dev, dwc3_core_clks, sizeof(dwc3_core_clks),
1277 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1279 dev_err(dev, "missing memory resource\n");
1283 dwc->xhci_resources[0].start = res->start;
1284 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1286 dwc->xhci_resources[0].flags = res->flags;
1287 dwc->xhci_resources[0].name = res->name;
1290 * Request memory region but exclude xHCI regs,
1291 * since it will be requested by the xhci-plat driver.
1294 dwc_res.start += DWC3_GLOBALS_REGS_START;
1296 regs = devm_ioremap_resource(dev, &dwc_res);
1298 return PTR_ERR(regs);
1301 dwc->regs_size = resource_size(&dwc_res);
1303 dwc3_get_properties(dwc);
1305 dwc->reset = devm_reset_control_get_optional_shared(dev, NULL);
1306 if (IS_ERR(dwc->reset))
1307 return PTR_ERR(dwc->reset);
1310 dwc->num_clks = ARRAY_SIZE(dwc3_core_clks);
1312 ret = clk_bulk_get(dev, dwc->num_clks, dwc->clks);
1313 if (ret == -EPROBE_DEFER)
1316 * Clocks are optional, but new DT platforms should support all
1317 * clocks as required by the DT-binding.
1323 ret = reset_control_deassert(dwc->reset);
1327 ret = clk_bulk_prepare(dwc->num_clks, dwc->clks);
1331 ret = clk_bulk_enable(dwc->num_clks, dwc->clks);
1333 goto unprepare_clks;
1335 platform_set_drvdata(pdev, dwc);
1336 dwc3_cache_hwparams(dwc);
1338 spin_lock_init(&dwc->lock);
1340 pm_runtime_set_active(dev);
1341 pm_runtime_use_autosuspend(dev);
1342 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
1343 pm_runtime_enable(dev);
1344 ret = pm_runtime_get_sync(dev);
1348 pm_runtime_forbid(dev);
1350 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1352 dev_err(dwc->dev, "failed to allocate event buffers\n");
1357 ret = dwc3_get_dr_mode(dwc);
1361 ret = dwc3_alloc_scratch_buffers(dwc);
1365 ret = dwc3_core_init(dwc);
1367 dev_err(dev, "failed to initialize core\n");
1371 dwc3_check_params(dwc);
1373 ret = dwc3_core_init_mode(dwc);
1377 dwc3_debugfs_init(dwc);
1378 pm_runtime_put(dev);
1383 dwc3_event_buffers_cleanup(dwc);
1386 dwc3_free_scratch_buffers(dwc);
1389 dwc3_free_event_buffers(dwc);
1392 pm_runtime_allow(&pdev->dev);
1395 pm_runtime_put_sync(&pdev->dev);
1396 pm_runtime_disable(&pdev->dev);
1398 clk_bulk_disable(dwc->num_clks, dwc->clks);
1400 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
1402 reset_control_assert(dwc->reset);
1404 clk_bulk_put(dwc->num_clks, dwc->clks);
1409 static int dwc3_remove(struct platform_device *pdev)
1411 struct dwc3 *dwc = platform_get_drvdata(pdev);
1413 pm_runtime_get_sync(&pdev->dev);
1415 dwc3_debugfs_exit(dwc);
1416 dwc3_core_exit_mode(dwc);
1418 dwc3_core_exit(dwc);
1419 dwc3_ulpi_exit(dwc);
1421 pm_runtime_put_sync(&pdev->dev);
1422 pm_runtime_allow(&pdev->dev);
1423 pm_runtime_disable(&pdev->dev);
1425 dwc3_free_event_buffers(dwc);
1426 dwc3_free_scratch_buffers(dwc);
1427 clk_bulk_put(dwc->num_clks, dwc->clks);
1433 static int dwc3_core_init_for_resume(struct dwc3 *dwc)
1437 ret = reset_control_deassert(dwc->reset);
1441 ret = clk_bulk_prepare(dwc->num_clks, dwc->clks);
1445 ret = clk_bulk_enable(dwc->num_clks, dwc->clks);
1447 goto unprepare_clks;
1449 ret = dwc3_core_init(dwc);
1456 clk_bulk_disable(dwc->num_clks, dwc->clks);
1458 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
1460 reset_control_assert(dwc->reset);
1465 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
1467 unsigned long flags;
1470 switch (dwc->current_dr_role) {
1471 case DWC3_GCTL_PRTCAP_DEVICE:
1472 spin_lock_irqsave(&dwc->lock, flags);
1473 dwc3_gadget_suspend(dwc);
1474 spin_unlock_irqrestore(&dwc->lock, flags);
1475 dwc3_core_exit(dwc);
1477 case DWC3_GCTL_PRTCAP_HOST:
1478 if (!PMSG_IS_AUTO(msg)) {
1479 dwc3_core_exit(dwc);
1483 /* Let controller to suspend HSPHY before PHY driver suspends */
1484 if (dwc->dis_u2_susphy_quirk ||
1485 dwc->dis_enblslpm_quirk) {
1486 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1487 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
1488 DWC3_GUSB2PHYCFG_SUSPHY;
1489 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1491 /* Give some time for USB2 PHY to suspend */
1492 usleep_range(5000, 6000);
1495 phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
1496 phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
1498 case DWC3_GCTL_PRTCAP_OTG:
1499 /* do nothing during runtime_suspend */
1500 if (PMSG_IS_AUTO(msg))
1503 if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1504 spin_lock_irqsave(&dwc->lock, flags);
1505 dwc3_gadget_suspend(dwc);
1506 spin_unlock_irqrestore(&dwc->lock, flags);
1510 dwc3_core_exit(dwc);
1520 static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
1522 unsigned long flags;
1526 switch (dwc->current_dr_role) {
1527 case DWC3_GCTL_PRTCAP_DEVICE:
1528 ret = dwc3_core_init_for_resume(dwc);
1532 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1533 spin_lock_irqsave(&dwc->lock, flags);
1534 dwc3_gadget_resume(dwc);
1535 spin_unlock_irqrestore(&dwc->lock, flags);
1537 case DWC3_GCTL_PRTCAP_HOST:
1538 if (!PMSG_IS_AUTO(msg)) {
1539 ret = dwc3_core_init_for_resume(dwc);
1542 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1545 /* Restore GUSB2PHYCFG bits that were modified in suspend */
1546 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1547 if (dwc->dis_u2_susphy_quirk)
1548 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1550 if (dwc->dis_enblslpm_quirk)
1551 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
1553 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1555 phy_pm_runtime_get_sync(dwc->usb2_generic_phy);
1556 phy_pm_runtime_get_sync(dwc->usb3_generic_phy);
1558 case DWC3_GCTL_PRTCAP_OTG:
1559 /* nothing to do on runtime_resume */
1560 if (PMSG_IS_AUTO(msg))
1563 ret = dwc3_core_init(dwc);
1567 dwc3_set_prtcap(dwc, dwc->current_dr_role);
1570 if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
1571 dwc3_otg_host_init(dwc);
1572 } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1573 spin_lock_irqsave(&dwc->lock, flags);
1574 dwc3_gadget_resume(dwc);
1575 spin_unlock_irqrestore(&dwc->lock, flags);
1587 static int dwc3_runtime_checks(struct dwc3 *dwc)
1589 switch (dwc->current_dr_role) {
1590 case DWC3_GCTL_PRTCAP_DEVICE:
1594 case DWC3_GCTL_PRTCAP_HOST:
1603 static int dwc3_runtime_suspend(struct device *dev)
1605 struct dwc3 *dwc = dev_get_drvdata(dev);
1608 if (dwc3_runtime_checks(dwc))
1611 ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
1615 device_init_wakeup(dev, true);
1620 static int dwc3_runtime_resume(struct device *dev)
1622 struct dwc3 *dwc = dev_get_drvdata(dev);
1625 device_init_wakeup(dev, false);
1627 ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
1631 switch (dwc->current_dr_role) {
1632 case DWC3_GCTL_PRTCAP_DEVICE:
1633 dwc3_gadget_process_pending_events(dwc);
1635 case DWC3_GCTL_PRTCAP_HOST:
1641 pm_runtime_mark_last_busy(dev);
1646 static int dwc3_runtime_idle(struct device *dev)
1648 struct dwc3 *dwc = dev_get_drvdata(dev);
1650 switch (dwc->current_dr_role) {
1651 case DWC3_GCTL_PRTCAP_DEVICE:
1652 if (dwc3_runtime_checks(dwc))
1655 case DWC3_GCTL_PRTCAP_HOST:
1661 pm_runtime_mark_last_busy(dev);
1662 pm_runtime_autosuspend(dev);
1666 #endif /* CONFIG_PM */
1668 #ifdef CONFIG_PM_SLEEP
1669 static int dwc3_suspend(struct device *dev)
1671 struct dwc3 *dwc = dev_get_drvdata(dev);
1674 ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
1678 pinctrl_pm_select_sleep_state(dev);
1683 static int dwc3_resume(struct device *dev)
1685 struct dwc3 *dwc = dev_get_drvdata(dev);
1688 pinctrl_pm_select_default_state(dev);
1690 ret = dwc3_resume_common(dwc, PMSG_RESUME);
1694 pm_runtime_disable(dev);
1695 pm_runtime_set_active(dev);
1696 pm_runtime_enable(dev);
1700 #endif /* CONFIG_PM_SLEEP */
1702 static const struct dev_pm_ops dwc3_dev_pm_ops = {
1703 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
1704 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
1709 static const struct of_device_id of_dwc3_match[] = {
1711 .compatible = "snps,dwc3"
1714 .compatible = "synopsys,dwc3"
1718 MODULE_DEVICE_TABLE(of, of_dwc3_match);
1723 #define ACPI_ID_INTEL_BSW "808622B7"
1725 static const struct acpi_device_id dwc3_acpi_match[] = {
1726 { ACPI_ID_INTEL_BSW, 0 },
1729 MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
1732 static struct platform_driver dwc3_driver = {
1733 .probe = dwc3_probe,
1734 .remove = dwc3_remove,
1737 .of_match_table = of_match_ptr(of_dwc3_match),
1738 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
1739 .pm = &dwc3_dev_pm_ops,
1743 module_platform_driver(dwc3_driver);
1745 MODULE_ALIAS("platform:dwc3");
1746 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1747 MODULE_LICENSE("GPL v2");
1748 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");