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/acpi.h>
27 #include <linux/pinctrl/consumer.h>
28 #include <linux/reset.h>
29 #include <linux/bitfield.h>
31 #include <linux/usb/ch9.h>
32 #include <linux/usb/gadget.h>
33 #include <linux/usb/of.h>
34 #include <linux/usb/otg.h>
42 #define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
45 * dwc3_get_dr_mode - Validates and sets dr_mode
46 * @dwc: pointer to our context structure
48 static int dwc3_get_dr_mode(struct dwc3 *dwc)
50 enum usb_dr_mode mode;
51 struct device *dev = dwc->dev;
54 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
55 dwc->dr_mode = USB_DR_MODE_OTG;
58 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
61 case DWC3_GHWPARAMS0_MODE_GADGET:
62 if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) {
64 "Controller does not support host mode.\n");
67 mode = USB_DR_MODE_PERIPHERAL;
69 case DWC3_GHWPARAMS0_MODE_HOST:
70 if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
72 "Controller does not support device mode.\n");
75 mode = USB_DR_MODE_HOST;
78 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
79 mode = USB_DR_MODE_HOST;
80 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
81 mode = USB_DR_MODE_PERIPHERAL;
84 * DWC_usb31 and DWC_usb3 v3.30a and higher do not support OTG
85 * mode. If the controller supports DRD but the dr_mode is not
86 * specified or set to OTG, then set the mode to peripheral.
88 if (mode == USB_DR_MODE_OTG &&
89 (!IS_ENABLED(CONFIG_USB_ROLE_SWITCH) ||
90 !device_property_read_bool(dwc->dev, "usb-role-switch")) &&
91 !DWC3_VER_IS_PRIOR(DWC3, 330A))
92 mode = USB_DR_MODE_PERIPHERAL;
95 if (mode != dwc->dr_mode) {
97 "Configuration mismatch. dr_mode forced to %s\n",
98 mode == USB_DR_MODE_HOST ? "host" : "gadget");
106 void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
110 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
111 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
112 reg |= DWC3_GCTL_PRTCAPDIR(mode);
113 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
115 dwc->current_dr_role = mode;
118 static void __dwc3_set_mode(struct work_struct *work)
120 struct dwc3 *dwc = work_to_dwc(work);
125 mutex_lock(&dwc->mutex);
127 pm_runtime_get_sync(dwc->dev);
129 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
130 dwc3_otg_update(dwc, 0);
132 if (!dwc->desired_dr_role)
135 if (dwc->desired_dr_role == dwc->current_dr_role)
138 if (dwc->desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev)
141 switch (dwc->current_dr_role) {
142 case DWC3_GCTL_PRTCAP_HOST:
145 case DWC3_GCTL_PRTCAP_DEVICE:
146 dwc3_gadget_exit(dwc);
147 dwc3_event_buffers_cleanup(dwc);
149 case DWC3_GCTL_PRTCAP_OTG:
151 spin_lock_irqsave(&dwc->lock, flags);
152 dwc->desired_otg_role = DWC3_OTG_ROLE_IDLE;
153 spin_unlock_irqrestore(&dwc->lock, flags);
154 dwc3_otg_update(dwc, 1);
160 /* For DRD host or device mode only */
161 if (dwc->desired_dr_role != DWC3_GCTL_PRTCAP_OTG) {
162 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
163 reg |= DWC3_GCTL_CORESOFTRESET;
164 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
167 * Wait for internal clocks to synchronized. DWC_usb31 and
168 * DWC_usb32 may need at least 50ms (less for DWC_usb3). To
169 * keep it consistent across different IPs, let's wait up to
170 * 100ms before clearing GCTL.CORESOFTRESET.
174 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
175 reg &= ~DWC3_GCTL_CORESOFTRESET;
176 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
179 spin_lock_irqsave(&dwc->lock, flags);
181 dwc3_set_prtcap(dwc, dwc->desired_dr_role);
183 spin_unlock_irqrestore(&dwc->lock, flags);
185 switch (dwc->desired_dr_role) {
186 case DWC3_GCTL_PRTCAP_HOST:
187 ret = dwc3_host_init(dwc);
189 dev_err(dwc->dev, "failed to initialize host\n");
192 otg_set_vbus(dwc->usb2_phy->otg, true);
193 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
194 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
195 if (dwc->dis_split_quirk) {
196 reg = dwc3_readl(dwc->regs, DWC3_GUCTL3);
197 reg |= DWC3_GUCTL3_SPLITDISABLE;
198 dwc3_writel(dwc->regs, DWC3_GUCTL3, reg);
202 case DWC3_GCTL_PRTCAP_DEVICE:
203 dwc3_core_soft_reset(dwc);
205 dwc3_event_buffers_setup(dwc);
208 otg_set_vbus(dwc->usb2_phy->otg, false);
209 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
210 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
212 ret = dwc3_gadget_init(dwc);
214 dev_err(dwc->dev, "failed to initialize peripheral\n");
216 case DWC3_GCTL_PRTCAP_OTG:
218 dwc3_otg_update(dwc, 0);
225 pm_runtime_mark_last_busy(dwc->dev);
226 pm_runtime_put_autosuspend(dwc->dev);
227 mutex_unlock(&dwc->mutex);
230 void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
234 if (dwc->dr_mode != USB_DR_MODE_OTG)
237 spin_lock_irqsave(&dwc->lock, flags);
238 dwc->desired_dr_role = mode;
239 spin_unlock_irqrestore(&dwc->lock, flags);
241 queue_work(system_freezable_wq, &dwc->drd_work);
244 u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
246 struct dwc3 *dwc = dep->dwc;
249 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
250 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
251 DWC3_GDBGFIFOSPACE_TYPE(type));
253 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
255 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
259 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
260 * @dwc: pointer to our context structure
262 int dwc3_core_soft_reset(struct dwc3 *dwc)
268 * We're resetting only the device side because, if we're in host mode,
269 * XHCI driver will reset the host block. If dwc3 was configured for
270 * host-only mode, then we can return early.
272 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST)
275 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
276 reg |= DWC3_DCTL_CSFTRST;
277 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
280 * For DWC_usb31 controller 1.90a and later, the DCTL.CSFRST bit
281 * is cleared only after all the clocks are synchronized. This can
282 * take a little more than 50ms. Set the polling rate at 20ms
283 * for 10 times instead.
285 if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
289 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
290 if (!(reg & DWC3_DCTL_CSFTRST))
293 if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
303 * For DWC_usb31 controller 1.80a and prior, once DCTL.CSFRST bit
304 * is cleared, we must wait at least 50ms before accessing the PHY
305 * domain (synchronization delay).
307 if (DWC3_VER_IS_WITHIN(DWC31, ANY, 180A))
314 * dwc3_frame_length_adjustment - Adjusts frame length if required
315 * @dwc3: Pointer to our controller context structure
317 static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
322 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
328 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
329 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
330 if (dft != dwc->fladj) {
331 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
332 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
333 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
338 * dwc3_ref_clk_period - Reference clock period configuration
339 * Default reference clock period depends on hardware
340 * configuration. For systems with reference clock that differs
341 * from the default, this will set clock period in DWC3_GUCTL
343 * @dwc: Pointer to our controller context structure
344 * @ref_clk_per: reference clock period in ns
346 static void dwc3_ref_clk_period(struct dwc3 *dwc)
348 unsigned long period;
355 rate = clk_get_rate(dwc->ref_clk);
358 period = NSEC_PER_SEC / rate;
359 } else if (dwc->ref_clk_per) {
360 period = dwc->ref_clk_per;
361 rate = NSEC_PER_SEC / period;
366 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
367 reg &= ~DWC3_GUCTL_REFCLKPER_MASK;
368 reg |= FIELD_PREP(DWC3_GUCTL_REFCLKPER_MASK, period);
369 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
371 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
375 * The calculation below is
377 * 125000 * (NSEC_PER_SEC / (rate * period) - 1)
379 * but rearranged for fixed-point arithmetic. The division must be
380 * 64-bit because 125000 * NSEC_PER_SEC doesn't fit in 32 bits (and
381 * neither does rate * period).
383 * Note that rate * period ~= NSEC_PER_SECOND, minus the number of
384 * nanoseconds of error caused by the truncation which happened during
385 * the division when calculating rate or period (whichever one was
386 * derived from the other). We first calculate the relative error, then
387 * scale it to units of 8 ppm.
389 fladj = div64_u64(125000ULL * NSEC_PER_SEC, (u64)rate * period);
393 * The documented 240MHz constant is scaled by 2 to get PLS1 as well.
395 decr = 480000000 / rate;
397 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
398 reg &= ~DWC3_GFLADJ_REFCLK_FLADJ_MASK
399 & ~DWC3_GFLADJ_240MHZDECR
400 & ~DWC3_GFLADJ_240MHZDECR_PLS1;
401 reg |= FIELD_PREP(DWC3_GFLADJ_REFCLK_FLADJ_MASK, fladj)
402 | FIELD_PREP(DWC3_GFLADJ_240MHZDECR, decr >> 1)
403 | FIELD_PREP(DWC3_GFLADJ_240MHZDECR_PLS1, decr & 1);
404 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
408 * dwc3_free_one_event_buffer - Frees one event buffer
409 * @dwc: Pointer to our controller context structure
410 * @evt: Pointer to event buffer to be freed
412 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
413 struct dwc3_event_buffer *evt)
415 dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
419 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
420 * @dwc: Pointer to our controller context structure
421 * @length: size of the event buffer
423 * Returns a pointer to the allocated event buffer structure on success
424 * otherwise ERR_PTR(errno).
426 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
429 struct dwc3_event_buffer *evt;
431 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
433 return ERR_PTR(-ENOMEM);
436 evt->length = length;
437 evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
439 return ERR_PTR(-ENOMEM);
441 evt->buf = dma_alloc_coherent(dwc->sysdev, length,
442 &evt->dma, GFP_KERNEL);
444 return ERR_PTR(-ENOMEM);
450 * dwc3_free_event_buffers - frees all allocated event buffers
451 * @dwc: Pointer to our controller context structure
453 static void dwc3_free_event_buffers(struct dwc3 *dwc)
455 struct dwc3_event_buffer *evt;
459 dwc3_free_one_event_buffer(dwc, evt);
463 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
464 * @dwc: pointer to our controller context structure
465 * @length: size of event buffer
467 * Returns 0 on success otherwise negative errno. In the error case, dwc
468 * may contain some buffers allocated but not all which were requested.
470 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
472 struct dwc3_event_buffer *evt;
474 evt = dwc3_alloc_one_event_buffer(dwc, length);
476 dev_err(dwc->dev, "can't allocate event buffer\n");
485 * dwc3_event_buffers_setup - setup our allocated event buffers
486 * @dwc: pointer to our controller context structure
488 * Returns 0 on success otherwise negative errno.
490 int dwc3_event_buffers_setup(struct dwc3 *dwc)
492 struct dwc3_event_buffer *evt;
496 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
497 lower_32_bits(evt->dma));
498 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
499 upper_32_bits(evt->dma));
500 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
501 DWC3_GEVNTSIZ_SIZE(evt->length));
502 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
507 void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
509 struct dwc3_event_buffer *evt;
515 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
516 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
517 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
518 | DWC3_GEVNTSIZ_SIZE(0));
519 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
522 static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
524 if (!dwc->has_hibernation)
527 if (!dwc->nr_scratch)
530 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
531 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
532 if (!dwc->scratchbuf)
538 static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
540 dma_addr_t scratch_addr;
544 if (!dwc->has_hibernation)
547 if (!dwc->nr_scratch)
550 /* should never fall here */
551 if (!WARN_ON(dwc->scratchbuf))
554 scratch_addr = dma_map_single(dwc->sysdev, dwc->scratchbuf,
555 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
557 if (dma_mapping_error(dwc->sysdev, scratch_addr)) {
558 dev_err(dwc->sysdev, "failed to map scratch buffer\n");
563 dwc->scratch_addr = scratch_addr;
565 param = lower_32_bits(scratch_addr);
567 ret = dwc3_send_gadget_generic_command(dwc,
568 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
572 param = upper_32_bits(scratch_addr);
574 ret = dwc3_send_gadget_generic_command(dwc,
575 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
582 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
583 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
589 static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
591 if (!dwc->has_hibernation)
594 if (!dwc->nr_scratch)
597 /* should never fall here */
598 if (!WARN_ON(dwc->scratchbuf))
601 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
602 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
603 kfree(dwc->scratchbuf);
606 static void dwc3_core_num_eps(struct dwc3 *dwc)
608 struct dwc3_hwparams *parms = &dwc->hwparams;
610 dwc->num_eps = DWC3_NUM_EPS(parms);
613 static void dwc3_cache_hwparams(struct dwc3 *dwc)
615 struct dwc3_hwparams *parms = &dwc->hwparams;
617 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
618 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
619 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
620 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
621 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
622 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
623 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
624 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
625 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
627 if (DWC3_IP_IS(DWC32))
628 parms->hwparams9 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS9);
631 static int dwc3_core_ulpi_init(struct dwc3 *dwc)
636 intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
638 if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
639 (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
640 dwc->hsphy_interface &&
641 !strncmp(dwc->hsphy_interface, "ulpi", 4)))
642 ret = dwc3_ulpi_init(dwc);
648 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
649 * @dwc: Pointer to our controller context structure
651 * Returns 0 on success. The USB PHY interfaces are configured but not
652 * initialized. The PHY interfaces and the PHYs get initialized together with
653 * the core in dwc3_core_init.
655 static int dwc3_phy_setup(struct dwc3 *dwc)
657 unsigned int hw_mode;
660 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
662 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
665 * Make sure UX_EXIT_PX is cleared as that causes issues with some
666 * PHYs. Also, this bit is not supposed to be used in normal operation.
668 reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
671 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
672 * to '0' during coreConsultant configuration. So default value
673 * will be '0' when the core is reset. Application needs to set it
674 * to '1' after the core initialization is completed.
676 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
677 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
680 * For DRD controllers, GUSB3PIPECTL.SUSPENDENABLE must be cleared after
681 * power-on reset, and it can be set after core initialization, which is
682 * after device soft-reset during initialization.
684 if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD)
685 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
687 if (dwc->u2ss_inp3_quirk)
688 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
690 if (dwc->dis_rxdet_inp3_quirk)
691 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
693 if (dwc->req_p1p2p3_quirk)
694 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
696 if (dwc->del_p1p2p3_quirk)
697 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
699 if (dwc->del_phy_power_chg_quirk)
700 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
702 if (dwc->lfps_filter_quirk)
703 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
705 if (dwc->rx_detect_poll_quirk)
706 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
708 if (dwc->tx_de_emphasis_quirk)
709 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
711 if (dwc->dis_u3_susphy_quirk)
712 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
714 if (dwc->dis_del_phy_power_chg_quirk)
715 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
717 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
719 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
721 /* Select the HS PHY interface */
722 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
723 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
724 if (dwc->hsphy_interface &&
725 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
726 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
728 } else if (dwc->hsphy_interface &&
729 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
730 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
731 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
733 /* Relying on default value. */
734 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
738 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
743 switch (dwc->hsphy_mode) {
744 case USBPHY_INTERFACE_MODE_UTMI:
745 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
746 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
747 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
748 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
750 case USBPHY_INTERFACE_MODE_UTMIW:
751 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
752 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
753 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
754 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
761 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
762 * '0' during coreConsultant configuration. So default value will
763 * be '0' when the core is reset. Application needs to set it to
764 * '1' after the core initialization is completed.
766 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
767 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
770 * For DRD controllers, GUSB2PHYCFG.SUSPHY must be cleared after
771 * power-on reset, and it can be set after core initialization, which is
772 * after device soft-reset during initialization.
774 if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD)
775 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
777 if (dwc->dis_u2_susphy_quirk)
778 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
780 if (dwc->dis_enblslpm_quirk)
781 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
783 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM;
785 if (dwc->dis_u2_freeclk_exists_quirk)
786 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
788 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
793 static int dwc3_clk_enable(struct dwc3 *dwc)
797 ret = clk_prepare_enable(dwc->bus_clk);
801 ret = clk_prepare_enable(dwc->ref_clk);
803 goto disable_bus_clk;
805 ret = clk_prepare_enable(dwc->susp_clk);
807 goto disable_ref_clk;
812 clk_disable_unprepare(dwc->ref_clk);
814 clk_disable_unprepare(dwc->bus_clk);
818 static void dwc3_clk_disable(struct dwc3 *dwc)
820 clk_disable_unprepare(dwc->susp_clk);
821 clk_disable_unprepare(dwc->ref_clk);
822 clk_disable_unprepare(dwc->bus_clk);
825 static void dwc3_core_exit(struct dwc3 *dwc)
827 dwc3_event_buffers_cleanup(dwc);
829 usb_phy_shutdown(dwc->usb2_phy);
830 usb_phy_shutdown(dwc->usb3_phy);
831 phy_exit(dwc->usb2_generic_phy);
832 phy_exit(dwc->usb3_generic_phy);
834 usb_phy_set_suspend(dwc->usb2_phy, 1);
835 usb_phy_set_suspend(dwc->usb3_phy, 1);
836 phy_power_off(dwc->usb2_generic_phy);
837 phy_power_off(dwc->usb3_generic_phy);
838 dwc3_clk_disable(dwc);
839 reset_control_assert(dwc->reset);
842 static bool dwc3_core_is_valid(struct dwc3 *dwc)
846 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
847 dwc->ip = DWC3_GSNPS_ID(reg);
849 /* This should read as U3 followed by revision number */
850 if (DWC3_IP_IS(DWC3)) {
852 } else if (DWC3_IP_IS(DWC31) || DWC3_IP_IS(DWC32)) {
853 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
854 dwc->version_type = dwc3_readl(dwc->regs, DWC3_VER_TYPE);
862 static void dwc3_core_setup_global_control(struct dwc3 *dwc)
864 u32 hwparams4 = dwc->hwparams.hwparams4;
867 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
868 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
870 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
871 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
873 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
874 * issue which would cause xHCI compliance tests to fail.
876 * Because of that we cannot enable clock gating on such
881 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
884 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
885 dwc->dr_mode == USB_DR_MODE_OTG) &&
886 DWC3_VER_IS_WITHIN(DWC3, 210A, 250A))
887 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
889 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
891 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
892 /* enable hibernation here */
893 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
896 * REVISIT Enabling this bit so that host-mode hibernation
897 * will work. Device-mode hibernation is not yet implemented.
899 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
906 /* check if current dwc3 is on simulation board */
907 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
908 dev_info(dwc->dev, "Running with FPGA optimizations\n");
912 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
913 "disable_scramble cannot be used on non-FPGA builds\n");
915 if (dwc->disable_scramble_quirk && dwc->is_fpga)
916 reg |= DWC3_GCTL_DISSCRAMBLE;
918 reg &= ~DWC3_GCTL_DISSCRAMBLE;
920 if (dwc->u2exit_lfps_quirk)
921 reg |= DWC3_GCTL_U2EXIT_LFPS;
924 * WORKAROUND: DWC3 revisions <1.90a have a bug
925 * where the device can fail to connect at SuperSpeed
926 * and falls back to high-speed mode which causes
927 * the device to enter a Connect/Disconnect loop
929 if (DWC3_VER_IS_PRIOR(DWC3, 190A))
930 reg |= DWC3_GCTL_U2RSTECN;
932 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
935 static int dwc3_core_get_phy(struct dwc3 *dwc);
936 static int dwc3_core_ulpi_init(struct dwc3 *dwc);
938 /* set global incr burst type configuration registers */
939 static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
941 struct device *dev = dwc->dev;
942 /* incrx_mode : for INCR burst type. */
944 /* incrx_size : for size of INCRX burst. */
952 cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
955 * Handle property "snps,incr-burst-type-adjustment".
956 * Get the number of value from this property:
957 * result <= 0, means this property is not supported.
958 * result = 1, means INCRx burst mode supported.
959 * result > 1, means undefined length burst mode supported.
961 ntype = device_property_count_u32(dev, "snps,incr-burst-type-adjustment");
965 vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL);
967 dev_err(dev, "Error to get memory\n");
971 /* Get INCR burst type, and parse it */
972 ret = device_property_read_u32_array(dev,
973 "snps,incr-burst-type-adjustment", vals, ntype);
976 dev_err(dev, "Error to get property\n");
983 /* INCRX (undefined length) burst mode */
984 incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
985 for (i = 1; i < ntype; i++) {
986 if (vals[i] > incrx_size)
987 incrx_size = vals[i];
990 /* INCRX burst mode */
991 incrx_mode = INCRX_BURST_MODE;
996 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
997 cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
999 cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
1000 switch (incrx_size) {
1002 cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
1005 cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
1008 cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
1011 cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
1014 cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
1017 cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
1020 cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
1025 dev_err(dev, "Invalid property\n");
1029 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
1033 * dwc3_core_init - Low-level initialization of DWC3 Core
1034 * @dwc: Pointer to our controller context structure
1036 * Returns 0 on success otherwise negative errno.
1038 static int dwc3_core_init(struct dwc3 *dwc)
1040 unsigned int hw_mode;
1044 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
1047 * Write Linux Version Code to our GUID register so it's easy to figure
1048 * out which kernel version a bug was found.
1050 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
1052 ret = dwc3_phy_setup(dwc);
1056 if (!dwc->ulpi_ready) {
1057 ret = dwc3_core_ulpi_init(dwc);
1060 dwc->ulpi_ready = true;
1063 if (!dwc->phys_ready) {
1064 ret = dwc3_core_get_phy(dwc);
1067 dwc->phys_ready = true;
1070 usb_phy_init(dwc->usb2_phy);
1071 usb_phy_init(dwc->usb3_phy);
1072 ret = phy_init(dwc->usb2_generic_phy);
1076 ret = phy_init(dwc->usb3_generic_phy);
1078 phy_exit(dwc->usb2_generic_phy);
1082 ret = dwc3_core_soft_reset(dwc);
1086 if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD &&
1087 !DWC3_VER_IS_WITHIN(DWC3, ANY, 194A)) {
1088 if (!dwc->dis_u3_susphy_quirk) {
1089 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1090 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1091 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1094 if (!dwc->dis_u2_susphy_quirk) {
1095 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1096 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1097 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1101 dwc3_core_setup_global_control(dwc);
1102 dwc3_core_num_eps(dwc);
1104 ret = dwc3_setup_scratch_buffers(dwc);
1108 /* Adjust Frame Length */
1109 dwc3_frame_length_adjustment(dwc);
1111 /* Adjust Reference Clock Period */
1112 dwc3_ref_clk_period(dwc);
1114 dwc3_set_incr_burst_type(dwc);
1116 usb_phy_set_suspend(dwc->usb2_phy, 0);
1117 usb_phy_set_suspend(dwc->usb3_phy, 0);
1118 ret = phy_power_on(dwc->usb2_generic_phy);
1122 ret = phy_power_on(dwc->usb3_generic_phy);
1126 ret = dwc3_event_buffers_setup(dwc);
1128 dev_err(dwc->dev, "failed to setup event buffers\n");
1133 * ENDXFER polling is available on version 3.10a and later of
1134 * the DWC_usb3 controller. It is NOT available in the
1135 * DWC_usb31 controller.
1137 if (DWC3_VER_IS_WITHIN(DWC3, 310A, ANY)) {
1138 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
1139 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
1140 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
1143 if (!DWC3_VER_IS_PRIOR(DWC3, 250A)) {
1144 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1147 * Enable hardware control of sending remote wakeup
1148 * in HS when the device is in the L1 state.
1150 if (!DWC3_VER_IS_PRIOR(DWC3, 290A))
1151 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
1154 * Decouple USB 2.0 L1 & L2 events which will allow for
1155 * gadget driver to only receive U3/L2 suspend & wakeup
1156 * events and prevent the more frequent L1 LPM transitions
1157 * from interrupting the driver.
1159 if (!DWC3_VER_IS_PRIOR(DWC3, 300A))
1160 reg |= DWC3_GUCTL1_DEV_DECOUPLE_L1L2_EVT;
1162 if (dwc->dis_tx_ipgap_linecheck_quirk)
1163 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
1165 if (dwc->parkmode_disable_ss_quirk)
1166 reg |= DWC3_GUCTL1_PARKMODE_DISABLE_SS;
1168 if (DWC3_VER_IS_WITHIN(DWC3, 290A, ANY) &&
1169 (dwc->maximum_speed == USB_SPEED_HIGH ||
1170 dwc->maximum_speed == USB_SPEED_FULL))
1171 reg |= DWC3_GUCTL1_DEV_FORCE_20_CLK_FOR_30_CLK;
1173 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1176 if (dwc->dr_mode == USB_DR_MODE_HOST ||
1177 dwc->dr_mode == USB_DR_MODE_OTG) {
1178 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
1181 * Enable Auto retry Feature to make the controller operating in
1182 * Host mode on seeing transaction errors(CRC errors or internal
1183 * overrun scenerios) on IN transfers to reply to the device
1184 * with a non-terminating retry ACK (i.e, an ACK transcation
1185 * packet with Retry=1 & Nump != 0)
1187 reg |= DWC3_GUCTL_HSTINAUTORETRY;
1189 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
1193 * Must config both number of packets and max burst settings to enable
1194 * RX and/or TX threshold.
1196 if (!DWC3_IP_IS(DWC3) && dwc->dr_mode == USB_DR_MODE_HOST) {
1197 u8 rx_thr_num = dwc->rx_thr_num_pkt_prd;
1198 u8 rx_maxburst = dwc->rx_max_burst_prd;
1199 u8 tx_thr_num = dwc->tx_thr_num_pkt_prd;
1200 u8 tx_maxburst = dwc->tx_max_burst_prd;
1202 if (rx_thr_num && rx_maxburst) {
1203 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1204 reg |= DWC31_RXTHRNUMPKTSEL_PRD;
1206 reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
1207 reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
1209 reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
1210 reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
1212 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1215 if (tx_thr_num && tx_maxburst) {
1216 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1217 reg |= DWC31_TXTHRNUMPKTSEL_PRD;
1219 reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
1220 reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
1222 reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
1223 reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
1225 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1232 phy_power_off(dwc->usb3_generic_phy);
1235 phy_power_off(dwc->usb2_generic_phy);
1238 usb_phy_set_suspend(dwc->usb2_phy, 1);
1239 usb_phy_set_suspend(dwc->usb3_phy, 1);
1242 usb_phy_shutdown(dwc->usb2_phy);
1243 usb_phy_shutdown(dwc->usb3_phy);
1244 phy_exit(dwc->usb2_generic_phy);
1245 phy_exit(dwc->usb3_generic_phy);
1248 dwc3_ulpi_exit(dwc);
1254 static int dwc3_core_get_phy(struct dwc3 *dwc)
1256 struct device *dev = dwc->dev;
1257 struct device_node *node = dev->of_node;
1261 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
1262 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
1264 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1265 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
1268 if (IS_ERR(dwc->usb2_phy)) {
1269 ret = PTR_ERR(dwc->usb2_phy);
1270 if (ret == -ENXIO || ret == -ENODEV) {
1271 dwc->usb2_phy = NULL;
1273 return dev_err_probe(dev, ret, "no usb2 phy configured\n");
1277 if (IS_ERR(dwc->usb3_phy)) {
1278 ret = PTR_ERR(dwc->usb3_phy);
1279 if (ret == -ENXIO || ret == -ENODEV) {
1280 dwc->usb3_phy = NULL;
1282 return dev_err_probe(dev, ret, "no usb3 phy configured\n");
1286 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
1287 if (IS_ERR(dwc->usb2_generic_phy)) {
1288 ret = PTR_ERR(dwc->usb2_generic_phy);
1289 if (ret == -ENOSYS || ret == -ENODEV) {
1290 dwc->usb2_generic_phy = NULL;
1292 return dev_err_probe(dev, ret, "no usb2 phy configured\n");
1296 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
1297 if (IS_ERR(dwc->usb3_generic_phy)) {
1298 ret = PTR_ERR(dwc->usb3_generic_phy);
1299 if (ret == -ENOSYS || ret == -ENODEV) {
1300 dwc->usb3_generic_phy = NULL;
1302 return dev_err_probe(dev, ret, "no usb3 phy configured\n");
1309 static int dwc3_core_init_mode(struct dwc3 *dwc)
1311 struct device *dev = dwc->dev;
1314 switch (dwc->dr_mode) {
1315 case USB_DR_MODE_PERIPHERAL:
1316 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1319 otg_set_vbus(dwc->usb2_phy->otg, false);
1320 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
1321 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
1323 ret = dwc3_gadget_init(dwc);
1325 return dev_err_probe(dev, ret, "failed to initialize gadget\n");
1327 case USB_DR_MODE_HOST:
1328 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1331 otg_set_vbus(dwc->usb2_phy->otg, true);
1332 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
1333 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
1335 ret = dwc3_host_init(dwc);
1337 return dev_err_probe(dev, ret, "failed to initialize host\n");
1339 case USB_DR_MODE_OTG:
1340 INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
1341 ret = dwc3_drd_init(dwc);
1343 return dev_err_probe(dev, ret, "failed to initialize dual-role\n");
1346 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1353 static void dwc3_core_exit_mode(struct dwc3 *dwc)
1355 switch (dwc->dr_mode) {
1356 case USB_DR_MODE_PERIPHERAL:
1357 dwc3_gadget_exit(dwc);
1359 case USB_DR_MODE_HOST:
1360 dwc3_host_exit(dwc);
1362 case USB_DR_MODE_OTG:
1370 /* de-assert DRVVBUS for HOST and OTG mode */
1371 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1374 static void dwc3_get_properties(struct dwc3 *dwc)
1376 struct device *dev = dwc->dev;
1377 u8 lpm_nyet_threshold;
1380 u8 rx_thr_num_pkt_prd;
1381 u8 rx_max_burst_prd;
1382 u8 tx_thr_num_pkt_prd;
1383 u8 tx_max_burst_prd;
1384 u8 tx_fifo_resize_max_num;
1385 const char *usb_psy_name;
1388 /* default to highest possible threshold */
1389 lpm_nyet_threshold = 0xf;
1391 /* default to -3.5dB de-emphasis */
1395 * default to assert utmi_sleep_n and use maximum allowed HIRD
1396 * threshold value of 0b1100
1398 hird_threshold = 12;
1401 * default to a TXFIFO size large enough to fit 6 max packets. This
1402 * allows for systems with larger bus latencies to have some headroom
1403 * for endpoints that have a large bMaxBurst value.
1405 tx_fifo_resize_max_num = 6;
1407 dwc->maximum_speed = usb_get_maximum_speed(dev);
1408 dwc->max_ssp_rate = usb_get_maximum_ssp_rate(dev);
1409 dwc->dr_mode = usb_get_dr_mode(dev);
1410 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
1412 dwc->sysdev_is_parent = device_property_read_bool(dev,
1413 "linux,sysdev_is_parent");
1414 if (dwc->sysdev_is_parent)
1415 dwc->sysdev = dwc->dev->parent;
1417 dwc->sysdev = dwc->dev;
1419 ret = device_property_read_string(dev, "usb-psy-name", &usb_psy_name);
1421 dwc->usb_psy = power_supply_get_by_name(usb_psy_name);
1423 dev_err(dev, "couldn't get usb power supply\n");
1426 dwc->has_lpm_erratum = device_property_read_bool(dev,
1427 "snps,has-lpm-erratum");
1428 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
1429 &lpm_nyet_threshold);
1430 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
1431 "snps,is-utmi-l1-suspend");
1432 device_property_read_u8(dev, "snps,hird-threshold",
1434 dwc->dis_start_transfer_quirk = device_property_read_bool(dev,
1435 "snps,dis-start-transfer-quirk");
1436 dwc->usb3_lpm_capable = device_property_read_bool(dev,
1437 "snps,usb3_lpm_capable");
1438 dwc->usb2_lpm_disable = device_property_read_bool(dev,
1439 "snps,usb2-lpm-disable");
1440 dwc->usb2_gadget_lpm_disable = device_property_read_bool(dev,
1441 "snps,usb2-gadget-lpm-disable");
1442 device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
1443 &rx_thr_num_pkt_prd);
1444 device_property_read_u8(dev, "snps,rx-max-burst-prd",
1446 device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
1447 &tx_thr_num_pkt_prd);
1448 device_property_read_u8(dev, "snps,tx-max-burst-prd",
1450 dwc->do_fifo_resize = device_property_read_bool(dev,
1452 if (dwc->do_fifo_resize)
1453 device_property_read_u8(dev, "tx-fifo-max-num",
1454 &tx_fifo_resize_max_num);
1456 dwc->disable_scramble_quirk = device_property_read_bool(dev,
1457 "snps,disable_scramble_quirk");
1458 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
1459 "snps,u2exit_lfps_quirk");
1460 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
1461 "snps,u2ss_inp3_quirk");
1462 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
1463 "snps,req_p1p2p3_quirk");
1464 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
1465 "snps,del_p1p2p3_quirk");
1466 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
1467 "snps,del_phy_power_chg_quirk");
1468 dwc->lfps_filter_quirk = device_property_read_bool(dev,
1469 "snps,lfps_filter_quirk");
1470 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
1471 "snps,rx_detect_poll_quirk");
1472 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
1473 "snps,dis_u3_susphy_quirk");
1474 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
1475 "snps,dis_u2_susphy_quirk");
1476 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1477 "snps,dis_enblslpm_quirk");
1478 dwc->dis_u1_entry_quirk = device_property_read_bool(dev,
1479 "snps,dis-u1-entry-quirk");
1480 dwc->dis_u2_entry_quirk = device_property_read_bool(dev,
1481 "snps,dis-u2-entry-quirk");
1482 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1483 "snps,dis_rxdet_inp3_quirk");
1484 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1485 "snps,dis-u2-freeclk-exists-quirk");
1486 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1487 "snps,dis-del-phy-power-chg-quirk");
1488 dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1489 "snps,dis-tx-ipgap-linecheck-quirk");
1490 dwc->parkmode_disable_ss_quirk = device_property_read_bool(dev,
1491 "snps,parkmode-disable-ss-quirk");
1493 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
1494 "snps,tx_de_emphasis_quirk");
1495 device_property_read_u8(dev, "snps,tx_de_emphasis",
1497 device_property_read_string(dev, "snps,hsphy_interface",
1498 &dwc->hsphy_interface);
1499 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
1501 device_property_read_u32(dev, "snps,ref-clock-period-ns",
1504 dwc->dis_metastability_quirk = device_property_read_bool(dev,
1505 "snps,dis_metastability_quirk");
1507 dwc->dis_split_quirk = device_property_read_bool(dev,
1508 "snps,dis-split-quirk");
1510 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1511 dwc->tx_de_emphasis = tx_de_emphasis;
1513 dwc->hird_threshold = hird_threshold;
1515 dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd;
1516 dwc->rx_max_burst_prd = rx_max_burst_prd;
1518 dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd;
1519 dwc->tx_max_burst_prd = tx_max_burst_prd;
1521 dwc->imod_interval = 0;
1523 dwc->tx_fifo_resize_max_num = tx_fifo_resize_max_num;
1526 /* check whether the core supports IMOD */
1527 bool dwc3_has_imod(struct dwc3 *dwc)
1529 return DWC3_VER_IS_WITHIN(DWC3, 300A, ANY) ||
1530 DWC3_VER_IS_WITHIN(DWC31, 120A, ANY) ||
1534 static void dwc3_check_params(struct dwc3 *dwc)
1536 struct device *dev = dwc->dev;
1537 unsigned int hwparam_gen =
1538 DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3);
1540 /* Check for proper value of imod_interval */
1541 if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
1542 dev_warn(dwc->dev, "Interrupt moderation not supported\n");
1543 dwc->imod_interval = 0;
1547 * Workaround for STAR 9000961433 which affects only version
1548 * 3.00a of the DWC_usb3 core. This prevents the controller
1549 * interrupt from being masked while handling events. IMOD
1550 * allows us to work around this issue. Enable it for the
1553 if (!dwc->imod_interval &&
1554 DWC3_VER_IS(DWC3, 300A))
1555 dwc->imod_interval = 1;
1557 /* Check the maximum_speed parameter */
1558 switch (dwc->maximum_speed) {
1559 case USB_SPEED_FULL:
1560 case USB_SPEED_HIGH:
1562 case USB_SPEED_SUPER:
1563 if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS)
1564 dev_warn(dev, "UDC doesn't support Gen 1\n");
1566 case USB_SPEED_SUPER_PLUS:
1567 if ((DWC3_IP_IS(DWC32) &&
1568 hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS) ||
1569 (!DWC3_IP_IS(DWC32) &&
1570 hwparam_gen != DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1571 dev_warn(dev, "UDC doesn't support SSP\n");
1574 dev_err(dev, "invalid maximum_speed parameter %d\n",
1575 dwc->maximum_speed);
1577 case USB_SPEED_UNKNOWN:
1578 switch (hwparam_gen) {
1579 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
1580 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1582 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
1583 if (DWC3_IP_IS(DWC32))
1584 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1586 dwc->maximum_speed = USB_SPEED_SUPER;
1588 case DWC3_GHWPARAMS3_SSPHY_IFC_DIS:
1589 dwc->maximum_speed = USB_SPEED_HIGH;
1592 dwc->maximum_speed = USB_SPEED_SUPER;
1599 * Currently the controller does not have visibility into the HW
1600 * parameter to determine the maximum number of lanes the HW supports.
1601 * If the number of lanes is not specified in the device property, then
1602 * set the default to support dual-lane for DWC_usb32 and single-lane
1603 * for DWC_usb31 for super-speed-plus.
1605 if (dwc->maximum_speed == USB_SPEED_SUPER_PLUS) {
1606 switch (dwc->max_ssp_rate) {
1607 case USB_SSP_GEN_2x1:
1608 if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_GEN1)
1609 dev_warn(dev, "UDC only supports Gen 1\n");
1611 case USB_SSP_GEN_1x2:
1612 case USB_SSP_GEN_2x2:
1613 if (DWC3_IP_IS(DWC31))
1614 dev_warn(dev, "UDC only supports single lane\n");
1616 case USB_SSP_GEN_UNKNOWN:
1618 switch (hwparam_gen) {
1619 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
1620 if (DWC3_IP_IS(DWC32))
1621 dwc->max_ssp_rate = USB_SSP_GEN_2x2;
1623 dwc->max_ssp_rate = USB_SSP_GEN_2x1;
1625 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
1626 if (DWC3_IP_IS(DWC32))
1627 dwc->max_ssp_rate = USB_SSP_GEN_1x2;
1635 static int dwc3_probe(struct platform_device *pdev)
1637 struct device *dev = &pdev->dev;
1638 struct resource *res, dwc_res;
1645 dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
1651 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1653 dev_err(dev, "missing memory resource\n");
1657 dwc->xhci_resources[0].start = res->start;
1658 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1660 dwc->xhci_resources[0].flags = res->flags;
1661 dwc->xhci_resources[0].name = res->name;
1664 * Request memory region but exclude xHCI regs,
1665 * since it will be requested by the xhci-plat driver.
1668 dwc_res.start += DWC3_GLOBALS_REGS_START;
1670 regs = devm_ioremap_resource(dev, &dwc_res);
1672 return PTR_ERR(regs);
1675 dwc->regs_size = resource_size(&dwc_res);
1677 dwc3_get_properties(dwc);
1679 if (!dwc->sysdev_is_parent) {
1680 ret = dma_set_mask_and_coherent(dwc->sysdev, DMA_BIT_MASK(64));
1685 dwc->reset = devm_reset_control_array_get_optional_shared(dev);
1686 if (IS_ERR(dwc->reset))
1687 return PTR_ERR(dwc->reset);
1691 * Clocks are optional, but new DT platforms should support all
1692 * clocks as required by the DT-binding.
1694 dwc->bus_clk = devm_clk_get_optional(dev, "bus_early");
1695 if (IS_ERR(dwc->bus_clk))
1696 return dev_err_probe(dev, PTR_ERR(dwc->bus_clk),
1697 "could not get bus clock\n");
1699 dwc->ref_clk = devm_clk_get_optional(dev, "ref");
1700 if (IS_ERR(dwc->ref_clk))
1701 return dev_err_probe(dev, PTR_ERR(dwc->ref_clk),
1702 "could not get ref clock\n");
1704 dwc->susp_clk = devm_clk_get_optional(dev, "suspend");
1705 if (IS_ERR(dwc->susp_clk))
1706 return dev_err_probe(dev, PTR_ERR(dwc->susp_clk),
1707 "could not get suspend clock\n");
1710 ret = reset_control_deassert(dwc->reset);
1714 ret = dwc3_clk_enable(dwc);
1718 if (!dwc3_core_is_valid(dwc)) {
1719 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
1724 platform_set_drvdata(pdev, dwc);
1725 dwc3_cache_hwparams(dwc);
1727 spin_lock_init(&dwc->lock);
1728 mutex_init(&dwc->mutex);
1730 pm_runtime_set_active(dev);
1731 pm_runtime_use_autosuspend(dev);
1732 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
1733 pm_runtime_enable(dev);
1734 ret = pm_runtime_get_sync(dev);
1738 pm_runtime_forbid(dev);
1740 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1742 dev_err(dwc->dev, "failed to allocate event buffers\n");
1747 ret = dwc3_get_dr_mode(dwc);
1751 ret = dwc3_alloc_scratch_buffers(dwc);
1755 ret = dwc3_core_init(dwc);
1757 dev_err_probe(dev, ret, "failed to initialize core\n");
1761 dwc3_check_params(dwc);
1762 dwc3_debugfs_init(dwc);
1764 ret = dwc3_core_init_mode(dwc);
1768 pm_runtime_put(dev);
1773 dwc3_debugfs_exit(dwc);
1774 dwc3_event_buffers_cleanup(dwc);
1776 usb_phy_shutdown(dwc->usb2_phy);
1777 usb_phy_shutdown(dwc->usb3_phy);
1778 phy_exit(dwc->usb2_generic_phy);
1779 phy_exit(dwc->usb3_generic_phy);
1781 usb_phy_set_suspend(dwc->usb2_phy, 1);
1782 usb_phy_set_suspend(dwc->usb3_phy, 1);
1783 phy_power_off(dwc->usb2_generic_phy);
1784 phy_power_off(dwc->usb3_generic_phy);
1786 dwc3_ulpi_exit(dwc);
1789 dwc3_free_scratch_buffers(dwc);
1792 dwc3_free_event_buffers(dwc);
1795 pm_runtime_allow(&pdev->dev);
1798 pm_runtime_put_sync(&pdev->dev);
1799 pm_runtime_disable(&pdev->dev);
1802 dwc3_clk_disable(dwc);
1804 reset_control_assert(dwc->reset);
1807 power_supply_put(dwc->usb_psy);
1812 static int dwc3_remove(struct platform_device *pdev)
1814 struct dwc3 *dwc = platform_get_drvdata(pdev);
1816 pm_runtime_get_sync(&pdev->dev);
1818 dwc3_core_exit_mode(dwc);
1819 dwc3_debugfs_exit(dwc);
1821 dwc3_core_exit(dwc);
1822 dwc3_ulpi_exit(dwc);
1824 pm_runtime_disable(&pdev->dev);
1825 pm_runtime_put_noidle(&pdev->dev);
1826 pm_runtime_set_suspended(&pdev->dev);
1828 dwc3_free_event_buffers(dwc);
1829 dwc3_free_scratch_buffers(dwc);
1832 power_supply_put(dwc->usb_psy);
1838 static int dwc3_core_init_for_resume(struct dwc3 *dwc)
1842 ret = reset_control_deassert(dwc->reset);
1846 ret = dwc3_clk_enable(dwc);
1850 ret = dwc3_core_init(dwc);
1857 dwc3_clk_disable(dwc);
1859 reset_control_assert(dwc->reset);
1864 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
1866 unsigned long flags;
1869 switch (dwc->current_dr_role) {
1870 case DWC3_GCTL_PRTCAP_DEVICE:
1871 if (pm_runtime_suspended(dwc->dev))
1873 spin_lock_irqsave(&dwc->lock, flags);
1874 dwc3_gadget_suspend(dwc);
1875 spin_unlock_irqrestore(&dwc->lock, flags);
1876 synchronize_irq(dwc->irq_gadget);
1877 dwc3_core_exit(dwc);
1879 case DWC3_GCTL_PRTCAP_HOST:
1880 if (!PMSG_IS_AUTO(msg)) {
1881 dwc3_core_exit(dwc);
1885 /* Let controller to suspend HSPHY before PHY driver suspends */
1886 if (dwc->dis_u2_susphy_quirk ||
1887 dwc->dis_enblslpm_quirk) {
1888 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1889 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
1890 DWC3_GUSB2PHYCFG_SUSPHY;
1891 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1893 /* Give some time for USB2 PHY to suspend */
1894 usleep_range(5000, 6000);
1897 phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
1898 phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
1900 case DWC3_GCTL_PRTCAP_OTG:
1901 /* do nothing during runtime_suspend */
1902 if (PMSG_IS_AUTO(msg))
1905 if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1906 spin_lock_irqsave(&dwc->lock, flags);
1907 dwc3_gadget_suspend(dwc);
1908 spin_unlock_irqrestore(&dwc->lock, flags);
1909 synchronize_irq(dwc->irq_gadget);
1913 dwc3_core_exit(dwc);
1923 static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
1925 unsigned long flags;
1929 switch (dwc->current_dr_role) {
1930 case DWC3_GCTL_PRTCAP_DEVICE:
1931 ret = dwc3_core_init_for_resume(dwc);
1935 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1936 spin_lock_irqsave(&dwc->lock, flags);
1937 dwc3_gadget_resume(dwc);
1938 spin_unlock_irqrestore(&dwc->lock, flags);
1940 case DWC3_GCTL_PRTCAP_HOST:
1941 if (!PMSG_IS_AUTO(msg)) {
1942 ret = dwc3_core_init_for_resume(dwc);
1945 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1948 /* Restore GUSB2PHYCFG bits that were modified in suspend */
1949 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1950 if (dwc->dis_u2_susphy_quirk)
1951 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1953 if (dwc->dis_enblslpm_quirk)
1954 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
1956 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1958 phy_pm_runtime_get_sync(dwc->usb2_generic_phy);
1959 phy_pm_runtime_get_sync(dwc->usb3_generic_phy);
1961 case DWC3_GCTL_PRTCAP_OTG:
1962 /* nothing to do on runtime_resume */
1963 if (PMSG_IS_AUTO(msg))
1966 ret = dwc3_core_init_for_resume(dwc);
1970 dwc3_set_prtcap(dwc, dwc->current_dr_role);
1973 if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
1974 dwc3_otg_host_init(dwc);
1975 } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1976 spin_lock_irqsave(&dwc->lock, flags);
1977 dwc3_gadget_resume(dwc);
1978 spin_unlock_irqrestore(&dwc->lock, flags);
1990 static int dwc3_runtime_checks(struct dwc3 *dwc)
1992 switch (dwc->current_dr_role) {
1993 case DWC3_GCTL_PRTCAP_DEVICE:
1997 case DWC3_GCTL_PRTCAP_HOST:
2006 static int dwc3_runtime_suspend(struct device *dev)
2008 struct dwc3 *dwc = dev_get_drvdata(dev);
2011 if (dwc3_runtime_checks(dwc))
2014 ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
2018 device_init_wakeup(dev, true);
2023 static int dwc3_runtime_resume(struct device *dev)
2025 struct dwc3 *dwc = dev_get_drvdata(dev);
2028 device_init_wakeup(dev, false);
2030 ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
2034 switch (dwc->current_dr_role) {
2035 case DWC3_GCTL_PRTCAP_DEVICE:
2036 dwc3_gadget_process_pending_events(dwc);
2038 case DWC3_GCTL_PRTCAP_HOST:
2044 pm_runtime_mark_last_busy(dev);
2049 static int dwc3_runtime_idle(struct device *dev)
2051 struct dwc3 *dwc = dev_get_drvdata(dev);
2053 switch (dwc->current_dr_role) {
2054 case DWC3_GCTL_PRTCAP_DEVICE:
2055 if (dwc3_runtime_checks(dwc))
2058 case DWC3_GCTL_PRTCAP_HOST:
2064 pm_runtime_mark_last_busy(dev);
2065 pm_runtime_autosuspend(dev);
2069 #endif /* CONFIG_PM */
2071 #ifdef CONFIG_PM_SLEEP
2072 static int dwc3_suspend(struct device *dev)
2074 struct dwc3 *dwc = dev_get_drvdata(dev);
2077 ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
2081 pinctrl_pm_select_sleep_state(dev);
2086 static int dwc3_resume(struct device *dev)
2088 struct dwc3 *dwc = dev_get_drvdata(dev);
2091 pinctrl_pm_select_default_state(dev);
2093 ret = dwc3_resume_common(dwc, PMSG_RESUME);
2097 pm_runtime_disable(dev);
2098 pm_runtime_set_active(dev);
2099 pm_runtime_enable(dev);
2104 static void dwc3_complete(struct device *dev)
2106 struct dwc3 *dwc = dev_get_drvdata(dev);
2109 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST &&
2110 dwc->dis_split_quirk) {
2111 reg = dwc3_readl(dwc->regs, DWC3_GUCTL3);
2112 reg |= DWC3_GUCTL3_SPLITDISABLE;
2113 dwc3_writel(dwc->regs, DWC3_GUCTL3, reg);
2117 #define dwc3_complete NULL
2118 #endif /* CONFIG_PM_SLEEP */
2120 static const struct dev_pm_ops dwc3_dev_pm_ops = {
2121 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
2122 .complete = dwc3_complete,
2123 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
2128 static const struct of_device_id of_dwc3_match[] = {
2130 .compatible = "snps,dwc3"
2133 .compatible = "synopsys,dwc3"
2137 MODULE_DEVICE_TABLE(of, of_dwc3_match);
2142 #define ACPI_ID_INTEL_BSW "808622B7"
2144 static const struct acpi_device_id dwc3_acpi_match[] = {
2145 { ACPI_ID_INTEL_BSW, 0 },
2148 MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
2151 static struct platform_driver dwc3_driver = {
2152 .probe = dwc3_probe,
2153 .remove = dwc3_remove,
2156 .of_match_table = of_match_ptr(of_dwc3_match),
2157 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
2158 .pm = &dwc3_dev_pm_ops,
2162 module_platform_driver(dwc3_driver);
2164 MODULE_ALIAS("platform:dwc3");
2166 MODULE_LICENSE("GPL v2");
2167 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");