blob: 25e424e404624bc19a48a3537ecaec99ceee9c65 [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * core.c - DesignWare USB3 DRD Controller Core file
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
Felipe Balbi5945f782013-06-30 14:15:11 +03009 * 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.
Felipe Balbi72246da2011-08-19 18:10:58 +030012 *
Felipe Balbi5945f782013-06-30 14:15:11 +030013 * 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.
Felipe Balbi72246da2011-08-19 18:10:58 +030017 *
Felipe Balbi5945f782013-06-30 14:15:11 +030018 * 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/>.
Felipe Balbi72246da2011-08-19 18:10:58 +030020 */
21
Felipe Balbifa0ea132014-09-19 15:51:11 -050022#include <linux/version.h>
Felipe Balbia72e6582011-09-05 13:37:28 +030023#include <linux/module.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030024#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>
Felipe Balbi457e84b2012-01-18 18:04:09 +020035#include <linux/of.h>
Heikki Krogerus404905a2014-09-25 10:57:02 +030036#include <linux/acpi.h>
Sekhar Nori63444752015-08-31 21:09:08 +053037#include <linux/pinctrl/consumer.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030038
39#include <linux/usb/ch9.h>
40#include <linux/usb/gadget.h>
Felipe Balbif7e846f2013-06-30 14:29:51 +030041#include <linux/usb/of.h>
Ruchika Kharwara45c82b82013-07-06 07:52:49 -050042#include <linux/usb/otg.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030043
44#include "core.h"
45#include "gadget.h"
46#include "io.h"
47
48#include "debug.h"
49
Felipe Balbifc8bb912016-05-16 13:14:48 +030050#define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
Felipe Balbi8300dd22011-10-18 13:54:01 +030051
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +010052void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
53{
54 u32 reg;
55
56 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
57 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
58 reg |= DWC3_GCTL_PRTCAPDIR(mode);
59 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
60}
Felipe Balbi8300dd22011-10-18 13:54:01 +030061
Felipe Balbicf6d8672016-04-14 15:03:39 +030062u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
63{
64 struct dwc3 *dwc = dep->dwc;
65 u32 reg;
66
67 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
68 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
69 DWC3_GDBGFIFOSPACE_TYPE(type));
70
71 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
72
73 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
74}
75
Felipe Balbi72246da2011-08-19 18:10:58 +030076/**
77 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
78 * @dwc: pointer to our context structure
79 */
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +053080static int dwc3_core_soft_reset(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +030081{
82 u32 reg;
Felipe Balbif59dcab2016-03-11 10:51:52 +020083 int retries = 1000;
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +053084 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +030085
Felipe Balbi51e1e7b2012-07-19 14:09:48 +030086 usb_phy_init(dwc->usb2_phy);
87 usb_phy_init(dwc->usb3_phy);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +053088 ret = phy_init(dwc->usb2_generic_phy);
89 if (ret < 0)
90 return ret;
91
92 ret = phy_init(dwc->usb3_generic_phy);
93 if (ret < 0) {
94 phy_exit(dwc->usb2_generic_phy);
95 return ret;
96 }
Felipe Balbi72246da2011-08-19 18:10:58 +030097
Felipe Balbif59dcab2016-03-11 10:51:52 +020098 /*
99 * We're resetting only the device side because, if we're in host mode,
100 * XHCI driver will reset the host block. If dwc3 was configured for
101 * host-only mode, then we can return early.
102 */
103 if (dwc->dr_mode == USB_DR_MODE_HOST)
104 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300105
Felipe Balbif59dcab2016-03-11 10:51:52 +0200106 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
107 reg |= DWC3_DCTL_CSFTRST;
108 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300109
Felipe Balbif59dcab2016-03-11 10:51:52 +0200110 do {
111 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
112 if (!(reg & DWC3_DCTL_CSFTRST))
113 return 0;
Pratyush Anand45627ac2012-06-21 17:44:28 +0530114
Felipe Balbif59dcab2016-03-11 10:51:52 +0200115 udelay(1);
116 } while (--retries);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530117
Felipe Balbif59dcab2016-03-11 10:51:52 +0200118 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +0300119}
120
121/**
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300122 * dwc3_soft_reset - Issue soft reset
123 * @dwc: Pointer to our controller context structure
124 */
125static int dwc3_soft_reset(struct dwc3 *dwc)
126{
127 unsigned long timeout;
128 u32 reg;
129
130 timeout = jiffies + msecs_to_jiffies(500);
131 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
132 do {
133 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
134 if (!(reg & DWC3_DCTL_CSFTRST))
135 break;
136
137 if (time_after(jiffies, timeout)) {
138 dev_err(dwc->dev, "Reset Timed Out\n");
139 return -ETIMEDOUT;
140 }
141
142 cpu_relax();
143 } while (true);
144
145 return 0;
146}
147
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530148/*
149 * dwc3_frame_length_adjustment - Adjusts frame length if required
150 * @dwc3: Pointer to our controller context structure
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530151 */
Felipe Balbibcdb3272016-05-16 10:42:23 +0300152static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530153{
154 u32 reg;
155 u32 dft;
156
157 if (dwc->revision < DWC3_REVISION_250A)
158 return;
159
Felipe Balbibcdb3272016-05-16 10:42:23 +0300160 if (dwc->fladj == 0)
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530161 return;
162
163 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
164 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
Felipe Balbibcdb3272016-05-16 10:42:23 +0300165 if (!dev_WARN_ONCE(dwc->dev, dft == dwc->fladj,
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530166 "request value same as default, ignoring\n")) {
167 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
Felipe Balbibcdb3272016-05-16 10:42:23 +0300168 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530169 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
170 }
171}
172
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300173/**
Felipe Balbi72246da2011-08-19 18:10:58 +0300174 * dwc3_free_one_event_buffer - Frees one event buffer
175 * @dwc: Pointer to our controller context structure
176 * @evt: Pointer to event buffer to be freed
177 */
178static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
179 struct dwc3_event_buffer *evt)
180{
181 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300182}
183
184/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800185 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300186 * @dwc: Pointer to our controller context structure
187 * @length: size of the event buffer
188 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800189 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300190 * otherwise ERR_PTR(errno).
191 */
Felipe Balbi67d0b502013-02-22 16:31:07 +0200192static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
193 unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300194{
195 struct dwc3_event_buffer *evt;
196
Felipe Balbi380f0d22012-10-11 13:48:36 +0300197 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300198 if (!evt)
199 return ERR_PTR(-ENOMEM);
200
201 evt->dwc = dwc;
202 evt->length = length;
203 evt->buf = dma_alloc_coherent(dwc->dev, length,
204 &evt->dma, GFP_KERNEL);
Felipe Balbie32672f2012-11-08 15:26:41 +0200205 if (!evt->buf)
Felipe Balbi72246da2011-08-19 18:10:58 +0300206 return ERR_PTR(-ENOMEM);
Felipe Balbi72246da2011-08-19 18:10:58 +0300207
208 return evt;
209}
210
211/**
212 * dwc3_free_event_buffers - frees all allocated event buffers
213 * @dwc: Pointer to our controller context structure
214 */
215static void dwc3_free_event_buffers(struct dwc3 *dwc)
216{
217 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300218
Felipe Balbi696c8b12016-03-30 09:37:03 +0300219 evt = dwc->ev_buf;
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300220 if (evt)
221 dwc3_free_one_event_buffer(dwc, evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300222}
223
224/**
225 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800226 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300227 * @length: size of event buffer
228 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800229 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300230 * may contain some buffers allocated but not all which were requested.
231 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500232static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300233{
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300234 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300235
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300236 evt = dwc3_alloc_one_event_buffer(dwc, length);
237 if (IS_ERR(evt)) {
238 dev_err(dwc->dev, "can't allocate event buffer\n");
239 return PTR_ERR(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300240 }
Felipe Balbi696c8b12016-03-30 09:37:03 +0300241 dwc->ev_buf = evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300242
243 return 0;
244}
245
246/**
247 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800248 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300249 *
250 * Returns 0 on success otherwise negative errno.
251 */
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300252static int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300253{
254 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300255
Felipe Balbi696c8b12016-03-30 09:37:03 +0300256 evt = dwc->ev_buf;
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300257 dwc3_trace(trace_dwc3_core,
258 "Event buf %p dma %08llx length %d\n",
259 evt->buf, (unsigned long long) evt->dma,
260 evt->length);
Felipe Balbi72246da2011-08-19 18:10:58 +0300261
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300262 evt->lpos = 0;
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300263
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300264 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
265 lower_32_bits(evt->dma));
266 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
267 upper_32_bits(evt->dma));
268 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
269 DWC3_GEVNTSIZ_SIZE(evt->length));
270 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300271
272 return 0;
273}
274
275static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
276{
277 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300278
Felipe Balbi696c8b12016-03-30 09:37:03 +0300279 evt = dwc->ev_buf;
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300280
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300281 evt->lpos = 0;
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300282
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300283 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
284 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
285 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
286 | DWC3_GEVNTSIZ_SIZE(0));
287 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300288}
289
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600290static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
291{
292 if (!dwc->has_hibernation)
293 return 0;
294
295 if (!dwc->nr_scratch)
296 return 0;
297
298 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
299 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
300 if (!dwc->scratchbuf)
301 return -ENOMEM;
302
303 return 0;
304}
305
306static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
307{
308 dma_addr_t scratch_addr;
309 u32 param;
310 int ret;
311
312 if (!dwc->has_hibernation)
313 return 0;
314
315 if (!dwc->nr_scratch)
316 return 0;
317
318 /* should never fall here */
319 if (!WARN_ON(dwc->scratchbuf))
320 return 0;
321
322 scratch_addr = dma_map_single(dwc->dev, dwc->scratchbuf,
323 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
324 DMA_BIDIRECTIONAL);
325 if (dma_mapping_error(dwc->dev, scratch_addr)) {
326 dev_err(dwc->dev, "failed to map scratch buffer\n");
327 ret = -EFAULT;
328 goto err0;
329 }
330
331 dwc->scratch_addr = scratch_addr;
332
333 param = lower_32_bits(scratch_addr);
334
335 ret = dwc3_send_gadget_generic_command(dwc,
336 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
337 if (ret < 0)
338 goto err1;
339
340 param = upper_32_bits(scratch_addr);
341
342 ret = dwc3_send_gadget_generic_command(dwc,
343 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
344 if (ret < 0)
345 goto err1;
346
347 return 0;
348
349err1:
350 dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
351 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
352
353err0:
354 return ret;
355}
356
357static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
358{
359 if (!dwc->has_hibernation)
360 return;
361
362 if (!dwc->nr_scratch)
363 return;
364
365 /* should never fall here */
366 if (!WARN_ON(dwc->scratchbuf))
367 return;
368
369 dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
370 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
371 kfree(dwc->scratchbuf);
372}
373
Felipe Balbi789451f62011-05-05 15:53:10 +0300374static void dwc3_core_num_eps(struct dwc3 *dwc)
375{
376 struct dwc3_hwparams *parms = &dwc->hwparams;
377
378 dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
379 dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
380
Felipe Balbi73815282015-01-27 13:48:14 -0600381 dwc3_trace(trace_dwc3_core, "found %d IN and %d OUT endpoints",
Felipe Balbi789451f62011-05-05 15:53:10 +0300382 dwc->num_in_eps, dwc->num_out_eps);
383}
384
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500385static void dwc3_cache_hwparams(struct dwc3 *dwc)
Felipe Balbi26ceca92011-09-30 10:58:49 +0300386{
387 struct dwc3_hwparams *parms = &dwc->hwparams;
388
389 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
390 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
391 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
392 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
393 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
394 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
395 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
396 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
397 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
398}
399
Felipe Balbi72246da2011-08-19 18:10:58 +0300400/**
Huang Ruib5a65c42014-10-28 19:54:28 +0800401 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
402 * @dwc: Pointer to our controller context structure
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300403 *
404 * Returns 0 on success. The USB PHY interfaces are configured but not
405 * initialized. The PHY interfaces and the PHYs get initialized together with
406 * the core in dwc3_core_init.
Huang Ruib5a65c42014-10-28 19:54:28 +0800407 */
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300408static int dwc3_phy_setup(struct dwc3 *dwc)
Huang Ruib5a65c42014-10-28 19:54:28 +0800409{
410 u32 reg;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300411 int ret;
Huang Ruib5a65c42014-10-28 19:54:28 +0800412
413 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
414
Huang Rui2164a472014-10-28 19:54:35 +0800415 /*
416 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
417 * to '0' during coreConsultant configuration. So default value
418 * will be '0' when the core is reset. Application needs to set it
419 * to '1' after the core initialization is completed.
420 */
421 if (dwc->revision > DWC3_REVISION_194A)
422 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
423
Huang Ruib5a65c42014-10-28 19:54:28 +0800424 if (dwc->u2ss_inp3_quirk)
425 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
426
Rajesh Bhagate58dd352016-03-14 14:40:50 +0530427 if (dwc->dis_rxdet_inp3_quirk)
428 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
429
Huang Ruidf31f5b2014-10-28 19:54:29 +0800430 if (dwc->req_p1p2p3_quirk)
431 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
432
Huang Ruia2a1d0f2014-10-28 19:54:30 +0800433 if (dwc->del_p1p2p3_quirk)
434 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
435
Huang Rui41c06ff2014-10-28 19:54:31 +0800436 if (dwc->del_phy_power_chg_quirk)
437 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
438
Huang Ruifb67afc2014-10-28 19:54:32 +0800439 if (dwc->lfps_filter_quirk)
440 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
441
Huang Rui14f4ac52014-10-28 19:54:33 +0800442 if (dwc->rx_detect_poll_quirk)
443 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
444
Huang Rui6b6a0c92014-10-31 11:11:12 +0800445 if (dwc->tx_de_emphasis_quirk)
446 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
447
Felipe Balbicd72f892014-11-06 11:31:00 -0600448 if (dwc->dis_u3_susphy_quirk)
Huang Rui59acfa22014-10-31 11:11:13 +0800449 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
450
William Wu00fe0812016-08-16 22:44:39 +0800451 if (dwc->dis_del_phy_power_chg_quirk)
452 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
453
Huang Ruib5a65c42014-10-28 19:54:28 +0800454 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
455
Huang Rui2164a472014-10-28 19:54:35 +0800456 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
457
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300458 /* Select the HS PHY interface */
459 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
460 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
Felipe Balbi43cacb02015-07-01 22:03:09 -0500461 if (dwc->hsphy_interface &&
462 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300463 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300464 break;
Felipe Balbi43cacb02015-07-01 22:03:09 -0500465 } else if (dwc->hsphy_interface &&
466 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300467 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300468 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300469 } else {
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300470 /* Relying on default value. */
471 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
472 break;
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300473 }
474 /* FALLTHROUGH */
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300475 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
476 /* Making sure the interface and PHY are operational */
477 ret = dwc3_soft_reset(dwc);
478 if (ret)
479 return ret;
480
481 udelay(1);
482
483 ret = dwc3_ulpi_init(dwc);
484 if (ret)
485 return ret;
486 /* FALLTHROUGH */
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300487 default:
488 break;
489 }
490
William Wu32f2ed82016-08-16 22:44:38 +0800491 switch (dwc->hsphy_mode) {
492 case USBPHY_INTERFACE_MODE_UTMI:
493 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
494 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
495 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
496 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
497 break;
498 case USBPHY_INTERFACE_MODE_UTMIW:
499 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
500 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
501 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
502 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
503 break;
504 default:
505 break;
506 }
507
Huang Rui2164a472014-10-28 19:54:35 +0800508 /*
509 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
510 * '0' during coreConsultant configuration. So default value will
511 * be '0' when the core is reset. Application needs to set it to
512 * '1' after the core initialization is completed.
513 */
514 if (dwc->revision > DWC3_REVISION_194A)
515 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
516
Felipe Balbicd72f892014-11-06 11:31:00 -0600517 if (dwc->dis_u2_susphy_quirk)
Huang Rui0effe0a2014-10-31 11:11:14 +0800518 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
519
John Younec791d12015-10-02 20:30:57 -0700520 if (dwc->dis_enblslpm_quirk)
521 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
522
William Wu16199f32016-08-16 22:44:37 +0800523 if (dwc->dis_u2_freeclk_exists_quirk)
524 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
525
Huang Rui2164a472014-10-28 19:54:35 +0800526 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300527
528 return 0;
Huang Ruib5a65c42014-10-28 19:54:28 +0800529}
530
Felipe Balbic499ff72016-05-16 10:49:01 +0300531static void dwc3_core_exit(struct dwc3 *dwc)
532{
533 dwc3_event_buffers_cleanup(dwc);
534
535 usb_phy_shutdown(dwc->usb2_phy);
536 usb_phy_shutdown(dwc->usb3_phy);
537 phy_exit(dwc->usb2_generic_phy);
538 phy_exit(dwc->usb3_generic_phy);
539
540 usb_phy_set_suspend(dwc->usb2_phy, 1);
541 usb_phy_set_suspend(dwc->usb3_phy, 1);
542 phy_power_off(dwc->usb2_generic_phy);
543 phy_power_off(dwc->usb3_generic_phy);
544}
545
Huang Ruib5a65c42014-10-28 19:54:28 +0800546/**
Felipe Balbi72246da2011-08-19 18:10:58 +0300547 * dwc3_core_init - Low-level initialization of DWC3 Core
548 * @dwc: Pointer to our controller context structure
549 *
550 * Returns 0 on success otherwise negative errno.
551 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500552static int dwc3_core_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300553{
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600554 u32 hwparams4 = dwc->hwparams.hwparams4;
Felipe Balbi72246da2011-08-19 18:10:58 +0300555 u32 reg;
556 int ret;
557
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200558 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
559 /* This should read as U3 followed by revision number */
John Youn690fb372015-09-04 19:15:10 -0700560 if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
561 /* Detected DWC_usb3 IP */
562 dwc->revision = reg;
563 } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
564 /* Detected DWC_usb31 IP */
565 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
566 dwc->revision |= DWC3_REVISION_IS_DWC31;
567 } else {
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200568 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
569 ret = -ENODEV;
570 goto err0;
571 }
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200572
Felipe Balbifa0ea132014-09-19 15:51:11 -0500573 /*
574 * Write Linux Version Code to our GUID register so it's easy to figure
575 * out which kernel version a bug was found.
576 */
577 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
578
Paul Zimmerman0e1e5c42014-05-23 11:39:24 -0700579 /* Handle USB2.0-only core configuration */
580 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
581 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
582 if (dwc->maximum_speed == USB_SPEED_SUPER)
583 dwc->maximum_speed = USB_SPEED_HIGH;
584 }
585
Felipe Balbi72246da2011-08-19 18:10:58 +0300586 /* issue device SoftReset too */
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300587 ret = dwc3_soft_reset(dwc);
588 if (ret)
589 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300590
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530591 ret = dwc3_core_soft_reset(dwc);
592 if (ret)
593 goto err0;
Pratyush Anand58a0f232012-06-21 17:44:29 +0530594
Felipe Balbic499ff72016-05-16 10:49:01 +0300595 ret = dwc3_phy_setup(dwc);
596 if (ret)
597 goto err0;
598
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100599 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800600 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100601
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100602 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100603 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
Felipe Balbi32a4a132014-02-25 14:00:13 -0600604 /**
605 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
606 * issue which would cause xHCI compliance tests to fail.
607 *
608 * Because of that we cannot enable clock gating on such
609 * configurations.
610 *
611 * Refers to:
612 *
613 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
614 * SOF/ITP Mode Used
615 */
616 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
617 dwc->dr_mode == USB_DR_MODE_OTG) &&
618 (dwc->revision >= DWC3_REVISION_210A &&
619 dwc->revision <= DWC3_REVISION_250A))
620 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
621 else
622 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100623 break;
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600624 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
625 /* enable hibernation here */
626 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
Huang Rui2eac3992014-10-28 19:54:22 +0800627
628 /*
629 * REVISIT Enabling this bit so that host-mode hibernation
630 * will work. Device-mode hibernation is not yet implemented.
631 */
632 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600633 break;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100634 default:
Felipe Balbi1407bf12015-11-16 16:06:37 -0600635 dwc3_trace(trace_dwc3_core, "No power optimization available\n");
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100636 }
637
Huang Rui946bd572014-10-28 19:54:23 +0800638 /* check if current dwc3 is on simulation board */
639 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
Felipe Balbi1407bf12015-11-16 16:06:37 -0600640 dwc3_trace(trace_dwc3_core,
641 "running on FPGA platform\n");
Huang Rui946bd572014-10-28 19:54:23 +0800642 dwc->is_fpga = true;
643 }
644
Huang Rui3b812212014-10-28 19:54:25 +0800645 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
646 "disable_scramble cannot be used on non-FPGA builds\n");
647
648 if (dwc->disable_scramble_quirk && dwc->is_fpga)
649 reg |= DWC3_GCTL_DISSCRAMBLE;
650 else
651 reg &= ~DWC3_GCTL_DISSCRAMBLE;
652
Huang Rui9a5b2f32014-10-28 19:54:27 +0800653 if (dwc->u2exit_lfps_quirk)
654 reg |= DWC3_GCTL_U2EXIT_LFPS;
655
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100656 /*
657 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800658 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100659 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800660 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100661 */
662 if (dwc->revision < DWC3_REVISION_190A)
663 reg |= DWC3_GCTL_U2RSTECN;
664
665 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
666
Felipe Balbic499ff72016-05-16 10:49:01 +0300667 dwc3_core_num_eps(dwc);
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600668
669 ret = dwc3_setup_scratch_buffers(dwc);
670 if (ret)
Felipe Balbic499ff72016-05-16 10:49:01 +0300671 goto err1;
672
673 /* Adjust Frame Length */
674 dwc3_frame_length_adjustment(dwc);
675
676 usb_phy_set_suspend(dwc->usb2_phy, 0);
677 usb_phy_set_suspend(dwc->usb3_phy, 0);
678 ret = phy_power_on(dwc->usb2_generic_phy);
679 if (ret < 0)
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600680 goto err2;
681
Felipe Balbic499ff72016-05-16 10:49:01 +0300682 ret = phy_power_on(dwc->usb3_generic_phy);
683 if (ret < 0)
684 goto err3;
685
686 ret = dwc3_event_buffers_setup(dwc);
687 if (ret) {
688 dev_err(dwc->dev, "failed to setup event buffers\n");
689 goto err4;
690 }
691
Baolin Wang00af6232016-07-15 17:13:27 +0800692 switch (dwc->dr_mode) {
693 case USB_DR_MODE_PERIPHERAL:
694 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
695 break;
696 case USB_DR_MODE_HOST:
697 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
698 break;
699 case USB_DR_MODE_OTG:
700 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
701 break;
702 default:
703 dev_warn(dwc->dev, "Unsupported mode %d\n", dwc->dr_mode);
704 break;
705 }
706
Felipe Balbi72246da2011-08-19 18:10:58 +0300707 return 0;
708
Felipe Balbic499ff72016-05-16 10:49:01 +0300709err4:
710 phy_power_off(dwc->usb2_generic_phy);
711
712err3:
713 phy_power_off(dwc->usb3_generic_phy);
714
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600715err2:
Felipe Balbic499ff72016-05-16 10:49:01 +0300716 usb_phy_set_suspend(dwc->usb2_phy, 1);
717 usb_phy_set_suspend(dwc->usb3_phy, 1);
718 dwc3_core_exit(dwc);
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600719
720err1:
721 usb_phy_shutdown(dwc->usb2_phy);
722 usb_phy_shutdown(dwc->usb3_phy);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530723 phy_exit(dwc->usb2_generic_phy);
724 phy_exit(dwc->usb3_generic_phy);
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600725
Felipe Balbi72246da2011-08-19 18:10:58 +0300726err0:
727 return ret;
728}
729
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500730static int dwc3_core_get_phy(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300731{
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500732 struct device *dev = dwc->dev;
Felipe Balbi941ea362013-07-31 09:21:25 +0300733 struct device_node *node = dev->of_node;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500734 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300735
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530736 if (node) {
737 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
738 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
Felipe Balbibb674902013-08-14 13:21:23 -0500739 } else {
740 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
741 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530742 }
743
Felipe Balbid105e7f2013-03-15 10:52:08 +0200744 if (IS_ERR(dwc->usb2_phy)) {
745 ret = PTR_ERR(dwc->usb2_phy);
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530746 if (ret == -ENXIO || ret == -ENODEV) {
747 dwc->usb2_phy = NULL;
748 } else if (ret == -EPROBE_DEFER) {
Felipe Balbid105e7f2013-03-15 10:52:08 +0200749 return ret;
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530750 } else {
751 dev_err(dev, "no usb2 phy configured\n");
752 return ret;
753 }
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300754 }
755
Felipe Balbid105e7f2013-03-15 10:52:08 +0200756 if (IS_ERR(dwc->usb3_phy)) {
Ruchika Kharwar315955d72013-07-04 00:59:34 -0500757 ret = PTR_ERR(dwc->usb3_phy);
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530758 if (ret == -ENXIO || ret == -ENODEV) {
759 dwc->usb3_phy = NULL;
760 } else if (ret == -EPROBE_DEFER) {
Felipe Balbid105e7f2013-03-15 10:52:08 +0200761 return ret;
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530762 } else {
763 dev_err(dev, "no usb3 phy configured\n");
764 return ret;
765 }
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300766 }
767
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530768 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
769 if (IS_ERR(dwc->usb2_generic_phy)) {
770 ret = PTR_ERR(dwc->usb2_generic_phy);
771 if (ret == -ENOSYS || ret == -ENODEV) {
772 dwc->usb2_generic_phy = NULL;
773 } else if (ret == -EPROBE_DEFER) {
774 return ret;
775 } else {
776 dev_err(dev, "no usb2 phy configured\n");
777 return ret;
778 }
779 }
780
781 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
782 if (IS_ERR(dwc->usb3_generic_phy)) {
783 ret = PTR_ERR(dwc->usb3_generic_phy);
784 if (ret == -ENOSYS || ret == -ENODEV) {
785 dwc->usb3_generic_phy = NULL;
786 } else if (ret == -EPROBE_DEFER) {
787 return ret;
788 } else {
789 dev_err(dev, "no usb3 phy configured\n");
790 return ret;
791 }
792 }
793
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500794 return 0;
795}
796
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500797static int dwc3_core_init_mode(struct dwc3 *dwc)
798{
799 struct device *dev = dwc->dev;
800 int ret;
801
802 switch (dwc->dr_mode) {
803 case USB_DR_MODE_PERIPHERAL:
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500804 ret = dwc3_gadget_init(dwc);
805 if (ret) {
Roger Quadros9522def2016-06-10 14:48:38 +0300806 if (ret != -EPROBE_DEFER)
807 dev_err(dev, "failed to initialize gadget\n");
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500808 return ret;
809 }
810 break;
811 case USB_DR_MODE_HOST:
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500812 ret = dwc3_host_init(dwc);
813 if (ret) {
Roger Quadros9522def2016-06-10 14:48:38 +0300814 if (ret != -EPROBE_DEFER)
815 dev_err(dev, "failed to initialize host\n");
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500816 return ret;
817 }
818 break;
819 case USB_DR_MODE_OTG:
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500820 ret = dwc3_host_init(dwc);
821 if (ret) {
Roger Quadros9522def2016-06-10 14:48:38 +0300822 if (ret != -EPROBE_DEFER)
823 dev_err(dev, "failed to initialize host\n");
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500824 return ret;
825 }
826
827 ret = dwc3_gadget_init(dwc);
828 if (ret) {
Roger Quadros9522def2016-06-10 14:48:38 +0300829 if (ret != -EPROBE_DEFER)
830 dev_err(dev, "failed to initialize gadget\n");
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500831 return ret;
832 }
833 break;
834 default:
835 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
836 return -EINVAL;
837 }
838
839 return 0;
840}
841
842static void dwc3_core_exit_mode(struct dwc3 *dwc)
843{
844 switch (dwc->dr_mode) {
845 case USB_DR_MODE_PERIPHERAL:
846 dwc3_gadget_exit(dwc);
847 break;
848 case USB_DR_MODE_HOST:
849 dwc3_host_exit(dwc);
850 break;
851 case USB_DR_MODE_OTG:
852 dwc3_host_exit(dwc);
853 dwc3_gadget_exit(dwc);
854 break;
855 default:
856 /* do nothing */
857 break;
858 }
859}
860
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500861#define DWC3_ALIGN_MASK (16 - 1)
862
863static int dwc3_probe(struct platform_device *pdev)
864{
865 struct device *dev = &pdev->dev;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500866 struct resource *res;
867 struct dwc3 *dwc;
Huang Rui80caf7d2014-10-28 19:54:26 +0800868 u8 lpm_nyet_threshold;
Huang Rui6b6a0c92014-10-31 11:11:12 +0800869 u8 tx_de_emphasis;
Huang Rui460d0982014-10-31 11:11:18 +0800870 u8 hird_threshold;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500871
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300872 int ret;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500873
874 void __iomem *regs;
875 void *mem;
876
877 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +0900878 if (!mem)
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500879 return -ENOMEM;
Jingoo Han734d5a52014-07-17 12:45:11 +0900880
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500881 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
882 dwc->mem = mem;
883 dwc->dev = dev;
884
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500885 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
886 if (!res) {
887 dev_err(dev, "missing memory resource\n");
888 return -ENODEV;
889 }
890
Vivek Gautamf32a5e22014-06-04 14:34:52 +0530891 dwc->xhci_resources[0].start = res->start;
892 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
893 DWC3_XHCI_REGS_END;
894 dwc->xhci_resources[0].flags = res->flags;
895 dwc->xhci_resources[0].name = res->name;
896
897 res->start += DWC3_GLOBALS_REGS_START;
898
899 /*
900 * Request memory region but exclude xHCI regs,
901 * since it will be requested by the xhci-plat driver.
902 */
903 regs = devm_ioremap_resource(dev, res);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -0500904 if (IS_ERR(regs)) {
905 ret = PTR_ERR(regs);
906 goto err0;
907 }
Vivek Gautamf32a5e22014-06-04 14:34:52 +0530908
909 dwc->regs = regs;
910 dwc->regs_size = resource_size(res);
Vivek Gautamf32a5e22014-06-04 14:34:52 +0530911
Huang Rui80caf7d2014-10-28 19:54:26 +0800912 /* default to highest possible threshold */
913 lpm_nyet_threshold = 0xff;
914
Huang Rui6b6a0c92014-10-31 11:11:12 +0800915 /* default to -3.5dB de-emphasis */
916 tx_de_emphasis = 1;
917
Huang Rui460d0982014-10-31 11:11:18 +0800918 /*
919 * default to assert utmi_sleep_n and use maximum allowed HIRD
920 * threshold value of 0b1100
921 */
922 hird_threshold = 12;
923
Heikki Krogerus63863b92015-09-21 11:14:32 +0300924 dwc->maximum_speed = usb_get_maximum_speed(dev);
Heikki Krogerus06e71142015-09-21 11:14:34 +0300925 dwc->dr_mode = usb_get_dr_mode(dev);
William Wu32f2ed82016-08-16 22:44:38 +0800926 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
Heikki Krogerus63863b92015-09-21 11:14:32 +0300927
Heikki Krogerus3d128912015-09-21 11:14:35 +0300928 dwc->has_lpm_erratum = device_property_read_bool(dev,
Huang Rui80caf7d2014-10-28 19:54:26 +0800929 "snps,has-lpm-erratum");
Heikki Krogerus3d128912015-09-21 11:14:35 +0300930 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
Huang Rui80caf7d2014-10-28 19:54:26 +0800931 &lpm_nyet_threshold);
Heikki Krogerus3d128912015-09-21 11:14:35 +0300932 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
Huang Rui460d0982014-10-31 11:11:18 +0800933 "snps,is-utmi-l1-suspend");
Heikki Krogerus3d128912015-09-21 11:14:35 +0300934 device_property_read_u8(dev, "snps,hird-threshold",
Huang Rui460d0982014-10-31 11:11:18 +0800935 &hird_threshold);
Heikki Krogerus3d128912015-09-21 11:14:35 +0300936 dwc->usb3_lpm_capable = device_property_read_bool(dev,
Robert Baldygaeac68e82015-03-09 15:06:12 +0100937 "snps,usb3_lpm_capable");
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500938
Heikki Krogerus3d128912015-09-21 11:14:35 +0300939 dwc->disable_scramble_quirk = device_property_read_bool(dev,
Huang Rui3b812212014-10-28 19:54:25 +0800940 "snps,disable_scramble_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +0300941 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
Huang Rui9a5b2f32014-10-28 19:54:27 +0800942 "snps,u2exit_lfps_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +0300943 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
Huang Ruib5a65c42014-10-28 19:54:28 +0800944 "snps,u2ss_inp3_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +0300945 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
Huang Ruidf31f5b2014-10-28 19:54:29 +0800946 "snps,req_p1p2p3_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +0300947 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
Huang Ruia2a1d0f2014-10-28 19:54:30 +0800948 "snps,del_p1p2p3_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +0300949 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
Huang Rui41c06ff2014-10-28 19:54:31 +0800950 "snps,del_phy_power_chg_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +0300951 dwc->lfps_filter_quirk = device_property_read_bool(dev,
Huang Ruifb67afc2014-10-28 19:54:32 +0800952 "snps,lfps_filter_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +0300953 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
Huang Rui14f4ac52014-10-28 19:54:33 +0800954 "snps,rx_detect_poll_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +0300955 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
Huang Rui59acfa22014-10-31 11:11:13 +0800956 "snps,dis_u3_susphy_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +0300957 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
Huang Rui0effe0a2014-10-31 11:11:14 +0800958 "snps,dis_u2_susphy_quirk");
John Younec791d12015-10-02 20:30:57 -0700959 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
960 "snps,dis_enblslpm_quirk");
Rajesh Bhagate58dd352016-03-14 14:40:50 +0530961 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
962 "snps,dis_rxdet_inp3_quirk");
William Wu16199f32016-08-16 22:44:37 +0800963 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
964 "snps,dis-u2-freeclk-exists-quirk");
William Wu00fe0812016-08-16 22:44:39 +0800965 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
966 "snps,dis-del-phy-power-chg-quirk");
Huang Rui6b6a0c92014-10-31 11:11:12 +0800967
Heikki Krogerus3d128912015-09-21 11:14:35 +0300968 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
Huang Rui6b6a0c92014-10-31 11:11:12 +0800969 "snps,tx_de_emphasis_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +0300970 device_property_read_u8(dev, "snps,tx_de_emphasis",
Huang Rui6b6a0c92014-10-31 11:11:12 +0800971 &tx_de_emphasis);
Heikki Krogerus3d128912015-09-21 11:14:35 +0300972 device_property_read_string(dev, "snps,hsphy_interface",
973 &dwc->hsphy_interface);
974 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
Felipe Balbibcdb3272016-05-16 10:42:23 +0300975 &dwc->fladj);
Heikki Krogerus3d128912015-09-21 11:14:35 +0300976
Huang Rui80caf7d2014-10-28 19:54:26 +0800977 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
Huang Rui6b6a0c92014-10-31 11:11:12 +0800978 dwc->tx_de_emphasis = tx_de_emphasis;
Huang Rui80caf7d2014-10-28 19:54:26 +0800979
Huang Rui460d0982014-10-31 11:11:18 +0800980 dwc->hird_threshold = hird_threshold
981 | (dwc->is_utmi_l1_suspend << 4);
982
Heikki Krogerus6c89cce02015-05-13 15:26:45 +0300983 platform_set_drvdata(pdev, dwc);
Heikki Krogerus2917e712015-05-13 15:26:46 +0300984 dwc3_cache_hwparams(dwc);
Heikki Krogerus6c89cce02015-05-13 15:26:45 +0300985
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500986 ret = dwc3_core_get_phy(dwc);
987 if (ret)
Felipe Balbi3da1f6e2014-09-02 15:19:43 -0500988 goto err0;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500989
Felipe Balbi72246da2011-08-19 18:10:58 +0300990 spin_lock_init(&dwc->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +0300991
Heikki Krogerus19bacdc2014-09-24 11:00:38 +0300992 if (!dev->dma_mask) {
993 dev->dma_mask = dev->parent->dma_mask;
994 dev->dma_parms = dev->parent->dma_parms;
995 dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
996 }
Kishon Vijay Abraham Iddff14f2013-03-07 18:51:43 +0530997
Felipe Balbifc8bb912016-05-16 13:14:48 +0300998 pm_runtime_set_active(dev);
999 pm_runtime_use_autosuspend(dev);
1000 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
Chanho Park802ca852012-02-15 18:27:55 +09001001 pm_runtime_enable(dev);
Roger Quadros32808232016-06-10 14:38:02 +03001002 ret = pm_runtime_get_sync(dev);
1003 if (ret < 0)
1004 goto err1;
1005
Chanho Park802ca852012-02-15 18:27:55 +09001006 pm_runtime_forbid(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001007
Felipe Balbi39214262012-10-11 13:54:36 +03001008 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1009 if (ret) {
1010 dev_err(dwc->dev, "failed to allocate event buffers\n");
1011 ret = -ENOMEM;
Roger Quadros32808232016-06-10 14:38:02 +03001012 goto err2;
Felipe Balbi39214262012-10-11 13:54:36 +03001013 }
1014
Felipe Balbi5f822792016-06-07 12:55:19 +03001015 if (IS_ENABLED(CONFIG_USB_DWC3_HOST) &&
1016 (dwc->dr_mode == USB_DR_MODE_OTG ||
1017 dwc->dr_mode == USB_DR_MODE_UNKNOWN))
Felipe Balbi32a4a132014-02-25 14:00:13 -06001018 dwc->dr_mode = USB_DR_MODE_HOST;
Felipe Balbi5f822792016-06-07 12:55:19 +03001019 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET) &&
1020 (dwc->dr_mode == USB_DR_MODE_OTG ||
1021 dwc->dr_mode == USB_DR_MODE_UNKNOWN))
Felipe Balbi32a4a132014-02-25 14:00:13 -06001022 dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
1023
1024 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
1025 dwc->dr_mode = USB_DR_MODE_OTG;
1026
Felipe Balbic499ff72016-05-16 10:49:01 +03001027 ret = dwc3_alloc_scratch_buffers(dwc);
1028 if (ret)
Roger Quadros32808232016-06-10 14:38:02 +03001029 goto err3;
Felipe Balbic499ff72016-05-16 10:49:01 +03001030
Felipe Balbi72246da2011-08-19 18:10:58 +03001031 ret = dwc3_core_init(dwc);
1032 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +09001033 dev_err(dev, "failed to initialize core\n");
Roger Quadros32808232016-06-10 14:38:02 +03001034 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03001035 }
1036
John Youn77966eb2016-02-19 17:31:01 -08001037 /* Check the maximum_speed parameter */
1038 switch (dwc->maximum_speed) {
1039 case USB_SPEED_LOW:
1040 case USB_SPEED_FULL:
1041 case USB_SPEED_HIGH:
1042 case USB_SPEED_SUPER:
1043 case USB_SPEED_SUPER_PLUS:
1044 break;
1045 default:
1046 dev_err(dev, "invalid maximum_speed parameter %d\n",
1047 dwc->maximum_speed);
1048 /* fall through */
1049 case USB_SPEED_UNKNOWN:
1050 /* default to superspeed */
John Youn2c7f1bd2016-02-05 17:08:59 -08001051 dwc->maximum_speed = USB_SPEED_SUPER;
1052
1053 /*
1054 * default to superspeed plus if we are capable.
1055 */
1056 if (dwc3_is_usb31(dwc) &&
1057 (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
1058 DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1059 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
John Youn77966eb2016-02-19 17:31:01 -08001060
1061 break;
John Youn2c7f1bd2016-02-05 17:08:59 -08001062 }
1063
Felipe Balbi5f94adf2014-04-16 15:13:45 -05001064 ret = dwc3_core_init_mode(dwc);
1065 if (ret)
Roger Quadros32808232016-06-10 14:38:02 +03001066 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03001067
Du, Changbin4e9f3112016-04-12 19:10:18 +08001068 dwc3_debugfs_init(dwc);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001069 pm_runtime_put(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001070
1071 return 0;
1072
Roger Quadros32808232016-06-10 14:38:02 +03001073err5:
Felipe Balbif122d332013-02-08 15:15:11 +02001074 dwc3_event_buffers_cleanup(dwc);
1075
Roger Quadros32808232016-06-10 14:38:02 +03001076err4:
Felipe Balbic499ff72016-05-16 10:49:01 +03001077 dwc3_free_scratch_buffers(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001078
Roger Quadros32808232016-06-10 14:38:02 +03001079err3:
Felipe Balbi39214262012-10-11 13:54:36 +03001080 dwc3_free_event_buffers(dwc);
Heikki Krogerus88bc9d12015-05-13 15:26:51 +03001081 dwc3_ulpi_exit(dwc);
Felipe Balbi39214262012-10-11 13:54:36 +03001082
Roger Quadros32808232016-06-10 14:38:02 +03001083err2:
1084 pm_runtime_allow(&pdev->dev);
1085
1086err1:
1087 pm_runtime_put_sync(&pdev->dev);
1088 pm_runtime_disable(&pdev->dev);
1089
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001090err0:
1091 /*
1092 * restore res->start back to its original value so that, in case the
1093 * probe is deferred, we don't end up getting error in request the
1094 * memory region the next time probe is called.
1095 */
1096 res->start -= DWC3_GLOBALS_REGS_START;
1097
Felipe Balbi72246da2011-08-19 18:10:58 +03001098 return ret;
1099}
1100
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05001101static int dwc3_remove(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +03001102{
Felipe Balbi72246da2011-08-19 18:10:58 +03001103 struct dwc3 *dwc = platform_get_drvdata(pdev);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001104 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1105
Felipe Balbifc8bb912016-05-16 13:14:48 +03001106 pm_runtime_get_sync(&pdev->dev);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001107 /*
1108 * restore res->start back to its original value so that, in case the
1109 * probe is deferred, we don't end up getting error in request the
1110 * memory region the next time probe is called.
1111 */
1112 res->start -= DWC3_GLOBALS_REGS_START;
Felipe Balbi72246da2011-08-19 18:10:58 +03001113
Felipe Balbidc99f162014-09-03 16:13:37 -05001114 dwc3_debugfs_exit(dwc);
1115 dwc3_core_exit_mode(dwc);
Kishon Vijay Abraham I8ba007a2013-01-25 08:30:54 +05301116
Felipe Balbi72246da2011-08-19 18:10:58 +03001117 dwc3_core_exit(dwc);
Heikki Krogerus88bc9d12015-05-13 15:26:51 +03001118 dwc3_ulpi_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001119
Felipe Balbifc8bb912016-05-16 13:14:48 +03001120 pm_runtime_put_sync(&pdev->dev);
1121 pm_runtime_allow(&pdev->dev);
1122 pm_runtime_disable(&pdev->dev);
1123
Felipe Balbic499ff72016-05-16 10:49:01 +03001124 dwc3_free_event_buffers(dwc);
1125 dwc3_free_scratch_buffers(dwc);
1126
Felipe Balbi72246da2011-08-19 18:10:58 +03001127 return 0;
1128}
1129
Felipe Balbifc8bb912016-05-16 13:14:48 +03001130#ifdef CONFIG_PM
1131static int dwc3_suspend_common(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03001132{
Felipe Balbifc8bb912016-05-16 13:14:48 +03001133 unsigned long flags;
Felipe Balbi7415f172012-04-30 14:56:33 +03001134
Ruchika Kharwara45c82b82013-07-06 07:52:49 -05001135 switch (dwc->dr_mode) {
1136 case USB_DR_MODE_PERIPHERAL:
1137 case USB_DR_MODE_OTG:
Felipe Balbifc8bb912016-05-16 13:14:48 +03001138 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7415f172012-04-30 14:56:33 +03001139 dwc3_gadget_suspend(dwc);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001140 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi51f5d492016-05-16 10:52:58 +03001141 break;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -05001142 case USB_DR_MODE_HOST:
Felipe Balbi7415f172012-04-30 14:56:33 +03001143 default:
Felipe Balbi51f5d492016-05-16 10:52:58 +03001144 /* do nothing */
Felipe Balbi7415f172012-04-30 14:56:33 +03001145 break;
1146 }
1147
Felipe Balbi51f5d492016-05-16 10:52:58 +03001148 dwc3_core_exit(dwc);
Felipe Balbi5c4ad3182016-04-11 17:12:34 +03001149
Felipe Balbifc8bb912016-05-16 13:14:48 +03001150 return 0;
1151}
1152
1153static int dwc3_resume_common(struct dwc3 *dwc)
1154{
1155 unsigned long flags;
1156 int ret;
1157
1158 ret = dwc3_core_init(dwc);
1159 if (ret)
1160 return ret;
1161
1162 switch (dwc->dr_mode) {
1163 case USB_DR_MODE_PERIPHERAL:
1164 case USB_DR_MODE_OTG:
1165 spin_lock_irqsave(&dwc->lock, flags);
1166 dwc3_gadget_resume(dwc);
1167 spin_unlock_irqrestore(&dwc->lock, flags);
1168 /* FALLTHROUGH */
1169 case USB_DR_MODE_HOST:
1170 default:
1171 /* do nothing */
1172 break;
1173 }
1174
1175 return 0;
1176}
1177
1178static int dwc3_runtime_checks(struct dwc3 *dwc)
1179{
1180 switch (dwc->dr_mode) {
1181 case USB_DR_MODE_PERIPHERAL:
1182 case USB_DR_MODE_OTG:
1183 if (dwc->connected)
1184 return -EBUSY;
1185 break;
1186 case USB_DR_MODE_HOST:
1187 default:
1188 /* do nothing */
1189 break;
1190 }
1191
1192 return 0;
1193}
1194
1195static int dwc3_runtime_suspend(struct device *dev)
1196{
1197 struct dwc3 *dwc = dev_get_drvdata(dev);
1198 int ret;
1199
1200 if (dwc3_runtime_checks(dwc))
1201 return -EBUSY;
1202
1203 ret = dwc3_suspend_common(dwc);
1204 if (ret)
1205 return ret;
1206
1207 device_init_wakeup(dev, true);
1208
1209 return 0;
1210}
1211
1212static int dwc3_runtime_resume(struct device *dev)
1213{
1214 struct dwc3 *dwc = dev_get_drvdata(dev);
1215 int ret;
1216
1217 device_init_wakeup(dev, false);
1218
1219 ret = dwc3_resume_common(dwc);
1220 if (ret)
1221 return ret;
1222
1223 switch (dwc->dr_mode) {
1224 case USB_DR_MODE_PERIPHERAL:
1225 case USB_DR_MODE_OTG:
1226 dwc3_gadget_process_pending_events(dwc);
1227 break;
1228 case USB_DR_MODE_HOST:
1229 default:
1230 /* do nothing */
1231 break;
1232 }
1233
1234 pm_runtime_mark_last_busy(dev);
1235
1236 return 0;
1237}
1238
1239static int dwc3_runtime_idle(struct device *dev)
1240{
1241 struct dwc3 *dwc = dev_get_drvdata(dev);
1242
1243 switch (dwc->dr_mode) {
1244 case USB_DR_MODE_PERIPHERAL:
1245 case USB_DR_MODE_OTG:
1246 if (dwc3_runtime_checks(dwc))
1247 return -EBUSY;
1248 break;
1249 case USB_DR_MODE_HOST:
1250 default:
1251 /* do nothing */
1252 break;
1253 }
1254
1255 pm_runtime_mark_last_busy(dev);
1256 pm_runtime_autosuspend(dev);
1257
1258 return 0;
1259}
1260#endif /* CONFIG_PM */
1261
1262#ifdef CONFIG_PM_SLEEP
1263static int dwc3_suspend(struct device *dev)
1264{
1265 struct dwc3 *dwc = dev_get_drvdata(dev);
1266 int ret;
1267
1268 ret = dwc3_suspend_common(dwc);
1269 if (ret)
1270 return ret;
1271
Sekhar Nori63444752015-08-31 21:09:08 +05301272 pinctrl_pm_select_sleep_state(dev);
1273
Felipe Balbi7415f172012-04-30 14:56:33 +03001274 return 0;
1275}
1276
1277static int dwc3_resume(struct device *dev)
1278{
1279 struct dwc3 *dwc = dev_get_drvdata(dev);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +05301280 int ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03001281
Sekhar Nori63444752015-08-31 21:09:08 +05301282 pinctrl_pm_select_default_state(dev);
1283
Felipe Balbifc8bb912016-05-16 13:14:48 +03001284 ret = dwc3_resume_common(dwc);
Felipe Balbi51f5d492016-05-16 10:52:58 +03001285 if (ret)
Felipe Balbi5c4ad3182016-04-11 17:12:34 +03001286 return ret;
1287
Felipe Balbi7415f172012-04-30 14:56:33 +03001288 pm_runtime_disable(dev);
1289 pm_runtime_set_active(dev);
1290 pm_runtime_enable(dev);
1291
1292 return 0;
1293}
Felipe Balbi7f370ed2016-05-09 15:27:01 +03001294#endif /* CONFIG_PM_SLEEP */
Felipe Balbi7415f172012-04-30 14:56:33 +03001295
1296static const struct dev_pm_ops dwc3_dev_pm_ops = {
Felipe Balbi7415f172012-04-30 14:56:33 +03001297 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
Felipe Balbifc8bb912016-05-16 13:14:48 +03001298 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
1299 dwc3_runtime_idle)
Felipe Balbi7415f172012-04-30 14:56:33 +03001300};
1301
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301302#ifdef CONFIG_OF
1303static const struct of_device_id of_dwc3_match[] = {
1304 {
Felipe Balbi22a5aa12013-07-02 21:20:24 +03001305 .compatible = "snps,dwc3"
1306 },
1307 {
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301308 .compatible = "synopsys,dwc3"
1309 },
1310 { },
1311};
1312MODULE_DEVICE_TABLE(of, of_dwc3_match);
1313#endif
1314
Heikki Krogerus404905a2014-09-25 10:57:02 +03001315#ifdef CONFIG_ACPI
1316
1317#define ACPI_ID_INTEL_BSW "808622B7"
1318
1319static const struct acpi_device_id dwc3_acpi_match[] = {
1320 { ACPI_ID_INTEL_BSW, 0 },
1321 { },
1322};
1323MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
1324#endif
1325
Felipe Balbi72246da2011-08-19 18:10:58 +03001326static struct platform_driver dwc3_driver = {
1327 .probe = dwc3_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05001328 .remove = dwc3_remove,
Felipe Balbi72246da2011-08-19 18:10:58 +03001329 .driver = {
1330 .name = "dwc3",
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301331 .of_match_table = of_match_ptr(of_dwc3_match),
Heikki Krogerus404905a2014-09-25 10:57:02 +03001332 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
Felipe Balbi7f370ed2016-05-09 15:27:01 +03001333 .pm = &dwc3_dev_pm_ops,
Felipe Balbi72246da2011-08-19 18:10:58 +03001334 },
Felipe Balbi72246da2011-08-19 18:10:58 +03001335};
1336
Tobias Klauserb1116dc2012-02-28 12:57:20 +01001337module_platform_driver(dwc3_driver);
1338
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +02001339MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +03001340MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
Felipe Balbi5945f782013-06-30 14:15:11 +03001341MODULE_LICENSE("GPL v2");
Felipe Balbi72246da2011-08-19 18:10:58 +03001342MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");