blob: 74bc159f498d8da6532ad9c5920d980b538da94c [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>
Mayank Ranaa99689a2016-08-10 17:39:47 -070038#include <linux/irq.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030039
40#include <linux/usb/ch9.h>
41#include <linux/usb/gadget.h>
Felipe Balbif7e846f2013-06-30 14:29:51 +030042#include <linux/usb/of.h>
Ruchika Kharwara45c82b82013-07-06 07:52:49 -050043#include <linux/usb/otg.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030044
45#include "core.h"
46#include "gadget.h"
47#include "io.h"
48
49#include "debug.h"
50
Felipe Balbifc8bb912016-05-16 13:14:48 +030051#define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
Felipe Balbi8300dd22011-10-18 13:54:01 +030052
Mayank Ranaa99689a2016-08-10 17:39:47 -070053void dwc3_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
54{
55 u32 reg;
56
57 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
58
59 if (suspend)
60 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
61 else
62 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
63
64 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
65}
66
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +010067void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
68{
69 u32 reg;
70
71 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
72 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
73 reg |= DWC3_GCTL_PRTCAPDIR(mode);
74 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
Mayank Ranaa99689a2016-08-10 17:39:47 -070075
76 /*
77 * Set this bit so that device attempts three more times at SS, even
78 * if it failed previously to operate in SS mode.
79 */
80 reg |= DWC3_GCTL_U2RSTECN;
81 reg &= ~(DWC3_GCTL_SOFITPSYNC);
82 reg &= ~(DWC3_GCTL_PWRDNSCALEMASK);
83 reg |= DWC3_GCTL_PWRDNSCALE(2);
84 reg |= DWC3_GCTL_U2EXIT_LFPS;
85 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
86
87 if (mode == DWC3_GCTL_PRTCAP_OTG || mode == DWC3_GCTL_PRTCAP_HOST) {
88 /*
89 * Allow ITP generated off of ref clk based counter instead
90 * of UTMI/ULPI clk based counter, when superspeed only is
91 * active so that UTMI/ULPI PHY can be suspened.
92 *
93 * Starting with revision 2.50A, GFLADJ_REFCLK_LPM_SEL is used
94 * instead.
95 */
96 if (dwc->revision < DWC3_REVISION_250A) {
97 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
98 reg |= DWC3_GCTL_SOFITPSYNC;
99 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
100 } else {
101 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
102 reg |= DWC3_GFLADJ_REFCLK_LPM_SEL;
103 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
104 }
105 }
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100106}
Felipe Balbi8300dd22011-10-18 13:54:01 +0300107
Felipe Balbicf6d8672016-04-14 15:03:39 +0300108u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
109{
110 struct dwc3 *dwc = dep->dwc;
111 u32 reg;
112
113 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
114 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
115 DWC3_GDBGFIFOSPACE_TYPE(type));
116
117 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
118
119 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
120}
121
Felipe Balbi72246da2011-08-19 18:10:58 +0300122/**
Mayank Ranaa99689a2016-08-10 17:39:47 -0700123 * Peforms initialization of HS and SS PHYs.
124 * If used as a part of POR or init sequence it is recommended
125 * that we should perform hard reset of the PHYs prior to invoking
126 * this function.
Felipe Balbi72246da2011-08-19 18:10:58 +0300127 * @dwc: pointer to our context structure
Mayank Ranaa99689a2016-08-10 17:39:47 -0700128*/
129static int dwc3_init_usb_phys(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300130{
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530131 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300132
Mayank Ranaa99689a2016-08-10 17:39:47 -0700133 /* Bring up PHYs */
134 ret = usb_phy_init(dwc->usb2_phy);
135 if (ret) {
136 pr_err("%s: usb_phy_init(dwc->usb2_phy) returned %d\n",
137 __func__, ret);
138 return ret;
139 }
140
141 ret = usb_phy_init(dwc->usb3_phy);
142 if (ret == -EBUSY) {
143 /*
144 * Setting Max speed as high when USB3 PHY initialiation
145 * is failing and USB superspeed can't be supported.
146 */
147 dwc->maximum_speed = USB_SPEED_HIGH;
148 } else if (ret) {
149 pr_err("%s: usb_phy_init(dwc->usb3_phy) returned %d\n",
150 __func__, ret);
151 return ret;
152 }
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530153 ret = phy_init(dwc->usb2_generic_phy);
154 if (ret < 0)
155 return ret;
156
157 ret = phy_init(dwc->usb3_generic_phy);
158 if (ret < 0) {
159 phy_exit(dwc->usb2_generic_phy);
160 return ret;
161 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300162
Mayank Ranaa99689a2016-08-10 17:39:47 -0700163 return 0;
164}
Felipe Balbi72246da2011-08-19 18:10:58 +0300165
Mayank Ranaa99689a2016-08-10 17:39:47 -0700166/**
167 * dwc3_core_reset - Issues core soft reset and PHY reset
168 * @dwc: pointer to our context structure
169 */
170static int dwc3_core_reset(struct dwc3 *dwc)
171{
172 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300173
Mayank Ranaa99689a2016-08-10 17:39:47 -0700174 /* Reset PHYs */
175 usb_phy_reset(dwc->usb2_phy);
176 usb_phy_reset(dwc->usb3_phy);
Pratyush Anand45627ac2012-06-21 17:44:28 +0530177
Mayank Ranaa99689a2016-08-10 17:39:47 -0700178 /* Initialize PHYs */
179 ret = dwc3_init_usb_phys(dwc);
180 if (ret) {
181 pr_err("%s: dwc3_init_phys returned %d\n",
182 __func__, ret);
183 return ret;
184 }
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530185
Mayank Ranaa99689a2016-08-10 17:39:47 -0700186 dwc3_notify_event(dwc, DWC3_CONTROLLER_RESET_EVENT);
187
188 dwc3_notify_event(dwc, DWC3_CONTROLLER_POST_RESET_EVENT);
189
190 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300191}
192
193/**
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300194 * dwc3_soft_reset - Issue soft reset
195 * @dwc: Pointer to our controller context structure
196 */
197static int dwc3_soft_reset(struct dwc3 *dwc)
198{
199 unsigned long timeout;
200 u32 reg;
201
202 timeout = jiffies + msecs_to_jiffies(500);
203 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
204 do {
205 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
206 if (!(reg & DWC3_DCTL_CSFTRST))
207 break;
208
209 if (time_after(jiffies, timeout)) {
210 dev_err(dwc->dev, "Reset Timed Out\n");
211 return -ETIMEDOUT;
212 }
213
214 cpu_relax();
215 } while (true);
216
217 return 0;
218}
219
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530220/*
221 * dwc3_frame_length_adjustment - Adjusts frame length if required
222 * @dwc3: Pointer to our controller context structure
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530223 */
Felipe Balbibcdb3272016-05-16 10:42:23 +0300224static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530225{
226 u32 reg;
227 u32 dft;
228
229 if (dwc->revision < DWC3_REVISION_250A)
230 return;
231
Felipe Balbibcdb3272016-05-16 10:42:23 +0300232 if (dwc->fladj == 0)
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530233 return;
234
235 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
236 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
Felipe Balbibcdb3272016-05-16 10:42:23 +0300237 if (!dev_WARN_ONCE(dwc->dev, dft == dwc->fladj,
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530238 "request value same as default, ignoring\n")) {
239 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
Felipe Balbibcdb3272016-05-16 10:42:23 +0300240 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530241 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
242 }
243}
244
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300245/**
Felipe Balbi72246da2011-08-19 18:10:58 +0300246 * dwc3_free_one_event_buffer - Frees one event buffer
247 * @dwc: Pointer to our controller context structure
248 * @evt: Pointer to event buffer to be freed
249 */
250static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
251 struct dwc3_event_buffer *evt)
252{
253 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300254}
255
256/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800257 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300258 * @dwc: Pointer to our controller context structure
259 * @length: size of the event buffer
260 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800261 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300262 * otherwise ERR_PTR(errno).
263 */
Felipe Balbi67d0b502013-02-22 16:31:07 +0200264static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
265 unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300266{
267 struct dwc3_event_buffer *evt;
268
Felipe Balbi380f0d22012-10-11 13:48:36 +0300269 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300270 if (!evt)
271 return ERR_PTR(-ENOMEM);
272
273 evt->dwc = dwc;
274 evt->length = length;
275 evt->buf = dma_alloc_coherent(dwc->dev, length,
276 &evt->dma, GFP_KERNEL);
Felipe Balbie32672f2012-11-08 15:26:41 +0200277 if (!evt->buf)
Felipe Balbi72246da2011-08-19 18:10:58 +0300278 return ERR_PTR(-ENOMEM);
Felipe Balbi72246da2011-08-19 18:10:58 +0300279
280 return evt;
281}
282
283/**
284 * dwc3_free_event_buffers - frees all allocated event buffers
285 * @dwc: Pointer to our controller context structure
286 */
287static void dwc3_free_event_buffers(struct dwc3 *dwc)
288{
289 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300290
Felipe Balbi696c8b12016-03-30 09:37:03 +0300291 evt = dwc->ev_buf;
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300292 if (evt)
293 dwc3_free_one_event_buffer(dwc, evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300294}
295
296/**
297 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800298 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300299 * @length: size of event buffer
300 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800301 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300302 * may contain some buffers allocated but not all which were requested.
303 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500304static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300305{
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300306 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300307
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300308 evt = dwc3_alloc_one_event_buffer(dwc, length);
309 if (IS_ERR(evt)) {
310 dev_err(dwc->dev, "can't allocate event buffer\n");
311 return PTR_ERR(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300312 }
Felipe Balbi696c8b12016-03-30 09:37:03 +0300313 dwc->ev_buf = evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300314
315 return 0;
316}
317
318/**
319 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800320 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300321 *
322 * Returns 0 on success otherwise negative errno.
323 */
Mayank Ranaa99689a2016-08-10 17:39:47 -0700324int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300325{
326 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300327
Felipe Balbi696c8b12016-03-30 09:37:03 +0300328 evt = dwc->ev_buf;
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300329 dwc3_trace(trace_dwc3_core,
330 "Event buf %p dma %08llx length %d\n",
331 evt->buf, (unsigned long long) evt->dma,
332 evt->length);
Felipe Balbi72246da2011-08-19 18:10:58 +0300333
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300334 evt->lpos = 0;
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300335
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300336 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
337 lower_32_bits(evt->dma));
338 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
339 upper_32_bits(evt->dma));
340 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
341 DWC3_GEVNTSIZ_SIZE(evt->length));
342 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300343
344 return 0;
345}
346
347static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
348{
349 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300350
Felipe Balbi696c8b12016-03-30 09:37:03 +0300351 evt = dwc->ev_buf;
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300352
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300353 evt->lpos = 0;
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300354
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300355 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
356 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
357 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
358 | DWC3_GEVNTSIZ_SIZE(0));
359 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300360}
361
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600362static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
363{
364 if (!dwc->has_hibernation)
365 return 0;
366
367 if (!dwc->nr_scratch)
368 return 0;
369
370 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
371 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
372 if (!dwc->scratchbuf)
373 return -ENOMEM;
374
375 return 0;
376}
377
378static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
379{
380 dma_addr_t scratch_addr;
381 u32 param;
382 int ret;
383
384 if (!dwc->has_hibernation)
385 return 0;
386
387 if (!dwc->nr_scratch)
388 return 0;
389
390 /* should never fall here */
391 if (!WARN_ON(dwc->scratchbuf))
392 return 0;
393
394 scratch_addr = dma_map_single(dwc->dev, dwc->scratchbuf,
395 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
396 DMA_BIDIRECTIONAL);
397 if (dma_mapping_error(dwc->dev, scratch_addr)) {
398 dev_err(dwc->dev, "failed to map scratch buffer\n");
399 ret = -EFAULT;
400 goto err0;
401 }
402
403 dwc->scratch_addr = scratch_addr;
404
405 param = lower_32_bits(scratch_addr);
406
407 ret = dwc3_send_gadget_generic_command(dwc,
408 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
409 if (ret < 0)
410 goto err1;
411
412 param = upper_32_bits(scratch_addr);
413
414 ret = dwc3_send_gadget_generic_command(dwc,
415 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
416 if (ret < 0)
417 goto err1;
418
419 return 0;
420
421err1:
422 dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
423 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
424
425err0:
426 return ret;
427}
428
429static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
430{
431 if (!dwc->has_hibernation)
432 return;
433
434 if (!dwc->nr_scratch)
435 return;
436
437 /* should never fall here */
438 if (!WARN_ON(dwc->scratchbuf))
439 return;
440
441 dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
442 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
443 kfree(dwc->scratchbuf);
444}
445
Felipe Balbi789451f62011-05-05 15:53:10 +0300446static void dwc3_core_num_eps(struct dwc3 *dwc)
447{
448 struct dwc3_hwparams *parms = &dwc->hwparams;
449
450 dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
451 dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
452
Felipe Balbi73815282015-01-27 13:48:14 -0600453 dwc3_trace(trace_dwc3_core, "found %d IN and %d OUT endpoints",
Felipe Balbi789451f62011-05-05 15:53:10 +0300454 dwc->num_in_eps, dwc->num_out_eps);
455}
456
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500457static void dwc3_cache_hwparams(struct dwc3 *dwc)
Felipe Balbi26ceca92011-09-30 10:58:49 +0300458{
459 struct dwc3_hwparams *parms = &dwc->hwparams;
460
461 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
462 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
463 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
464 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
465 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
466 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
467 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
468 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
469 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
470}
471
Felipe Balbi72246da2011-08-19 18:10:58 +0300472/**
Huang Ruib5a65c42014-10-28 19:54:28 +0800473 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
474 * @dwc: Pointer to our controller context structure
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300475 *
476 * Returns 0 on success. The USB PHY interfaces are configured but not
477 * initialized. The PHY interfaces and the PHYs get initialized together with
478 * the core in dwc3_core_init.
Huang Ruib5a65c42014-10-28 19:54:28 +0800479 */
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300480static int dwc3_phy_setup(struct dwc3 *dwc)
Huang Ruib5a65c42014-10-28 19:54:28 +0800481{
482 u32 reg;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300483 int ret;
Huang Ruib5a65c42014-10-28 19:54:28 +0800484
485 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
486
Huang Rui2164a472014-10-28 19:54:35 +0800487 /*
488 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
489 * to '0' during coreConsultant configuration. So default value
490 * will be '0' when the core is reset. Application needs to set it
491 * to '1' after the core initialization is completed.
492 */
493 if (dwc->revision > DWC3_REVISION_194A)
494 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
495
Huang Ruib5a65c42014-10-28 19:54:28 +0800496 if (dwc->u2ss_inp3_quirk)
497 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
498
Rajesh Bhagate58dd352016-03-14 14:40:50 +0530499 if (dwc->dis_rxdet_inp3_quirk)
500 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
501
Huang Ruidf31f5b2014-10-28 19:54:29 +0800502 if (dwc->req_p1p2p3_quirk)
503 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
504
Huang Ruia2a1d0f2014-10-28 19:54:30 +0800505 if (dwc->del_p1p2p3_quirk)
506 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
507
Huang Rui41c06ff2014-10-28 19:54:31 +0800508 if (dwc->del_phy_power_chg_quirk)
509 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
510
Huang Ruifb67afc2014-10-28 19:54:32 +0800511 if (dwc->lfps_filter_quirk)
512 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
513
Huang Rui14f4ac52014-10-28 19:54:33 +0800514 if (dwc->rx_detect_poll_quirk)
515 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
516
Huang Rui6b6a0c92014-10-31 11:11:12 +0800517 if (dwc->tx_de_emphasis_quirk)
518 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
519
Felipe Balbicd72f892014-11-06 11:31:00 -0600520 if (dwc->dis_u3_susphy_quirk)
Huang Rui59acfa22014-10-31 11:11:13 +0800521 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
522
Huang Ruib5a65c42014-10-28 19:54:28 +0800523 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
524
Huang Rui2164a472014-10-28 19:54:35 +0800525 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
526
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300527 /* Select the HS PHY interface */
528 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
529 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
Felipe Balbi43cacb02015-07-01 22:03:09 -0500530 if (dwc->hsphy_interface &&
531 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300532 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300533 break;
Felipe Balbi43cacb02015-07-01 22:03:09 -0500534 } else if (dwc->hsphy_interface &&
535 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300536 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300537 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300538 } else {
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300539 /* Relying on default value. */
540 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
541 break;
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300542 }
543 /* FALLTHROUGH */
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300544 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
545 /* Making sure the interface and PHY are operational */
546 ret = dwc3_soft_reset(dwc);
547 if (ret)
548 return ret;
549
550 udelay(1);
551
552 ret = dwc3_ulpi_init(dwc);
553 if (ret)
554 return ret;
555 /* FALLTHROUGH */
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300556 default:
557 break;
558 }
559
Huang Rui2164a472014-10-28 19:54:35 +0800560 /*
561 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
562 * '0' during coreConsultant configuration. So default value will
563 * be '0' when the core is reset. Application needs to set it to
564 * '1' after the core initialization is completed.
565 */
566 if (dwc->revision > DWC3_REVISION_194A)
567 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
568
Felipe Balbicd72f892014-11-06 11:31:00 -0600569 if (dwc->dis_u2_susphy_quirk)
Huang Rui0effe0a2014-10-31 11:11:14 +0800570 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
571
John Younec791d12015-10-02 20:30:57 -0700572 if (dwc->dis_enblslpm_quirk)
573 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
574
Huang Rui2164a472014-10-28 19:54:35 +0800575 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300576
577 return 0;
Huang Ruib5a65c42014-10-28 19:54:28 +0800578}
579
Felipe Balbic499ff72016-05-16 10:49:01 +0300580static void dwc3_core_exit(struct dwc3 *dwc)
581{
582 dwc3_event_buffers_cleanup(dwc);
583
584 usb_phy_shutdown(dwc->usb2_phy);
585 usb_phy_shutdown(dwc->usb3_phy);
586 phy_exit(dwc->usb2_generic_phy);
587 phy_exit(dwc->usb3_generic_phy);
588
589 usb_phy_set_suspend(dwc->usb2_phy, 1);
590 usb_phy_set_suspend(dwc->usb3_phy, 1);
591 phy_power_off(dwc->usb2_generic_phy);
592 phy_power_off(dwc->usb3_generic_phy);
593}
594
Huang Ruib5a65c42014-10-28 19:54:28 +0800595/**
Felipe Balbi72246da2011-08-19 18:10:58 +0300596 * dwc3_core_init - Low-level initialization of DWC3 Core
597 * @dwc: Pointer to our controller context structure
598 *
599 * Returns 0 on success otherwise negative errno.
600 */
Mayank Ranaa99689a2016-08-10 17:39:47 -0700601int dwc3_core_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300602{
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600603 u32 hwparams4 = dwc->hwparams.hwparams4;
Felipe Balbi72246da2011-08-19 18:10:58 +0300604 u32 reg;
605 int ret;
606
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200607 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
608 /* This should read as U3 followed by revision number */
John Youn690fb372015-09-04 19:15:10 -0700609 if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
610 /* Detected DWC_usb3 IP */
611 dwc->revision = reg;
612 } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
613 /* Detected DWC_usb31 IP */
614 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
615 dwc->revision |= DWC3_REVISION_IS_DWC31;
616 } else {
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200617 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
618 ret = -ENODEV;
619 goto err0;
620 }
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200621
Felipe Balbifa0ea132014-09-19 15:51:11 -0500622 /*
623 * Write Linux Version Code to our GUID register so it's easy to figure
624 * out which kernel version a bug was found.
625 */
626 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
627
Paul Zimmerman0e1e5c42014-05-23 11:39:24 -0700628 /* Handle USB2.0-only core configuration */
629 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
630 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
631 if (dwc->maximum_speed == USB_SPEED_SUPER)
632 dwc->maximum_speed = USB_SPEED_HIGH;
633 }
634
Felipe Balbi72246da2011-08-19 18:10:58 +0300635 /* issue device SoftReset too */
Mayank Ranaa99689a2016-08-10 17:39:47 -0700636 ret = dwc3_core_reset(dwc);
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300637 if (ret)
638 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300639
Mayank Ranaa99689a2016-08-10 17:39:47 -0700640 /* issue device SoftReset too */
641 ret = dwc3_soft_reset(dwc);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530642 if (ret)
643 goto err0;
Pratyush Anand58a0f232012-06-21 17:44:29 +0530644
Felipe Balbic499ff72016-05-16 10:49:01 +0300645 ret = dwc3_phy_setup(dwc);
646 if (ret)
647 goto err0;
648
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100649 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800650 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100651
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100652 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100653 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
Felipe Balbi32a4a132014-02-25 14:00:13 -0600654 /**
655 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
656 * issue which would cause xHCI compliance tests to fail.
657 *
658 * Because of that we cannot enable clock gating on such
659 * configurations.
660 *
661 * Refers to:
662 *
663 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
664 * SOF/ITP Mode Used
665 */
666 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
667 dwc->dr_mode == USB_DR_MODE_OTG) &&
668 (dwc->revision >= DWC3_REVISION_210A &&
669 dwc->revision <= DWC3_REVISION_250A))
670 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
671 else
672 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100673 break;
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600674 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
675 /* enable hibernation here */
676 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
Huang Rui2eac3992014-10-28 19:54:22 +0800677
678 /*
679 * REVISIT Enabling this bit so that host-mode hibernation
680 * will work. Device-mode hibernation is not yet implemented.
681 */
682 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600683 break;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100684 default:
Felipe Balbi1407bf12015-11-16 16:06:37 -0600685 dwc3_trace(trace_dwc3_core, "No power optimization available\n");
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100686 }
687
Huang Rui946bd572014-10-28 19:54:23 +0800688 /* check if current dwc3 is on simulation board */
689 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
Felipe Balbi1407bf12015-11-16 16:06:37 -0600690 dwc3_trace(trace_dwc3_core,
691 "running on FPGA platform\n");
Huang Rui946bd572014-10-28 19:54:23 +0800692 dwc->is_fpga = true;
693 }
694
Huang Rui3b812212014-10-28 19:54:25 +0800695 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
696 "disable_scramble cannot be used on non-FPGA builds\n");
697
698 if (dwc->disable_scramble_quirk && dwc->is_fpga)
699 reg |= DWC3_GCTL_DISSCRAMBLE;
700 else
701 reg &= ~DWC3_GCTL_DISSCRAMBLE;
702
Huang Rui9a5b2f32014-10-28 19:54:27 +0800703 if (dwc->u2exit_lfps_quirk)
704 reg |= DWC3_GCTL_U2EXIT_LFPS;
705
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100706 /*
707 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800708 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100709 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800710 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100711 */
712 if (dwc->revision < DWC3_REVISION_190A)
713 reg |= DWC3_GCTL_U2RSTECN;
714
Mayank Ranaa99689a2016-08-10 17:39:47 -0700715 dwc3_core_num_eps(dwc);
716
717 /*
718 * Disable clock gating to work around a known HW bug that causes the
719 * internal RAM clock to get stuck when entering low power modes.
720 */
721 if (dwc->disable_clk_gating) {
722 dev_dbg(dwc->dev, "Disabling controller clock gating.\n");
723 reg |= DWC3_GCTL_DSBLCLKGTNG;
724 }
725
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100726 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
727
Mayank Ranaa99689a2016-08-10 17:39:47 -0700728 ret = dwc3_alloc_scratch_buffers(dwc);
729 if (ret)
730 goto err1;
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600731
732 ret = dwc3_setup_scratch_buffers(dwc);
733 if (ret)
Mayank Ranaa99689a2016-08-10 17:39:47 -0700734 goto err2;
Felipe Balbic499ff72016-05-16 10:49:01 +0300735
736 /* Adjust Frame Length */
737 dwc3_frame_length_adjustment(dwc);
738
739 usb_phy_set_suspend(dwc->usb2_phy, 0);
740 usb_phy_set_suspend(dwc->usb3_phy, 0);
741 ret = phy_power_on(dwc->usb2_generic_phy);
742 if (ret < 0)
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600743 goto err2;
744
Mayank Ranaa99689a2016-08-10 17:39:47 -0700745 /*
746 * clear Elastic buffer mode in GUSBPIPE_CTRL(0) register, otherwise
747 * it results in high link errors and could cause SS mode transfer
748 * failure.
749 */
750 if (!dwc->nominal_elastic_buffer) {
751 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
752 reg &= ~DWC3_GUSB3PIPECTL_ELASTIC_BUF_MODE;
753 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
Felipe Balbic499ff72016-05-16 10:49:01 +0300754 }
755
Felipe Balbi72246da2011-08-19 18:10:58 +0300756 return 0;
757
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600758err2:
Mayank Ranaa99689a2016-08-10 17:39:47 -0700759 dwc3_free_scratch_buffers(dwc);
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600760
761err1:
762 usb_phy_shutdown(dwc->usb2_phy);
763 usb_phy_shutdown(dwc->usb3_phy);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530764 phy_exit(dwc->usb2_generic_phy);
765 phy_exit(dwc->usb3_generic_phy);
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600766
Felipe Balbi72246da2011-08-19 18:10:58 +0300767err0:
768 return ret;
769}
770
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500771static int dwc3_core_get_phy(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300772{
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500773 struct device *dev = dwc->dev;
Felipe Balbi941ea362013-07-31 09:21:25 +0300774 struct device_node *node = dev->of_node;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500775 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300776
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530777 if (node) {
778 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
779 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
Felipe Balbibb674902013-08-14 13:21:23 -0500780 } else {
781 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
782 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530783 }
784
Felipe Balbid105e7f2013-03-15 10:52:08 +0200785 if (IS_ERR(dwc->usb2_phy)) {
786 ret = PTR_ERR(dwc->usb2_phy);
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530787 if (ret == -ENXIO || ret == -ENODEV) {
788 dwc->usb2_phy = NULL;
789 } else if (ret == -EPROBE_DEFER) {
Felipe Balbid105e7f2013-03-15 10:52:08 +0200790 return ret;
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530791 } else {
792 dev_err(dev, "no usb2 phy configured\n");
793 return ret;
794 }
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300795 }
796
Felipe Balbid105e7f2013-03-15 10:52:08 +0200797 if (IS_ERR(dwc->usb3_phy)) {
Ruchika Kharwar315955d72013-07-04 00:59:34 -0500798 ret = PTR_ERR(dwc->usb3_phy);
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530799 if (ret == -ENXIO || ret == -ENODEV) {
800 dwc->usb3_phy = NULL;
801 } else if (ret == -EPROBE_DEFER) {
Felipe Balbid105e7f2013-03-15 10:52:08 +0200802 return ret;
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530803 } else {
804 dev_err(dev, "no usb3 phy configured\n");
805 return ret;
806 }
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300807 }
808
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530809 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
810 if (IS_ERR(dwc->usb2_generic_phy)) {
811 ret = PTR_ERR(dwc->usb2_generic_phy);
812 if (ret == -ENOSYS || ret == -ENODEV) {
813 dwc->usb2_generic_phy = NULL;
814 } else if (ret == -EPROBE_DEFER) {
815 return ret;
816 } else {
817 dev_err(dev, "no usb2 phy configured\n");
818 return ret;
819 }
820 }
821
822 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
823 if (IS_ERR(dwc->usb3_generic_phy)) {
824 ret = PTR_ERR(dwc->usb3_generic_phy);
825 if (ret == -ENOSYS || ret == -ENODEV) {
826 dwc->usb3_generic_phy = NULL;
827 } else if (ret == -EPROBE_DEFER) {
828 return ret;
829 } else {
830 dev_err(dev, "no usb3 phy configured\n");
831 return ret;
832 }
833 }
834
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500835 return 0;
836}
837
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500838static int dwc3_core_init_mode(struct dwc3 *dwc)
839{
840 struct device *dev = dwc->dev;
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500841
842 switch (dwc->dr_mode) {
843 case USB_DR_MODE_PERIPHERAL:
844 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500845 break;
846 case USB_DR_MODE_HOST:
847 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500848 break;
849 case USB_DR_MODE_OTG:
850 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500851 break;
852 default:
853 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
854 return -EINVAL;
855 }
856
857 return 0;
858}
859
860static void dwc3_core_exit_mode(struct dwc3 *dwc)
861{
862 switch (dwc->dr_mode) {
863 case USB_DR_MODE_PERIPHERAL:
864 dwc3_gadget_exit(dwc);
865 break;
866 case USB_DR_MODE_HOST:
867 dwc3_host_exit(dwc);
868 break;
869 case USB_DR_MODE_OTG:
870 dwc3_host_exit(dwc);
871 dwc3_gadget_exit(dwc);
872 break;
873 default:
874 /* do nothing */
875 break;
876 }
877}
878
Mayank Ranaa99689a2016-08-10 17:39:47 -0700879/* XHCI reset, resets other CORE registers as well, re-init those */
880void dwc3_post_host_reset_core_init(struct dwc3 *dwc)
881{
882 dwc3_core_init(dwc);
883 dwc3_gadget_restart(dwc);
884}
885
886static void (*notify_event)(struct dwc3 *, unsigned int);
887void dwc3_set_notifier(void (*notify)(struct dwc3 *, unsigned int))
888{
889 notify_event = notify;
890}
891EXPORT_SYMBOL(dwc3_set_notifier);
892
893int dwc3_notify_event(struct dwc3 *dwc, unsigned int event)
894{
895 int ret = 0;
896
897 if (dwc->notify_event)
898 dwc->notify_event(dwc, event);
899 else
900 ret = -ENODEV;
901
902 return ret;
903}
904EXPORT_SYMBOL(dwc3_notify_event);
905
906int dwc3_core_pre_init(struct dwc3 *dwc)
907{
908 int ret;
909
910 dwc3_cache_hwparams(dwc);
911
912 ret = dwc3_phy_setup(dwc);
913 if (ret)
914 goto err0;
915
916 if (!dwc->ev_buf) {
917 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
918 if (ret) {
919 dev_err(dwc->dev, "failed to allocate event buffers\n");
920 ret = -ENOMEM;
921 goto err1;
922 }
923 }
924
925 ret = dwc3_core_init(dwc);
926 if (ret) {
927 dev_err(dwc->dev, "failed to initialize core\n");
928 goto err2;
929 }
930
931 ret = phy_power_on(dwc->usb2_generic_phy);
932 if (ret < 0)
933 goto err3;
934
935 ret = phy_power_on(dwc->usb3_generic_phy);
936 if (ret < 0)
937 goto err4;
938
939 ret = dwc3_event_buffers_setup(dwc);
940 if (ret) {
941 dev_err(dwc->dev, "failed to setup event buffers\n");
942 goto err5;
943 }
944
945 ret = dwc3_core_init_mode(dwc);
946 if (ret) {
947 dev_err(dwc->dev, "failed to set mode with dwc3 core\n");
948 goto err6;
949 }
950
951 return ret;
952
953err6:
954 dwc3_event_buffers_cleanup(dwc);
955err5:
956 phy_power_off(dwc->usb3_generic_phy);
957err4:
958 phy_power_off(dwc->usb2_generic_phy);
959err3:
960 dwc3_core_exit(dwc);
961err2:
962 dwc3_free_event_buffers(dwc);
963err1:
964 dwc3_ulpi_exit(dwc);
965err0:
966 return ret;
967}
968
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500969#define DWC3_ALIGN_MASK (16 - 1)
970
971static int dwc3_probe(struct platform_device *pdev)
972{
973 struct device *dev = &pdev->dev;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500974 struct resource *res;
975 struct dwc3 *dwc;
Huang Rui80caf7d2014-10-28 19:54:26 +0800976 u8 lpm_nyet_threshold;
Huang Rui6b6a0c92014-10-31 11:11:12 +0800977 u8 tx_de_emphasis;
Huang Rui460d0982014-10-31 11:11:18 +0800978 u8 hird_threshold;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700979 int irq;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500980
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300981 int ret;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500982
983 void __iomem *regs;
984 void *mem;
985
986 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +0900987 if (!mem)
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500988 return -ENOMEM;
Jingoo Han734d5a52014-07-17 12:45:11 +0900989
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500990 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
991 dwc->mem = mem;
992 dwc->dev = dev;
993
Mayank Ranaa99689a2016-08-10 17:39:47 -0700994 dwc->notify_event = notify_event;
995 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
996 if (!res) {
997 dev_err(dev, "missing IRQ\n");
998 return -ENODEV;
999 }
1000 dwc->xhci_resources[1].start = res->start;
1001 dwc->xhci_resources[1].end = res->end;
1002 dwc->xhci_resources[1].flags = res->flags;
1003 dwc->xhci_resources[1].name = res->name;
1004
1005 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1006
1007 /* will be enabled in dwc3_msm_resume() */
1008 irq_set_status_flags(irq, IRQ_NOAUTOEN);
1009 ret = devm_request_threaded_irq(dev, irq, NULL, dwc3_interrupt,
1010 IRQF_SHARED | IRQF_ONESHOT, "dwc3", dwc);
1011 if (ret) {
1012 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1013 irq, ret);
1014 return -ENODEV;
1015 }
1016
1017 dwc->irq = irq;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001018 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1019 if (!res) {
1020 dev_err(dev, "missing memory resource\n");
1021 return -ENODEV;
1022 }
1023
Mayank Ranaa99689a2016-08-10 17:39:47 -07001024 dwc->reg_phys = res->start;
Vivek Gautamf32a5e22014-06-04 14:34:52 +05301025 dwc->xhci_resources[0].start = res->start;
1026 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1027 DWC3_XHCI_REGS_END;
1028 dwc->xhci_resources[0].flags = res->flags;
1029 dwc->xhci_resources[0].name = res->name;
1030
1031 res->start += DWC3_GLOBALS_REGS_START;
1032
1033 /*
1034 * Request memory region but exclude xHCI regs,
1035 * since it will be requested by the xhci-plat driver.
1036 */
1037 regs = devm_ioremap_resource(dev, res);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001038 if (IS_ERR(regs)) {
1039 ret = PTR_ERR(regs);
1040 goto err0;
1041 }
Vivek Gautamf32a5e22014-06-04 14:34:52 +05301042
1043 dwc->regs = regs;
1044 dwc->regs_size = resource_size(res);
Vivek Gautamf32a5e22014-06-04 14:34:52 +05301045
Huang Rui80caf7d2014-10-28 19:54:26 +08001046 /* default to highest possible threshold */
1047 lpm_nyet_threshold = 0xff;
1048
Huang Rui6b6a0c92014-10-31 11:11:12 +08001049 /* default to -3.5dB de-emphasis */
1050 tx_de_emphasis = 1;
1051
Huang Rui460d0982014-10-31 11:11:18 +08001052 /*
1053 * default to assert utmi_sleep_n and use maximum allowed HIRD
1054 * threshold value of 0b1100
1055 */
1056 hird_threshold = 12;
1057
Heikki Krogerus63863b92015-09-21 11:14:32 +03001058 dwc->maximum_speed = usb_get_maximum_speed(dev);
Heikki Krogerus06e71142015-09-21 11:14:34 +03001059 dwc->dr_mode = usb_get_dr_mode(dev);
Heikki Krogerus63863b92015-09-21 11:14:32 +03001060
Heikki Krogerus3d128912015-09-21 11:14:35 +03001061 dwc->has_lpm_erratum = device_property_read_bool(dev,
Huang Rui80caf7d2014-10-28 19:54:26 +08001062 "snps,has-lpm-erratum");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001063 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
Huang Rui80caf7d2014-10-28 19:54:26 +08001064 &lpm_nyet_threshold);
Heikki Krogerus3d128912015-09-21 11:14:35 +03001065 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
Huang Rui460d0982014-10-31 11:11:18 +08001066 "snps,is-utmi-l1-suspend");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001067 device_property_read_u8(dev, "snps,hird-threshold",
Huang Rui460d0982014-10-31 11:11:18 +08001068 &hird_threshold);
Heikki Krogerus3d128912015-09-21 11:14:35 +03001069 dwc->usb3_lpm_capable = device_property_read_bool(dev,
Robert Baldygaeac68e82015-03-09 15:06:12 +01001070 "snps,usb3_lpm_capable");
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001071
Heikki Krogerus3d128912015-09-21 11:14:35 +03001072 dwc->disable_scramble_quirk = device_property_read_bool(dev,
Huang Rui3b812212014-10-28 19:54:25 +08001073 "snps,disable_scramble_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001074 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
Huang Rui9a5b2f32014-10-28 19:54:27 +08001075 "snps,u2exit_lfps_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001076 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
Huang Ruib5a65c42014-10-28 19:54:28 +08001077 "snps,u2ss_inp3_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001078 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
Huang Ruidf31f5b2014-10-28 19:54:29 +08001079 "snps,req_p1p2p3_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001080 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
Huang Ruia2a1d0f2014-10-28 19:54:30 +08001081 "snps,del_p1p2p3_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001082 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
Huang Rui41c06ff2014-10-28 19:54:31 +08001083 "snps,del_phy_power_chg_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001084 dwc->lfps_filter_quirk = device_property_read_bool(dev,
Huang Ruifb67afc2014-10-28 19:54:32 +08001085 "snps,lfps_filter_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001086 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
Huang Rui14f4ac52014-10-28 19:54:33 +08001087 "snps,rx_detect_poll_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001088 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
Huang Rui59acfa22014-10-31 11:11:13 +08001089 "snps,dis_u3_susphy_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001090 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
Huang Rui0effe0a2014-10-31 11:11:14 +08001091 "snps,dis_u2_susphy_quirk");
John Younec791d12015-10-02 20:30:57 -07001092 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1093 "snps,dis_enblslpm_quirk");
Rajesh Bhagate58dd352016-03-14 14:40:50 +05301094 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1095 "snps,dis_rxdet_inp3_quirk");
Huang Rui6b6a0c92014-10-31 11:11:12 +08001096
Heikki Krogerus3d128912015-09-21 11:14:35 +03001097 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
Huang Rui6b6a0c92014-10-31 11:11:12 +08001098 "snps,tx_de_emphasis_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001099 device_property_read_u8(dev, "snps,tx_de_emphasis",
Huang Rui6b6a0c92014-10-31 11:11:12 +08001100 &tx_de_emphasis);
Heikki Krogerus3d128912015-09-21 11:14:35 +03001101 device_property_read_string(dev, "snps,hsphy_interface",
1102 &dwc->hsphy_interface);
1103 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
Felipe Balbibcdb3272016-05-16 10:42:23 +03001104 &dwc->fladj);
Heikki Krogerus3d128912015-09-21 11:14:35 +03001105
Mayank Ranaa99689a2016-08-10 17:39:47 -07001106 if (dwc->enable_bus_suspend) {
1107 pm_runtime_set_autosuspend_delay(dev, 500);
1108 pm_runtime_use_autosuspend(dev);
1109 }
1110
Huang Rui80caf7d2014-10-28 19:54:26 +08001111 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
Huang Rui6b6a0c92014-10-31 11:11:12 +08001112 dwc->tx_de_emphasis = tx_de_emphasis;
Huang Rui80caf7d2014-10-28 19:54:26 +08001113
Huang Rui460d0982014-10-31 11:11:18 +08001114 dwc->hird_threshold = hird_threshold
1115 | (dwc->is_utmi_l1_suspend << 4);
1116
Mayank Ranaa99689a2016-08-10 17:39:47 -07001117 init_waitqueue_head(&dwc->wait_linkstate);
Heikki Krogerus6c89cce02015-05-13 15:26:45 +03001118 platform_set_drvdata(pdev, dwc);
1119
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001120 ret = dwc3_core_get_phy(dwc);
1121 if (ret)
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001122 goto err0;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001123
Felipe Balbi72246da2011-08-19 18:10:58 +03001124 spin_lock_init(&dwc->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03001125
Heikki Krogerus19bacdc2014-09-24 11:00:38 +03001126 if (!dev->dma_mask) {
1127 dev->dma_mask = dev->parent->dma_mask;
1128 dev->dma_parms = dev->parent->dma_parms;
1129 dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
1130 }
Kishon Vijay Abraham Iddff14f2013-03-07 18:51:43 +05301131
Mayank Ranaa99689a2016-08-10 17:39:47 -07001132 pm_runtime_no_callbacks(dev);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001133 pm_runtime_set_active(dev);
Chanho Park802ca852012-02-15 18:27:55 +09001134 pm_runtime_enable(dev);
Chanho Park802ca852012-02-15 18:27:55 +09001135 pm_runtime_forbid(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001136
Mayank Ranaa99689a2016-08-10 17:39:47 -07001137 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
Felipe Balbi32a4a132014-02-25 14:00:13 -06001138 dwc->dr_mode = USB_DR_MODE_HOST;
Felipe Balbi5f822792016-06-07 12:55:19 +03001139 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET) &&
1140 (dwc->dr_mode == USB_DR_MODE_OTG ||
1141 dwc->dr_mode == USB_DR_MODE_UNKNOWN))
Felipe Balbi32a4a132014-02-25 14:00:13 -06001142 dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
1143
Mayank Ranaa99689a2016-08-10 17:39:47 -07001144 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN) {
Felipe Balbi32a4a132014-02-25 14:00:13 -06001145 dwc->dr_mode = USB_DR_MODE_OTG;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001146 dwc->is_drd = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03001147 }
1148
John Youn77966eb2016-02-19 17:31:01 -08001149 /* Check the maximum_speed parameter */
1150 switch (dwc->maximum_speed) {
1151 case USB_SPEED_LOW:
1152 case USB_SPEED_FULL:
1153 case USB_SPEED_HIGH:
1154 case USB_SPEED_SUPER:
1155 case USB_SPEED_SUPER_PLUS:
1156 break;
1157 default:
1158 dev_err(dev, "invalid maximum_speed parameter %d\n",
1159 dwc->maximum_speed);
1160 /* fall through */
1161 case USB_SPEED_UNKNOWN:
1162 /* default to superspeed */
John Youn2c7f1bd2016-02-05 17:08:59 -08001163 dwc->maximum_speed = USB_SPEED_SUPER;
1164
1165 /*
1166 * default to superspeed plus if we are capable.
1167 */
1168 if (dwc3_is_usb31(dwc) &&
1169 (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
1170 DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1171 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
John Youn77966eb2016-02-19 17:31:01 -08001172
1173 break;
John Youn2c7f1bd2016-02-05 17:08:59 -08001174 }
1175
Mayank Ranaa99689a2016-08-10 17:39:47 -07001176 /* Adjust Frame Length */
1177 dwc3_frame_length_adjustment(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001178
Mayank Ranaa99689a2016-08-10 17:39:47 -07001179 /* Hardcode number of eps */
1180 dwc->num_in_eps = 16;
1181 dwc->num_out_eps = 16;
Felipe Balbi72246da2011-08-19 18:10:58 +03001182
Mayank Ranaa99689a2016-08-10 17:39:47 -07001183 if (dwc->dr_mode == USB_DR_MODE_OTG ||
1184 dwc->dr_mode == USB_DR_MODE_PERIPHERAL) {
1185 ret = dwc3_gadget_init(dwc);
1186 if (ret) {
1187 dev_err(dev, "failed to initialize gadget\n");
1188 goto err0;
1189 }
1190 }
1191
1192 if (dwc->dr_mode == USB_DR_MODE_OTG ||
1193 dwc->dr_mode == USB_DR_MODE_HOST) {
1194 ret = dwc3_host_init(dwc);
1195 if (ret) {
1196 dev_err(dev, "failed to initialize host\n");
1197 goto err_gadget;
1198 }
1199 }
1200
1201 ret = dwc3_debugfs_init(dwc);
1202 if (ret) {
1203 dev_err(dev, "failed to initialize debugfs\n");
1204 goto err_host;
1205 }
1206
1207 pm_runtime_allow(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001208 return 0;
1209
Mayank Ranaa99689a2016-08-10 17:39:47 -07001210err_host:
1211 if (dwc->dr_mode == USB_DR_MODE_OTG ||
1212 dwc->dr_mode == USB_DR_MODE_HOST)
1213 dwc3_host_exit(dwc);
1214err_gadget:
1215 if (dwc->dr_mode == USB_DR_MODE_OTG ||
1216 dwc->dr_mode == USB_DR_MODE_PERIPHERAL)
1217 dwc3_gadget_exit(dwc);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001218err0:
1219 /*
1220 * restore res->start back to its original value so that, in case the
1221 * probe is deferred, we don't end up getting error in request the
1222 * memory region the next time probe is called.
1223 */
1224 res->start -= DWC3_GLOBALS_REGS_START;
1225
Felipe Balbi72246da2011-08-19 18:10:58 +03001226 return ret;
1227}
1228
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05001229static int dwc3_remove(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +03001230{
Felipe Balbi72246da2011-08-19 18:10:58 +03001231 struct dwc3 *dwc = platform_get_drvdata(pdev);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001232 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1233
Felipe Balbifc8bb912016-05-16 13:14:48 +03001234 pm_runtime_get_sync(&pdev->dev);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001235 /*
1236 * restore res->start back to its original value so that, in case the
1237 * probe is deferred, we don't end up getting error in request the
1238 * memory region the next time probe is called.
1239 */
1240 res->start -= DWC3_GLOBALS_REGS_START;
Felipe Balbi72246da2011-08-19 18:10:58 +03001241
Felipe Balbidc99f162014-09-03 16:13:37 -05001242 dwc3_debugfs_exit(dwc);
1243 dwc3_core_exit_mode(dwc);
Kishon Vijay Abraham I8ba007a2013-01-25 08:30:54 +05301244
Felipe Balbi72246da2011-08-19 18:10:58 +03001245 dwc3_core_exit(dwc);
Heikki Krogerus88bc9d12015-05-13 15:26:51 +03001246 dwc3_ulpi_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001247
Felipe Balbifc8bb912016-05-16 13:14:48 +03001248 pm_runtime_put_sync(&pdev->dev);
1249 pm_runtime_allow(&pdev->dev);
1250 pm_runtime_disable(&pdev->dev);
1251
Felipe Balbic499ff72016-05-16 10:49:01 +03001252 dwc3_free_event_buffers(dwc);
1253 dwc3_free_scratch_buffers(dwc);
1254
Felipe Balbi72246da2011-08-19 18:10:58 +03001255 return 0;
1256}
1257
Felipe Balbifc8bb912016-05-16 13:14:48 +03001258#ifdef CONFIG_PM
1259static int dwc3_suspend_common(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03001260{
Felipe Balbifc8bb912016-05-16 13:14:48 +03001261 unsigned long flags;
Felipe Balbi7415f172012-04-30 14:56:33 +03001262
Ruchika Kharwara45c82b82013-07-06 07:52:49 -05001263 switch (dwc->dr_mode) {
1264 case USB_DR_MODE_PERIPHERAL:
1265 case USB_DR_MODE_OTG:
Felipe Balbifc8bb912016-05-16 13:14:48 +03001266 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7415f172012-04-30 14:56:33 +03001267 dwc3_gadget_suspend(dwc);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001268 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi51f5d492016-05-16 10:52:58 +03001269 break;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -05001270 case USB_DR_MODE_HOST:
Felipe Balbi7415f172012-04-30 14:56:33 +03001271 default:
Felipe Balbi51f5d492016-05-16 10:52:58 +03001272 /* do nothing */
Felipe Balbi7415f172012-04-30 14:56:33 +03001273 break;
1274 }
1275
Felipe Balbi51f5d492016-05-16 10:52:58 +03001276 dwc3_core_exit(dwc);
Felipe Balbi5c4ad3182016-04-11 17:12:34 +03001277
Felipe Balbifc8bb912016-05-16 13:14:48 +03001278 return 0;
1279}
1280
1281static int dwc3_resume_common(struct dwc3 *dwc)
1282{
1283 unsigned long flags;
1284 int ret;
1285
1286 ret = dwc3_core_init(dwc);
1287 if (ret)
1288 return ret;
1289
1290 switch (dwc->dr_mode) {
1291 case USB_DR_MODE_PERIPHERAL:
1292 case USB_DR_MODE_OTG:
1293 spin_lock_irqsave(&dwc->lock, flags);
1294 dwc3_gadget_resume(dwc);
1295 spin_unlock_irqrestore(&dwc->lock, flags);
1296 /* FALLTHROUGH */
1297 case USB_DR_MODE_HOST:
1298 default:
1299 /* do nothing */
1300 break;
1301 }
1302
1303 return 0;
1304}
1305
1306static int dwc3_runtime_checks(struct dwc3 *dwc)
1307{
1308 switch (dwc->dr_mode) {
1309 case USB_DR_MODE_PERIPHERAL:
1310 case USB_DR_MODE_OTG:
1311 if (dwc->connected)
1312 return -EBUSY;
1313 break;
1314 case USB_DR_MODE_HOST:
1315 default:
1316 /* do nothing */
1317 break;
1318 }
1319
1320 return 0;
1321}
1322
1323static int dwc3_runtime_suspend(struct device *dev)
1324{
1325 struct dwc3 *dwc = dev_get_drvdata(dev);
1326 int ret;
1327
Mayank Ranaa99689a2016-08-10 17:39:47 -07001328 /* Check if platform glue driver handling PM, if not then handle here */
1329 if (!dwc3_notify_event(dwc, DWC3_CORE_PM_RESUME_EVENT))
1330 return 0;
Felipe Balbifc8bb912016-05-16 13:14:48 +03001331
1332 ret = dwc3_suspend_common(dwc);
1333 if (ret)
1334 return ret;
1335
1336 device_init_wakeup(dev, true);
1337
1338 return 0;
1339}
1340
1341static int dwc3_runtime_resume(struct device *dev)
1342{
1343 struct dwc3 *dwc = dev_get_drvdata(dev);
1344 int ret;
1345
Mayank Ranaa99689a2016-08-10 17:39:47 -07001346 /* Check if platform glue driver handling PM, if not then handle here */
1347 if (!dwc3_notify_event(dwc, DWC3_CORE_PM_RESUME_EVENT))
1348 return 0;
1349
Felipe Balbifc8bb912016-05-16 13:14:48 +03001350 device_init_wakeup(dev, false);
1351
1352 ret = dwc3_resume_common(dwc);
1353 if (ret)
1354 return ret;
1355
1356 switch (dwc->dr_mode) {
1357 case USB_DR_MODE_PERIPHERAL:
1358 case USB_DR_MODE_OTG:
1359 dwc3_gadget_process_pending_events(dwc);
1360 break;
1361 case USB_DR_MODE_HOST:
1362 default:
1363 /* do nothing */
1364 break;
1365 }
1366
1367 pm_runtime_mark_last_busy(dev);
1368
1369 return 0;
1370}
1371
1372static int dwc3_runtime_idle(struct device *dev)
1373{
1374 struct dwc3 *dwc = dev_get_drvdata(dev);
1375
1376 switch (dwc->dr_mode) {
1377 case USB_DR_MODE_PERIPHERAL:
1378 case USB_DR_MODE_OTG:
1379 if (dwc3_runtime_checks(dwc))
1380 return -EBUSY;
1381 break;
1382 case USB_DR_MODE_HOST:
1383 default:
1384 /* do nothing */
1385 break;
1386 }
1387
1388 pm_runtime_mark_last_busy(dev);
1389 pm_runtime_autosuspend(dev);
1390
1391 return 0;
1392}
1393#endif /* CONFIG_PM */
1394
1395#ifdef CONFIG_PM_SLEEP
1396static int dwc3_suspend(struct device *dev)
1397{
1398 struct dwc3 *dwc = dev_get_drvdata(dev);
1399 int ret;
1400
1401 ret = dwc3_suspend_common(dwc);
1402 if (ret)
1403 return ret;
1404
Sekhar Nori63444752015-08-31 21:09:08 +05301405 pinctrl_pm_select_sleep_state(dev);
1406
Felipe Balbi7415f172012-04-30 14:56:33 +03001407 return 0;
1408}
1409
1410static int dwc3_resume(struct device *dev)
1411{
1412 struct dwc3 *dwc = dev_get_drvdata(dev);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +05301413 int ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03001414
Sekhar Nori63444752015-08-31 21:09:08 +05301415 pinctrl_pm_select_default_state(dev);
1416
Felipe Balbifc8bb912016-05-16 13:14:48 +03001417 ret = dwc3_resume_common(dwc);
Felipe Balbi51f5d492016-05-16 10:52:58 +03001418 if (ret)
Felipe Balbi5c4ad3182016-04-11 17:12:34 +03001419 return ret;
1420
Felipe Balbi7415f172012-04-30 14:56:33 +03001421 pm_runtime_disable(dev);
1422 pm_runtime_set_active(dev);
1423 pm_runtime_enable(dev);
1424
1425 return 0;
1426}
Felipe Balbi7f370ed2016-05-09 15:27:01 +03001427#endif /* CONFIG_PM_SLEEP */
Felipe Balbi7415f172012-04-30 14:56:33 +03001428
1429static const struct dev_pm_ops dwc3_dev_pm_ops = {
Felipe Balbi7415f172012-04-30 14:56:33 +03001430 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
Felipe Balbifc8bb912016-05-16 13:14:48 +03001431 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
1432 dwc3_runtime_idle)
Felipe Balbi7415f172012-04-30 14:56:33 +03001433};
1434
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301435#ifdef CONFIG_OF
1436static const struct of_device_id of_dwc3_match[] = {
1437 {
Felipe Balbi22a5aa12013-07-02 21:20:24 +03001438 .compatible = "snps,dwc3"
1439 },
1440 {
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301441 .compatible = "synopsys,dwc3"
1442 },
1443 { },
1444};
1445MODULE_DEVICE_TABLE(of, of_dwc3_match);
1446#endif
1447
Heikki Krogerus404905a2014-09-25 10:57:02 +03001448#ifdef CONFIG_ACPI
1449
1450#define ACPI_ID_INTEL_BSW "808622B7"
1451
1452static const struct acpi_device_id dwc3_acpi_match[] = {
1453 { ACPI_ID_INTEL_BSW, 0 },
1454 { },
1455};
1456MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
1457#endif
1458
Felipe Balbi72246da2011-08-19 18:10:58 +03001459static struct platform_driver dwc3_driver = {
1460 .probe = dwc3_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05001461 .remove = dwc3_remove,
Felipe Balbi72246da2011-08-19 18:10:58 +03001462 .driver = {
1463 .name = "dwc3",
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301464 .of_match_table = of_match_ptr(of_dwc3_match),
Heikki Krogerus404905a2014-09-25 10:57:02 +03001465 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
Felipe Balbi7f370ed2016-05-09 15:27:01 +03001466 .pm = &dwc3_dev_pm_ops,
Felipe Balbi72246da2011-08-19 18:10:58 +03001467 },
Felipe Balbi72246da2011-08-19 18:10:58 +03001468};
1469
Tobias Klauserb1116dc2012-02-28 12:57:20 +01001470module_platform_driver(dwc3_driver);
1471
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +02001472MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +03001473MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
Felipe Balbi5945f782013-06-30 14:15:11 +03001474MODULE_LICENSE("GPL v2");
Felipe Balbi72246da2011-08-19 18:10:58 +03001475MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");