blob: 98c68844091b2504e3073cf983547951027ab646 [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 Vennapusaba79f6b2013-07-30 13:39:52 +0530156 if (dwc->revision >= DWC3_REVISION_230A)
157 dwc3_notify_event(dwc, DWC3_CONTROLLER_RESET_EVENT);
158
Felipe Balbi72246da2011-08-19 18:10:58 +0300159 /* Assert USB3 PHY reset */
160 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
161 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
162 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
163
164 /* Assert USB2 PHY reset */
165 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
166 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
167 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
168
169 mdelay(100);
170
171 /* Clear USB3 PHY reset */
172 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
173 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
174 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
175
176 /* Clear USB2 PHY reset */
177 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
178 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
179 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
180
Pratyush Anand38a535c2012-06-21 17:44:28 +0530181 mdelay(100);
182
Felipe Balbi72246da2011-08-19 18:10:58 +0300183 /* After PHYs are stable we can take Core out of reset state */
184 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
185 reg &= ~DWC3_GCTL_CORESOFTRESET;
186 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530187
188 if (dwc->revision >= DWC3_REVISION_230A)
189 dwc3_notify_event(dwc, DWC3_CONTROLLER_POST_RESET_EVENT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300190}
191
192/**
193 * dwc3_free_one_event_buffer - Frees one event buffer
194 * @dwc: Pointer to our controller context structure
195 * @evt: Pointer to event buffer to be freed
196 */
197static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
198 struct dwc3_event_buffer *evt)
199{
200 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
201 kfree(evt);
202}
203
204/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800205 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300206 * @dwc: Pointer to our controller context structure
207 * @length: size of the event buffer
208 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800209 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300210 * otherwise ERR_PTR(errno).
211 */
212static struct dwc3_event_buffer *__devinit
213dwc3_alloc_one_event_buffer(struct dwc3 *dwc, unsigned length)
214{
215 struct dwc3_event_buffer *evt;
216
217 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
218 if (!evt)
219 return ERR_PTR(-ENOMEM);
220
221 evt->dwc = dwc;
222 evt->length = length;
223 evt->buf = dma_alloc_coherent(dwc->dev, length,
224 &evt->dma, GFP_KERNEL);
225 if (!evt->buf) {
226 kfree(evt);
227 return ERR_PTR(-ENOMEM);
228 }
229
230 return evt;
231}
232
233/**
234 * dwc3_free_event_buffers - frees all allocated event buffers
235 * @dwc: Pointer to our controller context structure
236 */
237static void dwc3_free_event_buffers(struct dwc3 *dwc)
238{
239 struct dwc3_event_buffer *evt;
240 int i;
241
Felipe Balbi9f622b22011-10-12 10:31:04 +0300242 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300243 evt = dwc->ev_buffs[i];
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900244 if (evt)
Felipe Balbi72246da2011-08-19 18:10:58 +0300245 dwc3_free_one_event_buffer(dwc, evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300246 }
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900247
248 kfree(dwc->ev_buffs);
Felipe Balbi72246da2011-08-19 18:10:58 +0300249}
250
251/**
252 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800253 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300254 * @length: size of event buffer
255 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800256 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300257 * may contain some buffers allocated but not all which were requested.
258 */
Felipe Balbi9f622b22011-10-12 10:31:04 +0300259static int __devinit dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300260{
Felipe Balbi9f622b22011-10-12 10:31:04 +0300261 int num;
Felipe Balbi72246da2011-08-19 18:10:58 +0300262 int i;
263
Felipe Balbi9f622b22011-10-12 10:31:04 +0300264 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
265 dwc->num_event_buffers = num;
266
Felipe Balbi457d3f22011-10-24 12:03:13 +0300267 dwc->ev_buffs = kzalloc(sizeof(*dwc->ev_buffs) * num, GFP_KERNEL);
268 if (!dwc->ev_buffs) {
269 dev_err(dwc->dev, "can't allocate event buffers array\n");
270 return -ENOMEM;
271 }
272
Felipe Balbi72246da2011-08-19 18:10:58 +0300273 for (i = 0; i < num; i++) {
274 struct dwc3_event_buffer *evt;
275
Vijayavardhan Vennapusacf45f022013-05-30 13:39:00 +0530276 /*
277 * As SW workaround, allocate 8 bytes more than size of event
278 * buffer given to USB Controller to avoid possible memory
279 * corruption caused by event buffer overflow when Hw writes
280 * Vendor Device test event which could be of 12 bytes.
281 */
282 evt = dwc3_alloc_one_event_buffer(dwc, (length + 8));
Felipe Balbi72246da2011-08-19 18:10:58 +0300283 if (IS_ERR(evt)) {
284 dev_err(dwc->dev, "can't allocate event buffer\n");
285 return PTR_ERR(evt);
286 }
287 dwc->ev_buffs[i] = evt;
288 }
289
290 return 0;
291}
292
293/**
294 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800295 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300296 *
297 * Returns 0 on success otherwise negative errno.
298 */
Wesley Cheng446ad8d2013-06-05 16:15:01 +0530299int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300300{
301 struct dwc3_event_buffer *evt;
302 int n;
303
Felipe Balbi9f622b22011-10-12 10:31:04 +0300304 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300305 evt = dwc->ev_buffs[n];
306 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
307 evt->buf, (unsigned long long) evt->dma,
308 evt->length);
309
Paul Zimmerman43fa01a2012-04-27 14:28:02 +0300310 evt->lpos = 0;
311
Felipe Balbi72246da2011-08-19 18:10:58 +0300312 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
313 lower_32_bits(evt->dma));
314 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
315 upper_32_bits(evt->dma));
316 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
Vijayavardhan Vennapusacf45f022013-05-30 13:39:00 +0530317 (evt->length - 8) & 0xffff);
Felipe Balbi72246da2011-08-19 18:10:58 +0300318 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
319 }
320
321 return 0;
322}
323
324static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
325{
326 struct dwc3_event_buffer *evt;
327 int n;
328
Felipe Balbi9f622b22011-10-12 10:31:04 +0300329 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300330 evt = dwc->ev_buffs[n];
Paul Zimmerman43fa01a2012-04-27 14:28:02 +0300331
332 evt->lpos = 0;
333
Felipe Balbi72246da2011-08-19 18:10:58 +0300334 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
335 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
336 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
337 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
338 }
339}
340
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530341static void dwc3_cache_hwparams(struct dwc3 *dwc)
Felipe Balbi26ceca92011-09-30 10:58:49 +0300342{
343 struct dwc3_hwparams *parms = &dwc->hwparams;
344
345 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
346 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
347 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
348 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
349 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
350 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
351 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
352 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
353 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
354}
355
Felipe Balbi72246da2011-08-19 18:10:58 +0300356/**
357 * dwc3_core_init - Low-level initialization of DWC3 Core
358 * @dwc: Pointer to our controller context structure
359 *
360 * Returns 0 on success otherwise negative errno.
361 */
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530362static int dwc3_core_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300363{
364 unsigned long timeout;
365 u32 reg;
366 int ret;
367
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200368 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
369 /* This should read as U3 followed by revision number */
370 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
371 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
372 ret = -ENODEV;
373 goto err0;
374 }
Felipe Balbi248b1222011-12-14 21:59:30 +0200375 dwc->revision = reg;
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200376
Felipe Balbi72246da2011-08-19 18:10:58 +0300377 /* issue device SoftReset too */
378 timeout = jiffies + msecs_to_jiffies(500);
379 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
380 do {
381 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
382 if (!(reg & DWC3_DCTL_CSFTRST))
383 break;
384
385 if (time_after(jiffies, timeout)) {
386 dev_err(dwc->dev, "Reset Timed Out\n");
387 ret = -ETIMEDOUT;
388 goto err0;
389 }
390
391 cpu_relax();
392 } while (true);
393
Pratyush Anand99d4da82012-06-21 17:44:29 +0530394 dwc3_core_soft_reset(dwc);
395
Felipe Balbi9f622b22011-10-12 10:31:04 +0300396 dwc3_cache_hwparams(dwc);
397
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100398 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800399 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100400 reg &= ~DWC3_GCTL_DISSCRAMBLE;
401
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100402 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100403 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
404 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
405 break;
406 default:
407 dev_dbg(dwc->dev, "No power optimization available\n");
408 }
409
410 /*
411 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800412 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100413 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800414 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100415 */
416 if (dwc->revision < DWC3_REVISION_190A)
417 reg |= DWC3_GCTL_U2RSTECN;
418
419 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
420
Pavankumar Kondetife2c0632012-06-12 15:21:13 +0530421 /*
Pavankumar Kondetic6e15aa2012-07-16 11:37:15 +0530422 * The default value of GUCTL[31:22] should be 0x8. But on cores
423 * revision < 2.30a, the default value is mistakenly overridden
424 * with 0x0. Restore the correct default value.
425 */
426 if (dwc->revision < DWC3_REVISION_230A) {
427 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
428 reg &= ~DWC3_GUCTL_REFCLKPER;
429 reg |= 0x8 << __ffs(DWC3_GUCTL_REFCLKPER);
430 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
431 }
432 /*
Pavankumar Kondetife2c0632012-06-12 15:21:13 +0530433 * Currently, the default and the recommended value for GUSB3PIPECTL
434 * [21:19] in the RTL is 3'b100 or 32 consecutive errors. Based on
435 * analysis and experiments in the lab, it is found that there is a
436 * relatively low probability of getting 32 consecutive word errors
437 * in the presence of random recovered noise (during electrical idle).
438 * This can delay the entry to a low power state such that for
439 * applications where the link stays in a non-U0 state for a short
440 * duration (< 1 microsecond), the local PHY does not enter the low
441 * power state prior to receiving a potential LFPS wakeup. This causes
442 * the PHY CDR (Clock and Data Recovery) operation to be unstable for
443 * some Synopsys PHYs.
444 *
445 * The proposal now is to change the default and the recommended value
446 * for GUSB3PIPECTL[21:19] in the RTL from 3'b100 to a minimum of
447 * 3'b001. Perform the same in software for controllers prior to 2.30a
448 * revision.
449 */
450
451 if (dwc->revision < DWC3_REVISION_230A) {
452 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
453 reg &= ~DWC3_GUSB3PIPECTL_DELAY_P1P2P3;
454 reg |= 1 << __ffs(DWC3_GUSB3PIPECTL_DELAY_P1P2P3);
Pavankumar Kondeti5acb4ba2012-07-16 11:44:46 +0530455 /*
456 * Receiver Detection in U3/Rx.Det is mistakenly disabled in
457 * cores < 2.30a. Fix it here.
458 */
459 reg &= ~DWC3_GUSB3PIPECTL_DIS_RXDET_U3_RXDET;
Pavankumar Kondetife2c0632012-06-12 15:21:13 +0530460 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
461 }
Vijayavardhan Vennapusad0136a72013-06-07 13:22:18 +0530462 /*
463 * clear Elastic buffer mode in GUSBPIPE_CTRL(0) register, otherwise
464 * it results in high link errors and could cause SS mode transfer
465 * failure.
466 */
467 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
468 reg &= ~DWC3_GUSB3PIPECTL_ELASTIC_BUF_MODE;
469 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
Pavankumar Kondetife2c0632012-06-12 15:21:13 +0530470
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530471 if (!dwc->ev_buffs) {
472 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
473 if (ret) {
474 dev_err(dwc->dev, "failed to allocate event buffers\n");
475 ret = -ENOMEM;
476 goto err1;
477 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300478 }
479
480 ret = dwc3_event_buffers_setup(dwc);
481 if (ret) {
482 dev_err(dwc->dev, "failed to setup event buffers\n");
483 goto err1;
484 }
485
Felipe Balbi72246da2011-08-19 18:10:58 +0300486 return 0;
487
488err1:
489 dwc3_free_event_buffers(dwc);
490
491err0:
492 return ret;
493}
494
495static void dwc3_core_exit(struct dwc3 *dwc)
496{
497 dwc3_event_buffers_cleanup(dwc);
498 dwc3_free_event_buffers(dwc);
499}
500
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530501/* XHCI reset, resets other CORE registers as well, re-init those */
502void dwc3_post_host_reset_core_init(struct dwc3 *dwc)
503{
504 dwc3_core_init(dwc);
505 dwc3_gadget_restart(dwc);
506}
507
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +0530508static void (*notify_event) (struct dwc3 *, unsigned);
509void dwc3_set_notifier(void (*notify)(struct dwc3 *, unsigned))
510{
511 notify_event = notify;
512}
513EXPORT_SYMBOL(dwc3_set_notifier);
514
515void dwc3_notify_event(struct dwc3 *dwc, unsigned event)
516{
517 if (dwc->notify_event)
518 dwc->notify_event(dwc, event);
519}
520EXPORT_SYMBOL(dwc3_notify_event);
521
Felipe Balbi72246da2011-08-19 18:10:58 +0300522#define DWC3_ALIGN_MASK (16 - 1)
523
Vijayavardhan Vennapusa8eb68732013-03-26 13:05:38 +0530524static u64 dwc3_dma_mask = DMA_BIT_MASK(64);
Felipe Balbi72246da2011-08-19 18:10:58 +0300525static int __devinit dwc3_probe(struct platform_device *pdev)
526{
Felipe Balbi457e84b2012-01-18 18:04:09 +0200527 struct device_node *node = pdev->dev.of_node;
Felipe Balbi72246da2011-08-19 18:10:58 +0300528 struct resource *res;
529 struct dwc3 *dwc;
Chanho Park802ca852012-02-15 18:27:55 +0900530 struct device *dev = &pdev->dev;
Felipe Balbi0949e992011-10-12 10:44:56 +0300531
Felipe Balbi72246da2011-08-19 18:10:58 +0300532 int ret = -ENOMEM;
Felipe Balbi0949e992011-10-12 10:44:56 +0300533
534 void __iomem *regs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300535 void *mem;
536
Felipe Balbi0949e992011-10-12 10:44:56 +0300537 u8 mode;
Manu Gautambb825d72013-03-12 16:25:42 +0530538 bool host_only_mode;
Felipe Balbi0949e992011-10-12 10:44:56 +0300539
Chanho Park802ca852012-02-15 18:27:55 +0900540 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300541 if (!mem) {
Chanho Park802ca852012-02-15 18:27:55 +0900542 dev_err(dev, "not enough memory\n");
543 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300544 }
545 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
546 dwc->mem = mem;
547
Vijayavardhan Vennapusa8eb68732013-03-26 13:05:38 +0530548 if (!dev->dma_mask)
549 dev->dma_mask = &dwc3_dma_mask;
550 if (!dev->coherent_dma_mask)
Hemant Kumar1b378d92013-04-19 11:24:05 -0700551 dev->coherent_dma_mask = DMA_BIT_MASK(64);
Vijayavardhan Vennapusa8eb68732013-03-26 13:05:38 +0530552
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +0530553 dwc->notify_event = notify_event;
Ido Shayevitz4a187332012-04-23 14:53:37 +0200554 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300555 if (!res) {
Ido Shayevitz4a187332012-04-23 14:53:37 +0200556 dev_err(dev, "missing IRQ\n");
Chanho Park802ca852012-02-15 18:27:55 +0900557 return -ENODEV;
Felipe Balbi72246da2011-08-19 18:10:58 +0300558 }
Kishon Vijay Abraham I2f8ae022012-08-21 14:56:16 +0530559 dwc->xhci_resources[1].start = res->start;
560 dwc->xhci_resources[1].end = res->end;
561 dwc->xhci_resources[1].flags = res->flags;
562 dwc->xhci_resources[1].name = res->name;
Felipe Balbi72246da2011-08-19 18:10:58 +0300563
Ido Shayevitz4a187332012-04-23 14:53:37 +0200564 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
565 if (!res) {
566 dev_err(dev, "missing memory resource\n");
567 return -ENODEV;
568 }
Kishon Vijay Abraham I2f8ae022012-08-21 14:56:16 +0530569 dwc->xhci_resources[0].start = res->start;
Ido Shayevitz4a187332012-04-23 14:53:37 +0200570 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
571 DWC3_XHCI_REGS_END;
Kishon Vijay Abraham I2f8ae022012-08-21 14:56:16 +0530572 dwc->xhci_resources[0].flags = res->flags;
573 dwc->xhci_resources[0].name = res->name;
Felipe Balbid07e8812011-10-12 14:08:26 +0300574
Ido Shayevitz4a187332012-04-23 14:53:37 +0200575 /*
576 * Request memory region but exclude xHCI regs,
577 * since it will be requested by the xhci-plat driver.
578 */
579 res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START,
580 resource_size(res) - DWC3_GLOBALS_REGS_START,
Chanho Park802ca852012-02-15 18:27:55 +0900581 dev_name(dev));
Ido Shayevitz4a187332012-04-23 14:53:37 +0200582
Felipe Balbi72246da2011-08-19 18:10:58 +0300583 if (!res) {
Chanho Park802ca852012-02-15 18:27:55 +0900584 dev_err(dev, "can't request mem region\n");
585 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300586 }
587
Felipe Balbi497a2a32012-08-10 09:16:43 +0300588 regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
Felipe Balbi72246da2011-08-19 18:10:58 +0300589 if (!regs) {
Chanho Park802ca852012-02-15 18:27:55 +0900590 dev_err(dev, "ioremap failed\n");
591 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300592 }
593
Felipe Balbi72246da2011-08-19 18:10:58 +0300594 spin_lock_init(&dwc->lock);
595 platform_set_drvdata(pdev, dwc);
596
597 dwc->regs = regs;
598 dwc->regs_size = resource_size(res);
Chanho Park802ca852012-02-15 18:27:55 +0900599 dwc->dev = dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300600
Felipe Balbi6c167fc2011-10-07 22:55:04 +0300601 if (!strncmp("super", maximum_speed, 5))
602 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
603 else if (!strncmp("high", maximum_speed, 4))
604 dwc->maximum_speed = DWC3_DCFG_HIGHSPEED;
605 else if (!strncmp("full", maximum_speed, 4))
606 dwc->maximum_speed = DWC3_DCFG_FULLSPEED1;
607 else if (!strncmp("low", maximum_speed, 3))
608 dwc->maximum_speed = DWC3_DCFG_LOWSPEED;
609 else
610 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
611
Kishon Vijay Abraham Ibdc707a2013-03-18 12:18:57 +0530612 dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize");
Manu Gautambb825d72013-03-12 16:25:42 +0530613 host_only_mode = of_property_read_bool(node, "host-only-mode");
Felipe Balbi457e84b2012-01-18 18:04:09 +0200614
Manu Gautamb5067272012-07-02 09:53:41 +0530615 pm_runtime_no_callbacks(dev);
616 pm_runtime_set_active(dev);
Chanho Park802ca852012-02-15 18:27:55 +0900617 pm_runtime_enable(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300618
619 ret = dwc3_core_init(dwc);
620 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900621 dev_err(dev, "failed to initialize core\n");
622 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300623 }
624
Felipe Balbi0949e992011-10-12 10:44:56 +0300625 mode = DWC3_MODE(dwc->hwparams.hwparams0);
626
Manu Gautambb825d72013-03-12 16:25:42 +0530627 /* Override mode if user selects host-only config with DRD core */
628 if (host_only_mode && (mode == DWC3_MODE_DRD)) {
629 dev_dbg(dev, "host only mode selected\n");
630 mode = DWC3_MODE_HOST;
631 }
632
Felipe Balbi0949e992011-10-12 10:44:56 +0300633 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300634 case DWC3_MODE_DEVICE:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100635 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300636 ret = dwc3_gadget_init(dwc);
637 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900638 dev_err(dev, "failed to initialize gadget\n");
639 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300640 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300641 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300642 case DWC3_MODE_HOST:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100643 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
Felipe Balbid07e8812011-10-12 14:08:26 +0300644 ret = dwc3_host_init(dwc);
645 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900646 dev_err(dev, "failed to initialize host\n");
647 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300648 }
649 break;
650 case DWC3_MODE_DRD:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100651 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200652 ret = dwc3_otg_init(dwc);
653 if (ret) {
654 dev_err(dev, "failed to initialize otg\n");
655 goto err1;
656 }
657
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530658 ret = dwc3_host_init(dwc);
659 if (ret) {
660 dev_err(dev, "failed to initialize host\n");
661 dwc3_otg_exit(dwc);
662 goto err1;
663 }
664
Felipe Balbid07e8812011-10-12 14:08:26 +0300665 ret = dwc3_gadget_init(dwc);
666 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900667 dev_err(dev, "failed to initialize gadget\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200668 dwc3_host_exit(dwc);
669 dwc3_otg_exit(dwc);
Chanho Park802ca852012-02-15 18:27:55 +0900670 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300671 }
672 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300673 default:
Chanho Park802ca852012-02-15 18:27:55 +0900674 dev_err(dev, "Unsupported mode of operation %d\n", mode);
675 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300676 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300677 dwc->mode = mode;
Felipe Balbi72246da2011-08-19 18:10:58 +0300678
679 ret = dwc3_debugfs_init(dwc);
680 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900681 dev_err(dev, "failed to initialize debugfs\n");
682 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +0300683 }
684
Felipe Balbi72246da2011-08-19 18:10:58 +0300685 return 0;
686
Chanho Park802ca852012-02-15 18:27:55 +0900687err2:
Felipe Balbi0949e992011-10-12 10:44:56 +0300688 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300689 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300690 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300691 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300692 case DWC3_MODE_HOST:
693 dwc3_host_exit(dwc);
694 break;
695 case DWC3_MODE_DRD:
Felipe Balbid07e8812011-10-12 14:08:26 +0300696 dwc3_gadget_exit(dwc);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200697 dwc3_host_exit(dwc);
698 dwc3_otg_exit(dwc);
Felipe Balbid07e8812011-10-12 14:08:26 +0300699 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300700 default:
701 /* do nothing */
702 break;
703 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300704
Chanho Park802ca852012-02-15 18:27:55 +0900705err1:
Felipe Balbi72246da2011-08-19 18:10:58 +0300706 dwc3_core_exit(dwc);
707
Felipe Balbi72246da2011-08-19 18:10:58 +0300708 return ret;
709}
710
711static int __devexit dwc3_remove(struct platform_device *pdev)
712{
Felipe Balbi72246da2011-08-19 18:10:58 +0300713 struct dwc3 *dwc = platform_get_drvdata(pdev);
714 struct resource *res;
Felipe Balbi72246da2011-08-19 18:10:58 +0300715
716 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
717
Felipe Balbi72246da2011-08-19 18:10:58 +0300718 pm_runtime_disable(&pdev->dev);
719
720 dwc3_debugfs_exit(dwc);
721
Felipe Balbi0949e992011-10-12 10:44:56 +0300722 switch (dwc->mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300723 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300724 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300725 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300726 case DWC3_MODE_HOST:
727 dwc3_host_exit(dwc);
728 break;
729 case DWC3_MODE_DRD:
Felipe Balbid07e8812011-10-12 14:08:26 +0300730 dwc3_gadget_exit(dwc);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200731 dwc3_host_exit(dwc);
732 dwc3_otg_exit(dwc);
Felipe Balbid07e8812011-10-12 14:08:26 +0300733 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300734 default:
735 /* do nothing */
736 break;
737 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300738
739 dwc3_core_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300740
741 return 0;
742}
743
Kishon Vijay Abraham Ibdc707a2013-03-18 12:18:57 +0530744#ifdef CONFIG_OF
745static const struct of_device_id of_dwc3_match[] = {
746 {
747 .compatible = "synopsys,dwc3"
748 },
749 { },
750};
751MODULE_DEVICE_TABLE(of, of_dwc3_match);
752#endif
753
Felipe Balbi72246da2011-08-19 18:10:58 +0300754static struct platform_driver dwc3_driver = {
755 .probe = dwc3_probe,
756 .remove = __devexit_p(dwc3_remove),
757 .driver = {
758 .name = "dwc3",
Kishon Vijay Abraham Ibdc707a2013-03-18 12:18:57 +0530759 .of_match_table = of_match_ptr(of_dwc3_match),
Felipe Balbi72246da2011-08-19 18:10:58 +0300760 },
Felipe Balbi72246da2011-08-19 18:10:58 +0300761};
762
Tobias Klauserb1116dc2012-02-28 12:57:20 +0100763module_platform_driver(dwc3_driver);
764
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +0200765MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +0300766MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
767MODULE_LICENSE("Dual BSD/GPL");
768MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");