1 // SPDX-License-Identifier: GPL-2.0
3 * core.c - DesignWare USB3 DRD Controller Core file
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://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/of_graph.h>
27 #include <linux/acpi.h>
28 #include <linux/pinctrl/consumer.h>
29 #include <linux/reset.h>
30 #include <linux/bitfield.h>
32 #include <linux/usb/ch9.h>
33 #include <linux/usb/gadget.h>
34 #include <linux/usb/of.h>
35 #include <linux/usb/otg.h>
43 #define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
46 * dwc3_get_dr_mode - Validates and sets dr_mode
47 * @dwc: pointer to our context structure
49 static int dwc3_get_dr_mode(struct dwc3 *dwc)
51 enum usb_dr_mode mode;
52 struct device *dev = dwc->dev;
55 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
56 dwc->dr_mode = USB_DR_MODE_OTG;
59 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
62 case DWC3_GHWPARAMS0_MODE_GADGET:
63 if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) {
65 "Controller does not support host mode.\n");
68 mode = USB_DR_MODE_PERIPHERAL;
70 case DWC3_GHWPARAMS0_MODE_HOST:
71 if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
73 "Controller does not support device mode.\n");
76 mode = USB_DR_MODE_HOST;
79 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
80 mode = USB_DR_MODE_HOST;
81 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
82 mode = USB_DR_MODE_PERIPHERAL;
85 * DWC_usb31 and DWC_usb3 v3.30a and higher do not support OTG
86 * mode. If the controller supports DRD but the dr_mode is not
87 * specified or set to OTG, then set the mode to peripheral.
89 if (mode == USB_DR_MODE_OTG && !dwc->edev &&
90 (!IS_ENABLED(CONFIG_USB_ROLE_SWITCH) ||
91 !device_property_read_bool(dwc->dev, "usb-role-switch")) &&
92 !DWC3_VER_IS_PRIOR(DWC3, 330A))
93 mode = USB_DR_MODE_PERIPHERAL;
96 if (mode != dwc->dr_mode) {
98 "Configuration mismatch. dr_mode forced to %s\n",
99 mode == USB_DR_MODE_HOST ? "host" : "gadget");
107 void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
111 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
112 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
113 reg |= DWC3_GCTL_PRTCAPDIR(mode);
114 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
116 dwc->current_dr_role = mode;
119 static void __dwc3_set_mode(struct work_struct *work)
121 struct dwc3 *dwc = work_to_dwc(work);
126 mutex_lock(&dwc->mutex);
128 pm_runtime_get_sync(dwc->dev);
130 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
131 dwc3_otg_update(dwc, 0);
133 if (!dwc->desired_dr_role)
136 if (dwc->desired_dr_role == dwc->current_dr_role)
139 if (dwc->desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev)
142 switch (dwc->current_dr_role) {
143 case DWC3_GCTL_PRTCAP_HOST:
146 case DWC3_GCTL_PRTCAP_DEVICE:
147 dwc3_gadget_exit(dwc);
148 dwc3_event_buffers_cleanup(dwc);
150 case DWC3_GCTL_PRTCAP_OTG:
152 spin_lock_irqsave(&dwc->lock, flags);
153 dwc->desired_otg_role = DWC3_OTG_ROLE_IDLE;
154 spin_unlock_irqrestore(&dwc->lock, flags);
155 dwc3_otg_update(dwc, 1);
162 * When current_dr_role is not set, there's no role switching.
163 * Only perform GCTL.CoreSoftReset when there's DRD role switching.
165 if (dwc->current_dr_role && ((DWC3_IP_IS(DWC3) ||
166 DWC3_VER_IS_PRIOR(DWC31, 190A)) &&
167 dwc->desired_dr_role != DWC3_GCTL_PRTCAP_OTG)) {
168 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
169 reg |= DWC3_GCTL_CORESOFTRESET;
170 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
173 * Wait for internal clocks to synchronized. DWC_usb31 and
174 * DWC_usb32 may need at least 50ms (less for DWC_usb3). To
175 * keep it consistent across different IPs, let's wait up to
176 * 100ms before clearing GCTL.CORESOFTRESET.
180 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
181 reg &= ~DWC3_GCTL_CORESOFTRESET;
182 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
185 spin_lock_irqsave(&dwc->lock, flags);
187 dwc3_set_prtcap(dwc, dwc->desired_dr_role);
189 spin_unlock_irqrestore(&dwc->lock, flags);
191 switch (dwc->desired_dr_role) {
192 case DWC3_GCTL_PRTCAP_HOST:
193 ret = dwc3_host_init(dwc);
195 dev_err(dwc->dev, "failed to initialize host\n");
198 otg_set_vbus(dwc->usb2_phy->otg, true);
199 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
200 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
201 if (dwc->dis_split_quirk) {
202 reg = dwc3_readl(dwc->regs, DWC3_GUCTL3);
203 reg |= DWC3_GUCTL3_SPLITDISABLE;
204 dwc3_writel(dwc->regs, DWC3_GUCTL3, reg);
208 case DWC3_GCTL_PRTCAP_DEVICE:
209 dwc3_core_soft_reset(dwc);
211 dwc3_event_buffers_setup(dwc);
214 otg_set_vbus(dwc->usb2_phy->otg, false);
215 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
216 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
218 ret = dwc3_gadget_init(dwc);
220 dev_err(dwc->dev, "failed to initialize peripheral\n");
222 case DWC3_GCTL_PRTCAP_OTG:
224 dwc3_otg_update(dwc, 0);
231 pm_runtime_mark_last_busy(dwc->dev);
232 pm_runtime_put_autosuspend(dwc->dev);
233 mutex_unlock(&dwc->mutex);
236 void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
240 if (dwc->dr_mode != USB_DR_MODE_OTG)
243 spin_lock_irqsave(&dwc->lock, flags);
244 dwc->desired_dr_role = mode;
245 spin_unlock_irqrestore(&dwc->lock, flags);
247 queue_work(system_freezable_wq, &dwc->drd_work);
250 u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
252 struct dwc3 *dwc = dep->dwc;
255 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
256 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
257 DWC3_GDBGFIFOSPACE_TYPE(type));
259 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
261 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
265 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
266 * @dwc: pointer to our context structure
268 int dwc3_core_soft_reset(struct dwc3 *dwc)
274 * We're resetting only the device side because, if we're in host mode,
275 * XHCI driver will reset the host block. If dwc3 was configured for
276 * host-only mode, then we can return early.
278 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST)
281 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
282 reg |= DWC3_DCTL_CSFTRST;
283 reg &= ~DWC3_DCTL_RUN_STOP;
284 dwc3_gadget_dctl_write_safe(dwc, reg);
287 * For DWC_usb31 controller 1.90a and later, the DCTL.CSFRST bit
288 * is cleared only after all the clocks are synchronized. This can
289 * take a little more than 50ms. Set the polling rate at 20ms
290 * for 10 times instead.
292 if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
296 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
297 if (!(reg & DWC3_DCTL_CSFTRST))
300 if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
306 dev_warn(dwc->dev, "DWC3 controller soft reset failed.\n");
311 * For DWC_usb31 controller 1.80a and prior, once DCTL.CSFRST bit
312 * is cleared, we must wait at least 50ms before accessing the PHY
313 * domain (synchronization delay).
315 if (DWC3_VER_IS_WITHIN(DWC31, ANY, 180A))
322 * dwc3_frame_length_adjustment - Adjusts frame length if required
323 * @dwc3: Pointer to our controller context structure
325 static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
330 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
336 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
337 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
338 if (dft != dwc->fladj) {
339 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
340 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
341 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
346 * dwc3_ref_clk_period - Reference clock period configuration
347 * Default reference clock period depends on hardware
348 * configuration. For systems with reference clock that differs
349 * from the default, this will set clock period in DWC3_GUCTL
351 * @dwc: Pointer to our controller context structure
353 static void dwc3_ref_clk_period(struct dwc3 *dwc)
355 unsigned long period;
362 rate = clk_get_rate(dwc->ref_clk);
365 period = NSEC_PER_SEC / rate;
366 } else if (dwc->ref_clk_per) {
367 period = dwc->ref_clk_per;
368 rate = NSEC_PER_SEC / period;
373 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
374 reg &= ~DWC3_GUCTL_REFCLKPER_MASK;
375 reg |= FIELD_PREP(DWC3_GUCTL_REFCLKPER_MASK, period);
376 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
378 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
382 * The calculation below is
384 * 125000 * (NSEC_PER_SEC / (rate * period) - 1)
386 * but rearranged for fixed-point arithmetic. The division must be
387 * 64-bit because 125000 * NSEC_PER_SEC doesn't fit in 32 bits (and
388 * neither does rate * period).
390 * Note that rate * period ~= NSEC_PER_SECOND, minus the number of
391 * nanoseconds of error caused by the truncation which happened during
392 * the division when calculating rate or period (whichever one was
393 * derived from the other). We first calculate the relative error, then
394 * scale it to units of 8 ppm.
396 fladj = div64_u64(125000ULL * NSEC_PER_SEC, (u64)rate * period);
400 * The documented 240MHz constant is scaled by 2 to get PLS1 as well.
402 decr = 480000000 / rate;
404 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
405 reg &= ~DWC3_GFLADJ_REFCLK_FLADJ_MASK
406 & ~DWC3_GFLADJ_240MHZDECR
407 & ~DWC3_GFLADJ_240MHZDECR_PLS1;
408 reg |= FIELD_PREP(DWC3_GFLADJ_REFCLK_FLADJ_MASK, fladj)
409 | FIELD_PREP(DWC3_GFLADJ_240MHZDECR, decr >> 1)
410 | FIELD_PREP(DWC3_GFLADJ_240MHZDECR_PLS1, decr & 1);
411 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
415 * dwc3_free_one_event_buffer - Frees one event buffer
416 * @dwc: Pointer to our controller context structure
417 * @evt: Pointer to event buffer to be freed
419 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
420 struct dwc3_event_buffer *evt)
422 dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
426 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
427 * @dwc: Pointer to our controller context structure
428 * @length: size of the event buffer
430 * Returns a pointer to the allocated event buffer structure on success
431 * otherwise ERR_PTR(errno).
433 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
436 struct dwc3_event_buffer *evt;
438 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
440 return ERR_PTR(-ENOMEM);
443 evt->length = length;
444 evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
446 return ERR_PTR(-ENOMEM);
448 evt->buf = dma_alloc_coherent(dwc->sysdev, length,
449 &evt->dma, GFP_KERNEL);
451 return ERR_PTR(-ENOMEM);
457 * dwc3_free_event_buffers - frees all allocated event buffers
458 * @dwc: Pointer to our controller context structure
460 static void dwc3_free_event_buffers(struct dwc3 *dwc)
462 struct dwc3_event_buffer *evt;
466 dwc3_free_one_event_buffer(dwc, evt);
470 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
471 * @dwc: pointer to our controller context structure
472 * @length: size of event buffer
474 * Returns 0 on success otherwise negative errno. In the error case, dwc
475 * may contain some buffers allocated but not all which were requested.
477 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned int length)
479 struct dwc3_event_buffer *evt;
481 evt = dwc3_alloc_one_event_buffer(dwc, length);
483 dev_err(dwc->dev, "can't allocate event buffer\n");
492 * dwc3_event_buffers_setup - setup our allocated event buffers
493 * @dwc: pointer to our controller context structure
495 * Returns 0 on success otherwise negative errno.
497 int dwc3_event_buffers_setup(struct dwc3 *dwc)
499 struct dwc3_event_buffer *evt;
503 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
504 lower_32_bits(evt->dma));
505 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
506 upper_32_bits(evt->dma));
507 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
508 DWC3_GEVNTSIZ_SIZE(evt->length));
509 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
514 void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
516 struct dwc3_event_buffer *evt;
522 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
523 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
524 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
525 | DWC3_GEVNTSIZ_SIZE(0));
526 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
529 static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
531 if (!dwc->has_hibernation)
534 if (!dwc->nr_scratch)
537 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
538 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
539 if (!dwc->scratchbuf)
545 static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
547 dma_addr_t scratch_addr;
551 if (!dwc->has_hibernation)
554 if (!dwc->nr_scratch)
557 /* should never fall here */
558 if (!WARN_ON(dwc->scratchbuf))
561 scratch_addr = dma_map_single(dwc->sysdev, dwc->scratchbuf,
562 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
564 if (dma_mapping_error(dwc->sysdev, scratch_addr)) {
565 dev_err(dwc->sysdev, "failed to map scratch buffer\n");
570 dwc->scratch_addr = scratch_addr;
572 param = lower_32_bits(scratch_addr);
574 ret = dwc3_send_gadget_generic_command(dwc,
575 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
579 param = upper_32_bits(scratch_addr);
581 ret = dwc3_send_gadget_generic_command(dwc,
582 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
589 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
590 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
596 static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
598 if (!dwc->has_hibernation)
601 if (!dwc->nr_scratch)
604 /* should never fall here */
605 if (!WARN_ON(dwc->scratchbuf))
608 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
609 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
610 kfree(dwc->scratchbuf);
613 static void dwc3_core_num_eps(struct dwc3 *dwc)
615 struct dwc3_hwparams *parms = &dwc->hwparams;
617 dwc->num_eps = DWC3_NUM_EPS(parms);
620 static void dwc3_cache_hwparams(struct dwc3 *dwc)
622 struct dwc3_hwparams *parms = &dwc->hwparams;
624 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
625 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
626 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
627 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
628 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
629 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
630 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
631 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
632 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
634 if (DWC3_IP_IS(DWC32))
635 parms->hwparams9 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS9);
638 static int dwc3_core_ulpi_init(struct dwc3 *dwc)
643 intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
645 if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
646 (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
647 dwc->hsphy_interface &&
648 !strncmp(dwc->hsphy_interface, "ulpi", 4)))
649 ret = dwc3_ulpi_init(dwc);
655 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
656 * @dwc: Pointer to our controller context structure
658 * Returns 0 on success. The USB PHY interfaces are configured but not
659 * initialized. The PHY interfaces and the PHYs get initialized together with
660 * the core in dwc3_core_init.
662 static int dwc3_phy_setup(struct dwc3 *dwc)
664 unsigned int hw_mode;
667 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
669 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
672 * Make sure UX_EXIT_PX is cleared as that causes issues with some
673 * PHYs. Also, this bit is not supposed to be used in normal operation.
675 reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
678 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
679 * to '0' during coreConsultant configuration. So default value
680 * will be '0' when the core is reset. Application needs to set it
681 * to '1' after the core initialization is completed.
683 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
684 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
687 * For DRD controllers, GUSB3PIPECTL.SUSPENDENABLE must be cleared after
688 * power-on reset, and it can be set after core initialization, which is
689 * after device soft-reset during initialization.
691 if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD)
692 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
694 if (dwc->u2ss_inp3_quirk)
695 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
697 if (dwc->dis_rxdet_inp3_quirk)
698 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
700 if (dwc->req_p1p2p3_quirk)
701 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
703 if (dwc->del_p1p2p3_quirk)
704 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
706 if (dwc->del_phy_power_chg_quirk)
707 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
709 if (dwc->lfps_filter_quirk)
710 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
712 if (dwc->rx_detect_poll_quirk)
713 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
715 if (dwc->tx_de_emphasis_quirk)
716 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
718 if (dwc->dis_u3_susphy_quirk)
719 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
721 if (dwc->dis_del_phy_power_chg_quirk)
722 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
724 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
726 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
728 /* Select the HS PHY interface */
729 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
730 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
731 if (dwc->hsphy_interface &&
732 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
733 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
735 } else if (dwc->hsphy_interface &&
736 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
737 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
738 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
740 /* Relying on default value. */
741 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
745 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
750 switch (dwc->hsphy_mode) {
751 case USBPHY_INTERFACE_MODE_UTMI:
752 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
753 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
754 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
755 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
757 case USBPHY_INTERFACE_MODE_UTMIW:
758 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
759 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
760 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
761 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
768 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
769 * '0' during coreConsultant configuration. So default value will
770 * be '0' when the core is reset. Application needs to set it to
771 * '1' after the core initialization is completed.
773 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
774 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
777 * For DRD controllers, GUSB2PHYCFG.SUSPHY must be cleared after
778 * power-on reset, and it can be set after core initialization, which is
779 * after device soft-reset during initialization.
781 if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD)
782 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
784 if (dwc->dis_u2_susphy_quirk)
785 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
787 if (dwc->dis_enblslpm_quirk)
788 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
790 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM;
792 if (dwc->dis_u2_freeclk_exists_quirk)
793 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
795 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
800 static int dwc3_clk_enable(struct dwc3 *dwc)
804 ret = clk_prepare_enable(dwc->bus_clk);
808 ret = clk_prepare_enable(dwc->ref_clk);
810 goto disable_bus_clk;
812 ret = clk_prepare_enable(dwc->susp_clk);
814 goto disable_ref_clk;
819 clk_disable_unprepare(dwc->ref_clk);
821 clk_disable_unprepare(dwc->bus_clk);
825 static void dwc3_clk_disable(struct dwc3 *dwc)
827 clk_disable_unprepare(dwc->susp_clk);
828 clk_disable_unprepare(dwc->ref_clk);
829 clk_disable_unprepare(dwc->bus_clk);
832 static void dwc3_core_exit(struct dwc3 *dwc)
834 dwc3_event_buffers_cleanup(dwc);
836 usb_phy_shutdown(dwc->usb2_phy);
837 usb_phy_shutdown(dwc->usb3_phy);
838 phy_exit(dwc->usb2_generic_phy);
839 phy_exit(dwc->usb3_generic_phy);
841 usb_phy_set_suspend(dwc->usb2_phy, 1);
842 usb_phy_set_suspend(dwc->usb3_phy, 1);
843 phy_power_off(dwc->usb2_generic_phy);
844 phy_power_off(dwc->usb3_generic_phy);
845 dwc3_clk_disable(dwc);
846 reset_control_assert(dwc->reset);
849 static bool dwc3_core_is_valid(struct dwc3 *dwc)
853 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
854 dwc->ip = DWC3_GSNPS_ID(reg);
856 /* This should read as U3 followed by revision number */
857 if (DWC3_IP_IS(DWC3)) {
859 } else if (DWC3_IP_IS(DWC31) || DWC3_IP_IS(DWC32)) {
860 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
861 dwc->version_type = dwc3_readl(dwc->regs, DWC3_VER_TYPE);
869 static void dwc3_core_setup_global_control(struct dwc3 *dwc)
871 u32 hwparams4 = dwc->hwparams.hwparams4;
874 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
875 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
877 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
878 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
880 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
881 * issue which would cause xHCI compliance tests to fail.
883 * Because of that we cannot enable clock gating on such
888 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
891 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
892 dwc->dr_mode == USB_DR_MODE_OTG) &&
893 DWC3_VER_IS_WITHIN(DWC3, 210A, 250A))
894 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
896 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
898 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
899 /* enable hibernation here */
900 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
903 * REVISIT Enabling this bit so that host-mode hibernation
904 * will work. Device-mode hibernation is not yet implemented.
906 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
913 /* check if current dwc3 is on simulation board */
914 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
915 dev_info(dwc->dev, "Running with FPGA optimizations\n");
919 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
920 "disable_scramble cannot be used on non-FPGA builds\n");
922 if (dwc->disable_scramble_quirk && dwc->is_fpga)
923 reg |= DWC3_GCTL_DISSCRAMBLE;
925 reg &= ~DWC3_GCTL_DISSCRAMBLE;
927 if (dwc->u2exit_lfps_quirk)
928 reg |= DWC3_GCTL_U2EXIT_LFPS;
931 * WORKAROUND: DWC3 revisions <1.90a have a bug
932 * where the device can fail to connect at SuperSpeed
933 * and falls back to high-speed mode which causes
934 * the device to enter a Connect/Disconnect loop
936 if (DWC3_VER_IS_PRIOR(DWC3, 190A))
937 reg |= DWC3_GCTL_U2RSTECN;
939 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
942 static int dwc3_core_get_phy(struct dwc3 *dwc);
943 static int dwc3_core_ulpi_init(struct dwc3 *dwc);
945 /* set global incr burst type configuration registers */
946 static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
948 struct device *dev = dwc->dev;
949 /* incrx_mode : for INCR burst type. */
951 /* incrx_size : for size of INCRX burst. */
959 cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
962 * Handle property "snps,incr-burst-type-adjustment".
963 * Get the number of value from this property:
964 * result <= 0, means this property is not supported.
965 * result = 1, means INCRx burst mode supported.
966 * result > 1, means undefined length burst mode supported.
968 ntype = device_property_count_u32(dev, "snps,incr-burst-type-adjustment");
972 vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL);
976 /* Get INCR burst type, and parse it */
977 ret = device_property_read_u32_array(dev,
978 "snps,incr-burst-type-adjustment", vals, ntype);
981 dev_err(dev, "Error to get property\n");
988 /* INCRX (undefined length) burst mode */
989 incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
990 for (i = 1; i < ntype; i++) {
991 if (vals[i] > incrx_size)
992 incrx_size = vals[i];
995 /* INCRX burst mode */
996 incrx_mode = INCRX_BURST_MODE;
1001 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
1002 cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
1004 cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
1005 switch (incrx_size) {
1007 cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
1010 cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
1013 cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
1016 cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
1019 cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
1022 cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
1025 cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
1030 dev_err(dev, "Invalid property\n");
1034 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
1037 static void dwc3_set_power_down_clk_scale(struct dwc3 *dwc)
1046 * The power down scale field specifies how many suspend_clk
1047 * periods fit into a 16KHz clock period. When performing
1048 * the division, round up the remainder.
1050 * The power down scale value is calculated using the fastest
1051 * frequency of the suspend_clk. If it isn't fixed (but within
1052 * the accuracy requirement), the driver may not know the max
1053 * rate of the suspend_clk, so only update the power down scale
1054 * if the default is less than the calculated value from
1055 * clk_get_rate() or if the default is questionably high
1056 * (3x or more) to be within the requirement.
1058 scale = DIV_ROUND_UP(clk_get_rate(dwc->susp_clk), 16000);
1059 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1060 if ((reg & DWC3_GCTL_PWRDNSCALE_MASK) < DWC3_GCTL_PWRDNSCALE(scale) ||
1061 (reg & DWC3_GCTL_PWRDNSCALE_MASK) > DWC3_GCTL_PWRDNSCALE(scale*3)) {
1062 reg &= ~(DWC3_GCTL_PWRDNSCALE_MASK);
1063 reg |= DWC3_GCTL_PWRDNSCALE(scale);
1064 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1069 * dwc3_core_init - Low-level initialization of DWC3 Core
1070 * @dwc: Pointer to our controller context structure
1072 * Returns 0 on success otherwise negative errno.
1074 static int dwc3_core_init(struct dwc3 *dwc)
1076 unsigned int hw_mode;
1080 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
1083 * Write Linux Version Code to our GUID register so it's easy to figure
1084 * out which kernel version a bug was found.
1086 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
1088 ret = dwc3_phy_setup(dwc);
1092 if (!dwc->ulpi_ready) {
1093 ret = dwc3_core_ulpi_init(dwc);
1096 dwc->ulpi_ready = true;
1099 if (!dwc->phys_ready) {
1100 ret = dwc3_core_get_phy(dwc);
1103 dwc->phys_ready = true;
1106 usb_phy_init(dwc->usb2_phy);
1107 usb_phy_init(dwc->usb3_phy);
1108 ret = phy_init(dwc->usb2_generic_phy);
1112 ret = phy_init(dwc->usb3_generic_phy);
1114 phy_exit(dwc->usb2_generic_phy);
1118 ret = dwc3_core_soft_reset(dwc);
1122 if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD &&
1123 !DWC3_VER_IS_WITHIN(DWC3, ANY, 194A)) {
1124 if (!dwc->dis_u3_susphy_quirk) {
1125 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1126 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1127 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1130 if (!dwc->dis_u2_susphy_quirk) {
1131 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1132 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1133 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1137 dwc3_core_setup_global_control(dwc);
1138 dwc3_core_num_eps(dwc);
1140 ret = dwc3_setup_scratch_buffers(dwc);
1144 /* Set power down scale of suspend_clk */
1145 dwc3_set_power_down_clk_scale(dwc);
1147 /* Adjust Frame Length */
1148 dwc3_frame_length_adjustment(dwc);
1150 /* Adjust Reference Clock Period */
1151 dwc3_ref_clk_period(dwc);
1153 dwc3_set_incr_burst_type(dwc);
1155 usb_phy_set_suspend(dwc->usb2_phy, 0);
1156 usb_phy_set_suspend(dwc->usb3_phy, 0);
1157 ret = phy_power_on(dwc->usb2_generic_phy);
1161 ret = phy_power_on(dwc->usb3_generic_phy);
1165 ret = dwc3_event_buffers_setup(dwc);
1167 dev_err(dwc->dev, "failed to setup event buffers\n");
1172 * ENDXFER polling is available on version 3.10a and later of
1173 * the DWC_usb3 controller. It is NOT available in the
1174 * DWC_usb31 controller.
1176 if (DWC3_VER_IS_WITHIN(DWC3, 310A, ANY)) {
1177 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
1178 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
1179 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
1182 if (!DWC3_VER_IS_PRIOR(DWC3, 250A)) {
1183 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1186 * Enable hardware control of sending remote wakeup
1187 * in HS when the device is in the L1 state.
1189 if (!DWC3_VER_IS_PRIOR(DWC3, 290A))
1190 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
1193 * Decouple USB 2.0 L1 & L2 events which will allow for
1194 * gadget driver to only receive U3/L2 suspend & wakeup
1195 * events and prevent the more frequent L1 LPM transitions
1196 * from interrupting the driver.
1198 if (!DWC3_VER_IS_PRIOR(DWC3, 300A))
1199 reg |= DWC3_GUCTL1_DEV_DECOUPLE_L1L2_EVT;
1201 if (dwc->dis_tx_ipgap_linecheck_quirk)
1202 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
1204 if (dwc->parkmode_disable_ss_quirk)
1205 reg |= DWC3_GUCTL1_PARKMODE_DISABLE_SS;
1207 if (DWC3_VER_IS_WITHIN(DWC3, 290A, ANY) &&
1208 (dwc->maximum_speed == USB_SPEED_HIGH ||
1209 dwc->maximum_speed == USB_SPEED_FULL))
1210 reg |= DWC3_GUCTL1_DEV_FORCE_20_CLK_FOR_30_CLK;
1212 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1215 if (dwc->dr_mode == USB_DR_MODE_HOST ||
1216 dwc->dr_mode == USB_DR_MODE_OTG) {
1217 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
1220 * Enable Auto retry Feature to make the controller operating in
1221 * Host mode on seeing transaction errors(CRC errors or internal
1222 * overrun scenerios) on IN transfers to reply to the device
1223 * with a non-terminating retry ACK (i.e, an ACK transcation
1224 * packet with Retry=1 & Nump != 0)
1226 reg |= DWC3_GUCTL_HSTINAUTORETRY;
1228 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
1232 * Must config both number of packets and max burst settings to enable
1233 * RX and/or TX threshold.
1235 if (!DWC3_IP_IS(DWC3) && dwc->dr_mode == USB_DR_MODE_HOST) {
1236 u8 rx_thr_num = dwc->rx_thr_num_pkt_prd;
1237 u8 rx_maxburst = dwc->rx_max_burst_prd;
1238 u8 tx_thr_num = dwc->tx_thr_num_pkt_prd;
1239 u8 tx_maxburst = dwc->tx_max_burst_prd;
1241 if (rx_thr_num && rx_maxburst) {
1242 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1243 reg |= DWC31_RXTHRNUMPKTSEL_PRD;
1245 reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
1246 reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
1248 reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
1249 reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
1251 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1254 if (tx_thr_num && tx_maxburst) {
1255 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1256 reg |= DWC31_TXTHRNUMPKTSEL_PRD;
1258 reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
1259 reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
1261 reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
1262 reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
1264 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1271 phy_power_off(dwc->usb3_generic_phy);
1274 phy_power_off(dwc->usb2_generic_phy);
1277 usb_phy_set_suspend(dwc->usb2_phy, 1);
1278 usb_phy_set_suspend(dwc->usb3_phy, 1);
1281 usb_phy_shutdown(dwc->usb2_phy);
1282 usb_phy_shutdown(dwc->usb3_phy);
1283 phy_exit(dwc->usb2_generic_phy);
1284 phy_exit(dwc->usb3_generic_phy);
1287 dwc3_ulpi_exit(dwc);
1293 static int dwc3_core_get_phy(struct dwc3 *dwc)
1295 struct device *dev = dwc->dev;
1296 struct device_node *node = dev->of_node;
1300 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
1301 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
1303 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1304 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
1307 if (IS_ERR(dwc->usb2_phy)) {
1308 ret = PTR_ERR(dwc->usb2_phy);
1309 if (ret == -ENXIO || ret == -ENODEV)
1310 dwc->usb2_phy = NULL;
1312 return dev_err_probe(dev, ret, "no usb2 phy configured\n");
1315 if (IS_ERR(dwc->usb3_phy)) {
1316 ret = PTR_ERR(dwc->usb3_phy);
1317 if (ret == -ENXIO || ret == -ENODEV)
1318 dwc->usb3_phy = NULL;
1320 return dev_err_probe(dev, ret, "no usb3 phy configured\n");
1323 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
1324 if (IS_ERR(dwc->usb2_generic_phy)) {
1325 ret = PTR_ERR(dwc->usb2_generic_phy);
1326 if (ret == -ENOSYS || ret == -ENODEV)
1327 dwc->usb2_generic_phy = NULL;
1329 return dev_err_probe(dev, ret, "no usb2 phy configured\n");
1332 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
1333 if (IS_ERR(dwc->usb3_generic_phy)) {
1334 ret = PTR_ERR(dwc->usb3_generic_phy);
1335 if (ret == -ENOSYS || ret == -ENODEV)
1336 dwc->usb3_generic_phy = NULL;
1338 return dev_err_probe(dev, ret, "no usb3 phy configured\n");
1344 static int dwc3_core_init_mode(struct dwc3 *dwc)
1346 struct device *dev = dwc->dev;
1349 switch (dwc->dr_mode) {
1350 case USB_DR_MODE_PERIPHERAL:
1351 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1354 otg_set_vbus(dwc->usb2_phy->otg, false);
1355 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
1356 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
1358 ret = dwc3_gadget_init(dwc);
1360 return dev_err_probe(dev, ret, "failed to initialize gadget\n");
1362 case USB_DR_MODE_HOST:
1363 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1366 otg_set_vbus(dwc->usb2_phy->otg, true);
1367 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
1368 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
1370 ret = dwc3_host_init(dwc);
1372 return dev_err_probe(dev, ret, "failed to initialize host\n");
1374 case USB_DR_MODE_OTG:
1375 INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
1376 ret = dwc3_drd_init(dwc);
1378 return dev_err_probe(dev, ret, "failed to initialize dual-role\n");
1381 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1388 static void dwc3_core_exit_mode(struct dwc3 *dwc)
1390 switch (dwc->dr_mode) {
1391 case USB_DR_MODE_PERIPHERAL:
1392 dwc3_gadget_exit(dwc);
1394 case USB_DR_MODE_HOST:
1395 dwc3_host_exit(dwc);
1397 case USB_DR_MODE_OTG:
1405 /* de-assert DRVVBUS for HOST and OTG mode */
1406 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1409 static void dwc3_get_properties(struct dwc3 *dwc)
1411 struct device *dev = dwc->dev;
1412 u8 lpm_nyet_threshold;
1415 u8 rx_thr_num_pkt_prd = 0;
1416 u8 rx_max_burst_prd = 0;
1417 u8 tx_thr_num_pkt_prd = 0;
1418 u8 tx_max_burst_prd = 0;
1419 u8 tx_fifo_resize_max_num;
1420 const char *usb_psy_name;
1423 /* default to highest possible threshold */
1424 lpm_nyet_threshold = 0xf;
1426 /* default to -3.5dB de-emphasis */
1430 * default to assert utmi_sleep_n and use maximum allowed HIRD
1431 * threshold value of 0b1100
1433 hird_threshold = 12;
1436 * default to a TXFIFO size large enough to fit 6 max packets. This
1437 * allows for systems with larger bus latencies to have some headroom
1438 * for endpoints that have a large bMaxBurst value.
1440 tx_fifo_resize_max_num = 6;
1442 dwc->maximum_speed = usb_get_maximum_speed(dev);
1443 dwc->max_ssp_rate = usb_get_maximum_ssp_rate(dev);
1444 dwc->dr_mode = usb_get_dr_mode(dev);
1445 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
1447 dwc->sysdev_is_parent = device_property_read_bool(dev,
1448 "linux,sysdev_is_parent");
1449 if (dwc->sysdev_is_parent)
1450 dwc->sysdev = dwc->dev->parent;
1452 dwc->sysdev = dwc->dev;
1454 ret = device_property_read_string(dev, "usb-psy-name", &usb_psy_name);
1456 dwc->usb_psy = power_supply_get_by_name(usb_psy_name);
1458 dev_err(dev, "couldn't get usb power supply\n");
1461 dwc->has_lpm_erratum = device_property_read_bool(dev,
1462 "snps,has-lpm-erratum");
1463 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
1464 &lpm_nyet_threshold);
1465 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
1466 "snps,is-utmi-l1-suspend");
1467 device_property_read_u8(dev, "snps,hird-threshold",
1469 dwc->dis_start_transfer_quirk = device_property_read_bool(dev,
1470 "snps,dis-start-transfer-quirk");
1471 dwc->usb3_lpm_capable = device_property_read_bool(dev,
1472 "snps,usb3_lpm_capable");
1473 dwc->usb2_lpm_disable = device_property_read_bool(dev,
1474 "snps,usb2-lpm-disable");
1475 dwc->usb2_gadget_lpm_disable = device_property_read_bool(dev,
1476 "snps,usb2-gadget-lpm-disable");
1477 device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
1478 &rx_thr_num_pkt_prd);
1479 device_property_read_u8(dev, "snps,rx-max-burst-prd",
1481 device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
1482 &tx_thr_num_pkt_prd);
1483 device_property_read_u8(dev, "snps,tx-max-burst-prd",
1485 dwc->do_fifo_resize = device_property_read_bool(dev,
1487 if (dwc->do_fifo_resize)
1488 device_property_read_u8(dev, "tx-fifo-max-num",
1489 &tx_fifo_resize_max_num);
1491 dwc->disable_scramble_quirk = device_property_read_bool(dev,
1492 "snps,disable_scramble_quirk");
1493 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
1494 "snps,u2exit_lfps_quirk");
1495 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
1496 "snps,u2ss_inp3_quirk");
1497 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
1498 "snps,req_p1p2p3_quirk");
1499 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
1500 "snps,del_p1p2p3_quirk");
1501 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
1502 "snps,del_phy_power_chg_quirk");
1503 dwc->lfps_filter_quirk = device_property_read_bool(dev,
1504 "snps,lfps_filter_quirk");
1505 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
1506 "snps,rx_detect_poll_quirk");
1507 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
1508 "snps,dis_u3_susphy_quirk");
1509 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
1510 "snps,dis_u2_susphy_quirk");
1511 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1512 "snps,dis_enblslpm_quirk");
1513 dwc->dis_u1_entry_quirk = device_property_read_bool(dev,
1514 "snps,dis-u1-entry-quirk");
1515 dwc->dis_u2_entry_quirk = device_property_read_bool(dev,
1516 "snps,dis-u2-entry-quirk");
1517 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1518 "snps,dis_rxdet_inp3_quirk");
1519 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1520 "snps,dis-u2-freeclk-exists-quirk");
1521 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1522 "snps,dis-del-phy-power-chg-quirk");
1523 dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1524 "snps,dis-tx-ipgap-linecheck-quirk");
1525 dwc->parkmode_disable_ss_quirk = device_property_read_bool(dev,
1526 "snps,parkmode-disable-ss-quirk");
1528 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
1529 "snps,tx_de_emphasis_quirk");
1530 device_property_read_u8(dev, "snps,tx_de_emphasis",
1532 device_property_read_string(dev, "snps,hsphy_interface",
1533 &dwc->hsphy_interface);
1534 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
1536 device_property_read_u32(dev, "snps,ref-clock-period-ns",
1539 dwc->dis_metastability_quirk = device_property_read_bool(dev,
1540 "snps,dis_metastability_quirk");
1542 dwc->dis_split_quirk = device_property_read_bool(dev,
1543 "snps,dis-split-quirk");
1545 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1546 dwc->tx_de_emphasis = tx_de_emphasis;
1548 dwc->hird_threshold = hird_threshold;
1550 dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd;
1551 dwc->rx_max_burst_prd = rx_max_burst_prd;
1553 dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd;
1554 dwc->tx_max_burst_prd = tx_max_burst_prd;
1556 dwc->imod_interval = 0;
1558 dwc->tx_fifo_resize_max_num = tx_fifo_resize_max_num;
1561 /* check whether the core supports IMOD */
1562 bool dwc3_has_imod(struct dwc3 *dwc)
1564 return DWC3_VER_IS_WITHIN(DWC3, 300A, ANY) ||
1565 DWC3_VER_IS_WITHIN(DWC31, 120A, ANY) ||
1569 static void dwc3_check_params(struct dwc3 *dwc)
1571 struct device *dev = dwc->dev;
1572 unsigned int hwparam_gen =
1573 DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3);
1575 /* Check for proper value of imod_interval */
1576 if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
1577 dev_warn(dwc->dev, "Interrupt moderation not supported\n");
1578 dwc->imod_interval = 0;
1582 * Workaround for STAR 9000961433 which affects only version
1583 * 3.00a of the DWC_usb3 core. This prevents the controller
1584 * interrupt from being masked while handling events. IMOD
1585 * allows us to work around this issue. Enable it for the
1588 if (!dwc->imod_interval &&
1589 DWC3_VER_IS(DWC3, 300A))
1590 dwc->imod_interval = 1;
1592 /* Check the maximum_speed parameter */
1593 switch (dwc->maximum_speed) {
1594 case USB_SPEED_FULL:
1595 case USB_SPEED_HIGH:
1597 case USB_SPEED_SUPER:
1598 if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS)
1599 dev_warn(dev, "UDC doesn't support Gen 1\n");
1601 case USB_SPEED_SUPER_PLUS:
1602 if ((DWC3_IP_IS(DWC32) &&
1603 hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS) ||
1604 (!DWC3_IP_IS(DWC32) &&
1605 hwparam_gen != DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1606 dev_warn(dev, "UDC doesn't support SSP\n");
1609 dev_err(dev, "invalid maximum_speed parameter %d\n",
1610 dwc->maximum_speed);
1612 case USB_SPEED_UNKNOWN:
1613 switch (hwparam_gen) {
1614 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
1615 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1617 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
1618 if (DWC3_IP_IS(DWC32))
1619 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1621 dwc->maximum_speed = USB_SPEED_SUPER;
1623 case DWC3_GHWPARAMS3_SSPHY_IFC_DIS:
1624 dwc->maximum_speed = USB_SPEED_HIGH;
1627 dwc->maximum_speed = USB_SPEED_SUPER;
1634 * Currently the controller does not have visibility into the HW
1635 * parameter to determine the maximum number of lanes the HW supports.
1636 * If the number of lanes is not specified in the device property, then
1637 * set the default to support dual-lane for DWC_usb32 and single-lane
1638 * for DWC_usb31 for super-speed-plus.
1640 if (dwc->maximum_speed == USB_SPEED_SUPER_PLUS) {
1641 switch (dwc->max_ssp_rate) {
1642 case USB_SSP_GEN_2x1:
1643 if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_GEN1)
1644 dev_warn(dev, "UDC only supports Gen 1\n");
1646 case USB_SSP_GEN_1x2:
1647 case USB_SSP_GEN_2x2:
1648 if (DWC3_IP_IS(DWC31))
1649 dev_warn(dev, "UDC only supports single lane\n");
1651 case USB_SSP_GEN_UNKNOWN:
1653 switch (hwparam_gen) {
1654 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
1655 if (DWC3_IP_IS(DWC32))
1656 dwc->max_ssp_rate = USB_SSP_GEN_2x2;
1658 dwc->max_ssp_rate = USB_SSP_GEN_2x1;
1660 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
1661 if (DWC3_IP_IS(DWC32))
1662 dwc->max_ssp_rate = USB_SSP_GEN_1x2;
1670 static struct extcon_dev *dwc3_get_extcon(struct dwc3 *dwc)
1672 struct device *dev = dwc->dev;
1673 struct device_node *np_phy;
1674 struct extcon_dev *edev = NULL;
1677 if (device_property_read_bool(dev, "extcon"))
1678 return extcon_get_edev_by_phandle(dev, 0);
1681 * Device tree platforms should get extcon via phandle.
1682 * On ACPI platforms, we get the name from a device property.
1683 * This device property is for kernel internal use only and
1684 * is expected to be set by the glue code.
1686 if (device_property_read_string(dev, "linux,extcon-name", &name) == 0)
1687 return extcon_get_extcon_dev(name);
1690 * Try to get an extcon device from the USB PHY controller's "port"
1691 * node. Check if it has the "port" node first, to avoid printing the
1692 * error message from underlying code, as it's a valid case: extcon
1693 * device (and "port" node) may be missing in case of "usb-role-switch"
1696 np_phy = of_parse_phandle(dev->of_node, "phys", 0);
1697 if (of_graph_is_present(np_phy)) {
1698 struct device_node *np_conn;
1700 np_conn = of_graph_get_remote_node(np_phy, -1, -1);
1702 edev = extcon_find_edev_by_node(np_conn);
1703 of_node_put(np_conn);
1705 of_node_put(np_phy);
1710 static int dwc3_probe(struct platform_device *pdev)
1712 struct device *dev = &pdev->dev;
1713 struct resource *res, dwc_res;
1720 dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
1726 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1728 dev_err(dev, "missing memory resource\n");
1732 dwc->xhci_resources[0].start = res->start;
1733 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1735 dwc->xhci_resources[0].flags = res->flags;
1736 dwc->xhci_resources[0].name = res->name;
1739 * Request memory region but exclude xHCI regs,
1740 * since it will be requested by the xhci-plat driver.
1743 dwc_res.start += DWC3_GLOBALS_REGS_START;
1745 regs = devm_ioremap_resource(dev, &dwc_res);
1747 return PTR_ERR(regs);
1750 dwc->regs_size = resource_size(&dwc_res);
1752 dwc3_get_properties(dwc);
1754 if (!dwc->sysdev_is_parent) {
1755 ret = dma_set_mask_and_coherent(dwc->sysdev, DMA_BIT_MASK(64));
1760 dwc->reset = devm_reset_control_array_get_optional_shared(dev);
1761 if (IS_ERR(dwc->reset))
1762 return PTR_ERR(dwc->reset);
1766 * Clocks are optional, but new DT platforms should support all
1767 * clocks as required by the DT-binding.
1768 * Some devices have different clock names in legacy device trees,
1769 * check for them to retain backwards compatibility.
1771 dwc->bus_clk = devm_clk_get_optional(dev, "bus_early");
1772 if (IS_ERR(dwc->bus_clk))
1773 return dev_err_probe(dev, PTR_ERR(dwc->bus_clk),
1774 "could not get bus clock\n");
1776 if (dwc->bus_clk == NULL) {
1777 dwc->bus_clk = devm_clk_get_optional(dev, "bus_clk");
1778 if (IS_ERR(dwc->bus_clk))
1779 return dev_err_probe(dev, PTR_ERR(dwc->bus_clk),
1780 "could not get bus clock\n");
1783 dwc->ref_clk = devm_clk_get_optional(dev, "ref");
1784 if (IS_ERR(dwc->ref_clk))
1785 return dev_err_probe(dev, PTR_ERR(dwc->ref_clk),
1786 "could not get ref clock\n");
1788 if (dwc->ref_clk == NULL) {
1789 dwc->ref_clk = devm_clk_get_optional(dev, "ref_clk");
1790 if (IS_ERR(dwc->ref_clk))
1791 return dev_err_probe(dev, PTR_ERR(dwc->ref_clk),
1792 "could not get ref clock\n");
1795 dwc->susp_clk = devm_clk_get_optional(dev, "suspend");
1796 if (IS_ERR(dwc->susp_clk))
1797 return dev_err_probe(dev, PTR_ERR(dwc->susp_clk),
1798 "could not get suspend clock\n");
1800 if (dwc->susp_clk == NULL) {
1801 dwc->susp_clk = devm_clk_get_optional(dev, "suspend_clk");
1802 if (IS_ERR(dwc->susp_clk))
1803 return dev_err_probe(dev, PTR_ERR(dwc->susp_clk),
1804 "could not get suspend clock\n");
1808 ret = reset_control_deassert(dwc->reset);
1812 ret = dwc3_clk_enable(dwc);
1816 if (!dwc3_core_is_valid(dwc)) {
1817 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
1822 platform_set_drvdata(pdev, dwc);
1823 dwc3_cache_hwparams(dwc);
1824 device_init_wakeup(&pdev->dev, of_property_read_bool(dev->of_node, "wakeup-source"));
1826 spin_lock_init(&dwc->lock);
1827 mutex_init(&dwc->mutex);
1829 pm_runtime_set_active(dev);
1830 pm_runtime_use_autosuspend(dev);
1831 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
1832 pm_runtime_enable(dev);
1833 ret = pm_runtime_get_sync(dev);
1837 pm_runtime_forbid(dev);
1839 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1841 dev_err(dwc->dev, "failed to allocate event buffers\n");
1846 dwc->edev = dwc3_get_extcon(dwc);
1847 if (IS_ERR(dwc->edev)) {
1848 ret = PTR_ERR(dwc->edev);
1849 dev_err_probe(dwc->dev, ret, "failed to get extcon\n");
1853 ret = dwc3_get_dr_mode(dwc);
1857 ret = dwc3_alloc_scratch_buffers(dwc);
1861 ret = dwc3_core_init(dwc);
1863 dev_err_probe(dev, ret, "failed to initialize core\n");
1867 dwc3_check_params(dwc);
1868 dwc3_debugfs_init(dwc);
1870 ret = dwc3_core_init_mode(dwc);
1874 pm_runtime_put(dev);
1879 dwc3_debugfs_exit(dwc);
1880 dwc3_event_buffers_cleanup(dwc);
1882 usb_phy_shutdown(dwc->usb2_phy);
1883 usb_phy_shutdown(dwc->usb3_phy);
1884 phy_exit(dwc->usb2_generic_phy);
1885 phy_exit(dwc->usb3_generic_phy);
1887 usb_phy_set_suspend(dwc->usb2_phy, 1);
1888 usb_phy_set_suspend(dwc->usb3_phy, 1);
1889 phy_power_off(dwc->usb2_generic_phy);
1890 phy_power_off(dwc->usb3_generic_phy);
1892 dwc3_ulpi_exit(dwc);
1895 dwc3_free_scratch_buffers(dwc);
1898 dwc3_free_event_buffers(dwc);
1901 pm_runtime_allow(&pdev->dev);
1904 pm_runtime_put_sync(&pdev->dev);
1905 pm_runtime_disable(&pdev->dev);
1908 dwc3_clk_disable(dwc);
1910 reset_control_assert(dwc->reset);
1913 power_supply_put(dwc->usb_psy);
1918 static int dwc3_remove(struct platform_device *pdev)
1920 struct dwc3 *dwc = platform_get_drvdata(pdev);
1922 pm_runtime_get_sync(&pdev->dev);
1924 dwc3_core_exit_mode(dwc);
1925 dwc3_debugfs_exit(dwc);
1927 dwc3_core_exit(dwc);
1928 dwc3_ulpi_exit(dwc);
1930 pm_runtime_disable(&pdev->dev);
1931 pm_runtime_put_noidle(&pdev->dev);
1932 pm_runtime_set_suspended(&pdev->dev);
1934 dwc3_free_event_buffers(dwc);
1935 dwc3_free_scratch_buffers(dwc);
1938 power_supply_put(dwc->usb_psy);
1944 static int dwc3_core_init_for_resume(struct dwc3 *dwc)
1948 ret = reset_control_deassert(dwc->reset);
1952 ret = dwc3_clk_enable(dwc);
1956 ret = dwc3_core_init(dwc);
1963 dwc3_clk_disable(dwc);
1965 reset_control_assert(dwc->reset);
1970 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
1972 unsigned long flags;
1975 switch (dwc->current_dr_role) {
1976 case DWC3_GCTL_PRTCAP_DEVICE:
1977 if (pm_runtime_suspended(dwc->dev))
1979 spin_lock_irqsave(&dwc->lock, flags);
1980 dwc3_gadget_suspend(dwc);
1981 spin_unlock_irqrestore(&dwc->lock, flags);
1982 synchronize_irq(dwc->irq_gadget);
1983 dwc3_core_exit(dwc);
1985 case DWC3_GCTL_PRTCAP_HOST:
1986 if (!PMSG_IS_AUTO(msg) && !device_can_wakeup(dwc->dev)) {
1987 dwc3_core_exit(dwc);
1991 /* Let controller to suspend HSPHY before PHY driver suspends */
1992 if (dwc->dis_u2_susphy_quirk ||
1993 dwc->dis_enblslpm_quirk) {
1994 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1995 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
1996 DWC3_GUSB2PHYCFG_SUSPHY;
1997 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1999 /* Give some time for USB2 PHY to suspend */
2000 usleep_range(5000, 6000);
2003 phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
2004 phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
2006 case DWC3_GCTL_PRTCAP_OTG:
2007 /* do nothing during runtime_suspend */
2008 if (PMSG_IS_AUTO(msg))
2011 if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
2012 spin_lock_irqsave(&dwc->lock, flags);
2013 dwc3_gadget_suspend(dwc);
2014 spin_unlock_irqrestore(&dwc->lock, flags);
2015 synchronize_irq(dwc->irq_gadget);
2019 dwc3_core_exit(dwc);
2029 static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
2031 unsigned long flags;
2035 switch (dwc->current_dr_role) {
2036 case DWC3_GCTL_PRTCAP_DEVICE:
2037 ret = dwc3_core_init_for_resume(dwc);
2041 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
2042 spin_lock_irqsave(&dwc->lock, flags);
2043 dwc3_gadget_resume(dwc);
2044 spin_unlock_irqrestore(&dwc->lock, flags);
2046 case DWC3_GCTL_PRTCAP_HOST:
2047 if (!PMSG_IS_AUTO(msg) && !device_can_wakeup(dwc->dev)) {
2048 ret = dwc3_core_init_for_resume(dwc);
2051 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
2054 /* Restore GUSB2PHYCFG bits that were modified in suspend */
2055 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2056 if (dwc->dis_u2_susphy_quirk)
2057 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
2059 if (dwc->dis_enblslpm_quirk)
2060 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
2062 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2064 phy_pm_runtime_get_sync(dwc->usb2_generic_phy);
2065 phy_pm_runtime_get_sync(dwc->usb3_generic_phy);
2067 case DWC3_GCTL_PRTCAP_OTG:
2068 /* nothing to do on runtime_resume */
2069 if (PMSG_IS_AUTO(msg))
2072 ret = dwc3_core_init_for_resume(dwc);
2076 dwc3_set_prtcap(dwc, dwc->current_dr_role);
2079 if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
2080 dwc3_otg_host_init(dwc);
2081 } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
2082 spin_lock_irqsave(&dwc->lock, flags);
2083 dwc3_gadget_resume(dwc);
2084 spin_unlock_irqrestore(&dwc->lock, flags);
2096 static int dwc3_runtime_checks(struct dwc3 *dwc)
2098 switch (dwc->current_dr_role) {
2099 case DWC3_GCTL_PRTCAP_DEVICE:
2103 case DWC3_GCTL_PRTCAP_HOST:
2112 static int dwc3_runtime_suspend(struct device *dev)
2114 struct dwc3 *dwc = dev_get_drvdata(dev);
2117 if (dwc3_runtime_checks(dwc))
2120 ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
2127 static int dwc3_runtime_resume(struct device *dev)
2129 struct dwc3 *dwc = dev_get_drvdata(dev);
2132 ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
2136 switch (dwc->current_dr_role) {
2137 case DWC3_GCTL_PRTCAP_DEVICE:
2138 dwc3_gadget_process_pending_events(dwc);
2140 case DWC3_GCTL_PRTCAP_HOST:
2146 pm_runtime_mark_last_busy(dev);
2151 static int dwc3_runtime_idle(struct device *dev)
2153 struct dwc3 *dwc = dev_get_drvdata(dev);
2155 switch (dwc->current_dr_role) {
2156 case DWC3_GCTL_PRTCAP_DEVICE:
2157 if (dwc3_runtime_checks(dwc))
2160 case DWC3_GCTL_PRTCAP_HOST:
2166 pm_runtime_mark_last_busy(dev);
2167 pm_runtime_autosuspend(dev);
2171 #endif /* CONFIG_PM */
2173 #ifdef CONFIG_PM_SLEEP
2174 static int dwc3_suspend(struct device *dev)
2176 struct dwc3 *dwc = dev_get_drvdata(dev);
2179 ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
2183 pinctrl_pm_select_sleep_state(dev);
2188 static int dwc3_resume(struct device *dev)
2190 struct dwc3 *dwc = dev_get_drvdata(dev);
2193 pinctrl_pm_select_default_state(dev);
2195 ret = dwc3_resume_common(dwc, PMSG_RESUME);
2199 pm_runtime_disable(dev);
2200 pm_runtime_set_active(dev);
2201 pm_runtime_enable(dev);
2206 static void dwc3_complete(struct device *dev)
2208 struct dwc3 *dwc = dev_get_drvdata(dev);
2211 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST &&
2212 dwc->dis_split_quirk) {
2213 reg = dwc3_readl(dwc->regs, DWC3_GUCTL3);
2214 reg |= DWC3_GUCTL3_SPLITDISABLE;
2215 dwc3_writel(dwc->regs, DWC3_GUCTL3, reg);
2219 #define dwc3_complete NULL
2220 #endif /* CONFIG_PM_SLEEP */
2222 static const struct dev_pm_ops dwc3_dev_pm_ops = {
2223 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
2224 .complete = dwc3_complete,
2225 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
2230 static const struct of_device_id of_dwc3_match[] = {
2232 .compatible = "snps,dwc3"
2235 .compatible = "synopsys,dwc3"
2239 MODULE_DEVICE_TABLE(of, of_dwc3_match);
2244 #define ACPI_ID_INTEL_BSW "808622B7"
2246 static const struct acpi_device_id dwc3_acpi_match[] = {
2247 { ACPI_ID_INTEL_BSW, 0 },
2250 MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
2253 static struct platform_driver dwc3_driver = {
2254 .probe = dwc3_probe,
2255 .remove = dwc3_remove,
2258 .of_match_table = of_match_ptr(of_dwc3_match),
2259 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
2260 .pm = &dwc3_dev_pm_ops,
2264 module_platform_driver(dwc3_driver);
2266 MODULE_ALIAS("platform:dwc3");
2268 MODULE_LICENSE("GPL v2");
2269 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");