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 int dwc3_core_soft_reset(struct dwc3 *dwc);
120 static void __dwc3_set_mode(struct work_struct *work)
122 struct dwc3 *dwc = work_to_dwc(work);
127 mutex_lock(&dwc->mutex);
129 pm_runtime_get_sync(dwc->dev);
131 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
132 dwc3_otg_update(dwc, 0);
134 if (!dwc->desired_dr_role)
137 if (dwc->desired_dr_role == dwc->current_dr_role)
140 if (dwc->desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev)
143 switch (dwc->current_dr_role) {
144 case DWC3_GCTL_PRTCAP_HOST:
147 case DWC3_GCTL_PRTCAP_DEVICE:
148 dwc3_gadget_exit(dwc);
149 dwc3_event_buffers_cleanup(dwc);
151 case DWC3_GCTL_PRTCAP_OTG:
153 spin_lock_irqsave(&dwc->lock, flags);
154 dwc->desired_otg_role = DWC3_OTG_ROLE_IDLE;
155 spin_unlock_irqrestore(&dwc->lock, flags);
156 dwc3_otg_update(dwc, 1);
162 /* For DRD host or device mode only */
163 if (dwc->desired_dr_role != DWC3_GCTL_PRTCAP_OTG) {
164 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
165 reg |= DWC3_GCTL_CORESOFTRESET;
166 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
169 * Wait for internal clocks to synchronized. DWC_usb31 and
170 * DWC_usb32 may need at least 50ms (less for DWC_usb3). To
171 * keep it consistent across different IPs, let's wait up to
172 * 100ms before clearing GCTL.CORESOFTRESET.
176 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
177 reg &= ~DWC3_GCTL_CORESOFTRESET;
178 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
181 spin_lock_irqsave(&dwc->lock, flags);
183 dwc3_set_prtcap(dwc, dwc->desired_dr_role);
185 spin_unlock_irqrestore(&dwc->lock, flags);
187 switch (dwc->desired_dr_role) {
188 case DWC3_GCTL_PRTCAP_HOST:
189 ret = dwc3_host_init(dwc);
191 dev_err(dwc->dev, "failed to initialize host\n");
194 otg_set_vbus(dwc->usb2_phy->otg, true);
195 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
196 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
197 if (dwc->dis_split_quirk) {
198 reg = dwc3_readl(dwc->regs, DWC3_GUCTL3);
199 reg |= DWC3_GUCTL3_SPLITDISABLE;
200 dwc3_writel(dwc->regs, DWC3_GUCTL3, reg);
204 case DWC3_GCTL_PRTCAP_DEVICE:
205 dwc3_core_soft_reset(dwc);
207 dwc3_event_buffers_setup(dwc);
210 otg_set_vbus(dwc->usb2_phy->otg, false);
211 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
212 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
214 ret = dwc3_gadget_init(dwc);
216 dev_err(dwc->dev, "failed to initialize peripheral\n");
218 case DWC3_GCTL_PRTCAP_OTG:
220 dwc3_otg_update(dwc, 0);
227 pm_runtime_mark_last_busy(dwc->dev);
228 pm_runtime_put_autosuspend(dwc->dev);
229 mutex_unlock(&dwc->mutex);
232 void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
236 if (dwc->dr_mode != USB_DR_MODE_OTG)
239 spin_lock_irqsave(&dwc->lock, flags);
240 dwc->desired_dr_role = mode;
241 spin_unlock_irqrestore(&dwc->lock, flags);
243 queue_work(system_freezable_wq, &dwc->drd_work);
246 u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
248 struct dwc3 *dwc = dep->dwc;
251 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
252 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
253 DWC3_GDBGFIFOSPACE_TYPE(type));
255 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
257 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
261 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
262 * @dwc: pointer to our context structure
264 static int dwc3_core_soft_reset(struct dwc3 *dwc)
270 * We're resetting only the device side because, if we're in host mode,
271 * XHCI driver will reset the host block. If dwc3 was configured for
272 * host-only mode, then we can return early.
274 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST)
277 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
278 reg |= DWC3_DCTL_CSFTRST;
279 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
282 * For DWC_usb31 controller 1.90a and later, the DCTL.CSFRST bit
283 * is cleared only after all the clocks are synchronized. This can
284 * take a little more than 50ms. Set the polling rate at 20ms
285 * for 10 times instead.
287 if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
291 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
292 if (!(reg & DWC3_DCTL_CSFTRST))
295 if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
305 * For DWC_usb31 controller 1.80a and prior, once DCTL.CSFRST bit
306 * is cleared, we must wait at least 50ms before accessing the PHY
307 * domain (synchronization delay).
309 if (DWC3_VER_IS_WITHIN(DWC31, ANY, 180A))
316 * dwc3_frame_length_adjustment - Adjusts frame length if required
317 * @dwc3: Pointer to our controller context structure
319 static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
324 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
330 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
331 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
332 if (dft != dwc->fladj) {
333 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
334 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
335 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
340 * dwc3_ref_clk_period - Reference clock period configuration
341 * Default reference clock period depends on hardware
342 * configuration. For systems with reference clock that differs
343 * from the default, this will set clock period in DWC3_GUCTL
345 * @dwc: Pointer to our controller context structure
346 * @ref_clk_per: reference clock period in ns
348 static void dwc3_ref_clk_period(struct dwc3 *dwc)
352 if (dwc->ref_clk_per == 0)
355 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
356 reg &= ~DWC3_GUCTL_REFCLKPER_MASK;
357 reg |= FIELD_PREP(DWC3_GUCTL_REFCLKPER_MASK, dwc->ref_clk_per);
358 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
363 * dwc3_free_one_event_buffer - Frees one event buffer
364 * @dwc: Pointer to our controller context structure
365 * @evt: Pointer to event buffer to be freed
367 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
368 struct dwc3_event_buffer *evt)
370 dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
374 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
375 * @dwc: Pointer to our controller context structure
376 * @length: size of the event buffer
378 * Returns a pointer to the allocated event buffer structure on success
379 * otherwise ERR_PTR(errno).
381 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
384 struct dwc3_event_buffer *evt;
386 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
388 return ERR_PTR(-ENOMEM);
391 evt->length = length;
392 evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
394 return ERR_PTR(-ENOMEM);
396 evt->buf = dma_alloc_coherent(dwc->sysdev, length,
397 &evt->dma, GFP_KERNEL);
399 return ERR_PTR(-ENOMEM);
405 * dwc3_free_event_buffers - frees all allocated event buffers
406 * @dwc: Pointer to our controller context structure
408 static void dwc3_free_event_buffers(struct dwc3 *dwc)
410 struct dwc3_event_buffer *evt;
414 dwc3_free_one_event_buffer(dwc, evt);
418 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
419 * @dwc: pointer to our controller context structure
420 * @length: size of event buffer
422 * Returns 0 on success otherwise negative errno. In the error case, dwc
423 * may contain some buffers allocated but not all which were requested.
425 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
427 struct dwc3_event_buffer *evt;
429 evt = dwc3_alloc_one_event_buffer(dwc, length);
431 dev_err(dwc->dev, "can't allocate event buffer\n");
440 * dwc3_event_buffers_setup - setup our allocated event buffers
441 * @dwc: pointer to our controller context structure
443 * Returns 0 on success otherwise negative errno.
445 int dwc3_event_buffers_setup(struct dwc3 *dwc)
447 struct dwc3_event_buffer *evt;
451 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
452 lower_32_bits(evt->dma));
453 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
454 upper_32_bits(evt->dma));
455 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
456 DWC3_GEVNTSIZ_SIZE(evt->length));
457 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
462 void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
464 struct dwc3_event_buffer *evt;
470 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
471 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
472 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
473 | DWC3_GEVNTSIZ_SIZE(0));
474 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
477 static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
479 if (!dwc->has_hibernation)
482 if (!dwc->nr_scratch)
485 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
486 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
487 if (!dwc->scratchbuf)
493 static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
495 dma_addr_t scratch_addr;
499 if (!dwc->has_hibernation)
502 if (!dwc->nr_scratch)
505 /* should never fall here */
506 if (!WARN_ON(dwc->scratchbuf))
509 scratch_addr = dma_map_single(dwc->sysdev, dwc->scratchbuf,
510 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
512 if (dma_mapping_error(dwc->sysdev, scratch_addr)) {
513 dev_err(dwc->sysdev, "failed to map scratch buffer\n");
518 dwc->scratch_addr = scratch_addr;
520 param = lower_32_bits(scratch_addr);
522 ret = dwc3_send_gadget_generic_command(dwc,
523 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
527 param = upper_32_bits(scratch_addr);
529 ret = dwc3_send_gadget_generic_command(dwc,
530 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
537 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
538 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
544 static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
546 if (!dwc->has_hibernation)
549 if (!dwc->nr_scratch)
552 /* should never fall here */
553 if (!WARN_ON(dwc->scratchbuf))
556 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
557 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
558 kfree(dwc->scratchbuf);
561 static void dwc3_core_num_eps(struct dwc3 *dwc)
563 struct dwc3_hwparams *parms = &dwc->hwparams;
565 dwc->num_eps = DWC3_NUM_EPS(parms);
568 static void dwc3_cache_hwparams(struct dwc3 *dwc)
570 struct dwc3_hwparams *parms = &dwc->hwparams;
572 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
573 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
574 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
575 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
576 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
577 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
578 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
579 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
580 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
582 if (DWC3_IP_IS(DWC32))
583 parms->hwparams9 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS9);
586 static int dwc3_core_ulpi_init(struct dwc3 *dwc)
591 intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
593 if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
594 (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
595 dwc->hsphy_interface &&
596 !strncmp(dwc->hsphy_interface, "ulpi", 4)))
597 ret = dwc3_ulpi_init(dwc);
603 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
604 * @dwc: Pointer to our controller context structure
606 * Returns 0 on success. The USB PHY interfaces are configured but not
607 * initialized. The PHY interfaces and the PHYs get initialized together with
608 * the core in dwc3_core_init.
610 static int dwc3_phy_setup(struct dwc3 *dwc)
612 unsigned int hw_mode;
615 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
617 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
620 * Make sure UX_EXIT_PX is cleared as that causes issues with some
621 * PHYs. Also, this bit is not supposed to be used in normal operation.
623 reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
626 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
627 * to '0' during coreConsultant configuration. So default value
628 * will be '0' when the core is reset. Application needs to set it
629 * to '1' after the core initialization is completed.
631 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
632 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
635 * For DRD controllers, GUSB3PIPECTL.SUSPENDENABLE must be cleared after
636 * power-on reset, and it can be set after core initialization, which is
637 * after device soft-reset during initialization.
639 if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD)
640 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
642 if (dwc->u2ss_inp3_quirk)
643 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
645 if (dwc->dis_rxdet_inp3_quirk)
646 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
648 if (dwc->req_p1p2p3_quirk)
649 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
651 if (dwc->del_p1p2p3_quirk)
652 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
654 if (dwc->del_phy_power_chg_quirk)
655 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
657 if (dwc->lfps_filter_quirk)
658 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
660 if (dwc->rx_detect_poll_quirk)
661 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
663 if (dwc->tx_de_emphasis_quirk)
664 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
666 if (dwc->dis_u3_susphy_quirk)
667 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
669 if (dwc->dis_del_phy_power_chg_quirk)
670 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
672 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
674 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
676 /* Select the HS PHY interface */
677 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
678 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
679 if (dwc->hsphy_interface &&
680 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
681 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
683 } else if (dwc->hsphy_interface &&
684 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
685 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
686 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
688 /* Relying on default value. */
689 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
693 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
698 switch (dwc->hsphy_mode) {
699 case USBPHY_INTERFACE_MODE_UTMI:
700 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
701 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
702 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
703 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
705 case USBPHY_INTERFACE_MODE_UTMIW:
706 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
707 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
708 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
709 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
716 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
717 * '0' during coreConsultant configuration. So default value will
718 * be '0' when the core is reset. Application needs to set it to
719 * '1' after the core initialization is completed.
721 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
722 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
725 * For DRD controllers, GUSB2PHYCFG.SUSPHY must be cleared after
726 * power-on reset, and it can be set after core initialization, which is
727 * after device soft-reset during initialization.
729 if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD)
730 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
732 if (dwc->dis_u2_susphy_quirk)
733 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
735 if (dwc->dis_enblslpm_quirk)
736 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
738 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM;
740 if (dwc->dis_u2_freeclk_exists_quirk)
741 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
743 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
748 static void dwc3_core_exit(struct dwc3 *dwc)
750 dwc3_event_buffers_cleanup(dwc);
752 usb_phy_shutdown(dwc->usb2_phy);
753 usb_phy_shutdown(dwc->usb3_phy);
754 phy_exit(dwc->usb2_generic_phy);
755 phy_exit(dwc->usb3_generic_phy);
757 usb_phy_set_suspend(dwc->usb2_phy, 1);
758 usb_phy_set_suspend(dwc->usb3_phy, 1);
759 phy_power_off(dwc->usb2_generic_phy);
760 phy_power_off(dwc->usb3_generic_phy);
761 clk_bulk_disable_unprepare(dwc->num_clks, dwc->clks);
762 reset_control_assert(dwc->reset);
765 static bool dwc3_core_is_valid(struct dwc3 *dwc)
769 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
770 dwc->ip = DWC3_GSNPS_ID(reg);
772 /* This should read as U3 followed by revision number */
773 if (DWC3_IP_IS(DWC3)) {
775 } else if (DWC3_IP_IS(DWC31) || DWC3_IP_IS(DWC32)) {
776 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
777 dwc->version_type = dwc3_readl(dwc->regs, DWC3_VER_TYPE);
785 static void dwc3_core_setup_global_control(struct dwc3 *dwc)
787 u32 hwparams4 = dwc->hwparams.hwparams4;
790 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
791 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
793 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
794 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
796 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
797 * issue which would cause xHCI compliance tests to fail.
799 * Because of that we cannot enable clock gating on such
804 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
807 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
808 dwc->dr_mode == USB_DR_MODE_OTG) &&
809 DWC3_VER_IS_WITHIN(DWC3, 210A, 250A))
810 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
812 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
814 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
815 /* enable hibernation here */
816 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
819 * REVISIT Enabling this bit so that host-mode hibernation
820 * will work. Device-mode hibernation is not yet implemented.
822 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
829 /* check if current dwc3 is on simulation board */
830 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
831 dev_info(dwc->dev, "Running with FPGA optimizations\n");
835 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
836 "disable_scramble cannot be used on non-FPGA builds\n");
838 if (dwc->disable_scramble_quirk && dwc->is_fpga)
839 reg |= DWC3_GCTL_DISSCRAMBLE;
841 reg &= ~DWC3_GCTL_DISSCRAMBLE;
843 if (dwc->u2exit_lfps_quirk)
844 reg |= DWC3_GCTL_U2EXIT_LFPS;
847 * WORKAROUND: DWC3 revisions <1.90a have a bug
848 * where the device can fail to connect at SuperSpeed
849 * and falls back to high-speed mode which causes
850 * the device to enter a Connect/Disconnect loop
852 if (DWC3_VER_IS_PRIOR(DWC3, 190A))
853 reg |= DWC3_GCTL_U2RSTECN;
855 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
858 static int dwc3_core_get_phy(struct dwc3 *dwc);
859 static int dwc3_core_ulpi_init(struct dwc3 *dwc);
861 /* set global incr burst type configuration registers */
862 static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
864 struct device *dev = dwc->dev;
865 /* incrx_mode : for INCR burst type. */
867 /* incrx_size : for size of INCRX burst. */
875 cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
878 * Handle property "snps,incr-burst-type-adjustment".
879 * Get the number of value from this property:
880 * result <= 0, means this property is not supported.
881 * result = 1, means INCRx burst mode supported.
882 * result > 1, means undefined length burst mode supported.
884 ntype = device_property_count_u32(dev, "snps,incr-burst-type-adjustment");
888 vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL);
890 dev_err(dev, "Error to get memory\n");
894 /* Get INCR burst type, and parse it */
895 ret = device_property_read_u32_array(dev,
896 "snps,incr-burst-type-adjustment", vals, ntype);
899 dev_err(dev, "Error to get property\n");
906 /* INCRX (undefined length) burst mode */
907 incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
908 for (i = 1; i < ntype; i++) {
909 if (vals[i] > incrx_size)
910 incrx_size = vals[i];
913 /* INCRX burst mode */
914 incrx_mode = INCRX_BURST_MODE;
919 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
920 cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
922 cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
923 switch (incrx_size) {
925 cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
928 cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
931 cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
934 cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
937 cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
940 cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
943 cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
948 dev_err(dev, "Invalid property\n");
952 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
956 * dwc3_core_init - Low-level initialization of DWC3 Core
957 * @dwc: Pointer to our controller context structure
959 * Returns 0 on success otherwise negative errno.
961 static int dwc3_core_init(struct dwc3 *dwc)
963 unsigned int hw_mode;
967 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
970 * Write Linux Version Code to our GUID register so it's easy to figure
971 * out which kernel version a bug was found.
973 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
975 ret = dwc3_phy_setup(dwc);
979 if (!dwc->ulpi_ready) {
980 ret = dwc3_core_ulpi_init(dwc);
983 dwc->ulpi_ready = true;
986 if (!dwc->phys_ready) {
987 ret = dwc3_core_get_phy(dwc);
990 dwc->phys_ready = true;
993 usb_phy_init(dwc->usb2_phy);
994 usb_phy_init(dwc->usb3_phy);
995 ret = phy_init(dwc->usb2_generic_phy);
999 ret = phy_init(dwc->usb3_generic_phy);
1001 phy_exit(dwc->usb2_generic_phy);
1005 ret = dwc3_core_soft_reset(dwc);
1009 if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD &&
1010 !DWC3_VER_IS_WITHIN(DWC3, ANY, 194A)) {
1011 if (!dwc->dis_u3_susphy_quirk) {
1012 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1013 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1014 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1017 if (!dwc->dis_u2_susphy_quirk) {
1018 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1019 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1020 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1024 dwc3_core_setup_global_control(dwc);
1025 dwc3_core_num_eps(dwc);
1027 ret = dwc3_setup_scratch_buffers(dwc);
1031 /* Adjust Frame Length */
1032 dwc3_frame_length_adjustment(dwc);
1034 /* Adjust Reference Clock Period */
1035 dwc3_ref_clk_period(dwc);
1037 dwc3_set_incr_burst_type(dwc);
1039 usb_phy_set_suspend(dwc->usb2_phy, 0);
1040 usb_phy_set_suspend(dwc->usb3_phy, 0);
1041 ret = phy_power_on(dwc->usb2_generic_phy);
1045 ret = phy_power_on(dwc->usb3_generic_phy);
1049 ret = dwc3_event_buffers_setup(dwc);
1051 dev_err(dwc->dev, "failed to setup event buffers\n");
1056 * ENDXFER polling is available on version 3.10a and later of
1057 * the DWC_usb3 controller. It is NOT available in the
1058 * DWC_usb31 controller.
1060 if (DWC3_VER_IS_WITHIN(DWC3, 310A, ANY)) {
1061 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
1062 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
1063 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
1066 if (!DWC3_VER_IS_PRIOR(DWC3, 250A)) {
1067 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1070 * Enable hardware control of sending remote wakeup
1071 * in HS when the device is in the L1 state.
1073 if (!DWC3_VER_IS_PRIOR(DWC3, 290A))
1074 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
1077 * Decouple USB 2.0 L1 & L2 events which will allow for
1078 * gadget driver to only receive U3/L2 suspend & wakeup
1079 * events and prevent the more frequent L1 LPM transitions
1080 * from interrupting the driver.
1082 if (!DWC3_VER_IS_PRIOR(DWC3, 300A))
1083 reg |= DWC3_GUCTL1_DEV_DECOUPLE_L1L2_EVT;
1085 if (dwc->dis_tx_ipgap_linecheck_quirk)
1086 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
1088 if (dwc->parkmode_disable_ss_quirk)
1089 reg |= DWC3_GUCTL1_PARKMODE_DISABLE_SS;
1091 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1094 if (dwc->dr_mode == USB_DR_MODE_HOST ||
1095 dwc->dr_mode == USB_DR_MODE_OTG) {
1096 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
1099 * Enable Auto retry Feature to make the controller operating in
1100 * Host mode on seeing transaction errors(CRC errors or internal
1101 * overrun scenerios) on IN transfers to reply to the device
1102 * with a non-terminating retry ACK (i.e, an ACK transcation
1103 * packet with Retry=1 & Nump != 0)
1105 reg |= DWC3_GUCTL_HSTINAUTORETRY;
1107 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
1111 * Must config both number of packets and max burst settings to enable
1112 * RX and/or TX threshold.
1114 if (!DWC3_IP_IS(DWC3) && dwc->dr_mode == USB_DR_MODE_HOST) {
1115 u8 rx_thr_num = dwc->rx_thr_num_pkt_prd;
1116 u8 rx_maxburst = dwc->rx_max_burst_prd;
1117 u8 tx_thr_num = dwc->tx_thr_num_pkt_prd;
1118 u8 tx_maxburst = dwc->tx_max_burst_prd;
1120 if (rx_thr_num && rx_maxburst) {
1121 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1122 reg |= DWC31_RXTHRNUMPKTSEL_PRD;
1124 reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
1125 reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
1127 reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
1128 reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
1130 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1133 if (tx_thr_num && tx_maxburst) {
1134 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1135 reg |= DWC31_TXTHRNUMPKTSEL_PRD;
1137 reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
1138 reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
1140 reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
1141 reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
1143 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1150 phy_power_off(dwc->usb3_generic_phy);
1153 phy_power_off(dwc->usb2_generic_phy);
1156 usb_phy_set_suspend(dwc->usb2_phy, 1);
1157 usb_phy_set_suspend(dwc->usb3_phy, 1);
1160 usb_phy_shutdown(dwc->usb2_phy);
1161 usb_phy_shutdown(dwc->usb3_phy);
1162 phy_exit(dwc->usb2_generic_phy);
1163 phy_exit(dwc->usb3_generic_phy);
1166 dwc3_ulpi_exit(dwc);
1172 static int dwc3_core_get_phy(struct dwc3 *dwc)
1174 struct device *dev = dwc->dev;
1175 struct device_node *node = dev->of_node;
1179 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
1180 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
1182 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1183 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
1186 if (IS_ERR(dwc->usb2_phy)) {
1187 ret = PTR_ERR(dwc->usb2_phy);
1188 if (ret == -ENXIO || ret == -ENODEV) {
1189 dwc->usb2_phy = NULL;
1191 return dev_err_probe(dev, ret, "no usb2 phy configured\n");
1195 if (IS_ERR(dwc->usb3_phy)) {
1196 ret = PTR_ERR(dwc->usb3_phy);
1197 if (ret == -ENXIO || ret == -ENODEV) {
1198 dwc->usb3_phy = NULL;
1200 return dev_err_probe(dev, ret, "no usb3 phy configured\n");
1204 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
1205 if (IS_ERR(dwc->usb2_generic_phy)) {
1206 ret = PTR_ERR(dwc->usb2_generic_phy);
1207 if (ret == -ENOSYS || ret == -ENODEV) {
1208 dwc->usb2_generic_phy = NULL;
1210 return dev_err_probe(dev, ret, "no usb2 phy configured\n");
1214 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
1215 if (IS_ERR(dwc->usb3_generic_phy)) {
1216 ret = PTR_ERR(dwc->usb3_generic_phy);
1217 if (ret == -ENOSYS || ret == -ENODEV) {
1218 dwc->usb3_generic_phy = NULL;
1220 return dev_err_probe(dev, ret, "no usb3 phy configured\n");
1227 static int dwc3_core_init_mode(struct dwc3 *dwc)
1229 struct device *dev = dwc->dev;
1232 switch (dwc->dr_mode) {
1233 case USB_DR_MODE_PERIPHERAL:
1234 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1237 otg_set_vbus(dwc->usb2_phy->otg, false);
1238 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
1239 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
1241 ret = dwc3_gadget_init(dwc);
1243 return dev_err_probe(dev, ret, "failed to initialize gadget\n");
1245 case USB_DR_MODE_HOST:
1246 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1249 otg_set_vbus(dwc->usb2_phy->otg, true);
1250 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
1251 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
1253 ret = dwc3_host_init(dwc);
1255 return dev_err_probe(dev, ret, "failed to initialize host\n");
1257 case USB_DR_MODE_OTG:
1258 INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
1259 ret = dwc3_drd_init(dwc);
1261 return dev_err_probe(dev, ret, "failed to initialize dual-role\n");
1264 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1271 static void dwc3_core_exit_mode(struct dwc3 *dwc)
1273 switch (dwc->dr_mode) {
1274 case USB_DR_MODE_PERIPHERAL:
1275 dwc3_gadget_exit(dwc);
1277 case USB_DR_MODE_HOST:
1278 dwc3_host_exit(dwc);
1280 case USB_DR_MODE_OTG:
1288 /* de-assert DRVVBUS for HOST and OTG mode */
1289 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1292 static void dwc3_get_properties(struct dwc3 *dwc)
1294 struct device *dev = dwc->dev;
1295 u8 lpm_nyet_threshold;
1298 u8 rx_thr_num_pkt_prd;
1299 u8 rx_max_burst_prd;
1300 u8 tx_thr_num_pkt_prd;
1301 u8 tx_max_burst_prd;
1302 u8 tx_fifo_resize_max_num;
1303 const char *usb_psy_name;
1306 /* default to highest possible threshold */
1307 lpm_nyet_threshold = 0xf;
1309 /* default to -3.5dB de-emphasis */
1313 * default to assert utmi_sleep_n and use maximum allowed HIRD
1314 * threshold value of 0b1100
1316 hird_threshold = 12;
1319 * default to a TXFIFO size large enough to fit 6 max packets. This
1320 * allows for systems with larger bus latencies to have some headroom
1321 * for endpoints that have a large bMaxBurst value.
1323 tx_fifo_resize_max_num = 6;
1325 dwc->maximum_speed = usb_get_maximum_speed(dev);
1326 dwc->max_ssp_rate = usb_get_maximum_ssp_rate(dev);
1327 dwc->dr_mode = usb_get_dr_mode(dev);
1328 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
1330 dwc->sysdev_is_parent = device_property_read_bool(dev,
1331 "linux,sysdev_is_parent");
1332 if (dwc->sysdev_is_parent)
1333 dwc->sysdev = dwc->dev->parent;
1335 dwc->sysdev = dwc->dev;
1337 ret = device_property_read_string(dev, "usb-psy-name", &usb_psy_name);
1339 dwc->usb_psy = power_supply_get_by_name(usb_psy_name);
1341 dev_err(dev, "couldn't get usb power supply\n");
1344 dwc->has_lpm_erratum = device_property_read_bool(dev,
1345 "snps,has-lpm-erratum");
1346 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
1347 &lpm_nyet_threshold);
1348 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
1349 "snps,is-utmi-l1-suspend");
1350 device_property_read_u8(dev, "snps,hird-threshold",
1352 dwc->dis_start_transfer_quirk = device_property_read_bool(dev,
1353 "snps,dis-start-transfer-quirk");
1354 dwc->usb3_lpm_capable = device_property_read_bool(dev,
1355 "snps,usb3_lpm_capable");
1356 dwc->usb2_lpm_disable = device_property_read_bool(dev,
1357 "snps,usb2-lpm-disable");
1358 dwc->usb2_gadget_lpm_disable = device_property_read_bool(dev,
1359 "snps,usb2-gadget-lpm-disable");
1360 device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
1361 &rx_thr_num_pkt_prd);
1362 device_property_read_u8(dev, "snps,rx-max-burst-prd",
1364 device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
1365 &tx_thr_num_pkt_prd);
1366 device_property_read_u8(dev, "snps,tx-max-burst-prd",
1368 dwc->do_fifo_resize = device_property_read_bool(dev,
1370 if (dwc->do_fifo_resize)
1371 device_property_read_u8(dev, "tx-fifo-max-num",
1372 &tx_fifo_resize_max_num);
1374 dwc->disable_scramble_quirk = device_property_read_bool(dev,
1375 "snps,disable_scramble_quirk");
1376 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
1377 "snps,u2exit_lfps_quirk");
1378 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
1379 "snps,u2ss_inp3_quirk");
1380 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
1381 "snps,req_p1p2p3_quirk");
1382 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
1383 "snps,del_p1p2p3_quirk");
1384 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
1385 "snps,del_phy_power_chg_quirk");
1386 dwc->lfps_filter_quirk = device_property_read_bool(dev,
1387 "snps,lfps_filter_quirk");
1388 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
1389 "snps,rx_detect_poll_quirk");
1390 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
1391 "snps,dis_u3_susphy_quirk");
1392 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
1393 "snps,dis_u2_susphy_quirk");
1394 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1395 "snps,dis_enblslpm_quirk");
1396 dwc->dis_u1_entry_quirk = device_property_read_bool(dev,
1397 "snps,dis-u1-entry-quirk");
1398 dwc->dis_u2_entry_quirk = device_property_read_bool(dev,
1399 "snps,dis-u2-entry-quirk");
1400 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1401 "snps,dis_rxdet_inp3_quirk");
1402 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1403 "snps,dis-u2-freeclk-exists-quirk");
1404 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1405 "snps,dis-del-phy-power-chg-quirk");
1406 dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1407 "snps,dis-tx-ipgap-linecheck-quirk");
1408 dwc->parkmode_disable_ss_quirk = device_property_read_bool(dev,
1409 "snps,parkmode-disable-ss-quirk");
1411 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
1412 "snps,tx_de_emphasis_quirk");
1413 device_property_read_u8(dev, "snps,tx_de_emphasis",
1415 device_property_read_string(dev, "snps,hsphy_interface",
1416 &dwc->hsphy_interface);
1417 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
1419 device_property_read_u32(dev, "snps,ref-clock-period-ns",
1422 dwc->dis_metastability_quirk = device_property_read_bool(dev,
1423 "snps,dis_metastability_quirk");
1425 dwc->dis_split_quirk = device_property_read_bool(dev,
1426 "snps,dis-split-quirk");
1428 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1429 dwc->tx_de_emphasis = tx_de_emphasis;
1431 dwc->hird_threshold = hird_threshold;
1433 dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd;
1434 dwc->rx_max_burst_prd = rx_max_burst_prd;
1436 dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd;
1437 dwc->tx_max_burst_prd = tx_max_burst_prd;
1439 dwc->imod_interval = 0;
1441 dwc->tx_fifo_resize_max_num = tx_fifo_resize_max_num;
1444 /* check whether the core supports IMOD */
1445 bool dwc3_has_imod(struct dwc3 *dwc)
1447 return DWC3_VER_IS_WITHIN(DWC3, 300A, ANY) ||
1448 DWC3_VER_IS_WITHIN(DWC31, 120A, ANY) ||
1452 static void dwc3_check_params(struct dwc3 *dwc)
1454 struct device *dev = dwc->dev;
1455 unsigned int hwparam_gen =
1456 DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3);
1458 /* Check for proper value of imod_interval */
1459 if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
1460 dev_warn(dwc->dev, "Interrupt moderation not supported\n");
1461 dwc->imod_interval = 0;
1465 * Workaround for STAR 9000961433 which affects only version
1466 * 3.00a of the DWC_usb3 core. This prevents the controller
1467 * interrupt from being masked while handling events. IMOD
1468 * allows us to work around this issue. Enable it for the
1471 if (!dwc->imod_interval &&
1472 DWC3_VER_IS(DWC3, 300A))
1473 dwc->imod_interval = 1;
1475 /* Check the maximum_speed parameter */
1476 switch (dwc->maximum_speed) {
1477 case USB_SPEED_FULL:
1478 case USB_SPEED_HIGH:
1480 case USB_SPEED_SUPER:
1481 if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS)
1482 dev_warn(dev, "UDC doesn't support Gen 1\n");
1484 case USB_SPEED_SUPER_PLUS:
1485 if ((DWC3_IP_IS(DWC32) &&
1486 hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS) ||
1487 (!DWC3_IP_IS(DWC32) &&
1488 hwparam_gen != DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1489 dev_warn(dev, "UDC doesn't support SSP\n");
1492 dev_err(dev, "invalid maximum_speed parameter %d\n",
1493 dwc->maximum_speed);
1495 case USB_SPEED_UNKNOWN:
1496 switch (hwparam_gen) {
1497 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
1498 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1500 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
1501 if (DWC3_IP_IS(DWC32))
1502 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1504 dwc->maximum_speed = USB_SPEED_SUPER;
1506 case DWC3_GHWPARAMS3_SSPHY_IFC_DIS:
1507 dwc->maximum_speed = USB_SPEED_HIGH;
1510 dwc->maximum_speed = USB_SPEED_SUPER;
1517 * Currently the controller does not have visibility into the HW
1518 * parameter to determine the maximum number of lanes the HW supports.
1519 * If the number of lanes is not specified in the device property, then
1520 * set the default to support dual-lane for DWC_usb32 and single-lane
1521 * for DWC_usb31 for super-speed-plus.
1523 if (dwc->maximum_speed == USB_SPEED_SUPER_PLUS) {
1524 switch (dwc->max_ssp_rate) {
1525 case USB_SSP_GEN_2x1:
1526 if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_GEN1)
1527 dev_warn(dev, "UDC only supports Gen 1\n");
1529 case USB_SSP_GEN_1x2:
1530 case USB_SSP_GEN_2x2:
1531 if (DWC3_IP_IS(DWC31))
1532 dev_warn(dev, "UDC only supports single lane\n");
1534 case USB_SSP_GEN_UNKNOWN:
1536 switch (hwparam_gen) {
1537 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
1538 if (DWC3_IP_IS(DWC32))
1539 dwc->max_ssp_rate = USB_SSP_GEN_2x2;
1541 dwc->max_ssp_rate = USB_SSP_GEN_2x1;
1543 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
1544 if (DWC3_IP_IS(DWC32))
1545 dwc->max_ssp_rate = USB_SSP_GEN_1x2;
1553 static int dwc3_probe(struct platform_device *pdev)
1555 struct device *dev = &pdev->dev;
1556 struct resource *res, dwc_res;
1563 dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
1569 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1571 dev_err(dev, "missing memory resource\n");
1575 dwc->xhci_resources[0].start = res->start;
1576 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1578 dwc->xhci_resources[0].flags = res->flags;
1579 dwc->xhci_resources[0].name = res->name;
1582 * Request memory region but exclude xHCI regs,
1583 * since it will be requested by the xhci-plat driver.
1586 dwc_res.start += DWC3_GLOBALS_REGS_START;
1588 regs = devm_ioremap_resource(dev, &dwc_res);
1590 return PTR_ERR(regs);
1593 dwc->regs_size = resource_size(&dwc_res);
1595 dwc3_get_properties(dwc);
1597 if (!dwc->sysdev_is_parent) {
1598 ret = dma_set_mask_and_coherent(dwc->sysdev, DMA_BIT_MASK(64));
1603 dwc->reset = devm_reset_control_array_get_optional_shared(dev);
1604 if (IS_ERR(dwc->reset))
1605 return PTR_ERR(dwc->reset);
1608 ret = devm_clk_bulk_get_all(dev, &dwc->clks);
1609 if (ret == -EPROBE_DEFER)
1612 * Clocks are optional, but new DT platforms should support all
1613 * clocks as required by the DT-binding.
1618 dwc->num_clks = ret;
1622 ret = reset_control_deassert(dwc->reset);
1626 ret = clk_bulk_prepare_enable(dwc->num_clks, dwc->clks);
1630 if (!dwc3_core_is_valid(dwc)) {
1631 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
1636 platform_set_drvdata(pdev, dwc);
1637 dwc3_cache_hwparams(dwc);
1639 spin_lock_init(&dwc->lock);
1640 mutex_init(&dwc->mutex);
1642 pm_runtime_set_active(dev);
1643 pm_runtime_use_autosuspend(dev);
1644 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
1645 pm_runtime_enable(dev);
1646 ret = pm_runtime_get_sync(dev);
1650 pm_runtime_forbid(dev);
1652 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1654 dev_err(dwc->dev, "failed to allocate event buffers\n");
1659 ret = dwc3_get_dr_mode(dwc);
1663 ret = dwc3_alloc_scratch_buffers(dwc);
1667 ret = dwc3_core_init(dwc);
1669 dev_err_probe(dev, ret, "failed to initialize core\n");
1673 dwc3_check_params(dwc);
1674 dwc3_debugfs_init(dwc);
1676 ret = dwc3_core_init_mode(dwc);
1680 pm_runtime_put(dev);
1685 dwc3_debugfs_exit(dwc);
1686 dwc3_event_buffers_cleanup(dwc);
1688 usb_phy_shutdown(dwc->usb2_phy);
1689 usb_phy_shutdown(dwc->usb3_phy);
1690 phy_exit(dwc->usb2_generic_phy);
1691 phy_exit(dwc->usb3_generic_phy);
1693 usb_phy_set_suspend(dwc->usb2_phy, 1);
1694 usb_phy_set_suspend(dwc->usb3_phy, 1);
1695 phy_power_off(dwc->usb2_generic_phy);
1696 phy_power_off(dwc->usb3_generic_phy);
1698 dwc3_ulpi_exit(dwc);
1701 dwc3_free_scratch_buffers(dwc);
1704 dwc3_free_event_buffers(dwc);
1707 pm_runtime_allow(&pdev->dev);
1710 pm_runtime_put_sync(&pdev->dev);
1711 pm_runtime_disable(&pdev->dev);
1714 clk_bulk_disable_unprepare(dwc->num_clks, dwc->clks);
1716 reset_control_assert(dwc->reset);
1719 power_supply_put(dwc->usb_psy);
1724 static int dwc3_remove(struct platform_device *pdev)
1726 struct dwc3 *dwc = platform_get_drvdata(pdev);
1728 pm_runtime_get_sync(&pdev->dev);
1730 dwc3_core_exit_mode(dwc);
1731 dwc3_debugfs_exit(dwc);
1733 dwc3_core_exit(dwc);
1734 dwc3_ulpi_exit(dwc);
1736 pm_runtime_disable(&pdev->dev);
1737 pm_runtime_put_noidle(&pdev->dev);
1738 pm_runtime_set_suspended(&pdev->dev);
1740 dwc3_free_event_buffers(dwc);
1741 dwc3_free_scratch_buffers(dwc);
1744 power_supply_put(dwc->usb_psy);
1750 static int dwc3_core_init_for_resume(struct dwc3 *dwc)
1754 ret = reset_control_deassert(dwc->reset);
1758 ret = clk_bulk_prepare_enable(dwc->num_clks, dwc->clks);
1762 ret = dwc3_core_init(dwc);
1769 clk_bulk_disable_unprepare(dwc->num_clks, dwc->clks);
1771 reset_control_assert(dwc->reset);
1776 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
1778 unsigned long flags;
1781 switch (dwc->current_dr_role) {
1782 case DWC3_GCTL_PRTCAP_DEVICE:
1783 if (pm_runtime_suspended(dwc->dev))
1785 spin_lock_irqsave(&dwc->lock, flags);
1786 dwc3_gadget_suspend(dwc);
1787 spin_unlock_irqrestore(&dwc->lock, flags);
1788 synchronize_irq(dwc->irq_gadget);
1789 dwc3_core_exit(dwc);
1791 case DWC3_GCTL_PRTCAP_HOST:
1792 if (!PMSG_IS_AUTO(msg)) {
1793 dwc3_core_exit(dwc);
1797 /* Let controller to suspend HSPHY before PHY driver suspends */
1798 if (dwc->dis_u2_susphy_quirk ||
1799 dwc->dis_enblslpm_quirk) {
1800 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1801 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
1802 DWC3_GUSB2PHYCFG_SUSPHY;
1803 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1805 /* Give some time for USB2 PHY to suspend */
1806 usleep_range(5000, 6000);
1809 phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
1810 phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
1812 case DWC3_GCTL_PRTCAP_OTG:
1813 /* do nothing during runtime_suspend */
1814 if (PMSG_IS_AUTO(msg))
1817 if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1818 spin_lock_irqsave(&dwc->lock, flags);
1819 dwc3_gadget_suspend(dwc);
1820 spin_unlock_irqrestore(&dwc->lock, flags);
1821 synchronize_irq(dwc->irq_gadget);
1825 dwc3_core_exit(dwc);
1835 static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
1837 unsigned long flags;
1841 switch (dwc->current_dr_role) {
1842 case DWC3_GCTL_PRTCAP_DEVICE:
1843 ret = dwc3_core_init_for_resume(dwc);
1847 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1848 spin_lock_irqsave(&dwc->lock, flags);
1849 dwc3_gadget_resume(dwc);
1850 spin_unlock_irqrestore(&dwc->lock, flags);
1852 case DWC3_GCTL_PRTCAP_HOST:
1853 if (!PMSG_IS_AUTO(msg)) {
1854 ret = dwc3_core_init_for_resume(dwc);
1857 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1860 /* Restore GUSB2PHYCFG bits that were modified in suspend */
1861 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1862 if (dwc->dis_u2_susphy_quirk)
1863 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1865 if (dwc->dis_enblslpm_quirk)
1866 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
1868 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1870 phy_pm_runtime_get_sync(dwc->usb2_generic_phy);
1871 phy_pm_runtime_get_sync(dwc->usb3_generic_phy);
1873 case DWC3_GCTL_PRTCAP_OTG:
1874 /* nothing to do on runtime_resume */
1875 if (PMSG_IS_AUTO(msg))
1878 ret = dwc3_core_init_for_resume(dwc);
1882 dwc3_set_prtcap(dwc, dwc->current_dr_role);
1885 if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
1886 dwc3_otg_host_init(dwc);
1887 } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1888 spin_lock_irqsave(&dwc->lock, flags);
1889 dwc3_gadget_resume(dwc);
1890 spin_unlock_irqrestore(&dwc->lock, flags);
1902 static int dwc3_runtime_checks(struct dwc3 *dwc)
1904 switch (dwc->current_dr_role) {
1905 case DWC3_GCTL_PRTCAP_DEVICE:
1909 case DWC3_GCTL_PRTCAP_HOST:
1918 static int dwc3_runtime_suspend(struct device *dev)
1920 struct dwc3 *dwc = dev_get_drvdata(dev);
1923 if (dwc3_runtime_checks(dwc))
1926 ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
1930 device_init_wakeup(dev, true);
1935 static int dwc3_runtime_resume(struct device *dev)
1937 struct dwc3 *dwc = dev_get_drvdata(dev);
1940 device_init_wakeup(dev, false);
1942 ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
1946 switch (dwc->current_dr_role) {
1947 case DWC3_GCTL_PRTCAP_DEVICE:
1948 dwc3_gadget_process_pending_events(dwc);
1950 case DWC3_GCTL_PRTCAP_HOST:
1956 pm_runtime_mark_last_busy(dev);
1961 static int dwc3_runtime_idle(struct device *dev)
1963 struct dwc3 *dwc = dev_get_drvdata(dev);
1965 switch (dwc->current_dr_role) {
1966 case DWC3_GCTL_PRTCAP_DEVICE:
1967 if (dwc3_runtime_checks(dwc))
1970 case DWC3_GCTL_PRTCAP_HOST:
1976 pm_runtime_mark_last_busy(dev);
1977 pm_runtime_autosuspend(dev);
1981 #endif /* CONFIG_PM */
1983 #ifdef CONFIG_PM_SLEEP
1984 static int dwc3_suspend(struct device *dev)
1986 struct dwc3 *dwc = dev_get_drvdata(dev);
1989 ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
1993 pinctrl_pm_select_sleep_state(dev);
1998 static int dwc3_resume(struct device *dev)
2000 struct dwc3 *dwc = dev_get_drvdata(dev);
2003 pinctrl_pm_select_default_state(dev);
2005 ret = dwc3_resume_common(dwc, PMSG_RESUME);
2009 pm_runtime_disable(dev);
2010 pm_runtime_set_active(dev);
2011 pm_runtime_enable(dev);
2016 static void dwc3_complete(struct device *dev)
2018 struct dwc3 *dwc = dev_get_drvdata(dev);
2021 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST &&
2022 dwc->dis_split_quirk) {
2023 reg = dwc3_readl(dwc->regs, DWC3_GUCTL3);
2024 reg |= DWC3_GUCTL3_SPLITDISABLE;
2025 dwc3_writel(dwc->regs, DWC3_GUCTL3, reg);
2029 #define dwc3_complete NULL
2030 #endif /* CONFIG_PM_SLEEP */
2032 static const struct dev_pm_ops dwc3_dev_pm_ops = {
2033 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
2034 .complete = dwc3_complete,
2035 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
2040 static const struct of_device_id of_dwc3_match[] = {
2042 .compatible = "snps,dwc3"
2045 .compatible = "synopsys,dwc3"
2049 MODULE_DEVICE_TABLE(of, of_dwc3_match);
2054 #define ACPI_ID_INTEL_BSW "808622B7"
2056 static const struct acpi_device_id dwc3_acpi_match[] = {
2057 { ACPI_ID_INTEL_BSW, 0 },
2060 MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
2063 static struct platform_driver dwc3_driver = {
2064 .probe = dwc3_probe,
2065 .remove = dwc3_remove,
2068 .of_match_table = of_match_ptr(of_dwc3_match),
2069 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
2070 .pm = &dwc3_dev_pm_ops,
2074 module_platform_driver(dwc3_driver);
2076 MODULE_ALIAS("platform:dwc3");
2077 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
2078 MODULE_LICENSE("GPL v2");
2079 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");