1 // SPDX-License-Identifier: GPL-2.0
3 * core.c - DesignWare USB3 DRD Controller Core file
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://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/of_address.h>
29 #include <linux/reset.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 does not support OTG mode. If the controller
85 * supports DRD but the dr_mode is not specified or set to OTG,
86 * then set the mode to peripheral.
88 if (mode == USB_DR_MODE_OTG && dwc3_is_usb31(dwc))
89 mode = USB_DR_MODE_PERIPHERAL;
92 if (mode != dwc->dr_mode) {
94 "Configuration mismatch. dr_mode forced to %s\n",
95 mode == USB_DR_MODE_HOST ? "host" : "gadget");
103 void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
107 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
108 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
109 reg |= DWC3_GCTL_PRTCAPDIR(mode);
110 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
112 dwc->current_dr_role = mode;
115 static void __dwc3_set_mode(struct work_struct *work)
117 struct dwc3 *dwc = work_to_dwc(work);
121 if (dwc->dr_mode != USB_DR_MODE_OTG)
124 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
125 dwc3_otg_update(dwc, 0);
127 if (!dwc->desired_dr_role)
130 if (dwc->desired_dr_role == dwc->current_dr_role)
133 if (dwc->desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev)
136 switch (dwc->current_dr_role) {
137 case DWC3_GCTL_PRTCAP_HOST:
140 case DWC3_GCTL_PRTCAP_DEVICE:
141 dwc3_gadget_exit(dwc);
142 dwc3_event_buffers_cleanup(dwc);
144 case DWC3_GCTL_PRTCAP_OTG:
146 spin_lock_irqsave(&dwc->lock, flags);
147 dwc->desired_otg_role = DWC3_OTG_ROLE_IDLE;
148 spin_unlock_irqrestore(&dwc->lock, flags);
149 dwc3_otg_update(dwc, 1);
155 spin_lock_irqsave(&dwc->lock, flags);
157 dwc3_set_prtcap(dwc, dwc->desired_dr_role);
159 spin_unlock_irqrestore(&dwc->lock, flags);
161 switch (dwc->desired_dr_role) {
162 case DWC3_GCTL_PRTCAP_HOST:
163 ret = dwc3_host_init(dwc);
165 dev_err(dwc->dev, "failed to initialize host\n");
168 otg_set_vbus(dwc->usb2_phy->otg, true);
169 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
170 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
171 phy_calibrate(dwc->usb2_generic_phy);
174 case DWC3_GCTL_PRTCAP_DEVICE:
175 dwc3_event_buffers_setup(dwc);
178 otg_set_vbus(dwc->usb2_phy->otg, false);
179 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
180 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
182 ret = dwc3_gadget_init(dwc);
184 dev_err(dwc->dev, "failed to initialize peripheral\n");
186 case DWC3_GCTL_PRTCAP_OTG:
188 dwc3_otg_update(dwc, 0);
196 void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
200 spin_lock_irqsave(&dwc->lock, flags);
201 dwc->desired_dr_role = mode;
202 spin_unlock_irqrestore(&dwc->lock, flags);
204 queue_work(system_freezable_wq, &dwc->drd_work);
207 u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
209 struct dwc3 *dwc = dep->dwc;
212 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
213 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
214 DWC3_GDBGFIFOSPACE_TYPE(type));
216 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
218 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
222 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
223 * @dwc: pointer to our context structure
225 static int dwc3_core_soft_reset(struct dwc3 *dwc)
231 usb_phy_init(dwc->usb2_phy);
232 usb_phy_init(dwc->usb3_phy);
233 ret = phy_init(dwc->usb2_generic_phy);
237 ret = phy_init(dwc->usb3_generic_phy);
239 phy_exit(dwc->usb2_generic_phy);
244 * We're resetting only the device side because, if we're in host mode,
245 * XHCI driver will reset the host block. If dwc3 was configured for
246 * host-only mode, then we can return early.
248 if (dwc->dr_mode == USB_DR_MODE_HOST || dwc->is_hibernated == true)
251 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST)
254 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
255 reg |= DWC3_DCTL_CSFTRST;
256 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
259 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
260 if (!(reg & DWC3_DCTL_CSFTRST))
266 phy_exit(dwc->usb3_generic_phy);
267 phy_exit(dwc->usb2_generic_phy);
273 * For DWC_usb31 controller, once DWC3_DCTL_CSFTRST bit is cleared,
274 * we must wait at least 50ms before accessing the PHY domain
275 * (synchronization delay). DWC_usb31 programming guide section 1.3.2.
277 if (dwc3_is_usb31(dwc))
283 static const struct clk_bulk_data dwc3_core_clks[] = {
285 { .id = "bus_early" },
290 * dwc3_frame_length_adjustment - Adjusts frame length if required
291 * @dwc3: Pointer to our controller context structure
293 static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
298 if (dwc->revision < DWC3_REVISION_250A)
304 /* Save the initial DWC3_GFLADJ register value */
305 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
308 if (dwc->refclk_fladj) {
309 if ((reg & DWC3_GFLADJ_REFCLK_FLADJ) !=
310 (dwc->fladj & DWC3_GFLADJ_REFCLK_FLADJ)) {
311 reg &= ~DWC3_GFLADJ_REFCLK_FLADJ;
312 reg |= (dwc->fladj & DWC3_GFLADJ_REFCLK_FLADJ);
316 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
317 if (dft != dwc->fladj) {
318 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
319 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
322 /* Update DWC3_GFLADJ if there is any change from initial value */
324 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
328 * dwc3_free_one_event_buffer - Frees one event buffer
329 * @dwc: Pointer to our controller context structure
330 * @evt: Pointer to event buffer to be freed
332 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
333 struct dwc3_event_buffer *evt)
335 dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
339 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
340 * @dwc: Pointer to our controller context structure
341 * @length: size of the event buffer
343 * Returns a pointer to the allocated event buffer structure on success
344 * otherwise ERR_PTR(errno).
346 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
349 struct dwc3_event_buffer *evt;
351 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
353 return ERR_PTR(-ENOMEM);
356 evt->length = length;
357 evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
359 return ERR_PTR(-ENOMEM);
361 evt->buf = dma_alloc_coherent(dwc->sysdev, length,
362 &evt->dma, GFP_KERNEL);
364 return ERR_PTR(-ENOMEM);
370 * dwc3_free_event_buffers - frees all allocated event buffers
371 * @dwc: Pointer to our controller context structure
373 void dwc3_free_event_buffers(struct dwc3 *dwc)
375 struct dwc3_event_buffer *evt;
379 dwc3_free_one_event_buffer(dwc, evt);
383 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
384 * @dwc: pointer to our controller context structure
385 * @length: size of event buffer
387 * Returns 0 on success otherwise negative errno. In the error case, dwc
388 * may contain some buffers allocated but not all which were requested.
390 int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
392 struct dwc3_event_buffer *evt;
394 evt = dwc3_alloc_one_event_buffer(dwc, length);
396 dev_err(dwc->dev, "can't allocate event buffer\n");
405 * dwc3_event_buffers_setup - setup our allocated event buffers
406 * @dwc: pointer to our controller context structure
408 * Returns 0 on success otherwise negative errno.
410 int dwc3_event_buffers_setup(struct dwc3 *dwc)
412 struct dwc3_event_buffer *evt;
414 if (dwc->dr_mode == USB_DR_MODE_HOST)
419 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
420 lower_32_bits(evt->dma));
421 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
422 upper_32_bits(evt->dma));
423 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
424 DWC3_GEVNTSIZ_SIZE(evt->length));
425 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
430 void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
432 struct dwc3_event_buffer *evt;
438 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
439 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
440 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
441 | DWC3_GEVNTSIZ_SIZE(0));
442 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
445 static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
449 if (dwc->dr_mode == USB_DR_MODE_HOST)
452 if (!dwc->has_hibernation)
455 if (!dwc->nr_scratch)
458 /* Allocate only if scratchbuf is NULL */
462 size = dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE;
464 dwc->scratchbuf = kzalloc(size, GFP_KERNEL);
466 if (!dwc->scratchbuf)
469 dwc->scratch_addr = dma_map_single(dwc->dev, dwc->scratchbuf, size,
471 if (dma_mapping_error(dwc->dev, dwc->scratch_addr)) {
472 dev_err(dwc->dev, "failed to map scratch buffer\n");
479 static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
484 if (dwc->dr_mode == USB_DR_MODE_HOST)
487 if (!dwc->has_hibernation)
490 if (!dwc->nr_scratch)
493 /* should never fall here */
494 if (WARN_ON(!dwc->scratchbuf))
497 param = lower_32_bits(dwc->scratch_addr);
499 ret = dwc3_send_gadget_generic_command(dwc,
500 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
504 param = upper_32_bits(dwc->scratch_addr);
506 ret = dwc3_send_gadget_generic_command(dwc,
507 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
514 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
515 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
520 static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
522 if (!dwc->has_hibernation)
525 if (!dwc->nr_scratch)
528 /* should never fall here */
529 if (WARN_ON(!dwc->scratchbuf))
532 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
533 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
534 kfree(dwc->scratchbuf);
537 static void dwc3_core_num_eps(struct dwc3 *dwc)
539 struct dwc3_hwparams *parms = &dwc->hwparams;
541 dwc->num_eps = DWC3_NUM_EPS(parms);
544 static void dwc3_cache_hwparams(struct dwc3 *dwc)
546 struct dwc3_hwparams *parms = &dwc->hwparams;
548 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
549 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
550 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
551 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
552 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
553 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
554 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
555 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
556 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
559 static int dwc3_config_soc_bus(struct dwc3 *dwc)
564 * Check if CCI is enabled for USB. Returns true
565 * if the node has property 'dma-coherent'. Otherwise
568 if (of_dma_is_coherent(dwc->dev->of_node)) {
571 reg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
572 reg |= DWC3_GSBUSCFG0_DATRDREQINFO |
573 DWC3_GSBUSCFG0_DESRDREQINFO |
574 DWC3_GSBUSCFG0_DATWRREQINFO |
575 DWC3_GSBUSCFG0_DESWRREQINFO;
576 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, reg);
580 * This routes the usb dma traffic to go through CCI path instead
581 * of reaching DDR directly. This traffic routing is needed to
582 * to make SMMU and CCI work with USB dma.
584 if (of_dma_is_coherent(dwc->dev->of_node) || dwc->dev->iommu_group) {
585 ret = dwc3_enable_hw_coherency(dwc->dev);
590 /* Send struct dwc3 to dwc3-of-simple for configuring VBUS
591 * during suspend/resume
593 dwc3_set_simple_data(dwc);
598 static int dwc3_core_ulpi_init(struct dwc3 *dwc)
603 intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
605 if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
606 (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
607 dwc->hsphy_interface &&
608 !strncmp(dwc->hsphy_interface, "ulpi", 4)))
609 ret = dwc3_ulpi_init(dwc);
615 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
616 * @dwc: Pointer to our controller context structure
618 * Returns 0 on success. The USB PHY interfaces are configured but not
619 * initialized. The PHY interfaces and the PHYs get initialized together with
620 * the core in dwc3_core_init.
622 static int dwc3_phy_setup(struct dwc3 *dwc)
626 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
629 * Make sure UX_EXIT_PX is cleared as that causes issues with some
630 * PHYs. Also, this bit is not supposed to be used in normal operation.
632 reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
635 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
636 * to '0' during coreConsultant configuration. So default value
637 * will be '0' when the core is reset. Application needs to set it
638 * to '1' after the core initialization is completed.
640 if (dwc->revision > DWC3_REVISION_194A)
641 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
643 if (dwc->u2ss_inp3_quirk)
644 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
646 if (dwc->dis_rxdet_inp3_quirk)
647 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
649 if (dwc->req_p1p2p3_quirk)
650 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
652 if (dwc->del_p1p2p3_quirk)
653 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
655 if (dwc->del_phy_power_chg_quirk)
656 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
658 if (dwc->lfps_filter_quirk)
659 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
661 if (dwc->rx_detect_poll_quirk)
662 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
664 if (dwc->tx_de_emphasis_quirk)
665 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
667 if (dwc->dis_u3_susphy_quirk)
668 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
670 if (dwc->dis_del_phy_power_chg_quirk)
671 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
673 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
675 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
677 /* Select the HS PHY interface */
678 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
679 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
680 if (dwc->hsphy_interface &&
681 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
682 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
684 } else if (dwc->hsphy_interface &&
685 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
686 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
687 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
689 /* Relying on default value. */
690 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
694 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
700 switch (dwc->hsphy_mode) {
701 case USBPHY_INTERFACE_MODE_UTMI:
702 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
703 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
704 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
705 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
707 case USBPHY_INTERFACE_MODE_UTMIW:
708 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
709 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
710 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
711 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
718 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
719 * '0' during coreConsultant configuration. So default value will
720 * be '0' when the core is reset. Application needs to set it to
721 * '1' after the core initialization is completed.
723 if (dwc->revision > DWC3_REVISION_194A)
724 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
726 if (dwc->dis_u2_susphy_quirk)
727 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
729 if (dwc->dis_enblslpm_quirk)
730 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
732 if (dwc->dis_u2_freeclk_exists_quirk)
733 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
735 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
740 static void dwc3_core_exit(struct dwc3 *dwc)
742 usb_phy_shutdown(dwc->usb2_phy);
743 usb_phy_shutdown(dwc->usb3_phy);
744 phy_exit(dwc->usb2_generic_phy);
745 phy_exit(dwc->usb3_generic_phy);
747 usb_phy_set_suspend(dwc->usb2_phy, 1);
748 usb_phy_set_suspend(dwc->usb3_phy, 1);
749 phy_power_off(dwc->usb2_generic_phy);
750 phy_power_off(dwc->usb3_generic_phy);
751 clk_bulk_disable(dwc->num_clks, dwc->clks);
752 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
753 reset_control_assert(dwc->reset);
756 static bool dwc3_core_is_valid(struct dwc3 *dwc)
760 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
762 /* This should read as U3 followed by revision number */
763 if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
764 /* Detected DWC_usb3 IP */
766 } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
767 /* Detected DWC_usb31 IP */
768 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
769 dwc->revision |= DWC3_REVISION_IS_DWC31;
777 static void dwc3_core_setup_global_control(struct dwc3 *dwc)
779 u32 hwparams4 = dwc->hwparams.hwparams4;
782 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
783 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
785 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
786 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
788 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
789 * issue which would cause xHCI compliance tests to fail.
791 * Because of that we cannot enable clock gating on such
796 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
799 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
800 dwc->dr_mode == USB_DR_MODE_OTG) &&
801 (dwc->revision >= DWC3_REVISION_210A &&
802 dwc->revision <= DWC3_REVISION_250A))
803 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
805 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
807 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
808 if (!device_property_read_bool(dwc->dev,
809 "snps,enable-hibernation")) {
810 dev_dbg(dwc->dev, "Hibernation not enabled\n");
812 /* enable hibernation here */
814 DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
815 dwc->has_hibernation = 1;
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 optmizations\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 (dwc->revision < DWC3_REVISION_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_read_u32_array(dev,
885 "snps,incr-burst-type-adjustment", NULL, 0);
889 vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL);
891 dev_err(dev, "Error to get memory\n");
895 /* Get INCR burst type, and parse it */
896 ret = device_property_read_u32_array(dev,
897 "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;
917 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
918 cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
920 cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
921 switch (incrx_size) {
923 cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
926 cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
929 cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
932 cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
935 cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
938 cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
941 cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
946 dev_err(dev, "Invalid property\n");
950 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
954 * dwc3_core_init - Low-level initialization of DWC3 Core
955 * @dwc: Pointer to our controller context structure
957 * Returns 0 on success otherwise negative errno.
959 int dwc3_core_init(struct dwc3 *dwc)
964 if (!dwc3_core_is_valid(dwc)) {
965 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
971 * Write Linux Version Code to our GUID register so it's easy to figure
972 * out which kernel version a bug was found.
974 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
976 /* Handle USB2.0-only core configuration */
977 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
978 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
979 if (dwc->maximum_speed == USB_SPEED_SUPER)
980 dwc->maximum_speed = USB_SPEED_HIGH;
983 ret = dwc3_phy_setup(dwc);
987 if (!dwc->ulpi_ready) {
988 ret = dwc3_core_ulpi_init(dwc);
991 dwc->ulpi_ready = true;
994 if (!dwc->phys_ready) {
995 ret = dwc3_core_get_phy(dwc);
998 dwc->phys_ready = true;
1001 ret = dwc3_core_soft_reset(dwc);
1005 dwc3_core_setup_global_control(dwc);
1006 dwc3_core_num_eps(dwc);
1008 if (dwc->scratchbuf == NULL) {
1009 ret = dwc3_alloc_scratch_buffers(dwc);
1012 "Not enough memory for scratch buffers\n");
1017 ret = dwc3_setup_scratch_buffers(dwc);
1019 dev_err(dwc->dev, "Failed to setup scratch buffers: %d\n", ret);
1023 /* Adjust Frame Length */
1024 dwc3_frame_length_adjustment(dwc);
1026 dwc3_set_incr_burst_type(dwc);
1028 ret = dwc3_config_soc_bus(dwc);
1032 usb_phy_set_suspend(dwc->usb2_phy, 0);
1033 usb_phy_set_suspend(dwc->usb3_phy, 0);
1034 ret = phy_power_on(dwc->usb2_generic_phy);
1038 ret = phy_power_on(dwc->usb3_generic_phy);
1042 ret = dwc3_event_buffers_setup(dwc);
1044 dev_err(dwc->dev, "failed to setup event buffers\n");
1048 switch (dwc->dr_mode) {
1049 case USB_DR_MODE_PERIPHERAL:
1050 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1052 case USB_DR_MODE_HOST:
1053 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1055 case USB_DR_MODE_OTG:
1056 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_OTG);
1059 dev_warn(dwc->dev, "Unsupported mode %d\n", dwc->dr_mode);
1064 * ENDXFER polling is available on version 3.10a and later of
1065 * the DWC_usb3 controller. It is NOT available in the
1066 * DWC_usb31 controller.
1068 if (!dwc3_is_usb31(dwc) && dwc->revision >= DWC3_REVISION_310A) {
1069 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
1070 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
1071 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
1074 /* When configured in HOST mode, after issuing U3/L2 exit controller
1075 * fails to send proper CRC checksum in CRC5 feild. Because of this
1076 * behaviour Transaction Error is generated, resulting in reset and
1077 * re-enumeration of usb device attached. Enabling bit 10 of GUCTL1
1078 * will correct this problem
1080 if (dwc->enable_guctl1_resume_quirk) {
1081 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1082 reg |= DWC3_GUCTL1_RESUME_QUIRK;
1083 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1086 /* SNPS controller when configureed in HOST mode maintains Inter Packet
1087 * Delay (IPD) of ~380ns which works with most of the super-speed hubs
1088 * except VIA-LAB hubs. When IPD is ~380ns HOST controller fails to
1089 * enumerate FS/LS devices when connected behind VIA-LAB hubs.
1090 * Enabling bit 9 of GUCTL1 enables the workaround in HW to reduce the
1091 * ULPI clock latency by 1 cycle, thus reducing the IPD (~360ns) and
1092 * making controller enumerate FS/LS devices connected behind VIA-LAB.
1094 if (dwc->enable_guctl1_ipd_quirk) {
1095 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1096 reg |= DWC3_GUCTL1_IPD_QUIRK;
1097 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1100 if (dwc->revision >= DWC3_REVISION_250A) {
1101 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1104 * Enable hardware control of sending remote wakeup
1105 * in HS when the device is in the L1 state.
1107 if (dwc->revision >= DWC3_REVISION_290A)
1108 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
1110 if (dwc->dis_tx_ipgap_linecheck_quirk)
1111 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
1113 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1116 if (dwc->dr_mode == USB_DR_MODE_HOST ||
1117 dwc->dr_mode == USB_DR_MODE_OTG) {
1118 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
1121 * Enable Auto retry Feature to make the controller operating in
1122 * Host mode on seeing transaction errors(CRC errors or internal
1123 * overrun scenerios) on IN transfers to reply to the device
1124 * with a non-terminating retry ACK (i.e, an ACK transcation
1125 * packet with Retry=1 & Nump != 0)
1127 reg |= DWC3_GUCTL_HSTINAUTORETRY;
1129 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
1133 * Must config both number of packets and max burst settings to enable
1134 * RX and/or TX threshold.
1136 if (dwc3_is_usb31(dwc) && dwc->dr_mode == USB_DR_MODE_HOST) {
1137 u8 rx_thr_num = dwc->rx_thr_num_pkt_prd;
1138 u8 rx_maxburst = dwc->rx_max_burst_prd;
1139 u8 tx_thr_num = dwc->tx_thr_num_pkt_prd;
1140 u8 tx_maxburst = dwc->tx_max_burst_prd;
1142 if (rx_thr_num && rx_maxburst) {
1143 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1144 reg |= DWC31_RXTHRNUMPKTSEL_PRD;
1146 reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
1147 reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
1149 reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
1150 reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
1152 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1155 if (tx_thr_num && tx_maxburst) {
1156 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1157 reg |= DWC31_TXTHRNUMPKTSEL_PRD;
1159 reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
1160 reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
1162 reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
1163 reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
1165 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1172 phy_power_off(dwc->usb3_generic_phy);
1175 phy_power_off(dwc->usb2_generic_phy);
1178 usb_phy_set_suspend(dwc->usb2_phy, 1);
1179 usb_phy_set_suspend(dwc->usb3_phy, 1);
1182 usb_phy_shutdown(dwc->usb2_phy);
1183 usb_phy_shutdown(dwc->usb3_phy);
1184 phy_exit(dwc->usb2_generic_phy);
1185 phy_exit(dwc->usb3_generic_phy);
1188 dwc3_ulpi_exit(dwc);
1194 static int dwc3_core_get_phy(struct dwc3 *dwc)
1196 struct device *dev = dwc->dev;
1197 struct device_node *node = dev->of_node;
1201 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
1202 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
1204 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1205 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
1208 if (IS_ERR(dwc->usb2_phy)) {
1209 ret = PTR_ERR(dwc->usb2_phy);
1210 if (ret == -ENXIO || ret == -ENODEV) {
1211 dwc->usb2_phy = NULL;
1212 } else if (ret == -EPROBE_DEFER) {
1215 dev_err(dev, "no usb2 phy configured\n");
1220 if (IS_ERR(dwc->usb3_phy)) {
1221 ret = PTR_ERR(dwc->usb3_phy);
1222 if (ret == -ENXIO || ret == -ENODEV) {
1223 dwc->usb3_phy = NULL;
1224 } else if (ret == -EPROBE_DEFER) {
1227 dev_err(dev, "no usb3 phy configured\n");
1232 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
1233 if (IS_ERR(dwc->usb2_generic_phy)) {
1234 ret = PTR_ERR(dwc->usb2_generic_phy);
1235 if (ret == -ENOSYS || ret == -ENODEV) {
1236 dwc->usb2_generic_phy = NULL;
1237 } else if (ret == -EPROBE_DEFER) {
1240 dev_err(dev, "no usb2 phy configured\n");
1245 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
1246 if (IS_ERR(dwc->usb3_generic_phy)) {
1247 ret = PTR_ERR(dwc->usb3_generic_phy);
1248 if (ret == -ENOSYS || ret == -ENODEV) {
1249 dwc->usb3_generic_phy = NULL;
1250 } else if (ret == -EPROBE_DEFER) {
1253 dev_err(dev, "no usb3 phy configured\n");
1261 static int dwc3_core_init_mode(struct dwc3 *dwc)
1263 struct device *dev = dwc->dev;
1266 switch (dwc->dr_mode) {
1267 case USB_DR_MODE_PERIPHERAL:
1268 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1271 otg_set_vbus(dwc->usb2_phy->otg, false);
1272 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
1273 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
1275 ret = dwc3_gadget_init(dwc);
1277 if (ret != -EPROBE_DEFER)
1278 dev_err(dev, "failed to initialize gadget\n");
1282 case USB_DR_MODE_HOST:
1283 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1286 otg_set_vbus(dwc->usb2_phy->otg, true);
1287 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
1288 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
1290 ret = dwc3_host_init(dwc);
1292 if (ret != -EPROBE_DEFER)
1293 dev_err(dev, "failed to initialize host\n");
1296 phy_calibrate(dwc->usb2_generic_phy);
1298 case USB_DR_MODE_OTG:
1299 INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
1300 ret = dwc3_drd_init(dwc);
1302 if (ret != -EPROBE_DEFER)
1303 dev_err(dev, "failed to initialize dual-role\n");
1307 #if IS_ENABLED(CONFIG_USB_DWC3_OTG)
1308 dwc->current_dr_role = 0;
1309 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
1313 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1320 static void dwc3_core_exit_mode(struct dwc3 *dwc)
1322 switch (dwc->dr_mode) {
1323 case USB_DR_MODE_PERIPHERAL:
1324 dwc3_gadget_exit(dwc);
1326 case USB_DR_MODE_HOST:
1327 dwc3_host_exit(dwc);
1329 case USB_DR_MODE_OTG:
1338 static void dwc3_get_properties(struct dwc3 *dwc)
1340 struct device *dev = dwc->dev;
1341 u8 lpm_nyet_threshold;
1344 u8 rx_thr_num_pkt_prd;
1345 u8 rx_max_burst_prd;
1346 u8 tx_thr_num_pkt_prd;
1347 u8 tx_max_burst_prd;
1349 /* default to highest possible threshold */
1350 lpm_nyet_threshold = 0xff;
1352 /* default to -3.5dB de-emphasis */
1356 * default to assert utmi_sleep_n and use maximum allowed HIRD
1357 * threshold value of 0b1100
1359 hird_threshold = 12;
1361 dwc->maximum_speed = usb_get_maximum_speed(dev);
1362 dwc->dr_mode = usb_get_dr_mode(dev);
1363 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
1365 dwc->sysdev_is_parent = device_property_read_bool(dev,
1366 "linux,sysdev_is_parent");
1367 if (dwc->sysdev_is_parent)
1368 dwc->sysdev = dwc->dev->parent;
1370 dwc->sysdev = dwc->dev;
1372 dwc->has_lpm_erratum = device_property_read_bool(dev,
1373 "snps,has-lpm-erratum");
1374 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
1375 &lpm_nyet_threshold);
1376 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
1377 "snps,is-utmi-l1-suspend");
1378 device_property_read_u8(dev, "snps,hird-threshold",
1380 dwc->usb3_lpm_capable = device_property_read_bool(dev,
1381 "snps,usb3_lpm_capable");
1382 device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
1383 &rx_thr_num_pkt_prd);
1384 device_property_read_u8(dev, "snps,rx-max-burst-prd",
1386 device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
1387 &tx_thr_num_pkt_prd);
1388 device_property_read_u8(dev, "snps,tx-max-burst-prd",
1391 dwc->disable_scramble_quirk = device_property_read_bool(dev,
1392 "snps,disable_scramble_quirk");
1393 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
1394 "snps,u2exit_lfps_quirk");
1395 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
1396 "snps,u2ss_inp3_quirk");
1397 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
1398 "snps,req_p1p2p3_quirk");
1399 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
1400 "snps,del_p1p2p3_quirk");
1401 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
1402 "snps,del_phy_power_chg_quirk");
1403 dwc->lfps_filter_quirk = device_property_read_bool(dev,
1404 "snps,lfps_filter_quirk");
1405 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
1406 "snps,rx_detect_poll_quirk");
1407 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
1408 "snps,dis_u3_susphy_quirk");
1409 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
1410 "snps,dis_u2_susphy_quirk");
1411 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1412 "snps,dis_enblslpm_quirk");
1413 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1414 "snps,dis_rxdet_inp3_quirk");
1415 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1416 "snps,dis-u2-freeclk-exists-quirk");
1417 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1418 "snps,dis-del-phy-power-chg-quirk");
1419 dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1420 "snps,dis-tx-ipgap-linecheck-quirk");
1422 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
1423 "snps,tx_de_emphasis_quirk");
1424 device_property_read_u8(dev, "snps,tx_de_emphasis",
1426 device_property_read_string(dev, "snps,hsphy_interface",
1427 &dwc->hsphy_interface);
1428 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
1431 dwc->refclk_fladj = device_property_read_bool(dev,
1432 "snps,refclk_fladj");
1433 dwc->enable_guctl1_resume_quirk = device_property_read_bool(dev,
1434 "snps,enable_guctl1_resume_quirk");
1435 dwc->enable_guctl1_ipd_quirk = device_property_read_bool(dev,
1436 "snps,enable_guctl1_ipd_quirk");
1437 dwc->dis_metastability_quirk = device_property_read_bool(dev,
1438 "snps,dis_metastability_quirk");
1440 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1441 dwc->tx_de_emphasis = tx_de_emphasis;
1443 dwc->hird_threshold = hird_threshold
1444 | (dwc->is_utmi_l1_suspend << 4);
1446 /* Check if extra quirks to be added */
1447 dwc3_simple_check_quirks(dwc);
1449 dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd;
1450 dwc->rx_max_burst_prd = rx_max_burst_prd;
1452 dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd;
1453 dwc->tx_max_burst_prd = tx_max_burst_prd;
1455 dwc->imod_interval = 0;
1458 /* check whether the core supports IMOD */
1459 bool dwc3_has_imod(struct dwc3 *dwc)
1461 return ((dwc3_is_usb3(dwc) &&
1462 dwc->revision >= DWC3_REVISION_300A) ||
1463 (dwc3_is_usb31(dwc) &&
1464 dwc->revision >= DWC3_USB31_REVISION_120A));
1467 static void dwc3_check_params(struct dwc3 *dwc)
1469 struct device *dev = dwc->dev;
1471 /* Check for proper value of imod_interval */
1472 if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
1473 dev_warn(dwc->dev, "Interrupt moderation not supported\n");
1474 dwc->imod_interval = 0;
1478 * Workaround for STAR 9000961433 which affects only version
1479 * 3.00a of the DWC_usb3 core. This prevents the controller
1480 * interrupt from being masked while handling events. IMOD
1481 * allows us to work around this issue. Enable it for the
1484 if (!dwc->imod_interval &&
1485 (dwc->revision == DWC3_REVISION_300A))
1486 dwc->imod_interval = 1;
1488 /* Check the maximum_speed parameter */
1489 switch (dwc->maximum_speed) {
1491 case USB_SPEED_FULL:
1492 case USB_SPEED_HIGH:
1493 case USB_SPEED_SUPER:
1494 case USB_SPEED_SUPER_PLUS:
1497 dev_err(dev, "invalid maximum_speed parameter %d\n",
1498 dwc->maximum_speed);
1500 case USB_SPEED_UNKNOWN:
1501 /* default to superspeed */
1502 dwc->maximum_speed = USB_SPEED_SUPER;
1505 * default to superspeed plus if we are capable.
1507 if (dwc3_is_usb31(dwc) &&
1508 (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
1509 DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1510 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1516 static int dwc3_probe(struct platform_device *pdev)
1518 struct device *dev = &pdev->dev;
1519 struct resource *res, dwc_res;
1525 dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
1529 dwc->clks = devm_kmemdup(dev, dwc3_core_clks, sizeof(dwc3_core_clks),
1536 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1538 dev_err(dev, "missing memory resource\n");
1542 dwc->xhci_resources[0].start = res->start;
1543 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1545 dwc->xhci_resources[0].flags = res->flags;
1546 dwc->xhci_resources[0].name = res->name;
1549 * Request memory region but exclude xHCI regs,
1550 * since it will be requested by the xhci-plat driver.
1553 dwc_res.start += DWC3_GLOBALS_REGS_START;
1555 regs = devm_ioremap_resource(dev, &dwc_res);
1557 return PTR_ERR(regs);
1560 dwc->regs_size = resource_size(&dwc_res);
1562 dwc3_get_properties(dwc);
1564 dwc->reset = devm_reset_control_get_optional_shared(dev, NULL);
1565 if (IS_ERR(dwc->reset))
1566 return PTR_ERR(dwc->reset);
1569 dwc->num_clks = ARRAY_SIZE(dwc3_core_clks);
1571 ret = clk_bulk_get(dev, dwc->num_clks, dwc->clks);
1572 if (ret == -EPROBE_DEFER)
1575 * Clocks are optional, but new DT platforms should support all
1576 * clocks as required by the DT-binding.
1582 ret = reset_control_deassert(dwc->reset);
1586 ret = clk_bulk_prepare(dwc->num_clks, dwc->clks);
1590 ret = clk_bulk_enable(dwc->num_clks, dwc->clks);
1592 goto unprepare_clks;
1594 platform_set_drvdata(pdev, dwc);
1595 dwc3_cache_hwparams(dwc);
1597 spin_lock_init(&dwc->lock);
1599 /* Set dma coherent mask to DMA BUS data width */
1600 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1601 dev_dbg(dev, "Enabling %d-bit DMA addresses.\n", mdwidth);
1602 dma_set_coherent_mask(dev, DMA_BIT_MASK(mdwidth));
1604 pm_runtime_set_active(dev);
1605 pm_runtime_use_autosuspend(dev);
1606 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
1607 pm_runtime_enable(dev);
1608 ret = pm_runtime_get_sync(dev);
1612 pm_runtime_forbid(dev);
1614 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1616 dev_err(dwc->dev, "failed to allocate event buffers\n");
1621 ret = dwc3_get_dr_mode(dwc);
1625 ret = dwc3_core_init(dwc);
1627 dev_err(dev, "failed to initialize core\n");
1631 dwc3_check_params(dwc);
1633 ret = dwc3_core_init_mode(dwc);
1637 dwc3_debugfs_init(dwc);
1638 pm_runtime_put(dev);
1643 dwc3_event_buffers_cleanup(dwc);
1646 dwc3_free_scratch_buffers(dwc);
1649 dwc3_free_event_buffers(dwc);
1652 pm_runtime_allow(&pdev->dev);
1655 pm_runtime_put_sync(&pdev->dev);
1656 pm_runtime_disable(&pdev->dev);
1658 clk_bulk_disable(dwc->num_clks, dwc->clks);
1660 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
1662 reset_control_assert(dwc->reset);
1664 clk_bulk_put(dwc->num_clks, dwc->clks);
1669 static int dwc3_remove(struct platform_device *pdev)
1671 struct dwc3 *dwc = platform_get_drvdata(pdev);
1673 pm_runtime_get_sync(&pdev->dev);
1675 dwc3_debugfs_exit(dwc);
1676 dwc3_core_exit_mode(dwc);
1678 dwc3_event_buffers_cleanup(dwc);
1679 dwc3_core_exit(dwc);
1680 dwc3_ulpi_exit(dwc);
1682 pm_runtime_put_sync(&pdev->dev);
1683 pm_runtime_allow(&pdev->dev);
1684 pm_runtime_disable(&pdev->dev);
1686 dwc3_free_event_buffers(dwc);
1687 dwc3_free_scratch_buffers(dwc);
1688 clk_bulk_put(dwc->num_clks, dwc->clks);
1694 static int dwc3_core_init_for_resume(struct dwc3 *dwc)
1698 ret = reset_control_deassert(dwc->reset);
1702 ret = clk_bulk_prepare(dwc->num_clks, dwc->clks);
1706 ret = clk_bulk_enable(dwc->num_clks, dwc->clks);
1708 goto unprepare_clks;
1710 ret = dwc3_core_init(dwc);
1717 clk_bulk_disable(dwc->num_clks, dwc->clks);
1719 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
1721 reset_control_assert(dwc->reset);
1726 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
1728 unsigned long flags;
1731 switch (dwc->current_dr_role) {
1732 case DWC3_GCTL_PRTCAP_DEVICE:
1733 spin_lock_irqsave(&dwc->lock, flags);
1734 dwc3_gadget_suspend(dwc);
1735 spin_unlock_irqrestore(&dwc->lock, flags);
1736 dwc3_core_exit(dwc);
1738 case DWC3_GCTL_PRTCAP_HOST:
1739 if (!PMSG_IS_AUTO(msg)) {
1740 dwc3_core_exit(dwc);
1744 /* Let controller to suspend HSPHY before PHY driver suspends */
1745 if (dwc->dis_u2_susphy_quirk ||
1746 dwc->dis_enblslpm_quirk) {
1747 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1748 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
1749 DWC3_GUSB2PHYCFG_SUSPHY;
1750 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1752 /* Give some time for USB2 PHY to suspend */
1753 usleep_range(5000, 6000);
1756 phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
1757 phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
1759 case DWC3_GCTL_PRTCAP_OTG:
1760 /* do nothing during runtime_suspend */
1761 if (PMSG_IS_AUTO(msg))
1764 if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1765 spin_lock_irqsave(&dwc->lock, flags);
1766 dwc3_gadget_suspend(dwc);
1767 spin_unlock_irqrestore(&dwc->lock, flags);
1771 dwc3_core_exit(dwc);
1778 dwc3_event_buffers_cleanup(dwc);
1780 /* Put the core into D3 state */
1781 dwc3_set_usb_core_power(dwc, false);
1784 * To avoid reinit of phy during resume, prevent calling the
1785 * dwc3_core_exit() when in D3 state
1788 dwc3_core_exit(dwc);
1793 static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
1795 unsigned long flags;
1799 /* Bring core to D0 state */
1800 dwc3_set_usb_core_power(dwc, true);
1802 ret = dwc3_core_init(dwc);
1806 switch (dwc->current_dr_role) {
1807 case DWC3_GCTL_PRTCAP_DEVICE:
1808 ret = dwc3_core_init_for_resume(dwc);
1812 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1813 spin_lock_irqsave(&dwc->lock, flags);
1814 dwc3_gadget_resume(dwc);
1815 spin_unlock_irqrestore(&dwc->lock, flags);
1817 case DWC3_GCTL_PRTCAP_HOST:
1818 if (!PMSG_IS_AUTO(msg)) {
1819 ret = dwc3_core_init_for_resume(dwc);
1822 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1825 /* Restore GUSB2PHYCFG bits that were modified in suspend */
1826 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1827 if (dwc->dis_u2_susphy_quirk)
1828 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1830 if (dwc->dis_enblslpm_quirk)
1831 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
1833 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1835 phy_pm_runtime_get_sync(dwc->usb2_generic_phy);
1836 phy_pm_runtime_get_sync(dwc->usb3_generic_phy);
1838 case DWC3_GCTL_PRTCAP_OTG:
1839 /* nothing to do on runtime_resume */
1840 if (PMSG_IS_AUTO(msg))
1843 ret = dwc3_core_init(dwc);
1847 dwc3_set_prtcap(dwc, dwc->current_dr_role);
1850 if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
1851 dwc3_otg_host_init(dwc);
1852 } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1853 spin_lock_irqsave(&dwc->lock, flags);
1854 dwc3_gadget_resume(dwc);
1855 spin_unlock_irqrestore(&dwc->lock, flags);
1867 static int dwc3_runtime_checks(struct dwc3 *dwc)
1869 switch (dwc->current_dr_role) {
1870 case DWC3_GCTL_PRTCAP_DEVICE:
1874 case DWC3_GCTL_PRTCAP_HOST:
1883 static int dwc3_runtime_suspend(struct device *dev)
1885 struct dwc3 *dwc = dev_get_drvdata(dev);
1888 if (dwc3_runtime_checks(dwc))
1891 ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
1895 device_init_wakeup(dev, true);
1900 static int dwc3_runtime_resume(struct device *dev)
1902 struct dwc3 *dwc = dev_get_drvdata(dev);
1905 device_init_wakeup(dev, false);
1907 ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
1911 switch (dwc->current_dr_role) {
1912 case DWC3_GCTL_PRTCAP_DEVICE:
1913 dwc3_gadget_process_pending_events(dwc);
1915 case DWC3_GCTL_PRTCAP_HOST:
1921 pm_runtime_mark_last_busy(dev);
1926 static int dwc3_runtime_idle(struct device *dev)
1928 struct dwc3 *dwc = dev_get_drvdata(dev);
1930 switch (dwc->current_dr_role) {
1931 case DWC3_GCTL_PRTCAP_DEVICE:
1932 if (dwc3_runtime_checks(dwc))
1935 case DWC3_GCTL_PRTCAP_HOST:
1941 pm_runtime_mark_last_busy(dev);
1942 pm_runtime_autosuspend(dev);
1946 #endif /* CONFIG_PM */
1948 #ifdef CONFIG_PM_SLEEP
1949 static int dwc3_suspend(struct device *dev)
1951 struct dwc3 *dwc = dev_get_drvdata(dev);
1954 ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
1958 pinctrl_pm_select_sleep_state(dev);
1963 static int dwc3_resume(struct device *dev)
1965 struct dwc3 *dwc = dev_get_drvdata(dev);
1968 pinctrl_pm_select_default_state(dev);
1970 ret = dwc3_resume_common(dwc, PMSG_RESUME);
1974 pm_runtime_disable(dev);
1975 pm_runtime_set_active(dev);
1976 pm_runtime_enable(dev);
1980 #endif /* CONFIG_PM_SLEEP */
1982 static const struct dev_pm_ops dwc3_dev_pm_ops = {
1983 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
1984 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
1989 static const struct of_device_id of_dwc3_match[] = {
1991 .compatible = "snps,dwc3"
1994 .compatible = "synopsys,dwc3"
1998 MODULE_DEVICE_TABLE(of, of_dwc3_match);
2003 #define ACPI_ID_INTEL_BSW "808622B7"
2005 static const struct acpi_device_id dwc3_acpi_match[] = {
2006 { ACPI_ID_INTEL_BSW, 0 },
2009 MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
2012 static struct platform_driver dwc3_driver = {
2013 .probe = dwc3_probe,
2014 .remove = dwc3_remove,
2017 .of_match_table = of_match_ptr(of_dwc3_match),
2018 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
2019 .pm = &dwc3_dev_pm_ops,
2023 module_platform_driver(dwc3_driver);
2025 MODULE_ALIAS("platform:dwc3");
2026 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
2027 MODULE_LICENSE("GPL v2");
2028 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");