blob: 64ed83426002302dbc6bbe812dc257aa55981b4f [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;
Hemant Kumar58eb1df2017-01-27 14:51:07 -0800252
253 /* core exits U1/U2/U3 only in PHY power state P1/P2/P3 respectively */
254 if (dwc->revision <= DWC3_REVISION_310A)
255 reg |= DWC3_GUSB3PIPECTL_UX_EXIT_IN_PX;
256
Mayank Ranaf8ebb7f2016-09-08 11:09:37 -0700257 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
258
Mayank Ranaa99689a2016-08-10 17:39:47 -0700259 dwc3_notify_event(dwc, DWC3_CONTROLLER_RESET_EVENT);
260
261 dwc3_notify_event(dwc, DWC3_CONTROLLER_POST_RESET_EVENT);
262
263 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300264}
265
266/**
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300267 * dwc3_soft_reset - Issue soft reset
268 * @dwc: Pointer to our controller context structure
269 */
270static int dwc3_soft_reset(struct dwc3 *dwc)
271{
272 unsigned long timeout;
273 u32 reg;
274
275 timeout = jiffies + msecs_to_jiffies(500);
276 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
277 do {
278 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
279 if (!(reg & DWC3_DCTL_CSFTRST))
280 break;
281
282 if (time_after(jiffies, timeout)) {
283 dev_err(dwc->dev, "Reset Timed Out\n");
284 return -ETIMEDOUT;
285 }
286
287 cpu_relax();
288 } while (true);
289
290 return 0;
291}
292
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530293/*
294 * dwc3_frame_length_adjustment - Adjusts frame length if required
295 * @dwc3: Pointer to our controller context structure
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530296 */
Felipe Balbibcdb3272016-05-16 10:42:23 +0300297static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530298{
299 u32 reg;
300 u32 dft;
301
302 if (dwc->revision < DWC3_REVISION_250A)
303 return;
304
Felipe Balbibcdb3272016-05-16 10:42:23 +0300305 if (dwc->fladj == 0)
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530306 return;
307
308 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
309 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
Felipe Balbibcdb3272016-05-16 10:42:23 +0300310 if (!dev_WARN_ONCE(dwc->dev, dft == dwc->fladj,
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530311 "request value same as default, ignoring\n")) {
312 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
Felipe Balbibcdb3272016-05-16 10:42:23 +0300313 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530314 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
315 }
316}
317
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300318/**
Felipe Balbi72246da2011-08-19 18:10:58 +0300319 * dwc3_free_one_event_buffer - Frees one event buffer
320 * @dwc: Pointer to our controller context structure
321 * @evt: Pointer to event buffer to be freed
322 */
323static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
324 struct dwc3_event_buffer *evt)
325{
Arnd Bergmann42695fc2016-11-17 17:13:47 +0530326 dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300327}
328
329/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800330 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300331 * @dwc: Pointer to our controller context structure
332 * @length: size of the event buffer
333 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800334 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300335 * otherwise ERR_PTR(errno).
336 */
Felipe Balbi67d0b502013-02-22 16:31:07 +0200337static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
338 unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300339{
340 struct dwc3_event_buffer *evt;
341
Felipe Balbi380f0d22012-10-11 13:48:36 +0300342 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300343 if (!evt)
344 return ERR_PTR(-ENOMEM);
345
346 evt->dwc = dwc;
347 evt->length = length;
Arnd Bergmann42695fc2016-11-17 17:13:47 +0530348 evt->buf = dma_alloc_coherent(dwc->sysdev, length,
Felipe Balbi72246da2011-08-19 18:10:58 +0300349 &evt->dma, GFP_KERNEL);
Felipe Balbie32672f2012-11-08 15:26:41 +0200350 if (!evt->buf)
Felipe Balbi72246da2011-08-19 18:10:58 +0300351 return ERR_PTR(-ENOMEM);
Felipe Balbi72246da2011-08-19 18:10:58 +0300352
353 return evt;
354}
355
356/**
357 * dwc3_free_event_buffers - frees all allocated event buffers
358 * @dwc: Pointer to our controller context structure
359 */
360static void dwc3_free_event_buffers(struct dwc3 *dwc)
361{
362 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300363
Felipe Balbi696c8b12016-03-30 09:37:03 +0300364 evt = dwc->ev_buf;
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300365 if (evt)
366 dwc3_free_one_event_buffer(dwc, evt);
Mayank Ranaf4918d32016-12-15 13:35:55 -0800367
368 /* free GSI related event buffers */
369 dwc3_notify_event(dwc, DWC3_GSI_EVT_BUF_FREE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300370}
371
372/**
373 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800374 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300375 * @length: size of event buffer
376 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800377 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300378 * may contain some buffers allocated but not all which were requested.
379 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500380static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300381{
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300382 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300383
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300384 evt = dwc3_alloc_one_event_buffer(dwc, length);
385 if (IS_ERR(evt)) {
386 dev_err(dwc->dev, "can't allocate event buffer\n");
387 return PTR_ERR(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300388 }
Felipe Balbi696c8b12016-03-30 09:37:03 +0300389 dwc->ev_buf = evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300390
Mayank Ranaf4918d32016-12-15 13:35:55 -0800391 /* alloc GSI related event buffers */
392 dwc3_notify_event(dwc, DWC3_GSI_EVT_BUF_ALLOC);
Felipe Balbi72246da2011-08-19 18:10:58 +0300393 return 0;
394}
395
396/**
397 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800398 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300399 *
400 * Returns 0 on success otherwise negative errno.
401 */
Mayank Ranaa99689a2016-08-10 17:39:47 -0700402int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300403{
404 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300405
Felipe Balbi696c8b12016-03-30 09:37:03 +0300406 evt = dwc->ev_buf;
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300407 dwc3_trace(trace_dwc3_core,
408 "Event buf %p dma %08llx length %d\n",
409 evt->buf, (unsigned long long) evt->dma,
410 evt->length);
Felipe Balbi72246da2011-08-19 18:10:58 +0300411
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300412 evt->lpos = 0;
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300413
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300414 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
415 lower_32_bits(evt->dma));
416 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
417 upper_32_bits(evt->dma));
418 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
419 DWC3_GEVNTSIZ_SIZE(evt->length));
420 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300421
Mayank Ranaf4918d32016-12-15 13:35:55 -0800422 /* setup GSI related event buffers */
423 dwc3_notify_event(dwc, DWC3_GSI_EVT_BUF_SETUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300424 return 0;
425}
426
427static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
428{
429 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300430
Mayank Rana0eb0db72017-10-03 13:46:32 -0700431 if (!dwc->ev_buf)
432 return;
433
Felipe Balbi696c8b12016-03-30 09:37:03 +0300434 evt = dwc->ev_buf;
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300435
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300436 evt->lpos = 0;
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300437
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300438 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
439 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
440 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
441 | DWC3_GEVNTSIZ_SIZE(0));
442 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
Mayank Ranaf4918d32016-12-15 13:35:55 -0800443
444 /* cleanup GSI related event buffers */
445 dwc3_notify_event(dwc, DWC3_GSI_EVT_BUF_CLEANUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300446}
447
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600448static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
449{
450 if (!dwc->has_hibernation)
451 return 0;
452
453 if (!dwc->nr_scratch)
454 return 0;
455
456 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
457 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
458 if (!dwc->scratchbuf)
459 return -ENOMEM;
460
461 return 0;
462}
463
464static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
465{
466 dma_addr_t scratch_addr;
467 u32 param;
468 int ret;
469
470 if (!dwc->has_hibernation)
471 return 0;
472
473 if (!dwc->nr_scratch)
474 return 0;
475
476 /* should never fall here */
477 if (!WARN_ON(dwc->scratchbuf))
478 return 0;
479
Arnd Bergmann42695fc2016-11-17 17:13:47 +0530480 scratch_addr = dma_map_single(dwc->sysdev, dwc->scratchbuf,
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600481 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
482 DMA_BIDIRECTIONAL);
Arnd Bergmann42695fc2016-11-17 17:13:47 +0530483 if (dma_mapping_error(dwc->sysdev, scratch_addr)) {
484 dev_err(dwc->sysdev, "failed to map scratch buffer\n");
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600485 ret = -EFAULT;
486 goto err0;
487 }
488
489 dwc->scratch_addr = scratch_addr;
490
491 param = lower_32_bits(scratch_addr);
492
493 ret = dwc3_send_gadget_generic_command(dwc,
494 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
495 if (ret < 0)
496 goto err1;
497
498 param = upper_32_bits(scratch_addr);
499
500 ret = dwc3_send_gadget_generic_command(dwc,
501 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
502 if (ret < 0)
503 goto err1;
504
505 return 0;
506
507err1:
Arnd Bergmann42695fc2016-11-17 17:13:47 +0530508 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600509 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
510
511err0:
512 return ret;
513}
514
515static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
516{
517 if (!dwc->has_hibernation)
518 return;
519
520 if (!dwc->nr_scratch)
521 return;
522
523 /* should never fall here */
524 if (!WARN_ON(dwc->scratchbuf))
525 return;
526
Arnd Bergmann42695fc2016-11-17 17:13:47 +0530527 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600528 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
529 kfree(dwc->scratchbuf);
530}
531
Felipe Balbi789451f62011-05-05 15:53:10 +0300532static void dwc3_core_num_eps(struct dwc3 *dwc)
533{
534 struct dwc3_hwparams *parms = &dwc->hwparams;
535
536 dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
537 dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
538
Felipe Balbi73815282015-01-27 13:48:14 -0600539 dwc3_trace(trace_dwc3_core, "found %d IN and %d OUT endpoints",
Felipe Balbi789451f62011-05-05 15:53:10 +0300540 dwc->num_in_eps, dwc->num_out_eps);
541}
542
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500543static void dwc3_cache_hwparams(struct dwc3 *dwc)
Felipe Balbi26ceca92011-09-30 10:58:49 +0300544{
545 struct dwc3_hwparams *parms = &dwc->hwparams;
546
547 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
548 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
549 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
550 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
551 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
552 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
553 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
554 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
555 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
556}
557
Felipe Balbi72246da2011-08-19 18:10:58 +0300558/**
Huang Ruib5a65c42014-10-28 19:54:28 +0800559 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
560 * @dwc: Pointer to our controller context structure
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300561 *
562 * Returns 0 on success. The USB PHY interfaces are configured but not
563 * initialized. The PHY interfaces and the PHYs get initialized together with
564 * the core in dwc3_core_init.
Huang Ruib5a65c42014-10-28 19:54:28 +0800565 */
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300566static int dwc3_phy_setup(struct dwc3 *dwc)
Huang Ruib5a65c42014-10-28 19:54:28 +0800567{
568 u32 reg;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300569 int ret;
Huang Ruib5a65c42014-10-28 19:54:28 +0800570
571 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
572
Huang Rui2164a472014-10-28 19:54:35 +0800573 /*
574 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
575 * to '0' during coreConsultant configuration. So default value
576 * will be '0' when the core is reset. Application needs to set it
577 * to '1' after the core initialization is completed.
578 */
579 if (dwc->revision > DWC3_REVISION_194A)
580 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
581
Huang Ruib5a65c42014-10-28 19:54:28 +0800582 if (dwc->u2ss_inp3_quirk)
583 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
584
Rajesh Bhagate58dd352016-03-14 14:40:50 +0530585 if (dwc->dis_rxdet_inp3_quirk)
586 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
587
Huang Ruidf31f5b2014-10-28 19:54:29 +0800588 if (dwc->req_p1p2p3_quirk)
589 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
590
Huang Ruia2a1d0f2014-10-28 19:54:30 +0800591 if (dwc->del_p1p2p3_quirk)
592 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
593
Huang Rui41c06ff2014-10-28 19:54:31 +0800594 if (dwc->del_phy_power_chg_quirk)
595 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
596
Huang Ruifb67afc2014-10-28 19:54:32 +0800597 if (dwc->lfps_filter_quirk)
598 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
599
Huang Rui14f4ac52014-10-28 19:54:33 +0800600 if (dwc->rx_detect_poll_quirk)
601 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
602
Huang Rui6b6a0c92014-10-31 11:11:12 +0800603 if (dwc->tx_de_emphasis_quirk)
604 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
605
Felipe Balbicd72f892014-11-06 11:31:00 -0600606 if (dwc->dis_u3_susphy_quirk)
Huang Rui59acfa22014-10-31 11:11:13 +0800607 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
608
William Wu00fe0812016-08-16 22:44:39 +0800609 if (dwc->dis_del_phy_power_chg_quirk)
610 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
611
Huang Ruib5a65c42014-10-28 19:54:28 +0800612 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
613
Huang Rui2164a472014-10-28 19:54:35 +0800614 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
615
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300616 /* Select the HS PHY interface */
617 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
618 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
Felipe Balbi43cacb02015-07-01 22:03:09 -0500619 if (dwc->hsphy_interface &&
620 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300621 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300622 break;
Felipe Balbi43cacb02015-07-01 22:03:09 -0500623 } else if (dwc->hsphy_interface &&
624 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300625 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300626 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300627 } else {
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300628 /* Relying on default value. */
629 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
630 break;
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300631 }
632 /* FALLTHROUGH */
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300633 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
634 /* Making sure the interface and PHY are operational */
635 ret = dwc3_soft_reset(dwc);
636 if (ret)
637 return ret;
638
639 udelay(1);
640
641 ret = dwc3_ulpi_init(dwc);
642 if (ret)
643 return ret;
644 /* FALLTHROUGH */
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300645 default:
646 break;
647 }
648
William Wu32f2ed82016-08-16 22:44:38 +0800649 switch (dwc->hsphy_mode) {
650 case USBPHY_INTERFACE_MODE_UTMI:
651 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
652 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
653 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
654 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
655 break;
656 case USBPHY_INTERFACE_MODE_UTMIW:
657 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
658 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
659 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
660 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
661 break;
662 default:
663 break;
664 }
665
Huang Rui2164a472014-10-28 19:54:35 +0800666 /*
667 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
668 * '0' during coreConsultant configuration. So default value will
669 * be '0' when the core is reset. Application needs to set it to
670 * '1' after the core initialization is completed.
671 */
672 if (dwc->revision > DWC3_REVISION_194A)
673 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
674
Felipe Balbicd72f892014-11-06 11:31:00 -0600675 if (dwc->dis_u2_susphy_quirk)
Huang Rui0effe0a2014-10-31 11:11:14 +0800676 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
677
John Younec791d12015-10-02 20:30:57 -0700678 if (dwc->dis_enblslpm_quirk)
679 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
680
William Wu16199f32016-08-16 22:44:37 +0800681 if (dwc->dis_u2_freeclk_exists_quirk)
682 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
683
Huang Rui2164a472014-10-28 19:54:35 +0800684 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300685
686 return 0;
Huang Ruib5a65c42014-10-28 19:54:28 +0800687}
688
Felipe Balbic499ff72016-05-16 10:49:01 +0300689static void dwc3_core_exit(struct dwc3 *dwc)
690{
691 dwc3_event_buffers_cleanup(dwc);
692
Felipe Balbic499ff72016-05-16 10:49:01 +0300693 phy_exit(dwc->usb2_generic_phy);
694 phy_exit(dwc->usb3_generic_phy);
695
Felipe Balbic499ff72016-05-16 10:49:01 +0300696 phy_power_off(dwc->usb2_generic_phy);
697 phy_power_off(dwc->usb3_generic_phy);
698}
699
Huang Ruib5a65c42014-10-28 19:54:28 +0800700/**
Felipe Balbi72246da2011-08-19 18:10:58 +0300701 * dwc3_core_init - Low-level initialization of DWC3 Core
702 * @dwc: Pointer to our controller context structure
703 *
704 * Returns 0 on success otherwise negative errno.
705 */
Mayank Ranaa99689a2016-08-10 17:39:47 -0700706int dwc3_core_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300707{
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600708 u32 hwparams4 = dwc->hwparams.hwparams4;
Felipe Balbi72246da2011-08-19 18:10:58 +0300709 u32 reg;
710 int ret;
711
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200712 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
713 /* This should read as U3 followed by revision number */
John Youn690fb372015-09-04 19:15:10 -0700714 if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
715 /* Detected DWC_usb3 IP */
716 dwc->revision = reg;
717 } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
718 /* Detected DWC_usb31 IP */
719 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
720 dwc->revision |= DWC3_REVISION_IS_DWC31;
721 } else {
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200722 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
723 ret = -ENODEV;
724 goto err0;
725 }
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200726
Felipe Balbifa0ea132014-09-19 15:51:11 -0500727 /*
728 * Write Linux Version Code to our GUID register so it's easy to figure
729 * out which kernel version a bug was found.
730 */
731 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
732
Paul Zimmerman0e1e5c42014-05-23 11:39:24 -0700733 /* Handle USB2.0-only core configuration */
734 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
735 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
Vamsi Krishna Samavedam86ed20b2017-01-31 13:55:38 -0800736 if (dwc->max_hw_supp_speed == USB_SPEED_SUPER) {
737 dwc->max_hw_supp_speed = USB_SPEED_HIGH;
738 dwc->maximum_speed = dwc->max_hw_supp_speed;
739 }
Paul Zimmerman0e1e5c42014-05-23 11:39:24 -0700740 }
741
John Youn7ecdb052016-11-14 12:32:45 -0800742 /*
743 * Workaround for STAR 9000961433 which affects only version
744 * 3.00a of the DWC_usb3 core. This prevents the controller
745 * interrupt from being masked while handling events. IMOD
746 * allows us to work around this issue. Enable it for the
747 * affected version.
748 */
749 if (!dwc->imod_interval && (dwc->revision == DWC3_REVISION_300A))
750 dwc->imod_interval = 1;
751
Felipe Balbi72246da2011-08-19 18:10:58 +0300752 /* issue device SoftReset too */
Mayank Ranaa99689a2016-08-10 17:39:47 -0700753 ret = dwc3_core_reset(dwc);
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300754 if (ret)
755 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300756
Mayank Ranaa99689a2016-08-10 17:39:47 -0700757 /* issue device SoftReset too */
758 ret = dwc3_soft_reset(dwc);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530759 if (ret)
760 goto err0;
Pratyush Anand58a0f232012-06-21 17:44:29 +0530761
Felipe Balbic499ff72016-05-16 10:49:01 +0300762 ret = dwc3_phy_setup(dwc);
763 if (ret)
764 goto err0;
765
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100766 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800767 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100768
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100769 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100770 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
Felipe Balbi32a4a132014-02-25 14:00:13 -0600771 /**
772 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
773 * issue which would cause xHCI compliance tests to fail.
774 *
775 * Because of that we cannot enable clock gating on such
776 * configurations.
777 *
778 * Refers to:
779 *
780 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
781 * SOF/ITP Mode Used
782 */
783 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
784 dwc->dr_mode == USB_DR_MODE_OTG) &&
785 (dwc->revision >= DWC3_REVISION_210A &&
786 dwc->revision <= DWC3_REVISION_250A))
787 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
788 else
789 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100790 break;
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600791 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
792 /* enable hibernation here */
793 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
Huang Rui2eac3992014-10-28 19:54:22 +0800794
795 /*
796 * REVISIT Enabling this bit so that host-mode hibernation
797 * will work. Device-mode hibernation is not yet implemented.
798 */
799 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600800 break;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100801 default:
Felipe Balbi1407bf12015-11-16 16:06:37 -0600802 dwc3_trace(trace_dwc3_core, "No power optimization available\n");
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100803 }
804
Huang Rui946bd572014-10-28 19:54:23 +0800805 /* check if current dwc3 is on simulation board */
806 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
Felipe Balbi1407bf12015-11-16 16:06:37 -0600807 dwc3_trace(trace_dwc3_core,
808 "running on FPGA platform\n");
Huang Rui946bd572014-10-28 19:54:23 +0800809 dwc->is_fpga = true;
810 }
811
Huang Rui3b812212014-10-28 19:54:25 +0800812 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
813 "disable_scramble cannot be used on non-FPGA builds\n");
814
815 if (dwc->disable_scramble_quirk && dwc->is_fpga)
816 reg |= DWC3_GCTL_DISSCRAMBLE;
817 else
818 reg &= ~DWC3_GCTL_DISSCRAMBLE;
819
Huang Rui9a5b2f32014-10-28 19:54:27 +0800820 if (dwc->u2exit_lfps_quirk)
821 reg |= DWC3_GCTL_U2EXIT_LFPS;
822
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100823 /*
824 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800825 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100826 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800827 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100828 */
829 if (dwc->revision < DWC3_REVISION_190A)
830 reg |= DWC3_GCTL_U2RSTECN;
831
Mayank Ranafb9cd932016-11-03 23:26:38 -0700832 ret = dwc3_get_dr_mode(dwc);
833 if (ret)
834 goto err0;
835
Mayank Ranaa99689a2016-08-10 17:39:47 -0700836 dwc3_core_num_eps(dwc);
837
838 /*
839 * Disable clock gating to work around a known HW bug that causes the
840 * internal RAM clock to get stuck when entering low power modes.
841 */
842 if (dwc->disable_clk_gating) {
843 dev_dbg(dwc->dev, "Disabling controller clock gating.\n");
844 reg |= DWC3_GCTL_DSBLCLKGTNG;
845 }
846
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100847 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
848
Mayank Ranaa99689a2016-08-10 17:39:47 -0700849 ret = dwc3_alloc_scratch_buffers(dwc);
850 if (ret)
851 goto err1;
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600852
853 ret = dwc3_setup_scratch_buffers(dwc);
854 if (ret)
Mayank Ranaa99689a2016-08-10 17:39:47 -0700855 goto err2;
Felipe Balbic499ff72016-05-16 10:49:01 +0300856
857 /* Adjust Frame Length */
858 dwc3_frame_length_adjustment(dwc);
859
860 usb_phy_set_suspend(dwc->usb2_phy, 0);
861 usb_phy_set_suspend(dwc->usb3_phy, 0);
862 ret = phy_power_on(dwc->usb2_generic_phy);
863 if (ret < 0)
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600864 goto err2;
865
Baolin Wang00af6232016-07-15 17:13:27 +0800866 switch (dwc->dr_mode) {
867 case USB_DR_MODE_PERIPHERAL:
868 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
869 break;
870 case USB_DR_MODE_HOST:
871 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
872 break;
873 case USB_DR_MODE_OTG:
874 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
875 break;
876 default:
877 dev_warn(dwc->dev, "Unsupported mode %d\n", dwc->dr_mode);
878 break;
879 }
880
John Youn06281d42016-08-22 15:39:13 -0700881 /*
882 * ENDXFER polling is available on version 3.10a and later of
883 * the DWC_usb3 controller. It is NOT available in the
884 * DWC_usb31 controller.
885 */
886 if (!dwc3_is_usb31(dwc) && dwc->revision >= DWC3_REVISION_310A) {
887 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
888 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
889 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
890 }
891
Felipe Balbi72246da2011-08-19 18:10:58 +0300892 return 0;
893
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600894err2:
Mayank Ranaa99689a2016-08-10 17:39:47 -0700895 dwc3_free_scratch_buffers(dwc);
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600896err1:
897 usb_phy_shutdown(dwc->usb2_phy);
898 usb_phy_shutdown(dwc->usb3_phy);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530899 phy_exit(dwc->usb2_generic_phy);
900 phy_exit(dwc->usb3_generic_phy);
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600901
Felipe Balbi72246da2011-08-19 18:10:58 +0300902err0:
903 return ret;
904}
905
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500906static int dwc3_core_get_phy(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300907{
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500908 struct device *dev = dwc->dev;
Felipe Balbi941ea362013-07-31 09:21:25 +0300909 struct device_node *node = dev->of_node;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500910 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300911
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530912 if (node) {
913 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
914 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
Felipe Balbibb674902013-08-14 13:21:23 -0500915 } else {
916 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
917 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530918 }
919
Felipe Balbid105e7f2013-03-15 10:52:08 +0200920 if (IS_ERR(dwc->usb2_phy)) {
921 ret = PTR_ERR(dwc->usb2_phy);
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530922 if (ret == -ENXIO || ret == -ENODEV) {
923 dwc->usb2_phy = NULL;
924 } else if (ret == -EPROBE_DEFER) {
Felipe Balbid105e7f2013-03-15 10:52:08 +0200925 return ret;
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530926 } else {
927 dev_err(dev, "no usb2 phy configured\n");
928 return ret;
929 }
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300930 }
931
Felipe Balbid105e7f2013-03-15 10:52:08 +0200932 if (IS_ERR(dwc->usb3_phy)) {
Ruchika Kharwar315955d72013-07-04 00:59:34 -0500933 ret = PTR_ERR(dwc->usb3_phy);
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530934 if (ret == -ENXIO || ret == -ENODEV) {
935 dwc->usb3_phy = NULL;
936 } else if (ret == -EPROBE_DEFER) {
Felipe Balbid105e7f2013-03-15 10:52:08 +0200937 return ret;
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530938 } else {
939 dev_err(dev, "no usb3 phy configured\n");
940 return ret;
941 }
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300942 }
943
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530944 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
945 if (IS_ERR(dwc->usb2_generic_phy)) {
946 ret = PTR_ERR(dwc->usb2_generic_phy);
947 if (ret == -ENOSYS || ret == -ENODEV) {
948 dwc->usb2_generic_phy = NULL;
949 } else if (ret == -EPROBE_DEFER) {
950 return ret;
951 } else {
952 dev_err(dev, "no usb2 phy configured\n");
953 return ret;
954 }
955 }
956
957 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
958 if (IS_ERR(dwc->usb3_generic_phy)) {
959 ret = PTR_ERR(dwc->usb3_generic_phy);
960 if (ret == -ENOSYS || ret == -ENODEV) {
961 dwc->usb3_generic_phy = NULL;
962 } else if (ret == -EPROBE_DEFER) {
963 return ret;
964 } else {
965 dev_err(dev, "no usb3 phy configured\n");
966 return ret;
967 }
968 }
969
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500970 return 0;
971}
972
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500973static int dwc3_core_init_mode(struct dwc3 *dwc)
974{
975 struct device *dev = dwc->dev;
976 int ret;
977
Mayank Ranaa75caa52017-10-10 11:45:13 -0700978 if (dwc->dr_mode == USB_DR_MODE_OTG ||
979 dwc->dr_mode == USB_DR_MODE_PERIPHERAL) {
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500980 ret = dwc3_gadget_init(dwc);
981 if (ret) {
Roger Quadros9522def2016-06-10 14:48:38 +0300982 if (ret != -EPROBE_DEFER)
983 dev_err(dev, "failed to initialize gadget\n");
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500984 return ret;
985 }
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500986 }
987
988 return 0;
989}
990
991static void dwc3_core_exit_mode(struct dwc3 *dwc)
992{
Mayank Ranaa75caa52017-10-10 11:45:13 -0700993 if (dwc->dr_mode == USB_DR_MODE_PERIPHERAL ||
994 dwc->dr_mode == USB_DR_MODE_OTG)
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500995 dwc3_gadget_exit(dwc);
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500996}
997
Mayank Ranaa99689a2016-08-10 17:39:47 -0700998/* XHCI reset, resets other CORE registers as well, re-init those */
999void dwc3_post_host_reset_core_init(struct dwc3 *dwc)
1000{
1001 dwc3_core_init(dwc);
1002 dwc3_gadget_restart(dwc);
1003}
1004
1005static void (*notify_event)(struct dwc3 *, unsigned int);
1006void dwc3_set_notifier(void (*notify)(struct dwc3 *, unsigned int))
1007{
1008 notify_event = notify;
1009}
1010EXPORT_SYMBOL(dwc3_set_notifier);
1011
1012int dwc3_notify_event(struct dwc3 *dwc, unsigned int event)
1013{
1014 int ret = 0;
1015
1016 if (dwc->notify_event)
1017 dwc->notify_event(dwc, event);
1018 else
1019 ret = -ENODEV;
1020
1021 return ret;
1022}
1023EXPORT_SYMBOL(dwc3_notify_event);
1024
1025int dwc3_core_pre_init(struct dwc3 *dwc)
1026{
Mayank Rana5e268282017-04-10 15:55:18 -07001027 int ret = 0;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001028
1029 dwc3_cache_hwparams(dwc);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001030 if (!dwc->ev_buf) {
1031 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1032 if (ret) {
1033 dev_err(dwc->dev, "failed to allocate event buffers\n");
1034 ret = -ENOMEM;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001035 }
1036 }
1037
Mayank Ranaa99689a2016-08-10 17:39:47 -07001038 return ret;
1039}
1040
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001041#define DWC3_ALIGN_MASK (16 - 1)
1042
John Youn26cac202016-11-14 12:32:43 -08001043/* check whether the core supports IMOD */
1044bool dwc3_has_imod(struct dwc3 *dwc)
1045{
1046 return ((dwc3_is_usb3(dwc) &&
1047 dwc->revision >= DWC3_REVISION_300A) ||
1048 (dwc3_is_usb31(dwc) &&
1049 dwc->revision >= DWC3_USB31_REVISION_120A));
1050}
1051
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001052static int dwc3_probe(struct platform_device *pdev)
1053{
1054 struct device *dev = &pdev->dev;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001055 struct resource *res;
1056 struct dwc3 *dwc;
Huang Rui80caf7d2014-10-28 19:54:26 +08001057 u8 lpm_nyet_threshold;
Huang Rui6b6a0c92014-10-31 11:11:12 +08001058 u8 tx_de_emphasis;
Huang Rui460d0982014-10-31 11:11:18 +08001059 u8 hird_threshold;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001060 int irq;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001061
Andy Shevchenkob09e99e2014-05-15 15:53:32 +03001062 int ret;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001063
1064 void __iomem *regs;
1065 void *mem;
1066
Mayank Rana861da2b2016-07-13 13:47:57 -07001067 if (count >= DWC_CTRL_COUNT) {
1068 dev_err(dev, "Err dwc instance %d >= %d available\n",
1069 count, DWC_CTRL_COUNT);
1070 ret = -EINVAL;
1071 return ret;
1072 }
1073
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001074 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001075 if (!mem)
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001076 return -ENOMEM;
Jingoo Han734d5a52014-07-17 12:45:11 +09001077
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001078 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
1079 dwc->mem = mem;
1080 dwc->dev = dev;
1081
Mayank Ranaa99689a2016-08-10 17:39:47 -07001082 dwc->notify_event = notify_event;
1083 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1084 if (!res) {
1085 dev_err(dev, "missing IRQ\n");
1086 return -ENODEV;
1087 }
1088 dwc->xhci_resources[1].start = res->start;
1089 dwc->xhci_resources[1].end = res->end;
1090 dwc->xhci_resources[1].flags = res->flags;
1091 dwc->xhci_resources[1].name = res->name;
1092
1093 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1094
1095 /* will be enabled in dwc3_msm_resume() */
1096 irq_set_status_flags(irq, IRQ_NOAUTOEN);
Mayank Ranaf616a7f2017-03-20 16:10:39 -07001097 ret = devm_request_irq(dev, irq, dwc3_interrupt, IRQF_SHARED, "dwc3",
1098 dwc);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001099 if (ret) {
1100 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1101 irq, ret);
1102 return -ENODEV;
1103 }
1104
1105 dwc->irq = irq;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001106 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1107 if (!res) {
1108 dev_err(dev, "missing memory resource\n");
1109 return -ENODEV;
1110 }
1111
Mayank Ranaa99689a2016-08-10 17:39:47 -07001112 dwc->reg_phys = res->start;
Vivek Gautamf32a5e22014-06-04 14:34:52 +05301113 dwc->xhci_resources[0].start = res->start;
1114 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1115 DWC3_XHCI_REGS_END;
1116 dwc->xhci_resources[0].flags = res->flags;
1117 dwc->xhci_resources[0].name = res->name;
1118
1119 res->start += DWC3_GLOBALS_REGS_START;
1120
1121 /*
1122 * Request memory region but exclude xHCI regs,
1123 * since it will be requested by the xhci-plat driver.
1124 */
1125 regs = devm_ioremap_resource(dev, res);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001126 if (IS_ERR(regs)) {
1127 ret = PTR_ERR(regs);
1128 goto err0;
1129 }
Vivek Gautamf32a5e22014-06-04 14:34:52 +05301130
1131 dwc->regs = regs;
1132 dwc->regs_size = resource_size(res);
Vivek Gautamf32a5e22014-06-04 14:34:52 +05301133
Huang Rui80caf7d2014-10-28 19:54:26 +08001134 /* default to highest possible threshold */
Hemant Kumar628b8912017-02-08 16:15:57 -08001135 lpm_nyet_threshold = 0xf;
Huang Rui80caf7d2014-10-28 19:54:26 +08001136
Huang Rui6b6a0c92014-10-31 11:11:12 +08001137 /* default to -3.5dB de-emphasis */
1138 tx_de_emphasis = 1;
1139
Huang Rui460d0982014-10-31 11:11:18 +08001140 /*
1141 * default to assert utmi_sleep_n and use maximum allowed HIRD
1142 * threshold value of 0b1100
1143 */
1144 hird_threshold = 12;
1145
Heikki Krogerus63863b92015-09-21 11:14:32 +03001146 dwc->maximum_speed = usb_get_maximum_speed(dev);
Vamsi Krishna Samavedam86ed20b2017-01-31 13:55:38 -08001147 dwc->max_hw_supp_speed = dwc->maximum_speed;
Heikki Krogerus06e71142015-09-21 11:14:34 +03001148 dwc->dr_mode = usb_get_dr_mode(dev);
Mayank Ranafb9cd932016-11-03 23:26:38 -07001149
1150 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN) {
1151 dwc->dr_mode = USB_DR_MODE_OTG;
1152 dwc->is_drd = 1;
1153 }
1154
William Wu32f2ed82016-08-16 22:44:38 +08001155 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
Heikki Krogerus63863b92015-09-21 11:14:32 +03001156
Arnd Bergmann42695fc2016-11-17 17:13:47 +05301157 dwc->sysdev_is_parent = device_property_read_bool(dev,
1158 "linux,sysdev_is_parent");
1159 if (dwc->sysdev_is_parent)
1160 dwc->sysdev = dwc->dev->parent;
1161 else
1162 dwc->sysdev = dwc->dev;
1163
Heikki Krogerus3d128912015-09-21 11:14:35 +03001164 dwc->has_lpm_erratum = device_property_read_bool(dev,
Huang Rui80caf7d2014-10-28 19:54:26 +08001165 "snps,has-lpm-erratum");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001166 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
Huang Rui80caf7d2014-10-28 19:54:26 +08001167 &lpm_nyet_threshold);
Heikki Krogerus3d128912015-09-21 11:14:35 +03001168 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
Huang Rui460d0982014-10-31 11:11:18 +08001169 "snps,is-utmi-l1-suspend");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001170 device_property_read_u8(dev, "snps,hird-threshold",
Huang Rui460d0982014-10-31 11:11:18 +08001171 &hird_threshold);
Mayank Ranae27420f2017-10-10 15:44:19 -07001172
1173 device_property_read_u32(dev, "snps,xhci-imod-value",
1174 &dwc->xhci_imod_value);
1175
Heikki Krogerus3d128912015-09-21 11:14:35 +03001176 dwc->usb3_lpm_capable = device_property_read_bool(dev,
Robert Baldygaeac68e82015-03-09 15:06:12 +01001177 "snps,usb3_lpm_capable");
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001178
Mayank Ranaa8e4de62016-12-13 17:11:15 -08001179 dwc->needs_fifo_resize = device_property_read_bool(dev,
1180 "tx-fifo-resize");
1181
Heikki Krogerus3d128912015-09-21 11:14:35 +03001182 dwc->disable_scramble_quirk = device_property_read_bool(dev,
Huang Rui3b812212014-10-28 19:54:25 +08001183 "snps,disable_scramble_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001184 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
Huang Rui9a5b2f32014-10-28 19:54:27 +08001185 "snps,u2exit_lfps_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001186 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
Huang Ruib5a65c42014-10-28 19:54:28 +08001187 "snps,u2ss_inp3_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001188 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
Huang Ruidf31f5b2014-10-28 19:54:29 +08001189 "snps,req_p1p2p3_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001190 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
Huang Ruia2a1d0f2014-10-28 19:54:30 +08001191 "snps,del_p1p2p3_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001192 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
Huang Rui41c06ff2014-10-28 19:54:31 +08001193 "snps,del_phy_power_chg_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001194 dwc->lfps_filter_quirk = device_property_read_bool(dev,
Huang Ruifb67afc2014-10-28 19:54:32 +08001195 "snps,lfps_filter_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001196 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
Huang Rui14f4ac52014-10-28 19:54:33 +08001197 "snps,rx_detect_poll_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001198 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
Huang Rui59acfa22014-10-31 11:11:13 +08001199 "snps,dis_u3_susphy_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001200 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
Huang Rui0effe0a2014-10-31 11:11:14 +08001201 "snps,dis_u2_susphy_quirk");
John Younec791d12015-10-02 20:30:57 -07001202 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1203 "snps,dis_enblslpm_quirk");
Rajesh Bhagate58dd352016-03-14 14:40:50 +05301204 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1205 "snps,dis_rxdet_inp3_quirk");
William Wu16199f32016-08-16 22:44:37 +08001206 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1207 "snps,dis-u2-freeclk-exists-quirk");
William Wu00fe0812016-08-16 22:44:39 +08001208 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1209 "snps,dis-del-phy-power-chg-quirk");
Huang Rui6b6a0c92014-10-31 11:11:12 +08001210
Heikki Krogerus3d128912015-09-21 11:14:35 +03001211 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
Huang Rui6b6a0c92014-10-31 11:11:12 +08001212 "snps,tx_de_emphasis_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001213 device_property_read_u8(dev, "snps,tx_de_emphasis",
Huang Rui6b6a0c92014-10-31 11:11:12 +08001214 &tx_de_emphasis);
Heikki Krogerus3d128912015-09-21 11:14:35 +03001215 device_property_read_string(dev, "snps,hsphy_interface",
1216 &dwc->hsphy_interface);
1217 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
Felipe Balbibcdb3272016-05-16 10:42:23 +03001218 &dwc->fladj);
Mayank Rana00b03982015-06-10 11:43:09 -07001219 dwc->disable_clk_gating = device_property_read_bool(dev,
1220 "snps,disable-clk-gating");
Hemant Kumarde2ef6e2017-11-02 17:00:33 -07001221 dwc->enable_bus_suspend = device_property_read_bool(dev,
1222 "snps,bus-suspend-enable");
Mayank Ranaa99689a2016-08-10 17:39:47 -07001223 if (dwc->enable_bus_suspend) {
1224 pm_runtime_set_autosuspend_delay(dev, 500);
1225 pm_runtime_use_autosuspend(dev);
1226 }
1227
Huang Rui80caf7d2014-10-28 19:54:26 +08001228 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
Huang Rui6b6a0c92014-10-31 11:11:12 +08001229 dwc->tx_de_emphasis = tx_de_emphasis;
Huang Rui80caf7d2014-10-28 19:54:26 +08001230
Huang Rui460d0982014-10-31 11:11:18 +08001231 dwc->hird_threshold = hird_threshold
1232 | (dwc->is_utmi_l1_suspend << 4);
1233
Mayank Ranaa99689a2016-08-10 17:39:47 -07001234 init_waitqueue_head(&dwc->wait_linkstate);
Heikki Krogerus6c89cce02015-05-13 15:26:45 +03001235 platform_set_drvdata(pdev, dwc);
1236
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001237 ret = dwc3_core_get_phy(dwc);
1238 if (ret)
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001239 goto err0;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001240
Felipe Balbi72246da2011-08-19 18:10:58 +03001241 spin_lock_init(&dwc->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03001242
Mayank Ranaf616a7f2017-03-20 16:10:39 -07001243 dwc->dwc_wq = alloc_ordered_workqueue("dwc_wq", WQ_HIGHPRI);
1244 if (!dwc->dwc_wq) {
1245 pr_err("%s: Unable to create workqueue dwc_wq\n", __func__);
Hemant Kumar9877e6d2017-11-28 14:24:33 -08001246 goto err0;
Mayank Ranaf616a7f2017-03-20 16:10:39 -07001247 }
1248
1249 INIT_WORK(&dwc->bh_work, dwc3_bh_work);
1250
Mayank Ranaa99689a2016-08-10 17:39:47 -07001251 pm_runtime_no_callbacks(dev);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001252 pm_runtime_set_active(dev);
Chanho Park802ca852012-02-15 18:27:55 +09001253 pm_runtime_enable(dev);
Chanho Park802ca852012-02-15 18:27:55 +09001254 pm_runtime_forbid(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001255
John Youn77966eb2016-02-19 17:31:01 -08001256 /* Check the maximum_speed parameter */
1257 switch (dwc->maximum_speed) {
1258 case USB_SPEED_LOW:
1259 case USB_SPEED_FULL:
1260 case USB_SPEED_HIGH:
1261 case USB_SPEED_SUPER:
1262 case USB_SPEED_SUPER_PLUS:
1263 break;
1264 default:
1265 dev_err(dev, "invalid maximum_speed parameter %d\n",
1266 dwc->maximum_speed);
1267 /* fall through */
1268 case USB_SPEED_UNKNOWN:
1269 /* default to superspeed */
John Youn2c7f1bd2016-02-05 17:08:59 -08001270 dwc->maximum_speed = USB_SPEED_SUPER;
1271
1272 /*
1273 * default to superspeed plus if we are capable.
1274 */
1275 if (dwc3_is_usb31(dwc) &&
1276 (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
1277 DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1278 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
John Youn77966eb2016-02-19 17:31:01 -08001279
Vamsi Krishna Samavedam86ed20b2017-01-31 13:55:38 -08001280 dwc->max_hw_supp_speed = dwc->maximum_speed;
John Youn77966eb2016-02-19 17:31:01 -08001281 break;
John Youn2c7f1bd2016-02-05 17:08:59 -08001282 }
1283
Mayank Ranaa99689a2016-08-10 17:39:47 -07001284 /* Adjust Frame Length */
1285 dwc3_frame_length_adjustment(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001286
Mayank Ranaa99689a2016-08-10 17:39:47 -07001287 /* Hardcode number of eps */
1288 dwc->num_in_eps = 16;
1289 dwc->num_out_eps = 16;
Felipe Balbi72246da2011-08-19 18:10:58 +03001290
Felipe Balbi72246da2011-08-19 18:10:58 +03001291 ret = dwc3_core_init_mode(dwc);
1292 if (ret)
Hemant Kumar9877e6d2017-11-28 14:24:33 -08001293 goto err1;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001294
1295 ret = dwc3_debugfs_init(dwc);
1296 if (ret) {
1297 dev_err(dev, "failed to initialize debugfs\n");
Kyle Yan65be4a52016-10-31 15:05:00 -07001298 goto err_core_init;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001299 }
1300
Mayank Rana861da2b2016-07-13 13:47:57 -07001301 dwc->dwc_ipc_log_ctxt = ipc_log_context_create(NUM_LOG_PAGES,
1302 dev_name(dwc->dev), 0);
1303 if (!dwc->dwc_ipc_log_ctxt)
1304 dev_err(dwc->dev, "Error getting ipc_log_ctxt\n");
1305
1306 dwc3_instance[count] = dwc;
1307 dwc->index = count;
1308 count++;
1309
Mayank Ranaa99689a2016-08-10 17:39:47 -07001310 pm_runtime_allow(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001311 return 0;
1312
Kyle Yan65be4a52016-10-31 15:05:00 -07001313err_core_init:
1314 dwc3_core_exit_mode(dwc);
Hemant Kumar9877e6d2017-11-28 14:24:33 -08001315err1:
1316 destroy_workqueue(dwc->dwc_wq);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001317err0:
1318 /*
1319 * restore res->start back to its original value so that, in case the
1320 * probe is deferred, we don't end up getting error in request the
1321 * memory region the next time probe is called.
1322 */
1323 res->start -= DWC3_GLOBALS_REGS_START;
1324
Felipe Balbi72246da2011-08-19 18:10:58 +03001325 return ret;
1326}
1327
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05001328static int dwc3_remove(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +03001329{
Felipe Balbi72246da2011-08-19 18:10:58 +03001330 struct dwc3 *dwc = platform_get_drvdata(pdev);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001331 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1332
Felipe Balbifc8bb912016-05-16 13:14:48 +03001333 pm_runtime_get_sync(&pdev->dev);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001334 /*
1335 * restore res->start back to its original value so that, in case the
1336 * probe is deferred, we don't end up getting error in request the
1337 * memory region the next time probe is called.
1338 */
1339 res->start -= DWC3_GLOBALS_REGS_START;
Felipe Balbi72246da2011-08-19 18:10:58 +03001340
Felipe Balbidc99f162014-09-03 16:13:37 -05001341 dwc3_debugfs_exit(dwc);
1342 dwc3_core_exit_mode(dwc);
Kishon Vijay Abraham I8ba007a2013-01-25 08:30:54 +05301343
Felipe Balbi72246da2011-08-19 18:10:58 +03001344 dwc3_core_exit(dwc);
Heikki Krogerus88bc9d12015-05-13 15:26:51 +03001345 dwc3_ulpi_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001346
Mayank Ranaf616a7f2017-03-20 16:10:39 -07001347 destroy_workqueue(dwc->dwc_wq);
1348
Felipe Balbifc8bb912016-05-16 13:14:48 +03001349 pm_runtime_put_sync(&pdev->dev);
1350 pm_runtime_allow(&pdev->dev);
1351 pm_runtime_disable(&pdev->dev);
1352
Felipe Balbic499ff72016-05-16 10:49:01 +03001353 dwc3_free_event_buffers(dwc);
1354 dwc3_free_scratch_buffers(dwc);
1355
Mayank Rana861da2b2016-07-13 13:47:57 -07001356 ipc_log_context_destroy(dwc->dwc_ipc_log_ctxt);
1357 dwc->dwc_ipc_log_ctxt = NULL;
1358 count--;
1359 dwc3_instance[dwc->index] = NULL;
1360
Felipe Balbi72246da2011-08-19 18:10:58 +03001361 return 0;
1362}
1363
Felipe Balbifc8bb912016-05-16 13:14:48 +03001364#ifdef CONFIG_PM
1365static int dwc3_suspend_common(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03001366{
Felipe Balbifc8bb912016-05-16 13:14:48 +03001367 unsigned long flags;
Felipe Balbi7415f172012-04-30 14:56:33 +03001368
Ruchika Kharwara45c82b82013-07-06 07:52:49 -05001369 switch (dwc->dr_mode) {
1370 case USB_DR_MODE_PERIPHERAL:
1371 case USB_DR_MODE_OTG:
Felipe Balbifc8bb912016-05-16 13:14:48 +03001372 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7415f172012-04-30 14:56:33 +03001373 dwc3_gadget_suspend(dwc);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001374 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi51f5d492016-05-16 10:52:58 +03001375 break;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -05001376 case USB_DR_MODE_HOST:
Felipe Balbi7415f172012-04-30 14:56:33 +03001377 default:
Felipe Balbi51f5d492016-05-16 10:52:58 +03001378 /* do nothing */
Felipe Balbi7415f172012-04-30 14:56:33 +03001379 break;
1380 }
1381
Felipe Balbi51f5d492016-05-16 10:52:58 +03001382 dwc3_core_exit(dwc);
Felipe Balbi5c4ad3182016-04-11 17:12:34 +03001383
Felipe Balbifc8bb912016-05-16 13:14:48 +03001384 return 0;
1385}
1386
1387static int dwc3_resume_common(struct dwc3 *dwc)
1388{
1389 unsigned long flags;
1390 int ret;
1391
1392 ret = dwc3_core_init(dwc);
1393 if (ret)
1394 return ret;
1395
1396 switch (dwc->dr_mode) {
1397 case USB_DR_MODE_PERIPHERAL:
1398 case USB_DR_MODE_OTG:
1399 spin_lock_irqsave(&dwc->lock, flags);
1400 dwc3_gadget_resume(dwc);
1401 spin_unlock_irqrestore(&dwc->lock, flags);
1402 /* FALLTHROUGH */
1403 case USB_DR_MODE_HOST:
1404 default:
1405 /* do nothing */
1406 break;
1407 }
1408
1409 return 0;
1410}
1411
1412static int dwc3_runtime_checks(struct dwc3 *dwc)
1413{
1414 switch (dwc->dr_mode) {
1415 case USB_DR_MODE_PERIPHERAL:
1416 case USB_DR_MODE_OTG:
1417 if (dwc->connected)
1418 return -EBUSY;
1419 break;
1420 case USB_DR_MODE_HOST:
1421 default:
1422 /* do nothing */
1423 break;
1424 }
1425
1426 return 0;
1427}
1428
1429static int dwc3_runtime_suspend(struct device *dev)
1430{
1431 struct dwc3 *dwc = dev_get_drvdata(dev);
1432 int ret;
1433
Mayank Ranaa99689a2016-08-10 17:39:47 -07001434 /* Check if platform glue driver handling PM, if not then handle here */
1435 if (!dwc3_notify_event(dwc, DWC3_CORE_PM_RESUME_EVENT))
1436 return 0;
Felipe Balbifc8bb912016-05-16 13:14:48 +03001437
1438 ret = dwc3_suspend_common(dwc);
1439 if (ret)
1440 return ret;
1441
1442 device_init_wakeup(dev, true);
1443
1444 return 0;
1445}
1446
1447static int dwc3_runtime_resume(struct device *dev)
1448{
1449 struct dwc3 *dwc = dev_get_drvdata(dev);
1450 int ret;
1451
Mayank Ranaa99689a2016-08-10 17:39:47 -07001452 /* Check if platform glue driver handling PM, if not then handle here */
1453 if (!dwc3_notify_event(dwc, DWC3_CORE_PM_RESUME_EVENT))
1454 return 0;
1455
Felipe Balbifc8bb912016-05-16 13:14:48 +03001456 device_init_wakeup(dev, false);
1457
1458 ret = dwc3_resume_common(dwc);
1459 if (ret)
1460 return ret;
1461
1462 switch (dwc->dr_mode) {
1463 case USB_DR_MODE_PERIPHERAL:
1464 case USB_DR_MODE_OTG:
1465 dwc3_gadget_process_pending_events(dwc);
1466 break;
1467 case USB_DR_MODE_HOST:
1468 default:
1469 /* do nothing */
1470 break;
1471 }
1472
1473 pm_runtime_mark_last_busy(dev);
Felipe Balbib74c2d82016-07-28 13:07:07 +03001474 pm_runtime_put(dev);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001475
1476 return 0;
1477}
1478
1479static int dwc3_runtime_idle(struct device *dev)
1480{
1481 struct dwc3 *dwc = dev_get_drvdata(dev);
1482
1483 switch (dwc->dr_mode) {
1484 case USB_DR_MODE_PERIPHERAL:
1485 case USB_DR_MODE_OTG:
1486 if (dwc3_runtime_checks(dwc))
1487 return -EBUSY;
1488 break;
1489 case USB_DR_MODE_HOST:
1490 default:
1491 /* do nothing */
1492 break;
1493 }
1494
1495 pm_runtime_mark_last_busy(dev);
1496 pm_runtime_autosuspend(dev);
1497
1498 return 0;
1499}
1500#endif /* CONFIG_PM */
1501
1502#ifdef CONFIG_PM_SLEEP
1503static int dwc3_suspend(struct device *dev)
1504{
1505 struct dwc3 *dwc = dev_get_drvdata(dev);
1506 int ret;
1507
Mayank Rana5ca41122017-03-24 11:30:25 -07001508 /* Check if platform glue driver handling PM, if not then handle here */
1509 if (!dwc3_notify_event(dwc, DWC3_CORE_PM_SUSPEND_EVENT))
1510 return 0;
1511
Felipe Balbifc8bb912016-05-16 13:14:48 +03001512 ret = dwc3_suspend_common(dwc);
1513 if (ret)
1514 return ret;
1515
Sekhar Nori63444752015-08-31 21:09:08 +05301516 pinctrl_pm_select_sleep_state(dev);
1517
Felipe Balbi7415f172012-04-30 14:56:33 +03001518 return 0;
1519}
1520
1521static int dwc3_resume(struct device *dev)
1522{
1523 struct dwc3 *dwc = dev_get_drvdata(dev);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +05301524 int ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03001525
Mayank Rana5ca41122017-03-24 11:30:25 -07001526 /* Check if platform glue driver handling PM, if not then handle here */
1527 if (!dwc3_notify_event(dwc, DWC3_CORE_PM_RESUME_EVENT))
1528 return 0;
1529
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");