1 // SPDX-License-Identifier: GPL-2.0
3 * core.c - DesignWare USB3 DRD Controller Core file
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
11 #include <linux/clk.h>
12 #include <linux/version.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/interrupt.h>
20 #include <linux/ioport.h>
22 #include <linux/list.h>
23 #include <linux/delay.h>
24 #include <linux/dma-mapping.h>
26 #include <linux/acpi.h>
27 #include <linux/pinctrl/consumer.h>
28 #include <linux/reset.h>
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 (!IS_ENABLED(CONFIG_USB_ROLE_SWITCH) ||
89 !device_property_read_bool(dwc->dev, "usb-role-switch")) &&
90 !DWC3_VER_IS_PRIOR(DWC3, 330A))
91 mode = USB_DR_MODE_PERIPHERAL;
94 if (mode != dwc->dr_mode) {
96 "Configuration mismatch. dr_mode forced to %s\n",
97 mode == USB_DR_MODE_HOST ? "host" : "gadget");
105 void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
109 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
110 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
111 reg |= DWC3_GCTL_PRTCAPDIR(mode);
112 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
114 dwc->current_dr_role = mode;
117 static void __dwc3_set_mode(struct work_struct *work)
119 struct dwc3 *dwc = work_to_dwc(work);
125 mutex_lock(&dwc->mutex);
126 spin_lock_irqsave(&dwc->lock, flags);
127 desired_dr_role = dwc->desired_dr_role;
128 spin_unlock_irqrestore(&dwc->lock, flags);
130 pm_runtime_get_sync(dwc->dev);
132 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
133 dwc3_otg_update(dwc, 0);
135 if (!desired_dr_role)
138 if (desired_dr_role == dwc->current_dr_role)
141 if (desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev)
144 switch (dwc->current_dr_role) {
145 case DWC3_GCTL_PRTCAP_HOST:
148 case DWC3_GCTL_PRTCAP_DEVICE:
149 dwc3_gadget_exit(dwc);
150 dwc3_event_buffers_cleanup(dwc);
152 case DWC3_GCTL_PRTCAP_OTG:
154 spin_lock_irqsave(&dwc->lock, flags);
155 dwc->desired_otg_role = DWC3_OTG_ROLE_IDLE;
156 spin_unlock_irqrestore(&dwc->lock, flags);
157 dwc3_otg_update(dwc, 1);
164 * When current_dr_role is not set, there's no role switching.
165 * Only perform GCTL.CoreSoftReset when there's DRD role switching.
167 if (dwc->current_dr_role && ((DWC3_IP_IS(DWC3) ||
168 DWC3_VER_IS_PRIOR(DWC31, 190A)) &&
169 desired_dr_role != DWC3_GCTL_PRTCAP_OTG)) {
170 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
171 reg |= DWC3_GCTL_CORESOFTRESET;
172 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
175 * Wait for internal clocks to synchronized. DWC_usb31 and
176 * DWC_usb32 may need at least 50ms (less for DWC_usb3). To
177 * keep it consistent across different IPs, let's wait up to
178 * 100ms before clearing GCTL.CORESOFTRESET.
182 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
183 reg &= ~DWC3_GCTL_CORESOFTRESET;
184 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
187 spin_lock_irqsave(&dwc->lock, flags);
189 dwc3_set_prtcap(dwc, desired_dr_role);
191 spin_unlock_irqrestore(&dwc->lock, flags);
193 switch (desired_dr_role) {
194 case DWC3_GCTL_PRTCAP_HOST:
195 ret = dwc3_host_init(dwc);
197 dev_err(dwc->dev, "failed to initialize host\n");
200 otg_set_vbus(dwc->usb2_phy->otg, true);
201 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
202 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
203 if (dwc->dis_split_quirk) {
204 reg = dwc3_readl(dwc->regs, DWC3_GUCTL3);
205 reg |= DWC3_GUCTL3_SPLITDISABLE;
206 dwc3_writel(dwc->regs, DWC3_GUCTL3, reg);
210 case DWC3_GCTL_PRTCAP_DEVICE:
211 dwc3_core_soft_reset(dwc);
213 dwc3_event_buffers_setup(dwc);
216 otg_set_vbus(dwc->usb2_phy->otg, false);
217 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
218 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
220 ret = dwc3_gadget_init(dwc);
222 dev_err(dwc->dev, "failed to initialize peripheral\n");
224 case DWC3_GCTL_PRTCAP_OTG:
226 dwc3_otg_update(dwc, 0);
233 pm_runtime_mark_last_busy(dwc->dev);
234 pm_runtime_put_autosuspend(dwc->dev);
235 mutex_unlock(&dwc->mutex);
238 void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
242 if (dwc->dr_mode != USB_DR_MODE_OTG)
245 spin_lock_irqsave(&dwc->lock, flags);
246 dwc->desired_dr_role = mode;
247 spin_unlock_irqrestore(&dwc->lock, flags);
249 queue_work(system_freezable_wq, &dwc->drd_work);
252 u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
254 struct dwc3 *dwc = dep->dwc;
257 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
258 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
259 DWC3_GDBGFIFOSPACE_TYPE(type));
261 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
263 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
267 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
268 * @dwc: pointer to our context structure
270 int dwc3_core_soft_reset(struct dwc3 *dwc)
276 * We're resetting only the device side because, if we're in host mode,
277 * XHCI driver will reset the host block. If dwc3 was configured for
278 * host-only mode, then we can return early.
280 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST)
283 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
284 reg |= DWC3_DCTL_CSFTRST;
285 reg &= ~DWC3_DCTL_RUN_STOP;
286 dwc3_gadget_dctl_write_safe(dwc, reg);
289 * For DWC_usb31 controller 1.90a and later, the DCTL.CSFRST bit
290 * is cleared only after all the clocks are synchronized. This can
291 * take a little more than 50ms. Set the polling rate at 20ms
292 * for 10 times instead.
294 if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
298 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
299 if (!(reg & DWC3_DCTL_CSFTRST))
302 if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
312 * For DWC_usb31 controller 1.80a and prior, once DCTL.CSFRST bit
313 * is cleared, we must wait at least 50ms before accessing the PHY
314 * domain (synchronization delay).
316 if (DWC3_VER_IS_WITHIN(DWC31, ANY, 180A))
323 * dwc3_frame_length_adjustment - Adjusts frame length if required
324 * @dwc3: Pointer to our controller context structure
326 static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
331 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
337 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
338 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
339 if (dft != dwc->fladj) {
340 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
341 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
342 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
347 * dwc3_free_one_event_buffer - Frees one event buffer
348 * @dwc: Pointer to our controller context structure
349 * @evt: Pointer to event buffer to be freed
351 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
352 struct dwc3_event_buffer *evt)
354 dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
358 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
359 * @dwc: Pointer to our controller context structure
360 * @length: size of the event buffer
362 * Returns a pointer to the allocated event buffer structure on success
363 * otherwise ERR_PTR(errno).
365 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
368 struct dwc3_event_buffer *evt;
370 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
372 return ERR_PTR(-ENOMEM);
375 evt->length = length;
376 evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
378 return ERR_PTR(-ENOMEM);
380 evt->buf = dma_alloc_coherent(dwc->sysdev, length,
381 &evt->dma, GFP_KERNEL);
383 return ERR_PTR(-ENOMEM);
389 * dwc3_free_event_buffers - frees all allocated event buffers
390 * @dwc: Pointer to our controller context structure
392 static void dwc3_free_event_buffers(struct dwc3 *dwc)
394 struct dwc3_event_buffer *evt;
398 dwc3_free_one_event_buffer(dwc, evt);
402 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
403 * @dwc: pointer to our controller context structure
404 * @length: size of event buffer
406 * Returns 0 on success otherwise negative errno. In the error case, dwc
407 * may contain some buffers allocated but not all which were requested.
409 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
411 struct dwc3_event_buffer *evt;
413 evt = dwc3_alloc_one_event_buffer(dwc, length);
415 dev_err(dwc->dev, "can't allocate event buffer\n");
424 * dwc3_event_buffers_setup - setup our allocated event buffers
425 * @dwc: pointer to our controller context structure
427 * Returns 0 on success otherwise negative errno.
429 int dwc3_event_buffers_setup(struct dwc3 *dwc)
431 struct dwc3_event_buffer *evt;
435 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
436 lower_32_bits(evt->dma));
437 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
438 upper_32_bits(evt->dma));
439 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
440 DWC3_GEVNTSIZ_SIZE(evt->length));
441 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
446 void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
448 struct dwc3_event_buffer *evt;
454 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
455 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
456 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
457 | DWC3_GEVNTSIZ_SIZE(0));
458 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
461 static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
463 if (!dwc->has_hibernation)
466 if (!dwc->nr_scratch)
469 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
470 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
471 if (!dwc->scratchbuf)
477 static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
479 dma_addr_t scratch_addr;
483 if (!dwc->has_hibernation)
486 if (!dwc->nr_scratch)
489 /* should never fall here */
490 if (!WARN_ON(dwc->scratchbuf))
493 scratch_addr = dma_map_single(dwc->sysdev, dwc->scratchbuf,
494 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
496 if (dma_mapping_error(dwc->sysdev, scratch_addr)) {
497 dev_err(dwc->sysdev, "failed to map scratch buffer\n");
502 dwc->scratch_addr = scratch_addr;
504 param = lower_32_bits(scratch_addr);
506 ret = dwc3_send_gadget_generic_command(dwc,
507 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
511 param = upper_32_bits(scratch_addr);
513 ret = dwc3_send_gadget_generic_command(dwc,
514 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
521 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
522 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
528 static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
530 if (!dwc->has_hibernation)
533 if (!dwc->nr_scratch)
536 /* should never fall here */
537 if (!WARN_ON(dwc->scratchbuf))
540 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
541 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
542 kfree(dwc->scratchbuf);
545 static void dwc3_core_num_eps(struct dwc3 *dwc)
547 struct dwc3_hwparams *parms = &dwc->hwparams;
549 dwc->num_eps = DWC3_NUM_EPS(parms);
552 static void dwc3_cache_hwparams(struct dwc3 *dwc)
554 struct dwc3_hwparams *parms = &dwc->hwparams;
556 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
557 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
558 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
559 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
560 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
561 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
562 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
563 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
564 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
567 static int dwc3_core_ulpi_init(struct dwc3 *dwc)
572 intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
574 if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
575 (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
576 dwc->hsphy_interface &&
577 !strncmp(dwc->hsphy_interface, "ulpi", 4)))
578 ret = dwc3_ulpi_init(dwc);
584 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
585 * @dwc: Pointer to our controller context structure
587 * Returns 0 on success. The USB PHY interfaces are configured but not
588 * initialized. The PHY interfaces and the PHYs get initialized together with
589 * the core in dwc3_core_init.
591 static int dwc3_phy_setup(struct dwc3 *dwc)
593 unsigned int hw_mode;
596 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
598 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
601 * Make sure UX_EXIT_PX is cleared as that causes issues with some
602 * PHYs. Also, this bit is not supposed to be used in normal operation.
604 reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
607 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
608 * to '0' during coreConsultant configuration. So default value
609 * will be '0' when the core is reset. Application needs to set it
610 * to '1' after the core initialization is completed.
612 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
613 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
616 * For DRD controllers, GUSB3PIPECTL.SUSPENDENABLE must be cleared after
617 * power-on reset, and it can be set after core initialization, which is
618 * after device soft-reset during initialization.
620 if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD)
621 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
623 if (dwc->u2ss_inp3_quirk)
624 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
626 if (dwc->dis_rxdet_inp3_quirk)
627 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
629 if (dwc->req_p1p2p3_quirk)
630 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
632 if (dwc->del_p1p2p3_quirk)
633 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
635 if (dwc->del_phy_power_chg_quirk)
636 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
638 if (dwc->lfps_filter_quirk)
639 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
641 if (dwc->rx_detect_poll_quirk)
642 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
644 if (dwc->tx_de_emphasis_quirk)
645 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
647 if (dwc->dis_u3_susphy_quirk)
648 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
650 if (dwc->dis_del_phy_power_chg_quirk)
651 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
653 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
655 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
657 /* Select the HS PHY interface */
658 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
659 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
660 if (dwc->hsphy_interface &&
661 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
662 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
664 } else if (dwc->hsphy_interface &&
665 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
666 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
667 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
669 /* Relying on default value. */
670 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
674 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
679 switch (dwc->hsphy_mode) {
680 case USBPHY_INTERFACE_MODE_UTMI:
681 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
682 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
683 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
684 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
686 case USBPHY_INTERFACE_MODE_UTMIW:
687 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
688 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
689 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
690 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
697 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
698 * '0' during coreConsultant configuration. So default value will
699 * be '0' when the core is reset. Application needs to set it to
700 * '1' after the core initialization is completed.
702 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
703 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
706 * For DRD controllers, GUSB2PHYCFG.SUSPHY must be cleared after
707 * power-on reset, and it can be set after core initialization, which is
708 * after device soft-reset during initialization.
710 if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD)
711 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
713 if (dwc->dis_u2_susphy_quirk)
714 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
716 if (dwc->dis_enblslpm_quirk)
717 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
719 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM;
721 if (dwc->dis_u2_freeclk_exists_quirk)
722 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
724 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
729 static void dwc3_core_exit(struct dwc3 *dwc)
731 dwc3_event_buffers_cleanup(dwc);
733 usb_phy_set_suspend(dwc->usb2_phy, 1);
734 usb_phy_set_suspend(dwc->usb3_phy, 1);
735 phy_power_off(dwc->usb2_generic_phy);
736 phy_power_off(dwc->usb3_generic_phy);
738 usb_phy_shutdown(dwc->usb2_phy);
739 usb_phy_shutdown(dwc->usb3_phy);
740 phy_exit(dwc->usb2_generic_phy);
741 phy_exit(dwc->usb3_generic_phy);
743 clk_bulk_disable_unprepare(dwc->num_clks, dwc->clks);
744 reset_control_assert(dwc->reset);
747 static bool dwc3_core_is_valid(struct dwc3 *dwc)
751 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
752 dwc->ip = DWC3_GSNPS_ID(reg);
754 /* This should read as U3 followed by revision number */
755 if (DWC3_IP_IS(DWC3)) {
757 } else if (DWC3_IP_IS(DWC31) || DWC3_IP_IS(DWC32)) {
758 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
759 dwc->version_type = dwc3_readl(dwc->regs, DWC3_VER_TYPE);
767 static void dwc3_core_setup_global_control(struct dwc3 *dwc)
769 u32 hwparams4 = dwc->hwparams.hwparams4;
772 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
773 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
775 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
776 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
778 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
779 * issue which would cause xHCI compliance tests to fail.
781 * Because of that we cannot enable clock gating on such
786 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
789 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
790 dwc->dr_mode == USB_DR_MODE_OTG) &&
791 DWC3_VER_IS_WITHIN(DWC3, 210A, 250A))
792 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
794 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
796 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
797 /* enable hibernation here */
798 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
801 * REVISIT Enabling this bit so that host-mode hibernation
802 * will work. Device-mode hibernation is not yet implemented.
804 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
811 /* check if current dwc3 is on simulation board */
812 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
813 dev_info(dwc->dev, "Running with FPGA optimizations\n");
817 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
818 "disable_scramble cannot be used on non-FPGA builds\n");
820 if (dwc->disable_scramble_quirk && dwc->is_fpga)
821 reg |= DWC3_GCTL_DISSCRAMBLE;
823 reg &= ~DWC3_GCTL_DISSCRAMBLE;
825 if (dwc->u2exit_lfps_quirk)
826 reg |= DWC3_GCTL_U2EXIT_LFPS;
829 * WORKAROUND: DWC3 revisions <1.90a have a bug
830 * where the device can fail to connect at SuperSpeed
831 * and falls back to high-speed mode which causes
832 * the device to enter a Connect/Disconnect loop
834 if (DWC3_VER_IS_PRIOR(DWC3, 190A))
835 reg |= DWC3_GCTL_U2RSTECN;
837 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
840 static int dwc3_core_get_phy(struct dwc3 *dwc);
841 static int dwc3_core_ulpi_init(struct dwc3 *dwc);
843 /* set global incr burst type configuration registers */
844 static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
846 struct device *dev = dwc->dev;
847 /* incrx_mode : for INCR burst type. */
849 /* incrx_size : for size of INCRX burst. */
857 cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
860 * Handle property "snps,incr-burst-type-adjustment".
861 * Get the number of value from this property:
862 * result <= 0, means this property is not supported.
863 * result = 1, means INCRx burst mode supported.
864 * result > 1, means undefined length burst mode supported.
866 ntype = device_property_count_u32(dev, "snps,incr-burst-type-adjustment");
870 vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL);
872 dev_err(dev, "Error to get memory\n");
876 /* Get INCR burst type, and parse it */
877 ret = device_property_read_u32_array(dev,
878 "snps,incr-burst-type-adjustment", vals, ntype);
881 dev_err(dev, "Error to get property\n");
888 /* INCRX (undefined length) burst mode */
889 incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
890 for (i = 1; i < ntype; i++) {
891 if (vals[i] > incrx_size)
892 incrx_size = vals[i];
895 /* INCRX burst mode */
896 incrx_mode = INCRX_BURST_MODE;
901 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
902 cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
904 cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
905 switch (incrx_size) {
907 cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
910 cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
913 cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
916 cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
919 cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
922 cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
925 cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
930 dev_err(dev, "Invalid property\n");
934 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
938 * dwc3_core_init - Low-level initialization of DWC3 Core
939 * @dwc: Pointer to our controller context structure
941 * Returns 0 on success otherwise negative errno.
943 static int dwc3_core_init(struct dwc3 *dwc)
945 unsigned int hw_mode;
949 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
952 * Write Linux Version Code to our GUID register so it's easy to figure
953 * out which kernel version a bug was found.
955 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
957 ret = dwc3_phy_setup(dwc);
961 if (!dwc->ulpi_ready) {
962 ret = dwc3_core_ulpi_init(dwc);
964 if (ret == -ETIMEDOUT) {
965 dwc3_core_soft_reset(dwc);
970 dwc->ulpi_ready = true;
973 if (!dwc->phys_ready) {
974 ret = dwc3_core_get_phy(dwc);
977 dwc->phys_ready = true;
980 usb_phy_init(dwc->usb2_phy);
981 usb_phy_init(dwc->usb3_phy);
982 ret = phy_init(dwc->usb2_generic_phy);
986 ret = phy_init(dwc->usb3_generic_phy);
988 phy_exit(dwc->usb2_generic_phy);
992 ret = dwc3_core_soft_reset(dwc);
996 if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD &&
997 !DWC3_VER_IS_WITHIN(DWC3, ANY, 194A)) {
998 if (!dwc->dis_u3_susphy_quirk) {
999 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1000 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1001 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1004 if (!dwc->dis_u2_susphy_quirk) {
1005 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1006 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1007 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1011 dwc3_core_setup_global_control(dwc);
1012 dwc3_core_num_eps(dwc);
1014 ret = dwc3_setup_scratch_buffers(dwc);
1018 /* Adjust Frame Length */
1019 dwc3_frame_length_adjustment(dwc);
1021 dwc3_set_incr_burst_type(dwc);
1023 usb_phy_set_suspend(dwc->usb2_phy, 0);
1024 usb_phy_set_suspend(dwc->usb3_phy, 0);
1025 ret = phy_power_on(dwc->usb2_generic_phy);
1029 ret = phy_power_on(dwc->usb3_generic_phy);
1033 ret = dwc3_event_buffers_setup(dwc);
1035 dev_err(dwc->dev, "failed to setup event buffers\n");
1040 * ENDXFER polling is available on version 3.10a and later of
1041 * the DWC_usb3 controller. It is NOT available in the
1042 * DWC_usb31 controller.
1044 if (DWC3_VER_IS_WITHIN(DWC3, 310A, ANY)) {
1045 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
1046 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
1047 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
1050 if (!DWC3_VER_IS_PRIOR(DWC3, 250A)) {
1051 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1054 * Enable hardware control of sending remote wakeup
1055 * in HS when the device is in the L1 state.
1057 if (!DWC3_VER_IS_PRIOR(DWC3, 290A))
1058 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
1060 if (dwc->dis_tx_ipgap_linecheck_quirk)
1061 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
1063 if (dwc->parkmode_disable_ss_quirk)
1064 reg |= DWC3_GUCTL1_PARKMODE_DISABLE_SS;
1066 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1070 * Must config both number of packets and max burst settings to enable
1071 * RX and/or TX threshold.
1073 if (!DWC3_IP_IS(DWC3) && dwc->dr_mode == USB_DR_MODE_HOST) {
1074 u8 rx_thr_num = dwc->rx_thr_num_pkt_prd;
1075 u8 rx_maxburst = dwc->rx_max_burst_prd;
1076 u8 tx_thr_num = dwc->tx_thr_num_pkt_prd;
1077 u8 tx_maxburst = dwc->tx_max_burst_prd;
1079 if (rx_thr_num && rx_maxburst) {
1080 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1081 reg |= DWC31_RXTHRNUMPKTSEL_PRD;
1083 reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
1084 reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
1086 reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
1087 reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
1089 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1092 if (tx_thr_num && tx_maxburst) {
1093 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1094 reg |= DWC31_TXTHRNUMPKTSEL_PRD;
1096 reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
1097 reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
1099 reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
1100 reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
1102 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1109 phy_power_off(dwc->usb3_generic_phy);
1112 phy_power_off(dwc->usb2_generic_phy);
1115 usb_phy_set_suspend(dwc->usb2_phy, 1);
1116 usb_phy_set_suspend(dwc->usb3_phy, 1);
1119 usb_phy_shutdown(dwc->usb2_phy);
1120 usb_phy_shutdown(dwc->usb3_phy);
1121 phy_exit(dwc->usb2_generic_phy);
1122 phy_exit(dwc->usb3_generic_phy);
1125 dwc3_ulpi_exit(dwc);
1131 static int dwc3_core_get_phy(struct dwc3 *dwc)
1133 struct device *dev = dwc->dev;
1134 struct device_node *node = dev->of_node;
1138 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
1139 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
1141 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1142 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
1145 if (IS_ERR(dwc->usb2_phy)) {
1146 ret = PTR_ERR(dwc->usb2_phy);
1147 if (ret == -ENXIO || ret == -ENODEV) {
1148 dwc->usb2_phy = NULL;
1149 } else if (ret == -EPROBE_DEFER) {
1152 dev_err(dev, "no usb2 phy configured\n");
1157 if (IS_ERR(dwc->usb3_phy)) {
1158 ret = PTR_ERR(dwc->usb3_phy);
1159 if (ret == -ENXIO || ret == -ENODEV) {
1160 dwc->usb3_phy = NULL;
1161 } else if (ret == -EPROBE_DEFER) {
1164 dev_err(dev, "no usb3 phy configured\n");
1169 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
1170 if (IS_ERR(dwc->usb2_generic_phy)) {
1171 ret = PTR_ERR(dwc->usb2_generic_phy);
1172 if (ret == -ENOSYS || ret == -ENODEV) {
1173 dwc->usb2_generic_phy = NULL;
1174 } else if (ret == -EPROBE_DEFER) {
1177 dev_err(dev, "no usb2 phy configured\n");
1182 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
1183 if (IS_ERR(dwc->usb3_generic_phy)) {
1184 ret = PTR_ERR(dwc->usb3_generic_phy);
1185 if (ret == -ENOSYS || ret == -ENODEV) {
1186 dwc->usb3_generic_phy = NULL;
1187 } else if (ret == -EPROBE_DEFER) {
1190 dev_err(dev, "no usb3 phy configured\n");
1198 static int dwc3_core_init_mode(struct dwc3 *dwc)
1200 struct device *dev = dwc->dev;
1203 switch (dwc->dr_mode) {
1204 case USB_DR_MODE_PERIPHERAL:
1205 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1208 otg_set_vbus(dwc->usb2_phy->otg, false);
1209 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
1210 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
1212 ret = dwc3_gadget_init(dwc);
1214 if (ret != -EPROBE_DEFER)
1215 dev_err(dev, "failed to initialize gadget\n");
1219 case USB_DR_MODE_HOST:
1220 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1223 otg_set_vbus(dwc->usb2_phy->otg, true);
1224 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
1225 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
1227 ret = dwc3_host_init(dwc);
1229 if (ret != -EPROBE_DEFER)
1230 dev_err(dev, "failed to initialize host\n");
1234 case USB_DR_MODE_OTG:
1235 INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
1236 ret = dwc3_drd_init(dwc);
1238 if (ret != -EPROBE_DEFER)
1239 dev_err(dev, "failed to initialize dual-role\n");
1244 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1251 static void dwc3_core_exit_mode(struct dwc3 *dwc)
1253 switch (dwc->dr_mode) {
1254 case USB_DR_MODE_PERIPHERAL:
1255 dwc3_gadget_exit(dwc);
1257 case USB_DR_MODE_HOST:
1258 dwc3_host_exit(dwc);
1260 case USB_DR_MODE_OTG:
1268 /* de-assert DRVVBUS for HOST and OTG mode */
1269 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1272 static void dwc3_get_properties(struct dwc3 *dwc)
1274 struct device *dev = dwc->dev;
1275 u8 lpm_nyet_threshold;
1278 u8 rx_thr_num_pkt_prd = 0;
1279 u8 rx_max_burst_prd = 0;
1280 u8 tx_thr_num_pkt_prd = 0;
1281 u8 tx_max_burst_prd = 0;
1283 /* default to highest possible threshold */
1284 lpm_nyet_threshold = 0xf;
1286 /* default to -3.5dB de-emphasis */
1290 * default to assert utmi_sleep_n and use maximum allowed HIRD
1291 * threshold value of 0b1100
1293 hird_threshold = 12;
1295 dwc->maximum_speed = usb_get_maximum_speed(dev);
1296 dwc->dr_mode = usb_get_dr_mode(dev);
1297 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
1299 dwc->sysdev_is_parent = device_property_read_bool(dev,
1300 "linux,sysdev_is_parent");
1301 if (dwc->sysdev_is_parent)
1302 dwc->sysdev = dwc->dev->parent;
1304 dwc->sysdev = dwc->dev;
1306 dwc->has_lpm_erratum = device_property_read_bool(dev,
1307 "snps,has-lpm-erratum");
1308 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
1309 &lpm_nyet_threshold);
1310 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
1311 "snps,is-utmi-l1-suspend");
1312 device_property_read_u8(dev, "snps,hird-threshold",
1314 dwc->dis_start_transfer_quirk = device_property_read_bool(dev,
1315 "snps,dis-start-transfer-quirk");
1316 dwc->usb3_lpm_capable = device_property_read_bool(dev,
1317 "snps,usb3_lpm_capable");
1318 dwc->usb2_lpm_disable = device_property_read_bool(dev,
1319 "snps,usb2-lpm-disable");
1320 dwc->usb2_gadget_lpm_disable = device_property_read_bool(dev,
1321 "snps,usb2-gadget-lpm-disable");
1322 device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
1323 &rx_thr_num_pkt_prd);
1324 device_property_read_u8(dev, "snps,rx-max-burst-prd",
1326 device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
1327 &tx_thr_num_pkt_prd);
1328 device_property_read_u8(dev, "snps,tx-max-burst-prd",
1331 dwc->disable_scramble_quirk = device_property_read_bool(dev,
1332 "snps,disable_scramble_quirk");
1333 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
1334 "snps,u2exit_lfps_quirk");
1335 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
1336 "snps,u2ss_inp3_quirk");
1337 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
1338 "snps,req_p1p2p3_quirk");
1339 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
1340 "snps,del_p1p2p3_quirk");
1341 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
1342 "snps,del_phy_power_chg_quirk");
1343 dwc->lfps_filter_quirk = device_property_read_bool(dev,
1344 "snps,lfps_filter_quirk");
1345 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
1346 "snps,rx_detect_poll_quirk");
1347 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
1348 "snps,dis_u3_susphy_quirk");
1349 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
1350 "snps,dis_u2_susphy_quirk");
1351 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1352 "snps,dis_enblslpm_quirk");
1353 dwc->dis_u1_entry_quirk = device_property_read_bool(dev,
1354 "snps,dis-u1-entry-quirk");
1355 dwc->dis_u2_entry_quirk = device_property_read_bool(dev,
1356 "snps,dis-u2-entry-quirk");
1357 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1358 "snps,dis_rxdet_inp3_quirk");
1359 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1360 "snps,dis-u2-freeclk-exists-quirk");
1361 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1362 "snps,dis-del-phy-power-chg-quirk");
1363 dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1364 "snps,dis-tx-ipgap-linecheck-quirk");
1365 dwc->parkmode_disable_ss_quirk = device_property_read_bool(dev,
1366 "snps,parkmode-disable-ss-quirk");
1368 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
1369 "snps,tx_de_emphasis_quirk");
1370 device_property_read_u8(dev, "snps,tx_de_emphasis",
1372 device_property_read_string(dev, "snps,hsphy_interface",
1373 &dwc->hsphy_interface);
1374 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
1377 dwc->dis_metastability_quirk = device_property_read_bool(dev,
1378 "snps,dis_metastability_quirk");
1380 dwc->dis_split_quirk = device_property_read_bool(dev,
1381 "snps,dis-split-quirk");
1383 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1384 dwc->tx_de_emphasis = tx_de_emphasis;
1386 dwc->hird_threshold = hird_threshold;
1388 dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd;
1389 dwc->rx_max_burst_prd = rx_max_burst_prd;
1391 dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd;
1392 dwc->tx_max_burst_prd = tx_max_burst_prd;
1394 dwc->imod_interval = 0;
1397 /* check whether the core supports IMOD */
1398 bool dwc3_has_imod(struct dwc3 *dwc)
1400 return DWC3_VER_IS_WITHIN(DWC3, 300A, ANY) ||
1401 DWC3_VER_IS_WITHIN(DWC31, 120A, ANY) ||
1405 static void dwc3_check_params(struct dwc3 *dwc)
1407 struct device *dev = dwc->dev;
1408 unsigned int hwparam_gen =
1409 DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3);
1411 /* Check for proper value of imod_interval */
1412 if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
1413 dev_warn(dwc->dev, "Interrupt moderation not supported\n");
1414 dwc->imod_interval = 0;
1418 * Workaround for STAR 9000961433 which affects only version
1419 * 3.00a of the DWC_usb3 core. This prevents the controller
1420 * interrupt from being masked while handling events. IMOD
1421 * allows us to work around this issue. Enable it for the
1424 if (!dwc->imod_interval &&
1425 DWC3_VER_IS(DWC3, 300A))
1426 dwc->imod_interval = 1;
1428 /* Check the maximum_speed parameter */
1429 switch (dwc->maximum_speed) {
1431 case USB_SPEED_FULL:
1432 case USB_SPEED_HIGH:
1434 case USB_SPEED_SUPER:
1435 if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS)
1436 dev_warn(dev, "UDC doesn't support Gen 1\n");
1438 case USB_SPEED_SUPER_PLUS:
1439 if ((DWC3_IP_IS(DWC32) &&
1440 hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS) ||
1441 (!DWC3_IP_IS(DWC32) &&
1442 hwparam_gen != DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1443 dev_warn(dev, "UDC doesn't support SSP\n");
1446 dev_err(dev, "invalid maximum_speed parameter %d\n",
1447 dwc->maximum_speed);
1449 case USB_SPEED_UNKNOWN:
1450 switch (hwparam_gen) {
1451 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
1452 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1454 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
1455 if (DWC3_IP_IS(DWC32))
1456 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1458 dwc->maximum_speed = USB_SPEED_SUPER;
1460 case DWC3_GHWPARAMS3_SSPHY_IFC_DIS:
1461 dwc->maximum_speed = USB_SPEED_HIGH;
1464 dwc->maximum_speed = USB_SPEED_SUPER;
1471 static int dwc3_probe(struct platform_device *pdev)
1473 struct device *dev = &pdev->dev;
1474 struct resource *res, dwc_res;
1481 dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
1487 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1489 dev_err(dev, "missing memory resource\n");
1493 dwc->xhci_resources[0].start = res->start;
1494 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1496 dwc->xhci_resources[0].flags = res->flags;
1497 dwc->xhci_resources[0].name = res->name;
1500 * Request memory region but exclude xHCI regs,
1501 * since it will be requested by the xhci-plat driver.
1504 dwc_res.start += DWC3_GLOBALS_REGS_START;
1506 regs = devm_ioremap_resource(dev, &dwc_res);
1508 return PTR_ERR(regs);
1511 dwc->regs_size = resource_size(&dwc_res);
1513 dwc3_get_properties(dwc);
1515 dwc->reset = devm_reset_control_array_get(dev, true, true);
1516 if (IS_ERR(dwc->reset))
1517 return PTR_ERR(dwc->reset);
1520 ret = devm_clk_bulk_get_all(dev, &dwc->clks);
1521 if (ret == -EPROBE_DEFER)
1524 * Clocks are optional, but new DT platforms should support all
1525 * clocks as required by the DT-binding.
1530 dwc->num_clks = ret;
1534 ret = reset_control_deassert(dwc->reset);
1538 ret = clk_bulk_prepare_enable(dwc->num_clks, dwc->clks);
1542 if (!dwc3_core_is_valid(dwc)) {
1543 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
1548 platform_set_drvdata(pdev, dwc);
1549 dwc3_cache_hwparams(dwc);
1551 spin_lock_init(&dwc->lock);
1552 mutex_init(&dwc->mutex);
1554 pm_runtime_get_noresume(dev);
1555 pm_runtime_set_active(dev);
1556 pm_runtime_use_autosuspend(dev);
1557 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
1558 pm_runtime_enable(dev);
1560 pm_runtime_forbid(dev);
1562 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1564 dev_err(dwc->dev, "failed to allocate event buffers\n");
1569 ret = dwc3_get_dr_mode(dwc);
1573 ret = dwc3_alloc_scratch_buffers(dwc);
1577 ret = dwc3_core_init(dwc);
1579 if (ret != -EPROBE_DEFER)
1580 dev_err(dev, "failed to initialize core: %d\n", ret);
1584 dwc3_check_params(dwc);
1585 dwc3_debugfs_init(dwc);
1587 ret = dwc3_core_init_mode(dwc);
1591 pm_runtime_put(dev);
1593 dma_set_max_seg_size(dev, UINT_MAX);
1598 dwc3_debugfs_exit(dwc);
1599 dwc3_event_buffers_cleanup(dwc);
1601 usb_phy_set_suspend(dwc->usb2_phy, 1);
1602 usb_phy_set_suspend(dwc->usb3_phy, 1);
1603 phy_power_off(dwc->usb2_generic_phy);
1604 phy_power_off(dwc->usb3_generic_phy);
1606 usb_phy_shutdown(dwc->usb2_phy);
1607 usb_phy_shutdown(dwc->usb3_phy);
1608 phy_exit(dwc->usb2_generic_phy);
1609 phy_exit(dwc->usb3_generic_phy);
1611 dwc3_ulpi_exit(dwc);
1614 dwc3_free_scratch_buffers(dwc);
1617 dwc3_free_event_buffers(dwc);
1620 pm_runtime_allow(dev);
1621 pm_runtime_disable(dev);
1622 pm_runtime_set_suspended(dev);
1623 pm_runtime_put_noidle(dev);
1625 clk_bulk_disable_unprepare(dwc->num_clks, dwc->clks);
1627 reset_control_assert(dwc->reset);
1632 static int dwc3_remove(struct platform_device *pdev)
1634 struct dwc3 *dwc = platform_get_drvdata(pdev);
1636 pm_runtime_get_sync(&pdev->dev);
1638 dwc3_core_exit_mode(dwc);
1639 dwc3_debugfs_exit(dwc);
1641 dwc3_core_exit(dwc);
1642 dwc3_ulpi_exit(dwc);
1644 pm_runtime_allow(&pdev->dev);
1645 pm_runtime_disable(&pdev->dev);
1646 pm_runtime_put_noidle(&pdev->dev);
1647 pm_runtime_set_suspended(&pdev->dev);
1649 dwc3_free_event_buffers(dwc);
1650 dwc3_free_scratch_buffers(dwc);
1656 static int dwc3_core_init_for_resume(struct dwc3 *dwc)
1660 ret = reset_control_deassert(dwc->reset);
1664 ret = clk_bulk_prepare_enable(dwc->num_clks, dwc->clks);
1668 ret = dwc3_core_init(dwc);
1675 clk_bulk_disable_unprepare(dwc->num_clks, dwc->clks);
1677 reset_control_assert(dwc->reset);
1682 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
1684 unsigned long flags;
1687 switch (dwc->current_dr_role) {
1688 case DWC3_GCTL_PRTCAP_DEVICE:
1689 if (pm_runtime_suspended(dwc->dev))
1691 spin_lock_irqsave(&dwc->lock, flags);
1692 dwc3_gadget_suspend(dwc);
1693 spin_unlock_irqrestore(&dwc->lock, flags);
1694 synchronize_irq(dwc->irq_gadget);
1695 dwc3_core_exit(dwc);
1697 case DWC3_GCTL_PRTCAP_HOST:
1698 if (!PMSG_IS_AUTO(msg)) {
1699 dwc3_core_exit(dwc);
1703 /* Let controller to suspend HSPHY before PHY driver suspends */
1704 if (dwc->dis_u2_susphy_quirk ||
1705 dwc->dis_enblslpm_quirk) {
1706 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1707 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
1708 DWC3_GUSB2PHYCFG_SUSPHY;
1709 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1711 /* Give some time for USB2 PHY to suspend */
1712 usleep_range(5000, 6000);
1715 phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
1716 phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
1718 case DWC3_GCTL_PRTCAP_OTG:
1719 /* do nothing during runtime_suspend */
1720 if (PMSG_IS_AUTO(msg))
1723 if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1724 spin_lock_irqsave(&dwc->lock, flags);
1725 dwc3_gadget_suspend(dwc);
1726 spin_unlock_irqrestore(&dwc->lock, flags);
1727 synchronize_irq(dwc->irq_gadget);
1731 dwc3_core_exit(dwc);
1741 static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
1743 unsigned long flags;
1747 switch (dwc->current_dr_role) {
1748 case DWC3_GCTL_PRTCAP_DEVICE:
1749 ret = dwc3_core_init_for_resume(dwc);
1753 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1754 spin_lock_irqsave(&dwc->lock, flags);
1755 dwc3_gadget_resume(dwc);
1756 spin_unlock_irqrestore(&dwc->lock, flags);
1758 case DWC3_GCTL_PRTCAP_HOST:
1759 if (!PMSG_IS_AUTO(msg)) {
1760 ret = dwc3_core_init_for_resume(dwc);
1763 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1766 /* Restore GUSB2PHYCFG bits that were modified in suspend */
1767 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1768 if (dwc->dis_u2_susphy_quirk)
1769 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1771 if (dwc->dis_enblslpm_quirk)
1772 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
1774 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1776 phy_pm_runtime_get_sync(dwc->usb2_generic_phy);
1777 phy_pm_runtime_get_sync(dwc->usb3_generic_phy);
1779 case DWC3_GCTL_PRTCAP_OTG:
1780 /* nothing to do on runtime_resume */
1781 if (PMSG_IS_AUTO(msg))
1784 ret = dwc3_core_init_for_resume(dwc);
1788 dwc3_set_prtcap(dwc, dwc->current_dr_role);
1791 if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
1792 dwc3_otg_host_init(dwc);
1793 } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1794 spin_lock_irqsave(&dwc->lock, flags);
1795 dwc3_gadget_resume(dwc);
1796 spin_unlock_irqrestore(&dwc->lock, flags);
1808 static int dwc3_runtime_checks(struct dwc3 *dwc)
1810 switch (dwc->current_dr_role) {
1811 case DWC3_GCTL_PRTCAP_DEVICE:
1815 case DWC3_GCTL_PRTCAP_HOST:
1824 static int dwc3_runtime_suspend(struct device *dev)
1826 struct dwc3 *dwc = dev_get_drvdata(dev);
1829 if (dwc3_runtime_checks(dwc))
1832 ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
1836 device_init_wakeup(dev, true);
1841 static int dwc3_runtime_resume(struct device *dev)
1843 struct dwc3 *dwc = dev_get_drvdata(dev);
1846 device_init_wakeup(dev, false);
1848 ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
1852 switch (dwc->current_dr_role) {
1853 case DWC3_GCTL_PRTCAP_DEVICE:
1854 dwc3_gadget_process_pending_events(dwc);
1856 case DWC3_GCTL_PRTCAP_HOST:
1862 pm_runtime_mark_last_busy(dev);
1867 static int dwc3_runtime_idle(struct device *dev)
1869 struct dwc3 *dwc = dev_get_drvdata(dev);
1871 switch (dwc->current_dr_role) {
1872 case DWC3_GCTL_PRTCAP_DEVICE:
1873 if (dwc3_runtime_checks(dwc))
1876 case DWC3_GCTL_PRTCAP_HOST:
1882 pm_runtime_mark_last_busy(dev);
1883 pm_runtime_autosuspend(dev);
1887 #endif /* CONFIG_PM */
1889 #ifdef CONFIG_PM_SLEEP
1890 static int dwc3_suspend(struct device *dev)
1892 struct dwc3 *dwc = dev_get_drvdata(dev);
1895 ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
1899 pinctrl_pm_select_sleep_state(dev);
1904 static int dwc3_resume(struct device *dev)
1906 struct dwc3 *dwc = dev_get_drvdata(dev);
1909 pinctrl_pm_select_default_state(dev);
1911 ret = dwc3_resume_common(dwc, PMSG_RESUME);
1915 pm_runtime_disable(dev);
1916 pm_runtime_set_active(dev);
1917 pm_runtime_enable(dev);
1922 static void dwc3_complete(struct device *dev)
1924 struct dwc3 *dwc = dev_get_drvdata(dev);
1927 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST &&
1928 dwc->dis_split_quirk) {
1929 reg = dwc3_readl(dwc->regs, DWC3_GUCTL3);
1930 reg |= DWC3_GUCTL3_SPLITDISABLE;
1931 dwc3_writel(dwc->regs, DWC3_GUCTL3, reg);
1935 #define dwc3_complete NULL
1936 #endif /* CONFIG_PM_SLEEP */
1938 static const struct dev_pm_ops dwc3_dev_pm_ops = {
1939 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
1940 .complete = dwc3_complete,
1941 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
1946 static const struct of_device_id of_dwc3_match[] = {
1948 .compatible = "snps,dwc3"
1951 .compatible = "synopsys,dwc3"
1955 MODULE_DEVICE_TABLE(of, of_dwc3_match);
1960 #define ACPI_ID_INTEL_BSW "808622B7"
1962 static const struct acpi_device_id dwc3_acpi_match[] = {
1963 { ACPI_ID_INTEL_BSW, 0 },
1966 MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
1969 static struct platform_driver dwc3_driver = {
1970 .probe = dwc3_probe,
1971 .remove = dwc3_remove,
1974 .of_match_table = of_match_ptr(of_dwc3_match),
1975 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
1976 .pm = &dwc3_dev_pm_ops,
1980 module_platform_driver(dwc3_driver);
1982 MODULE_ALIAS("platform:dwc3");
1983 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1984 MODULE_LICENSE("GPL v2");
1985 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");