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
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 * dwc_usb31 does not support OTG mode. If the controller
84 * supports DRD but the dr_mode is not specified or set to OTG,
85 * then set the mode to peripheral.
87 if (mode == USB_DR_MODE_OTG && dwc3_is_usb31(dwc))
88 mode = USB_DR_MODE_PERIPHERAL;
91 if (mode != dwc->dr_mode) {
93 "Configuration mismatch. dr_mode forced to %s\n",
94 mode == USB_DR_MODE_HOST ? "host" : "gadget");
102 void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
106 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
107 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
108 reg |= DWC3_GCTL_PRTCAPDIR(mode);
109 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
111 dwc->current_dr_role = mode;
114 static void __dwc3_set_mode(struct work_struct *work)
116 struct dwc3 *dwc = work_to_dwc(work);
120 if (dwc->dr_mode != USB_DR_MODE_OTG)
123 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
124 dwc3_otg_update(dwc, 0);
126 if (!dwc->desired_dr_role)
129 if (dwc->desired_dr_role == dwc->current_dr_role)
132 if (dwc->desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev)
135 switch (dwc->current_dr_role) {
136 case DWC3_GCTL_PRTCAP_HOST:
139 case DWC3_GCTL_PRTCAP_DEVICE:
140 dwc3_gadget_exit(dwc);
141 dwc3_event_buffers_cleanup(dwc);
143 case DWC3_GCTL_PRTCAP_OTG:
145 spin_lock_irqsave(&dwc->lock, flags);
146 dwc->desired_otg_role = DWC3_OTG_ROLE_IDLE;
147 spin_unlock_irqrestore(&dwc->lock, flags);
148 dwc3_otg_update(dwc, 1);
154 spin_lock_irqsave(&dwc->lock, flags);
156 dwc3_set_prtcap(dwc, dwc->desired_dr_role);
158 spin_unlock_irqrestore(&dwc->lock, flags);
160 switch (dwc->desired_dr_role) {
161 case DWC3_GCTL_PRTCAP_HOST:
162 ret = dwc3_host_init(dwc);
164 dev_err(dwc->dev, "failed to initialize host\n");
167 otg_set_vbus(dwc->usb2_phy->otg, true);
168 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
169 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
170 phy_calibrate(dwc->usb2_generic_phy);
173 case DWC3_GCTL_PRTCAP_DEVICE:
174 dwc3_event_buffers_setup(dwc);
177 otg_set_vbus(dwc->usb2_phy->otg, false);
178 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
179 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
181 ret = dwc3_gadget_init(dwc);
183 dev_err(dwc->dev, "failed to initialize peripheral\n");
185 case DWC3_GCTL_PRTCAP_OTG:
187 dwc3_otg_update(dwc, 0);
195 void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
199 spin_lock_irqsave(&dwc->lock, flags);
200 dwc->desired_dr_role = mode;
201 spin_unlock_irqrestore(&dwc->lock, flags);
203 queue_work(system_freezable_wq, &dwc->drd_work);
206 u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
208 struct dwc3 *dwc = dep->dwc;
211 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
212 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
213 DWC3_GDBGFIFOSPACE_TYPE(type));
215 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
217 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
221 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
222 * @dwc: pointer to our context structure
224 static int dwc3_core_soft_reset(struct dwc3 *dwc)
230 usb_phy_init(dwc->usb2_phy);
231 usb_phy_init(dwc->usb3_phy);
232 ret = phy_init(dwc->usb2_generic_phy);
236 ret = phy_init(dwc->usb3_generic_phy);
238 phy_exit(dwc->usb2_generic_phy);
243 * We're resetting only the device side because, if we're in host mode,
244 * XHCI driver will reset the host block. If dwc3 was configured for
245 * host-only mode, then we can return early.
247 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST)
250 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
251 reg |= DWC3_DCTL_CSFTRST;
252 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
255 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
256 if (!(reg & DWC3_DCTL_CSFTRST))
262 phy_exit(dwc->usb3_generic_phy);
263 phy_exit(dwc->usb2_generic_phy);
269 * For DWC_usb31 controller, once DWC3_DCTL_CSFTRST bit is cleared,
270 * we must wait at least 50ms before accessing the PHY domain
271 * (synchronization delay). DWC_usb31 programming guide section 1.3.2.
273 if (dwc3_is_usb31(dwc))
279 static const struct clk_bulk_data dwc3_core_clks[] = {
281 { .id = "bus_early" },
286 * dwc3_frame_length_adjustment - Adjusts frame length if required
287 * @dwc3: Pointer to our controller context structure
289 static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
294 if (dwc->revision < DWC3_REVISION_250A)
300 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
301 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
302 if (!dev_WARN_ONCE(dwc->dev, dft == dwc->fladj,
303 "request value same as default, ignoring\n")) {
304 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
305 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
306 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
311 * dwc3_free_one_event_buffer - Frees one event buffer
312 * @dwc: Pointer to our controller context structure
313 * @evt: Pointer to event buffer to be freed
315 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
316 struct dwc3_event_buffer *evt)
318 dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
322 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
323 * @dwc: Pointer to our controller context structure
324 * @length: size of the event buffer
326 * Returns a pointer to the allocated event buffer structure on success
327 * otherwise ERR_PTR(errno).
329 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
332 struct dwc3_event_buffer *evt;
334 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
336 return ERR_PTR(-ENOMEM);
339 evt->length = length;
340 evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
342 return ERR_PTR(-ENOMEM);
344 evt->buf = dma_alloc_coherent(dwc->sysdev, length,
345 &evt->dma, GFP_KERNEL);
347 return ERR_PTR(-ENOMEM);
353 * dwc3_free_event_buffers - frees all allocated event buffers
354 * @dwc: Pointer to our controller context structure
356 static void dwc3_free_event_buffers(struct dwc3 *dwc)
358 struct dwc3_event_buffer *evt;
362 dwc3_free_one_event_buffer(dwc, evt);
366 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
367 * @dwc: pointer to our controller context structure
368 * @length: size of event buffer
370 * Returns 0 on success otherwise negative errno. In the error case, dwc
371 * may contain some buffers allocated but not all which were requested.
373 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
375 struct dwc3_event_buffer *evt;
377 evt = dwc3_alloc_one_event_buffer(dwc, length);
379 dev_err(dwc->dev, "can't allocate event buffer\n");
388 * dwc3_event_buffers_setup - setup our allocated event buffers
389 * @dwc: pointer to our controller context structure
391 * Returns 0 on success otherwise negative errno.
393 int dwc3_event_buffers_setup(struct dwc3 *dwc)
395 struct dwc3_event_buffer *evt;
399 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
400 lower_32_bits(evt->dma));
401 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
402 upper_32_bits(evt->dma));
403 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
404 DWC3_GEVNTSIZ_SIZE(evt->length));
405 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
410 void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
412 struct dwc3_event_buffer *evt;
418 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
419 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
420 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
421 | DWC3_GEVNTSIZ_SIZE(0));
422 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
425 static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
427 if (!dwc->has_hibernation)
430 if (!dwc->nr_scratch)
433 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
434 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
435 if (!dwc->scratchbuf)
441 static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
443 dma_addr_t scratch_addr;
447 if (!dwc->has_hibernation)
450 if (!dwc->nr_scratch)
453 /* should never fall here */
454 if (!WARN_ON(dwc->scratchbuf))
457 scratch_addr = dma_map_single(dwc->sysdev, dwc->scratchbuf,
458 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
460 if (dma_mapping_error(dwc->sysdev, scratch_addr)) {
461 dev_err(dwc->sysdev, "failed to map scratch buffer\n");
466 dwc->scratch_addr = scratch_addr;
468 param = lower_32_bits(scratch_addr);
470 ret = dwc3_send_gadget_generic_command(dwc,
471 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
475 param = upper_32_bits(scratch_addr);
477 ret = dwc3_send_gadget_generic_command(dwc,
478 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
485 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
486 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
492 static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
494 if (!dwc->has_hibernation)
497 if (!dwc->nr_scratch)
500 /* should never fall here */
501 if (!WARN_ON(dwc->scratchbuf))
504 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
505 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
506 kfree(dwc->scratchbuf);
509 static void dwc3_core_num_eps(struct dwc3 *dwc)
511 struct dwc3_hwparams *parms = &dwc->hwparams;
513 dwc->num_eps = DWC3_NUM_EPS(parms);
516 static void dwc3_cache_hwparams(struct dwc3 *dwc)
518 struct dwc3_hwparams *parms = &dwc->hwparams;
520 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
521 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
522 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
523 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
524 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
525 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
526 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
527 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
528 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
531 static int dwc3_core_ulpi_init(struct dwc3 *dwc)
536 intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
538 if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
539 (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
540 dwc->hsphy_interface &&
541 !strncmp(dwc->hsphy_interface, "ulpi", 4)))
542 ret = dwc3_ulpi_init(dwc);
548 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
549 * @dwc: Pointer to our controller context structure
551 * Returns 0 on success. The USB PHY interfaces are configured but not
552 * initialized. The PHY interfaces and the PHYs get initialized together with
553 * the core in dwc3_core_init.
555 static int dwc3_phy_setup(struct dwc3 *dwc)
559 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
562 * Make sure UX_EXIT_PX is cleared as that causes issues with some
563 * PHYs. Also, this bit is not supposed to be used in normal operation.
565 reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
568 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
569 * to '0' during coreConsultant configuration. So default value
570 * will be '0' when the core is reset. Application needs to set it
571 * to '1' after the core initialization is completed.
573 if (dwc->revision > DWC3_REVISION_194A)
574 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
576 if (dwc->u2ss_inp3_quirk)
577 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
579 if (dwc->dis_rxdet_inp3_quirk)
580 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
582 if (dwc->req_p1p2p3_quirk)
583 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
585 if (dwc->del_p1p2p3_quirk)
586 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
588 if (dwc->del_phy_power_chg_quirk)
589 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
591 if (dwc->lfps_filter_quirk)
592 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
594 if (dwc->rx_detect_poll_quirk)
595 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
597 if (dwc->tx_de_emphasis_quirk)
598 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
600 if (dwc->dis_u3_susphy_quirk)
601 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
603 if (dwc->dis_del_phy_power_chg_quirk)
604 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
606 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
608 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
610 /* Select the HS PHY interface */
611 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
612 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
613 if (dwc->hsphy_interface &&
614 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
615 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
617 } else if (dwc->hsphy_interface &&
618 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
619 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
620 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
622 /* Relying on default value. */
623 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
627 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
633 switch (dwc->hsphy_mode) {
634 case USBPHY_INTERFACE_MODE_UTMI:
635 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
636 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
637 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
638 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
640 case USBPHY_INTERFACE_MODE_UTMIW:
641 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
642 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
643 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
644 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
651 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
652 * '0' during coreConsultant configuration. So default value will
653 * be '0' when the core is reset. Application needs to set it to
654 * '1' after the core initialization is completed.
656 if (dwc->revision > DWC3_REVISION_194A)
657 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
659 if (dwc->dis_u2_susphy_quirk)
660 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
662 if (dwc->dis_enblslpm_quirk)
663 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
665 if (dwc->dis_u2_freeclk_exists_quirk)
666 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
668 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
673 static void dwc3_core_exit(struct dwc3 *dwc)
675 dwc3_event_buffers_cleanup(dwc);
677 usb_phy_shutdown(dwc->usb2_phy);
678 usb_phy_shutdown(dwc->usb3_phy);
679 phy_exit(dwc->usb2_generic_phy);
680 phy_exit(dwc->usb3_generic_phy);
682 usb_phy_set_suspend(dwc->usb2_phy, 1);
683 usb_phy_set_suspend(dwc->usb3_phy, 1);
684 phy_power_off(dwc->usb2_generic_phy);
685 phy_power_off(dwc->usb3_generic_phy);
686 clk_bulk_disable(dwc->num_clks, dwc->clks);
687 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
688 reset_control_assert(dwc->reset);
691 static bool dwc3_core_is_valid(struct dwc3 *dwc)
695 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
697 /* This should read as U3 followed by revision number */
698 if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
699 /* Detected DWC_usb3 IP */
701 } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
702 /* Detected DWC_usb31 IP */
703 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
704 dwc->revision |= DWC3_REVISION_IS_DWC31;
712 static void dwc3_core_setup_global_control(struct dwc3 *dwc)
714 u32 hwparams4 = dwc->hwparams.hwparams4;
717 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
718 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
720 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
721 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
723 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
724 * issue which would cause xHCI compliance tests to fail.
726 * Because of that we cannot enable clock gating on such
731 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
734 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
735 dwc->dr_mode == USB_DR_MODE_OTG) &&
736 (dwc->revision >= DWC3_REVISION_210A &&
737 dwc->revision <= DWC3_REVISION_250A))
738 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
740 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
742 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
743 /* enable hibernation here */
744 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
747 * REVISIT Enabling this bit so that host-mode hibernation
748 * will work. Device-mode hibernation is not yet implemented.
750 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
757 /* check if current dwc3 is on simulation board */
758 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
759 dev_info(dwc->dev, "Running with FPGA optimizations\n");
763 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
764 "disable_scramble cannot be used on non-FPGA builds\n");
766 if (dwc->disable_scramble_quirk && dwc->is_fpga)
767 reg |= DWC3_GCTL_DISSCRAMBLE;
769 reg &= ~DWC3_GCTL_DISSCRAMBLE;
771 if (dwc->u2exit_lfps_quirk)
772 reg |= DWC3_GCTL_U2EXIT_LFPS;
775 * WORKAROUND: DWC3 revisions <1.90a have a bug
776 * where the device can fail to connect at SuperSpeed
777 * and falls back to high-speed mode which causes
778 * the device to enter a Connect/Disconnect loop
780 if (dwc->revision < DWC3_REVISION_190A)
781 reg |= DWC3_GCTL_U2RSTECN;
783 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
786 static int dwc3_core_get_phy(struct dwc3 *dwc);
787 static int dwc3_core_ulpi_init(struct dwc3 *dwc);
789 /* set global incr burst type configuration registers */
790 static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
792 struct device *dev = dwc->dev;
793 /* incrx_mode : for INCR burst type. */
795 /* incrx_size : for size of INCRX burst. */
803 cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
806 * Handle property "snps,incr-burst-type-adjustment".
807 * Get the number of value from this property:
808 * result <= 0, means this property is not supported.
809 * result = 1, means INCRx burst mode supported.
810 * result > 1, means undefined length burst mode supported.
812 ntype = device_property_read_u32_array(dev,
813 "snps,incr-burst-type-adjustment", NULL, 0);
817 vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL);
819 dev_err(dev, "Error to get memory\n");
823 /* Get INCR burst type, and parse it */
824 ret = device_property_read_u32_array(dev,
825 "snps,incr-burst-type-adjustment", vals, ntype);
827 dev_err(dev, "Error to get property\n");
834 /* INCRX (undefined length) burst mode */
835 incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
836 for (i = 1; i < ntype; i++) {
837 if (vals[i] > incrx_size)
838 incrx_size = vals[i];
841 /* INCRX burst mode */
842 incrx_mode = INCRX_BURST_MODE;
845 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
846 cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
848 cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
849 switch (incrx_size) {
851 cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
854 cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
857 cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
860 cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
863 cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
866 cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
869 cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
874 dev_err(dev, "Invalid property\n");
878 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
882 * dwc3_core_init - Low-level initialization of DWC3 Core
883 * @dwc: Pointer to our controller context structure
885 * Returns 0 on success otherwise negative errno.
887 static int dwc3_core_init(struct dwc3 *dwc)
892 if (!dwc3_core_is_valid(dwc)) {
893 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
899 * Write Linux Version Code to our GUID register so it's easy to figure
900 * out which kernel version a bug was found.
902 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
904 /* Handle USB2.0-only core configuration */
905 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
906 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
907 if (dwc->maximum_speed == USB_SPEED_SUPER)
908 dwc->maximum_speed = USB_SPEED_HIGH;
911 ret = dwc3_phy_setup(dwc);
915 if (!dwc->ulpi_ready) {
916 ret = dwc3_core_ulpi_init(dwc);
919 dwc->ulpi_ready = true;
922 if (!dwc->phys_ready) {
923 ret = dwc3_core_get_phy(dwc);
926 dwc->phys_ready = true;
929 ret = dwc3_core_soft_reset(dwc);
933 dwc3_core_setup_global_control(dwc);
934 dwc3_core_num_eps(dwc);
936 ret = dwc3_setup_scratch_buffers(dwc);
940 /* Adjust Frame Length */
941 dwc3_frame_length_adjustment(dwc);
943 dwc3_set_incr_burst_type(dwc);
945 usb_phy_set_suspend(dwc->usb2_phy, 0);
946 usb_phy_set_suspend(dwc->usb3_phy, 0);
947 ret = phy_power_on(dwc->usb2_generic_phy);
951 ret = phy_power_on(dwc->usb3_generic_phy);
955 ret = dwc3_event_buffers_setup(dwc);
957 dev_err(dwc->dev, "failed to setup event buffers\n");
962 * ENDXFER polling is available on version 3.10a and later of
963 * the DWC_usb3 controller. It is NOT available in the
964 * DWC_usb31 controller.
966 if (!dwc3_is_usb31(dwc) && dwc->revision >= DWC3_REVISION_310A) {
967 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
968 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
969 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
972 if (dwc->revision >= DWC3_REVISION_250A) {
973 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
976 * Enable hardware control of sending remote wakeup
977 * in HS when the device is in the L1 state.
979 if (dwc->revision >= DWC3_REVISION_290A)
980 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
982 if (dwc->dis_tx_ipgap_linecheck_quirk)
983 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
985 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
988 if (dwc->dr_mode == USB_DR_MODE_HOST ||
989 dwc->dr_mode == USB_DR_MODE_OTG) {
990 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
993 * Enable Auto retry Feature to make the controller operating in
994 * Host mode on seeing transaction errors(CRC errors or internal
995 * overrun scenerios) on IN transfers to reply to the device
996 * with a non-terminating retry ACK (i.e, an ACK transcation
997 * packet with Retry=1 & Nump != 0)
999 reg |= DWC3_GUCTL_HSTINAUTORETRY;
1001 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
1005 * Must config both number of packets and max burst settings to enable
1006 * RX and/or TX threshold.
1008 if (dwc3_is_usb31(dwc) && dwc->dr_mode == USB_DR_MODE_HOST) {
1009 u8 rx_thr_num = dwc->rx_thr_num_pkt_prd;
1010 u8 rx_maxburst = dwc->rx_max_burst_prd;
1011 u8 tx_thr_num = dwc->tx_thr_num_pkt_prd;
1012 u8 tx_maxburst = dwc->tx_max_burst_prd;
1014 if (rx_thr_num && rx_maxburst) {
1015 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1016 reg |= DWC31_RXTHRNUMPKTSEL_PRD;
1018 reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
1019 reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
1021 reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
1022 reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
1024 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1027 if (tx_thr_num && tx_maxburst) {
1028 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1029 reg |= DWC31_TXTHRNUMPKTSEL_PRD;
1031 reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
1032 reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
1034 reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
1035 reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
1037 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1044 phy_power_off(dwc->usb3_generic_phy);
1047 phy_power_off(dwc->usb2_generic_phy);
1050 usb_phy_set_suspend(dwc->usb2_phy, 1);
1051 usb_phy_set_suspend(dwc->usb3_phy, 1);
1054 usb_phy_shutdown(dwc->usb2_phy);
1055 usb_phy_shutdown(dwc->usb3_phy);
1056 phy_exit(dwc->usb2_generic_phy);
1057 phy_exit(dwc->usb3_generic_phy);
1060 dwc3_ulpi_exit(dwc);
1066 static int dwc3_core_get_phy(struct dwc3 *dwc)
1068 struct device *dev = dwc->dev;
1069 struct device_node *node = dev->of_node;
1073 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
1074 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
1076 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1077 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
1080 if (IS_ERR(dwc->usb2_phy)) {
1081 ret = PTR_ERR(dwc->usb2_phy);
1082 if (ret == -ENXIO || ret == -ENODEV) {
1083 dwc->usb2_phy = NULL;
1084 } else if (ret == -EPROBE_DEFER) {
1087 dev_err(dev, "no usb2 phy configured\n");
1092 if (IS_ERR(dwc->usb3_phy)) {
1093 ret = PTR_ERR(dwc->usb3_phy);
1094 if (ret == -ENXIO || ret == -ENODEV) {
1095 dwc->usb3_phy = NULL;
1096 } else if (ret == -EPROBE_DEFER) {
1099 dev_err(dev, "no usb3 phy configured\n");
1104 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
1105 if (IS_ERR(dwc->usb2_generic_phy)) {
1106 ret = PTR_ERR(dwc->usb2_generic_phy);
1107 if (ret == -ENOSYS || ret == -ENODEV) {
1108 dwc->usb2_generic_phy = NULL;
1109 } else if (ret == -EPROBE_DEFER) {
1112 dev_err(dev, "no usb2 phy configured\n");
1117 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
1118 if (IS_ERR(dwc->usb3_generic_phy)) {
1119 ret = PTR_ERR(dwc->usb3_generic_phy);
1120 if (ret == -ENOSYS || ret == -ENODEV) {
1121 dwc->usb3_generic_phy = NULL;
1122 } else if (ret == -EPROBE_DEFER) {
1125 dev_err(dev, "no usb3 phy configured\n");
1133 static int dwc3_core_init_mode(struct dwc3 *dwc)
1135 struct device *dev = dwc->dev;
1138 switch (dwc->dr_mode) {
1139 case USB_DR_MODE_PERIPHERAL:
1140 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1143 otg_set_vbus(dwc->usb2_phy->otg, false);
1144 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
1145 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
1147 ret = dwc3_gadget_init(dwc);
1149 if (ret != -EPROBE_DEFER)
1150 dev_err(dev, "failed to initialize gadget\n");
1154 case USB_DR_MODE_HOST:
1155 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1158 otg_set_vbus(dwc->usb2_phy->otg, true);
1159 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
1160 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
1162 ret = dwc3_host_init(dwc);
1164 if (ret != -EPROBE_DEFER)
1165 dev_err(dev, "failed to initialize host\n");
1168 phy_calibrate(dwc->usb2_generic_phy);
1170 case USB_DR_MODE_OTG:
1171 INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
1172 ret = dwc3_drd_init(dwc);
1174 if (ret != -EPROBE_DEFER)
1175 dev_err(dev, "failed to initialize dual-role\n");
1180 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1187 static void dwc3_core_exit_mode(struct dwc3 *dwc)
1189 switch (dwc->dr_mode) {
1190 case USB_DR_MODE_PERIPHERAL:
1191 dwc3_gadget_exit(dwc);
1193 case USB_DR_MODE_HOST:
1194 dwc3_host_exit(dwc);
1196 case USB_DR_MODE_OTG:
1205 static void dwc3_get_properties(struct dwc3 *dwc)
1207 struct device *dev = dwc->dev;
1208 u8 lpm_nyet_threshold;
1211 u8 rx_thr_num_pkt_prd;
1212 u8 rx_max_burst_prd;
1213 u8 tx_thr_num_pkt_prd;
1214 u8 tx_max_burst_prd;
1216 /* default to highest possible threshold */
1217 lpm_nyet_threshold = 0xff;
1219 /* default to -3.5dB de-emphasis */
1223 * default to assert utmi_sleep_n and use maximum allowed HIRD
1224 * threshold value of 0b1100
1226 hird_threshold = 12;
1228 dwc->maximum_speed = usb_get_maximum_speed(dev);
1229 dwc->dr_mode = usb_get_dr_mode(dev);
1230 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
1232 dwc->sysdev_is_parent = device_property_read_bool(dev,
1233 "linux,sysdev_is_parent");
1234 if (dwc->sysdev_is_parent)
1235 dwc->sysdev = dwc->dev->parent;
1237 dwc->sysdev = dwc->dev;
1239 dwc->has_lpm_erratum = device_property_read_bool(dev,
1240 "snps,has-lpm-erratum");
1241 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
1242 &lpm_nyet_threshold);
1243 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
1244 "snps,is-utmi-l1-suspend");
1245 device_property_read_u8(dev, "snps,hird-threshold",
1247 dwc->usb3_lpm_capable = device_property_read_bool(dev,
1248 "snps,usb3_lpm_capable");
1249 device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
1250 &rx_thr_num_pkt_prd);
1251 device_property_read_u8(dev, "snps,rx-max-burst-prd",
1253 device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
1254 &tx_thr_num_pkt_prd);
1255 device_property_read_u8(dev, "snps,tx-max-burst-prd",
1258 dwc->disable_scramble_quirk = device_property_read_bool(dev,
1259 "snps,disable_scramble_quirk");
1260 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
1261 "snps,u2exit_lfps_quirk");
1262 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
1263 "snps,u2ss_inp3_quirk");
1264 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
1265 "snps,req_p1p2p3_quirk");
1266 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
1267 "snps,del_p1p2p3_quirk");
1268 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
1269 "snps,del_phy_power_chg_quirk");
1270 dwc->lfps_filter_quirk = device_property_read_bool(dev,
1271 "snps,lfps_filter_quirk");
1272 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
1273 "snps,rx_detect_poll_quirk");
1274 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
1275 "snps,dis_u3_susphy_quirk");
1276 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
1277 "snps,dis_u2_susphy_quirk");
1278 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1279 "snps,dis_enblslpm_quirk");
1280 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1281 "snps,dis_rxdet_inp3_quirk");
1282 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1283 "snps,dis-u2-freeclk-exists-quirk");
1284 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1285 "snps,dis-del-phy-power-chg-quirk");
1286 dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1287 "snps,dis-tx-ipgap-linecheck-quirk");
1289 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
1290 "snps,tx_de_emphasis_quirk");
1291 device_property_read_u8(dev, "snps,tx_de_emphasis",
1293 device_property_read_string(dev, "snps,hsphy_interface",
1294 &dwc->hsphy_interface);
1295 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
1298 dwc->dis_metastability_quirk = device_property_read_bool(dev,
1299 "snps,dis_metastability_quirk");
1301 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1302 dwc->tx_de_emphasis = tx_de_emphasis;
1304 dwc->hird_threshold = hird_threshold
1305 | (dwc->is_utmi_l1_suspend << 4);
1307 dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd;
1308 dwc->rx_max_burst_prd = rx_max_burst_prd;
1310 dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd;
1311 dwc->tx_max_burst_prd = tx_max_burst_prd;
1313 dwc->imod_interval = 0;
1316 /* check whether the core supports IMOD */
1317 bool dwc3_has_imod(struct dwc3 *dwc)
1319 return ((dwc3_is_usb3(dwc) &&
1320 dwc->revision >= DWC3_REVISION_300A) ||
1321 (dwc3_is_usb31(dwc) &&
1322 dwc->revision >= DWC3_USB31_REVISION_120A));
1325 static void dwc3_check_params(struct dwc3 *dwc)
1327 struct device *dev = dwc->dev;
1329 /* Check for proper value of imod_interval */
1330 if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
1331 dev_warn(dwc->dev, "Interrupt moderation not supported\n");
1332 dwc->imod_interval = 0;
1336 * Workaround for STAR 9000961433 which affects only version
1337 * 3.00a of the DWC_usb3 core. This prevents the controller
1338 * interrupt from being masked while handling events. IMOD
1339 * allows us to work around this issue. Enable it for the
1342 if (!dwc->imod_interval &&
1343 (dwc->revision == DWC3_REVISION_300A))
1344 dwc->imod_interval = 1;
1346 /* Check the maximum_speed parameter */
1347 switch (dwc->maximum_speed) {
1349 case USB_SPEED_FULL:
1350 case USB_SPEED_HIGH:
1351 case USB_SPEED_SUPER:
1352 case USB_SPEED_SUPER_PLUS:
1355 dev_err(dev, "invalid maximum_speed parameter %d\n",
1356 dwc->maximum_speed);
1358 case USB_SPEED_UNKNOWN:
1359 /* default to superspeed */
1360 dwc->maximum_speed = USB_SPEED_SUPER;
1363 * default to superspeed plus if we are capable.
1365 if (dwc3_is_usb31(dwc) &&
1366 (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
1367 DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1368 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1374 static int dwc3_probe(struct platform_device *pdev)
1376 struct device *dev = &pdev->dev;
1377 struct resource *res, dwc_res;
1384 dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
1388 dwc->clks = devm_kmemdup(dev, dwc3_core_clks, sizeof(dwc3_core_clks),
1395 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1397 dev_err(dev, "missing memory resource\n");
1401 dwc->xhci_resources[0].start = res->start;
1402 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1404 dwc->xhci_resources[0].flags = res->flags;
1405 dwc->xhci_resources[0].name = res->name;
1408 * Request memory region but exclude xHCI regs,
1409 * since it will be requested by the xhci-plat driver.
1412 dwc_res.start += DWC3_GLOBALS_REGS_START;
1414 regs = devm_ioremap_resource(dev, &dwc_res);
1416 return PTR_ERR(regs);
1419 dwc->regs_size = resource_size(&dwc_res);
1421 dwc3_get_properties(dwc);
1423 dwc->reset = devm_reset_control_get_optional_shared(dev, NULL);
1424 if (IS_ERR(dwc->reset))
1425 return PTR_ERR(dwc->reset);
1428 dwc->num_clks = ARRAY_SIZE(dwc3_core_clks);
1430 ret = clk_bulk_get(dev, dwc->num_clks, dwc->clks);
1431 if (ret == -EPROBE_DEFER)
1434 * Clocks are optional, but new DT platforms should support all
1435 * clocks as required by the DT-binding.
1441 ret = reset_control_deassert(dwc->reset);
1445 ret = clk_bulk_prepare(dwc->num_clks, dwc->clks);
1449 ret = clk_bulk_enable(dwc->num_clks, dwc->clks);
1451 goto unprepare_clks;
1453 platform_set_drvdata(pdev, dwc);
1454 dwc3_cache_hwparams(dwc);
1456 spin_lock_init(&dwc->lock);
1458 pm_runtime_set_active(dev);
1459 pm_runtime_use_autosuspend(dev);
1460 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
1461 pm_runtime_enable(dev);
1462 ret = pm_runtime_get_sync(dev);
1466 pm_runtime_forbid(dev);
1468 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1470 dev_err(dwc->dev, "failed to allocate event buffers\n");
1475 ret = dwc3_get_dr_mode(dwc);
1479 ret = dwc3_alloc_scratch_buffers(dwc);
1483 ret = dwc3_core_init(dwc);
1485 dev_err(dev, "failed to initialize core\n");
1489 dwc3_check_params(dwc);
1491 ret = dwc3_core_init_mode(dwc);
1495 dwc3_debugfs_init(dwc);
1496 pm_runtime_put(dev);
1501 dwc3_event_buffers_cleanup(dwc);
1504 dwc3_free_scratch_buffers(dwc);
1507 dwc3_free_event_buffers(dwc);
1510 pm_runtime_allow(&pdev->dev);
1513 pm_runtime_put_sync(&pdev->dev);
1514 pm_runtime_disable(&pdev->dev);
1516 clk_bulk_disable(dwc->num_clks, dwc->clks);
1518 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
1520 reset_control_assert(dwc->reset);
1522 clk_bulk_put(dwc->num_clks, dwc->clks);
1527 static int dwc3_remove(struct platform_device *pdev)
1529 struct dwc3 *dwc = platform_get_drvdata(pdev);
1531 pm_runtime_get_sync(&pdev->dev);
1533 dwc3_debugfs_exit(dwc);
1534 dwc3_core_exit_mode(dwc);
1536 dwc3_core_exit(dwc);
1537 dwc3_ulpi_exit(dwc);
1539 pm_runtime_put_sync(&pdev->dev);
1540 pm_runtime_allow(&pdev->dev);
1541 pm_runtime_disable(&pdev->dev);
1543 dwc3_free_event_buffers(dwc);
1544 dwc3_free_scratch_buffers(dwc);
1545 clk_bulk_put(dwc->num_clks, dwc->clks);
1551 static int dwc3_core_init_for_resume(struct dwc3 *dwc)
1555 ret = reset_control_deassert(dwc->reset);
1559 ret = clk_bulk_prepare(dwc->num_clks, dwc->clks);
1563 ret = clk_bulk_enable(dwc->num_clks, dwc->clks);
1565 goto unprepare_clks;
1567 ret = dwc3_core_init(dwc);
1574 clk_bulk_disable(dwc->num_clks, dwc->clks);
1576 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
1578 reset_control_assert(dwc->reset);
1583 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
1585 unsigned long flags;
1588 switch (dwc->current_dr_role) {
1589 case DWC3_GCTL_PRTCAP_DEVICE:
1590 spin_lock_irqsave(&dwc->lock, flags);
1591 dwc3_gadget_suspend(dwc);
1592 spin_unlock_irqrestore(&dwc->lock, flags);
1593 dwc3_core_exit(dwc);
1595 case DWC3_GCTL_PRTCAP_HOST:
1596 if (!PMSG_IS_AUTO(msg)) {
1597 dwc3_core_exit(dwc);
1601 /* Let controller to suspend HSPHY before PHY driver suspends */
1602 if (dwc->dis_u2_susphy_quirk ||
1603 dwc->dis_enblslpm_quirk) {
1604 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1605 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
1606 DWC3_GUSB2PHYCFG_SUSPHY;
1607 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1609 /* Give some time for USB2 PHY to suspend */
1610 usleep_range(5000, 6000);
1613 phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
1614 phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
1616 case DWC3_GCTL_PRTCAP_OTG:
1617 /* do nothing during runtime_suspend */
1618 if (PMSG_IS_AUTO(msg))
1621 if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1622 spin_lock_irqsave(&dwc->lock, flags);
1623 dwc3_gadget_suspend(dwc);
1624 spin_unlock_irqrestore(&dwc->lock, flags);
1628 dwc3_core_exit(dwc);
1638 static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
1640 unsigned long flags;
1644 switch (dwc->current_dr_role) {
1645 case DWC3_GCTL_PRTCAP_DEVICE:
1646 ret = dwc3_core_init_for_resume(dwc);
1650 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1651 spin_lock_irqsave(&dwc->lock, flags);
1652 dwc3_gadget_resume(dwc);
1653 spin_unlock_irqrestore(&dwc->lock, flags);
1655 case DWC3_GCTL_PRTCAP_HOST:
1656 if (!PMSG_IS_AUTO(msg)) {
1657 ret = dwc3_core_init_for_resume(dwc);
1660 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1663 /* Restore GUSB2PHYCFG bits that were modified in suspend */
1664 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1665 if (dwc->dis_u2_susphy_quirk)
1666 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1668 if (dwc->dis_enblslpm_quirk)
1669 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
1671 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1673 phy_pm_runtime_get_sync(dwc->usb2_generic_phy);
1674 phy_pm_runtime_get_sync(dwc->usb3_generic_phy);
1676 case DWC3_GCTL_PRTCAP_OTG:
1677 /* nothing to do on runtime_resume */
1678 if (PMSG_IS_AUTO(msg))
1681 ret = dwc3_core_init(dwc);
1685 dwc3_set_prtcap(dwc, dwc->current_dr_role);
1688 if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
1689 dwc3_otg_host_init(dwc);
1690 } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1691 spin_lock_irqsave(&dwc->lock, flags);
1692 dwc3_gadget_resume(dwc);
1693 spin_unlock_irqrestore(&dwc->lock, flags);
1705 static int dwc3_runtime_checks(struct dwc3 *dwc)
1707 switch (dwc->current_dr_role) {
1708 case DWC3_GCTL_PRTCAP_DEVICE:
1712 case DWC3_GCTL_PRTCAP_HOST:
1721 static int dwc3_runtime_suspend(struct device *dev)
1723 struct dwc3 *dwc = dev_get_drvdata(dev);
1726 if (dwc3_runtime_checks(dwc))
1729 ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
1733 device_init_wakeup(dev, true);
1738 static int dwc3_runtime_resume(struct device *dev)
1740 struct dwc3 *dwc = dev_get_drvdata(dev);
1743 device_init_wakeup(dev, false);
1745 ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
1749 switch (dwc->current_dr_role) {
1750 case DWC3_GCTL_PRTCAP_DEVICE:
1751 dwc3_gadget_process_pending_events(dwc);
1753 case DWC3_GCTL_PRTCAP_HOST:
1759 pm_runtime_mark_last_busy(dev);
1764 static int dwc3_runtime_idle(struct device *dev)
1766 struct dwc3 *dwc = dev_get_drvdata(dev);
1768 switch (dwc->current_dr_role) {
1769 case DWC3_GCTL_PRTCAP_DEVICE:
1770 if (dwc3_runtime_checks(dwc))
1773 case DWC3_GCTL_PRTCAP_HOST:
1779 pm_runtime_mark_last_busy(dev);
1780 pm_runtime_autosuspend(dev);
1784 #endif /* CONFIG_PM */
1786 #ifdef CONFIG_PM_SLEEP
1787 static int dwc3_suspend(struct device *dev)
1789 struct dwc3 *dwc = dev_get_drvdata(dev);
1792 ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
1796 pinctrl_pm_select_sleep_state(dev);
1801 static int dwc3_resume(struct device *dev)
1803 struct dwc3 *dwc = dev_get_drvdata(dev);
1806 pinctrl_pm_select_default_state(dev);
1808 ret = dwc3_resume_common(dwc, PMSG_RESUME);
1812 pm_runtime_disable(dev);
1813 pm_runtime_set_active(dev);
1814 pm_runtime_enable(dev);
1818 #endif /* CONFIG_PM_SLEEP */
1820 static const struct dev_pm_ops dwc3_dev_pm_ops = {
1821 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
1822 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
1827 static const struct of_device_id of_dwc3_match[] = {
1829 .compatible = "snps,dwc3"
1832 .compatible = "synopsys,dwc3"
1836 MODULE_DEVICE_TABLE(of, of_dwc3_match);
1841 #define ACPI_ID_INTEL_BSW "808622B7"
1843 static const struct acpi_device_id dwc3_acpi_match[] = {
1844 { ACPI_ID_INTEL_BSW, 0 },
1847 MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
1850 static struct platform_driver dwc3_driver = {
1851 .probe = dwc3_probe,
1852 .remove = dwc3_remove,
1855 .of_match_table = of_match_ptr(of_dwc3_match),
1856 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
1857 .pm = &dwc3_dev_pm_ops,
1861 module_platform_driver(dwc3_driver);
1863 MODULE_ALIAS("platform:dwc3");
1865 MODULE_LICENSE("GPL v2");
1866 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");