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>
44 #include "platform_data.h"
51 /* -------------------------------------------------------------------------- */
53 void dwc3_set_mode(struct dwc3
*dwc
, u32 mode
)
57 reg
= dwc3_readl(dwc
->regs
, DWC3_GCTL
);
58 reg
&= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG
));
59 reg
|= DWC3_GCTL_PRTCAPDIR(mode
);
60 dwc3_writel(dwc
->regs
, DWC3_GCTL
, reg
);
63 u32
dwc3_core_fifo_space(struct dwc3_ep
*dep
, u8 type
)
65 struct dwc3
*dwc
= dep
->dwc
;
68 dwc3_writel(dwc
->regs
, DWC3_GDBGFIFOSPACE
,
69 DWC3_GDBGFIFOSPACE_NUM(dep
->number
) |
70 DWC3_GDBGFIFOSPACE_TYPE(type
));
72 reg
= dwc3_readl(dwc
->regs
, DWC3_GDBGFIFOSPACE
);
74 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg
);
78 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
79 * @dwc: pointer to our context structure
81 static int dwc3_core_soft_reset(struct dwc3
*dwc
)
87 usb_phy_init(dwc
->usb2_phy
);
88 usb_phy_init(dwc
->usb3_phy
);
89 ret
= phy_init(dwc
->usb2_generic_phy
);
93 ret
= phy_init(dwc
->usb3_generic_phy
);
95 phy_exit(dwc
->usb2_generic_phy
);
100 * We're resetting only the device side because, if we're in host mode,
101 * XHCI driver will reset the host block. If dwc3 was configured for
102 * host-only mode, then we can return early.
104 if (dwc
->dr_mode
== USB_DR_MODE_HOST
)
107 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
108 reg
|= DWC3_DCTL_CSFTRST
;
109 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
112 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
113 if (!(reg
& DWC3_DCTL_CSFTRST
))
123 * dwc3_soft_reset - Issue soft reset
124 * @dwc: Pointer to our controller context structure
126 static int dwc3_soft_reset(struct dwc3
*dwc
)
128 unsigned long timeout
;
131 timeout
= jiffies
+ msecs_to_jiffies(500);
132 dwc3_writel(dwc
->regs
, DWC3_DCTL
, DWC3_DCTL_CSFTRST
);
134 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
135 if (!(reg
& DWC3_DCTL_CSFTRST
))
138 if (time_after(jiffies
, timeout
)) {
139 dev_err(dwc
->dev
, "Reset Timed Out\n");
150 * dwc3_frame_length_adjustment - Adjusts frame length if required
151 * @dwc3: Pointer to our controller context structure
152 * @fladj: Value of GFLADJ_30MHZ to adjust frame length
154 static void dwc3_frame_length_adjustment(struct dwc3
*dwc
, u32 fladj
)
159 if (dwc
->revision
< DWC3_REVISION_250A
)
165 reg
= dwc3_readl(dwc
->regs
, DWC3_GFLADJ
);
166 dft
= reg
& DWC3_GFLADJ_30MHZ_MASK
;
167 if (!dev_WARN_ONCE(dwc
->dev
, dft
== fladj
,
168 "request value same as default, ignoring\n")) {
169 reg
&= ~DWC3_GFLADJ_30MHZ_MASK
;
170 reg
|= DWC3_GFLADJ_30MHZ_SDBND_SEL
| fladj
;
171 dwc3_writel(dwc
->regs
, DWC3_GFLADJ
, reg
);
176 * dwc3_free_one_event_buffer - Frees one event buffer
177 * @dwc: Pointer to our controller context structure
178 * @evt: Pointer to event buffer to be freed
180 static void dwc3_free_one_event_buffer(struct dwc3
*dwc
,
181 struct dwc3_event_buffer
*evt
)
183 dma_free_coherent(dwc
->dev
, evt
->length
, evt
->buf
, evt
->dma
);
187 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
188 * @dwc: Pointer to our controller context structure
189 * @length: size of the event buffer
191 * Returns a pointer to the allocated event buffer structure on success
192 * otherwise ERR_PTR(errno).
194 static struct dwc3_event_buffer
*dwc3_alloc_one_event_buffer(struct dwc3
*dwc
,
197 struct dwc3_event_buffer
*evt
;
199 evt
= devm_kzalloc(dwc
->dev
, sizeof(*evt
), GFP_KERNEL
);
201 return ERR_PTR(-ENOMEM
);
204 evt
->length
= length
;
205 evt
->buf
= dma_alloc_coherent(dwc
->dev
, length
,
206 &evt
->dma
, GFP_KERNEL
);
208 return ERR_PTR(-ENOMEM
);
214 * dwc3_free_event_buffers - frees all allocated event buffers
215 * @dwc: Pointer to our controller context structure
217 static void dwc3_free_event_buffers(struct dwc3
*dwc
)
219 struct dwc3_event_buffer
*evt
;
223 dwc3_free_one_event_buffer(dwc
, evt
);
227 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
228 * @dwc: pointer to our controller context structure
229 * @length: size of event buffer
231 * Returns 0 on success otherwise negative errno. In the error case, dwc
232 * may contain some buffers allocated but not all which were requested.
234 static int dwc3_alloc_event_buffers(struct dwc3
*dwc
, unsigned length
)
236 struct dwc3_event_buffer
*evt
;
238 evt
= dwc3_alloc_one_event_buffer(dwc
, length
);
240 dev_err(dwc
->dev
, "can't allocate event buffer\n");
249 * dwc3_event_buffers_setup - setup our allocated event buffers
250 * @dwc: pointer to our controller context structure
252 * Returns 0 on success otherwise negative errno.
254 static int dwc3_event_buffers_setup(struct dwc3
*dwc
)
256 struct dwc3_event_buffer
*evt
;
259 dwc3_trace(trace_dwc3_core
,
260 "Event buf %p dma %08llx length %d\n",
261 evt
->buf
, (unsigned long long) evt
->dma
,
266 dwc3_writel(dwc
->regs
, DWC3_GEVNTADRLO(0),
267 lower_32_bits(evt
->dma
));
268 dwc3_writel(dwc
->regs
, DWC3_GEVNTADRHI(0),
269 upper_32_bits(evt
->dma
));
270 dwc3_writel(dwc
->regs
, DWC3_GEVNTSIZ(0),
271 DWC3_GEVNTSIZ_SIZE(evt
->length
));
272 dwc3_writel(dwc
->regs
, DWC3_GEVNTCOUNT(0), 0);
277 static void dwc3_event_buffers_cleanup(struct dwc3
*dwc
)
279 struct dwc3_event_buffer
*evt
;
285 dwc3_writel(dwc
->regs
, DWC3_GEVNTADRLO(0), 0);
286 dwc3_writel(dwc
->regs
, DWC3_GEVNTADRHI(0), 0);
287 dwc3_writel(dwc
->regs
, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
288 | DWC3_GEVNTSIZ_SIZE(0));
289 dwc3_writel(dwc
->regs
, DWC3_GEVNTCOUNT(0), 0);
292 static int dwc3_alloc_scratch_buffers(struct dwc3
*dwc
)
294 if (!dwc
->has_hibernation
)
297 if (!dwc
->nr_scratch
)
300 dwc
->scratchbuf
= kmalloc_array(dwc
->nr_scratch
,
301 DWC3_SCRATCHBUF_SIZE
, GFP_KERNEL
);
302 if (!dwc
->scratchbuf
)
308 static int dwc3_setup_scratch_buffers(struct dwc3
*dwc
)
310 dma_addr_t scratch_addr
;
314 if (!dwc
->has_hibernation
)
317 if (!dwc
->nr_scratch
)
320 /* should never fall here */
321 if (!WARN_ON(dwc
->scratchbuf
))
324 scratch_addr
= dma_map_single(dwc
->dev
, dwc
->scratchbuf
,
325 dwc
->nr_scratch
* DWC3_SCRATCHBUF_SIZE
,
327 if (dma_mapping_error(dwc
->dev
, scratch_addr
)) {
328 dev_err(dwc
->dev
, "failed to map scratch buffer\n");
333 dwc
->scratch_addr
= scratch_addr
;
335 param
= lower_32_bits(scratch_addr
);
337 ret
= dwc3_send_gadget_generic_command(dwc
,
338 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO
, param
);
342 param
= upper_32_bits(scratch_addr
);
344 ret
= dwc3_send_gadget_generic_command(dwc
,
345 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI
, param
);
352 dma_unmap_single(dwc
->dev
, dwc
->scratch_addr
, dwc
->nr_scratch
*
353 DWC3_SCRATCHBUF_SIZE
, DMA_BIDIRECTIONAL
);
359 static void dwc3_free_scratch_buffers(struct dwc3
*dwc
)
361 if (!dwc
->has_hibernation
)
364 if (!dwc
->nr_scratch
)
367 /* should never fall here */
368 if (!WARN_ON(dwc
->scratchbuf
))
371 dma_unmap_single(dwc
->dev
, dwc
->scratch_addr
, dwc
->nr_scratch
*
372 DWC3_SCRATCHBUF_SIZE
, DMA_BIDIRECTIONAL
);
373 kfree(dwc
->scratchbuf
);
376 static void dwc3_core_num_eps(struct dwc3
*dwc
)
378 struct dwc3_hwparams
*parms
= &dwc
->hwparams
;
380 dwc
->num_in_eps
= DWC3_NUM_IN_EPS(parms
);
381 dwc
->num_out_eps
= DWC3_NUM_EPS(parms
) - dwc
->num_in_eps
;
383 dwc3_trace(trace_dwc3_core
, "found %d IN and %d OUT endpoints",
384 dwc
->num_in_eps
, dwc
->num_out_eps
);
387 static void dwc3_cache_hwparams(struct dwc3
*dwc
)
389 struct dwc3_hwparams
*parms
= &dwc
->hwparams
;
391 parms
->hwparams0
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS0
);
392 parms
->hwparams1
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS1
);
393 parms
->hwparams2
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS2
);
394 parms
->hwparams3
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS3
);
395 parms
->hwparams4
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS4
);
396 parms
->hwparams5
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS5
);
397 parms
->hwparams6
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS6
);
398 parms
->hwparams7
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS7
);
399 parms
->hwparams8
= dwc3_readl(dwc
->regs
, DWC3_GHWPARAMS8
);
403 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
404 * @dwc: Pointer to our controller context structure
406 * Returns 0 on success. The USB PHY interfaces are configured but not
407 * initialized. The PHY interfaces and the PHYs get initialized together with
408 * the core in dwc3_core_init.
410 static int dwc3_phy_setup(struct dwc3
*dwc
)
415 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB3PIPECTL(0));
418 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
419 * to '0' during coreConsultant configuration. So default value
420 * will be '0' when the core is reset. Application needs to set it
421 * to '1' after the core initialization is completed.
423 if (dwc
->revision
> DWC3_REVISION_194A
)
424 reg
|= DWC3_GUSB3PIPECTL_SUSPHY
;
426 if (dwc
->u2ss_inp3_quirk
)
427 reg
|= DWC3_GUSB3PIPECTL_U2SSINP3OK
;
429 if (dwc
->dis_rxdet_inp3_quirk
)
430 reg
|= DWC3_GUSB3PIPECTL_DISRXDETINP3
;
432 if (dwc
->req_p1p2p3_quirk
)
433 reg
|= DWC3_GUSB3PIPECTL_REQP1P2P3
;
435 if (dwc
->del_p1p2p3_quirk
)
436 reg
|= DWC3_GUSB3PIPECTL_DEP1P2P3_EN
;
438 if (dwc
->del_phy_power_chg_quirk
)
439 reg
|= DWC3_GUSB3PIPECTL_DEPOCHANGE
;
441 if (dwc
->lfps_filter_quirk
)
442 reg
|= DWC3_GUSB3PIPECTL_LFPSFILT
;
444 if (dwc
->rx_detect_poll_quirk
)
445 reg
|= DWC3_GUSB3PIPECTL_RX_DETOPOLL
;
447 if (dwc
->tx_de_emphasis_quirk
)
448 reg
|= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc
->tx_de_emphasis
);
450 if (dwc
->dis_u3_susphy_quirk
)
451 reg
&= ~DWC3_GUSB3PIPECTL_SUSPHY
;
453 dwc3_writel(dwc
->regs
, DWC3_GUSB3PIPECTL(0), reg
);
455 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(0));
457 /* Select the HS PHY interface */
458 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc
->hwparams
.hwparams3
)) {
459 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI
:
460 if (dwc
->hsphy_interface
&&
461 !strncmp(dwc
->hsphy_interface
, "utmi", 4)) {
462 reg
&= ~DWC3_GUSB2PHYCFG_ULPI_UTMI
;
464 } else if (dwc
->hsphy_interface
&&
465 !strncmp(dwc
->hsphy_interface
, "ulpi", 4)) {
466 reg
|= DWC3_GUSB2PHYCFG_ULPI_UTMI
;
467 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(0), reg
);
469 /* Relying on default value. */
470 if (!(reg
& DWC3_GUSB2PHYCFG_ULPI_UTMI
))
474 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI
:
475 /* Making sure the interface and PHY are operational */
476 ret
= dwc3_soft_reset(dwc
);
482 ret
= dwc3_ulpi_init(dwc
);
491 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
492 * '0' during coreConsultant configuration. So default value will
493 * be '0' when the core is reset. Application needs to set it to
494 * '1' after the core initialization is completed.
496 if (dwc
->revision
> DWC3_REVISION_194A
)
497 reg
|= DWC3_GUSB2PHYCFG_SUSPHY
;
499 if (dwc
->dis_u2_susphy_quirk
)
500 reg
&= ~DWC3_GUSB2PHYCFG_SUSPHY
;
502 if (dwc
->dis_enblslpm_quirk
)
503 reg
&= ~DWC3_GUSB2PHYCFG_ENBLSLPM
;
505 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(0), reg
);
511 * dwc3_core_init - Low-level initialization of DWC3 Core
512 * @dwc: Pointer to our controller context structure
514 * Returns 0 on success otherwise negative errno.
516 static int dwc3_core_init(struct dwc3
*dwc
)
518 u32 hwparams4
= dwc
->hwparams
.hwparams4
;
522 reg
= dwc3_readl(dwc
->regs
, DWC3_GSNPSID
);
523 /* This should read as U3 followed by revision number */
524 if ((reg
& DWC3_GSNPSID_MASK
) == 0x55330000) {
525 /* Detected DWC_usb3 IP */
527 } else if ((reg
& DWC3_GSNPSID_MASK
) == 0x33310000) {
528 /* Detected DWC_usb31 IP */
529 dwc
->revision
= dwc3_readl(dwc
->regs
, DWC3_VER_NUMBER
);
530 dwc
->revision
|= DWC3_REVISION_IS_DWC31
;
532 dev_err(dwc
->dev
, "this is not a DesignWare USB3 DRD Core\n");
538 * Write Linux Version Code to our GUID register so it's easy to figure
539 * out which kernel version a bug was found.
541 dwc3_writel(dwc
->regs
, DWC3_GUID
, LINUX_VERSION_CODE
);
543 /* Handle USB2.0-only core configuration */
544 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc
->hwparams
.hwparams3
) ==
545 DWC3_GHWPARAMS3_SSPHY_IFC_DIS
) {
546 if (dwc
->maximum_speed
== USB_SPEED_SUPER
)
547 dwc
->maximum_speed
= USB_SPEED_HIGH
;
550 /* issue device SoftReset too */
551 ret
= dwc3_soft_reset(dwc
);
555 ret
= dwc3_core_soft_reset(dwc
);
559 reg
= dwc3_readl(dwc
->regs
, DWC3_GCTL
);
560 reg
&= ~DWC3_GCTL_SCALEDOWN_MASK
;
562 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc
->hwparams
.hwparams1
)) {
563 case DWC3_GHWPARAMS1_EN_PWROPT_CLK
:
565 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
566 * issue which would cause xHCI compliance tests to fail.
568 * Because of that we cannot enable clock gating on such
573 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
576 if ((dwc
->dr_mode
== USB_DR_MODE_HOST
||
577 dwc
->dr_mode
== USB_DR_MODE_OTG
) &&
578 (dwc
->revision
>= DWC3_REVISION_210A
&&
579 dwc
->revision
<= DWC3_REVISION_250A
))
580 reg
|= DWC3_GCTL_DSBLCLKGTNG
| DWC3_GCTL_SOFITPSYNC
;
582 reg
&= ~DWC3_GCTL_DSBLCLKGTNG
;
584 case DWC3_GHWPARAMS1_EN_PWROPT_HIB
:
585 /* enable hibernation here */
586 dwc
->nr_scratch
= DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4
);
589 * REVISIT Enabling this bit so that host-mode hibernation
590 * will work. Device-mode hibernation is not yet implemented.
592 reg
|= DWC3_GCTL_GBLHIBERNATIONEN
;
595 dwc3_trace(trace_dwc3_core
, "No power optimization available\n");
598 /* check if current dwc3 is on simulation board */
599 if (dwc
->hwparams
.hwparams6
& DWC3_GHWPARAMS6_EN_FPGA
) {
600 dwc3_trace(trace_dwc3_core
,
601 "running on FPGA platform\n");
605 WARN_ONCE(dwc
->disable_scramble_quirk
&& !dwc
->is_fpga
,
606 "disable_scramble cannot be used on non-FPGA builds\n");
608 if (dwc
->disable_scramble_quirk
&& dwc
->is_fpga
)
609 reg
|= DWC3_GCTL_DISSCRAMBLE
;
611 reg
&= ~DWC3_GCTL_DISSCRAMBLE
;
613 if (dwc
->u2exit_lfps_quirk
)
614 reg
|= DWC3_GCTL_U2EXIT_LFPS
;
617 * WORKAROUND: DWC3 revisions <1.90a have a bug
618 * where the device can fail to connect at SuperSpeed
619 * and falls back to high-speed mode which causes
620 * the device to enter a Connect/Disconnect loop
622 if (dwc
->revision
< DWC3_REVISION_190A
)
623 reg
|= DWC3_GCTL_U2RSTECN
;
625 dwc3_core_num_eps(dwc
);
627 dwc3_writel(dwc
->regs
, DWC3_GCTL
, reg
);
629 ret
= dwc3_alloc_scratch_buffers(dwc
);
633 ret
= dwc3_setup_scratch_buffers(dwc
);
640 dwc3_free_scratch_buffers(dwc
);
643 usb_phy_shutdown(dwc
->usb2_phy
);
644 usb_phy_shutdown(dwc
->usb3_phy
);
645 phy_exit(dwc
->usb2_generic_phy
);
646 phy_exit(dwc
->usb3_generic_phy
);
652 static void dwc3_core_exit(struct dwc3
*dwc
)
654 dwc3_free_scratch_buffers(dwc
);
655 usb_phy_shutdown(dwc
->usb2_phy
);
656 usb_phy_shutdown(dwc
->usb3_phy
);
657 phy_exit(dwc
->usb2_generic_phy
);
658 phy_exit(dwc
->usb3_generic_phy
);
661 static int dwc3_core_get_phy(struct dwc3
*dwc
)
663 struct device
*dev
= dwc
->dev
;
664 struct device_node
*node
= dev
->of_node
;
668 dwc
->usb2_phy
= devm_usb_get_phy_by_phandle(dev
, "usb-phy", 0);
669 dwc
->usb3_phy
= devm_usb_get_phy_by_phandle(dev
, "usb-phy", 1);
671 dwc
->usb2_phy
= devm_usb_get_phy(dev
, USB_PHY_TYPE_USB2
);
672 dwc
->usb3_phy
= devm_usb_get_phy(dev
, USB_PHY_TYPE_USB3
);
675 if (IS_ERR(dwc
->usb2_phy
)) {
676 ret
= PTR_ERR(dwc
->usb2_phy
);
677 if (ret
== -ENXIO
|| ret
== -ENODEV
) {
678 dwc
->usb2_phy
= NULL
;
679 } else if (ret
== -EPROBE_DEFER
) {
682 dev_err(dev
, "no usb2 phy configured\n");
687 if (IS_ERR(dwc
->usb3_phy
)) {
688 ret
= PTR_ERR(dwc
->usb3_phy
);
689 if (ret
== -ENXIO
|| ret
== -ENODEV
) {
690 dwc
->usb3_phy
= NULL
;
691 } else if (ret
== -EPROBE_DEFER
) {
694 dev_err(dev
, "no usb3 phy configured\n");
699 dwc
->usb2_generic_phy
= devm_phy_get(dev
, "usb2-phy");
700 if (IS_ERR(dwc
->usb2_generic_phy
)) {
701 ret
= PTR_ERR(dwc
->usb2_generic_phy
);
702 if (ret
== -ENOSYS
|| ret
== -ENODEV
) {
703 dwc
->usb2_generic_phy
= NULL
;
704 } else if (ret
== -EPROBE_DEFER
) {
707 dev_err(dev
, "no usb2 phy configured\n");
712 dwc
->usb3_generic_phy
= devm_phy_get(dev
, "usb3-phy");
713 if (IS_ERR(dwc
->usb3_generic_phy
)) {
714 ret
= PTR_ERR(dwc
->usb3_generic_phy
);
715 if (ret
== -ENOSYS
|| ret
== -ENODEV
) {
716 dwc
->usb3_generic_phy
= NULL
;
717 } else if (ret
== -EPROBE_DEFER
) {
720 dev_err(dev
, "no usb3 phy configured\n");
728 static int dwc3_core_init_mode(struct dwc3
*dwc
)
730 struct device
*dev
= dwc
->dev
;
733 switch (dwc
->dr_mode
) {
734 case USB_DR_MODE_PERIPHERAL
:
735 dwc3_set_mode(dwc
, DWC3_GCTL_PRTCAP_DEVICE
);
736 ret
= dwc3_gadget_init(dwc
);
738 dev_err(dev
, "failed to initialize gadget\n");
742 case USB_DR_MODE_HOST
:
743 dwc3_set_mode(dwc
, DWC3_GCTL_PRTCAP_HOST
);
744 ret
= dwc3_host_init(dwc
);
746 dev_err(dev
, "failed to initialize host\n");
750 case USB_DR_MODE_OTG
:
751 dwc3_set_mode(dwc
, DWC3_GCTL_PRTCAP_OTG
);
752 ret
= dwc3_host_init(dwc
);
754 dev_err(dev
, "failed to initialize host\n");
758 ret
= dwc3_gadget_init(dwc
);
760 dev_err(dev
, "failed to initialize gadget\n");
765 dev_err(dev
, "Unsupported mode of operation %d\n", dwc
->dr_mode
);
772 static void dwc3_core_exit_mode(struct dwc3
*dwc
)
774 switch (dwc
->dr_mode
) {
775 case USB_DR_MODE_PERIPHERAL
:
776 dwc3_gadget_exit(dwc
);
778 case USB_DR_MODE_HOST
:
781 case USB_DR_MODE_OTG
:
783 dwc3_gadget_exit(dwc
);
791 #define DWC3_ALIGN_MASK (16 - 1)
793 static int dwc3_probe(struct platform_device
*pdev
)
795 struct device
*dev
= &pdev
->dev
;
796 struct dwc3_platform_data
*pdata
= dev_get_platdata(dev
);
797 struct resource
*res
;
799 u8 lpm_nyet_threshold
;
809 mem
= devm_kzalloc(dev
, sizeof(*dwc
) + DWC3_ALIGN_MASK
, GFP_KERNEL
);
813 dwc
= PTR_ALIGN(mem
, DWC3_ALIGN_MASK
+ 1);
817 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
819 dev_err(dev
, "missing IRQ\n");
822 dwc
->xhci_resources
[1].start
= res
->start
;
823 dwc
->xhci_resources
[1].end
= res
->end
;
824 dwc
->xhci_resources
[1].flags
= res
->flags
;
825 dwc
->xhci_resources
[1].name
= res
->name
;
827 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
829 dev_err(dev
, "missing memory resource\n");
833 dwc
->xhci_resources
[0].start
= res
->start
;
834 dwc
->xhci_resources
[0].end
= dwc
->xhci_resources
[0].start
+
836 dwc
->xhci_resources
[0].flags
= res
->flags
;
837 dwc
->xhci_resources
[0].name
= res
->name
;
839 res
->start
+= DWC3_GLOBALS_REGS_START
;
842 * Request memory region but exclude xHCI regs,
843 * since it will be requested by the xhci-plat driver.
845 regs
= devm_ioremap_resource(dev
, res
);
852 dwc
->regs_size
= resource_size(res
);
854 /* default to highest possible threshold */
855 lpm_nyet_threshold
= 0xff;
857 /* default to -3.5dB de-emphasis */
861 * default to assert utmi_sleep_n and use maximum allowed HIRD
862 * threshold value of 0b1100
866 dwc
->maximum_speed
= usb_get_maximum_speed(dev
);
867 dwc
->dr_mode
= usb_get_dr_mode(dev
);
869 dwc
->has_lpm_erratum
= device_property_read_bool(dev
,
870 "snps,has-lpm-erratum");
871 device_property_read_u8(dev
, "snps,lpm-nyet-threshold",
872 &lpm_nyet_threshold
);
873 dwc
->is_utmi_l1_suspend
= device_property_read_bool(dev
,
874 "snps,is-utmi-l1-suspend");
875 device_property_read_u8(dev
, "snps,hird-threshold",
877 dwc
->usb3_lpm_capable
= device_property_read_bool(dev
,
878 "snps,usb3_lpm_capable");
880 dwc
->disable_scramble_quirk
= device_property_read_bool(dev
,
881 "snps,disable_scramble_quirk");
882 dwc
->u2exit_lfps_quirk
= device_property_read_bool(dev
,
883 "snps,u2exit_lfps_quirk");
884 dwc
->u2ss_inp3_quirk
= device_property_read_bool(dev
,
885 "snps,u2ss_inp3_quirk");
886 dwc
->req_p1p2p3_quirk
= device_property_read_bool(dev
,
887 "snps,req_p1p2p3_quirk");
888 dwc
->del_p1p2p3_quirk
= device_property_read_bool(dev
,
889 "snps,del_p1p2p3_quirk");
890 dwc
->del_phy_power_chg_quirk
= device_property_read_bool(dev
,
891 "snps,del_phy_power_chg_quirk");
892 dwc
->lfps_filter_quirk
= device_property_read_bool(dev
,
893 "snps,lfps_filter_quirk");
894 dwc
->rx_detect_poll_quirk
= device_property_read_bool(dev
,
895 "snps,rx_detect_poll_quirk");
896 dwc
->dis_u3_susphy_quirk
= device_property_read_bool(dev
,
897 "snps,dis_u3_susphy_quirk");
898 dwc
->dis_u2_susphy_quirk
= device_property_read_bool(dev
,
899 "snps,dis_u2_susphy_quirk");
900 dwc
->dis_enblslpm_quirk
= device_property_read_bool(dev
,
901 "snps,dis_enblslpm_quirk");
902 dwc
->dis_rxdet_inp3_quirk
= device_property_read_bool(dev
,
903 "snps,dis_rxdet_inp3_quirk");
905 dwc
->tx_de_emphasis_quirk
= device_property_read_bool(dev
,
906 "snps,tx_de_emphasis_quirk");
907 device_property_read_u8(dev
, "snps,tx_de_emphasis",
909 device_property_read_string(dev
, "snps,hsphy_interface",
910 &dwc
->hsphy_interface
);
911 device_property_read_u32(dev
, "snps,quirk-frame-length-adjustment",
915 dwc
->maximum_speed
= pdata
->maximum_speed
;
916 dwc
->has_lpm_erratum
= pdata
->has_lpm_erratum
;
917 if (pdata
->lpm_nyet_threshold
)
918 lpm_nyet_threshold
= pdata
->lpm_nyet_threshold
;
919 dwc
->is_utmi_l1_suspend
= pdata
->is_utmi_l1_suspend
;
920 if (pdata
->hird_threshold
)
921 hird_threshold
= pdata
->hird_threshold
;
923 dwc
->usb3_lpm_capable
= pdata
->usb3_lpm_capable
;
924 dwc
->dr_mode
= pdata
->dr_mode
;
926 dwc
->disable_scramble_quirk
= pdata
->disable_scramble_quirk
;
927 dwc
->u2exit_lfps_quirk
= pdata
->u2exit_lfps_quirk
;
928 dwc
->u2ss_inp3_quirk
= pdata
->u2ss_inp3_quirk
;
929 dwc
->req_p1p2p3_quirk
= pdata
->req_p1p2p3_quirk
;
930 dwc
->del_p1p2p3_quirk
= pdata
->del_p1p2p3_quirk
;
931 dwc
->del_phy_power_chg_quirk
= pdata
->del_phy_power_chg_quirk
;
932 dwc
->lfps_filter_quirk
= pdata
->lfps_filter_quirk
;
933 dwc
->rx_detect_poll_quirk
= pdata
->rx_detect_poll_quirk
;
934 dwc
->dis_u3_susphy_quirk
= pdata
->dis_u3_susphy_quirk
;
935 dwc
->dis_u2_susphy_quirk
= pdata
->dis_u2_susphy_quirk
;
936 dwc
->dis_enblslpm_quirk
= pdata
->dis_enblslpm_quirk
;
937 dwc
->dis_rxdet_inp3_quirk
= pdata
->dis_rxdet_inp3_quirk
;
939 dwc
->tx_de_emphasis_quirk
= pdata
->tx_de_emphasis_quirk
;
940 if (pdata
->tx_de_emphasis
)
941 tx_de_emphasis
= pdata
->tx_de_emphasis
;
943 dwc
->hsphy_interface
= pdata
->hsphy_interface
;
944 fladj
= pdata
->fladj_value
;
947 dwc
->lpm_nyet_threshold
= lpm_nyet_threshold
;
948 dwc
->tx_de_emphasis
= tx_de_emphasis
;
950 dwc
->hird_threshold
= hird_threshold
951 | (dwc
->is_utmi_l1_suspend
<< 4);
953 platform_set_drvdata(pdev
, dwc
);
954 dwc3_cache_hwparams(dwc
);
956 ret
= dwc3_phy_setup(dwc
);
960 ret
= dwc3_core_get_phy(dwc
);
964 spin_lock_init(&dwc
->lock
);
966 if (!dev
->dma_mask
) {
967 dev
->dma_mask
= dev
->parent
->dma_mask
;
968 dev
->dma_parms
= dev
->parent
->dma_parms
;
969 dma_set_coherent_mask(dev
, dev
->parent
->coherent_dma_mask
);
972 pm_runtime_enable(dev
);
973 pm_runtime_get_sync(dev
);
974 pm_runtime_forbid(dev
);
976 ret
= dwc3_alloc_event_buffers(dwc
, DWC3_EVENT_BUFFERS_SIZE
);
978 dev_err(dwc
->dev
, "failed to allocate event buffers\n");
983 if (IS_ENABLED(CONFIG_USB_DWC3_HOST
))
984 dwc
->dr_mode
= USB_DR_MODE_HOST
;
985 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET
))
986 dwc
->dr_mode
= USB_DR_MODE_PERIPHERAL
;
988 if (dwc
->dr_mode
== USB_DR_MODE_UNKNOWN
)
989 dwc
->dr_mode
= USB_DR_MODE_OTG
;
991 ret
= dwc3_core_init(dwc
);
993 dev_err(dev
, "failed to initialize core\n");
997 /* Check the maximum_speed parameter */
998 switch (dwc
->maximum_speed
) {
1000 case USB_SPEED_FULL
:
1001 case USB_SPEED_HIGH
:
1002 case USB_SPEED_SUPER
:
1003 case USB_SPEED_SUPER_PLUS
:
1006 dev_err(dev
, "invalid maximum_speed parameter %d\n",
1007 dwc
->maximum_speed
);
1009 case USB_SPEED_UNKNOWN
:
1010 /* default to superspeed */
1011 dwc
->maximum_speed
= USB_SPEED_SUPER
;
1014 * default to superspeed plus if we are capable.
1016 if (dwc3_is_usb31(dwc
) &&
1017 (DWC3_GHWPARAMS3_SSPHY_IFC(dwc
->hwparams
.hwparams3
) ==
1018 DWC3_GHWPARAMS3_SSPHY_IFC_GEN2
))
1019 dwc
->maximum_speed
= USB_SPEED_SUPER_PLUS
;
1024 /* Adjust Frame Length */
1025 dwc3_frame_length_adjustment(dwc
, fladj
);
1027 usb_phy_set_suspend(dwc
->usb2_phy
, 0);
1028 usb_phy_set_suspend(dwc
->usb3_phy
, 0);
1029 ret
= phy_power_on(dwc
->usb2_generic_phy
);
1033 ret
= phy_power_on(dwc
->usb3_generic_phy
);
1037 ret
= dwc3_event_buffers_setup(dwc
);
1039 dev_err(dwc
->dev
, "failed to setup event buffers\n");
1043 ret
= dwc3_core_init_mode(dwc
);
1047 dwc3_debugfs_init(dwc
);
1048 pm_runtime_allow(dev
);
1053 dwc3_event_buffers_cleanup(dwc
);
1056 phy_power_off(dwc
->usb3_generic_phy
);
1059 phy_power_off(dwc
->usb2_generic_phy
);
1062 usb_phy_set_suspend(dwc
->usb2_phy
, 1);
1063 usb_phy_set_suspend(dwc
->usb3_phy
, 1);
1064 dwc3_core_exit(dwc
);
1067 dwc3_free_event_buffers(dwc
);
1068 dwc3_ulpi_exit(dwc
);
1072 * restore res->start back to its original value so that, in case the
1073 * probe is deferred, we don't end up getting error in request the
1074 * memory region the next time probe is called.
1076 res
->start
-= DWC3_GLOBALS_REGS_START
;
1081 static int dwc3_remove(struct platform_device
*pdev
)
1083 struct dwc3
*dwc
= platform_get_drvdata(pdev
);
1084 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1087 * restore res->start back to its original value so that, in case the
1088 * probe is deferred, we don't end up getting error in request the
1089 * memory region the next time probe is called.
1091 res
->start
-= DWC3_GLOBALS_REGS_START
;
1093 dwc3_debugfs_exit(dwc
);
1094 dwc3_core_exit_mode(dwc
);
1095 dwc3_event_buffers_cleanup(dwc
);
1096 dwc3_free_event_buffers(dwc
);
1098 usb_phy_set_suspend(dwc
->usb2_phy
, 1);
1099 usb_phy_set_suspend(dwc
->usb3_phy
, 1);
1100 phy_power_off(dwc
->usb2_generic_phy
);
1101 phy_power_off(dwc
->usb3_generic_phy
);
1103 dwc3_core_exit(dwc
);
1104 dwc3_ulpi_exit(dwc
);
1106 pm_runtime_put_sync(&pdev
->dev
);
1107 pm_runtime_disable(&pdev
->dev
);
1112 #ifdef CONFIG_PM_SLEEP
1113 static int dwc3_suspend(struct device
*dev
)
1115 struct dwc3
*dwc
= dev_get_drvdata(dev
);
1116 unsigned long flags
;
1118 spin_lock_irqsave(&dwc
->lock
, flags
);
1120 switch (dwc
->dr_mode
) {
1121 case USB_DR_MODE_PERIPHERAL
:
1122 case USB_DR_MODE_OTG
:
1123 dwc3_gadget_suspend(dwc
);
1125 case USB_DR_MODE_HOST
:
1127 dwc3_event_buffers_cleanup(dwc
);
1131 dwc
->gctl
= dwc3_readl(dwc
->regs
, DWC3_GCTL
);
1132 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1134 usb_phy_shutdown(dwc
->usb3_phy
);
1135 usb_phy_shutdown(dwc
->usb2_phy
);
1136 phy_exit(dwc
->usb2_generic_phy
);
1137 phy_exit(dwc
->usb3_generic_phy
);
1139 usb_phy_set_suspend(dwc
->usb2_phy
, 1);
1140 usb_phy_set_suspend(dwc
->usb3_phy
, 1);
1141 WARN_ON(phy_power_off(dwc
->usb2_generic_phy
) < 0);
1142 WARN_ON(phy_power_off(dwc
->usb3_generic_phy
) < 0);
1144 pinctrl_pm_select_sleep_state(dev
);
1149 static int dwc3_resume(struct device
*dev
)
1151 struct dwc3
*dwc
= dev_get_drvdata(dev
);
1152 unsigned long flags
;
1155 pinctrl_pm_select_default_state(dev
);
1157 usb_phy_set_suspend(dwc
->usb2_phy
, 0);
1158 usb_phy_set_suspend(dwc
->usb3_phy
, 0);
1159 ret
= phy_power_on(dwc
->usb2_generic_phy
);
1163 ret
= phy_power_on(dwc
->usb3_generic_phy
);
1165 goto err_usb2phy_power
;
1167 usb_phy_init(dwc
->usb3_phy
);
1168 usb_phy_init(dwc
->usb2_phy
);
1169 ret
= phy_init(dwc
->usb2_generic_phy
);
1171 goto err_usb3phy_power
;
1173 ret
= phy_init(dwc
->usb3_generic_phy
);
1175 goto err_usb2phy_init
;
1177 spin_lock_irqsave(&dwc
->lock
, flags
);
1179 dwc3_event_buffers_setup(dwc
);
1180 dwc3_writel(dwc
->regs
, DWC3_GCTL
, dwc
->gctl
);
1182 switch (dwc
->dr_mode
) {
1183 case USB_DR_MODE_PERIPHERAL
:
1184 case USB_DR_MODE_OTG
:
1185 dwc3_gadget_resume(dwc
);
1187 case USB_DR_MODE_HOST
:
1193 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1195 pm_runtime_disable(dev
);
1196 pm_runtime_set_active(dev
);
1197 pm_runtime_enable(dev
);
1202 phy_exit(dwc
->usb2_generic_phy
);
1205 phy_power_off(dwc
->usb3_generic_phy
);
1208 phy_power_off(dwc
->usb2_generic_phy
);
1213 static const struct dev_pm_ops dwc3_dev_pm_ops
= {
1214 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend
, dwc3_resume
)
1217 #define DWC3_PM_OPS &(dwc3_dev_pm_ops)
1219 #define DWC3_PM_OPS NULL
1223 static const struct of_device_id of_dwc3_match
[] = {
1225 .compatible
= "snps,dwc3"
1228 .compatible
= "synopsys,dwc3"
1232 MODULE_DEVICE_TABLE(of
, of_dwc3_match
);
1237 #define ACPI_ID_INTEL_BSW "808622B7"
1239 static const struct acpi_device_id dwc3_acpi_match
[] = {
1240 { ACPI_ID_INTEL_BSW
, 0 },
1243 MODULE_DEVICE_TABLE(acpi
, dwc3_acpi_match
);
1246 static struct platform_driver dwc3_driver
= {
1247 .probe
= dwc3_probe
,
1248 .remove
= dwc3_remove
,
1251 .of_match_table
= of_match_ptr(of_dwc3_match
),
1252 .acpi_match_table
= ACPI_PTR(dwc3_acpi_match
),
1257 module_platform_driver(dwc3_driver
);
1259 MODULE_ALIAS("platform:dwc3");
1260 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1261 MODULE_LICENSE("GPL v2");
1262 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");