blob: 7b054aa9e49fec39aea7ab4f412f70edb3fa4788 [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 Rana861da2b2016-07-13 13:47:57 -070053static int count;
54static struct dwc3 *dwc3_instance[DWC_CTRL_COUNT];
55
Mayank Ranaa99689a2016-08-10 17:39:47 -070056void dwc3_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
57{
58 u32 reg;
59
60 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
61
62 if (suspend)
63 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
64 else
65 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
66
67 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
68}
69
Thinh Nguyen9d6173e2016-09-06 19:22:03 -070070/**
71 * dwc3_get_dr_mode - Validates and sets dr_mode
72 * @dwc: pointer to our context structure
73 */
74static int dwc3_get_dr_mode(struct dwc3 *dwc)
75{
76 enum usb_dr_mode mode;
77 struct device *dev = dwc->dev;
78 unsigned int hw_mode;
79
Thinh Nguyen9d6173e2016-09-06 19:22:03 -070080
Mayank Ranafb9cd932016-11-03 23:26:38 -070081 dwc->is_drd = 0;
Thinh Nguyen9d6173e2016-09-06 19:22:03 -070082 mode = dwc->dr_mode;
83 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
84
85 switch (hw_mode) {
86 case DWC3_GHWPARAMS0_MODE_GADGET:
87 if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) {
88 dev_err(dev,
89 "Controller does not support host mode.\n");
90 return -EINVAL;
91 }
92 mode = USB_DR_MODE_PERIPHERAL;
93 break;
94 case DWC3_GHWPARAMS0_MODE_HOST:
95 if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
96 dev_err(dev,
97 "Controller does not support device mode.\n");
98 return -EINVAL;
99 }
100 mode = USB_DR_MODE_HOST;
101 break;
102 default:
103 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
104 mode = USB_DR_MODE_HOST;
105 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
106 mode = USB_DR_MODE_PERIPHERAL;
107 }
108
109 if (mode != dwc->dr_mode) {
110 dev_warn(dev,
111 "Configuration mismatch. dr_mode forced to %s\n",
112 mode == USB_DR_MODE_HOST ? "host" : "gadget");
113
114 dwc->dr_mode = mode;
115 }
116
Mayank Ranafb9cd932016-11-03 23:26:38 -0700117 if (dwc->dr_mode == USB_DR_MODE_OTG)
118 dwc->is_drd = 1;
119
Thinh Nguyen9d6173e2016-09-06 19:22:03 -0700120 return 0;
121}
122
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100123void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
124{
125 u32 reg;
126
127 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
128 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
129 reg |= DWC3_GCTL_PRTCAPDIR(mode);
130 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700131
132 /*
133 * Set this bit so that device attempts three more times at SS, even
134 * if it failed previously to operate in SS mode.
135 */
136 reg |= DWC3_GCTL_U2RSTECN;
137 reg &= ~(DWC3_GCTL_SOFITPSYNC);
138 reg &= ~(DWC3_GCTL_PWRDNSCALEMASK);
139 reg |= DWC3_GCTL_PWRDNSCALE(2);
140 reg |= DWC3_GCTL_U2EXIT_LFPS;
141 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
142
143 if (mode == DWC3_GCTL_PRTCAP_OTG || mode == DWC3_GCTL_PRTCAP_HOST) {
144 /*
145 * Allow ITP generated off of ref clk based counter instead
146 * of UTMI/ULPI clk based counter, when superspeed only is
147 * active so that UTMI/ULPI PHY can be suspened.
148 *
149 * Starting with revision 2.50A, GFLADJ_REFCLK_LPM_SEL is used
150 * instead.
151 */
152 if (dwc->revision < DWC3_REVISION_250A) {
153 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
154 reg |= DWC3_GCTL_SOFITPSYNC;
155 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
156 } else {
157 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
158 reg |= DWC3_GFLADJ_REFCLK_LPM_SEL;
159 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
160 }
161 }
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100162}
Felipe Balbi8300dd22011-10-18 13:54:01 +0300163
Felipe Balbicf6d8672016-04-14 15:03:39 +0300164u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
165{
166 struct dwc3 *dwc = dep->dwc;
167 u32 reg;
168
169 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
170 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
171 DWC3_GDBGFIFOSPACE_TYPE(type));
172
173 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
174
175 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
176}
177
Felipe Balbi72246da2011-08-19 18:10:58 +0300178/**
Mayank Ranaa99689a2016-08-10 17:39:47 -0700179 * Peforms initialization of HS and SS PHYs.
180 * If used as a part of POR or init sequence it is recommended
181 * that we should perform hard reset of the PHYs prior to invoking
182 * this function.
Felipe Balbi72246da2011-08-19 18:10:58 +0300183 * @dwc: pointer to our context structure
Mayank Ranaa99689a2016-08-10 17:39:47 -0700184*/
185static int dwc3_init_usb_phys(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300186{
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530187 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300188
Mayank Ranaa99689a2016-08-10 17:39:47 -0700189 /* Bring up PHYs */
190 ret = usb_phy_init(dwc->usb2_phy);
191 if (ret) {
192 pr_err("%s: usb_phy_init(dwc->usb2_phy) returned %d\n",
193 __func__, ret);
194 return ret;
195 }
196
Hemant Kumarde1df692016-04-26 19:36:48 -0700197 if (dwc->maximum_speed == USB_SPEED_HIGH)
198 goto generic_phy_init;
199
Mayank Ranaa99689a2016-08-10 17:39:47 -0700200 ret = usb_phy_init(dwc->usb3_phy);
201 if (ret == -EBUSY) {
202 /*
203 * Setting Max speed as high when USB3 PHY initialiation
204 * is failing and USB superspeed can't be supported.
205 */
206 dwc->maximum_speed = USB_SPEED_HIGH;
207 } else if (ret) {
208 pr_err("%s: usb_phy_init(dwc->usb3_phy) returned %d\n",
209 __func__, ret);
210 return ret;
211 }
Hemant Kumarde1df692016-04-26 19:36:48 -0700212
213generic_phy_init:
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530214 ret = phy_init(dwc->usb2_generic_phy);
215 if (ret < 0)
216 return ret;
217
218 ret = phy_init(dwc->usb3_generic_phy);
219 if (ret < 0) {
220 phy_exit(dwc->usb2_generic_phy);
221 return ret;
222 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300223
Mayank Ranaa99689a2016-08-10 17:39:47 -0700224 return 0;
225}
Felipe Balbi72246da2011-08-19 18:10:58 +0300226
Mayank Ranaa99689a2016-08-10 17:39:47 -0700227/**
228 * dwc3_core_reset - Issues core soft reset and PHY reset
229 * @dwc: pointer to our context structure
230 */
231static int dwc3_core_reset(struct dwc3 *dwc)
232{
233 int ret;
Mayank Ranaf8ebb7f2016-09-08 11:09:37 -0700234 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300235
Mayank Ranaa99689a2016-08-10 17:39:47 -0700236 /* Reset PHYs */
237 usb_phy_reset(dwc->usb2_phy);
Hemant Kumarde1df692016-04-26 19:36:48 -0700238
239 if (dwc->maximum_speed == USB_SPEED_SUPER)
240 usb_phy_reset(dwc->usb3_phy);
Pratyush Anand45627ac2012-06-21 17:44:28 +0530241
Mayank Ranaa99689a2016-08-10 17:39:47 -0700242 /* Initialize PHYs */
243 ret = dwc3_init_usb_phys(dwc);
244 if (ret) {
245 pr_err("%s: dwc3_init_phys returned %d\n",
246 __func__, ret);
247 return ret;
248 }
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530249
Mayank Ranaf8ebb7f2016-09-08 11:09:37 -0700250 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
251 reg &= ~DWC3_GUSB3PIPECTL_DELAYP1TRANS;
252 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
253
Mayank Ranaa99689a2016-08-10 17:39:47 -0700254 dwc3_notify_event(dwc, DWC3_CONTROLLER_RESET_EVENT);
255
256 dwc3_notify_event(dwc, DWC3_CONTROLLER_POST_RESET_EVENT);
257
258 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300259}
260
261/**
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300262 * dwc3_soft_reset - Issue soft reset
263 * @dwc: Pointer to our controller context structure
264 */
265static int dwc3_soft_reset(struct dwc3 *dwc)
266{
267 unsigned long timeout;
268 u32 reg;
269
270 timeout = jiffies + msecs_to_jiffies(500);
271 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
272 do {
273 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
274 if (!(reg & DWC3_DCTL_CSFTRST))
275 break;
276
277 if (time_after(jiffies, timeout)) {
278 dev_err(dwc->dev, "Reset Timed Out\n");
279 return -ETIMEDOUT;
280 }
281
282 cpu_relax();
283 } while (true);
284
285 return 0;
286}
287
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530288/*
289 * dwc3_frame_length_adjustment - Adjusts frame length if required
290 * @dwc3: Pointer to our controller context structure
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530291 */
Felipe Balbibcdb3272016-05-16 10:42:23 +0300292static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530293{
294 u32 reg;
295 u32 dft;
296
297 if (dwc->revision < DWC3_REVISION_250A)
298 return;
299
Felipe Balbibcdb3272016-05-16 10:42:23 +0300300 if (dwc->fladj == 0)
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530301 return;
302
303 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
304 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
Felipe Balbibcdb3272016-05-16 10:42:23 +0300305 if (!dev_WARN_ONCE(dwc->dev, dft == dwc->fladj,
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530306 "request value same as default, ignoring\n")) {
307 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
Felipe Balbibcdb3272016-05-16 10:42:23 +0300308 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530309 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
310 }
311}
312
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300313/**
Felipe Balbi72246da2011-08-19 18:10:58 +0300314 * dwc3_free_one_event_buffer - Frees one event buffer
315 * @dwc: Pointer to our controller context structure
316 * @evt: Pointer to event buffer to be freed
317 */
318static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
319 struct dwc3_event_buffer *evt)
320{
321 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300322}
323
324/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800325 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300326 * @dwc: Pointer to our controller context structure
327 * @length: size of the event buffer
328 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800329 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300330 * otherwise ERR_PTR(errno).
331 */
Felipe Balbi67d0b502013-02-22 16:31:07 +0200332static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
333 unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300334{
335 struct dwc3_event_buffer *evt;
336
Felipe Balbi380f0d22012-10-11 13:48:36 +0300337 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300338 if (!evt)
339 return ERR_PTR(-ENOMEM);
340
341 evt->dwc = dwc;
342 evt->length = length;
343 evt->buf = dma_alloc_coherent(dwc->dev, length,
344 &evt->dma, GFP_KERNEL);
Felipe Balbie32672f2012-11-08 15:26:41 +0200345 if (!evt->buf)
Felipe Balbi72246da2011-08-19 18:10:58 +0300346 return ERR_PTR(-ENOMEM);
Felipe Balbi72246da2011-08-19 18:10:58 +0300347
348 return evt;
349}
350
351/**
352 * dwc3_free_event_buffers - frees all allocated event buffers
353 * @dwc: Pointer to our controller context structure
354 */
355static void dwc3_free_event_buffers(struct dwc3 *dwc)
356{
357 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300358
Felipe Balbi696c8b12016-03-30 09:37:03 +0300359 evt = dwc->ev_buf;
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300360 if (evt)
361 dwc3_free_one_event_buffer(dwc, evt);
Mayank Ranaf4918d32016-12-15 13:35:55 -0800362
363 /* free GSI related event buffers */
364 dwc3_notify_event(dwc, DWC3_GSI_EVT_BUF_FREE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300365}
366
367/**
368 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800369 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300370 * @length: size of event buffer
371 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800372 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300373 * may contain some buffers allocated but not all which were requested.
374 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500375static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300376{
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300377 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300378
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300379 evt = dwc3_alloc_one_event_buffer(dwc, length);
380 if (IS_ERR(evt)) {
381 dev_err(dwc->dev, "can't allocate event buffer\n");
382 return PTR_ERR(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300383 }
Felipe Balbi696c8b12016-03-30 09:37:03 +0300384 dwc->ev_buf = evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300385
Mayank Ranaf4918d32016-12-15 13:35:55 -0800386 /* alloc GSI related event buffers */
387 dwc3_notify_event(dwc, DWC3_GSI_EVT_BUF_ALLOC);
Felipe Balbi72246da2011-08-19 18:10:58 +0300388 return 0;
389}
390
391/**
392 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800393 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300394 *
395 * Returns 0 on success otherwise negative errno.
396 */
Mayank Ranaa99689a2016-08-10 17:39:47 -0700397int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300398{
399 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300400
Felipe Balbi696c8b12016-03-30 09:37:03 +0300401 evt = dwc->ev_buf;
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300402 dwc3_trace(trace_dwc3_core,
403 "Event buf %p dma %08llx length %d\n",
404 evt->buf, (unsigned long long) evt->dma,
405 evt->length);
Felipe Balbi72246da2011-08-19 18:10:58 +0300406
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300407 evt->lpos = 0;
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300408
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300409 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
410 lower_32_bits(evt->dma));
411 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
412 upper_32_bits(evt->dma));
413 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
414 DWC3_GEVNTSIZ_SIZE(evt->length));
415 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300416
Mayank Ranaf4918d32016-12-15 13:35:55 -0800417 /* setup GSI related event buffers */
418 dwc3_notify_event(dwc, DWC3_GSI_EVT_BUF_SETUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300419 return 0;
420}
421
422static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
423{
424 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300425
Felipe Balbi696c8b12016-03-30 09:37:03 +0300426 evt = dwc->ev_buf;
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300427
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300428 evt->lpos = 0;
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300429
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300430 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
431 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
432 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
433 | DWC3_GEVNTSIZ_SIZE(0));
434 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
Mayank Ranaf4918d32016-12-15 13:35:55 -0800435
436 /* cleanup GSI related event buffers */
437 dwc3_notify_event(dwc, DWC3_GSI_EVT_BUF_CLEANUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300438}
439
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600440static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
441{
442 if (!dwc->has_hibernation)
443 return 0;
444
445 if (!dwc->nr_scratch)
446 return 0;
447
448 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
449 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
450 if (!dwc->scratchbuf)
451 return -ENOMEM;
452
453 return 0;
454}
455
456static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
457{
458 dma_addr_t scratch_addr;
459 u32 param;
460 int ret;
461
462 if (!dwc->has_hibernation)
463 return 0;
464
465 if (!dwc->nr_scratch)
466 return 0;
467
468 /* should never fall here */
469 if (!WARN_ON(dwc->scratchbuf))
470 return 0;
471
472 scratch_addr = dma_map_single(dwc->dev, dwc->scratchbuf,
473 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
474 DMA_BIDIRECTIONAL);
475 if (dma_mapping_error(dwc->dev, scratch_addr)) {
476 dev_err(dwc->dev, "failed to map scratch buffer\n");
477 ret = -EFAULT;
478 goto err0;
479 }
480
481 dwc->scratch_addr = scratch_addr;
482
483 param = lower_32_bits(scratch_addr);
484
485 ret = dwc3_send_gadget_generic_command(dwc,
486 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
487 if (ret < 0)
488 goto err1;
489
490 param = upper_32_bits(scratch_addr);
491
492 ret = dwc3_send_gadget_generic_command(dwc,
493 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
494 if (ret < 0)
495 goto err1;
496
497 return 0;
498
499err1:
500 dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
501 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
502
503err0:
504 return ret;
505}
506
507static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
508{
509 if (!dwc->has_hibernation)
510 return;
511
512 if (!dwc->nr_scratch)
513 return;
514
515 /* should never fall here */
516 if (!WARN_ON(dwc->scratchbuf))
517 return;
518
519 dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
520 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
521 kfree(dwc->scratchbuf);
522}
523
Felipe Balbi789451f62011-05-05 15:53:10 +0300524static void dwc3_core_num_eps(struct dwc3 *dwc)
525{
526 struct dwc3_hwparams *parms = &dwc->hwparams;
527
528 dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
529 dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
530
Felipe Balbi73815282015-01-27 13:48:14 -0600531 dwc3_trace(trace_dwc3_core, "found %d IN and %d OUT endpoints",
Felipe Balbi789451f62011-05-05 15:53:10 +0300532 dwc->num_in_eps, dwc->num_out_eps);
533}
534
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500535static void dwc3_cache_hwparams(struct dwc3 *dwc)
Felipe Balbi26ceca92011-09-30 10:58:49 +0300536{
537 struct dwc3_hwparams *parms = &dwc->hwparams;
538
539 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
540 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
541 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
542 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
543 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
544 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
545 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
546 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
547 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
548}
549
Felipe Balbi72246da2011-08-19 18:10:58 +0300550/**
Huang Ruib5a65c42014-10-28 19:54:28 +0800551 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
552 * @dwc: Pointer to our controller context structure
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300553 *
554 * Returns 0 on success. The USB PHY interfaces are configured but not
555 * initialized. The PHY interfaces and the PHYs get initialized together with
556 * the core in dwc3_core_init.
Huang Ruib5a65c42014-10-28 19:54:28 +0800557 */
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300558static int dwc3_phy_setup(struct dwc3 *dwc)
Huang Ruib5a65c42014-10-28 19:54:28 +0800559{
560 u32 reg;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300561 int ret;
Huang Ruib5a65c42014-10-28 19:54:28 +0800562
563 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
564
Huang Rui2164a472014-10-28 19:54:35 +0800565 /*
566 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
567 * to '0' during coreConsultant configuration. So default value
568 * will be '0' when the core is reset. Application needs to set it
569 * to '1' after the core initialization is completed.
570 */
571 if (dwc->revision > DWC3_REVISION_194A)
572 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
573
Huang Ruib5a65c42014-10-28 19:54:28 +0800574 if (dwc->u2ss_inp3_quirk)
575 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
576
Rajesh Bhagate58dd352016-03-14 14:40:50 +0530577 if (dwc->dis_rxdet_inp3_quirk)
578 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
579
Huang Ruidf31f5b2014-10-28 19:54:29 +0800580 if (dwc->req_p1p2p3_quirk)
581 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
582
Huang Ruia2a1d0f2014-10-28 19:54:30 +0800583 if (dwc->del_p1p2p3_quirk)
584 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
585
Huang Rui41c06ff2014-10-28 19:54:31 +0800586 if (dwc->del_phy_power_chg_quirk)
587 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
588
Huang Ruifb67afc2014-10-28 19:54:32 +0800589 if (dwc->lfps_filter_quirk)
590 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
591
Huang Rui14f4ac52014-10-28 19:54:33 +0800592 if (dwc->rx_detect_poll_quirk)
593 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
594
Huang Rui6b6a0c92014-10-31 11:11:12 +0800595 if (dwc->tx_de_emphasis_quirk)
596 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
597
Felipe Balbicd72f892014-11-06 11:31:00 -0600598 if (dwc->dis_u3_susphy_quirk)
Huang Rui59acfa22014-10-31 11:11:13 +0800599 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
600
William Wu00fe0812016-08-16 22:44:39 +0800601 if (dwc->dis_del_phy_power_chg_quirk)
602 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
603
Huang Ruib5a65c42014-10-28 19:54:28 +0800604 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
605
Huang Rui2164a472014-10-28 19:54:35 +0800606 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
607
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300608 /* Select the HS PHY interface */
609 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
610 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
Felipe Balbi43cacb02015-07-01 22:03:09 -0500611 if (dwc->hsphy_interface &&
612 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300613 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300614 break;
Felipe Balbi43cacb02015-07-01 22:03:09 -0500615 } else if (dwc->hsphy_interface &&
616 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300617 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300618 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300619 } else {
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300620 /* Relying on default value. */
621 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
622 break;
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300623 }
624 /* FALLTHROUGH */
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300625 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
626 /* Making sure the interface and PHY are operational */
627 ret = dwc3_soft_reset(dwc);
628 if (ret)
629 return ret;
630
631 udelay(1);
632
633 ret = dwc3_ulpi_init(dwc);
634 if (ret)
635 return ret;
636 /* FALLTHROUGH */
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300637 default:
638 break;
639 }
640
William Wu32f2ed82016-08-16 22:44:38 +0800641 switch (dwc->hsphy_mode) {
642 case USBPHY_INTERFACE_MODE_UTMI:
643 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
644 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
645 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
646 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
647 break;
648 case USBPHY_INTERFACE_MODE_UTMIW:
649 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
650 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
651 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
652 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
653 break;
654 default:
655 break;
656 }
657
Huang Rui2164a472014-10-28 19:54:35 +0800658 /*
659 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
660 * '0' during coreConsultant configuration. So default value will
661 * be '0' when the core is reset. Application needs to set it to
662 * '1' after the core initialization is completed.
663 */
664 if (dwc->revision > DWC3_REVISION_194A)
665 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
666
Felipe Balbicd72f892014-11-06 11:31:00 -0600667 if (dwc->dis_u2_susphy_quirk)
Huang Rui0effe0a2014-10-31 11:11:14 +0800668 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
669
John Younec791d12015-10-02 20:30:57 -0700670 if (dwc->dis_enblslpm_quirk)
671 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
672
William Wu16199f32016-08-16 22:44:37 +0800673 if (dwc->dis_u2_freeclk_exists_quirk)
674 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
675
Huang Rui2164a472014-10-28 19:54:35 +0800676 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300677
678 return 0;
Huang Ruib5a65c42014-10-28 19:54:28 +0800679}
680
Felipe Balbic499ff72016-05-16 10:49:01 +0300681static void dwc3_core_exit(struct dwc3 *dwc)
682{
683 dwc3_event_buffers_cleanup(dwc);
684
685 usb_phy_shutdown(dwc->usb2_phy);
686 usb_phy_shutdown(dwc->usb3_phy);
687 phy_exit(dwc->usb2_generic_phy);
688 phy_exit(dwc->usb3_generic_phy);
689
690 usb_phy_set_suspend(dwc->usb2_phy, 1);
691 usb_phy_set_suspend(dwc->usb3_phy, 1);
692 phy_power_off(dwc->usb2_generic_phy);
693 phy_power_off(dwc->usb3_generic_phy);
694}
695
Huang Ruib5a65c42014-10-28 19:54:28 +0800696/**
Felipe Balbi72246da2011-08-19 18:10:58 +0300697 * dwc3_core_init - Low-level initialization of DWC3 Core
698 * @dwc: Pointer to our controller context structure
699 *
700 * Returns 0 on success otherwise negative errno.
701 */
Mayank Ranaa99689a2016-08-10 17:39:47 -0700702int dwc3_core_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300703{
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600704 u32 hwparams4 = dwc->hwparams.hwparams4;
Felipe Balbi72246da2011-08-19 18:10:58 +0300705 u32 reg;
706 int ret;
707
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200708 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
709 /* This should read as U3 followed by revision number */
John Youn690fb372015-09-04 19:15:10 -0700710 if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
711 /* Detected DWC_usb3 IP */
712 dwc->revision = reg;
713 } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
714 /* Detected DWC_usb31 IP */
715 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
716 dwc->revision |= DWC3_REVISION_IS_DWC31;
717 } else {
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200718 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
719 ret = -ENODEV;
720 goto err0;
721 }
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200722
Felipe Balbifa0ea132014-09-19 15:51:11 -0500723 /*
724 * Write Linux Version Code to our GUID register so it's easy to figure
725 * out which kernel version a bug was found.
726 */
727 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
728
Paul Zimmerman0e1e5c42014-05-23 11:39:24 -0700729 /* Handle USB2.0-only core configuration */
730 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
731 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
Vamsi Krishna Samavedam86ed20b2017-01-31 13:55:38 -0800732 if (dwc->max_hw_supp_speed == USB_SPEED_SUPER) {
733 dwc->max_hw_supp_speed = USB_SPEED_HIGH;
734 dwc->maximum_speed = dwc->max_hw_supp_speed;
735 }
Paul Zimmerman0e1e5c42014-05-23 11:39:24 -0700736 }
737
Felipe Balbi72246da2011-08-19 18:10:58 +0300738 /* issue device SoftReset too */
Mayank Ranaa99689a2016-08-10 17:39:47 -0700739 ret = dwc3_core_reset(dwc);
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300740 if (ret)
741 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300742
Mayank Ranaa99689a2016-08-10 17:39:47 -0700743 /* issue device SoftReset too */
744 ret = dwc3_soft_reset(dwc);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530745 if (ret)
746 goto err0;
Pratyush Anand58a0f232012-06-21 17:44:29 +0530747
Felipe Balbic499ff72016-05-16 10:49:01 +0300748 ret = dwc3_phy_setup(dwc);
749 if (ret)
750 goto err0;
751
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100752 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800753 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100754
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100755 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100756 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
Felipe Balbi32a4a132014-02-25 14:00:13 -0600757 /**
758 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
759 * issue which would cause xHCI compliance tests to fail.
760 *
761 * Because of that we cannot enable clock gating on such
762 * configurations.
763 *
764 * Refers to:
765 *
766 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
767 * SOF/ITP Mode Used
768 */
769 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
770 dwc->dr_mode == USB_DR_MODE_OTG) &&
771 (dwc->revision >= DWC3_REVISION_210A &&
772 dwc->revision <= DWC3_REVISION_250A))
773 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
774 else
775 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100776 break;
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600777 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
778 /* enable hibernation here */
779 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
Huang Rui2eac3992014-10-28 19:54:22 +0800780
781 /*
782 * REVISIT Enabling this bit so that host-mode hibernation
783 * will work. Device-mode hibernation is not yet implemented.
784 */
785 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600786 break;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100787 default:
Felipe Balbi1407bf12015-11-16 16:06:37 -0600788 dwc3_trace(trace_dwc3_core, "No power optimization available\n");
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100789 }
790
Huang Rui946bd572014-10-28 19:54:23 +0800791 /* check if current dwc3 is on simulation board */
792 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
Felipe Balbi1407bf12015-11-16 16:06:37 -0600793 dwc3_trace(trace_dwc3_core,
794 "running on FPGA platform\n");
Huang Rui946bd572014-10-28 19:54:23 +0800795 dwc->is_fpga = true;
796 }
797
Huang Rui3b812212014-10-28 19:54:25 +0800798 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
799 "disable_scramble cannot be used on non-FPGA builds\n");
800
801 if (dwc->disable_scramble_quirk && dwc->is_fpga)
802 reg |= DWC3_GCTL_DISSCRAMBLE;
803 else
804 reg &= ~DWC3_GCTL_DISSCRAMBLE;
805
Huang Rui9a5b2f32014-10-28 19:54:27 +0800806 if (dwc->u2exit_lfps_quirk)
807 reg |= DWC3_GCTL_U2EXIT_LFPS;
808
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100809 /*
810 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800811 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100812 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800813 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100814 */
815 if (dwc->revision < DWC3_REVISION_190A)
816 reg |= DWC3_GCTL_U2RSTECN;
817
Mayank Ranafb9cd932016-11-03 23:26:38 -0700818 ret = dwc3_get_dr_mode(dwc);
819 if (ret)
820 goto err0;
821
Mayank Ranaa99689a2016-08-10 17:39:47 -0700822 dwc3_core_num_eps(dwc);
823
824 /*
825 * Disable clock gating to work around a known HW bug that causes the
826 * internal RAM clock to get stuck when entering low power modes.
827 */
828 if (dwc->disable_clk_gating) {
829 dev_dbg(dwc->dev, "Disabling controller clock gating.\n");
830 reg |= DWC3_GCTL_DSBLCLKGTNG;
831 }
832
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100833 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
834
Mayank Ranaa99689a2016-08-10 17:39:47 -0700835 ret = dwc3_alloc_scratch_buffers(dwc);
836 if (ret)
837 goto err1;
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600838
839 ret = dwc3_setup_scratch_buffers(dwc);
840 if (ret)
Mayank Ranaa99689a2016-08-10 17:39:47 -0700841 goto err2;
Felipe Balbic499ff72016-05-16 10:49:01 +0300842
843 /* Adjust Frame Length */
844 dwc3_frame_length_adjustment(dwc);
845
846 usb_phy_set_suspend(dwc->usb2_phy, 0);
847 usb_phy_set_suspend(dwc->usb3_phy, 0);
848 ret = phy_power_on(dwc->usb2_generic_phy);
849 if (ret < 0)
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600850 goto err2;
851
Mayank Ranaa99689a2016-08-10 17:39:47 -0700852 /*
853 * clear Elastic buffer mode in GUSBPIPE_CTRL(0) register, otherwise
854 * it results in high link errors and could cause SS mode transfer
855 * failure.
856 */
857 if (!dwc->nominal_elastic_buffer) {
858 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
859 reg &= ~DWC3_GUSB3PIPECTL_ELASTIC_BUF_MODE;
860 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
Felipe Balbic499ff72016-05-16 10:49:01 +0300861 }
862
Baolin Wang00af6232016-07-15 17:13:27 +0800863 switch (dwc->dr_mode) {
864 case USB_DR_MODE_PERIPHERAL:
865 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
866 break;
867 case USB_DR_MODE_HOST:
868 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
869 break;
870 case USB_DR_MODE_OTG:
871 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
872 break;
873 default:
874 dev_warn(dwc->dev, "Unsupported mode %d\n", dwc->dr_mode);
875 break;
876 }
877
John Youn06281d42016-08-22 15:39:13 -0700878 /*
879 * ENDXFER polling is available on version 3.10a and later of
880 * the DWC_usb3 controller. It is NOT available in the
881 * DWC_usb31 controller.
882 */
883 if (!dwc3_is_usb31(dwc) && dwc->revision >= DWC3_REVISION_310A) {
884 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
885 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
886 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
887 }
888
Felipe Balbi72246da2011-08-19 18:10:58 +0300889 return 0;
890
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600891err2:
Mayank Ranaa99689a2016-08-10 17:39:47 -0700892 dwc3_free_scratch_buffers(dwc);
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600893err1:
894 usb_phy_shutdown(dwc->usb2_phy);
895 usb_phy_shutdown(dwc->usb3_phy);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530896 phy_exit(dwc->usb2_generic_phy);
897 phy_exit(dwc->usb3_generic_phy);
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600898
Felipe Balbi72246da2011-08-19 18:10:58 +0300899err0:
900 return ret;
901}
902
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500903static int dwc3_core_get_phy(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300904{
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500905 struct device *dev = dwc->dev;
Felipe Balbi941ea362013-07-31 09:21:25 +0300906 struct device_node *node = dev->of_node;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500907 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300908
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530909 if (node) {
910 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
911 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
Felipe Balbibb674902013-08-14 13:21:23 -0500912 } else {
913 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
914 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530915 }
916
Felipe Balbid105e7f2013-03-15 10:52:08 +0200917 if (IS_ERR(dwc->usb2_phy)) {
918 ret = PTR_ERR(dwc->usb2_phy);
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530919 if (ret == -ENXIO || ret == -ENODEV) {
920 dwc->usb2_phy = NULL;
921 } else if (ret == -EPROBE_DEFER) {
Felipe Balbid105e7f2013-03-15 10:52:08 +0200922 return ret;
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530923 } else {
924 dev_err(dev, "no usb2 phy configured\n");
925 return ret;
926 }
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300927 }
928
Felipe Balbid105e7f2013-03-15 10:52:08 +0200929 if (IS_ERR(dwc->usb3_phy)) {
Ruchika Kharwar315955d72013-07-04 00:59:34 -0500930 ret = PTR_ERR(dwc->usb3_phy);
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530931 if (ret == -ENXIO || ret == -ENODEV) {
932 dwc->usb3_phy = NULL;
933 } else if (ret == -EPROBE_DEFER) {
Felipe Balbid105e7f2013-03-15 10:52:08 +0200934 return ret;
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530935 } else {
936 dev_err(dev, "no usb3 phy configured\n");
937 return ret;
938 }
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300939 }
940
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530941 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
942 if (IS_ERR(dwc->usb2_generic_phy)) {
943 ret = PTR_ERR(dwc->usb2_generic_phy);
944 if (ret == -ENOSYS || ret == -ENODEV) {
945 dwc->usb2_generic_phy = NULL;
946 } else if (ret == -EPROBE_DEFER) {
947 return ret;
948 } else {
949 dev_err(dev, "no usb2 phy configured\n");
950 return ret;
951 }
952 }
953
954 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
955 if (IS_ERR(dwc->usb3_generic_phy)) {
956 ret = PTR_ERR(dwc->usb3_generic_phy);
957 if (ret == -ENOSYS || ret == -ENODEV) {
958 dwc->usb3_generic_phy = NULL;
959 } else if (ret == -EPROBE_DEFER) {
960 return ret;
961 } else {
962 dev_err(dev, "no usb3 phy configured\n");
963 return ret;
964 }
965 }
966
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500967 return 0;
968}
969
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500970static int dwc3_core_init_mode(struct dwc3 *dwc)
971{
972 struct device *dev = dwc->dev;
973 int ret;
974
975 switch (dwc->dr_mode) {
976 case USB_DR_MODE_PERIPHERAL:
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500977 ret = dwc3_gadget_init(dwc);
978 if (ret) {
Roger Quadros9522def2016-06-10 14:48:38 +0300979 if (ret != -EPROBE_DEFER)
980 dev_err(dev, "failed to initialize gadget\n");
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500981 return ret;
982 }
983 break;
984 case USB_DR_MODE_HOST:
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500985 ret = dwc3_host_init(dwc);
986 if (ret) {
Roger Quadros9522def2016-06-10 14:48:38 +0300987 if (ret != -EPROBE_DEFER)
988 dev_err(dev, "failed to initialize host\n");
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500989 return ret;
990 }
991 break;
992 case USB_DR_MODE_OTG:
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500993 ret = dwc3_host_init(dwc);
994 if (ret) {
Roger Quadros9522def2016-06-10 14:48:38 +0300995 if (ret != -EPROBE_DEFER)
996 dev_err(dev, "failed to initialize host\n");
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500997 return ret;
998 }
999
1000 ret = dwc3_gadget_init(dwc);
1001 if (ret) {
Roger Quadros9522def2016-06-10 14:48:38 +03001002 if (ret != -EPROBE_DEFER)
1003 dev_err(dev, "failed to initialize gadget\n");
Felipe Balbi5f94adf2014-04-16 15:13:45 -05001004 return ret;
1005 }
1006 break;
1007 default:
1008 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1009 return -EINVAL;
1010 }
1011
1012 return 0;
1013}
1014
1015static void dwc3_core_exit_mode(struct dwc3 *dwc)
1016{
1017 switch (dwc->dr_mode) {
1018 case USB_DR_MODE_PERIPHERAL:
1019 dwc3_gadget_exit(dwc);
1020 break;
1021 case USB_DR_MODE_HOST:
1022 dwc3_host_exit(dwc);
1023 break;
1024 case USB_DR_MODE_OTG:
1025 dwc3_host_exit(dwc);
1026 dwc3_gadget_exit(dwc);
1027 break;
1028 default:
1029 /* do nothing */
1030 break;
1031 }
1032}
1033
Mayank Ranaa99689a2016-08-10 17:39:47 -07001034/* XHCI reset, resets other CORE registers as well, re-init those */
1035void dwc3_post_host_reset_core_init(struct dwc3 *dwc)
1036{
1037 dwc3_core_init(dwc);
1038 dwc3_gadget_restart(dwc);
1039}
1040
1041static void (*notify_event)(struct dwc3 *, unsigned int);
1042void dwc3_set_notifier(void (*notify)(struct dwc3 *, unsigned int))
1043{
1044 notify_event = notify;
1045}
1046EXPORT_SYMBOL(dwc3_set_notifier);
1047
1048int dwc3_notify_event(struct dwc3 *dwc, unsigned int event)
1049{
1050 int ret = 0;
1051
1052 if (dwc->notify_event)
1053 dwc->notify_event(dwc, event);
1054 else
1055 ret = -ENODEV;
1056
1057 return ret;
1058}
1059EXPORT_SYMBOL(dwc3_notify_event);
1060
1061int dwc3_core_pre_init(struct dwc3 *dwc)
1062{
1063 int ret;
1064
1065 dwc3_cache_hwparams(dwc);
1066
1067 ret = dwc3_phy_setup(dwc);
1068 if (ret)
1069 goto err0;
1070
1071 if (!dwc->ev_buf) {
1072 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1073 if (ret) {
1074 dev_err(dwc->dev, "failed to allocate event buffers\n");
1075 ret = -ENOMEM;
1076 goto err1;
1077 }
1078 }
1079
1080 ret = dwc3_core_init(dwc);
1081 if (ret) {
1082 dev_err(dwc->dev, "failed to initialize core\n");
1083 goto err2;
1084 }
1085
1086 ret = phy_power_on(dwc->usb2_generic_phy);
1087 if (ret < 0)
1088 goto err3;
1089
1090 ret = phy_power_on(dwc->usb3_generic_phy);
1091 if (ret < 0)
1092 goto err4;
1093
1094 ret = dwc3_event_buffers_setup(dwc);
1095 if (ret) {
1096 dev_err(dwc->dev, "failed to setup event buffers\n");
1097 goto err5;
1098 }
1099
Mayank Ranaa99689a2016-08-10 17:39:47 -07001100 return ret;
1101
Mayank Ranaa99689a2016-08-10 17:39:47 -07001102err5:
1103 phy_power_off(dwc->usb3_generic_phy);
1104err4:
1105 phy_power_off(dwc->usb2_generic_phy);
1106err3:
1107 dwc3_core_exit(dwc);
1108err2:
1109 dwc3_free_event_buffers(dwc);
1110err1:
1111 dwc3_ulpi_exit(dwc);
1112err0:
1113 return ret;
1114}
1115
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001116#define DWC3_ALIGN_MASK (16 - 1)
1117
1118static int dwc3_probe(struct platform_device *pdev)
1119{
1120 struct device *dev = &pdev->dev;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001121 struct resource *res;
1122 struct dwc3 *dwc;
Huang Rui80caf7d2014-10-28 19:54:26 +08001123 u8 lpm_nyet_threshold;
Huang Rui6b6a0c92014-10-31 11:11:12 +08001124 u8 tx_de_emphasis;
Huang Rui460d0982014-10-31 11:11:18 +08001125 u8 hird_threshold;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001126 int irq;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001127
Andy Shevchenkob09e99e2014-05-15 15:53:32 +03001128 int ret;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001129
1130 void __iomem *regs;
1131 void *mem;
1132
Mayank Rana861da2b2016-07-13 13:47:57 -07001133 if (count >= DWC_CTRL_COUNT) {
1134 dev_err(dev, "Err dwc instance %d >= %d available\n",
1135 count, DWC_CTRL_COUNT);
1136 ret = -EINVAL;
1137 return ret;
1138 }
1139
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001140 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001141 if (!mem)
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001142 return -ENOMEM;
Jingoo Han734d5a52014-07-17 12:45:11 +09001143
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001144 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
1145 dwc->mem = mem;
1146 dwc->dev = dev;
1147
Mayank Ranaa99689a2016-08-10 17:39:47 -07001148 dwc->notify_event = notify_event;
1149 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1150 if (!res) {
1151 dev_err(dev, "missing IRQ\n");
1152 return -ENODEV;
1153 }
1154 dwc->xhci_resources[1].start = res->start;
1155 dwc->xhci_resources[1].end = res->end;
1156 dwc->xhci_resources[1].flags = res->flags;
1157 dwc->xhci_resources[1].name = res->name;
1158
1159 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1160
1161 /* will be enabled in dwc3_msm_resume() */
1162 irq_set_status_flags(irq, IRQ_NOAUTOEN);
1163 ret = devm_request_threaded_irq(dev, irq, NULL, dwc3_interrupt,
1164 IRQF_SHARED | IRQF_ONESHOT, "dwc3", dwc);
1165 if (ret) {
1166 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1167 irq, ret);
1168 return -ENODEV;
1169 }
1170
1171 dwc->irq = irq;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001172 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1173 if (!res) {
1174 dev_err(dev, "missing memory resource\n");
1175 return -ENODEV;
1176 }
1177
Mayank Ranaa99689a2016-08-10 17:39:47 -07001178 dwc->reg_phys = res->start;
Vivek Gautamf32a5e22014-06-04 14:34:52 +05301179 dwc->xhci_resources[0].start = res->start;
1180 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1181 DWC3_XHCI_REGS_END;
1182 dwc->xhci_resources[0].flags = res->flags;
1183 dwc->xhci_resources[0].name = res->name;
1184
1185 res->start += DWC3_GLOBALS_REGS_START;
1186
1187 /*
1188 * Request memory region but exclude xHCI regs,
1189 * since it will be requested by the xhci-plat driver.
1190 */
1191 regs = devm_ioremap_resource(dev, res);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001192 if (IS_ERR(regs)) {
1193 ret = PTR_ERR(regs);
1194 goto err0;
1195 }
Vivek Gautamf32a5e22014-06-04 14:34:52 +05301196
1197 dwc->regs = regs;
1198 dwc->regs_size = resource_size(res);
Vivek Gautamf32a5e22014-06-04 14:34:52 +05301199
Huang Rui80caf7d2014-10-28 19:54:26 +08001200 /* default to highest possible threshold */
1201 lpm_nyet_threshold = 0xff;
1202
Huang Rui6b6a0c92014-10-31 11:11:12 +08001203 /* default to -3.5dB de-emphasis */
1204 tx_de_emphasis = 1;
1205
Huang Rui460d0982014-10-31 11:11:18 +08001206 /*
1207 * default to assert utmi_sleep_n and use maximum allowed HIRD
1208 * threshold value of 0b1100
1209 */
1210 hird_threshold = 12;
1211
Heikki Krogerus63863b92015-09-21 11:14:32 +03001212 dwc->maximum_speed = usb_get_maximum_speed(dev);
Vamsi Krishna Samavedam86ed20b2017-01-31 13:55:38 -08001213 dwc->max_hw_supp_speed = dwc->maximum_speed;
Heikki Krogerus06e71142015-09-21 11:14:34 +03001214 dwc->dr_mode = usb_get_dr_mode(dev);
Mayank Ranafb9cd932016-11-03 23:26:38 -07001215
1216 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN) {
1217 dwc->dr_mode = USB_DR_MODE_OTG;
1218 dwc->is_drd = 1;
1219 }
1220
William Wu32f2ed82016-08-16 22:44:38 +08001221 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
Heikki Krogerus63863b92015-09-21 11:14:32 +03001222
Heikki Krogerus3d128912015-09-21 11:14:35 +03001223 dwc->has_lpm_erratum = device_property_read_bool(dev,
Huang Rui80caf7d2014-10-28 19:54:26 +08001224 "snps,has-lpm-erratum");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001225 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
Huang Rui80caf7d2014-10-28 19:54:26 +08001226 &lpm_nyet_threshold);
Heikki Krogerus3d128912015-09-21 11:14:35 +03001227 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
Huang Rui460d0982014-10-31 11:11:18 +08001228 "snps,is-utmi-l1-suspend");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001229 device_property_read_u8(dev, "snps,hird-threshold",
Huang Rui460d0982014-10-31 11:11:18 +08001230 &hird_threshold);
Heikki Krogerus3d128912015-09-21 11:14:35 +03001231 dwc->usb3_lpm_capable = device_property_read_bool(dev,
Robert Baldygaeac68e82015-03-09 15:06:12 +01001232 "snps,usb3_lpm_capable");
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001233
Mayank Ranaa8e4de62016-12-13 17:11:15 -08001234 dwc->needs_fifo_resize = device_property_read_bool(dev,
1235 "tx-fifo-resize");
1236
Heikki Krogerus3d128912015-09-21 11:14:35 +03001237 dwc->disable_scramble_quirk = device_property_read_bool(dev,
Huang Rui3b812212014-10-28 19:54:25 +08001238 "snps,disable_scramble_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001239 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
Huang Rui9a5b2f32014-10-28 19:54:27 +08001240 "snps,u2exit_lfps_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001241 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
Huang Ruib5a65c42014-10-28 19:54:28 +08001242 "snps,u2ss_inp3_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001243 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
Huang Ruidf31f5b2014-10-28 19:54:29 +08001244 "snps,req_p1p2p3_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001245 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
Huang Ruia2a1d0f2014-10-28 19:54:30 +08001246 "snps,del_p1p2p3_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001247 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
Huang Rui41c06ff2014-10-28 19:54:31 +08001248 "snps,del_phy_power_chg_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001249 dwc->lfps_filter_quirk = device_property_read_bool(dev,
Huang Ruifb67afc2014-10-28 19:54:32 +08001250 "snps,lfps_filter_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001251 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
Huang Rui14f4ac52014-10-28 19:54:33 +08001252 "snps,rx_detect_poll_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001253 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
Huang Rui59acfa22014-10-31 11:11:13 +08001254 "snps,dis_u3_susphy_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001255 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
Huang Rui0effe0a2014-10-31 11:11:14 +08001256 "snps,dis_u2_susphy_quirk");
John Younec791d12015-10-02 20:30:57 -07001257 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1258 "snps,dis_enblslpm_quirk");
Rajesh Bhagate58dd352016-03-14 14:40:50 +05301259 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1260 "snps,dis_rxdet_inp3_quirk");
William Wu16199f32016-08-16 22:44:37 +08001261 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1262 "snps,dis-u2-freeclk-exists-quirk");
William Wu00fe0812016-08-16 22:44:39 +08001263 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1264 "snps,dis-del-phy-power-chg-quirk");
Huang Rui6b6a0c92014-10-31 11:11:12 +08001265
Heikki Krogerus3d128912015-09-21 11:14:35 +03001266 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
Huang Rui6b6a0c92014-10-31 11:11:12 +08001267 "snps,tx_de_emphasis_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001268 device_property_read_u8(dev, "snps,tx_de_emphasis",
Huang Rui6b6a0c92014-10-31 11:11:12 +08001269 &tx_de_emphasis);
Heikki Krogerus3d128912015-09-21 11:14:35 +03001270 device_property_read_string(dev, "snps,hsphy_interface",
1271 &dwc->hsphy_interface);
1272 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
Felipe Balbibcdb3272016-05-16 10:42:23 +03001273 &dwc->fladj);
Mayank Rana00b03982015-06-10 11:43:09 -07001274 dwc->disable_clk_gating = device_property_read_bool(dev,
1275 "snps,disable-clk-gating");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001276
Mayank Ranaa99689a2016-08-10 17:39:47 -07001277 if (dwc->enable_bus_suspend) {
1278 pm_runtime_set_autosuspend_delay(dev, 500);
1279 pm_runtime_use_autosuspend(dev);
1280 }
1281
Huang Rui80caf7d2014-10-28 19:54:26 +08001282 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
Huang Rui6b6a0c92014-10-31 11:11:12 +08001283 dwc->tx_de_emphasis = tx_de_emphasis;
Huang Rui80caf7d2014-10-28 19:54:26 +08001284
Huang Rui460d0982014-10-31 11:11:18 +08001285 dwc->hird_threshold = hird_threshold
1286 | (dwc->is_utmi_l1_suspend << 4);
1287
Mayank Ranaa99689a2016-08-10 17:39:47 -07001288 init_waitqueue_head(&dwc->wait_linkstate);
Heikki Krogerus6c89cce02015-05-13 15:26:45 +03001289 platform_set_drvdata(pdev, dwc);
1290
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001291 ret = dwc3_core_get_phy(dwc);
1292 if (ret)
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001293 goto err0;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001294
Felipe Balbi72246da2011-08-19 18:10:58 +03001295 spin_lock_init(&dwc->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03001296
Heikki Krogerus19bacdc2014-09-24 11:00:38 +03001297 if (!dev->dma_mask) {
1298 dev->dma_mask = dev->parent->dma_mask;
1299 dev->dma_parms = dev->parent->dma_parms;
1300 dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
1301 }
Kishon Vijay Abraham Iddff14f2013-03-07 18:51:43 +05301302
Mayank Ranaa99689a2016-08-10 17:39:47 -07001303 pm_runtime_no_callbacks(dev);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001304 pm_runtime_set_active(dev);
Chanho Park802ca852012-02-15 18:27:55 +09001305 pm_runtime_enable(dev);
Chanho Park802ca852012-02-15 18:27:55 +09001306 pm_runtime_forbid(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001307
John Youn77966eb2016-02-19 17:31:01 -08001308 /* Check the maximum_speed parameter */
1309 switch (dwc->maximum_speed) {
1310 case USB_SPEED_LOW:
1311 case USB_SPEED_FULL:
1312 case USB_SPEED_HIGH:
1313 case USB_SPEED_SUPER:
1314 case USB_SPEED_SUPER_PLUS:
1315 break;
1316 default:
1317 dev_err(dev, "invalid maximum_speed parameter %d\n",
1318 dwc->maximum_speed);
1319 /* fall through */
1320 case USB_SPEED_UNKNOWN:
1321 /* default to superspeed */
John Youn2c7f1bd2016-02-05 17:08:59 -08001322 dwc->maximum_speed = USB_SPEED_SUPER;
1323
1324 /*
1325 * default to superspeed plus if we are capable.
1326 */
1327 if (dwc3_is_usb31(dwc) &&
1328 (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
1329 DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1330 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
John Youn77966eb2016-02-19 17:31:01 -08001331
Vamsi Krishna Samavedam86ed20b2017-01-31 13:55:38 -08001332 dwc->max_hw_supp_speed = dwc->maximum_speed;
John Youn77966eb2016-02-19 17:31:01 -08001333 break;
John Youn2c7f1bd2016-02-05 17:08:59 -08001334 }
1335
Mayank Ranaa99689a2016-08-10 17:39:47 -07001336 /* Adjust Frame Length */
1337 dwc3_frame_length_adjustment(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001338
Mayank Ranaa99689a2016-08-10 17:39:47 -07001339 /* Hardcode number of eps */
1340 dwc->num_in_eps = 16;
1341 dwc->num_out_eps = 16;
Felipe Balbi72246da2011-08-19 18:10:58 +03001342
Felipe Balbi72246da2011-08-19 18:10:58 +03001343 ret = dwc3_core_init_mode(dwc);
1344 if (ret)
Kyle Yan65be4a52016-10-31 15:05:00 -07001345 goto err0;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001346
1347 ret = dwc3_debugfs_init(dwc);
1348 if (ret) {
1349 dev_err(dev, "failed to initialize debugfs\n");
Kyle Yan65be4a52016-10-31 15:05:00 -07001350 goto err_core_init;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001351 }
1352
Mayank Rana861da2b2016-07-13 13:47:57 -07001353 dwc->dwc_ipc_log_ctxt = ipc_log_context_create(NUM_LOG_PAGES,
1354 dev_name(dwc->dev), 0);
1355 if (!dwc->dwc_ipc_log_ctxt)
1356 dev_err(dwc->dev, "Error getting ipc_log_ctxt\n");
1357
1358 dwc3_instance[count] = dwc;
1359 dwc->index = count;
1360 count++;
1361
Mayank Ranaa99689a2016-08-10 17:39:47 -07001362 pm_runtime_allow(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001363 return 0;
1364
Kyle Yan65be4a52016-10-31 15:05:00 -07001365err_core_init:
1366 dwc3_core_exit_mode(dwc);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001367err0:
1368 /*
1369 * restore res->start back to its original value so that, in case the
1370 * probe is deferred, we don't end up getting error in request the
1371 * memory region the next time probe is called.
1372 */
1373 res->start -= DWC3_GLOBALS_REGS_START;
1374
Felipe Balbi72246da2011-08-19 18:10:58 +03001375 return ret;
1376}
1377
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05001378static int dwc3_remove(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +03001379{
Felipe Balbi72246da2011-08-19 18:10:58 +03001380 struct dwc3 *dwc = platform_get_drvdata(pdev);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001381 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1382
Felipe Balbifc8bb912016-05-16 13:14:48 +03001383 pm_runtime_get_sync(&pdev->dev);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001384 /*
1385 * restore res->start back to its original value so that, in case the
1386 * probe is deferred, we don't end up getting error in request the
1387 * memory region the next time probe is called.
1388 */
1389 res->start -= DWC3_GLOBALS_REGS_START;
Felipe Balbi72246da2011-08-19 18:10:58 +03001390
Felipe Balbidc99f162014-09-03 16:13:37 -05001391 dwc3_debugfs_exit(dwc);
1392 dwc3_core_exit_mode(dwc);
Kishon Vijay Abraham I8ba007a2013-01-25 08:30:54 +05301393
Felipe Balbi72246da2011-08-19 18:10:58 +03001394 dwc3_core_exit(dwc);
Heikki Krogerus88bc9d12015-05-13 15:26:51 +03001395 dwc3_ulpi_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001396
Felipe Balbifc8bb912016-05-16 13:14:48 +03001397 pm_runtime_put_sync(&pdev->dev);
1398 pm_runtime_allow(&pdev->dev);
1399 pm_runtime_disable(&pdev->dev);
1400
Felipe Balbic499ff72016-05-16 10:49:01 +03001401 dwc3_free_event_buffers(dwc);
1402 dwc3_free_scratch_buffers(dwc);
1403
Mayank Rana861da2b2016-07-13 13:47:57 -07001404 ipc_log_context_destroy(dwc->dwc_ipc_log_ctxt);
1405 dwc->dwc_ipc_log_ctxt = NULL;
1406 count--;
1407 dwc3_instance[dwc->index] = NULL;
1408
Felipe Balbi72246da2011-08-19 18:10:58 +03001409 return 0;
1410}
1411
Felipe Balbifc8bb912016-05-16 13:14:48 +03001412#ifdef CONFIG_PM
1413static int dwc3_suspend_common(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03001414{
Felipe Balbifc8bb912016-05-16 13:14:48 +03001415 unsigned long flags;
Felipe Balbi7415f172012-04-30 14:56:33 +03001416
Ruchika Kharwara45c82b82013-07-06 07:52:49 -05001417 switch (dwc->dr_mode) {
1418 case USB_DR_MODE_PERIPHERAL:
1419 case USB_DR_MODE_OTG:
Felipe Balbifc8bb912016-05-16 13:14:48 +03001420 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7415f172012-04-30 14:56:33 +03001421 dwc3_gadget_suspend(dwc);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001422 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi51f5d492016-05-16 10:52:58 +03001423 break;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -05001424 case USB_DR_MODE_HOST:
Felipe Balbi7415f172012-04-30 14:56:33 +03001425 default:
Felipe Balbi51f5d492016-05-16 10:52:58 +03001426 /* do nothing */
Felipe Balbi7415f172012-04-30 14:56:33 +03001427 break;
1428 }
1429
Felipe Balbi51f5d492016-05-16 10:52:58 +03001430 dwc3_core_exit(dwc);
Felipe Balbi5c4ad3182016-04-11 17:12:34 +03001431
Felipe Balbifc8bb912016-05-16 13:14:48 +03001432 return 0;
1433}
1434
1435static int dwc3_resume_common(struct dwc3 *dwc)
1436{
1437 unsigned long flags;
1438 int ret;
1439
1440 ret = dwc3_core_init(dwc);
1441 if (ret)
1442 return ret;
1443
1444 switch (dwc->dr_mode) {
1445 case USB_DR_MODE_PERIPHERAL:
1446 case USB_DR_MODE_OTG:
1447 spin_lock_irqsave(&dwc->lock, flags);
1448 dwc3_gadget_resume(dwc);
1449 spin_unlock_irqrestore(&dwc->lock, flags);
1450 /* FALLTHROUGH */
1451 case USB_DR_MODE_HOST:
1452 default:
1453 /* do nothing */
1454 break;
1455 }
1456
1457 return 0;
1458}
1459
1460static int dwc3_runtime_checks(struct dwc3 *dwc)
1461{
1462 switch (dwc->dr_mode) {
1463 case USB_DR_MODE_PERIPHERAL:
1464 case USB_DR_MODE_OTG:
1465 if (dwc->connected)
1466 return -EBUSY;
1467 break;
1468 case USB_DR_MODE_HOST:
1469 default:
1470 /* do nothing */
1471 break;
1472 }
1473
1474 return 0;
1475}
1476
1477static int dwc3_runtime_suspend(struct device *dev)
1478{
1479 struct dwc3 *dwc = dev_get_drvdata(dev);
1480 int ret;
1481
Mayank Ranaa99689a2016-08-10 17:39:47 -07001482 /* Check if platform glue driver handling PM, if not then handle here */
1483 if (!dwc3_notify_event(dwc, DWC3_CORE_PM_RESUME_EVENT))
1484 return 0;
Felipe Balbifc8bb912016-05-16 13:14:48 +03001485
1486 ret = dwc3_suspend_common(dwc);
1487 if (ret)
1488 return ret;
1489
1490 device_init_wakeup(dev, true);
1491
1492 return 0;
1493}
1494
1495static int dwc3_runtime_resume(struct device *dev)
1496{
1497 struct dwc3 *dwc = dev_get_drvdata(dev);
1498 int ret;
1499
Mayank Ranaa99689a2016-08-10 17:39:47 -07001500 /* Check if platform glue driver handling PM, if not then handle here */
1501 if (!dwc3_notify_event(dwc, DWC3_CORE_PM_RESUME_EVENT))
1502 return 0;
1503
Felipe Balbifc8bb912016-05-16 13:14:48 +03001504 device_init_wakeup(dev, false);
1505
1506 ret = dwc3_resume_common(dwc);
1507 if (ret)
1508 return ret;
1509
1510 switch (dwc->dr_mode) {
1511 case USB_DR_MODE_PERIPHERAL:
1512 case USB_DR_MODE_OTG:
1513 dwc3_gadget_process_pending_events(dwc);
1514 break;
1515 case USB_DR_MODE_HOST:
1516 default:
1517 /* do nothing */
1518 break;
1519 }
1520
1521 pm_runtime_mark_last_busy(dev);
Felipe Balbib74c2d82016-07-28 13:07:07 +03001522 pm_runtime_put(dev);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001523
1524 return 0;
1525}
1526
1527static int dwc3_runtime_idle(struct device *dev)
1528{
1529 struct dwc3 *dwc = dev_get_drvdata(dev);
1530
1531 switch (dwc->dr_mode) {
1532 case USB_DR_MODE_PERIPHERAL:
1533 case USB_DR_MODE_OTG:
1534 if (dwc3_runtime_checks(dwc))
1535 return -EBUSY;
1536 break;
1537 case USB_DR_MODE_HOST:
1538 default:
1539 /* do nothing */
1540 break;
1541 }
1542
1543 pm_runtime_mark_last_busy(dev);
1544 pm_runtime_autosuspend(dev);
1545
1546 return 0;
1547}
1548#endif /* CONFIG_PM */
1549
1550#ifdef CONFIG_PM_SLEEP
1551static int dwc3_suspend(struct device *dev)
1552{
1553 struct dwc3 *dwc = dev_get_drvdata(dev);
1554 int ret;
1555
1556 ret = dwc3_suspend_common(dwc);
1557 if (ret)
1558 return ret;
1559
Sekhar Nori63444752015-08-31 21:09:08 +05301560 pinctrl_pm_select_sleep_state(dev);
1561
Felipe Balbi7415f172012-04-30 14:56:33 +03001562 return 0;
1563}
1564
1565static int dwc3_resume(struct device *dev)
1566{
1567 struct dwc3 *dwc = dev_get_drvdata(dev);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +05301568 int ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03001569
Sekhar Nori63444752015-08-31 21:09:08 +05301570 pinctrl_pm_select_default_state(dev);
1571
Felipe Balbifc8bb912016-05-16 13:14:48 +03001572 ret = dwc3_resume_common(dwc);
Felipe Balbi51f5d492016-05-16 10:52:58 +03001573 if (ret)
Felipe Balbi5c4ad3182016-04-11 17:12:34 +03001574 return ret;
1575
Felipe Balbi7415f172012-04-30 14:56:33 +03001576 pm_runtime_disable(dev);
1577 pm_runtime_set_active(dev);
1578 pm_runtime_enable(dev);
1579
1580 return 0;
1581}
Felipe Balbi7f370ed2016-05-09 15:27:01 +03001582#endif /* CONFIG_PM_SLEEP */
Felipe Balbi7415f172012-04-30 14:56:33 +03001583
1584static const struct dev_pm_ops dwc3_dev_pm_ops = {
Felipe Balbi7415f172012-04-30 14:56:33 +03001585 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
Felipe Balbifc8bb912016-05-16 13:14:48 +03001586 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
1587 dwc3_runtime_idle)
Felipe Balbi7415f172012-04-30 14:56:33 +03001588};
1589
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301590#ifdef CONFIG_OF
1591static const struct of_device_id of_dwc3_match[] = {
1592 {
Felipe Balbi22a5aa12013-07-02 21:20:24 +03001593 .compatible = "snps,dwc3"
1594 },
1595 {
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301596 .compatible = "synopsys,dwc3"
1597 },
1598 { },
1599};
1600MODULE_DEVICE_TABLE(of, of_dwc3_match);
1601#endif
1602
Heikki Krogerus404905a2014-09-25 10:57:02 +03001603#ifdef CONFIG_ACPI
1604
1605#define ACPI_ID_INTEL_BSW "808622B7"
1606
1607static const struct acpi_device_id dwc3_acpi_match[] = {
1608 { ACPI_ID_INTEL_BSW, 0 },
1609 { },
1610};
1611MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
1612#endif
1613
Felipe Balbi72246da2011-08-19 18:10:58 +03001614static struct platform_driver dwc3_driver = {
1615 .probe = dwc3_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05001616 .remove = dwc3_remove,
Felipe Balbi72246da2011-08-19 18:10:58 +03001617 .driver = {
1618 .name = "dwc3",
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301619 .of_match_table = of_match_ptr(of_dwc3_match),
Heikki Krogerus404905a2014-09-25 10:57:02 +03001620 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
Felipe Balbi7f370ed2016-05-09 15:27:01 +03001621 .pm = &dwc3_dev_pm_ops,
Felipe Balbi72246da2011-08-19 18:10:58 +03001622 },
Felipe Balbi72246da2011-08-19 18:10:58 +03001623};
1624
Tobias Klauserb1116dc2012-02-28 12:57:20 +01001625module_platform_driver(dwc3_driver);
1626
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +02001627MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +03001628MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
Felipe Balbi5945f782013-06-30 14:15:11 +03001629MODULE_LICENSE("GPL v2");
Felipe Balbi72246da2011-08-19 18:10:58 +03001630MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");