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