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
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>
30 #include <linux/usb/ch9.h>
31 #include <linux/usb/gadget.h>
32 #include <linux/usb/of.h>
33 #include <linux/usb/otg.h>
41 #define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
44 * dwc3_get_dr_mode - Validates and sets dr_mode
45 * @dwc: pointer to our context structure
47 static int dwc3_get_dr_mode(struct dwc3 *dwc)
49 enum usb_dr_mode mode;
50 struct device *dev = dwc->dev;
53 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
54 dwc->dr_mode = USB_DR_MODE_OTG;
57 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
60 case DWC3_GHWPARAMS0_MODE_GADGET:
61 if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) {
63 "Controller does not support host mode.\n");
66 mode = USB_DR_MODE_PERIPHERAL;
68 case DWC3_GHWPARAMS0_MODE_HOST:
69 if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
71 "Controller does not support device mode.\n");
74 mode = USB_DR_MODE_HOST;
77 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
78 mode = USB_DR_MODE_HOST;
79 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
80 mode = USB_DR_MODE_PERIPHERAL;
83 * DWC_usb31 and DWC_usb3 v3.30a and higher do not support OTG
84 * mode. If the controller supports DRD but the dr_mode is not
85 * specified or set to OTG, then set the mode to peripheral.
87 if (mode == USB_DR_MODE_OTG &&
88 dwc->revision >= DWC3_REVISION_330A)
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->current_dr_role == DWC3_GCTL_PRTCAP_HOST)
251 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
252 reg |= DWC3_DCTL_CSFTRST;
253 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
256 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
257 if (!(reg & DWC3_DCTL_CSFTRST))
263 phy_exit(dwc->usb3_generic_phy);
264 phy_exit(dwc->usb2_generic_phy);
270 * For DWC_usb31 controller, once DWC3_DCTL_CSFTRST bit is cleared,
271 * we must wait at least 50ms before accessing the PHY domain
272 * (synchronization delay). DWC_usb31 programming guide section 1.3.2.
274 if (dwc3_is_usb31(dwc))
280 static const struct clk_bulk_data dwc3_core_clks[] = {
282 { .id = "bus_early" },
287 * dwc3_frame_length_adjustment - Adjusts frame length if required
288 * @dwc3: Pointer to our controller context structure
290 static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
295 if (dwc->revision < DWC3_REVISION_250A)
301 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
302 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
303 if (!dev_WARN_ONCE(dwc->dev, dft == dwc->fladj,
304 "request value same as default, ignoring\n")) {
305 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
306 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
307 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
312 * dwc3_free_one_event_buffer - Frees one event buffer
313 * @dwc: Pointer to our controller context structure
314 * @evt: Pointer to event buffer to be freed
316 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
317 struct dwc3_event_buffer *evt)
319 dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
323 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
324 * @dwc: Pointer to our controller context structure
325 * @length: size of the event buffer
327 * Returns a pointer to the allocated event buffer structure on success
328 * otherwise ERR_PTR(errno).
330 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
333 struct dwc3_event_buffer *evt;
335 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
337 return ERR_PTR(-ENOMEM);
340 evt->length = length;
341 evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
343 return ERR_PTR(-ENOMEM);
345 evt->buf = dma_alloc_coherent(dwc->sysdev, length,
346 &evt->dma, GFP_KERNEL);
348 return ERR_PTR(-ENOMEM);
354 * dwc3_free_event_buffers - frees all allocated event buffers
355 * @dwc: Pointer to our controller context structure
357 static void dwc3_free_event_buffers(struct dwc3 *dwc)
359 struct dwc3_event_buffer *evt;
363 dwc3_free_one_event_buffer(dwc, evt);
367 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
368 * @dwc: pointer to our controller context structure
369 * @length: size of event buffer
371 * Returns 0 on success otherwise negative errno. In the error case, dwc
372 * may contain some buffers allocated but not all which were requested.
374 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
376 struct dwc3_event_buffer *evt;
378 evt = dwc3_alloc_one_event_buffer(dwc, length);
380 dev_err(dwc->dev, "can't allocate event buffer\n");
389 * dwc3_event_buffers_setup - setup our allocated event buffers
390 * @dwc: pointer to our controller context structure
392 * Returns 0 on success otherwise negative errno.
394 int dwc3_event_buffers_setup(struct dwc3 *dwc)
396 struct dwc3_event_buffer *evt;
400 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
401 lower_32_bits(evt->dma));
402 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
403 upper_32_bits(evt->dma));
404 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
405 DWC3_GEVNTSIZ_SIZE(evt->length));
406 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
411 void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
413 struct dwc3_event_buffer *evt;
419 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
420 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
421 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
422 | DWC3_GEVNTSIZ_SIZE(0));
423 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
426 static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
428 if (!dwc->has_hibernation)
431 if (!dwc->nr_scratch)
434 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
435 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
436 if (!dwc->scratchbuf)
442 static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
444 dma_addr_t scratch_addr;
448 if (!dwc->has_hibernation)
451 if (!dwc->nr_scratch)
454 /* should never fall here */
455 if (!WARN_ON(dwc->scratchbuf))
458 scratch_addr = dma_map_single(dwc->sysdev, dwc->scratchbuf,
459 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
461 if (dma_mapping_error(dwc->sysdev, scratch_addr)) {
462 dev_err(dwc->sysdev, "failed to map scratch buffer\n");
467 dwc->scratch_addr = scratch_addr;
469 param = lower_32_bits(scratch_addr);
471 ret = dwc3_send_gadget_generic_command(dwc,
472 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
476 param = upper_32_bits(scratch_addr);
478 ret = dwc3_send_gadget_generic_command(dwc,
479 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
486 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
487 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
493 static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
495 if (!dwc->has_hibernation)
498 if (!dwc->nr_scratch)
501 /* should never fall here */
502 if (!WARN_ON(dwc->scratchbuf))
505 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
506 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
507 kfree(dwc->scratchbuf);
510 static void dwc3_core_num_eps(struct dwc3 *dwc)
512 struct dwc3_hwparams *parms = &dwc->hwparams;
514 dwc->num_eps = DWC3_NUM_EPS(parms);
517 static void dwc3_cache_hwparams(struct dwc3 *dwc)
519 struct dwc3_hwparams *parms = &dwc->hwparams;
521 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
522 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
523 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
524 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
525 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
526 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
527 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
528 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
529 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
532 static int dwc3_core_ulpi_init(struct dwc3 *dwc)
537 intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
539 if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
540 (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
541 dwc->hsphy_interface &&
542 !strncmp(dwc->hsphy_interface, "ulpi", 4)))
543 ret = dwc3_ulpi_init(dwc);
549 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
550 * @dwc: Pointer to our controller context structure
552 * Returns 0 on success. The USB PHY interfaces are configured but not
553 * initialized. The PHY interfaces and the PHYs get initialized together with
554 * the core in dwc3_core_init.
556 static int dwc3_phy_setup(struct dwc3 *dwc)
560 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
563 * Make sure UX_EXIT_PX is cleared as that causes issues with some
564 * PHYs. Also, this bit is not supposed to be used in normal operation.
566 reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
569 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
570 * to '0' during coreConsultant configuration. So default value
571 * will be '0' when the core is reset. Application needs to set it
572 * to '1' after the core initialization is completed.
574 if (dwc->revision > DWC3_REVISION_194A)
575 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
577 if (dwc->u2ss_inp3_quirk)
578 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
580 if (dwc->dis_rxdet_inp3_quirk)
581 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
583 if (dwc->req_p1p2p3_quirk)
584 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
586 if (dwc->del_p1p2p3_quirk)
587 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
589 if (dwc->del_phy_power_chg_quirk)
590 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
592 if (dwc->lfps_filter_quirk)
593 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
595 if (dwc->rx_detect_poll_quirk)
596 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
598 if (dwc->tx_de_emphasis_quirk)
599 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
601 if (dwc->dis_u3_susphy_quirk)
602 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
604 if (dwc->dis_del_phy_power_chg_quirk)
605 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
607 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
609 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
611 /* Select the HS PHY interface */
612 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
613 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
614 if (dwc->hsphy_interface &&
615 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
616 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
618 } else if (dwc->hsphy_interface &&
619 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
620 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
621 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
623 /* Relying on default value. */
624 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
628 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
634 switch (dwc->hsphy_mode) {
635 case USBPHY_INTERFACE_MODE_UTMI:
636 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
637 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
638 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
639 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
641 case USBPHY_INTERFACE_MODE_UTMIW:
642 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
643 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
644 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
645 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
652 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
653 * '0' during coreConsultant configuration. So default value will
654 * be '0' when the core is reset. Application needs to set it to
655 * '1' after the core initialization is completed.
657 if (dwc->revision > DWC3_REVISION_194A)
658 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
660 if (dwc->dis_u2_susphy_quirk)
661 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
663 if (dwc->dis_enblslpm_quirk)
664 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
666 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM;
668 if (dwc->dis_u2_freeclk_exists_quirk)
669 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
671 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
676 static void dwc3_core_exit(struct dwc3 *dwc)
678 dwc3_event_buffers_cleanup(dwc);
680 usb_phy_shutdown(dwc->usb2_phy);
681 usb_phy_shutdown(dwc->usb3_phy);
682 phy_exit(dwc->usb2_generic_phy);
683 phy_exit(dwc->usb3_generic_phy);
685 usb_phy_set_suspend(dwc->usb2_phy, 1);
686 usb_phy_set_suspend(dwc->usb3_phy, 1);
687 phy_power_off(dwc->usb2_generic_phy);
688 phy_power_off(dwc->usb3_generic_phy);
689 clk_bulk_disable(dwc->num_clks, dwc->clks);
690 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
691 reset_control_assert(dwc->reset);
694 static bool dwc3_core_is_valid(struct dwc3 *dwc)
698 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
700 /* This should read as U3 followed by revision number */
701 if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
702 /* Detected DWC_usb3 IP */
704 } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
705 /* Detected DWC_usb31 IP */
706 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
707 dwc->revision |= DWC3_REVISION_IS_DWC31;
708 dwc->version_type = dwc3_readl(dwc->regs, DWC3_VER_TYPE);
716 static void dwc3_core_setup_global_control(struct dwc3 *dwc)
718 u32 hwparams4 = dwc->hwparams.hwparams4;
721 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
722 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
724 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
725 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
727 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
728 * issue which would cause xHCI compliance tests to fail.
730 * Because of that we cannot enable clock gating on such
735 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
738 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
739 dwc->dr_mode == USB_DR_MODE_OTG) &&
740 (dwc->revision >= DWC3_REVISION_210A &&
741 dwc->revision <= DWC3_REVISION_250A))
742 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
744 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
746 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
747 /* enable hibernation here */
748 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
751 * REVISIT Enabling this bit so that host-mode hibernation
752 * will work. Device-mode hibernation is not yet implemented.
754 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
761 /* check if current dwc3 is on simulation board */
762 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
763 dev_info(dwc->dev, "Running with FPGA optimizations\n");
767 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
768 "disable_scramble cannot be used on non-FPGA builds\n");
770 if (dwc->disable_scramble_quirk && dwc->is_fpga)
771 reg |= DWC3_GCTL_DISSCRAMBLE;
773 reg &= ~DWC3_GCTL_DISSCRAMBLE;
775 if (dwc->u2exit_lfps_quirk)
776 reg |= DWC3_GCTL_U2EXIT_LFPS;
779 * WORKAROUND: DWC3 revisions <1.90a have a bug
780 * where the device can fail to connect at SuperSpeed
781 * and falls back to high-speed mode which causes
782 * the device to enter a Connect/Disconnect loop
784 if (dwc->revision < DWC3_REVISION_190A)
785 reg |= DWC3_GCTL_U2RSTECN;
787 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
790 static int dwc3_core_get_phy(struct dwc3 *dwc);
791 static int dwc3_core_ulpi_init(struct dwc3 *dwc);
793 /* set global incr burst type configuration registers */
794 static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
796 struct device *dev = dwc->dev;
797 /* incrx_mode : for INCR burst type. */
799 /* incrx_size : for size of INCRX burst. */
807 cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
810 * Handle property "snps,incr-burst-type-adjustment".
811 * Get the number of value from this property:
812 * result <= 0, means this property is not supported.
813 * result = 1, means INCRx burst mode supported.
814 * result > 1, means undefined length burst mode supported.
816 ntype = device_property_read_u32_array(dev,
817 "snps,incr-burst-type-adjustment", NULL, 0);
821 vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL);
823 dev_err(dev, "Error to get memory\n");
827 /* Get INCR burst type, and parse it */
828 ret = device_property_read_u32_array(dev,
829 "snps,incr-burst-type-adjustment", vals, ntype);
831 dev_err(dev, "Error to get property\n");
838 /* INCRX (undefined length) burst mode */
839 incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
840 for (i = 1; i < ntype; i++) {
841 if (vals[i] > incrx_size)
842 incrx_size = vals[i];
845 /* INCRX burst mode */
846 incrx_mode = INCRX_BURST_MODE;
849 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
850 cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
852 cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
853 switch (incrx_size) {
855 cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
858 cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
861 cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
864 cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
867 cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
870 cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
873 cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
878 dev_err(dev, "Invalid property\n");
882 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
886 * dwc3_core_init - Low-level initialization of DWC3 Core
887 * @dwc: Pointer to our controller context structure
889 * Returns 0 on success otherwise negative errno.
891 static int dwc3_core_init(struct dwc3 *dwc)
896 if (!dwc3_core_is_valid(dwc)) {
897 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
903 * Write Linux Version Code to our GUID register so it's easy to figure
904 * out which kernel version a bug was found.
906 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
908 /* Handle USB2.0-only core configuration */
909 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
910 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
911 if (dwc->maximum_speed == USB_SPEED_SUPER)
912 dwc->maximum_speed = USB_SPEED_HIGH;
915 ret = dwc3_phy_setup(dwc);
919 if (!dwc->ulpi_ready) {
920 ret = dwc3_core_ulpi_init(dwc);
923 dwc->ulpi_ready = true;
926 if (!dwc->phys_ready) {
927 ret = dwc3_core_get_phy(dwc);
930 dwc->phys_ready = true;
933 ret = dwc3_core_soft_reset(dwc);
937 dwc3_core_setup_global_control(dwc);
938 dwc3_core_num_eps(dwc);
940 ret = dwc3_setup_scratch_buffers(dwc);
944 /* Adjust Frame Length */
945 dwc3_frame_length_adjustment(dwc);
947 dwc3_set_incr_burst_type(dwc);
949 usb_phy_set_suspend(dwc->usb2_phy, 0);
950 usb_phy_set_suspend(dwc->usb3_phy, 0);
951 ret = phy_power_on(dwc->usb2_generic_phy);
955 ret = phy_power_on(dwc->usb3_generic_phy);
959 ret = dwc3_event_buffers_setup(dwc);
961 dev_err(dwc->dev, "failed to setup event buffers\n");
966 * ENDXFER polling is available on version 3.10a and later of
967 * the DWC_usb3 controller. It is NOT available in the
968 * DWC_usb31 controller.
970 if (!dwc3_is_usb31(dwc) && dwc->revision >= DWC3_REVISION_310A) {
971 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
972 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
973 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
976 if (dwc->revision >= DWC3_REVISION_250A) {
977 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
980 * Enable hardware control of sending remote wakeup
981 * in HS when the device is in the L1 state.
983 if (dwc->revision >= DWC3_REVISION_290A)
984 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
986 if (dwc->dis_tx_ipgap_linecheck_quirk)
987 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
989 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
992 if (dwc->dr_mode == USB_DR_MODE_HOST ||
993 dwc->dr_mode == USB_DR_MODE_OTG) {
994 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
997 * Enable Auto retry Feature to make the controller operating in
998 * Host mode on seeing transaction errors(CRC errors or internal
999 * overrun scenerios) on IN transfers to reply to the device
1000 * with a non-terminating retry ACK (i.e, an ACK transcation
1001 * packet with Retry=1 & Nump != 0)
1003 reg |= DWC3_GUCTL_HSTINAUTORETRY;
1005 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
1009 * Must config both number of packets and max burst settings to enable
1010 * RX and/or TX threshold.
1012 if (dwc3_is_usb31(dwc) && dwc->dr_mode == USB_DR_MODE_HOST) {
1013 u8 rx_thr_num = dwc->rx_thr_num_pkt_prd;
1014 u8 rx_maxburst = dwc->rx_max_burst_prd;
1015 u8 tx_thr_num = dwc->tx_thr_num_pkt_prd;
1016 u8 tx_maxburst = dwc->tx_max_burst_prd;
1018 if (rx_thr_num && rx_maxburst) {
1019 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1020 reg |= DWC31_RXTHRNUMPKTSEL_PRD;
1022 reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
1023 reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
1025 reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
1026 reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
1028 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1031 if (tx_thr_num && tx_maxburst) {
1032 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1033 reg |= DWC31_TXTHRNUMPKTSEL_PRD;
1035 reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
1036 reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
1038 reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
1039 reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
1041 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1048 phy_power_off(dwc->usb3_generic_phy);
1051 phy_power_off(dwc->usb2_generic_phy);
1054 usb_phy_set_suspend(dwc->usb2_phy, 1);
1055 usb_phy_set_suspend(dwc->usb3_phy, 1);
1058 usb_phy_shutdown(dwc->usb2_phy);
1059 usb_phy_shutdown(dwc->usb3_phy);
1060 phy_exit(dwc->usb2_generic_phy);
1061 phy_exit(dwc->usb3_generic_phy);
1064 dwc3_ulpi_exit(dwc);
1070 static int dwc3_core_get_phy(struct dwc3 *dwc)
1072 struct device *dev = dwc->dev;
1073 struct device_node *node = dev->of_node;
1077 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
1078 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
1080 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1081 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
1084 if (IS_ERR(dwc->usb2_phy)) {
1085 ret = PTR_ERR(dwc->usb2_phy);
1086 if (ret == -ENXIO || ret == -ENODEV) {
1087 dwc->usb2_phy = NULL;
1088 } else if (ret == -EPROBE_DEFER) {
1091 dev_err(dev, "no usb2 phy configured\n");
1096 if (IS_ERR(dwc->usb3_phy)) {
1097 ret = PTR_ERR(dwc->usb3_phy);
1098 if (ret == -ENXIO || ret == -ENODEV) {
1099 dwc->usb3_phy = NULL;
1100 } else if (ret == -EPROBE_DEFER) {
1103 dev_err(dev, "no usb3 phy configured\n");
1108 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
1109 if (IS_ERR(dwc->usb2_generic_phy)) {
1110 ret = PTR_ERR(dwc->usb2_generic_phy);
1111 if (ret == -ENOSYS || ret == -ENODEV) {
1112 dwc->usb2_generic_phy = NULL;
1113 } else if (ret == -EPROBE_DEFER) {
1116 dev_err(dev, "no usb2 phy configured\n");
1121 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
1122 if (IS_ERR(dwc->usb3_generic_phy)) {
1123 ret = PTR_ERR(dwc->usb3_generic_phy);
1124 if (ret == -ENOSYS || ret == -ENODEV) {
1125 dwc->usb3_generic_phy = NULL;
1126 } else if (ret == -EPROBE_DEFER) {
1129 dev_err(dev, "no usb3 phy configured\n");
1137 static int dwc3_core_init_mode(struct dwc3 *dwc)
1139 struct device *dev = dwc->dev;
1142 switch (dwc->dr_mode) {
1143 case USB_DR_MODE_PERIPHERAL:
1144 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1147 otg_set_vbus(dwc->usb2_phy->otg, false);
1148 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
1149 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
1151 ret = dwc3_gadget_init(dwc);
1153 if (ret != -EPROBE_DEFER)
1154 dev_err(dev, "failed to initialize gadget\n");
1158 case USB_DR_MODE_HOST:
1159 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1162 otg_set_vbus(dwc->usb2_phy->otg, true);
1163 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
1164 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
1166 ret = dwc3_host_init(dwc);
1168 if (ret != -EPROBE_DEFER)
1169 dev_err(dev, "failed to initialize host\n");
1172 phy_calibrate(dwc->usb2_generic_phy);
1174 case USB_DR_MODE_OTG:
1175 INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
1176 ret = dwc3_drd_init(dwc);
1178 if (ret != -EPROBE_DEFER)
1179 dev_err(dev, "failed to initialize dual-role\n");
1184 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1191 static void dwc3_core_exit_mode(struct dwc3 *dwc)
1193 switch (dwc->dr_mode) {
1194 case USB_DR_MODE_PERIPHERAL:
1195 dwc3_gadget_exit(dwc);
1197 case USB_DR_MODE_HOST:
1198 dwc3_host_exit(dwc);
1200 case USB_DR_MODE_OTG:
1209 static void dwc3_get_properties(struct dwc3 *dwc)
1211 struct device *dev = dwc->dev;
1212 u8 lpm_nyet_threshold;
1215 u8 rx_thr_num_pkt_prd;
1216 u8 rx_max_burst_prd;
1217 u8 tx_thr_num_pkt_prd;
1218 u8 tx_max_burst_prd;
1220 /* default to highest possible threshold */
1221 lpm_nyet_threshold = 0xff;
1223 /* default to -3.5dB de-emphasis */
1227 * default to assert utmi_sleep_n and use maximum allowed HIRD
1228 * threshold value of 0b1100
1230 hird_threshold = 12;
1232 dwc->maximum_speed = usb_get_maximum_speed(dev);
1233 dwc->dr_mode = usb_get_dr_mode(dev);
1234 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
1236 dwc->sysdev_is_parent = device_property_read_bool(dev,
1237 "linux,sysdev_is_parent");
1238 if (dwc->sysdev_is_parent)
1239 dwc->sysdev = dwc->dev->parent;
1241 dwc->sysdev = dwc->dev;
1243 dwc->has_lpm_erratum = device_property_read_bool(dev,
1244 "snps,has-lpm-erratum");
1245 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
1246 &lpm_nyet_threshold);
1247 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
1248 "snps,is-utmi-l1-suspend");
1249 device_property_read_u8(dev, "snps,hird-threshold",
1251 dwc->dis_start_transfer_quirk = device_property_read_bool(dev,
1252 "snps,dis-start-transfer-quirk");
1253 dwc->usb3_lpm_capable = device_property_read_bool(dev,
1254 "snps,usb3_lpm_capable");
1255 dwc->usb2_lpm_disable = device_property_read_bool(dev,
1256 "snps,usb2-lpm-disable");
1257 device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
1258 &rx_thr_num_pkt_prd);
1259 device_property_read_u8(dev, "snps,rx-max-burst-prd",
1261 device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
1262 &tx_thr_num_pkt_prd);
1263 device_property_read_u8(dev, "snps,tx-max-burst-prd",
1266 dwc->disable_scramble_quirk = device_property_read_bool(dev,
1267 "snps,disable_scramble_quirk");
1268 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
1269 "snps,u2exit_lfps_quirk");
1270 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
1271 "snps,u2ss_inp3_quirk");
1272 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
1273 "snps,req_p1p2p3_quirk");
1274 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
1275 "snps,del_p1p2p3_quirk");
1276 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
1277 "snps,del_phy_power_chg_quirk");
1278 dwc->lfps_filter_quirk = device_property_read_bool(dev,
1279 "snps,lfps_filter_quirk");
1280 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
1281 "snps,rx_detect_poll_quirk");
1282 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
1283 "snps,dis_u3_susphy_quirk");
1284 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
1285 "snps,dis_u2_susphy_quirk");
1286 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1287 "snps,dis_enblslpm_quirk");
1288 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1289 "snps,dis_rxdet_inp3_quirk");
1290 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1291 "snps,dis-u2-freeclk-exists-quirk");
1292 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1293 "snps,dis-del-phy-power-chg-quirk");
1294 dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1295 "snps,dis-tx-ipgap-linecheck-quirk");
1297 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
1298 "snps,tx_de_emphasis_quirk");
1299 device_property_read_u8(dev, "snps,tx_de_emphasis",
1301 device_property_read_string(dev, "snps,hsphy_interface",
1302 &dwc->hsphy_interface);
1303 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
1306 dwc->dis_metastability_quirk = device_property_read_bool(dev,
1307 "snps,dis_metastability_quirk");
1309 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1310 dwc->tx_de_emphasis = tx_de_emphasis;
1312 dwc->hird_threshold = hird_threshold
1313 | (dwc->is_utmi_l1_suspend << 4);
1315 dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd;
1316 dwc->rx_max_burst_prd = rx_max_burst_prd;
1318 dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd;
1319 dwc->tx_max_burst_prd = tx_max_burst_prd;
1321 dwc->imod_interval = 0;
1324 /* check whether the core supports IMOD */
1325 bool dwc3_has_imod(struct dwc3 *dwc)
1327 return ((dwc3_is_usb3(dwc) &&
1328 dwc->revision >= DWC3_REVISION_300A) ||
1329 (dwc3_is_usb31(dwc) &&
1330 dwc->revision >= DWC3_USB31_REVISION_120A));
1333 static void dwc3_check_params(struct dwc3 *dwc)
1335 struct device *dev = dwc->dev;
1337 /* Check for proper value of imod_interval */
1338 if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
1339 dev_warn(dwc->dev, "Interrupt moderation not supported\n");
1340 dwc->imod_interval = 0;
1344 * Workaround for STAR 9000961433 which affects only version
1345 * 3.00a of the DWC_usb3 core. This prevents the controller
1346 * interrupt from being masked while handling events. IMOD
1347 * allows us to work around this issue. Enable it for the
1350 if (!dwc->imod_interval &&
1351 (dwc->revision == DWC3_REVISION_300A))
1352 dwc->imod_interval = 1;
1354 /* Check the maximum_speed parameter */
1355 switch (dwc->maximum_speed) {
1357 case USB_SPEED_FULL:
1358 case USB_SPEED_HIGH:
1359 case USB_SPEED_SUPER:
1360 case USB_SPEED_SUPER_PLUS:
1363 dev_err(dev, "invalid maximum_speed parameter %d\n",
1364 dwc->maximum_speed);
1366 case USB_SPEED_UNKNOWN:
1367 /* default to superspeed */
1368 dwc->maximum_speed = USB_SPEED_SUPER;
1371 * default to superspeed plus if we are capable.
1373 if (dwc3_is_usb31(dwc) &&
1374 (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
1375 DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1376 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1382 static int dwc3_probe(struct platform_device *pdev)
1384 struct device *dev = &pdev->dev;
1385 struct resource *res, dwc_res;
1392 dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
1396 dwc->clks = devm_kmemdup(dev, dwc3_core_clks, sizeof(dwc3_core_clks),
1403 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1405 dev_err(dev, "missing memory resource\n");
1409 dwc->xhci_resources[0].start = res->start;
1410 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1412 dwc->xhci_resources[0].flags = res->flags;
1413 dwc->xhci_resources[0].name = res->name;
1416 * Request memory region but exclude xHCI regs,
1417 * since it will be requested by the xhci-plat driver.
1420 dwc_res.start += DWC3_GLOBALS_REGS_START;
1422 regs = devm_ioremap_resource(dev, &dwc_res);
1424 return PTR_ERR(regs);
1427 dwc->regs_size = resource_size(&dwc_res);
1429 dwc3_get_properties(dwc);
1431 dwc->reset = devm_reset_control_get_optional_shared(dev, NULL);
1432 if (IS_ERR(dwc->reset))
1433 return PTR_ERR(dwc->reset);
1436 dwc->num_clks = ARRAY_SIZE(dwc3_core_clks);
1438 ret = clk_bulk_get(dev, dwc->num_clks, dwc->clks);
1439 if (ret == -EPROBE_DEFER)
1442 * Clocks are optional, but new DT platforms should support all
1443 * clocks as required by the DT-binding.
1449 ret = reset_control_deassert(dwc->reset);
1453 ret = clk_bulk_prepare(dwc->num_clks, dwc->clks);
1457 ret = clk_bulk_enable(dwc->num_clks, dwc->clks);
1459 goto unprepare_clks;
1461 platform_set_drvdata(pdev, dwc);
1462 dwc3_cache_hwparams(dwc);
1464 spin_lock_init(&dwc->lock);
1466 pm_runtime_set_active(dev);
1467 pm_runtime_use_autosuspend(dev);
1468 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
1469 pm_runtime_enable(dev);
1470 ret = pm_runtime_get_sync(dev);
1474 pm_runtime_forbid(dev);
1476 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1478 dev_err(dwc->dev, "failed to allocate event buffers\n");
1483 ret = dwc3_get_dr_mode(dwc);
1487 ret = dwc3_alloc_scratch_buffers(dwc);
1491 ret = dwc3_core_init(dwc);
1493 if (ret != -EPROBE_DEFER)
1494 dev_err(dev, "failed to initialize core: %d\n", ret);
1498 dwc3_check_params(dwc);
1500 ret = dwc3_core_init_mode(dwc);
1504 dwc3_debugfs_init(dwc);
1505 pm_runtime_put(dev);
1510 dwc3_event_buffers_cleanup(dwc);
1511 dwc3_ulpi_exit(dwc);
1514 dwc3_free_scratch_buffers(dwc);
1517 dwc3_free_event_buffers(dwc);
1520 pm_runtime_allow(&pdev->dev);
1523 pm_runtime_put_sync(&pdev->dev);
1524 pm_runtime_disable(&pdev->dev);
1526 clk_bulk_disable(dwc->num_clks, dwc->clks);
1528 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
1530 reset_control_assert(dwc->reset);
1532 clk_bulk_put(dwc->num_clks, dwc->clks);
1537 static int dwc3_remove(struct platform_device *pdev)
1539 struct dwc3 *dwc = platform_get_drvdata(pdev);
1541 pm_runtime_get_sync(&pdev->dev);
1543 dwc3_debugfs_exit(dwc);
1544 dwc3_core_exit_mode(dwc);
1546 dwc3_core_exit(dwc);
1547 dwc3_ulpi_exit(dwc);
1549 pm_runtime_put_sync(&pdev->dev);
1550 pm_runtime_allow(&pdev->dev);
1551 pm_runtime_disable(&pdev->dev);
1553 dwc3_free_event_buffers(dwc);
1554 dwc3_free_scratch_buffers(dwc);
1555 clk_bulk_put(dwc->num_clks, dwc->clks);
1561 static int dwc3_core_init_for_resume(struct dwc3 *dwc)
1565 ret = reset_control_deassert(dwc->reset);
1569 ret = clk_bulk_prepare(dwc->num_clks, dwc->clks);
1573 ret = clk_bulk_enable(dwc->num_clks, dwc->clks);
1575 goto unprepare_clks;
1577 ret = dwc3_core_init(dwc);
1584 clk_bulk_disable(dwc->num_clks, dwc->clks);
1586 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
1588 reset_control_assert(dwc->reset);
1593 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
1595 unsigned long flags;
1598 switch (dwc->current_dr_role) {
1599 case DWC3_GCTL_PRTCAP_DEVICE:
1600 spin_lock_irqsave(&dwc->lock, flags);
1601 dwc3_gadget_suspend(dwc);
1602 spin_unlock_irqrestore(&dwc->lock, flags);
1603 dwc3_core_exit(dwc);
1605 case DWC3_GCTL_PRTCAP_HOST:
1606 if (!PMSG_IS_AUTO(msg)) {
1607 dwc3_core_exit(dwc);
1611 /* Let controller to suspend HSPHY before PHY driver suspends */
1612 if (dwc->dis_u2_susphy_quirk ||
1613 dwc->dis_enblslpm_quirk) {
1614 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1615 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
1616 DWC3_GUSB2PHYCFG_SUSPHY;
1617 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1619 /* Give some time for USB2 PHY to suspend */
1620 usleep_range(5000, 6000);
1623 phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
1624 phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
1626 case DWC3_GCTL_PRTCAP_OTG:
1627 /* do nothing during runtime_suspend */
1628 if (PMSG_IS_AUTO(msg))
1631 if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1632 spin_lock_irqsave(&dwc->lock, flags);
1633 dwc3_gadget_suspend(dwc);
1634 spin_unlock_irqrestore(&dwc->lock, flags);
1638 dwc3_core_exit(dwc);
1648 static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
1650 unsigned long flags;
1654 switch (dwc->current_dr_role) {
1655 case DWC3_GCTL_PRTCAP_DEVICE:
1656 ret = dwc3_core_init_for_resume(dwc);
1660 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1661 spin_lock_irqsave(&dwc->lock, flags);
1662 dwc3_gadget_resume(dwc);
1663 spin_unlock_irqrestore(&dwc->lock, flags);
1665 case DWC3_GCTL_PRTCAP_HOST:
1666 if (!PMSG_IS_AUTO(msg)) {
1667 ret = dwc3_core_init_for_resume(dwc);
1670 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1673 /* Restore GUSB2PHYCFG bits that were modified in suspend */
1674 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1675 if (dwc->dis_u2_susphy_quirk)
1676 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1678 if (dwc->dis_enblslpm_quirk)
1679 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
1681 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1683 phy_pm_runtime_get_sync(dwc->usb2_generic_phy);
1684 phy_pm_runtime_get_sync(dwc->usb3_generic_phy);
1686 case DWC3_GCTL_PRTCAP_OTG:
1687 /* nothing to do on runtime_resume */
1688 if (PMSG_IS_AUTO(msg))
1691 ret = dwc3_core_init(dwc);
1695 dwc3_set_prtcap(dwc, dwc->current_dr_role);
1698 if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
1699 dwc3_otg_host_init(dwc);
1700 } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1701 spin_lock_irqsave(&dwc->lock, flags);
1702 dwc3_gadget_resume(dwc);
1703 spin_unlock_irqrestore(&dwc->lock, flags);
1715 static int dwc3_runtime_checks(struct dwc3 *dwc)
1717 switch (dwc->current_dr_role) {
1718 case DWC3_GCTL_PRTCAP_DEVICE:
1722 case DWC3_GCTL_PRTCAP_HOST:
1731 static int dwc3_runtime_suspend(struct device *dev)
1733 struct dwc3 *dwc = dev_get_drvdata(dev);
1736 if (dwc3_runtime_checks(dwc))
1739 ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
1743 device_init_wakeup(dev, true);
1748 static int dwc3_runtime_resume(struct device *dev)
1750 struct dwc3 *dwc = dev_get_drvdata(dev);
1753 device_init_wakeup(dev, false);
1755 ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
1759 switch (dwc->current_dr_role) {
1760 case DWC3_GCTL_PRTCAP_DEVICE:
1761 dwc3_gadget_process_pending_events(dwc);
1763 case DWC3_GCTL_PRTCAP_HOST:
1769 pm_runtime_mark_last_busy(dev);
1774 static int dwc3_runtime_idle(struct device *dev)
1776 struct dwc3 *dwc = dev_get_drvdata(dev);
1778 switch (dwc->current_dr_role) {
1779 case DWC3_GCTL_PRTCAP_DEVICE:
1780 if (dwc3_runtime_checks(dwc))
1783 case DWC3_GCTL_PRTCAP_HOST:
1789 pm_runtime_mark_last_busy(dev);
1790 pm_runtime_autosuspend(dev);
1794 #endif /* CONFIG_PM */
1796 #ifdef CONFIG_PM_SLEEP
1797 static int dwc3_suspend(struct device *dev)
1799 struct dwc3 *dwc = dev_get_drvdata(dev);
1802 ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
1806 pinctrl_pm_select_sleep_state(dev);
1811 static int dwc3_resume(struct device *dev)
1813 struct dwc3 *dwc = dev_get_drvdata(dev);
1816 pinctrl_pm_select_default_state(dev);
1818 ret = dwc3_resume_common(dwc, PMSG_RESUME);
1822 pm_runtime_disable(dev);
1823 pm_runtime_set_active(dev);
1824 pm_runtime_enable(dev);
1828 #endif /* CONFIG_PM_SLEEP */
1830 static const struct dev_pm_ops dwc3_dev_pm_ops = {
1831 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
1832 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
1837 static const struct of_device_id of_dwc3_match[] = {
1839 .compatible = "snps,dwc3"
1842 .compatible = "synopsys,dwc3"
1846 MODULE_DEVICE_TABLE(of, of_dwc3_match);
1851 #define ACPI_ID_INTEL_BSW "808622B7"
1853 static const struct acpi_device_id dwc3_acpi_match[] = {
1854 { ACPI_ID_INTEL_BSW, 0 },
1857 MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
1860 static struct platform_driver dwc3_driver = {
1861 .probe = dwc3_probe,
1862 .remove = dwc3_remove,
1865 .of_match_table = of_match_ptr(of_dwc3_match),
1866 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
1867 .pm = &dwc3_dev_pm_ops,
1871 module_platform_driver(dwc3_driver);
1873 MODULE_ALIAS("platform:dwc3");
1875 MODULE_LICENSE("GPL v2");
1876 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");