blob: 7a3d696674eaea6f3d91784927dbc9a67d5bb0a5 [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
Thinh Nguyen9d6173e2016-09-06 19:22:03 -070067/**
68 * dwc3_get_dr_mode - Validates and sets dr_mode
69 * @dwc: pointer to our context structure
70 */
71static int dwc3_get_dr_mode(struct dwc3 *dwc)
72{
73 enum usb_dr_mode mode;
74 struct device *dev = dwc->dev;
75 unsigned int hw_mode;
76
Thinh Nguyen9d6173e2016-09-06 19:22:03 -070077
Mayank Ranafb9cd932016-11-03 23:26:38 -070078 dwc->is_drd = 0;
Thinh Nguyen9d6173e2016-09-06 19:22:03 -070079 mode = dwc->dr_mode;
80 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
81
82 switch (hw_mode) {
83 case DWC3_GHWPARAMS0_MODE_GADGET:
84 if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) {
85 dev_err(dev,
86 "Controller does not support host mode.\n");
87 return -EINVAL;
88 }
89 mode = USB_DR_MODE_PERIPHERAL;
90 break;
91 case DWC3_GHWPARAMS0_MODE_HOST:
92 if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
93 dev_err(dev,
94 "Controller does not support device mode.\n");
95 return -EINVAL;
96 }
97 mode = USB_DR_MODE_HOST;
98 break;
99 default:
100 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
101 mode = USB_DR_MODE_HOST;
102 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
103 mode = USB_DR_MODE_PERIPHERAL;
104 }
105
106 if (mode != dwc->dr_mode) {
107 dev_warn(dev,
108 "Configuration mismatch. dr_mode forced to %s\n",
109 mode == USB_DR_MODE_HOST ? "host" : "gadget");
110
111 dwc->dr_mode = mode;
112 }
113
Mayank Ranafb9cd932016-11-03 23:26:38 -0700114 if (dwc->dr_mode == USB_DR_MODE_OTG)
115 dwc->is_drd = 1;
116
Thinh Nguyen9d6173e2016-09-06 19:22:03 -0700117 return 0;
118}
119
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100120void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
121{
122 u32 reg;
123
124 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
125 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
126 reg |= DWC3_GCTL_PRTCAPDIR(mode);
127 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700128
129 /*
130 * Set this bit so that device attempts three more times at SS, even
131 * if it failed previously to operate in SS mode.
132 */
133 reg |= DWC3_GCTL_U2RSTECN;
134 reg &= ~(DWC3_GCTL_SOFITPSYNC);
135 reg &= ~(DWC3_GCTL_PWRDNSCALEMASK);
136 reg |= DWC3_GCTL_PWRDNSCALE(2);
137 reg |= DWC3_GCTL_U2EXIT_LFPS;
138 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
139
140 if (mode == DWC3_GCTL_PRTCAP_OTG || mode == DWC3_GCTL_PRTCAP_HOST) {
141 /*
142 * Allow ITP generated off of ref clk based counter instead
143 * of UTMI/ULPI clk based counter, when superspeed only is
144 * active so that UTMI/ULPI PHY can be suspened.
145 *
146 * Starting with revision 2.50A, GFLADJ_REFCLK_LPM_SEL is used
147 * instead.
148 */
149 if (dwc->revision < DWC3_REVISION_250A) {
150 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
151 reg |= DWC3_GCTL_SOFITPSYNC;
152 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
153 } else {
154 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
155 reg |= DWC3_GFLADJ_REFCLK_LPM_SEL;
156 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
157 }
158 }
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100159}
Felipe Balbi8300dd22011-10-18 13:54:01 +0300160
Felipe Balbicf6d8672016-04-14 15:03:39 +0300161u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
162{
163 struct dwc3 *dwc = dep->dwc;
164 u32 reg;
165
166 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
167 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
168 DWC3_GDBGFIFOSPACE_TYPE(type));
169
170 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
171
172 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
173}
174
Felipe Balbi72246da2011-08-19 18:10:58 +0300175/**
Mayank Ranaa99689a2016-08-10 17:39:47 -0700176 * Peforms initialization of HS and SS PHYs.
177 * If used as a part of POR or init sequence it is recommended
178 * that we should perform hard reset of the PHYs prior to invoking
179 * this function.
Felipe Balbi72246da2011-08-19 18:10:58 +0300180 * @dwc: pointer to our context structure
Mayank Ranaa99689a2016-08-10 17:39:47 -0700181*/
182static int dwc3_init_usb_phys(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300183{
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530184 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300185
Mayank Ranaa99689a2016-08-10 17:39:47 -0700186 /* Bring up PHYs */
187 ret = usb_phy_init(dwc->usb2_phy);
188 if (ret) {
189 pr_err("%s: usb_phy_init(dwc->usb2_phy) returned %d\n",
190 __func__, ret);
191 return ret;
192 }
193
Hemant Kumarde1df692016-04-26 19:36:48 -0700194 if (dwc->maximum_speed == USB_SPEED_HIGH)
195 goto generic_phy_init;
196
Mayank Ranaa99689a2016-08-10 17:39:47 -0700197 ret = usb_phy_init(dwc->usb3_phy);
198 if (ret == -EBUSY) {
199 /*
200 * Setting Max speed as high when USB3 PHY initialiation
201 * is failing and USB superspeed can't be supported.
202 */
203 dwc->maximum_speed = USB_SPEED_HIGH;
204 } else if (ret) {
205 pr_err("%s: usb_phy_init(dwc->usb3_phy) returned %d\n",
206 __func__, ret);
207 return ret;
208 }
Hemant Kumarde1df692016-04-26 19:36:48 -0700209
210generic_phy_init:
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530211 ret = phy_init(dwc->usb2_generic_phy);
212 if (ret < 0)
213 return ret;
214
215 ret = phy_init(dwc->usb3_generic_phy);
216 if (ret < 0) {
217 phy_exit(dwc->usb2_generic_phy);
218 return ret;
219 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300220
Mayank Ranaa99689a2016-08-10 17:39:47 -0700221 return 0;
222}
Felipe Balbi72246da2011-08-19 18:10:58 +0300223
Mayank Ranaa99689a2016-08-10 17:39:47 -0700224/**
225 * dwc3_core_reset - Issues core soft reset and PHY reset
226 * @dwc: pointer to our context structure
227 */
228static int dwc3_core_reset(struct dwc3 *dwc)
229{
230 int ret;
Mayank Ranaf8ebb7f2016-09-08 11:09:37 -0700231 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300232
Mayank Ranaa99689a2016-08-10 17:39:47 -0700233 /* Reset PHYs */
234 usb_phy_reset(dwc->usb2_phy);
Hemant Kumarde1df692016-04-26 19:36:48 -0700235
236 if (dwc->maximum_speed == USB_SPEED_SUPER)
237 usb_phy_reset(dwc->usb3_phy);
Pratyush Anand45627ac2012-06-21 17:44:28 +0530238
Mayank Ranaa99689a2016-08-10 17:39:47 -0700239 /* Initialize PHYs */
240 ret = dwc3_init_usb_phys(dwc);
241 if (ret) {
242 pr_err("%s: dwc3_init_phys returned %d\n",
243 __func__, ret);
244 return ret;
245 }
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530246
Mayank Ranaf8ebb7f2016-09-08 11:09:37 -0700247 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
248 reg &= ~DWC3_GUSB3PIPECTL_DELAYP1TRANS;
249 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
250
Mayank Ranaa99689a2016-08-10 17:39:47 -0700251 dwc3_notify_event(dwc, DWC3_CONTROLLER_RESET_EVENT);
252
253 dwc3_notify_event(dwc, DWC3_CONTROLLER_POST_RESET_EVENT);
254
255 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300256}
257
258/**
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300259 * dwc3_soft_reset - Issue soft reset
260 * @dwc: Pointer to our controller context structure
261 */
262static int dwc3_soft_reset(struct dwc3 *dwc)
263{
264 unsigned long timeout;
265 u32 reg;
266
267 timeout = jiffies + msecs_to_jiffies(500);
268 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
269 do {
270 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
271 if (!(reg & DWC3_DCTL_CSFTRST))
272 break;
273
274 if (time_after(jiffies, timeout)) {
275 dev_err(dwc->dev, "Reset Timed Out\n");
276 return -ETIMEDOUT;
277 }
278
279 cpu_relax();
280 } while (true);
281
282 return 0;
283}
284
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530285/*
286 * dwc3_frame_length_adjustment - Adjusts frame length if required
287 * @dwc3: Pointer to our controller context structure
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530288 */
Felipe Balbibcdb3272016-05-16 10:42:23 +0300289static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530290{
291 u32 reg;
292 u32 dft;
293
294 if (dwc->revision < DWC3_REVISION_250A)
295 return;
296
Felipe Balbibcdb3272016-05-16 10:42:23 +0300297 if (dwc->fladj == 0)
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530298 return;
299
300 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
301 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
Felipe Balbibcdb3272016-05-16 10:42:23 +0300302 if (!dev_WARN_ONCE(dwc->dev, dft == dwc->fladj,
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530303 "request value same as default, ignoring\n")) {
304 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
Felipe Balbibcdb3272016-05-16 10:42:23 +0300305 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530306 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
307 }
308}
309
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300310/**
Felipe Balbi72246da2011-08-19 18:10:58 +0300311 * dwc3_free_one_event_buffer - Frees one event buffer
312 * @dwc: Pointer to our controller context structure
313 * @evt: Pointer to event buffer to be freed
314 */
315static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
316 struct dwc3_event_buffer *evt)
317{
318 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300319}
320
321/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800322 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300323 * @dwc: Pointer to our controller context structure
324 * @length: size of the event buffer
325 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800326 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300327 * otherwise ERR_PTR(errno).
328 */
Felipe Balbi67d0b502013-02-22 16:31:07 +0200329static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
330 unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300331{
332 struct dwc3_event_buffer *evt;
333
Felipe Balbi380f0d22012-10-11 13:48:36 +0300334 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300335 if (!evt)
336 return ERR_PTR(-ENOMEM);
337
338 evt->dwc = dwc;
339 evt->length = length;
340 evt->buf = dma_alloc_coherent(dwc->dev, length,
341 &evt->dma, GFP_KERNEL);
Felipe Balbie32672f2012-11-08 15:26:41 +0200342 if (!evt->buf)
Felipe Balbi72246da2011-08-19 18:10:58 +0300343 return ERR_PTR(-ENOMEM);
Felipe Balbi72246da2011-08-19 18:10:58 +0300344
345 return evt;
346}
347
348/**
349 * dwc3_free_event_buffers - frees all allocated event buffers
350 * @dwc: Pointer to our controller context structure
351 */
352static void dwc3_free_event_buffers(struct dwc3 *dwc)
353{
354 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300355
Felipe Balbi696c8b12016-03-30 09:37:03 +0300356 evt = dwc->ev_buf;
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300357 if (evt)
358 dwc3_free_one_event_buffer(dwc, evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300359}
360
361/**
362 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800363 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300364 * @length: size of event buffer
365 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800366 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300367 * may contain some buffers allocated but not all which were requested.
368 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500369static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300370{
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300371 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300372
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300373 evt = dwc3_alloc_one_event_buffer(dwc, length);
374 if (IS_ERR(evt)) {
375 dev_err(dwc->dev, "can't allocate event buffer\n");
376 return PTR_ERR(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300377 }
Felipe Balbi696c8b12016-03-30 09:37:03 +0300378 dwc->ev_buf = evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300379
380 return 0;
381}
382
383/**
384 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800385 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300386 *
387 * Returns 0 on success otherwise negative errno.
388 */
Mayank Ranaa99689a2016-08-10 17:39:47 -0700389int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300390{
391 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300392
Felipe Balbi696c8b12016-03-30 09:37:03 +0300393 evt = dwc->ev_buf;
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300394 dwc3_trace(trace_dwc3_core,
395 "Event buf %p dma %08llx length %d\n",
396 evt->buf, (unsigned long long) evt->dma,
397 evt->length);
Felipe Balbi72246da2011-08-19 18:10:58 +0300398
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300399 evt->lpos = 0;
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300400
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300401 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
402 lower_32_bits(evt->dma));
403 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
404 upper_32_bits(evt->dma));
405 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
406 DWC3_GEVNTSIZ_SIZE(evt->length));
407 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300408
409 return 0;
410}
411
412static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
413{
414 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300415
Felipe Balbi696c8b12016-03-30 09:37:03 +0300416 evt = dwc->ev_buf;
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300417
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300418 evt->lpos = 0;
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300419
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300420 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
421 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
422 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
423 | DWC3_GEVNTSIZ_SIZE(0));
424 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300425}
426
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600427static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
428{
429 if (!dwc->has_hibernation)
430 return 0;
431
432 if (!dwc->nr_scratch)
433 return 0;
434
435 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
436 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
437 if (!dwc->scratchbuf)
438 return -ENOMEM;
439
440 return 0;
441}
442
443static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
444{
445 dma_addr_t scratch_addr;
446 u32 param;
447 int ret;
448
449 if (!dwc->has_hibernation)
450 return 0;
451
452 if (!dwc->nr_scratch)
453 return 0;
454
455 /* should never fall here */
456 if (!WARN_ON(dwc->scratchbuf))
457 return 0;
458
459 scratch_addr = dma_map_single(dwc->dev, dwc->scratchbuf,
460 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
461 DMA_BIDIRECTIONAL);
462 if (dma_mapping_error(dwc->dev, scratch_addr)) {
463 dev_err(dwc->dev, "failed to map scratch buffer\n");
464 ret = -EFAULT;
465 goto err0;
466 }
467
468 dwc->scratch_addr = scratch_addr;
469
470 param = lower_32_bits(scratch_addr);
471
472 ret = dwc3_send_gadget_generic_command(dwc,
473 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
474 if (ret < 0)
475 goto err1;
476
477 param = upper_32_bits(scratch_addr);
478
479 ret = dwc3_send_gadget_generic_command(dwc,
480 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
481 if (ret < 0)
482 goto err1;
483
484 return 0;
485
486err1:
487 dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
488 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
489
490err0:
491 return ret;
492}
493
494static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
495{
496 if (!dwc->has_hibernation)
497 return;
498
499 if (!dwc->nr_scratch)
500 return;
501
502 /* should never fall here */
503 if (!WARN_ON(dwc->scratchbuf))
504 return;
505
506 dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
507 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
508 kfree(dwc->scratchbuf);
509}
510
Felipe Balbi789451f62011-05-05 15:53:10 +0300511static void dwc3_core_num_eps(struct dwc3 *dwc)
512{
513 struct dwc3_hwparams *parms = &dwc->hwparams;
514
515 dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
516 dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
517
Felipe Balbi73815282015-01-27 13:48:14 -0600518 dwc3_trace(trace_dwc3_core, "found %d IN and %d OUT endpoints",
Felipe Balbi789451f62011-05-05 15:53:10 +0300519 dwc->num_in_eps, dwc->num_out_eps);
520}
521
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500522static void dwc3_cache_hwparams(struct dwc3 *dwc)
Felipe Balbi26ceca92011-09-30 10:58:49 +0300523{
524 struct dwc3_hwparams *parms = &dwc->hwparams;
525
526 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
527 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
528 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
529 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
530 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
531 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
532 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
533 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
534 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
535}
536
Felipe Balbi72246da2011-08-19 18:10:58 +0300537/**
Huang Ruib5a65c42014-10-28 19:54:28 +0800538 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
539 * @dwc: Pointer to our controller context structure
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300540 *
541 * Returns 0 on success. The USB PHY interfaces are configured but not
542 * initialized. The PHY interfaces and the PHYs get initialized together with
543 * the core in dwc3_core_init.
Huang Ruib5a65c42014-10-28 19:54:28 +0800544 */
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300545static int dwc3_phy_setup(struct dwc3 *dwc)
Huang Ruib5a65c42014-10-28 19:54:28 +0800546{
547 u32 reg;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300548 int ret;
Huang Ruib5a65c42014-10-28 19:54:28 +0800549
550 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
551
Huang Rui2164a472014-10-28 19:54:35 +0800552 /*
553 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
554 * to '0' during coreConsultant configuration. So default value
555 * will be '0' when the core is reset. Application needs to set it
556 * to '1' after the core initialization is completed.
557 */
558 if (dwc->revision > DWC3_REVISION_194A)
559 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
560
Huang Ruib5a65c42014-10-28 19:54:28 +0800561 if (dwc->u2ss_inp3_quirk)
562 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
563
Rajesh Bhagate58dd352016-03-14 14:40:50 +0530564 if (dwc->dis_rxdet_inp3_quirk)
565 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
566
Huang Ruidf31f5b2014-10-28 19:54:29 +0800567 if (dwc->req_p1p2p3_quirk)
568 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
569
Huang Ruia2a1d0f2014-10-28 19:54:30 +0800570 if (dwc->del_p1p2p3_quirk)
571 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
572
Huang Rui41c06ff2014-10-28 19:54:31 +0800573 if (dwc->del_phy_power_chg_quirk)
574 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
575
Huang Ruifb67afc2014-10-28 19:54:32 +0800576 if (dwc->lfps_filter_quirk)
577 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
578
Huang Rui14f4ac52014-10-28 19:54:33 +0800579 if (dwc->rx_detect_poll_quirk)
580 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
581
Huang Rui6b6a0c92014-10-31 11:11:12 +0800582 if (dwc->tx_de_emphasis_quirk)
583 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
584
Felipe Balbicd72f892014-11-06 11:31:00 -0600585 if (dwc->dis_u3_susphy_quirk)
Huang Rui59acfa22014-10-31 11:11:13 +0800586 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
587
William Wu00fe0812016-08-16 22:44:39 +0800588 if (dwc->dis_del_phy_power_chg_quirk)
589 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
590
Huang Ruib5a65c42014-10-28 19:54:28 +0800591 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
592
Huang Rui2164a472014-10-28 19:54:35 +0800593 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
594
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300595 /* Select the HS PHY interface */
596 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
597 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
Felipe Balbi43cacb02015-07-01 22:03:09 -0500598 if (dwc->hsphy_interface &&
599 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300600 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300601 break;
Felipe Balbi43cacb02015-07-01 22:03:09 -0500602 } else if (dwc->hsphy_interface &&
603 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300604 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300605 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300606 } else {
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300607 /* Relying on default value. */
608 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
609 break;
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300610 }
611 /* FALLTHROUGH */
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300612 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
613 /* Making sure the interface and PHY are operational */
614 ret = dwc3_soft_reset(dwc);
615 if (ret)
616 return ret;
617
618 udelay(1);
619
620 ret = dwc3_ulpi_init(dwc);
621 if (ret)
622 return ret;
623 /* FALLTHROUGH */
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300624 default:
625 break;
626 }
627
William Wu32f2ed82016-08-16 22:44:38 +0800628 switch (dwc->hsphy_mode) {
629 case USBPHY_INTERFACE_MODE_UTMI:
630 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
631 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
632 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
633 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
634 break;
635 case USBPHY_INTERFACE_MODE_UTMIW:
636 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
637 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
638 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
639 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
640 break;
641 default:
642 break;
643 }
644
Huang Rui2164a472014-10-28 19:54:35 +0800645 /*
646 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
647 * '0' during coreConsultant configuration. So default value will
648 * be '0' when the core is reset. Application needs to set it to
649 * '1' after the core initialization is completed.
650 */
651 if (dwc->revision > DWC3_REVISION_194A)
652 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
653
Felipe Balbicd72f892014-11-06 11:31:00 -0600654 if (dwc->dis_u2_susphy_quirk)
Huang Rui0effe0a2014-10-31 11:11:14 +0800655 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
656
John Younec791d12015-10-02 20:30:57 -0700657 if (dwc->dis_enblslpm_quirk)
658 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
659
William Wu16199f32016-08-16 22:44:37 +0800660 if (dwc->dis_u2_freeclk_exists_quirk)
661 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
662
Huang Rui2164a472014-10-28 19:54:35 +0800663 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300664
665 return 0;
Huang Ruib5a65c42014-10-28 19:54:28 +0800666}
667
Felipe Balbic499ff72016-05-16 10:49:01 +0300668static void dwc3_core_exit(struct dwc3 *dwc)
669{
670 dwc3_event_buffers_cleanup(dwc);
671
672 usb_phy_shutdown(dwc->usb2_phy);
673 usb_phy_shutdown(dwc->usb3_phy);
674 phy_exit(dwc->usb2_generic_phy);
675 phy_exit(dwc->usb3_generic_phy);
676
677 usb_phy_set_suspend(dwc->usb2_phy, 1);
678 usb_phy_set_suspend(dwc->usb3_phy, 1);
679 phy_power_off(dwc->usb2_generic_phy);
680 phy_power_off(dwc->usb3_generic_phy);
681}
682
Huang Ruib5a65c42014-10-28 19:54:28 +0800683/**
Felipe Balbi72246da2011-08-19 18:10:58 +0300684 * dwc3_core_init - Low-level initialization of DWC3 Core
685 * @dwc: Pointer to our controller context structure
686 *
687 * Returns 0 on success otherwise negative errno.
688 */
Mayank Ranaa99689a2016-08-10 17:39:47 -0700689int dwc3_core_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300690{
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600691 u32 hwparams4 = dwc->hwparams.hwparams4;
Felipe Balbi72246da2011-08-19 18:10:58 +0300692 u32 reg;
693 int ret;
694
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200695 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
696 /* This should read as U3 followed by revision number */
John Youn690fb372015-09-04 19:15:10 -0700697 if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
698 /* Detected DWC_usb3 IP */
699 dwc->revision = reg;
700 } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
701 /* Detected DWC_usb31 IP */
702 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
703 dwc->revision |= DWC3_REVISION_IS_DWC31;
704 } else {
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200705 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
706 ret = -ENODEV;
707 goto err0;
708 }
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200709
Felipe Balbifa0ea132014-09-19 15:51:11 -0500710 /*
711 * Write Linux Version Code to our GUID register so it's easy to figure
712 * out which kernel version a bug was found.
713 */
714 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
715
Paul Zimmerman0e1e5c42014-05-23 11:39:24 -0700716 /* Handle USB2.0-only core configuration */
717 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
718 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
719 if (dwc->maximum_speed == USB_SPEED_SUPER)
720 dwc->maximum_speed = USB_SPEED_HIGH;
721 }
722
Felipe Balbi72246da2011-08-19 18:10:58 +0300723 /* issue device SoftReset too */
Mayank Ranaa99689a2016-08-10 17:39:47 -0700724 ret = dwc3_core_reset(dwc);
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300725 if (ret)
726 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300727
Mayank Ranaa99689a2016-08-10 17:39:47 -0700728 /* issue device SoftReset too */
729 ret = dwc3_soft_reset(dwc);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530730 if (ret)
731 goto err0;
Pratyush Anand58a0f232012-06-21 17:44:29 +0530732
Felipe Balbic499ff72016-05-16 10:49:01 +0300733 ret = dwc3_phy_setup(dwc);
734 if (ret)
735 goto err0;
736
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100737 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800738 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100739
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100740 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100741 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
Felipe Balbi32a4a132014-02-25 14:00:13 -0600742 /**
743 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
744 * issue which would cause xHCI compliance tests to fail.
745 *
746 * Because of that we cannot enable clock gating on such
747 * configurations.
748 *
749 * Refers to:
750 *
751 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
752 * SOF/ITP Mode Used
753 */
754 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
755 dwc->dr_mode == USB_DR_MODE_OTG) &&
756 (dwc->revision >= DWC3_REVISION_210A &&
757 dwc->revision <= DWC3_REVISION_250A))
758 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
759 else
760 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100761 break;
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600762 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
763 /* enable hibernation here */
764 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
Huang Rui2eac3992014-10-28 19:54:22 +0800765
766 /*
767 * REVISIT Enabling this bit so that host-mode hibernation
768 * will work. Device-mode hibernation is not yet implemented.
769 */
770 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600771 break;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100772 default:
Felipe Balbi1407bf12015-11-16 16:06:37 -0600773 dwc3_trace(trace_dwc3_core, "No power optimization available\n");
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100774 }
775
Huang Rui946bd572014-10-28 19:54:23 +0800776 /* check if current dwc3 is on simulation board */
777 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
Felipe Balbi1407bf12015-11-16 16:06:37 -0600778 dwc3_trace(trace_dwc3_core,
779 "running on FPGA platform\n");
Huang Rui946bd572014-10-28 19:54:23 +0800780 dwc->is_fpga = true;
781 }
782
Huang Rui3b812212014-10-28 19:54:25 +0800783 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
784 "disable_scramble cannot be used on non-FPGA builds\n");
785
786 if (dwc->disable_scramble_quirk && dwc->is_fpga)
787 reg |= DWC3_GCTL_DISSCRAMBLE;
788 else
789 reg &= ~DWC3_GCTL_DISSCRAMBLE;
790
Huang Rui9a5b2f32014-10-28 19:54:27 +0800791 if (dwc->u2exit_lfps_quirk)
792 reg |= DWC3_GCTL_U2EXIT_LFPS;
793
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100794 /*
795 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800796 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100797 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800798 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100799 */
800 if (dwc->revision < DWC3_REVISION_190A)
801 reg |= DWC3_GCTL_U2RSTECN;
802
Mayank Ranafb9cd932016-11-03 23:26:38 -0700803 ret = dwc3_get_dr_mode(dwc);
804 if (ret)
805 goto err0;
806
Mayank Ranaa99689a2016-08-10 17:39:47 -0700807 dwc3_core_num_eps(dwc);
808
809 /*
810 * Disable clock gating to work around a known HW bug that causes the
811 * internal RAM clock to get stuck when entering low power modes.
812 */
813 if (dwc->disable_clk_gating) {
814 dev_dbg(dwc->dev, "Disabling controller clock gating.\n");
815 reg |= DWC3_GCTL_DSBLCLKGTNG;
816 }
817
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100818 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
819
Mayank Ranaa99689a2016-08-10 17:39:47 -0700820 ret = dwc3_alloc_scratch_buffers(dwc);
821 if (ret)
822 goto err1;
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600823
824 ret = dwc3_setup_scratch_buffers(dwc);
825 if (ret)
Mayank Ranaa99689a2016-08-10 17:39:47 -0700826 goto err2;
Felipe Balbic499ff72016-05-16 10:49:01 +0300827
828 /* Adjust Frame Length */
829 dwc3_frame_length_adjustment(dwc);
830
831 usb_phy_set_suspend(dwc->usb2_phy, 0);
832 usb_phy_set_suspend(dwc->usb3_phy, 0);
833 ret = phy_power_on(dwc->usb2_generic_phy);
834 if (ret < 0)
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600835 goto err2;
836
Mayank Ranaa99689a2016-08-10 17:39:47 -0700837 /*
838 * clear Elastic buffer mode in GUSBPIPE_CTRL(0) register, otherwise
839 * it results in high link errors and could cause SS mode transfer
840 * failure.
841 */
842 if (!dwc->nominal_elastic_buffer) {
843 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
844 reg &= ~DWC3_GUSB3PIPECTL_ELASTIC_BUF_MODE;
845 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
Felipe Balbic499ff72016-05-16 10:49:01 +0300846 }
847
Baolin Wang00af6232016-07-15 17:13:27 +0800848 switch (dwc->dr_mode) {
849 case USB_DR_MODE_PERIPHERAL:
850 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
851 break;
852 case USB_DR_MODE_HOST:
853 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
854 break;
855 case USB_DR_MODE_OTG:
856 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
857 break;
858 default:
859 dev_warn(dwc->dev, "Unsupported mode %d\n", dwc->dr_mode);
860 break;
861 }
862
John Youn06281d42016-08-22 15:39:13 -0700863 /*
864 * ENDXFER polling is available on version 3.10a and later of
865 * the DWC_usb3 controller. It is NOT available in the
866 * DWC_usb31 controller.
867 */
868 if (!dwc3_is_usb31(dwc) && dwc->revision >= DWC3_REVISION_310A) {
869 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
870 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
871 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
872 }
873
Felipe Balbi72246da2011-08-19 18:10:58 +0300874 return 0;
875
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600876err2:
Mayank Ranaa99689a2016-08-10 17:39:47 -0700877 dwc3_free_scratch_buffers(dwc);
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600878err1:
879 usb_phy_shutdown(dwc->usb2_phy);
880 usb_phy_shutdown(dwc->usb3_phy);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530881 phy_exit(dwc->usb2_generic_phy);
882 phy_exit(dwc->usb3_generic_phy);
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600883
Felipe Balbi72246da2011-08-19 18:10:58 +0300884err0:
885 return ret;
886}
887
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500888static int dwc3_core_get_phy(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300889{
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500890 struct device *dev = dwc->dev;
Felipe Balbi941ea362013-07-31 09:21:25 +0300891 struct device_node *node = dev->of_node;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500892 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300893
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530894 if (node) {
895 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
896 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
Felipe Balbibb674902013-08-14 13:21:23 -0500897 } else {
898 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
899 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530900 }
901
Felipe Balbid105e7f2013-03-15 10:52:08 +0200902 if (IS_ERR(dwc->usb2_phy)) {
903 ret = PTR_ERR(dwc->usb2_phy);
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530904 if (ret == -ENXIO || ret == -ENODEV) {
905 dwc->usb2_phy = NULL;
906 } else if (ret == -EPROBE_DEFER) {
Felipe Balbid105e7f2013-03-15 10:52:08 +0200907 return ret;
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530908 } else {
909 dev_err(dev, "no usb2 phy configured\n");
910 return ret;
911 }
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300912 }
913
Felipe Balbid105e7f2013-03-15 10:52:08 +0200914 if (IS_ERR(dwc->usb3_phy)) {
Ruchika Kharwar315955d72013-07-04 00:59:34 -0500915 ret = PTR_ERR(dwc->usb3_phy);
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530916 if (ret == -ENXIO || ret == -ENODEV) {
917 dwc->usb3_phy = NULL;
918 } else if (ret == -EPROBE_DEFER) {
Felipe Balbid105e7f2013-03-15 10:52:08 +0200919 return ret;
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530920 } else {
921 dev_err(dev, "no usb3 phy configured\n");
922 return ret;
923 }
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300924 }
925
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530926 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
927 if (IS_ERR(dwc->usb2_generic_phy)) {
928 ret = PTR_ERR(dwc->usb2_generic_phy);
929 if (ret == -ENOSYS || ret == -ENODEV) {
930 dwc->usb2_generic_phy = NULL;
931 } else if (ret == -EPROBE_DEFER) {
932 return ret;
933 } else {
934 dev_err(dev, "no usb2 phy configured\n");
935 return ret;
936 }
937 }
938
939 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
940 if (IS_ERR(dwc->usb3_generic_phy)) {
941 ret = PTR_ERR(dwc->usb3_generic_phy);
942 if (ret == -ENOSYS || ret == -ENODEV) {
943 dwc->usb3_generic_phy = NULL;
944 } else if (ret == -EPROBE_DEFER) {
945 return ret;
946 } else {
947 dev_err(dev, "no usb3 phy configured\n");
948 return ret;
949 }
950 }
951
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500952 return 0;
953}
954
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500955static int dwc3_core_init_mode(struct dwc3 *dwc)
956{
957 struct device *dev = dwc->dev;
958 int ret;
959
960 switch (dwc->dr_mode) {
961 case USB_DR_MODE_PERIPHERAL:
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500962 ret = dwc3_gadget_init(dwc);
963 if (ret) {
Roger Quadros9522def2016-06-10 14:48:38 +0300964 if (ret != -EPROBE_DEFER)
965 dev_err(dev, "failed to initialize gadget\n");
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500966 return ret;
967 }
968 break;
969 case USB_DR_MODE_HOST:
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500970 ret = dwc3_host_init(dwc);
971 if (ret) {
Roger Quadros9522def2016-06-10 14:48:38 +0300972 if (ret != -EPROBE_DEFER)
973 dev_err(dev, "failed to initialize host\n");
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500974 return ret;
975 }
976 break;
977 case USB_DR_MODE_OTG:
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500978 ret = dwc3_host_init(dwc);
979 if (ret) {
Roger Quadros9522def2016-06-10 14:48:38 +0300980 if (ret != -EPROBE_DEFER)
981 dev_err(dev, "failed to initialize host\n");
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500982 return ret;
983 }
984
985 ret = dwc3_gadget_init(dwc);
986 if (ret) {
Roger Quadros9522def2016-06-10 14:48:38 +0300987 if (ret != -EPROBE_DEFER)
988 dev_err(dev, "failed to initialize gadget\n");
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500989 return ret;
990 }
991 break;
992 default:
993 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
994 return -EINVAL;
995 }
996
997 return 0;
998}
999
1000static void dwc3_core_exit_mode(struct dwc3 *dwc)
1001{
1002 switch (dwc->dr_mode) {
1003 case USB_DR_MODE_PERIPHERAL:
1004 dwc3_gadget_exit(dwc);
1005 break;
1006 case USB_DR_MODE_HOST:
1007 dwc3_host_exit(dwc);
1008 break;
1009 case USB_DR_MODE_OTG:
1010 dwc3_host_exit(dwc);
1011 dwc3_gadget_exit(dwc);
1012 break;
1013 default:
1014 /* do nothing */
1015 break;
1016 }
1017}
1018
Mayank Ranaa99689a2016-08-10 17:39:47 -07001019/* XHCI reset, resets other CORE registers as well, re-init those */
1020void dwc3_post_host_reset_core_init(struct dwc3 *dwc)
1021{
1022 dwc3_core_init(dwc);
1023 dwc3_gadget_restart(dwc);
1024}
1025
1026static void (*notify_event)(struct dwc3 *, unsigned int);
1027void dwc3_set_notifier(void (*notify)(struct dwc3 *, unsigned int))
1028{
1029 notify_event = notify;
1030}
1031EXPORT_SYMBOL(dwc3_set_notifier);
1032
1033int dwc3_notify_event(struct dwc3 *dwc, unsigned int event)
1034{
1035 int ret = 0;
1036
1037 if (dwc->notify_event)
1038 dwc->notify_event(dwc, event);
1039 else
1040 ret = -ENODEV;
1041
1042 return ret;
1043}
1044EXPORT_SYMBOL(dwc3_notify_event);
1045
1046int dwc3_core_pre_init(struct dwc3 *dwc)
1047{
1048 int ret;
1049
1050 dwc3_cache_hwparams(dwc);
1051
1052 ret = dwc3_phy_setup(dwc);
1053 if (ret)
1054 goto err0;
1055
1056 if (!dwc->ev_buf) {
1057 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1058 if (ret) {
1059 dev_err(dwc->dev, "failed to allocate event buffers\n");
1060 ret = -ENOMEM;
1061 goto err1;
1062 }
1063 }
1064
1065 ret = dwc3_core_init(dwc);
1066 if (ret) {
1067 dev_err(dwc->dev, "failed to initialize core\n");
1068 goto err2;
1069 }
1070
1071 ret = phy_power_on(dwc->usb2_generic_phy);
1072 if (ret < 0)
1073 goto err3;
1074
1075 ret = phy_power_on(dwc->usb3_generic_phy);
1076 if (ret < 0)
1077 goto err4;
1078
1079 ret = dwc3_event_buffers_setup(dwc);
1080 if (ret) {
1081 dev_err(dwc->dev, "failed to setup event buffers\n");
1082 goto err5;
1083 }
1084
Mayank Ranaa99689a2016-08-10 17:39:47 -07001085 return ret;
1086
Mayank Ranaa99689a2016-08-10 17:39:47 -07001087err5:
1088 phy_power_off(dwc->usb3_generic_phy);
1089err4:
1090 phy_power_off(dwc->usb2_generic_phy);
1091err3:
1092 dwc3_core_exit(dwc);
1093err2:
1094 dwc3_free_event_buffers(dwc);
1095err1:
1096 dwc3_ulpi_exit(dwc);
1097err0:
1098 return ret;
1099}
1100
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001101#define DWC3_ALIGN_MASK (16 - 1)
1102
1103static int dwc3_probe(struct platform_device *pdev)
1104{
1105 struct device *dev = &pdev->dev;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001106 struct resource *res;
1107 struct dwc3 *dwc;
Huang Rui80caf7d2014-10-28 19:54:26 +08001108 u8 lpm_nyet_threshold;
Huang Rui6b6a0c92014-10-31 11:11:12 +08001109 u8 tx_de_emphasis;
Huang Rui460d0982014-10-31 11:11:18 +08001110 u8 hird_threshold;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001111 int irq;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001112
Andy Shevchenkob09e99e2014-05-15 15:53:32 +03001113 int ret;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001114
1115 void __iomem *regs;
1116 void *mem;
1117
1118 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001119 if (!mem)
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001120 return -ENOMEM;
Jingoo Han734d5a52014-07-17 12:45:11 +09001121
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001122 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
1123 dwc->mem = mem;
1124 dwc->dev = dev;
1125
Mayank Ranaa99689a2016-08-10 17:39:47 -07001126 dwc->notify_event = notify_event;
1127 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1128 if (!res) {
1129 dev_err(dev, "missing IRQ\n");
1130 return -ENODEV;
1131 }
1132 dwc->xhci_resources[1].start = res->start;
1133 dwc->xhci_resources[1].end = res->end;
1134 dwc->xhci_resources[1].flags = res->flags;
1135 dwc->xhci_resources[1].name = res->name;
1136
1137 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1138
1139 /* will be enabled in dwc3_msm_resume() */
1140 irq_set_status_flags(irq, IRQ_NOAUTOEN);
1141 ret = devm_request_threaded_irq(dev, irq, NULL, dwc3_interrupt,
1142 IRQF_SHARED | IRQF_ONESHOT, "dwc3", dwc);
1143 if (ret) {
1144 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1145 irq, ret);
1146 return -ENODEV;
1147 }
1148
1149 dwc->irq = irq;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001150 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1151 if (!res) {
1152 dev_err(dev, "missing memory resource\n");
1153 return -ENODEV;
1154 }
1155
Mayank Ranaa99689a2016-08-10 17:39:47 -07001156 dwc->reg_phys = res->start;
Vivek Gautamf32a5e22014-06-04 14:34:52 +05301157 dwc->xhci_resources[0].start = res->start;
1158 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1159 DWC3_XHCI_REGS_END;
1160 dwc->xhci_resources[0].flags = res->flags;
1161 dwc->xhci_resources[0].name = res->name;
1162
1163 res->start += DWC3_GLOBALS_REGS_START;
1164
1165 /*
1166 * Request memory region but exclude xHCI regs,
1167 * since it will be requested by the xhci-plat driver.
1168 */
1169 regs = devm_ioremap_resource(dev, res);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001170 if (IS_ERR(regs)) {
1171 ret = PTR_ERR(regs);
1172 goto err0;
1173 }
Vivek Gautamf32a5e22014-06-04 14:34:52 +05301174
1175 dwc->regs = regs;
1176 dwc->regs_size = resource_size(res);
Vivek Gautamf32a5e22014-06-04 14:34:52 +05301177
Huang Rui80caf7d2014-10-28 19:54:26 +08001178 /* default to highest possible threshold */
1179 lpm_nyet_threshold = 0xff;
1180
Huang Rui6b6a0c92014-10-31 11:11:12 +08001181 /* default to -3.5dB de-emphasis */
1182 tx_de_emphasis = 1;
1183
Huang Rui460d0982014-10-31 11:11:18 +08001184 /*
1185 * default to assert utmi_sleep_n and use maximum allowed HIRD
1186 * threshold value of 0b1100
1187 */
1188 hird_threshold = 12;
1189
Heikki Krogerus63863b92015-09-21 11:14:32 +03001190 dwc->maximum_speed = usb_get_maximum_speed(dev);
Heikki Krogerus06e71142015-09-21 11:14:34 +03001191 dwc->dr_mode = usb_get_dr_mode(dev);
Mayank Ranafb9cd932016-11-03 23:26:38 -07001192
1193 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN) {
1194 dwc->dr_mode = USB_DR_MODE_OTG;
1195 dwc->is_drd = 1;
1196 }
1197
William Wu32f2ed82016-08-16 22:44:38 +08001198 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
Heikki Krogerus63863b92015-09-21 11:14:32 +03001199
Heikki Krogerus3d128912015-09-21 11:14:35 +03001200 dwc->has_lpm_erratum = device_property_read_bool(dev,
Huang Rui80caf7d2014-10-28 19:54:26 +08001201 "snps,has-lpm-erratum");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001202 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
Huang Rui80caf7d2014-10-28 19:54:26 +08001203 &lpm_nyet_threshold);
Heikki Krogerus3d128912015-09-21 11:14:35 +03001204 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
Huang Rui460d0982014-10-31 11:11:18 +08001205 "snps,is-utmi-l1-suspend");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001206 device_property_read_u8(dev, "snps,hird-threshold",
Huang Rui460d0982014-10-31 11:11:18 +08001207 &hird_threshold);
Heikki Krogerus3d128912015-09-21 11:14:35 +03001208 dwc->usb3_lpm_capable = device_property_read_bool(dev,
Robert Baldygaeac68e82015-03-09 15:06:12 +01001209 "snps,usb3_lpm_capable");
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001210
Mayank Ranaa8e4de62016-12-13 17:11:15 -08001211 dwc->needs_fifo_resize = device_property_read_bool(dev,
1212 "tx-fifo-resize");
1213
Heikki Krogerus3d128912015-09-21 11:14:35 +03001214 dwc->disable_scramble_quirk = device_property_read_bool(dev,
Huang Rui3b812212014-10-28 19:54:25 +08001215 "snps,disable_scramble_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001216 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
Huang Rui9a5b2f32014-10-28 19:54:27 +08001217 "snps,u2exit_lfps_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001218 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
Huang Ruib5a65c42014-10-28 19:54:28 +08001219 "snps,u2ss_inp3_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001220 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
Huang Ruidf31f5b2014-10-28 19:54:29 +08001221 "snps,req_p1p2p3_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001222 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
Huang Ruia2a1d0f2014-10-28 19:54:30 +08001223 "snps,del_p1p2p3_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001224 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
Huang Rui41c06ff2014-10-28 19:54:31 +08001225 "snps,del_phy_power_chg_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001226 dwc->lfps_filter_quirk = device_property_read_bool(dev,
Huang Ruifb67afc2014-10-28 19:54:32 +08001227 "snps,lfps_filter_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001228 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
Huang Rui14f4ac52014-10-28 19:54:33 +08001229 "snps,rx_detect_poll_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001230 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
Huang Rui59acfa22014-10-31 11:11:13 +08001231 "snps,dis_u3_susphy_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001232 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
Huang Rui0effe0a2014-10-31 11:11:14 +08001233 "snps,dis_u2_susphy_quirk");
John Younec791d12015-10-02 20:30:57 -07001234 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1235 "snps,dis_enblslpm_quirk");
Rajesh Bhagate58dd352016-03-14 14:40:50 +05301236 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1237 "snps,dis_rxdet_inp3_quirk");
William Wu16199f32016-08-16 22:44:37 +08001238 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1239 "snps,dis-u2-freeclk-exists-quirk");
William Wu00fe0812016-08-16 22:44:39 +08001240 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1241 "snps,dis-del-phy-power-chg-quirk");
Huang Rui6b6a0c92014-10-31 11:11:12 +08001242
Heikki Krogerus3d128912015-09-21 11:14:35 +03001243 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
Huang Rui6b6a0c92014-10-31 11:11:12 +08001244 "snps,tx_de_emphasis_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001245 device_property_read_u8(dev, "snps,tx_de_emphasis",
Huang Rui6b6a0c92014-10-31 11:11:12 +08001246 &tx_de_emphasis);
Heikki Krogerus3d128912015-09-21 11:14:35 +03001247 device_property_read_string(dev, "snps,hsphy_interface",
1248 &dwc->hsphy_interface);
1249 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
Felipe Balbibcdb3272016-05-16 10:42:23 +03001250 &dwc->fladj);
Heikki Krogerus3d128912015-09-21 11:14:35 +03001251
Mayank Ranaa99689a2016-08-10 17:39:47 -07001252 if (dwc->enable_bus_suspend) {
1253 pm_runtime_set_autosuspend_delay(dev, 500);
1254 pm_runtime_use_autosuspend(dev);
1255 }
1256
Huang Rui80caf7d2014-10-28 19:54:26 +08001257 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
Huang Rui6b6a0c92014-10-31 11:11:12 +08001258 dwc->tx_de_emphasis = tx_de_emphasis;
Huang Rui80caf7d2014-10-28 19:54:26 +08001259
Huang Rui460d0982014-10-31 11:11:18 +08001260 dwc->hird_threshold = hird_threshold
1261 | (dwc->is_utmi_l1_suspend << 4);
1262
Mayank Ranaa99689a2016-08-10 17:39:47 -07001263 init_waitqueue_head(&dwc->wait_linkstate);
Heikki Krogerus6c89cce02015-05-13 15:26:45 +03001264 platform_set_drvdata(pdev, dwc);
1265
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001266 ret = dwc3_core_get_phy(dwc);
1267 if (ret)
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001268 goto err0;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001269
Felipe Balbi72246da2011-08-19 18:10:58 +03001270 spin_lock_init(&dwc->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03001271
Heikki Krogerus19bacdc2014-09-24 11:00:38 +03001272 if (!dev->dma_mask) {
1273 dev->dma_mask = dev->parent->dma_mask;
1274 dev->dma_parms = dev->parent->dma_parms;
1275 dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
1276 }
Kishon Vijay Abraham Iddff14f2013-03-07 18:51:43 +05301277
Mayank Ranaa99689a2016-08-10 17:39:47 -07001278 pm_runtime_no_callbacks(dev);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001279 pm_runtime_set_active(dev);
Chanho Park802ca852012-02-15 18:27:55 +09001280 pm_runtime_enable(dev);
Chanho Park802ca852012-02-15 18:27:55 +09001281 pm_runtime_forbid(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001282
John Youn77966eb2016-02-19 17:31:01 -08001283 /* Check the maximum_speed parameter */
1284 switch (dwc->maximum_speed) {
1285 case USB_SPEED_LOW:
1286 case USB_SPEED_FULL:
1287 case USB_SPEED_HIGH:
1288 case USB_SPEED_SUPER:
1289 case USB_SPEED_SUPER_PLUS:
1290 break;
1291 default:
1292 dev_err(dev, "invalid maximum_speed parameter %d\n",
1293 dwc->maximum_speed);
1294 /* fall through */
1295 case USB_SPEED_UNKNOWN:
1296 /* default to superspeed */
John Youn2c7f1bd2016-02-05 17:08:59 -08001297 dwc->maximum_speed = USB_SPEED_SUPER;
1298
1299 /*
1300 * default to superspeed plus if we are capable.
1301 */
1302 if (dwc3_is_usb31(dwc) &&
1303 (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
1304 DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1305 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
John Youn77966eb2016-02-19 17:31:01 -08001306
1307 break;
John Youn2c7f1bd2016-02-05 17:08:59 -08001308 }
1309
Mayank Ranaa99689a2016-08-10 17:39:47 -07001310 /* Adjust Frame Length */
1311 dwc3_frame_length_adjustment(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001312
Mayank Ranaa99689a2016-08-10 17:39:47 -07001313 /* Hardcode number of eps */
1314 dwc->num_in_eps = 16;
1315 dwc->num_out_eps = 16;
Felipe Balbi72246da2011-08-19 18:10:58 +03001316
Felipe Balbi72246da2011-08-19 18:10:58 +03001317 ret = dwc3_core_init_mode(dwc);
1318 if (ret)
Kyle Yan65be4a52016-10-31 15:05:00 -07001319 goto err0;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001320
1321 ret = dwc3_debugfs_init(dwc);
1322 if (ret) {
1323 dev_err(dev, "failed to initialize debugfs\n");
Kyle Yan65be4a52016-10-31 15:05:00 -07001324 goto err_core_init;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001325 }
1326
1327 pm_runtime_allow(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001328 return 0;
1329
Kyle Yan65be4a52016-10-31 15:05:00 -07001330err_core_init:
1331 dwc3_core_exit_mode(dwc);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001332err0:
1333 /*
1334 * restore res->start back to its original value so that, in case the
1335 * probe is deferred, we don't end up getting error in request the
1336 * memory region the next time probe is called.
1337 */
1338 res->start -= DWC3_GLOBALS_REGS_START;
1339
Felipe Balbi72246da2011-08-19 18:10:58 +03001340 return ret;
1341}
1342
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05001343static int dwc3_remove(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +03001344{
Felipe Balbi72246da2011-08-19 18:10:58 +03001345 struct dwc3 *dwc = platform_get_drvdata(pdev);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001346 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1347
Felipe Balbifc8bb912016-05-16 13:14:48 +03001348 pm_runtime_get_sync(&pdev->dev);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001349 /*
1350 * restore res->start back to its original value so that, in case the
1351 * probe is deferred, we don't end up getting error in request the
1352 * memory region the next time probe is called.
1353 */
1354 res->start -= DWC3_GLOBALS_REGS_START;
Felipe Balbi72246da2011-08-19 18:10:58 +03001355
Felipe Balbidc99f162014-09-03 16:13:37 -05001356 dwc3_debugfs_exit(dwc);
1357 dwc3_core_exit_mode(dwc);
Kishon Vijay Abraham I8ba007a2013-01-25 08:30:54 +05301358
Felipe Balbi72246da2011-08-19 18:10:58 +03001359 dwc3_core_exit(dwc);
Heikki Krogerus88bc9d12015-05-13 15:26:51 +03001360 dwc3_ulpi_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001361
Felipe Balbifc8bb912016-05-16 13:14:48 +03001362 pm_runtime_put_sync(&pdev->dev);
1363 pm_runtime_allow(&pdev->dev);
1364 pm_runtime_disable(&pdev->dev);
1365
Felipe Balbic499ff72016-05-16 10:49:01 +03001366 dwc3_free_event_buffers(dwc);
1367 dwc3_free_scratch_buffers(dwc);
1368
Felipe Balbi72246da2011-08-19 18:10:58 +03001369 return 0;
1370}
1371
Felipe Balbifc8bb912016-05-16 13:14:48 +03001372#ifdef CONFIG_PM
1373static int dwc3_suspend_common(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03001374{
Felipe Balbifc8bb912016-05-16 13:14:48 +03001375 unsigned long flags;
Felipe Balbi7415f172012-04-30 14:56:33 +03001376
Ruchika Kharwara45c82b82013-07-06 07:52:49 -05001377 switch (dwc->dr_mode) {
1378 case USB_DR_MODE_PERIPHERAL:
1379 case USB_DR_MODE_OTG:
Felipe Balbifc8bb912016-05-16 13:14:48 +03001380 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7415f172012-04-30 14:56:33 +03001381 dwc3_gadget_suspend(dwc);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001382 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi51f5d492016-05-16 10:52:58 +03001383 break;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -05001384 case USB_DR_MODE_HOST:
Felipe Balbi7415f172012-04-30 14:56:33 +03001385 default:
Felipe Balbi51f5d492016-05-16 10:52:58 +03001386 /* do nothing */
Felipe Balbi7415f172012-04-30 14:56:33 +03001387 break;
1388 }
1389
Felipe Balbi51f5d492016-05-16 10:52:58 +03001390 dwc3_core_exit(dwc);
Felipe Balbi5c4ad3182016-04-11 17:12:34 +03001391
Felipe Balbifc8bb912016-05-16 13:14:48 +03001392 return 0;
1393}
1394
1395static int dwc3_resume_common(struct dwc3 *dwc)
1396{
1397 unsigned long flags;
1398 int ret;
1399
1400 ret = dwc3_core_init(dwc);
1401 if (ret)
1402 return ret;
1403
1404 switch (dwc->dr_mode) {
1405 case USB_DR_MODE_PERIPHERAL:
1406 case USB_DR_MODE_OTG:
1407 spin_lock_irqsave(&dwc->lock, flags);
1408 dwc3_gadget_resume(dwc);
1409 spin_unlock_irqrestore(&dwc->lock, flags);
1410 /* FALLTHROUGH */
1411 case USB_DR_MODE_HOST:
1412 default:
1413 /* do nothing */
1414 break;
1415 }
1416
1417 return 0;
1418}
1419
1420static int dwc3_runtime_checks(struct dwc3 *dwc)
1421{
1422 switch (dwc->dr_mode) {
1423 case USB_DR_MODE_PERIPHERAL:
1424 case USB_DR_MODE_OTG:
1425 if (dwc->connected)
1426 return -EBUSY;
1427 break;
1428 case USB_DR_MODE_HOST:
1429 default:
1430 /* do nothing */
1431 break;
1432 }
1433
1434 return 0;
1435}
1436
1437static int dwc3_runtime_suspend(struct device *dev)
1438{
1439 struct dwc3 *dwc = dev_get_drvdata(dev);
1440 int ret;
1441
Mayank Ranaa99689a2016-08-10 17:39:47 -07001442 /* Check if platform glue driver handling PM, if not then handle here */
1443 if (!dwc3_notify_event(dwc, DWC3_CORE_PM_RESUME_EVENT))
1444 return 0;
Felipe Balbifc8bb912016-05-16 13:14:48 +03001445
1446 ret = dwc3_suspend_common(dwc);
1447 if (ret)
1448 return ret;
1449
1450 device_init_wakeup(dev, true);
1451
1452 return 0;
1453}
1454
1455static int dwc3_runtime_resume(struct device *dev)
1456{
1457 struct dwc3 *dwc = dev_get_drvdata(dev);
1458 int ret;
1459
Mayank Ranaa99689a2016-08-10 17:39:47 -07001460 /* Check if platform glue driver handling PM, if not then handle here */
1461 if (!dwc3_notify_event(dwc, DWC3_CORE_PM_RESUME_EVENT))
1462 return 0;
1463
Felipe Balbifc8bb912016-05-16 13:14:48 +03001464 device_init_wakeup(dev, false);
1465
1466 ret = dwc3_resume_common(dwc);
1467 if (ret)
1468 return ret;
1469
1470 switch (dwc->dr_mode) {
1471 case USB_DR_MODE_PERIPHERAL:
1472 case USB_DR_MODE_OTG:
1473 dwc3_gadget_process_pending_events(dwc);
1474 break;
1475 case USB_DR_MODE_HOST:
1476 default:
1477 /* do nothing */
1478 break;
1479 }
1480
1481 pm_runtime_mark_last_busy(dev);
Felipe Balbib74c2d82016-07-28 13:07:07 +03001482 pm_runtime_put(dev);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001483
1484 return 0;
1485}
1486
1487static int dwc3_runtime_idle(struct device *dev)
1488{
1489 struct dwc3 *dwc = dev_get_drvdata(dev);
1490
1491 switch (dwc->dr_mode) {
1492 case USB_DR_MODE_PERIPHERAL:
1493 case USB_DR_MODE_OTG:
1494 if (dwc3_runtime_checks(dwc))
1495 return -EBUSY;
1496 break;
1497 case USB_DR_MODE_HOST:
1498 default:
1499 /* do nothing */
1500 break;
1501 }
1502
1503 pm_runtime_mark_last_busy(dev);
1504 pm_runtime_autosuspend(dev);
1505
1506 return 0;
1507}
1508#endif /* CONFIG_PM */
1509
1510#ifdef CONFIG_PM_SLEEP
1511static int dwc3_suspend(struct device *dev)
1512{
1513 struct dwc3 *dwc = dev_get_drvdata(dev);
1514 int ret;
1515
1516 ret = dwc3_suspend_common(dwc);
1517 if (ret)
1518 return ret;
1519
Sekhar Nori63444752015-08-31 21:09:08 +05301520 pinctrl_pm_select_sleep_state(dev);
1521
Felipe Balbi7415f172012-04-30 14:56:33 +03001522 return 0;
1523}
1524
1525static int dwc3_resume(struct device *dev)
1526{
1527 struct dwc3 *dwc = dev_get_drvdata(dev);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +05301528 int ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03001529
Sekhar Nori63444752015-08-31 21:09:08 +05301530 pinctrl_pm_select_default_state(dev);
1531
Felipe Balbifc8bb912016-05-16 13:14:48 +03001532 ret = dwc3_resume_common(dwc);
Felipe Balbi51f5d492016-05-16 10:52:58 +03001533 if (ret)
Felipe Balbi5c4ad3182016-04-11 17:12:34 +03001534 return ret;
1535
Felipe Balbi7415f172012-04-30 14:56:33 +03001536 pm_runtime_disable(dev);
1537 pm_runtime_set_active(dev);
1538 pm_runtime_enable(dev);
1539
1540 return 0;
1541}
Felipe Balbi7f370ed2016-05-09 15:27:01 +03001542#endif /* CONFIG_PM_SLEEP */
Felipe Balbi7415f172012-04-30 14:56:33 +03001543
1544static const struct dev_pm_ops dwc3_dev_pm_ops = {
Felipe Balbi7415f172012-04-30 14:56:33 +03001545 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
Felipe Balbifc8bb912016-05-16 13:14:48 +03001546 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
1547 dwc3_runtime_idle)
Felipe Balbi7415f172012-04-30 14:56:33 +03001548};
1549
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301550#ifdef CONFIG_OF
1551static const struct of_device_id of_dwc3_match[] = {
1552 {
Felipe Balbi22a5aa12013-07-02 21:20:24 +03001553 .compatible = "snps,dwc3"
1554 },
1555 {
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301556 .compatible = "synopsys,dwc3"
1557 },
1558 { },
1559};
1560MODULE_DEVICE_TABLE(of, of_dwc3_match);
1561#endif
1562
Heikki Krogerus404905a2014-09-25 10:57:02 +03001563#ifdef CONFIG_ACPI
1564
1565#define ACPI_ID_INTEL_BSW "808622B7"
1566
1567static const struct acpi_device_id dwc3_acpi_match[] = {
1568 { ACPI_ID_INTEL_BSW, 0 },
1569 { },
1570};
1571MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
1572#endif
1573
Felipe Balbi72246da2011-08-19 18:10:58 +03001574static struct platform_driver dwc3_driver = {
1575 .probe = dwc3_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05001576 .remove = dwc3_remove,
Felipe Balbi72246da2011-08-19 18:10:58 +03001577 .driver = {
1578 .name = "dwc3",
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301579 .of_match_table = of_match_ptr(of_dwc3_match),
Heikki Krogerus404905a2014-09-25 10:57:02 +03001580 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
Felipe Balbi7f370ed2016-05-09 15:27:01 +03001581 .pm = &dwc3_dev_pm_ops,
Felipe Balbi72246da2011-08-19 18:10:58 +03001582 },
Felipe Balbi72246da2011-08-19 18:10:58 +03001583};
1584
Tobias Klauserb1116dc2012-02-28 12:57:20 +01001585module_platform_driver(dwc3_driver);
1586
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +02001587MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +03001588MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
Felipe Balbi5945f782013-06-30 14:15:11 +03001589MODULE_LICENSE("GPL v2");
Felipe Balbi72246da2011-08-19 18:10:58 +03001590MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");