blob: 64a0e01c16d61c365993d927bd3f6100a5c66604 [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
Pratyush Anand38a535c2012-06-21 17:44:28 +0530151 mdelay(100);
152
Felipe Balbi72246da2011-08-19 18:10:58 +0300153 /* After PHYs are stable we can take Core out of reset state */
154 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
155 reg &= ~DWC3_GCTL_CORESOFTRESET;
156 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
157}
158
159/**
160 * dwc3_free_one_event_buffer - Frees one event buffer
161 * @dwc: Pointer to our controller context structure
162 * @evt: Pointer to event buffer to be freed
163 */
164static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
165 struct dwc3_event_buffer *evt)
166{
167 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
168 kfree(evt);
169}
170
171/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800172 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300173 * @dwc: Pointer to our controller context structure
174 * @length: size of the event buffer
175 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800176 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300177 * otherwise ERR_PTR(errno).
178 */
179static struct dwc3_event_buffer *__devinit
180dwc3_alloc_one_event_buffer(struct dwc3 *dwc, unsigned length)
181{
182 struct dwc3_event_buffer *evt;
183
184 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
185 if (!evt)
186 return ERR_PTR(-ENOMEM);
187
188 evt->dwc = dwc;
189 evt->length = length;
190 evt->buf = dma_alloc_coherent(dwc->dev, length,
191 &evt->dma, GFP_KERNEL);
192 if (!evt->buf) {
193 kfree(evt);
194 return ERR_PTR(-ENOMEM);
195 }
196
197 return evt;
198}
199
200/**
201 * dwc3_free_event_buffers - frees all allocated event buffers
202 * @dwc: Pointer to our controller context structure
203 */
204static void dwc3_free_event_buffers(struct dwc3 *dwc)
205{
206 struct dwc3_event_buffer *evt;
207 int i;
208
Felipe Balbi9f622b22011-10-12 10:31:04 +0300209 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300210 evt = dwc->ev_buffs[i];
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900211 if (evt)
Felipe Balbi72246da2011-08-19 18:10:58 +0300212 dwc3_free_one_event_buffer(dwc, evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300213 }
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900214
215 kfree(dwc->ev_buffs);
Felipe Balbi72246da2011-08-19 18:10:58 +0300216}
217
218/**
219 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800220 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300221 * @length: size of event buffer
222 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800223 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300224 * may contain some buffers allocated but not all which were requested.
225 */
Felipe Balbi9f622b22011-10-12 10:31:04 +0300226static int __devinit dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300227{
Felipe Balbi9f622b22011-10-12 10:31:04 +0300228 int num;
Felipe Balbi72246da2011-08-19 18:10:58 +0300229 int i;
230
Felipe Balbi9f622b22011-10-12 10:31:04 +0300231 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
232 dwc->num_event_buffers = num;
233
Felipe Balbi457d3f22011-10-24 12:03:13 +0300234 dwc->ev_buffs = kzalloc(sizeof(*dwc->ev_buffs) * num, GFP_KERNEL);
235 if (!dwc->ev_buffs) {
236 dev_err(dwc->dev, "can't allocate event buffers array\n");
237 return -ENOMEM;
238 }
239
Felipe Balbi72246da2011-08-19 18:10:58 +0300240 for (i = 0; i < num; i++) {
241 struct dwc3_event_buffer *evt;
242
243 evt = dwc3_alloc_one_event_buffer(dwc, length);
244 if (IS_ERR(evt)) {
245 dev_err(dwc->dev, "can't allocate event buffer\n");
246 return PTR_ERR(evt);
247 }
248 dwc->ev_buffs[i] = evt;
249 }
250
251 return 0;
252}
253
254/**
255 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800256 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300257 *
258 * Returns 0 on success otherwise negative errno.
259 */
Paul Zimmerman43fa01a2012-04-27 14:28:02 +0300260static int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300261{
262 struct dwc3_event_buffer *evt;
263 int n;
264
Felipe Balbi9f622b22011-10-12 10:31:04 +0300265 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300266 evt = dwc->ev_buffs[n];
267 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
268 evt->buf, (unsigned long long) evt->dma,
269 evt->length);
270
Paul Zimmerman43fa01a2012-04-27 14:28:02 +0300271 evt->lpos = 0;
272
Felipe Balbi72246da2011-08-19 18:10:58 +0300273 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
274 lower_32_bits(evt->dma));
275 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
276 upper_32_bits(evt->dma));
277 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
278 evt->length & 0xffff);
279 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
280 }
281
282 return 0;
283}
284
285static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
286{
287 struct dwc3_event_buffer *evt;
288 int n;
289
Felipe Balbi9f622b22011-10-12 10:31:04 +0300290 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300291 evt = dwc->ev_buffs[n];
Paul Zimmerman43fa01a2012-04-27 14:28:02 +0300292
293 evt->lpos = 0;
294
Felipe Balbi72246da2011-08-19 18:10:58 +0300295 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
296 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
297 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
298 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
299 }
300}
301
Felipe Balbi26ceca92011-09-30 10:58:49 +0300302static void __devinit dwc3_cache_hwparams(struct dwc3 *dwc)
303{
304 struct dwc3_hwparams *parms = &dwc->hwparams;
305
306 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
307 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
308 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
309 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
310 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
311 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
312 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
313 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
314 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
315}
316
Felipe Balbi72246da2011-08-19 18:10:58 +0300317/**
318 * dwc3_core_init - Low-level initialization of DWC3 Core
319 * @dwc: Pointer to our controller context structure
320 *
321 * Returns 0 on success otherwise negative errno.
322 */
323static int __devinit dwc3_core_init(struct dwc3 *dwc)
324{
325 unsigned long timeout;
326 u32 reg;
327 int ret;
328
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200329 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
330 /* This should read as U3 followed by revision number */
331 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
332 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
333 ret = -ENODEV;
334 goto err0;
335 }
Felipe Balbi248b1222011-12-14 21:59:30 +0200336 dwc->revision = reg;
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200337
Felipe Balbi72246da2011-08-19 18:10:58 +0300338 /* issue device SoftReset too */
339 timeout = jiffies + msecs_to_jiffies(500);
340 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
341 do {
342 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
343 if (!(reg & DWC3_DCTL_CSFTRST))
344 break;
345
346 if (time_after(jiffies, timeout)) {
347 dev_err(dwc->dev, "Reset Timed Out\n");
348 ret = -ETIMEDOUT;
349 goto err0;
350 }
351
352 cpu_relax();
353 } while (true);
354
Pratyush Anand99d4da82012-06-21 17:44:29 +0530355 dwc3_core_soft_reset(dwc);
356
Felipe Balbi9f622b22011-10-12 10:31:04 +0300357 dwc3_cache_hwparams(dwc);
358
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100359 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800360 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100361 reg &= ~DWC3_GCTL_DISSCRAMBLE;
362
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100363 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100364 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
365 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
366 break;
367 default:
368 dev_dbg(dwc->dev, "No power optimization available\n");
369 }
370
371 /*
372 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800373 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100374 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800375 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100376 */
377 if (dwc->revision < DWC3_REVISION_190A)
378 reg |= DWC3_GCTL_U2RSTECN;
379
380 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
381
Pavankumar Kondetife2c0632012-06-12 15:21:13 +0530382 /*
Pavankumar Kondetic6e15aa2012-07-16 11:37:15 +0530383 * The default value of GUCTL[31:22] should be 0x8. But on cores
384 * revision < 2.30a, the default value is mistakenly overridden
385 * with 0x0. Restore the correct default value.
386 */
387 if (dwc->revision < DWC3_REVISION_230A) {
388 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
389 reg &= ~DWC3_GUCTL_REFCLKPER;
390 reg |= 0x8 << __ffs(DWC3_GUCTL_REFCLKPER);
391 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
392 }
393 /*
Pavankumar Kondetife2c0632012-06-12 15:21:13 +0530394 * Currently, the default and the recommended value for GUSB3PIPECTL
395 * [21:19] in the RTL is 3'b100 or 32 consecutive errors. Based on
396 * analysis and experiments in the lab, it is found that there is a
397 * relatively low probability of getting 32 consecutive word errors
398 * in the presence of random recovered noise (during electrical idle).
399 * This can delay the entry to a low power state such that for
400 * applications where the link stays in a non-U0 state for a short
401 * duration (< 1 microsecond), the local PHY does not enter the low
402 * power state prior to receiving a potential LFPS wakeup. This causes
403 * the PHY CDR (Clock and Data Recovery) operation to be unstable for
404 * some Synopsys PHYs.
405 *
406 * The proposal now is to change the default and the recommended value
407 * for GUSB3PIPECTL[21:19] in the RTL from 3'b100 to a minimum of
408 * 3'b001. Perform the same in software for controllers prior to 2.30a
409 * revision.
410 */
411
412 if (dwc->revision < DWC3_REVISION_230A) {
413 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
414 reg &= ~DWC3_GUSB3PIPECTL_DELAY_P1P2P3;
415 reg |= 1 << __ffs(DWC3_GUSB3PIPECTL_DELAY_P1P2P3);
Pavankumar Kondeti5acb4ba2012-07-16 11:44:46 +0530416 /*
417 * Receiver Detection in U3/Rx.Det is mistakenly disabled in
418 * cores < 2.30a. Fix it here.
419 */
420 reg &= ~DWC3_GUSB3PIPECTL_DIS_RXDET_U3_RXDET;
Pavankumar Kondetife2c0632012-06-12 15:21:13 +0530421 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
422 }
423
Felipe Balbi9f622b22011-10-12 10:31:04 +0300424 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300425 if (ret) {
426 dev_err(dwc->dev, "failed to allocate event buffers\n");
427 ret = -ENOMEM;
428 goto err1;
429 }
430
431 ret = dwc3_event_buffers_setup(dwc);
432 if (ret) {
433 dev_err(dwc->dev, "failed to setup event buffers\n");
434 goto err1;
435 }
436
Felipe Balbi72246da2011-08-19 18:10:58 +0300437 return 0;
438
439err1:
440 dwc3_free_event_buffers(dwc);
441
442err0:
443 return ret;
444}
445
446static void dwc3_core_exit(struct dwc3 *dwc)
447{
448 dwc3_event_buffers_cleanup(dwc);
449 dwc3_free_event_buffers(dwc);
450}
451
452#define DWC3_ALIGN_MASK (16 - 1)
453
454static int __devinit dwc3_probe(struct platform_device *pdev)
455{
Felipe Balbi457e84b2012-01-18 18:04:09 +0200456 struct device_node *node = pdev->dev.of_node;
Felipe Balbi72246da2011-08-19 18:10:58 +0300457 struct resource *res;
458 struct dwc3 *dwc;
Chanho Park802ca852012-02-15 18:27:55 +0900459 struct device *dev = &pdev->dev;
Felipe Balbi0949e992011-10-12 10:44:56 +0300460
Felipe Balbi72246da2011-08-19 18:10:58 +0300461 int ret = -ENOMEM;
Felipe Balbi0949e992011-10-12 10:44:56 +0300462
463 void __iomem *regs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300464 void *mem;
465
Felipe Balbi0949e992011-10-12 10:44:56 +0300466 u8 mode;
467
Chanho Park802ca852012-02-15 18:27:55 +0900468 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300469 if (!mem) {
Chanho Park802ca852012-02-15 18:27:55 +0900470 dev_err(dev, "not enough memory\n");
471 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300472 }
473 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
474 dwc->mem = mem;
475
Ido Shayevitz4a187332012-04-23 14:53:37 +0200476 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300477 if (!res) {
Ido Shayevitz4a187332012-04-23 14:53:37 +0200478 dev_err(dev, "missing IRQ\n");
Chanho Park802ca852012-02-15 18:27:55 +0900479 return -ENODEV;
Felipe Balbi72246da2011-08-19 18:10:58 +0300480 }
Ido Shayevitz4a187332012-04-23 14:53:37 +0200481 dwc->xhci_resources[1] = *res;
Felipe Balbi72246da2011-08-19 18:10:58 +0300482
Ido Shayevitz4a187332012-04-23 14:53:37 +0200483 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
484 if (!res) {
485 dev_err(dev, "missing memory resource\n");
486 return -ENODEV;
487 }
488 dwc->xhci_resources[0] = *res;
489 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
490 DWC3_XHCI_REGS_END;
Felipe Balbid07e8812011-10-12 14:08:26 +0300491
Ido Shayevitz4a187332012-04-23 14:53:37 +0200492 /*
493 * Request memory region but exclude xHCI regs,
494 * since it will be requested by the xhci-plat driver.
495 */
496 res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START,
497 resource_size(res) - DWC3_GLOBALS_REGS_START,
Chanho Park802ca852012-02-15 18:27:55 +0900498 dev_name(dev));
Ido Shayevitz4a187332012-04-23 14:53:37 +0200499
Felipe Balbi72246da2011-08-19 18:10:58 +0300500 if (!res) {
Chanho Park802ca852012-02-15 18:27:55 +0900501 dev_err(dev, "can't request mem region\n");
502 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300503 }
504
Felipe Balbi497a2a32012-08-10 09:16:43 +0300505 regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
Felipe Balbi72246da2011-08-19 18:10:58 +0300506 if (!regs) {
Chanho Park802ca852012-02-15 18:27:55 +0900507 dev_err(dev, "ioremap failed\n");
508 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300509 }
510
Felipe Balbi72246da2011-08-19 18:10:58 +0300511 spin_lock_init(&dwc->lock);
512 platform_set_drvdata(pdev, dwc);
513
514 dwc->regs = regs;
515 dwc->regs_size = resource_size(res);
Chanho Park802ca852012-02-15 18:27:55 +0900516 dwc->dev = dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300517
Felipe Balbi6c167fc2011-10-07 22:55:04 +0300518 if (!strncmp("super", maximum_speed, 5))
519 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
520 else if (!strncmp("high", maximum_speed, 4))
521 dwc->maximum_speed = DWC3_DCFG_HIGHSPEED;
522 else if (!strncmp("full", maximum_speed, 4))
523 dwc->maximum_speed = DWC3_DCFG_FULLSPEED1;
524 else if (!strncmp("low", maximum_speed, 3))
525 dwc->maximum_speed = DWC3_DCFG_LOWSPEED;
526 else
527 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
528
Felipe Balbi457e84b2012-01-18 18:04:09 +0200529 if (of_get_property(node, "tx-fifo-resize", NULL))
530 dwc->needs_fifo_resize = true;
531
Manu Gautamb5067272012-07-02 09:53:41 +0530532 pm_runtime_no_callbacks(dev);
533 pm_runtime_set_active(dev);
Chanho Park802ca852012-02-15 18:27:55 +0900534 pm_runtime_enable(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300535
536 ret = dwc3_core_init(dwc);
537 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900538 dev_err(dev, "failed to initialize core\n");
539 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300540 }
541
Felipe Balbi0949e992011-10-12 10:44:56 +0300542 mode = DWC3_MODE(dwc->hwparams.hwparams0);
543
544 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300545 case DWC3_MODE_DEVICE:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100546 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300547 ret = dwc3_gadget_init(dwc);
548 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900549 dev_err(dev, "failed to initialize gadget\n");
550 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300551 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300552 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300553 case DWC3_MODE_HOST:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100554 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
Felipe Balbid07e8812011-10-12 14:08:26 +0300555 ret = dwc3_host_init(dwc);
556 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900557 dev_err(dev, "failed to initialize host\n");
558 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300559 }
560 break;
561 case DWC3_MODE_DRD:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100562 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200563 ret = dwc3_otg_init(dwc);
564 if (ret) {
565 dev_err(dev, "failed to initialize otg\n");
566 goto err1;
567 }
568
Felipe Balbid07e8812011-10-12 14:08:26 +0300569 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
Felipe Balbi72246da2011-08-19 18:10:58 +0300589 return 0;
590
Chanho Park802ca852012-02-15 18:27:55 +0900591err2:
Felipe Balbi0949e992011-10-12 10:44:56 +0300592 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300593 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300594 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300595 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300596 case DWC3_MODE_HOST:
597 dwc3_host_exit(dwc);
598 break;
599 case DWC3_MODE_DRD:
Felipe Balbid07e8812011-10-12 14:08:26 +0300600 dwc3_gadget_exit(dwc);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200601 dwc3_host_exit(dwc);
602 dwc3_otg_exit(dwc);
Felipe Balbid07e8812011-10-12 14:08:26 +0300603 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300604 default:
605 /* do nothing */
606 break;
607 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300608
Chanho Park802ca852012-02-15 18:27:55 +0900609err1:
Felipe Balbi72246da2011-08-19 18:10:58 +0300610 dwc3_core_exit(dwc);
611
Felipe Balbi72246da2011-08-19 18:10:58 +0300612 return ret;
613}
614
615static int __devexit dwc3_remove(struct platform_device *pdev)
616{
Felipe Balbi72246da2011-08-19 18:10:58 +0300617 struct dwc3 *dwc = platform_get_drvdata(pdev);
618 struct resource *res;
Felipe Balbi72246da2011-08-19 18:10:58 +0300619
620 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
621
Felipe Balbi72246da2011-08-19 18:10:58 +0300622 pm_runtime_disable(&pdev->dev);
623
624 dwc3_debugfs_exit(dwc);
625
Felipe Balbi0949e992011-10-12 10:44:56 +0300626 switch (dwc->mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300627 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300628 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300629 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300630 case DWC3_MODE_HOST:
631 dwc3_host_exit(dwc);
632 break;
633 case DWC3_MODE_DRD:
Felipe Balbid07e8812011-10-12 14:08:26 +0300634 dwc3_gadget_exit(dwc);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200635 dwc3_host_exit(dwc);
636 dwc3_otg_exit(dwc);
Felipe Balbid07e8812011-10-12 14:08:26 +0300637 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300638 default:
639 /* do nothing */
640 break;
641 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300642
643 dwc3_core_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300644
645 return 0;
646}
647
Felipe Balbi72246da2011-08-19 18:10:58 +0300648static struct platform_driver dwc3_driver = {
649 .probe = dwc3_probe,
650 .remove = __devexit_p(dwc3_remove),
651 .driver = {
652 .name = "dwc3",
653 },
Felipe Balbi72246da2011-08-19 18:10:58 +0300654};
655
Tobias Klauserb1116dc2012-02-28 12:57:20 +0100656module_platform_driver(dwc3_driver);
657
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +0200658MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +0300659MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
660MODULE_LICENSE("Dual BSD/GPL");
661MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");