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/of_graph.h>
27 #include <linux/acpi.h>
28 #include <linux/pinctrl/consumer.h>
29 #include <linux/reset.h>
30 #include <linux/bitfield.h>
32 #include <linux/usb/ch9.h>
33 #include <linux/usb/gadget.h>
34 #include <linux/usb/of.h>
35 #include <linux/usb/otg.h>
42 #include "../host/xhci-ext-caps.h"
44 #define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
47 * dwc3_get_dr_mode - Validates and sets dr_mode
48 * @dwc: pointer to our context structure
50 static int dwc3_get_dr_mode(struct dwc3
*dwc
)
52 enum usb_dr_mode mode
;
53 struct device
*dev
= dwc
->dev
;
56 if (dwc
->dr_mode
== USB_DR_MODE_UNKNOWN
)
57 dwc
->dr_mode
= USB_DR_MODE_OTG
;
60 hw_mode
= DWC3_GHWPARAMS0_MODE(dwc
->hwparams
.hwparams0
);
63 case DWC3_GHWPARAMS0_MODE_GADGET
:
64 if (IS_ENABLED(CONFIG_USB_DWC3_HOST
)) {
66 "Controller does not support host mode.\n");
69 mode
= USB_DR_MODE_PERIPHERAL
;
71 case DWC3_GHWPARAMS0_MODE_HOST
:
72 if (IS_ENABLED(CONFIG_USB_DWC3_GADGET
)) {
74 "Controller does not support device mode.\n");
77 mode
= USB_DR_MODE_HOST
;
80 if (IS_ENABLED(CONFIG_USB_DWC3_HOST
))
81 mode
= USB_DR_MODE_HOST
;
82 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET
))
83 mode
= USB_DR_MODE_PERIPHERAL
;
86 * DWC_usb31 and DWC_usb3 v3.30a and higher do not support OTG
87 * mode. If the controller supports DRD but the dr_mode is not
88 * specified or set to OTG, then set the mode to peripheral.
90 if (mode
== USB_DR_MODE_OTG
&& !dwc
->edev
&&
91 (!IS_ENABLED(CONFIG_USB_ROLE_SWITCH
) ||
92 !device_property_read_bool(dwc
->dev
, "usb-role-switch")) &&
93 !DWC3_VER_IS_PRIOR(DWC3
, 330A
))
94 mode
= USB_DR_MODE_PERIPHERAL
;
97 if (mode
!= dwc
->dr_mode
) {
99 "Configuration mismatch. dr_mode forced to %s\n",
100 mode
== USB_DR_MODE_HOST
? "host" : "gadget");
108 void dwc3_enable_susphy(struct dwc3
*dwc
, bool enable
)
113 for (i
= 0; i
< dwc
->num_usb3_ports
; i
++) {
114 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB3PIPECTL(i
));
115 if (enable
&& !dwc
->dis_u3_susphy_quirk
)
116 reg
|= DWC3_GUSB3PIPECTL_SUSPHY
;
118 reg
&= ~DWC3_GUSB3PIPECTL_SUSPHY
;
120 dwc3_writel(dwc
->regs
, DWC3_GUSB3PIPECTL(i
), reg
);
123 for (i
= 0; i
< dwc
->num_usb2_ports
; i
++) {
124 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(i
));
125 if (enable
&& !dwc
->dis_u2_susphy_quirk
)
126 reg
|= DWC3_GUSB2PHYCFG_SUSPHY
;
128 reg
&= ~DWC3_GUSB2PHYCFG_SUSPHY
;
130 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(i
), reg
);
134 void dwc3_set_prtcap(struct dwc3
*dwc
, u32 mode
)
138 reg
= dwc3_readl(dwc
->regs
, DWC3_GCTL
);
139 reg
&= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG
));
140 reg
|= DWC3_GCTL_PRTCAPDIR(mode
);
141 dwc3_writel(dwc
->regs
, DWC3_GCTL
, reg
);
143 dwc
->current_dr_role
= mode
;
146 static void __dwc3_set_mode(struct work_struct
*work
)
148 struct dwc3
*dwc
= work_to_dwc(work
);
155 mutex_lock(&dwc
->mutex
);
156 spin_lock_irqsave(&dwc
->lock
, flags
);
157 desired_dr_role
= dwc
->desired_dr_role
;
158 spin_unlock_irqrestore(&dwc
->lock
, flags
);
160 pm_runtime_get_sync(dwc
->dev
);
162 if (dwc
->current_dr_role
== DWC3_GCTL_PRTCAP_OTG
)
163 dwc3_otg_update(dwc
, 0);
165 if (!desired_dr_role
)
168 if (desired_dr_role
== dwc
->current_dr_role
)
171 if (desired_dr_role
== DWC3_GCTL_PRTCAP_OTG
&& dwc
->edev
)
174 switch (dwc
->current_dr_role
) {
175 case DWC3_GCTL_PRTCAP_HOST
:
178 case DWC3_GCTL_PRTCAP_DEVICE
:
179 dwc3_gadget_exit(dwc
);
180 dwc3_event_buffers_cleanup(dwc
);
182 case DWC3_GCTL_PRTCAP_OTG
:
184 spin_lock_irqsave(&dwc
->lock
, flags
);
185 dwc
->desired_otg_role
= DWC3_OTG_ROLE_IDLE
;
186 spin_unlock_irqrestore(&dwc
->lock
, flags
);
187 dwc3_otg_update(dwc
, 1);
194 * When current_dr_role is not set, there's no role switching.
195 * Only perform GCTL.CoreSoftReset when there's DRD role switching.
197 if (dwc
->current_dr_role
&& ((DWC3_IP_IS(DWC3
) ||
198 DWC3_VER_IS_PRIOR(DWC31
, 190A
)) &&
199 desired_dr_role
!= DWC3_GCTL_PRTCAP_OTG
)) {
200 reg
= dwc3_readl(dwc
->regs
, DWC3_GCTL
);
201 reg
|= DWC3_GCTL_CORESOFTRESET
;
202 dwc3_writel(dwc
->regs
, DWC3_GCTL
, reg
);
205 * Wait for internal clocks to synchronized. DWC_usb31 and
206 * DWC_usb32 may need at least 50ms (less for DWC_usb3). To
207 * keep it consistent across different IPs, let's wait up to
208 * 100ms before clearing GCTL.CORESOFTRESET.
212 reg
= dwc3_readl(dwc
->regs
, DWC3_GCTL
);
213 reg
&= ~DWC3_GCTL_CORESOFTRESET
;
214 dwc3_writel(dwc
->regs
, DWC3_GCTL
, reg
);
217 spin_lock_irqsave(&dwc
->lock
, flags
);
219 dwc3_set_prtcap(dwc
, desired_dr_role
);
221 spin_unlock_irqrestore(&dwc
->lock
, flags
);
223 switch (desired_dr_role
) {
224 case DWC3_GCTL_PRTCAP_HOST
:
225 ret
= dwc3_host_init(dwc
);
227 dev_err(dwc
->dev
, "failed to initialize host\n");
230 otg_set_vbus(dwc
->usb2_phy
->otg
, true);
232 for (i
= 0; i
< dwc
->num_usb2_ports
; i
++)
233 phy_set_mode(dwc
->usb2_generic_phy
[i
], PHY_MODE_USB_HOST
);
234 for (i
= 0; i
< dwc
->num_usb3_ports
; i
++)
235 phy_set_mode(dwc
->usb3_generic_phy
[i
], PHY_MODE_USB_HOST
);
237 if (dwc
->dis_split_quirk
) {
238 reg
= dwc3_readl(dwc
->regs
, DWC3_GUCTL3
);
239 reg
|= DWC3_GUCTL3_SPLITDISABLE
;
240 dwc3_writel(dwc
->regs
, DWC3_GUCTL3
, reg
);
244 case DWC3_GCTL_PRTCAP_DEVICE
:
245 dwc3_core_soft_reset(dwc
);
247 dwc3_event_buffers_setup(dwc
);
250 otg_set_vbus(dwc
->usb2_phy
->otg
, false);
251 phy_set_mode(dwc
->usb2_generic_phy
[0], PHY_MODE_USB_DEVICE
);
252 phy_set_mode(dwc
->usb3_generic_phy
[0], PHY_MODE_USB_DEVICE
);
254 ret
= dwc3_gadget_init(dwc
);
256 dev_err(dwc
->dev
, "failed to initialize peripheral\n");
258 case DWC3_GCTL_PRTCAP_OTG
:
260 dwc3_otg_update(dwc
, 0);
267 pm_runtime_mark_last_busy(dwc
->dev
);
268 pm_runtime_put_autosuspend(dwc
->dev
);
269 mutex_unlock(&dwc
->mutex
);
272 void dwc3_set_mode(struct dwc3
*dwc
, u32 mode
)
276 if (dwc
->dr_mode
!= USB_DR_MODE_OTG
)
279 spin_lock_irqsave(&dwc
->lock
, flags
);
280 dwc
->desired_dr_role
= mode
;
281 spin_unlock_irqrestore(&dwc
->lock
, flags
);
283 queue_work(system_freezable_wq
, &dwc
->drd_work
);
286 u32
dwc3_core_fifo_space(struct dwc3_ep
*dep
, u8 type
)
288 struct dwc3
*dwc
= dep
->dwc
;
291 dwc3_writel(dwc
->regs
, DWC3_GDBGFIFOSPACE
,
292 DWC3_GDBGFIFOSPACE_NUM(dep
->number
) |
293 DWC3_GDBGFIFOSPACE_TYPE(type
));
295 reg
= dwc3_readl(dwc
->regs
, DWC3_GDBGFIFOSPACE
);
297 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg
);
301 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
302 * @dwc: pointer to our context structure
304 int dwc3_core_soft_reset(struct dwc3
*dwc
)
310 * We're resetting only the device side because, if we're in host mode,
311 * XHCI driver will reset the host block. If dwc3 was configured for
312 * host-only mode, then we can return early.
314 if (dwc
->current_dr_role
== DWC3_GCTL_PRTCAP_HOST
)
317 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
318 reg
|= DWC3_DCTL_CSFTRST
;
319 reg
&= ~DWC3_DCTL_RUN_STOP
;
320 dwc3_gadget_dctl_write_safe(dwc
, reg
);
323 * For DWC_usb31 controller 1.90a and later, the DCTL.CSFRST bit
324 * is cleared only after all the clocks are synchronized. This can
325 * take a little more than 50ms. Set the polling rate at 20ms
326 * for 10 times instead.
328 if (DWC3_VER_IS_WITHIN(DWC31
, 190A
, ANY
) || DWC3_IP_IS(DWC32
))
332 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
333 if (!(reg
& DWC3_DCTL_CSFTRST
))
336 if (DWC3_VER_IS_WITHIN(DWC31
, 190A
, ANY
) || DWC3_IP_IS(DWC32
))
342 dev_warn(dwc
->dev
, "DWC3 controller soft reset failed.\n");
347 * For DWC_usb31 controller 1.80a and prior, once DCTL.CSFRST bit
348 * is cleared, we must wait at least 50ms before accessing the PHY
349 * domain (synchronization delay).
351 if (DWC3_VER_IS_WITHIN(DWC31
, ANY
, 180A
))
358 * dwc3_frame_length_adjustment - Adjusts frame length if required
359 * @dwc3: Pointer to our controller context structure
361 static void dwc3_frame_length_adjustment(struct dwc3
*dwc
)
366 if (DWC3_VER_IS_PRIOR(DWC3
, 250A
))
372 reg
= dwc3_readl(dwc
->regs
, DWC3_GFLADJ
);
373 dft
= reg
& DWC3_GFLADJ_30MHZ_MASK
;
374 if (dft
!= dwc
->fladj
) {
375 reg
&= ~DWC3_GFLADJ_30MHZ_MASK
;
376 reg
|= DWC3_GFLADJ_30MHZ_SDBND_SEL
| dwc
->fladj
;
377 dwc3_writel(dwc
->regs
, DWC3_GFLADJ
, reg
);
382 * dwc3_ref_clk_period - Reference clock period configuration
383 * Default reference clock period depends on hardware
384 * configuration. For systems with reference clock that differs
385 * from the default, this will set clock period in DWC3_GUCTL
387 * @dwc: Pointer to our controller context structure
389 static void dwc3_ref_clk_period(struct dwc3
*dwc
)
391 unsigned long period
;
398 rate
= clk_get_rate(dwc
->ref_clk
);
401 period
= NSEC_PER_SEC
/ rate
;
402 } else if (dwc
->ref_clk_per
) {
403 period
= dwc
->ref_clk_per
;
404 rate
= NSEC_PER_SEC
/ period
;
409 reg
= dwc3_readl(dwc
->regs
, DWC3_GUCTL
);
410 reg
&= ~DWC3_GUCTL_REFCLKPER_MASK
;
411 reg
|= FIELD_PREP(DWC3_GUCTL_REFCLKPER_MASK
, period
);
412 dwc3_writel(dwc
->regs
, DWC3_GUCTL
, reg
);
414 if (DWC3_VER_IS_PRIOR(DWC3
, 250A
))
418 * The calculation below is
420 * 125000 * (NSEC_PER_SEC / (rate * period) - 1)
422 * but rearranged for fixed-point arithmetic. The division must be
423 * 64-bit because 125000 * NSEC_PER_SEC doesn't fit in 32 bits (and
424 * neither does rate * period).
426 * Note that rate * period ~= NSEC_PER_SECOND, minus the number of
427 * nanoseconds of error caused by the truncation which happened during
428 * the division when calculating rate or period (whichever one was
429 * derived from the other). We first calculate the relative error, then
430 * scale it to units of 8 ppm.
432 fladj
= div64_u64(125000ULL * NSEC_PER_SEC
, (u64
)rate
* period
);
436 * The documented 240MHz constant is scaled by 2 to get PLS1 as well.
438 decr
= 480000000 / rate
;
440 reg
= dwc3_readl(dwc
->regs
, DWC3_GFLADJ
);
441 reg
&= ~DWC3_GFLADJ_REFCLK_FLADJ_MASK
442 & ~DWC3_GFLADJ_240MHZDECR
443 & ~DWC3_GFLADJ_240MHZDECR_PLS1
;
444 reg
|= FIELD_PREP(DWC3_GFLADJ_REFCLK_FLADJ_MASK
, fladj
)
445 | FIELD_PREP(DWC3_GFLADJ_240MHZDECR
, decr
>> 1)
446 | FIELD_PREP(DWC3_GFLADJ_240MHZDECR_PLS1
, decr
& 1);
448 if (dwc
->gfladj_refclk_lpm_sel
)
449 reg
|= DWC3_GFLADJ_REFCLK_LPM_SEL
;
451 dwc3_writel(dwc
->regs
, DWC3_GFLADJ
, reg
);
455 * dwc3_free_one_event_buffer - Frees one event buffer
456 * @dwc: Pointer to our controller context structure
457 * @evt: Pointer to event buffer to be freed
459 static void dwc3_free_one_event_buffer(struct dwc3
*dwc
,
460 struct dwc3_event_buffer
*evt
)
462 dma_free_coherent(dwc
->sysdev
, evt
->length
, evt
->buf
, evt
->dma
);
466 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
467 * @dwc: Pointer to our controller context structure
468 * @length: size of the event buffer
470 * Returns a pointer to the allocated event buffer structure on success
471 * otherwise ERR_PTR(errno).
473 static struct dwc3_event_buffer
*dwc3_alloc_one_event_buffer(struct dwc3
*dwc
,
476 struct dwc3_event_buffer
*evt
;
478 evt
= devm_kzalloc(dwc
->dev
, sizeof(*evt
), GFP_KERNEL
);
480 return ERR_PTR(-ENOMEM
);
483 evt
->length
= length
;
484 evt
->cache
= devm_kzalloc(dwc
->dev
, length
, GFP_KERNEL
);
486 return ERR_PTR(-ENOMEM
);
488 evt
->buf
= dma_alloc_coherent(dwc
->sysdev
, length
,
489 &evt
->dma
, GFP_KERNEL
);
491 return ERR_PTR(-ENOMEM
);
497 * dwc3_free_event_buffers - frees all allocated event buffers
498 * @dwc: Pointer to our controller context structure
500 static void dwc3_free_event_buffers(struct dwc3
*dwc
)
502 struct dwc3_event_buffer
*evt
;
506 dwc3_free_one_event_buffer(dwc
, evt
);
510 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
511 * @dwc: pointer to our controller context structure
512 * @length: size of event buffer
514 * Returns 0 on success otherwise negative errno. In the error case, dwc
515 * may contain some buffers allocated but not all which were requested.
517 static int dwc3_alloc_event_buffers(struct dwc3
*dwc
, unsigned int length
)
519 struct dwc3_event_buffer
*evt
;
520 unsigned int hw_mode
;
522 hw_mode
= DWC3_GHWPARAMS0_MODE(dwc
->hwparams
.hwparams0
);
523 if (hw_mode
== DWC3_GHWPARAMS0_MODE_HOST
) {
528 evt
= dwc3_alloc_one_event_buffer(dwc
, length
);
530 dev_err(dwc
->dev
, "can't allocate event buffer\n");
539 * dwc3_event_buffers_setup - setup our allocated event buffers
540 * @dwc: pointer to our controller context structure
542 * Returns 0 on success otherwise negative errno.
544 int dwc3_event_buffers_setup(struct dwc3
*dwc
)
546 struct dwc3_event_buffer
*evt
;
554 dwc3_writel(dwc
->regs
, DWC3_GEVNTADRLO(0),
555 lower_32_bits(evt
->dma
));
556 dwc3_writel(dwc
->regs
, DWC3_GEVNTADRHI(0),
557 upper_32_bits(evt
->dma
));
558 dwc3_writel(dwc
->regs
, DWC3_GEVNTSIZ(0),
559 DWC3_GEVNTSIZ_SIZE(evt
->length
));
561 /* Clear any stale event */
562 reg
= dwc3_readl(dwc
->regs
, DWC3_GEVNTCOUNT(0));
563 dwc3_writel(dwc
->regs
, DWC3_GEVNTCOUNT(0), reg
);
567 void dwc3_event_buffers_cleanup(struct dwc3
*dwc
)
569 struct dwc3_event_buffer
*evt
;
575 * Exynos platforms may not be able to access event buffer if the
576 * controller failed to halt on dwc3_core_exit().
578 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
579 if (!(reg
& DWC3_DSTS_DEVCTRLHLT
))
586 dwc3_writel(dwc
->regs
, DWC3_GEVNTADRLO(0), 0);
587 dwc3_writel(dwc
->regs
, DWC3_GEVNTADRHI(0), 0);
588 dwc3_writel(dwc
->regs
, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
589 | DWC3_GEVNTSIZ_SIZE(0));
591 /* Clear any stale event */
592 reg
= dwc3_readl(dwc
->regs
, DWC3_GEVNTCOUNT(0));
593 dwc3_writel(dwc
->regs
, DWC3_GEVNTCOUNT(0), reg
);
596 static void dwc3_core_num_eps(struct dwc3
*dwc
)
598 struct dwc3_hwparams
*parms
= &dwc
->hwparams
;
600 dwc
->num_eps
= DWC3_NUM_EPS(parms
);
603 static void dwc3_cache_hwparams(struct dwc3
*dwc
)
605 struct dwc3_hwparams
*parms
= &dwc
->hwparams
;
607 parms
->hwparams0
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS0
);
608 parms
->hwparams1
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS1
);
609 parms
->hwparams2
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS2
);
610 parms
->hwparams3
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS3
);
611 parms
->hwparams4
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS4
);
612 parms
->hwparams5
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS5
);
613 parms
->hwparams6
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS6
);
614 parms
->hwparams7
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS7
);
615 parms
->hwparams8
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS8
);
617 if (DWC3_IP_IS(DWC32
))
618 parms
->hwparams9
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS9
);
621 static void dwc3_config_soc_bus(struct dwc3
*dwc
)
623 if (dwc
->gsbuscfg0_reqinfo
!= DWC3_GSBUSCFG0_REQINFO_UNSPECIFIED
) {
626 reg
= dwc3_readl(dwc
->regs
, DWC3_GSBUSCFG0
);
627 reg
&= ~DWC3_GSBUSCFG0_REQINFO(~0);
628 reg
|= DWC3_GSBUSCFG0_REQINFO(dwc
->gsbuscfg0_reqinfo
);
629 dwc3_writel(dwc
->regs
, DWC3_GSBUSCFG0
, reg
);
633 static int dwc3_core_ulpi_init(struct dwc3
*dwc
)
638 intf
= DWC3_GHWPARAMS3_HSPHY_IFC(dwc
->hwparams
.hwparams3
);
640 if (intf
== DWC3_GHWPARAMS3_HSPHY_IFC_ULPI
||
641 (intf
== DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI
&&
642 dwc
->hsphy_interface
&&
643 !strncmp(dwc
->hsphy_interface
, "ulpi", 4)))
644 ret
= dwc3_ulpi_init(dwc
);
649 static int dwc3_ss_phy_setup(struct dwc3
*dwc
, int index
)
653 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB3PIPECTL(index
));
656 * Make sure UX_EXIT_PX is cleared as that causes issues with some
657 * PHYs. Also, this bit is not supposed to be used in normal operation.
659 reg
&= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX
;
662 * Above DWC_usb3.0 1.94a, it is recommended to set
663 * DWC3_GUSB3PIPECTL_SUSPHY to '0' during coreConsultant configuration.
664 * So default value will be '0' when the core is reset. Application
665 * needs to set it to '1' after the core initialization is completed.
667 * Similarly for DRD controllers, GUSB3PIPECTL.SUSPENDENABLE must be
668 * cleared after power-on reset, and it can be set after core
671 reg
&= ~DWC3_GUSB3PIPECTL_SUSPHY
;
673 if (dwc
->u2ss_inp3_quirk
)
674 reg
|= DWC3_GUSB3PIPECTL_U2SSINP3OK
;
676 if (dwc
->dis_rxdet_inp3_quirk
)
677 reg
|= DWC3_GUSB3PIPECTL_DISRXDETINP3
;
679 if (dwc
->req_p1p2p3_quirk
)
680 reg
|= DWC3_GUSB3PIPECTL_REQP1P2P3
;
682 if (dwc
->del_p1p2p3_quirk
)
683 reg
|= DWC3_GUSB3PIPECTL_DEP1P2P3_EN
;
685 if (dwc
->del_phy_power_chg_quirk
)
686 reg
|= DWC3_GUSB3PIPECTL_DEPOCHANGE
;
688 if (dwc
->lfps_filter_quirk
)
689 reg
|= DWC3_GUSB3PIPECTL_LFPSFILT
;
691 if (dwc
->rx_detect_poll_quirk
)
692 reg
|= DWC3_GUSB3PIPECTL_RX_DETOPOLL
;
694 if (dwc
->tx_de_emphasis_quirk
)
695 reg
|= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc
->tx_de_emphasis
);
697 if (dwc
->dis_del_phy_power_chg_quirk
)
698 reg
&= ~DWC3_GUSB3PIPECTL_DEPOCHANGE
;
700 dwc3_writel(dwc
->regs
, DWC3_GUSB3PIPECTL(index
), reg
);
705 static int dwc3_hs_phy_setup(struct dwc3
*dwc
, int index
)
709 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(index
));
711 /* Select the HS PHY interface */
712 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc
->hwparams
.hwparams3
)) {
713 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI
:
714 if (dwc
->hsphy_interface
&&
715 !strncmp(dwc
->hsphy_interface
, "utmi", 4)) {
716 reg
&= ~DWC3_GUSB2PHYCFG_ULPI_UTMI
;
718 } else if (dwc
->hsphy_interface
&&
719 !strncmp(dwc
->hsphy_interface
, "ulpi", 4)) {
720 reg
|= DWC3_GUSB2PHYCFG_ULPI_UTMI
;
721 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(index
), reg
);
723 /* Relying on default value. */
724 if (!(reg
& DWC3_GUSB2PHYCFG_ULPI_UTMI
))
728 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI
:
733 switch (dwc
->hsphy_mode
) {
734 case USBPHY_INTERFACE_MODE_UTMI
:
735 reg
&= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK
|
736 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK
);
737 reg
|= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT
) |
738 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT
);
740 case USBPHY_INTERFACE_MODE_UTMIW
:
741 reg
&= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK
|
742 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK
);
743 reg
|= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT
) |
744 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT
);
751 * Above DWC_usb3.0 1.94a, it is recommended to set
752 * DWC3_GUSB2PHYCFG_SUSPHY to '0' during coreConsultant configuration.
753 * So default value will be '0' when the core is reset. Application
754 * needs to set it to '1' after the core initialization is completed.
756 * Similarly for DRD controllers, GUSB2PHYCFG.SUSPHY must be cleared
757 * after power-on reset, and it can be set after core initialization.
759 reg
&= ~DWC3_GUSB2PHYCFG_SUSPHY
;
761 if (dwc
->dis_enblslpm_quirk
)
762 reg
&= ~DWC3_GUSB2PHYCFG_ENBLSLPM
;
764 reg
|= DWC3_GUSB2PHYCFG_ENBLSLPM
;
766 if (dwc
->dis_u2_freeclk_exists_quirk
|| dwc
->gfladj_refclk_lpm_sel
)
767 reg
&= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS
;
770 * Some ULPI USB PHY does not support internal VBUS supply, to drive
771 * the CPEN pin requires the configuration of the ULPI DRVVBUSEXTERNAL
772 * bit of OTG_CTRL register. Controller configures the USB2 PHY
773 * ULPIEXTVBUSDRV bit[17] of the GUSB2PHYCFG register to drive vBus
774 * with an external supply.
776 if (dwc
->ulpi_ext_vbus_drv
)
777 reg
|= DWC3_GUSB2PHYCFG_ULPIEXTVBUSDRV
;
779 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(index
), reg
);
785 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
786 * @dwc: Pointer to our controller context structure
788 * Returns 0 on success. The USB PHY interfaces are configured but not
789 * initialized. The PHY interfaces and the PHYs get initialized together with
790 * the core in dwc3_core_init.
792 static int dwc3_phy_setup(struct dwc3
*dwc
)
797 for (i
= 0; i
< dwc
->num_usb3_ports
; i
++) {
798 ret
= dwc3_ss_phy_setup(dwc
, i
);
803 for (i
= 0; i
< dwc
->num_usb2_ports
; i
++) {
804 ret
= dwc3_hs_phy_setup(dwc
, i
);
812 static int dwc3_phy_init(struct dwc3
*dwc
)
818 usb_phy_init(dwc
->usb2_phy
);
819 usb_phy_init(dwc
->usb3_phy
);
821 for (i
= 0; i
< dwc
->num_usb2_ports
; i
++) {
822 ret
= phy_init(dwc
->usb2_generic_phy
[i
]);
824 goto err_exit_usb2_phy
;
827 for (j
= 0; j
< dwc
->num_usb3_ports
; j
++) {
828 ret
= phy_init(dwc
->usb3_generic_phy
[j
]);
830 goto err_exit_usb3_phy
;
837 phy_exit(dwc
->usb3_generic_phy
[j
]);
841 phy_exit(dwc
->usb2_generic_phy
[i
]);
843 usb_phy_shutdown(dwc
->usb3_phy
);
844 usb_phy_shutdown(dwc
->usb2_phy
);
849 static void dwc3_phy_exit(struct dwc3
*dwc
)
853 for (i
= 0; i
< dwc
->num_usb3_ports
; i
++)
854 phy_exit(dwc
->usb3_generic_phy
[i
]);
856 for (i
= 0; i
< dwc
->num_usb2_ports
; i
++)
857 phy_exit(dwc
->usb2_generic_phy
[i
]);
859 usb_phy_shutdown(dwc
->usb3_phy
);
860 usb_phy_shutdown(dwc
->usb2_phy
);
863 static int dwc3_phy_power_on(struct dwc3
*dwc
)
869 usb_phy_set_suspend(dwc
->usb2_phy
, 0);
870 usb_phy_set_suspend(dwc
->usb3_phy
, 0);
872 for (i
= 0; i
< dwc
->num_usb2_ports
; i
++) {
873 ret
= phy_power_on(dwc
->usb2_generic_phy
[i
]);
875 goto err_power_off_usb2_phy
;
878 for (j
= 0; j
< dwc
->num_usb3_ports
; j
++) {
879 ret
= phy_power_on(dwc
->usb3_generic_phy
[j
]);
881 goto err_power_off_usb3_phy
;
886 err_power_off_usb3_phy
:
888 phy_power_off(dwc
->usb3_generic_phy
[j
]);
890 err_power_off_usb2_phy
:
892 phy_power_off(dwc
->usb2_generic_phy
[i
]);
894 usb_phy_set_suspend(dwc
->usb3_phy
, 1);
895 usb_phy_set_suspend(dwc
->usb2_phy
, 1);
900 static void dwc3_phy_power_off(struct dwc3
*dwc
)
904 for (i
= 0; i
< dwc
->num_usb3_ports
; i
++)
905 phy_power_off(dwc
->usb3_generic_phy
[i
]);
907 for (i
= 0; i
< dwc
->num_usb2_ports
; i
++)
908 phy_power_off(dwc
->usb2_generic_phy
[i
]);
910 usb_phy_set_suspend(dwc
->usb3_phy
, 1);
911 usb_phy_set_suspend(dwc
->usb2_phy
, 1);
914 static int dwc3_clk_enable(struct dwc3
*dwc
)
918 ret
= clk_prepare_enable(dwc
->bus_clk
);
922 ret
= clk_prepare_enable(dwc
->ref_clk
);
924 goto disable_bus_clk
;
926 ret
= clk_prepare_enable(dwc
->susp_clk
);
928 goto disable_ref_clk
;
930 ret
= clk_prepare_enable(dwc
->utmi_clk
);
932 goto disable_susp_clk
;
934 ret
= clk_prepare_enable(dwc
->pipe_clk
);
936 goto disable_utmi_clk
;
941 clk_disable_unprepare(dwc
->utmi_clk
);
943 clk_disable_unprepare(dwc
->susp_clk
);
945 clk_disable_unprepare(dwc
->ref_clk
);
947 clk_disable_unprepare(dwc
->bus_clk
);
951 static void dwc3_clk_disable(struct dwc3
*dwc
)
953 clk_disable_unprepare(dwc
->pipe_clk
);
954 clk_disable_unprepare(dwc
->utmi_clk
);
955 clk_disable_unprepare(dwc
->susp_clk
);
956 clk_disable_unprepare(dwc
->ref_clk
);
957 clk_disable_unprepare(dwc
->bus_clk
);
960 static void dwc3_core_exit(struct dwc3
*dwc
)
962 dwc3_event_buffers_cleanup(dwc
);
963 dwc3_phy_power_off(dwc
);
965 dwc3_clk_disable(dwc
);
966 reset_control_assert(dwc
->reset
);
969 static bool dwc3_core_is_valid(struct dwc3
*dwc
)
973 reg
= dwc3_readl(dwc
->regs
, DWC3_GSNPSID
);
974 dwc
->ip
= DWC3_GSNPS_ID(reg
);
976 /* This should read as U3 followed by revision number */
977 if (DWC3_IP_IS(DWC3
)) {
979 } else if (DWC3_IP_IS(DWC31
) || DWC3_IP_IS(DWC32
)) {
980 dwc
->revision
= dwc3_readl(dwc
->regs
, DWC3_VER_NUMBER
);
981 dwc
->version_type
= dwc3_readl(dwc
->regs
, DWC3_VER_TYPE
);
989 static void dwc3_core_setup_global_control(struct dwc3
*dwc
)
991 unsigned int power_opt
;
992 unsigned int hw_mode
;
995 reg
= dwc3_readl(dwc
->regs
, DWC3_GCTL
);
996 reg
&= ~DWC3_GCTL_SCALEDOWN_MASK
;
997 hw_mode
= DWC3_GHWPARAMS0_MODE(dwc
->hwparams
.hwparams0
);
998 power_opt
= DWC3_GHWPARAMS1_EN_PWROPT(dwc
->hwparams
.hwparams1
);
1000 switch (power_opt
) {
1001 case DWC3_GHWPARAMS1_EN_PWROPT_CLK
:
1003 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
1004 * issue which would cause xHCI compliance tests to fail.
1006 * Because of that we cannot enable clock gating on such
1011 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
1014 if ((dwc
->dr_mode
== USB_DR_MODE_HOST
||
1015 dwc
->dr_mode
== USB_DR_MODE_OTG
) &&
1016 DWC3_VER_IS_WITHIN(DWC3
, 210A
, 250A
))
1017 reg
|= DWC3_GCTL_DSBLCLKGTNG
| DWC3_GCTL_SOFITPSYNC
;
1019 reg
&= ~DWC3_GCTL_DSBLCLKGTNG
;
1021 case DWC3_GHWPARAMS1_EN_PWROPT_HIB
:
1023 * REVISIT Enabling this bit so that host-mode hibernation
1024 * will work. Device-mode hibernation is not yet implemented.
1026 reg
|= DWC3_GCTL_GBLHIBERNATIONEN
;
1034 * This is a workaround for STAR#4846132, which only affects
1035 * DWC_usb31 version2.00a operating in host mode.
1037 * There is a problem in DWC_usb31 version 2.00a operating
1038 * in host mode that would cause a CSR read timeout When CSR
1039 * read coincides with RAM Clock Gating Entry. By disable
1040 * Clock Gating, sacrificing power consumption for normal
1043 if (power_opt
!= DWC3_GHWPARAMS1_EN_PWROPT_NO
&&
1044 hw_mode
!= DWC3_GHWPARAMS0_MODE_GADGET
&& DWC3_VER_IS(DWC31
, 200A
))
1045 reg
|= DWC3_GCTL_DSBLCLKGTNG
;
1047 /* check if current dwc3 is on simulation board */
1048 if (dwc
->hwparams
.hwparams6
& DWC3_GHWPARAMS6_EN_FPGA
) {
1049 dev_info(dwc
->dev
, "Running with FPGA optimizations\n");
1050 dwc
->is_fpga
= true;
1053 WARN_ONCE(dwc
->disable_scramble_quirk
&& !dwc
->is_fpga
,
1054 "disable_scramble cannot be used on non-FPGA builds\n");
1056 if (dwc
->disable_scramble_quirk
&& dwc
->is_fpga
)
1057 reg
|= DWC3_GCTL_DISSCRAMBLE
;
1059 reg
&= ~DWC3_GCTL_DISSCRAMBLE
;
1061 if (dwc
->u2exit_lfps_quirk
)
1062 reg
|= DWC3_GCTL_U2EXIT_LFPS
;
1065 * WORKAROUND: DWC3 revisions <1.90a have a bug
1066 * where the device can fail to connect at SuperSpeed
1067 * and falls back to high-speed mode which causes
1068 * the device to enter a Connect/Disconnect loop
1070 if (DWC3_VER_IS_PRIOR(DWC3
, 190A
))
1071 reg
|= DWC3_GCTL_U2RSTECN
;
1073 dwc3_writel(dwc
->regs
, DWC3_GCTL
, reg
);
1076 static int dwc3_core_get_phy(struct dwc3
*dwc
);
1077 static int dwc3_core_ulpi_init(struct dwc3
*dwc
);
1079 /* set global incr burst type configuration registers */
1080 static void dwc3_set_incr_burst_type(struct dwc3
*dwc
)
1082 struct device
*dev
= dwc
->dev
;
1083 /* incrx_mode : for INCR burst type. */
1085 /* incrx_size : for size of INCRX burst. */
1093 cfg
= dwc3_readl(dwc
->regs
, DWC3_GSBUSCFG0
);
1096 * Handle property "snps,incr-burst-type-adjustment".
1097 * Get the number of value from this property:
1098 * result <= 0, means this property is not supported.
1099 * result = 1, means INCRx burst mode supported.
1100 * result > 1, means undefined length burst mode supported.
1102 ntype
= device_property_count_u32(dev
, "snps,incr-burst-type-adjustment");
1106 vals
= kcalloc(ntype
, sizeof(u32
), GFP_KERNEL
);
1110 /* Get INCR burst type, and parse it */
1111 ret
= device_property_read_u32_array(dev
,
1112 "snps,incr-burst-type-adjustment", vals
, ntype
);
1115 dev_err(dev
, "Error to get property\n");
1122 /* INCRX (undefined length) burst mode */
1123 incrx_mode
= INCRX_UNDEF_LENGTH_BURST_MODE
;
1124 for (i
= 1; i
< ntype
; i
++) {
1125 if (vals
[i
] > incrx_size
)
1126 incrx_size
= vals
[i
];
1129 /* INCRX burst mode */
1130 incrx_mode
= INCRX_BURST_MODE
;
1135 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
1136 cfg
&= ~DWC3_GSBUSCFG0_INCRBRST_MASK
;
1138 cfg
|= DWC3_GSBUSCFG0_INCRBRSTENA
;
1139 switch (incrx_size
) {
1141 cfg
|= DWC3_GSBUSCFG0_INCR256BRSTENA
;
1144 cfg
|= DWC3_GSBUSCFG0_INCR128BRSTENA
;
1147 cfg
|= DWC3_GSBUSCFG0_INCR64BRSTENA
;
1150 cfg
|= DWC3_GSBUSCFG0_INCR32BRSTENA
;
1153 cfg
|= DWC3_GSBUSCFG0_INCR16BRSTENA
;
1156 cfg
|= DWC3_GSBUSCFG0_INCR8BRSTENA
;
1159 cfg
|= DWC3_GSBUSCFG0_INCR4BRSTENA
;
1164 dev_err(dev
, "Invalid property\n");
1168 dwc3_writel(dwc
->regs
, DWC3_GSBUSCFG0
, cfg
);
1171 static void dwc3_set_power_down_clk_scale(struct dwc3
*dwc
)
1180 * The power down scale field specifies how many suspend_clk
1181 * periods fit into a 16KHz clock period. When performing
1182 * the division, round up the remainder.
1184 * The power down scale value is calculated using the fastest
1185 * frequency of the suspend_clk. If it isn't fixed (but within
1186 * the accuracy requirement), the driver may not know the max
1187 * rate of the suspend_clk, so only update the power down scale
1188 * if the default is less than the calculated value from
1189 * clk_get_rate() or if the default is questionably high
1190 * (3x or more) to be within the requirement.
1192 scale
= DIV_ROUND_UP(clk_get_rate(dwc
->susp_clk
), 16000);
1193 reg
= dwc3_readl(dwc
->regs
, DWC3_GCTL
);
1194 if ((reg
& DWC3_GCTL_PWRDNSCALE_MASK
) < DWC3_GCTL_PWRDNSCALE(scale
) ||
1195 (reg
& DWC3_GCTL_PWRDNSCALE_MASK
) > DWC3_GCTL_PWRDNSCALE(scale
*3)) {
1196 reg
&= ~(DWC3_GCTL_PWRDNSCALE_MASK
);
1197 reg
|= DWC3_GCTL_PWRDNSCALE(scale
);
1198 dwc3_writel(dwc
->regs
, DWC3_GCTL
, reg
);
1202 static void dwc3_config_threshold(struct dwc3
*dwc
)
1211 * Must config both number of packets and max burst settings to enable
1212 * RX and/or TX threshold.
1214 if (!DWC3_IP_IS(DWC3
) && dwc
->dr_mode
== USB_DR_MODE_HOST
) {
1215 rx_thr_num
= dwc
->rx_thr_num_pkt_prd
;
1216 rx_maxburst
= dwc
->rx_max_burst_prd
;
1217 tx_thr_num
= dwc
->tx_thr_num_pkt_prd
;
1218 tx_maxburst
= dwc
->tx_max_burst_prd
;
1220 if (rx_thr_num
&& rx_maxburst
) {
1221 reg
= dwc3_readl(dwc
->regs
, DWC3_GRXTHRCFG
);
1222 reg
|= DWC31_RXTHRNUMPKTSEL_PRD
;
1224 reg
&= ~DWC31_RXTHRNUMPKT_PRD(~0);
1225 reg
|= DWC31_RXTHRNUMPKT_PRD(rx_thr_num
);
1227 reg
&= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
1228 reg
|= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst
);
1230 dwc3_writel(dwc
->regs
, DWC3_GRXTHRCFG
, reg
);
1233 if (tx_thr_num
&& tx_maxburst
) {
1234 reg
= dwc3_readl(dwc
->regs
, DWC3_GTXTHRCFG
);
1235 reg
|= DWC31_TXTHRNUMPKTSEL_PRD
;
1237 reg
&= ~DWC31_TXTHRNUMPKT_PRD(~0);
1238 reg
|= DWC31_TXTHRNUMPKT_PRD(tx_thr_num
);
1240 reg
&= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
1241 reg
|= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst
);
1243 dwc3_writel(dwc
->regs
, DWC3_GTXTHRCFG
, reg
);
1247 rx_thr_num
= dwc
->rx_thr_num_pkt
;
1248 rx_maxburst
= dwc
->rx_max_burst
;
1249 tx_thr_num
= dwc
->tx_thr_num_pkt
;
1250 tx_maxburst
= dwc
->tx_max_burst
;
1252 if (DWC3_IP_IS(DWC3
)) {
1253 if (rx_thr_num
&& rx_maxburst
) {
1254 reg
= dwc3_readl(dwc
->regs
, DWC3_GRXTHRCFG
);
1255 reg
|= DWC3_GRXTHRCFG_PKTCNTSEL
;
1257 reg
&= ~DWC3_GRXTHRCFG_RXPKTCNT(~0);
1258 reg
|= DWC3_GRXTHRCFG_RXPKTCNT(rx_thr_num
);
1260 reg
&= ~DWC3_GRXTHRCFG_MAXRXBURSTSIZE(~0);
1261 reg
|= DWC3_GRXTHRCFG_MAXRXBURSTSIZE(rx_maxburst
);
1263 dwc3_writel(dwc
->regs
, DWC3_GRXTHRCFG
, reg
);
1266 if (tx_thr_num
&& tx_maxburst
) {
1267 reg
= dwc3_readl(dwc
->regs
, DWC3_GTXTHRCFG
);
1268 reg
|= DWC3_GTXTHRCFG_PKTCNTSEL
;
1270 reg
&= ~DWC3_GTXTHRCFG_TXPKTCNT(~0);
1271 reg
|= DWC3_GTXTHRCFG_TXPKTCNT(tx_thr_num
);
1273 reg
&= ~DWC3_GTXTHRCFG_MAXTXBURSTSIZE(~0);
1274 reg
|= DWC3_GTXTHRCFG_MAXTXBURSTSIZE(tx_maxburst
);
1276 dwc3_writel(dwc
->regs
, DWC3_GTXTHRCFG
, reg
);
1279 if (rx_thr_num
&& rx_maxburst
) {
1280 reg
= dwc3_readl(dwc
->regs
, DWC3_GRXTHRCFG
);
1281 reg
|= DWC31_GRXTHRCFG_PKTCNTSEL
;
1283 reg
&= ~DWC31_GRXTHRCFG_RXPKTCNT(~0);
1284 reg
|= DWC31_GRXTHRCFG_RXPKTCNT(rx_thr_num
);
1286 reg
&= ~DWC31_GRXTHRCFG_MAXRXBURSTSIZE(~0);
1287 reg
|= DWC31_GRXTHRCFG_MAXRXBURSTSIZE(rx_maxburst
);
1289 dwc3_writel(dwc
->regs
, DWC3_GRXTHRCFG
, reg
);
1292 if (tx_thr_num
&& tx_maxburst
) {
1293 reg
= dwc3_readl(dwc
->regs
, DWC3_GTXTHRCFG
);
1294 reg
|= DWC31_GTXTHRCFG_PKTCNTSEL
;
1296 reg
&= ~DWC31_GTXTHRCFG_TXPKTCNT(~0);
1297 reg
|= DWC31_GTXTHRCFG_TXPKTCNT(tx_thr_num
);
1299 reg
&= ~DWC31_GTXTHRCFG_MAXTXBURSTSIZE(~0);
1300 reg
|= DWC31_GTXTHRCFG_MAXTXBURSTSIZE(tx_maxburst
);
1302 dwc3_writel(dwc
->regs
, DWC3_GTXTHRCFG
, reg
);
1308 * dwc3_core_init - Low-level initialization of DWC3 Core
1309 * @dwc: Pointer to our controller context structure
1311 * Returns 0 on success otherwise negative errno.
1313 static int dwc3_core_init(struct dwc3
*dwc
)
1315 unsigned int hw_mode
;
1319 hw_mode
= DWC3_GHWPARAMS0_MODE(dwc
->hwparams
.hwparams0
);
1322 * Write Linux Version Code to our GUID register so it's easy to figure
1323 * out which kernel version a bug was found.
1325 dwc3_writel(dwc
->regs
, DWC3_GUID
, LINUX_VERSION_CODE
);
1327 ret
= dwc3_phy_setup(dwc
);
1331 if (!dwc
->ulpi_ready
) {
1332 ret
= dwc3_core_ulpi_init(dwc
);
1334 if (ret
== -ETIMEDOUT
) {
1335 dwc3_core_soft_reset(dwc
);
1336 ret
= -EPROBE_DEFER
;
1340 dwc
->ulpi_ready
= true;
1343 if (!dwc
->phys_ready
) {
1344 ret
= dwc3_core_get_phy(dwc
);
1347 dwc
->phys_ready
= true;
1350 ret
= dwc3_phy_init(dwc
);
1354 ret
= dwc3_core_soft_reset(dwc
);
1358 dwc3_core_setup_global_control(dwc
);
1359 dwc3_core_num_eps(dwc
);
1361 /* Set power down scale of suspend_clk */
1362 dwc3_set_power_down_clk_scale(dwc
);
1364 /* Adjust Frame Length */
1365 dwc3_frame_length_adjustment(dwc
);
1367 /* Adjust Reference Clock Period */
1368 dwc3_ref_clk_period(dwc
);
1370 dwc3_set_incr_burst_type(dwc
);
1372 dwc3_config_soc_bus(dwc
);
1374 ret
= dwc3_phy_power_on(dwc
);
1378 ret
= dwc3_event_buffers_setup(dwc
);
1380 dev_err(dwc
->dev
, "failed to setup event buffers\n");
1381 goto err_power_off_phy
;
1385 * ENDXFER polling is available on version 3.10a and later of
1386 * the DWC_usb3 controller. It is NOT available in the
1387 * DWC_usb31 controller.
1389 if (DWC3_VER_IS_WITHIN(DWC3
, 310A
, ANY
)) {
1390 reg
= dwc3_readl(dwc
->regs
, DWC3_GUCTL2
);
1391 reg
|= DWC3_GUCTL2_RST_ACTBITLATER
;
1392 dwc3_writel(dwc
->regs
, DWC3_GUCTL2
, reg
);
1396 * STAR 9001285599: This issue affects DWC_usb3 version 3.20a
1397 * only. If the PM TIMER ECM is enabled through GUCTL2[19], the
1398 * link compliance test (TD7.21) may fail. If the ECN is not
1399 * enabled (GUCTL2[19] = 0), the controller will use the old timer
1400 * value (5us), which is still acceptable for the link compliance
1401 * test. Therefore, do not enable PM TIMER ECM in 3.20a by
1402 * setting GUCTL2[19] by default; instead, use GUCTL2[19] = 0.
1404 if (DWC3_VER_IS(DWC3
, 320A
)) {
1405 reg
= dwc3_readl(dwc
->regs
, DWC3_GUCTL2
);
1406 reg
&= ~DWC3_GUCTL2_LC_TIMER
;
1407 dwc3_writel(dwc
->regs
, DWC3_GUCTL2
, reg
);
1411 * When configured in HOST mode, after issuing U3/L2 exit controller
1412 * fails to send proper CRC checksum in CRC5 field. Because of this
1413 * behaviour Transaction Error is generated, resulting in reset and
1414 * re-enumeration of usb device attached. All the termsel, xcvrsel,
1415 * opmode becomes 0 during end of resume. Enabling bit 10 of GUCTL1
1416 * will correct this problem. This option is to support certain
1419 if (dwc
->resume_hs_terminations
) {
1420 reg
= dwc3_readl(dwc
->regs
, DWC3_GUCTL1
);
1421 reg
|= DWC3_GUCTL1_RESUME_OPMODE_HS_HOST
;
1422 dwc3_writel(dwc
->regs
, DWC3_GUCTL1
, reg
);
1425 if (!DWC3_VER_IS_PRIOR(DWC3
, 250A
)) {
1426 reg
= dwc3_readl(dwc
->regs
, DWC3_GUCTL1
);
1429 * Enable hardware control of sending remote wakeup
1430 * in HS when the device is in the L1 state.
1432 if (!DWC3_VER_IS_PRIOR(DWC3
, 290A
))
1433 reg
|= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW
;
1436 * Decouple USB 2.0 L1 & L2 events which will allow for
1437 * gadget driver to only receive U3/L2 suspend & wakeup
1438 * events and prevent the more frequent L1 LPM transitions
1439 * from interrupting the driver.
1441 if (!DWC3_VER_IS_PRIOR(DWC3
, 300A
))
1442 reg
|= DWC3_GUCTL1_DEV_DECOUPLE_L1L2_EVT
;
1444 if (dwc
->dis_tx_ipgap_linecheck_quirk
)
1445 reg
|= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS
;
1447 if (dwc
->parkmode_disable_ss_quirk
)
1448 reg
|= DWC3_GUCTL1_PARKMODE_DISABLE_SS
;
1450 if (dwc
->parkmode_disable_hs_quirk
)
1451 reg
|= DWC3_GUCTL1_PARKMODE_DISABLE_HS
;
1453 if (DWC3_VER_IS_WITHIN(DWC3
, 290A
, ANY
)) {
1454 if (dwc
->maximum_speed
== USB_SPEED_FULL
||
1455 dwc
->maximum_speed
== USB_SPEED_HIGH
)
1456 reg
|= DWC3_GUCTL1_DEV_FORCE_20_CLK_FOR_30_CLK
;
1458 reg
&= ~DWC3_GUCTL1_DEV_FORCE_20_CLK_FOR_30_CLK
;
1461 dwc3_writel(dwc
->regs
, DWC3_GUCTL1
, reg
);
1464 dwc3_config_threshold(dwc
);
1467 * Modify this for all supported Super Speed ports when
1468 * multiport support is added.
1470 if (hw_mode
!= DWC3_GHWPARAMS0_MODE_GADGET
&&
1471 (DWC3_IP_IS(DWC31
)) &&
1472 dwc
->maximum_speed
== USB_SPEED_SUPER
) {
1475 for (i
= 0; i
< dwc
->num_usb3_ports
; i
++) {
1476 reg
= dwc3_readl(dwc
->regs
, DWC3_LLUCTL(i
));
1477 reg
|= DWC3_LLUCTL_FORCE_GEN1
;
1478 dwc3_writel(dwc
->regs
, DWC3_LLUCTL(i
), reg
);
1485 dwc3_phy_power_off(dwc
);
1489 dwc3_ulpi_exit(dwc
);
1494 static int dwc3_core_get_phy(struct dwc3
*dwc
)
1496 struct device
*dev
= dwc
->dev
;
1497 struct device_node
*node
= dev
->of_node
;
1503 dwc
->usb2_phy
= devm_usb_get_phy_by_phandle(dev
, "usb-phy", 0);
1504 dwc
->usb3_phy
= devm_usb_get_phy_by_phandle(dev
, "usb-phy", 1);
1506 dwc
->usb2_phy
= devm_usb_get_phy(dev
, USB_PHY_TYPE_USB2
);
1507 dwc
->usb3_phy
= devm_usb_get_phy(dev
, USB_PHY_TYPE_USB3
);
1510 if (IS_ERR(dwc
->usb2_phy
)) {
1511 ret
= PTR_ERR(dwc
->usb2_phy
);
1512 if (ret
== -ENXIO
|| ret
== -ENODEV
)
1513 dwc
->usb2_phy
= NULL
;
1515 return dev_err_probe(dev
, ret
, "no usb2 phy configured\n");
1518 if (IS_ERR(dwc
->usb3_phy
)) {
1519 ret
= PTR_ERR(dwc
->usb3_phy
);
1520 if (ret
== -ENXIO
|| ret
== -ENODEV
)
1521 dwc
->usb3_phy
= NULL
;
1523 return dev_err_probe(dev
, ret
, "no usb3 phy configured\n");
1526 for (i
= 0; i
< dwc
->num_usb2_ports
; i
++) {
1527 if (dwc
->num_usb2_ports
== 1)
1528 snprintf(phy_name
, sizeof(phy_name
), "usb2-phy");
1530 snprintf(phy_name
, sizeof(phy_name
), "usb2-%u", i
);
1532 dwc
->usb2_generic_phy
[i
] = devm_phy_get(dev
, phy_name
);
1533 if (IS_ERR(dwc
->usb2_generic_phy
[i
])) {
1534 ret
= PTR_ERR(dwc
->usb2_generic_phy
[i
]);
1535 if (ret
== -ENOSYS
|| ret
== -ENODEV
)
1536 dwc
->usb2_generic_phy
[i
] = NULL
;
1538 return dev_err_probe(dev
, ret
, "failed to lookup phy %s\n",
1543 for (i
= 0; i
< dwc
->num_usb3_ports
; i
++) {
1544 if (dwc
->num_usb3_ports
== 1)
1545 snprintf(phy_name
, sizeof(phy_name
), "usb3-phy");
1547 snprintf(phy_name
, sizeof(phy_name
), "usb3-%u", i
);
1549 dwc
->usb3_generic_phy
[i
] = devm_phy_get(dev
, phy_name
);
1550 if (IS_ERR(dwc
->usb3_generic_phy
[i
])) {
1551 ret
= PTR_ERR(dwc
->usb3_generic_phy
[i
]);
1552 if (ret
== -ENOSYS
|| ret
== -ENODEV
)
1553 dwc
->usb3_generic_phy
[i
] = NULL
;
1555 return dev_err_probe(dev
, ret
, "failed to lookup phy %s\n",
1563 static int dwc3_core_init_mode(struct dwc3
*dwc
)
1565 struct device
*dev
= dwc
->dev
;
1569 switch (dwc
->dr_mode
) {
1570 case USB_DR_MODE_PERIPHERAL
:
1571 dwc3_set_prtcap(dwc
, DWC3_GCTL_PRTCAP_DEVICE
);
1574 otg_set_vbus(dwc
->usb2_phy
->otg
, false);
1575 phy_set_mode(dwc
->usb2_generic_phy
[0], PHY_MODE_USB_DEVICE
);
1576 phy_set_mode(dwc
->usb3_generic_phy
[0], PHY_MODE_USB_DEVICE
);
1578 ret
= dwc3_gadget_init(dwc
);
1580 return dev_err_probe(dev
, ret
, "failed to initialize gadget\n");
1582 case USB_DR_MODE_HOST
:
1583 dwc3_set_prtcap(dwc
, DWC3_GCTL_PRTCAP_HOST
);
1586 otg_set_vbus(dwc
->usb2_phy
->otg
, true);
1587 for (i
= 0; i
< dwc
->num_usb2_ports
; i
++)
1588 phy_set_mode(dwc
->usb2_generic_phy
[i
], PHY_MODE_USB_HOST
);
1589 for (i
= 0; i
< dwc
->num_usb3_ports
; i
++)
1590 phy_set_mode(dwc
->usb3_generic_phy
[i
], PHY_MODE_USB_HOST
);
1592 ret
= dwc3_host_init(dwc
);
1594 return dev_err_probe(dev
, ret
, "failed to initialize host\n");
1596 case USB_DR_MODE_OTG
:
1597 INIT_WORK(&dwc
->drd_work
, __dwc3_set_mode
);
1598 ret
= dwc3_drd_init(dwc
);
1600 return dev_err_probe(dev
, ret
, "failed to initialize dual-role\n");
1603 dev_err(dev
, "Unsupported mode of operation %d\n", dwc
->dr_mode
);
1610 static void dwc3_core_exit_mode(struct dwc3
*dwc
)
1612 switch (dwc
->dr_mode
) {
1613 case USB_DR_MODE_PERIPHERAL
:
1614 dwc3_gadget_exit(dwc
);
1616 case USB_DR_MODE_HOST
:
1617 dwc3_host_exit(dwc
);
1619 case USB_DR_MODE_OTG
:
1627 /* de-assert DRVVBUS for HOST and OTG mode */
1628 dwc3_set_prtcap(dwc
, DWC3_GCTL_PRTCAP_DEVICE
);
1631 static void dwc3_get_software_properties(struct dwc3
*dwc
)
1633 struct device
*tmpdev
;
1634 u16 gsbuscfg0_reqinfo
;
1637 dwc
->gsbuscfg0_reqinfo
= DWC3_GSBUSCFG0_REQINFO_UNSPECIFIED
;
1640 * Iterate over all parent nodes for finding swnode properties
1641 * and non-DT (non-ABI) properties.
1643 for (tmpdev
= dwc
->dev
; tmpdev
; tmpdev
= tmpdev
->parent
) {
1644 ret
= device_property_read_u16(tmpdev
,
1645 "snps,gsbuscfg0-reqinfo",
1646 &gsbuscfg0_reqinfo
);
1648 dwc
->gsbuscfg0_reqinfo
= gsbuscfg0_reqinfo
;
1652 static void dwc3_get_properties(struct dwc3
*dwc
)
1654 struct device
*dev
= dwc
->dev
;
1655 u8 lpm_nyet_threshold
;
1658 u8 rx_thr_num_pkt
= 0;
1659 u8 rx_max_burst
= 0;
1660 u8 tx_thr_num_pkt
= 0;
1661 u8 tx_max_burst
= 0;
1662 u8 rx_thr_num_pkt_prd
= 0;
1663 u8 rx_max_burst_prd
= 0;
1664 u8 tx_thr_num_pkt_prd
= 0;
1665 u8 tx_max_burst_prd
= 0;
1666 u8 tx_fifo_resize_max_num
;
1667 const char *usb_psy_name
;
1670 /* default to highest possible threshold */
1671 lpm_nyet_threshold
= 0xf;
1673 /* default to -3.5dB de-emphasis */
1677 * default to assert utmi_sleep_n and use maximum allowed HIRD
1678 * threshold value of 0b1100
1680 hird_threshold
= 12;
1683 * default to a TXFIFO size large enough to fit 6 max packets. This
1684 * allows for systems with larger bus latencies to have some headroom
1685 * for endpoints that have a large bMaxBurst value.
1687 tx_fifo_resize_max_num
= 6;
1689 dwc
->maximum_speed
= usb_get_maximum_speed(dev
);
1690 dwc
->max_ssp_rate
= usb_get_maximum_ssp_rate(dev
);
1691 dwc
->dr_mode
= usb_get_dr_mode(dev
);
1692 dwc
->hsphy_mode
= of_usb_get_phy_mode(dev
->of_node
);
1694 dwc
->sysdev_is_parent
= device_property_read_bool(dev
,
1695 "linux,sysdev_is_parent");
1696 if (dwc
->sysdev_is_parent
)
1697 dwc
->sysdev
= dwc
->dev
->parent
;
1699 dwc
->sysdev
= dwc
->dev
;
1701 dwc
->sys_wakeup
= device_may_wakeup(dwc
->sysdev
);
1703 ret
= device_property_read_string(dev
, "usb-psy-name", &usb_psy_name
);
1705 dwc
->usb_psy
= power_supply_get_by_name(usb_psy_name
);
1707 dev_err(dev
, "couldn't get usb power supply\n");
1710 dwc
->has_lpm_erratum
= device_property_read_bool(dev
,
1711 "snps,has-lpm-erratum");
1712 device_property_read_u8(dev
, "snps,lpm-nyet-threshold",
1713 &lpm_nyet_threshold
);
1714 dwc
->is_utmi_l1_suspend
= device_property_read_bool(dev
,
1715 "snps,is-utmi-l1-suspend");
1716 device_property_read_u8(dev
, "snps,hird-threshold",
1718 dwc
->dis_start_transfer_quirk
= device_property_read_bool(dev
,
1719 "snps,dis-start-transfer-quirk");
1720 dwc
->usb3_lpm_capable
= device_property_read_bool(dev
,
1721 "snps,usb3_lpm_capable");
1722 dwc
->usb2_lpm_disable
= device_property_read_bool(dev
,
1723 "snps,usb2-lpm-disable");
1724 dwc
->usb2_gadget_lpm_disable
= device_property_read_bool(dev
,
1725 "snps,usb2-gadget-lpm-disable");
1726 device_property_read_u8(dev
, "snps,rx-thr-num-pkt",
1728 device_property_read_u8(dev
, "snps,rx-max-burst",
1730 device_property_read_u8(dev
, "snps,tx-thr-num-pkt",
1732 device_property_read_u8(dev
, "snps,tx-max-burst",
1734 device_property_read_u8(dev
, "snps,rx-thr-num-pkt-prd",
1735 &rx_thr_num_pkt_prd
);
1736 device_property_read_u8(dev
, "snps,rx-max-burst-prd",
1738 device_property_read_u8(dev
, "snps,tx-thr-num-pkt-prd",
1739 &tx_thr_num_pkt_prd
);
1740 device_property_read_u8(dev
, "snps,tx-max-burst-prd",
1742 dwc
->do_fifo_resize
= device_property_read_bool(dev
,
1744 if (dwc
->do_fifo_resize
)
1745 device_property_read_u8(dev
, "tx-fifo-max-num",
1746 &tx_fifo_resize_max_num
);
1748 dwc
->disable_scramble_quirk
= device_property_read_bool(dev
,
1749 "snps,disable_scramble_quirk");
1750 dwc
->u2exit_lfps_quirk
= device_property_read_bool(dev
,
1751 "snps,u2exit_lfps_quirk");
1752 dwc
->u2ss_inp3_quirk
= device_property_read_bool(dev
,
1753 "snps,u2ss_inp3_quirk");
1754 dwc
->req_p1p2p3_quirk
= device_property_read_bool(dev
,
1755 "snps,req_p1p2p3_quirk");
1756 dwc
->del_p1p2p3_quirk
= device_property_read_bool(dev
,
1757 "snps,del_p1p2p3_quirk");
1758 dwc
->del_phy_power_chg_quirk
= device_property_read_bool(dev
,
1759 "snps,del_phy_power_chg_quirk");
1760 dwc
->lfps_filter_quirk
= device_property_read_bool(dev
,
1761 "snps,lfps_filter_quirk");
1762 dwc
->rx_detect_poll_quirk
= device_property_read_bool(dev
,
1763 "snps,rx_detect_poll_quirk");
1764 dwc
->dis_u3_susphy_quirk
= device_property_read_bool(dev
,
1765 "snps,dis_u3_susphy_quirk");
1766 dwc
->dis_u2_susphy_quirk
= device_property_read_bool(dev
,
1767 "snps,dis_u2_susphy_quirk");
1768 dwc
->dis_enblslpm_quirk
= device_property_read_bool(dev
,
1769 "snps,dis_enblslpm_quirk");
1770 dwc
->dis_u1_entry_quirk
= device_property_read_bool(dev
,
1771 "snps,dis-u1-entry-quirk");
1772 dwc
->dis_u2_entry_quirk
= device_property_read_bool(dev
,
1773 "snps,dis-u2-entry-quirk");
1774 dwc
->dis_rxdet_inp3_quirk
= device_property_read_bool(dev
,
1775 "snps,dis_rxdet_inp3_quirk");
1776 dwc
->dis_u2_freeclk_exists_quirk
= device_property_read_bool(dev
,
1777 "snps,dis-u2-freeclk-exists-quirk");
1778 dwc
->dis_del_phy_power_chg_quirk
= device_property_read_bool(dev
,
1779 "snps,dis-del-phy-power-chg-quirk");
1780 dwc
->dis_tx_ipgap_linecheck_quirk
= device_property_read_bool(dev
,
1781 "snps,dis-tx-ipgap-linecheck-quirk");
1782 dwc
->resume_hs_terminations
= device_property_read_bool(dev
,
1783 "snps,resume-hs-terminations");
1784 dwc
->ulpi_ext_vbus_drv
= device_property_read_bool(dev
,
1785 "snps,ulpi-ext-vbus-drv");
1786 dwc
->parkmode_disable_ss_quirk
= device_property_read_bool(dev
,
1787 "snps,parkmode-disable-ss-quirk");
1788 dwc
->parkmode_disable_hs_quirk
= device_property_read_bool(dev
,
1789 "snps,parkmode-disable-hs-quirk");
1790 dwc
->gfladj_refclk_lpm_sel
= device_property_read_bool(dev
,
1791 "snps,gfladj-refclk-lpm-sel-quirk");
1793 dwc
->tx_de_emphasis_quirk
= device_property_read_bool(dev
,
1794 "snps,tx_de_emphasis_quirk");
1795 device_property_read_u8(dev
, "snps,tx_de_emphasis",
1797 device_property_read_string(dev
, "snps,hsphy_interface",
1798 &dwc
->hsphy_interface
);
1799 device_property_read_u32(dev
, "snps,quirk-frame-length-adjustment",
1801 device_property_read_u32(dev
, "snps,ref-clock-period-ns",
1804 dwc
->dis_metastability_quirk
= device_property_read_bool(dev
,
1805 "snps,dis_metastability_quirk");
1807 dwc
->dis_split_quirk
= device_property_read_bool(dev
,
1808 "snps,dis-split-quirk");
1810 dwc
->lpm_nyet_threshold
= lpm_nyet_threshold
;
1811 dwc
->tx_de_emphasis
= tx_de_emphasis
;
1813 dwc
->hird_threshold
= hird_threshold
;
1815 dwc
->rx_thr_num_pkt
= rx_thr_num_pkt
;
1816 dwc
->rx_max_burst
= rx_max_burst
;
1818 dwc
->tx_thr_num_pkt
= tx_thr_num_pkt
;
1819 dwc
->tx_max_burst
= tx_max_burst
;
1821 dwc
->rx_thr_num_pkt_prd
= rx_thr_num_pkt_prd
;
1822 dwc
->rx_max_burst_prd
= rx_max_burst_prd
;
1824 dwc
->tx_thr_num_pkt_prd
= tx_thr_num_pkt_prd
;
1825 dwc
->tx_max_burst_prd
= tx_max_burst_prd
;
1827 dwc
->imod_interval
= 0;
1829 dwc
->tx_fifo_resize_max_num
= tx_fifo_resize_max_num
;
1832 /* check whether the core supports IMOD */
1833 bool dwc3_has_imod(struct dwc3
*dwc
)
1835 return DWC3_VER_IS_WITHIN(DWC3
, 300A
, ANY
) ||
1836 DWC3_VER_IS_WITHIN(DWC31
, 120A
, ANY
) ||
1840 static void dwc3_check_params(struct dwc3
*dwc
)
1842 struct device
*dev
= dwc
->dev
;
1843 unsigned int hwparam_gen
=
1844 DWC3_GHWPARAMS3_SSPHY_IFC(dwc
->hwparams
.hwparams3
);
1846 /* Check for proper value of imod_interval */
1847 if (dwc
->imod_interval
&& !dwc3_has_imod(dwc
)) {
1848 dev_warn(dwc
->dev
, "Interrupt moderation not supported\n");
1849 dwc
->imod_interval
= 0;
1853 * Workaround for STAR 9000961433 which affects only version
1854 * 3.00a of the DWC_usb3 core. This prevents the controller
1855 * interrupt from being masked while handling events. IMOD
1856 * allows us to work around this issue. Enable it for the
1859 if (!dwc
->imod_interval
&&
1860 DWC3_VER_IS(DWC3
, 300A
))
1861 dwc
->imod_interval
= 1;
1863 /* Check the maximum_speed parameter */
1864 switch (dwc
->maximum_speed
) {
1865 case USB_SPEED_FULL
:
1866 case USB_SPEED_HIGH
:
1868 case USB_SPEED_SUPER
:
1869 if (hwparam_gen
== DWC3_GHWPARAMS3_SSPHY_IFC_DIS
)
1870 dev_warn(dev
, "UDC doesn't support Gen 1\n");
1872 case USB_SPEED_SUPER_PLUS
:
1873 if ((DWC3_IP_IS(DWC32
) &&
1874 hwparam_gen
== DWC3_GHWPARAMS3_SSPHY_IFC_DIS
) ||
1875 (!DWC3_IP_IS(DWC32
) &&
1876 hwparam_gen
!= DWC3_GHWPARAMS3_SSPHY_IFC_GEN2
))
1877 dev_warn(dev
, "UDC doesn't support SSP\n");
1880 dev_err(dev
, "invalid maximum_speed parameter %d\n",
1881 dwc
->maximum_speed
);
1883 case USB_SPEED_UNKNOWN
:
1884 switch (hwparam_gen
) {
1885 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2
:
1886 dwc
->maximum_speed
= USB_SPEED_SUPER_PLUS
;
1888 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1
:
1889 if (DWC3_IP_IS(DWC32
))
1890 dwc
->maximum_speed
= USB_SPEED_SUPER_PLUS
;
1892 dwc
->maximum_speed
= USB_SPEED_SUPER
;
1894 case DWC3_GHWPARAMS3_SSPHY_IFC_DIS
:
1895 dwc
->maximum_speed
= USB_SPEED_HIGH
;
1898 dwc
->maximum_speed
= USB_SPEED_SUPER
;
1905 * Currently the controller does not have visibility into the HW
1906 * parameter to determine the maximum number of lanes the HW supports.
1907 * If the number of lanes is not specified in the device property, then
1908 * set the default to support dual-lane for DWC_usb32 and single-lane
1909 * for DWC_usb31 for super-speed-plus.
1911 if (dwc
->maximum_speed
== USB_SPEED_SUPER_PLUS
) {
1912 switch (dwc
->max_ssp_rate
) {
1913 case USB_SSP_GEN_2x1
:
1914 if (hwparam_gen
== DWC3_GHWPARAMS3_SSPHY_IFC_GEN1
)
1915 dev_warn(dev
, "UDC only supports Gen 1\n");
1917 case USB_SSP_GEN_1x2
:
1918 case USB_SSP_GEN_2x2
:
1919 if (DWC3_IP_IS(DWC31
))
1920 dev_warn(dev
, "UDC only supports single lane\n");
1922 case USB_SSP_GEN_UNKNOWN
:
1924 switch (hwparam_gen
) {
1925 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2
:
1926 if (DWC3_IP_IS(DWC32
))
1927 dwc
->max_ssp_rate
= USB_SSP_GEN_2x2
;
1929 dwc
->max_ssp_rate
= USB_SSP_GEN_2x1
;
1931 case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1
:
1932 if (DWC3_IP_IS(DWC32
))
1933 dwc
->max_ssp_rate
= USB_SSP_GEN_1x2
;
1941 static struct extcon_dev
*dwc3_get_extcon(struct dwc3
*dwc
)
1943 struct device
*dev
= dwc
->dev
;
1944 struct device_node
*np_phy
;
1945 struct extcon_dev
*edev
= NULL
;
1948 if (device_property_present(dev
, "extcon"))
1949 return extcon_get_edev_by_phandle(dev
, 0);
1952 * Device tree platforms should get extcon via phandle.
1953 * On ACPI platforms, we get the name from a device property.
1954 * This device property is for kernel internal use only and
1955 * is expected to be set by the glue code.
1957 if (device_property_read_string(dev
, "linux,extcon-name", &name
) == 0)
1958 return extcon_get_extcon_dev(name
);
1961 * Check explicitly if "usb-role-switch" is used since
1962 * extcon_find_edev_by_node() can not be used to check the absence of
1963 * an extcon device. In the absence of an device it will always return
1966 if (IS_ENABLED(CONFIG_USB_ROLE_SWITCH
) &&
1967 device_property_read_bool(dev
, "usb-role-switch"))
1971 * Try to get an extcon device from the USB PHY controller's "port"
1972 * node. Check if it has the "port" node first, to avoid printing the
1973 * error message from underlying code, as it's a valid case: extcon
1974 * device (and "port" node) may be missing in case of "usb-role-switch"
1977 np_phy
= of_parse_phandle(dev
->of_node
, "phys", 0);
1978 if (of_graph_is_present(np_phy
)) {
1979 struct device_node
*np_conn
;
1981 np_conn
= of_graph_get_remote_node(np_phy
, -1, -1);
1983 edev
= extcon_find_edev_by_node(np_conn
);
1984 of_node_put(np_conn
);
1986 of_node_put(np_phy
);
1991 static int dwc3_get_clocks(struct dwc3
*dwc
)
1993 struct device
*dev
= dwc
->dev
;
1999 * Clocks are optional, but new DT platforms should support all clocks
2000 * as required by the DT-binding.
2001 * Some devices have different clock names in legacy device trees,
2002 * check for them to retain backwards compatibility.
2004 dwc
->bus_clk
= devm_clk_get_optional(dev
, "bus_early");
2005 if (IS_ERR(dwc
->bus_clk
)) {
2006 return dev_err_probe(dev
, PTR_ERR(dwc
->bus_clk
),
2007 "could not get bus clock\n");
2010 if (dwc
->bus_clk
== NULL
) {
2011 dwc
->bus_clk
= devm_clk_get_optional(dev
, "bus_clk");
2012 if (IS_ERR(dwc
->bus_clk
)) {
2013 return dev_err_probe(dev
, PTR_ERR(dwc
->bus_clk
),
2014 "could not get bus clock\n");
2018 dwc
->ref_clk
= devm_clk_get_optional(dev
, "ref");
2019 if (IS_ERR(dwc
->ref_clk
)) {
2020 return dev_err_probe(dev
, PTR_ERR(dwc
->ref_clk
),
2021 "could not get ref clock\n");
2024 if (dwc
->ref_clk
== NULL
) {
2025 dwc
->ref_clk
= devm_clk_get_optional(dev
, "ref_clk");
2026 if (IS_ERR(dwc
->ref_clk
)) {
2027 return dev_err_probe(dev
, PTR_ERR(dwc
->ref_clk
),
2028 "could not get ref clock\n");
2032 dwc
->susp_clk
= devm_clk_get_optional(dev
, "suspend");
2033 if (IS_ERR(dwc
->susp_clk
)) {
2034 return dev_err_probe(dev
, PTR_ERR(dwc
->susp_clk
),
2035 "could not get suspend clock\n");
2038 if (dwc
->susp_clk
== NULL
) {
2039 dwc
->susp_clk
= devm_clk_get_optional(dev
, "suspend_clk");
2040 if (IS_ERR(dwc
->susp_clk
)) {
2041 return dev_err_probe(dev
, PTR_ERR(dwc
->susp_clk
),
2042 "could not get suspend clock\n");
2046 /* specific to Rockchip RK3588 */
2047 dwc
->utmi_clk
= devm_clk_get_optional(dev
, "utmi");
2048 if (IS_ERR(dwc
->utmi_clk
)) {
2049 return dev_err_probe(dev
, PTR_ERR(dwc
->utmi_clk
),
2050 "could not get utmi clock\n");
2053 /* specific to Rockchip RK3588 */
2054 dwc
->pipe_clk
= devm_clk_get_optional(dev
, "pipe");
2055 if (IS_ERR(dwc
->pipe_clk
)) {
2056 return dev_err_probe(dev
, PTR_ERR(dwc
->pipe_clk
),
2057 "could not get pipe clock\n");
2063 static int dwc3_get_num_ports(struct dwc3
*dwc
)
2071 * Remap xHCI address space to access XHCI ext cap regs since it is
2072 * needed to get information on number of ports present.
2074 base
= ioremap(dwc
->xhci_resources
[0].start
,
2075 resource_size(&dwc
->xhci_resources
[0]));
2081 offset
= xhci_find_next_ext_cap(base
, offset
,
2082 XHCI_EXT_CAPS_PROTOCOL
);
2086 val
= readl(base
+ offset
);
2087 major_revision
= XHCI_EXT_PORT_MAJOR(val
);
2089 val
= readl(base
+ offset
+ 0x08);
2090 if (major_revision
== 0x03) {
2091 dwc
->num_usb3_ports
+= XHCI_EXT_PORT_COUNT(val
);
2092 } else if (major_revision
<= 0x02) {
2093 dwc
->num_usb2_ports
+= XHCI_EXT_PORT_COUNT(val
);
2095 dev_warn(dwc
->dev
, "unrecognized port major revision %d\n",
2100 dev_dbg(dwc
->dev
, "hs-ports: %u ss-ports: %u\n",
2101 dwc
->num_usb2_ports
, dwc
->num_usb3_ports
);
2105 if (dwc
->num_usb2_ports
> DWC3_USB2_MAX_PORTS
||
2106 dwc
->num_usb3_ports
> DWC3_USB3_MAX_PORTS
)
2112 static int dwc3_probe(struct platform_device
*pdev
)
2114 struct device
*dev
= &pdev
->dev
;
2115 struct resource
*res
, dwc_res
;
2116 unsigned int hw_mode
;
2121 dwc
= devm_kzalloc(dev
, sizeof(*dwc
), GFP_KERNEL
);
2127 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2129 dev_err(dev
, "missing memory resource\n");
2133 dwc
->xhci_resources
[0].start
= res
->start
;
2134 dwc
->xhci_resources
[0].end
= dwc
->xhci_resources
[0].start
+
2136 dwc
->xhci_resources
[0].flags
= res
->flags
;
2137 dwc
->xhci_resources
[0].name
= res
->name
;
2140 * Request memory region but exclude xHCI regs,
2141 * since it will be requested by the xhci-plat driver.
2144 dwc_res
.start
+= DWC3_GLOBALS_REGS_START
;
2147 struct device_node
*parent
= of_get_parent(dev
->of_node
);
2149 if (of_device_is_compatible(parent
, "realtek,rtd-dwc3")) {
2150 dwc_res
.start
-= DWC3_GLOBALS_REGS_START
;
2151 dwc_res
.start
+= DWC3_RTK_RTD_GLOBALS_REGS_START
;
2154 of_node_put(parent
);
2157 regs
= devm_ioremap_resource(dev
, &dwc_res
);
2159 return PTR_ERR(regs
);
2162 dwc
->regs_size
= resource_size(&dwc_res
);
2164 dwc3_get_properties(dwc
);
2166 dwc3_get_software_properties(dwc
);
2168 dwc
->reset
= devm_reset_control_array_get_optional_shared(dev
);
2169 if (IS_ERR(dwc
->reset
)) {
2170 ret
= PTR_ERR(dwc
->reset
);
2174 ret
= dwc3_get_clocks(dwc
);
2178 ret
= reset_control_deassert(dwc
->reset
);
2182 ret
= dwc3_clk_enable(dwc
);
2184 goto err_assert_reset
;
2186 if (!dwc3_core_is_valid(dwc
)) {
2187 dev_err(dwc
->dev
, "this is not a DesignWare USB3 DRD Core\n");
2189 goto err_disable_clks
;
2192 platform_set_drvdata(pdev
, dwc
);
2193 dwc3_cache_hwparams(dwc
);
2195 if (!dwc
->sysdev_is_parent
&&
2196 DWC3_GHWPARAMS0_AWIDTH(dwc
->hwparams
.hwparams0
) == 64) {
2197 ret
= dma_set_mask_and_coherent(dwc
->sysdev
, DMA_BIT_MASK(64));
2199 goto err_disable_clks
;
2203 * Currently only DWC3 controllers that are host-only capable
2204 * can have more than one port.
2206 hw_mode
= DWC3_GHWPARAMS0_MODE(dwc
->hwparams
.hwparams0
);
2207 if (hw_mode
== DWC3_GHWPARAMS0_MODE_HOST
) {
2208 ret
= dwc3_get_num_ports(dwc
);
2210 goto err_disable_clks
;
2212 dwc
->num_usb2_ports
= 1;
2213 dwc
->num_usb3_ports
= 1;
2216 spin_lock_init(&dwc
->lock
);
2217 mutex_init(&dwc
->mutex
);
2219 pm_runtime_get_noresume(dev
);
2220 pm_runtime_set_active(dev
);
2221 pm_runtime_use_autosuspend(dev
);
2222 pm_runtime_set_autosuspend_delay(dev
, DWC3_DEFAULT_AUTOSUSPEND_DELAY
);
2223 pm_runtime_enable(dev
);
2225 pm_runtime_forbid(dev
);
2227 ret
= dwc3_alloc_event_buffers(dwc
, DWC3_EVENT_BUFFERS_SIZE
);
2229 dev_err(dwc
->dev
, "failed to allocate event buffers\n");
2234 dwc
->edev
= dwc3_get_extcon(dwc
);
2235 if (IS_ERR(dwc
->edev
)) {
2236 ret
= dev_err_probe(dwc
->dev
, PTR_ERR(dwc
->edev
), "failed to get extcon\n");
2237 goto err_free_event_buffers
;
2240 ret
= dwc3_get_dr_mode(dwc
);
2242 goto err_free_event_buffers
;
2244 ret
= dwc3_core_init(dwc
);
2246 dev_err_probe(dev
, ret
, "failed to initialize core\n");
2247 goto err_free_event_buffers
;
2250 dwc3_check_params(dwc
);
2251 dwc3_debugfs_init(dwc
);
2253 ret
= dwc3_core_init_mode(dwc
);
2255 goto err_exit_debugfs
;
2257 pm_runtime_put(dev
);
2259 dma_set_max_seg_size(dev
, UINT_MAX
);
2264 dwc3_debugfs_exit(dwc
);
2265 dwc3_event_buffers_cleanup(dwc
);
2266 dwc3_phy_power_off(dwc
);
2268 dwc3_ulpi_exit(dwc
);
2269 err_free_event_buffers
:
2270 dwc3_free_event_buffers(dwc
);
2272 pm_runtime_allow(dev
);
2273 pm_runtime_disable(dev
);
2274 pm_runtime_dont_use_autosuspend(dev
);
2275 pm_runtime_set_suspended(dev
);
2276 pm_runtime_put_noidle(dev
);
2278 dwc3_clk_disable(dwc
);
2280 reset_control_assert(dwc
->reset
);
2283 power_supply_put(dwc
->usb_psy
);
2288 static void dwc3_remove(struct platform_device
*pdev
)
2290 struct dwc3
*dwc
= platform_get_drvdata(pdev
);
2292 pm_runtime_get_sync(&pdev
->dev
);
2294 dwc3_core_exit_mode(dwc
);
2295 dwc3_debugfs_exit(dwc
);
2297 dwc3_core_exit(dwc
);
2298 dwc3_ulpi_exit(dwc
);
2300 pm_runtime_allow(&pdev
->dev
);
2301 pm_runtime_disable(&pdev
->dev
);
2302 pm_runtime_dont_use_autosuspend(&pdev
->dev
);
2303 pm_runtime_put_noidle(&pdev
->dev
);
2305 * HACK: Clear the driver data, which is currently accessed by parent
2306 * glue drivers, before allowing the parent to suspend.
2308 platform_set_drvdata(pdev
, NULL
);
2309 pm_runtime_set_suspended(&pdev
->dev
);
2311 dwc3_free_event_buffers(dwc
);
2314 power_supply_put(dwc
->usb_psy
);
2318 static int dwc3_core_init_for_resume(struct dwc3
*dwc
)
2322 ret
= reset_control_deassert(dwc
->reset
);
2326 ret
= dwc3_clk_enable(dwc
);
2330 ret
= dwc3_core_init(dwc
);
2337 dwc3_clk_disable(dwc
);
2339 reset_control_assert(dwc
->reset
);
2344 static int dwc3_suspend_common(struct dwc3
*dwc
, pm_message_t msg
)
2349 if (!pm_runtime_suspended(dwc
->dev
) && !PMSG_IS_AUTO(msg
)) {
2350 dwc
->susphy_state
= (dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(0)) &
2351 DWC3_GUSB2PHYCFG_SUSPHY
) ||
2352 (dwc3_readl(dwc
->regs
, DWC3_GUSB3PIPECTL(0)) &
2353 DWC3_GUSB3PIPECTL_SUSPHY
);
2355 * TI AM62 platform requires SUSPHY to be
2356 * enabled for system suspend to work.
2358 if (!dwc
->susphy_state
)
2359 dwc3_enable_susphy(dwc
, true);
2362 switch (dwc
->current_dr_role
) {
2363 case DWC3_GCTL_PRTCAP_DEVICE
:
2364 if (pm_runtime_suspended(dwc
->dev
))
2366 dwc3_gadget_suspend(dwc
);
2367 synchronize_irq(dwc
->irq_gadget
);
2368 dwc3_core_exit(dwc
);
2370 case DWC3_GCTL_PRTCAP_HOST
:
2371 if (!PMSG_IS_AUTO(msg
) && !device_may_wakeup(dwc
->dev
)) {
2372 dwc3_core_exit(dwc
);
2376 /* Let controller to suspend HSPHY before PHY driver suspends */
2377 if (dwc
->dis_u2_susphy_quirk
||
2378 dwc
->dis_enblslpm_quirk
) {
2379 for (i
= 0; i
< dwc
->num_usb2_ports
; i
++) {
2380 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(i
));
2381 reg
|= DWC3_GUSB2PHYCFG_ENBLSLPM
|
2382 DWC3_GUSB2PHYCFG_SUSPHY
;
2383 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(i
), reg
);
2386 /* Give some time for USB2 PHY to suspend */
2387 usleep_range(5000, 6000);
2390 for (i
= 0; i
< dwc
->num_usb2_ports
; i
++)
2391 phy_pm_runtime_put_sync(dwc
->usb2_generic_phy
[i
]);
2392 for (i
= 0; i
< dwc
->num_usb3_ports
; i
++)
2393 phy_pm_runtime_put_sync(dwc
->usb3_generic_phy
[i
]);
2395 case DWC3_GCTL_PRTCAP_OTG
:
2396 /* do nothing during runtime_suspend */
2397 if (PMSG_IS_AUTO(msg
))
2400 if (dwc
->current_otg_role
== DWC3_OTG_ROLE_DEVICE
) {
2401 dwc3_gadget_suspend(dwc
);
2402 synchronize_irq(dwc
->irq_gadget
);
2406 dwc3_core_exit(dwc
);
2416 static int dwc3_resume_common(struct dwc3
*dwc
, pm_message_t msg
)
2422 switch (dwc
->current_dr_role
) {
2423 case DWC3_GCTL_PRTCAP_DEVICE
:
2424 ret
= dwc3_core_init_for_resume(dwc
);
2428 dwc3_set_prtcap(dwc
, DWC3_GCTL_PRTCAP_DEVICE
);
2429 dwc3_gadget_resume(dwc
);
2431 case DWC3_GCTL_PRTCAP_HOST
:
2432 if (!PMSG_IS_AUTO(msg
) && !device_may_wakeup(dwc
->dev
)) {
2433 ret
= dwc3_core_init_for_resume(dwc
);
2436 dwc3_set_prtcap(dwc
, DWC3_GCTL_PRTCAP_HOST
);
2439 /* Restore GUSB2PHYCFG bits that were modified in suspend */
2440 for (i
= 0; i
< dwc
->num_usb2_ports
; i
++) {
2441 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(i
));
2442 if (dwc
->dis_u2_susphy_quirk
)
2443 reg
&= ~DWC3_GUSB2PHYCFG_SUSPHY
;
2445 if (dwc
->dis_enblslpm_quirk
)
2446 reg
&= ~DWC3_GUSB2PHYCFG_ENBLSLPM
;
2448 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(i
), reg
);
2451 for (i
= 0; i
< dwc
->num_usb2_ports
; i
++)
2452 phy_pm_runtime_get_sync(dwc
->usb2_generic_phy
[i
]);
2453 for (i
= 0; i
< dwc
->num_usb3_ports
; i
++)
2454 phy_pm_runtime_get_sync(dwc
->usb3_generic_phy
[i
]);
2456 case DWC3_GCTL_PRTCAP_OTG
:
2457 /* nothing to do on runtime_resume */
2458 if (PMSG_IS_AUTO(msg
))
2461 ret
= dwc3_core_init_for_resume(dwc
);
2465 dwc3_set_prtcap(dwc
, dwc
->current_dr_role
);
2468 if (dwc
->current_otg_role
== DWC3_OTG_ROLE_HOST
) {
2469 dwc3_otg_host_init(dwc
);
2470 } else if (dwc
->current_otg_role
== DWC3_OTG_ROLE_DEVICE
) {
2471 dwc3_gadget_resume(dwc
);
2480 if (!PMSG_IS_AUTO(msg
)) {
2481 /* restore SUSPHY state to that before system suspend. */
2482 dwc3_enable_susphy(dwc
, dwc
->susphy_state
);
2488 static int dwc3_runtime_checks(struct dwc3
*dwc
)
2490 switch (dwc
->current_dr_role
) {
2491 case DWC3_GCTL_PRTCAP_DEVICE
:
2495 case DWC3_GCTL_PRTCAP_HOST
:
2504 static int dwc3_runtime_suspend(struct device
*dev
)
2506 struct dwc3
*dwc
= dev_get_drvdata(dev
);
2509 if (dwc3_runtime_checks(dwc
))
2512 ret
= dwc3_suspend_common(dwc
, PMSG_AUTO_SUSPEND
);
2519 static int dwc3_runtime_resume(struct device
*dev
)
2521 struct dwc3
*dwc
= dev_get_drvdata(dev
);
2524 ret
= dwc3_resume_common(dwc
, PMSG_AUTO_RESUME
);
2528 switch (dwc
->current_dr_role
) {
2529 case DWC3_GCTL_PRTCAP_DEVICE
:
2530 if (dwc
->pending_events
) {
2531 pm_runtime_put(dwc
->dev
);
2532 dwc
->pending_events
= false;
2533 enable_irq(dwc
->irq_gadget
);
2536 case DWC3_GCTL_PRTCAP_HOST
:
2542 pm_runtime_mark_last_busy(dev
);
2547 static int dwc3_runtime_idle(struct device
*dev
)
2549 struct dwc3
*dwc
= dev_get_drvdata(dev
);
2551 switch (dwc
->current_dr_role
) {
2552 case DWC3_GCTL_PRTCAP_DEVICE
:
2553 if (dwc3_runtime_checks(dwc
))
2556 case DWC3_GCTL_PRTCAP_HOST
:
2562 pm_runtime_mark_last_busy(dev
);
2563 pm_runtime_autosuspend(dev
);
2567 #endif /* CONFIG_PM */
2569 #ifdef CONFIG_PM_SLEEP
2570 static int dwc3_suspend(struct device
*dev
)
2572 struct dwc3
*dwc
= dev_get_drvdata(dev
);
2575 ret
= dwc3_suspend_common(dwc
, PMSG_SUSPEND
);
2579 pinctrl_pm_select_sleep_state(dev
);
2584 static int dwc3_resume(struct device
*dev
)
2586 struct dwc3
*dwc
= dev_get_drvdata(dev
);
2589 pinctrl_pm_select_default_state(dev
);
2591 pm_runtime_disable(dev
);
2592 pm_runtime_set_active(dev
);
2594 ret
= dwc3_resume_common(dwc
, PMSG_RESUME
);
2596 pm_runtime_set_suspended(dev
);
2598 pm_runtime_enable(dev
);
2603 static void dwc3_complete(struct device
*dev
)
2605 struct dwc3
*dwc
= dev_get_drvdata(dev
);
2608 if (dwc
->current_dr_role
== DWC3_GCTL_PRTCAP_HOST
&&
2609 dwc
->dis_split_quirk
) {
2610 reg
= dwc3_readl(dwc
->regs
, DWC3_GUCTL3
);
2611 reg
|= DWC3_GUCTL3_SPLITDISABLE
;
2612 dwc3_writel(dwc
->regs
, DWC3_GUCTL3
, reg
);
2616 #define dwc3_complete NULL
2617 #endif /* CONFIG_PM_SLEEP */
2619 static const struct dev_pm_ops dwc3_dev_pm_ops
= {
2620 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend
, dwc3_resume
)
2621 .complete
= dwc3_complete
,
2624 * Runtime suspend halts the controller on disconnection. It relies on
2625 * platforms with custom connection notification to start the controller
2628 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend
, dwc3_runtime_resume
,
2633 static const struct of_device_id of_dwc3_match
[] = {
2635 .compatible
= "snps,dwc3"
2638 .compatible
= "synopsys,dwc3"
2642 MODULE_DEVICE_TABLE(of
, of_dwc3_match
);
2647 #define ACPI_ID_INTEL_BSW "808622B7"
2649 static const struct acpi_device_id dwc3_acpi_match
[] = {
2650 { ACPI_ID_INTEL_BSW
, 0 },
2653 MODULE_DEVICE_TABLE(acpi
, dwc3_acpi_match
);
2656 static struct platform_driver dwc3_driver
= {
2657 .probe
= dwc3_probe
,
2658 .remove
= dwc3_remove
,
2661 .of_match_table
= of_match_ptr(of_dwc3_match
),
2662 .acpi_match_table
= ACPI_PTR(dwc3_acpi_match
),
2663 .pm
= &dwc3_dev_pm_ops
,
2667 module_platform_driver(dwc3_driver
);
2669 MODULE_ALIAS("platform:dwc3");
2670 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
2671 MODULE_LICENSE("GPL v2");
2672 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");