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/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 does not support OTG mode. If the controller
84 * supports DRD but the dr_mode is not specified or set to OTG,
85 * then set the mode to peripheral.
87 if (mode == USB_DR_MODE_OTG && dwc3_is_usb31(dwc))
88 mode = USB_DR_MODE_PERIPHERAL;
91 if (mode != dwc->dr_mode) {
93 "Configuration mismatch. dr_mode forced to %s\n",
94 mode == USB_DR_MODE_HOST ? "host" : "gadget");
102 void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
106 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
107 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
108 reg |= DWC3_GCTL_PRTCAPDIR(mode);
109 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
111 dwc->current_dr_role = mode;
114 static void __dwc3_set_mode(struct work_struct *work)
116 struct dwc3 *dwc = work_to_dwc(work);
120 if (dwc->dr_mode != USB_DR_MODE_OTG)
123 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
124 dwc3_otg_update(dwc, 0);
126 if (!dwc->desired_dr_role)
129 if (dwc->desired_dr_role == dwc->current_dr_role)
132 if (dwc->desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev)
135 switch (dwc->current_dr_role) {
136 case DWC3_GCTL_PRTCAP_HOST:
139 case DWC3_GCTL_PRTCAP_DEVICE:
140 dwc3_gadget_exit(dwc);
141 dwc3_event_buffers_cleanup(dwc);
143 case DWC3_GCTL_PRTCAP_OTG:
145 spin_lock_irqsave(&dwc->lock, flags);
146 dwc->desired_otg_role = DWC3_OTG_ROLE_IDLE;
147 spin_unlock_irqrestore(&dwc->lock, flags);
148 dwc3_otg_update(dwc, 1);
154 spin_lock_irqsave(&dwc->lock, flags);
156 dwc3_set_prtcap(dwc, dwc->desired_dr_role);
158 spin_unlock_irqrestore(&dwc->lock, flags);
160 switch (dwc->desired_dr_role) {
161 case DWC3_GCTL_PRTCAP_HOST:
162 ret = dwc3_host_init(dwc);
164 dev_err(dwc->dev, "failed to initialize host\n");
167 otg_set_vbus(dwc->usb2_phy->otg, true);
168 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
169 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
170 phy_calibrate(dwc->usb2_generic_phy);
173 case DWC3_GCTL_PRTCAP_DEVICE:
174 dwc3_event_buffers_setup(dwc);
177 otg_set_vbus(dwc->usb2_phy->otg, false);
178 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
179 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
181 ret = dwc3_gadget_init(dwc);
183 dev_err(dwc->dev, "failed to initialize peripheral\n");
185 case DWC3_GCTL_PRTCAP_OTG:
187 dwc3_otg_update(dwc, 0);
195 void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
199 spin_lock_irqsave(&dwc->lock, flags);
200 dwc->desired_dr_role = mode;
201 spin_unlock_irqrestore(&dwc->lock, flags);
203 queue_work(system_freezable_wq, &dwc->drd_work);
206 u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
208 struct dwc3 *dwc = dep->dwc;
211 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
212 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
213 DWC3_GDBGFIFOSPACE_TYPE(type));
215 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
217 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
221 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
222 * @dwc: pointer to our context structure
224 static int dwc3_core_soft_reset(struct dwc3 *dwc)
230 usb_phy_init(dwc->usb2_phy);
231 usb_phy_init(dwc->usb3_phy);
232 ret = phy_init(dwc->usb2_generic_phy);
236 ret = phy_init(dwc->usb3_generic_phy);
238 phy_exit(dwc->usb2_generic_phy);
243 * We're resetting only the device side because, if we're in host mode,
244 * XHCI driver will reset the host block. If dwc3 was configured for
245 * host-only mode, then we can return early.
247 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST)
250 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
251 reg |= DWC3_DCTL_CSFTRST;
252 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
255 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
256 if (!(reg & DWC3_DCTL_CSFTRST))
262 phy_exit(dwc->usb3_generic_phy);
263 phy_exit(dwc->usb2_generic_phy);
269 * For DWC_usb31 controller, once DWC3_DCTL_CSFTRST bit is cleared,
270 * we must wait at least 50ms before accessing the PHY domain
271 * (synchronization delay). DWC_usb31 programming guide section 1.3.2.
273 if (dwc3_is_usb31(dwc))
279 static const struct clk_bulk_data dwc3_core_clks[] = {
281 { .id = "bus_early" },
286 * dwc3_frame_length_adjustment - Adjusts frame length if required
287 * @dwc3: Pointer to our controller context structure
289 static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
294 if (dwc->revision < DWC3_REVISION_250A)
300 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
301 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
302 if (dft != dwc->fladj) {
303 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
304 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
305 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
310 * dwc3_free_one_event_buffer - Frees one event buffer
311 * @dwc: Pointer to our controller context structure
312 * @evt: Pointer to event buffer to be freed
314 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
315 struct dwc3_event_buffer *evt)
317 dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
321 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
322 * @dwc: Pointer to our controller context structure
323 * @length: size of the event buffer
325 * Returns a pointer to the allocated event buffer structure on success
326 * otherwise ERR_PTR(errno).
328 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
331 struct dwc3_event_buffer *evt;
333 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
335 return ERR_PTR(-ENOMEM);
338 evt->length = length;
339 evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
341 return ERR_PTR(-ENOMEM);
343 evt->buf = dma_alloc_coherent(dwc->sysdev, length,
344 &evt->dma, GFP_KERNEL);
346 return ERR_PTR(-ENOMEM);
352 * dwc3_free_event_buffers - frees all allocated event buffers
353 * @dwc: Pointer to our controller context structure
355 static void dwc3_free_event_buffers(struct dwc3 *dwc)
357 struct dwc3_event_buffer *evt;
361 dwc3_free_one_event_buffer(dwc, evt);
365 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
366 * @dwc: pointer to our controller context structure
367 * @length: size of event buffer
369 * Returns 0 on success otherwise negative errno. In the error case, dwc
370 * may contain some buffers allocated but not all which were requested.
372 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
374 struct dwc3_event_buffer *evt;
376 evt = dwc3_alloc_one_event_buffer(dwc, length);
378 dev_err(dwc->dev, "can't allocate event buffer\n");
387 * dwc3_event_buffers_setup - setup our allocated event buffers
388 * @dwc: pointer to our controller context structure
390 * Returns 0 on success otherwise negative errno.
392 int dwc3_event_buffers_setup(struct dwc3 *dwc)
394 struct dwc3_event_buffer *evt;
398 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
399 lower_32_bits(evt->dma));
400 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
401 upper_32_bits(evt->dma));
402 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
403 DWC3_GEVNTSIZ_SIZE(evt->length));
404 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
409 void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
411 struct dwc3_event_buffer *evt;
417 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
418 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
419 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
420 | DWC3_GEVNTSIZ_SIZE(0));
421 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
424 static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
426 if (!dwc->has_hibernation)
429 if (!dwc->nr_scratch)
432 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
433 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
434 if (!dwc->scratchbuf)
440 static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
442 dma_addr_t scratch_addr;
446 if (!dwc->has_hibernation)
449 if (!dwc->nr_scratch)
452 /* should never fall here */
453 if (!WARN_ON(dwc->scratchbuf))
456 scratch_addr = dma_map_single(dwc->sysdev, dwc->scratchbuf,
457 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
459 if (dma_mapping_error(dwc->sysdev, scratch_addr)) {
460 dev_err(dwc->sysdev, "failed to map scratch buffer\n");
465 dwc->scratch_addr = scratch_addr;
467 param = lower_32_bits(scratch_addr);
469 ret = dwc3_send_gadget_generic_command(dwc,
470 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
474 param = upper_32_bits(scratch_addr);
476 ret = dwc3_send_gadget_generic_command(dwc,
477 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
484 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
485 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
491 static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
493 if (!dwc->has_hibernation)
496 if (!dwc->nr_scratch)
499 /* should never fall here */
500 if (!WARN_ON(dwc->scratchbuf))
503 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
504 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
505 kfree(dwc->scratchbuf);
508 static void dwc3_core_num_eps(struct dwc3 *dwc)
510 struct dwc3_hwparams *parms = &dwc->hwparams;
512 dwc->num_eps = DWC3_NUM_EPS(parms);
515 static void dwc3_cache_hwparams(struct dwc3 *dwc)
517 struct dwc3_hwparams *parms = &dwc->hwparams;
519 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
520 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
521 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
522 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
523 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
524 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
525 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
526 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
527 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
530 static int dwc3_core_ulpi_init(struct dwc3 *dwc)
535 intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
537 if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
538 (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
539 dwc->hsphy_interface &&
540 !strncmp(dwc->hsphy_interface, "ulpi", 4)))
541 ret = dwc3_ulpi_init(dwc);
547 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
548 * @dwc: Pointer to our controller context structure
550 * Returns 0 on success. The USB PHY interfaces are configured but not
551 * initialized. The PHY interfaces and the PHYs get initialized together with
552 * the core in dwc3_core_init.
554 static int dwc3_phy_setup(struct dwc3 *dwc)
558 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
561 * Make sure UX_EXIT_PX is cleared as that causes issues with some
562 * PHYs. Also, this bit is not supposed to be used in normal operation.
564 reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
567 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
568 * to '0' during coreConsultant configuration. So default value
569 * will be '0' when the core is reset. Application needs to set it
570 * to '1' after the core initialization is completed.
572 if (dwc->revision > DWC3_REVISION_194A)
573 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
575 if (dwc->u2ss_inp3_quirk)
576 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
578 if (dwc->dis_rxdet_inp3_quirk)
579 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
581 if (dwc->req_p1p2p3_quirk)
582 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
584 if (dwc->del_p1p2p3_quirk)
585 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
587 if (dwc->del_phy_power_chg_quirk)
588 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
590 if (dwc->lfps_filter_quirk)
591 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
593 if (dwc->rx_detect_poll_quirk)
594 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
596 if (dwc->tx_de_emphasis_quirk)
597 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
599 if (dwc->dis_u3_susphy_quirk)
600 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
602 if (dwc->dis_del_phy_power_chg_quirk)
603 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
605 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
607 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
609 /* Select the HS PHY interface */
610 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
611 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
612 if (dwc->hsphy_interface &&
613 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
614 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
616 } else if (dwc->hsphy_interface &&
617 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
618 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
619 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
621 /* Relying on default value. */
622 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
626 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
632 switch (dwc->hsphy_mode) {
633 case USBPHY_INTERFACE_MODE_UTMI:
634 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
635 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
636 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
637 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
639 case USBPHY_INTERFACE_MODE_UTMIW:
640 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
641 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
642 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
643 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
650 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
651 * '0' during coreConsultant configuration. So default value will
652 * be '0' when the core is reset. Application needs to set it to
653 * '1' after the core initialization is completed.
655 if (dwc->revision > DWC3_REVISION_194A)
656 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
658 if (dwc->dis_u2_susphy_quirk)
659 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
661 if (dwc->dis_enblslpm_quirk)
662 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
664 if (dwc->dis_u2_freeclk_exists_quirk)
665 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
667 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
672 static void dwc3_core_exit(struct dwc3 *dwc)
674 dwc3_event_buffers_cleanup(dwc);
676 usb_phy_set_suspend(dwc->usb2_phy, 1);
677 usb_phy_set_suspend(dwc->usb3_phy, 1);
678 phy_power_off(dwc->usb2_generic_phy);
679 phy_power_off(dwc->usb3_generic_phy);
681 usb_phy_shutdown(dwc->usb2_phy);
682 usb_phy_shutdown(dwc->usb3_phy);
683 phy_exit(dwc->usb2_generic_phy);
684 phy_exit(dwc->usb3_generic_phy);
686 clk_bulk_disable(dwc->num_clks, dwc->clks);
687 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
688 reset_control_assert(dwc->reset);
691 static bool dwc3_core_is_valid(struct dwc3 *dwc)
695 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
697 /* This should read as U3 followed by revision number */
698 if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
699 /* Detected DWC_usb3 IP */
701 } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
702 /* Detected DWC_usb31 IP */
703 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
704 dwc->revision |= DWC3_REVISION_IS_DWC31;
712 static void dwc3_core_setup_global_control(struct dwc3 *dwc)
714 u32 hwparams4 = dwc->hwparams.hwparams4;
717 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
718 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
720 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
721 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
723 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
724 * issue which would cause xHCI compliance tests to fail.
726 * Because of that we cannot enable clock gating on such
731 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
734 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
735 dwc->dr_mode == USB_DR_MODE_OTG) &&
736 (dwc->revision >= DWC3_REVISION_210A &&
737 dwc->revision <= DWC3_REVISION_250A))
738 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
740 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
742 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
743 /* enable hibernation here */
744 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
747 * REVISIT Enabling this bit so that host-mode hibernation
748 * will work. Device-mode hibernation is not yet implemented.
750 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
757 /* check if current dwc3 is on simulation board */
758 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
759 dev_info(dwc->dev, "Running with FPGA optmizations\n");
763 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
764 "disable_scramble cannot be used on non-FPGA builds\n");
766 if (dwc->disable_scramble_quirk && dwc->is_fpga)
767 reg |= DWC3_GCTL_DISSCRAMBLE;
769 reg &= ~DWC3_GCTL_DISSCRAMBLE;
771 if (dwc->u2exit_lfps_quirk)
772 reg |= DWC3_GCTL_U2EXIT_LFPS;
775 * WORKAROUND: DWC3 revisions <1.90a have a bug
776 * where the device can fail to connect at SuperSpeed
777 * and falls back to high-speed mode which causes
778 * the device to enter a Connect/Disconnect loop
780 if (dwc->revision < DWC3_REVISION_190A)
781 reg |= DWC3_GCTL_U2RSTECN;
783 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
786 static int dwc3_core_get_phy(struct dwc3 *dwc);
787 static int dwc3_core_ulpi_init(struct dwc3 *dwc);
789 /* set global incr burst type configuration registers */
790 static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
792 struct device *dev = dwc->dev;
793 /* incrx_mode : for INCR burst type. */
795 /* incrx_size : for size of INCRX burst. */
803 cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
806 * Handle property "snps,incr-burst-type-adjustment".
807 * Get the number of value from this property:
808 * result <= 0, means this property is not supported.
809 * result = 1, means INCRx burst mode supported.
810 * result > 1, means undefined length burst mode supported.
812 ntype = device_property_read_u32_array(dev,
813 "snps,incr-burst-type-adjustment", NULL, 0);
817 vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL);
819 dev_err(dev, "Error to get memory\n");
823 /* Get INCR burst type, and parse it */
824 ret = device_property_read_u32_array(dev,
825 "snps,incr-burst-type-adjustment", vals, ntype);
827 dev_err(dev, "Error to get property\n");
834 /* INCRX (undefined length) burst mode */
835 incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
836 for (i = 1; i < ntype; i++) {
837 if (vals[i] > incrx_size)
838 incrx_size = vals[i];
841 /* INCRX burst mode */
842 incrx_mode = INCRX_BURST_MODE;
845 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
846 cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
848 cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
849 switch (incrx_size) {
851 cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
854 cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
857 cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
860 cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
863 cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
866 cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
869 cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
874 dev_err(dev, "Invalid property\n");
878 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
882 * dwc3_core_init - Low-level initialization of DWC3 Core
883 * @dwc: Pointer to our controller context structure
885 * Returns 0 on success otherwise negative errno.
887 static int dwc3_core_init(struct dwc3 *dwc)
892 if (!dwc3_core_is_valid(dwc)) {
893 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
899 * Write Linux Version Code to our GUID register so it's easy to figure
900 * out which kernel version a bug was found.
902 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
904 /* Handle USB2.0-only core configuration */
905 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
906 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
907 if (dwc->maximum_speed == USB_SPEED_SUPER)
908 dwc->maximum_speed = USB_SPEED_HIGH;
911 ret = dwc3_phy_setup(dwc);
915 if (!dwc->ulpi_ready) {
916 ret = dwc3_core_ulpi_init(dwc);
919 dwc->ulpi_ready = true;
922 if (!dwc->phys_ready) {
923 ret = dwc3_core_get_phy(dwc);
926 dwc->phys_ready = true;
929 ret = dwc3_core_soft_reset(dwc);
933 dwc3_core_setup_global_control(dwc);
934 dwc3_core_num_eps(dwc);
936 ret = dwc3_setup_scratch_buffers(dwc);
940 /* Adjust Frame Length */
941 dwc3_frame_length_adjustment(dwc);
943 dwc3_set_incr_burst_type(dwc);
945 usb_phy_set_suspend(dwc->usb2_phy, 0);
946 usb_phy_set_suspend(dwc->usb3_phy, 0);
947 ret = phy_power_on(dwc->usb2_generic_phy);
951 ret = phy_power_on(dwc->usb3_generic_phy);
955 ret = dwc3_event_buffers_setup(dwc);
957 dev_err(dwc->dev, "failed to setup event buffers\n");
962 * ENDXFER polling is available on version 3.10a and later of
963 * the DWC_usb3 controller. It is NOT available in the
964 * DWC_usb31 controller.
966 if (!dwc3_is_usb31(dwc) && dwc->revision >= DWC3_REVISION_310A) {
967 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
968 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
969 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
972 if (dwc->revision >= DWC3_REVISION_250A) {
973 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
976 * Enable hardware control of sending remote wakeup
977 * in HS when the device is in the L1 state.
979 if (dwc->revision >= DWC3_REVISION_290A)
980 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
982 if (dwc->dis_tx_ipgap_linecheck_quirk)
983 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
985 if (dwc->parkmode_disable_ss_quirk)
986 reg |= DWC3_GUCTL1_PARKMODE_DISABLE_SS;
988 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
991 if (dwc->dr_mode == USB_DR_MODE_HOST ||
992 dwc->dr_mode == USB_DR_MODE_OTG) {
993 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
996 * Enable Auto retry Feature to make the controller operating in
997 * Host mode on seeing transaction errors(CRC errors or internal
998 * overrun scenerios) on IN transfers to reply to the device
999 * with a non-terminating retry ACK (i.e, an ACK transcation
1000 * packet with Retry=1 & Nump != 0)
1002 reg |= DWC3_GUCTL_HSTINAUTORETRY;
1004 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
1008 * Must config both number of packets and max burst settings to enable
1009 * RX and/or TX threshold.
1011 if (dwc3_is_usb31(dwc) && dwc->dr_mode == USB_DR_MODE_HOST) {
1012 u8 rx_thr_num = dwc->rx_thr_num_pkt_prd;
1013 u8 rx_maxburst = dwc->rx_max_burst_prd;
1014 u8 tx_thr_num = dwc->tx_thr_num_pkt_prd;
1015 u8 tx_maxburst = dwc->tx_max_burst_prd;
1017 if (rx_thr_num && rx_maxburst) {
1018 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1019 reg |= DWC31_RXTHRNUMPKTSEL_PRD;
1021 reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
1022 reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
1024 reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
1025 reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
1027 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1030 if (tx_thr_num && tx_maxburst) {
1031 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1032 reg |= DWC31_TXTHRNUMPKTSEL_PRD;
1034 reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
1035 reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
1037 reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
1038 reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
1040 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1047 phy_power_off(dwc->usb3_generic_phy);
1050 phy_power_off(dwc->usb2_generic_phy);
1053 usb_phy_set_suspend(dwc->usb2_phy, 1);
1054 usb_phy_set_suspend(dwc->usb3_phy, 1);
1057 usb_phy_shutdown(dwc->usb2_phy);
1058 usb_phy_shutdown(dwc->usb3_phy);
1059 phy_exit(dwc->usb2_generic_phy);
1060 phy_exit(dwc->usb3_generic_phy);
1063 dwc3_ulpi_exit(dwc);
1069 static int dwc3_core_get_phy(struct dwc3 *dwc)
1071 struct device *dev = dwc->dev;
1072 struct device_node *node = dev->of_node;
1076 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
1077 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
1079 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1080 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
1083 if (IS_ERR(dwc->usb2_phy)) {
1084 ret = PTR_ERR(dwc->usb2_phy);
1085 if (ret == -ENXIO || ret == -ENODEV) {
1086 dwc->usb2_phy = NULL;
1087 } else if (ret == -EPROBE_DEFER) {
1090 dev_err(dev, "no usb2 phy configured\n");
1095 if (IS_ERR(dwc->usb3_phy)) {
1096 ret = PTR_ERR(dwc->usb3_phy);
1097 if (ret == -ENXIO || ret == -ENODEV) {
1098 dwc->usb3_phy = NULL;
1099 } else if (ret == -EPROBE_DEFER) {
1102 dev_err(dev, "no usb3 phy configured\n");
1107 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
1108 if (IS_ERR(dwc->usb2_generic_phy)) {
1109 ret = PTR_ERR(dwc->usb2_generic_phy);
1110 if (ret == -ENOSYS || ret == -ENODEV) {
1111 dwc->usb2_generic_phy = NULL;
1112 } else if (ret == -EPROBE_DEFER) {
1115 dev_err(dev, "no usb2 phy configured\n");
1120 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
1121 if (IS_ERR(dwc->usb3_generic_phy)) {
1122 ret = PTR_ERR(dwc->usb3_generic_phy);
1123 if (ret == -ENOSYS || ret == -ENODEV) {
1124 dwc->usb3_generic_phy = NULL;
1125 } else if (ret == -EPROBE_DEFER) {
1128 dev_err(dev, "no usb3 phy configured\n");
1136 static int dwc3_core_init_mode(struct dwc3 *dwc)
1138 struct device *dev = dwc->dev;
1141 switch (dwc->dr_mode) {
1142 case USB_DR_MODE_PERIPHERAL:
1143 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1146 otg_set_vbus(dwc->usb2_phy->otg, false);
1147 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
1148 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
1150 ret = dwc3_gadget_init(dwc);
1152 if (ret != -EPROBE_DEFER)
1153 dev_err(dev, "failed to initialize gadget\n");
1157 case USB_DR_MODE_HOST:
1158 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1161 otg_set_vbus(dwc->usb2_phy->otg, true);
1162 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
1163 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
1165 ret = dwc3_host_init(dwc);
1167 if (ret != -EPROBE_DEFER)
1168 dev_err(dev, "failed to initialize host\n");
1171 phy_calibrate(dwc->usb2_generic_phy);
1173 case USB_DR_MODE_OTG:
1174 INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
1175 ret = dwc3_drd_init(dwc);
1177 if (ret != -EPROBE_DEFER)
1178 dev_err(dev, "failed to initialize dual-role\n");
1183 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1190 static void dwc3_core_exit_mode(struct dwc3 *dwc)
1192 switch (dwc->dr_mode) {
1193 case USB_DR_MODE_PERIPHERAL:
1194 dwc3_gadget_exit(dwc);
1196 case USB_DR_MODE_HOST:
1197 dwc3_host_exit(dwc);
1199 case USB_DR_MODE_OTG:
1207 /* de-assert DRVVBUS for HOST and OTG mode */
1208 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1211 static void dwc3_get_properties(struct dwc3 *dwc)
1213 struct device *dev = dwc->dev;
1214 u8 lpm_nyet_threshold;
1217 u8 rx_thr_num_pkt_prd = 0;
1218 u8 rx_max_burst_prd = 0;
1219 u8 tx_thr_num_pkt_prd = 0;
1220 u8 tx_max_burst_prd = 0;
1222 /* default to highest possible threshold */
1223 lpm_nyet_threshold = 0xf;
1225 /* default to -3.5dB de-emphasis */
1229 * default to assert utmi_sleep_n and use maximum allowed HIRD
1230 * threshold value of 0b1100
1232 hird_threshold = 12;
1234 dwc->maximum_speed = usb_get_maximum_speed(dev);
1235 dwc->dr_mode = usb_get_dr_mode(dev);
1236 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
1238 dwc->sysdev_is_parent = device_property_read_bool(dev,
1239 "linux,sysdev_is_parent");
1240 if (dwc->sysdev_is_parent)
1241 dwc->sysdev = dwc->dev->parent;
1243 dwc->sysdev = dwc->dev;
1245 dwc->has_lpm_erratum = device_property_read_bool(dev,
1246 "snps,has-lpm-erratum");
1247 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
1248 &lpm_nyet_threshold);
1249 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
1250 "snps,is-utmi-l1-suspend");
1251 device_property_read_u8(dev, "snps,hird-threshold",
1253 dwc->usb3_lpm_capable = device_property_read_bool(dev,
1254 "snps,usb3_lpm_capable");
1255 device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
1256 &rx_thr_num_pkt_prd);
1257 device_property_read_u8(dev, "snps,rx-max-burst-prd",
1259 device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
1260 &tx_thr_num_pkt_prd);
1261 device_property_read_u8(dev, "snps,tx-max-burst-prd",
1264 dwc->disable_scramble_quirk = device_property_read_bool(dev,
1265 "snps,disable_scramble_quirk");
1266 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
1267 "snps,u2exit_lfps_quirk");
1268 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
1269 "snps,u2ss_inp3_quirk");
1270 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
1271 "snps,req_p1p2p3_quirk");
1272 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
1273 "snps,del_p1p2p3_quirk");
1274 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
1275 "snps,del_phy_power_chg_quirk");
1276 dwc->lfps_filter_quirk = device_property_read_bool(dev,
1277 "snps,lfps_filter_quirk");
1278 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
1279 "snps,rx_detect_poll_quirk");
1280 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
1281 "snps,dis_u3_susphy_quirk");
1282 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
1283 "snps,dis_u2_susphy_quirk");
1284 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1285 "snps,dis_enblslpm_quirk");
1286 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1287 "snps,dis_rxdet_inp3_quirk");
1288 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1289 "snps,dis-u2-freeclk-exists-quirk");
1290 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1291 "snps,dis-del-phy-power-chg-quirk");
1292 dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1293 "snps,dis-tx-ipgap-linecheck-quirk");
1294 dwc->parkmode_disable_ss_quirk = device_property_read_bool(dev,
1295 "snps,parkmode-disable-ss-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);
1499 dwc3_debugfs_init(dwc);
1501 ret = dwc3_core_init_mode(dwc);
1505 pm_runtime_put(dev);
1510 dwc3_debugfs_exit(dwc);
1511 dwc3_event_buffers_cleanup(dwc);
1513 usb_phy_set_suspend(dwc->usb2_phy, 1);
1514 usb_phy_set_suspend(dwc->usb3_phy, 1);
1515 phy_power_off(dwc->usb2_generic_phy);
1516 phy_power_off(dwc->usb3_generic_phy);
1518 usb_phy_shutdown(dwc->usb2_phy);
1519 usb_phy_shutdown(dwc->usb3_phy);
1520 phy_exit(dwc->usb2_generic_phy);
1521 phy_exit(dwc->usb3_generic_phy);
1523 dwc3_ulpi_exit(dwc);
1526 dwc3_free_scratch_buffers(dwc);
1529 dwc3_free_event_buffers(dwc);
1532 pm_runtime_allow(&pdev->dev);
1535 pm_runtime_put_sync(&pdev->dev);
1536 pm_runtime_disable(&pdev->dev);
1538 clk_bulk_disable(dwc->num_clks, dwc->clks);
1540 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
1542 reset_control_assert(dwc->reset);
1544 clk_bulk_put(dwc->num_clks, dwc->clks);
1549 static int dwc3_remove(struct platform_device *pdev)
1551 struct dwc3 *dwc = platform_get_drvdata(pdev);
1553 pm_runtime_get_sync(&pdev->dev);
1555 dwc3_core_exit_mode(dwc);
1556 dwc3_debugfs_exit(dwc);
1558 dwc3_core_exit(dwc);
1559 dwc3_ulpi_exit(dwc);
1561 pm_runtime_disable(&pdev->dev);
1562 pm_runtime_put_noidle(&pdev->dev);
1563 pm_runtime_set_suspended(&pdev->dev);
1565 dwc3_free_event_buffers(dwc);
1566 dwc3_free_scratch_buffers(dwc);
1567 clk_bulk_put(dwc->num_clks, dwc->clks);
1573 static int dwc3_core_init_for_resume(struct dwc3 *dwc)
1577 ret = reset_control_deassert(dwc->reset);
1581 ret = clk_bulk_prepare(dwc->num_clks, dwc->clks);
1585 ret = clk_bulk_enable(dwc->num_clks, dwc->clks);
1587 goto unprepare_clks;
1589 ret = dwc3_core_init(dwc);
1596 clk_bulk_disable(dwc->num_clks, dwc->clks);
1598 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
1600 reset_control_assert(dwc->reset);
1605 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
1607 unsigned long flags;
1610 switch (dwc->current_dr_role) {
1611 case DWC3_GCTL_PRTCAP_DEVICE:
1612 spin_lock_irqsave(&dwc->lock, flags);
1613 dwc3_gadget_suspend(dwc);
1614 spin_unlock_irqrestore(&dwc->lock, flags);
1615 synchronize_irq(dwc->irq_gadget);
1616 dwc3_core_exit(dwc);
1618 case DWC3_GCTL_PRTCAP_HOST:
1619 if (!PMSG_IS_AUTO(msg)) {
1620 dwc3_core_exit(dwc);
1624 /* Let controller to suspend HSPHY before PHY driver suspends */
1625 if (dwc->dis_u2_susphy_quirk ||
1626 dwc->dis_enblslpm_quirk) {
1627 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1628 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
1629 DWC3_GUSB2PHYCFG_SUSPHY;
1630 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1632 /* Give some time for USB2 PHY to suspend */
1633 usleep_range(5000, 6000);
1636 phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
1637 phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
1639 case DWC3_GCTL_PRTCAP_OTG:
1640 /* do nothing during runtime_suspend */
1641 if (PMSG_IS_AUTO(msg))
1644 if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1645 spin_lock_irqsave(&dwc->lock, flags);
1646 dwc3_gadget_suspend(dwc);
1647 spin_unlock_irqrestore(&dwc->lock, flags);
1648 synchronize_irq(dwc->irq_gadget);
1652 dwc3_core_exit(dwc);
1662 static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
1664 unsigned long flags;
1668 switch (dwc->current_dr_role) {
1669 case DWC3_GCTL_PRTCAP_DEVICE:
1670 ret = dwc3_core_init_for_resume(dwc);
1674 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1675 spin_lock_irqsave(&dwc->lock, flags);
1676 dwc3_gadget_resume(dwc);
1677 spin_unlock_irqrestore(&dwc->lock, flags);
1679 case DWC3_GCTL_PRTCAP_HOST:
1680 if (!PMSG_IS_AUTO(msg)) {
1681 ret = dwc3_core_init_for_resume(dwc);
1684 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1687 /* Restore GUSB2PHYCFG bits that were modified in suspend */
1688 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1689 if (dwc->dis_u2_susphy_quirk)
1690 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1692 if (dwc->dis_enblslpm_quirk)
1693 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
1695 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1697 phy_pm_runtime_get_sync(dwc->usb2_generic_phy);
1698 phy_pm_runtime_get_sync(dwc->usb3_generic_phy);
1700 case DWC3_GCTL_PRTCAP_OTG:
1701 /* nothing to do on runtime_resume */
1702 if (PMSG_IS_AUTO(msg))
1705 ret = dwc3_core_init_for_resume(dwc);
1709 dwc3_set_prtcap(dwc, dwc->current_dr_role);
1712 if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
1713 dwc3_otg_host_init(dwc);
1714 } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1715 spin_lock_irqsave(&dwc->lock, flags);
1716 dwc3_gadget_resume(dwc);
1717 spin_unlock_irqrestore(&dwc->lock, flags);
1729 static int dwc3_runtime_checks(struct dwc3 *dwc)
1731 switch (dwc->current_dr_role) {
1732 case DWC3_GCTL_PRTCAP_DEVICE:
1736 case DWC3_GCTL_PRTCAP_HOST:
1745 static int dwc3_runtime_suspend(struct device *dev)
1747 struct dwc3 *dwc = dev_get_drvdata(dev);
1750 if (dwc3_runtime_checks(dwc))
1753 ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
1757 device_init_wakeup(dev, true);
1762 static int dwc3_runtime_resume(struct device *dev)
1764 struct dwc3 *dwc = dev_get_drvdata(dev);
1767 device_init_wakeup(dev, false);
1769 ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
1773 switch (dwc->current_dr_role) {
1774 case DWC3_GCTL_PRTCAP_DEVICE:
1775 dwc3_gadget_process_pending_events(dwc);
1777 case DWC3_GCTL_PRTCAP_HOST:
1783 pm_runtime_mark_last_busy(dev);
1788 static int dwc3_runtime_idle(struct device *dev)
1790 struct dwc3 *dwc = dev_get_drvdata(dev);
1792 switch (dwc->current_dr_role) {
1793 case DWC3_GCTL_PRTCAP_DEVICE:
1794 if (dwc3_runtime_checks(dwc))
1797 case DWC3_GCTL_PRTCAP_HOST:
1803 pm_runtime_mark_last_busy(dev);
1804 pm_runtime_autosuspend(dev);
1808 #endif /* CONFIG_PM */
1810 #ifdef CONFIG_PM_SLEEP
1811 static int dwc3_suspend(struct device *dev)
1813 struct dwc3 *dwc = dev_get_drvdata(dev);
1816 ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
1820 pinctrl_pm_select_sleep_state(dev);
1825 static int dwc3_resume(struct device *dev)
1827 struct dwc3 *dwc = dev_get_drvdata(dev);
1830 pinctrl_pm_select_default_state(dev);
1832 ret = dwc3_resume_common(dwc, PMSG_RESUME);
1836 pm_runtime_disable(dev);
1837 pm_runtime_set_active(dev);
1838 pm_runtime_enable(dev);
1842 #endif /* CONFIG_PM_SLEEP */
1844 static const struct dev_pm_ops dwc3_dev_pm_ops = {
1845 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
1846 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
1851 static const struct of_device_id of_dwc3_match[] = {
1853 .compatible = "snps,dwc3"
1856 .compatible = "synopsys,dwc3"
1860 MODULE_DEVICE_TABLE(of, of_dwc3_match);
1865 #define ACPI_ID_INTEL_BSW "808622B7"
1867 static const struct acpi_device_id dwc3_acpi_match[] = {
1868 { ACPI_ID_INTEL_BSW, 0 },
1871 MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
1874 static struct platform_driver dwc3_driver = {
1875 .probe = dwc3_probe,
1876 .remove = dwc3_remove,
1879 .of_match_table = of_match_ptr(of_dwc3_match),
1880 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
1881 .pm = &dwc3_dev_pm_ops,
1885 module_platform_driver(dwc3_driver);
1887 MODULE_ALIAS("platform:dwc3");
1888 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1889 MODULE_LICENSE("GPL v2");
1890 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");