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