1 // SPDX-License-Identifier: GPL-2.0
3 * core.c - DesignWare USB3 DRD Controller Core file
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com
11 #include <linux/clk.h>
12 #include <linux/version.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/interrupt.h>
20 #include <linux/ioport.h>
22 #include <linux/list.h>
23 #include <linux/delay.h>
24 #include <linux/dma-mapping.h>
26 #include <linux/of_graph.h>
27 #include <linux/acpi.h>
28 #include <linux/pinctrl/consumer.h>
29 #include <linux/reset.h>
30 #include <linux/bitfield.h>
32 #include <linux/usb/ch9.h>
33 #include <linux/usb/gadget.h>
34 #include <linux/usb/of.h>
35 #include <linux/usb/otg.h>
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)
113 for (i = 0; i < dwc->num_usb3_ports; i++) {
114 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(i));
115 if (enable && !dwc->dis_u3_susphy_quirk)
116 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
118 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
120 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(i), reg);
123 for (i = 0; i < dwc->num_usb2_ports; i++) {
124 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(i));
125 if (enable && !dwc->dis_u2_susphy_quirk)
126 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
128 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
130 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(i), reg);
134 void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
138 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
139 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
140 reg |= DWC3_GCTL_PRTCAPDIR(mode);
141 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
143 dwc->current_dr_role = mode;
146 static void __dwc3_set_mode(struct work_struct *work)
148 struct dwc3 *dwc = work_to_dwc(work);
155 mutex_lock(&dwc->mutex);
156 spin_lock_irqsave(&dwc->lock, flags);
157 desired_dr_role = dwc->desired_dr_role;
158 spin_unlock_irqrestore(&dwc->lock, flags);
160 pm_runtime_get_sync(dwc->dev);
162 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
163 dwc3_otg_update(dwc, 0);
165 if (!desired_dr_role)
168 if (desired_dr_role == dwc->current_dr_role)
171 if (desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev)
174 switch (dwc->current_dr_role) {
175 case DWC3_GCTL_PRTCAP_HOST:
178 case DWC3_GCTL_PRTCAP_DEVICE:
179 dwc3_gadget_exit(dwc);
180 dwc3_event_buffers_cleanup(dwc);
182 case DWC3_GCTL_PRTCAP_OTG:
184 spin_lock_irqsave(&dwc->lock, flags);
185 dwc->desired_otg_role = DWC3_OTG_ROLE_IDLE;
186 spin_unlock_irqrestore(&dwc->lock, flags);
187 dwc3_otg_update(dwc, 1);
194 * When current_dr_role is not set, there's no role switching.
195 * Only perform GCTL.CoreSoftReset when there's DRD role switching.
197 if (dwc->current_dr_role && ((DWC3_IP_IS(DWC3) ||
198 DWC3_VER_IS_PRIOR(DWC31, 190A)) &&
199 desired_dr_role != DWC3_GCTL_PRTCAP_OTG)) {
200 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
201 reg |= DWC3_GCTL_CORESOFTRESET;
202 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
205 * Wait for internal clocks to synchronized. DWC_usb31 and
206 * DWC_usb32 may need at least 50ms (less for DWC_usb3). To
207 * keep it consistent across different IPs, let's wait up to
208 * 100ms before clearing GCTL.CORESOFTRESET.
212 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
213 reg &= ~DWC3_GCTL_CORESOFTRESET;
214 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
217 spin_lock_irqsave(&dwc->lock, flags);
219 dwc3_set_prtcap(dwc, desired_dr_role);
221 spin_unlock_irqrestore(&dwc->lock, flags);
223 switch (desired_dr_role) {
224 case DWC3_GCTL_PRTCAP_HOST:
225 ret = dwc3_host_init(dwc);
227 dev_err(dwc->dev, "failed to initialize host\n");
230 otg_set_vbus(dwc->usb2_phy->otg, true);
232 for (i = 0; i < dwc->num_usb2_ports; i++)
233 phy_set_mode(dwc->usb2_generic_phy[i], PHY_MODE_USB_HOST);
234 for (i = 0; i < dwc->num_usb3_ports; i++)
235 phy_set_mode(dwc->usb3_generic_phy[i], PHY_MODE_USB_HOST);
237 if (dwc->dis_split_quirk) {
238 reg = dwc3_readl(dwc->regs, DWC3_GUCTL3);
239 reg |= DWC3_GUCTL3_SPLITDISABLE;
240 dwc3_writel(dwc->regs, DWC3_GUCTL3, reg);
244 case DWC3_GCTL_PRTCAP_DEVICE:
245 dwc3_core_soft_reset(dwc);
247 dwc3_event_buffers_setup(dwc);
250 otg_set_vbus(dwc->usb2_phy->otg, false);
251 phy_set_mode(dwc->usb2_generic_phy[0], PHY_MODE_USB_DEVICE);
252 phy_set_mode(dwc->usb3_generic_phy[0], PHY_MODE_USB_DEVICE);
254 ret = dwc3_gadget_init(dwc);
256 dev_err(dwc->dev, "failed to initialize peripheral\n");
258 case DWC3_GCTL_PRTCAP_OTG:
260 dwc3_otg_update(dwc, 0);
267 pm_runtime_mark_last_busy(dwc->dev);
268 pm_runtime_put_autosuspend(dwc->dev);
269 mutex_unlock(&dwc->mutex);
272 void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
276 if (dwc->dr_mode != USB_DR_MODE_OTG)
279 spin_lock_irqsave(&dwc->lock, flags);
280 dwc->desired_dr_role = mode;
281 spin_unlock_irqrestore(&dwc->lock, flags);
283 queue_work(system_freezable_wq, &dwc->drd_work);
286 u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
288 struct dwc3 *dwc = dep->dwc;
291 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
292 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
293 DWC3_GDBGFIFOSPACE_TYPE(type));
295 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
297 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
301 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
302 * @dwc: pointer to our context structure
304 int dwc3_core_soft_reset(struct dwc3 *dwc)
310 * We're resetting only the device side because, if we're in host mode,
311 * XHCI driver will reset the host block. If dwc3 was configured for
312 * host-only mode, then we can return early.
314 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST)
317 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
318 reg |= DWC3_DCTL_CSFTRST;
319 reg &= ~DWC3_DCTL_RUN_STOP;
320 dwc3_gadget_dctl_write_safe(dwc, reg);
323 * For DWC_usb31 controller 1.90a and later, the DCTL.CSFRST bit
324 * is cleared only after all the clocks are synchronized. This can
325 * take a little more than 50ms. Set the polling rate at 20ms
326 * for 10 times instead.
328 if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
332 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
333 if (!(reg & DWC3_DCTL_CSFTRST))
336 if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
342 dev_warn(dwc->dev, "DWC3 controller soft reset failed.\n");
347 * For DWC_usb31 controller 1.80a and prior, once DCTL.CSFRST bit
348 * is cleared, we must wait at least 50ms before accessing the PHY
349 * domain (synchronization delay).
351 if (DWC3_VER_IS_WITHIN(DWC31, ANY, 180A))
358 * dwc3_frame_length_adjustment - Adjusts frame length if required
359 * @dwc3: Pointer to our controller context structure
361 static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
366 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
372 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
373 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
374 if (dft != dwc->fladj) {
375 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
376 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
377 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
382 * dwc3_ref_clk_period - Reference clock period configuration
383 * Default reference clock period depends on hardware
384 * configuration. For systems with reference clock that differs
385 * from the default, this will set clock period in DWC3_GUCTL
387 * @dwc: Pointer to our controller context structure
389 static void dwc3_ref_clk_period(struct dwc3 *dwc)
391 unsigned long period;
398 rate = clk_get_rate(dwc->ref_clk);
401 period = NSEC_PER_SEC / rate;
402 } else if (dwc->ref_clk_per) {
403 period = dwc->ref_clk_per;
404 rate = NSEC_PER_SEC / period;
409 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
410 reg &= ~DWC3_GUCTL_REFCLKPER_MASK;
411 reg |= FIELD_PREP(DWC3_GUCTL_REFCLKPER_MASK, period);
412 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
414 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
418 * The calculation below is
420 * 125000 * (NSEC_PER_SEC / (rate * period) - 1)
422 * but rearranged for fixed-point arithmetic. The division must be
423 * 64-bit because 125000 * NSEC_PER_SEC doesn't fit in 32 bits (and
424 * neither does rate * period).
426 * Note that rate * period ~= NSEC_PER_SECOND, minus the number of
427 * nanoseconds of error caused by the truncation which happened during
428 * the division when calculating rate or period (whichever one was
429 * derived from the other). We first calculate the relative error, then
430 * scale it to units of 8 ppm.
432 fladj = div64_u64(125000ULL * NSEC_PER_SEC, (u64)rate * period);
436 * The documented 240MHz constant is scaled by 2 to get PLS1 as well.
438 decr = 480000000 / rate;
440 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
441 reg &= ~DWC3_GFLADJ_REFCLK_FLADJ_MASK
442 & ~DWC3_GFLADJ_240MHZDECR
443 & ~DWC3_GFLADJ_240MHZDECR_PLS1;
444 reg |= FIELD_PREP(DWC3_GFLADJ_REFCLK_FLADJ_MASK, fladj)
445 | FIELD_PREP(DWC3_GFLADJ_240MHZDECR, decr >> 1)
446 | FIELD_PREP(DWC3_GFLADJ_240MHZDECR_PLS1, decr & 1);
448 if (dwc->gfladj_refclk_lpm_sel)
449 reg |= DWC3_GFLADJ_REFCLK_LPM_SEL;
451 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
455 * dwc3_free_one_event_buffer - Frees one event buffer
456 * @dwc: Pointer to our controller context structure
457 * @evt: Pointer to event buffer to be freed
459 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
460 struct dwc3_event_buffer *evt)
462 dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
466 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
467 * @dwc: Pointer to our controller context structure
468 * @length: size of the event buffer
470 * Returns a pointer to the allocated event buffer structure on success
471 * otherwise ERR_PTR(errno).
473 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
476 struct dwc3_event_buffer *evt;
478 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
480 return ERR_PTR(-ENOMEM);
483 evt->length = length;
484 evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
486 return ERR_PTR(-ENOMEM);
488 evt->buf = dma_alloc_coherent(dwc->sysdev, length,
489 &evt->dma, GFP_KERNEL);
491 return ERR_PTR(-ENOMEM);
497 * dwc3_free_event_buffers - frees all allocated event buffers
498 * @dwc: Pointer to our controller context structure
500 static void dwc3_free_event_buffers(struct dwc3 *dwc)
502 struct dwc3_event_buffer *evt;
506 dwc3_free_one_event_buffer(dwc, evt);
510 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
511 * @dwc: pointer to our controller context structure
512 * @length: size of event buffer
514 * Returns 0 on success otherwise negative errno. In the error case, dwc
515 * may contain some buffers allocated but not all which were requested.
517 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned int length)
519 struct dwc3_event_buffer *evt;
520 unsigned int hw_mode;
522 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
523 if (hw_mode == DWC3_GHWPARAMS0_MODE_HOST) {
528 evt = dwc3_alloc_one_event_buffer(dwc, length);
530 dev_err(dwc->dev, "can't allocate event buffer\n");
539 * dwc3_event_buffers_setup - setup our allocated event buffers
540 * @dwc: pointer to our controller context structure
542 * Returns 0 on success otherwise negative errno.
544 int dwc3_event_buffers_setup(struct dwc3 *dwc)
546 struct dwc3_event_buffer *evt;
553 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
554 lower_32_bits(evt->dma));
555 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
556 upper_32_bits(evt->dma));
557 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
558 DWC3_GEVNTSIZ_SIZE(evt->length));
559 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
564 void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
566 struct dwc3_event_buffer *evt;
572 * Exynos platforms may not be able to access event buffer if the
573 * controller failed to halt on dwc3_core_exit().
575 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
576 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
583 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
584 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
585 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
586 | DWC3_GEVNTSIZ_SIZE(0));
587 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
590 static void dwc3_core_num_eps(struct dwc3 *dwc)
592 struct dwc3_hwparams *parms = &dwc->hwparams;
594 dwc->num_eps = DWC3_NUM_EPS(parms);
597 static void dwc3_cache_hwparams(struct dwc3 *dwc)
599 struct dwc3_hwparams *parms = &dwc->hwparams;
601 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
602 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
603 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
604 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
605 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
606 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
607 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
608 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
609 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
611 if (DWC3_IP_IS(DWC32))
612 parms->hwparams9 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS9);
615 static void dwc3_config_soc_bus(struct dwc3 *dwc)
617 if (dwc->gsbuscfg0_reqinfo != DWC3_GSBUSCFG0_REQINFO_UNSPECIFIED) {
620 reg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
621 reg &= ~DWC3_GSBUSCFG0_REQINFO(~0);
622 reg |= DWC3_GSBUSCFG0_REQINFO(dwc->gsbuscfg0_reqinfo);
623 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, reg);
627 static int dwc3_core_ulpi_init(struct dwc3 *dwc)
632 intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
634 if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
635 (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
636 dwc->hsphy_interface &&
637 !strncmp(dwc->hsphy_interface, "ulpi", 4)))
638 ret = dwc3_ulpi_init(dwc);
643 static int dwc3_ss_phy_setup(struct dwc3 *dwc, int index)
647 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(index));
650 * Make sure UX_EXIT_PX is cleared as that causes issues with some
651 * PHYs. Also, this bit is not supposed to be used in normal operation.
653 reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
656 * Above DWC_usb3.0 1.94a, it is recommended to set
657 * DWC3_GUSB3PIPECTL_SUSPHY to '0' during coreConsultant configuration.
658 * So default value will be '0' when the core is reset. Application
659 * needs to set it to '1' after the core initialization is completed.
661 * Similarly for DRD controllers, GUSB3PIPECTL.SUSPENDENABLE must be
662 * cleared after power-on reset, and it can be set after core
665 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
667 if (dwc->u2ss_inp3_quirk)
668 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
670 if (dwc->dis_rxdet_inp3_quirk)
671 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
673 if (dwc->req_p1p2p3_quirk)
674 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
676 if (dwc->del_p1p2p3_quirk)
677 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
679 if (dwc->del_phy_power_chg_quirk)
680 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
682 if (dwc->lfps_filter_quirk)
683 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
685 if (dwc->rx_detect_poll_quirk)
686 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
688 if (dwc->tx_de_emphasis_quirk)
689 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
691 if (dwc->dis_del_phy_power_chg_quirk)
692 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
694 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(index), reg);
699 static int dwc3_hs_phy_setup(struct dwc3 *dwc, int index)
703 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(index));
705 /* Select the HS PHY interface */
706 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
707 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
708 if (dwc->hsphy_interface &&
709 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
710 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
712 } else if (dwc->hsphy_interface &&
713 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
714 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
715 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(index), reg);
717 /* Relying on default value. */
718 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
722 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
727 switch (dwc->hsphy_mode) {
728 case USBPHY_INTERFACE_MODE_UTMI:
729 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
730 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
731 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
732 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
734 case USBPHY_INTERFACE_MODE_UTMIW:
735 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
736 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
737 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
738 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
745 * Above DWC_usb3.0 1.94a, it is recommended to set
746 * DWC3_GUSB2PHYCFG_SUSPHY to '0' during coreConsultant configuration.
747 * So default value will be '0' when the core is reset. Application
748 * needs to set it to '1' after the core initialization is completed.
750 * Similarly for DRD controllers, GUSB2PHYCFG.SUSPHY must be cleared
751 * after power-on reset, and it can be set after core initialization.
753 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
755 if (dwc->dis_enblslpm_quirk)
756 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
758 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM;
760 if (dwc->dis_u2_freeclk_exists_quirk || dwc->gfladj_refclk_lpm_sel)
761 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
764 * Some ULPI USB PHY does not support internal VBUS supply, to drive
765 * the CPEN pin requires the configuration of the ULPI DRVVBUSEXTERNAL
766 * bit of OTG_CTRL register. Controller configures the USB2 PHY
767 * ULPIEXTVBUSDRV bit[17] of the GUSB2PHYCFG register to drive vBus
768 * with an external supply.
770 if (dwc->ulpi_ext_vbus_drv)
771 reg |= DWC3_GUSB2PHYCFG_ULPIEXTVBUSDRV;
773 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(index), reg);
779 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
780 * @dwc: Pointer to our controller context structure
782 * Returns 0 on success. The USB PHY interfaces are configured but not
783 * initialized. The PHY interfaces and the PHYs get initialized together with
784 * the core in dwc3_core_init.
786 static int dwc3_phy_setup(struct dwc3 *dwc)
791 for (i = 0; i < dwc->num_usb3_ports; i++) {
792 ret = dwc3_ss_phy_setup(dwc, i);
797 for (i = 0; i < dwc->num_usb2_ports; i++) {
798 ret = dwc3_hs_phy_setup(dwc, i);
806 static int dwc3_phy_init(struct dwc3 *dwc)
812 usb_phy_init(dwc->usb2_phy);
813 usb_phy_init(dwc->usb3_phy);
815 for (i = 0; i < dwc->num_usb2_ports; i++) {
816 ret = phy_init(dwc->usb2_generic_phy[i]);
818 goto err_exit_usb2_phy;
821 for (j = 0; j < dwc->num_usb3_ports; j++) {
822 ret = phy_init(dwc->usb3_generic_phy[j]);
824 goto err_exit_usb3_phy;
831 phy_exit(dwc->usb3_generic_phy[j]);
835 phy_exit(dwc->usb2_generic_phy[i]);
837 usb_phy_shutdown(dwc->usb3_phy);
838 usb_phy_shutdown(dwc->usb2_phy);
843 static void dwc3_phy_exit(struct dwc3 *dwc)
847 for (i = 0; i < dwc->num_usb3_ports; i++)
848 phy_exit(dwc->usb3_generic_phy[i]);
850 for (i = 0; i < dwc->num_usb2_ports; i++)
851 phy_exit(dwc->usb2_generic_phy[i]);
853 usb_phy_shutdown(dwc->usb3_phy);
854 usb_phy_shutdown(dwc->usb2_phy);
857 static int dwc3_phy_power_on(struct dwc3 *dwc)
863 usb_phy_set_suspend(dwc->usb2_phy, 0);
864 usb_phy_set_suspend(dwc->usb3_phy, 0);
866 for (i = 0; i < dwc->num_usb2_ports; i++) {
867 ret = phy_power_on(dwc->usb2_generic_phy[i]);
869 goto err_power_off_usb2_phy;
872 for (j = 0; j < dwc->num_usb3_ports; j++) {
873 ret = phy_power_on(dwc->usb3_generic_phy[j]);
875 goto err_power_off_usb3_phy;
880 err_power_off_usb3_phy:
882 phy_power_off(dwc->usb3_generic_phy[j]);
884 err_power_off_usb2_phy:
886 phy_power_off(dwc->usb2_generic_phy[i]);
888 usb_phy_set_suspend(dwc->usb3_phy, 1);
889 usb_phy_set_suspend(dwc->usb2_phy, 1);
894 static void dwc3_phy_power_off(struct dwc3 *dwc)
898 for (i = 0; i < dwc->num_usb3_ports; i++)
899 phy_power_off(dwc->usb3_generic_phy[i]);
901 for (i = 0; i < dwc->num_usb2_ports; i++)
902 phy_power_off(dwc->usb2_generic_phy[i]);
904 usb_phy_set_suspend(dwc->usb3_phy, 1);
905 usb_phy_set_suspend(dwc->usb2_phy, 1);
908 static int dwc3_clk_enable(struct dwc3 *dwc)
912 ret = clk_prepare_enable(dwc->bus_clk);
916 ret = clk_prepare_enable(dwc->ref_clk);
918 goto disable_bus_clk;
920 ret = clk_prepare_enable(dwc->susp_clk);
922 goto disable_ref_clk;
924 ret = clk_prepare_enable(dwc->utmi_clk);
926 goto disable_susp_clk;
928 ret = clk_prepare_enable(dwc->pipe_clk);
930 goto disable_utmi_clk;
935 clk_disable_unprepare(dwc->utmi_clk);
937 clk_disable_unprepare(dwc->susp_clk);
939 clk_disable_unprepare(dwc->ref_clk);
941 clk_disable_unprepare(dwc->bus_clk);
945 static void dwc3_clk_disable(struct dwc3 *dwc)
947 clk_disable_unprepare(dwc->pipe_clk);
948 clk_disable_unprepare(dwc->utmi_clk);
949 clk_disable_unprepare(dwc->susp_clk);
950 clk_disable_unprepare(dwc->ref_clk);
951 clk_disable_unprepare(dwc->bus_clk);
954 static void dwc3_core_exit(struct dwc3 *dwc)
956 dwc3_event_buffers_cleanup(dwc);
957 dwc3_phy_power_off(dwc);
959 dwc3_clk_disable(dwc);
960 reset_control_assert(dwc->reset);
963 static bool dwc3_core_is_valid(struct dwc3 *dwc)
967 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
968 dwc->ip = DWC3_GSNPS_ID(reg);
970 /* This should read as U3 followed by revision number */
971 if (DWC3_IP_IS(DWC3)) {
973 } else if (DWC3_IP_IS(DWC31) || DWC3_IP_IS(DWC32)) {
974 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
975 dwc->version_type = dwc3_readl(dwc->regs, DWC3_VER_TYPE);
983 static void dwc3_core_setup_global_control(struct dwc3 *dwc)
985 unsigned int power_opt;
986 unsigned int hw_mode;
989 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
990 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
991 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
992 power_opt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
995 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
997 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
998 * issue which would cause xHCI compliance tests to fail.
1000 * Because of that we cannot enable clock gating on such
1005 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
1008 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
1009 dwc->dr_mode == USB_DR_MODE_OTG) &&
1010 DWC3_VER_IS_WITHIN(DWC3, 210A, 250A))
1011 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
1013 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
1015 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
1017 * REVISIT Enabling this bit so that host-mode hibernation
1018 * will work. Device-mode hibernation is not yet implemented.
1020 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
1028 * This is a workaround for STAR#4846132, which only affects
1029 * DWC_usb31 version2.00a operating in host mode.
1031 * There is a problem in DWC_usb31 version 2.00a operating
1032 * in host mode that would cause a CSR read timeout When CSR
1033 * read coincides with RAM Clock Gating Entry. By disable
1034 * Clock Gating, sacrificing power consumption for normal
1037 if (power_opt != DWC3_GHWPARAMS1_EN_PWROPT_NO &&
1038 hw_mode != DWC3_GHWPARAMS0_MODE_GADGET && DWC3_VER_IS(DWC31, 200A))
1039 reg |= DWC3_GCTL_DSBLCLKGTNG;
1041 /* check if current dwc3 is on simulation board */
1042 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
1043 dev_info(dwc->dev, "Running with FPGA optimizations\n");
1044 dwc->is_fpga = true;
1047 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
1048 "disable_scramble cannot be used on non-FPGA builds\n");
1050 if (dwc->disable_scramble_quirk && dwc->is_fpga)
1051 reg |= DWC3_GCTL_DISSCRAMBLE;
1053 reg &= ~DWC3_GCTL_DISSCRAMBLE;
1055 if (dwc->u2exit_lfps_quirk)
1056 reg |= DWC3_GCTL_U2EXIT_LFPS;
1059 * WORKAROUND: DWC3 revisions <1.90a have a bug
1060 * where the device can fail to connect at SuperSpeed
1061 * and falls back to high-speed mode which causes
1062 * the device to enter a Connect/Disconnect loop
1064 if (DWC3_VER_IS_PRIOR(DWC3, 190A))
1065 reg |= DWC3_GCTL_U2RSTECN;
1067 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1070 static int dwc3_core_get_phy(struct dwc3 *dwc);
1071 static int dwc3_core_ulpi_init(struct dwc3 *dwc);
1073 /* set global incr burst type configuration registers */
1074 static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
1076 struct device *dev = dwc->dev;
1077 /* incrx_mode : for INCR burst type. */
1079 /* incrx_size : for size of INCRX burst. */
1087 cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
1090 * Handle property "snps,incr-burst-type-adjustment".
1091 * Get the number of value from this property:
1092 * result <= 0, means this property is not supported.
1093 * result = 1, means INCRx burst mode supported.
1094 * result > 1, means undefined length burst mode supported.
1096 ntype = device_property_count_u32(dev, "snps,incr-burst-type-adjustment");
1100 vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL);
1104 /* Get INCR burst type, and parse it */
1105 ret = device_property_read_u32_array(dev,
1106 "snps,incr-burst-type-adjustment", vals, ntype);
1109 dev_err(dev, "Error to get property\n");
1116 /* INCRX (undefined length) burst mode */
1117 incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
1118 for (i = 1; i < ntype; i++) {
1119 if (vals[i] > incrx_size)
1120 incrx_size = vals[i];
1123 /* INCRX burst mode */
1124 incrx_mode = INCRX_BURST_MODE;
1129 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
1130 cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
1132 cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
1133 switch (incrx_size) {
1135 cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
1138 cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
1141 cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
1144 cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
1147 cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
1150 cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
1153 cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
1158 dev_err(dev, "Invalid property\n");
1162 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
1165 static void dwc3_set_power_down_clk_scale(struct dwc3 *dwc)
1174 * The power down scale field specifies how many suspend_clk
1175 * periods fit into a 16KHz clock period. When performing
1176 * the division, round up the remainder.
1178 * The power down scale value is calculated using the fastest
1179 * frequency of the suspend_clk. If it isn't fixed (but within
1180 * the accuracy requirement), the driver may not know the max
1181 * rate of the suspend_clk, so only update the power down scale
1182 * if the default is less than the calculated value from
1183 * clk_get_rate() or if the default is questionably high
1184 * (3x or more) to be within the requirement.
1186 scale = DIV_ROUND_UP(clk_get_rate(dwc->susp_clk), 16000);
1187 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1188 if ((reg & DWC3_GCTL_PWRDNSCALE_MASK) < DWC3_GCTL_PWRDNSCALE(scale) ||
1189 (reg & DWC3_GCTL_PWRDNSCALE_MASK) > DWC3_GCTL_PWRDNSCALE(scale*3)) {
1190 reg &= ~(DWC3_GCTL_PWRDNSCALE_MASK);
1191 reg |= DWC3_GCTL_PWRDNSCALE(scale);
1192 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1196 static void dwc3_config_threshold(struct dwc3 *dwc)
1205 * Must config both number of packets and max burst settings to enable
1206 * RX and/or TX threshold.
1208 if (!DWC3_IP_IS(DWC3) && dwc->dr_mode == USB_DR_MODE_HOST) {
1209 rx_thr_num = dwc->rx_thr_num_pkt_prd;
1210 rx_maxburst = dwc->rx_max_burst_prd;
1211 tx_thr_num = dwc->tx_thr_num_pkt_prd;
1212 tx_maxburst = dwc->tx_max_burst_prd;
1214 if (rx_thr_num && rx_maxburst) {
1215 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1216 reg |= DWC31_RXTHRNUMPKTSEL_PRD;
1218 reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
1219 reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
1221 reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
1222 reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
1224 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1227 if (tx_thr_num && tx_maxburst) {
1228 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1229 reg |= DWC31_TXTHRNUMPKTSEL_PRD;
1231 reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
1232 reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
1234 reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
1235 reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
1237 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1241 rx_thr_num = dwc->rx_thr_num_pkt;
1242 rx_maxburst = dwc->rx_max_burst;
1243 tx_thr_num = dwc->tx_thr_num_pkt;
1244 tx_maxburst = dwc->tx_max_burst;
1246 if (DWC3_IP_IS(DWC3)) {
1247 if (rx_thr_num && rx_maxburst) {
1248 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1249 reg |= DWC3_GRXTHRCFG_PKTCNTSEL;
1251 reg &= ~DWC3_GRXTHRCFG_RXPKTCNT(~0);
1252 reg |= DWC3_GRXTHRCFG_RXPKTCNT(rx_thr_num);
1254 reg &= ~DWC3_GRXTHRCFG_MAXRXBURSTSIZE(~0);
1255 reg |= DWC3_GRXTHRCFG_MAXRXBURSTSIZE(rx_maxburst);
1257 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1260 if (tx_thr_num && tx_maxburst) {
1261 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1262 reg |= DWC3_GTXTHRCFG_PKTCNTSEL;
1264 reg &= ~DWC3_GTXTHRCFG_TXPKTCNT(~0);
1265 reg |= DWC3_GTXTHRCFG_TXPKTCNT(tx_thr_num);
1267 reg &= ~DWC3_GTXTHRCFG_MAXTXBURSTSIZE(~0);
1268 reg |= DWC3_GTXTHRCFG_MAXTXBURSTSIZE(tx_maxburst);
1270 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1273 if (rx_thr_num && rx_maxburst) {
1274 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1275 reg |= DWC31_GRXTHRCFG_PKTCNTSEL;
1277 reg &= ~DWC31_GRXTHRCFG_RXPKTCNT(~0);
1278 reg |= DWC31_GRXTHRCFG_RXPKTCNT(rx_thr_num);
1280 reg &= ~DWC31_GRXTHRCFG_MAXRXBURSTSIZE(~0);
1281 reg |= DWC31_GRXTHRCFG_MAXRXBURSTSIZE(rx_maxburst);
1283 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1286 if (tx_thr_num && tx_maxburst) {
1287 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1288 reg |= DWC31_GTXTHRCFG_PKTCNTSEL;
1290 reg &= ~DWC31_GTXTHRCFG_TXPKTCNT(~0);
1291 reg |= DWC31_GTXTHRCFG_TXPKTCNT(tx_thr_num);
1293 reg &= ~DWC31_GTXTHRCFG_MAXTXBURSTSIZE(~0);
1294 reg |= DWC31_GTXTHRCFG_MAXTXBURSTSIZE(tx_maxburst);
1296 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1302 * dwc3_core_init - Low-level initialization of DWC3 Core
1303 * @dwc: Pointer to our controller context structure
1305 * Returns 0 on success otherwise negative errno.
1307 static int dwc3_core_init(struct dwc3 *dwc)
1309 unsigned int hw_mode;
1313 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
1316 * Write Linux Version Code to our GUID register so it's easy to figure
1317 * out which kernel version a bug was found.
1319 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
1321 ret = dwc3_phy_setup(dwc);
1325 if (!dwc->ulpi_ready) {
1326 ret = dwc3_core_ulpi_init(dwc);
1328 if (ret == -ETIMEDOUT) {
1329 dwc3_core_soft_reset(dwc);
1330 ret = -EPROBE_DEFER;
1334 dwc->ulpi_ready = true;
1337 if (!dwc->phys_ready) {
1338 ret = dwc3_core_get_phy(dwc);
1341 dwc->phys_ready = true;
1344 ret = dwc3_phy_init(dwc);
1348 ret = dwc3_core_soft_reset(dwc);
1352 dwc3_core_setup_global_control(dwc);
1353 dwc3_core_num_eps(dwc);
1355 /* Set power down scale of suspend_clk */
1356 dwc3_set_power_down_clk_scale(dwc);
1358 /* Adjust Frame Length */
1359 dwc3_frame_length_adjustment(dwc);
1361 /* Adjust Reference Clock Period */
1362 dwc3_ref_clk_period(dwc);
1364 dwc3_set_incr_burst_type(dwc);
1366 dwc3_config_soc_bus(dwc);
1368 ret = dwc3_phy_power_on(dwc);
1372 ret = dwc3_event_buffers_setup(dwc);
1374 dev_err(dwc->dev, "failed to setup event buffers\n");
1375 goto err_power_off_phy;
1379 * ENDXFER polling is available on version 3.10a and later of
1380 * the DWC_usb3 controller. It is NOT available in the
1381 * DWC_usb31 controller.
1383 if (DWC3_VER_IS_WITHIN(DWC3, 310A, ANY)) {
1384 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
1385 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
1386 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
1390 * STAR 9001285599: This issue affects DWC_usb3 version 3.20a
1391 * only. If the PM TIMER ECM is enabled through GUCTL2[19], the
1392 * link compliance test (TD7.21) may fail. If the ECN is not
1393 * enabled (GUCTL2[19] = 0), the controller will use the old timer
1394 * value (5us), which is still acceptable for the link compliance
1395 * test. Therefore, do not enable PM TIMER ECM in 3.20a by
1396 * setting GUCTL2[19] by default; instead, use GUCTL2[19] = 0.
1398 if (DWC3_VER_IS(DWC3, 320A)) {
1399 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
1400 reg &= ~DWC3_GUCTL2_LC_TIMER;
1401 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
1405 * When configured in HOST mode, after issuing U3/L2 exit controller
1406 * fails to send proper CRC checksum in CRC5 feild. Because of this
1407 * behaviour Transaction Error is generated, resulting in reset and
1408 * re-enumeration of usb device attached. All the termsel, xcvrsel,
1409 * opmode becomes 0 during end of resume. Enabling bit 10 of GUCTL1
1410 * will correct this problem. This option is to support certain
1413 if (dwc->resume_hs_terminations) {
1414 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1415 reg |= DWC3_GUCTL1_RESUME_OPMODE_HS_HOST;
1416 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1419 if (!DWC3_VER_IS_PRIOR(DWC3, 250A)) {
1420 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1423 * Enable hardware control of sending remote wakeup
1424 * in HS when the device is in the L1 state.
1426 if (!DWC3_VER_IS_PRIOR(DWC3, 290A))
1427 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
1430 * Decouple USB 2.0 L1 & L2 events which will allow for
1431 * gadget driver to only receive U3/L2 suspend & wakeup
1432 * events and prevent the more frequent L1 LPM transitions
1433 * from interrupting the driver.
1435 if (!DWC3_VER_IS_PRIOR(DWC3, 300A))
1436 reg |= DWC3_GUCTL1_DEV_DECOUPLE_L1L2_EVT;
1438 if (dwc->dis_tx_ipgap_linecheck_quirk)
1439 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
1441 if (dwc->parkmode_disable_ss_quirk)
1442 reg |= DWC3_GUCTL1_PARKMODE_DISABLE_SS;
1444 if (dwc->parkmode_disable_hs_quirk)
1445 reg |= DWC3_GUCTL1_PARKMODE_DISABLE_HS;
1447 if (DWC3_VER_IS_WITHIN(DWC3, 290A, ANY)) {
1448 if (dwc->maximum_speed == USB_SPEED_FULL ||
1449 dwc->maximum_speed == USB_SPEED_HIGH)
1450 reg |= DWC3_GUCTL1_DEV_FORCE_20_CLK_FOR_30_CLK;
1452 reg &= ~DWC3_GUCTL1_DEV_FORCE_20_CLK_FOR_30_CLK;
1455 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1458 dwc3_config_threshold(dwc);
1461 * Modify this for all supported Super Speed ports when
1462 * multiport support is added.
1464 if (hw_mode != DWC3_GHWPARAMS0_MODE_GADGET &&
1465 (DWC3_IP_IS(DWC31)) &&
1466 dwc->maximum_speed == USB_SPEED_SUPER) {
1467 reg = dwc3_readl(dwc->regs, DWC3_LLUCTL);
1468 reg |= DWC3_LLUCTL_FORCE_GEN1;
1469 dwc3_writel(dwc->regs, DWC3_LLUCTL, reg);
1475 dwc3_phy_power_off(dwc);
1479 dwc3_ulpi_exit(dwc);
1484 static int dwc3_core_get_phy(struct dwc3 *dwc)
1486 struct device *dev = dwc->dev;
1487 struct device_node *node = dev->of_node;
1493 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
1494 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
1496 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1497 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
1500 if (IS_ERR(dwc->usb2_phy)) {
1501 ret = PTR_ERR(dwc->usb2_phy);
1502 if (ret == -ENXIO || ret == -ENODEV)
1503 dwc->usb2_phy = NULL;
1505 return dev_err_probe(dev, ret, "no usb2 phy configured\n");
1508 if (IS_ERR(dwc->usb3_phy)) {
1509 ret = PTR_ERR(dwc->usb3_phy);
1510 if (ret == -ENXIO || ret == -ENODEV)
1511 dwc->usb3_phy = NULL;
1513 return dev_err_probe(dev, ret, "no usb3 phy configured\n");
1516 for (i = 0; i < dwc->num_usb2_ports; i++) {
1517 if (dwc->num_usb2_ports == 1)
1518 snprintf(phy_name, sizeof(phy_name), "usb2-phy");
1520 snprintf(phy_name, sizeof(phy_name), "usb2-%u", i);
1522 dwc->usb2_generic_phy[i] = devm_phy_get(dev, phy_name);
1523 if (IS_ERR(dwc->usb2_generic_phy[i])) {
1524 ret = PTR_ERR(dwc->usb2_generic_phy[i]);
1525 if (ret == -ENOSYS || ret == -ENODEV)
1526 dwc->usb2_generic_phy[i] = NULL;
1528 return dev_err_probe(dev, ret, "failed to lookup phy %s\n",
1533 for (i = 0; i < dwc->num_usb3_ports; i++) {
1534 if (dwc->num_usb3_ports == 1)
1535 snprintf(phy_name, sizeof(phy_name), "usb3-phy");
1537 snprintf(phy_name, sizeof(phy_name), "usb3-%u", i);
1539 dwc->usb3_generic_phy[i] = devm_phy_get(dev, phy_name);
1540 if (IS_ERR(dwc->usb3_generic_phy[i])) {
1541 ret = PTR_ERR(dwc->usb3_generic_phy[i]);
1542 if (ret == -ENOSYS || ret == -ENODEV)
1543 dwc->usb3_generic_phy[i] = NULL;
1545 return dev_err_probe(dev, ret, "failed to lookup phy %s\n",
1553 static int dwc3_core_init_mode(struct dwc3 *dwc)
1555 struct device *dev = dwc->dev;
1559 switch (dwc->dr_mode) {
1560 case USB_DR_MODE_PERIPHERAL:
1561 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1564 otg_set_vbus(dwc->usb2_phy->otg, false);
1565 phy_set_mode(dwc->usb2_generic_phy[0], PHY_MODE_USB_DEVICE);
1566 phy_set_mode(dwc->usb3_generic_phy[0], PHY_MODE_USB_DEVICE);
1568 ret = dwc3_gadget_init(dwc);
1570 return dev_err_probe(dev, ret, "failed to initialize gadget\n");
1572 case USB_DR_MODE_HOST:
1573 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1576 otg_set_vbus(dwc->usb2_phy->otg, true);
1577 for (i = 0; i < dwc->num_usb2_ports; i++)
1578 phy_set_mode(dwc->usb2_generic_phy[i], PHY_MODE_USB_HOST);
1579 for (i = 0; i < dwc->num_usb3_ports; i++)
1580 phy_set_mode(dwc->usb3_generic_phy[i], PHY_MODE_USB_HOST);
1582 ret = dwc3_host_init(dwc);
1584 return dev_err_probe(dev, ret, "failed to initialize host\n");
1586 case USB_DR_MODE_OTG:
1587 INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
1588 ret = dwc3_drd_init(dwc);
1590 return dev_err_probe(dev, ret, "failed to initialize dual-role\n");
1593 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1600 static void dwc3_core_exit_mode(struct dwc3 *dwc)
1602 switch (dwc->dr_mode) {
1603 case USB_DR_MODE_PERIPHERAL:
1604 dwc3_gadget_exit(dwc);
1606 case USB_DR_MODE_HOST:
1607 dwc3_host_exit(dwc);
1609 case USB_DR_MODE_OTG:
1617 /* de-assert DRVVBUS for HOST and OTG mode */
1618 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1621 static void dwc3_get_software_properties(struct dwc3 *dwc)
1623 struct device *tmpdev;
1624 u16 gsbuscfg0_reqinfo;
1627 dwc->gsbuscfg0_reqinfo = DWC3_GSBUSCFG0_REQINFO_UNSPECIFIED;
1630 * Iterate over all parent nodes for finding swnode properties
1631 * and non-DT (non-ABI) properties.
1633 for (tmpdev = dwc->dev; tmpdev; tmpdev = tmpdev->parent) {
1634 ret = device_property_read_u16(tmpdev,
1635 "snps,gsbuscfg0-reqinfo",
1636 &gsbuscfg0_reqinfo);
1638 dwc->gsbuscfg0_reqinfo = gsbuscfg0_reqinfo;
1642 static void dwc3_get_properties(struct dwc3 *dwc)
1644 struct device *dev = dwc->dev;
1645 u8 lpm_nyet_threshold;
1648 u8 rx_thr_num_pkt = 0;
1649 u8 rx_max_burst = 0;
1650 u8 tx_thr_num_pkt = 0;
1651 u8 tx_max_burst = 0;
1652 u8 rx_thr_num_pkt_prd = 0;
1653 u8 rx_max_burst_prd = 0;
1654 u8 tx_thr_num_pkt_prd = 0;
1655 u8 tx_max_burst_prd = 0;
1656 u8 tx_fifo_resize_max_num;
1657 const char *usb_psy_name;
1660 /* default to highest possible threshold */
1661 lpm_nyet_threshold = 0xf;
1663 /* default to -3.5dB de-emphasis */
1667 * default to assert utmi_sleep_n and use maximum allowed HIRD
1668 * threshold value of 0b1100
1670 hird_threshold = 12;
1673 * default to a TXFIFO size large enough to fit 6 max packets. This
1674 * allows for systems with larger bus latencies to have some headroom
1675 * for endpoints that have a large bMaxBurst value.
1677 tx_fifo_resize_max_num = 6;
1679 dwc->maximum_speed = usb_get_maximum_speed(dev);
1680 dwc->max_ssp_rate = usb_get_maximum_ssp_rate(dev);
1681 dwc->dr_mode = usb_get_dr_mode(dev);
1682 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
1684 dwc->sysdev_is_parent = device_property_read_bool(dev,
1685 "linux,sysdev_is_parent");
1686 if (dwc->sysdev_is_parent)
1687 dwc->sysdev = dwc->dev->parent;
1689 dwc->sysdev = dwc->dev;
1691 dwc->sys_wakeup = device_may_wakeup(dwc->sysdev);
1693 ret = device_property_read_string(dev, "usb-psy-name", &usb_psy_name);
1695 dwc->usb_psy = power_supply_get_by_name(usb_psy_name);
1697 dev_err(dev, "couldn't get usb power supply\n");
1700 dwc->has_lpm_erratum = device_property_read_bool(dev,
1701 "snps,has-lpm-erratum");
1702 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
1703 &lpm_nyet_threshold);
1704 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
1705 "snps,is-utmi-l1-suspend");
1706 device_property_read_u8(dev, "snps,hird-threshold",
1708 dwc->dis_start_transfer_quirk = device_property_read_bool(dev,
1709 "snps,dis-start-transfer-quirk");
1710 dwc->usb3_lpm_capable = device_property_read_bool(dev,
1711 "snps,usb3_lpm_capable");
1712 dwc->usb2_lpm_disable = device_property_read_bool(dev,
1713 "snps,usb2-lpm-disable");
1714 dwc->usb2_gadget_lpm_disable = device_property_read_bool(dev,
1715 "snps,usb2-gadget-lpm-disable");
1716 device_property_read_u8(dev, "snps,rx-thr-num-pkt",
1718 device_property_read_u8(dev, "snps,rx-max-burst",
1720 device_property_read_u8(dev, "snps,tx-thr-num-pkt",
1722 device_property_read_u8(dev, "snps,tx-max-burst",
1724 device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
1725 &rx_thr_num_pkt_prd);
1726 device_property_read_u8(dev, "snps,rx-max-burst-prd",
1728 device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
1729 &tx_thr_num_pkt_prd);
1730 device_property_read_u8(dev, "snps,tx-max-burst-prd",
1732 dwc->do_fifo_resize = device_property_read_bool(dev,
1734 if (dwc->do_fifo_resize)
1735 device_property_read_u8(dev, "tx-fifo-max-num",
1736 &tx_fifo_resize_max_num);
1738 dwc->disable_scramble_quirk = device_property_read_bool(dev,
1739 "snps,disable_scramble_quirk");
1740 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
1741 "snps,u2exit_lfps_quirk");
1742 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
1743 "snps,u2ss_inp3_quirk");
1744 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
1745 "snps,req_p1p2p3_quirk");
1746 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
1747 "snps,del_p1p2p3_quirk");
1748 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
1749 "snps,del_phy_power_chg_quirk");
1750 dwc->lfps_filter_quirk = device_property_read_bool(dev,
1751 "snps,lfps_filter_quirk");
1752 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
1753 "snps,rx_detect_poll_quirk");
1754 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
1755 "snps,dis_u3_susphy_quirk");
1756 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
1757 "snps,dis_u2_susphy_quirk");
1758 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1759 "snps,dis_enblslpm_quirk");
1760 dwc->dis_u1_entry_quirk = device_property_read_bool(dev,
1761 "snps,dis-u1-entry-quirk");
1762 dwc->dis_u2_entry_quirk = device_property_read_bool(dev,
1763 "snps,dis-u2-entry-quirk");
1764 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1765 "snps,dis_rxdet_inp3_quirk");
1766 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1767 "snps,dis-u2-freeclk-exists-quirk");
1768 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1769 "snps,dis-del-phy-power-chg-quirk");
1770 dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1771 "snps,dis-tx-ipgap-linecheck-quirk");
1772 dwc->resume_hs_terminations = device_property_read_bool(dev,
1773 "snps,resume-hs-terminations");
1774 dwc->ulpi_ext_vbus_drv = device_property_read_bool(dev,
1775 "snps,ulpi-ext-vbus-drv");
1776 dwc->parkmode_disable_ss_quirk = device_property_read_bool(dev,
1777 "snps,parkmode-disable-ss-quirk");
1778 dwc->parkmode_disable_hs_quirk = device_property_read_bool(dev,
1779 "snps,parkmode-disable-hs-quirk");
1780 dwc->gfladj_refclk_lpm_sel = device_property_read_bool(dev,
1781 "snps,gfladj-refclk-lpm-sel-quirk");
1783 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
1784 "snps,tx_de_emphasis_quirk");
1785 device_property_read_u8(dev, "snps,tx_de_emphasis",
1787 device_property_read_string(dev, "snps,hsphy_interface",
1788 &dwc->hsphy_interface);
1789 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
1791 device_property_read_u32(dev, "snps,ref-clock-period-ns",
1794 dwc->dis_metastability_quirk = device_property_read_bool(dev,
1795 "snps,dis_metastability_quirk");
1797 dwc->dis_split_quirk = device_property_read_bool(dev,
1798 "snps,dis-split-quirk");
1800 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1801 dwc->tx_de_emphasis = tx_de_emphasis;
1803 dwc->hird_threshold = hird_threshold;
1805 dwc->rx_thr_num_pkt = rx_thr_num_pkt;
1806 dwc->rx_max_burst = rx_max_burst;
1808 dwc->tx_thr_num_pkt = tx_thr_num_pkt;
1809 dwc->tx_max_burst = tx_max_burst;
1811 dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd;
1812 dwc->rx_max_burst_prd = rx_max_burst_prd;
1814 dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd;
1815 dwc->tx_max_burst_prd = tx_max_burst_prd;
1817 dwc->imod_interval = 0;
1819 dwc->tx_fifo_resize_max_num = tx_fifo_resize_max_num;
1822 /* check whether the core supports IMOD */
1823 bool dwc3_has_imod(struct dwc3 *dwc)
1825 return DWC3_VER_IS_WITHIN(DWC3, 300A, ANY) ||
1826 DWC3_VER_IS_WITHIN(DWC31, 120A, ANY) ||
1830 static void dwc3_check_params(struct dwc3 *dwc)
1832 struct device *dev = dwc->dev;
1833 unsigned int hwparam_gen =
1834 DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3);
1836 /* Check for proper value of imod_interval */
1837 if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
1838 dev_warn(dwc->dev, "Interrupt moderation not supported\n");
1839 dwc->imod_interval = 0;
1843 * Workaround for STAR 9000961433 which affects only version
1844 * 3.00a of the DWC_usb3 core. This prevents the controller
1845 * interrupt from being masked while handling events. IMOD
1846 * allows us to work around this issue. Enable it for the
1849 if (!dwc->imod_interval &&
1850 DWC3_VER_IS(DWC3, 300A))
1851 dwc->imod_interval = 1;
1853 /* Check the maximum_speed parameter */
1854 switch (dwc->maximum_speed) {
1855 case USB_SPEED_FULL:
1856 case USB_SPEED_HIGH:
1858 case USB_SPEED_SUPER:
1859 if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS)
1860 dev_warn(dev, "UDC doesn't support Gen 1\n");
1862 case USB_SPEED_SUPER_PLUS:
1863 if ((DWC3_IP_IS(DWC32) &&
1864 hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS) ||
1865 (!DWC3_IP_IS(DWC32) &&
1866 hwparam_gen != DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1867 dev_warn(dev, "UDC doesn't support SSP\n");
1870 dev_err(dev, "invalid maximum_speed parameter %d\n",
1871 dwc->maximum_speed);
1873 case USB_SPEED_UNKNOWN:
1874 switch (hwparam_gen) {
1875 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
1876 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1878 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
1879 if (DWC3_IP_IS(DWC32))
1880 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1882 dwc->maximum_speed = USB_SPEED_SUPER;
1884 case DWC3_GHWPARAMS3_SSPHY_IFC_DIS:
1885 dwc->maximum_speed = USB_SPEED_HIGH;
1888 dwc->maximum_speed = USB_SPEED_SUPER;
1895 * Currently the controller does not have visibility into the HW
1896 * parameter to determine the maximum number of lanes the HW supports.
1897 * If the number of lanes is not specified in the device property, then
1898 * set the default to support dual-lane for DWC_usb32 and single-lane
1899 * for DWC_usb31 for super-speed-plus.
1901 if (dwc->maximum_speed == USB_SPEED_SUPER_PLUS) {
1902 switch (dwc->max_ssp_rate) {
1903 case USB_SSP_GEN_2x1:
1904 if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_GEN1)
1905 dev_warn(dev, "UDC only supports Gen 1\n");
1907 case USB_SSP_GEN_1x2:
1908 case USB_SSP_GEN_2x2:
1909 if (DWC3_IP_IS(DWC31))
1910 dev_warn(dev, "UDC only supports single lane\n");
1912 case USB_SSP_GEN_UNKNOWN:
1914 switch (hwparam_gen) {
1915 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
1916 if (DWC3_IP_IS(DWC32))
1917 dwc->max_ssp_rate = USB_SSP_GEN_2x2;
1919 dwc->max_ssp_rate = USB_SSP_GEN_2x1;
1921 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
1922 if (DWC3_IP_IS(DWC32))
1923 dwc->max_ssp_rate = USB_SSP_GEN_1x2;
1931 static struct extcon_dev *dwc3_get_extcon(struct dwc3 *dwc)
1933 struct device *dev = dwc->dev;
1934 struct device_node *np_phy;
1935 struct extcon_dev *edev = NULL;
1938 if (device_property_read_bool(dev, "extcon"))
1939 return extcon_get_edev_by_phandle(dev, 0);
1942 * Device tree platforms should get extcon via phandle.
1943 * On ACPI platforms, we get the name from a device property.
1944 * This device property is for kernel internal use only and
1945 * is expected to be set by the glue code.
1947 if (device_property_read_string(dev, "linux,extcon-name", &name) == 0)
1948 return extcon_get_extcon_dev(name);
1951 * Check explicitly if "usb-role-switch" is used since
1952 * extcon_find_edev_by_node() can not be used to check the absence of
1953 * an extcon device. In the absence of an device it will always return
1956 if (IS_ENABLED(CONFIG_USB_ROLE_SWITCH) &&
1957 device_property_read_bool(dev, "usb-role-switch"))
1961 * Try to get an extcon device from the USB PHY controller's "port"
1962 * node. Check if it has the "port" node first, to avoid printing the
1963 * error message from underlying code, as it's a valid case: extcon
1964 * device (and "port" node) may be missing in case of "usb-role-switch"
1967 np_phy = of_parse_phandle(dev->of_node, "phys", 0);
1968 if (of_graph_is_present(np_phy)) {
1969 struct device_node *np_conn;
1971 np_conn = of_graph_get_remote_node(np_phy, -1, -1);
1973 edev = extcon_find_edev_by_node(np_conn);
1974 of_node_put(np_conn);
1976 of_node_put(np_phy);
1981 static int dwc3_get_clocks(struct dwc3 *dwc)
1983 struct device *dev = dwc->dev;
1989 * Clocks are optional, but new DT platforms should support all clocks
1990 * as required by the DT-binding.
1991 * Some devices have different clock names in legacy device trees,
1992 * check for them to retain backwards compatibility.
1994 dwc->bus_clk = devm_clk_get_optional(dev, "bus_early");
1995 if (IS_ERR(dwc->bus_clk)) {
1996 return dev_err_probe(dev, PTR_ERR(dwc->bus_clk),
1997 "could not get bus clock\n");
2000 if (dwc->bus_clk == NULL) {
2001 dwc->bus_clk = devm_clk_get_optional(dev, "bus_clk");
2002 if (IS_ERR(dwc->bus_clk)) {
2003 return dev_err_probe(dev, PTR_ERR(dwc->bus_clk),
2004 "could not get bus clock\n");
2008 dwc->ref_clk = devm_clk_get_optional(dev, "ref");
2009 if (IS_ERR(dwc->ref_clk)) {
2010 return dev_err_probe(dev, PTR_ERR(dwc->ref_clk),
2011 "could not get ref clock\n");
2014 if (dwc->ref_clk == NULL) {
2015 dwc->ref_clk = devm_clk_get_optional(dev, "ref_clk");
2016 if (IS_ERR(dwc->ref_clk)) {
2017 return dev_err_probe(dev, PTR_ERR(dwc->ref_clk),
2018 "could not get ref clock\n");
2022 dwc->susp_clk = devm_clk_get_optional(dev, "suspend");
2023 if (IS_ERR(dwc->susp_clk)) {
2024 return dev_err_probe(dev, PTR_ERR(dwc->susp_clk),
2025 "could not get suspend clock\n");
2028 if (dwc->susp_clk == NULL) {
2029 dwc->susp_clk = devm_clk_get_optional(dev, "suspend_clk");
2030 if (IS_ERR(dwc->susp_clk)) {
2031 return dev_err_probe(dev, PTR_ERR(dwc->susp_clk),
2032 "could not get suspend clock\n");
2036 /* specific to Rockchip RK3588 */
2037 dwc->utmi_clk = devm_clk_get_optional(dev, "utmi");
2038 if (IS_ERR(dwc->utmi_clk)) {
2039 return dev_err_probe(dev, PTR_ERR(dwc->utmi_clk),
2040 "could not get utmi clock\n");
2043 /* specific to Rockchip RK3588 */
2044 dwc->pipe_clk = devm_clk_get_optional(dev, "pipe");
2045 if (IS_ERR(dwc->pipe_clk)) {
2046 return dev_err_probe(dev, PTR_ERR(dwc->pipe_clk),
2047 "could not get pipe clock\n");
2053 static int dwc3_get_num_ports(struct dwc3 *dwc)
2061 * Remap xHCI address space to access XHCI ext cap regs since it is
2062 * needed to get information on number of ports present.
2064 base = ioremap(dwc->xhci_resources[0].start,
2065 resource_size(&dwc->xhci_resources[0]));
2071 offset = xhci_find_next_ext_cap(base, offset,
2072 XHCI_EXT_CAPS_PROTOCOL);
2076 val = readl(base + offset);
2077 major_revision = XHCI_EXT_PORT_MAJOR(val);
2079 val = readl(base + offset + 0x08);
2080 if (major_revision == 0x03) {
2081 dwc->num_usb3_ports += XHCI_EXT_PORT_COUNT(val);
2082 } else if (major_revision <= 0x02) {
2083 dwc->num_usb2_ports += XHCI_EXT_PORT_COUNT(val);
2085 dev_warn(dwc->dev, "unrecognized port major revision %d\n",
2090 dev_dbg(dwc->dev, "hs-ports: %u ss-ports: %u\n",
2091 dwc->num_usb2_ports, dwc->num_usb3_ports);
2095 if (dwc->num_usb2_ports > DWC3_USB2_MAX_PORTS ||
2096 dwc->num_usb3_ports > DWC3_USB3_MAX_PORTS)
2102 static int dwc3_probe(struct platform_device *pdev)
2104 struct device *dev = &pdev->dev;
2105 struct resource *res, dwc_res;
2106 unsigned int hw_mode;
2111 dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
2117 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2119 dev_err(dev, "missing memory resource\n");
2123 dwc->xhci_resources[0].start = res->start;
2124 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
2126 dwc->xhci_resources[0].flags = res->flags;
2127 dwc->xhci_resources[0].name = res->name;
2130 * Request memory region but exclude xHCI regs,
2131 * since it will be requested by the xhci-plat driver.
2134 dwc_res.start += DWC3_GLOBALS_REGS_START;
2137 struct device_node *parent = of_get_parent(dev->of_node);
2139 if (of_device_is_compatible(parent, "realtek,rtd-dwc3")) {
2140 dwc_res.start -= DWC3_GLOBALS_REGS_START;
2141 dwc_res.start += DWC3_RTK_RTD_GLOBALS_REGS_START;
2144 of_node_put(parent);
2147 regs = devm_ioremap_resource(dev, &dwc_res);
2149 return PTR_ERR(regs);
2152 dwc->regs_size = resource_size(&dwc_res);
2154 dwc3_get_properties(dwc);
2156 dwc3_get_software_properties(dwc);
2158 dwc->reset = devm_reset_control_array_get_optional_shared(dev);
2159 if (IS_ERR(dwc->reset)) {
2160 ret = PTR_ERR(dwc->reset);
2164 ret = dwc3_get_clocks(dwc);
2168 ret = reset_control_deassert(dwc->reset);
2172 ret = dwc3_clk_enable(dwc);
2174 goto err_assert_reset;
2176 if (!dwc3_core_is_valid(dwc)) {
2177 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
2179 goto err_disable_clks;
2182 platform_set_drvdata(pdev, dwc);
2183 dwc3_cache_hwparams(dwc);
2185 if (!dwc->sysdev_is_parent &&
2186 DWC3_GHWPARAMS0_AWIDTH(dwc->hwparams.hwparams0) == 64) {
2187 ret = dma_set_mask_and_coherent(dwc->sysdev, DMA_BIT_MASK(64));
2189 goto err_disable_clks;
2193 * Currently only DWC3 controllers that are host-only capable
2194 * can have more than one port.
2196 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
2197 if (hw_mode == DWC3_GHWPARAMS0_MODE_HOST) {
2198 ret = dwc3_get_num_ports(dwc);
2200 goto err_disable_clks;
2202 dwc->num_usb2_ports = 1;
2203 dwc->num_usb3_ports = 1;
2206 spin_lock_init(&dwc->lock);
2207 mutex_init(&dwc->mutex);
2209 pm_runtime_get_noresume(dev);
2210 pm_runtime_set_active(dev);
2211 pm_runtime_use_autosuspend(dev);
2212 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
2213 pm_runtime_enable(dev);
2215 pm_runtime_forbid(dev);
2217 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
2219 dev_err(dwc->dev, "failed to allocate event buffers\n");
2224 dwc->edev = dwc3_get_extcon(dwc);
2225 if (IS_ERR(dwc->edev)) {
2226 ret = dev_err_probe(dwc->dev, PTR_ERR(dwc->edev), "failed to get extcon\n");
2227 goto err_free_event_buffers;
2230 ret = dwc3_get_dr_mode(dwc);
2232 goto err_free_event_buffers;
2234 ret = dwc3_core_init(dwc);
2236 dev_err_probe(dev, ret, "failed to initialize core\n");
2237 goto err_free_event_buffers;
2240 dwc3_check_params(dwc);
2241 dwc3_debugfs_init(dwc);
2243 ret = dwc3_core_init_mode(dwc);
2245 goto err_exit_debugfs;
2247 pm_runtime_put(dev);
2249 dma_set_max_seg_size(dev, UINT_MAX);
2254 dwc3_debugfs_exit(dwc);
2255 dwc3_event_buffers_cleanup(dwc);
2256 dwc3_phy_power_off(dwc);
2258 dwc3_ulpi_exit(dwc);
2259 err_free_event_buffers:
2260 dwc3_free_event_buffers(dwc);
2262 pm_runtime_allow(dev);
2263 pm_runtime_disable(dev);
2264 pm_runtime_dont_use_autosuspend(dev);
2265 pm_runtime_set_suspended(dev);
2266 pm_runtime_put_noidle(dev);
2268 dwc3_clk_disable(dwc);
2270 reset_control_assert(dwc->reset);
2273 power_supply_put(dwc->usb_psy);
2278 static void dwc3_remove(struct platform_device *pdev)
2280 struct dwc3 *dwc = platform_get_drvdata(pdev);
2282 pm_runtime_get_sync(&pdev->dev);
2284 dwc3_core_exit_mode(dwc);
2285 dwc3_debugfs_exit(dwc);
2287 dwc3_core_exit(dwc);
2288 dwc3_ulpi_exit(dwc);
2290 pm_runtime_allow(&pdev->dev);
2291 pm_runtime_disable(&pdev->dev);
2292 pm_runtime_dont_use_autosuspend(&pdev->dev);
2293 pm_runtime_put_noidle(&pdev->dev);
2295 * HACK: Clear the driver data, which is currently accessed by parent
2296 * glue drivers, before allowing the parent to suspend.
2298 platform_set_drvdata(pdev, NULL);
2299 pm_runtime_set_suspended(&pdev->dev);
2301 dwc3_free_event_buffers(dwc);
2304 power_supply_put(dwc->usb_psy);
2308 static int dwc3_core_init_for_resume(struct dwc3 *dwc)
2312 ret = reset_control_deassert(dwc->reset);
2316 ret = dwc3_clk_enable(dwc);
2320 ret = dwc3_core_init(dwc);
2327 dwc3_clk_disable(dwc);
2329 reset_control_assert(dwc->reset);
2334 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
2339 switch (dwc->current_dr_role) {
2340 case DWC3_GCTL_PRTCAP_DEVICE:
2341 if (pm_runtime_suspended(dwc->dev))
2343 dwc3_gadget_suspend(dwc);
2344 synchronize_irq(dwc->irq_gadget);
2345 dwc3_core_exit(dwc);
2347 case DWC3_GCTL_PRTCAP_HOST:
2348 if (!PMSG_IS_AUTO(msg) && !device_may_wakeup(dwc->dev)) {
2349 dwc3_core_exit(dwc);
2353 /* Let controller to suspend HSPHY before PHY driver suspends */
2354 if (dwc->dis_u2_susphy_quirk ||
2355 dwc->dis_enblslpm_quirk) {
2356 for (i = 0; i < dwc->num_usb2_ports; i++) {
2357 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(i));
2358 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
2359 DWC3_GUSB2PHYCFG_SUSPHY;
2360 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(i), reg);
2363 /* Give some time for USB2 PHY to suspend */
2364 usleep_range(5000, 6000);
2367 for (i = 0; i < dwc->num_usb2_ports; i++)
2368 phy_pm_runtime_put_sync(dwc->usb2_generic_phy[i]);
2369 for (i = 0; i < dwc->num_usb3_ports; i++)
2370 phy_pm_runtime_put_sync(dwc->usb3_generic_phy[i]);
2372 case DWC3_GCTL_PRTCAP_OTG:
2373 /* do nothing during runtime_suspend */
2374 if (PMSG_IS_AUTO(msg))
2377 if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
2378 dwc3_gadget_suspend(dwc);
2379 synchronize_irq(dwc->irq_gadget);
2383 dwc3_core_exit(dwc);
2393 static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
2399 switch (dwc->current_dr_role) {
2400 case DWC3_GCTL_PRTCAP_DEVICE:
2401 ret = dwc3_core_init_for_resume(dwc);
2405 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
2406 dwc3_gadget_resume(dwc);
2408 case DWC3_GCTL_PRTCAP_HOST:
2409 if (!PMSG_IS_AUTO(msg) && !device_may_wakeup(dwc->dev)) {
2410 ret = dwc3_core_init_for_resume(dwc);
2413 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
2416 /* Restore GUSB2PHYCFG bits that were modified in suspend */
2417 for (i = 0; i < dwc->num_usb2_ports; i++) {
2418 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(i));
2419 if (dwc->dis_u2_susphy_quirk)
2420 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
2422 if (dwc->dis_enblslpm_quirk)
2423 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
2425 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(i), reg);
2428 for (i = 0; i < dwc->num_usb2_ports; i++)
2429 phy_pm_runtime_get_sync(dwc->usb2_generic_phy[i]);
2430 for (i = 0; i < dwc->num_usb3_ports; i++)
2431 phy_pm_runtime_get_sync(dwc->usb3_generic_phy[i]);
2433 case DWC3_GCTL_PRTCAP_OTG:
2434 /* nothing to do on runtime_resume */
2435 if (PMSG_IS_AUTO(msg))
2438 ret = dwc3_core_init_for_resume(dwc);
2442 dwc3_set_prtcap(dwc, dwc->current_dr_role);
2445 if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
2446 dwc3_otg_host_init(dwc);
2447 } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
2448 dwc3_gadget_resume(dwc);
2460 static int dwc3_runtime_checks(struct dwc3 *dwc)
2462 switch (dwc->current_dr_role) {
2463 case DWC3_GCTL_PRTCAP_DEVICE:
2467 case DWC3_GCTL_PRTCAP_HOST:
2476 static int dwc3_runtime_suspend(struct device *dev)
2478 struct dwc3 *dwc = dev_get_drvdata(dev);
2481 if (dwc3_runtime_checks(dwc))
2484 ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
2491 static int dwc3_runtime_resume(struct device *dev)
2493 struct dwc3 *dwc = dev_get_drvdata(dev);
2496 ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
2500 switch (dwc->current_dr_role) {
2501 case DWC3_GCTL_PRTCAP_DEVICE:
2502 dwc3_gadget_process_pending_events(dwc);
2504 case DWC3_GCTL_PRTCAP_HOST:
2510 pm_runtime_mark_last_busy(dev);
2515 static int dwc3_runtime_idle(struct device *dev)
2517 struct dwc3 *dwc = dev_get_drvdata(dev);
2519 switch (dwc->current_dr_role) {
2520 case DWC3_GCTL_PRTCAP_DEVICE:
2521 if (dwc3_runtime_checks(dwc))
2524 case DWC3_GCTL_PRTCAP_HOST:
2530 pm_runtime_mark_last_busy(dev);
2531 pm_runtime_autosuspend(dev);
2535 #endif /* CONFIG_PM */
2537 #ifdef CONFIG_PM_SLEEP
2538 static int dwc3_suspend(struct device *dev)
2540 struct dwc3 *dwc = dev_get_drvdata(dev);
2543 ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
2547 pinctrl_pm_select_sleep_state(dev);
2552 static int dwc3_resume(struct device *dev)
2554 struct dwc3 *dwc = dev_get_drvdata(dev);
2557 pinctrl_pm_select_default_state(dev);
2559 pm_runtime_disable(dev);
2560 pm_runtime_set_active(dev);
2562 ret = dwc3_resume_common(dwc, PMSG_RESUME);
2564 pm_runtime_set_suspended(dev);
2568 pm_runtime_enable(dev);
2573 static void dwc3_complete(struct device *dev)
2575 struct dwc3 *dwc = dev_get_drvdata(dev);
2578 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST &&
2579 dwc->dis_split_quirk) {
2580 reg = dwc3_readl(dwc->regs, DWC3_GUCTL3);
2581 reg |= DWC3_GUCTL3_SPLITDISABLE;
2582 dwc3_writel(dwc->regs, DWC3_GUCTL3, reg);
2586 #define dwc3_complete NULL
2587 #endif /* CONFIG_PM_SLEEP */
2589 static const struct dev_pm_ops dwc3_dev_pm_ops = {
2590 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
2591 .complete = dwc3_complete,
2592 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
2597 static const struct of_device_id of_dwc3_match[] = {
2599 .compatible = "snps,dwc3"
2602 .compatible = "synopsys,dwc3"
2606 MODULE_DEVICE_TABLE(of, of_dwc3_match);
2611 #define ACPI_ID_INTEL_BSW "808622B7"
2613 static const struct acpi_device_id dwc3_acpi_match[] = {
2614 { ACPI_ID_INTEL_BSW, 0 },
2617 MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
2620 static struct platform_driver dwc3_driver = {
2621 .probe = dwc3_probe,
2622 .remove_new = dwc3_remove,
2625 .of_match_table = of_match_ptr(of_dwc3_match),
2626 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
2627 .pm = &dwc3_dev_pm_ops,
2631 module_platform_driver(dwc3_driver);
2633 MODULE_ALIAS("platform:dwc3");
2635 MODULE_LICENSE("GPL v2");
2636 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");