blob: 04da6f80375a7a7c2c5457a0f06bdcc8a2d63ac0 [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);
114 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
115}
Felipe Balbi8300dd22011-10-18 13:54:01 +0300116
Felipe Balbi72246da2011-08-19 18:10:58 +0300117/**
118 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
119 * @dwc: pointer to our context structure
120 */
121static void dwc3_core_soft_reset(struct dwc3 *dwc)
122{
123 u32 reg;
124
125 /* Before Resetting PHY, put Core in Reset */
126 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
127 reg |= DWC3_GCTL_CORESOFTRESET;
128 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
129
130 /* Assert USB3 PHY reset */
131 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
132 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
133 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
134
135 /* Assert USB2 PHY reset */
136 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
137 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
138 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
139
140 mdelay(100);
141
142 /* Clear USB3 PHY reset */
143 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
144 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
145 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
146
147 /* Clear USB2 PHY reset */
148 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
149 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
150 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
151
Pratyush Anand38a535c2012-06-21 17:44:28 +0530152 mdelay(100);
153
Felipe Balbi72246da2011-08-19 18:10:58 +0300154 /* After PHYs are stable we can take Core out of reset state */
155 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
156 reg &= ~DWC3_GCTL_CORESOFTRESET;
157 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
158}
159
160/**
161 * dwc3_free_one_event_buffer - Frees one event buffer
162 * @dwc: Pointer to our controller context structure
163 * @evt: Pointer to event buffer to be freed
164 */
165static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
166 struct dwc3_event_buffer *evt)
167{
168 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
169 kfree(evt);
170}
171
172/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800173 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300174 * @dwc: Pointer to our controller context structure
175 * @length: size of the event buffer
176 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800177 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300178 * otherwise ERR_PTR(errno).
179 */
180static struct dwc3_event_buffer *__devinit
181dwc3_alloc_one_event_buffer(struct dwc3 *dwc, unsigned length)
182{
183 struct dwc3_event_buffer *evt;
184
185 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
186 if (!evt)
187 return ERR_PTR(-ENOMEM);
188
189 evt->dwc = dwc;
190 evt->length = length;
191 evt->buf = dma_alloc_coherent(dwc->dev, length,
192 &evt->dma, GFP_KERNEL);
193 if (!evt->buf) {
194 kfree(evt);
195 return ERR_PTR(-ENOMEM);
196 }
197
198 return evt;
199}
200
201/**
202 * dwc3_free_event_buffers - frees all allocated event buffers
203 * @dwc: Pointer to our controller context structure
204 */
205static void dwc3_free_event_buffers(struct dwc3 *dwc)
206{
207 struct dwc3_event_buffer *evt;
208 int i;
209
Felipe Balbi9f622b22011-10-12 10:31:04 +0300210 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300211 evt = dwc->ev_buffs[i];
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900212 if (evt)
Felipe Balbi72246da2011-08-19 18:10:58 +0300213 dwc3_free_one_event_buffer(dwc, evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300214 }
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900215
216 kfree(dwc->ev_buffs);
Felipe Balbi72246da2011-08-19 18:10:58 +0300217}
218
219/**
220 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800221 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300222 * @length: size of event buffer
223 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800224 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300225 * may contain some buffers allocated but not all which were requested.
226 */
Felipe Balbi9f622b22011-10-12 10:31:04 +0300227static int __devinit dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300228{
Felipe Balbi9f622b22011-10-12 10:31:04 +0300229 int num;
Felipe Balbi72246da2011-08-19 18:10:58 +0300230 int i;
231
Felipe Balbi9f622b22011-10-12 10:31:04 +0300232 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
233 dwc->num_event_buffers = num;
234
Felipe Balbi457d3f22011-10-24 12:03:13 +0300235 dwc->ev_buffs = kzalloc(sizeof(*dwc->ev_buffs) * num, GFP_KERNEL);
236 if (!dwc->ev_buffs) {
237 dev_err(dwc->dev, "can't allocate event buffers array\n");
238 return -ENOMEM;
239 }
240
Felipe Balbi72246da2011-08-19 18:10:58 +0300241 for (i = 0; i < num; i++) {
242 struct dwc3_event_buffer *evt;
243
Vijayavardhan Vennapusacf45f022013-05-30 13:39:00 +0530244 /*
245 * As SW workaround, allocate 8 bytes more than size of event
246 * buffer given to USB Controller to avoid possible memory
247 * corruption caused by event buffer overflow when Hw writes
248 * Vendor Device test event which could be of 12 bytes.
249 */
250 evt = dwc3_alloc_one_event_buffer(dwc, (length + 8));
Felipe Balbi72246da2011-08-19 18:10:58 +0300251 if (IS_ERR(evt)) {
252 dev_err(dwc->dev, "can't allocate event buffer\n");
253 return PTR_ERR(evt);
254 }
255 dwc->ev_buffs[i] = evt;
256 }
257
258 return 0;
259}
260
261/**
262 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800263 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300264 *
265 * Returns 0 on success otherwise negative errno.
266 */
Paul Zimmerman43fa01a2012-04-27 14:28:02 +0300267static int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300268{
269 struct dwc3_event_buffer *evt;
270 int n;
271
Felipe Balbi9f622b22011-10-12 10:31:04 +0300272 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300273 evt = dwc->ev_buffs[n];
274 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
275 evt->buf, (unsigned long long) evt->dma,
276 evt->length);
277
Paul Zimmerman43fa01a2012-04-27 14:28:02 +0300278 evt->lpos = 0;
279
Felipe Balbi72246da2011-08-19 18:10:58 +0300280 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
281 lower_32_bits(evt->dma));
282 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
283 upper_32_bits(evt->dma));
284 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
Vijayavardhan Vennapusacf45f022013-05-30 13:39:00 +0530285 (evt->length - 8) & 0xffff);
Felipe Balbi72246da2011-08-19 18:10:58 +0300286 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
287 }
288
289 return 0;
290}
291
292static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
293{
294 struct dwc3_event_buffer *evt;
295 int n;
296
Felipe Balbi9f622b22011-10-12 10:31:04 +0300297 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300298 evt = dwc->ev_buffs[n];
Paul Zimmerman43fa01a2012-04-27 14:28:02 +0300299
300 evt->lpos = 0;
301
Felipe Balbi72246da2011-08-19 18:10:58 +0300302 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
303 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
304 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
305 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
306 }
307}
308
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530309static void dwc3_cache_hwparams(struct dwc3 *dwc)
Felipe Balbi26ceca92011-09-30 10:58:49 +0300310{
311 struct dwc3_hwparams *parms = &dwc->hwparams;
312
313 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
314 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
315 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
316 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
317 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
318 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
319 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
320 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
321 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
322}
323
Felipe Balbi72246da2011-08-19 18:10:58 +0300324/**
325 * dwc3_core_init - Low-level initialization of DWC3 Core
326 * @dwc: Pointer to our controller context structure
327 *
328 * Returns 0 on success otherwise negative errno.
329 */
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530330static int dwc3_core_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300331{
332 unsigned long timeout;
333 u32 reg;
334 int ret;
335
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200336 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
337 /* This should read as U3 followed by revision number */
338 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
339 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
340 ret = -ENODEV;
341 goto err0;
342 }
Felipe Balbi248b1222011-12-14 21:59:30 +0200343 dwc->revision = reg;
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200344
Felipe Balbi72246da2011-08-19 18:10:58 +0300345 /* issue device SoftReset too */
346 timeout = jiffies + msecs_to_jiffies(500);
347 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
348 do {
349 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
350 if (!(reg & DWC3_DCTL_CSFTRST))
351 break;
352
353 if (time_after(jiffies, timeout)) {
354 dev_err(dwc->dev, "Reset Timed Out\n");
355 ret = -ETIMEDOUT;
356 goto err0;
357 }
358
359 cpu_relax();
360 } while (true);
361
Pratyush Anand99d4da82012-06-21 17:44:29 +0530362 dwc3_core_soft_reset(dwc);
363
Felipe Balbi9f622b22011-10-12 10:31:04 +0300364 dwc3_cache_hwparams(dwc);
365
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100366 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800367 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100368 reg &= ~DWC3_GCTL_DISSCRAMBLE;
369
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100370 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100371 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
372 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
373 break;
374 default:
375 dev_dbg(dwc->dev, "No power optimization available\n");
376 }
377
378 /*
379 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800380 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100381 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800382 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100383 */
384 if (dwc->revision < DWC3_REVISION_190A)
385 reg |= DWC3_GCTL_U2RSTECN;
386
387 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
388
Pavankumar Kondetife2c0632012-06-12 15:21:13 +0530389 /*
Pavankumar Kondetic6e15aa2012-07-16 11:37:15 +0530390 * The default value of GUCTL[31:22] should be 0x8. But on cores
391 * revision < 2.30a, the default value is mistakenly overridden
392 * with 0x0. Restore the correct default value.
393 */
394 if (dwc->revision < DWC3_REVISION_230A) {
395 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
396 reg &= ~DWC3_GUCTL_REFCLKPER;
397 reg |= 0x8 << __ffs(DWC3_GUCTL_REFCLKPER);
398 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
399 }
400 /*
Pavankumar Kondetife2c0632012-06-12 15:21:13 +0530401 * Currently, the default and the recommended value for GUSB3PIPECTL
402 * [21:19] in the RTL is 3'b100 or 32 consecutive errors. Based on
403 * analysis and experiments in the lab, it is found that there is a
404 * relatively low probability of getting 32 consecutive word errors
405 * in the presence of random recovered noise (during electrical idle).
406 * This can delay the entry to a low power state such that for
407 * applications where the link stays in a non-U0 state for a short
408 * duration (< 1 microsecond), the local PHY does not enter the low
409 * power state prior to receiving a potential LFPS wakeup. This causes
410 * the PHY CDR (Clock and Data Recovery) operation to be unstable for
411 * some Synopsys PHYs.
412 *
413 * The proposal now is to change the default and the recommended value
414 * for GUSB3PIPECTL[21:19] in the RTL from 3'b100 to a minimum of
415 * 3'b001. Perform the same in software for controllers prior to 2.30a
416 * revision.
417 */
418
419 if (dwc->revision < DWC3_REVISION_230A) {
420 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
421 reg &= ~DWC3_GUSB3PIPECTL_DELAY_P1P2P3;
422 reg |= 1 << __ffs(DWC3_GUSB3PIPECTL_DELAY_P1P2P3);
Pavankumar Kondeti5acb4ba2012-07-16 11:44:46 +0530423 /*
424 * Receiver Detection in U3/Rx.Det is mistakenly disabled in
425 * cores < 2.30a. Fix it here.
426 */
427 reg &= ~DWC3_GUSB3PIPECTL_DIS_RXDET_U3_RXDET;
Pavankumar Kondetife2c0632012-06-12 15:21:13 +0530428 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
429 }
430
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530431 if (!dwc->ev_buffs) {
432 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
433 if (ret) {
434 dev_err(dwc->dev, "failed to allocate event buffers\n");
435 ret = -ENOMEM;
436 goto err1;
437 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300438 }
439
440 ret = dwc3_event_buffers_setup(dwc);
441 if (ret) {
442 dev_err(dwc->dev, "failed to setup event buffers\n");
443 goto err1;
444 }
445
Felipe Balbi72246da2011-08-19 18:10:58 +0300446 return 0;
447
448err1:
449 dwc3_free_event_buffers(dwc);
450
451err0:
452 return ret;
453}
454
455static void dwc3_core_exit(struct dwc3 *dwc)
456{
457 dwc3_event_buffers_cleanup(dwc);
458 dwc3_free_event_buffers(dwc);
459}
460
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530461/* XHCI reset, resets other CORE registers as well, re-init those */
462void dwc3_post_host_reset_core_init(struct dwc3 *dwc)
463{
464 dwc3_core_init(dwc);
465 dwc3_gadget_restart(dwc);
466}
467
Felipe Balbi72246da2011-08-19 18:10:58 +0300468#define DWC3_ALIGN_MASK (16 - 1)
469
Vijayavardhan Vennapusa8eb68732013-03-26 13:05:38 +0530470static u64 dwc3_dma_mask = DMA_BIT_MASK(64);
Felipe Balbi72246da2011-08-19 18:10:58 +0300471static int __devinit dwc3_probe(struct platform_device *pdev)
472{
Felipe Balbi457e84b2012-01-18 18:04:09 +0200473 struct device_node *node = pdev->dev.of_node;
Felipe Balbi72246da2011-08-19 18:10:58 +0300474 struct resource *res;
475 struct dwc3 *dwc;
Chanho Park802ca852012-02-15 18:27:55 +0900476 struct device *dev = &pdev->dev;
Felipe Balbi0949e992011-10-12 10:44:56 +0300477
Felipe Balbi72246da2011-08-19 18:10:58 +0300478 int ret = -ENOMEM;
Felipe Balbi0949e992011-10-12 10:44:56 +0300479
480 void __iomem *regs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300481 void *mem;
482
Felipe Balbi0949e992011-10-12 10:44:56 +0300483 u8 mode;
Manu Gautambb825d72013-03-12 16:25:42 +0530484 bool host_only_mode;
Felipe Balbi0949e992011-10-12 10:44:56 +0300485
Chanho Park802ca852012-02-15 18:27:55 +0900486 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300487 if (!mem) {
Chanho Park802ca852012-02-15 18:27:55 +0900488 dev_err(dev, "not enough memory\n");
489 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300490 }
491 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
492 dwc->mem = mem;
493
Vijayavardhan Vennapusa8eb68732013-03-26 13:05:38 +0530494 if (!dev->dma_mask)
495 dev->dma_mask = &dwc3_dma_mask;
496 if (!dev->coherent_dma_mask)
Hemant Kumar1b378d92013-04-19 11:24:05 -0700497 dev->coherent_dma_mask = DMA_BIT_MASK(64);
Vijayavardhan Vennapusa8eb68732013-03-26 13:05:38 +0530498
Ido Shayevitz4a187332012-04-23 14:53:37 +0200499 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300500 if (!res) {
Ido Shayevitz4a187332012-04-23 14:53:37 +0200501 dev_err(dev, "missing IRQ\n");
Chanho Park802ca852012-02-15 18:27:55 +0900502 return -ENODEV;
Felipe Balbi72246da2011-08-19 18:10:58 +0300503 }
Kishon Vijay Abraham I2f8ae022012-08-21 14:56:16 +0530504 dwc->xhci_resources[1].start = res->start;
505 dwc->xhci_resources[1].end = res->end;
506 dwc->xhci_resources[1].flags = res->flags;
507 dwc->xhci_resources[1].name = res->name;
Felipe Balbi72246da2011-08-19 18:10:58 +0300508
Ido Shayevitz4a187332012-04-23 14:53:37 +0200509 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
510 if (!res) {
511 dev_err(dev, "missing memory resource\n");
512 return -ENODEV;
513 }
Kishon Vijay Abraham I2f8ae022012-08-21 14:56:16 +0530514 dwc->xhci_resources[0].start = res->start;
Ido Shayevitz4a187332012-04-23 14:53:37 +0200515 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
516 DWC3_XHCI_REGS_END;
Kishon Vijay Abraham I2f8ae022012-08-21 14:56:16 +0530517 dwc->xhci_resources[0].flags = res->flags;
518 dwc->xhci_resources[0].name = res->name;
Felipe Balbid07e8812011-10-12 14:08:26 +0300519
Ido Shayevitz4a187332012-04-23 14:53:37 +0200520 /*
521 * Request memory region but exclude xHCI regs,
522 * since it will be requested by the xhci-plat driver.
523 */
524 res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START,
525 resource_size(res) - DWC3_GLOBALS_REGS_START,
Chanho Park802ca852012-02-15 18:27:55 +0900526 dev_name(dev));
Ido Shayevitz4a187332012-04-23 14:53:37 +0200527
Felipe Balbi72246da2011-08-19 18:10:58 +0300528 if (!res) {
Chanho Park802ca852012-02-15 18:27:55 +0900529 dev_err(dev, "can't request mem region\n");
530 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300531 }
532
Felipe Balbi497a2a32012-08-10 09:16:43 +0300533 regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
Felipe Balbi72246da2011-08-19 18:10:58 +0300534 if (!regs) {
Chanho Park802ca852012-02-15 18:27:55 +0900535 dev_err(dev, "ioremap failed\n");
536 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300537 }
538
Felipe Balbi72246da2011-08-19 18:10:58 +0300539 spin_lock_init(&dwc->lock);
540 platform_set_drvdata(pdev, dwc);
541
542 dwc->regs = regs;
543 dwc->regs_size = resource_size(res);
Chanho Park802ca852012-02-15 18:27:55 +0900544 dwc->dev = dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300545
Felipe Balbi6c167fc2011-10-07 22:55:04 +0300546 if (!strncmp("super", maximum_speed, 5))
547 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
548 else if (!strncmp("high", maximum_speed, 4))
549 dwc->maximum_speed = DWC3_DCFG_HIGHSPEED;
550 else if (!strncmp("full", maximum_speed, 4))
551 dwc->maximum_speed = DWC3_DCFG_FULLSPEED1;
552 else if (!strncmp("low", maximum_speed, 3))
553 dwc->maximum_speed = DWC3_DCFG_LOWSPEED;
554 else
555 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
556
Kishon Vijay Abraham Ibdc707a2013-03-18 12:18:57 +0530557 dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize");
Manu Gautambb825d72013-03-12 16:25:42 +0530558 host_only_mode = of_property_read_bool(node, "host-only-mode");
Felipe Balbi457e84b2012-01-18 18:04:09 +0200559
Manu Gautamb5067272012-07-02 09:53:41 +0530560 pm_runtime_no_callbacks(dev);
561 pm_runtime_set_active(dev);
Chanho Park802ca852012-02-15 18:27:55 +0900562 pm_runtime_enable(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300563
564 ret = dwc3_core_init(dwc);
565 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900566 dev_err(dev, "failed to initialize core\n");
567 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300568 }
569
Felipe Balbi0949e992011-10-12 10:44:56 +0300570 mode = DWC3_MODE(dwc->hwparams.hwparams0);
571
Manu Gautambb825d72013-03-12 16:25:42 +0530572 /* Override mode if user selects host-only config with DRD core */
573 if (host_only_mode && (mode == DWC3_MODE_DRD)) {
574 dev_dbg(dev, "host only mode selected\n");
575 mode = DWC3_MODE_HOST;
576 }
577
Felipe Balbi0949e992011-10-12 10:44:56 +0300578 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300579 case DWC3_MODE_DEVICE:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100580 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300581 ret = dwc3_gadget_init(dwc);
582 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900583 dev_err(dev, "failed to initialize gadget\n");
584 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300585 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300586 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300587 case DWC3_MODE_HOST:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100588 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
Felipe Balbid07e8812011-10-12 14:08:26 +0300589 ret = dwc3_host_init(dwc);
590 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900591 dev_err(dev, "failed to initialize host\n");
592 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300593 }
594 break;
595 case DWC3_MODE_DRD:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100596 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200597 ret = dwc3_otg_init(dwc);
598 if (ret) {
599 dev_err(dev, "failed to initialize otg\n");
600 goto err1;
601 }
602
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530603 ret = dwc3_host_init(dwc);
604 if (ret) {
605 dev_err(dev, "failed to initialize host\n");
606 dwc3_otg_exit(dwc);
607 goto err1;
608 }
609
Felipe Balbid07e8812011-10-12 14:08:26 +0300610 ret = dwc3_gadget_init(dwc);
611 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900612 dev_err(dev, "failed to initialize gadget\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200613 dwc3_host_exit(dwc);
614 dwc3_otg_exit(dwc);
Chanho Park802ca852012-02-15 18:27:55 +0900615 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300616 }
617 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300618 default:
Chanho Park802ca852012-02-15 18:27:55 +0900619 dev_err(dev, "Unsupported mode of operation %d\n", mode);
620 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300621 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300622 dwc->mode = mode;
Felipe Balbi72246da2011-08-19 18:10:58 +0300623
624 ret = dwc3_debugfs_init(dwc);
625 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900626 dev_err(dev, "failed to initialize debugfs\n");
627 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +0300628 }
629
Felipe Balbi72246da2011-08-19 18:10:58 +0300630 return 0;
631
Chanho Park802ca852012-02-15 18:27:55 +0900632err2:
Felipe Balbi0949e992011-10-12 10:44:56 +0300633 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300634 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300635 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300636 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300637 case DWC3_MODE_HOST:
638 dwc3_host_exit(dwc);
639 break;
640 case DWC3_MODE_DRD:
Felipe Balbid07e8812011-10-12 14:08:26 +0300641 dwc3_gadget_exit(dwc);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200642 dwc3_host_exit(dwc);
643 dwc3_otg_exit(dwc);
Felipe Balbid07e8812011-10-12 14:08:26 +0300644 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300645 default:
646 /* do nothing */
647 break;
648 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300649
Chanho Park802ca852012-02-15 18:27:55 +0900650err1:
Felipe Balbi72246da2011-08-19 18:10:58 +0300651 dwc3_core_exit(dwc);
652
Felipe Balbi72246da2011-08-19 18:10:58 +0300653 return ret;
654}
655
656static int __devexit dwc3_remove(struct platform_device *pdev)
657{
Felipe Balbi72246da2011-08-19 18:10:58 +0300658 struct dwc3 *dwc = platform_get_drvdata(pdev);
659 struct resource *res;
Felipe Balbi72246da2011-08-19 18:10:58 +0300660
661 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
662
Felipe Balbi72246da2011-08-19 18:10:58 +0300663 pm_runtime_disable(&pdev->dev);
664
665 dwc3_debugfs_exit(dwc);
666
Felipe Balbi0949e992011-10-12 10:44:56 +0300667 switch (dwc->mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300668 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300669 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300670 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300671 case DWC3_MODE_HOST:
672 dwc3_host_exit(dwc);
673 break;
674 case DWC3_MODE_DRD:
Felipe Balbid07e8812011-10-12 14:08:26 +0300675 dwc3_gadget_exit(dwc);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200676 dwc3_host_exit(dwc);
677 dwc3_otg_exit(dwc);
Felipe Balbid07e8812011-10-12 14:08:26 +0300678 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300679 default:
680 /* do nothing */
681 break;
682 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300683
684 dwc3_core_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300685
686 return 0;
687}
688
Kishon Vijay Abraham Ibdc707a2013-03-18 12:18:57 +0530689#ifdef CONFIG_OF
690static const struct of_device_id of_dwc3_match[] = {
691 {
692 .compatible = "synopsys,dwc3"
693 },
694 { },
695};
696MODULE_DEVICE_TABLE(of, of_dwc3_match);
697#endif
698
Felipe Balbi72246da2011-08-19 18:10:58 +0300699static struct platform_driver dwc3_driver = {
700 .probe = dwc3_probe,
701 .remove = __devexit_p(dwc3_remove),
702 .driver = {
703 .name = "dwc3",
Kishon Vijay Abraham Ibdc707a2013-03-18 12:18:57 +0530704 .of_match_table = of_match_ptr(of_dwc3_match),
Felipe Balbi72246da2011-08-19 18:10:58 +0300705 },
Felipe Balbi72246da2011-08-19 18:10:58 +0300706};
707
Tobias Klauserb1116dc2012-02-28 12:57:20 +0100708module_platform_driver(dwc3_driver);
709
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +0200710MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +0300711MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
712MODULE_LICENSE("Dual BSD/GPL");
713MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");