blob: 79a24fab13d142fd7da020c2ba794f33b82263a4 [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
Felipe Balbi51e1e7b2012-07-19 14:09:48 +030053#include <linux/usb/otg.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030054#include <linux/usb/ch9.h>
55#include <linux/usb/gadget.h>
56
57#include "core.h"
58#include "gadget.h"
59#include "io.h"
60
61#include "debug.h"
62
Felipe Balbi6c167fc2011-10-07 22:55:04 +030063static char *maximum_speed = "super";
64module_param(maximum_speed, charp, 0);
65MODULE_PARM_DESC(maximum_speed, "Maximum supported speed.");
66
Felipe Balbi8300dd22011-10-18 13:54:01 +030067/* -------------------------------------------------------------------------- */
68
69#define DWC3_DEVS_POSSIBLE 32
70
71static DECLARE_BITMAP(dwc3_devs, DWC3_DEVS_POSSIBLE);
72
73int dwc3_get_device_id(void)
74{
75 int id;
76
77again:
78 id = find_first_zero_bit(dwc3_devs, DWC3_DEVS_POSSIBLE);
79 if (id < DWC3_DEVS_POSSIBLE) {
80 int old;
81
82 old = test_and_set_bit(id, dwc3_devs);
83 if (old)
84 goto again;
85 } else {
86 pr_err("dwc3: no space for new device\n");
87 id = -ENOMEM;
88 }
89
Dan Carpenter075cd142012-02-04 16:37:14 +030090 return id;
Felipe Balbi8300dd22011-10-18 13:54:01 +030091}
92EXPORT_SYMBOL_GPL(dwc3_get_device_id);
93
94void dwc3_put_device_id(int id)
95{
96 int ret;
97
98 if (id < 0)
99 return;
100
101 ret = test_bit(id, dwc3_devs);
102 WARN(!ret, "dwc3: ID %d not in use\n", id);
103 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
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300140 usb_phy_init(dwc->usb2_phy);
141 usb_phy_init(dwc->usb3_phy);
Felipe Balbi72246da2011-08-19 18:10:58 +0300142 mdelay(100);
143
144 /* Clear USB3 PHY reset */
145 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
146 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
147 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
148
149 /* Clear USB2 PHY reset */
150 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
151 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
152 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
153
Pratyush Anand45627ac2012-06-21 17:44:28 +0530154 mdelay(100);
155
Felipe Balbi72246da2011-08-19 18:10:58 +0300156 /* After PHYs are stable we can take Core out of reset state */
157 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
158 reg &= ~DWC3_GCTL_CORESOFTRESET;
159 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
160}
161
162/**
163 * dwc3_free_one_event_buffer - Frees one event buffer
164 * @dwc: Pointer to our controller context structure
165 * @evt: Pointer to event buffer to be freed
166 */
167static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
168 struct dwc3_event_buffer *evt)
169{
170 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
171 kfree(evt);
172}
173
174/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800175 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300176 * @dwc: Pointer to our controller context structure
177 * @length: size of the event buffer
178 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800179 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300180 * otherwise ERR_PTR(errno).
181 */
182static struct dwc3_event_buffer *__devinit
183dwc3_alloc_one_event_buffer(struct dwc3 *dwc, unsigned length)
184{
185 struct dwc3_event_buffer *evt;
186
187 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
188 if (!evt)
189 return ERR_PTR(-ENOMEM);
190
191 evt->dwc = dwc;
192 evt->length = length;
193 evt->buf = dma_alloc_coherent(dwc->dev, length,
194 &evt->dma, GFP_KERNEL);
195 if (!evt->buf) {
196 kfree(evt);
197 return ERR_PTR(-ENOMEM);
198 }
199
200 return evt;
201}
202
203/**
204 * dwc3_free_event_buffers - frees all allocated event buffers
205 * @dwc: Pointer to our controller context structure
206 */
207static void dwc3_free_event_buffers(struct dwc3 *dwc)
208{
209 struct dwc3_event_buffer *evt;
210 int i;
211
Felipe Balbi9f622b22011-10-12 10:31:04 +0300212 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300213 evt = dwc->ev_buffs[i];
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900214 if (evt)
Felipe Balbi72246da2011-08-19 18:10:58 +0300215 dwc3_free_one_event_buffer(dwc, evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300216 }
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900217
218 kfree(dwc->ev_buffs);
Felipe Balbi72246da2011-08-19 18:10:58 +0300219}
220
221/**
222 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800223 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300224 * @length: size of event buffer
225 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800226 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300227 * may contain some buffers allocated but not all which were requested.
228 */
Felipe Balbi9f622b22011-10-12 10:31:04 +0300229static int __devinit dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300230{
Felipe Balbi9f622b22011-10-12 10:31:04 +0300231 int num;
Felipe Balbi72246da2011-08-19 18:10:58 +0300232 int i;
233
Felipe Balbi9f622b22011-10-12 10:31:04 +0300234 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
235 dwc->num_event_buffers = num;
236
Felipe Balbi457d3f22011-10-24 12:03:13 +0300237 dwc->ev_buffs = kzalloc(sizeof(*dwc->ev_buffs) * num, GFP_KERNEL);
238 if (!dwc->ev_buffs) {
239 dev_err(dwc->dev, "can't allocate event buffers array\n");
240 return -ENOMEM;
241 }
242
Felipe Balbi72246da2011-08-19 18:10:58 +0300243 for (i = 0; i < num; i++) {
244 struct dwc3_event_buffer *evt;
245
246 evt = dwc3_alloc_one_event_buffer(dwc, length);
247 if (IS_ERR(evt)) {
248 dev_err(dwc->dev, "can't allocate event buffer\n");
249 return PTR_ERR(evt);
250 }
251 dwc->ev_buffs[i] = evt;
252 }
253
254 return 0;
255}
256
257/**
258 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800259 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300260 *
261 * Returns 0 on success otherwise negative errno.
262 */
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300263static int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300264{
265 struct dwc3_event_buffer *evt;
266 int n;
267
Felipe Balbi9f622b22011-10-12 10:31:04 +0300268 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300269 evt = dwc->ev_buffs[n];
270 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
271 evt->buf, (unsigned long long) evt->dma,
272 evt->length);
273
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300274 evt->lpos = 0;
275
Felipe Balbi72246da2011-08-19 18:10:58 +0300276 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
277 lower_32_bits(evt->dma));
278 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
279 upper_32_bits(evt->dma));
280 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
281 evt->length & 0xffff);
282 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
283 }
284
285 return 0;
286}
287
288static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
289{
290 struct dwc3_event_buffer *evt;
291 int n;
292
Felipe Balbi9f622b22011-10-12 10:31:04 +0300293 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300294 evt = dwc->ev_buffs[n];
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300295
296 evt->lpos = 0;
297
Felipe Balbi72246da2011-08-19 18:10:58 +0300298 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
299 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
300 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
301 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
302 }
303}
304
Felipe Balbi26ceca92011-09-30 10:58:49 +0300305static void __devinit dwc3_cache_hwparams(struct dwc3 *dwc)
306{
307 struct dwc3_hwparams *parms = &dwc->hwparams;
308
309 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
310 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
311 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
312 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
313 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
314 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
315 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
316 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
317 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
318}
319
Felipe Balbi72246da2011-08-19 18:10:58 +0300320/**
321 * dwc3_core_init - Low-level initialization of DWC3 Core
322 * @dwc: Pointer to our controller context structure
323 *
324 * Returns 0 on success otherwise negative errno.
325 */
326static int __devinit dwc3_core_init(struct dwc3 *dwc)
327{
328 unsigned long timeout;
329 u32 reg;
330 int ret;
331
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200332 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
333 /* This should read as U3 followed by revision number */
334 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
335 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
336 ret = -ENODEV;
337 goto err0;
338 }
Felipe Balbi248b1222011-12-14 21:59:30 +0200339 dwc->revision = reg;
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200340
Felipe Balbi72246da2011-08-19 18:10:58 +0300341 /* issue device SoftReset too */
342 timeout = jiffies + msecs_to_jiffies(500);
343 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
344 do {
345 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
346 if (!(reg & DWC3_DCTL_CSFTRST))
347 break;
348
349 if (time_after(jiffies, timeout)) {
350 dev_err(dwc->dev, "Reset Timed Out\n");
351 ret = -ETIMEDOUT;
352 goto err0;
353 }
354
355 cpu_relax();
356 } while (true);
357
Pratyush Anand58a0f232012-06-21 17:44:29 +0530358 dwc3_core_soft_reset(dwc);
359
Felipe Balbi9f622b22011-10-12 10:31:04 +0300360 dwc3_cache_hwparams(dwc);
361
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100362 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800363 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100364 reg &= ~DWC3_GCTL_DISSCRAMBLE;
365
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100366 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100367 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
368 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
369 break;
370 default:
371 dev_dbg(dwc->dev, "No power optimization available\n");
372 }
373
374 /*
375 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800376 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100377 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800378 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100379 */
380 if (dwc->revision < DWC3_REVISION_190A)
381 reg |= DWC3_GCTL_U2RSTECN;
382
383 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
384
Felipe Balbi9f622b22011-10-12 10:31:04 +0300385 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300386 if (ret) {
387 dev_err(dwc->dev, "failed to allocate event buffers\n");
388 ret = -ENOMEM;
389 goto err1;
390 }
391
392 ret = dwc3_event_buffers_setup(dwc);
393 if (ret) {
394 dev_err(dwc->dev, "failed to setup event buffers\n");
395 goto err1;
396 }
397
Felipe Balbi72246da2011-08-19 18:10:58 +0300398 return 0;
399
400err1:
401 dwc3_free_event_buffers(dwc);
402
403err0:
404 return ret;
405}
406
407static void dwc3_core_exit(struct dwc3 *dwc)
408{
409 dwc3_event_buffers_cleanup(dwc);
410 dwc3_free_event_buffers(dwc);
411}
412
413#define DWC3_ALIGN_MASK (16 - 1)
414
415static int __devinit dwc3_probe(struct platform_device *pdev)
416{
Felipe Balbi457e84b2012-01-18 18:04:09 +0200417 struct device_node *node = pdev->dev.of_node;
Felipe Balbi72246da2011-08-19 18:10:58 +0300418 struct resource *res;
419 struct dwc3 *dwc;
Chanho Park802ca852012-02-15 18:27:55 +0900420 struct device *dev = &pdev->dev;
Felipe Balbi0949e992011-10-12 10:44:56 +0300421
Felipe Balbi72246da2011-08-19 18:10:58 +0300422 int ret = -ENOMEM;
Felipe Balbi0949e992011-10-12 10:44:56 +0300423
424 void __iomem *regs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300425 void *mem;
426
Felipe Balbi0949e992011-10-12 10:44:56 +0300427 u8 mode;
428
Chanho Park802ca852012-02-15 18:27:55 +0900429 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300430 if (!mem) {
Chanho Park802ca852012-02-15 18:27:55 +0900431 dev_err(dev, "not enough memory\n");
432 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300433 }
434 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
435 dwc->mem = mem;
436
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300437 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300438 if (!res) {
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300439 dev_err(dev, "missing IRQ\n");
Chanho Park802ca852012-02-15 18:27:55 +0900440 return -ENODEV;
Felipe Balbi72246da2011-08-19 18:10:58 +0300441 }
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300442 dwc->xhci_resources[1] = *res;
Felipe Balbi72246da2011-08-19 18:10:58 +0300443
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300444 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
445 if (!res) {
446 dev_err(dev, "missing memory resource\n");
447 return -ENODEV;
448 }
449 dwc->xhci_resources[0] = *res;
450 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
451 DWC3_XHCI_REGS_END;
Felipe Balbid07e8812011-10-12 14:08:26 +0300452
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300453 /*
454 * Request memory region but exclude xHCI regs,
455 * since it will be requested by the xhci-plat driver.
456 */
457 res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START,
458 resource_size(res) - DWC3_GLOBALS_REGS_START,
Chanho Park802ca852012-02-15 18:27:55 +0900459 dev_name(dev));
Felipe Balbi72246da2011-08-19 18:10:58 +0300460 if (!res) {
Chanho Park802ca852012-02-15 18:27:55 +0900461 dev_err(dev, "can't request mem region\n");
462 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300463 }
464
Chanho Park802ca852012-02-15 18:27:55 +0900465 regs = devm_ioremap(dev, res->start, resource_size(res));
Felipe Balbi72246da2011-08-19 18:10:58 +0300466 if (!regs) {
Chanho Park802ca852012-02-15 18:27:55 +0900467 dev_err(dev, "ioremap failed\n");
468 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300469 }
470
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300471 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
472 if (IS_ERR_OR_NULL(dwc->usb2_phy)) {
473 dev_err(dev, "no usb2 phy configured\n");
474 return -EPROBE_DEFER;
475 }
476
477 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
478 if (IS_ERR_OR_NULL(dwc->usb3_phy)) {
479 dev_err(dev, "no usb3 phy configured\n");
480 return -EPROBE_DEFER;
481 }
482
Felipe Balbi72246da2011-08-19 18:10:58 +0300483 spin_lock_init(&dwc->lock);
484 platform_set_drvdata(pdev, dwc);
485
486 dwc->regs = regs;
487 dwc->regs_size = resource_size(res);
Chanho Park802ca852012-02-15 18:27:55 +0900488 dwc->dev = dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300489
Felipe Balbi6c167fc2011-10-07 22:55:04 +0300490 if (!strncmp("super", maximum_speed, 5))
491 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
492 else if (!strncmp("high", maximum_speed, 4))
493 dwc->maximum_speed = DWC3_DCFG_HIGHSPEED;
494 else if (!strncmp("full", maximum_speed, 4))
495 dwc->maximum_speed = DWC3_DCFG_FULLSPEED1;
496 else if (!strncmp("low", maximum_speed, 3))
497 dwc->maximum_speed = DWC3_DCFG_LOWSPEED;
498 else
499 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
500
Felipe Balbi457e84b2012-01-18 18:04:09 +0200501 if (of_get_property(node, "tx-fifo-resize", NULL))
502 dwc->needs_fifo_resize = true;
503
Chanho Park802ca852012-02-15 18:27:55 +0900504 pm_runtime_enable(dev);
505 pm_runtime_get_sync(dev);
506 pm_runtime_forbid(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300507
508 ret = dwc3_core_init(dwc);
509 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900510 dev_err(dev, "failed to initialize core\n");
511 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300512 }
513
Felipe Balbi0949e992011-10-12 10:44:56 +0300514 mode = DWC3_MODE(dwc->hwparams.hwparams0);
515
516 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300517 case DWC3_MODE_DEVICE:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100518 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300519 ret = dwc3_gadget_init(dwc);
520 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900521 dev_err(dev, "failed to initialize gadget\n");
522 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300523 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300524 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300525 case DWC3_MODE_HOST:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100526 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
Felipe Balbid07e8812011-10-12 14:08:26 +0300527 ret = dwc3_host_init(dwc);
528 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900529 dev_err(dev, "failed to initialize host\n");
530 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300531 }
532 break;
533 case DWC3_MODE_DRD:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100534 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
Felipe Balbid07e8812011-10-12 14:08:26 +0300535 ret = dwc3_host_init(dwc);
536 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900537 dev_err(dev, "failed to initialize host\n");
538 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300539 }
540
541 ret = dwc3_gadget_init(dwc);
542 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900543 dev_err(dev, "failed to initialize gadget\n");
544 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300545 }
546 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300547 default:
Chanho Park802ca852012-02-15 18:27:55 +0900548 dev_err(dev, "Unsupported mode of operation %d\n", mode);
549 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300550 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300551 dwc->mode = mode;
Felipe Balbi72246da2011-08-19 18:10:58 +0300552
553 ret = dwc3_debugfs_init(dwc);
554 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900555 dev_err(dev, "failed to initialize debugfs\n");
556 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +0300557 }
558
Chanho Park802ca852012-02-15 18:27:55 +0900559 pm_runtime_allow(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300560
561 return 0;
562
Chanho Park802ca852012-02-15 18:27:55 +0900563err2:
Felipe Balbi0949e992011-10-12 10:44:56 +0300564 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300565 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300566 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300567 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300568 case DWC3_MODE_HOST:
569 dwc3_host_exit(dwc);
570 break;
571 case DWC3_MODE_DRD:
572 dwc3_host_exit(dwc);
573 dwc3_gadget_exit(dwc);
574 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300575 default:
576 /* do nothing */
577 break;
578 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300579
Chanho Park802ca852012-02-15 18:27:55 +0900580err1:
Felipe Balbi72246da2011-08-19 18:10:58 +0300581 dwc3_core_exit(dwc);
582
Felipe Balbi72246da2011-08-19 18:10:58 +0300583 return ret;
584}
585
586static int __devexit dwc3_remove(struct platform_device *pdev)
587{
Felipe Balbi72246da2011-08-19 18:10:58 +0300588 struct dwc3 *dwc = platform_get_drvdata(pdev);
589 struct resource *res;
Felipe Balbi72246da2011-08-19 18:10:58 +0300590
591 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
592
593 pm_runtime_put(&pdev->dev);
594 pm_runtime_disable(&pdev->dev);
595
596 dwc3_debugfs_exit(dwc);
597
Felipe Balbi0949e992011-10-12 10:44:56 +0300598 switch (dwc->mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300599 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300600 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300601 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300602 case DWC3_MODE_HOST:
603 dwc3_host_exit(dwc);
604 break;
605 case DWC3_MODE_DRD:
606 dwc3_host_exit(dwc);
607 dwc3_gadget_exit(dwc);
608 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300609 default:
610 /* do nothing */
611 break;
612 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300613
614 dwc3_core_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300615
616 return 0;
617}
618
Felipe Balbi72246da2011-08-19 18:10:58 +0300619static struct platform_driver dwc3_driver = {
620 .probe = dwc3_probe,
621 .remove = __devexit_p(dwc3_remove),
622 .driver = {
623 .name = "dwc3",
624 },
Felipe Balbi72246da2011-08-19 18:10:58 +0300625};
626
Tobias Klauserb1116dc2012-02-28 12:57:20 +0100627module_platform_driver(dwc3_driver);
628
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +0200629MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +0300630MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
631MODULE_LICENSE("Dual BSD/GPL");
632MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");