2 * core.c - DesignWare USB3 DRD Controller Core file
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <linux/version.h>
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
27 #include <linux/platform_device.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/interrupt.h>
30 #include <linux/ioport.h>
32 #include <linux/list.h>
33 #include <linux/delay.h>
34 #include <linux/dma-mapping.h>
36 #include <linux/acpi.h>
37 #include <linux/pinctrl/consumer.h>
39 #include <linux/usb/ch9.h>
40 #include <linux/usb/gadget.h>
41 #include <linux/usb/of.h>
42 #include <linux/usb/otg.h>
50 #define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
52 void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
56 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
57 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
58 reg |= DWC3_GCTL_PRTCAPDIR(mode);
59 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
62 u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
64 struct dwc3 *dwc = dep->dwc;
67 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
68 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
69 DWC3_GDBGFIFOSPACE_TYPE(type));
71 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
73 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
77 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
78 * @dwc: pointer to our context structure
80 static int dwc3_core_soft_reset(struct dwc3 *dwc)
86 usb_phy_init(dwc->usb2_phy);
87 usb_phy_init(dwc->usb3_phy);
88 ret = phy_init(dwc->usb2_generic_phy);
92 ret = phy_init(dwc->usb3_generic_phy);
94 phy_exit(dwc->usb2_generic_phy);
99 * We're resetting only the device side because, if we're in host mode,
100 * XHCI driver will reset the host block. If dwc3 was configured for
101 * host-only mode, then we can return early.
103 if (dwc->dr_mode == USB_DR_MODE_HOST)
106 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
107 reg |= DWC3_DCTL_CSFTRST;
108 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
111 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
112 if (!(reg & DWC3_DCTL_CSFTRST))
122 * dwc3_soft_reset - Issue soft reset
123 * @dwc: Pointer to our controller context structure
125 static int dwc3_soft_reset(struct dwc3 *dwc)
127 unsigned long timeout;
130 timeout = jiffies + msecs_to_jiffies(500);
131 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
133 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
134 if (!(reg & DWC3_DCTL_CSFTRST))
137 if (time_after(jiffies, timeout)) {
138 dev_err(dwc->dev, "Reset Timed Out\n");
149 * dwc3_frame_length_adjustment - Adjusts frame length if required
150 * @dwc3: Pointer to our controller context structure
152 static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
157 if (dwc->revision < DWC3_REVISION_250A)
163 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
164 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
165 if (!dev_WARN_ONCE(dwc->dev, dft == dwc->fladj,
166 "request value same as default, ignoring\n")) {
167 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
168 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
169 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
174 * dwc3_free_one_event_buffer - Frees one event buffer
175 * @dwc: Pointer to our controller context structure
176 * @evt: Pointer to event buffer to be freed
178 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
179 struct dwc3_event_buffer *evt)
181 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
185 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
186 * @dwc: Pointer to our controller context structure
187 * @length: size of the event buffer
189 * Returns a pointer to the allocated event buffer structure on success
190 * otherwise ERR_PTR(errno).
192 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
195 struct dwc3_event_buffer *evt;
197 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
199 return ERR_PTR(-ENOMEM);
202 evt->length = length;
203 evt->buf = dma_alloc_coherent(dwc->dev, length,
204 &evt->dma, GFP_KERNEL);
206 return ERR_PTR(-ENOMEM);
212 * dwc3_free_event_buffers - frees all allocated event buffers
213 * @dwc: Pointer to our controller context structure
215 static void dwc3_free_event_buffers(struct dwc3 *dwc)
217 struct dwc3_event_buffer *evt;
221 dwc3_free_one_event_buffer(dwc, evt);
225 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
226 * @dwc: pointer to our controller context structure
227 * @length: size of event buffer
229 * Returns 0 on success otherwise negative errno. In the error case, dwc
230 * may contain some buffers allocated but not all which were requested.
232 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
234 struct dwc3_event_buffer *evt;
236 evt = dwc3_alloc_one_event_buffer(dwc, length);
238 dev_err(dwc->dev, "can't allocate event buffer\n");
247 * dwc3_event_buffers_setup - setup our allocated event buffers
248 * @dwc: pointer to our controller context structure
250 * Returns 0 on success otherwise negative errno.
252 static int dwc3_event_buffers_setup(struct dwc3 *dwc)
254 struct dwc3_event_buffer *evt;
257 dwc3_trace(trace_dwc3_core,
258 "Event buf %p dma %08llx length %d\n",
259 evt->buf, (unsigned long long) evt->dma,
264 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
265 lower_32_bits(evt->dma));
266 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
267 upper_32_bits(evt->dma));
268 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
269 DWC3_GEVNTSIZ_SIZE(evt->length));
270 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
275 static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
277 struct dwc3_event_buffer *evt;
283 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
284 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
285 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
286 | DWC3_GEVNTSIZ_SIZE(0));
287 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
290 static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
292 if (!dwc->has_hibernation)
295 if (!dwc->nr_scratch)
298 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
299 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
300 if (!dwc->scratchbuf)
306 static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
308 dma_addr_t scratch_addr;
312 if (!dwc->has_hibernation)
315 if (!dwc->nr_scratch)
318 /* should never fall here */
319 if (!WARN_ON(dwc->scratchbuf))
322 scratch_addr = dma_map_single(dwc->dev, dwc->scratchbuf,
323 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
325 if (dma_mapping_error(dwc->dev, scratch_addr)) {
326 dev_err(dwc->dev, "failed to map scratch buffer\n");
331 dwc->scratch_addr = scratch_addr;
333 param = lower_32_bits(scratch_addr);
335 ret = dwc3_send_gadget_generic_command(dwc,
336 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
340 param = upper_32_bits(scratch_addr);
342 ret = dwc3_send_gadget_generic_command(dwc,
343 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
350 dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
351 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
357 static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
359 if (!dwc->has_hibernation)
362 if (!dwc->nr_scratch)
365 /* should never fall here */
366 if (!WARN_ON(dwc->scratchbuf))
369 dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
370 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
371 kfree(dwc->scratchbuf);
374 static void dwc3_core_num_eps(struct dwc3 *dwc)
376 struct dwc3_hwparams *parms = &dwc->hwparams;
378 dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
379 dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
381 dwc3_trace(trace_dwc3_core, "found %d IN and %d OUT endpoints",
382 dwc->num_in_eps, dwc->num_out_eps);
385 static void dwc3_cache_hwparams(struct dwc3 *dwc)
387 struct dwc3_hwparams *parms = &dwc->hwparams;
389 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
390 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
391 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
392 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
393 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
394 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
395 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
396 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
397 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
401 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
402 * @dwc: Pointer to our controller context structure
404 * Returns 0 on success. The USB PHY interfaces are configured but not
405 * initialized. The PHY interfaces and the PHYs get initialized together with
406 * the core in dwc3_core_init.
408 static int dwc3_phy_setup(struct dwc3 *dwc)
413 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
416 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
417 * to '0' during coreConsultant configuration. So default value
418 * will be '0' when the core is reset. Application needs to set it
419 * to '1' after the core initialization is completed.
421 if (dwc->revision > DWC3_REVISION_194A)
422 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
424 if (dwc->u2ss_inp3_quirk)
425 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
427 if (dwc->dis_rxdet_inp3_quirk)
428 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
430 if (dwc->req_p1p2p3_quirk)
431 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
433 if (dwc->del_p1p2p3_quirk)
434 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
436 if (dwc->del_phy_power_chg_quirk)
437 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
439 if (dwc->lfps_filter_quirk)
440 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
442 if (dwc->rx_detect_poll_quirk)
443 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
445 if (dwc->tx_de_emphasis_quirk)
446 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
448 if (dwc->dis_u3_susphy_quirk)
449 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
451 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
453 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
455 /* Select the HS PHY interface */
456 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
457 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
458 if (dwc->hsphy_interface &&
459 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
460 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
462 } else if (dwc->hsphy_interface &&
463 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
464 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
465 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
467 /* Relying on default value. */
468 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
472 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
473 /* Making sure the interface and PHY are operational */
474 ret = dwc3_soft_reset(dwc);
480 ret = dwc3_ulpi_init(dwc);
488 switch (dwc->hsphy_mode) {
489 case USBPHY_INTERFACE_MODE_UTMI:
490 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
491 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
492 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
493 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
495 case USBPHY_INTERFACE_MODE_UTMIW:
496 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
497 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
498 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
499 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
506 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
507 * '0' during coreConsultant configuration. So default value will
508 * be '0' when the core is reset. Application needs to set it to
509 * '1' after the core initialization is completed.
511 if (dwc->revision > DWC3_REVISION_194A)
512 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
514 if (dwc->dis_u2_susphy_quirk)
515 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
517 if (dwc->dis_enblslpm_quirk)
518 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
520 if (dwc->dis_u2_freeclk_exists_quirk)
521 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
523 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
528 static void dwc3_core_exit(struct dwc3 *dwc)
530 dwc3_event_buffers_cleanup(dwc);
532 usb_phy_shutdown(dwc->usb2_phy);
533 usb_phy_shutdown(dwc->usb3_phy);
534 phy_exit(dwc->usb2_generic_phy);
535 phy_exit(dwc->usb3_generic_phy);
537 usb_phy_set_suspend(dwc->usb2_phy, 1);
538 usb_phy_set_suspend(dwc->usb3_phy, 1);
539 phy_power_off(dwc->usb2_generic_phy);
540 phy_power_off(dwc->usb3_generic_phy);
544 * dwc3_core_init - Low-level initialization of DWC3 Core
545 * @dwc: Pointer to our controller context structure
547 * Returns 0 on success otherwise negative errno.
549 static int dwc3_core_init(struct dwc3 *dwc)
551 u32 hwparams4 = dwc->hwparams.hwparams4;
555 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
556 /* This should read as U3 followed by revision number */
557 if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
558 /* Detected DWC_usb3 IP */
560 } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
561 /* Detected DWC_usb31 IP */
562 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
563 dwc->revision |= DWC3_REVISION_IS_DWC31;
565 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
571 * Write Linux Version Code to our GUID register so it's easy to figure
572 * out which kernel version a bug was found.
574 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
576 /* Handle USB2.0-only core configuration */
577 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
578 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
579 if (dwc->maximum_speed == USB_SPEED_SUPER)
580 dwc->maximum_speed = USB_SPEED_HIGH;
583 /* issue device SoftReset too */
584 ret = dwc3_soft_reset(dwc);
588 ret = dwc3_core_soft_reset(dwc);
592 ret = dwc3_phy_setup(dwc);
596 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
597 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
599 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
600 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
602 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
603 * issue which would cause xHCI compliance tests to fail.
605 * Because of that we cannot enable clock gating on such
610 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
613 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
614 dwc->dr_mode == USB_DR_MODE_OTG) &&
615 (dwc->revision >= DWC3_REVISION_210A &&
616 dwc->revision <= DWC3_REVISION_250A))
617 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
619 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
621 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
622 /* enable hibernation here */
623 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
626 * REVISIT Enabling this bit so that host-mode hibernation
627 * will work. Device-mode hibernation is not yet implemented.
629 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
632 dwc3_trace(trace_dwc3_core, "No power optimization available\n");
635 /* check if current dwc3 is on simulation board */
636 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
637 dwc3_trace(trace_dwc3_core,
638 "running on FPGA platform\n");
642 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
643 "disable_scramble cannot be used on non-FPGA builds\n");
645 if (dwc->disable_scramble_quirk && dwc->is_fpga)
646 reg |= DWC3_GCTL_DISSCRAMBLE;
648 reg &= ~DWC3_GCTL_DISSCRAMBLE;
650 if (dwc->u2exit_lfps_quirk)
651 reg |= DWC3_GCTL_U2EXIT_LFPS;
654 * WORKAROUND: DWC3 revisions <1.90a have a bug
655 * where the device can fail to connect at SuperSpeed
656 * and falls back to high-speed mode which causes
657 * the device to enter a Connect/Disconnect loop
659 if (dwc->revision < DWC3_REVISION_190A)
660 reg |= DWC3_GCTL_U2RSTECN;
662 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
664 dwc3_core_num_eps(dwc);
666 ret = dwc3_setup_scratch_buffers(dwc);
670 /* Adjust Frame Length */
671 dwc3_frame_length_adjustment(dwc);
673 usb_phy_set_suspend(dwc->usb2_phy, 0);
674 usb_phy_set_suspend(dwc->usb3_phy, 0);
675 ret = phy_power_on(dwc->usb2_generic_phy);
679 ret = phy_power_on(dwc->usb3_generic_phy);
683 ret = dwc3_event_buffers_setup(dwc);
685 dev_err(dwc->dev, "failed to setup event buffers\n");
692 phy_power_off(dwc->usb2_generic_phy);
695 phy_power_off(dwc->usb3_generic_phy);
698 usb_phy_set_suspend(dwc->usb2_phy, 1);
699 usb_phy_set_suspend(dwc->usb3_phy, 1);
703 usb_phy_shutdown(dwc->usb2_phy);
704 usb_phy_shutdown(dwc->usb3_phy);
705 phy_exit(dwc->usb2_generic_phy);
706 phy_exit(dwc->usb3_generic_phy);
712 static int dwc3_core_get_phy(struct dwc3 *dwc)
714 struct device *dev = dwc->dev;
715 struct device_node *node = dev->of_node;
719 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
720 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
722 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
723 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
726 if (IS_ERR(dwc->usb2_phy)) {
727 ret = PTR_ERR(dwc->usb2_phy);
728 if (ret == -ENXIO || ret == -ENODEV) {
729 dwc->usb2_phy = NULL;
730 } else if (ret == -EPROBE_DEFER) {
733 dev_err(dev, "no usb2 phy configured\n");
738 if (IS_ERR(dwc->usb3_phy)) {
739 ret = PTR_ERR(dwc->usb3_phy);
740 if (ret == -ENXIO || ret == -ENODEV) {
741 dwc->usb3_phy = NULL;
742 } else if (ret == -EPROBE_DEFER) {
745 dev_err(dev, "no usb3 phy configured\n");
750 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
751 if (IS_ERR(dwc->usb2_generic_phy)) {
752 ret = PTR_ERR(dwc->usb2_generic_phy);
753 if (ret == -ENOSYS || ret == -ENODEV) {
754 dwc->usb2_generic_phy = NULL;
755 } else if (ret == -EPROBE_DEFER) {
758 dev_err(dev, "no usb2 phy configured\n");
763 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
764 if (IS_ERR(dwc->usb3_generic_phy)) {
765 ret = PTR_ERR(dwc->usb3_generic_phy);
766 if (ret == -ENOSYS || ret == -ENODEV) {
767 dwc->usb3_generic_phy = NULL;
768 } else if (ret == -EPROBE_DEFER) {
771 dev_err(dev, "no usb3 phy configured\n");
779 static int dwc3_core_init_mode(struct dwc3 *dwc)
781 struct device *dev = dwc->dev;
784 switch (dwc->dr_mode) {
785 case USB_DR_MODE_PERIPHERAL:
786 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
787 ret = dwc3_gadget_init(dwc);
789 if (ret != -EPROBE_DEFER)
790 dev_err(dev, "failed to initialize gadget\n");
794 case USB_DR_MODE_HOST:
795 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
796 ret = dwc3_host_init(dwc);
798 if (ret != -EPROBE_DEFER)
799 dev_err(dev, "failed to initialize host\n");
803 case USB_DR_MODE_OTG:
804 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
805 ret = dwc3_host_init(dwc);
807 if (ret != -EPROBE_DEFER)
808 dev_err(dev, "failed to initialize host\n");
812 ret = dwc3_gadget_init(dwc);
814 if (ret != -EPROBE_DEFER)
815 dev_err(dev, "failed to initialize gadget\n");
820 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
827 static void dwc3_core_exit_mode(struct dwc3 *dwc)
829 switch (dwc->dr_mode) {
830 case USB_DR_MODE_PERIPHERAL:
831 dwc3_gadget_exit(dwc);
833 case USB_DR_MODE_HOST:
836 case USB_DR_MODE_OTG:
838 dwc3_gadget_exit(dwc);
846 #define DWC3_ALIGN_MASK (16 - 1)
848 static int dwc3_probe(struct platform_device *pdev)
850 struct device *dev = &pdev->dev;
851 struct resource *res;
853 u8 lpm_nyet_threshold;
862 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
866 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
870 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
872 dev_err(dev, "missing memory resource\n");
876 dwc->xhci_resources[0].start = res->start;
877 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
879 dwc->xhci_resources[0].flags = res->flags;
880 dwc->xhci_resources[0].name = res->name;
882 res->start += DWC3_GLOBALS_REGS_START;
885 * Request memory region but exclude xHCI regs,
886 * since it will be requested by the xhci-plat driver.
888 regs = devm_ioremap_resource(dev, res);
895 dwc->regs_size = resource_size(res);
897 /* default to highest possible threshold */
898 lpm_nyet_threshold = 0xff;
900 /* default to -3.5dB de-emphasis */
904 * default to assert utmi_sleep_n and use maximum allowed HIRD
905 * threshold value of 0b1100
909 dwc->maximum_speed = usb_get_maximum_speed(dev);
910 dwc->dr_mode = usb_get_dr_mode(dev);
911 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
913 dwc->has_lpm_erratum = device_property_read_bool(dev,
914 "snps,has-lpm-erratum");
915 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
916 &lpm_nyet_threshold);
917 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
918 "snps,is-utmi-l1-suspend");
919 device_property_read_u8(dev, "snps,hird-threshold",
921 dwc->usb3_lpm_capable = device_property_read_bool(dev,
922 "snps,usb3_lpm_capable");
924 dwc->disable_scramble_quirk = device_property_read_bool(dev,
925 "snps,disable_scramble_quirk");
926 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
927 "snps,u2exit_lfps_quirk");
928 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
929 "snps,u2ss_inp3_quirk");
930 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
931 "snps,req_p1p2p3_quirk");
932 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
933 "snps,del_p1p2p3_quirk");
934 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
935 "snps,del_phy_power_chg_quirk");
936 dwc->lfps_filter_quirk = device_property_read_bool(dev,
937 "snps,lfps_filter_quirk");
938 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
939 "snps,rx_detect_poll_quirk");
940 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
941 "snps,dis_u3_susphy_quirk");
942 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
943 "snps,dis_u2_susphy_quirk");
944 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
945 "snps,dis_enblslpm_quirk");
946 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
947 "snps,dis_rxdet_inp3_quirk");
948 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
949 "snps,dis-u2-freeclk-exists-quirk");
951 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
952 "snps,tx_de_emphasis_quirk");
953 device_property_read_u8(dev, "snps,tx_de_emphasis",
955 device_property_read_string(dev, "snps,hsphy_interface",
956 &dwc->hsphy_interface);
957 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
960 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
961 dwc->tx_de_emphasis = tx_de_emphasis;
963 dwc->hird_threshold = hird_threshold
964 | (dwc->is_utmi_l1_suspend << 4);
966 platform_set_drvdata(pdev, dwc);
967 dwc3_cache_hwparams(dwc);
969 ret = dwc3_core_get_phy(dwc);
973 spin_lock_init(&dwc->lock);
975 if (!dev->dma_mask) {
976 dev->dma_mask = dev->parent->dma_mask;
977 dev->dma_parms = dev->parent->dma_parms;
978 dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
981 pm_runtime_set_active(dev);
982 pm_runtime_use_autosuspend(dev);
983 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
984 pm_runtime_enable(dev);
985 ret = pm_runtime_get_sync(dev);
989 pm_runtime_forbid(dev);
991 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
993 dev_err(dwc->dev, "failed to allocate event buffers\n");
998 if (IS_ENABLED(CONFIG_USB_DWC3_HOST) &&
999 (dwc->dr_mode == USB_DR_MODE_OTG ||
1000 dwc->dr_mode == USB_DR_MODE_UNKNOWN))
1001 dwc->dr_mode = USB_DR_MODE_HOST;
1002 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET) &&
1003 (dwc->dr_mode == USB_DR_MODE_OTG ||
1004 dwc->dr_mode == USB_DR_MODE_UNKNOWN))
1005 dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
1007 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
1008 dwc->dr_mode = USB_DR_MODE_OTG;
1010 ret = dwc3_alloc_scratch_buffers(dwc);
1014 ret = dwc3_core_init(dwc);
1016 dev_err(dev, "failed to initialize core\n");
1020 /* Check the maximum_speed parameter */
1021 switch (dwc->maximum_speed) {
1023 case USB_SPEED_FULL:
1024 case USB_SPEED_HIGH:
1025 case USB_SPEED_SUPER:
1026 case USB_SPEED_SUPER_PLUS:
1029 dev_err(dev, "invalid maximum_speed parameter %d\n",
1030 dwc->maximum_speed);
1032 case USB_SPEED_UNKNOWN:
1033 /* default to superspeed */
1034 dwc->maximum_speed = USB_SPEED_SUPER;
1037 * default to superspeed plus if we are capable.
1039 if (dwc3_is_usb31(dwc) &&
1040 (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
1041 DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1042 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1047 ret = dwc3_core_init_mode(dwc);
1051 dwc3_debugfs_init(dwc);
1052 pm_runtime_put(dev);
1057 dwc3_event_buffers_cleanup(dwc);
1060 dwc3_free_scratch_buffers(dwc);
1063 dwc3_free_event_buffers(dwc);
1064 dwc3_ulpi_exit(dwc);
1067 pm_runtime_allow(&pdev->dev);
1070 pm_runtime_put_sync(&pdev->dev);
1071 pm_runtime_disable(&pdev->dev);
1075 * restore res->start back to its original value so that, in case the
1076 * probe is deferred, we don't end up getting error in request the
1077 * memory region the next time probe is called.
1079 res->start -= DWC3_GLOBALS_REGS_START;
1084 static int dwc3_remove(struct platform_device *pdev)
1086 struct dwc3 *dwc = platform_get_drvdata(pdev);
1087 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1089 pm_runtime_get_sync(&pdev->dev);
1091 * restore res->start back to its original value so that, in case the
1092 * probe is deferred, we don't end up getting error in request the
1093 * memory region the next time probe is called.
1095 res->start -= DWC3_GLOBALS_REGS_START;
1097 dwc3_debugfs_exit(dwc);
1098 dwc3_core_exit_mode(dwc);
1100 dwc3_core_exit(dwc);
1101 dwc3_ulpi_exit(dwc);
1103 pm_runtime_put_sync(&pdev->dev);
1104 pm_runtime_allow(&pdev->dev);
1105 pm_runtime_disable(&pdev->dev);
1107 dwc3_free_event_buffers(dwc);
1108 dwc3_free_scratch_buffers(dwc);
1114 static int dwc3_suspend_common(struct dwc3 *dwc)
1116 unsigned long flags;
1118 switch (dwc->dr_mode) {
1119 case USB_DR_MODE_PERIPHERAL:
1120 case USB_DR_MODE_OTG:
1121 spin_lock_irqsave(&dwc->lock, flags);
1122 dwc3_gadget_suspend(dwc);
1123 spin_unlock_irqrestore(&dwc->lock, flags);
1125 case USB_DR_MODE_HOST:
1131 dwc3_core_exit(dwc);
1136 static int dwc3_resume_common(struct dwc3 *dwc)
1138 unsigned long flags;
1141 ret = dwc3_core_init(dwc);
1145 switch (dwc->dr_mode) {
1146 case USB_DR_MODE_PERIPHERAL:
1147 case USB_DR_MODE_OTG:
1148 spin_lock_irqsave(&dwc->lock, flags);
1149 dwc3_gadget_resume(dwc);
1150 spin_unlock_irqrestore(&dwc->lock, flags);
1152 case USB_DR_MODE_HOST:
1161 static int dwc3_runtime_checks(struct dwc3 *dwc)
1163 switch (dwc->dr_mode) {
1164 case USB_DR_MODE_PERIPHERAL:
1165 case USB_DR_MODE_OTG:
1169 case USB_DR_MODE_HOST:
1178 static int dwc3_runtime_suspend(struct device *dev)
1180 struct dwc3 *dwc = dev_get_drvdata(dev);
1183 if (dwc3_runtime_checks(dwc))
1186 ret = dwc3_suspend_common(dwc);
1190 device_init_wakeup(dev, true);
1195 static int dwc3_runtime_resume(struct device *dev)
1197 struct dwc3 *dwc = dev_get_drvdata(dev);
1200 device_init_wakeup(dev, false);
1202 ret = dwc3_resume_common(dwc);
1206 switch (dwc->dr_mode) {
1207 case USB_DR_MODE_PERIPHERAL:
1208 case USB_DR_MODE_OTG:
1209 dwc3_gadget_process_pending_events(dwc);
1211 case USB_DR_MODE_HOST:
1217 pm_runtime_mark_last_busy(dev);
1222 static int dwc3_runtime_idle(struct device *dev)
1224 struct dwc3 *dwc = dev_get_drvdata(dev);
1226 switch (dwc->dr_mode) {
1227 case USB_DR_MODE_PERIPHERAL:
1228 case USB_DR_MODE_OTG:
1229 if (dwc3_runtime_checks(dwc))
1232 case USB_DR_MODE_HOST:
1238 pm_runtime_mark_last_busy(dev);
1239 pm_runtime_autosuspend(dev);
1243 #endif /* CONFIG_PM */
1245 #ifdef CONFIG_PM_SLEEP
1246 static int dwc3_suspend(struct device *dev)
1248 struct dwc3 *dwc = dev_get_drvdata(dev);
1251 ret = dwc3_suspend_common(dwc);
1255 pinctrl_pm_select_sleep_state(dev);
1260 static int dwc3_resume(struct device *dev)
1262 struct dwc3 *dwc = dev_get_drvdata(dev);
1265 pinctrl_pm_select_default_state(dev);
1267 ret = dwc3_resume_common(dwc);
1271 pm_runtime_disable(dev);
1272 pm_runtime_set_active(dev);
1273 pm_runtime_enable(dev);
1277 #endif /* CONFIG_PM_SLEEP */
1279 static const struct dev_pm_ops dwc3_dev_pm_ops = {
1280 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
1281 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
1286 static const struct of_device_id of_dwc3_match[] = {
1288 .compatible = "snps,dwc3"
1291 .compatible = "synopsys,dwc3"
1295 MODULE_DEVICE_TABLE(of, of_dwc3_match);
1300 #define ACPI_ID_INTEL_BSW "808622B7"
1302 static const struct acpi_device_id dwc3_acpi_match[] = {
1303 { ACPI_ID_INTEL_BSW, 0 },
1306 MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
1309 static struct platform_driver dwc3_driver = {
1310 .probe = dwc3_probe,
1311 .remove = dwc3_remove,
1314 .of_match_table = of_match_ptr(of_dwc3_match),
1315 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
1316 .pm = &dwc3_dev_pm_ops,
1320 module_platform_driver(dwc3_driver);
1322 MODULE_ALIAS("platform:dwc3");
1323 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1324 MODULE_LICENSE("GPL v2");
1325 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");