1 // SPDX-License-Identifier: GPL-2.0
3 * core.c - DesignWare USB3 DRD Controller Core file
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
11 #include <linux/clk.h>
12 #include <linux/version.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/interrupt.h>
20 #include <linux/ioport.h>
22 #include <linux/list.h>
23 #include <linux/delay.h>
24 #include <linux/dma-mapping.h>
26 #include <linux/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 reg &= ~DWC3_DCTL_RUN_STOP;
278 dwc3_gadget_dctl_write_safe(dwc, reg);
281 * For DWC_usb31 controller 1.90a and later, the DCTL.CSFRST bit
282 * is cleared only after all the clocks are synchronized. This can
283 * take a little more than 50ms. Set the polling rate at 20ms
284 * for 10 times instead.
286 if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
290 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
291 if (!(reg & DWC3_DCTL_CSFTRST))
294 if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
304 * For DWC_usb31 controller 1.80a and prior, once DCTL.CSFRST bit
305 * is cleared, we must wait at least 50ms before accessing the PHY
306 * domain (synchronization delay).
308 if (DWC3_VER_IS_WITHIN(DWC31, ANY, 180A))
315 * dwc3_frame_length_adjustment - Adjusts frame length if required
316 * @dwc3: Pointer to our controller context structure
318 static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
323 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
329 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
330 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
331 if (dft != dwc->fladj) {
332 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
333 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
334 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
339 * dwc3_ref_clk_period - Reference clock period configuration
340 * Default reference clock period depends on hardware
341 * configuration. For systems with reference clock that differs
342 * from the default, this will set clock period in DWC3_GUCTL
344 * @dwc: Pointer to our controller context structure
345 * @ref_clk_per: reference clock period in ns
347 static void dwc3_ref_clk_period(struct dwc3 *dwc)
349 unsigned long period;
356 rate = clk_get_rate(dwc->ref_clk);
359 period = NSEC_PER_SEC / rate;
360 } else if (dwc->ref_clk_per) {
361 period = dwc->ref_clk_per;
362 rate = NSEC_PER_SEC / period;
367 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
368 reg &= ~DWC3_GUCTL_REFCLKPER_MASK;
369 reg |= FIELD_PREP(DWC3_GUCTL_REFCLKPER_MASK, period);
370 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
372 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
376 * The calculation below is
378 * 125000 * (NSEC_PER_SEC / (rate * period) - 1)
380 * but rearranged for fixed-point arithmetic. The division must be
381 * 64-bit because 125000 * NSEC_PER_SEC doesn't fit in 32 bits (and
382 * neither does rate * period).
384 * Note that rate * period ~= NSEC_PER_SECOND, minus the number of
385 * nanoseconds of error caused by the truncation which happened during
386 * the division when calculating rate or period (whichever one was
387 * derived from the other). We first calculate the relative error, then
388 * scale it to units of 8 ppm.
390 fladj = div64_u64(125000ULL * NSEC_PER_SEC, (u64)rate * period);
394 * The documented 240MHz constant is scaled by 2 to get PLS1 as well.
396 decr = 480000000 / rate;
398 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
399 reg &= ~DWC3_GFLADJ_REFCLK_FLADJ_MASK
400 & ~DWC3_GFLADJ_240MHZDECR
401 & ~DWC3_GFLADJ_240MHZDECR_PLS1;
402 reg |= FIELD_PREP(DWC3_GFLADJ_REFCLK_FLADJ_MASK, fladj)
403 | FIELD_PREP(DWC3_GFLADJ_240MHZDECR, decr >> 1)
404 | FIELD_PREP(DWC3_GFLADJ_240MHZDECR_PLS1, decr & 1);
405 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
409 * dwc3_free_one_event_buffer - Frees one event buffer
410 * @dwc: Pointer to our controller context structure
411 * @evt: Pointer to event buffer to be freed
413 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
414 struct dwc3_event_buffer *evt)
416 dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
420 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
421 * @dwc: Pointer to our controller context structure
422 * @length: size of the event buffer
424 * Returns a pointer to the allocated event buffer structure on success
425 * otherwise ERR_PTR(errno).
427 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
430 struct dwc3_event_buffer *evt;
432 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
434 return ERR_PTR(-ENOMEM);
437 evt->length = length;
438 evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
440 return ERR_PTR(-ENOMEM);
442 evt->buf = dma_alloc_coherent(dwc->sysdev, length,
443 &evt->dma, GFP_KERNEL);
445 return ERR_PTR(-ENOMEM);
451 * dwc3_free_event_buffers - frees all allocated event buffers
452 * @dwc: Pointer to our controller context structure
454 static void dwc3_free_event_buffers(struct dwc3 *dwc)
456 struct dwc3_event_buffer *evt;
460 dwc3_free_one_event_buffer(dwc, evt);
464 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
465 * @dwc: pointer to our controller context structure
466 * @length: size of event buffer
468 * Returns 0 on success otherwise negative errno. In the error case, dwc
469 * may contain some buffers allocated but not all which were requested.
471 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
473 struct dwc3_event_buffer *evt;
475 evt = dwc3_alloc_one_event_buffer(dwc, length);
477 dev_err(dwc->dev, "can't allocate event buffer\n");
486 * dwc3_event_buffers_setup - setup our allocated event buffers
487 * @dwc: pointer to our controller context structure
489 * Returns 0 on success otherwise negative errno.
491 int dwc3_event_buffers_setup(struct dwc3 *dwc)
493 struct dwc3_event_buffer *evt;
497 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
498 lower_32_bits(evt->dma));
499 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
500 upper_32_bits(evt->dma));
501 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
502 DWC3_GEVNTSIZ_SIZE(evt->length));
503 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
508 void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
510 struct dwc3_event_buffer *evt;
516 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
517 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
518 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
519 | DWC3_GEVNTSIZ_SIZE(0));
520 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
523 static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
525 if (!dwc->has_hibernation)
528 if (!dwc->nr_scratch)
531 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
532 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
533 if (!dwc->scratchbuf)
539 static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
541 dma_addr_t scratch_addr;
545 if (!dwc->has_hibernation)
548 if (!dwc->nr_scratch)
551 /* should never fall here */
552 if (!WARN_ON(dwc->scratchbuf))
555 scratch_addr = dma_map_single(dwc->sysdev, dwc->scratchbuf,
556 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
558 if (dma_mapping_error(dwc->sysdev, scratch_addr)) {
559 dev_err(dwc->sysdev, "failed to map scratch buffer\n");
564 dwc->scratch_addr = scratch_addr;
566 param = lower_32_bits(scratch_addr);
568 ret = dwc3_send_gadget_generic_command(dwc,
569 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
573 param = upper_32_bits(scratch_addr);
575 ret = dwc3_send_gadget_generic_command(dwc,
576 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
583 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
584 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
590 static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
592 if (!dwc->has_hibernation)
595 if (!dwc->nr_scratch)
598 /* should never fall here */
599 if (!WARN_ON(dwc->scratchbuf))
602 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
603 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
604 kfree(dwc->scratchbuf);
607 static void dwc3_core_num_eps(struct dwc3 *dwc)
609 struct dwc3_hwparams *parms = &dwc->hwparams;
611 dwc->num_eps = DWC3_NUM_EPS(parms);
614 static void dwc3_cache_hwparams(struct dwc3 *dwc)
616 struct dwc3_hwparams *parms = &dwc->hwparams;
618 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
619 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
620 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
621 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
622 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
623 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
624 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
625 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
626 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
628 if (DWC3_IP_IS(DWC32))
629 parms->hwparams9 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS9);
632 static int dwc3_core_ulpi_init(struct dwc3 *dwc)
637 intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
639 if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
640 (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
641 dwc->hsphy_interface &&
642 !strncmp(dwc->hsphy_interface, "ulpi", 4)))
643 ret = dwc3_ulpi_init(dwc);
649 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
650 * @dwc: Pointer to our controller context structure
652 * Returns 0 on success. The USB PHY interfaces are configured but not
653 * initialized. The PHY interfaces and the PHYs get initialized together with
654 * the core in dwc3_core_init.
656 static int dwc3_phy_setup(struct dwc3 *dwc)
658 unsigned int hw_mode;
661 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
663 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
666 * Make sure UX_EXIT_PX is cleared as that causes issues with some
667 * PHYs. Also, this bit is not supposed to be used in normal operation.
669 reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
672 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
673 * to '0' during coreConsultant configuration. So default value
674 * will be '0' when the core is reset. Application needs to set it
675 * to '1' after the core initialization is completed.
677 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
678 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
681 * For DRD controllers, GUSB3PIPECTL.SUSPENDENABLE must be cleared after
682 * power-on reset, and it can be set after core initialization, which is
683 * after device soft-reset during initialization.
685 if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD)
686 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
688 if (dwc->u2ss_inp3_quirk)
689 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
691 if (dwc->dis_rxdet_inp3_quirk)
692 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
694 if (dwc->req_p1p2p3_quirk)
695 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
697 if (dwc->del_p1p2p3_quirk)
698 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
700 if (dwc->del_phy_power_chg_quirk)
701 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
703 if (dwc->lfps_filter_quirk)
704 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
706 if (dwc->rx_detect_poll_quirk)
707 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
709 if (dwc->tx_de_emphasis_quirk)
710 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
712 if (dwc->dis_u3_susphy_quirk)
713 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
715 if (dwc->dis_del_phy_power_chg_quirk)
716 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
718 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
720 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
722 /* Select the HS PHY interface */
723 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
724 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
725 if (dwc->hsphy_interface &&
726 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
727 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
729 } else if (dwc->hsphy_interface &&
730 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
731 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
732 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
734 /* Relying on default value. */
735 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
739 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
744 switch (dwc->hsphy_mode) {
745 case USBPHY_INTERFACE_MODE_UTMI:
746 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
747 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
748 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
749 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
751 case USBPHY_INTERFACE_MODE_UTMIW:
752 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
753 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
754 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
755 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
762 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
763 * '0' during coreConsultant configuration. So default value will
764 * be '0' when the core is reset. Application needs to set it to
765 * '1' after the core initialization is completed.
767 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
768 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
771 * For DRD controllers, GUSB2PHYCFG.SUSPHY must be cleared after
772 * power-on reset, and it can be set after core initialization, which is
773 * after device soft-reset during initialization.
775 if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD)
776 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
778 if (dwc->dis_u2_susphy_quirk)
779 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
781 if (dwc->dis_enblslpm_quirk)
782 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
784 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM;
786 if (dwc->dis_u2_freeclk_exists_quirk)
787 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
789 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
794 static int dwc3_clk_enable(struct dwc3 *dwc)
798 ret = clk_prepare_enable(dwc->bus_clk);
802 ret = clk_prepare_enable(dwc->ref_clk);
804 goto disable_bus_clk;
806 ret = clk_prepare_enable(dwc->susp_clk);
808 goto disable_ref_clk;
813 clk_disable_unprepare(dwc->ref_clk);
815 clk_disable_unprepare(dwc->bus_clk);
819 static void dwc3_clk_disable(struct dwc3 *dwc)
821 clk_disable_unprepare(dwc->susp_clk);
822 clk_disable_unprepare(dwc->ref_clk);
823 clk_disable_unprepare(dwc->bus_clk);
826 static void dwc3_core_exit(struct dwc3 *dwc)
828 dwc3_event_buffers_cleanup(dwc);
830 usb_phy_shutdown(dwc->usb2_phy);
831 usb_phy_shutdown(dwc->usb3_phy);
832 phy_exit(dwc->usb2_generic_phy);
833 phy_exit(dwc->usb3_generic_phy);
835 usb_phy_set_suspend(dwc->usb2_phy, 1);
836 usb_phy_set_suspend(dwc->usb3_phy, 1);
837 phy_power_off(dwc->usb2_generic_phy);
838 phy_power_off(dwc->usb3_generic_phy);
839 dwc3_clk_disable(dwc);
840 reset_control_assert(dwc->reset);
843 static bool dwc3_core_is_valid(struct dwc3 *dwc)
847 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
848 dwc->ip = DWC3_GSNPS_ID(reg);
850 /* This should read as U3 followed by revision number */
851 if (DWC3_IP_IS(DWC3)) {
853 } else if (DWC3_IP_IS(DWC31) || DWC3_IP_IS(DWC32)) {
854 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
855 dwc->version_type = dwc3_readl(dwc->regs, DWC3_VER_TYPE);
863 static void dwc3_core_setup_global_control(struct dwc3 *dwc)
865 u32 hwparams4 = dwc->hwparams.hwparams4;
868 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
869 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
871 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
872 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
874 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
875 * issue which would cause xHCI compliance tests to fail.
877 * Because of that we cannot enable clock gating on such
882 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
885 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
886 dwc->dr_mode == USB_DR_MODE_OTG) &&
887 DWC3_VER_IS_WITHIN(DWC3, 210A, 250A))
888 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
890 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
892 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
893 /* enable hibernation here */
894 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
897 * REVISIT Enabling this bit so that host-mode hibernation
898 * will work. Device-mode hibernation is not yet implemented.
900 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
907 /* check if current dwc3 is on simulation board */
908 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
909 dev_info(dwc->dev, "Running with FPGA optimizations\n");
913 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
914 "disable_scramble cannot be used on non-FPGA builds\n");
916 if (dwc->disable_scramble_quirk && dwc->is_fpga)
917 reg |= DWC3_GCTL_DISSCRAMBLE;
919 reg &= ~DWC3_GCTL_DISSCRAMBLE;
921 if (dwc->u2exit_lfps_quirk)
922 reg |= DWC3_GCTL_U2EXIT_LFPS;
925 * WORKAROUND: DWC3 revisions <1.90a have a bug
926 * where the device can fail to connect at SuperSpeed
927 * and falls back to high-speed mode which causes
928 * the device to enter a Connect/Disconnect loop
930 if (DWC3_VER_IS_PRIOR(DWC3, 190A))
931 reg |= DWC3_GCTL_U2RSTECN;
933 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
936 static int dwc3_core_get_phy(struct dwc3 *dwc);
937 static int dwc3_core_ulpi_init(struct dwc3 *dwc);
939 /* set global incr burst type configuration registers */
940 static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
942 struct device *dev = dwc->dev;
943 /* incrx_mode : for INCR burst type. */
945 /* incrx_size : for size of INCRX burst. */
953 cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
956 * Handle property "snps,incr-burst-type-adjustment".
957 * Get the number of value from this property:
958 * result <= 0, means this property is not supported.
959 * result = 1, means INCRx burst mode supported.
960 * result > 1, means undefined length burst mode supported.
962 ntype = device_property_count_u32(dev, "snps,incr-burst-type-adjustment");
966 vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL);
968 dev_err(dev, "Error to get memory\n");
972 /* Get INCR burst type, and parse it */
973 ret = device_property_read_u32_array(dev,
974 "snps,incr-burst-type-adjustment", vals, ntype);
977 dev_err(dev, "Error to get property\n");
984 /* INCRX (undefined length) burst mode */
985 incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
986 for (i = 1; i < ntype; i++) {
987 if (vals[i] > incrx_size)
988 incrx_size = vals[i];
991 /* INCRX burst mode */
992 incrx_mode = INCRX_BURST_MODE;
997 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
998 cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
1000 cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
1001 switch (incrx_size) {
1003 cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
1006 cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
1009 cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
1012 cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
1015 cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
1018 cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
1021 cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
1026 dev_err(dev, "Invalid property\n");
1030 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
1034 * dwc3_core_init - Low-level initialization of DWC3 Core
1035 * @dwc: Pointer to our controller context structure
1037 * Returns 0 on success otherwise negative errno.
1039 static int dwc3_core_init(struct dwc3 *dwc)
1041 unsigned int hw_mode;
1045 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
1048 * Write Linux Version Code to our GUID register so it's easy to figure
1049 * out which kernel version a bug was found.
1051 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
1053 ret = dwc3_phy_setup(dwc);
1057 if (!dwc->ulpi_ready) {
1058 ret = dwc3_core_ulpi_init(dwc);
1061 dwc->ulpi_ready = true;
1064 if (!dwc->phys_ready) {
1065 ret = dwc3_core_get_phy(dwc);
1068 dwc->phys_ready = true;
1071 usb_phy_init(dwc->usb2_phy);
1072 usb_phy_init(dwc->usb3_phy);
1073 ret = phy_init(dwc->usb2_generic_phy);
1077 ret = phy_init(dwc->usb3_generic_phy);
1079 phy_exit(dwc->usb2_generic_phy);
1083 ret = dwc3_core_soft_reset(dwc);
1087 if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD &&
1088 !DWC3_VER_IS_WITHIN(DWC3, ANY, 194A)) {
1089 if (!dwc->dis_u3_susphy_quirk) {
1090 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1091 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1092 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1095 if (!dwc->dis_u2_susphy_quirk) {
1096 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1097 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1098 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1102 dwc3_core_setup_global_control(dwc);
1103 dwc3_core_num_eps(dwc);
1105 ret = dwc3_setup_scratch_buffers(dwc);
1109 /* Adjust Frame Length */
1110 dwc3_frame_length_adjustment(dwc);
1112 /* Adjust Reference Clock Period */
1113 dwc3_ref_clk_period(dwc);
1115 dwc3_set_incr_burst_type(dwc);
1117 usb_phy_set_suspend(dwc->usb2_phy, 0);
1118 usb_phy_set_suspend(dwc->usb3_phy, 0);
1119 ret = phy_power_on(dwc->usb2_generic_phy);
1123 ret = phy_power_on(dwc->usb3_generic_phy);
1127 ret = dwc3_event_buffers_setup(dwc);
1129 dev_err(dwc->dev, "failed to setup event buffers\n");
1134 * ENDXFER polling is available on version 3.10a and later of
1135 * the DWC_usb3 controller. It is NOT available in the
1136 * DWC_usb31 controller.
1138 if (DWC3_VER_IS_WITHIN(DWC3, 310A, ANY)) {
1139 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
1140 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
1141 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
1144 if (!DWC3_VER_IS_PRIOR(DWC3, 250A)) {
1145 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1148 * Enable hardware control of sending remote wakeup
1149 * in HS when the device is in the L1 state.
1151 if (!DWC3_VER_IS_PRIOR(DWC3, 290A))
1152 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
1155 * Decouple USB 2.0 L1 & L2 events which will allow for
1156 * gadget driver to only receive U3/L2 suspend & wakeup
1157 * events and prevent the more frequent L1 LPM transitions
1158 * from interrupting the driver.
1160 if (!DWC3_VER_IS_PRIOR(DWC3, 300A))
1161 reg |= DWC3_GUCTL1_DEV_DECOUPLE_L1L2_EVT;
1163 if (dwc->dis_tx_ipgap_linecheck_quirk)
1164 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
1166 if (dwc->parkmode_disable_ss_quirk)
1167 reg |= DWC3_GUCTL1_PARKMODE_DISABLE_SS;
1169 if (DWC3_VER_IS_WITHIN(DWC3, 290A, ANY) &&
1170 (dwc->maximum_speed == USB_SPEED_HIGH ||
1171 dwc->maximum_speed == USB_SPEED_FULL))
1172 reg |= DWC3_GUCTL1_DEV_FORCE_20_CLK_FOR_30_CLK;
1174 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1177 if (dwc->dr_mode == USB_DR_MODE_HOST ||
1178 dwc->dr_mode == USB_DR_MODE_OTG) {
1179 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
1182 * Enable Auto retry Feature to make the controller operating in
1183 * Host mode on seeing transaction errors(CRC errors or internal
1184 * overrun scenerios) on IN transfers to reply to the device
1185 * with a non-terminating retry ACK (i.e, an ACK transcation
1186 * packet with Retry=1 & Nump != 0)
1188 reg |= DWC3_GUCTL_HSTINAUTORETRY;
1190 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
1194 * Must config both number of packets and max burst settings to enable
1195 * RX and/or TX threshold.
1197 if (!DWC3_IP_IS(DWC3) && dwc->dr_mode == USB_DR_MODE_HOST) {
1198 u8 rx_thr_num = dwc->rx_thr_num_pkt_prd;
1199 u8 rx_maxburst = dwc->rx_max_burst_prd;
1200 u8 tx_thr_num = dwc->tx_thr_num_pkt_prd;
1201 u8 tx_maxburst = dwc->tx_max_burst_prd;
1203 if (rx_thr_num && rx_maxburst) {
1204 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1205 reg |= DWC31_RXTHRNUMPKTSEL_PRD;
1207 reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
1208 reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
1210 reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
1211 reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
1213 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1216 if (tx_thr_num && tx_maxburst) {
1217 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1218 reg |= DWC31_TXTHRNUMPKTSEL_PRD;
1220 reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
1221 reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
1223 reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
1224 reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
1226 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1233 phy_power_off(dwc->usb3_generic_phy);
1236 phy_power_off(dwc->usb2_generic_phy);
1239 usb_phy_set_suspend(dwc->usb2_phy, 1);
1240 usb_phy_set_suspend(dwc->usb3_phy, 1);
1243 usb_phy_shutdown(dwc->usb2_phy);
1244 usb_phy_shutdown(dwc->usb3_phy);
1245 phy_exit(dwc->usb2_generic_phy);
1246 phy_exit(dwc->usb3_generic_phy);
1249 dwc3_ulpi_exit(dwc);
1255 static int dwc3_core_get_phy(struct dwc3 *dwc)
1257 struct device *dev = dwc->dev;
1258 struct device_node *node = dev->of_node;
1262 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
1263 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
1265 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1266 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
1269 if (IS_ERR(dwc->usb2_phy)) {
1270 ret = PTR_ERR(dwc->usb2_phy);
1271 if (ret == -ENXIO || ret == -ENODEV) {
1272 dwc->usb2_phy = NULL;
1274 return dev_err_probe(dev, ret, "no usb2 phy configured\n");
1278 if (IS_ERR(dwc->usb3_phy)) {
1279 ret = PTR_ERR(dwc->usb3_phy);
1280 if (ret == -ENXIO || ret == -ENODEV) {
1281 dwc->usb3_phy = NULL;
1283 return dev_err_probe(dev, ret, "no usb3 phy configured\n");
1287 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
1288 if (IS_ERR(dwc->usb2_generic_phy)) {
1289 ret = PTR_ERR(dwc->usb2_generic_phy);
1290 if (ret == -ENOSYS || ret == -ENODEV) {
1291 dwc->usb2_generic_phy = NULL;
1293 return dev_err_probe(dev, ret, "no usb2 phy configured\n");
1297 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
1298 if (IS_ERR(dwc->usb3_generic_phy)) {
1299 ret = PTR_ERR(dwc->usb3_generic_phy);
1300 if (ret == -ENOSYS || ret == -ENODEV) {
1301 dwc->usb3_generic_phy = NULL;
1303 return dev_err_probe(dev, ret, "no usb3 phy configured\n");
1310 static int dwc3_core_init_mode(struct dwc3 *dwc)
1312 struct device *dev = dwc->dev;
1315 switch (dwc->dr_mode) {
1316 case USB_DR_MODE_PERIPHERAL:
1317 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1320 otg_set_vbus(dwc->usb2_phy->otg, false);
1321 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
1322 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
1324 ret = dwc3_gadget_init(dwc);
1326 return dev_err_probe(dev, ret, "failed to initialize gadget\n");
1328 case USB_DR_MODE_HOST:
1329 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1332 otg_set_vbus(dwc->usb2_phy->otg, true);
1333 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
1334 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
1336 ret = dwc3_host_init(dwc);
1338 return dev_err_probe(dev, ret, "failed to initialize host\n");
1340 case USB_DR_MODE_OTG:
1341 INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
1342 ret = dwc3_drd_init(dwc);
1344 return dev_err_probe(dev, ret, "failed to initialize dual-role\n");
1347 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1354 static void dwc3_core_exit_mode(struct dwc3 *dwc)
1356 switch (dwc->dr_mode) {
1357 case USB_DR_MODE_PERIPHERAL:
1358 dwc3_gadget_exit(dwc);
1360 case USB_DR_MODE_HOST:
1361 dwc3_host_exit(dwc);
1363 case USB_DR_MODE_OTG:
1371 /* de-assert DRVVBUS for HOST and OTG mode */
1372 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1375 static void dwc3_get_properties(struct dwc3 *dwc)
1377 struct device *dev = dwc->dev;
1378 u8 lpm_nyet_threshold;
1381 u8 rx_thr_num_pkt_prd = 0;
1382 u8 rx_max_burst_prd = 0;
1383 u8 tx_thr_num_pkt_prd = 0;
1384 u8 tx_max_burst_prd = 0;
1385 u8 tx_fifo_resize_max_num;
1386 const char *usb_psy_name;
1389 /* default to highest possible threshold */
1390 lpm_nyet_threshold = 0xf;
1392 /* default to -3.5dB de-emphasis */
1396 * default to assert utmi_sleep_n and use maximum allowed HIRD
1397 * threshold value of 0b1100
1399 hird_threshold = 12;
1402 * default to a TXFIFO size large enough to fit 6 max packets. This
1403 * allows for systems with larger bus latencies to have some headroom
1404 * for endpoints that have a large bMaxBurst value.
1406 tx_fifo_resize_max_num = 6;
1408 dwc->maximum_speed = usb_get_maximum_speed(dev);
1409 dwc->max_ssp_rate = usb_get_maximum_ssp_rate(dev);
1410 dwc->dr_mode = usb_get_dr_mode(dev);
1411 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
1413 dwc->sysdev_is_parent = device_property_read_bool(dev,
1414 "linux,sysdev_is_parent");
1415 if (dwc->sysdev_is_parent)
1416 dwc->sysdev = dwc->dev->parent;
1418 dwc->sysdev = dwc->dev;
1420 ret = device_property_read_string(dev, "usb-psy-name", &usb_psy_name);
1422 dwc->usb_psy = power_supply_get_by_name(usb_psy_name);
1424 dev_err(dev, "couldn't get usb power supply\n");
1427 dwc->has_lpm_erratum = device_property_read_bool(dev,
1428 "snps,has-lpm-erratum");
1429 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
1430 &lpm_nyet_threshold);
1431 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
1432 "snps,is-utmi-l1-suspend");
1433 device_property_read_u8(dev, "snps,hird-threshold",
1435 dwc->dis_start_transfer_quirk = device_property_read_bool(dev,
1436 "snps,dis-start-transfer-quirk");
1437 dwc->usb3_lpm_capable = device_property_read_bool(dev,
1438 "snps,usb3_lpm_capable");
1439 dwc->usb2_lpm_disable = device_property_read_bool(dev,
1440 "snps,usb2-lpm-disable");
1441 dwc->usb2_gadget_lpm_disable = device_property_read_bool(dev,
1442 "snps,usb2-gadget-lpm-disable");
1443 device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
1444 &rx_thr_num_pkt_prd);
1445 device_property_read_u8(dev, "snps,rx-max-burst-prd",
1447 device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
1448 &tx_thr_num_pkt_prd);
1449 device_property_read_u8(dev, "snps,tx-max-burst-prd",
1451 dwc->do_fifo_resize = device_property_read_bool(dev,
1453 if (dwc->do_fifo_resize)
1454 device_property_read_u8(dev, "tx-fifo-max-num",
1455 &tx_fifo_resize_max_num);
1457 dwc->disable_scramble_quirk = device_property_read_bool(dev,
1458 "snps,disable_scramble_quirk");
1459 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
1460 "snps,u2exit_lfps_quirk");
1461 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
1462 "snps,u2ss_inp3_quirk");
1463 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
1464 "snps,req_p1p2p3_quirk");
1465 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
1466 "snps,del_p1p2p3_quirk");
1467 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
1468 "snps,del_phy_power_chg_quirk");
1469 dwc->lfps_filter_quirk = device_property_read_bool(dev,
1470 "snps,lfps_filter_quirk");
1471 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
1472 "snps,rx_detect_poll_quirk");
1473 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
1474 "snps,dis_u3_susphy_quirk");
1475 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
1476 "snps,dis_u2_susphy_quirk");
1477 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1478 "snps,dis_enblslpm_quirk");
1479 dwc->dis_u1_entry_quirk = device_property_read_bool(dev,
1480 "snps,dis-u1-entry-quirk");
1481 dwc->dis_u2_entry_quirk = device_property_read_bool(dev,
1482 "snps,dis-u2-entry-quirk");
1483 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1484 "snps,dis_rxdet_inp3_quirk");
1485 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1486 "snps,dis-u2-freeclk-exists-quirk");
1487 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1488 "snps,dis-del-phy-power-chg-quirk");
1489 dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1490 "snps,dis-tx-ipgap-linecheck-quirk");
1491 dwc->parkmode_disable_ss_quirk = device_property_read_bool(dev,
1492 "snps,parkmode-disable-ss-quirk");
1494 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
1495 "snps,tx_de_emphasis_quirk");
1496 device_property_read_u8(dev, "snps,tx_de_emphasis",
1498 device_property_read_string(dev, "snps,hsphy_interface",
1499 &dwc->hsphy_interface);
1500 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
1502 device_property_read_u32(dev, "snps,ref-clock-period-ns",
1505 dwc->dis_metastability_quirk = device_property_read_bool(dev,
1506 "snps,dis_metastability_quirk");
1508 dwc->dis_split_quirk = device_property_read_bool(dev,
1509 "snps,dis-split-quirk");
1511 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1512 dwc->tx_de_emphasis = tx_de_emphasis;
1514 dwc->hird_threshold = hird_threshold;
1516 dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd;
1517 dwc->rx_max_burst_prd = rx_max_burst_prd;
1519 dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd;
1520 dwc->tx_max_burst_prd = tx_max_burst_prd;
1522 dwc->imod_interval = 0;
1524 dwc->tx_fifo_resize_max_num = tx_fifo_resize_max_num;
1527 /* check whether the core supports IMOD */
1528 bool dwc3_has_imod(struct dwc3 *dwc)
1530 return DWC3_VER_IS_WITHIN(DWC3, 300A, ANY) ||
1531 DWC3_VER_IS_WITHIN(DWC31, 120A, ANY) ||
1535 static void dwc3_check_params(struct dwc3 *dwc)
1537 struct device *dev = dwc->dev;
1538 unsigned int hwparam_gen =
1539 DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3);
1541 /* Check for proper value of imod_interval */
1542 if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
1543 dev_warn(dwc->dev, "Interrupt moderation not supported\n");
1544 dwc->imod_interval = 0;
1548 * Workaround for STAR 9000961433 which affects only version
1549 * 3.00a of the DWC_usb3 core. This prevents the controller
1550 * interrupt from being masked while handling events. IMOD
1551 * allows us to work around this issue. Enable it for the
1554 if (!dwc->imod_interval &&
1555 DWC3_VER_IS(DWC3, 300A))
1556 dwc->imod_interval = 1;
1558 /* Check the maximum_speed parameter */
1559 switch (dwc->maximum_speed) {
1560 case USB_SPEED_FULL:
1561 case USB_SPEED_HIGH:
1563 case USB_SPEED_SUPER:
1564 if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS)
1565 dev_warn(dev, "UDC doesn't support Gen 1\n");
1567 case USB_SPEED_SUPER_PLUS:
1568 if ((DWC3_IP_IS(DWC32) &&
1569 hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS) ||
1570 (!DWC3_IP_IS(DWC32) &&
1571 hwparam_gen != DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1572 dev_warn(dev, "UDC doesn't support SSP\n");
1575 dev_err(dev, "invalid maximum_speed parameter %d\n",
1576 dwc->maximum_speed);
1578 case USB_SPEED_UNKNOWN:
1579 switch (hwparam_gen) {
1580 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
1581 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1583 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
1584 if (DWC3_IP_IS(DWC32))
1585 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1587 dwc->maximum_speed = USB_SPEED_SUPER;
1589 case DWC3_GHWPARAMS3_SSPHY_IFC_DIS:
1590 dwc->maximum_speed = USB_SPEED_HIGH;
1593 dwc->maximum_speed = USB_SPEED_SUPER;
1600 * Currently the controller does not have visibility into the HW
1601 * parameter to determine the maximum number of lanes the HW supports.
1602 * If the number of lanes is not specified in the device property, then
1603 * set the default to support dual-lane for DWC_usb32 and single-lane
1604 * for DWC_usb31 for super-speed-plus.
1606 if (dwc->maximum_speed == USB_SPEED_SUPER_PLUS) {
1607 switch (dwc->max_ssp_rate) {
1608 case USB_SSP_GEN_2x1:
1609 if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_GEN1)
1610 dev_warn(dev, "UDC only supports Gen 1\n");
1612 case USB_SSP_GEN_1x2:
1613 case USB_SSP_GEN_2x2:
1614 if (DWC3_IP_IS(DWC31))
1615 dev_warn(dev, "UDC only supports single lane\n");
1617 case USB_SSP_GEN_UNKNOWN:
1619 switch (hwparam_gen) {
1620 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
1621 if (DWC3_IP_IS(DWC32))
1622 dwc->max_ssp_rate = USB_SSP_GEN_2x2;
1624 dwc->max_ssp_rate = USB_SSP_GEN_2x1;
1626 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
1627 if (DWC3_IP_IS(DWC32))
1628 dwc->max_ssp_rate = USB_SSP_GEN_1x2;
1636 static int dwc3_probe(struct platform_device *pdev)
1638 struct device *dev = &pdev->dev;
1639 struct resource *res, dwc_res;
1646 dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
1652 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1654 dev_err(dev, "missing memory resource\n");
1658 dwc->xhci_resources[0].start = res->start;
1659 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1661 dwc->xhci_resources[0].flags = res->flags;
1662 dwc->xhci_resources[0].name = res->name;
1665 * Request memory region but exclude xHCI regs,
1666 * since it will be requested by the xhci-plat driver.
1669 dwc_res.start += DWC3_GLOBALS_REGS_START;
1671 regs = devm_ioremap_resource(dev, &dwc_res);
1673 return PTR_ERR(regs);
1676 dwc->regs_size = resource_size(&dwc_res);
1678 dwc3_get_properties(dwc);
1680 if (!dwc->sysdev_is_parent) {
1681 ret = dma_set_mask_and_coherent(dwc->sysdev, DMA_BIT_MASK(64));
1686 dwc->reset = devm_reset_control_array_get_optional_shared(dev);
1687 if (IS_ERR(dwc->reset))
1688 return PTR_ERR(dwc->reset);
1692 * Clocks are optional, but new DT platforms should support all
1693 * clocks as required by the DT-binding.
1694 * Some devices have different clock names in legacy device trees,
1695 * check for them to retain backwards compatibility.
1697 dwc->bus_clk = devm_clk_get_optional(dev, "bus_early");
1698 if (IS_ERR(dwc->bus_clk))
1699 return dev_err_probe(dev, PTR_ERR(dwc->bus_clk),
1700 "could not get bus clock\n");
1702 if (dwc->bus_clk == NULL) {
1703 dwc->bus_clk = devm_clk_get_optional(dev, "bus_clk");
1704 if (IS_ERR(dwc->bus_clk))
1705 return dev_err_probe(dev, PTR_ERR(dwc->bus_clk),
1706 "could not get bus clock\n");
1709 dwc->ref_clk = devm_clk_get_optional(dev, "ref");
1710 if (IS_ERR(dwc->ref_clk))
1711 return dev_err_probe(dev, PTR_ERR(dwc->ref_clk),
1712 "could not get ref clock\n");
1714 if (dwc->ref_clk == NULL) {
1715 dwc->ref_clk = devm_clk_get_optional(dev, "ref_clk");
1716 if (IS_ERR(dwc->ref_clk))
1717 return dev_err_probe(dev, PTR_ERR(dwc->ref_clk),
1718 "could not get ref clock\n");
1721 dwc->susp_clk = devm_clk_get_optional(dev, "suspend");
1722 if (IS_ERR(dwc->susp_clk))
1723 return dev_err_probe(dev, PTR_ERR(dwc->susp_clk),
1724 "could not get suspend clock\n");
1726 if (dwc->susp_clk == NULL) {
1727 dwc->susp_clk = devm_clk_get_optional(dev, "suspend_clk");
1728 if (IS_ERR(dwc->susp_clk))
1729 return dev_err_probe(dev, PTR_ERR(dwc->susp_clk),
1730 "could not get suspend clock\n");
1734 ret = reset_control_deassert(dwc->reset);
1738 ret = dwc3_clk_enable(dwc);
1742 if (!dwc3_core_is_valid(dwc)) {
1743 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
1748 platform_set_drvdata(pdev, dwc);
1749 dwc3_cache_hwparams(dwc);
1751 spin_lock_init(&dwc->lock);
1752 mutex_init(&dwc->mutex);
1754 pm_runtime_set_active(dev);
1755 pm_runtime_use_autosuspend(dev);
1756 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
1757 pm_runtime_enable(dev);
1758 ret = pm_runtime_get_sync(dev);
1762 pm_runtime_forbid(dev);
1764 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1766 dev_err(dwc->dev, "failed to allocate event buffers\n");
1771 ret = dwc3_get_dr_mode(dwc);
1775 ret = dwc3_alloc_scratch_buffers(dwc);
1779 ret = dwc3_core_init(dwc);
1781 dev_err_probe(dev, ret, "failed to initialize core\n");
1785 dwc3_check_params(dwc);
1786 dwc3_debugfs_init(dwc);
1788 ret = dwc3_core_init_mode(dwc);
1792 pm_runtime_put(dev);
1797 dwc3_debugfs_exit(dwc);
1798 dwc3_event_buffers_cleanup(dwc);
1800 usb_phy_shutdown(dwc->usb2_phy);
1801 usb_phy_shutdown(dwc->usb3_phy);
1802 phy_exit(dwc->usb2_generic_phy);
1803 phy_exit(dwc->usb3_generic_phy);
1805 usb_phy_set_suspend(dwc->usb2_phy, 1);
1806 usb_phy_set_suspend(dwc->usb3_phy, 1);
1807 phy_power_off(dwc->usb2_generic_phy);
1808 phy_power_off(dwc->usb3_generic_phy);
1810 dwc3_ulpi_exit(dwc);
1813 dwc3_free_scratch_buffers(dwc);
1816 dwc3_free_event_buffers(dwc);
1819 pm_runtime_allow(&pdev->dev);
1822 pm_runtime_put_sync(&pdev->dev);
1823 pm_runtime_disable(&pdev->dev);
1826 dwc3_clk_disable(dwc);
1828 reset_control_assert(dwc->reset);
1831 power_supply_put(dwc->usb_psy);
1836 static int dwc3_remove(struct platform_device *pdev)
1838 struct dwc3 *dwc = platform_get_drvdata(pdev);
1840 pm_runtime_get_sync(&pdev->dev);
1842 dwc3_core_exit_mode(dwc);
1843 dwc3_debugfs_exit(dwc);
1845 dwc3_core_exit(dwc);
1846 dwc3_ulpi_exit(dwc);
1848 pm_runtime_disable(&pdev->dev);
1849 pm_runtime_put_noidle(&pdev->dev);
1850 pm_runtime_set_suspended(&pdev->dev);
1852 dwc3_free_event_buffers(dwc);
1853 dwc3_free_scratch_buffers(dwc);
1856 power_supply_put(dwc->usb_psy);
1862 static int dwc3_core_init_for_resume(struct dwc3 *dwc)
1866 ret = reset_control_deassert(dwc->reset);
1870 ret = dwc3_clk_enable(dwc);
1874 ret = dwc3_core_init(dwc);
1881 dwc3_clk_disable(dwc);
1883 reset_control_assert(dwc->reset);
1888 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
1890 unsigned long flags;
1893 switch (dwc->current_dr_role) {
1894 case DWC3_GCTL_PRTCAP_DEVICE:
1895 if (pm_runtime_suspended(dwc->dev))
1897 spin_lock_irqsave(&dwc->lock, flags);
1898 dwc3_gadget_suspend(dwc);
1899 spin_unlock_irqrestore(&dwc->lock, flags);
1900 synchronize_irq(dwc->irq_gadget);
1901 dwc3_core_exit(dwc);
1903 case DWC3_GCTL_PRTCAP_HOST:
1904 if (!PMSG_IS_AUTO(msg)) {
1905 dwc3_core_exit(dwc);
1909 /* Let controller to suspend HSPHY before PHY driver suspends */
1910 if (dwc->dis_u2_susphy_quirk ||
1911 dwc->dis_enblslpm_quirk) {
1912 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1913 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
1914 DWC3_GUSB2PHYCFG_SUSPHY;
1915 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1917 /* Give some time for USB2 PHY to suspend */
1918 usleep_range(5000, 6000);
1921 phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
1922 phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
1924 case DWC3_GCTL_PRTCAP_OTG:
1925 /* do nothing during runtime_suspend */
1926 if (PMSG_IS_AUTO(msg))
1929 if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1930 spin_lock_irqsave(&dwc->lock, flags);
1931 dwc3_gadget_suspend(dwc);
1932 spin_unlock_irqrestore(&dwc->lock, flags);
1933 synchronize_irq(dwc->irq_gadget);
1937 dwc3_core_exit(dwc);
1947 static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
1949 unsigned long flags;
1953 switch (dwc->current_dr_role) {
1954 case DWC3_GCTL_PRTCAP_DEVICE:
1955 ret = dwc3_core_init_for_resume(dwc);
1959 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1960 spin_lock_irqsave(&dwc->lock, flags);
1961 dwc3_gadget_resume(dwc);
1962 spin_unlock_irqrestore(&dwc->lock, flags);
1964 case DWC3_GCTL_PRTCAP_HOST:
1965 if (!PMSG_IS_AUTO(msg)) {
1966 ret = dwc3_core_init_for_resume(dwc);
1969 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1972 /* Restore GUSB2PHYCFG bits that were modified in suspend */
1973 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1974 if (dwc->dis_u2_susphy_quirk)
1975 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1977 if (dwc->dis_enblslpm_quirk)
1978 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
1980 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1982 phy_pm_runtime_get_sync(dwc->usb2_generic_phy);
1983 phy_pm_runtime_get_sync(dwc->usb3_generic_phy);
1985 case DWC3_GCTL_PRTCAP_OTG:
1986 /* nothing to do on runtime_resume */
1987 if (PMSG_IS_AUTO(msg))
1990 ret = dwc3_core_init_for_resume(dwc);
1994 dwc3_set_prtcap(dwc, dwc->current_dr_role);
1997 if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
1998 dwc3_otg_host_init(dwc);
1999 } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
2000 spin_lock_irqsave(&dwc->lock, flags);
2001 dwc3_gadget_resume(dwc);
2002 spin_unlock_irqrestore(&dwc->lock, flags);
2014 static int dwc3_runtime_checks(struct dwc3 *dwc)
2016 switch (dwc->current_dr_role) {
2017 case DWC3_GCTL_PRTCAP_DEVICE:
2021 case DWC3_GCTL_PRTCAP_HOST:
2030 static int dwc3_runtime_suspend(struct device *dev)
2032 struct dwc3 *dwc = dev_get_drvdata(dev);
2035 if (dwc3_runtime_checks(dwc))
2038 ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
2042 device_init_wakeup(dev, true);
2047 static int dwc3_runtime_resume(struct device *dev)
2049 struct dwc3 *dwc = dev_get_drvdata(dev);
2052 device_init_wakeup(dev, false);
2054 ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
2058 switch (dwc->current_dr_role) {
2059 case DWC3_GCTL_PRTCAP_DEVICE:
2060 dwc3_gadget_process_pending_events(dwc);
2062 case DWC3_GCTL_PRTCAP_HOST:
2068 pm_runtime_mark_last_busy(dev);
2073 static int dwc3_runtime_idle(struct device *dev)
2075 struct dwc3 *dwc = dev_get_drvdata(dev);
2077 switch (dwc->current_dr_role) {
2078 case DWC3_GCTL_PRTCAP_DEVICE:
2079 if (dwc3_runtime_checks(dwc))
2082 case DWC3_GCTL_PRTCAP_HOST:
2088 pm_runtime_mark_last_busy(dev);
2089 pm_runtime_autosuspend(dev);
2093 #endif /* CONFIG_PM */
2095 #ifdef CONFIG_PM_SLEEP
2096 static int dwc3_suspend(struct device *dev)
2098 struct dwc3 *dwc = dev_get_drvdata(dev);
2101 ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
2105 pinctrl_pm_select_sleep_state(dev);
2110 static int dwc3_resume(struct device *dev)
2112 struct dwc3 *dwc = dev_get_drvdata(dev);
2115 pinctrl_pm_select_default_state(dev);
2117 ret = dwc3_resume_common(dwc, PMSG_RESUME);
2121 pm_runtime_disable(dev);
2122 pm_runtime_set_active(dev);
2123 pm_runtime_enable(dev);
2128 static void dwc3_complete(struct device *dev)
2130 struct dwc3 *dwc = dev_get_drvdata(dev);
2133 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST &&
2134 dwc->dis_split_quirk) {
2135 reg = dwc3_readl(dwc->regs, DWC3_GUCTL3);
2136 reg |= DWC3_GUCTL3_SPLITDISABLE;
2137 dwc3_writel(dwc->regs, DWC3_GUCTL3, reg);
2141 #define dwc3_complete NULL
2142 #endif /* CONFIG_PM_SLEEP */
2144 static const struct dev_pm_ops dwc3_dev_pm_ops = {
2145 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
2146 .complete = dwc3_complete,
2147 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
2152 static const struct of_device_id of_dwc3_match[] = {
2154 .compatible = "snps,dwc3"
2157 .compatible = "synopsys,dwc3"
2161 MODULE_DEVICE_TABLE(of, of_dwc3_match);
2166 #define ACPI_ID_INTEL_BSW "808622B7"
2168 static const struct acpi_device_id dwc3_acpi_match[] = {
2169 { ACPI_ID_INTEL_BSW, 0 },
2172 MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
2175 static struct platform_driver dwc3_driver = {
2176 .probe = dwc3_probe,
2177 .remove = dwc3_remove,
2180 .of_match_table = of_match_ptr(of_dwc3_match),
2181 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
2182 .pm = &dwc3_dev_pm_ops,
2186 module_platform_driver(dwc3_driver);
2188 MODULE_ALIAS("platform:dwc3");
2189 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
2190 MODULE_LICENSE("GPL v2");
2191 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");