xref: /openbmc/linux/drivers/usb/dwc3/core.c (revision 858c0840)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * core.c - DesignWare USB3 DRD Controller Core file
4  *
5  * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com
6  *
7  * Authors: Felipe Balbi <balbi@ti.com>,
8  *	    Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9  */
10 
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>
21 #include <linux/io.h>
22 #include <linux/list.h>
23 #include <linux/delay.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/of.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>
31 
32 #include <linux/usb/ch9.h>
33 #include <linux/usb/gadget.h>
34 #include <linux/usb/of.h>
35 #include <linux/usb/otg.h>
36 
37 #include "core.h"
38 #include "gadget.h"
39 #include "io.h"
40 
41 #include "debug.h"
42 
43 #define DWC3_DEFAULT_AUTOSUSPEND_DELAY	5000 /* ms */
44 
45 /**
46  * dwc3_get_dr_mode - Validates and sets dr_mode
47  * @dwc: pointer to our context structure
48  */
49 static int dwc3_get_dr_mode(struct dwc3 *dwc)
50 {
51 	enum usb_dr_mode mode;
52 	struct device *dev = dwc->dev;
53 	unsigned int hw_mode;
54 
55 	if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
56 		dwc->dr_mode = USB_DR_MODE_OTG;
57 
58 	mode = dwc->dr_mode;
59 	hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
60 
61 	switch (hw_mode) {
62 	case DWC3_GHWPARAMS0_MODE_GADGET:
63 		if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) {
64 			dev_err(dev,
65 				"Controller does not support host mode.\n");
66 			return -EINVAL;
67 		}
68 		mode = USB_DR_MODE_PERIPHERAL;
69 		break;
70 	case DWC3_GHWPARAMS0_MODE_HOST:
71 		if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
72 			dev_err(dev,
73 				"Controller does not support device mode.\n");
74 			return -EINVAL;
75 		}
76 		mode = USB_DR_MODE_HOST;
77 		break;
78 	default:
79 		if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
80 			mode = USB_DR_MODE_HOST;
81 		else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
82 			mode = USB_DR_MODE_PERIPHERAL;
83 
84 		/*
85 		 * DWC_usb31 and DWC_usb3 v3.30a and higher do not support OTG
86 		 * mode. If the controller supports DRD but the dr_mode is not
87 		 * specified or set to OTG, then set the mode to peripheral.
88 		 */
89 		if (mode == USB_DR_MODE_OTG && !dwc->edev &&
90 		    (!IS_ENABLED(CONFIG_USB_ROLE_SWITCH) ||
91 		     !device_property_read_bool(dwc->dev, "usb-role-switch")) &&
92 		    !DWC3_VER_IS_PRIOR(DWC3, 330A))
93 			mode = USB_DR_MODE_PERIPHERAL;
94 	}
95 
96 	if (mode != dwc->dr_mode) {
97 		dev_warn(dev,
98 			 "Configuration mismatch. dr_mode forced to %s\n",
99 			 mode == USB_DR_MODE_HOST ? "host" : "gadget");
100 
101 		dwc->dr_mode = mode;
102 	}
103 
104 	return 0;
105 }
106 
107 void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
108 {
109 	u32 reg;
110 
111 	reg = dwc3_readl(dwc->regs, DWC3_GCTL);
112 	reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
113 	reg |= DWC3_GCTL_PRTCAPDIR(mode);
114 	dwc3_writel(dwc->regs, DWC3_GCTL, reg);
115 
116 	dwc->current_dr_role = mode;
117 }
118 
119 static void __dwc3_set_mode(struct work_struct *work)
120 {
121 	struct dwc3 *dwc = work_to_dwc(work);
122 	unsigned long flags;
123 	int ret;
124 	u32 reg;
125 	u32 desired_dr_role;
126 
127 	mutex_lock(&dwc->mutex);
128 	spin_lock_irqsave(&dwc->lock, flags);
129 	desired_dr_role = dwc->desired_dr_role;
130 	spin_unlock_irqrestore(&dwc->lock, flags);
131 
132 	pm_runtime_get_sync(dwc->dev);
133 
134 	if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
135 		dwc3_otg_update(dwc, 0);
136 
137 	if (!desired_dr_role)
138 		goto out;
139 
140 	if (desired_dr_role == dwc->current_dr_role)
141 		goto out;
142 
143 	if (desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev)
144 		goto out;
145 
146 	switch (dwc->current_dr_role) {
147 	case DWC3_GCTL_PRTCAP_HOST:
148 		dwc3_host_exit(dwc);
149 		break;
150 	case DWC3_GCTL_PRTCAP_DEVICE:
151 		dwc3_gadget_exit(dwc);
152 		dwc3_event_buffers_cleanup(dwc);
153 		break;
154 	case DWC3_GCTL_PRTCAP_OTG:
155 		dwc3_otg_exit(dwc);
156 		spin_lock_irqsave(&dwc->lock, flags);
157 		dwc->desired_otg_role = DWC3_OTG_ROLE_IDLE;
158 		spin_unlock_irqrestore(&dwc->lock, flags);
159 		dwc3_otg_update(dwc, 1);
160 		break;
161 	default:
162 		break;
163 	}
164 
165 	/*
166 	 * When current_dr_role is not set, there's no role switching.
167 	 * Only perform GCTL.CoreSoftReset when there's DRD role switching.
168 	 */
169 	if (dwc->current_dr_role && ((DWC3_IP_IS(DWC3) ||
170 			DWC3_VER_IS_PRIOR(DWC31, 190A)) &&
171 			desired_dr_role != DWC3_GCTL_PRTCAP_OTG)) {
172 		reg = dwc3_readl(dwc->regs, DWC3_GCTL);
173 		reg |= DWC3_GCTL_CORESOFTRESET;
174 		dwc3_writel(dwc->regs, DWC3_GCTL, reg);
175 
176 		/*
177 		 * Wait for internal clocks to synchronized. DWC_usb31 and
178 		 * DWC_usb32 may need at least 50ms (less for DWC_usb3). To
179 		 * keep it consistent across different IPs, let's wait up to
180 		 * 100ms before clearing GCTL.CORESOFTRESET.
181 		 */
182 		msleep(100);
183 
184 		reg = dwc3_readl(dwc->regs, DWC3_GCTL);
185 		reg &= ~DWC3_GCTL_CORESOFTRESET;
186 		dwc3_writel(dwc->regs, DWC3_GCTL, reg);
187 	}
188 
189 	spin_lock_irqsave(&dwc->lock, flags);
190 
191 	dwc3_set_prtcap(dwc, desired_dr_role);
192 
193 	spin_unlock_irqrestore(&dwc->lock, flags);
194 
195 	switch (desired_dr_role) {
196 	case DWC3_GCTL_PRTCAP_HOST:
197 		ret = dwc3_host_init(dwc);
198 		if (ret) {
199 			dev_err(dwc->dev, "failed to initialize host\n");
200 		} else {
201 			if (dwc->usb2_phy)
202 				otg_set_vbus(dwc->usb2_phy->otg, true);
203 			phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
204 			phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
205 			if (dwc->dis_split_quirk) {
206 				reg = dwc3_readl(dwc->regs, DWC3_GUCTL3);
207 				reg |= DWC3_GUCTL3_SPLITDISABLE;
208 				dwc3_writel(dwc->regs, DWC3_GUCTL3, reg);
209 			}
210 		}
211 		break;
212 	case DWC3_GCTL_PRTCAP_DEVICE:
213 		dwc3_core_soft_reset(dwc);
214 
215 		dwc3_event_buffers_setup(dwc);
216 
217 		if (dwc->usb2_phy)
218 			otg_set_vbus(dwc->usb2_phy->otg, false);
219 		phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
220 		phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
221 
222 		ret = dwc3_gadget_init(dwc);
223 		if (ret)
224 			dev_err(dwc->dev, "failed to initialize peripheral\n");
225 		break;
226 	case DWC3_GCTL_PRTCAP_OTG:
227 		dwc3_otg_init(dwc);
228 		dwc3_otg_update(dwc, 0);
229 		break;
230 	default:
231 		break;
232 	}
233 
234 out:
235 	pm_runtime_mark_last_busy(dwc->dev);
236 	pm_runtime_put_autosuspend(dwc->dev);
237 	mutex_unlock(&dwc->mutex);
238 }
239 
240 void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
241 {
242 	unsigned long flags;
243 
244 	if (dwc->dr_mode != USB_DR_MODE_OTG)
245 		return;
246 
247 	spin_lock_irqsave(&dwc->lock, flags);
248 	dwc->desired_dr_role = mode;
249 	spin_unlock_irqrestore(&dwc->lock, flags);
250 
251 	queue_work(system_freezable_wq, &dwc->drd_work);
252 }
253 
254 u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
255 {
256 	struct dwc3		*dwc = dep->dwc;
257 	u32			reg;
258 
259 	dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
260 			DWC3_GDBGFIFOSPACE_NUM(dep->number) |
261 			DWC3_GDBGFIFOSPACE_TYPE(type));
262 
263 	reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
264 
265 	return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
266 }
267 
268 /**
269  * dwc3_core_soft_reset - Issues core soft reset and PHY reset
270  * @dwc: pointer to our context structure
271  */
272 int dwc3_core_soft_reset(struct dwc3 *dwc)
273 {
274 	u32		reg;
275 	int		retries = 1000;
276 
277 	/*
278 	 * We're resetting only the device side because, if we're in host mode,
279 	 * XHCI driver will reset the host block. If dwc3 was configured for
280 	 * host-only mode or current role is host, then we can return early.
281 	 */
282 	if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST)
283 		return 0;
284 
285 	/*
286 	 * If the dr_mode is host and the dwc->current_dr_role is not the
287 	 * corresponding DWC3_GCTL_PRTCAP_HOST, then the dwc3_core_init_mode
288 	 * isn't executed yet. Ensure the phy is ready before the controller
289 	 * updates the GCTL.PRTCAPDIR or other settings by soft-resetting
290 	 * the phy.
291 	 *
292 	 * Note: GUSB3PIPECTL[n] and GUSB2PHYCFG[n] are port settings where n
293 	 * is port index. If this is a multiport host, then we need to reset
294 	 * all active ports.
295 	 */
296 	if (dwc->dr_mode == USB_DR_MODE_HOST) {
297 		u32 usb3_port;
298 		u32 usb2_port;
299 
300 		usb3_port = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
301 		usb3_port |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
302 		dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), usb3_port);
303 
304 		usb2_port = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
305 		usb2_port |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
306 		dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), usb2_port);
307 
308 		/* Small delay for phy reset assertion */
309 		usleep_range(1000, 2000);
310 
311 		usb3_port &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
312 		dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), usb3_port);
313 
314 		usb2_port &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
315 		dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), usb2_port);
316 
317 		/* Wait for clock synchronization */
318 		msleep(50);
319 		return 0;
320 	}
321 
322 	reg = dwc3_readl(dwc->regs, DWC3_DCTL);
323 	reg |= DWC3_DCTL_CSFTRST;
324 	reg &= ~DWC3_DCTL_RUN_STOP;
325 	dwc3_gadget_dctl_write_safe(dwc, reg);
326 
327 	/*
328 	 * For DWC_usb31 controller 1.90a and later, the DCTL.CSFRST bit
329 	 * is cleared only after all the clocks are synchronized. This can
330 	 * take a little more than 50ms. Set the polling rate at 20ms
331 	 * for 10 times instead.
332 	 */
333 	if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
334 		retries = 10;
335 
336 	do {
337 		reg = dwc3_readl(dwc->regs, DWC3_DCTL);
338 		if (!(reg & DWC3_DCTL_CSFTRST))
339 			goto done;
340 
341 		if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
342 			msleep(20);
343 		else
344 			udelay(1);
345 	} while (--retries);
346 
347 	dev_warn(dwc->dev, "DWC3 controller soft reset failed.\n");
348 	return -ETIMEDOUT;
349 
350 done:
351 	/*
352 	 * For DWC_usb31 controller 1.80a and prior, once DCTL.CSFRST bit
353 	 * is cleared, we must wait at least 50ms before accessing the PHY
354 	 * domain (synchronization delay).
355 	 */
356 	if (DWC3_VER_IS_WITHIN(DWC31, ANY, 180A))
357 		msleep(50);
358 
359 	return 0;
360 }
361 
362 /*
363  * dwc3_frame_length_adjustment - Adjusts frame length if required
364  * @dwc3: Pointer to our controller context structure
365  */
366 static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
367 {
368 	u32 reg;
369 	u32 dft;
370 
371 	if (DWC3_VER_IS_PRIOR(DWC3, 250A))
372 		return;
373 
374 	if (dwc->fladj == 0)
375 		return;
376 
377 	reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
378 	dft = reg & DWC3_GFLADJ_30MHZ_MASK;
379 	if (dft != dwc->fladj) {
380 		reg &= ~DWC3_GFLADJ_30MHZ_MASK;
381 		reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
382 		dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
383 	}
384 }
385 
386 /**
387  * dwc3_ref_clk_period - Reference clock period configuration
388  *		Default reference clock period depends on hardware
389  *		configuration. For systems with reference clock that differs
390  *		from the default, this will set clock period in DWC3_GUCTL
391  *		register.
392  * @dwc: Pointer to our controller context structure
393  */
394 static void dwc3_ref_clk_period(struct dwc3 *dwc)
395 {
396 	unsigned long period;
397 	unsigned long fladj;
398 	unsigned long decr;
399 	unsigned long rate;
400 	u32 reg;
401 
402 	if (dwc->ref_clk) {
403 		rate = clk_get_rate(dwc->ref_clk);
404 		if (!rate)
405 			return;
406 		period = NSEC_PER_SEC / rate;
407 	} else if (dwc->ref_clk_per) {
408 		period = dwc->ref_clk_per;
409 		rate = NSEC_PER_SEC / period;
410 	} else {
411 		return;
412 	}
413 
414 	reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
415 	reg &= ~DWC3_GUCTL_REFCLKPER_MASK;
416 	reg |=  FIELD_PREP(DWC3_GUCTL_REFCLKPER_MASK, period);
417 	dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
418 
419 	if (DWC3_VER_IS_PRIOR(DWC3, 250A))
420 		return;
421 
422 	/*
423 	 * The calculation below is
424 	 *
425 	 * 125000 * (NSEC_PER_SEC / (rate * period) - 1)
426 	 *
427 	 * but rearranged for fixed-point arithmetic. The division must be
428 	 * 64-bit because 125000 * NSEC_PER_SEC doesn't fit in 32 bits (and
429 	 * neither does rate * period).
430 	 *
431 	 * Note that rate * period ~= NSEC_PER_SECOND, minus the number of
432 	 * nanoseconds of error caused by the truncation which happened during
433 	 * the division when calculating rate or period (whichever one was
434 	 * derived from the other). We first calculate the relative error, then
435 	 * scale it to units of 8 ppm.
436 	 */
437 	fladj = div64_u64(125000ULL * NSEC_PER_SEC, (u64)rate * period);
438 	fladj -= 125000;
439 
440 	/*
441 	 * The documented 240MHz constant is scaled by 2 to get PLS1 as well.
442 	 */
443 	decr = 480000000 / rate;
444 
445 	reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
446 	reg &= ~DWC3_GFLADJ_REFCLK_FLADJ_MASK
447 	    &  ~DWC3_GFLADJ_240MHZDECR
448 	    &  ~DWC3_GFLADJ_240MHZDECR_PLS1;
449 	reg |= FIELD_PREP(DWC3_GFLADJ_REFCLK_FLADJ_MASK, fladj)
450 	    |  FIELD_PREP(DWC3_GFLADJ_240MHZDECR, decr >> 1)
451 	    |  FIELD_PREP(DWC3_GFLADJ_240MHZDECR_PLS1, decr & 1);
452 
453 	if (dwc->gfladj_refclk_lpm_sel)
454 		reg |=  DWC3_GFLADJ_REFCLK_LPM_SEL;
455 
456 	dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
457 }
458 
459 /**
460  * dwc3_free_one_event_buffer - Frees one event buffer
461  * @dwc: Pointer to our controller context structure
462  * @evt: Pointer to event buffer to be freed
463  */
464 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
465 		struct dwc3_event_buffer *evt)
466 {
467 	dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
468 }
469 
470 /**
471  * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
472  * @dwc: Pointer to our controller context structure
473  * @length: size of the event buffer
474  *
475  * Returns a pointer to the allocated event buffer structure on success
476  * otherwise ERR_PTR(errno).
477  */
478 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
479 		unsigned int length)
480 {
481 	struct dwc3_event_buffer	*evt;
482 
483 	evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
484 	if (!evt)
485 		return ERR_PTR(-ENOMEM);
486 
487 	evt->dwc	= dwc;
488 	evt->length	= length;
489 	evt->cache	= devm_kzalloc(dwc->dev, length, GFP_KERNEL);
490 	if (!evt->cache)
491 		return ERR_PTR(-ENOMEM);
492 
493 	evt->buf	= dma_alloc_coherent(dwc->sysdev, length,
494 			&evt->dma, GFP_KERNEL);
495 	if (!evt->buf)
496 		return ERR_PTR(-ENOMEM);
497 
498 	return evt;
499 }
500 
501 /**
502  * dwc3_free_event_buffers - frees all allocated event buffers
503  * @dwc: Pointer to our controller context structure
504  */
505 static void dwc3_free_event_buffers(struct dwc3 *dwc)
506 {
507 	struct dwc3_event_buffer	*evt;
508 
509 	evt = dwc->ev_buf;
510 	if (evt)
511 		dwc3_free_one_event_buffer(dwc, evt);
512 }
513 
514 /**
515  * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
516  * @dwc: pointer to our controller context structure
517  * @length: size of event buffer
518  *
519  * Returns 0 on success otherwise negative errno. In the error case, dwc
520  * may contain some buffers allocated but not all which were requested.
521  */
522 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned int length)
523 {
524 	struct dwc3_event_buffer *evt;
525 
526 	evt = dwc3_alloc_one_event_buffer(dwc, length);
527 	if (IS_ERR(evt)) {
528 		dev_err(dwc->dev, "can't allocate event buffer\n");
529 		return PTR_ERR(evt);
530 	}
531 	dwc->ev_buf = evt;
532 
533 	return 0;
534 }
535 
536 /**
537  * dwc3_event_buffers_setup - setup our allocated event buffers
538  * @dwc: pointer to our controller context structure
539  *
540  * Returns 0 on success otherwise negative errno.
541  */
542 int dwc3_event_buffers_setup(struct dwc3 *dwc)
543 {
544 	struct dwc3_event_buffer	*evt;
545 
546 	evt = dwc->ev_buf;
547 	evt->lpos = 0;
548 	dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
549 			lower_32_bits(evt->dma));
550 	dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
551 			upper_32_bits(evt->dma));
552 	dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
553 			DWC3_GEVNTSIZ_SIZE(evt->length));
554 	dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
555 
556 	return 0;
557 }
558 
559 void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
560 {
561 	struct dwc3_event_buffer	*evt;
562 
563 	evt = dwc->ev_buf;
564 
565 	evt->lpos = 0;
566 
567 	dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
568 	dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
569 	dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
570 			| DWC3_GEVNTSIZ_SIZE(0));
571 	dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
572 }
573 
574 static void dwc3_core_num_eps(struct dwc3 *dwc)
575 {
576 	struct dwc3_hwparams	*parms = &dwc->hwparams;
577 
578 	dwc->num_eps = DWC3_NUM_EPS(parms);
579 }
580 
581 static void dwc3_cache_hwparams(struct dwc3 *dwc)
582 {
583 	struct dwc3_hwparams	*parms = &dwc->hwparams;
584 
585 	parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
586 	parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
587 	parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
588 	parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
589 	parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
590 	parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
591 	parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
592 	parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
593 	parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
594 
595 	if (DWC3_IP_IS(DWC32))
596 		parms->hwparams9 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS9);
597 }
598 
599 static int dwc3_core_ulpi_init(struct dwc3 *dwc)
600 {
601 	int intf;
602 	int ret = 0;
603 
604 	intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
605 
606 	if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
607 	    (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
608 	     dwc->hsphy_interface &&
609 	     !strncmp(dwc->hsphy_interface, "ulpi", 4)))
610 		ret = dwc3_ulpi_init(dwc);
611 
612 	return ret;
613 }
614 
615 /**
616  * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
617  * @dwc: Pointer to our controller context structure
618  *
619  * Returns 0 on success. The USB PHY interfaces are configured but not
620  * initialized. The PHY interfaces and the PHYs get initialized together with
621  * the core in dwc3_core_init.
622  */
623 static int dwc3_phy_setup(struct dwc3 *dwc)
624 {
625 	unsigned int hw_mode;
626 	u32 reg;
627 
628 	hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
629 
630 	reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
631 
632 	/*
633 	 * Make sure UX_EXIT_PX is cleared as that causes issues with some
634 	 * PHYs. Also, this bit is not supposed to be used in normal operation.
635 	 */
636 	reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
637 
638 	/*
639 	 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
640 	 * to '0' during coreConsultant configuration. So default value
641 	 * will be '0' when the core is reset. Application needs to set it
642 	 * to '1' after the core initialization is completed.
643 	 */
644 	if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
645 		reg |= DWC3_GUSB3PIPECTL_SUSPHY;
646 
647 	/*
648 	 * For DRD controllers, GUSB3PIPECTL.SUSPENDENABLE must be cleared after
649 	 * power-on reset, and it can be set after core initialization, which is
650 	 * after device soft-reset during initialization.
651 	 */
652 	if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD)
653 		reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
654 
655 	if (dwc->u2ss_inp3_quirk)
656 		reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
657 
658 	if (dwc->dis_rxdet_inp3_quirk)
659 		reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
660 
661 	if (dwc->req_p1p2p3_quirk)
662 		reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
663 
664 	if (dwc->del_p1p2p3_quirk)
665 		reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
666 
667 	if (dwc->del_phy_power_chg_quirk)
668 		reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
669 
670 	if (dwc->lfps_filter_quirk)
671 		reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
672 
673 	if (dwc->rx_detect_poll_quirk)
674 		reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
675 
676 	if (dwc->tx_de_emphasis_quirk)
677 		reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
678 
679 	if (dwc->dis_u3_susphy_quirk)
680 		reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
681 
682 	if (dwc->dis_del_phy_power_chg_quirk)
683 		reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
684 
685 	dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
686 
687 	reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
688 
689 	/* Select the HS PHY interface */
690 	switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
691 	case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
692 		if (dwc->hsphy_interface &&
693 				!strncmp(dwc->hsphy_interface, "utmi", 4)) {
694 			reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
695 			break;
696 		} else if (dwc->hsphy_interface &&
697 				!strncmp(dwc->hsphy_interface, "ulpi", 4)) {
698 			reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
699 			dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
700 		} else {
701 			/* Relying on default value. */
702 			if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
703 				break;
704 		}
705 		fallthrough;
706 	case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
707 	default:
708 		break;
709 	}
710 
711 	switch (dwc->hsphy_mode) {
712 	case USBPHY_INTERFACE_MODE_UTMI:
713 		reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
714 		       DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
715 		reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
716 		       DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
717 		break;
718 	case USBPHY_INTERFACE_MODE_UTMIW:
719 		reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
720 		       DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
721 		reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
722 		       DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
723 		break;
724 	default:
725 		break;
726 	}
727 
728 	/*
729 	 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
730 	 * '0' during coreConsultant configuration. So default value will
731 	 * be '0' when the core is reset. Application needs to set it to
732 	 * '1' after the core initialization is completed.
733 	 */
734 	if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
735 		reg |= DWC3_GUSB2PHYCFG_SUSPHY;
736 
737 	/*
738 	 * For DRD controllers, GUSB2PHYCFG.SUSPHY must be cleared after
739 	 * power-on reset, and it can be set after core initialization, which is
740 	 * after device soft-reset during initialization.
741 	 */
742 	if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD)
743 		reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
744 
745 	if (dwc->dis_u2_susphy_quirk)
746 		reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
747 
748 	if (dwc->dis_enblslpm_quirk)
749 		reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
750 	else
751 		reg |= DWC3_GUSB2PHYCFG_ENBLSLPM;
752 
753 	if (dwc->dis_u2_freeclk_exists_quirk || dwc->gfladj_refclk_lpm_sel)
754 		reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
755 
756 	/*
757 	 * Some ULPI USB PHY does not support internal VBUS supply, to drive
758 	 * the CPEN pin requires the configuration of the ULPI DRVVBUSEXTERNAL
759 	 * bit of OTG_CTRL register. Controller configures the USB2 PHY
760 	 * ULPIEXTVBUSDRV bit[17] of the GUSB2PHYCFG register to drive vBus
761 	 * with an external supply.
762 	 */
763 	if (dwc->ulpi_ext_vbus_drv)
764 		reg |= DWC3_GUSB2PHYCFG_ULPIEXTVBUSDRV;
765 
766 	dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
767 
768 	return 0;
769 }
770 
771 static int dwc3_phy_init(struct dwc3 *dwc)
772 {
773 	int ret;
774 
775 	usb_phy_init(dwc->usb2_phy);
776 	usb_phy_init(dwc->usb3_phy);
777 
778 	ret = phy_init(dwc->usb2_generic_phy);
779 	if (ret < 0)
780 		goto err_shutdown_usb3_phy;
781 
782 	ret = phy_init(dwc->usb3_generic_phy);
783 	if (ret < 0)
784 		goto err_exit_usb2_phy;
785 
786 	return 0;
787 
788 err_exit_usb2_phy:
789 	phy_exit(dwc->usb2_generic_phy);
790 err_shutdown_usb3_phy:
791 	usb_phy_shutdown(dwc->usb3_phy);
792 	usb_phy_shutdown(dwc->usb2_phy);
793 
794 	return ret;
795 }
796 
797 static void dwc3_phy_exit(struct dwc3 *dwc)
798 {
799 	phy_exit(dwc->usb3_generic_phy);
800 	phy_exit(dwc->usb2_generic_phy);
801 
802 	usb_phy_shutdown(dwc->usb3_phy);
803 	usb_phy_shutdown(dwc->usb2_phy);
804 }
805 
806 static int dwc3_phy_power_on(struct dwc3 *dwc)
807 {
808 	int ret;
809 
810 	usb_phy_set_suspend(dwc->usb2_phy, 0);
811 	usb_phy_set_suspend(dwc->usb3_phy, 0);
812 
813 	ret = phy_power_on(dwc->usb2_generic_phy);
814 	if (ret < 0)
815 		goto err_suspend_usb3_phy;
816 
817 	ret = phy_power_on(dwc->usb3_generic_phy);
818 	if (ret < 0)
819 		goto err_power_off_usb2_phy;
820 
821 	return 0;
822 
823 err_power_off_usb2_phy:
824 	phy_power_off(dwc->usb2_generic_phy);
825 err_suspend_usb3_phy:
826 	usb_phy_set_suspend(dwc->usb3_phy, 1);
827 	usb_phy_set_suspend(dwc->usb2_phy, 1);
828 
829 	return ret;
830 }
831 
832 static void dwc3_phy_power_off(struct dwc3 *dwc)
833 {
834 	phy_power_off(dwc->usb3_generic_phy);
835 	phy_power_off(dwc->usb2_generic_phy);
836 
837 	usb_phy_set_suspend(dwc->usb3_phy, 1);
838 	usb_phy_set_suspend(dwc->usb2_phy, 1);
839 }
840 
841 static int dwc3_clk_enable(struct dwc3 *dwc)
842 {
843 	int ret;
844 
845 	ret = clk_prepare_enable(dwc->bus_clk);
846 	if (ret)
847 		return ret;
848 
849 	ret = clk_prepare_enable(dwc->ref_clk);
850 	if (ret)
851 		goto disable_bus_clk;
852 
853 	ret = clk_prepare_enable(dwc->susp_clk);
854 	if (ret)
855 		goto disable_ref_clk;
856 
857 	return 0;
858 
859 disable_ref_clk:
860 	clk_disable_unprepare(dwc->ref_clk);
861 disable_bus_clk:
862 	clk_disable_unprepare(dwc->bus_clk);
863 	return ret;
864 }
865 
866 static void dwc3_clk_disable(struct dwc3 *dwc)
867 {
868 	clk_disable_unprepare(dwc->susp_clk);
869 	clk_disable_unprepare(dwc->ref_clk);
870 	clk_disable_unprepare(dwc->bus_clk);
871 }
872 
873 static void dwc3_core_exit(struct dwc3 *dwc)
874 {
875 	dwc3_event_buffers_cleanup(dwc);
876 	dwc3_phy_power_off(dwc);
877 	dwc3_phy_exit(dwc);
878 	dwc3_clk_disable(dwc);
879 	reset_control_assert(dwc->reset);
880 }
881 
882 static bool dwc3_core_is_valid(struct dwc3 *dwc)
883 {
884 	u32 reg;
885 
886 	reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
887 	dwc->ip = DWC3_GSNPS_ID(reg);
888 
889 	/* This should read as U3 followed by revision number */
890 	if (DWC3_IP_IS(DWC3)) {
891 		dwc->revision = reg;
892 	} else if (DWC3_IP_IS(DWC31) || DWC3_IP_IS(DWC32)) {
893 		dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
894 		dwc->version_type = dwc3_readl(dwc->regs, DWC3_VER_TYPE);
895 	} else {
896 		return false;
897 	}
898 
899 	return true;
900 }
901 
902 static void dwc3_core_setup_global_control(struct dwc3 *dwc)
903 {
904 	u32 reg;
905 
906 	reg = dwc3_readl(dwc->regs, DWC3_GCTL);
907 	reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
908 
909 	switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
910 	case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
911 		/**
912 		 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
913 		 * issue which would cause xHCI compliance tests to fail.
914 		 *
915 		 * Because of that we cannot enable clock gating on such
916 		 * configurations.
917 		 *
918 		 * Refers to:
919 		 *
920 		 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
921 		 * SOF/ITP Mode Used
922 		 */
923 		if ((dwc->dr_mode == USB_DR_MODE_HOST ||
924 				dwc->dr_mode == USB_DR_MODE_OTG) &&
925 				DWC3_VER_IS_WITHIN(DWC3, 210A, 250A))
926 			reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
927 		else
928 			reg &= ~DWC3_GCTL_DSBLCLKGTNG;
929 		break;
930 	case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
931 		/*
932 		 * REVISIT Enabling this bit so that host-mode hibernation
933 		 * will work. Device-mode hibernation is not yet implemented.
934 		 */
935 		reg |= DWC3_GCTL_GBLHIBERNATIONEN;
936 		break;
937 	default:
938 		/* nothing */
939 		break;
940 	}
941 
942 	/* check if current dwc3 is on simulation board */
943 	if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
944 		dev_info(dwc->dev, "Running with FPGA optimizations\n");
945 		dwc->is_fpga = true;
946 	}
947 
948 	WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
949 			"disable_scramble cannot be used on non-FPGA builds\n");
950 
951 	if (dwc->disable_scramble_quirk && dwc->is_fpga)
952 		reg |= DWC3_GCTL_DISSCRAMBLE;
953 	else
954 		reg &= ~DWC3_GCTL_DISSCRAMBLE;
955 
956 	if (dwc->u2exit_lfps_quirk)
957 		reg |= DWC3_GCTL_U2EXIT_LFPS;
958 
959 	/*
960 	 * WORKAROUND: DWC3 revisions <1.90a have a bug
961 	 * where the device can fail to connect at SuperSpeed
962 	 * and falls back to high-speed mode which causes
963 	 * the device to enter a Connect/Disconnect loop
964 	 */
965 	if (DWC3_VER_IS_PRIOR(DWC3, 190A))
966 		reg |= DWC3_GCTL_U2RSTECN;
967 
968 	dwc3_writel(dwc->regs, DWC3_GCTL, reg);
969 }
970 
971 static int dwc3_core_get_phy(struct dwc3 *dwc);
972 static int dwc3_core_ulpi_init(struct dwc3 *dwc);
973 
974 /* set global incr burst type configuration registers */
975 static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
976 {
977 	struct device *dev = dwc->dev;
978 	/* incrx_mode : for INCR burst type. */
979 	bool incrx_mode;
980 	/* incrx_size : for size of INCRX burst. */
981 	u32 incrx_size;
982 	u32 *vals;
983 	u32 cfg;
984 	int ntype;
985 	int ret;
986 	int i;
987 
988 	cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
989 
990 	/*
991 	 * Handle property "snps,incr-burst-type-adjustment".
992 	 * Get the number of value from this property:
993 	 * result <= 0, means this property is not supported.
994 	 * result = 1, means INCRx burst mode supported.
995 	 * result > 1, means undefined length burst mode supported.
996 	 */
997 	ntype = device_property_count_u32(dev, "snps,incr-burst-type-adjustment");
998 	if (ntype <= 0)
999 		return;
1000 
1001 	vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL);
1002 	if (!vals)
1003 		return;
1004 
1005 	/* Get INCR burst type, and parse it */
1006 	ret = device_property_read_u32_array(dev,
1007 			"snps,incr-burst-type-adjustment", vals, ntype);
1008 	if (ret) {
1009 		kfree(vals);
1010 		dev_err(dev, "Error to get property\n");
1011 		return;
1012 	}
1013 
1014 	incrx_size = *vals;
1015 
1016 	if (ntype > 1) {
1017 		/* INCRX (undefined length) burst mode */
1018 		incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
1019 		for (i = 1; i < ntype; i++) {
1020 			if (vals[i] > incrx_size)
1021 				incrx_size = vals[i];
1022 		}
1023 	} else {
1024 		/* INCRX burst mode */
1025 		incrx_mode = INCRX_BURST_MODE;
1026 	}
1027 
1028 	kfree(vals);
1029 
1030 	/* Enable Undefined Length INCR Burst and Enable INCRx Burst */
1031 	cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
1032 	if (incrx_mode)
1033 		cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
1034 	switch (incrx_size) {
1035 	case 256:
1036 		cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
1037 		break;
1038 	case 128:
1039 		cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
1040 		break;
1041 	case 64:
1042 		cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
1043 		break;
1044 	case 32:
1045 		cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
1046 		break;
1047 	case 16:
1048 		cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
1049 		break;
1050 	case 8:
1051 		cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
1052 		break;
1053 	case 4:
1054 		cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
1055 		break;
1056 	case 1:
1057 		break;
1058 	default:
1059 		dev_err(dev, "Invalid property\n");
1060 		break;
1061 	}
1062 
1063 	dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
1064 }
1065 
1066 static void dwc3_set_power_down_clk_scale(struct dwc3 *dwc)
1067 {
1068 	u32 scale;
1069 	u32 reg;
1070 
1071 	if (!dwc->susp_clk)
1072 		return;
1073 
1074 	/*
1075 	 * The power down scale field specifies how many suspend_clk
1076 	 * periods fit into a 16KHz clock period. When performing
1077 	 * the division, round up the remainder.
1078 	 *
1079 	 * The power down scale value is calculated using the fastest
1080 	 * frequency of the suspend_clk. If it isn't fixed (but within
1081 	 * the accuracy requirement), the driver may not know the max
1082 	 * rate of the suspend_clk, so only update the power down scale
1083 	 * if the default is less than the calculated value from
1084 	 * clk_get_rate() or if the default is questionably high
1085 	 * (3x or more) to be within the requirement.
1086 	 */
1087 	scale = DIV_ROUND_UP(clk_get_rate(dwc->susp_clk), 16000);
1088 	reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1089 	if ((reg & DWC3_GCTL_PWRDNSCALE_MASK) < DWC3_GCTL_PWRDNSCALE(scale) ||
1090 	    (reg & DWC3_GCTL_PWRDNSCALE_MASK) > DWC3_GCTL_PWRDNSCALE(scale*3)) {
1091 		reg &= ~(DWC3_GCTL_PWRDNSCALE_MASK);
1092 		reg |= DWC3_GCTL_PWRDNSCALE(scale);
1093 		dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1094 	}
1095 }
1096 
1097 static void dwc3_config_threshold(struct dwc3 *dwc)
1098 {
1099 	u32 reg;
1100 	u8 rx_thr_num;
1101 	u8 rx_maxburst;
1102 	u8 tx_thr_num;
1103 	u8 tx_maxburst;
1104 
1105 	/*
1106 	 * Must config both number of packets and max burst settings to enable
1107 	 * RX and/or TX threshold.
1108 	 */
1109 	if (!DWC3_IP_IS(DWC3) && dwc->dr_mode == USB_DR_MODE_HOST) {
1110 		rx_thr_num = dwc->rx_thr_num_pkt_prd;
1111 		rx_maxburst = dwc->rx_max_burst_prd;
1112 		tx_thr_num = dwc->tx_thr_num_pkt_prd;
1113 		tx_maxburst = dwc->tx_max_burst_prd;
1114 
1115 		if (rx_thr_num && rx_maxburst) {
1116 			reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1117 			reg |= DWC31_RXTHRNUMPKTSEL_PRD;
1118 
1119 			reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
1120 			reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
1121 
1122 			reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
1123 			reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
1124 
1125 			dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1126 		}
1127 
1128 		if (tx_thr_num && tx_maxburst) {
1129 			reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1130 			reg |= DWC31_TXTHRNUMPKTSEL_PRD;
1131 
1132 			reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
1133 			reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
1134 
1135 			reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
1136 			reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
1137 
1138 			dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1139 		}
1140 	}
1141 
1142 	rx_thr_num = dwc->rx_thr_num_pkt;
1143 	rx_maxburst = dwc->rx_max_burst;
1144 	tx_thr_num = dwc->tx_thr_num_pkt;
1145 	tx_maxburst = dwc->tx_max_burst;
1146 
1147 	if (DWC3_IP_IS(DWC3)) {
1148 		if (rx_thr_num && rx_maxburst) {
1149 			reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1150 			reg |= DWC3_GRXTHRCFG_PKTCNTSEL;
1151 
1152 			reg &= ~DWC3_GRXTHRCFG_RXPKTCNT(~0);
1153 			reg |= DWC3_GRXTHRCFG_RXPKTCNT(rx_thr_num);
1154 
1155 			reg &= ~DWC3_GRXTHRCFG_MAXRXBURSTSIZE(~0);
1156 			reg |= DWC3_GRXTHRCFG_MAXRXBURSTSIZE(rx_maxburst);
1157 
1158 			dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1159 		}
1160 
1161 		if (tx_thr_num && tx_maxburst) {
1162 			reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1163 			reg |= DWC3_GTXTHRCFG_PKTCNTSEL;
1164 
1165 			reg &= ~DWC3_GTXTHRCFG_TXPKTCNT(~0);
1166 			reg |= DWC3_GTXTHRCFG_TXPKTCNT(tx_thr_num);
1167 
1168 			reg &= ~DWC3_GTXTHRCFG_MAXTXBURSTSIZE(~0);
1169 			reg |= DWC3_GTXTHRCFG_MAXTXBURSTSIZE(tx_maxburst);
1170 
1171 			dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1172 		}
1173 	} else {
1174 		if (rx_thr_num && rx_maxburst) {
1175 			reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1176 			reg |= DWC31_GRXTHRCFG_PKTCNTSEL;
1177 
1178 			reg &= ~DWC31_GRXTHRCFG_RXPKTCNT(~0);
1179 			reg |= DWC31_GRXTHRCFG_RXPKTCNT(rx_thr_num);
1180 
1181 			reg &= ~DWC31_GRXTHRCFG_MAXRXBURSTSIZE(~0);
1182 			reg |= DWC31_GRXTHRCFG_MAXRXBURSTSIZE(rx_maxburst);
1183 
1184 			dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1185 		}
1186 
1187 		if (tx_thr_num && tx_maxburst) {
1188 			reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1189 			reg |= DWC31_GTXTHRCFG_PKTCNTSEL;
1190 
1191 			reg &= ~DWC31_GTXTHRCFG_TXPKTCNT(~0);
1192 			reg |= DWC31_GTXTHRCFG_TXPKTCNT(tx_thr_num);
1193 
1194 			reg &= ~DWC31_GTXTHRCFG_MAXTXBURSTSIZE(~0);
1195 			reg |= DWC31_GTXTHRCFG_MAXTXBURSTSIZE(tx_maxburst);
1196 
1197 			dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1198 		}
1199 	}
1200 }
1201 
1202 /**
1203  * dwc3_core_init - Low-level initialization of DWC3 Core
1204  * @dwc: Pointer to our controller context structure
1205  *
1206  * Returns 0 on success otherwise negative errno.
1207  */
1208 static int dwc3_core_init(struct dwc3 *dwc)
1209 {
1210 	unsigned int		hw_mode;
1211 	u32			reg;
1212 	int			ret;
1213 
1214 	hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
1215 
1216 	/*
1217 	 * Write Linux Version Code to our GUID register so it's easy to figure
1218 	 * out which kernel version a bug was found.
1219 	 */
1220 	dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
1221 
1222 	ret = dwc3_phy_setup(dwc);
1223 	if (ret)
1224 		return ret;
1225 
1226 	if (!dwc->ulpi_ready) {
1227 		ret = dwc3_core_ulpi_init(dwc);
1228 		if (ret) {
1229 			if (ret == -ETIMEDOUT) {
1230 				dwc3_core_soft_reset(dwc);
1231 				ret = -EPROBE_DEFER;
1232 			}
1233 			return ret;
1234 		}
1235 		dwc->ulpi_ready = true;
1236 	}
1237 
1238 	if (!dwc->phys_ready) {
1239 		ret = dwc3_core_get_phy(dwc);
1240 		if (ret)
1241 			goto err_exit_ulpi;
1242 		dwc->phys_ready = true;
1243 	}
1244 
1245 	ret = dwc3_phy_init(dwc);
1246 	if (ret)
1247 		goto err_exit_ulpi;
1248 
1249 	ret = dwc3_core_soft_reset(dwc);
1250 	if (ret)
1251 		goto err_exit_phy;
1252 
1253 	if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD &&
1254 	    !DWC3_VER_IS_WITHIN(DWC3, ANY, 194A)) {
1255 		if (!dwc->dis_u3_susphy_quirk) {
1256 			reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1257 			reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1258 			dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1259 		}
1260 
1261 		if (!dwc->dis_u2_susphy_quirk) {
1262 			reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1263 			reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1264 			dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1265 		}
1266 	}
1267 
1268 	dwc3_core_setup_global_control(dwc);
1269 	dwc3_core_num_eps(dwc);
1270 
1271 	/* Set power down scale of suspend_clk */
1272 	dwc3_set_power_down_clk_scale(dwc);
1273 
1274 	/* Adjust Frame Length */
1275 	dwc3_frame_length_adjustment(dwc);
1276 
1277 	/* Adjust Reference Clock Period */
1278 	dwc3_ref_clk_period(dwc);
1279 
1280 	dwc3_set_incr_burst_type(dwc);
1281 
1282 	ret = dwc3_phy_power_on(dwc);
1283 	if (ret)
1284 		goto err_exit_phy;
1285 
1286 	ret = dwc3_event_buffers_setup(dwc);
1287 	if (ret) {
1288 		dev_err(dwc->dev, "failed to setup event buffers\n");
1289 		goto err_power_off_phy;
1290 	}
1291 
1292 	/*
1293 	 * ENDXFER polling is available on version 3.10a and later of
1294 	 * the DWC_usb3 controller. It is NOT available in the
1295 	 * DWC_usb31 controller.
1296 	 */
1297 	if (DWC3_VER_IS_WITHIN(DWC3, 310A, ANY)) {
1298 		reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
1299 		reg |= DWC3_GUCTL2_RST_ACTBITLATER;
1300 		dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
1301 	}
1302 
1303 	/*
1304 	 * When configured in HOST mode, after issuing U3/L2 exit controller
1305 	 * fails to send proper CRC checksum in CRC5 feild. Because of this
1306 	 * behaviour Transaction Error is generated, resulting in reset and
1307 	 * re-enumeration of usb device attached. All the termsel, xcvrsel,
1308 	 * opmode becomes 0 during end of resume. Enabling bit 10 of GUCTL1
1309 	 * will correct this problem. This option is to support certain
1310 	 * legacy ULPI PHYs.
1311 	 */
1312 	if (dwc->resume_hs_terminations) {
1313 		reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1314 		reg |= DWC3_GUCTL1_RESUME_OPMODE_HS_HOST;
1315 		dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1316 	}
1317 
1318 	if (!DWC3_VER_IS_PRIOR(DWC3, 250A)) {
1319 		reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1320 
1321 		/*
1322 		 * Enable hardware control of sending remote wakeup
1323 		 * in HS when the device is in the L1 state.
1324 		 */
1325 		if (!DWC3_VER_IS_PRIOR(DWC3, 290A))
1326 			reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
1327 
1328 		/*
1329 		 * Decouple USB 2.0 L1 & L2 events which will allow for
1330 		 * gadget driver to only receive U3/L2 suspend & wakeup
1331 		 * events and prevent the more frequent L1 LPM transitions
1332 		 * from interrupting the driver.
1333 		 */
1334 		if (!DWC3_VER_IS_PRIOR(DWC3, 300A))
1335 			reg |= DWC3_GUCTL1_DEV_DECOUPLE_L1L2_EVT;
1336 
1337 		if (dwc->dis_tx_ipgap_linecheck_quirk)
1338 			reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
1339 
1340 		if (dwc->parkmode_disable_ss_quirk)
1341 			reg |= DWC3_GUCTL1_PARKMODE_DISABLE_SS;
1342 
1343 		if (dwc->parkmode_disable_hs_quirk)
1344 			reg |= DWC3_GUCTL1_PARKMODE_DISABLE_HS;
1345 
1346 		if (DWC3_VER_IS_WITHIN(DWC3, 290A, ANY) &&
1347 		    (dwc->maximum_speed == USB_SPEED_HIGH ||
1348 		     dwc->maximum_speed == USB_SPEED_FULL))
1349 			reg |= DWC3_GUCTL1_DEV_FORCE_20_CLK_FOR_30_CLK;
1350 
1351 		dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1352 	}
1353 
1354 	dwc3_config_threshold(dwc);
1355 
1356 	return 0;
1357 
1358 err_power_off_phy:
1359 	dwc3_phy_power_off(dwc);
1360 err_exit_phy:
1361 	dwc3_phy_exit(dwc);
1362 err_exit_ulpi:
1363 	dwc3_ulpi_exit(dwc);
1364 
1365 	return ret;
1366 }
1367 
1368 static int dwc3_core_get_phy(struct dwc3 *dwc)
1369 {
1370 	struct device		*dev = dwc->dev;
1371 	struct device_node	*node = dev->of_node;
1372 	int ret;
1373 
1374 	if (node) {
1375 		dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
1376 		dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
1377 	} else {
1378 		dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1379 		dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
1380 	}
1381 
1382 	if (IS_ERR(dwc->usb2_phy)) {
1383 		ret = PTR_ERR(dwc->usb2_phy);
1384 		if (ret == -ENXIO || ret == -ENODEV)
1385 			dwc->usb2_phy = NULL;
1386 		else
1387 			return dev_err_probe(dev, ret, "no usb2 phy configured\n");
1388 	}
1389 
1390 	if (IS_ERR(dwc->usb3_phy)) {
1391 		ret = PTR_ERR(dwc->usb3_phy);
1392 		if (ret == -ENXIO || ret == -ENODEV)
1393 			dwc->usb3_phy = NULL;
1394 		else
1395 			return dev_err_probe(dev, ret, "no usb3 phy configured\n");
1396 	}
1397 
1398 	dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
1399 	if (IS_ERR(dwc->usb2_generic_phy)) {
1400 		ret = PTR_ERR(dwc->usb2_generic_phy);
1401 		if (ret == -ENOSYS || ret == -ENODEV)
1402 			dwc->usb2_generic_phy = NULL;
1403 		else
1404 			return dev_err_probe(dev, ret, "no usb2 phy configured\n");
1405 	}
1406 
1407 	dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
1408 	if (IS_ERR(dwc->usb3_generic_phy)) {
1409 		ret = PTR_ERR(dwc->usb3_generic_phy);
1410 		if (ret == -ENOSYS || ret == -ENODEV)
1411 			dwc->usb3_generic_phy = NULL;
1412 		else
1413 			return dev_err_probe(dev, ret, "no usb3 phy configured\n");
1414 	}
1415 
1416 	return 0;
1417 }
1418 
1419 static int dwc3_core_init_mode(struct dwc3 *dwc)
1420 {
1421 	struct device *dev = dwc->dev;
1422 	int ret;
1423 
1424 	switch (dwc->dr_mode) {
1425 	case USB_DR_MODE_PERIPHERAL:
1426 		dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1427 
1428 		if (dwc->usb2_phy)
1429 			otg_set_vbus(dwc->usb2_phy->otg, false);
1430 		phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
1431 		phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
1432 
1433 		ret = dwc3_gadget_init(dwc);
1434 		if (ret)
1435 			return dev_err_probe(dev, ret, "failed to initialize gadget\n");
1436 		break;
1437 	case USB_DR_MODE_HOST:
1438 		dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
1439 
1440 		if (dwc->usb2_phy)
1441 			otg_set_vbus(dwc->usb2_phy->otg, true);
1442 		phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
1443 		phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
1444 
1445 		ret = dwc3_host_init(dwc);
1446 		if (ret)
1447 			return dev_err_probe(dev, ret, "failed to initialize host\n");
1448 		break;
1449 	case USB_DR_MODE_OTG:
1450 		INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
1451 		ret = dwc3_drd_init(dwc);
1452 		if (ret)
1453 			return dev_err_probe(dev, ret, "failed to initialize dual-role\n");
1454 		break;
1455 	default:
1456 		dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1457 		return -EINVAL;
1458 	}
1459 
1460 	return 0;
1461 }
1462 
1463 static void dwc3_core_exit_mode(struct dwc3 *dwc)
1464 {
1465 	switch (dwc->dr_mode) {
1466 	case USB_DR_MODE_PERIPHERAL:
1467 		dwc3_gadget_exit(dwc);
1468 		break;
1469 	case USB_DR_MODE_HOST:
1470 		dwc3_host_exit(dwc);
1471 		break;
1472 	case USB_DR_MODE_OTG:
1473 		dwc3_drd_exit(dwc);
1474 		break;
1475 	default:
1476 		/* do nothing */
1477 		break;
1478 	}
1479 
1480 	/* de-assert DRVVBUS for HOST and OTG mode */
1481 	dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
1482 }
1483 
1484 static void dwc3_get_properties(struct dwc3 *dwc)
1485 {
1486 	struct device		*dev = dwc->dev;
1487 	u8			lpm_nyet_threshold;
1488 	u8			tx_de_emphasis;
1489 	u8			hird_threshold;
1490 	u8			rx_thr_num_pkt = 0;
1491 	u8			rx_max_burst = 0;
1492 	u8			tx_thr_num_pkt = 0;
1493 	u8			tx_max_burst = 0;
1494 	u8			rx_thr_num_pkt_prd = 0;
1495 	u8			rx_max_burst_prd = 0;
1496 	u8			tx_thr_num_pkt_prd = 0;
1497 	u8			tx_max_burst_prd = 0;
1498 	u8			tx_fifo_resize_max_num;
1499 	const char		*usb_psy_name;
1500 	int			ret;
1501 
1502 	/* default to highest possible threshold */
1503 	lpm_nyet_threshold = 0xf;
1504 
1505 	/* default to -3.5dB de-emphasis */
1506 	tx_de_emphasis = 1;
1507 
1508 	/*
1509 	 * default to assert utmi_sleep_n and use maximum allowed HIRD
1510 	 * threshold value of 0b1100
1511 	 */
1512 	hird_threshold = 12;
1513 
1514 	/*
1515 	 * default to a TXFIFO size large enough to fit 6 max packets.  This
1516 	 * allows for systems with larger bus latencies to have some headroom
1517 	 * for endpoints that have a large bMaxBurst value.
1518 	 */
1519 	tx_fifo_resize_max_num = 6;
1520 
1521 	dwc->maximum_speed = usb_get_maximum_speed(dev);
1522 	dwc->max_ssp_rate = usb_get_maximum_ssp_rate(dev);
1523 	dwc->dr_mode = usb_get_dr_mode(dev);
1524 	dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
1525 
1526 	dwc->sysdev_is_parent = device_property_read_bool(dev,
1527 				"linux,sysdev_is_parent");
1528 	if (dwc->sysdev_is_parent)
1529 		dwc->sysdev = dwc->dev->parent;
1530 	else
1531 		dwc->sysdev = dwc->dev;
1532 
1533 	ret = device_property_read_string(dev, "usb-psy-name", &usb_psy_name);
1534 	if (ret >= 0) {
1535 		dwc->usb_psy = power_supply_get_by_name(usb_psy_name);
1536 		if (!dwc->usb_psy)
1537 			dev_err(dev, "couldn't get usb power supply\n");
1538 	}
1539 
1540 	dwc->has_lpm_erratum = device_property_read_bool(dev,
1541 				"snps,has-lpm-erratum");
1542 	device_property_read_u8(dev, "snps,lpm-nyet-threshold",
1543 				&lpm_nyet_threshold);
1544 	dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
1545 				"snps,is-utmi-l1-suspend");
1546 	device_property_read_u8(dev, "snps,hird-threshold",
1547 				&hird_threshold);
1548 	dwc->dis_start_transfer_quirk = device_property_read_bool(dev,
1549 				"snps,dis-start-transfer-quirk");
1550 	dwc->usb3_lpm_capable = device_property_read_bool(dev,
1551 				"snps,usb3_lpm_capable");
1552 	dwc->usb2_lpm_disable = device_property_read_bool(dev,
1553 				"snps,usb2-lpm-disable");
1554 	dwc->usb2_gadget_lpm_disable = device_property_read_bool(dev,
1555 				"snps,usb2-gadget-lpm-disable");
1556 	device_property_read_u8(dev, "snps,rx-thr-num-pkt",
1557 				&rx_thr_num_pkt);
1558 	device_property_read_u8(dev, "snps,rx-max-burst",
1559 				&rx_max_burst);
1560 	device_property_read_u8(dev, "snps,tx-thr-num-pkt",
1561 				&tx_thr_num_pkt);
1562 	device_property_read_u8(dev, "snps,tx-max-burst",
1563 				&tx_max_burst);
1564 	device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
1565 				&rx_thr_num_pkt_prd);
1566 	device_property_read_u8(dev, "snps,rx-max-burst-prd",
1567 				&rx_max_burst_prd);
1568 	device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
1569 				&tx_thr_num_pkt_prd);
1570 	device_property_read_u8(dev, "snps,tx-max-burst-prd",
1571 				&tx_max_burst_prd);
1572 	dwc->do_fifo_resize = device_property_read_bool(dev,
1573 							"tx-fifo-resize");
1574 	if (dwc->do_fifo_resize)
1575 		device_property_read_u8(dev, "tx-fifo-max-num",
1576 					&tx_fifo_resize_max_num);
1577 
1578 	dwc->disable_scramble_quirk = device_property_read_bool(dev,
1579 				"snps,disable_scramble_quirk");
1580 	dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
1581 				"snps,u2exit_lfps_quirk");
1582 	dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
1583 				"snps,u2ss_inp3_quirk");
1584 	dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
1585 				"snps,req_p1p2p3_quirk");
1586 	dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
1587 				"snps,del_p1p2p3_quirk");
1588 	dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
1589 				"snps,del_phy_power_chg_quirk");
1590 	dwc->lfps_filter_quirk = device_property_read_bool(dev,
1591 				"snps,lfps_filter_quirk");
1592 	dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
1593 				"snps,rx_detect_poll_quirk");
1594 	dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
1595 				"snps,dis_u3_susphy_quirk");
1596 	dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
1597 				"snps,dis_u2_susphy_quirk");
1598 	dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1599 				"snps,dis_enblslpm_quirk");
1600 	dwc->dis_u1_entry_quirk = device_property_read_bool(dev,
1601 				"snps,dis-u1-entry-quirk");
1602 	dwc->dis_u2_entry_quirk = device_property_read_bool(dev,
1603 				"snps,dis-u2-entry-quirk");
1604 	dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1605 				"snps,dis_rxdet_inp3_quirk");
1606 	dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1607 				"snps,dis-u2-freeclk-exists-quirk");
1608 	dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1609 				"snps,dis-del-phy-power-chg-quirk");
1610 	dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1611 				"snps,dis-tx-ipgap-linecheck-quirk");
1612 	dwc->resume_hs_terminations = device_property_read_bool(dev,
1613 				"snps,resume-hs-terminations");
1614 	dwc->ulpi_ext_vbus_drv = device_property_read_bool(dev,
1615 				"snps,ulpi-ext-vbus-drv");
1616 	dwc->parkmode_disable_ss_quirk = device_property_read_bool(dev,
1617 				"snps,parkmode-disable-ss-quirk");
1618 	dwc->parkmode_disable_hs_quirk = device_property_read_bool(dev,
1619 				"snps,parkmode-disable-hs-quirk");
1620 	dwc->gfladj_refclk_lpm_sel = device_property_read_bool(dev,
1621 				"snps,gfladj-refclk-lpm-sel-quirk");
1622 
1623 	dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
1624 				"snps,tx_de_emphasis_quirk");
1625 	device_property_read_u8(dev, "snps,tx_de_emphasis",
1626 				&tx_de_emphasis);
1627 	device_property_read_string(dev, "snps,hsphy_interface",
1628 				    &dwc->hsphy_interface);
1629 	device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
1630 				 &dwc->fladj);
1631 	device_property_read_u32(dev, "snps,ref-clock-period-ns",
1632 				 &dwc->ref_clk_per);
1633 
1634 	dwc->dis_metastability_quirk = device_property_read_bool(dev,
1635 				"snps,dis_metastability_quirk");
1636 
1637 	dwc->dis_split_quirk = device_property_read_bool(dev,
1638 				"snps,dis-split-quirk");
1639 
1640 	dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1641 	dwc->tx_de_emphasis = tx_de_emphasis;
1642 
1643 	dwc->hird_threshold = hird_threshold;
1644 
1645 	dwc->rx_thr_num_pkt = rx_thr_num_pkt;
1646 	dwc->rx_max_burst = rx_max_burst;
1647 
1648 	dwc->tx_thr_num_pkt = tx_thr_num_pkt;
1649 	dwc->tx_max_burst = tx_max_burst;
1650 
1651 	dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd;
1652 	dwc->rx_max_burst_prd = rx_max_burst_prd;
1653 
1654 	dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd;
1655 	dwc->tx_max_burst_prd = tx_max_burst_prd;
1656 
1657 	dwc->imod_interval = 0;
1658 
1659 	dwc->tx_fifo_resize_max_num = tx_fifo_resize_max_num;
1660 }
1661 
1662 /* check whether the core supports IMOD */
1663 bool dwc3_has_imod(struct dwc3 *dwc)
1664 {
1665 	return DWC3_VER_IS_WITHIN(DWC3, 300A, ANY) ||
1666 		DWC3_VER_IS_WITHIN(DWC31, 120A, ANY) ||
1667 		DWC3_IP_IS(DWC32);
1668 }
1669 
1670 static void dwc3_check_params(struct dwc3 *dwc)
1671 {
1672 	struct device *dev = dwc->dev;
1673 	unsigned int hwparam_gen =
1674 		DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3);
1675 
1676 	/* Check for proper value of imod_interval */
1677 	if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
1678 		dev_warn(dwc->dev, "Interrupt moderation not supported\n");
1679 		dwc->imod_interval = 0;
1680 	}
1681 
1682 	/*
1683 	 * Workaround for STAR 9000961433 which affects only version
1684 	 * 3.00a of the DWC_usb3 core. This prevents the controller
1685 	 * interrupt from being masked while handling events. IMOD
1686 	 * allows us to work around this issue. Enable it for the
1687 	 * affected version.
1688 	 */
1689 	if (!dwc->imod_interval &&
1690 	    DWC3_VER_IS(DWC3, 300A))
1691 		dwc->imod_interval = 1;
1692 
1693 	/* Check the maximum_speed parameter */
1694 	switch (dwc->maximum_speed) {
1695 	case USB_SPEED_FULL:
1696 	case USB_SPEED_HIGH:
1697 		break;
1698 	case USB_SPEED_SUPER:
1699 		if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS)
1700 			dev_warn(dev, "UDC doesn't support Gen 1\n");
1701 		break;
1702 	case USB_SPEED_SUPER_PLUS:
1703 		if ((DWC3_IP_IS(DWC32) &&
1704 		     hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS) ||
1705 		    (!DWC3_IP_IS(DWC32) &&
1706 		     hwparam_gen != DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1707 			dev_warn(dev, "UDC doesn't support SSP\n");
1708 		break;
1709 	default:
1710 		dev_err(dev, "invalid maximum_speed parameter %d\n",
1711 			dwc->maximum_speed);
1712 		fallthrough;
1713 	case USB_SPEED_UNKNOWN:
1714 		switch (hwparam_gen) {
1715 		case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
1716 			dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1717 			break;
1718 		case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
1719 			if (DWC3_IP_IS(DWC32))
1720 				dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1721 			else
1722 				dwc->maximum_speed = USB_SPEED_SUPER;
1723 			break;
1724 		case DWC3_GHWPARAMS3_SSPHY_IFC_DIS:
1725 			dwc->maximum_speed = USB_SPEED_HIGH;
1726 			break;
1727 		default:
1728 			dwc->maximum_speed = USB_SPEED_SUPER;
1729 			break;
1730 		}
1731 		break;
1732 	}
1733 
1734 	/*
1735 	 * Currently the controller does not have visibility into the HW
1736 	 * parameter to determine the maximum number of lanes the HW supports.
1737 	 * If the number of lanes is not specified in the device property, then
1738 	 * set the default to support dual-lane for DWC_usb32 and single-lane
1739 	 * for DWC_usb31 for super-speed-plus.
1740 	 */
1741 	if (dwc->maximum_speed == USB_SPEED_SUPER_PLUS) {
1742 		switch (dwc->max_ssp_rate) {
1743 		case USB_SSP_GEN_2x1:
1744 			if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_GEN1)
1745 				dev_warn(dev, "UDC only supports Gen 1\n");
1746 			break;
1747 		case USB_SSP_GEN_1x2:
1748 		case USB_SSP_GEN_2x2:
1749 			if (DWC3_IP_IS(DWC31))
1750 				dev_warn(dev, "UDC only supports single lane\n");
1751 			break;
1752 		case USB_SSP_GEN_UNKNOWN:
1753 		default:
1754 			switch (hwparam_gen) {
1755 			case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
1756 				if (DWC3_IP_IS(DWC32))
1757 					dwc->max_ssp_rate = USB_SSP_GEN_2x2;
1758 				else
1759 					dwc->max_ssp_rate = USB_SSP_GEN_2x1;
1760 				break;
1761 			case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
1762 				if (DWC3_IP_IS(DWC32))
1763 					dwc->max_ssp_rate = USB_SSP_GEN_1x2;
1764 				break;
1765 			}
1766 			break;
1767 		}
1768 	}
1769 }
1770 
1771 static struct extcon_dev *dwc3_get_extcon(struct dwc3 *dwc)
1772 {
1773 	struct device *dev = dwc->dev;
1774 	struct device_node *np_phy;
1775 	struct extcon_dev *edev = NULL;
1776 	const char *name;
1777 
1778 	if (device_property_read_bool(dev, "extcon"))
1779 		return extcon_get_edev_by_phandle(dev, 0);
1780 
1781 	/*
1782 	 * Device tree platforms should get extcon via phandle.
1783 	 * On ACPI platforms, we get the name from a device property.
1784 	 * This device property is for kernel internal use only and
1785 	 * is expected to be set by the glue code.
1786 	 */
1787 	if (device_property_read_string(dev, "linux,extcon-name", &name) == 0)
1788 		return extcon_get_extcon_dev(name);
1789 
1790 	/*
1791 	 * Check explicitly if "usb-role-switch" is used since
1792 	 * extcon_find_edev_by_node() can not be used to check the absence of
1793 	 * an extcon device. In the absence of an device it will always return
1794 	 * EPROBE_DEFER.
1795 	 */
1796 	if (IS_ENABLED(CONFIG_USB_ROLE_SWITCH) &&
1797 	    device_property_read_bool(dev, "usb-role-switch"))
1798 		return NULL;
1799 
1800 	/*
1801 	 * Try to get an extcon device from the USB PHY controller's "port"
1802 	 * node. Check if it has the "port" node first, to avoid printing the
1803 	 * error message from underlying code, as it's a valid case: extcon
1804 	 * device (and "port" node) may be missing in case of "usb-role-switch"
1805 	 * or OTG mode.
1806 	 */
1807 	np_phy = of_parse_phandle(dev->of_node, "phys", 0);
1808 	if (of_graph_is_present(np_phy)) {
1809 		struct device_node *np_conn;
1810 
1811 		np_conn = of_graph_get_remote_node(np_phy, -1, -1);
1812 		if (np_conn)
1813 			edev = extcon_find_edev_by_node(np_conn);
1814 		of_node_put(np_conn);
1815 	}
1816 	of_node_put(np_phy);
1817 
1818 	return edev;
1819 }
1820 
1821 static int dwc3_get_clocks(struct dwc3 *dwc)
1822 {
1823 	struct device *dev = dwc->dev;
1824 
1825 	if (!dev->of_node)
1826 		return 0;
1827 
1828 	/*
1829 	 * Clocks are optional, but new DT platforms should support all clocks
1830 	 * as required by the DT-binding.
1831 	 * Some devices have different clock names in legacy device trees,
1832 	 * check for them to retain backwards compatibility.
1833 	 */
1834 	dwc->bus_clk = devm_clk_get_optional(dev, "bus_early");
1835 	if (IS_ERR(dwc->bus_clk)) {
1836 		return dev_err_probe(dev, PTR_ERR(dwc->bus_clk),
1837 				"could not get bus clock\n");
1838 	}
1839 
1840 	if (dwc->bus_clk == NULL) {
1841 		dwc->bus_clk = devm_clk_get_optional(dev, "bus_clk");
1842 		if (IS_ERR(dwc->bus_clk)) {
1843 			return dev_err_probe(dev, PTR_ERR(dwc->bus_clk),
1844 					"could not get bus clock\n");
1845 		}
1846 	}
1847 
1848 	dwc->ref_clk = devm_clk_get_optional(dev, "ref");
1849 	if (IS_ERR(dwc->ref_clk)) {
1850 		return dev_err_probe(dev, PTR_ERR(dwc->ref_clk),
1851 				"could not get ref clock\n");
1852 	}
1853 
1854 	if (dwc->ref_clk == NULL) {
1855 		dwc->ref_clk = devm_clk_get_optional(dev, "ref_clk");
1856 		if (IS_ERR(dwc->ref_clk)) {
1857 			return dev_err_probe(dev, PTR_ERR(dwc->ref_clk),
1858 					"could not get ref clock\n");
1859 		}
1860 	}
1861 
1862 	dwc->susp_clk = devm_clk_get_optional(dev, "suspend");
1863 	if (IS_ERR(dwc->susp_clk)) {
1864 		return dev_err_probe(dev, PTR_ERR(dwc->susp_clk),
1865 				"could not get suspend clock\n");
1866 	}
1867 
1868 	if (dwc->susp_clk == NULL) {
1869 		dwc->susp_clk = devm_clk_get_optional(dev, "suspend_clk");
1870 		if (IS_ERR(dwc->susp_clk)) {
1871 			return dev_err_probe(dev, PTR_ERR(dwc->susp_clk),
1872 					"could not get suspend clock\n");
1873 		}
1874 	}
1875 
1876 	return 0;
1877 }
1878 
1879 static int dwc3_probe(struct platform_device *pdev)
1880 {
1881 	struct device		*dev = &pdev->dev;
1882 	struct resource		*res, dwc_res;
1883 	void __iomem		*regs;
1884 	struct dwc3		*dwc;
1885 	int			ret;
1886 
1887 	dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
1888 	if (!dwc)
1889 		return -ENOMEM;
1890 
1891 	dwc->dev = dev;
1892 
1893 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1894 	if (!res) {
1895 		dev_err(dev, "missing memory resource\n");
1896 		return -ENODEV;
1897 	}
1898 
1899 	dwc->xhci_resources[0].start = res->start;
1900 	dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1901 					DWC3_XHCI_REGS_END;
1902 	dwc->xhci_resources[0].flags = res->flags;
1903 	dwc->xhci_resources[0].name = res->name;
1904 
1905 	/*
1906 	 * Request memory region but exclude xHCI regs,
1907 	 * since it will be requested by the xhci-plat driver.
1908 	 */
1909 	dwc_res = *res;
1910 	dwc_res.start += DWC3_GLOBALS_REGS_START;
1911 
1912 	if (dev->of_node) {
1913 		struct device_node *parent = of_get_parent(dev->of_node);
1914 
1915 		if (of_device_is_compatible(parent, "realtek,rtd-dwc3")) {
1916 			dwc_res.start -= DWC3_GLOBALS_REGS_START;
1917 			dwc_res.start += DWC3_RTK_RTD_GLOBALS_REGS_START;
1918 		}
1919 
1920 		of_node_put(parent);
1921 	}
1922 
1923 	regs = devm_ioremap_resource(dev, &dwc_res);
1924 	if (IS_ERR(regs))
1925 		return PTR_ERR(regs);
1926 
1927 	dwc->regs	= regs;
1928 	dwc->regs_size	= resource_size(&dwc_res);
1929 
1930 	dwc3_get_properties(dwc);
1931 
1932 	dwc->reset = devm_reset_control_array_get_optional_shared(dev);
1933 	if (IS_ERR(dwc->reset)) {
1934 		ret = PTR_ERR(dwc->reset);
1935 		goto err_put_psy;
1936 	}
1937 
1938 	ret = dwc3_get_clocks(dwc);
1939 	if (ret)
1940 		goto err_put_psy;
1941 
1942 	ret = reset_control_deassert(dwc->reset);
1943 	if (ret)
1944 		goto err_put_psy;
1945 
1946 	ret = dwc3_clk_enable(dwc);
1947 	if (ret)
1948 		goto err_assert_reset;
1949 
1950 	if (!dwc3_core_is_valid(dwc)) {
1951 		dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
1952 		ret = -ENODEV;
1953 		goto err_disable_clks;
1954 	}
1955 
1956 	platform_set_drvdata(pdev, dwc);
1957 	dwc3_cache_hwparams(dwc);
1958 
1959 	if (!dwc->sysdev_is_parent &&
1960 	    DWC3_GHWPARAMS0_AWIDTH(dwc->hwparams.hwparams0) == 64) {
1961 		ret = dma_set_mask_and_coherent(dwc->sysdev, DMA_BIT_MASK(64));
1962 		if (ret)
1963 			goto err_disable_clks;
1964 	}
1965 
1966 	spin_lock_init(&dwc->lock);
1967 	mutex_init(&dwc->mutex);
1968 
1969 	pm_runtime_get_noresume(dev);
1970 	pm_runtime_set_active(dev);
1971 	pm_runtime_use_autosuspend(dev);
1972 	pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
1973 	pm_runtime_enable(dev);
1974 
1975 	pm_runtime_forbid(dev);
1976 
1977 	ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1978 	if (ret) {
1979 		dev_err(dwc->dev, "failed to allocate event buffers\n");
1980 		ret = -ENOMEM;
1981 		goto err_allow_rpm;
1982 	}
1983 
1984 	dwc->edev = dwc3_get_extcon(dwc);
1985 	if (IS_ERR(dwc->edev)) {
1986 		ret = dev_err_probe(dwc->dev, PTR_ERR(dwc->edev), "failed to get extcon\n");
1987 		goto err_free_event_buffers;
1988 	}
1989 
1990 	ret = dwc3_get_dr_mode(dwc);
1991 	if (ret)
1992 		goto err_free_event_buffers;
1993 
1994 	ret = dwc3_core_init(dwc);
1995 	if (ret) {
1996 		dev_err_probe(dev, ret, "failed to initialize core\n");
1997 		goto err_free_event_buffers;
1998 	}
1999 
2000 	dwc3_check_params(dwc);
2001 	dwc3_debugfs_init(dwc);
2002 
2003 	ret = dwc3_core_init_mode(dwc);
2004 	if (ret)
2005 		goto err_exit_debugfs;
2006 
2007 	pm_runtime_put(dev);
2008 
2009 	dma_set_max_seg_size(dev, UINT_MAX);
2010 
2011 	return 0;
2012 
2013 err_exit_debugfs:
2014 	dwc3_debugfs_exit(dwc);
2015 	dwc3_event_buffers_cleanup(dwc);
2016 	dwc3_phy_power_off(dwc);
2017 	dwc3_phy_exit(dwc);
2018 	dwc3_ulpi_exit(dwc);
2019 err_free_event_buffers:
2020 	dwc3_free_event_buffers(dwc);
2021 err_allow_rpm:
2022 	pm_runtime_allow(dev);
2023 	pm_runtime_disable(dev);
2024 	pm_runtime_dont_use_autosuspend(dev);
2025 	pm_runtime_set_suspended(dev);
2026 	pm_runtime_put_noidle(dev);
2027 err_disable_clks:
2028 	dwc3_clk_disable(dwc);
2029 err_assert_reset:
2030 	reset_control_assert(dwc->reset);
2031 err_put_psy:
2032 	if (dwc->usb_psy)
2033 		power_supply_put(dwc->usb_psy);
2034 
2035 	return ret;
2036 }
2037 
2038 static void dwc3_remove(struct platform_device *pdev)
2039 {
2040 	struct dwc3	*dwc = platform_get_drvdata(pdev);
2041 
2042 	pm_runtime_get_sync(&pdev->dev);
2043 
2044 	dwc3_core_exit_mode(dwc);
2045 	dwc3_debugfs_exit(dwc);
2046 
2047 	dwc3_core_exit(dwc);
2048 	dwc3_ulpi_exit(dwc);
2049 
2050 	pm_runtime_allow(&pdev->dev);
2051 	pm_runtime_disable(&pdev->dev);
2052 	pm_runtime_dont_use_autosuspend(&pdev->dev);
2053 	pm_runtime_put_noidle(&pdev->dev);
2054 	/*
2055 	 * HACK: Clear the driver data, which is currently accessed by parent
2056 	 * glue drivers, before allowing the parent to suspend.
2057 	 */
2058 	platform_set_drvdata(pdev, NULL);
2059 	pm_runtime_set_suspended(&pdev->dev);
2060 
2061 	dwc3_free_event_buffers(dwc);
2062 
2063 	if (dwc->usb_psy)
2064 		power_supply_put(dwc->usb_psy);
2065 }
2066 
2067 #ifdef CONFIG_PM
2068 static int dwc3_core_init_for_resume(struct dwc3 *dwc)
2069 {
2070 	int ret;
2071 
2072 	ret = reset_control_deassert(dwc->reset);
2073 	if (ret)
2074 		return ret;
2075 
2076 	ret = dwc3_clk_enable(dwc);
2077 	if (ret)
2078 		goto assert_reset;
2079 
2080 	ret = dwc3_core_init(dwc);
2081 	if (ret)
2082 		goto disable_clks;
2083 
2084 	return 0;
2085 
2086 disable_clks:
2087 	dwc3_clk_disable(dwc);
2088 assert_reset:
2089 	reset_control_assert(dwc->reset);
2090 
2091 	return ret;
2092 }
2093 
2094 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
2095 {
2096 	unsigned long	flags;
2097 	u32 reg;
2098 
2099 	switch (dwc->current_dr_role) {
2100 	case DWC3_GCTL_PRTCAP_DEVICE:
2101 		if (pm_runtime_suspended(dwc->dev))
2102 			break;
2103 		dwc3_gadget_suspend(dwc);
2104 		synchronize_irq(dwc->irq_gadget);
2105 		dwc3_core_exit(dwc);
2106 		break;
2107 	case DWC3_GCTL_PRTCAP_HOST:
2108 		if (!PMSG_IS_AUTO(msg) && !device_may_wakeup(dwc->dev)) {
2109 			dwc3_core_exit(dwc);
2110 			break;
2111 		}
2112 
2113 		/* Let controller to suspend HSPHY before PHY driver suspends */
2114 		if (dwc->dis_u2_susphy_quirk ||
2115 		    dwc->dis_enblslpm_quirk) {
2116 			reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2117 			reg |=  DWC3_GUSB2PHYCFG_ENBLSLPM |
2118 				DWC3_GUSB2PHYCFG_SUSPHY;
2119 			dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2120 
2121 			/* Give some time for USB2 PHY to suspend */
2122 			usleep_range(5000, 6000);
2123 		}
2124 
2125 		phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
2126 		phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
2127 		break;
2128 	case DWC3_GCTL_PRTCAP_OTG:
2129 		/* do nothing during runtime_suspend */
2130 		if (PMSG_IS_AUTO(msg))
2131 			break;
2132 
2133 		if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
2134 			spin_lock_irqsave(&dwc->lock, flags);
2135 			dwc3_gadget_suspend(dwc);
2136 			spin_unlock_irqrestore(&dwc->lock, flags);
2137 			synchronize_irq(dwc->irq_gadget);
2138 		}
2139 
2140 		dwc3_otg_exit(dwc);
2141 		dwc3_core_exit(dwc);
2142 		break;
2143 	default:
2144 		/* do nothing */
2145 		break;
2146 	}
2147 
2148 	return 0;
2149 }
2150 
2151 static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
2152 {
2153 	unsigned long	flags;
2154 	int		ret;
2155 	u32		reg;
2156 
2157 	switch (dwc->current_dr_role) {
2158 	case DWC3_GCTL_PRTCAP_DEVICE:
2159 		ret = dwc3_core_init_for_resume(dwc);
2160 		if (ret)
2161 			return ret;
2162 
2163 		dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
2164 		dwc3_gadget_resume(dwc);
2165 		break;
2166 	case DWC3_GCTL_PRTCAP_HOST:
2167 		if (!PMSG_IS_AUTO(msg) && !device_may_wakeup(dwc->dev)) {
2168 			ret = dwc3_core_init_for_resume(dwc);
2169 			if (ret)
2170 				return ret;
2171 			dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
2172 			break;
2173 		}
2174 		/* Restore GUSB2PHYCFG bits that were modified in suspend */
2175 		reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2176 		if (dwc->dis_u2_susphy_quirk)
2177 			reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
2178 
2179 		if (dwc->dis_enblslpm_quirk)
2180 			reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
2181 
2182 		dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2183 
2184 		phy_pm_runtime_get_sync(dwc->usb2_generic_phy);
2185 		phy_pm_runtime_get_sync(dwc->usb3_generic_phy);
2186 		break;
2187 	case DWC3_GCTL_PRTCAP_OTG:
2188 		/* nothing to do on runtime_resume */
2189 		if (PMSG_IS_AUTO(msg))
2190 			break;
2191 
2192 		ret = dwc3_core_init_for_resume(dwc);
2193 		if (ret)
2194 			return ret;
2195 
2196 		dwc3_set_prtcap(dwc, dwc->current_dr_role);
2197 
2198 		dwc3_otg_init(dwc);
2199 		if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
2200 			dwc3_otg_host_init(dwc);
2201 		} else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
2202 			spin_lock_irqsave(&dwc->lock, flags);
2203 			dwc3_gadget_resume(dwc);
2204 			spin_unlock_irqrestore(&dwc->lock, flags);
2205 		}
2206 
2207 		break;
2208 	default:
2209 		/* do nothing */
2210 		break;
2211 	}
2212 
2213 	return 0;
2214 }
2215 
2216 static int dwc3_runtime_checks(struct dwc3 *dwc)
2217 {
2218 	switch (dwc->current_dr_role) {
2219 	case DWC3_GCTL_PRTCAP_DEVICE:
2220 		if (dwc->connected)
2221 			return -EBUSY;
2222 		break;
2223 	case DWC3_GCTL_PRTCAP_HOST:
2224 	default:
2225 		/* do nothing */
2226 		break;
2227 	}
2228 
2229 	return 0;
2230 }
2231 
2232 static int dwc3_runtime_suspend(struct device *dev)
2233 {
2234 	struct dwc3     *dwc = dev_get_drvdata(dev);
2235 	int		ret;
2236 
2237 	if (dwc3_runtime_checks(dwc))
2238 		return -EBUSY;
2239 
2240 	ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
2241 	if (ret)
2242 		return ret;
2243 
2244 	return 0;
2245 }
2246 
2247 static int dwc3_runtime_resume(struct device *dev)
2248 {
2249 	struct dwc3     *dwc = dev_get_drvdata(dev);
2250 	int		ret;
2251 
2252 	ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
2253 	if (ret)
2254 		return ret;
2255 
2256 	switch (dwc->current_dr_role) {
2257 	case DWC3_GCTL_PRTCAP_DEVICE:
2258 		dwc3_gadget_process_pending_events(dwc);
2259 		break;
2260 	case DWC3_GCTL_PRTCAP_HOST:
2261 	default:
2262 		/* do nothing */
2263 		break;
2264 	}
2265 
2266 	pm_runtime_mark_last_busy(dev);
2267 
2268 	return 0;
2269 }
2270 
2271 static int dwc3_runtime_idle(struct device *dev)
2272 {
2273 	struct dwc3     *dwc = dev_get_drvdata(dev);
2274 
2275 	switch (dwc->current_dr_role) {
2276 	case DWC3_GCTL_PRTCAP_DEVICE:
2277 		if (dwc3_runtime_checks(dwc))
2278 			return -EBUSY;
2279 		break;
2280 	case DWC3_GCTL_PRTCAP_HOST:
2281 	default:
2282 		/* do nothing */
2283 		break;
2284 	}
2285 
2286 	pm_runtime_mark_last_busy(dev);
2287 	pm_runtime_autosuspend(dev);
2288 
2289 	return 0;
2290 }
2291 #endif /* CONFIG_PM */
2292 
2293 #ifdef CONFIG_PM_SLEEP
2294 static int dwc3_suspend(struct device *dev)
2295 {
2296 	struct dwc3	*dwc = dev_get_drvdata(dev);
2297 	int		ret;
2298 
2299 	ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
2300 	if (ret)
2301 		return ret;
2302 
2303 	pinctrl_pm_select_sleep_state(dev);
2304 
2305 	return 0;
2306 }
2307 
2308 static int dwc3_resume(struct device *dev)
2309 {
2310 	struct dwc3	*dwc = dev_get_drvdata(dev);
2311 	int		ret;
2312 
2313 	pinctrl_pm_select_default_state(dev);
2314 
2315 	ret = dwc3_resume_common(dwc, PMSG_RESUME);
2316 	if (ret)
2317 		return ret;
2318 
2319 	pm_runtime_disable(dev);
2320 	pm_runtime_set_active(dev);
2321 	pm_runtime_enable(dev);
2322 
2323 	return 0;
2324 }
2325 
2326 static void dwc3_complete(struct device *dev)
2327 {
2328 	struct dwc3	*dwc = dev_get_drvdata(dev);
2329 	u32		reg;
2330 
2331 	if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST &&
2332 			dwc->dis_split_quirk) {
2333 		reg = dwc3_readl(dwc->regs, DWC3_GUCTL3);
2334 		reg |= DWC3_GUCTL3_SPLITDISABLE;
2335 		dwc3_writel(dwc->regs, DWC3_GUCTL3, reg);
2336 	}
2337 }
2338 #else
2339 #define dwc3_complete NULL
2340 #endif /* CONFIG_PM_SLEEP */
2341 
2342 static const struct dev_pm_ops dwc3_dev_pm_ops = {
2343 	SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
2344 	.complete = dwc3_complete,
2345 	SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
2346 			dwc3_runtime_idle)
2347 };
2348 
2349 #ifdef CONFIG_OF
2350 static const struct of_device_id of_dwc3_match[] = {
2351 	{
2352 		.compatible = "snps,dwc3"
2353 	},
2354 	{
2355 		.compatible = "synopsys,dwc3"
2356 	},
2357 	{ },
2358 };
2359 MODULE_DEVICE_TABLE(of, of_dwc3_match);
2360 #endif
2361 
2362 #ifdef CONFIG_ACPI
2363 
2364 #define ACPI_ID_INTEL_BSW	"808622B7"
2365 
2366 static const struct acpi_device_id dwc3_acpi_match[] = {
2367 	{ ACPI_ID_INTEL_BSW, 0 },
2368 	{ },
2369 };
2370 MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
2371 #endif
2372 
2373 static struct platform_driver dwc3_driver = {
2374 	.probe		= dwc3_probe,
2375 	.remove_new	= dwc3_remove,
2376 	.driver		= {
2377 		.name	= "dwc3",
2378 		.of_match_table	= of_match_ptr(of_dwc3_match),
2379 		.acpi_match_table = ACPI_PTR(dwc3_acpi_match),
2380 		.pm	= &dwc3_dev_pm_ops,
2381 	},
2382 };
2383 
2384 module_platform_driver(dwc3_driver);
2385 
2386 MODULE_ALIAS("platform:dwc3");
2387 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
2388 MODULE_LICENSE("GPL v2");
2389 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");
2390