blob: 41f810658a4552c27287ae54f1fdf455b1c1e1a8 [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);
102 clear_bit(id, dwc3_devs);
103}
104EXPORT_SYMBOL_GPL(dwc3_put_device_id);
105
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100106void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
107{
108 u32 reg;
109
110 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
111 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
112 reg |= DWC3_GCTL_PRTCAPDIR(mode);
113 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
114}
Felipe Balbi8300dd22011-10-18 13:54:01 +0300115
Felipe Balbi72246da2011-08-19 18:10:58 +0300116/**
117 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
118 * @dwc: pointer to our context structure
119 */
120static void dwc3_core_soft_reset(struct dwc3 *dwc)
121{
122 u32 reg;
123
124 /* Before Resetting PHY, put Core in Reset */
125 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
126 reg |= DWC3_GCTL_CORESOFTRESET;
127 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
128
129 /* Assert USB3 PHY reset */
130 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
131 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
132 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
133
134 /* Assert USB2 PHY reset */
135 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
136 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
137 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
138
139 mdelay(100);
140
141 /* Clear USB3 PHY reset */
142 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
143 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
144 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
145
146 /* Clear USB2 PHY reset */
147 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
148 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
149 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
150
151 /* After PHYs are stable we can take Core out of reset state */
152 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
153 reg &= ~DWC3_GCTL_CORESOFTRESET;
154 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
155}
156
157/**
158 * dwc3_free_one_event_buffer - Frees one event buffer
159 * @dwc: Pointer to our controller context structure
160 * @evt: Pointer to event buffer to be freed
161 */
162static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
163 struct dwc3_event_buffer *evt)
164{
165 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
166 kfree(evt);
167}
168
169/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800170 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300171 * @dwc: Pointer to our controller context structure
172 * @length: size of the event buffer
173 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800174 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300175 * otherwise ERR_PTR(errno).
176 */
177static struct dwc3_event_buffer *__devinit
178dwc3_alloc_one_event_buffer(struct dwc3 *dwc, unsigned length)
179{
180 struct dwc3_event_buffer *evt;
181
182 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
183 if (!evt)
184 return ERR_PTR(-ENOMEM);
185
186 evt->dwc = dwc;
187 evt->length = length;
188 evt->buf = dma_alloc_coherent(dwc->dev, length,
189 &evt->dma, GFP_KERNEL);
190 if (!evt->buf) {
191 kfree(evt);
192 return ERR_PTR(-ENOMEM);
193 }
194
195 return evt;
196}
197
198/**
199 * dwc3_free_event_buffers - frees all allocated event buffers
200 * @dwc: Pointer to our controller context structure
201 */
202static void dwc3_free_event_buffers(struct dwc3 *dwc)
203{
204 struct dwc3_event_buffer *evt;
205 int i;
206
Felipe Balbi9f622b22011-10-12 10:31:04 +0300207 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300208 evt = dwc->ev_buffs[i];
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900209 if (evt)
Felipe Balbi72246da2011-08-19 18:10:58 +0300210 dwc3_free_one_event_buffer(dwc, evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300211 }
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900212
213 kfree(dwc->ev_buffs);
Felipe Balbi72246da2011-08-19 18:10:58 +0300214}
215
216/**
217 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800218 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300219 * @length: size of event buffer
220 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800221 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300222 * may contain some buffers allocated but not all which were requested.
223 */
Felipe Balbi9f622b22011-10-12 10:31:04 +0300224static int __devinit dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300225{
Felipe Balbi9f622b22011-10-12 10:31:04 +0300226 int num;
Felipe Balbi72246da2011-08-19 18:10:58 +0300227 int i;
228
Felipe Balbi9f622b22011-10-12 10:31:04 +0300229 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
230 dwc->num_event_buffers = num;
231
Felipe Balbi457d3f22011-10-24 12:03:13 +0300232 dwc->ev_buffs = kzalloc(sizeof(*dwc->ev_buffs) * num, GFP_KERNEL);
233 if (!dwc->ev_buffs) {
234 dev_err(dwc->dev, "can't allocate event buffers array\n");
235 return -ENOMEM;
236 }
237
Felipe Balbi72246da2011-08-19 18:10:58 +0300238 for (i = 0; i < num; i++) {
239 struct dwc3_event_buffer *evt;
240
241 evt = dwc3_alloc_one_event_buffer(dwc, length);
242 if (IS_ERR(evt)) {
243 dev_err(dwc->dev, "can't allocate event buffer\n");
244 return PTR_ERR(evt);
245 }
246 dwc->ev_buffs[i] = evt;
247 }
248
249 return 0;
250}
251
252/**
253 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800254 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300255 *
256 * Returns 0 on success otherwise negative errno.
257 */
258static int __devinit dwc3_event_buffers_setup(struct dwc3 *dwc)
259{
260 struct dwc3_event_buffer *evt;
261 int n;
262
Felipe Balbi9f622b22011-10-12 10:31:04 +0300263 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300264 evt = dwc->ev_buffs[n];
265 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
266 evt->buf, (unsigned long long) evt->dma,
267 evt->length);
268
269 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
270 lower_32_bits(evt->dma));
271 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
272 upper_32_bits(evt->dma));
273 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
274 evt->length & 0xffff);
275 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
276 }
277
278 return 0;
279}
280
281static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
282{
283 struct dwc3_event_buffer *evt;
284 int n;
285
Felipe Balbi9f622b22011-10-12 10:31:04 +0300286 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300287 evt = dwc->ev_buffs[n];
288 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
289 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
290 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
291 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
292 }
293}
294
Felipe Balbi26ceca92011-09-30 10:58:49 +0300295static void __devinit dwc3_cache_hwparams(struct dwc3 *dwc)
296{
297 struct dwc3_hwparams *parms = &dwc->hwparams;
298
299 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
300 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
301 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
302 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
303 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
304 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
305 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
306 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
307 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
308}
309
Felipe Balbi72246da2011-08-19 18:10:58 +0300310/**
311 * dwc3_core_init - Low-level initialization of DWC3 Core
312 * @dwc: Pointer to our controller context structure
313 *
314 * Returns 0 on success otherwise negative errno.
315 */
316static int __devinit dwc3_core_init(struct dwc3 *dwc)
317{
318 unsigned long timeout;
319 u32 reg;
320 int ret;
321
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200322 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
323 /* This should read as U3 followed by revision number */
324 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
325 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
326 ret = -ENODEV;
327 goto err0;
328 }
Felipe Balbi248b1222011-12-14 21:59:30 +0200329 dwc->revision = reg;
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200330
Felipe Balbi72246da2011-08-19 18:10:58 +0300331 dwc3_core_soft_reset(dwc);
332
333 /* issue device SoftReset too */
334 timeout = jiffies + msecs_to_jiffies(500);
335 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
336 do {
337 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
338 if (!(reg & DWC3_DCTL_CSFTRST))
339 break;
340
341 if (time_after(jiffies, timeout)) {
342 dev_err(dwc->dev, "Reset Timed Out\n");
343 ret = -ETIMEDOUT;
344 goto err0;
345 }
346
347 cpu_relax();
348 } while (true);
349
Felipe Balbi9f622b22011-10-12 10:31:04 +0300350 dwc3_cache_hwparams(dwc);
351
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100352 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800353 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100354 reg &= ~DWC3_GCTL_DISSCRAMBLE;
355
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100356 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100357 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
358 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
359 break;
360 default:
361 dev_dbg(dwc->dev, "No power optimization available\n");
362 }
363
364 /*
365 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800366 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100367 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800368 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100369 */
370 if (dwc->revision < DWC3_REVISION_190A)
371 reg |= DWC3_GCTL_U2RSTECN;
372
373 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
374
Pavankumar Kondetife2c0632012-06-12 15:21:13 +0530375 /*
Pavankumar Kondetic6e15aa2012-07-16 11:37:15 +0530376 * The default value of GUCTL[31:22] should be 0x8. But on cores
377 * revision < 2.30a, the default value is mistakenly overridden
378 * with 0x0. Restore the correct default value.
379 */
380 if (dwc->revision < DWC3_REVISION_230A) {
381 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
382 reg &= ~DWC3_GUCTL_REFCLKPER;
383 reg |= 0x8 << __ffs(DWC3_GUCTL_REFCLKPER);
384 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
385 }
386 /*
Pavankumar Kondetife2c0632012-06-12 15:21:13 +0530387 * Currently, the default and the recommended value for GUSB3PIPECTL
388 * [21:19] in the RTL is 3'b100 or 32 consecutive errors. Based on
389 * analysis and experiments in the lab, it is found that there is a
390 * relatively low probability of getting 32 consecutive word errors
391 * in the presence of random recovered noise (during electrical idle).
392 * This can delay the entry to a low power state such that for
393 * applications where the link stays in a non-U0 state for a short
394 * duration (< 1 microsecond), the local PHY does not enter the low
395 * power state prior to receiving a potential LFPS wakeup. This causes
396 * the PHY CDR (Clock and Data Recovery) operation to be unstable for
397 * some Synopsys PHYs.
398 *
399 * The proposal now is to change the default and the recommended value
400 * for GUSB3PIPECTL[21:19] in the RTL from 3'b100 to a minimum of
401 * 3'b001. Perform the same in software for controllers prior to 2.30a
402 * revision.
403 */
404
405 if (dwc->revision < DWC3_REVISION_230A) {
406 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
407 reg &= ~DWC3_GUSB3PIPECTL_DELAY_P1P2P3;
408 reg |= 1 << __ffs(DWC3_GUSB3PIPECTL_DELAY_P1P2P3);
Pavankumar Kondeti5acb4ba2012-07-16 11:44:46 +0530409 /*
410 * Receiver Detection in U3/Rx.Det is mistakenly disabled in
411 * cores < 2.30a. Fix it here.
412 */
413 reg &= ~DWC3_GUSB3PIPECTL_DIS_RXDET_U3_RXDET;
Pavankumar Kondetife2c0632012-06-12 15:21:13 +0530414 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
415 }
416
Felipe Balbi9f622b22011-10-12 10:31:04 +0300417 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300418 if (ret) {
419 dev_err(dwc->dev, "failed to allocate event buffers\n");
420 ret = -ENOMEM;
421 goto err1;
422 }
423
424 ret = dwc3_event_buffers_setup(dwc);
425 if (ret) {
426 dev_err(dwc->dev, "failed to setup event buffers\n");
427 goto err1;
428 }
429
Felipe Balbi72246da2011-08-19 18:10:58 +0300430 return 0;
431
432err1:
433 dwc3_free_event_buffers(dwc);
434
435err0:
436 return ret;
437}
438
439static void dwc3_core_exit(struct dwc3 *dwc)
440{
441 dwc3_event_buffers_cleanup(dwc);
442 dwc3_free_event_buffers(dwc);
443}
444
445#define DWC3_ALIGN_MASK (16 - 1)
446
447static int __devinit dwc3_probe(struct platform_device *pdev)
448{
Felipe Balbi457e84b2012-01-18 18:04:09 +0200449 struct device_node *node = pdev->dev.of_node;
Felipe Balbi72246da2011-08-19 18:10:58 +0300450 struct resource *res;
451 struct dwc3 *dwc;
Chanho Park802ca852012-02-15 18:27:55 +0900452 struct device *dev = &pdev->dev;
Felipe Balbi0949e992011-10-12 10:44:56 +0300453
Felipe Balbi72246da2011-08-19 18:10:58 +0300454 int ret = -ENOMEM;
Felipe Balbi0949e992011-10-12 10:44:56 +0300455
456 void __iomem *regs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300457 void *mem;
458
Felipe Balbi0949e992011-10-12 10:44:56 +0300459 u8 mode;
460
Chanho Park802ca852012-02-15 18:27:55 +0900461 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300462 if (!mem) {
Chanho Park802ca852012-02-15 18:27:55 +0900463 dev_err(dev, "not enough memory\n");
464 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300465 }
466 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
467 dwc->mem = mem;
468
Ido Shayevitz4a187332012-04-23 14:53:37 +0200469 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300470 if (!res) {
Ido Shayevitz4a187332012-04-23 14:53:37 +0200471 dev_err(dev, "missing IRQ\n");
Chanho Park802ca852012-02-15 18:27:55 +0900472 return -ENODEV;
Felipe Balbi72246da2011-08-19 18:10:58 +0300473 }
Ido Shayevitz4a187332012-04-23 14:53:37 +0200474 dwc->xhci_resources[1] = *res;
Felipe Balbi72246da2011-08-19 18:10:58 +0300475
Ido Shayevitz4a187332012-04-23 14:53:37 +0200476 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
477 if (!res) {
478 dev_err(dev, "missing memory resource\n");
479 return -ENODEV;
480 }
481 dwc->xhci_resources[0] = *res;
482 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
483 DWC3_XHCI_REGS_END;
Felipe Balbid07e8812011-10-12 14:08:26 +0300484
Ido Shayevitz4a187332012-04-23 14:53:37 +0200485 /*
486 * Request memory region but exclude xHCI regs,
487 * since it will be requested by the xhci-plat driver.
488 */
489 res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START,
490 resource_size(res) - DWC3_GLOBALS_REGS_START,
Chanho Park802ca852012-02-15 18:27:55 +0900491 dev_name(dev));
Ido Shayevitz4a187332012-04-23 14:53:37 +0200492
Felipe Balbi72246da2011-08-19 18:10:58 +0300493 if (!res) {
Chanho Park802ca852012-02-15 18:27:55 +0900494 dev_err(dev, "can't request mem region\n");
495 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300496 }
497
Chanho Park802ca852012-02-15 18:27:55 +0900498 regs = devm_ioremap(dev, res->start, resource_size(res));
Felipe Balbi72246da2011-08-19 18:10:58 +0300499 if (!regs) {
Chanho Park802ca852012-02-15 18:27:55 +0900500 dev_err(dev, "ioremap failed\n");
501 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300502 }
503
Felipe Balbi72246da2011-08-19 18:10:58 +0300504 spin_lock_init(&dwc->lock);
505 platform_set_drvdata(pdev, dwc);
506
507 dwc->regs = regs;
508 dwc->regs_size = resource_size(res);
Chanho Park802ca852012-02-15 18:27:55 +0900509 dwc->dev = dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300510
Felipe Balbi6c167fc2011-10-07 22:55:04 +0300511 if (!strncmp("super", maximum_speed, 5))
512 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
513 else if (!strncmp("high", maximum_speed, 4))
514 dwc->maximum_speed = DWC3_DCFG_HIGHSPEED;
515 else if (!strncmp("full", maximum_speed, 4))
516 dwc->maximum_speed = DWC3_DCFG_FULLSPEED1;
517 else if (!strncmp("low", maximum_speed, 3))
518 dwc->maximum_speed = DWC3_DCFG_LOWSPEED;
519 else
520 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
521
Felipe Balbi457e84b2012-01-18 18:04:09 +0200522 if (of_get_property(node, "tx-fifo-resize", NULL))
523 dwc->needs_fifo_resize = true;
524
Chanho Park802ca852012-02-15 18:27:55 +0900525 pm_runtime_enable(dev);
526 pm_runtime_get_sync(dev);
527 pm_runtime_forbid(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300528
529 ret = dwc3_core_init(dwc);
530 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900531 dev_err(dev, "failed to initialize core\n");
532 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300533 }
534
Felipe Balbi0949e992011-10-12 10:44:56 +0300535 mode = DWC3_MODE(dwc->hwparams.hwparams0);
536
537 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300538 case DWC3_MODE_DEVICE:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100539 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300540 ret = dwc3_gadget_init(dwc);
541 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900542 dev_err(dev, "failed to initialize gadget\n");
543 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300544 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300545 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300546 case DWC3_MODE_HOST:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100547 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
Felipe Balbid07e8812011-10-12 14:08:26 +0300548 ret = dwc3_host_init(dwc);
549 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900550 dev_err(dev, "failed to initialize host\n");
551 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300552 }
553 break;
554 case DWC3_MODE_DRD:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100555 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200556 ret = dwc3_otg_init(dwc);
557 if (ret) {
558 dev_err(dev, "failed to initialize otg\n");
559 goto err1;
560 }
561
Felipe Balbid07e8812011-10-12 14:08:26 +0300562 ret = dwc3_host_init(dwc);
563 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900564 dev_err(dev, "failed to initialize host\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200565 dwc3_otg_exit(dwc);
Chanho Park802ca852012-02-15 18:27:55 +0900566 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300567 }
568
569 ret = dwc3_gadget_init(dwc);
570 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900571 dev_err(dev, "failed to initialize gadget\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200572 dwc3_host_exit(dwc);
573 dwc3_otg_exit(dwc);
Chanho Park802ca852012-02-15 18:27:55 +0900574 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300575 }
576 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300577 default:
Chanho Park802ca852012-02-15 18:27:55 +0900578 dev_err(dev, "Unsupported mode of operation %d\n", mode);
579 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300580 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300581 dwc->mode = mode;
Felipe Balbi72246da2011-08-19 18:10:58 +0300582
583 ret = dwc3_debugfs_init(dwc);
584 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900585 dev_err(dev, "failed to initialize debugfs\n");
586 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +0300587 }
588
Chanho Park802ca852012-02-15 18:27:55 +0900589 pm_runtime_allow(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300590
591 return 0;
592
Chanho Park802ca852012-02-15 18:27:55 +0900593err2:
Felipe Balbi0949e992011-10-12 10:44:56 +0300594 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300595 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300596 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300597 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300598 case DWC3_MODE_HOST:
599 dwc3_host_exit(dwc);
600 break;
601 case DWC3_MODE_DRD:
Felipe Balbid07e8812011-10-12 14:08:26 +0300602 dwc3_gadget_exit(dwc);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200603 dwc3_host_exit(dwc);
604 dwc3_otg_exit(dwc);
Felipe Balbid07e8812011-10-12 14:08:26 +0300605 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300606 default:
607 /* do nothing */
608 break;
609 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300610
Chanho Park802ca852012-02-15 18:27:55 +0900611err1:
Felipe Balbi72246da2011-08-19 18:10:58 +0300612 dwc3_core_exit(dwc);
613
Felipe Balbi72246da2011-08-19 18:10:58 +0300614 return ret;
615}
616
617static int __devexit dwc3_remove(struct platform_device *pdev)
618{
Felipe Balbi72246da2011-08-19 18:10:58 +0300619 struct dwc3 *dwc = platform_get_drvdata(pdev);
620 struct resource *res;
Felipe Balbi72246da2011-08-19 18:10:58 +0300621
622 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
623
624 pm_runtime_put(&pdev->dev);
625 pm_runtime_disable(&pdev->dev);
626
627 dwc3_debugfs_exit(dwc);
628
Felipe Balbi0949e992011-10-12 10:44:56 +0300629 switch (dwc->mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300630 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300631 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300632 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300633 case DWC3_MODE_HOST:
634 dwc3_host_exit(dwc);
635 break;
636 case DWC3_MODE_DRD:
Felipe Balbid07e8812011-10-12 14:08:26 +0300637 dwc3_gadget_exit(dwc);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200638 dwc3_host_exit(dwc);
639 dwc3_otg_exit(dwc);
Felipe Balbid07e8812011-10-12 14:08:26 +0300640 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300641 default:
642 /* do nothing */
643 break;
644 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300645
646 dwc3_core_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300647
648 return 0;
649}
650
Felipe Balbi72246da2011-08-19 18:10:58 +0300651static struct platform_driver dwc3_driver = {
652 .probe = dwc3_probe,
653 .remove = __devexit_p(dwc3_remove),
654 .driver = {
655 .name = "dwc3",
656 },
Felipe Balbi72246da2011-08-19 18:10:58 +0300657};
658
Tobias Klauserb1116dc2012-02-28 12:57:20 +0100659module_platform_driver(dwc3_driver);
660
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +0200661MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +0300662MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
663MODULE_LICENSE("Dual BSD/GPL");
664MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");