blob: 8d543ea4352aa2a722286593b94c003f6d0d3e28 [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);
Oliver Neukum2a540ed2012-08-26 21:34:19 +0200103 smp_mb__before_clear_bit();
Felipe Balbi8300dd22011-10-18 13:54:01 +0300104 clear_bit(id, dwc3_devs);
105}
106EXPORT_SYMBOL_GPL(dwc3_put_device_id);
107
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100108void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
109{
110 u32 reg;
111
112 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
113 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
114 reg |= DWC3_GCTL_PRTCAPDIR(mode);
115 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
116}
Felipe Balbi8300dd22011-10-18 13:54:01 +0300117
Felipe Balbi72246da2011-08-19 18:10:58 +0300118/**
119 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
120 * @dwc: pointer to our context structure
121 */
122static void dwc3_core_soft_reset(struct dwc3 *dwc)
123{
124 u32 reg;
125
126 /* Before Resetting PHY, put Core in Reset */
127 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
128 reg |= DWC3_GCTL_CORESOFTRESET;
129 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
130
131 /* Assert USB3 PHY reset */
132 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
133 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
134 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
135
136 /* Assert USB2 PHY reset */
137 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
138 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
139 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
140
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300141 usb_phy_init(dwc->usb2_phy);
142 usb_phy_init(dwc->usb3_phy);
Felipe Balbi72246da2011-08-19 18:10:58 +0300143 mdelay(100);
144
145 /* Clear USB3 PHY reset */
146 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
147 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
148 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
149
150 /* Clear USB2 PHY reset */
151 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
152 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
153 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
154
Pratyush Anand45627ac2012-06-21 17:44:28 +0530155 mdelay(100);
156
Felipe Balbi72246da2011-08-19 18:10:58 +0300157 /* After PHYs are stable we can take Core out of reset state */
158 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
159 reg &= ~DWC3_GCTL_CORESOFTRESET;
160 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
161}
162
163/**
164 * dwc3_free_one_event_buffer - Frees one event buffer
165 * @dwc: Pointer to our controller context structure
166 * @evt: Pointer to event buffer to be freed
167 */
168static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
169 struct dwc3_event_buffer *evt)
170{
171 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300172}
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
Felipe Balbi380f0d22012-10-11 13:48:36 +0300187 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300188 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 }
217}
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 Balbi380f0d22012-10-11 13:48:36 +0300235 dwc->ev_buffs = devm_kzalloc(dwc->dev, sizeof(*dwc->ev_buffs) * num,
236 GFP_KERNEL);
Felipe Balbi457d3f22011-10-24 12:03:13 +0300237 if (!dwc->ev_buffs) {
238 dev_err(dwc->dev, "can't allocate event buffers array\n");
239 return -ENOMEM;
240 }
241
Felipe Balbi72246da2011-08-19 18:10:58 +0300242 for (i = 0; i < num; i++) {
243 struct dwc3_event_buffer *evt;
244
245 evt = dwc3_alloc_one_event_buffer(dwc, length);
246 if (IS_ERR(evt)) {
247 dev_err(dwc->dev, "can't allocate event buffer\n");
248 return PTR_ERR(evt);
249 }
250 dwc->ev_buffs[i] = evt;
251 }
252
253 return 0;
254}
255
256/**
257 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800258 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300259 *
260 * Returns 0 on success otherwise negative errno.
261 */
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300262static int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300263{
264 struct dwc3_event_buffer *evt;
265 int n;
266
Felipe Balbi9f622b22011-10-12 10:31:04 +0300267 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300268 evt = dwc->ev_buffs[n];
269 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
270 evt->buf, (unsigned long long) evt->dma,
271 evt->length);
272
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300273 evt->lpos = 0;
274
Felipe Balbi72246da2011-08-19 18:10:58 +0300275 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
276 lower_32_bits(evt->dma));
277 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
278 upper_32_bits(evt->dma));
279 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
280 evt->length & 0xffff);
281 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
282 }
283
284 return 0;
285}
286
287static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
288{
289 struct dwc3_event_buffer *evt;
290 int n;
291
Felipe Balbi9f622b22011-10-12 10:31:04 +0300292 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300293 evt = dwc->ev_buffs[n];
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300294
295 evt->lpos = 0;
296
Felipe Balbi72246da2011-08-19 18:10:58 +0300297 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
298 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
299 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
300 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
301 }
302}
303
Felipe Balbi26ceca92011-09-30 10:58:49 +0300304static void __devinit dwc3_cache_hwparams(struct dwc3 *dwc)
305{
306 struct dwc3_hwparams *parms = &dwc->hwparams;
307
308 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
309 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
310 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
311 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
312 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
313 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
314 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
315 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
316 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
317}
318
Felipe Balbi72246da2011-08-19 18:10:58 +0300319/**
320 * dwc3_core_init - Low-level initialization of DWC3 Core
321 * @dwc: Pointer to our controller context structure
322 *
323 * Returns 0 on success otherwise negative errno.
324 */
325static int __devinit dwc3_core_init(struct dwc3 *dwc)
326{
327 unsigned long timeout;
328 u32 reg;
329 int ret;
330
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200331 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
332 /* This should read as U3 followed by revision number */
333 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
334 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
335 ret = -ENODEV;
336 goto err0;
337 }
Felipe Balbi248b1222011-12-14 21:59:30 +0200338 dwc->revision = reg;
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200339
Felipe Balbi72246da2011-08-19 18:10:58 +0300340 /* issue device SoftReset too */
341 timeout = jiffies + msecs_to_jiffies(500);
342 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
343 do {
344 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
345 if (!(reg & DWC3_DCTL_CSFTRST))
346 break;
347
348 if (time_after(jiffies, timeout)) {
349 dev_err(dwc->dev, "Reset Timed Out\n");
350 ret = -ETIMEDOUT;
351 goto err0;
352 }
353
354 cpu_relax();
355 } while (true);
356
Pratyush Anand58a0f232012-06-21 17:44:29 +0530357 dwc3_core_soft_reset(dwc);
358
Felipe Balbi9f622b22011-10-12 10:31:04 +0300359 dwc3_cache_hwparams(dwc);
360
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100361 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800362 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100363 reg &= ~DWC3_GCTL_DISSCRAMBLE;
364
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100365 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100366 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
367 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
368 break;
369 default:
370 dev_dbg(dwc->dev, "No power optimization available\n");
371 }
372
373 /*
374 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800375 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100376 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800377 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100378 */
379 if (dwc->revision < DWC3_REVISION_190A)
380 reg |= DWC3_GCTL_U2RSTECN;
381
382 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
383
Felipe Balbi9f622b22011-10-12 10:31:04 +0300384 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300385 if (ret) {
386 dev_err(dwc->dev, "failed to allocate event buffers\n");
387 ret = -ENOMEM;
388 goto err1;
389 }
390
391 ret = dwc3_event_buffers_setup(dwc);
392 if (ret) {
393 dev_err(dwc->dev, "failed to setup event buffers\n");
394 goto err1;
395 }
396
Felipe Balbi72246da2011-08-19 18:10:58 +0300397 return 0;
398
399err1:
400 dwc3_free_event_buffers(dwc);
401
402err0:
403 return ret;
404}
405
406static void dwc3_core_exit(struct dwc3 *dwc)
407{
408 dwc3_event_buffers_cleanup(dwc);
409 dwc3_free_event_buffers(dwc);
410}
411
412#define DWC3_ALIGN_MASK (16 - 1)
413
414static int __devinit dwc3_probe(struct platform_device *pdev)
415{
Felipe Balbi457e84b2012-01-18 18:04:09 +0200416 struct device_node *node = pdev->dev.of_node;
Felipe Balbi72246da2011-08-19 18:10:58 +0300417 struct resource *res;
418 struct dwc3 *dwc;
Chanho Park802ca852012-02-15 18:27:55 +0900419 struct device *dev = &pdev->dev;
Felipe Balbi0949e992011-10-12 10:44:56 +0300420
Felipe Balbi72246da2011-08-19 18:10:58 +0300421 int ret = -ENOMEM;
Felipe Balbi0949e992011-10-12 10:44:56 +0300422
423 void __iomem *regs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300424 void *mem;
425
Felipe Balbi0949e992011-10-12 10:44:56 +0300426 u8 mode;
427
Chanho Park802ca852012-02-15 18:27:55 +0900428 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300429 if (!mem) {
Chanho Park802ca852012-02-15 18:27:55 +0900430 dev_err(dev, "not enough memory\n");
431 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300432 }
433 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
434 dwc->mem = mem;
435
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300436 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300437 if (!res) {
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300438 dev_err(dev, "missing IRQ\n");
Chanho Park802ca852012-02-15 18:27:55 +0900439 return -ENODEV;
Felipe Balbi72246da2011-08-19 18:10:58 +0300440 }
Kishon Vijay Abraham I066618b2012-08-21 14:56:16 +0530441 dwc->xhci_resources[1].start = res->start;
442 dwc->xhci_resources[1].end = res->end;
443 dwc->xhci_resources[1].flags = res->flags;
444 dwc->xhci_resources[1].name = res->name;
Felipe Balbi72246da2011-08-19 18:10:58 +0300445
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300446 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
447 if (!res) {
448 dev_err(dev, "missing memory resource\n");
449 return -ENODEV;
450 }
Kishon Vijay Abraham I066618b2012-08-21 14:56:16 +0530451 dwc->xhci_resources[0].start = res->start;
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300452 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
453 DWC3_XHCI_REGS_END;
Kishon Vijay Abraham I066618b2012-08-21 14:56:16 +0530454 dwc->xhci_resources[0].flags = res->flags;
455 dwc->xhci_resources[0].name = res->name;
Felipe Balbid07e8812011-10-12 14:08:26 +0300456
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300457 /*
458 * Request memory region but exclude xHCI regs,
459 * since it will be requested by the xhci-plat driver.
460 */
461 res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START,
462 resource_size(res) - DWC3_GLOBALS_REGS_START,
Chanho Park802ca852012-02-15 18:27:55 +0900463 dev_name(dev));
Felipe Balbi72246da2011-08-19 18:10:58 +0300464 if (!res) {
Chanho Park802ca852012-02-15 18:27:55 +0900465 dev_err(dev, "can't request mem region\n");
466 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300467 }
468
Felipe Balbib7e38aa2012-08-10 09:16:43 +0300469 regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
Felipe Balbi72246da2011-08-19 18:10:58 +0300470 if (!regs) {
Chanho Park802ca852012-02-15 18:27:55 +0900471 dev_err(dev, "ioremap failed\n");
472 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300473 }
474
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300475 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
476 if (IS_ERR_OR_NULL(dwc->usb2_phy)) {
477 dev_err(dev, "no usb2 phy configured\n");
478 return -EPROBE_DEFER;
479 }
480
481 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
482 if (IS_ERR_OR_NULL(dwc->usb3_phy)) {
483 dev_err(dev, "no usb3 phy configured\n");
484 return -EPROBE_DEFER;
485 }
486
Felipe Balbi72246da2011-08-19 18:10:58 +0300487 spin_lock_init(&dwc->lock);
488 platform_set_drvdata(pdev, dwc);
489
490 dwc->regs = regs;
491 dwc->regs_size = resource_size(res);
Chanho Park802ca852012-02-15 18:27:55 +0900492 dwc->dev = dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300493
Felipe Balbi6c167fc2011-10-07 22:55:04 +0300494 if (!strncmp("super", maximum_speed, 5))
495 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
496 else if (!strncmp("high", maximum_speed, 4))
497 dwc->maximum_speed = DWC3_DCFG_HIGHSPEED;
498 else if (!strncmp("full", maximum_speed, 4))
499 dwc->maximum_speed = DWC3_DCFG_FULLSPEED1;
500 else if (!strncmp("low", maximum_speed, 3))
501 dwc->maximum_speed = DWC3_DCFG_LOWSPEED;
502 else
503 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
504
Felipe Balbi457e84b2012-01-18 18:04:09 +0200505 if (of_get_property(node, "tx-fifo-resize", NULL))
506 dwc->needs_fifo_resize = true;
507
Chanho Park802ca852012-02-15 18:27:55 +0900508 pm_runtime_enable(dev);
509 pm_runtime_get_sync(dev);
510 pm_runtime_forbid(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300511
512 ret = dwc3_core_init(dwc);
513 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900514 dev_err(dev, "failed to initialize core\n");
515 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300516 }
517
Felipe Balbi0949e992011-10-12 10:44:56 +0300518 mode = DWC3_MODE(dwc->hwparams.hwparams0);
519
520 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300521 case DWC3_MODE_DEVICE:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100522 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300523 ret = dwc3_gadget_init(dwc);
524 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900525 dev_err(dev, "failed to initialize gadget\n");
526 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300527 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300528 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300529 case DWC3_MODE_HOST:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100530 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
Felipe Balbid07e8812011-10-12 14:08:26 +0300531 ret = dwc3_host_init(dwc);
532 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900533 dev_err(dev, "failed to initialize host\n");
534 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300535 }
536 break;
537 case DWC3_MODE_DRD:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100538 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
Felipe Balbid07e8812011-10-12 14:08:26 +0300539 ret = dwc3_host_init(dwc);
540 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900541 dev_err(dev, "failed to initialize host\n");
542 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300543 }
544
545 ret = dwc3_gadget_init(dwc);
546 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900547 dev_err(dev, "failed to initialize gadget\n");
548 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300549 }
550 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300551 default:
Chanho Park802ca852012-02-15 18:27:55 +0900552 dev_err(dev, "Unsupported mode of operation %d\n", mode);
553 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300554 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300555 dwc->mode = mode;
Felipe Balbi72246da2011-08-19 18:10:58 +0300556
557 ret = dwc3_debugfs_init(dwc);
558 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900559 dev_err(dev, "failed to initialize debugfs\n");
560 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +0300561 }
562
Chanho Park802ca852012-02-15 18:27:55 +0900563 pm_runtime_allow(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300564
565 return 0;
566
Chanho Park802ca852012-02-15 18:27:55 +0900567err2:
Felipe Balbi0949e992011-10-12 10:44:56 +0300568 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300569 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300570 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300571 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300572 case DWC3_MODE_HOST:
573 dwc3_host_exit(dwc);
574 break;
575 case DWC3_MODE_DRD:
576 dwc3_host_exit(dwc);
577 dwc3_gadget_exit(dwc);
578 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300579 default:
580 /* do nothing */
581 break;
582 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300583
Chanho Park802ca852012-02-15 18:27:55 +0900584err1:
Felipe Balbi72246da2011-08-19 18:10:58 +0300585 dwc3_core_exit(dwc);
586
Felipe Balbi72246da2011-08-19 18:10:58 +0300587 return ret;
588}
589
590static int __devexit dwc3_remove(struct platform_device *pdev)
591{
Felipe Balbi72246da2011-08-19 18:10:58 +0300592 struct dwc3 *dwc = platform_get_drvdata(pdev);
593 struct resource *res;
Felipe Balbi72246da2011-08-19 18:10:58 +0300594
595 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
596
597 pm_runtime_put(&pdev->dev);
598 pm_runtime_disable(&pdev->dev);
599
600 dwc3_debugfs_exit(dwc);
601
Felipe Balbi0949e992011-10-12 10:44:56 +0300602 switch (dwc->mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300603 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300604 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300605 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300606 case DWC3_MODE_HOST:
607 dwc3_host_exit(dwc);
608 break;
609 case DWC3_MODE_DRD:
610 dwc3_host_exit(dwc);
611 dwc3_gadget_exit(dwc);
612 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300613 default:
614 /* do nothing */
615 break;
616 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300617
618 dwc3_core_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300619
620 return 0;
621}
622
Felipe Balbi72246da2011-08-19 18:10:58 +0300623static struct platform_driver dwc3_driver = {
624 .probe = dwc3_probe,
625 .remove = __devexit_p(dwc3_remove),
626 .driver = {
627 .name = "dwc3",
628 },
Felipe Balbi72246da2011-08-19 18:10:58 +0300629};
630
Tobias Klauserb1116dc2012-02-28 12:57:20 +0100631module_platform_driver(dwc3_driver);
632
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +0200633MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +0300634MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
635MODULE_LICENSE("Dual BSD/GPL");
636MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");