blob: 90dd1153c9cef3891dc5b8a0eab8a5bb089b970d [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 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
Felipe Balbia72e6582011-09-05 13:37:28 +030039#include <linux/module.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030040#include <linux/kernel.h>
41#include <linux/slab.h>
42#include <linux/spinlock.h>
43#include <linux/platform_device.h>
44#include <linux/pm_runtime.h>
45#include <linux/interrupt.h>
46#include <linux/ioport.h>
47#include <linux/io.h>
48#include <linux/list.h>
49#include <linux/delay.h>
50#include <linux/dma-mapping.h>
Felipe Balbi457e84b2012-01-18 18:04:09 +020051#include <linux/of.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030052
53#include <linux/usb/ch9.h>
54#include <linux/usb/gadget.h>
55
56#include "core.h"
57#include "gadget.h"
58#include "io.h"
59
60#include "debug.h"
61
Felipe Balbi6c167fc2011-10-07 22:55:04 +030062static char *maximum_speed = "super";
63module_param(maximum_speed, charp, 0);
64MODULE_PARM_DESC(maximum_speed, "Maximum supported speed.");
65
Felipe Balbi8300dd22011-10-18 13:54:01 +030066/* -------------------------------------------------------------------------- */
67
68#define DWC3_DEVS_POSSIBLE 32
69
70static DECLARE_BITMAP(dwc3_devs, DWC3_DEVS_POSSIBLE);
71
72int dwc3_get_device_id(void)
73{
74 int id;
75
76again:
77 id = find_first_zero_bit(dwc3_devs, DWC3_DEVS_POSSIBLE);
78 if (id < DWC3_DEVS_POSSIBLE) {
79 int old;
80
81 old = test_and_set_bit(id, dwc3_devs);
82 if (old)
83 goto again;
84 } else {
85 pr_err("dwc3: no space for new device\n");
86 id = -ENOMEM;
87 }
88
Dan Carpenter075cd142012-02-04 16:37:14 +030089 return id;
Felipe Balbi8300dd22011-10-18 13:54:01 +030090}
91EXPORT_SYMBOL_GPL(dwc3_get_device_id);
92
93void dwc3_put_device_id(int id)
94{
95 int ret;
96
97 if (id < 0)
98 return;
99
100 ret = test_bit(id, dwc3_devs);
101 WARN(!ret, "dwc3: ID %d not in use\n", id);
Oliver Neukum5fdeeb82012-08-26 21:34:19 +0200102 smp_mb__before_clear_bit();
Felipe Balbi8300dd22011-10-18 13:54:01 +0300103 clear_bit(id, dwc3_devs);
104}
105EXPORT_SYMBOL_GPL(dwc3_put_device_id);
106
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100107void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
108{
109 u32 reg;
110
111 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
112 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
113 reg |= DWC3_GCTL_PRTCAPDIR(mode);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530114 /*
115 * Set this bit so that device attempts three more times at SS, even
116 * if it failed previously to operate in SS mode.
117 */
118 reg |= DWC3_GCTL_U2RSTECN;
119 if (mode == DWC3_GCTL_PRTCAP_HOST) {
120 /*
121 * Allow ITP generated off of ref clk based counter instead
122 * of UTMI/ULPI clk based counter, when superspeed only is
123 * active so that UTMI/ULPI PHY can be suspened.
124 */
125 reg |= DWC3_GCTL_SOFITPSYNC;
126 reg &= ~(DWC3_GCTL_PWRDNSCALEMASK);
127 reg |= DWC3_GCTL_PWRDNSCALE(2);
128 } else if (mode == DWC3_GCTL_PRTCAP_DEVICE) {
129 reg &= ~(DWC3_GCTL_PWRDNSCALEMASK);
130 reg |= DWC3_GCTL_PWRDNSCALE(2);
131 reg &= ~(DWC3_GCTL_SOFITPSYNC);
132 }
133 reg |= DWC3_GCTL_U2EXIT_LFPS;
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100134 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530135 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
136 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
137 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
138 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
139 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
140 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100141}
Felipe Balbi8300dd22011-10-18 13:54:01 +0300142
Felipe Balbi72246da2011-08-19 18:10:58 +0300143/**
144 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
145 * @dwc: pointer to our context structure
146 */
147static void dwc3_core_soft_reset(struct dwc3 *dwc)
148{
149 u32 reg;
150
151 /* Before Resetting PHY, put Core in Reset */
152 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
153 reg |= DWC3_GCTL_CORESOFTRESET;
154 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
155
Vijayavardhan Vennapusada8d06c2013-10-22 19:19:57 +0530156 dwc3_notify_event(dwc, DWC3_CONTROLLER_RESET_EVENT);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530157
Felipe Balbi72246da2011-08-19 18:10:58 +0300158 /* Assert USB3 PHY reset */
159 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
160 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
161 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
162
163 /* Assert USB2 PHY reset */
164 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
165 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
166 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
167
168 mdelay(100);
169
170 /* Clear USB3 PHY reset */
171 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
172 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
173 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
174
175 /* Clear USB2 PHY reset */
176 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
177 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
178 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
179
Pratyush Anand38a535c2012-06-21 17:44:28 +0530180 mdelay(100);
181
Felipe Balbi72246da2011-08-19 18:10:58 +0300182 /* After PHYs are stable we can take Core out of reset state */
183 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
184 reg &= ~DWC3_GCTL_CORESOFTRESET;
185 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530186
Vijayavardhan Vennapusada8d06c2013-10-22 19:19:57 +0530187 dwc3_notify_event(dwc, DWC3_CONTROLLER_POST_RESET_EVENT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300188}
189
190/**
191 * dwc3_free_one_event_buffer - Frees one event buffer
192 * @dwc: Pointer to our controller context structure
193 * @evt: Pointer to event buffer to be freed
194 */
195static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
196 struct dwc3_event_buffer *evt)
197{
198 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
199 kfree(evt);
200}
201
202/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800203 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300204 * @dwc: Pointer to our controller context structure
205 * @length: size of the event buffer
206 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800207 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300208 * otherwise ERR_PTR(errno).
209 */
210static struct dwc3_event_buffer *__devinit
211dwc3_alloc_one_event_buffer(struct dwc3 *dwc, unsigned length)
212{
213 struct dwc3_event_buffer *evt;
214
215 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
216 if (!evt)
217 return ERR_PTR(-ENOMEM);
218
219 evt->dwc = dwc;
220 evt->length = length;
221 evt->buf = dma_alloc_coherent(dwc->dev, length,
222 &evt->dma, GFP_KERNEL);
223 if (!evt->buf) {
224 kfree(evt);
225 return ERR_PTR(-ENOMEM);
226 }
227
228 return evt;
229}
230
231/**
232 * dwc3_free_event_buffers - frees all allocated event buffers
233 * @dwc: Pointer to our controller context structure
234 */
235static void dwc3_free_event_buffers(struct dwc3 *dwc)
236{
237 struct dwc3_event_buffer *evt;
238 int i;
239
Felipe Balbi9f622b22011-10-12 10:31:04 +0300240 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300241 evt = dwc->ev_buffs[i];
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900242 if (evt)
Felipe Balbi72246da2011-08-19 18:10:58 +0300243 dwc3_free_one_event_buffer(dwc, evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300244 }
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900245
246 kfree(dwc->ev_buffs);
Felipe Balbi72246da2011-08-19 18:10:58 +0300247}
248
249/**
250 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800251 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300252 * @length: size of event buffer
253 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800254 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300255 * may contain some buffers allocated but not all which were requested.
256 */
Felipe Balbi9f622b22011-10-12 10:31:04 +0300257static int __devinit dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300258{
Felipe Balbi9f622b22011-10-12 10:31:04 +0300259 int num;
Felipe Balbi72246da2011-08-19 18:10:58 +0300260 int i;
261
Felipe Balbi9f622b22011-10-12 10:31:04 +0300262 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
263 dwc->num_event_buffers = num;
264
Felipe Balbi457d3f22011-10-24 12:03:13 +0300265 dwc->ev_buffs = kzalloc(sizeof(*dwc->ev_buffs) * num, GFP_KERNEL);
266 if (!dwc->ev_buffs) {
267 dev_err(dwc->dev, "can't allocate event buffers array\n");
268 return -ENOMEM;
269 }
270
Felipe Balbi72246da2011-08-19 18:10:58 +0300271 for (i = 0; i < num; i++) {
272 struct dwc3_event_buffer *evt;
273
Vijayavardhan Vennapusacf45f022013-05-30 13:39:00 +0530274 /*
275 * As SW workaround, allocate 8 bytes more than size of event
276 * buffer given to USB Controller to avoid possible memory
277 * corruption caused by event buffer overflow when Hw writes
278 * Vendor Device test event which could be of 12 bytes.
279 */
280 evt = dwc3_alloc_one_event_buffer(dwc, (length + 8));
Felipe Balbi72246da2011-08-19 18:10:58 +0300281 if (IS_ERR(evt)) {
282 dev_err(dwc->dev, "can't allocate event buffer\n");
283 return PTR_ERR(evt);
284 }
285 dwc->ev_buffs[i] = evt;
286 }
287
288 return 0;
289}
290
291/**
292 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800293 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300294 *
295 * Returns 0 on success otherwise negative errno.
296 */
Wesley Cheng446ad8d2013-06-05 16:15:01 +0530297int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300298{
299 struct dwc3_event_buffer *evt;
300 int n;
301
Felipe Balbi9f622b22011-10-12 10:31:04 +0300302 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300303 evt = dwc->ev_buffs[n];
304 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
305 evt->buf, (unsigned long long) evt->dma,
306 evt->length);
307
Paul Zimmerman43fa01a2012-04-27 14:28:02 +0300308 evt->lpos = 0;
309
Felipe Balbi72246da2011-08-19 18:10:58 +0300310 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
311 lower_32_bits(evt->dma));
312 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
313 upper_32_bits(evt->dma));
314 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
Vijayavardhan Vennapusacf45f022013-05-30 13:39:00 +0530315 (evt->length - 8) & 0xffff);
Felipe Balbi72246da2011-08-19 18:10:58 +0300316 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
317 }
318
319 return 0;
320}
321
322static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
323{
324 struct dwc3_event_buffer *evt;
325 int n;
326
Felipe Balbi9f622b22011-10-12 10:31:04 +0300327 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300328 evt = dwc->ev_buffs[n];
Paul Zimmerman43fa01a2012-04-27 14:28:02 +0300329
330 evt->lpos = 0;
331
Felipe Balbi72246da2011-08-19 18:10:58 +0300332 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
333 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
334 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
335 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
336 }
337}
338
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530339static void dwc3_cache_hwparams(struct dwc3 *dwc)
Felipe Balbi26ceca92011-09-30 10:58:49 +0300340{
341 struct dwc3_hwparams *parms = &dwc->hwparams;
342
343 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
344 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
345 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
346 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
347 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
348 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
349 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
350 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
351 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
352}
353
Felipe Balbi72246da2011-08-19 18:10:58 +0300354/**
355 * dwc3_core_init - Low-level initialization of DWC3 Core
356 * @dwc: Pointer to our controller context structure
357 *
358 * Returns 0 on success otherwise negative errno.
359 */
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530360static int dwc3_core_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300361{
362 unsigned long timeout;
363 u32 reg;
364 int ret;
365
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200366 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
367 /* This should read as U3 followed by revision number */
368 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
369 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
370 ret = -ENODEV;
371 goto err0;
372 }
Felipe Balbi248b1222011-12-14 21:59:30 +0200373 dwc->revision = reg;
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200374
Felipe Balbi72246da2011-08-19 18:10:58 +0300375 /* issue device SoftReset too */
376 timeout = jiffies + msecs_to_jiffies(500);
377 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
378 do {
379 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
380 if (!(reg & DWC3_DCTL_CSFTRST))
381 break;
382
383 if (time_after(jiffies, timeout)) {
384 dev_err(dwc->dev, "Reset Timed Out\n");
385 ret = -ETIMEDOUT;
386 goto err0;
387 }
388
389 cpu_relax();
390 } while (true);
391
Pratyush Anand99d4da82012-06-21 17:44:29 +0530392 dwc3_core_soft_reset(dwc);
393
Felipe Balbi9f622b22011-10-12 10:31:04 +0300394 dwc3_cache_hwparams(dwc);
395
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100396 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800397 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100398 reg &= ~DWC3_GCTL_DISSCRAMBLE;
399
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100400 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100401 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
402 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
403 break;
404 default:
405 dev_dbg(dwc->dev, "No power optimization available\n");
406 }
407
408 /*
409 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800410 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100411 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800412 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100413 */
414 if (dwc->revision < DWC3_REVISION_190A)
415 reg |= DWC3_GCTL_U2RSTECN;
416
417 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
418
Pavankumar Kondetife2c0632012-06-12 15:21:13 +0530419 /*
Pavankumar Kondetic6e15aa2012-07-16 11:37:15 +0530420 * The default value of GUCTL[31:22] should be 0x8. But on cores
421 * revision < 2.30a, the default value is mistakenly overridden
422 * with 0x0. Restore the correct default value.
423 */
424 if (dwc->revision < DWC3_REVISION_230A) {
425 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
426 reg &= ~DWC3_GUCTL_REFCLKPER;
427 reg |= 0x8 << __ffs(DWC3_GUCTL_REFCLKPER);
428 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
429 }
430 /*
Pavankumar Kondetife2c0632012-06-12 15:21:13 +0530431 * Currently, the default and the recommended value for GUSB3PIPECTL
432 * [21:19] in the RTL is 3'b100 or 32 consecutive errors. Based on
433 * analysis and experiments in the lab, it is found that there is a
434 * relatively low probability of getting 32 consecutive word errors
435 * in the presence of random recovered noise (during electrical idle).
436 * This can delay the entry to a low power state such that for
437 * applications where the link stays in a non-U0 state for a short
438 * duration (< 1 microsecond), the local PHY does not enter the low
439 * power state prior to receiving a potential LFPS wakeup. This causes
440 * the PHY CDR (Clock and Data Recovery) operation to be unstable for
441 * some Synopsys PHYs.
442 *
443 * The proposal now is to change the default and the recommended value
444 * for GUSB3PIPECTL[21:19] in the RTL from 3'b100 to a minimum of
445 * 3'b001. Perform the same in software for controllers prior to 2.30a
446 * revision.
447 */
448
449 if (dwc->revision < DWC3_REVISION_230A) {
450 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
451 reg &= ~DWC3_GUSB3PIPECTL_DELAY_P1P2P3;
452 reg |= 1 << __ffs(DWC3_GUSB3PIPECTL_DELAY_P1P2P3);
Pavankumar Kondeti5acb4ba2012-07-16 11:44:46 +0530453 /*
454 * Receiver Detection in U3/Rx.Det is mistakenly disabled in
455 * cores < 2.30a. Fix it here.
456 */
457 reg &= ~DWC3_GUSB3PIPECTL_DIS_RXDET_U3_RXDET;
Pavankumar Kondetife2c0632012-06-12 15:21:13 +0530458 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
459 }
Vijayavardhan Vennapusad0136a72013-06-07 13:22:18 +0530460 /*
461 * clear Elastic buffer mode in GUSBPIPE_CTRL(0) register, otherwise
462 * it results in high link errors and could cause SS mode transfer
463 * failure.
464 */
465 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
466 reg &= ~DWC3_GUSB3PIPECTL_ELASTIC_BUF_MODE;
467 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
Pavankumar Kondetife2c0632012-06-12 15:21:13 +0530468
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530469 if (!dwc->ev_buffs) {
470 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
471 if (ret) {
472 dev_err(dwc->dev, "failed to allocate event buffers\n");
473 ret = -ENOMEM;
474 goto err1;
475 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300476 }
477
478 ret = dwc3_event_buffers_setup(dwc);
479 if (ret) {
480 dev_err(dwc->dev, "failed to setup event buffers\n");
481 goto err1;
482 }
483
Felipe Balbi72246da2011-08-19 18:10:58 +0300484 return 0;
485
486err1:
487 dwc3_free_event_buffers(dwc);
488
489err0:
490 return ret;
491}
492
493static void dwc3_core_exit(struct dwc3 *dwc)
494{
495 dwc3_event_buffers_cleanup(dwc);
496 dwc3_free_event_buffers(dwc);
497}
498
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530499/* XHCI reset, resets other CORE registers as well, re-init those */
500void dwc3_post_host_reset_core_init(struct dwc3 *dwc)
501{
502 dwc3_core_init(dwc);
503 dwc3_gadget_restart(dwc);
Vijayavardhan Vennapusada8d06c2013-10-22 19:19:57 +0530504 dwc3_notify_event(dwc, DWC3_CONTROLLER_POST_INITIALIZATION_EVENT);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530505}
506
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +0530507static void (*notify_event) (struct dwc3 *, unsigned);
508void dwc3_set_notifier(void (*notify)(struct dwc3 *, unsigned))
509{
510 notify_event = notify;
511}
512EXPORT_SYMBOL(dwc3_set_notifier);
513
514void dwc3_notify_event(struct dwc3 *dwc, unsigned event)
515{
516 if (dwc->notify_event)
517 dwc->notify_event(dwc, event);
518}
519EXPORT_SYMBOL(dwc3_notify_event);
520
Felipe Balbi72246da2011-08-19 18:10:58 +0300521#define DWC3_ALIGN_MASK (16 - 1)
522
Vijayavardhan Vennapusa8eb68732013-03-26 13:05:38 +0530523static u64 dwc3_dma_mask = DMA_BIT_MASK(64);
Felipe Balbi72246da2011-08-19 18:10:58 +0300524static int __devinit dwc3_probe(struct platform_device *pdev)
525{
Felipe Balbi457e84b2012-01-18 18:04:09 +0200526 struct device_node *node = pdev->dev.of_node;
Felipe Balbi72246da2011-08-19 18:10:58 +0300527 struct resource *res;
528 struct dwc3 *dwc;
Chanho Park802ca852012-02-15 18:27:55 +0900529 struct device *dev = &pdev->dev;
Felipe Balbi0949e992011-10-12 10:44:56 +0300530
Felipe Balbi72246da2011-08-19 18:10:58 +0300531 int ret = -ENOMEM;
Felipe Balbi0949e992011-10-12 10:44:56 +0300532
533 void __iomem *regs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300534 void *mem;
535
Felipe Balbi0949e992011-10-12 10:44:56 +0300536 u8 mode;
Manu Gautambb825d72013-03-12 16:25:42 +0530537 bool host_only_mode;
Felipe Balbi0949e992011-10-12 10:44:56 +0300538
Chanho Park802ca852012-02-15 18:27:55 +0900539 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300540 if (!mem) {
Chanho Park802ca852012-02-15 18:27:55 +0900541 dev_err(dev, "not enough memory\n");
542 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300543 }
544 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
545 dwc->mem = mem;
546
Vijayavardhan Vennapusa8eb68732013-03-26 13:05:38 +0530547 if (!dev->dma_mask)
548 dev->dma_mask = &dwc3_dma_mask;
549 if (!dev->coherent_dma_mask)
Hemant Kumar1b378d92013-04-19 11:24:05 -0700550 dev->coherent_dma_mask = DMA_BIT_MASK(64);
Vijayavardhan Vennapusa8eb68732013-03-26 13:05:38 +0530551
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +0530552 dwc->notify_event = notify_event;
Ido Shayevitz4a187332012-04-23 14:53:37 +0200553 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300554 if (!res) {
Ido Shayevitz4a187332012-04-23 14:53:37 +0200555 dev_err(dev, "missing IRQ\n");
Chanho Park802ca852012-02-15 18:27:55 +0900556 return -ENODEV;
Felipe Balbi72246da2011-08-19 18:10:58 +0300557 }
Kishon Vijay Abraham I2f8ae022012-08-21 14:56:16 +0530558 dwc->xhci_resources[1].start = res->start;
559 dwc->xhci_resources[1].end = res->end;
560 dwc->xhci_resources[1].flags = res->flags;
561 dwc->xhci_resources[1].name = res->name;
Felipe Balbi72246da2011-08-19 18:10:58 +0300562
Ido Shayevitz4a187332012-04-23 14:53:37 +0200563 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
564 if (!res) {
565 dev_err(dev, "missing memory resource\n");
566 return -ENODEV;
567 }
Kishon Vijay Abraham I2f8ae022012-08-21 14:56:16 +0530568 dwc->xhci_resources[0].start = res->start;
Ido Shayevitz4a187332012-04-23 14:53:37 +0200569 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
570 DWC3_XHCI_REGS_END;
Kishon Vijay Abraham I2f8ae022012-08-21 14:56:16 +0530571 dwc->xhci_resources[0].flags = res->flags;
572 dwc->xhci_resources[0].name = res->name;
Felipe Balbid07e8812011-10-12 14:08:26 +0300573
Ido Shayevitz4a187332012-04-23 14:53:37 +0200574 /*
575 * Request memory region but exclude xHCI regs,
576 * since it will be requested by the xhci-plat driver.
577 */
578 res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START,
579 resource_size(res) - DWC3_GLOBALS_REGS_START,
Chanho Park802ca852012-02-15 18:27:55 +0900580 dev_name(dev));
Ido Shayevitz4a187332012-04-23 14:53:37 +0200581
Felipe Balbi72246da2011-08-19 18:10:58 +0300582 if (!res) {
Chanho Park802ca852012-02-15 18:27:55 +0900583 dev_err(dev, "can't request mem region\n");
584 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300585 }
586
Felipe Balbi497a2a32012-08-10 09:16:43 +0300587 regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
Felipe Balbi72246da2011-08-19 18:10:58 +0300588 if (!regs) {
Chanho Park802ca852012-02-15 18:27:55 +0900589 dev_err(dev, "ioremap failed\n");
590 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300591 }
592
Felipe Balbi72246da2011-08-19 18:10:58 +0300593 spin_lock_init(&dwc->lock);
594 platform_set_drvdata(pdev, dwc);
595
596 dwc->regs = regs;
597 dwc->regs_size = resource_size(res);
Chanho Park802ca852012-02-15 18:27:55 +0900598 dwc->dev = dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300599
Felipe Balbi6c167fc2011-10-07 22:55:04 +0300600 if (!strncmp("super", maximum_speed, 5))
601 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
602 else if (!strncmp("high", maximum_speed, 4))
603 dwc->maximum_speed = DWC3_DCFG_HIGHSPEED;
604 else if (!strncmp("full", maximum_speed, 4))
605 dwc->maximum_speed = DWC3_DCFG_FULLSPEED1;
606 else if (!strncmp("low", maximum_speed, 3))
607 dwc->maximum_speed = DWC3_DCFG_LOWSPEED;
608 else
609 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
610
Kishon Vijay Abraham Ibdc707a2013-03-18 12:18:57 +0530611 dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize");
Manu Gautambb825d72013-03-12 16:25:42 +0530612 host_only_mode = of_property_read_bool(node, "host-only-mode");
Felipe Balbi457e84b2012-01-18 18:04:09 +0200613
Manu Gautamb5067272012-07-02 09:53:41 +0530614 pm_runtime_no_callbacks(dev);
615 pm_runtime_set_active(dev);
Chanho Park802ca852012-02-15 18:27:55 +0900616 pm_runtime_enable(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300617
618 ret = dwc3_core_init(dwc);
619 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900620 dev_err(dev, "failed to initialize core\n");
621 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300622 }
623
Felipe Balbi0949e992011-10-12 10:44:56 +0300624 mode = DWC3_MODE(dwc->hwparams.hwparams0);
625
Manu Gautambb825d72013-03-12 16:25:42 +0530626 /* Override mode if user selects host-only config with DRD core */
627 if (host_only_mode && (mode == DWC3_MODE_DRD)) {
628 dev_dbg(dev, "host only mode selected\n");
629 mode = DWC3_MODE_HOST;
630 }
631
Felipe Balbi0949e992011-10-12 10:44:56 +0300632 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300633 case DWC3_MODE_DEVICE:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100634 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300635 ret = dwc3_gadget_init(dwc);
636 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900637 dev_err(dev, "failed to initialize gadget\n");
638 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300639 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300640 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300641 case DWC3_MODE_HOST:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100642 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
Felipe Balbid07e8812011-10-12 14:08:26 +0300643 ret = dwc3_host_init(dwc);
644 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900645 dev_err(dev, "failed to initialize host\n");
646 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300647 }
648 break;
649 case DWC3_MODE_DRD:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100650 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200651 ret = dwc3_otg_init(dwc);
652 if (ret) {
653 dev_err(dev, "failed to initialize otg\n");
654 goto err1;
655 }
656
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530657 ret = dwc3_host_init(dwc);
658 if (ret) {
659 dev_err(dev, "failed to initialize host\n");
660 dwc3_otg_exit(dwc);
661 goto err1;
662 }
663
Felipe Balbid07e8812011-10-12 14:08:26 +0300664 ret = dwc3_gadget_init(dwc);
665 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900666 dev_err(dev, "failed to initialize gadget\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200667 dwc3_host_exit(dwc);
668 dwc3_otg_exit(dwc);
Chanho Park802ca852012-02-15 18:27:55 +0900669 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300670 }
671 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300672 default:
Chanho Park802ca852012-02-15 18:27:55 +0900673 dev_err(dev, "Unsupported mode of operation %d\n", mode);
674 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300675 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300676 dwc->mode = mode;
Felipe Balbi72246da2011-08-19 18:10:58 +0300677
678 ret = dwc3_debugfs_init(dwc);
679 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900680 dev_err(dev, "failed to initialize debugfs\n");
681 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +0300682 }
683
Vijayavardhan Vennapusada8d06c2013-10-22 19:19:57 +0530684 dwc3_notify_event(dwc, DWC3_CONTROLLER_POST_INITIALIZATION_EVENT);
685
Felipe Balbi72246da2011-08-19 18:10:58 +0300686 return 0;
687
Chanho Park802ca852012-02-15 18:27:55 +0900688err2:
Felipe Balbi0949e992011-10-12 10:44:56 +0300689 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300690 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300691 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300692 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300693 case DWC3_MODE_HOST:
694 dwc3_host_exit(dwc);
695 break;
696 case DWC3_MODE_DRD:
Felipe Balbid07e8812011-10-12 14:08:26 +0300697 dwc3_gadget_exit(dwc);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200698 dwc3_host_exit(dwc);
699 dwc3_otg_exit(dwc);
Felipe Balbid07e8812011-10-12 14:08:26 +0300700 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300701 default:
702 /* do nothing */
703 break;
704 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300705
Chanho Park802ca852012-02-15 18:27:55 +0900706err1:
Felipe Balbi72246da2011-08-19 18:10:58 +0300707 dwc3_core_exit(dwc);
708
Felipe Balbi72246da2011-08-19 18:10:58 +0300709 return ret;
710}
711
712static int __devexit dwc3_remove(struct platform_device *pdev)
713{
Felipe Balbi72246da2011-08-19 18:10:58 +0300714 struct dwc3 *dwc = platform_get_drvdata(pdev);
715 struct resource *res;
Felipe Balbi72246da2011-08-19 18:10:58 +0300716
717 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
718
Felipe Balbi72246da2011-08-19 18:10:58 +0300719 pm_runtime_disable(&pdev->dev);
720
721 dwc3_debugfs_exit(dwc);
722
Felipe Balbi0949e992011-10-12 10:44:56 +0300723 switch (dwc->mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300724 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300725 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300726 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300727 case DWC3_MODE_HOST:
728 dwc3_host_exit(dwc);
729 break;
730 case DWC3_MODE_DRD:
Felipe Balbid07e8812011-10-12 14:08:26 +0300731 dwc3_gadget_exit(dwc);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200732 dwc3_host_exit(dwc);
733 dwc3_otg_exit(dwc);
Felipe Balbid07e8812011-10-12 14:08:26 +0300734 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300735 default:
736 /* do nothing */
737 break;
738 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300739
740 dwc3_core_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300741
742 return 0;
743}
744
Kishon Vijay Abraham Ibdc707a2013-03-18 12:18:57 +0530745#ifdef CONFIG_OF
746static const struct of_device_id of_dwc3_match[] = {
747 {
748 .compatible = "synopsys,dwc3"
749 },
750 { },
751};
752MODULE_DEVICE_TABLE(of, of_dwc3_match);
753#endif
754
Felipe Balbi72246da2011-08-19 18:10:58 +0300755static struct platform_driver dwc3_driver = {
756 .probe = dwc3_probe,
757 .remove = __devexit_p(dwc3_remove),
758 .driver = {
759 .name = "dwc3",
Kishon Vijay Abraham Ibdc707a2013-03-18 12:18:57 +0530760 .of_match_table = of_match_ptr(of_dwc3_match),
Felipe Balbi72246da2011-08-19 18:10:58 +0300761 },
Felipe Balbi72246da2011-08-19 18:10:58 +0300762};
763
Tobias Klauserb1116dc2012-02-28 12:57:20 +0100764module_platform_driver(dwc3_driver);
765
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +0200766MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +0300767MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
768MODULE_LICENSE("Dual BSD/GPL");
769MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");