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
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
11 #include <linux/clk.h>
12 #include <linux/version.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/interrupt.h>
20 #include <linux/ioport.h>
22 #include <linux/list.h>
23 #include <linux/delay.h>
24 #include <linux/dma-mapping.h>
26 #include <linux/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);
127 mutex_lock(&dwc->mutex);
128 spin_lock_irqsave(&dwc->lock, flags);
129 desired_dr_role = dwc->desired_dr_role;
130 spin_unlock_irqrestore(&dwc->lock, flags);
132 pm_runtime_get_sync(dwc->dev);
134 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
135 dwc3_otg_update(dwc, 0);
137 if (!desired_dr_role)
140 if (desired_dr_role == dwc->current_dr_role)
143 if (desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev)
146 switch (dwc->current_dr_role) {
147 case DWC3_GCTL_PRTCAP_HOST:
150 case DWC3_GCTL_PRTCAP_DEVICE:
151 dwc3_gadget_exit(dwc);
152 dwc3_event_buffers_cleanup(dwc);
154 case DWC3_GCTL_PRTCAP_OTG:
156 spin_lock_irqsave(&dwc->lock, flags);
157 dwc->desired_otg_role = DWC3_OTG_ROLE_IDLE;
158 spin_unlock_irqrestore(&dwc->lock, flags);
159 dwc3_otg_update(dwc, 1);
166 * When current_dr_role is not set, there's no role switching.
167 * Only perform GCTL.CoreSoftReset when there's DRD role switching.
169 if (dwc->current_dr_role && ((DWC3_IP_IS(DWC3) ||
170 DWC3_VER_IS_PRIOR(DWC31, 190A)) &&
171 desired_dr_role != DWC3_GCTL_PRTCAP_OTG)) {
172 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
173 reg |= DWC3_GCTL_CORESOFTRESET;
174 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
177 * Wait for internal clocks to synchronized. DWC_usb31 and
178 * DWC_usb32 may need at least 50ms (less for DWC_usb3). To
179 * keep it consistent across different IPs, let's wait up to
180 * 100ms before clearing GCTL.CORESOFTRESET.
184 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
185 reg &= ~DWC3_GCTL_CORESOFTRESET;
186 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
189 spin_lock_irqsave(&dwc->lock, flags);
191 dwc3_set_prtcap(dwc, desired_dr_role);
193 spin_unlock_irqrestore(&dwc->lock, flags);
195 switch (desired_dr_role) {
196 case DWC3_GCTL_PRTCAP_HOST:
197 ret = dwc3_host_init(dwc);
199 dev_err(dwc->dev, "failed to initialize host\n");
202 otg_set_vbus(dwc->usb2_phy->otg, true);
203 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
204 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
205 if (dwc->dis_split_quirk) {
206 reg = dwc3_readl(dwc->regs, DWC3_GUCTL3);
207 reg |= DWC3_GUCTL3_SPLITDISABLE;
208 dwc3_writel(dwc->regs, DWC3_GUCTL3, reg);
212 case DWC3_GCTL_PRTCAP_DEVICE:
213 dwc3_core_soft_reset(dwc);
215 dwc3_event_buffers_setup(dwc);
218 otg_set_vbus(dwc->usb2_phy->otg, false);
219 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
220 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
222 ret = dwc3_gadget_init(dwc);
224 dev_err(dwc->dev, "failed to initialize peripheral\n");
226 case DWC3_GCTL_PRTCAP_OTG:
228 dwc3_otg_update(dwc, 0);
235 pm_runtime_mark_last_busy(dwc->dev);
236 pm_runtime_put_autosuspend(dwc->dev);
237 mutex_unlock(&dwc->mutex);
240 void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
244 if (dwc->dr_mode != USB_DR_MODE_OTG)
247 spin_lock_irqsave(&dwc->lock, flags);
248 dwc->desired_dr_role = mode;
249 spin_unlock_irqrestore(&dwc->lock, flags);
251 queue_work(system_freezable_wq, &dwc->drd_work);
254 u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
256 struct dwc3 *dwc = dep->dwc;
259 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
260 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
261 DWC3_GDBGFIFOSPACE_TYPE(type));
263 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
265 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
269 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
270 * @dwc: pointer to our context structure
272 int dwc3_core_soft_reset(struct dwc3 *dwc)
278 * We're resetting only the device side because, if we're in host mode,
279 * XHCI driver will reset the host block. If dwc3 was configured for
280 * host-only mode or current role is host, then we can return early.
282 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST)
286 * If the dr_mode is host and the dwc->current_dr_role is not the
287 * corresponding DWC3_GCTL_PRTCAP_HOST, then the dwc3_core_init_mode
288 * isn't executed yet. Ensure the phy is ready before the controller
289 * updates the GCTL.PRTCAPDIR or other settings by soft-resetting
292 * Note: GUSB3PIPECTL[n] and GUSB2PHYCFG[n] are port settings where n
293 * is port index. If this is a multiport host, then we need to reset
296 if (dwc->dr_mode == USB_DR_MODE_HOST) {
300 usb3_port = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
301 usb3_port |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
302 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), usb3_port);
304 usb2_port = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
305 usb2_port |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
306 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), usb2_port);
308 /* Small delay for phy reset assertion */
309 usleep_range(1000, 2000);
311 usb3_port &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
312 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), usb3_port);
314 usb2_port &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
315 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), usb2_port);
317 /* Wait for clock synchronization */
322 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
323 reg |= DWC3_DCTL_CSFTRST;
324 reg &= ~DWC3_DCTL_RUN_STOP;
325 dwc3_gadget_dctl_write_safe(dwc, reg);
328 * For DWC_usb31 controller 1.90a and later, the DCTL.CSFRST bit
329 * is cleared only after all the clocks are synchronized. This can
330 * take a little more than 50ms. Set the polling rate at 20ms
331 * for 10 times instead.
333 if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
337 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
338 if (!(reg & DWC3_DCTL_CSFTRST))
341 if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
347 dev_warn(dwc->dev, "DWC3 controller soft reset failed.\n");
352 * For DWC_usb31 controller 1.80a and prior, once DCTL.CSFRST bit
353 * is cleared, we must wait at least 50ms before accessing the PHY
354 * domain (synchronization delay).
356 if (DWC3_VER_IS_WITHIN(DWC31, ANY, 180A))
363 * dwc3_frame_length_adjustment - Adjusts frame length if required
364 * @dwc3: Pointer to our controller context structure
366 static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
371 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
377 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
378 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
379 if (dft != dwc->fladj) {
380 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
381 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
382 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
387 * dwc3_ref_clk_period - Reference clock period configuration
388 * Default reference clock period depends on hardware
389 * configuration. For systems with reference clock that differs
390 * from the default, this will set clock period in DWC3_GUCTL
392 * @dwc: Pointer to our controller context structure
394 static void dwc3_ref_clk_period(struct dwc3 *dwc)
396 unsigned long period;
403 rate = clk_get_rate(dwc->ref_clk);
406 period = NSEC_PER_SEC / rate;
407 } else if (dwc->ref_clk_per) {
408 period = dwc->ref_clk_per;
409 rate = NSEC_PER_SEC / period;
414 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
415 reg &= ~DWC3_GUCTL_REFCLKPER_MASK;
416 reg |= FIELD_PREP(DWC3_GUCTL_REFCLKPER_MASK, period);
417 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
419 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
423 * The calculation below is
425 * 125000 * (NSEC_PER_SEC / (rate * period) - 1)
427 * but rearranged for fixed-point arithmetic. The division must be
428 * 64-bit because 125000 * NSEC_PER_SEC doesn't fit in 32 bits (and
429 * neither does rate * period).
431 * Note that rate * period ~= NSEC_PER_SECOND, minus the number of
432 * nanoseconds of error caused by the truncation which happened during
433 * the division when calculating rate or period (whichever one was
434 * derived from the other). We first calculate the relative error, then
435 * scale it to units of 8 ppm.
437 fladj = div64_u64(125000ULL * NSEC_PER_SEC, (u64)rate * period);
441 * The documented 240MHz constant is scaled by 2 to get PLS1 as well.
443 decr = 480000000 / rate;
445 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
446 reg &= ~DWC3_GFLADJ_REFCLK_FLADJ_MASK
447 & ~DWC3_GFLADJ_240MHZDECR
448 & ~DWC3_GFLADJ_240MHZDECR_PLS1;
449 reg |= FIELD_PREP(DWC3_GFLADJ_REFCLK_FLADJ_MASK, fladj)
450 | FIELD_PREP(DWC3_GFLADJ_240MHZDECR, decr >> 1)
451 | FIELD_PREP(DWC3_GFLADJ_240MHZDECR_PLS1, decr & 1);
453 if (dwc->gfladj_refclk_lpm_sel)
454 reg |= DWC3_GFLADJ_REFCLK_LPM_SEL;
456 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
460 * dwc3_free_one_event_buffer - Frees one event buffer
461 * @dwc: Pointer to our controller context structure
462 * @evt: Pointer to event buffer to be freed
464 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
465 struct dwc3_event_buffer *evt)
467 dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
471 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
472 * @dwc: Pointer to our controller context structure
473 * @length: size of the event buffer
475 * Returns a pointer to the allocated event buffer structure on success
476 * otherwise ERR_PTR(errno).
478 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
481 struct dwc3_event_buffer *evt;
483 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
485 return ERR_PTR(-ENOMEM);
488 evt->length = length;
489 evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
491 return ERR_PTR(-ENOMEM);
493 evt->buf = dma_alloc_coherent(dwc->sysdev, length,
494 &evt->dma, GFP_KERNEL);
496 return ERR_PTR(-ENOMEM);
502 * dwc3_free_event_buffers - frees all allocated event buffers
503 * @dwc: Pointer to our controller context structure
505 static void dwc3_free_event_buffers(struct dwc3 *dwc)
507 struct dwc3_event_buffer *evt;
511 dwc3_free_one_event_buffer(dwc, evt);
515 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
516 * @dwc: pointer to our controller context structure
517 * @length: size of event buffer
519 * Returns 0 on success otherwise negative errno. In the error case, dwc
520 * may contain some buffers allocated but not all which were requested.
522 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned int length)
524 struct dwc3_event_buffer *evt;
526 evt = dwc3_alloc_one_event_buffer(dwc, length);
528 dev_err(dwc->dev, "can't allocate event buffer\n");
537 * dwc3_event_buffers_setup - setup our allocated event buffers
538 * @dwc: pointer to our controller context structure
540 * Returns 0 on success otherwise negative errno.
542 int dwc3_event_buffers_setup(struct dwc3 *dwc)
544 struct dwc3_event_buffer *evt;
548 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
549 lower_32_bits(evt->dma));
550 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
551 upper_32_bits(evt->dma));
552 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
553 DWC3_GEVNTSIZ_SIZE(evt->length));
554 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
559 void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
561 struct dwc3_event_buffer *evt;
567 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
568 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
569 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
570 | DWC3_GEVNTSIZ_SIZE(0));
571 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
574 static void dwc3_core_num_eps(struct dwc3 *dwc)
576 struct dwc3_hwparams *parms = &dwc->hwparams;
578 dwc->num_eps = DWC3_NUM_EPS(parms);
581 static void dwc3_cache_hwparams(struct dwc3 *dwc)
583 struct dwc3_hwparams *parms = &dwc->hwparams;
585 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
586 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
587 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
588 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
589 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
590 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
591 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
592 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
593 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
595 if (DWC3_IP_IS(DWC32))
596 parms->hwparams9 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS9);
599 static int dwc3_core_ulpi_init(struct dwc3 *dwc)
604 intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
606 if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
607 (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
608 dwc->hsphy_interface &&
609 !strncmp(dwc->hsphy_interface, "ulpi", 4)))
610 ret = dwc3_ulpi_init(dwc);
616 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
617 * @dwc: Pointer to our controller context structure
619 * Returns 0 on success. The USB PHY interfaces are configured but not
620 * initialized. The PHY interfaces and the PHYs get initialized together with
621 * the core in dwc3_core_init.
623 static int dwc3_phy_setup(struct dwc3 *dwc)
625 unsigned int hw_mode;
628 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
630 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
633 * Make sure UX_EXIT_PX is cleared as that causes issues with some
634 * PHYs. Also, this bit is not supposed to be used in normal operation.
636 reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
639 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
640 * to '0' during coreConsultant configuration. So default value
641 * will be '0' when the core is reset. Application needs to set it
642 * to '1' after the core initialization is completed.
644 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
645 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
648 * For DRD controllers, GUSB3PIPECTL.SUSPENDENABLE must be cleared after
649 * power-on reset, and it can be set after core initialization, which is
650 * after device soft-reset during initialization.
652 if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD)
653 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
655 if (dwc->u2ss_inp3_quirk)
656 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
658 if (dwc->dis_rxdet_inp3_quirk)
659 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
661 if (dwc->req_p1p2p3_quirk)
662 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
664 if (dwc->del_p1p2p3_quirk)
665 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
667 if (dwc->del_phy_power_chg_quirk)
668 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
670 if (dwc->lfps_filter_quirk)
671 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
673 if (dwc->rx_detect_poll_quirk)
674 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
676 if (dwc->tx_de_emphasis_quirk)
677 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
679 if (dwc->dis_u3_susphy_quirk)
680 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
682 if (dwc->dis_del_phy_power_chg_quirk)
683 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
685 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
687 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
689 /* Select the HS PHY interface */
690 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
691 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
692 if (dwc->hsphy_interface &&
693 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
694 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
696 } else if (dwc->hsphy_interface &&
697 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
698 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
699 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
701 /* Relying on default value. */
702 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
706 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
711 switch (dwc->hsphy_mode) {
712 case USBPHY_INTERFACE_MODE_UTMI:
713 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
714 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
715 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
716 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
718 case USBPHY_INTERFACE_MODE_UTMIW:
719 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
720 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
721 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
722 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
729 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
730 * '0' during coreConsultant configuration. So default value will
731 * be '0' when the core is reset. Application needs to set it to
732 * '1' after the core initialization is completed.
734 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
735 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
738 * For DRD controllers, GUSB2PHYCFG.SUSPHY must be cleared after
739 * power-on reset, and it can be set after core initialization, which is
740 * after device soft-reset during initialization.
742 if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD)
743 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
745 if (dwc->dis_u2_susphy_quirk)
746 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
748 if (dwc->dis_enblslpm_quirk)
749 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
751 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM;
753 if (dwc->dis_u2_freeclk_exists_quirk || dwc->gfladj_refclk_lpm_sel)
754 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
757 * Some ULPI USB PHY does not support internal VBUS supply, to drive
758 * the CPEN pin requires the configuration of the ULPI DRVVBUSEXTERNAL
759 * bit of OTG_CTRL register. Controller configures the USB2 PHY
760 * ULPIEXTVBUSDRV bit[17] of the GUSB2PHYCFG register to drive vBus
761 * with an external supply.
763 if (dwc->ulpi_ext_vbus_drv)
764 reg |= DWC3_GUSB2PHYCFG_ULPIEXTVBUSDRV;
766 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
771 static int dwc3_phy_init(struct dwc3 *dwc)
775 usb_phy_init(dwc->usb2_phy);
776 usb_phy_init(dwc->usb3_phy);
778 ret = phy_init(dwc->usb2_generic_phy);
780 goto err_shutdown_usb3_phy;
782 ret = phy_init(dwc->usb3_generic_phy);
784 goto err_exit_usb2_phy;
789 phy_exit(dwc->usb2_generic_phy);
790 err_shutdown_usb3_phy:
791 usb_phy_shutdown(dwc->usb3_phy);
792 usb_phy_shutdown(dwc->usb2_phy);
797 static void dwc3_phy_exit(struct dwc3 *dwc)
799 phy_exit(dwc->usb3_generic_phy);
800 phy_exit(dwc->usb2_generic_phy);
802 usb_phy_shutdown(dwc->usb3_phy);
803 usb_phy_shutdown(dwc->usb2_phy);
806 static int dwc3_phy_power_on(struct dwc3 *dwc)
810 usb_phy_set_suspend(dwc->usb2_phy, 0);
811 usb_phy_set_suspend(dwc->usb3_phy, 0);
813 ret = phy_power_on(dwc->usb2_generic_phy);
815 goto err_suspend_usb3_phy;
817 ret = phy_power_on(dwc->usb3_generic_phy);
819 goto err_power_off_usb2_phy;
823 err_power_off_usb2_phy:
824 phy_power_off(dwc->usb2_generic_phy);
825 err_suspend_usb3_phy:
826 usb_phy_set_suspend(dwc->usb3_phy, 1);
827 usb_phy_set_suspend(dwc->usb2_phy, 1);
832 static void dwc3_phy_power_off(struct dwc3 *dwc)
834 phy_power_off(dwc->usb3_generic_phy);
835 phy_power_off(dwc->usb2_generic_phy);
837 usb_phy_set_suspend(dwc->usb3_phy, 1);
838 usb_phy_set_suspend(dwc->usb2_phy, 1);
841 static int dwc3_clk_enable(struct dwc3 *dwc)
845 ret = clk_prepare_enable(dwc->bus_clk);
849 ret = clk_prepare_enable(dwc->ref_clk);
851 goto disable_bus_clk;
853 ret = clk_prepare_enable(dwc->susp_clk);
855 goto disable_ref_clk;
860 clk_disable_unprepare(dwc->ref_clk);
862 clk_disable_unprepare(dwc->bus_clk);
866 static void dwc3_clk_disable(struct dwc3 *dwc)
868 clk_disable_unprepare(dwc->susp_clk);
869 clk_disable_unprepare(dwc->ref_clk);
870 clk_disable_unprepare(dwc->bus_clk);
873 static void dwc3_core_exit(struct dwc3 *dwc)
875 dwc3_event_buffers_cleanup(dwc);
876 dwc3_phy_power_off(dwc);
878 dwc3_clk_disable(dwc);
879 reset_control_assert(dwc->reset);
882 static bool dwc3_core_is_valid(struct dwc3 *dwc)
886 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
887 dwc->ip = DWC3_GSNPS_ID(reg);
889 /* This should read as U3 followed by revision number */
890 if (DWC3_IP_IS(DWC3)) {
892 } else if (DWC3_IP_IS(DWC31) || DWC3_IP_IS(DWC32)) {
893 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
894 dwc->version_type = dwc3_readl(dwc->regs, DWC3_VER_TYPE);
902 static void dwc3_core_setup_global_control(struct dwc3 *dwc)
906 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
907 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
909 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
910 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
912 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
913 * issue which would cause xHCI compliance tests to fail.
915 * Because of that we cannot enable clock gating on such
920 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
923 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
924 dwc->dr_mode == USB_DR_MODE_OTG) &&
925 DWC3_VER_IS_WITHIN(DWC3, 210A, 250A))
926 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
928 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
930 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
932 * REVISIT Enabling this bit so that host-mode hibernation
933 * will work. Device-mode hibernation is not yet implemented.
935 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
942 /* check if current dwc3 is on simulation board */
943 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
944 dev_info(dwc->dev, "Running with FPGA optimizations\n");
948 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
949 "disable_scramble cannot be used on non-FPGA builds\n");
951 if (dwc->disable_scramble_quirk && dwc->is_fpga)
952 reg |= DWC3_GCTL_DISSCRAMBLE;
954 reg &= ~DWC3_GCTL_DISSCRAMBLE;
956 if (dwc->u2exit_lfps_quirk)
957 reg |= DWC3_GCTL_U2EXIT_LFPS;
960 * WORKAROUND: DWC3 revisions <1.90a have a bug
961 * where the device can fail to connect at SuperSpeed
962 * and falls back to high-speed mode which causes
963 * the device to enter a Connect/Disconnect loop
965 if (DWC3_VER_IS_PRIOR(DWC3, 190A))
966 reg |= DWC3_GCTL_U2RSTECN;
968 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
971 static int dwc3_core_get_phy(struct dwc3 *dwc);
972 static int dwc3_core_ulpi_init(struct dwc3 *dwc);
974 /* set global incr burst type configuration registers */
975 static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
977 struct device *dev = dwc->dev;
978 /* incrx_mode : for INCR burst type. */
980 /* incrx_size : for size of INCRX burst. */
988 cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
991 * Handle property "snps,incr-burst-type-adjustment".
992 * Get the number of value from this property:
993 * result <= 0, means this property is not supported.
994 * result = 1, means INCRx burst mode supported.
995 * result > 1, means undefined length burst mode supported.
997 ntype = device_property_count_u32(dev, "snps,incr-burst-type-adjustment");
1001 vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL);
1005 /* Get INCR burst type, and parse it */
1006 ret = device_property_read_u32_array(dev,
1007 "snps,incr-burst-type-adjustment", vals, ntype);
1010 dev_err(dev, "Error to get property\n");
1017 /* INCRX (undefined length) burst mode */
1018 incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
1019 for (i = 1; i < ntype; i++) {
1020 if (vals[i] > incrx_size)
1021 incrx_size = vals[i];
1024 /* INCRX burst mode */
1025 incrx_mode = INCRX_BURST_MODE;
1030 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
1031 cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
1033 cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
1034 switch (incrx_size) {
1036 cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
1039 cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
1042 cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
1045 cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
1048 cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
1051 cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
1054 cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
1059 dev_err(dev, "Invalid property\n");
1063 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
1066 static void dwc3_set_power_down_clk_scale(struct dwc3 *dwc)
1075 * The power down scale field specifies how many suspend_clk
1076 * periods fit into a 16KHz clock period. When performing
1077 * the division, round up the remainder.
1079 * The power down scale value is calculated using the fastest
1080 * frequency of the suspend_clk. If it isn't fixed (but within
1081 * the accuracy requirement), the driver may not know the max
1082 * rate of the suspend_clk, so only update the power down scale
1083 * if the default is less than the calculated value from
1084 * clk_get_rate() or if the default is questionably high
1085 * (3x or more) to be within the requirement.
1087 scale = DIV_ROUND_UP(clk_get_rate(dwc->susp_clk), 16000);
1088 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1089 if ((reg & DWC3_GCTL_PWRDNSCALE_MASK) < DWC3_GCTL_PWRDNSCALE(scale) ||
1090 (reg & DWC3_GCTL_PWRDNSCALE_MASK) > DWC3_GCTL_PWRDNSCALE(scale*3)) {
1091 reg &= ~(DWC3_GCTL_PWRDNSCALE_MASK);
1092 reg |= DWC3_GCTL_PWRDNSCALE(scale);
1093 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1098 * dwc3_core_init - Low-level initialization of DWC3 Core
1099 * @dwc: Pointer to our controller context structure
1101 * Returns 0 on success otherwise negative errno.
1103 static int dwc3_core_init(struct dwc3 *dwc)
1105 unsigned int hw_mode;
1109 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
1112 * Write Linux Version Code to our GUID register so it's easy to figure
1113 * out which kernel version a bug was found.
1115 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
1117 ret = dwc3_phy_setup(dwc);
1121 if (!dwc->ulpi_ready) {
1122 ret = dwc3_core_ulpi_init(dwc);
1124 if (ret == -ETIMEDOUT) {
1125 dwc3_core_soft_reset(dwc);
1126 ret = -EPROBE_DEFER;
1130 dwc->ulpi_ready = true;
1133 if (!dwc->phys_ready) {
1134 ret = dwc3_core_get_phy(dwc);
1137 dwc->phys_ready = true;
1140 ret = dwc3_phy_init(dwc);
1144 ret = dwc3_core_soft_reset(dwc);
1148 if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD &&
1149 !DWC3_VER_IS_WITHIN(DWC3, ANY, 194A)) {
1150 if (!dwc->dis_u3_susphy_quirk) {
1151 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1152 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1153 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1156 if (!dwc->dis_u2_susphy_quirk) {
1157 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1158 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1159 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1163 dwc3_core_setup_global_control(dwc);
1164 dwc3_core_num_eps(dwc);
1166 /* Set power down scale of suspend_clk */
1167 dwc3_set_power_down_clk_scale(dwc);
1169 /* Adjust Frame Length */
1170 dwc3_frame_length_adjustment(dwc);
1172 /* Adjust Reference Clock Period */
1173 dwc3_ref_clk_period(dwc);
1175 dwc3_set_incr_burst_type(dwc);
1177 ret = dwc3_phy_power_on(dwc);
1181 ret = dwc3_event_buffers_setup(dwc);
1183 dev_err(dwc->dev, "failed to setup event buffers\n");
1184 goto err_power_off_phy;
1188 * ENDXFER polling is available on version 3.10a and later of
1189 * the DWC_usb3 controller. It is NOT available in the
1190 * DWC_usb31 controller.
1192 if (DWC3_VER_IS_WITHIN(DWC3, 310A, ANY)) {
1193 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
1194 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
1195 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
1199 * When configured in HOST mode, after issuing U3/L2 exit controller
1200 * fails to send proper CRC checksum in CRC5 feild. Because of this
1201 * behaviour Transaction Error is generated, resulting in reset and
1202 * re-enumeration of usb device attached. All the termsel, xcvrsel,
1203 * opmode becomes 0 during end of resume. Enabling bit 10 of GUCTL1
1204 * will correct this problem. This option is to support certain
1207 if (dwc->resume_hs_terminations) {
1208 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1209 reg |= DWC3_GUCTL1_RESUME_OPMODE_HS_HOST;
1210 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1213 if (!DWC3_VER_IS_PRIOR(DWC3, 250A)) {
1214 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1217 * Enable hardware control of sending remote wakeup
1218 * in HS when the device is in the L1 state.
1220 if (!DWC3_VER_IS_PRIOR(DWC3, 290A))
1221 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
1224 * Decouple USB 2.0 L1 & L2 events which will allow for
1225 * gadget driver to only receive U3/L2 suspend & wakeup
1226 * events and prevent the more frequent L1 LPM transitions
1227 * from interrupting the driver.
1229 if (!DWC3_VER_IS_PRIOR(DWC3, 300A))
1230 reg |= DWC3_GUCTL1_DEV_DECOUPLE_L1L2_EVT;
1232 if (dwc->dis_tx_ipgap_linecheck_quirk)
1233 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
1235 if (dwc->parkmode_disable_ss_quirk)
1236 reg |= DWC3_GUCTL1_PARKMODE_DISABLE_SS;
1238 if (dwc->parkmode_disable_hs_quirk)
1239 reg |= DWC3_GUCTL1_PARKMODE_DISABLE_HS;
1241 if (DWC3_VER_IS_WITHIN(DWC3, 290A, ANY) &&
1242 (dwc->maximum_speed == USB_SPEED_HIGH ||
1243 dwc->maximum_speed == USB_SPEED_FULL))
1244 reg |= DWC3_GUCTL1_DEV_FORCE_20_CLK_FOR_30_CLK;
1246 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1250 * Must config both number of packets and max burst settings to enable
1251 * RX and/or TX threshold.
1253 if (!DWC3_IP_IS(DWC3) && dwc->dr_mode == USB_DR_MODE_HOST) {
1254 u8 rx_thr_num = dwc->rx_thr_num_pkt_prd;
1255 u8 rx_maxburst = dwc->rx_max_burst_prd;
1256 u8 tx_thr_num = dwc->tx_thr_num_pkt_prd;
1257 u8 tx_maxburst = dwc->tx_max_burst_prd;
1259 if (rx_thr_num && rx_maxburst) {
1260 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1261 reg |= DWC31_RXTHRNUMPKTSEL_PRD;
1263 reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
1264 reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
1266 reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
1267 reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
1269 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1272 if (tx_thr_num && tx_maxburst) {
1273 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1274 reg |= DWC31_TXTHRNUMPKTSEL_PRD;
1276 reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
1277 reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
1279 reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
1280 reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
1282 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1289 dwc3_phy_power_off(dwc);
1293 dwc3_ulpi_exit(dwc);
1298 static int dwc3_core_get_phy(struct dwc3 *dwc)
1300 struct device *dev = dwc->dev;
1301 struct device_node *node = dev->of_node;
1305 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
1306 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
1308 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1309 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
1312 if (IS_ERR(dwc->usb2_phy)) {
1313 ret = PTR_ERR(dwc->usb2_phy);
1314 if (ret == -ENXIO || ret == -ENODEV)
1315 dwc->usb2_phy = NULL;
1317 return dev_err_probe(dev, ret, "no usb2 phy configured\n");
1320 if (IS_ERR(dwc->usb3_phy)) {
1321 ret = PTR_ERR(dwc->usb3_phy);
1322 if (ret == -ENXIO || ret == -ENODEV)
1323 dwc->usb3_phy = NULL;
1325 return dev_err_probe(dev, ret, "no usb3 phy configured\n");
1328 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
1329 if (IS_ERR(dwc->usb2_generic_phy)) {
1330 ret = PTR_ERR(dwc->usb2_generic_phy);
1331 if (ret == -ENOSYS || ret == -ENODEV)
1332 dwc->usb2_generic_phy = NULL;
1334 return dev_err_probe(dev, ret, "no usb2 phy configured\n");
1337 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
1338 if (IS_ERR(dwc->usb3_generic_phy)) {
1339 ret = PTR_ERR(dwc->usb3_generic_phy);
1340 if (ret == -ENOSYS || ret == -ENODEV)
1341 dwc->usb3_generic_phy = NULL;
1343 return dev_err_probe(dev, ret, "no usb3 phy configured\n");
1349 static int dwc3_core_init_mode(struct dwc3 *dwc)
1351 struct device *dev = dwc->dev;
1354 switch (dwc->dr_mode) {
1355 case USB_DR_MODE_PERIPHERAL:
1356 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1359 otg_set_vbus(dwc->usb2_phy->otg, false);
1360 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
1361 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
1363 ret = dwc3_gadget_init(dwc);
1365 return dev_err_probe(dev, ret, "failed to initialize gadget\n");
1367 case USB_DR_MODE_HOST:
1368 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1371 otg_set_vbus(dwc->usb2_phy->otg, true);
1372 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
1373 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
1375 ret = dwc3_host_init(dwc);
1377 return dev_err_probe(dev, ret, "failed to initialize host\n");
1379 case USB_DR_MODE_OTG:
1380 INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
1381 ret = dwc3_drd_init(dwc);
1383 return dev_err_probe(dev, ret, "failed to initialize dual-role\n");
1386 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1393 static void dwc3_core_exit_mode(struct dwc3 *dwc)
1395 switch (dwc->dr_mode) {
1396 case USB_DR_MODE_PERIPHERAL:
1397 dwc3_gadget_exit(dwc);
1399 case USB_DR_MODE_HOST:
1400 dwc3_host_exit(dwc);
1402 case USB_DR_MODE_OTG:
1410 /* de-assert DRVVBUS for HOST and OTG mode */
1411 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1414 static void dwc3_get_properties(struct dwc3 *dwc)
1416 struct device *dev = dwc->dev;
1417 u8 lpm_nyet_threshold;
1420 u8 rx_thr_num_pkt_prd = 0;
1421 u8 rx_max_burst_prd = 0;
1422 u8 tx_thr_num_pkt_prd = 0;
1423 u8 tx_max_burst_prd = 0;
1424 u8 tx_fifo_resize_max_num;
1425 const char *usb_psy_name;
1428 /* default to highest possible threshold */
1429 lpm_nyet_threshold = 0xf;
1431 /* default to -3.5dB de-emphasis */
1435 * default to assert utmi_sleep_n and use maximum allowed HIRD
1436 * threshold value of 0b1100
1438 hird_threshold = 12;
1441 * default to a TXFIFO size large enough to fit 6 max packets. This
1442 * allows for systems with larger bus latencies to have some headroom
1443 * for endpoints that have a large bMaxBurst value.
1445 tx_fifo_resize_max_num = 6;
1447 dwc->maximum_speed = usb_get_maximum_speed(dev);
1448 dwc->max_ssp_rate = usb_get_maximum_ssp_rate(dev);
1449 dwc->dr_mode = usb_get_dr_mode(dev);
1450 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
1452 dwc->sysdev_is_parent = device_property_read_bool(dev,
1453 "linux,sysdev_is_parent");
1454 if (dwc->sysdev_is_parent)
1455 dwc->sysdev = dwc->dev->parent;
1457 dwc->sysdev = dwc->dev;
1459 ret = device_property_read_string(dev, "usb-psy-name", &usb_psy_name);
1461 dwc->usb_psy = power_supply_get_by_name(usb_psy_name);
1463 dev_err(dev, "couldn't get usb power supply\n");
1466 dwc->has_lpm_erratum = device_property_read_bool(dev,
1467 "snps,has-lpm-erratum");
1468 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
1469 &lpm_nyet_threshold);
1470 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
1471 "snps,is-utmi-l1-suspend");
1472 device_property_read_u8(dev, "snps,hird-threshold",
1474 dwc->dis_start_transfer_quirk = device_property_read_bool(dev,
1475 "snps,dis-start-transfer-quirk");
1476 dwc->usb3_lpm_capable = device_property_read_bool(dev,
1477 "snps,usb3_lpm_capable");
1478 dwc->usb2_lpm_disable = device_property_read_bool(dev,
1479 "snps,usb2-lpm-disable");
1480 dwc->usb2_gadget_lpm_disable = device_property_read_bool(dev,
1481 "snps,usb2-gadget-lpm-disable");
1482 device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
1483 &rx_thr_num_pkt_prd);
1484 device_property_read_u8(dev, "snps,rx-max-burst-prd",
1486 device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
1487 &tx_thr_num_pkt_prd);
1488 device_property_read_u8(dev, "snps,tx-max-burst-prd",
1490 dwc->do_fifo_resize = device_property_read_bool(dev,
1492 if (dwc->do_fifo_resize)
1493 device_property_read_u8(dev, "tx-fifo-max-num",
1494 &tx_fifo_resize_max_num);
1496 dwc->disable_scramble_quirk = device_property_read_bool(dev,
1497 "snps,disable_scramble_quirk");
1498 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
1499 "snps,u2exit_lfps_quirk");
1500 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
1501 "snps,u2ss_inp3_quirk");
1502 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
1503 "snps,req_p1p2p3_quirk");
1504 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
1505 "snps,del_p1p2p3_quirk");
1506 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
1507 "snps,del_phy_power_chg_quirk");
1508 dwc->lfps_filter_quirk = device_property_read_bool(dev,
1509 "snps,lfps_filter_quirk");
1510 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
1511 "snps,rx_detect_poll_quirk");
1512 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
1513 "snps,dis_u3_susphy_quirk");
1514 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
1515 "snps,dis_u2_susphy_quirk");
1516 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1517 "snps,dis_enblslpm_quirk");
1518 dwc->dis_u1_entry_quirk = device_property_read_bool(dev,
1519 "snps,dis-u1-entry-quirk");
1520 dwc->dis_u2_entry_quirk = device_property_read_bool(dev,
1521 "snps,dis-u2-entry-quirk");
1522 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1523 "snps,dis_rxdet_inp3_quirk");
1524 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1525 "snps,dis-u2-freeclk-exists-quirk");
1526 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1527 "snps,dis-del-phy-power-chg-quirk");
1528 dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1529 "snps,dis-tx-ipgap-linecheck-quirk");
1530 dwc->resume_hs_terminations = device_property_read_bool(dev,
1531 "snps,resume-hs-terminations");
1532 dwc->ulpi_ext_vbus_drv = device_property_read_bool(dev,
1533 "snps,ulpi-ext-vbus-drv");
1534 dwc->parkmode_disable_ss_quirk = device_property_read_bool(dev,
1535 "snps,parkmode-disable-ss-quirk");
1536 dwc->parkmode_disable_hs_quirk = device_property_read_bool(dev,
1537 "snps,parkmode-disable-hs-quirk");
1538 dwc->gfladj_refclk_lpm_sel = device_property_read_bool(dev,
1539 "snps,gfladj-refclk-lpm-sel-quirk");
1541 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
1542 "snps,tx_de_emphasis_quirk");
1543 device_property_read_u8(dev, "snps,tx_de_emphasis",
1545 device_property_read_string(dev, "snps,hsphy_interface",
1546 &dwc->hsphy_interface);
1547 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
1549 device_property_read_u32(dev, "snps,ref-clock-period-ns",
1552 dwc->dis_metastability_quirk = device_property_read_bool(dev,
1553 "snps,dis_metastability_quirk");
1555 dwc->dis_split_quirk = device_property_read_bool(dev,
1556 "snps,dis-split-quirk");
1558 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1559 dwc->tx_de_emphasis = tx_de_emphasis;
1561 dwc->hird_threshold = hird_threshold;
1563 dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd;
1564 dwc->rx_max_burst_prd = rx_max_burst_prd;
1566 dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd;
1567 dwc->tx_max_burst_prd = tx_max_burst_prd;
1569 dwc->imod_interval = 0;
1571 dwc->tx_fifo_resize_max_num = tx_fifo_resize_max_num;
1574 /* check whether the core supports IMOD */
1575 bool dwc3_has_imod(struct dwc3 *dwc)
1577 return DWC3_VER_IS_WITHIN(DWC3, 300A, ANY) ||
1578 DWC3_VER_IS_WITHIN(DWC31, 120A, ANY) ||
1582 static void dwc3_check_params(struct dwc3 *dwc)
1584 struct device *dev = dwc->dev;
1585 unsigned int hwparam_gen =
1586 DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3);
1588 /* Check for proper value of imod_interval */
1589 if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
1590 dev_warn(dwc->dev, "Interrupt moderation not supported\n");
1591 dwc->imod_interval = 0;
1595 * Workaround for STAR 9000961433 which affects only version
1596 * 3.00a of the DWC_usb3 core. This prevents the controller
1597 * interrupt from being masked while handling events. IMOD
1598 * allows us to work around this issue. Enable it for the
1601 if (!dwc->imod_interval &&
1602 DWC3_VER_IS(DWC3, 300A))
1603 dwc->imod_interval = 1;
1605 /* Check the maximum_speed parameter */
1606 switch (dwc->maximum_speed) {
1607 case USB_SPEED_FULL:
1608 case USB_SPEED_HIGH:
1610 case USB_SPEED_SUPER:
1611 if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS)
1612 dev_warn(dev, "UDC doesn't support Gen 1\n");
1614 case USB_SPEED_SUPER_PLUS:
1615 if ((DWC3_IP_IS(DWC32) &&
1616 hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS) ||
1617 (!DWC3_IP_IS(DWC32) &&
1618 hwparam_gen != DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1619 dev_warn(dev, "UDC doesn't support SSP\n");
1622 dev_err(dev, "invalid maximum_speed parameter %d\n",
1623 dwc->maximum_speed);
1625 case USB_SPEED_UNKNOWN:
1626 switch (hwparam_gen) {
1627 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
1628 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1630 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
1631 if (DWC3_IP_IS(DWC32))
1632 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1634 dwc->maximum_speed = USB_SPEED_SUPER;
1636 case DWC3_GHWPARAMS3_SSPHY_IFC_DIS:
1637 dwc->maximum_speed = USB_SPEED_HIGH;
1640 dwc->maximum_speed = USB_SPEED_SUPER;
1647 * Currently the controller does not have visibility into the HW
1648 * parameter to determine the maximum number of lanes the HW supports.
1649 * If the number of lanes is not specified in the device property, then
1650 * set the default to support dual-lane for DWC_usb32 and single-lane
1651 * for DWC_usb31 for super-speed-plus.
1653 if (dwc->maximum_speed == USB_SPEED_SUPER_PLUS) {
1654 switch (dwc->max_ssp_rate) {
1655 case USB_SSP_GEN_2x1:
1656 if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_GEN1)
1657 dev_warn(dev, "UDC only supports Gen 1\n");
1659 case USB_SSP_GEN_1x2:
1660 case USB_SSP_GEN_2x2:
1661 if (DWC3_IP_IS(DWC31))
1662 dev_warn(dev, "UDC only supports single lane\n");
1664 case USB_SSP_GEN_UNKNOWN:
1666 switch (hwparam_gen) {
1667 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
1668 if (DWC3_IP_IS(DWC32))
1669 dwc->max_ssp_rate = USB_SSP_GEN_2x2;
1671 dwc->max_ssp_rate = USB_SSP_GEN_2x1;
1673 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
1674 if (DWC3_IP_IS(DWC32))
1675 dwc->max_ssp_rate = USB_SSP_GEN_1x2;
1683 static struct extcon_dev *dwc3_get_extcon(struct dwc3 *dwc)
1685 struct device *dev = dwc->dev;
1686 struct device_node *np_phy;
1687 struct extcon_dev *edev = NULL;
1690 if (device_property_read_bool(dev, "extcon"))
1691 return extcon_get_edev_by_phandle(dev, 0);
1694 * Device tree platforms should get extcon via phandle.
1695 * On ACPI platforms, we get the name from a device property.
1696 * This device property is for kernel internal use only and
1697 * is expected to be set by the glue code.
1699 if (device_property_read_string(dev, "linux,extcon-name", &name) == 0)
1700 return extcon_get_extcon_dev(name);
1703 * Check explicitly if "usb-role-switch" is used since
1704 * extcon_find_edev_by_node() can not be used to check the absence of
1705 * an extcon device. In the absence of an device it will always return
1708 if (IS_ENABLED(CONFIG_USB_ROLE_SWITCH) &&
1709 device_property_read_bool(dev, "usb-role-switch"))
1713 * Try to get an extcon device from the USB PHY controller's "port"
1714 * node. Check if it has the "port" node first, to avoid printing the
1715 * error message from underlying code, as it's a valid case: extcon
1716 * device (and "port" node) may be missing in case of "usb-role-switch"
1719 np_phy = of_parse_phandle(dev->of_node, "phys", 0);
1720 if (of_graph_is_present(np_phy)) {
1721 struct device_node *np_conn;
1723 np_conn = of_graph_get_remote_node(np_phy, -1, -1);
1725 edev = extcon_find_edev_by_node(np_conn);
1726 of_node_put(np_conn);
1728 of_node_put(np_phy);
1733 static int dwc3_get_clocks(struct dwc3 *dwc)
1735 struct device *dev = dwc->dev;
1741 * Clocks are optional, but new DT platforms should support all clocks
1742 * as required by the DT-binding.
1743 * Some devices have different clock names in legacy device trees,
1744 * check for them to retain backwards compatibility.
1746 dwc->bus_clk = devm_clk_get_optional(dev, "bus_early");
1747 if (IS_ERR(dwc->bus_clk)) {
1748 return dev_err_probe(dev, PTR_ERR(dwc->bus_clk),
1749 "could not get bus clock\n");
1752 if (dwc->bus_clk == NULL) {
1753 dwc->bus_clk = devm_clk_get_optional(dev, "bus_clk");
1754 if (IS_ERR(dwc->bus_clk)) {
1755 return dev_err_probe(dev, PTR_ERR(dwc->bus_clk),
1756 "could not get bus clock\n");
1760 dwc->ref_clk = devm_clk_get_optional(dev, "ref");
1761 if (IS_ERR(dwc->ref_clk)) {
1762 return dev_err_probe(dev, PTR_ERR(dwc->ref_clk),
1763 "could not get ref clock\n");
1766 if (dwc->ref_clk == NULL) {
1767 dwc->ref_clk = devm_clk_get_optional(dev, "ref_clk");
1768 if (IS_ERR(dwc->ref_clk)) {
1769 return dev_err_probe(dev, PTR_ERR(dwc->ref_clk),
1770 "could not get ref clock\n");
1774 dwc->susp_clk = devm_clk_get_optional(dev, "suspend");
1775 if (IS_ERR(dwc->susp_clk)) {
1776 return dev_err_probe(dev, PTR_ERR(dwc->susp_clk),
1777 "could not get suspend clock\n");
1780 if (dwc->susp_clk == NULL) {
1781 dwc->susp_clk = devm_clk_get_optional(dev, "suspend_clk");
1782 if (IS_ERR(dwc->susp_clk)) {
1783 return dev_err_probe(dev, PTR_ERR(dwc->susp_clk),
1784 "could not get suspend clock\n");
1791 static int dwc3_probe(struct platform_device *pdev)
1793 struct device *dev = &pdev->dev;
1794 struct resource *res, dwc_res;
1799 dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
1805 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1807 dev_err(dev, "missing memory resource\n");
1811 dwc->xhci_resources[0].start = res->start;
1812 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1814 dwc->xhci_resources[0].flags = res->flags;
1815 dwc->xhci_resources[0].name = res->name;
1818 * Request memory region but exclude xHCI regs,
1819 * since it will be requested by the xhci-plat driver.
1822 dwc_res.start += DWC3_GLOBALS_REGS_START;
1825 struct device_node *parent = of_get_parent(dev->of_node);
1827 if (of_device_is_compatible(parent, "realtek,rtd-dwc3")) {
1828 dwc_res.start -= DWC3_GLOBALS_REGS_START;
1829 dwc_res.start += DWC3_RTK_RTD_GLOBALS_REGS_START;
1832 of_node_put(parent);
1835 regs = devm_ioremap_resource(dev, &dwc_res);
1837 return PTR_ERR(regs);
1840 dwc->regs_size = resource_size(&dwc_res);
1842 dwc3_get_properties(dwc);
1844 dwc->reset = devm_reset_control_array_get_optional_shared(dev);
1845 if (IS_ERR(dwc->reset)) {
1846 ret = PTR_ERR(dwc->reset);
1850 ret = dwc3_get_clocks(dwc);
1854 ret = reset_control_deassert(dwc->reset);
1858 ret = dwc3_clk_enable(dwc);
1860 goto err_assert_reset;
1862 if (!dwc3_core_is_valid(dwc)) {
1863 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
1865 goto err_disable_clks;
1868 platform_set_drvdata(pdev, dwc);
1869 dwc3_cache_hwparams(dwc);
1871 if (!dwc->sysdev_is_parent &&
1872 DWC3_GHWPARAMS0_AWIDTH(dwc->hwparams.hwparams0) == 64) {
1873 ret = dma_set_mask_and_coherent(dwc->sysdev, DMA_BIT_MASK(64));
1875 goto err_disable_clks;
1878 spin_lock_init(&dwc->lock);
1879 mutex_init(&dwc->mutex);
1881 pm_runtime_get_noresume(dev);
1882 pm_runtime_set_active(dev);
1883 pm_runtime_use_autosuspend(dev);
1884 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
1885 pm_runtime_enable(dev);
1887 pm_runtime_forbid(dev);
1889 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1891 dev_err(dwc->dev, "failed to allocate event buffers\n");
1896 dwc->edev = dwc3_get_extcon(dwc);
1897 if (IS_ERR(dwc->edev)) {
1898 ret = dev_err_probe(dwc->dev, PTR_ERR(dwc->edev), "failed to get extcon\n");
1899 goto err_free_event_buffers;
1902 ret = dwc3_get_dr_mode(dwc);
1904 goto err_free_event_buffers;
1906 ret = dwc3_core_init(dwc);
1908 dev_err_probe(dev, ret, "failed to initialize core\n");
1909 goto err_free_event_buffers;
1912 dwc3_check_params(dwc);
1913 dwc3_debugfs_init(dwc);
1915 ret = dwc3_core_init_mode(dwc);
1917 goto err_exit_debugfs;
1919 pm_runtime_put(dev);
1924 dwc3_debugfs_exit(dwc);
1925 dwc3_event_buffers_cleanup(dwc);
1926 dwc3_phy_power_off(dwc);
1928 dwc3_ulpi_exit(dwc);
1929 err_free_event_buffers:
1930 dwc3_free_event_buffers(dwc);
1932 pm_runtime_allow(dev);
1933 pm_runtime_disable(dev);
1934 pm_runtime_dont_use_autosuspend(dev);
1935 pm_runtime_set_suspended(dev);
1936 pm_runtime_put_noidle(dev);
1938 dwc3_clk_disable(dwc);
1940 reset_control_assert(dwc->reset);
1943 power_supply_put(dwc->usb_psy);
1948 static void dwc3_remove(struct platform_device *pdev)
1950 struct dwc3 *dwc = platform_get_drvdata(pdev);
1952 pm_runtime_get_sync(&pdev->dev);
1954 dwc3_core_exit_mode(dwc);
1955 dwc3_debugfs_exit(dwc);
1957 dwc3_core_exit(dwc);
1958 dwc3_ulpi_exit(dwc);
1960 pm_runtime_allow(&pdev->dev);
1961 pm_runtime_disable(&pdev->dev);
1962 pm_runtime_dont_use_autosuspend(&pdev->dev);
1963 pm_runtime_put_noidle(&pdev->dev);
1965 * HACK: Clear the driver data, which is currently accessed by parent
1966 * glue drivers, before allowing the parent to suspend.
1968 platform_set_drvdata(pdev, NULL);
1969 pm_runtime_set_suspended(&pdev->dev);
1971 dwc3_free_event_buffers(dwc);
1974 power_supply_put(dwc->usb_psy);
1978 static int dwc3_core_init_for_resume(struct dwc3 *dwc)
1982 ret = reset_control_deassert(dwc->reset);
1986 ret = dwc3_clk_enable(dwc);
1990 ret = dwc3_core_init(dwc);
1997 dwc3_clk_disable(dwc);
1999 reset_control_assert(dwc->reset);
2004 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
2006 unsigned long flags;
2009 switch (dwc->current_dr_role) {
2010 case DWC3_GCTL_PRTCAP_DEVICE:
2011 if (pm_runtime_suspended(dwc->dev))
2013 dwc3_gadget_suspend(dwc);
2014 synchronize_irq(dwc->irq_gadget);
2015 dwc3_core_exit(dwc);
2017 case DWC3_GCTL_PRTCAP_HOST:
2018 if (!PMSG_IS_AUTO(msg) && !device_may_wakeup(dwc->dev)) {
2019 dwc3_core_exit(dwc);
2023 /* Let controller to suspend HSPHY before PHY driver suspends */
2024 if (dwc->dis_u2_susphy_quirk ||
2025 dwc->dis_enblslpm_quirk) {
2026 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2027 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
2028 DWC3_GUSB2PHYCFG_SUSPHY;
2029 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2031 /* Give some time for USB2 PHY to suspend */
2032 usleep_range(5000, 6000);
2035 phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
2036 phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
2038 case DWC3_GCTL_PRTCAP_OTG:
2039 /* do nothing during runtime_suspend */
2040 if (PMSG_IS_AUTO(msg))
2043 if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
2044 spin_lock_irqsave(&dwc->lock, flags);
2045 dwc3_gadget_suspend(dwc);
2046 spin_unlock_irqrestore(&dwc->lock, flags);
2047 synchronize_irq(dwc->irq_gadget);
2051 dwc3_core_exit(dwc);
2061 static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
2063 unsigned long flags;
2067 switch (dwc->current_dr_role) {
2068 case DWC3_GCTL_PRTCAP_DEVICE:
2069 ret = dwc3_core_init_for_resume(dwc);
2073 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
2074 dwc3_gadget_resume(dwc);
2076 case DWC3_GCTL_PRTCAP_HOST:
2077 if (!PMSG_IS_AUTO(msg) && !device_may_wakeup(dwc->dev)) {
2078 ret = dwc3_core_init_for_resume(dwc);
2081 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
2084 /* Restore GUSB2PHYCFG bits that were modified in suspend */
2085 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2086 if (dwc->dis_u2_susphy_quirk)
2087 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
2089 if (dwc->dis_enblslpm_quirk)
2090 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
2092 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2094 phy_pm_runtime_get_sync(dwc->usb2_generic_phy);
2095 phy_pm_runtime_get_sync(dwc->usb3_generic_phy);
2097 case DWC3_GCTL_PRTCAP_OTG:
2098 /* nothing to do on runtime_resume */
2099 if (PMSG_IS_AUTO(msg))
2102 ret = dwc3_core_init_for_resume(dwc);
2106 dwc3_set_prtcap(dwc, dwc->current_dr_role);
2109 if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
2110 dwc3_otg_host_init(dwc);
2111 } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
2112 spin_lock_irqsave(&dwc->lock, flags);
2113 dwc3_gadget_resume(dwc);
2114 spin_unlock_irqrestore(&dwc->lock, flags);
2126 static int dwc3_runtime_checks(struct dwc3 *dwc)
2128 switch (dwc->current_dr_role) {
2129 case DWC3_GCTL_PRTCAP_DEVICE:
2133 case DWC3_GCTL_PRTCAP_HOST:
2142 static int dwc3_runtime_suspend(struct device *dev)
2144 struct dwc3 *dwc = dev_get_drvdata(dev);
2147 if (dwc3_runtime_checks(dwc))
2150 ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
2157 static int dwc3_runtime_resume(struct device *dev)
2159 struct dwc3 *dwc = dev_get_drvdata(dev);
2162 ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
2166 switch (dwc->current_dr_role) {
2167 case DWC3_GCTL_PRTCAP_DEVICE:
2168 dwc3_gadget_process_pending_events(dwc);
2170 case DWC3_GCTL_PRTCAP_HOST:
2176 pm_runtime_mark_last_busy(dev);
2181 static int dwc3_runtime_idle(struct device *dev)
2183 struct dwc3 *dwc = dev_get_drvdata(dev);
2185 switch (dwc->current_dr_role) {
2186 case DWC3_GCTL_PRTCAP_DEVICE:
2187 if (dwc3_runtime_checks(dwc))
2190 case DWC3_GCTL_PRTCAP_HOST:
2196 pm_runtime_mark_last_busy(dev);
2197 pm_runtime_autosuspend(dev);
2201 #endif /* CONFIG_PM */
2203 #ifdef CONFIG_PM_SLEEP
2204 static int dwc3_suspend(struct device *dev)
2206 struct dwc3 *dwc = dev_get_drvdata(dev);
2209 ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
2213 pinctrl_pm_select_sleep_state(dev);
2218 static int dwc3_resume(struct device *dev)
2220 struct dwc3 *dwc = dev_get_drvdata(dev);
2223 pinctrl_pm_select_default_state(dev);
2225 ret = dwc3_resume_common(dwc, PMSG_RESUME);
2229 pm_runtime_disable(dev);
2230 pm_runtime_set_active(dev);
2231 pm_runtime_enable(dev);
2236 static void dwc3_complete(struct device *dev)
2238 struct dwc3 *dwc = dev_get_drvdata(dev);
2241 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST &&
2242 dwc->dis_split_quirk) {
2243 reg = dwc3_readl(dwc->regs, DWC3_GUCTL3);
2244 reg |= DWC3_GUCTL3_SPLITDISABLE;
2245 dwc3_writel(dwc->regs, DWC3_GUCTL3, reg);
2249 #define dwc3_complete NULL
2250 #endif /* CONFIG_PM_SLEEP */
2252 static const struct dev_pm_ops dwc3_dev_pm_ops = {
2253 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
2254 .complete = dwc3_complete,
2255 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
2260 static const struct of_device_id of_dwc3_match[] = {
2262 .compatible = "snps,dwc3"
2265 .compatible = "synopsys,dwc3"
2269 MODULE_DEVICE_TABLE(of, of_dwc3_match);
2274 #define ACPI_ID_INTEL_BSW "808622B7"
2276 static const struct acpi_device_id dwc3_acpi_match[] = {
2277 { ACPI_ID_INTEL_BSW, 0 },
2280 MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
2283 static struct platform_driver dwc3_driver = {
2284 .probe = dwc3_probe,
2285 .remove_new = dwc3_remove,
2288 .of_match_table = of_match_ptr(of_dwc3_match),
2289 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
2290 .pm = &dwc3_dev_pm_ops,
2294 module_platform_driver(dwc3_driver);
2296 MODULE_ALIAS("platform:dwc3");
2297 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
2298 MODULE_LICENSE("GPL v2");
2299 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");