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>
42 #include "../host/xhci-ext-caps.h"
44 #define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
47 * dwc3_get_dr_mode - Validates and sets dr_mode
48 * @dwc: pointer to our context structure
50 static int dwc3_get_dr_mode(struct dwc3 *dwc)
52 enum usb_dr_mode mode;
53 struct device *dev = dwc->dev;
56 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
57 dwc->dr_mode = USB_DR_MODE_OTG;
60 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
63 case DWC3_GHWPARAMS0_MODE_GADGET:
64 if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) {
66 "Controller does not support host mode.\n");
69 mode = USB_DR_MODE_PERIPHERAL;
71 case DWC3_GHWPARAMS0_MODE_HOST:
72 if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
74 "Controller does not support device mode.\n");
77 mode = USB_DR_MODE_HOST;
80 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
81 mode = USB_DR_MODE_HOST;
82 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
83 mode = USB_DR_MODE_PERIPHERAL;
86 * DWC_usb31 and DWC_usb3 v3.30a and higher do not support OTG
87 * mode. If the controller supports DRD but the dr_mode is not
88 * specified or set to OTG, then set the mode to peripheral.
90 if (mode == USB_DR_MODE_OTG && !dwc->edev &&
91 (!IS_ENABLED(CONFIG_USB_ROLE_SWITCH) ||
92 !device_property_read_bool(dwc->dev, "usb-role-switch")) &&
93 !DWC3_VER_IS_PRIOR(DWC3, 330A))
94 mode = USB_DR_MODE_PERIPHERAL;
97 if (mode != dwc->dr_mode) {
99 "Configuration mismatch. dr_mode forced to %s\n",
100 mode == USB_DR_MODE_HOST ? "host" : "gadget");
108 void dwc3_enable_susphy(struct dwc3 *dwc, bool enable)
112 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
113 if (enable && !dwc->dis_u3_susphy_quirk)
114 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
116 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
118 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
120 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
121 if (enable && !dwc->dis_u2_susphy_quirk)
122 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
124 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
126 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
129 void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
133 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
134 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
135 reg |= DWC3_GCTL_PRTCAPDIR(mode);
136 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
138 dwc->current_dr_role = mode;
141 static void __dwc3_set_mode(struct work_struct *work)
143 struct dwc3 *dwc = work_to_dwc(work);
150 mutex_lock(&dwc->mutex);
151 spin_lock_irqsave(&dwc->lock, flags);
152 desired_dr_role = dwc->desired_dr_role;
153 spin_unlock_irqrestore(&dwc->lock, flags);
155 pm_runtime_get_sync(dwc->dev);
157 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
158 dwc3_otg_update(dwc, 0);
160 if (!desired_dr_role)
163 if (desired_dr_role == dwc->current_dr_role)
166 if (desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev)
169 switch (dwc->current_dr_role) {
170 case DWC3_GCTL_PRTCAP_HOST:
173 case DWC3_GCTL_PRTCAP_DEVICE:
174 dwc3_gadget_exit(dwc);
175 dwc3_event_buffers_cleanup(dwc);
177 case DWC3_GCTL_PRTCAP_OTG:
179 spin_lock_irqsave(&dwc->lock, flags);
180 dwc->desired_otg_role = DWC3_OTG_ROLE_IDLE;
181 spin_unlock_irqrestore(&dwc->lock, flags);
182 dwc3_otg_update(dwc, 1);
189 * When current_dr_role is not set, there's no role switching.
190 * Only perform GCTL.CoreSoftReset when there's DRD role switching.
192 if (dwc->current_dr_role && ((DWC3_IP_IS(DWC3) ||
193 DWC3_VER_IS_PRIOR(DWC31, 190A)) &&
194 desired_dr_role != DWC3_GCTL_PRTCAP_OTG)) {
195 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
196 reg |= DWC3_GCTL_CORESOFTRESET;
197 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
200 * Wait for internal clocks to synchronized. DWC_usb31 and
201 * DWC_usb32 may need at least 50ms (less for DWC_usb3). To
202 * keep it consistent across different IPs, let's wait up to
203 * 100ms before clearing GCTL.CORESOFTRESET.
207 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
208 reg &= ~DWC3_GCTL_CORESOFTRESET;
209 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
212 spin_lock_irqsave(&dwc->lock, flags);
214 dwc3_set_prtcap(dwc, desired_dr_role);
216 spin_unlock_irqrestore(&dwc->lock, flags);
218 switch (desired_dr_role) {
219 case DWC3_GCTL_PRTCAP_HOST:
220 ret = dwc3_host_init(dwc);
222 dev_err(dwc->dev, "failed to initialize host\n");
225 otg_set_vbus(dwc->usb2_phy->otg, true);
227 for (i = 0; i < dwc->num_usb2_ports; i++)
228 phy_set_mode(dwc->usb2_generic_phy[i], PHY_MODE_USB_HOST);
229 for (i = 0; i < dwc->num_usb3_ports; i++)
230 phy_set_mode(dwc->usb3_generic_phy[i], PHY_MODE_USB_HOST);
232 if (dwc->dis_split_quirk) {
233 reg = dwc3_readl(dwc->regs, DWC3_GUCTL3);
234 reg |= DWC3_GUCTL3_SPLITDISABLE;
235 dwc3_writel(dwc->regs, DWC3_GUCTL3, reg);
239 case DWC3_GCTL_PRTCAP_DEVICE:
240 dwc3_core_soft_reset(dwc);
242 dwc3_event_buffers_setup(dwc);
245 otg_set_vbus(dwc->usb2_phy->otg, false);
246 phy_set_mode(dwc->usb2_generic_phy[0], PHY_MODE_USB_DEVICE);
247 phy_set_mode(dwc->usb3_generic_phy[0], PHY_MODE_USB_DEVICE);
249 ret = dwc3_gadget_init(dwc);
251 dev_err(dwc->dev, "failed to initialize peripheral\n");
253 case DWC3_GCTL_PRTCAP_OTG:
255 dwc3_otg_update(dwc, 0);
262 pm_runtime_mark_last_busy(dwc->dev);
263 pm_runtime_put_autosuspend(dwc->dev);
264 mutex_unlock(&dwc->mutex);
267 void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
271 if (dwc->dr_mode != USB_DR_MODE_OTG)
274 spin_lock_irqsave(&dwc->lock, flags);
275 dwc->desired_dr_role = mode;
276 spin_unlock_irqrestore(&dwc->lock, flags);
278 queue_work(system_freezable_wq, &dwc->drd_work);
281 u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
283 struct dwc3 *dwc = dep->dwc;
286 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
287 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
288 DWC3_GDBGFIFOSPACE_TYPE(type));
290 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
292 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
296 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
297 * @dwc: pointer to our context structure
299 int dwc3_core_soft_reset(struct dwc3 *dwc)
305 * We're resetting only the device side because, if we're in host mode,
306 * XHCI driver will reset the host block. If dwc3 was configured for
307 * host-only mode, then we can return early.
309 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST)
312 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
313 reg |= DWC3_DCTL_CSFTRST;
314 reg &= ~DWC3_DCTL_RUN_STOP;
315 dwc3_gadget_dctl_write_safe(dwc, reg);
318 * For DWC_usb31 controller 1.90a and later, the DCTL.CSFRST bit
319 * is cleared only after all the clocks are synchronized. This can
320 * take a little more than 50ms. Set the polling rate at 20ms
321 * for 10 times instead.
323 if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
327 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
328 if (!(reg & DWC3_DCTL_CSFTRST))
331 if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
337 dev_warn(dwc->dev, "DWC3 controller soft reset failed.\n");
342 * For DWC_usb31 controller 1.80a and prior, once DCTL.CSFRST bit
343 * is cleared, we must wait at least 50ms before accessing the PHY
344 * domain (synchronization delay).
346 if (DWC3_VER_IS_WITHIN(DWC31, ANY, 180A))
353 * dwc3_frame_length_adjustment - Adjusts frame length if required
354 * @dwc3: Pointer to our controller context structure
356 static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
361 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
367 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
368 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
369 if (dft != dwc->fladj) {
370 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
371 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
372 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
377 * dwc3_ref_clk_period - Reference clock period configuration
378 * Default reference clock period depends on hardware
379 * configuration. For systems with reference clock that differs
380 * from the default, this will set clock period in DWC3_GUCTL
382 * @dwc: Pointer to our controller context structure
384 static void dwc3_ref_clk_period(struct dwc3 *dwc)
386 unsigned long period;
393 rate = clk_get_rate(dwc->ref_clk);
396 period = NSEC_PER_SEC / rate;
397 } else if (dwc->ref_clk_per) {
398 period = dwc->ref_clk_per;
399 rate = NSEC_PER_SEC / period;
404 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
405 reg &= ~DWC3_GUCTL_REFCLKPER_MASK;
406 reg |= FIELD_PREP(DWC3_GUCTL_REFCLKPER_MASK, period);
407 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
409 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
413 * The calculation below is
415 * 125000 * (NSEC_PER_SEC / (rate * period) - 1)
417 * but rearranged for fixed-point arithmetic. The division must be
418 * 64-bit because 125000 * NSEC_PER_SEC doesn't fit in 32 bits (and
419 * neither does rate * period).
421 * Note that rate * period ~= NSEC_PER_SECOND, minus the number of
422 * nanoseconds of error caused by the truncation which happened during
423 * the division when calculating rate or period (whichever one was
424 * derived from the other). We first calculate the relative error, then
425 * scale it to units of 8 ppm.
427 fladj = div64_u64(125000ULL * NSEC_PER_SEC, (u64)rate * period);
431 * The documented 240MHz constant is scaled by 2 to get PLS1 as well.
433 decr = 480000000 / rate;
435 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
436 reg &= ~DWC3_GFLADJ_REFCLK_FLADJ_MASK
437 & ~DWC3_GFLADJ_240MHZDECR
438 & ~DWC3_GFLADJ_240MHZDECR_PLS1;
439 reg |= FIELD_PREP(DWC3_GFLADJ_REFCLK_FLADJ_MASK, fladj)
440 | FIELD_PREP(DWC3_GFLADJ_240MHZDECR, decr >> 1)
441 | FIELD_PREP(DWC3_GFLADJ_240MHZDECR_PLS1, decr & 1);
443 if (dwc->gfladj_refclk_lpm_sel)
444 reg |= DWC3_GFLADJ_REFCLK_LPM_SEL;
446 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
450 * dwc3_free_one_event_buffer - Frees one event buffer
451 * @dwc: Pointer to our controller context structure
452 * @evt: Pointer to event buffer to be freed
454 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
455 struct dwc3_event_buffer *evt)
457 dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
461 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
462 * @dwc: Pointer to our controller context structure
463 * @length: size of the event buffer
465 * Returns a pointer to the allocated event buffer structure on success
466 * otherwise ERR_PTR(errno).
468 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
471 struct dwc3_event_buffer *evt;
473 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
475 return ERR_PTR(-ENOMEM);
478 evt->length = length;
479 evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
481 return ERR_PTR(-ENOMEM);
483 evt->buf = dma_alloc_coherent(dwc->sysdev, length,
484 &evt->dma, GFP_KERNEL);
486 return ERR_PTR(-ENOMEM);
492 * dwc3_free_event_buffers - frees all allocated event buffers
493 * @dwc: Pointer to our controller context structure
495 static void dwc3_free_event_buffers(struct dwc3 *dwc)
497 struct dwc3_event_buffer *evt;
501 dwc3_free_one_event_buffer(dwc, evt);
505 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
506 * @dwc: pointer to our controller context structure
507 * @length: size of event buffer
509 * Returns 0 on success otherwise negative errno. In the error case, dwc
510 * may contain some buffers allocated but not all which were requested.
512 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned int length)
514 struct dwc3_event_buffer *evt;
515 unsigned int hw_mode;
517 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
518 if (hw_mode == DWC3_GHWPARAMS0_MODE_HOST) {
523 evt = dwc3_alloc_one_event_buffer(dwc, length);
525 dev_err(dwc->dev, "can't allocate event buffer\n");
534 * dwc3_event_buffers_setup - setup our allocated event buffers
535 * @dwc: pointer to our controller context structure
537 * Returns 0 on success otherwise negative errno.
539 int dwc3_event_buffers_setup(struct dwc3 *dwc)
541 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;
570 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
571 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
572 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
573 | DWC3_GEVNTSIZ_SIZE(0));
574 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
577 static void dwc3_core_num_eps(struct dwc3 *dwc)
579 struct dwc3_hwparams *parms = &dwc->hwparams;
581 dwc->num_eps = DWC3_NUM_EPS(parms);
584 static void dwc3_cache_hwparams(struct dwc3 *dwc)
586 struct dwc3_hwparams *parms = &dwc->hwparams;
588 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
589 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
590 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
591 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
592 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
593 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
594 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
595 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
596 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
598 if (DWC3_IP_IS(DWC32))
599 parms->hwparams9 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS9);
602 static int dwc3_core_ulpi_init(struct dwc3 *dwc)
607 intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
609 if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
610 (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
611 dwc->hsphy_interface &&
612 !strncmp(dwc->hsphy_interface, "ulpi", 4)))
613 ret = dwc3_ulpi_init(dwc);
618 static int dwc3_ss_phy_setup(struct dwc3 *dwc, int index)
622 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(index));
625 * Make sure UX_EXIT_PX is cleared as that causes issues with some
626 * PHYs. Also, this bit is not supposed to be used in normal operation.
628 reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
631 * Above DWC_usb3.0 1.94a, it is recommended to set
632 * DWC3_GUSB3PIPECTL_SUSPHY to '0' during coreConsultant configuration.
633 * So default value will be '0' when the core is reset. Application
634 * needs to set it to '1' after the core initialization is completed.
636 * Similarly for DRD controllers, GUSB3PIPECTL.SUSPENDENABLE must be
637 * cleared after power-on reset, and it can be set after core
640 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
642 if (dwc->u2ss_inp3_quirk)
643 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
645 if (dwc->dis_rxdet_inp3_quirk)
646 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
648 if (dwc->req_p1p2p3_quirk)
649 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
651 if (dwc->del_p1p2p3_quirk)
652 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
654 if (dwc->del_phy_power_chg_quirk)
655 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
657 if (dwc->lfps_filter_quirk)
658 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
660 if (dwc->rx_detect_poll_quirk)
661 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
663 if (dwc->tx_de_emphasis_quirk)
664 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
666 if (dwc->dis_del_phy_power_chg_quirk)
667 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
669 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(index), reg);
674 static int dwc3_hs_phy_setup(struct dwc3 *dwc, int index)
678 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(index));
680 /* Select the HS PHY interface */
681 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
682 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
683 if (dwc->hsphy_interface &&
684 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
685 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
687 } else if (dwc->hsphy_interface &&
688 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
689 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
690 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(index), reg);
692 /* Relying on default value. */
693 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
697 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
702 switch (dwc->hsphy_mode) {
703 case USBPHY_INTERFACE_MODE_UTMI:
704 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
705 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
706 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
707 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
709 case USBPHY_INTERFACE_MODE_UTMIW:
710 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
711 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
712 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
713 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
720 * Above DWC_usb3.0 1.94a, it is recommended to set
721 * DWC3_GUSB2PHYCFG_SUSPHY to '0' during coreConsultant configuration.
722 * So default value will be '0' when the core is reset. Application
723 * needs to set it to '1' after the core initialization is completed.
725 * Similarly for DRD controllers, GUSB2PHYCFG.SUSPHY must be cleared
726 * after power-on reset, and it can be set after core initialization.
728 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
730 if (dwc->dis_enblslpm_quirk)
731 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
733 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM;
735 if (dwc->dis_u2_freeclk_exists_quirk || dwc->gfladj_refclk_lpm_sel)
736 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
739 * Some ULPI USB PHY does not support internal VBUS supply, to drive
740 * the CPEN pin requires the configuration of the ULPI DRVVBUSEXTERNAL
741 * bit of OTG_CTRL register. Controller configures the USB2 PHY
742 * ULPIEXTVBUSDRV bit[17] of the GUSB2PHYCFG register to drive vBus
743 * with an external supply.
745 if (dwc->ulpi_ext_vbus_drv)
746 reg |= DWC3_GUSB2PHYCFG_ULPIEXTVBUSDRV;
748 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(index), reg);
754 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
755 * @dwc: Pointer to our controller context structure
757 * Returns 0 on success. The USB PHY interfaces are configured but not
758 * initialized. The PHY interfaces and the PHYs get initialized together with
759 * the core in dwc3_core_init.
761 static int dwc3_phy_setup(struct dwc3 *dwc)
766 for (i = 0; i < dwc->num_usb3_ports; i++) {
767 ret = dwc3_ss_phy_setup(dwc, i);
772 for (i = 0; i < dwc->num_usb2_ports; i++) {
773 ret = dwc3_hs_phy_setup(dwc, i);
781 static int dwc3_phy_init(struct dwc3 *dwc)
787 usb_phy_init(dwc->usb2_phy);
788 usb_phy_init(dwc->usb3_phy);
790 for (i = 0; i < dwc->num_usb2_ports; i++) {
791 ret = phy_init(dwc->usb2_generic_phy[i]);
793 goto err_exit_usb2_phy;
796 for (j = 0; j < dwc->num_usb3_ports; j++) {
797 ret = phy_init(dwc->usb3_generic_phy[j]);
799 goto err_exit_usb3_phy;
806 phy_exit(dwc->usb3_generic_phy[j]);
810 phy_exit(dwc->usb2_generic_phy[i]);
812 usb_phy_shutdown(dwc->usb3_phy);
813 usb_phy_shutdown(dwc->usb2_phy);
818 static void dwc3_phy_exit(struct dwc3 *dwc)
822 for (i = 0; i < dwc->num_usb3_ports; i++)
823 phy_exit(dwc->usb3_generic_phy[i]);
825 for (i = 0; i < dwc->num_usb2_ports; i++)
826 phy_exit(dwc->usb2_generic_phy[i]);
828 usb_phy_shutdown(dwc->usb3_phy);
829 usb_phy_shutdown(dwc->usb2_phy);
832 static int dwc3_phy_power_on(struct dwc3 *dwc)
838 usb_phy_set_suspend(dwc->usb2_phy, 0);
839 usb_phy_set_suspend(dwc->usb3_phy, 0);
841 for (i = 0; i < dwc->num_usb2_ports; i++) {
842 ret = phy_power_on(dwc->usb2_generic_phy[i]);
844 goto err_power_off_usb2_phy;
847 for (j = 0; j < dwc->num_usb3_ports; j++) {
848 ret = phy_power_on(dwc->usb3_generic_phy[j]);
850 goto err_power_off_usb3_phy;
855 err_power_off_usb3_phy:
857 phy_power_off(dwc->usb3_generic_phy[j]);
859 err_power_off_usb2_phy:
861 phy_power_off(dwc->usb2_generic_phy[i]);
863 usb_phy_set_suspend(dwc->usb3_phy, 1);
864 usb_phy_set_suspend(dwc->usb2_phy, 1);
869 static void dwc3_phy_power_off(struct dwc3 *dwc)
873 for (i = 0; i < dwc->num_usb3_ports; i++)
874 phy_power_off(dwc->usb3_generic_phy[i]);
876 for (i = 0; i < dwc->num_usb2_ports; i++)
877 phy_power_off(dwc->usb2_generic_phy[i]);
879 usb_phy_set_suspend(dwc->usb3_phy, 1);
880 usb_phy_set_suspend(dwc->usb2_phy, 1);
883 static int dwc3_clk_enable(struct dwc3 *dwc)
887 ret = clk_prepare_enable(dwc->bus_clk);
891 ret = clk_prepare_enable(dwc->ref_clk);
893 goto disable_bus_clk;
895 ret = clk_prepare_enable(dwc->susp_clk);
897 goto disable_ref_clk;
899 ret = clk_prepare_enable(dwc->utmi_clk);
901 goto disable_susp_clk;
903 ret = clk_prepare_enable(dwc->pipe_clk);
905 goto disable_utmi_clk;
910 clk_disable_unprepare(dwc->utmi_clk);
912 clk_disable_unprepare(dwc->susp_clk);
914 clk_disable_unprepare(dwc->ref_clk);
916 clk_disable_unprepare(dwc->bus_clk);
920 static void dwc3_clk_disable(struct dwc3 *dwc)
922 clk_disable_unprepare(dwc->pipe_clk);
923 clk_disable_unprepare(dwc->utmi_clk);
924 clk_disable_unprepare(dwc->susp_clk);
925 clk_disable_unprepare(dwc->ref_clk);
926 clk_disable_unprepare(dwc->bus_clk);
929 static void dwc3_core_exit(struct dwc3 *dwc)
931 dwc3_event_buffers_cleanup(dwc);
932 dwc3_phy_power_off(dwc);
934 dwc3_clk_disable(dwc);
935 reset_control_assert(dwc->reset);
938 static bool dwc3_core_is_valid(struct dwc3 *dwc)
942 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
943 dwc->ip = DWC3_GSNPS_ID(reg);
945 /* This should read as U3 followed by revision number */
946 if (DWC3_IP_IS(DWC3)) {
948 } else if (DWC3_IP_IS(DWC31) || DWC3_IP_IS(DWC32)) {
949 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
950 dwc->version_type = dwc3_readl(dwc->regs, DWC3_VER_TYPE);
958 static void dwc3_core_setup_global_control(struct dwc3 *dwc)
962 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
963 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
965 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
966 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
968 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
969 * issue which would cause xHCI compliance tests to fail.
971 * Because of that we cannot enable clock gating on such
976 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
979 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
980 dwc->dr_mode == USB_DR_MODE_OTG) &&
981 DWC3_VER_IS_WITHIN(DWC3, 210A, 250A))
982 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
984 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
986 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
988 * REVISIT Enabling this bit so that host-mode hibernation
989 * will work. Device-mode hibernation is not yet implemented.
991 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
998 /* check if current dwc3 is on simulation board */
999 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
1000 dev_info(dwc->dev, "Running with FPGA optimizations\n");
1001 dwc->is_fpga = true;
1004 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
1005 "disable_scramble cannot be used on non-FPGA builds\n");
1007 if (dwc->disable_scramble_quirk && dwc->is_fpga)
1008 reg |= DWC3_GCTL_DISSCRAMBLE;
1010 reg &= ~DWC3_GCTL_DISSCRAMBLE;
1012 if (dwc->u2exit_lfps_quirk)
1013 reg |= DWC3_GCTL_U2EXIT_LFPS;
1016 * WORKAROUND: DWC3 revisions <1.90a have a bug
1017 * where the device can fail to connect at SuperSpeed
1018 * and falls back to high-speed mode which causes
1019 * the device to enter a Connect/Disconnect loop
1021 if (DWC3_VER_IS_PRIOR(DWC3, 190A))
1022 reg |= DWC3_GCTL_U2RSTECN;
1024 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1027 static int dwc3_core_get_phy(struct dwc3 *dwc);
1028 static int dwc3_core_ulpi_init(struct dwc3 *dwc);
1030 /* set global incr burst type configuration registers */
1031 static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
1033 struct device *dev = dwc->dev;
1034 /* incrx_mode : for INCR burst type. */
1036 /* incrx_size : for size of INCRX burst. */
1044 cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
1047 * Handle property "snps,incr-burst-type-adjustment".
1048 * Get the number of value from this property:
1049 * result <= 0, means this property is not supported.
1050 * result = 1, means INCRx burst mode supported.
1051 * result > 1, means undefined length burst mode supported.
1053 ntype = device_property_count_u32(dev, "snps,incr-burst-type-adjustment");
1057 vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL);
1061 /* Get INCR burst type, and parse it */
1062 ret = device_property_read_u32_array(dev,
1063 "snps,incr-burst-type-adjustment", vals, ntype);
1066 dev_err(dev, "Error to get property\n");
1073 /* INCRX (undefined length) burst mode */
1074 incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
1075 for (i = 1; i < ntype; i++) {
1076 if (vals[i] > incrx_size)
1077 incrx_size = vals[i];
1080 /* INCRX burst mode */
1081 incrx_mode = INCRX_BURST_MODE;
1086 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
1087 cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
1089 cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
1090 switch (incrx_size) {
1092 cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
1095 cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
1098 cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
1101 cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
1104 cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
1107 cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
1110 cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
1115 dev_err(dev, "Invalid property\n");
1119 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
1122 static void dwc3_set_power_down_clk_scale(struct dwc3 *dwc)
1131 * The power down scale field specifies how many suspend_clk
1132 * periods fit into a 16KHz clock period. When performing
1133 * the division, round up the remainder.
1135 * The power down scale value is calculated using the fastest
1136 * frequency of the suspend_clk. If it isn't fixed (but within
1137 * the accuracy requirement), the driver may not know the max
1138 * rate of the suspend_clk, so only update the power down scale
1139 * if the default is less than the calculated value from
1140 * clk_get_rate() or if the default is questionably high
1141 * (3x or more) to be within the requirement.
1143 scale = DIV_ROUND_UP(clk_get_rate(dwc->susp_clk), 16000);
1144 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1145 if ((reg & DWC3_GCTL_PWRDNSCALE_MASK) < DWC3_GCTL_PWRDNSCALE(scale) ||
1146 (reg & DWC3_GCTL_PWRDNSCALE_MASK) > DWC3_GCTL_PWRDNSCALE(scale*3)) {
1147 reg &= ~(DWC3_GCTL_PWRDNSCALE_MASK);
1148 reg |= DWC3_GCTL_PWRDNSCALE(scale);
1149 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1153 static void dwc3_config_threshold(struct dwc3 *dwc)
1162 * Must config both number of packets and max burst settings to enable
1163 * RX and/or TX threshold.
1165 if (!DWC3_IP_IS(DWC3) && dwc->dr_mode == USB_DR_MODE_HOST) {
1166 rx_thr_num = dwc->rx_thr_num_pkt_prd;
1167 rx_maxburst = dwc->rx_max_burst_prd;
1168 tx_thr_num = dwc->tx_thr_num_pkt_prd;
1169 tx_maxburst = dwc->tx_max_burst_prd;
1171 if (rx_thr_num && rx_maxburst) {
1172 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1173 reg |= DWC31_RXTHRNUMPKTSEL_PRD;
1175 reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
1176 reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
1178 reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
1179 reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
1181 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1184 if (tx_thr_num && tx_maxburst) {
1185 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1186 reg |= DWC31_TXTHRNUMPKTSEL_PRD;
1188 reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
1189 reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
1191 reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
1192 reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
1194 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1198 rx_thr_num = dwc->rx_thr_num_pkt;
1199 rx_maxburst = dwc->rx_max_burst;
1200 tx_thr_num = dwc->tx_thr_num_pkt;
1201 tx_maxburst = dwc->tx_max_burst;
1203 if (DWC3_IP_IS(DWC3)) {
1204 if (rx_thr_num && rx_maxburst) {
1205 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1206 reg |= DWC3_GRXTHRCFG_PKTCNTSEL;
1208 reg &= ~DWC3_GRXTHRCFG_RXPKTCNT(~0);
1209 reg |= DWC3_GRXTHRCFG_RXPKTCNT(rx_thr_num);
1211 reg &= ~DWC3_GRXTHRCFG_MAXRXBURSTSIZE(~0);
1212 reg |= DWC3_GRXTHRCFG_MAXRXBURSTSIZE(rx_maxburst);
1214 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1217 if (tx_thr_num && tx_maxburst) {
1218 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1219 reg |= DWC3_GTXTHRCFG_PKTCNTSEL;
1221 reg &= ~DWC3_GTXTHRCFG_TXPKTCNT(~0);
1222 reg |= DWC3_GTXTHRCFG_TXPKTCNT(tx_thr_num);
1224 reg &= ~DWC3_GTXTHRCFG_MAXTXBURSTSIZE(~0);
1225 reg |= DWC3_GTXTHRCFG_MAXTXBURSTSIZE(tx_maxburst);
1227 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1230 if (rx_thr_num && rx_maxburst) {
1231 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1232 reg |= DWC31_GRXTHRCFG_PKTCNTSEL;
1234 reg &= ~DWC31_GRXTHRCFG_RXPKTCNT(~0);
1235 reg |= DWC31_GRXTHRCFG_RXPKTCNT(rx_thr_num);
1237 reg &= ~DWC31_GRXTHRCFG_MAXRXBURSTSIZE(~0);
1238 reg |= DWC31_GRXTHRCFG_MAXRXBURSTSIZE(rx_maxburst);
1240 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1243 if (tx_thr_num && tx_maxburst) {
1244 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1245 reg |= DWC31_GTXTHRCFG_PKTCNTSEL;
1247 reg &= ~DWC31_GTXTHRCFG_TXPKTCNT(~0);
1248 reg |= DWC31_GTXTHRCFG_TXPKTCNT(tx_thr_num);
1250 reg &= ~DWC31_GTXTHRCFG_MAXTXBURSTSIZE(~0);
1251 reg |= DWC31_GTXTHRCFG_MAXTXBURSTSIZE(tx_maxburst);
1253 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1259 * dwc3_core_init - Low-level initialization of DWC3 Core
1260 * @dwc: Pointer to our controller context structure
1262 * Returns 0 on success otherwise negative errno.
1264 static int dwc3_core_init(struct dwc3 *dwc)
1266 unsigned int hw_mode;
1270 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
1273 * Write Linux Version Code to our GUID register so it's easy to figure
1274 * out which kernel version a bug was found.
1276 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
1278 ret = dwc3_phy_setup(dwc);
1282 if (!dwc->ulpi_ready) {
1283 ret = dwc3_core_ulpi_init(dwc);
1285 if (ret == -ETIMEDOUT) {
1286 dwc3_core_soft_reset(dwc);
1287 ret = -EPROBE_DEFER;
1291 dwc->ulpi_ready = true;
1294 if (!dwc->phys_ready) {
1295 ret = dwc3_core_get_phy(dwc);
1298 dwc->phys_ready = true;
1301 ret = dwc3_phy_init(dwc);
1305 ret = dwc3_core_soft_reset(dwc);
1309 dwc3_core_setup_global_control(dwc);
1310 dwc3_core_num_eps(dwc);
1312 /* Set power down scale of suspend_clk */
1313 dwc3_set_power_down_clk_scale(dwc);
1315 /* Adjust Frame Length */
1316 dwc3_frame_length_adjustment(dwc);
1318 /* Adjust Reference Clock Period */
1319 dwc3_ref_clk_period(dwc);
1321 dwc3_set_incr_burst_type(dwc);
1323 ret = dwc3_phy_power_on(dwc);
1327 ret = dwc3_event_buffers_setup(dwc);
1329 dev_err(dwc->dev, "failed to setup event buffers\n");
1330 goto err_power_off_phy;
1334 * ENDXFER polling is available on version 3.10a and later of
1335 * the DWC_usb3 controller. It is NOT available in the
1336 * DWC_usb31 controller.
1338 if (DWC3_VER_IS_WITHIN(DWC3, 310A, ANY)) {
1339 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
1340 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
1341 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
1345 * When configured in HOST mode, after issuing U3/L2 exit controller
1346 * fails to send proper CRC checksum in CRC5 feild. Because of this
1347 * behaviour Transaction Error is generated, resulting in reset and
1348 * re-enumeration of usb device attached. All the termsel, xcvrsel,
1349 * opmode becomes 0 during end of resume. Enabling bit 10 of GUCTL1
1350 * will correct this problem. This option is to support certain
1353 if (dwc->resume_hs_terminations) {
1354 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1355 reg |= DWC3_GUCTL1_RESUME_OPMODE_HS_HOST;
1356 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1359 if (!DWC3_VER_IS_PRIOR(DWC3, 250A)) {
1360 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1363 * Enable hardware control of sending remote wakeup
1364 * in HS when the device is in the L1 state.
1366 if (!DWC3_VER_IS_PRIOR(DWC3, 290A))
1367 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
1370 * Decouple USB 2.0 L1 & L2 events which will allow for
1371 * gadget driver to only receive U3/L2 suspend & wakeup
1372 * events and prevent the more frequent L1 LPM transitions
1373 * from interrupting the driver.
1375 if (!DWC3_VER_IS_PRIOR(DWC3, 300A))
1376 reg |= DWC3_GUCTL1_DEV_DECOUPLE_L1L2_EVT;
1378 if (dwc->dis_tx_ipgap_linecheck_quirk)
1379 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
1381 if (dwc->parkmode_disable_ss_quirk)
1382 reg |= DWC3_GUCTL1_PARKMODE_DISABLE_SS;
1384 if (dwc->parkmode_disable_hs_quirk)
1385 reg |= DWC3_GUCTL1_PARKMODE_DISABLE_HS;
1387 if (DWC3_VER_IS_WITHIN(DWC3, 290A, ANY)) {
1388 if (dwc->maximum_speed == USB_SPEED_FULL ||
1389 dwc->maximum_speed == USB_SPEED_HIGH)
1390 reg |= DWC3_GUCTL1_DEV_FORCE_20_CLK_FOR_30_CLK;
1392 reg &= ~DWC3_GUCTL1_DEV_FORCE_20_CLK_FOR_30_CLK;
1395 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1398 dwc3_config_threshold(dwc);
1401 * Modify this for all supported Super Speed ports when
1402 * multiport support is added.
1404 if (hw_mode != DWC3_GHWPARAMS0_MODE_GADGET &&
1405 (DWC3_IP_IS(DWC31)) &&
1406 dwc->maximum_speed == USB_SPEED_SUPER) {
1407 reg = dwc3_readl(dwc->regs, DWC3_LLUCTL);
1408 reg |= DWC3_LLUCTL_FORCE_GEN1;
1409 dwc3_writel(dwc->regs, DWC3_LLUCTL, reg);
1415 dwc3_phy_power_off(dwc);
1419 dwc3_ulpi_exit(dwc);
1424 static int dwc3_core_get_phy(struct dwc3 *dwc)
1426 struct device *dev = dwc->dev;
1427 struct device_node *node = dev->of_node;
1433 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
1434 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
1436 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1437 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
1440 if (IS_ERR(dwc->usb2_phy)) {
1441 ret = PTR_ERR(dwc->usb2_phy);
1442 if (ret == -ENXIO || ret == -ENODEV)
1443 dwc->usb2_phy = NULL;
1445 return dev_err_probe(dev, ret, "no usb2 phy configured\n");
1448 if (IS_ERR(dwc->usb3_phy)) {
1449 ret = PTR_ERR(dwc->usb3_phy);
1450 if (ret == -ENXIO || ret == -ENODEV)
1451 dwc->usb3_phy = NULL;
1453 return dev_err_probe(dev, ret, "no usb3 phy configured\n");
1456 for (i = 0; i < dwc->num_usb2_ports; i++) {
1457 if (dwc->num_usb2_ports == 1)
1458 snprintf(phy_name, sizeof(phy_name), "usb2-phy");
1460 snprintf(phy_name, sizeof(phy_name), "usb2-%u", i);
1462 dwc->usb2_generic_phy[i] = devm_phy_get(dev, phy_name);
1463 if (IS_ERR(dwc->usb2_generic_phy[i])) {
1464 ret = PTR_ERR(dwc->usb2_generic_phy[i]);
1465 if (ret == -ENOSYS || ret == -ENODEV)
1466 dwc->usb2_generic_phy[i] = NULL;
1468 return dev_err_probe(dev, ret, "failed to lookup phy %s\n",
1473 for (i = 0; i < dwc->num_usb3_ports; i++) {
1474 if (dwc->num_usb3_ports == 1)
1475 snprintf(phy_name, sizeof(phy_name), "usb3-phy");
1477 snprintf(phy_name, sizeof(phy_name), "usb3-%u", i);
1479 dwc->usb3_generic_phy[i] = devm_phy_get(dev, phy_name);
1480 if (IS_ERR(dwc->usb3_generic_phy[i])) {
1481 ret = PTR_ERR(dwc->usb3_generic_phy[i]);
1482 if (ret == -ENOSYS || ret == -ENODEV)
1483 dwc->usb3_generic_phy[i] = NULL;
1485 return dev_err_probe(dev, ret, "failed to lookup phy %s\n",
1493 static int dwc3_core_init_mode(struct dwc3 *dwc)
1495 struct device *dev = dwc->dev;
1499 switch (dwc->dr_mode) {
1500 case USB_DR_MODE_PERIPHERAL:
1501 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1504 otg_set_vbus(dwc->usb2_phy->otg, false);
1505 phy_set_mode(dwc->usb2_generic_phy[0], PHY_MODE_USB_DEVICE);
1506 phy_set_mode(dwc->usb3_generic_phy[0], PHY_MODE_USB_DEVICE);
1508 ret = dwc3_gadget_init(dwc);
1510 return dev_err_probe(dev, ret, "failed to initialize gadget\n");
1512 case USB_DR_MODE_HOST:
1513 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1516 otg_set_vbus(dwc->usb2_phy->otg, true);
1517 for (i = 0; i < dwc->num_usb2_ports; i++)
1518 phy_set_mode(dwc->usb2_generic_phy[i], PHY_MODE_USB_HOST);
1519 for (i = 0; i < dwc->num_usb3_ports; i++)
1520 phy_set_mode(dwc->usb3_generic_phy[i], PHY_MODE_USB_HOST);
1522 ret = dwc3_host_init(dwc);
1524 return dev_err_probe(dev, ret, "failed to initialize host\n");
1526 case USB_DR_MODE_OTG:
1527 INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
1528 ret = dwc3_drd_init(dwc);
1530 return dev_err_probe(dev, ret, "failed to initialize dual-role\n");
1533 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1540 static void dwc3_core_exit_mode(struct dwc3 *dwc)
1542 switch (dwc->dr_mode) {
1543 case USB_DR_MODE_PERIPHERAL:
1544 dwc3_gadget_exit(dwc);
1546 case USB_DR_MODE_HOST:
1547 dwc3_host_exit(dwc);
1549 case USB_DR_MODE_OTG:
1557 /* de-assert DRVVBUS for HOST and OTG mode */
1558 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1561 static void dwc3_get_properties(struct dwc3 *dwc)
1563 struct device *dev = dwc->dev;
1564 u8 lpm_nyet_threshold;
1567 u8 rx_thr_num_pkt = 0;
1568 u8 rx_max_burst = 0;
1569 u8 tx_thr_num_pkt = 0;
1570 u8 tx_max_burst = 0;
1571 u8 rx_thr_num_pkt_prd = 0;
1572 u8 rx_max_burst_prd = 0;
1573 u8 tx_thr_num_pkt_prd = 0;
1574 u8 tx_max_burst_prd = 0;
1575 u8 tx_fifo_resize_max_num;
1576 const char *usb_psy_name;
1579 /* default to highest possible threshold */
1580 lpm_nyet_threshold = 0xf;
1582 /* default to -3.5dB de-emphasis */
1586 * default to assert utmi_sleep_n and use maximum allowed HIRD
1587 * threshold value of 0b1100
1589 hird_threshold = 12;
1592 * default to a TXFIFO size large enough to fit 6 max packets. This
1593 * allows for systems with larger bus latencies to have some headroom
1594 * for endpoints that have a large bMaxBurst value.
1596 tx_fifo_resize_max_num = 6;
1598 dwc->maximum_speed = usb_get_maximum_speed(dev);
1599 dwc->max_ssp_rate = usb_get_maximum_ssp_rate(dev);
1600 dwc->dr_mode = usb_get_dr_mode(dev);
1601 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
1603 dwc->sysdev_is_parent = device_property_read_bool(dev,
1604 "linux,sysdev_is_parent");
1605 if (dwc->sysdev_is_parent)
1606 dwc->sysdev = dwc->dev->parent;
1608 dwc->sysdev = dwc->dev;
1610 dwc->sys_wakeup = device_may_wakeup(dwc->sysdev);
1612 ret = device_property_read_string(dev, "usb-psy-name", &usb_psy_name);
1614 dwc->usb_psy = power_supply_get_by_name(usb_psy_name);
1616 dev_err(dev, "couldn't get usb power supply\n");
1619 dwc->has_lpm_erratum = device_property_read_bool(dev,
1620 "snps,has-lpm-erratum");
1621 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
1622 &lpm_nyet_threshold);
1623 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
1624 "snps,is-utmi-l1-suspend");
1625 device_property_read_u8(dev, "snps,hird-threshold",
1627 dwc->dis_start_transfer_quirk = device_property_read_bool(dev,
1628 "snps,dis-start-transfer-quirk");
1629 dwc->usb3_lpm_capable = device_property_read_bool(dev,
1630 "snps,usb3_lpm_capable");
1631 dwc->usb2_lpm_disable = device_property_read_bool(dev,
1632 "snps,usb2-lpm-disable");
1633 dwc->usb2_gadget_lpm_disable = device_property_read_bool(dev,
1634 "snps,usb2-gadget-lpm-disable");
1635 device_property_read_u8(dev, "snps,rx-thr-num-pkt",
1637 device_property_read_u8(dev, "snps,rx-max-burst",
1639 device_property_read_u8(dev, "snps,tx-thr-num-pkt",
1641 device_property_read_u8(dev, "snps,tx-max-burst",
1643 device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
1644 &rx_thr_num_pkt_prd);
1645 device_property_read_u8(dev, "snps,rx-max-burst-prd",
1647 device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
1648 &tx_thr_num_pkt_prd);
1649 device_property_read_u8(dev, "snps,tx-max-burst-prd",
1651 dwc->do_fifo_resize = device_property_read_bool(dev,
1653 if (dwc->do_fifo_resize)
1654 device_property_read_u8(dev, "tx-fifo-max-num",
1655 &tx_fifo_resize_max_num);
1657 dwc->disable_scramble_quirk = device_property_read_bool(dev,
1658 "snps,disable_scramble_quirk");
1659 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
1660 "snps,u2exit_lfps_quirk");
1661 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
1662 "snps,u2ss_inp3_quirk");
1663 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
1664 "snps,req_p1p2p3_quirk");
1665 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
1666 "snps,del_p1p2p3_quirk");
1667 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
1668 "snps,del_phy_power_chg_quirk");
1669 dwc->lfps_filter_quirk = device_property_read_bool(dev,
1670 "snps,lfps_filter_quirk");
1671 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
1672 "snps,rx_detect_poll_quirk");
1673 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
1674 "snps,dis_u3_susphy_quirk");
1675 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
1676 "snps,dis_u2_susphy_quirk");
1677 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1678 "snps,dis_enblslpm_quirk");
1679 dwc->dis_u1_entry_quirk = device_property_read_bool(dev,
1680 "snps,dis-u1-entry-quirk");
1681 dwc->dis_u2_entry_quirk = device_property_read_bool(dev,
1682 "snps,dis-u2-entry-quirk");
1683 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1684 "snps,dis_rxdet_inp3_quirk");
1685 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1686 "snps,dis-u2-freeclk-exists-quirk");
1687 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1688 "snps,dis-del-phy-power-chg-quirk");
1689 dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1690 "snps,dis-tx-ipgap-linecheck-quirk");
1691 dwc->resume_hs_terminations = device_property_read_bool(dev,
1692 "snps,resume-hs-terminations");
1693 dwc->ulpi_ext_vbus_drv = device_property_read_bool(dev,
1694 "snps,ulpi-ext-vbus-drv");
1695 dwc->parkmode_disable_ss_quirk = device_property_read_bool(dev,
1696 "snps,parkmode-disable-ss-quirk");
1697 dwc->parkmode_disable_hs_quirk = device_property_read_bool(dev,
1698 "snps,parkmode-disable-hs-quirk");
1699 dwc->gfladj_refclk_lpm_sel = device_property_read_bool(dev,
1700 "snps,gfladj-refclk-lpm-sel-quirk");
1702 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
1703 "snps,tx_de_emphasis_quirk");
1704 device_property_read_u8(dev, "snps,tx_de_emphasis",
1706 device_property_read_string(dev, "snps,hsphy_interface",
1707 &dwc->hsphy_interface);
1708 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
1710 device_property_read_u32(dev, "snps,ref-clock-period-ns",
1713 dwc->dis_metastability_quirk = device_property_read_bool(dev,
1714 "snps,dis_metastability_quirk");
1716 dwc->dis_split_quirk = device_property_read_bool(dev,
1717 "snps,dis-split-quirk");
1719 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1720 dwc->tx_de_emphasis = tx_de_emphasis;
1722 dwc->hird_threshold = hird_threshold;
1724 dwc->rx_thr_num_pkt = rx_thr_num_pkt;
1725 dwc->rx_max_burst = rx_max_burst;
1727 dwc->tx_thr_num_pkt = tx_thr_num_pkt;
1728 dwc->tx_max_burst = tx_max_burst;
1730 dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd;
1731 dwc->rx_max_burst_prd = rx_max_burst_prd;
1733 dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd;
1734 dwc->tx_max_burst_prd = tx_max_burst_prd;
1736 dwc->imod_interval = 0;
1738 dwc->tx_fifo_resize_max_num = tx_fifo_resize_max_num;
1741 /* check whether the core supports IMOD */
1742 bool dwc3_has_imod(struct dwc3 *dwc)
1744 return DWC3_VER_IS_WITHIN(DWC3, 300A, ANY) ||
1745 DWC3_VER_IS_WITHIN(DWC31, 120A, ANY) ||
1749 static void dwc3_check_params(struct dwc3 *dwc)
1751 struct device *dev = dwc->dev;
1752 unsigned int hwparam_gen =
1753 DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3);
1755 /* Check for proper value of imod_interval */
1756 if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
1757 dev_warn(dwc->dev, "Interrupt moderation not supported\n");
1758 dwc->imod_interval = 0;
1762 * Workaround for STAR 9000961433 which affects only version
1763 * 3.00a of the DWC_usb3 core. This prevents the controller
1764 * interrupt from being masked while handling events. IMOD
1765 * allows us to work around this issue. Enable it for the
1768 if (!dwc->imod_interval &&
1769 DWC3_VER_IS(DWC3, 300A))
1770 dwc->imod_interval = 1;
1772 /* Check the maximum_speed parameter */
1773 switch (dwc->maximum_speed) {
1774 case USB_SPEED_FULL:
1775 case USB_SPEED_HIGH:
1777 case USB_SPEED_SUPER:
1778 if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS)
1779 dev_warn(dev, "UDC doesn't support Gen 1\n");
1781 case USB_SPEED_SUPER_PLUS:
1782 if ((DWC3_IP_IS(DWC32) &&
1783 hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS) ||
1784 (!DWC3_IP_IS(DWC32) &&
1785 hwparam_gen != DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1786 dev_warn(dev, "UDC doesn't support SSP\n");
1789 dev_err(dev, "invalid maximum_speed parameter %d\n",
1790 dwc->maximum_speed);
1792 case USB_SPEED_UNKNOWN:
1793 switch (hwparam_gen) {
1794 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
1795 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1797 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
1798 if (DWC3_IP_IS(DWC32))
1799 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1801 dwc->maximum_speed = USB_SPEED_SUPER;
1803 case DWC3_GHWPARAMS3_SSPHY_IFC_DIS:
1804 dwc->maximum_speed = USB_SPEED_HIGH;
1807 dwc->maximum_speed = USB_SPEED_SUPER;
1814 * Currently the controller does not have visibility into the HW
1815 * parameter to determine the maximum number of lanes the HW supports.
1816 * If the number of lanes is not specified in the device property, then
1817 * set the default to support dual-lane for DWC_usb32 and single-lane
1818 * for DWC_usb31 for super-speed-plus.
1820 if (dwc->maximum_speed == USB_SPEED_SUPER_PLUS) {
1821 switch (dwc->max_ssp_rate) {
1822 case USB_SSP_GEN_2x1:
1823 if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_GEN1)
1824 dev_warn(dev, "UDC only supports Gen 1\n");
1826 case USB_SSP_GEN_1x2:
1827 case USB_SSP_GEN_2x2:
1828 if (DWC3_IP_IS(DWC31))
1829 dev_warn(dev, "UDC only supports single lane\n");
1831 case USB_SSP_GEN_UNKNOWN:
1833 switch (hwparam_gen) {
1834 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
1835 if (DWC3_IP_IS(DWC32))
1836 dwc->max_ssp_rate = USB_SSP_GEN_2x2;
1838 dwc->max_ssp_rate = USB_SSP_GEN_2x1;
1840 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
1841 if (DWC3_IP_IS(DWC32))
1842 dwc->max_ssp_rate = USB_SSP_GEN_1x2;
1850 static struct extcon_dev *dwc3_get_extcon(struct dwc3 *dwc)
1852 struct device *dev = dwc->dev;
1853 struct device_node *np_phy;
1854 struct extcon_dev *edev = NULL;
1857 if (device_property_read_bool(dev, "extcon"))
1858 return extcon_get_edev_by_phandle(dev, 0);
1861 * Device tree platforms should get extcon via phandle.
1862 * On ACPI platforms, we get the name from a device property.
1863 * This device property is for kernel internal use only and
1864 * is expected to be set by the glue code.
1866 if (device_property_read_string(dev, "linux,extcon-name", &name) == 0)
1867 return extcon_get_extcon_dev(name);
1870 * Check explicitly if "usb-role-switch" is used since
1871 * extcon_find_edev_by_node() can not be used to check the absence of
1872 * an extcon device. In the absence of an device it will always return
1875 if (IS_ENABLED(CONFIG_USB_ROLE_SWITCH) &&
1876 device_property_read_bool(dev, "usb-role-switch"))
1880 * Try to get an extcon device from the USB PHY controller's "port"
1881 * node. Check if it has the "port" node first, to avoid printing the
1882 * error message from underlying code, as it's a valid case: extcon
1883 * device (and "port" node) may be missing in case of "usb-role-switch"
1886 np_phy = of_parse_phandle(dev->of_node, "phys", 0);
1887 if (of_graph_is_present(np_phy)) {
1888 struct device_node *np_conn;
1890 np_conn = of_graph_get_remote_node(np_phy, -1, -1);
1892 edev = extcon_find_edev_by_node(np_conn);
1893 of_node_put(np_conn);
1895 of_node_put(np_phy);
1900 static int dwc3_get_clocks(struct dwc3 *dwc)
1902 struct device *dev = dwc->dev;
1908 * Clocks are optional, but new DT platforms should support all clocks
1909 * as required by the DT-binding.
1910 * Some devices have different clock names in legacy device trees,
1911 * check for them to retain backwards compatibility.
1913 dwc->bus_clk = devm_clk_get_optional(dev, "bus_early");
1914 if (IS_ERR(dwc->bus_clk)) {
1915 return dev_err_probe(dev, PTR_ERR(dwc->bus_clk),
1916 "could not get bus clock\n");
1919 if (dwc->bus_clk == NULL) {
1920 dwc->bus_clk = devm_clk_get_optional(dev, "bus_clk");
1921 if (IS_ERR(dwc->bus_clk)) {
1922 return dev_err_probe(dev, PTR_ERR(dwc->bus_clk),
1923 "could not get bus clock\n");
1927 dwc->ref_clk = devm_clk_get_optional(dev, "ref");
1928 if (IS_ERR(dwc->ref_clk)) {
1929 return dev_err_probe(dev, PTR_ERR(dwc->ref_clk),
1930 "could not get ref clock\n");
1933 if (dwc->ref_clk == NULL) {
1934 dwc->ref_clk = devm_clk_get_optional(dev, "ref_clk");
1935 if (IS_ERR(dwc->ref_clk)) {
1936 return dev_err_probe(dev, PTR_ERR(dwc->ref_clk),
1937 "could not get ref clock\n");
1941 dwc->susp_clk = devm_clk_get_optional(dev, "suspend");
1942 if (IS_ERR(dwc->susp_clk)) {
1943 return dev_err_probe(dev, PTR_ERR(dwc->susp_clk),
1944 "could not get suspend clock\n");
1947 if (dwc->susp_clk == NULL) {
1948 dwc->susp_clk = devm_clk_get_optional(dev, "suspend_clk");
1949 if (IS_ERR(dwc->susp_clk)) {
1950 return dev_err_probe(dev, PTR_ERR(dwc->susp_clk),
1951 "could not get suspend clock\n");
1955 /* specific to Rockchip RK3588 */
1956 dwc->utmi_clk = devm_clk_get_optional(dev, "utmi");
1957 if (IS_ERR(dwc->utmi_clk)) {
1958 return dev_err_probe(dev, PTR_ERR(dwc->utmi_clk),
1959 "could not get utmi clock\n");
1962 /* specific to Rockchip RK3588 */
1963 dwc->pipe_clk = devm_clk_get_optional(dev, "pipe");
1964 if (IS_ERR(dwc->pipe_clk)) {
1965 return dev_err_probe(dev, PTR_ERR(dwc->pipe_clk),
1966 "could not get pipe clock\n");
1972 static int dwc3_get_num_ports(struct dwc3 *dwc)
1980 * Remap xHCI address space to access XHCI ext cap regs since it is
1981 * needed to get information on number of ports present.
1983 base = ioremap(dwc->xhci_resources[0].start,
1984 resource_size(&dwc->xhci_resources[0]));
1990 offset = xhci_find_next_ext_cap(base, offset,
1991 XHCI_EXT_CAPS_PROTOCOL);
1995 val = readl(base + offset);
1996 major_revision = XHCI_EXT_PORT_MAJOR(val);
1998 val = readl(base + offset + 0x08);
1999 if (major_revision == 0x03) {
2000 dwc->num_usb3_ports += XHCI_EXT_PORT_COUNT(val);
2001 } else if (major_revision <= 0x02) {
2002 dwc->num_usb2_ports += XHCI_EXT_PORT_COUNT(val);
2004 dev_warn(dwc->dev, "unrecognized port major revision %d\n",
2009 dev_dbg(dwc->dev, "hs-ports: %u ss-ports: %u\n",
2010 dwc->num_usb2_ports, dwc->num_usb3_ports);
2014 if (dwc->num_usb2_ports > DWC3_USB2_MAX_PORTS ||
2015 dwc->num_usb3_ports > DWC3_USB3_MAX_PORTS)
2021 static int dwc3_probe(struct platform_device *pdev)
2023 struct device *dev = &pdev->dev;
2024 struct resource *res, dwc_res;
2025 unsigned int hw_mode;
2030 dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
2036 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2038 dev_err(dev, "missing memory resource\n");
2042 dwc->xhci_resources[0].start = res->start;
2043 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
2045 dwc->xhci_resources[0].flags = res->flags;
2046 dwc->xhci_resources[0].name = res->name;
2049 * Request memory region but exclude xHCI regs,
2050 * since it will be requested by the xhci-plat driver.
2053 dwc_res.start += DWC3_GLOBALS_REGS_START;
2056 struct device_node *parent = of_get_parent(dev->of_node);
2058 if (of_device_is_compatible(parent, "realtek,rtd-dwc3")) {
2059 dwc_res.start -= DWC3_GLOBALS_REGS_START;
2060 dwc_res.start += DWC3_RTK_RTD_GLOBALS_REGS_START;
2063 of_node_put(parent);
2066 regs = devm_ioremap_resource(dev, &dwc_res);
2068 return PTR_ERR(regs);
2071 dwc->regs_size = resource_size(&dwc_res);
2073 dwc3_get_properties(dwc);
2075 dwc->reset = devm_reset_control_array_get_optional_shared(dev);
2076 if (IS_ERR(dwc->reset)) {
2077 ret = PTR_ERR(dwc->reset);
2081 ret = dwc3_get_clocks(dwc);
2085 ret = reset_control_deassert(dwc->reset);
2089 ret = dwc3_clk_enable(dwc);
2091 goto err_assert_reset;
2093 if (!dwc3_core_is_valid(dwc)) {
2094 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
2096 goto err_disable_clks;
2099 platform_set_drvdata(pdev, dwc);
2100 dwc3_cache_hwparams(dwc);
2102 if (!dwc->sysdev_is_parent &&
2103 DWC3_GHWPARAMS0_AWIDTH(dwc->hwparams.hwparams0) == 64) {
2104 ret = dma_set_mask_and_coherent(dwc->sysdev, DMA_BIT_MASK(64));
2106 goto err_disable_clks;
2110 * Currently only DWC3 controllers that are host-only capable
2111 * can have more than one port.
2113 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
2114 if (hw_mode == DWC3_GHWPARAMS0_MODE_HOST) {
2115 ret = dwc3_get_num_ports(dwc);
2117 goto err_disable_clks;
2119 dwc->num_usb2_ports = 1;
2120 dwc->num_usb3_ports = 1;
2123 spin_lock_init(&dwc->lock);
2124 mutex_init(&dwc->mutex);
2126 pm_runtime_get_noresume(dev);
2127 pm_runtime_set_active(dev);
2128 pm_runtime_use_autosuspend(dev);
2129 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
2130 pm_runtime_enable(dev);
2132 pm_runtime_forbid(dev);
2134 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
2136 dev_err(dwc->dev, "failed to allocate event buffers\n");
2141 dwc->edev = dwc3_get_extcon(dwc);
2142 if (IS_ERR(dwc->edev)) {
2143 ret = dev_err_probe(dwc->dev, PTR_ERR(dwc->edev), "failed to get extcon\n");
2144 goto err_free_event_buffers;
2147 ret = dwc3_get_dr_mode(dwc);
2149 goto err_free_event_buffers;
2151 ret = dwc3_core_init(dwc);
2153 dev_err_probe(dev, ret, "failed to initialize core\n");
2154 goto err_free_event_buffers;
2157 dwc3_check_params(dwc);
2158 dwc3_debugfs_init(dwc);
2160 ret = dwc3_core_init_mode(dwc);
2162 goto err_exit_debugfs;
2164 pm_runtime_put(dev);
2166 dma_set_max_seg_size(dev, UINT_MAX);
2171 dwc3_debugfs_exit(dwc);
2172 dwc3_event_buffers_cleanup(dwc);
2173 dwc3_phy_power_off(dwc);
2175 dwc3_ulpi_exit(dwc);
2176 err_free_event_buffers:
2177 dwc3_free_event_buffers(dwc);
2179 pm_runtime_allow(dev);
2180 pm_runtime_disable(dev);
2181 pm_runtime_dont_use_autosuspend(dev);
2182 pm_runtime_set_suspended(dev);
2183 pm_runtime_put_noidle(dev);
2185 dwc3_clk_disable(dwc);
2187 reset_control_assert(dwc->reset);
2190 power_supply_put(dwc->usb_psy);
2195 static void dwc3_remove(struct platform_device *pdev)
2197 struct dwc3 *dwc = platform_get_drvdata(pdev);
2199 pm_runtime_get_sync(&pdev->dev);
2201 dwc3_core_exit_mode(dwc);
2202 dwc3_debugfs_exit(dwc);
2204 dwc3_core_exit(dwc);
2205 dwc3_ulpi_exit(dwc);
2207 pm_runtime_allow(&pdev->dev);
2208 pm_runtime_disable(&pdev->dev);
2209 pm_runtime_dont_use_autosuspend(&pdev->dev);
2210 pm_runtime_put_noidle(&pdev->dev);
2212 * HACK: Clear the driver data, which is currently accessed by parent
2213 * glue drivers, before allowing the parent to suspend.
2215 platform_set_drvdata(pdev, NULL);
2216 pm_runtime_set_suspended(&pdev->dev);
2218 dwc3_free_event_buffers(dwc);
2221 power_supply_put(dwc->usb_psy);
2225 static int dwc3_core_init_for_resume(struct dwc3 *dwc)
2229 ret = reset_control_deassert(dwc->reset);
2233 ret = dwc3_clk_enable(dwc);
2237 ret = dwc3_core_init(dwc);
2244 dwc3_clk_disable(dwc);
2246 reset_control_assert(dwc->reset);
2251 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
2253 unsigned long flags;
2257 switch (dwc->current_dr_role) {
2258 case DWC3_GCTL_PRTCAP_DEVICE:
2259 if (pm_runtime_suspended(dwc->dev))
2261 dwc3_gadget_suspend(dwc);
2262 synchronize_irq(dwc->irq_gadget);
2263 dwc3_core_exit(dwc);
2265 case DWC3_GCTL_PRTCAP_HOST:
2266 if (!PMSG_IS_AUTO(msg) && !device_may_wakeup(dwc->dev)) {
2267 dwc3_core_exit(dwc);
2271 /* Let controller to suspend HSPHY before PHY driver suspends */
2272 if (dwc->dis_u2_susphy_quirk ||
2273 dwc->dis_enblslpm_quirk) {
2274 for (i = 0; i < dwc->num_usb2_ports; i++) {
2275 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(i));
2276 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
2277 DWC3_GUSB2PHYCFG_SUSPHY;
2278 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(i), reg);
2281 /* Give some time for USB2 PHY to suspend */
2282 usleep_range(5000, 6000);
2285 for (i = 0; i < dwc->num_usb2_ports; i++)
2286 phy_pm_runtime_put_sync(dwc->usb2_generic_phy[i]);
2287 for (i = 0; i < dwc->num_usb3_ports; i++)
2288 phy_pm_runtime_put_sync(dwc->usb3_generic_phy[i]);
2290 case DWC3_GCTL_PRTCAP_OTG:
2291 /* do nothing during runtime_suspend */
2292 if (PMSG_IS_AUTO(msg))
2295 if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
2296 spin_lock_irqsave(&dwc->lock, flags);
2297 dwc3_gadget_suspend(dwc);
2298 spin_unlock_irqrestore(&dwc->lock, flags);
2299 synchronize_irq(dwc->irq_gadget);
2303 dwc3_core_exit(dwc);
2313 static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
2315 unsigned long flags;
2320 switch (dwc->current_dr_role) {
2321 case DWC3_GCTL_PRTCAP_DEVICE:
2322 ret = dwc3_core_init_for_resume(dwc);
2326 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
2327 dwc3_gadget_resume(dwc);
2329 case DWC3_GCTL_PRTCAP_HOST:
2330 if (!PMSG_IS_AUTO(msg) && !device_may_wakeup(dwc->dev)) {
2331 ret = dwc3_core_init_for_resume(dwc);
2334 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
2337 /* Restore GUSB2PHYCFG bits that were modified in suspend */
2338 for (i = 0; i < dwc->num_usb2_ports; i++) {
2339 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(i));
2340 if (dwc->dis_u2_susphy_quirk)
2341 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
2343 if (dwc->dis_enblslpm_quirk)
2344 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
2346 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(i), reg);
2349 for (i = 0; i < dwc->num_usb2_ports; i++)
2350 phy_pm_runtime_get_sync(dwc->usb2_generic_phy[i]);
2351 for (i = 0; i < dwc->num_usb3_ports; i++)
2352 phy_pm_runtime_get_sync(dwc->usb3_generic_phy[i]);
2354 case DWC3_GCTL_PRTCAP_OTG:
2355 /* nothing to do on runtime_resume */
2356 if (PMSG_IS_AUTO(msg))
2359 ret = dwc3_core_init_for_resume(dwc);
2363 dwc3_set_prtcap(dwc, dwc->current_dr_role);
2366 if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
2367 dwc3_otg_host_init(dwc);
2368 } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
2369 spin_lock_irqsave(&dwc->lock, flags);
2370 dwc3_gadget_resume(dwc);
2371 spin_unlock_irqrestore(&dwc->lock, flags);
2383 static int dwc3_runtime_checks(struct dwc3 *dwc)
2385 switch (dwc->current_dr_role) {
2386 case DWC3_GCTL_PRTCAP_DEVICE:
2390 case DWC3_GCTL_PRTCAP_HOST:
2399 static int dwc3_runtime_suspend(struct device *dev)
2401 struct dwc3 *dwc = dev_get_drvdata(dev);
2404 if (dwc3_runtime_checks(dwc))
2407 ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
2414 static int dwc3_runtime_resume(struct device *dev)
2416 struct dwc3 *dwc = dev_get_drvdata(dev);
2419 ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
2423 switch (dwc->current_dr_role) {
2424 case DWC3_GCTL_PRTCAP_DEVICE:
2425 dwc3_gadget_process_pending_events(dwc);
2427 case DWC3_GCTL_PRTCAP_HOST:
2433 pm_runtime_mark_last_busy(dev);
2438 static int dwc3_runtime_idle(struct device *dev)
2440 struct dwc3 *dwc = dev_get_drvdata(dev);
2442 switch (dwc->current_dr_role) {
2443 case DWC3_GCTL_PRTCAP_DEVICE:
2444 if (dwc3_runtime_checks(dwc))
2447 case DWC3_GCTL_PRTCAP_HOST:
2453 pm_runtime_mark_last_busy(dev);
2454 pm_runtime_autosuspend(dev);
2458 #endif /* CONFIG_PM */
2460 #ifdef CONFIG_PM_SLEEP
2461 static int dwc3_suspend(struct device *dev)
2463 struct dwc3 *dwc = dev_get_drvdata(dev);
2466 ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
2470 pinctrl_pm_select_sleep_state(dev);
2475 static int dwc3_resume(struct device *dev)
2477 struct dwc3 *dwc = dev_get_drvdata(dev);
2480 pinctrl_pm_select_default_state(dev);
2482 pm_runtime_disable(dev);
2483 pm_runtime_set_active(dev);
2485 ret = dwc3_resume_common(dwc, PMSG_RESUME);
2487 pm_runtime_set_suspended(dev);
2491 pm_runtime_enable(dev);
2496 static void dwc3_complete(struct device *dev)
2498 struct dwc3 *dwc = dev_get_drvdata(dev);
2501 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST &&
2502 dwc->dis_split_quirk) {
2503 reg = dwc3_readl(dwc->regs, DWC3_GUCTL3);
2504 reg |= DWC3_GUCTL3_SPLITDISABLE;
2505 dwc3_writel(dwc->regs, DWC3_GUCTL3, reg);
2509 #define dwc3_complete NULL
2510 #endif /* CONFIG_PM_SLEEP */
2512 static const struct dev_pm_ops dwc3_dev_pm_ops = {
2513 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
2514 .complete = dwc3_complete,
2515 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
2520 static const struct of_device_id of_dwc3_match[] = {
2522 .compatible = "snps,dwc3"
2525 .compatible = "synopsys,dwc3"
2529 MODULE_DEVICE_TABLE(of, of_dwc3_match);
2534 #define ACPI_ID_INTEL_BSW "808622B7"
2536 static const struct acpi_device_id dwc3_acpi_match[] = {
2537 { ACPI_ID_INTEL_BSW, 0 },
2540 MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
2543 static struct platform_driver dwc3_driver = {
2544 .probe = dwc3_probe,
2545 .remove_new = dwc3_remove,
2548 .of_match_table = of_match_ptr(of_dwc3_match),
2549 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
2550 .pm = &dwc3_dev_pm_ops,
2554 module_platform_driver(dwc3_driver);
2556 MODULE_ALIAS("platform:dwc3");
2557 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
2558 MODULE_LICENSE("GPL v2");
2559 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");