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