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_shutdown(dwc->usb2_phy);
677 usb_phy_shutdown(dwc->usb3_phy);
678 phy_exit(dwc->usb2_generic_phy);
679 phy_exit(dwc->usb3_generic_phy);
681 usb_phy_set_suspend(dwc->usb2_phy, 1);
682 usb_phy_set_suspend(dwc->usb3_phy, 1);
683 phy_power_off(dwc->usb2_generic_phy);
684 phy_power_off(dwc->usb3_generic_phy);
685 clk_bulk_disable(dwc->num_clks, dwc->clks);
686 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
687 reset_control_assert(dwc->reset);
690 static bool dwc3_core_is_valid(struct dwc3 *dwc)
694 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
696 /* This should read as U3 followed by revision number */
697 if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
698 /* Detected DWC_usb3 IP */
700 } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
701 /* Detected DWC_usb31 IP */
702 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
703 dwc->revision |= DWC3_REVISION_IS_DWC31;
711 static void dwc3_core_setup_global_control(struct dwc3 *dwc)
713 u32 hwparams4 = dwc->hwparams.hwparams4;
716 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
717 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
719 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
720 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
722 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
723 * issue which would cause xHCI compliance tests to fail.
725 * Because of that we cannot enable clock gating on such
730 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
733 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
734 dwc->dr_mode == USB_DR_MODE_OTG) &&
735 (dwc->revision >= DWC3_REVISION_210A &&
736 dwc->revision <= DWC3_REVISION_250A))
737 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
739 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
741 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
742 /* enable hibernation here */
743 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
746 * REVISIT Enabling this bit so that host-mode hibernation
747 * will work. Device-mode hibernation is not yet implemented.
749 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
756 /* check if current dwc3 is on simulation board */
757 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
758 dev_info(dwc->dev, "Running with FPGA optmizations\n");
762 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
763 "disable_scramble cannot be used on non-FPGA builds\n");
765 if (dwc->disable_scramble_quirk && dwc->is_fpga)
766 reg |= DWC3_GCTL_DISSCRAMBLE;
768 reg &= ~DWC3_GCTL_DISSCRAMBLE;
770 if (dwc->u2exit_lfps_quirk)
771 reg |= DWC3_GCTL_U2EXIT_LFPS;
774 * WORKAROUND: DWC3 revisions <1.90a have a bug
775 * where the device can fail to connect at SuperSpeed
776 * and falls back to high-speed mode which causes
777 * the device to enter a Connect/Disconnect loop
779 if (dwc->revision < DWC3_REVISION_190A)
780 reg |= DWC3_GCTL_U2RSTECN;
782 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
785 static int dwc3_core_get_phy(struct dwc3 *dwc);
786 static int dwc3_core_ulpi_init(struct dwc3 *dwc);
788 /* set global incr burst type configuration registers */
789 static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
791 struct device *dev = dwc->dev;
792 /* incrx_mode : for INCR burst type. */
794 /* incrx_size : for size of INCRX burst. */
802 cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
805 * Handle property "snps,incr-burst-type-adjustment".
806 * Get the number of value from this property:
807 * result <= 0, means this property is not supported.
808 * result = 1, means INCRx burst mode supported.
809 * result > 1, means undefined length burst mode supported.
811 ntype = device_property_read_u32_array(dev,
812 "snps,incr-burst-type-adjustment", NULL, 0);
816 vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL);
818 dev_err(dev, "Error to get memory\n");
822 /* Get INCR burst type, and parse it */
823 ret = device_property_read_u32_array(dev,
824 "snps,incr-burst-type-adjustment", vals, ntype);
826 dev_err(dev, "Error to get property\n");
833 /* INCRX (undefined length) burst mode */
834 incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
835 for (i = 1; i < ntype; i++) {
836 if (vals[i] > incrx_size)
837 incrx_size = vals[i];
840 /* INCRX burst mode */
841 incrx_mode = INCRX_BURST_MODE;
844 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
845 cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
847 cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
848 switch (incrx_size) {
850 cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
853 cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
856 cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
859 cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
862 cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
865 cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
868 cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
873 dev_err(dev, "Invalid property\n");
877 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
881 * dwc3_core_init - Low-level initialization of DWC3 Core
882 * @dwc: Pointer to our controller context structure
884 * Returns 0 on success otherwise negative errno.
886 static int dwc3_core_init(struct dwc3 *dwc)
891 if (!dwc3_core_is_valid(dwc)) {
892 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
898 * Write Linux Version Code to our GUID register so it's easy to figure
899 * out which kernel version a bug was found.
901 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
903 /* Handle USB2.0-only core configuration */
904 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
905 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
906 if (dwc->maximum_speed == USB_SPEED_SUPER)
907 dwc->maximum_speed = USB_SPEED_HIGH;
910 ret = dwc3_phy_setup(dwc);
914 if (!dwc->ulpi_ready) {
915 ret = dwc3_core_ulpi_init(dwc);
918 dwc->ulpi_ready = true;
921 if (!dwc->phys_ready) {
922 ret = dwc3_core_get_phy(dwc);
925 dwc->phys_ready = true;
928 ret = dwc3_core_soft_reset(dwc);
932 dwc3_core_setup_global_control(dwc);
933 dwc3_core_num_eps(dwc);
935 ret = dwc3_setup_scratch_buffers(dwc);
939 /* Adjust Frame Length */
940 dwc3_frame_length_adjustment(dwc);
942 dwc3_set_incr_burst_type(dwc);
944 usb_phy_set_suspend(dwc->usb2_phy, 0);
945 usb_phy_set_suspend(dwc->usb3_phy, 0);
946 ret = phy_power_on(dwc->usb2_generic_phy);
950 ret = phy_power_on(dwc->usb3_generic_phy);
954 ret = dwc3_event_buffers_setup(dwc);
956 dev_err(dwc->dev, "failed to setup event buffers\n");
961 * ENDXFER polling is available on version 3.10a and later of
962 * the DWC_usb3 controller. It is NOT available in the
963 * DWC_usb31 controller.
965 if (!dwc3_is_usb31(dwc) && dwc->revision >= DWC3_REVISION_310A) {
966 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
967 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
968 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
971 if (dwc->revision >= DWC3_REVISION_250A) {
972 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
975 * Enable hardware control of sending remote wakeup
976 * in HS when the device is in the L1 state.
978 if (dwc->revision >= DWC3_REVISION_290A)
979 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
981 if (dwc->dis_tx_ipgap_linecheck_quirk)
982 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
984 if (dwc->parkmode_disable_ss_quirk)
985 reg |= DWC3_GUCTL1_PARKMODE_DISABLE_SS;
987 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
990 if (dwc->dr_mode == USB_DR_MODE_HOST ||
991 dwc->dr_mode == USB_DR_MODE_OTG) {
992 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
995 * Enable Auto retry Feature to make the controller operating in
996 * Host mode on seeing transaction errors(CRC errors or internal
997 * overrun scenerios) on IN transfers to reply to the device
998 * with a non-terminating retry ACK (i.e, an ACK transcation
999 * packet with Retry=1 & Nump != 0)
1001 reg |= DWC3_GUCTL_HSTINAUTORETRY;
1003 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
1007 * Must config both number of packets and max burst settings to enable
1008 * RX and/or TX threshold.
1010 if (dwc3_is_usb31(dwc) && dwc->dr_mode == USB_DR_MODE_HOST) {
1011 u8 rx_thr_num = dwc->rx_thr_num_pkt_prd;
1012 u8 rx_maxburst = dwc->rx_max_burst_prd;
1013 u8 tx_thr_num = dwc->tx_thr_num_pkt_prd;
1014 u8 tx_maxburst = dwc->tx_max_burst_prd;
1016 if (rx_thr_num && rx_maxburst) {
1017 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1018 reg |= DWC31_RXTHRNUMPKTSEL_PRD;
1020 reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
1021 reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
1023 reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
1024 reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
1026 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1029 if (tx_thr_num && tx_maxburst) {
1030 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1031 reg |= DWC31_TXTHRNUMPKTSEL_PRD;
1033 reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
1034 reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
1036 reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
1037 reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
1039 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1046 phy_power_off(dwc->usb3_generic_phy);
1049 phy_power_off(dwc->usb2_generic_phy);
1052 usb_phy_set_suspend(dwc->usb2_phy, 1);
1053 usb_phy_set_suspend(dwc->usb3_phy, 1);
1056 usb_phy_shutdown(dwc->usb2_phy);
1057 usb_phy_shutdown(dwc->usb3_phy);
1058 phy_exit(dwc->usb2_generic_phy);
1059 phy_exit(dwc->usb3_generic_phy);
1062 dwc3_ulpi_exit(dwc);
1068 static int dwc3_core_get_phy(struct dwc3 *dwc)
1070 struct device *dev = dwc->dev;
1071 struct device_node *node = dev->of_node;
1075 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
1076 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
1078 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1079 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
1082 if (IS_ERR(dwc->usb2_phy)) {
1083 ret = PTR_ERR(dwc->usb2_phy);
1084 if (ret == -ENXIO || ret == -ENODEV) {
1085 dwc->usb2_phy = NULL;
1086 } else if (ret == -EPROBE_DEFER) {
1089 dev_err(dev, "no usb2 phy configured\n");
1094 if (IS_ERR(dwc->usb3_phy)) {
1095 ret = PTR_ERR(dwc->usb3_phy);
1096 if (ret == -ENXIO || ret == -ENODEV) {
1097 dwc->usb3_phy = NULL;
1098 } else if (ret == -EPROBE_DEFER) {
1101 dev_err(dev, "no usb3 phy configured\n");
1106 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
1107 if (IS_ERR(dwc->usb2_generic_phy)) {
1108 ret = PTR_ERR(dwc->usb2_generic_phy);
1109 if (ret == -ENOSYS || ret == -ENODEV) {
1110 dwc->usb2_generic_phy = NULL;
1111 } else if (ret == -EPROBE_DEFER) {
1114 dev_err(dev, "no usb2 phy configured\n");
1119 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
1120 if (IS_ERR(dwc->usb3_generic_phy)) {
1121 ret = PTR_ERR(dwc->usb3_generic_phy);
1122 if (ret == -ENOSYS || ret == -ENODEV) {
1123 dwc->usb3_generic_phy = NULL;
1124 } else if (ret == -EPROBE_DEFER) {
1127 dev_err(dev, "no usb3 phy configured\n");
1135 static int dwc3_core_init_mode(struct dwc3 *dwc)
1137 struct device *dev = dwc->dev;
1140 switch (dwc->dr_mode) {
1141 case USB_DR_MODE_PERIPHERAL:
1142 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1145 otg_set_vbus(dwc->usb2_phy->otg, false);
1146 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
1147 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
1149 ret = dwc3_gadget_init(dwc);
1151 if (ret != -EPROBE_DEFER)
1152 dev_err(dev, "failed to initialize gadget\n");
1156 case USB_DR_MODE_HOST:
1157 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1160 otg_set_vbus(dwc->usb2_phy->otg, true);
1161 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
1162 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
1164 ret = dwc3_host_init(dwc);
1166 if (ret != -EPROBE_DEFER)
1167 dev_err(dev, "failed to initialize host\n");
1170 phy_calibrate(dwc->usb2_generic_phy);
1172 case USB_DR_MODE_OTG:
1173 INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
1174 ret = dwc3_drd_init(dwc);
1176 if (ret != -EPROBE_DEFER)
1177 dev_err(dev, "failed to initialize dual-role\n");
1182 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1189 static void dwc3_core_exit_mode(struct dwc3 *dwc)
1191 switch (dwc->dr_mode) {
1192 case USB_DR_MODE_PERIPHERAL:
1193 dwc3_gadget_exit(dwc);
1195 case USB_DR_MODE_HOST:
1196 dwc3_host_exit(dwc);
1198 case USB_DR_MODE_OTG:
1206 /* de-assert DRVVBUS for HOST and OTG mode */
1207 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1210 static void dwc3_get_properties(struct dwc3 *dwc)
1212 struct device *dev = dwc->dev;
1213 u8 lpm_nyet_threshold;
1216 u8 rx_thr_num_pkt_prd;
1217 u8 rx_max_burst_prd;
1218 u8 tx_thr_num_pkt_prd;
1219 u8 tx_max_burst_prd;
1221 /* default to highest possible threshold */
1222 lpm_nyet_threshold = 0xf;
1224 /* default to -3.5dB de-emphasis */
1228 * default to assert utmi_sleep_n and use maximum allowed HIRD
1229 * threshold value of 0b1100
1231 hird_threshold = 12;
1233 dwc->maximum_speed = usb_get_maximum_speed(dev);
1234 dwc->dr_mode = usb_get_dr_mode(dev);
1235 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
1237 dwc->sysdev_is_parent = device_property_read_bool(dev,
1238 "linux,sysdev_is_parent");
1239 if (dwc->sysdev_is_parent)
1240 dwc->sysdev = dwc->dev->parent;
1242 dwc->sysdev = dwc->dev;
1244 dwc->has_lpm_erratum = device_property_read_bool(dev,
1245 "snps,has-lpm-erratum");
1246 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
1247 &lpm_nyet_threshold);
1248 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
1249 "snps,is-utmi-l1-suspend");
1250 device_property_read_u8(dev, "snps,hird-threshold",
1252 dwc->usb3_lpm_capable = device_property_read_bool(dev,
1253 "snps,usb3_lpm_capable");
1254 device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
1255 &rx_thr_num_pkt_prd);
1256 device_property_read_u8(dev, "snps,rx-max-burst-prd",
1258 device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
1259 &tx_thr_num_pkt_prd);
1260 device_property_read_u8(dev, "snps,tx-max-burst-prd",
1263 dwc->disable_scramble_quirk = device_property_read_bool(dev,
1264 "snps,disable_scramble_quirk");
1265 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
1266 "snps,u2exit_lfps_quirk");
1267 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
1268 "snps,u2ss_inp3_quirk");
1269 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
1270 "snps,req_p1p2p3_quirk");
1271 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
1272 "snps,del_p1p2p3_quirk");
1273 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
1274 "snps,del_phy_power_chg_quirk");
1275 dwc->lfps_filter_quirk = device_property_read_bool(dev,
1276 "snps,lfps_filter_quirk");
1277 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
1278 "snps,rx_detect_poll_quirk");
1279 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
1280 "snps,dis_u3_susphy_quirk");
1281 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
1282 "snps,dis_u2_susphy_quirk");
1283 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1284 "snps,dis_enblslpm_quirk");
1285 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1286 "snps,dis_rxdet_inp3_quirk");
1287 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1288 "snps,dis-u2-freeclk-exists-quirk");
1289 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1290 "snps,dis-del-phy-power-chg-quirk");
1291 dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1292 "snps,dis-tx-ipgap-linecheck-quirk");
1293 dwc->parkmode_disable_ss_quirk = device_property_read_bool(dev,
1294 "snps,parkmode-disable-ss-quirk");
1296 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
1297 "snps,tx_de_emphasis_quirk");
1298 device_property_read_u8(dev, "snps,tx_de_emphasis",
1300 device_property_read_string(dev, "snps,hsphy_interface",
1301 &dwc->hsphy_interface);
1302 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
1305 dwc->dis_metastability_quirk = device_property_read_bool(dev,
1306 "snps,dis_metastability_quirk");
1308 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1309 dwc->tx_de_emphasis = tx_de_emphasis;
1311 dwc->hird_threshold = hird_threshold
1312 | (dwc->is_utmi_l1_suspend << 4);
1314 dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd;
1315 dwc->rx_max_burst_prd = rx_max_burst_prd;
1317 dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd;
1318 dwc->tx_max_burst_prd = tx_max_burst_prd;
1320 dwc->imod_interval = 0;
1323 /* check whether the core supports IMOD */
1324 bool dwc3_has_imod(struct dwc3 *dwc)
1326 return ((dwc3_is_usb3(dwc) &&
1327 dwc->revision >= DWC3_REVISION_300A) ||
1328 (dwc3_is_usb31(dwc) &&
1329 dwc->revision >= DWC3_USB31_REVISION_120A));
1332 static void dwc3_check_params(struct dwc3 *dwc)
1334 struct device *dev = dwc->dev;
1336 /* Check for proper value of imod_interval */
1337 if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
1338 dev_warn(dwc->dev, "Interrupt moderation not supported\n");
1339 dwc->imod_interval = 0;
1343 * Workaround for STAR 9000961433 which affects only version
1344 * 3.00a of the DWC_usb3 core. This prevents the controller
1345 * interrupt from being masked while handling events. IMOD
1346 * allows us to work around this issue. Enable it for the
1349 if (!dwc->imod_interval &&
1350 (dwc->revision == DWC3_REVISION_300A))
1351 dwc->imod_interval = 1;
1353 /* Check the maximum_speed parameter */
1354 switch (dwc->maximum_speed) {
1356 case USB_SPEED_FULL:
1357 case USB_SPEED_HIGH:
1358 case USB_SPEED_SUPER:
1359 case USB_SPEED_SUPER_PLUS:
1362 dev_err(dev, "invalid maximum_speed parameter %d\n",
1363 dwc->maximum_speed);
1365 case USB_SPEED_UNKNOWN:
1366 /* default to superspeed */
1367 dwc->maximum_speed = USB_SPEED_SUPER;
1370 * default to superspeed plus if we are capable.
1372 if (dwc3_is_usb31(dwc) &&
1373 (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
1374 DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1375 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1381 static int dwc3_probe(struct platform_device *pdev)
1383 struct device *dev = &pdev->dev;
1384 struct resource *res, dwc_res;
1391 dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
1395 dwc->clks = devm_kmemdup(dev, dwc3_core_clks, sizeof(dwc3_core_clks),
1402 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1404 dev_err(dev, "missing memory resource\n");
1408 dwc->xhci_resources[0].start = res->start;
1409 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1411 dwc->xhci_resources[0].flags = res->flags;
1412 dwc->xhci_resources[0].name = res->name;
1415 * Request memory region but exclude xHCI regs,
1416 * since it will be requested by the xhci-plat driver.
1419 dwc_res.start += DWC3_GLOBALS_REGS_START;
1421 regs = devm_ioremap_resource(dev, &dwc_res);
1423 return PTR_ERR(regs);
1426 dwc->regs_size = resource_size(&dwc_res);
1428 dwc3_get_properties(dwc);
1430 dwc->reset = devm_reset_control_get_optional_shared(dev, NULL);
1431 if (IS_ERR(dwc->reset))
1432 return PTR_ERR(dwc->reset);
1435 dwc->num_clks = ARRAY_SIZE(dwc3_core_clks);
1437 ret = clk_bulk_get(dev, dwc->num_clks, dwc->clks);
1438 if (ret == -EPROBE_DEFER)
1441 * Clocks are optional, but new DT platforms should support all
1442 * clocks as required by the DT-binding.
1448 ret = reset_control_deassert(dwc->reset);
1452 ret = clk_bulk_prepare(dwc->num_clks, dwc->clks);
1456 ret = clk_bulk_enable(dwc->num_clks, dwc->clks);
1458 goto unprepare_clks;
1460 platform_set_drvdata(pdev, dwc);
1461 dwc3_cache_hwparams(dwc);
1463 spin_lock_init(&dwc->lock);
1465 pm_runtime_set_active(dev);
1466 pm_runtime_use_autosuspend(dev);
1467 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
1468 pm_runtime_enable(dev);
1469 ret = pm_runtime_get_sync(dev);
1473 pm_runtime_forbid(dev);
1475 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1477 dev_err(dwc->dev, "failed to allocate event buffers\n");
1482 ret = dwc3_get_dr_mode(dwc);
1486 ret = dwc3_alloc_scratch_buffers(dwc);
1490 ret = dwc3_core_init(dwc);
1492 if (ret != -EPROBE_DEFER)
1493 dev_err(dev, "failed to initialize core: %d\n", ret);
1497 dwc3_check_params(dwc);
1498 dwc3_debugfs_init(dwc);
1500 ret = dwc3_core_init_mode(dwc);
1504 pm_runtime_put(dev);
1509 dwc3_debugfs_exit(dwc);
1510 dwc3_event_buffers_cleanup(dwc);
1512 usb_phy_shutdown(dwc->usb2_phy);
1513 usb_phy_shutdown(dwc->usb3_phy);
1514 phy_exit(dwc->usb2_generic_phy);
1515 phy_exit(dwc->usb3_generic_phy);
1517 usb_phy_set_suspend(dwc->usb2_phy, 1);
1518 usb_phy_set_suspend(dwc->usb3_phy, 1);
1519 phy_power_off(dwc->usb2_generic_phy);
1520 phy_power_off(dwc->usb3_generic_phy);
1522 dwc3_ulpi_exit(dwc);
1525 dwc3_free_scratch_buffers(dwc);
1528 dwc3_free_event_buffers(dwc);
1531 pm_runtime_allow(&pdev->dev);
1534 pm_runtime_put_sync(&pdev->dev);
1535 pm_runtime_disable(&pdev->dev);
1537 clk_bulk_disable(dwc->num_clks, dwc->clks);
1539 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
1541 reset_control_assert(dwc->reset);
1543 clk_bulk_put(dwc->num_clks, dwc->clks);
1548 static int dwc3_remove(struct platform_device *pdev)
1550 struct dwc3 *dwc = platform_get_drvdata(pdev);
1552 pm_runtime_get_sync(&pdev->dev);
1554 dwc3_core_exit_mode(dwc);
1555 dwc3_debugfs_exit(dwc);
1557 dwc3_core_exit(dwc);
1558 dwc3_ulpi_exit(dwc);
1560 pm_runtime_disable(&pdev->dev);
1561 pm_runtime_put_noidle(&pdev->dev);
1562 pm_runtime_set_suspended(&pdev->dev);
1564 dwc3_free_event_buffers(dwc);
1565 dwc3_free_scratch_buffers(dwc);
1566 clk_bulk_put(dwc->num_clks, dwc->clks);
1572 static int dwc3_core_init_for_resume(struct dwc3 *dwc)
1576 ret = reset_control_deassert(dwc->reset);
1580 ret = clk_bulk_prepare(dwc->num_clks, dwc->clks);
1584 ret = clk_bulk_enable(dwc->num_clks, dwc->clks);
1586 goto unprepare_clks;
1588 ret = dwc3_core_init(dwc);
1595 clk_bulk_disable(dwc->num_clks, dwc->clks);
1597 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
1599 reset_control_assert(dwc->reset);
1604 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
1606 unsigned long flags;
1609 switch (dwc->current_dr_role) {
1610 case DWC3_GCTL_PRTCAP_DEVICE:
1611 spin_lock_irqsave(&dwc->lock, flags);
1612 dwc3_gadget_suspend(dwc);
1613 spin_unlock_irqrestore(&dwc->lock, flags);
1614 synchronize_irq(dwc->irq_gadget);
1615 dwc3_core_exit(dwc);
1617 case DWC3_GCTL_PRTCAP_HOST:
1618 if (!PMSG_IS_AUTO(msg)) {
1619 dwc3_core_exit(dwc);
1623 /* Let controller to suspend HSPHY before PHY driver suspends */
1624 if (dwc->dis_u2_susphy_quirk ||
1625 dwc->dis_enblslpm_quirk) {
1626 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1627 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
1628 DWC3_GUSB2PHYCFG_SUSPHY;
1629 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1631 /* Give some time for USB2 PHY to suspend */
1632 usleep_range(5000, 6000);
1635 phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
1636 phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
1638 case DWC3_GCTL_PRTCAP_OTG:
1639 /* do nothing during runtime_suspend */
1640 if (PMSG_IS_AUTO(msg))
1643 if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1644 spin_lock_irqsave(&dwc->lock, flags);
1645 dwc3_gadget_suspend(dwc);
1646 spin_unlock_irqrestore(&dwc->lock, flags);
1647 synchronize_irq(dwc->irq_gadget);
1651 dwc3_core_exit(dwc);
1661 static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
1663 unsigned long flags;
1667 switch (dwc->current_dr_role) {
1668 case DWC3_GCTL_PRTCAP_DEVICE:
1669 ret = dwc3_core_init_for_resume(dwc);
1673 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1674 spin_lock_irqsave(&dwc->lock, flags);
1675 dwc3_gadget_resume(dwc);
1676 spin_unlock_irqrestore(&dwc->lock, flags);
1678 case DWC3_GCTL_PRTCAP_HOST:
1679 if (!PMSG_IS_AUTO(msg)) {
1680 ret = dwc3_core_init_for_resume(dwc);
1683 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1686 /* Restore GUSB2PHYCFG bits that were modified in suspend */
1687 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1688 if (dwc->dis_u2_susphy_quirk)
1689 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1691 if (dwc->dis_enblslpm_quirk)
1692 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
1694 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1696 phy_pm_runtime_get_sync(dwc->usb2_generic_phy);
1697 phy_pm_runtime_get_sync(dwc->usb3_generic_phy);
1699 case DWC3_GCTL_PRTCAP_OTG:
1700 /* nothing to do on runtime_resume */
1701 if (PMSG_IS_AUTO(msg))
1704 ret = dwc3_core_init_for_resume(dwc);
1708 dwc3_set_prtcap(dwc, dwc->current_dr_role);
1711 if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
1712 dwc3_otg_host_init(dwc);
1713 } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1714 spin_lock_irqsave(&dwc->lock, flags);
1715 dwc3_gadget_resume(dwc);
1716 spin_unlock_irqrestore(&dwc->lock, flags);
1728 static int dwc3_runtime_checks(struct dwc3 *dwc)
1730 switch (dwc->current_dr_role) {
1731 case DWC3_GCTL_PRTCAP_DEVICE:
1735 case DWC3_GCTL_PRTCAP_HOST:
1744 static int dwc3_runtime_suspend(struct device *dev)
1746 struct dwc3 *dwc = dev_get_drvdata(dev);
1749 if (dwc3_runtime_checks(dwc))
1752 ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
1756 device_init_wakeup(dev, true);
1761 static int dwc3_runtime_resume(struct device *dev)
1763 struct dwc3 *dwc = dev_get_drvdata(dev);
1766 device_init_wakeup(dev, false);
1768 ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
1772 switch (dwc->current_dr_role) {
1773 case DWC3_GCTL_PRTCAP_DEVICE:
1774 dwc3_gadget_process_pending_events(dwc);
1776 case DWC3_GCTL_PRTCAP_HOST:
1782 pm_runtime_mark_last_busy(dev);
1787 static int dwc3_runtime_idle(struct device *dev)
1789 struct dwc3 *dwc = dev_get_drvdata(dev);
1791 switch (dwc->current_dr_role) {
1792 case DWC3_GCTL_PRTCAP_DEVICE:
1793 if (dwc3_runtime_checks(dwc))
1796 case DWC3_GCTL_PRTCAP_HOST:
1802 pm_runtime_mark_last_busy(dev);
1803 pm_runtime_autosuspend(dev);
1807 #endif /* CONFIG_PM */
1809 #ifdef CONFIG_PM_SLEEP
1810 static int dwc3_suspend(struct device *dev)
1812 struct dwc3 *dwc = dev_get_drvdata(dev);
1815 ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
1819 pinctrl_pm_select_sleep_state(dev);
1824 static int dwc3_resume(struct device *dev)
1826 struct dwc3 *dwc = dev_get_drvdata(dev);
1829 pinctrl_pm_select_default_state(dev);
1831 ret = dwc3_resume_common(dwc, PMSG_RESUME);
1835 pm_runtime_disable(dev);
1836 pm_runtime_set_active(dev);
1837 pm_runtime_enable(dev);
1841 #endif /* CONFIG_PM_SLEEP */
1843 static const struct dev_pm_ops dwc3_dev_pm_ops = {
1844 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
1845 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
1850 static const struct of_device_id of_dwc3_match[] = {
1852 .compatible = "snps,dwc3"
1855 .compatible = "synopsys,dwc3"
1859 MODULE_DEVICE_TABLE(of, of_dwc3_match);
1864 #define ACPI_ID_INTEL_BSW "808622B7"
1866 static const struct acpi_device_id dwc3_acpi_match[] = {
1867 { ACPI_ID_INTEL_BSW, 0 },
1870 MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
1873 static struct platform_driver dwc3_driver = {
1874 .probe = dwc3_probe,
1875 .remove = dwc3_remove,
1878 .of_match_table = of_match_ptr(of_dwc3_match),
1879 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
1880 .pm = &dwc3_dev_pm_ops,
1884 module_platform_driver(dwc3_driver);
1886 MODULE_ALIAS("platform:dwc3");
1887 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1888 MODULE_LICENSE("GPL v2");
1889 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");