usb: dwc3: Add support for device L1 exit
[linux-2.6-block.git] / drivers / usb / dwc3 / core.c
CommitLineData
72246da4
FB
1/**
2 * core.c - DesignWare USB3 DRD Controller Core file
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
72246da4
FB
5 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
5945f789
FB
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
72246da4 12 *
5945f789
FB
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
72246da4 17 *
5945f789
FB
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
72246da4
FB
20 */
21
fa0ea13e 22#include <linux/version.h>
a72e658b 23#include <linux/module.h>
72246da4
FB
24#include <linux/kernel.h>
25#include <linux/slab.h>
26#include <linux/spinlock.h>
27#include <linux/platform_device.h>
28#include <linux/pm_runtime.h>
29#include <linux/interrupt.h>
30#include <linux/ioport.h>
31#include <linux/io.h>
32#include <linux/list.h>
33#include <linux/delay.h>
34#include <linux/dma-mapping.h>
457e84b6 35#include <linux/of.h>
404905a6 36#include <linux/acpi.h>
6344475f 37#include <linux/pinctrl/consumer.h>
72246da4
FB
38
39#include <linux/usb/ch9.h>
40#include <linux/usb/gadget.h>
f7e846f0 41#include <linux/usb/of.h>
a45c82b8 42#include <linux/usb/otg.h>
72246da4
FB
43
44#include "core.h"
45#include "gadget.h"
46#include "io.h"
47
48#include "debug.h"
49
fc8bb91b 50#define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
8300dd23 51
9d6173e1
TN
52/**
53 * dwc3_get_dr_mode - Validates and sets dr_mode
54 * @dwc: pointer to our context structure
55 */
56static int dwc3_get_dr_mode(struct dwc3 *dwc)
57{
58 enum usb_dr_mode mode;
59 struct device *dev = dwc->dev;
60 unsigned int hw_mode;
61
62 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
63 dwc->dr_mode = USB_DR_MODE_OTG;
64
65 mode = dwc->dr_mode;
66 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
67
68 switch (hw_mode) {
69 case DWC3_GHWPARAMS0_MODE_GADGET:
70 if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) {
71 dev_err(dev,
72 "Controller does not support host mode.\n");
73 return -EINVAL;
74 }
75 mode = USB_DR_MODE_PERIPHERAL;
76 break;
77 case DWC3_GHWPARAMS0_MODE_HOST:
78 if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
79 dev_err(dev,
80 "Controller does not support device mode.\n");
81 return -EINVAL;
82 }
83 mode = USB_DR_MODE_HOST;
84 break;
85 default:
86 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
87 mode = USB_DR_MODE_HOST;
88 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
89 mode = USB_DR_MODE_PERIPHERAL;
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
3140e8cb
SAS
103void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
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);
111}
8300dd23 112
cf6d867d
FB
113u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
114{
115 struct dwc3 *dwc = dep->dwc;
116 u32 reg;
117
118 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
119 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
120 DWC3_GDBGFIFOSPACE_TYPE(type));
121
122 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
123
124 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
125}
126
72246da4
FB
127/**
128 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
129 * @dwc: pointer to our context structure
130 */
57303488 131static int dwc3_core_soft_reset(struct dwc3 *dwc)
72246da4
FB
132{
133 u32 reg;
f59dcab1 134 int retries = 1000;
57303488 135 int ret;
72246da4 136
51e1e7bc
FB
137 usb_phy_init(dwc->usb2_phy);
138 usb_phy_init(dwc->usb3_phy);
57303488
KVA
139 ret = phy_init(dwc->usb2_generic_phy);
140 if (ret < 0)
141 return ret;
142
143 ret = phy_init(dwc->usb3_generic_phy);
144 if (ret < 0) {
145 phy_exit(dwc->usb2_generic_phy);
146 return ret;
147 }
72246da4 148
f59dcab1
FB
149 /*
150 * We're resetting only the device side because, if we're in host mode,
151 * XHCI driver will reset the host block. If dwc3 was configured for
152 * host-only mode, then we can return early.
153 */
154 if (dwc->dr_mode == USB_DR_MODE_HOST)
155 return 0;
72246da4 156
f59dcab1
FB
157 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
158 reg |= DWC3_DCTL_CSFTRST;
159 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
72246da4 160
f59dcab1
FB
161 do {
162 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
163 if (!(reg & DWC3_DCTL_CSFTRST))
164 return 0;
45627ac6 165
f59dcab1
FB
166 udelay(1);
167 } while (--retries);
57303488 168
f59dcab1 169 return -ETIMEDOUT;
72246da4
FB
170}
171
c5cc74e8
HK
172/**
173 * dwc3_soft_reset - Issue soft reset
174 * @dwc: Pointer to our controller context structure
175 */
176static int dwc3_soft_reset(struct dwc3 *dwc)
177{
178 unsigned long timeout;
179 u32 reg;
180
181 timeout = jiffies + msecs_to_jiffies(500);
182 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
183 do {
184 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
185 if (!(reg & DWC3_DCTL_CSFTRST))
186 break;
187
188 if (time_after(jiffies, timeout)) {
189 dev_err(dwc->dev, "Reset Timed Out\n");
190 return -ETIMEDOUT;
191 }
192
193 cpu_relax();
194 } while (true);
195
196 return 0;
197}
198
db2be4e9
NB
199/*
200 * dwc3_frame_length_adjustment - Adjusts frame length if required
201 * @dwc3: Pointer to our controller context structure
db2be4e9 202 */
bcdb3272 203static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
db2be4e9
NB
204{
205 u32 reg;
206 u32 dft;
207
208 if (dwc->revision < DWC3_REVISION_250A)
209 return;
210
bcdb3272 211 if (dwc->fladj == 0)
db2be4e9
NB
212 return;
213
214 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
215 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
bcdb3272 216 if (!dev_WARN_ONCE(dwc->dev, dft == dwc->fladj,
db2be4e9
NB
217 "request value same as default, ignoring\n")) {
218 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
bcdb3272 219 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
db2be4e9
NB
220 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
221 }
222}
223
72246da4
FB
224/**
225 * dwc3_free_one_event_buffer - Frees one event buffer
226 * @dwc: Pointer to our controller context structure
227 * @evt: Pointer to event buffer to be freed
228 */
229static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
230 struct dwc3_event_buffer *evt)
231{
232 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
72246da4
FB
233}
234
235/**
1d046793 236 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
72246da4
FB
237 * @dwc: Pointer to our controller context structure
238 * @length: size of the event buffer
239 *
1d046793 240 * Returns a pointer to the allocated event buffer structure on success
72246da4
FB
241 * otherwise ERR_PTR(errno).
242 */
67d0b500
FB
243static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
244 unsigned length)
72246da4
FB
245{
246 struct dwc3_event_buffer *evt;
247
380f0d28 248 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
72246da4
FB
249 if (!evt)
250 return ERR_PTR(-ENOMEM);
251
252 evt->dwc = dwc;
253 evt->length = length;
254 evt->buf = dma_alloc_coherent(dwc->dev, length,
255 &evt->dma, GFP_KERNEL);
e32672f0 256 if (!evt->buf)
72246da4 257 return ERR_PTR(-ENOMEM);
72246da4
FB
258
259 return evt;
260}
261
262/**
263 * dwc3_free_event_buffers - frees all allocated event buffers
264 * @dwc: Pointer to our controller context structure
265 */
266static void dwc3_free_event_buffers(struct dwc3 *dwc)
267{
268 struct dwc3_event_buffer *evt;
72246da4 269
696c8b12 270 evt = dwc->ev_buf;
660e9bde
FB
271 if (evt)
272 dwc3_free_one_event_buffer(dwc, evt);
72246da4
FB
273}
274
275/**
276 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
1d046793 277 * @dwc: pointer to our controller context structure
72246da4
FB
278 * @length: size of event buffer
279 *
1d046793 280 * Returns 0 on success otherwise negative errno. In the error case, dwc
72246da4
FB
281 * may contain some buffers allocated but not all which were requested.
282 */
41ac7b3a 283static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
72246da4 284{
660e9bde 285 struct dwc3_event_buffer *evt;
72246da4 286
660e9bde
FB
287 evt = dwc3_alloc_one_event_buffer(dwc, length);
288 if (IS_ERR(evt)) {
289 dev_err(dwc->dev, "can't allocate event buffer\n");
290 return PTR_ERR(evt);
72246da4 291 }
696c8b12 292 dwc->ev_buf = evt;
72246da4
FB
293
294 return 0;
295}
296
297/**
298 * dwc3_event_buffers_setup - setup our allocated event buffers
1d046793 299 * @dwc: pointer to our controller context structure
72246da4
FB
300 *
301 * Returns 0 on success otherwise negative errno.
302 */
7acd85e0 303static int dwc3_event_buffers_setup(struct dwc3 *dwc)
72246da4
FB
304{
305 struct dwc3_event_buffer *evt;
72246da4 306
696c8b12 307 evt = dwc->ev_buf;
660e9bde
FB
308 dwc3_trace(trace_dwc3_core,
309 "Event buf %p dma %08llx length %d\n",
310 evt->buf, (unsigned long long) evt->dma,
311 evt->length);
312
313 evt->lpos = 0;
314
315 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
316 lower_32_bits(evt->dma));
317 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
318 upper_32_bits(evt->dma));
319 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
320 DWC3_GEVNTSIZ_SIZE(evt->length));
321 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
72246da4
FB
322
323 return 0;
324}
325
326static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
327{
328 struct dwc3_event_buffer *evt;
72246da4 329
696c8b12 330 evt = dwc->ev_buf;
7acd85e0 331
660e9bde 332 evt->lpos = 0;
7acd85e0 333
660e9bde
FB
334 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
335 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
336 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
337 | DWC3_GEVNTSIZ_SIZE(0));
338 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
72246da4
FB
339}
340
0ffcaf37
FB
341static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
342{
343 if (!dwc->has_hibernation)
344 return 0;
345
346 if (!dwc->nr_scratch)
347 return 0;
348
349 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
350 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
351 if (!dwc->scratchbuf)
352 return -ENOMEM;
353
354 return 0;
355}
356
357static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
358{
359 dma_addr_t scratch_addr;
360 u32 param;
361 int ret;
362
363 if (!dwc->has_hibernation)
364 return 0;
365
366 if (!dwc->nr_scratch)
367 return 0;
368
369 /* should never fall here */
370 if (!WARN_ON(dwc->scratchbuf))
371 return 0;
372
373 scratch_addr = dma_map_single(dwc->dev, dwc->scratchbuf,
374 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
375 DMA_BIDIRECTIONAL);
376 if (dma_mapping_error(dwc->dev, scratch_addr)) {
377 dev_err(dwc->dev, "failed to map scratch buffer\n");
378 ret = -EFAULT;
379 goto err0;
380 }
381
382 dwc->scratch_addr = scratch_addr;
383
384 param = lower_32_bits(scratch_addr);
385
386 ret = dwc3_send_gadget_generic_command(dwc,
387 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
388 if (ret < 0)
389 goto err1;
390
391 param = upper_32_bits(scratch_addr);
392
393 ret = dwc3_send_gadget_generic_command(dwc,
394 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
395 if (ret < 0)
396 goto err1;
397
398 return 0;
399
400err1:
401 dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
402 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
403
404err0:
405 return ret;
406}
407
408static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
409{
410 if (!dwc->has_hibernation)
411 return;
412
413 if (!dwc->nr_scratch)
414 return;
415
416 /* should never fall here */
417 if (!WARN_ON(dwc->scratchbuf))
418 return;
419
420 dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
421 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
422 kfree(dwc->scratchbuf);
423}
424
789451f6
FB
425static void dwc3_core_num_eps(struct dwc3 *dwc)
426{
427 struct dwc3_hwparams *parms = &dwc->hwparams;
428
429 dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
430 dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
431
73815280 432 dwc3_trace(trace_dwc3_core, "found %d IN and %d OUT endpoints",
789451f6
FB
433 dwc->num_in_eps, dwc->num_out_eps);
434}
435
41ac7b3a 436static void dwc3_cache_hwparams(struct dwc3 *dwc)
26ceca97
FB
437{
438 struct dwc3_hwparams *parms = &dwc->hwparams;
439
440 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
441 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
442 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
443 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
444 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
445 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
446 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
447 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
448 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
449}
450
b5a65c40
HR
451/**
452 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
453 * @dwc: Pointer to our controller context structure
88bc9d19
HK
454 *
455 * Returns 0 on success. The USB PHY interfaces are configured but not
456 * initialized. The PHY interfaces and the PHYs get initialized together with
457 * the core in dwc3_core_init.
b5a65c40 458 */
88bc9d19 459static int dwc3_phy_setup(struct dwc3 *dwc)
b5a65c40
HR
460{
461 u32 reg;
88bc9d19 462 int ret;
b5a65c40
HR
463
464 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
465
2164a476
HR
466 /*
467 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
468 * to '0' during coreConsultant configuration. So default value
469 * will be '0' when the core is reset. Application needs to set it
470 * to '1' after the core initialization is completed.
471 */
472 if (dwc->revision > DWC3_REVISION_194A)
473 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
474
b5a65c40
HR
475 if (dwc->u2ss_inp3_quirk)
476 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
477
e58dd357
RB
478 if (dwc->dis_rxdet_inp3_quirk)
479 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
480
df31f5b3
HR
481 if (dwc->req_p1p2p3_quirk)
482 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
483
a2a1d0f5
HR
484 if (dwc->del_p1p2p3_quirk)
485 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
486
41c06ffd
HR
487 if (dwc->del_phy_power_chg_quirk)
488 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
489
fb67afca
HR
490 if (dwc->lfps_filter_quirk)
491 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
492
14f4ac53
HR
493 if (dwc->rx_detect_poll_quirk)
494 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
495
6b6a0c9a
HR
496 if (dwc->tx_de_emphasis_quirk)
497 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
498
cd72f890 499 if (dwc->dis_u3_susphy_quirk)
59acfa20
HR
500 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
501
00fe081d
WW
502 if (dwc->dis_del_phy_power_chg_quirk)
503 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
504
b5a65c40
HR
505 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
506
2164a476
HR
507 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
508
3e10a2ce
HK
509 /* Select the HS PHY interface */
510 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
511 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
43cacb03
FB
512 if (dwc->hsphy_interface &&
513 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
3e10a2ce 514 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
88bc9d19 515 break;
43cacb03
FB
516 } else if (dwc->hsphy_interface &&
517 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
3e10a2ce 518 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
88bc9d19 519 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
3e10a2ce 520 } else {
88bc9d19
HK
521 /* Relying on default value. */
522 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
523 break;
3e10a2ce
HK
524 }
525 /* FALLTHROUGH */
88bc9d19
HK
526 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
527 /* Making sure the interface and PHY are operational */
528 ret = dwc3_soft_reset(dwc);
529 if (ret)
530 return ret;
531
532 udelay(1);
533
534 ret = dwc3_ulpi_init(dwc);
535 if (ret)
536 return ret;
537 /* FALLTHROUGH */
3e10a2ce
HK
538 default:
539 break;
540 }
541
32f2ed86
WW
542 switch (dwc->hsphy_mode) {
543 case USBPHY_INTERFACE_MODE_UTMI:
544 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
545 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
546 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
547 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
548 break;
549 case USBPHY_INTERFACE_MODE_UTMIW:
550 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
551 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
552 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
553 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
554 break;
555 default:
556 break;
557 }
558
2164a476
HR
559 /*
560 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
561 * '0' during coreConsultant configuration. So default value will
562 * be '0' when the core is reset. Application needs to set it to
563 * '1' after the core initialization is completed.
564 */
565 if (dwc->revision > DWC3_REVISION_194A)
566 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
567
cd72f890 568 if (dwc->dis_u2_susphy_quirk)
0effe0a3
HR
569 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
570
ec791d14
JY
571 if (dwc->dis_enblslpm_quirk)
572 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
573
16199f33
WW
574 if (dwc->dis_u2_freeclk_exists_quirk)
575 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
576
2164a476 577 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
88bc9d19
HK
578
579 return 0;
b5a65c40
HR
580}
581
c499ff71
FB
582static void dwc3_core_exit(struct dwc3 *dwc)
583{
584 dwc3_event_buffers_cleanup(dwc);
585
586 usb_phy_shutdown(dwc->usb2_phy);
587 usb_phy_shutdown(dwc->usb3_phy);
588 phy_exit(dwc->usb2_generic_phy);
589 phy_exit(dwc->usb3_generic_phy);
590
591 usb_phy_set_suspend(dwc->usb2_phy, 1);
592 usb_phy_set_suspend(dwc->usb3_phy, 1);
593 phy_power_off(dwc->usb2_generic_phy);
594 phy_power_off(dwc->usb3_generic_phy);
595}
596
72246da4
FB
597/**
598 * dwc3_core_init - Low-level initialization of DWC3 Core
599 * @dwc: Pointer to our controller context structure
600 *
601 * Returns 0 on success otherwise negative errno.
602 */
41ac7b3a 603static int dwc3_core_init(struct dwc3 *dwc)
72246da4 604{
0ffcaf37 605 u32 hwparams4 = dwc->hwparams.hwparams4;
72246da4
FB
606 u32 reg;
607 int ret;
608
7650bd74
SAS
609 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
610 /* This should read as U3 followed by revision number */
690fb371
JY
611 if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
612 /* Detected DWC_usb3 IP */
613 dwc->revision = reg;
614 } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
615 /* Detected DWC_usb31 IP */
616 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
617 dwc->revision |= DWC3_REVISION_IS_DWC31;
618 } else {
7650bd74
SAS
619 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
620 ret = -ENODEV;
621 goto err0;
622 }
7650bd74 623
fa0ea13e
FB
624 /*
625 * Write Linux Version Code to our GUID register so it's easy to figure
626 * out which kernel version a bug was found.
627 */
628 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
629
0e1e5c47
PZ
630 /* Handle USB2.0-only core configuration */
631 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
632 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
633 if (dwc->maximum_speed == USB_SPEED_SUPER)
634 dwc->maximum_speed = USB_SPEED_HIGH;
635 }
636
72246da4 637 /* issue device SoftReset too */
c5cc74e8
HK
638 ret = dwc3_soft_reset(dwc);
639 if (ret)
640 goto err0;
72246da4 641
57303488
KVA
642 ret = dwc3_core_soft_reset(dwc);
643 if (ret)
644 goto err0;
58a0f23f 645
c499ff71
FB
646 ret = dwc3_phy_setup(dwc);
647 if (ret)
648 goto err0;
649
4878a028 650 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
3e87c42a 651 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
4878a028 652
164d7731 653 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
4878a028 654 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
32a4a135
FB
655 /**
656 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
657 * issue which would cause xHCI compliance tests to fail.
658 *
659 * Because of that we cannot enable clock gating on such
660 * configurations.
661 *
662 * Refers to:
663 *
664 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
665 * SOF/ITP Mode Used
666 */
667 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
668 dwc->dr_mode == USB_DR_MODE_OTG) &&
669 (dwc->revision >= DWC3_REVISION_210A &&
670 dwc->revision <= DWC3_REVISION_250A))
671 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
672 else
673 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
4878a028 674 break;
0ffcaf37
FB
675 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
676 /* enable hibernation here */
677 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
2eac3992
HR
678
679 /*
680 * REVISIT Enabling this bit so that host-mode hibernation
681 * will work. Device-mode hibernation is not yet implemented.
682 */
683 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
0ffcaf37 684 break;
4878a028 685 default:
1407bf13 686 dwc3_trace(trace_dwc3_core, "No power optimization available\n");
4878a028
SAS
687 }
688
946bd579
HR
689 /* check if current dwc3 is on simulation board */
690 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
1407bf13
FB
691 dwc3_trace(trace_dwc3_core,
692 "running on FPGA platform\n");
946bd579
HR
693 dwc->is_fpga = true;
694 }
695
3b81221a
HR
696 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
697 "disable_scramble cannot be used on non-FPGA builds\n");
698
699 if (dwc->disable_scramble_quirk && dwc->is_fpga)
700 reg |= DWC3_GCTL_DISSCRAMBLE;
701 else
702 reg &= ~DWC3_GCTL_DISSCRAMBLE;
703
9a5b2f31
HR
704 if (dwc->u2exit_lfps_quirk)
705 reg |= DWC3_GCTL_U2EXIT_LFPS;
706
4878a028
SAS
707 /*
708 * WORKAROUND: DWC3 revisions <1.90a have a bug
1d046793 709 * where the device can fail to connect at SuperSpeed
4878a028 710 * and falls back to high-speed mode which causes
1d046793 711 * the device to enter a Connect/Disconnect loop
4878a028
SAS
712 */
713 if (dwc->revision < DWC3_REVISION_190A)
714 reg |= DWC3_GCTL_U2RSTECN;
715
716 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
717
c499ff71 718 dwc3_core_num_eps(dwc);
0ffcaf37
FB
719
720 ret = dwc3_setup_scratch_buffers(dwc);
721 if (ret)
c499ff71
FB
722 goto err1;
723
724 /* Adjust Frame Length */
725 dwc3_frame_length_adjustment(dwc);
726
727 usb_phy_set_suspend(dwc->usb2_phy, 0);
728 usb_phy_set_suspend(dwc->usb3_phy, 0);
729 ret = phy_power_on(dwc->usb2_generic_phy);
730 if (ret < 0)
0ffcaf37
FB
731 goto err2;
732
c499ff71
FB
733 ret = phy_power_on(dwc->usb3_generic_phy);
734 if (ret < 0)
735 goto err3;
736
737 ret = dwc3_event_buffers_setup(dwc);
738 if (ret) {
739 dev_err(dwc->dev, "failed to setup event buffers\n");
740 goto err4;
741 }
742
00af6233
BW
743 switch (dwc->dr_mode) {
744 case USB_DR_MODE_PERIPHERAL:
745 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
746 break;
747 case USB_DR_MODE_HOST:
748 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
749 break;
750 case USB_DR_MODE_OTG:
751 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
752 break;
753 default:
754 dev_warn(dwc->dev, "Unsupported mode %d\n", dwc->dr_mode);
755 break;
756 }
757
06281d46
JY
758 /*
759 * ENDXFER polling is available on version 3.10a and later of
760 * the DWC_usb3 controller. It is NOT available in the
761 * DWC_usb31 controller.
762 */
763 if (!dwc3_is_usb31(dwc) && dwc->revision >= DWC3_REVISION_310A) {
764 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
765 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
766 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
767 }
768
0bb39ca1
JY
769 /*
770 * Enable hardware control of sending remote wakeup in HS when
771 * the device is in the L1 state.
772 */
773 if (dwc->revision >= DWC3_REVISION_290A) {
774 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
775 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
776 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
777 }
778
72246da4
FB
779 return 0;
780
c499ff71
FB
781err4:
782 phy_power_off(dwc->usb2_generic_phy);
783
784err3:
785 phy_power_off(dwc->usb3_generic_phy);
786
0ffcaf37 787err2:
c499ff71
FB
788 usb_phy_set_suspend(dwc->usb2_phy, 1);
789 usb_phy_set_suspend(dwc->usb3_phy, 1);
790 dwc3_core_exit(dwc);
0ffcaf37
FB
791
792err1:
793 usb_phy_shutdown(dwc->usb2_phy);
794 usb_phy_shutdown(dwc->usb3_phy);
57303488
KVA
795 phy_exit(dwc->usb2_generic_phy);
796 phy_exit(dwc->usb3_generic_phy);
0ffcaf37 797
72246da4
FB
798err0:
799 return ret;
800}
801
3c9f94ac 802static int dwc3_core_get_phy(struct dwc3 *dwc)
72246da4 803{
3c9f94ac 804 struct device *dev = dwc->dev;
941ea361 805 struct device_node *node = dev->of_node;
3c9f94ac 806 int ret;
72246da4 807
5088b6f5
KVA
808 if (node) {
809 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
810 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
bb674907
FB
811 } else {
812 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
813 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
5088b6f5
KVA
814 }
815
d105e7f8
FB
816 if (IS_ERR(dwc->usb2_phy)) {
817 ret = PTR_ERR(dwc->usb2_phy);
122f06e6
KVA
818 if (ret == -ENXIO || ret == -ENODEV) {
819 dwc->usb2_phy = NULL;
820 } else if (ret == -EPROBE_DEFER) {
d105e7f8 821 return ret;
122f06e6
KVA
822 } else {
823 dev_err(dev, "no usb2 phy configured\n");
824 return ret;
825 }
51e1e7bc
FB
826 }
827
d105e7f8 828 if (IS_ERR(dwc->usb3_phy)) {
315955d7 829 ret = PTR_ERR(dwc->usb3_phy);
122f06e6
KVA
830 if (ret == -ENXIO || ret == -ENODEV) {
831 dwc->usb3_phy = NULL;
832 } else if (ret == -EPROBE_DEFER) {
d105e7f8 833 return ret;
122f06e6
KVA
834 } else {
835 dev_err(dev, "no usb3 phy configured\n");
836 return ret;
837 }
51e1e7bc
FB
838 }
839
57303488
KVA
840 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
841 if (IS_ERR(dwc->usb2_generic_phy)) {
842 ret = PTR_ERR(dwc->usb2_generic_phy);
843 if (ret == -ENOSYS || ret == -ENODEV) {
844 dwc->usb2_generic_phy = NULL;
845 } else if (ret == -EPROBE_DEFER) {
846 return ret;
847 } else {
848 dev_err(dev, "no usb2 phy configured\n");
849 return ret;
850 }
851 }
852
853 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
854 if (IS_ERR(dwc->usb3_generic_phy)) {
855 ret = PTR_ERR(dwc->usb3_generic_phy);
856 if (ret == -ENOSYS || ret == -ENODEV) {
857 dwc->usb3_generic_phy = NULL;
858 } else if (ret == -EPROBE_DEFER) {
859 return ret;
860 } else {
861 dev_err(dev, "no usb3 phy configured\n");
862 return ret;
863 }
864 }
865
3c9f94ac
FB
866 return 0;
867}
868
5f94adfe
FB
869static int dwc3_core_init_mode(struct dwc3 *dwc)
870{
871 struct device *dev = dwc->dev;
872 int ret;
873
874 switch (dwc->dr_mode) {
875 case USB_DR_MODE_PERIPHERAL:
5f94adfe
FB
876 ret = dwc3_gadget_init(dwc);
877 if (ret) {
9522def4
RQ
878 if (ret != -EPROBE_DEFER)
879 dev_err(dev, "failed to initialize gadget\n");
5f94adfe
FB
880 return ret;
881 }
882 break;
883 case USB_DR_MODE_HOST:
5f94adfe
FB
884 ret = dwc3_host_init(dwc);
885 if (ret) {
9522def4
RQ
886 if (ret != -EPROBE_DEFER)
887 dev_err(dev, "failed to initialize host\n");
5f94adfe
FB
888 return ret;
889 }
890 break;
891 case USB_DR_MODE_OTG:
5f94adfe
FB
892 ret = dwc3_host_init(dwc);
893 if (ret) {
9522def4
RQ
894 if (ret != -EPROBE_DEFER)
895 dev_err(dev, "failed to initialize host\n");
5f94adfe
FB
896 return ret;
897 }
898
899 ret = dwc3_gadget_init(dwc);
900 if (ret) {
9522def4
RQ
901 if (ret != -EPROBE_DEFER)
902 dev_err(dev, "failed to initialize gadget\n");
5f94adfe
FB
903 return ret;
904 }
905 break;
906 default:
907 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
908 return -EINVAL;
909 }
910
911 return 0;
912}
913
914static void dwc3_core_exit_mode(struct dwc3 *dwc)
915{
916 switch (dwc->dr_mode) {
917 case USB_DR_MODE_PERIPHERAL:
918 dwc3_gadget_exit(dwc);
919 break;
920 case USB_DR_MODE_HOST:
921 dwc3_host_exit(dwc);
922 break;
923 case USB_DR_MODE_OTG:
924 dwc3_host_exit(dwc);
925 dwc3_gadget_exit(dwc);
926 break;
927 default:
928 /* do nothing */
929 break;
930 }
931}
932
3c9f94ac
FB
933#define DWC3_ALIGN_MASK (16 - 1)
934
935static int dwc3_probe(struct platform_device *pdev)
936{
937 struct device *dev = &pdev->dev;
3c9f94ac
FB
938 struct resource *res;
939 struct dwc3 *dwc;
80caf7d2 940 u8 lpm_nyet_threshold;
6b6a0c9a 941 u8 tx_de_emphasis;
460d098c 942 u8 hird_threshold;
3c9f94ac 943
b09e99ee 944 int ret;
3c9f94ac
FB
945
946 void __iomem *regs;
947 void *mem;
948
949 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
734d5a53 950 if (!mem)
3c9f94ac 951 return -ENOMEM;
734d5a53 952
3c9f94ac 953 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
3c9f94ac
FB
954 dwc->dev = dev;
955
3c9f94ac
FB
956 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
957 if (!res) {
958 dev_err(dev, "missing memory resource\n");
959 return -ENODEV;
960 }
961
f32a5e23
VG
962 dwc->xhci_resources[0].start = res->start;
963 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
964 DWC3_XHCI_REGS_END;
965 dwc->xhci_resources[0].flags = res->flags;
966 dwc->xhci_resources[0].name = res->name;
967
968 res->start += DWC3_GLOBALS_REGS_START;
969
970 /*
971 * Request memory region but exclude xHCI regs,
972 * since it will be requested by the xhci-plat driver.
973 */
974 regs = devm_ioremap_resource(dev, res);
3da1f6ee
FB
975 if (IS_ERR(regs)) {
976 ret = PTR_ERR(regs);
977 goto err0;
978 }
f32a5e23
VG
979
980 dwc->regs = regs;
981 dwc->regs_size = resource_size(res);
f32a5e23 982
80caf7d2
HR
983 /* default to highest possible threshold */
984 lpm_nyet_threshold = 0xff;
985
6b6a0c9a
HR
986 /* default to -3.5dB de-emphasis */
987 tx_de_emphasis = 1;
988
460d098c
HR
989 /*
990 * default to assert utmi_sleep_n and use maximum allowed HIRD
991 * threshold value of 0b1100
992 */
993 hird_threshold = 12;
994
63863b98 995 dwc->maximum_speed = usb_get_maximum_speed(dev);
06e7114f 996 dwc->dr_mode = usb_get_dr_mode(dev);
32f2ed86 997 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
63863b98 998
3d128919 999 dwc->has_lpm_erratum = device_property_read_bool(dev,
80caf7d2 1000 "snps,has-lpm-erratum");
3d128919 1001 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
80caf7d2 1002 &lpm_nyet_threshold);
3d128919 1003 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
460d098c 1004 "snps,is-utmi-l1-suspend");
3d128919 1005 device_property_read_u8(dev, "snps,hird-threshold",
460d098c 1006 &hird_threshold);
3d128919 1007 dwc->usb3_lpm_capable = device_property_read_bool(dev,
eac68e8f 1008 "snps,usb3_lpm_capable");
3c9f94ac 1009
3d128919 1010 dwc->disable_scramble_quirk = device_property_read_bool(dev,
3b81221a 1011 "snps,disable_scramble_quirk");
3d128919 1012 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
9a5b2f31 1013 "snps,u2exit_lfps_quirk");
3d128919 1014 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
b5a65c40 1015 "snps,u2ss_inp3_quirk");
3d128919 1016 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
df31f5b3 1017 "snps,req_p1p2p3_quirk");
3d128919 1018 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
a2a1d0f5 1019 "snps,del_p1p2p3_quirk");
3d128919 1020 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
41c06ffd 1021 "snps,del_phy_power_chg_quirk");
3d128919 1022 dwc->lfps_filter_quirk = device_property_read_bool(dev,
fb67afca 1023 "snps,lfps_filter_quirk");
3d128919 1024 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
14f4ac53 1025 "snps,rx_detect_poll_quirk");
3d128919 1026 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
59acfa20 1027 "snps,dis_u3_susphy_quirk");
3d128919 1028 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
0effe0a3 1029 "snps,dis_u2_susphy_quirk");
ec791d14
JY
1030 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1031 "snps,dis_enblslpm_quirk");
e58dd357
RB
1032 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1033 "snps,dis_rxdet_inp3_quirk");
16199f33
WW
1034 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1035 "snps,dis-u2-freeclk-exists-quirk");
00fe081d
WW
1036 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1037 "snps,dis-del-phy-power-chg-quirk");
6b6a0c9a 1038
3d128919 1039 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
6b6a0c9a 1040 "snps,tx_de_emphasis_quirk");
3d128919 1041 device_property_read_u8(dev, "snps,tx_de_emphasis",
6b6a0c9a 1042 &tx_de_emphasis);
3d128919
HK
1043 device_property_read_string(dev, "snps,hsphy_interface",
1044 &dwc->hsphy_interface);
1045 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
bcdb3272 1046 &dwc->fladj);
3d128919 1047
80caf7d2 1048 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
6b6a0c9a 1049 dwc->tx_de_emphasis = tx_de_emphasis;
80caf7d2 1050
460d098c
HR
1051 dwc->hird_threshold = hird_threshold
1052 | (dwc->is_utmi_l1_suspend << 4);
1053
6c89cce0 1054 platform_set_drvdata(pdev, dwc);
2917e718 1055 dwc3_cache_hwparams(dwc);
6c89cce0 1056
3c9f94ac
FB
1057 ret = dwc3_core_get_phy(dwc);
1058 if (ret)
3da1f6ee 1059 goto err0;
3c9f94ac 1060
72246da4 1061 spin_lock_init(&dwc->lock);
72246da4 1062
19bacdc9
HK
1063 if (!dev->dma_mask) {
1064 dev->dma_mask = dev->parent->dma_mask;
1065 dev->dma_parms = dev->parent->dma_parms;
1066 dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
1067 }
ddff14f1 1068
fc8bb91b
FB
1069 pm_runtime_set_active(dev);
1070 pm_runtime_use_autosuspend(dev);
1071 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
802ca850 1072 pm_runtime_enable(dev);
32808237
RQ
1073 ret = pm_runtime_get_sync(dev);
1074 if (ret < 0)
1075 goto err1;
1076
802ca850 1077 pm_runtime_forbid(dev);
72246da4 1078
3921426b
FB
1079 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1080 if (ret) {
1081 dev_err(dwc->dev, "failed to allocate event buffers\n");
1082 ret = -ENOMEM;
32808237 1083 goto err2;
3921426b
FB
1084 }
1085
9d6173e1
TN
1086 ret = dwc3_get_dr_mode(dwc);
1087 if (ret)
1088 goto err3;
32a4a135 1089
c499ff71
FB
1090 ret = dwc3_alloc_scratch_buffers(dwc);
1091 if (ret)
32808237 1092 goto err3;
c499ff71 1093
72246da4
FB
1094 ret = dwc3_core_init(dwc);
1095 if (ret) {
802ca850 1096 dev_err(dev, "failed to initialize core\n");
32808237 1097 goto err4;
72246da4
FB
1098 }
1099
77966eb8
JY
1100 /* Check the maximum_speed parameter */
1101 switch (dwc->maximum_speed) {
1102 case USB_SPEED_LOW:
1103 case USB_SPEED_FULL:
1104 case USB_SPEED_HIGH:
1105 case USB_SPEED_SUPER:
1106 case USB_SPEED_SUPER_PLUS:
1107 break;
1108 default:
1109 dev_err(dev, "invalid maximum_speed parameter %d\n",
1110 dwc->maximum_speed);
1111 /* fall through */
1112 case USB_SPEED_UNKNOWN:
1113 /* default to superspeed */
2c7f1bd9
JY
1114 dwc->maximum_speed = USB_SPEED_SUPER;
1115
1116 /*
1117 * default to superspeed plus if we are capable.
1118 */
1119 if (dwc3_is_usb31(dwc) &&
1120 (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
1121 DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1122 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
77966eb8
JY
1123
1124 break;
2c7f1bd9
JY
1125 }
1126
5f94adfe
FB
1127 ret = dwc3_core_init_mode(dwc);
1128 if (ret)
32808237 1129 goto err5;
72246da4 1130
4e9f3118 1131 dwc3_debugfs_init(dwc);
fc8bb91b 1132 pm_runtime_put(dev);
72246da4
FB
1133
1134 return 0;
1135
32808237 1136err5:
c499ff71 1137 dwc3_event_buffers_cleanup(dwc);
57303488 1138
32808237 1139err4:
c499ff71 1140 dwc3_free_scratch_buffers(dwc);
72246da4 1141
32808237 1142err3:
3921426b 1143 dwc3_free_event_buffers(dwc);
88bc9d19 1144 dwc3_ulpi_exit(dwc);
3921426b 1145
32808237
RQ
1146err2:
1147 pm_runtime_allow(&pdev->dev);
1148
1149err1:
1150 pm_runtime_put_sync(&pdev->dev);
1151 pm_runtime_disable(&pdev->dev);
1152
3da1f6ee
FB
1153err0:
1154 /*
1155 * restore res->start back to its original value so that, in case the
1156 * probe is deferred, we don't end up getting error in request the
1157 * memory region the next time probe is called.
1158 */
1159 res->start -= DWC3_GLOBALS_REGS_START;
1160
72246da4
FB
1161 return ret;
1162}
1163
fb4e98ab 1164static int dwc3_remove(struct platform_device *pdev)
72246da4 1165{
72246da4 1166 struct dwc3 *dwc = platform_get_drvdata(pdev);
3da1f6ee
FB
1167 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1168
fc8bb91b 1169 pm_runtime_get_sync(&pdev->dev);
3da1f6ee
FB
1170 /*
1171 * restore res->start back to its original value so that, in case the
1172 * probe is deferred, we don't end up getting error in request the
1173 * memory region the next time probe is called.
1174 */
1175 res->start -= DWC3_GLOBALS_REGS_START;
72246da4 1176
dc99f16f
FB
1177 dwc3_debugfs_exit(dwc);
1178 dwc3_core_exit_mode(dwc);
8ba007a9 1179
72246da4 1180 dwc3_core_exit(dwc);
88bc9d19 1181 dwc3_ulpi_exit(dwc);
72246da4 1182
16b972a5 1183 pm_runtime_put_sync(&pdev->dev);
fc8bb91b 1184 pm_runtime_allow(&pdev->dev);
72246da4
FB
1185 pm_runtime_disable(&pdev->dev);
1186
fc8bb91b
FB
1187 dwc3_free_event_buffers(dwc);
1188 dwc3_free_scratch_buffers(dwc);
1189
72246da4
FB
1190 return 0;
1191}
1192
fc8bb91b
FB
1193#ifdef CONFIG_PM
1194static int dwc3_suspend_common(struct dwc3 *dwc)
7415f17c 1195{
fc8bb91b 1196 unsigned long flags;
7415f17c 1197
a45c82b8
RK
1198 switch (dwc->dr_mode) {
1199 case USB_DR_MODE_PERIPHERAL:
1200 case USB_DR_MODE_OTG:
fc8bb91b 1201 spin_lock_irqsave(&dwc->lock, flags);
7415f17c 1202 dwc3_gadget_suspend(dwc);
fc8bb91b 1203 spin_unlock_irqrestore(&dwc->lock, flags);
51f5d49a 1204 break;
a45c82b8 1205 case USB_DR_MODE_HOST:
7415f17c 1206 default:
51f5d49a 1207 /* do nothing */
7415f17c
FB
1208 break;
1209 }
1210
51f5d49a 1211 dwc3_core_exit(dwc);
5c4ad318 1212
7415f17c
FB
1213 return 0;
1214}
1215
fc8bb91b 1216static int dwc3_resume_common(struct dwc3 *dwc)
7415f17c 1217{
fc8bb91b 1218 unsigned long flags;
57303488 1219 int ret;
7415f17c 1220
51f5d49a
FB
1221 ret = dwc3_core_init(dwc);
1222 if (ret)
5c4ad318
FB
1223 return ret;
1224
a45c82b8
RK
1225 switch (dwc->dr_mode) {
1226 case USB_DR_MODE_PERIPHERAL:
1227 case USB_DR_MODE_OTG:
fc8bb91b 1228 spin_lock_irqsave(&dwc->lock, flags);
7415f17c 1229 dwc3_gadget_resume(dwc);
fc8bb91b 1230 spin_unlock_irqrestore(&dwc->lock, flags);
7415f17c 1231 /* FALLTHROUGH */
a45c82b8 1232 case USB_DR_MODE_HOST:
7415f17c
FB
1233 default:
1234 /* do nothing */
1235 break;
1236 }
1237
fc8bb91b
FB
1238 return 0;
1239}
1240
1241static int dwc3_runtime_checks(struct dwc3 *dwc)
1242{
1243 switch (dwc->dr_mode) {
1244 case USB_DR_MODE_PERIPHERAL:
1245 case USB_DR_MODE_OTG:
1246 if (dwc->connected)
1247 return -EBUSY;
1248 break;
1249 case USB_DR_MODE_HOST:
1250 default:
1251 /* do nothing */
1252 break;
1253 }
1254
1255 return 0;
1256}
1257
1258static int dwc3_runtime_suspend(struct device *dev)
1259{
1260 struct dwc3 *dwc = dev_get_drvdata(dev);
1261 int ret;
1262
1263 if (dwc3_runtime_checks(dwc))
1264 return -EBUSY;
1265
1266 ret = dwc3_suspend_common(dwc);
1267 if (ret)
1268 return ret;
1269
1270 device_init_wakeup(dev, true);
1271
1272 return 0;
1273}
1274
1275static int dwc3_runtime_resume(struct device *dev)
1276{
1277 struct dwc3 *dwc = dev_get_drvdata(dev);
1278 int ret;
1279
1280 device_init_wakeup(dev, false);
1281
1282 ret = dwc3_resume_common(dwc);
1283 if (ret)
1284 return ret;
1285
1286 switch (dwc->dr_mode) {
1287 case USB_DR_MODE_PERIPHERAL:
1288 case USB_DR_MODE_OTG:
1289 dwc3_gadget_process_pending_events(dwc);
1290 break;
1291 case USB_DR_MODE_HOST:
1292 default:
1293 /* do nothing */
1294 break;
1295 }
1296
1297 pm_runtime_mark_last_busy(dev);
b74c2d87 1298 pm_runtime_put(dev);
fc8bb91b
FB
1299
1300 return 0;
1301}
1302
1303static int dwc3_runtime_idle(struct device *dev)
1304{
1305 struct dwc3 *dwc = dev_get_drvdata(dev);
1306
1307 switch (dwc->dr_mode) {
1308 case USB_DR_MODE_PERIPHERAL:
1309 case USB_DR_MODE_OTG:
1310 if (dwc3_runtime_checks(dwc))
1311 return -EBUSY;
1312 break;
1313 case USB_DR_MODE_HOST:
1314 default:
1315 /* do nothing */
1316 break;
1317 }
1318
1319 pm_runtime_mark_last_busy(dev);
1320 pm_runtime_autosuspend(dev);
1321
1322 return 0;
1323}
1324#endif /* CONFIG_PM */
1325
1326#ifdef CONFIG_PM_SLEEP
1327static int dwc3_suspend(struct device *dev)
1328{
1329 struct dwc3 *dwc = dev_get_drvdata(dev);
1330 int ret;
1331
1332 ret = dwc3_suspend_common(dwc);
1333 if (ret)
1334 return ret;
1335
1336 pinctrl_pm_select_sleep_state(dev);
1337
1338 return 0;
1339}
1340
1341static int dwc3_resume(struct device *dev)
1342{
1343 struct dwc3 *dwc = dev_get_drvdata(dev);
1344 int ret;
1345
1346 pinctrl_pm_select_default_state(dev);
1347
1348 ret = dwc3_resume_common(dwc);
1349 if (ret)
1350 return ret;
1351
7415f17c
FB
1352 pm_runtime_disable(dev);
1353 pm_runtime_set_active(dev);
1354 pm_runtime_enable(dev);
1355
1356 return 0;
1357}
7f370ed0 1358#endif /* CONFIG_PM_SLEEP */
7415f17c
FB
1359
1360static const struct dev_pm_ops dwc3_dev_pm_ops = {
7415f17c 1361 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
fc8bb91b
FB
1362 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
1363 dwc3_runtime_idle)
7415f17c
FB
1364};
1365
5088b6f5
KVA
1366#ifdef CONFIG_OF
1367static const struct of_device_id of_dwc3_match[] = {
22a5aa17
FB
1368 {
1369 .compatible = "snps,dwc3"
1370 },
5088b6f5
KVA
1371 {
1372 .compatible = "synopsys,dwc3"
1373 },
1374 { },
1375};
1376MODULE_DEVICE_TABLE(of, of_dwc3_match);
1377#endif
1378
404905a6
HK
1379#ifdef CONFIG_ACPI
1380
1381#define ACPI_ID_INTEL_BSW "808622B7"
1382
1383static const struct acpi_device_id dwc3_acpi_match[] = {
1384 { ACPI_ID_INTEL_BSW, 0 },
1385 { },
1386};
1387MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
1388#endif
1389
72246da4
FB
1390static struct platform_driver dwc3_driver = {
1391 .probe = dwc3_probe,
7690417d 1392 .remove = dwc3_remove,
72246da4
FB
1393 .driver = {
1394 .name = "dwc3",
5088b6f5 1395 .of_match_table = of_match_ptr(of_dwc3_match),
404905a6 1396 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
7f370ed0 1397 .pm = &dwc3_dev_pm_ops,
72246da4 1398 },
72246da4
FB
1399};
1400
b1116dcc
TK
1401module_platform_driver(dwc3_driver);
1402
7ae4fc4d 1403MODULE_ALIAS("platform:dwc3");
72246da4 1404MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
5945f789 1405MODULE_LICENSE("GPL v2");
72246da4 1406MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");