2 * core.c - DesignWare USB3 DRD Controller Core file
4 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 * Taken from Linux Kernel v3.19-rc1 (drivers/usb/dwc3/core.c) and ported
12 * commit cd72f890d2 : usb: dwc3: core: enable phy suspend quirk on non-FPGA
14 * SPDX-License-Identifier: GPL-2.0
17 #include <linux/version.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/spinlock.h>
22 #include <linux/platform_device.h>
23 #include <linux/interrupt.h>
24 #include <linux/ioport.h>
26 #include <linux/list.h>
27 #include <linux/delay.h>
28 #include <linux/dma-mapping.h>
30 #include <linux/acpi.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>
37 #include "platform_data.h"
44 /* -------------------------------------------------------------------------- */
46 void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
50 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
51 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
52 reg |= DWC3_GCTL_PRTCAPDIR(mode);
53 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
57 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
58 * @dwc: pointer to our context structure
60 static int dwc3_core_soft_reset(struct dwc3 *dwc)
65 /* Before Resetting PHY, put Core in Reset */
66 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
67 reg |= DWC3_GCTL_CORESOFTRESET;
68 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
70 /* Assert USB3 PHY reset */
71 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
72 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
73 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
75 /* Assert USB2 PHY reset */
76 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
77 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
78 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
80 usb_phy_init(dwc->usb2_phy);
81 usb_phy_init(dwc->usb3_phy);
82 ret = phy_init(dwc->usb2_generic_phy);
86 ret = phy_init(dwc->usb3_generic_phy);
88 phy_exit(dwc->usb2_generic_phy);
93 /* Clear USB3 PHY reset */
94 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
95 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
96 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
98 /* Clear USB2 PHY reset */
99 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
100 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
101 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
105 /* After PHYs are stable we can take Core out of reset state */
106 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
107 reg &= ~DWC3_GCTL_CORESOFTRESET;
108 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
114 * dwc3_free_one_event_buffer - Frees one event buffer
115 * @dwc: Pointer to our controller context structure
116 * @evt: Pointer to event buffer to be freed
118 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
119 struct dwc3_event_buffer *evt)
121 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
125 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
126 * @dwc: Pointer to our controller context structure
127 * @length: size of the event buffer
129 * Returns a pointer to the allocated event buffer structure on success
130 * otherwise ERR_PTR(errno).
132 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
135 struct dwc3_event_buffer *evt;
137 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
139 return ERR_PTR(-ENOMEM);
142 evt->length = length;
143 evt->buf = dma_alloc_coherent(dwc->dev, length,
144 &evt->dma, GFP_KERNEL);
146 return ERR_PTR(-ENOMEM);
152 * dwc3_free_event_buffers - frees all allocated event buffers
153 * @dwc: Pointer to our controller context structure
155 static void dwc3_free_event_buffers(struct dwc3 *dwc)
157 struct dwc3_event_buffer *evt;
160 for (i = 0; i < dwc->num_event_buffers; i++) {
161 evt = dwc->ev_buffs[i];
163 dwc3_free_one_event_buffer(dwc, evt);
168 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
169 * @dwc: pointer to our controller context structure
170 * @length: size of event buffer
172 * Returns 0 on success otherwise negative errno. In the error case, dwc
173 * may contain some buffers allocated but not all which were requested.
175 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
180 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
181 dwc->num_event_buffers = num;
183 dwc->ev_buffs = devm_kzalloc(dwc->dev, sizeof(*dwc->ev_buffs) * num,
188 for (i = 0; i < num; i++) {
189 struct dwc3_event_buffer *evt;
191 evt = dwc3_alloc_one_event_buffer(dwc, length);
193 dev_err(dwc->dev, "can't allocate event buffer\n");
196 dwc->ev_buffs[i] = evt;
203 * dwc3_event_buffers_setup - setup our allocated event buffers
204 * @dwc: pointer to our controller context structure
206 * Returns 0 on success otherwise negative errno.
208 static int dwc3_event_buffers_setup(struct dwc3 *dwc)
210 struct dwc3_event_buffer *evt;
213 for (n = 0; n < dwc->num_event_buffers; n++) {
214 evt = dwc->ev_buffs[n];
215 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
216 evt->buf, (unsigned long long) evt->dma,
221 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
222 lower_32_bits(evt->dma));
223 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
224 upper_32_bits(evt->dma));
225 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
226 DWC3_GEVNTSIZ_SIZE(evt->length));
227 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
233 static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
235 struct dwc3_event_buffer *evt;
238 for (n = 0; n < dwc->num_event_buffers; n++) {
239 evt = dwc->ev_buffs[n];
243 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
244 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
245 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK
246 | DWC3_GEVNTSIZ_SIZE(0));
247 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
251 static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
253 if (!dwc->has_hibernation)
256 if (!dwc->nr_scratch)
259 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
260 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
261 if (!dwc->scratchbuf)
267 static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
269 dma_addr_t scratch_addr;
273 if (!dwc->has_hibernation)
276 if (!dwc->nr_scratch)
279 /* should never fall here */
280 if (!WARN_ON(dwc->scratchbuf))
283 scratch_addr = dma_map_single(dwc->dev, dwc->scratchbuf,
284 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
286 if (dma_mapping_error(dwc->dev, scratch_addr)) {
287 dev_err(dwc->dev, "failed to map scratch buffer\n");
292 dwc->scratch_addr = scratch_addr;
294 param = lower_32_bits(scratch_addr);
296 ret = dwc3_send_gadget_generic_command(dwc,
297 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
301 param = upper_32_bits(scratch_addr);
303 ret = dwc3_send_gadget_generic_command(dwc,
304 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
311 dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
312 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
318 static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
320 if (!dwc->has_hibernation)
323 if (!dwc->nr_scratch)
326 /* should never fall here */
327 if (!WARN_ON(dwc->scratchbuf))
330 dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
331 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
332 kfree(dwc->scratchbuf);
335 static void dwc3_core_num_eps(struct dwc3 *dwc)
337 struct dwc3_hwparams *parms = &dwc->hwparams;
339 dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
340 dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
342 dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n",
343 dwc->num_in_eps, dwc->num_out_eps);
346 static void dwc3_cache_hwparams(struct dwc3 *dwc)
348 struct dwc3_hwparams *parms = &dwc->hwparams;
350 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
351 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
352 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
353 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
354 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
355 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
356 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
357 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
358 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
362 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
363 * @dwc: Pointer to our controller context structure
365 static void dwc3_phy_setup(struct dwc3 *dwc)
369 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
372 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
373 * to '0' during coreConsultant configuration. So default value
374 * will be '0' when the core is reset. Application needs to set it
375 * to '1' after the core initialization is completed.
377 if (dwc->revision > DWC3_REVISION_194A)
378 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
380 if (dwc->u2ss_inp3_quirk)
381 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
383 if (dwc->req_p1p2p3_quirk)
384 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
386 if (dwc->del_p1p2p3_quirk)
387 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
389 if (dwc->del_phy_power_chg_quirk)
390 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
392 if (dwc->lfps_filter_quirk)
393 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
395 if (dwc->rx_detect_poll_quirk)
396 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
398 if (dwc->tx_de_emphasis_quirk)
399 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
401 if (dwc->dis_u3_susphy_quirk)
402 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
404 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
408 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
411 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
412 * '0' during coreConsultant configuration. So default value will
413 * be '0' when the core is reset. Application needs to set it to
414 * '1' after the core initialization is completed.
416 if (dwc->revision > DWC3_REVISION_194A)
417 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
419 if (dwc->dis_u2_susphy_quirk)
420 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
422 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
428 * dwc3_core_init - Low-level initialization of DWC3 Core
429 * @dwc: Pointer to our controller context structure
431 * Returns 0 on success otherwise negative errno.
433 static int dwc3_core_init(struct dwc3 *dwc)
435 unsigned long timeout;
436 u32 hwparams4 = dwc->hwparams.hwparams4;
440 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
441 /* This should read as U3 followed by revision number */
442 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
443 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
450 * Write Linux Version Code to our GUID register so it's easy to figure
451 * out which kernel version a bug was found.
453 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
455 /* Handle USB2.0-only core configuration */
456 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
457 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
458 if (dwc->maximum_speed == USB_SPEED_SUPER)
459 dwc->maximum_speed = USB_SPEED_HIGH;
462 /* issue device SoftReset too */
463 timeout = jiffies + msecs_to_jiffies(500);
464 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
466 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
467 if (!(reg & DWC3_DCTL_CSFTRST))
470 if (time_after(jiffies, timeout)) {
471 dev_err(dwc->dev, "Reset Timed Out\n");
479 ret = dwc3_core_soft_reset(dwc);
483 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
484 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
486 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
487 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
489 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
490 * issue which would cause xHCI compliance tests to fail.
492 * Because of that we cannot enable clock gating on such
497 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
500 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
501 dwc->dr_mode == USB_DR_MODE_OTG) &&
502 (dwc->revision >= DWC3_REVISION_210A &&
503 dwc->revision <= DWC3_REVISION_250A))
504 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
506 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
508 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
509 /* enable hibernation here */
510 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
513 * REVISIT Enabling this bit so that host-mode hibernation
514 * will work. Device-mode hibernation is not yet implemented.
516 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
519 dev_dbg(dwc->dev, "No power optimization available\n");
522 /* check if current dwc3 is on simulation board */
523 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
524 dev_dbg(dwc->dev, "it is on FPGA board\n");
528 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
529 "disable_scramble cannot be used on non-FPGA builds\n");
531 if (dwc->disable_scramble_quirk && dwc->is_fpga)
532 reg |= DWC3_GCTL_DISSCRAMBLE;
534 reg &= ~DWC3_GCTL_DISSCRAMBLE;
536 if (dwc->u2exit_lfps_quirk)
537 reg |= DWC3_GCTL_U2EXIT_LFPS;
540 * WORKAROUND: DWC3 revisions <1.90a have a bug
541 * where the device can fail to connect at SuperSpeed
542 * and falls back to high-speed mode which causes
543 * the device to enter a Connect/Disconnect loop
545 if (dwc->revision < DWC3_REVISION_190A)
546 reg |= DWC3_GCTL_U2RSTECN;
548 dwc3_core_num_eps(dwc);
550 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
554 ret = dwc3_alloc_scratch_buffers(dwc);
558 ret = dwc3_setup_scratch_buffers(dwc);
565 dwc3_free_scratch_buffers(dwc);
568 usb_phy_shutdown(dwc->usb2_phy);
569 usb_phy_shutdown(dwc->usb3_phy);
570 phy_exit(dwc->usb2_generic_phy);
571 phy_exit(dwc->usb3_generic_phy);
577 static void dwc3_core_exit(struct dwc3 *dwc)
579 dwc3_free_scratch_buffers(dwc);
580 usb_phy_shutdown(dwc->usb2_phy);
581 usb_phy_shutdown(dwc->usb3_phy);
582 phy_exit(dwc->usb2_generic_phy);
583 phy_exit(dwc->usb3_generic_phy);
586 static int dwc3_core_get_phy(struct dwc3 *dwc)
588 struct device *dev = dwc->dev;
589 struct device_node *node = dev->of_node;
593 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
594 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
596 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
597 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
600 if (IS_ERR(dwc->usb2_phy)) {
601 ret = PTR_ERR(dwc->usb2_phy);
602 if (ret == -ENXIO || ret == -ENODEV) {
603 dwc->usb2_phy = NULL;
604 } else if (ret == -EPROBE_DEFER) {
607 dev_err(dev, "no usb2 phy configured\n");
612 if (IS_ERR(dwc->usb3_phy)) {
613 ret = PTR_ERR(dwc->usb3_phy);
614 if (ret == -ENXIO || ret == -ENODEV) {
615 dwc->usb3_phy = NULL;
616 } else if (ret == -EPROBE_DEFER) {
619 dev_err(dev, "no usb3 phy configured\n");
624 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
625 if (IS_ERR(dwc->usb2_generic_phy)) {
626 ret = PTR_ERR(dwc->usb2_generic_phy);
627 if (ret == -ENOSYS || ret == -ENODEV) {
628 dwc->usb2_generic_phy = NULL;
629 } else if (ret == -EPROBE_DEFER) {
632 dev_err(dev, "no usb2 phy configured\n");
637 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
638 if (IS_ERR(dwc->usb3_generic_phy)) {
639 ret = PTR_ERR(dwc->usb3_generic_phy);
640 if (ret == -ENOSYS || ret == -ENODEV) {
641 dwc->usb3_generic_phy = NULL;
642 } else if (ret == -EPROBE_DEFER) {
645 dev_err(dev, "no usb3 phy configured\n");
653 static int dwc3_core_init_mode(struct dwc3 *dwc)
655 struct device *dev = dwc->dev;
658 switch (dwc->dr_mode) {
659 case USB_DR_MODE_PERIPHERAL:
660 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
661 ret = dwc3_gadget_init(dwc);
663 dev_err(dev, "failed to initialize gadget\n");
667 case USB_DR_MODE_HOST:
668 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
669 ret = dwc3_host_init(dwc);
671 dev_err(dev, "failed to initialize host\n");
675 case USB_DR_MODE_OTG:
676 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
677 ret = dwc3_host_init(dwc);
679 dev_err(dev, "failed to initialize host\n");
683 ret = dwc3_gadget_init(dwc);
685 dev_err(dev, "failed to initialize gadget\n");
690 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
697 static void dwc3_core_exit_mode(struct dwc3 *dwc)
699 switch (dwc->dr_mode) {
700 case USB_DR_MODE_PERIPHERAL:
701 dwc3_gadget_exit(dwc);
703 case USB_DR_MODE_HOST:
706 case USB_DR_MODE_OTG:
708 dwc3_gadget_exit(dwc);
716 #define DWC3_ALIGN_MASK (16 - 1)
718 static int dwc3_probe(struct platform_device *pdev)
720 struct device *dev = &pdev->dev;
721 struct dwc3_platform_data *pdata = dev_get_platdata(dev);
722 struct device_node *node = dev->of_node;
723 struct resource *res;
725 u8 lpm_nyet_threshold;
734 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
738 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
742 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
744 dev_err(dev, "missing IRQ\n");
747 dwc->xhci_resources[1].start = res->start;
748 dwc->xhci_resources[1].end = res->end;
749 dwc->xhci_resources[1].flags = res->flags;
750 dwc->xhci_resources[1].name = res->name;
752 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
754 dev_err(dev, "missing memory resource\n");
758 dwc->xhci_resources[0].start = res->start;
759 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
761 dwc->xhci_resources[0].flags = res->flags;
762 dwc->xhci_resources[0].name = res->name;
764 res->start += DWC3_GLOBALS_REGS_START;
767 * Request memory region but exclude xHCI regs,
768 * since it will be requested by the xhci-plat driver.
770 regs = devm_ioremap_resource(dev, res);
772 return PTR_ERR(regs);
775 dwc->regs_size = resource_size(res);
777 * restore res->start back to its original value so that,
778 * in case the probe is deferred, we don't end up getting error in
779 * request the memory region the next time probe is called.
781 res->start -= DWC3_GLOBALS_REGS_START;
783 /* default to highest possible threshold */
784 lpm_nyet_threshold = 0xff;
786 /* default to -3.5dB de-emphasis */
790 * default to assert utmi_sleep_n and use maximum allowed HIRD
791 * threshold value of 0b1100
796 dwc->maximum_speed = of_usb_get_maximum_speed(node);
797 dwc->has_lpm_erratum = of_property_read_bool(node,
798 "snps,has-lpm-erratum");
799 of_property_read_u8(node, "snps,lpm-nyet-threshold",
800 &lpm_nyet_threshold);
801 dwc->is_utmi_l1_suspend = of_property_read_bool(node,
802 "snps,is-utmi-l1-suspend");
803 of_property_read_u8(node, "snps,hird-threshold",
806 dwc->needs_fifo_resize = of_property_read_bool(node,
808 dwc->dr_mode = of_usb_get_dr_mode(node);
810 dwc->disable_scramble_quirk = of_property_read_bool(node,
811 "snps,disable_scramble_quirk");
812 dwc->u2exit_lfps_quirk = of_property_read_bool(node,
813 "snps,u2exit_lfps_quirk");
814 dwc->u2ss_inp3_quirk = of_property_read_bool(node,
815 "snps,u2ss_inp3_quirk");
816 dwc->req_p1p2p3_quirk = of_property_read_bool(node,
817 "snps,req_p1p2p3_quirk");
818 dwc->del_p1p2p3_quirk = of_property_read_bool(node,
819 "snps,del_p1p2p3_quirk");
820 dwc->del_phy_power_chg_quirk = of_property_read_bool(node,
821 "snps,del_phy_power_chg_quirk");
822 dwc->lfps_filter_quirk = of_property_read_bool(node,
823 "snps,lfps_filter_quirk");
824 dwc->rx_detect_poll_quirk = of_property_read_bool(node,
825 "snps,rx_detect_poll_quirk");
826 dwc->dis_u3_susphy_quirk = of_property_read_bool(node,
827 "snps,dis_u3_susphy_quirk");
828 dwc->dis_u2_susphy_quirk = of_property_read_bool(node,
829 "snps,dis_u2_susphy_quirk");
831 dwc->tx_de_emphasis_quirk = of_property_read_bool(node,
832 "snps,tx_de_emphasis_quirk");
833 of_property_read_u8(node, "snps,tx_de_emphasis",
836 dwc->maximum_speed = pdata->maximum_speed;
837 dwc->has_lpm_erratum = pdata->has_lpm_erratum;
838 if (pdata->lpm_nyet_threshold)
839 lpm_nyet_threshold = pdata->lpm_nyet_threshold;
840 dwc->is_utmi_l1_suspend = pdata->is_utmi_l1_suspend;
841 if (pdata->hird_threshold)
842 hird_threshold = pdata->hird_threshold;
844 dwc->needs_fifo_resize = pdata->tx_fifo_resize;
845 dwc->dr_mode = pdata->dr_mode;
847 dwc->disable_scramble_quirk = pdata->disable_scramble_quirk;
848 dwc->u2exit_lfps_quirk = pdata->u2exit_lfps_quirk;
849 dwc->u2ss_inp3_quirk = pdata->u2ss_inp3_quirk;
850 dwc->req_p1p2p3_quirk = pdata->req_p1p2p3_quirk;
851 dwc->del_p1p2p3_quirk = pdata->del_p1p2p3_quirk;
852 dwc->del_phy_power_chg_quirk = pdata->del_phy_power_chg_quirk;
853 dwc->lfps_filter_quirk = pdata->lfps_filter_quirk;
854 dwc->rx_detect_poll_quirk = pdata->rx_detect_poll_quirk;
855 dwc->dis_u3_susphy_quirk = pdata->dis_u3_susphy_quirk;
856 dwc->dis_u2_susphy_quirk = pdata->dis_u2_susphy_quirk;
858 dwc->tx_de_emphasis_quirk = pdata->tx_de_emphasis_quirk;
859 if (pdata->tx_de_emphasis)
860 tx_de_emphasis = pdata->tx_de_emphasis;
863 /* default to superspeed if no maximum_speed passed */
864 if (dwc->maximum_speed == USB_SPEED_UNKNOWN)
865 dwc->maximum_speed = USB_SPEED_SUPER;
867 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
868 dwc->tx_de_emphasis = tx_de_emphasis;
870 dwc->hird_threshold = hird_threshold
871 | (dwc->is_utmi_l1_suspend << 4);
873 ret = dwc3_core_get_phy(dwc);
877 spin_lock_init(&dwc->lock);
878 platform_set_drvdata(pdev, dwc);
880 if (!dev->dma_mask) {
881 dev->dma_mask = dev->parent->dma_mask;
882 dev->dma_parms = dev->parent->dma_parms;
883 dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
886 dwc3_cache_hwparams(dwc);
888 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
890 dev_err(dwc->dev, "failed to allocate event buffers\n");
895 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
896 dwc->dr_mode = USB_DR_MODE_HOST;
897 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
898 dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
900 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
901 dwc->dr_mode = USB_DR_MODE_OTG;
903 ret = dwc3_core_init(dwc);
905 dev_err(dev, "failed to initialize core\n");
909 usb_phy_set_suspend(dwc->usb2_phy, 0);
910 usb_phy_set_suspend(dwc->usb3_phy, 0);
911 ret = phy_power_on(dwc->usb2_generic_phy);
915 ret = phy_power_on(dwc->usb3_generic_phy);
917 goto err_usb2phy_power;
919 ret = dwc3_event_buffers_setup(dwc);
921 dev_err(dwc->dev, "failed to setup event buffers\n");
922 goto err_usb3phy_power;
925 ret = dwc3_core_init_mode(dwc);
929 ret = dwc3_debugfs_init(dwc);
931 dev_err(dev, "failed to initialize debugfs\n");
938 dwc3_core_exit_mode(dwc);
941 dwc3_event_buffers_cleanup(dwc);
944 phy_power_off(dwc->usb3_generic_phy);
947 phy_power_off(dwc->usb2_generic_phy);
950 usb_phy_set_suspend(dwc->usb2_phy, 1);
951 usb_phy_set_suspend(dwc->usb3_phy, 1);
955 dwc3_free_event_buffers(dwc);
960 static int dwc3_remove(struct platform_device *pdev)
962 struct dwc3 *dwc = platform_get_drvdata(pdev);
964 dwc3_debugfs_exit(dwc);
965 dwc3_core_exit_mode(dwc);
966 dwc3_event_buffers_cleanup(dwc);
967 dwc3_free_event_buffers(dwc);
969 usb_phy_set_suspend(dwc->usb2_phy, 1);
970 usb_phy_set_suspend(dwc->usb3_phy, 1);
971 phy_power_off(dwc->usb2_generic_phy);
972 phy_power_off(dwc->usb3_generic_phy);
980 static const struct of_device_id of_dwc3_match[] = {
982 .compatible = "snps,dwc3"
985 .compatible = "synopsys,dwc3"
989 MODULE_DEVICE_TABLE(of, of_dwc3_match);
994 #define ACPI_ID_INTEL_BSW "808622B7"
996 static const struct acpi_device_id dwc3_acpi_match[] = {
997 { ACPI_ID_INTEL_BSW, 0 },
1000 MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
1003 static struct platform_driver dwc3_driver = {
1004 .probe = dwc3_probe,
1005 .remove = dwc3_remove,
1008 .of_match_table = of_match_ptr(of_dwc3_match),
1009 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
1013 module_platform_driver(dwc3_driver);
1015 MODULE_ALIAS("platform:dwc3");
1016 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1017 MODULE_LICENSE("GPL v2");
1018 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");