Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / usb / dwc3 / core.c
blobf1d838a4acd61532823883a0debe63b630bcec8e
1 // SPDX-License-Identifier: GPL-2.0
2 /**
3 * core.c - DesignWare USB3 DRD Controller Core file
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 */
11 #include <linux/version.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/interrupt.h>
19 #include <linux/ioport.h>
20 #include <linux/io.h>
21 #include <linux/list.h>
22 #include <linux/delay.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/of.h>
25 #include <linux/acpi.h>
26 #include <linux/pinctrl/consumer.h>
28 #include <linux/usb/ch9.h>
29 #include <linux/usb/gadget.h>
30 #include <linux/usb/of.h>
31 #include <linux/usb/otg.h>
33 #include "core.h"
34 #include "gadget.h"
35 #include "io.h"
37 #include "debug.h"
39 #define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
41 /**
42 * dwc3_get_dr_mode - Validates and sets dr_mode
43 * @dwc: pointer to our context structure
45 static int dwc3_get_dr_mode(struct dwc3 *dwc)
47 enum usb_dr_mode mode;
48 struct device *dev = dwc->dev;
49 unsigned int hw_mode;
51 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
52 dwc->dr_mode = USB_DR_MODE_OTG;
54 mode = dwc->dr_mode;
55 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
57 switch (hw_mode) {
58 case DWC3_GHWPARAMS0_MODE_GADGET:
59 if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) {
60 dev_err(dev,
61 "Controller does not support host mode.\n");
62 return -EINVAL;
64 mode = USB_DR_MODE_PERIPHERAL;
65 break;
66 case DWC3_GHWPARAMS0_MODE_HOST:
67 if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
68 dev_err(dev,
69 "Controller does not support device mode.\n");
70 return -EINVAL;
72 mode = USB_DR_MODE_HOST;
73 break;
74 default:
75 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
76 mode = USB_DR_MODE_HOST;
77 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
78 mode = USB_DR_MODE_PERIPHERAL;
81 if (mode != dwc->dr_mode) {
82 dev_warn(dev,
83 "Configuration mismatch. dr_mode forced to %s\n",
84 mode == USB_DR_MODE_HOST ? "host" : "gadget");
86 dwc->dr_mode = mode;
89 return 0;
92 static void dwc3_event_buffers_cleanup(struct dwc3 *dwc);
93 static int dwc3_event_buffers_setup(struct dwc3 *dwc);
95 static void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
97 u32 reg;
99 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
100 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
101 reg |= DWC3_GCTL_PRTCAPDIR(mode);
102 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
104 dwc->current_dr_role = mode;
107 static void __dwc3_set_mode(struct work_struct *work)
109 struct dwc3 *dwc = work_to_dwc(work);
110 unsigned long flags;
111 int ret;
113 if (!dwc->desired_dr_role)
114 return;
116 if (dwc->desired_dr_role == dwc->current_dr_role)
117 return;
119 if (dwc->dr_mode != USB_DR_MODE_OTG)
120 return;
122 switch (dwc->current_dr_role) {
123 case DWC3_GCTL_PRTCAP_HOST:
124 dwc3_host_exit(dwc);
125 break;
126 case DWC3_GCTL_PRTCAP_DEVICE:
127 dwc3_gadget_exit(dwc);
128 dwc3_event_buffers_cleanup(dwc);
129 break;
130 default:
131 break;
134 spin_lock_irqsave(&dwc->lock, flags);
136 dwc3_set_prtcap(dwc, dwc->desired_dr_role);
138 spin_unlock_irqrestore(&dwc->lock, flags);
140 switch (dwc->desired_dr_role) {
141 case DWC3_GCTL_PRTCAP_HOST:
142 ret = dwc3_host_init(dwc);
143 if (ret) {
144 dev_err(dwc->dev, "failed to initialize host\n");
145 } else {
146 if (dwc->usb2_phy)
147 otg_set_vbus(dwc->usb2_phy->otg, true);
148 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
149 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
150 phy_calibrate(dwc->usb2_generic_phy);
152 break;
153 case DWC3_GCTL_PRTCAP_DEVICE:
154 dwc3_event_buffers_setup(dwc);
156 if (dwc->usb2_phy)
157 otg_set_vbus(dwc->usb2_phy->otg, false);
158 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
159 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
161 ret = dwc3_gadget_init(dwc);
162 if (ret)
163 dev_err(dwc->dev, "failed to initialize peripheral\n");
164 break;
165 default:
166 break;
170 void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
172 unsigned long flags;
174 spin_lock_irqsave(&dwc->lock, flags);
175 dwc->desired_dr_role = mode;
176 spin_unlock_irqrestore(&dwc->lock, flags);
178 queue_work(system_power_efficient_wq, &dwc->drd_work);
181 u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
183 struct dwc3 *dwc = dep->dwc;
184 u32 reg;
186 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
187 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
188 DWC3_GDBGFIFOSPACE_TYPE(type));
190 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
192 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
196 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
197 * @dwc: pointer to our context structure
199 static int dwc3_core_soft_reset(struct dwc3 *dwc)
201 u32 reg;
202 int retries = 1000;
203 int ret;
205 usb_phy_init(dwc->usb2_phy);
206 usb_phy_init(dwc->usb3_phy);
207 ret = phy_init(dwc->usb2_generic_phy);
208 if (ret < 0)
209 return ret;
211 ret = phy_init(dwc->usb3_generic_phy);
212 if (ret < 0) {
213 phy_exit(dwc->usb2_generic_phy);
214 return ret;
218 * We're resetting only the device side because, if we're in host mode,
219 * XHCI driver will reset the host block. If dwc3 was configured for
220 * host-only mode, then we can return early.
222 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST)
223 return 0;
225 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
226 reg |= DWC3_DCTL_CSFTRST;
227 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
229 do {
230 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
231 if (!(reg & DWC3_DCTL_CSFTRST))
232 return 0;
234 udelay(1);
235 } while (--retries);
237 phy_exit(dwc->usb3_generic_phy);
238 phy_exit(dwc->usb2_generic_phy);
240 return -ETIMEDOUT;
244 * dwc3_frame_length_adjustment - Adjusts frame length if required
245 * @dwc3: Pointer to our controller context structure
247 static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
249 u32 reg;
250 u32 dft;
252 if (dwc->revision < DWC3_REVISION_250A)
253 return;
255 if (dwc->fladj == 0)
256 return;
258 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
259 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
260 if (!dev_WARN_ONCE(dwc->dev, dft == dwc->fladj,
261 "request value same as default, ignoring\n")) {
262 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
263 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
264 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
269 * dwc3_free_one_event_buffer - Frees one event buffer
270 * @dwc: Pointer to our controller context structure
271 * @evt: Pointer to event buffer to be freed
273 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
274 struct dwc3_event_buffer *evt)
276 dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
280 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
281 * @dwc: Pointer to our controller context structure
282 * @length: size of the event buffer
284 * Returns a pointer to the allocated event buffer structure on success
285 * otherwise ERR_PTR(errno).
287 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
288 unsigned length)
290 struct dwc3_event_buffer *evt;
292 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
293 if (!evt)
294 return ERR_PTR(-ENOMEM);
296 evt->dwc = dwc;
297 evt->length = length;
298 evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
299 if (!evt->cache)
300 return ERR_PTR(-ENOMEM);
302 evt->buf = dma_alloc_coherent(dwc->sysdev, length,
303 &evt->dma, GFP_KERNEL);
304 if (!evt->buf)
305 return ERR_PTR(-ENOMEM);
307 return evt;
311 * dwc3_free_event_buffers - frees all allocated event buffers
312 * @dwc: Pointer to our controller context structure
314 static void dwc3_free_event_buffers(struct dwc3 *dwc)
316 struct dwc3_event_buffer *evt;
318 evt = dwc->ev_buf;
319 if (evt)
320 dwc3_free_one_event_buffer(dwc, evt);
324 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
325 * @dwc: pointer to our controller context structure
326 * @length: size of event buffer
328 * Returns 0 on success otherwise negative errno. In the error case, dwc
329 * may contain some buffers allocated but not all which were requested.
331 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
333 struct dwc3_event_buffer *evt;
335 evt = dwc3_alloc_one_event_buffer(dwc, length);
336 if (IS_ERR(evt)) {
337 dev_err(dwc->dev, "can't allocate event buffer\n");
338 return PTR_ERR(evt);
340 dwc->ev_buf = evt;
342 return 0;
346 * dwc3_event_buffers_setup - setup our allocated event buffers
347 * @dwc: pointer to our controller context structure
349 * Returns 0 on success otherwise negative errno.
351 static int dwc3_event_buffers_setup(struct dwc3 *dwc)
353 struct dwc3_event_buffer *evt;
355 evt = dwc->ev_buf;
356 evt->lpos = 0;
357 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
358 lower_32_bits(evt->dma));
359 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
360 upper_32_bits(evt->dma));
361 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
362 DWC3_GEVNTSIZ_SIZE(evt->length));
363 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
365 return 0;
368 static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
370 struct dwc3_event_buffer *evt;
372 evt = dwc->ev_buf;
374 evt->lpos = 0;
376 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
377 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
378 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
379 | DWC3_GEVNTSIZ_SIZE(0));
380 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
383 static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
385 if (!dwc->has_hibernation)
386 return 0;
388 if (!dwc->nr_scratch)
389 return 0;
391 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
392 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
393 if (!dwc->scratchbuf)
394 return -ENOMEM;
396 return 0;
399 static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
401 dma_addr_t scratch_addr;
402 u32 param;
403 int ret;
405 if (!dwc->has_hibernation)
406 return 0;
408 if (!dwc->nr_scratch)
409 return 0;
411 /* should never fall here */
412 if (!WARN_ON(dwc->scratchbuf))
413 return 0;
415 scratch_addr = dma_map_single(dwc->sysdev, dwc->scratchbuf,
416 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
417 DMA_BIDIRECTIONAL);
418 if (dma_mapping_error(dwc->sysdev, scratch_addr)) {
419 dev_err(dwc->sysdev, "failed to map scratch buffer\n");
420 ret = -EFAULT;
421 goto err0;
424 dwc->scratch_addr = scratch_addr;
426 param = lower_32_bits(scratch_addr);
428 ret = dwc3_send_gadget_generic_command(dwc,
429 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
430 if (ret < 0)
431 goto err1;
433 param = upper_32_bits(scratch_addr);
435 ret = dwc3_send_gadget_generic_command(dwc,
436 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
437 if (ret < 0)
438 goto err1;
440 return 0;
442 err1:
443 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
444 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
446 err0:
447 return ret;
450 static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
452 if (!dwc->has_hibernation)
453 return;
455 if (!dwc->nr_scratch)
456 return;
458 /* should never fall here */
459 if (!WARN_ON(dwc->scratchbuf))
460 return;
462 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
463 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
464 kfree(dwc->scratchbuf);
467 static void dwc3_core_num_eps(struct dwc3 *dwc)
469 struct dwc3_hwparams *parms = &dwc->hwparams;
471 dwc->num_eps = DWC3_NUM_EPS(parms);
474 static void dwc3_cache_hwparams(struct dwc3 *dwc)
476 struct dwc3_hwparams *parms = &dwc->hwparams;
478 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
479 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
480 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
481 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
482 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
483 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
484 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
485 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
486 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
489 static int dwc3_core_ulpi_init(struct dwc3 *dwc)
491 int intf;
492 int ret = 0;
494 intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
496 if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
497 (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
498 dwc->hsphy_interface &&
499 !strncmp(dwc->hsphy_interface, "ulpi", 4)))
500 ret = dwc3_ulpi_init(dwc);
502 return ret;
506 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
507 * @dwc: Pointer to our controller context structure
509 * Returns 0 on success. The USB PHY interfaces are configured but not
510 * initialized. The PHY interfaces and the PHYs get initialized together with
511 * the core in dwc3_core_init.
513 static int dwc3_phy_setup(struct dwc3 *dwc)
515 u32 reg;
517 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
520 * Make sure UX_EXIT_PX is cleared as that causes issues with some
521 * PHYs. Also, this bit is not supposed to be used in normal operation.
523 reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
526 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
527 * to '0' during coreConsultant configuration. So default value
528 * will be '0' when the core is reset. Application needs to set it
529 * to '1' after the core initialization is completed.
531 if (dwc->revision > DWC3_REVISION_194A)
532 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
534 if (dwc->u2ss_inp3_quirk)
535 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
537 if (dwc->dis_rxdet_inp3_quirk)
538 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
540 if (dwc->req_p1p2p3_quirk)
541 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
543 if (dwc->del_p1p2p3_quirk)
544 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
546 if (dwc->del_phy_power_chg_quirk)
547 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
549 if (dwc->lfps_filter_quirk)
550 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
552 if (dwc->rx_detect_poll_quirk)
553 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
555 if (dwc->tx_de_emphasis_quirk)
556 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
558 if (dwc->dis_u3_susphy_quirk)
559 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
561 if (dwc->dis_del_phy_power_chg_quirk)
562 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
564 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
566 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
568 /* Select the HS PHY interface */
569 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
570 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
571 if (dwc->hsphy_interface &&
572 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
573 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
574 break;
575 } else if (dwc->hsphy_interface &&
576 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
577 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
578 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
579 } else {
580 /* Relying on default value. */
581 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
582 break;
584 /* FALLTHROUGH */
585 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
586 /* FALLTHROUGH */
587 default:
588 break;
591 switch (dwc->hsphy_mode) {
592 case USBPHY_INTERFACE_MODE_UTMI:
593 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
594 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
595 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
596 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
597 break;
598 case USBPHY_INTERFACE_MODE_UTMIW:
599 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
600 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
601 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
602 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
603 break;
604 default:
605 break;
609 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
610 * '0' during coreConsultant configuration. So default value will
611 * be '0' when the core is reset. Application needs to set it to
612 * '1' after the core initialization is completed.
614 if (dwc->revision > DWC3_REVISION_194A)
615 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
617 if (dwc->dis_u2_susphy_quirk)
618 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
620 if (dwc->dis_enblslpm_quirk)
621 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
623 if (dwc->dis_u2_freeclk_exists_quirk)
624 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
626 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
628 return 0;
631 static void dwc3_core_exit(struct dwc3 *dwc)
633 dwc3_event_buffers_cleanup(dwc);
635 usb_phy_shutdown(dwc->usb2_phy);
636 usb_phy_shutdown(dwc->usb3_phy);
637 phy_exit(dwc->usb2_generic_phy);
638 phy_exit(dwc->usb3_generic_phy);
640 usb_phy_set_suspend(dwc->usb2_phy, 1);
641 usb_phy_set_suspend(dwc->usb3_phy, 1);
642 phy_power_off(dwc->usb2_generic_phy);
643 phy_power_off(dwc->usb3_generic_phy);
646 static bool dwc3_core_is_valid(struct dwc3 *dwc)
648 u32 reg;
650 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
652 /* This should read as U3 followed by revision number */
653 if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
654 /* Detected DWC_usb3 IP */
655 dwc->revision = reg;
656 } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
657 /* Detected DWC_usb31 IP */
658 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
659 dwc->revision |= DWC3_REVISION_IS_DWC31;
660 } else {
661 return false;
664 return true;
667 static void dwc3_core_setup_global_control(struct dwc3 *dwc)
669 u32 hwparams4 = dwc->hwparams.hwparams4;
670 u32 reg;
672 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
673 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
675 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
676 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
678 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
679 * issue which would cause xHCI compliance tests to fail.
681 * Because of that we cannot enable clock gating on such
682 * configurations.
684 * Refers to:
686 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
687 * SOF/ITP Mode Used
689 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
690 dwc->dr_mode == USB_DR_MODE_OTG) &&
691 (dwc->revision >= DWC3_REVISION_210A &&
692 dwc->revision <= DWC3_REVISION_250A))
693 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
694 else
695 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
696 break;
697 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
698 /* enable hibernation here */
699 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
702 * REVISIT Enabling this bit so that host-mode hibernation
703 * will work. Device-mode hibernation is not yet implemented.
705 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
706 break;
707 default:
708 /* nothing */
709 break;
712 /* check if current dwc3 is on simulation board */
713 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
714 dev_info(dwc->dev, "Running with FPGA optmizations\n");
715 dwc->is_fpga = true;
718 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
719 "disable_scramble cannot be used on non-FPGA builds\n");
721 if (dwc->disable_scramble_quirk && dwc->is_fpga)
722 reg |= DWC3_GCTL_DISSCRAMBLE;
723 else
724 reg &= ~DWC3_GCTL_DISSCRAMBLE;
726 if (dwc->u2exit_lfps_quirk)
727 reg |= DWC3_GCTL_U2EXIT_LFPS;
730 * WORKAROUND: DWC3 revisions <1.90a have a bug
731 * where the device can fail to connect at SuperSpeed
732 * and falls back to high-speed mode which causes
733 * the device to enter a Connect/Disconnect loop
735 if (dwc->revision < DWC3_REVISION_190A)
736 reg |= DWC3_GCTL_U2RSTECN;
738 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
741 static int dwc3_core_get_phy(struct dwc3 *dwc);
742 static int dwc3_core_ulpi_init(struct dwc3 *dwc);
745 * dwc3_core_init - Low-level initialization of DWC3 Core
746 * @dwc: Pointer to our controller context structure
748 * Returns 0 on success otherwise negative errno.
750 static int dwc3_core_init(struct dwc3 *dwc)
752 u32 reg;
753 int ret;
755 if (!dwc3_core_is_valid(dwc)) {
756 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
757 ret = -ENODEV;
758 goto err0;
762 * Write Linux Version Code to our GUID register so it's easy to figure
763 * out which kernel version a bug was found.
765 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
767 /* Handle USB2.0-only core configuration */
768 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
769 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
770 if (dwc->maximum_speed == USB_SPEED_SUPER)
771 dwc->maximum_speed = USB_SPEED_HIGH;
774 ret = dwc3_phy_setup(dwc);
775 if (ret)
776 goto err0;
778 if (!dwc->ulpi_ready) {
779 ret = dwc3_core_ulpi_init(dwc);
780 if (ret)
781 goto err0;
782 dwc->ulpi_ready = true;
785 if (!dwc->phys_ready) {
786 ret = dwc3_core_get_phy(dwc);
787 if (ret)
788 goto err0a;
789 dwc->phys_ready = true;
792 ret = dwc3_core_soft_reset(dwc);
793 if (ret)
794 goto err0a;
796 dwc3_core_setup_global_control(dwc);
797 dwc3_core_num_eps(dwc);
799 ret = dwc3_setup_scratch_buffers(dwc);
800 if (ret)
801 goto err1;
803 /* Adjust Frame Length */
804 dwc3_frame_length_adjustment(dwc);
806 usb_phy_set_suspend(dwc->usb2_phy, 0);
807 usb_phy_set_suspend(dwc->usb3_phy, 0);
808 ret = phy_power_on(dwc->usb2_generic_phy);
809 if (ret < 0)
810 goto err2;
812 ret = phy_power_on(dwc->usb3_generic_phy);
813 if (ret < 0)
814 goto err3;
816 ret = dwc3_event_buffers_setup(dwc);
817 if (ret) {
818 dev_err(dwc->dev, "failed to setup event buffers\n");
819 goto err4;
823 * ENDXFER polling is available on version 3.10a and later of
824 * the DWC_usb3 controller. It is NOT available in the
825 * DWC_usb31 controller.
827 if (!dwc3_is_usb31(dwc) && dwc->revision >= DWC3_REVISION_310A) {
828 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
829 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
830 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
833 if (dwc->revision >= DWC3_REVISION_250A) {
834 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
837 * Enable hardware control of sending remote wakeup
838 * in HS when the device is in the L1 state.
840 if (dwc->revision >= DWC3_REVISION_290A)
841 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
843 if (dwc->dis_tx_ipgap_linecheck_quirk)
844 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
846 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
849 return 0;
851 err4:
852 phy_power_off(dwc->usb3_generic_phy);
854 err3:
855 phy_power_off(dwc->usb2_generic_phy);
857 err2:
858 usb_phy_set_suspend(dwc->usb2_phy, 1);
859 usb_phy_set_suspend(dwc->usb3_phy, 1);
861 err1:
862 usb_phy_shutdown(dwc->usb2_phy);
863 usb_phy_shutdown(dwc->usb3_phy);
864 phy_exit(dwc->usb2_generic_phy);
865 phy_exit(dwc->usb3_generic_phy);
867 err0a:
868 dwc3_ulpi_exit(dwc);
870 err0:
871 return ret;
874 static int dwc3_core_get_phy(struct dwc3 *dwc)
876 struct device *dev = dwc->dev;
877 struct device_node *node = dev->of_node;
878 int ret;
880 if (node) {
881 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
882 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
883 } else {
884 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
885 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
888 if (IS_ERR(dwc->usb2_phy)) {
889 ret = PTR_ERR(dwc->usb2_phy);
890 if (ret == -ENXIO || ret == -ENODEV) {
891 dwc->usb2_phy = NULL;
892 } else if (ret == -EPROBE_DEFER) {
893 return ret;
894 } else {
895 dev_err(dev, "no usb2 phy configured\n");
896 return ret;
900 if (IS_ERR(dwc->usb3_phy)) {
901 ret = PTR_ERR(dwc->usb3_phy);
902 if (ret == -ENXIO || ret == -ENODEV) {
903 dwc->usb3_phy = NULL;
904 } else if (ret == -EPROBE_DEFER) {
905 return ret;
906 } else {
907 dev_err(dev, "no usb3 phy configured\n");
908 return ret;
912 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
913 if (IS_ERR(dwc->usb2_generic_phy)) {
914 ret = PTR_ERR(dwc->usb2_generic_phy);
915 if (ret == -ENOSYS || ret == -ENODEV) {
916 dwc->usb2_generic_phy = NULL;
917 } else if (ret == -EPROBE_DEFER) {
918 return ret;
919 } else {
920 dev_err(dev, "no usb2 phy configured\n");
921 return ret;
925 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
926 if (IS_ERR(dwc->usb3_generic_phy)) {
927 ret = PTR_ERR(dwc->usb3_generic_phy);
928 if (ret == -ENOSYS || ret == -ENODEV) {
929 dwc->usb3_generic_phy = NULL;
930 } else if (ret == -EPROBE_DEFER) {
931 return ret;
932 } else {
933 dev_err(dev, "no usb3 phy configured\n");
934 return ret;
938 return 0;
941 static int dwc3_core_init_mode(struct dwc3 *dwc)
943 struct device *dev = dwc->dev;
944 int ret;
946 switch (dwc->dr_mode) {
947 case USB_DR_MODE_PERIPHERAL:
948 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
950 if (dwc->usb2_phy)
951 otg_set_vbus(dwc->usb2_phy->otg, false);
952 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
953 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
955 ret = dwc3_gadget_init(dwc);
956 if (ret) {
957 if (ret != -EPROBE_DEFER)
958 dev_err(dev, "failed to initialize gadget\n");
959 return ret;
961 break;
962 case USB_DR_MODE_HOST:
963 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
965 if (dwc->usb2_phy)
966 otg_set_vbus(dwc->usb2_phy->otg, true);
967 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
968 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
970 ret = dwc3_host_init(dwc);
971 if (ret) {
972 if (ret != -EPROBE_DEFER)
973 dev_err(dev, "failed to initialize host\n");
974 return ret;
976 phy_calibrate(dwc->usb2_generic_phy);
977 break;
978 case USB_DR_MODE_OTG:
979 INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
980 ret = dwc3_drd_init(dwc);
981 if (ret) {
982 if (ret != -EPROBE_DEFER)
983 dev_err(dev, "failed to initialize dual-role\n");
984 return ret;
986 break;
987 default:
988 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
989 return -EINVAL;
992 return 0;
995 static void dwc3_core_exit_mode(struct dwc3 *dwc)
997 switch (dwc->dr_mode) {
998 case USB_DR_MODE_PERIPHERAL:
999 dwc3_gadget_exit(dwc);
1000 break;
1001 case USB_DR_MODE_HOST:
1002 dwc3_host_exit(dwc);
1003 break;
1004 case USB_DR_MODE_OTG:
1005 dwc3_drd_exit(dwc);
1006 break;
1007 default:
1008 /* do nothing */
1009 break;
1013 static void dwc3_get_properties(struct dwc3 *dwc)
1015 struct device *dev = dwc->dev;
1016 u8 lpm_nyet_threshold;
1017 u8 tx_de_emphasis;
1018 u8 hird_threshold;
1020 /* default to highest possible threshold */
1021 lpm_nyet_threshold = 0xff;
1023 /* default to -3.5dB de-emphasis */
1024 tx_de_emphasis = 1;
1027 * default to assert utmi_sleep_n and use maximum allowed HIRD
1028 * threshold value of 0b1100
1030 hird_threshold = 12;
1032 dwc->maximum_speed = usb_get_maximum_speed(dev);
1033 dwc->dr_mode = usb_get_dr_mode(dev);
1034 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
1036 dwc->sysdev_is_parent = device_property_read_bool(dev,
1037 "linux,sysdev_is_parent");
1038 if (dwc->sysdev_is_parent)
1039 dwc->sysdev = dwc->dev->parent;
1040 else
1041 dwc->sysdev = dwc->dev;
1043 dwc->has_lpm_erratum = device_property_read_bool(dev,
1044 "snps,has-lpm-erratum");
1045 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
1046 &lpm_nyet_threshold);
1047 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
1048 "snps,is-utmi-l1-suspend");
1049 device_property_read_u8(dev, "snps,hird-threshold",
1050 &hird_threshold);
1051 dwc->usb3_lpm_capable = device_property_read_bool(dev,
1052 "snps,usb3_lpm_capable");
1054 dwc->disable_scramble_quirk = device_property_read_bool(dev,
1055 "snps,disable_scramble_quirk");
1056 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
1057 "snps,u2exit_lfps_quirk");
1058 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
1059 "snps,u2ss_inp3_quirk");
1060 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
1061 "snps,req_p1p2p3_quirk");
1062 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
1063 "snps,del_p1p2p3_quirk");
1064 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
1065 "snps,del_phy_power_chg_quirk");
1066 dwc->lfps_filter_quirk = device_property_read_bool(dev,
1067 "snps,lfps_filter_quirk");
1068 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
1069 "snps,rx_detect_poll_quirk");
1070 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
1071 "snps,dis_u3_susphy_quirk");
1072 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
1073 "snps,dis_u2_susphy_quirk");
1074 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1075 "snps,dis_enblslpm_quirk");
1076 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1077 "snps,dis_rxdet_inp3_quirk");
1078 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1079 "snps,dis-u2-freeclk-exists-quirk");
1080 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1081 "snps,dis-del-phy-power-chg-quirk");
1082 dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1083 "snps,dis-tx-ipgap-linecheck-quirk");
1085 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
1086 "snps,tx_de_emphasis_quirk");
1087 device_property_read_u8(dev, "snps,tx_de_emphasis",
1088 &tx_de_emphasis);
1089 device_property_read_string(dev, "snps,hsphy_interface",
1090 &dwc->hsphy_interface);
1091 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
1092 &dwc->fladj);
1094 dwc->dis_metastability_quirk = device_property_read_bool(dev,
1095 "snps,dis_metastability_quirk");
1097 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1098 dwc->tx_de_emphasis = tx_de_emphasis;
1100 dwc->hird_threshold = hird_threshold
1101 | (dwc->is_utmi_l1_suspend << 4);
1103 dwc->imod_interval = 0;
1106 /* check whether the core supports IMOD */
1107 bool dwc3_has_imod(struct dwc3 *dwc)
1109 return ((dwc3_is_usb3(dwc) &&
1110 dwc->revision >= DWC3_REVISION_300A) ||
1111 (dwc3_is_usb31(dwc) &&
1112 dwc->revision >= DWC3_USB31_REVISION_120A));
1115 static void dwc3_check_params(struct dwc3 *dwc)
1117 struct device *dev = dwc->dev;
1119 /* Check for proper value of imod_interval */
1120 if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
1121 dev_warn(dwc->dev, "Interrupt moderation not supported\n");
1122 dwc->imod_interval = 0;
1126 * Workaround for STAR 9000961433 which affects only version
1127 * 3.00a of the DWC_usb3 core. This prevents the controller
1128 * interrupt from being masked while handling events. IMOD
1129 * allows us to work around this issue. Enable it for the
1130 * affected version.
1132 if (!dwc->imod_interval &&
1133 (dwc->revision == DWC3_REVISION_300A))
1134 dwc->imod_interval = 1;
1136 /* Check the maximum_speed parameter */
1137 switch (dwc->maximum_speed) {
1138 case USB_SPEED_LOW:
1139 case USB_SPEED_FULL:
1140 case USB_SPEED_HIGH:
1141 case USB_SPEED_SUPER:
1142 case USB_SPEED_SUPER_PLUS:
1143 break;
1144 default:
1145 dev_err(dev, "invalid maximum_speed parameter %d\n",
1146 dwc->maximum_speed);
1147 /* fall through */
1148 case USB_SPEED_UNKNOWN:
1149 /* default to superspeed */
1150 dwc->maximum_speed = USB_SPEED_SUPER;
1153 * default to superspeed plus if we are capable.
1155 if (dwc3_is_usb31(dwc) &&
1156 (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
1157 DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1158 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1160 break;
1164 static int dwc3_probe(struct platform_device *pdev)
1166 struct device *dev = &pdev->dev;
1167 struct resource *res;
1168 struct dwc3 *dwc;
1170 int ret;
1172 void __iomem *regs;
1174 dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
1175 if (!dwc)
1176 return -ENOMEM;
1178 dwc->dev = dev;
1180 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1181 if (!res) {
1182 dev_err(dev, "missing memory resource\n");
1183 return -ENODEV;
1186 dwc->xhci_resources[0].start = res->start;
1187 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1188 DWC3_XHCI_REGS_END;
1189 dwc->xhci_resources[0].flags = res->flags;
1190 dwc->xhci_resources[0].name = res->name;
1192 res->start += DWC3_GLOBALS_REGS_START;
1195 * Request memory region but exclude xHCI regs,
1196 * since it will be requested by the xhci-plat driver.
1198 regs = devm_ioremap_resource(dev, res);
1199 if (IS_ERR(regs)) {
1200 ret = PTR_ERR(regs);
1201 goto err0;
1204 dwc->regs = regs;
1205 dwc->regs_size = resource_size(res);
1207 dwc3_get_properties(dwc);
1209 platform_set_drvdata(pdev, dwc);
1210 dwc3_cache_hwparams(dwc);
1212 spin_lock_init(&dwc->lock);
1214 pm_runtime_set_active(dev);
1215 pm_runtime_use_autosuspend(dev);
1216 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
1217 pm_runtime_enable(dev);
1218 ret = pm_runtime_get_sync(dev);
1219 if (ret < 0)
1220 goto err1;
1222 pm_runtime_forbid(dev);
1224 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1225 if (ret) {
1226 dev_err(dwc->dev, "failed to allocate event buffers\n");
1227 ret = -ENOMEM;
1228 goto err2;
1231 ret = dwc3_get_dr_mode(dwc);
1232 if (ret)
1233 goto err3;
1235 ret = dwc3_alloc_scratch_buffers(dwc);
1236 if (ret)
1237 goto err3;
1239 ret = dwc3_core_init(dwc);
1240 if (ret) {
1241 dev_err(dev, "failed to initialize core\n");
1242 goto err4;
1245 dwc3_check_params(dwc);
1247 ret = dwc3_core_init_mode(dwc);
1248 if (ret)
1249 goto err5;
1251 dwc3_debugfs_init(dwc);
1252 pm_runtime_put(dev);
1254 return 0;
1256 err5:
1257 dwc3_event_buffers_cleanup(dwc);
1259 err4:
1260 dwc3_free_scratch_buffers(dwc);
1262 err3:
1263 dwc3_free_event_buffers(dwc);
1265 err2:
1266 pm_runtime_allow(&pdev->dev);
1268 err1:
1269 pm_runtime_put_sync(&pdev->dev);
1270 pm_runtime_disable(&pdev->dev);
1272 err0:
1274 * restore res->start back to its original value so that, in case the
1275 * probe is deferred, we don't end up getting error in request the
1276 * memory region the next time probe is called.
1278 res->start -= DWC3_GLOBALS_REGS_START;
1280 return ret;
1283 static int dwc3_remove(struct platform_device *pdev)
1285 struct dwc3 *dwc = platform_get_drvdata(pdev);
1286 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1288 pm_runtime_get_sync(&pdev->dev);
1290 * restore res->start back to its original value so that, in case the
1291 * probe is deferred, we don't end up getting error in request the
1292 * memory region the next time probe is called.
1294 res->start -= DWC3_GLOBALS_REGS_START;
1296 dwc3_debugfs_exit(dwc);
1297 dwc3_core_exit_mode(dwc);
1299 dwc3_core_exit(dwc);
1300 dwc3_ulpi_exit(dwc);
1302 pm_runtime_put_sync(&pdev->dev);
1303 pm_runtime_allow(&pdev->dev);
1304 pm_runtime_disable(&pdev->dev);
1306 dwc3_free_event_buffers(dwc);
1307 dwc3_free_scratch_buffers(dwc);
1309 return 0;
1312 #ifdef CONFIG_PM
1313 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
1315 unsigned long flags;
1317 switch (dwc->current_dr_role) {
1318 case DWC3_GCTL_PRTCAP_DEVICE:
1319 spin_lock_irqsave(&dwc->lock, flags);
1320 dwc3_gadget_suspend(dwc);
1321 spin_unlock_irqrestore(&dwc->lock, flags);
1322 dwc3_core_exit(dwc);
1323 break;
1324 case DWC3_GCTL_PRTCAP_HOST:
1325 /* do nothing during host runtime_suspend */
1326 if (!PMSG_IS_AUTO(msg))
1327 dwc3_core_exit(dwc);
1328 break;
1329 default:
1330 /* do nothing */
1331 break;
1334 return 0;
1337 static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
1339 unsigned long flags;
1340 int ret;
1342 switch (dwc->current_dr_role) {
1343 case DWC3_GCTL_PRTCAP_DEVICE:
1344 ret = dwc3_core_init(dwc);
1345 if (ret)
1346 return ret;
1348 spin_lock_irqsave(&dwc->lock, flags);
1349 dwc3_gadget_resume(dwc);
1350 spin_unlock_irqrestore(&dwc->lock, flags);
1351 break;
1352 case DWC3_GCTL_PRTCAP_HOST:
1353 /* nothing to do on host runtime_resume */
1354 if (!PMSG_IS_AUTO(msg)) {
1355 ret = dwc3_core_init(dwc);
1356 if (ret)
1357 return ret;
1359 break;
1360 default:
1361 /* do nothing */
1362 break;
1365 return 0;
1368 static int dwc3_runtime_checks(struct dwc3 *dwc)
1370 switch (dwc->current_dr_role) {
1371 case DWC3_GCTL_PRTCAP_DEVICE:
1372 if (dwc->connected)
1373 return -EBUSY;
1374 break;
1375 case DWC3_GCTL_PRTCAP_HOST:
1376 default:
1377 /* do nothing */
1378 break;
1381 return 0;
1384 static int dwc3_runtime_suspend(struct device *dev)
1386 struct dwc3 *dwc = dev_get_drvdata(dev);
1387 int ret;
1389 if (dwc3_runtime_checks(dwc))
1390 return -EBUSY;
1392 ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
1393 if (ret)
1394 return ret;
1396 device_init_wakeup(dev, true);
1398 return 0;
1401 static int dwc3_runtime_resume(struct device *dev)
1403 struct dwc3 *dwc = dev_get_drvdata(dev);
1404 int ret;
1406 device_init_wakeup(dev, false);
1408 ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
1409 if (ret)
1410 return ret;
1412 switch (dwc->current_dr_role) {
1413 case DWC3_GCTL_PRTCAP_DEVICE:
1414 dwc3_gadget_process_pending_events(dwc);
1415 break;
1416 case DWC3_GCTL_PRTCAP_HOST:
1417 default:
1418 /* do nothing */
1419 break;
1422 pm_runtime_mark_last_busy(dev);
1424 return 0;
1427 static int dwc3_runtime_idle(struct device *dev)
1429 struct dwc3 *dwc = dev_get_drvdata(dev);
1431 switch (dwc->current_dr_role) {
1432 case DWC3_GCTL_PRTCAP_DEVICE:
1433 if (dwc3_runtime_checks(dwc))
1434 return -EBUSY;
1435 break;
1436 case DWC3_GCTL_PRTCAP_HOST:
1437 default:
1438 /* do nothing */
1439 break;
1442 pm_runtime_mark_last_busy(dev);
1443 pm_runtime_autosuspend(dev);
1445 return 0;
1447 #endif /* CONFIG_PM */
1449 #ifdef CONFIG_PM_SLEEP
1450 static int dwc3_suspend(struct device *dev)
1452 struct dwc3 *dwc = dev_get_drvdata(dev);
1453 int ret;
1455 ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
1456 if (ret)
1457 return ret;
1459 pinctrl_pm_select_sleep_state(dev);
1461 return 0;
1464 static int dwc3_resume(struct device *dev)
1466 struct dwc3 *dwc = dev_get_drvdata(dev);
1467 int ret;
1469 pinctrl_pm_select_default_state(dev);
1471 ret = dwc3_resume_common(dwc, PMSG_RESUME);
1472 if (ret)
1473 return ret;
1475 pm_runtime_disable(dev);
1476 pm_runtime_set_active(dev);
1477 pm_runtime_enable(dev);
1479 return 0;
1481 #endif /* CONFIG_PM_SLEEP */
1483 static const struct dev_pm_ops dwc3_dev_pm_ops = {
1484 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
1485 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
1486 dwc3_runtime_idle)
1489 #ifdef CONFIG_OF
1490 static const struct of_device_id of_dwc3_match[] = {
1492 .compatible = "snps,dwc3"
1495 .compatible = "synopsys,dwc3"
1497 { },
1499 MODULE_DEVICE_TABLE(of, of_dwc3_match);
1500 #endif
1502 #ifdef CONFIG_ACPI
1504 #define ACPI_ID_INTEL_BSW "808622B7"
1506 static const struct acpi_device_id dwc3_acpi_match[] = {
1507 { ACPI_ID_INTEL_BSW, 0 },
1508 { },
1510 MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
1511 #endif
1513 static struct platform_driver dwc3_driver = {
1514 .probe = dwc3_probe,
1515 .remove = dwc3_remove,
1516 .driver = {
1517 .name = "dwc3",
1518 .of_match_table = of_match_ptr(of_dwc3_match),
1519 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
1520 .pm = &dwc3_dev_pm_ops,
1524 module_platform_driver(dwc3_driver);
1526 MODULE_ALIAS("platform:dwc3");
1527 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1528 MODULE_LICENSE("GPL v2");
1529 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");