usb: dwc3: Add disabling of start_transfer failure quirk
[linux-block.git] / drivers / usb / dwc3 / core.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0
72246da4
FB
2/**
3 * core.c - DesignWare USB3 DRD Controller Core file
4 *
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
72246da4
FB
6 *
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
72246da4
FB
9 */
10
fe8abf33 11#include <linux/clk.h>
fa0ea13e 12#include <linux/version.h>
a72e658b 13#include <linux/module.h>
72246da4
FB
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>
457e84b6 25#include <linux/of.h>
404905a6 26#include <linux/acpi.h>
6344475f 27#include <linux/pinctrl/consumer.h>
fe8abf33 28#include <linux/reset.h>
72246da4
FB
29
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
f7e846f0 32#include <linux/usb/of.h>
a45c82b8 33#include <linux/usb/otg.h>
72246da4
FB
34
35#include "core.h"
36#include "gadget.h"
37#include "io.h"
38
39#include "debug.h"
40
fc8bb91b 41#define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
8300dd23 42
9d6173e1
TN
43/**
44 * dwc3_get_dr_mode - Validates and sets dr_mode
45 * @dwc: pointer to our context structure
46 */
47static 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;
a7700468
TN
81
82 /*
89a9cc47
TN
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.
a7700468 86 */
89a9cc47
TN
87 if (mode == USB_DR_MODE_OTG &&
88 dwc->revision >= DWC3_REVISION_330A)
a7700468 89 mode = USB_DR_MODE_PERIPHERAL;
9d6173e1
TN
90 }
91
92 if (mode != dwc->dr_mode) {
93 dev_warn(dev,
94 "Configuration mismatch. dr_mode forced to %s\n",
95 mode == USB_DR_MODE_HOST ? "host" : "gadget");
96
97 dwc->dr_mode = mode;
98 }
99
100 return 0;
101}
102
f09cc79b 103void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
3140e8cb
SAS
104{
105 u32 reg;
106
107 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
108 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
109 reg |= DWC3_GCTL_PRTCAPDIR(mode);
110 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
c4a5153e
MG
111
112 dwc->current_dr_role = mode;
41ce1456
RQ
113}
114
115static void __dwc3_set_mode(struct work_struct *work)
116{
117 struct dwc3 *dwc = work_to_dwc(work);
118 unsigned long flags;
119 int ret;
120
f09cc79b 121 if (dwc->dr_mode != USB_DR_MODE_OTG)
41ce1456
RQ
122 return;
123
f09cc79b
RQ
124 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
125 dwc3_otg_update(dwc, 0);
126
41ce1456
RQ
127 if (!dwc->desired_dr_role)
128 return;
129
130 if (dwc->desired_dr_role == dwc->current_dr_role)
131 return;
132
f09cc79b 133 if (dwc->desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev)
41ce1456
RQ
134 return;
135
136 switch (dwc->current_dr_role) {
137 case DWC3_GCTL_PRTCAP_HOST:
138 dwc3_host_exit(dwc);
139 break;
140 case DWC3_GCTL_PRTCAP_DEVICE:
141 dwc3_gadget_exit(dwc);
142 dwc3_event_buffers_cleanup(dwc);
143 break;
f09cc79b
RQ
144 case DWC3_GCTL_PRTCAP_OTG:
145 dwc3_otg_exit(dwc);
146 spin_lock_irqsave(&dwc->lock, flags);
147 dwc->desired_otg_role = DWC3_OTG_ROLE_IDLE;
148 spin_unlock_irqrestore(&dwc->lock, flags);
149 dwc3_otg_update(dwc, 1);
150 break;
41ce1456
RQ
151 default:
152 break;
153 }
154
155 spin_lock_irqsave(&dwc->lock, flags);
156
157 dwc3_set_prtcap(dwc, dwc->desired_dr_role);
6b3261a2 158
41ce1456
RQ
159 spin_unlock_irqrestore(&dwc->lock, flags);
160
161 switch (dwc->desired_dr_role) {
162 case DWC3_GCTL_PRTCAP_HOST:
163 ret = dwc3_host_init(dwc);
958d1a4c 164 if (ret) {
41ce1456 165 dev_err(dwc->dev, "failed to initialize host\n");
958d1a4c
FB
166 } else {
167 if (dwc->usb2_phy)
168 otg_set_vbus(dwc->usb2_phy->otg, true);
644cbbc3
MG
169 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
170 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
d8c80bb3 171 phy_calibrate(dwc->usb2_generic_phy);
958d1a4c 172 }
41ce1456
RQ
173 break;
174 case DWC3_GCTL_PRTCAP_DEVICE:
175 dwc3_event_buffers_setup(dwc);
958d1a4c
FB
176
177 if (dwc->usb2_phy)
178 otg_set_vbus(dwc->usb2_phy->otg, false);
644cbbc3
MG
179 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
180 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
958d1a4c 181
41ce1456
RQ
182 ret = dwc3_gadget_init(dwc);
183 if (ret)
184 dev_err(dwc->dev, "failed to initialize peripheral\n");
185 break;
f09cc79b
RQ
186 case DWC3_GCTL_PRTCAP_OTG:
187 dwc3_otg_init(dwc);
188 dwc3_otg_update(dwc, 0);
189 break;
41ce1456
RQ
190 default:
191 break;
192 }
f09cc79b 193
41ce1456
RQ
194}
195
196void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
197{
198 unsigned long flags;
199
200 spin_lock_irqsave(&dwc->lock, flags);
201 dwc->desired_dr_role = mode;
202 spin_unlock_irqrestore(&dwc->lock, flags);
203
084a804e 204 queue_work(system_freezable_wq, &dwc->drd_work);
3140e8cb 205}
8300dd23 206
cf6d867d
FB
207u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
208{
209 struct dwc3 *dwc = dep->dwc;
210 u32 reg;
211
212 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
213 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
214 DWC3_GDBGFIFOSPACE_TYPE(type));
215
216 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
217
218 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
219}
220
72246da4
FB
221/**
222 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
223 * @dwc: pointer to our context structure
224 */
57303488 225static int dwc3_core_soft_reset(struct dwc3 *dwc)
72246da4
FB
226{
227 u32 reg;
f59dcab1 228 int retries = 1000;
57303488 229 int ret;
72246da4 230
51e1e7bc
FB
231 usb_phy_init(dwc->usb2_phy);
232 usb_phy_init(dwc->usb3_phy);
57303488
KVA
233 ret = phy_init(dwc->usb2_generic_phy);
234 if (ret < 0)
235 return ret;
236
237 ret = phy_init(dwc->usb3_generic_phy);
238 if (ret < 0) {
239 phy_exit(dwc->usb2_generic_phy);
240 return ret;
241 }
72246da4 242
f59dcab1
FB
243 /*
244 * We're resetting only the device side because, if we're in host mode,
245 * XHCI driver will reset the host block. If dwc3 was configured for
246 * host-only mode, then we can return early.
247 */
c4a5153e 248 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST)
f59dcab1 249 return 0;
72246da4 250
f59dcab1
FB
251 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
252 reg |= DWC3_DCTL_CSFTRST;
253 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
72246da4 254
f59dcab1
FB
255 do {
256 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
257 if (!(reg & DWC3_DCTL_CSFTRST))
fab38333 258 goto done;
45627ac6 259
f59dcab1
FB
260 udelay(1);
261 } while (--retries);
57303488 262
00b42170
BN
263 phy_exit(dwc->usb3_generic_phy);
264 phy_exit(dwc->usb2_generic_phy);
265
f59dcab1 266 return -ETIMEDOUT;
fab38333
TN
267
268done:
269 /*
270 * For DWC_usb31 controller, once DWC3_DCTL_CSFTRST bit is cleared,
271 * we must wait at least 50ms before accessing the PHY domain
272 * (synchronization delay). DWC_usb31 programming guide section 1.3.2.
273 */
274 if (dwc3_is_usb31(dwc))
275 msleep(50);
276
277 return 0;
72246da4
FB
278}
279
fe8abf33
MY
280static const struct clk_bulk_data dwc3_core_clks[] = {
281 { .id = "ref" },
282 { .id = "bus_early" },
283 { .id = "suspend" },
284};
285
db2be4e9
NB
286/*
287 * dwc3_frame_length_adjustment - Adjusts frame length if required
288 * @dwc3: Pointer to our controller context structure
db2be4e9 289 */
bcdb3272 290static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
db2be4e9
NB
291{
292 u32 reg;
293 u32 dft;
294
295 if (dwc->revision < DWC3_REVISION_250A)
296 return;
297
bcdb3272 298 if (dwc->fladj == 0)
db2be4e9
NB
299 return;
300
301 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
302 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
bcdb3272 303 if (!dev_WARN_ONCE(dwc->dev, dft == dwc->fladj,
db2be4e9
NB
304 "request value same as default, ignoring\n")) {
305 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
bcdb3272 306 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
db2be4e9
NB
307 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
308 }
309}
310
72246da4
FB
311/**
312 * dwc3_free_one_event_buffer - Frees one event buffer
313 * @dwc: Pointer to our controller context structure
314 * @evt: Pointer to event buffer to be freed
315 */
316static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
317 struct dwc3_event_buffer *evt)
318{
d64ff406 319 dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
72246da4
FB
320}
321
322/**
1d046793 323 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
72246da4
FB
324 * @dwc: Pointer to our controller context structure
325 * @length: size of the event buffer
326 *
1d046793 327 * Returns a pointer to the allocated event buffer structure on success
72246da4
FB
328 * otherwise ERR_PTR(errno).
329 */
67d0b500
FB
330static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
331 unsigned length)
72246da4
FB
332{
333 struct dwc3_event_buffer *evt;
334
380f0d28 335 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
72246da4
FB
336 if (!evt)
337 return ERR_PTR(-ENOMEM);
338
339 evt->dwc = dwc;
340 evt->length = length;
d9fa4c63
JY
341 evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
342 if (!evt->cache)
343 return ERR_PTR(-ENOMEM);
344
d64ff406 345 evt->buf = dma_alloc_coherent(dwc->sysdev, length,
72246da4 346 &evt->dma, GFP_KERNEL);
e32672f0 347 if (!evt->buf)
72246da4 348 return ERR_PTR(-ENOMEM);
72246da4
FB
349
350 return evt;
351}
352
353/**
354 * dwc3_free_event_buffers - frees all allocated event buffers
355 * @dwc: Pointer to our controller context structure
356 */
357static void dwc3_free_event_buffers(struct dwc3 *dwc)
358{
359 struct dwc3_event_buffer *evt;
72246da4 360
696c8b12 361 evt = dwc->ev_buf;
660e9bde
FB
362 if (evt)
363 dwc3_free_one_event_buffer(dwc, evt);
72246da4
FB
364}
365
366/**
367 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
1d046793 368 * @dwc: pointer to our controller context structure
72246da4
FB
369 * @length: size of event buffer
370 *
1d046793 371 * Returns 0 on success otherwise negative errno. In the error case, dwc
72246da4
FB
372 * may contain some buffers allocated but not all which were requested.
373 */
41ac7b3a 374static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
72246da4 375{
660e9bde 376 struct dwc3_event_buffer *evt;
72246da4 377
660e9bde
FB
378 evt = dwc3_alloc_one_event_buffer(dwc, length);
379 if (IS_ERR(evt)) {
380 dev_err(dwc->dev, "can't allocate event buffer\n");
381 return PTR_ERR(evt);
72246da4 382 }
696c8b12 383 dwc->ev_buf = evt;
72246da4
FB
384
385 return 0;
386}
387
388/**
389 * dwc3_event_buffers_setup - setup our allocated event buffers
1d046793 390 * @dwc: pointer to our controller context structure
72246da4
FB
391 *
392 * Returns 0 on success otherwise negative errno.
393 */
f09cc79b 394int dwc3_event_buffers_setup(struct dwc3 *dwc)
72246da4
FB
395{
396 struct dwc3_event_buffer *evt;
72246da4 397
696c8b12 398 evt = dwc->ev_buf;
660e9bde 399 evt->lpos = 0;
660e9bde
FB
400 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
401 lower_32_bits(evt->dma));
402 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
403 upper_32_bits(evt->dma));
404 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
405 DWC3_GEVNTSIZ_SIZE(evt->length));
406 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
72246da4
FB
407
408 return 0;
409}
410
f09cc79b 411void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
72246da4
FB
412{
413 struct dwc3_event_buffer *evt;
72246da4 414
696c8b12 415 evt = dwc->ev_buf;
7acd85e0 416
660e9bde 417 evt->lpos = 0;
7acd85e0 418
660e9bde
FB
419 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
420 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
421 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
422 | DWC3_GEVNTSIZ_SIZE(0));
423 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
72246da4
FB
424}
425
0ffcaf37
FB
426static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
427{
428 if (!dwc->has_hibernation)
429 return 0;
430
431 if (!dwc->nr_scratch)
432 return 0;
433
434 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
435 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
436 if (!dwc->scratchbuf)
437 return -ENOMEM;
438
439 return 0;
440}
441
442static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
443{
444 dma_addr_t scratch_addr;
445 u32 param;
446 int ret;
447
448 if (!dwc->has_hibernation)
449 return 0;
450
451 if (!dwc->nr_scratch)
452 return 0;
453
454 /* should never fall here */
455 if (!WARN_ON(dwc->scratchbuf))
456 return 0;
457
d64ff406 458 scratch_addr = dma_map_single(dwc->sysdev, dwc->scratchbuf,
0ffcaf37
FB
459 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
460 DMA_BIDIRECTIONAL);
d64ff406
AB
461 if (dma_mapping_error(dwc->sysdev, scratch_addr)) {
462 dev_err(dwc->sysdev, "failed to map scratch buffer\n");
0ffcaf37
FB
463 ret = -EFAULT;
464 goto err0;
465 }
466
467 dwc->scratch_addr = scratch_addr;
468
469 param = lower_32_bits(scratch_addr);
470
471 ret = dwc3_send_gadget_generic_command(dwc,
472 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
473 if (ret < 0)
474 goto err1;
475
476 param = upper_32_bits(scratch_addr);
477
478 ret = dwc3_send_gadget_generic_command(dwc,
479 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
480 if (ret < 0)
481 goto err1;
482
483 return 0;
484
485err1:
d64ff406 486 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
0ffcaf37
FB
487 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
488
489err0:
490 return ret;
491}
492
493static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
494{
495 if (!dwc->has_hibernation)
496 return;
497
498 if (!dwc->nr_scratch)
499 return;
500
501 /* should never fall here */
502 if (!WARN_ON(dwc->scratchbuf))
503 return;
504
d64ff406 505 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
0ffcaf37
FB
506 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
507 kfree(dwc->scratchbuf);
508}
509
789451f6
FB
510static void dwc3_core_num_eps(struct dwc3 *dwc)
511{
512 struct dwc3_hwparams *parms = &dwc->hwparams;
513
47d3946e 514 dwc->num_eps = DWC3_NUM_EPS(parms);
789451f6
FB
515}
516
41ac7b3a 517static void dwc3_cache_hwparams(struct dwc3 *dwc)
26ceca97
FB
518{
519 struct dwc3_hwparams *parms = &dwc->hwparams;
520
521 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
522 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
523 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
524 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
525 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
526 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
527 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
528 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
529 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
530}
531
98112041
RQ
532static int dwc3_core_ulpi_init(struct dwc3 *dwc)
533{
534 int intf;
535 int ret = 0;
536
537 intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
538
539 if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
540 (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
541 dwc->hsphy_interface &&
542 !strncmp(dwc->hsphy_interface, "ulpi", 4)))
543 ret = dwc3_ulpi_init(dwc);
544
545 return ret;
546}
547
b5a65c40
HR
548/**
549 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
550 * @dwc: Pointer to our controller context structure
88bc9d19
HK
551 *
552 * Returns 0 on success. The USB PHY interfaces are configured but not
553 * initialized. The PHY interfaces and the PHYs get initialized together with
554 * the core in dwc3_core_init.
b5a65c40 555 */
88bc9d19 556static int dwc3_phy_setup(struct dwc3 *dwc)
b5a65c40
HR
557{
558 u32 reg;
559
560 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
561
1966b865
FB
562 /*
563 * Make sure UX_EXIT_PX is cleared as that causes issues with some
564 * PHYs. Also, this bit is not supposed to be used in normal operation.
565 */
566 reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
567
2164a476
HR
568 /*
569 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
570 * to '0' during coreConsultant configuration. So default value
571 * will be '0' when the core is reset. Application needs to set it
572 * to '1' after the core initialization is completed.
573 */
574 if (dwc->revision > DWC3_REVISION_194A)
575 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
576
b5a65c40
HR
577 if (dwc->u2ss_inp3_quirk)
578 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
579
e58dd357
RB
580 if (dwc->dis_rxdet_inp3_quirk)
581 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
582
df31f5b3
HR
583 if (dwc->req_p1p2p3_quirk)
584 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
585
a2a1d0f5
HR
586 if (dwc->del_p1p2p3_quirk)
587 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
588
41c06ffd
HR
589 if (dwc->del_phy_power_chg_quirk)
590 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
591
fb67afca
HR
592 if (dwc->lfps_filter_quirk)
593 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
594
14f4ac53
HR
595 if (dwc->rx_detect_poll_quirk)
596 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
597
6b6a0c9a
HR
598 if (dwc->tx_de_emphasis_quirk)
599 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
600
cd72f890 601 if (dwc->dis_u3_susphy_quirk)
59acfa20
HR
602 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
603
00fe081d
WW
604 if (dwc->dis_del_phy_power_chg_quirk)
605 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
606
b5a65c40
HR
607 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
608
2164a476
HR
609 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
610
3e10a2ce
HK
611 /* Select the HS PHY interface */
612 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
613 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
43cacb03
FB
614 if (dwc->hsphy_interface &&
615 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
3e10a2ce 616 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
88bc9d19 617 break;
43cacb03
FB
618 } else if (dwc->hsphy_interface &&
619 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
3e10a2ce 620 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
88bc9d19 621 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
3e10a2ce 622 } else {
88bc9d19
HK
623 /* Relying on default value. */
624 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
625 break;
3e10a2ce
HK
626 }
627 /* FALLTHROUGH */
88bc9d19 628 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
88bc9d19 629 /* FALLTHROUGH */
3e10a2ce
HK
630 default:
631 break;
632 }
633
32f2ed86
WW
634 switch (dwc->hsphy_mode) {
635 case USBPHY_INTERFACE_MODE_UTMI:
636 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
637 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
638 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
639 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
640 break;
641 case USBPHY_INTERFACE_MODE_UTMIW:
642 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
643 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
644 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
645 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
646 break;
647 default:
648 break;
649 }
650
2164a476
HR
651 /*
652 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
653 * '0' during coreConsultant configuration. So default value will
654 * be '0' when the core is reset. Application needs to set it to
655 * '1' after the core initialization is completed.
656 */
657 if (dwc->revision > DWC3_REVISION_194A)
658 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
659
cd72f890 660 if (dwc->dis_u2_susphy_quirk)
0effe0a3
HR
661 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
662
ec791d14
JY
663 if (dwc->dis_enblslpm_quirk)
664 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
eafeacf1
TN
665 else
666 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM;
ec791d14 667
16199f33
WW
668 if (dwc->dis_u2_freeclk_exists_quirk)
669 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
670
2164a476 671 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
88bc9d19
HK
672
673 return 0;
b5a65c40
HR
674}
675
c499ff71
FB
676static void dwc3_core_exit(struct dwc3 *dwc)
677{
678 dwc3_event_buffers_cleanup(dwc);
679
680 usb_phy_shutdown(dwc->usb2_phy);
681 usb_phy_shutdown(dwc->usb3_phy);
682 phy_exit(dwc->usb2_generic_phy);
683 phy_exit(dwc->usb3_generic_phy);
684
685 usb_phy_set_suspend(dwc->usb2_phy, 1);
686 usb_phy_set_suspend(dwc->usb3_phy, 1);
687 phy_power_off(dwc->usb2_generic_phy);
688 phy_power_off(dwc->usb3_generic_phy);
fe8abf33
MY
689 clk_bulk_disable(dwc->num_clks, dwc->clks);
690 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
691 reset_control_assert(dwc->reset);
c499ff71
FB
692}
693
0759956f 694static bool dwc3_core_is_valid(struct dwc3 *dwc)
72246da4 695{
0759956f 696 u32 reg;
72246da4 697
7650bd74 698 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
0759956f 699
7650bd74 700 /* This should read as U3 followed by revision number */
690fb371
JY
701 if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
702 /* Detected DWC_usb3 IP */
703 dwc->revision = reg;
704 } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
705 /* Detected DWC_usb31 IP */
706 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
707 dwc->revision |= DWC3_REVISION_IS_DWC31;
475d8e01 708 dwc->version_type = dwc3_readl(dwc->regs, DWC3_VER_TYPE);
690fb371 709 } else {
0759956f 710 return false;
7650bd74 711 }
7650bd74 712
0759956f
FB
713 return true;
714}
58a0f23f 715
941f918e 716static void dwc3_core_setup_global_control(struct dwc3 *dwc)
0759956f 717{
941f918e
FB
718 u32 hwparams4 = dwc->hwparams.hwparams4;
719 u32 reg;
c499ff71 720
4878a028 721 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
3e87c42a 722 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
4878a028 723
164d7731 724 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
4878a028 725 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
32a4a135
FB
726 /**
727 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
728 * issue which would cause xHCI compliance tests to fail.
729 *
730 * Because of that we cannot enable clock gating on such
731 * configurations.
732 *
733 * Refers to:
734 *
735 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
736 * SOF/ITP Mode Used
737 */
738 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
739 dwc->dr_mode == USB_DR_MODE_OTG) &&
740 (dwc->revision >= DWC3_REVISION_210A &&
741 dwc->revision <= DWC3_REVISION_250A))
742 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
743 else
744 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
4878a028 745 break;
0ffcaf37
FB
746 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
747 /* enable hibernation here */
748 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
2eac3992
HR
749
750 /*
751 * REVISIT Enabling this bit so that host-mode hibernation
752 * will work. Device-mode hibernation is not yet implemented.
753 */
754 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
0ffcaf37 755 break;
4878a028 756 default:
5eb30ced
FB
757 /* nothing */
758 break;
4878a028
SAS
759 }
760
946bd579
HR
761 /* check if current dwc3 is on simulation board */
762 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
6af19fd1 763 dev_info(dwc->dev, "Running with FPGA optimizations\n");
946bd579
HR
764 dwc->is_fpga = true;
765 }
766
3b81221a
HR
767 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
768 "disable_scramble cannot be used on non-FPGA builds\n");
769
770 if (dwc->disable_scramble_quirk && dwc->is_fpga)
771 reg |= DWC3_GCTL_DISSCRAMBLE;
772 else
773 reg &= ~DWC3_GCTL_DISSCRAMBLE;
774
9a5b2f31
HR
775 if (dwc->u2exit_lfps_quirk)
776 reg |= DWC3_GCTL_U2EXIT_LFPS;
777
4878a028
SAS
778 /*
779 * WORKAROUND: DWC3 revisions <1.90a have a bug
1d046793 780 * where the device can fail to connect at SuperSpeed
4878a028 781 * and falls back to high-speed mode which causes
1d046793 782 * the device to enter a Connect/Disconnect loop
4878a028
SAS
783 */
784 if (dwc->revision < DWC3_REVISION_190A)
785 reg |= DWC3_GCTL_U2RSTECN;
786
787 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
941f918e
FB
788}
789
f54edb53 790static int dwc3_core_get_phy(struct dwc3 *dwc);
98112041 791static int dwc3_core_ulpi_init(struct dwc3 *dwc);
f54edb53 792
d9612c2f
PM
793/* set global incr burst type configuration registers */
794static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
795{
796 struct device *dev = dwc->dev;
797 /* incrx_mode : for INCR burst type. */
798 bool incrx_mode;
799 /* incrx_size : for size of INCRX burst. */
800 u32 incrx_size;
801 u32 *vals;
802 u32 cfg;
803 int ntype;
804 int ret;
805 int i;
806
807 cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
808
809 /*
810 * Handle property "snps,incr-burst-type-adjustment".
811 * Get the number of value from this property:
812 * result <= 0, means this property is not supported.
813 * result = 1, means INCRx burst mode supported.
814 * result > 1, means undefined length burst mode supported.
815 */
816 ntype = device_property_read_u32_array(dev,
817 "snps,incr-burst-type-adjustment", NULL, 0);
818 if (ntype <= 0)
819 return;
820
821 vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL);
822 if (!vals) {
823 dev_err(dev, "Error to get memory\n");
824 return;
825 }
826
827 /* Get INCR burst type, and parse it */
828 ret = device_property_read_u32_array(dev,
829 "snps,incr-burst-type-adjustment", vals, ntype);
830 if (ret) {
831 dev_err(dev, "Error to get property\n");
832 return;
833 }
834
835 incrx_size = *vals;
836
837 if (ntype > 1) {
838 /* INCRX (undefined length) burst mode */
839 incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
840 for (i = 1; i < ntype; i++) {
841 if (vals[i] > incrx_size)
842 incrx_size = vals[i];
843 }
844 } else {
845 /* INCRX burst mode */
846 incrx_mode = INCRX_BURST_MODE;
847 }
848
849 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
850 cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
851 if (incrx_mode)
852 cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
853 switch (incrx_size) {
854 case 256:
855 cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
856 break;
857 case 128:
858 cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
859 break;
860 case 64:
861 cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
862 break;
863 case 32:
864 cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
865 break;
866 case 16:
867 cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
868 break;
869 case 8:
870 cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
871 break;
872 case 4:
873 cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
874 break;
875 case 1:
876 break;
877 default:
878 dev_err(dev, "Invalid property\n");
879 break;
880 }
881
882 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
883}
884
941f918e
FB
885/**
886 * dwc3_core_init - Low-level initialization of DWC3 Core
887 * @dwc: Pointer to our controller context structure
888 *
889 * Returns 0 on success otherwise negative errno.
890 */
891static int dwc3_core_init(struct dwc3 *dwc)
892{
893 u32 reg;
894 int ret;
895
896 if (!dwc3_core_is_valid(dwc)) {
897 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
898 ret = -ENODEV;
899 goto err0;
900 }
901
902 /*
903 * Write Linux Version Code to our GUID register so it's easy to figure
904 * out which kernel version a bug was found.
905 */
906 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
907
908 /* Handle USB2.0-only core configuration */
909 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
910 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
911 if (dwc->maximum_speed == USB_SPEED_SUPER)
912 dwc->maximum_speed = USB_SPEED_HIGH;
913 }
914
98112041 915 ret = dwc3_phy_setup(dwc);
941f918e
FB
916 if (ret)
917 goto err0;
4878a028 918
98112041
RQ
919 if (!dwc->ulpi_ready) {
920 ret = dwc3_core_ulpi_init(dwc);
921 if (ret)
922 goto err0;
923 dwc->ulpi_ready = true;
924 }
4878a028 925
98112041
RQ
926 if (!dwc->phys_ready) {
927 ret = dwc3_core_get_phy(dwc);
928 if (ret)
929 goto err0a;
930 dwc->phys_ready = true;
931 }
932
933 ret = dwc3_core_soft_reset(dwc);
f54edb53 934 if (ret)
98112041 935 goto err0a;
f54edb53 936
941f918e 937 dwc3_core_setup_global_control(dwc);
c499ff71 938 dwc3_core_num_eps(dwc);
0ffcaf37
FB
939
940 ret = dwc3_setup_scratch_buffers(dwc);
941 if (ret)
c499ff71
FB
942 goto err1;
943
944 /* Adjust Frame Length */
945 dwc3_frame_length_adjustment(dwc);
946
d9612c2f
PM
947 dwc3_set_incr_burst_type(dwc);
948
c499ff71
FB
949 usb_phy_set_suspend(dwc->usb2_phy, 0);
950 usb_phy_set_suspend(dwc->usb3_phy, 0);
951 ret = phy_power_on(dwc->usb2_generic_phy);
952 if (ret < 0)
0ffcaf37
FB
953 goto err2;
954
c499ff71
FB
955 ret = phy_power_on(dwc->usb3_generic_phy);
956 if (ret < 0)
957 goto err3;
958
959 ret = dwc3_event_buffers_setup(dwc);
960 if (ret) {
961 dev_err(dwc->dev, "failed to setup event buffers\n");
962 goto err4;
963 }
964
06281d46
JY
965 /*
966 * ENDXFER polling is available on version 3.10a and later of
967 * the DWC_usb3 controller. It is NOT available in the
968 * DWC_usb31 controller.
969 */
970 if (!dwc3_is_usb31(dwc) && dwc->revision >= DWC3_REVISION_310A) {
971 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
972 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
973 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
974 }
975
65db7a0c 976 if (dwc->revision >= DWC3_REVISION_250A) {
0bb39ca1 977 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
65db7a0c
WW
978
979 /*
980 * Enable hardware control of sending remote wakeup
981 * in HS when the device is in the L1 state.
982 */
983 if (dwc->revision >= DWC3_REVISION_290A)
984 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
985
986 if (dwc->dis_tx_ipgap_linecheck_quirk)
987 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
988
0bb39ca1
JY
989 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
990 }
991
b138e23d
AKV
992 if (dwc->dr_mode == USB_DR_MODE_HOST ||
993 dwc->dr_mode == USB_DR_MODE_OTG) {
994 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
995
996 /*
997 * Enable Auto retry Feature to make the controller operating in
998 * Host mode on seeing transaction errors(CRC errors or internal
999 * overrun scenerios) on IN transfers to reply to the device
1000 * with a non-terminating retry ACK (i.e, an ACK transcation
1001 * packet with Retry=1 & Nump != 0)
1002 */
1003 reg |= DWC3_GUCTL_HSTINAUTORETRY;
1004
1005 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
1006 }
1007
938a5ad1
TN
1008 /*
1009 * Must config both number of packets and max burst settings to enable
1010 * RX and/or TX threshold.
1011 */
1012 if (dwc3_is_usb31(dwc) && dwc->dr_mode == USB_DR_MODE_HOST) {
1013 u8 rx_thr_num = dwc->rx_thr_num_pkt_prd;
1014 u8 rx_maxburst = dwc->rx_max_burst_prd;
1015 u8 tx_thr_num = dwc->tx_thr_num_pkt_prd;
1016 u8 tx_maxburst = dwc->tx_max_burst_prd;
1017
1018 if (rx_thr_num && rx_maxburst) {
1019 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1020 reg |= DWC31_RXTHRNUMPKTSEL_PRD;
1021
1022 reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
1023 reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
1024
1025 reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
1026 reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
1027
1028 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1029 }
1030
1031 if (tx_thr_num && tx_maxburst) {
1032 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1033 reg |= DWC31_TXTHRNUMPKTSEL_PRD;
1034
1035 reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
1036 reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
1037
1038 reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
1039 reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
1040
1041 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1042 }
1043 }
1044
72246da4
FB
1045 return 0;
1046
c499ff71 1047err4:
9b9d7cdd 1048 phy_power_off(dwc->usb3_generic_phy);
c499ff71
FB
1049
1050err3:
9b9d7cdd 1051 phy_power_off(dwc->usb2_generic_phy);
c499ff71 1052
0ffcaf37 1053err2:
c499ff71
FB
1054 usb_phy_set_suspend(dwc->usb2_phy, 1);
1055 usb_phy_set_suspend(dwc->usb3_phy, 1);
0ffcaf37
FB
1056
1057err1:
1058 usb_phy_shutdown(dwc->usb2_phy);
1059 usb_phy_shutdown(dwc->usb3_phy);
57303488
KVA
1060 phy_exit(dwc->usb2_generic_phy);
1061 phy_exit(dwc->usb3_generic_phy);
0ffcaf37 1062
98112041
RQ
1063err0a:
1064 dwc3_ulpi_exit(dwc);
1065
72246da4
FB
1066err0:
1067 return ret;
1068}
1069
3c9f94ac 1070static int dwc3_core_get_phy(struct dwc3 *dwc)
72246da4 1071{
3c9f94ac 1072 struct device *dev = dwc->dev;
941ea361 1073 struct device_node *node = dev->of_node;
3c9f94ac 1074 int ret;
72246da4 1075
5088b6f5
KVA
1076 if (node) {
1077 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
1078 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
bb674907
FB
1079 } else {
1080 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1081 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
5088b6f5
KVA
1082 }
1083
d105e7f8
FB
1084 if (IS_ERR(dwc->usb2_phy)) {
1085 ret = PTR_ERR(dwc->usb2_phy);
122f06e6
KVA
1086 if (ret == -ENXIO || ret == -ENODEV) {
1087 dwc->usb2_phy = NULL;
1088 } else if (ret == -EPROBE_DEFER) {
d105e7f8 1089 return ret;
122f06e6
KVA
1090 } else {
1091 dev_err(dev, "no usb2 phy configured\n");
1092 return ret;
1093 }
51e1e7bc
FB
1094 }
1095
d105e7f8 1096 if (IS_ERR(dwc->usb3_phy)) {
315955d7 1097 ret = PTR_ERR(dwc->usb3_phy);
122f06e6
KVA
1098 if (ret == -ENXIO || ret == -ENODEV) {
1099 dwc->usb3_phy = NULL;
1100 } else if (ret == -EPROBE_DEFER) {
d105e7f8 1101 return ret;
122f06e6
KVA
1102 } else {
1103 dev_err(dev, "no usb3 phy configured\n");
1104 return ret;
1105 }
51e1e7bc
FB
1106 }
1107
57303488
KVA
1108 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
1109 if (IS_ERR(dwc->usb2_generic_phy)) {
1110 ret = PTR_ERR(dwc->usb2_generic_phy);
1111 if (ret == -ENOSYS || ret == -ENODEV) {
1112 dwc->usb2_generic_phy = NULL;
1113 } else if (ret == -EPROBE_DEFER) {
1114 return ret;
1115 } else {
1116 dev_err(dev, "no usb2 phy configured\n");
1117 return ret;
1118 }
1119 }
1120
1121 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
1122 if (IS_ERR(dwc->usb3_generic_phy)) {
1123 ret = PTR_ERR(dwc->usb3_generic_phy);
1124 if (ret == -ENOSYS || ret == -ENODEV) {
1125 dwc->usb3_generic_phy = NULL;
1126 } else if (ret == -EPROBE_DEFER) {
1127 return ret;
1128 } else {
1129 dev_err(dev, "no usb3 phy configured\n");
1130 return ret;
1131 }
1132 }
1133
3c9f94ac
FB
1134 return 0;
1135}
1136
5f94adfe
FB
1137static int dwc3_core_init_mode(struct dwc3 *dwc)
1138{
1139 struct device *dev = dwc->dev;
1140 int ret;
1141
1142 switch (dwc->dr_mode) {
1143 case USB_DR_MODE_PERIPHERAL:
41ce1456 1144 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
958d1a4c
FB
1145
1146 if (dwc->usb2_phy)
1147 otg_set_vbus(dwc->usb2_phy->otg, false);
644cbbc3
MG
1148 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
1149 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
958d1a4c 1150
5f94adfe
FB
1151 ret = dwc3_gadget_init(dwc);
1152 if (ret) {
9522def4
RQ
1153 if (ret != -EPROBE_DEFER)
1154 dev_err(dev, "failed to initialize gadget\n");
5f94adfe
FB
1155 return ret;
1156 }
1157 break;
1158 case USB_DR_MODE_HOST:
41ce1456 1159 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
958d1a4c
FB
1160
1161 if (dwc->usb2_phy)
1162 otg_set_vbus(dwc->usb2_phy->otg, true);
644cbbc3
MG
1163 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
1164 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
958d1a4c 1165
5f94adfe
FB
1166 ret = dwc3_host_init(dwc);
1167 if (ret) {
9522def4
RQ
1168 if (ret != -EPROBE_DEFER)
1169 dev_err(dev, "failed to initialize host\n");
5f94adfe
FB
1170 return ret;
1171 }
d8c80bb3 1172 phy_calibrate(dwc->usb2_generic_phy);
5f94adfe
FB
1173 break;
1174 case USB_DR_MODE_OTG:
41ce1456 1175 INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
9840354f
RQ
1176 ret = dwc3_drd_init(dwc);
1177 if (ret) {
1178 if (ret != -EPROBE_DEFER)
1179 dev_err(dev, "failed to initialize dual-role\n");
1180 return ret;
1181 }
5f94adfe
FB
1182 break;
1183 default:
1184 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1185 return -EINVAL;
1186 }
1187
1188 return 0;
1189}
1190
1191static void dwc3_core_exit_mode(struct dwc3 *dwc)
1192{
1193 switch (dwc->dr_mode) {
1194 case USB_DR_MODE_PERIPHERAL:
1195 dwc3_gadget_exit(dwc);
1196 break;
1197 case USB_DR_MODE_HOST:
1198 dwc3_host_exit(dwc);
1199 break;
1200 case USB_DR_MODE_OTG:
9840354f 1201 dwc3_drd_exit(dwc);
5f94adfe
FB
1202 break;
1203 default:
1204 /* do nothing */
1205 break;
1206 }
1207}
1208
c5ac6116 1209static void dwc3_get_properties(struct dwc3 *dwc)
3c9f94ac 1210{
c5ac6116 1211 struct device *dev = dwc->dev;
80caf7d2 1212 u8 lpm_nyet_threshold;
6b6a0c9a 1213 u8 tx_de_emphasis;
460d098c 1214 u8 hird_threshold;
938a5ad1
TN
1215 u8 rx_thr_num_pkt_prd;
1216 u8 rx_max_burst_prd;
1217 u8 tx_thr_num_pkt_prd;
1218 u8 tx_max_burst_prd;
3c9f94ac 1219
80caf7d2
HR
1220 /* default to highest possible threshold */
1221 lpm_nyet_threshold = 0xff;
1222
6b6a0c9a
HR
1223 /* default to -3.5dB de-emphasis */
1224 tx_de_emphasis = 1;
1225
460d098c
HR
1226 /*
1227 * default to assert utmi_sleep_n and use maximum allowed HIRD
1228 * threshold value of 0b1100
1229 */
1230 hird_threshold = 12;
1231
63863b98 1232 dwc->maximum_speed = usb_get_maximum_speed(dev);
06e7114f 1233 dwc->dr_mode = usb_get_dr_mode(dev);
32f2ed86 1234 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
63863b98 1235
d64ff406
AB
1236 dwc->sysdev_is_parent = device_property_read_bool(dev,
1237 "linux,sysdev_is_parent");
1238 if (dwc->sysdev_is_parent)
1239 dwc->sysdev = dwc->dev->parent;
1240 else
1241 dwc->sysdev = dwc->dev;
1242
3d128919 1243 dwc->has_lpm_erratum = device_property_read_bool(dev,
80caf7d2 1244 "snps,has-lpm-erratum");
3d128919 1245 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
80caf7d2 1246 &lpm_nyet_threshold);
3d128919 1247 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
460d098c 1248 "snps,is-utmi-l1-suspend");
3d128919 1249 device_property_read_u8(dev, "snps,hird-threshold",
460d098c 1250 &hird_threshold);
3d128919 1251 dwc->usb3_lpm_capable = device_property_read_bool(dev,
eac68e8f 1252 "snps,usb3_lpm_capable");
022a0208
TN
1253 dwc->usb2_lpm_disable = device_property_read_bool(dev,
1254 "snps,usb2-lpm-disable");
938a5ad1
TN
1255 device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
1256 &rx_thr_num_pkt_prd);
1257 device_property_read_u8(dev, "snps,rx-max-burst-prd",
1258 &rx_max_burst_prd);
1259 device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
1260 &tx_thr_num_pkt_prd);
1261 device_property_read_u8(dev, "snps,tx-max-burst-prd",
1262 &tx_max_burst_prd);
3c9f94ac 1263
3d128919 1264 dwc->disable_scramble_quirk = device_property_read_bool(dev,
3b81221a 1265 "snps,disable_scramble_quirk");
3d128919 1266 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
9a5b2f31 1267 "snps,u2exit_lfps_quirk");
3d128919 1268 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
b5a65c40 1269 "snps,u2ss_inp3_quirk");
3d128919 1270 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
df31f5b3 1271 "snps,req_p1p2p3_quirk");
3d128919 1272 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
a2a1d0f5 1273 "snps,del_p1p2p3_quirk");
3d128919 1274 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
41c06ffd 1275 "snps,del_phy_power_chg_quirk");
3d128919 1276 dwc->lfps_filter_quirk = device_property_read_bool(dev,
fb67afca 1277 "snps,lfps_filter_quirk");
3d128919 1278 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
14f4ac53 1279 "snps,rx_detect_poll_quirk");
3d128919 1280 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
59acfa20 1281 "snps,dis_u3_susphy_quirk");
3d128919 1282 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
0effe0a3 1283 "snps,dis_u2_susphy_quirk");
ec791d14
JY
1284 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1285 "snps,dis_enblslpm_quirk");
e58dd357
RB
1286 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1287 "snps,dis_rxdet_inp3_quirk");
16199f33
WW
1288 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1289 "snps,dis-u2-freeclk-exists-quirk");
00fe081d
WW
1290 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1291 "snps,dis-del-phy-power-chg-quirk");
65db7a0c
WW
1292 dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1293 "snps,dis-tx-ipgap-linecheck-quirk");
6b6a0c9a 1294
3d128919 1295 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
6b6a0c9a 1296 "snps,tx_de_emphasis_quirk");
3d128919 1297 device_property_read_u8(dev, "snps,tx_de_emphasis",
6b6a0c9a 1298 &tx_de_emphasis);
3d128919
HK
1299 device_property_read_string(dev, "snps,hsphy_interface",
1300 &dwc->hsphy_interface);
1301 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
bcdb3272 1302 &dwc->fladj);
3d128919 1303
42bf02ec
RQ
1304 dwc->dis_metastability_quirk = device_property_read_bool(dev,
1305 "snps,dis_metastability_quirk");
1306
80caf7d2 1307 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
6b6a0c9a 1308 dwc->tx_de_emphasis = tx_de_emphasis;
80caf7d2 1309
460d098c
HR
1310 dwc->hird_threshold = hird_threshold
1311 | (dwc->is_utmi_l1_suspend << 4);
1312
938a5ad1
TN
1313 dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd;
1314 dwc->rx_max_burst_prd = rx_max_burst_prd;
1315
1316 dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd;
1317 dwc->tx_max_burst_prd = tx_max_burst_prd;
1318
cf40b86b
JY
1319 dwc->imod_interval = 0;
1320}
1321
1322/* check whether the core supports IMOD */
1323bool dwc3_has_imod(struct dwc3 *dwc)
1324{
1325 return ((dwc3_is_usb3(dwc) &&
1326 dwc->revision >= DWC3_REVISION_300A) ||
1327 (dwc3_is_usb31(dwc) &&
1328 dwc->revision >= DWC3_USB31_REVISION_120A));
c5ac6116
FB
1329}
1330
7ac51a12
JY
1331static void dwc3_check_params(struct dwc3 *dwc)
1332{
1333 struct device *dev = dwc->dev;
1334
cf40b86b
JY
1335 /* Check for proper value of imod_interval */
1336 if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
1337 dev_warn(dwc->dev, "Interrupt moderation not supported\n");
1338 dwc->imod_interval = 0;
1339 }
1340
28632b44
JY
1341 /*
1342 * Workaround for STAR 9000961433 which affects only version
1343 * 3.00a of the DWC_usb3 core. This prevents the controller
1344 * interrupt from being masked while handling events. IMOD
1345 * allows us to work around this issue. Enable it for the
1346 * affected version.
1347 */
1348 if (!dwc->imod_interval &&
1349 (dwc->revision == DWC3_REVISION_300A))
1350 dwc->imod_interval = 1;
1351
7ac51a12
JY
1352 /* Check the maximum_speed parameter */
1353 switch (dwc->maximum_speed) {
1354 case USB_SPEED_LOW:
1355 case USB_SPEED_FULL:
1356 case USB_SPEED_HIGH:
1357 case USB_SPEED_SUPER:
1358 case USB_SPEED_SUPER_PLUS:
1359 break;
1360 default:
1361 dev_err(dev, "invalid maximum_speed parameter %d\n",
1362 dwc->maximum_speed);
1363 /* fall through */
1364 case USB_SPEED_UNKNOWN:
1365 /* default to superspeed */
1366 dwc->maximum_speed = USB_SPEED_SUPER;
1367
1368 /*
1369 * default to superspeed plus if we are capable.
1370 */
1371 if (dwc3_is_usb31(dwc) &&
1372 (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
1373 DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1374 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1375
1376 break;
1377 }
1378}
1379
c5ac6116
FB
1380static int dwc3_probe(struct platform_device *pdev)
1381{
1382 struct device *dev = &pdev->dev;
44feb8e6 1383 struct resource *res, dwc_res;
c5ac6116
FB
1384 struct dwc3 *dwc;
1385
1386 int ret;
1387
1388 void __iomem *regs;
1389
1390 dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
1391 if (!dwc)
1392 return -ENOMEM;
1393
fe8abf33
MY
1394 dwc->clks = devm_kmemdup(dev, dwc3_core_clks, sizeof(dwc3_core_clks),
1395 GFP_KERNEL);
1396 if (!dwc->clks)
1397 return -ENOMEM;
1398
c5ac6116
FB
1399 dwc->dev = dev;
1400
1401 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1402 if (!res) {
1403 dev_err(dev, "missing memory resource\n");
1404 return -ENODEV;
1405 }
1406
1407 dwc->xhci_resources[0].start = res->start;
1408 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1409 DWC3_XHCI_REGS_END;
1410 dwc->xhci_resources[0].flags = res->flags;
1411 dwc->xhci_resources[0].name = res->name;
1412
c5ac6116
FB
1413 /*
1414 * Request memory region but exclude xHCI regs,
1415 * since it will be requested by the xhci-plat driver.
1416 */
44feb8e6
MY
1417 dwc_res = *res;
1418 dwc_res.start += DWC3_GLOBALS_REGS_START;
1419
1420 regs = devm_ioremap_resource(dev, &dwc_res);
1421 if (IS_ERR(regs))
1422 return PTR_ERR(regs);
c5ac6116
FB
1423
1424 dwc->regs = regs;
44feb8e6 1425 dwc->regs_size = resource_size(&dwc_res);
c5ac6116
FB
1426
1427 dwc3_get_properties(dwc);
1428
fe8abf33
MY
1429 dwc->reset = devm_reset_control_get_optional_shared(dev, NULL);
1430 if (IS_ERR(dwc->reset))
1431 return PTR_ERR(dwc->reset);
1432
61527777
HG
1433 if (dev->of_node) {
1434 dwc->num_clks = ARRAY_SIZE(dwc3_core_clks);
1435
1436 ret = clk_bulk_get(dev, dwc->num_clks, dwc->clks);
1437 if (ret == -EPROBE_DEFER)
1438 return ret;
1439 /*
1440 * Clocks are optional, but new DT platforms should support all
1441 * clocks as required by the DT-binding.
1442 */
1443 if (ret)
1444 dwc->num_clks = 0;
1445 }
fe8abf33
MY
1446
1447 ret = reset_control_deassert(dwc->reset);
1448 if (ret)
1449 goto put_clks;
1450
1451 ret = clk_bulk_prepare(dwc->num_clks, dwc->clks);
1452 if (ret)
1453 goto assert_reset;
1454
1455 ret = clk_bulk_enable(dwc->num_clks, dwc->clks);
1456 if (ret)
1457 goto unprepare_clks;
1458
6c89cce0 1459 platform_set_drvdata(pdev, dwc);
2917e718 1460 dwc3_cache_hwparams(dwc);
6c89cce0 1461
72246da4 1462 spin_lock_init(&dwc->lock);
72246da4 1463
fc8bb91b
FB
1464 pm_runtime_set_active(dev);
1465 pm_runtime_use_autosuspend(dev);
1466 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
802ca850 1467 pm_runtime_enable(dev);
32808237
RQ
1468 ret = pm_runtime_get_sync(dev);
1469 if (ret < 0)
1470 goto err1;
1471
802ca850 1472 pm_runtime_forbid(dev);
72246da4 1473
3921426b
FB
1474 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1475 if (ret) {
1476 dev_err(dwc->dev, "failed to allocate event buffers\n");
1477 ret = -ENOMEM;
32808237 1478 goto err2;
3921426b
FB
1479 }
1480
9d6173e1
TN
1481 ret = dwc3_get_dr_mode(dwc);
1482 if (ret)
1483 goto err3;
32a4a135 1484
c499ff71
FB
1485 ret = dwc3_alloc_scratch_buffers(dwc);
1486 if (ret)
32808237 1487 goto err3;
c499ff71 1488
72246da4
FB
1489 ret = dwc3_core_init(dwc);
1490 if (ret) {
408d3ba0
BN
1491 if (ret != -EPROBE_DEFER)
1492 dev_err(dev, "failed to initialize core: %d\n", ret);
32808237 1493 goto err4;
72246da4
FB
1494 }
1495
7ac51a12 1496 dwc3_check_params(dwc);
2c7f1bd9 1497
5f94adfe
FB
1498 ret = dwc3_core_init_mode(dwc);
1499 if (ret)
32808237 1500 goto err5;
72246da4 1501
4e9f3118 1502 dwc3_debugfs_init(dwc);
fc8bb91b 1503 pm_runtime_put(dev);
72246da4
FB
1504
1505 return 0;
1506
32808237 1507err5:
c499ff71 1508 dwc3_event_buffers_cleanup(dwc);
08fd9a82 1509 dwc3_ulpi_exit(dwc);
57303488 1510
32808237 1511err4:
c499ff71 1512 dwc3_free_scratch_buffers(dwc);
72246da4 1513
32808237 1514err3:
3921426b
FB
1515 dwc3_free_event_buffers(dwc);
1516
32808237
RQ
1517err2:
1518 pm_runtime_allow(&pdev->dev);
1519
1520err1:
1521 pm_runtime_put_sync(&pdev->dev);
1522 pm_runtime_disable(&pdev->dev);
1523
fe8abf33
MY
1524 clk_bulk_disable(dwc->num_clks, dwc->clks);
1525unprepare_clks:
1526 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
1527assert_reset:
1528 reset_control_assert(dwc->reset);
1529put_clks:
1530 clk_bulk_put(dwc->num_clks, dwc->clks);
1531
72246da4
FB
1532 return ret;
1533}
1534
fb4e98ab 1535static int dwc3_remove(struct platform_device *pdev)
72246da4 1536{
72246da4 1537 struct dwc3 *dwc = platform_get_drvdata(pdev);
3da1f6ee 1538
fc8bb91b 1539 pm_runtime_get_sync(&pdev->dev);
72246da4 1540
dc99f16f
FB
1541 dwc3_debugfs_exit(dwc);
1542 dwc3_core_exit_mode(dwc);
8ba007a9 1543
72246da4 1544 dwc3_core_exit(dwc);
88bc9d19 1545 dwc3_ulpi_exit(dwc);
72246da4 1546
16b972a5 1547 pm_runtime_put_sync(&pdev->dev);
fc8bb91b 1548 pm_runtime_allow(&pdev->dev);
72246da4
FB
1549 pm_runtime_disable(&pdev->dev);
1550
fc8bb91b
FB
1551 dwc3_free_event_buffers(dwc);
1552 dwc3_free_scratch_buffers(dwc);
fe8abf33 1553 clk_bulk_put(dwc->num_clks, dwc->clks);
fc8bb91b 1554
72246da4
FB
1555 return 0;
1556}
1557
fc8bb91b 1558#ifdef CONFIG_PM
fe8abf33
MY
1559static int dwc3_core_init_for_resume(struct dwc3 *dwc)
1560{
1561 int ret;
1562
1563 ret = reset_control_deassert(dwc->reset);
1564 if (ret)
1565 return ret;
1566
1567 ret = clk_bulk_prepare(dwc->num_clks, dwc->clks);
1568 if (ret)
1569 goto assert_reset;
1570
1571 ret = clk_bulk_enable(dwc->num_clks, dwc->clks);
1572 if (ret)
1573 goto unprepare_clks;
1574
1575 ret = dwc3_core_init(dwc);
1576 if (ret)
1577 goto disable_clks;
1578
1579 return 0;
1580
1581disable_clks:
1582 clk_bulk_disable(dwc->num_clks, dwc->clks);
1583unprepare_clks:
1584 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
1585assert_reset:
1586 reset_control_assert(dwc->reset);
1587
1588 return ret;
1589}
1590
c4a5153e 1591static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
7415f17c 1592{
fc8bb91b 1593 unsigned long flags;
bcb12877 1594 u32 reg;
7415f17c 1595
689bf72c
MG
1596 switch (dwc->current_dr_role) {
1597 case DWC3_GCTL_PRTCAP_DEVICE:
fc8bb91b 1598 spin_lock_irqsave(&dwc->lock, flags);
7415f17c 1599 dwc3_gadget_suspend(dwc);
fc8bb91b 1600 spin_unlock_irqrestore(&dwc->lock, flags);
689bf72c 1601 dwc3_core_exit(dwc);
51f5d49a 1602 break;
689bf72c 1603 case DWC3_GCTL_PRTCAP_HOST:
bcb12877 1604 if (!PMSG_IS_AUTO(msg)) {
c4a5153e 1605 dwc3_core_exit(dwc);
bcb12877
MG
1606 break;
1607 }
1608
1609 /* Let controller to suspend HSPHY before PHY driver suspends */
1610 if (dwc->dis_u2_susphy_quirk ||
1611 dwc->dis_enblslpm_quirk) {
1612 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1613 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
1614 DWC3_GUSB2PHYCFG_SUSPHY;
1615 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1616
1617 /* Give some time for USB2 PHY to suspend */
1618 usleep_range(5000, 6000);
1619 }
1620
1621 phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
1622 phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
c4a5153e 1623 break;
f09cc79b
RQ
1624 case DWC3_GCTL_PRTCAP_OTG:
1625 /* do nothing during runtime_suspend */
1626 if (PMSG_IS_AUTO(msg))
1627 break;
1628
1629 if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1630 spin_lock_irqsave(&dwc->lock, flags);
1631 dwc3_gadget_suspend(dwc);
1632 spin_unlock_irqrestore(&dwc->lock, flags);
1633 }
1634
1635 dwc3_otg_exit(dwc);
1636 dwc3_core_exit(dwc);
1637 break;
7415f17c 1638 default:
51f5d49a 1639 /* do nothing */
7415f17c
FB
1640 break;
1641 }
1642
7415f17c
FB
1643 return 0;
1644}
1645
c4a5153e 1646static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
7415f17c 1647{
fc8bb91b 1648 unsigned long flags;
57303488 1649 int ret;
bcb12877 1650 u32 reg;
7415f17c 1651
689bf72c
MG
1652 switch (dwc->current_dr_role) {
1653 case DWC3_GCTL_PRTCAP_DEVICE:
fe8abf33 1654 ret = dwc3_core_init_for_resume(dwc);
689bf72c
MG
1655 if (ret)
1656 return ret;
5c4ad318 1657
7d11c3ac 1658 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
fc8bb91b 1659 spin_lock_irqsave(&dwc->lock, flags);
7415f17c 1660 dwc3_gadget_resume(dwc);
fc8bb91b 1661 spin_unlock_irqrestore(&dwc->lock, flags);
689bf72c
MG
1662 break;
1663 case DWC3_GCTL_PRTCAP_HOST:
c4a5153e 1664 if (!PMSG_IS_AUTO(msg)) {
fe8abf33 1665 ret = dwc3_core_init_for_resume(dwc);
c4a5153e
MG
1666 if (ret)
1667 return ret;
7d11c3ac 1668 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
bcb12877 1669 break;
c4a5153e 1670 }
bcb12877
MG
1671 /* Restore GUSB2PHYCFG bits that were modified in suspend */
1672 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1673 if (dwc->dis_u2_susphy_quirk)
1674 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1675
1676 if (dwc->dis_enblslpm_quirk)
1677 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
1678
1679 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1680
1681 phy_pm_runtime_get_sync(dwc->usb2_generic_phy);
1682 phy_pm_runtime_get_sync(dwc->usb3_generic_phy);
f09cc79b
RQ
1683 break;
1684 case DWC3_GCTL_PRTCAP_OTG:
1685 /* nothing to do on runtime_resume */
1686 if (PMSG_IS_AUTO(msg))
1687 break;
1688
1689 ret = dwc3_core_init(dwc);
1690 if (ret)
1691 return ret;
1692
1693 dwc3_set_prtcap(dwc, dwc->current_dr_role);
1694
1695 dwc3_otg_init(dwc);
1696 if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
1697 dwc3_otg_host_init(dwc);
1698 } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1699 spin_lock_irqsave(&dwc->lock, flags);
1700 dwc3_gadget_resume(dwc);
1701 spin_unlock_irqrestore(&dwc->lock, flags);
c4a5153e 1702 }
f09cc79b 1703
c4a5153e 1704 break;
7415f17c
FB
1705 default:
1706 /* do nothing */
1707 break;
1708 }
1709
fc8bb91b
FB
1710 return 0;
1711}
1712
1713static int dwc3_runtime_checks(struct dwc3 *dwc)
1714{
689bf72c 1715 switch (dwc->current_dr_role) {
c4a5153e 1716 case DWC3_GCTL_PRTCAP_DEVICE:
fc8bb91b
FB
1717 if (dwc->connected)
1718 return -EBUSY;
1719 break;
c4a5153e 1720 case DWC3_GCTL_PRTCAP_HOST:
fc8bb91b
FB
1721 default:
1722 /* do nothing */
1723 break;
1724 }
1725
1726 return 0;
1727}
1728
1729static int dwc3_runtime_suspend(struct device *dev)
1730{
1731 struct dwc3 *dwc = dev_get_drvdata(dev);
1732 int ret;
1733
1734 if (dwc3_runtime_checks(dwc))
1735 return -EBUSY;
1736
c4a5153e 1737 ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
fc8bb91b
FB
1738 if (ret)
1739 return ret;
1740
1741 device_init_wakeup(dev, true);
1742
1743 return 0;
1744}
1745
1746static int dwc3_runtime_resume(struct device *dev)
1747{
1748 struct dwc3 *dwc = dev_get_drvdata(dev);
1749 int ret;
1750
1751 device_init_wakeup(dev, false);
1752
c4a5153e 1753 ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
fc8bb91b
FB
1754 if (ret)
1755 return ret;
1756
689bf72c
MG
1757 switch (dwc->current_dr_role) {
1758 case DWC3_GCTL_PRTCAP_DEVICE:
fc8bb91b
FB
1759 dwc3_gadget_process_pending_events(dwc);
1760 break;
689bf72c 1761 case DWC3_GCTL_PRTCAP_HOST:
fc8bb91b
FB
1762 default:
1763 /* do nothing */
1764 break;
1765 }
1766
1767 pm_runtime_mark_last_busy(dev);
1768
1769 return 0;
1770}
1771
1772static int dwc3_runtime_idle(struct device *dev)
1773{
1774 struct dwc3 *dwc = dev_get_drvdata(dev);
1775
689bf72c
MG
1776 switch (dwc->current_dr_role) {
1777 case DWC3_GCTL_PRTCAP_DEVICE:
fc8bb91b
FB
1778 if (dwc3_runtime_checks(dwc))
1779 return -EBUSY;
1780 break;
689bf72c 1781 case DWC3_GCTL_PRTCAP_HOST:
fc8bb91b
FB
1782 default:
1783 /* do nothing */
1784 break;
1785 }
1786
1787 pm_runtime_mark_last_busy(dev);
1788 pm_runtime_autosuspend(dev);
1789
1790 return 0;
1791}
1792#endif /* CONFIG_PM */
1793
1794#ifdef CONFIG_PM_SLEEP
1795static int dwc3_suspend(struct device *dev)
1796{
1797 struct dwc3 *dwc = dev_get_drvdata(dev);
1798 int ret;
1799
c4a5153e 1800 ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
fc8bb91b
FB
1801 if (ret)
1802 return ret;
1803
1804 pinctrl_pm_select_sleep_state(dev);
1805
1806 return 0;
1807}
1808
1809static int dwc3_resume(struct device *dev)
1810{
1811 struct dwc3 *dwc = dev_get_drvdata(dev);
1812 int ret;
1813
1814 pinctrl_pm_select_default_state(dev);
1815
c4a5153e 1816 ret = dwc3_resume_common(dwc, PMSG_RESUME);
fc8bb91b
FB
1817 if (ret)
1818 return ret;
1819
7415f17c
FB
1820 pm_runtime_disable(dev);
1821 pm_runtime_set_active(dev);
1822 pm_runtime_enable(dev);
1823
1824 return 0;
1825}
7f370ed0 1826#endif /* CONFIG_PM_SLEEP */
7415f17c
FB
1827
1828static const struct dev_pm_ops dwc3_dev_pm_ops = {
7415f17c 1829 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
fc8bb91b
FB
1830 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
1831 dwc3_runtime_idle)
7415f17c
FB
1832};
1833
5088b6f5
KVA
1834#ifdef CONFIG_OF
1835static const struct of_device_id of_dwc3_match[] = {
22a5aa17
FB
1836 {
1837 .compatible = "snps,dwc3"
1838 },
5088b6f5
KVA
1839 {
1840 .compatible = "synopsys,dwc3"
1841 },
1842 { },
1843};
1844MODULE_DEVICE_TABLE(of, of_dwc3_match);
1845#endif
1846
404905a6
HK
1847#ifdef CONFIG_ACPI
1848
1849#define ACPI_ID_INTEL_BSW "808622B7"
1850
1851static const struct acpi_device_id dwc3_acpi_match[] = {
1852 { ACPI_ID_INTEL_BSW, 0 },
1853 { },
1854};
1855MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
1856#endif
1857
72246da4
FB
1858static struct platform_driver dwc3_driver = {
1859 .probe = dwc3_probe,
7690417d 1860 .remove = dwc3_remove,
72246da4
FB
1861 .driver = {
1862 .name = "dwc3",
5088b6f5 1863 .of_match_table = of_match_ptr(of_dwc3_match),
404905a6 1864 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
7f370ed0 1865 .pm = &dwc3_dev_pm_ops,
72246da4 1866 },
72246da4
FB
1867};
1868
b1116dcc
TK
1869module_platform_driver(dwc3_driver);
1870
7ae4fc4d 1871MODULE_ALIAS("platform:dwc3");
72246da4 1872MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
5945f789 1873MODULE_LICENSE("GPL v2");
72246da4 1874MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");