blob: c14ebc975ba422028b663b4b90ac6e605c1c6115 [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 Siewior3140e8cb2011-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);
172 kfree(evt);
173}
174
175/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800176 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300177 * @dwc: Pointer to our controller context structure
178 * @length: size of the event buffer
179 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800180 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300181 * otherwise ERR_PTR(errno).
182 */
183static struct dwc3_event_buffer *__devinit
184dwc3_alloc_one_event_buffer(struct dwc3 *dwc, unsigned length)
185{
186 struct dwc3_event_buffer *evt;
187
188 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
189 if (!evt)
190 return ERR_PTR(-ENOMEM);
191
192 evt->dwc = dwc;
193 evt->length = length;
194 evt->buf = dma_alloc_coherent(dwc->dev, length,
195 &evt->dma, GFP_KERNEL);
196 if (!evt->buf) {
197 kfree(evt);
198 return ERR_PTR(-ENOMEM);
199 }
200
201 return evt;
202}
203
204/**
205 * dwc3_free_event_buffers - frees all allocated event buffers
206 * @dwc: Pointer to our controller context structure
207 */
208static void dwc3_free_event_buffers(struct dwc3 *dwc)
209{
210 struct dwc3_event_buffer *evt;
211 int i;
212
Felipe Balbi9f622b22011-10-12 10:31:04 +0300213 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300214 evt = dwc->ev_buffs[i];
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900215 if (evt)
Felipe Balbi72246da2011-08-19 18:10:58 +0300216 dwc3_free_one_event_buffer(dwc, evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300217 }
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900218
219 kfree(dwc->ev_buffs);
Felipe Balbi72246da2011-08-19 18:10:58 +0300220}
221
222/**
223 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800224 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300225 * @length: size of event buffer
226 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800227 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300228 * may contain some buffers allocated but not all which were requested.
229 */
Felipe Balbi9f622b22011-10-12 10:31:04 +0300230static int __devinit dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300231{
Felipe Balbi9f622b22011-10-12 10:31:04 +0300232 int num;
Felipe Balbi72246da2011-08-19 18:10:58 +0300233 int i;
234
Felipe Balbi9f622b22011-10-12 10:31:04 +0300235 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
236 dwc->num_event_buffers = num;
237
Felipe Balbi457d3f22011-10-24 12:03:13 +0300238 dwc->ev_buffs = kzalloc(sizeof(*dwc->ev_buffs) * num, GFP_KERNEL);
239 if (!dwc->ev_buffs) {
240 dev_err(dwc->dev, "can't allocate event buffers array\n");
241 return -ENOMEM;
242 }
243
Felipe Balbi72246da2011-08-19 18:10:58 +0300244 for (i = 0; i < num; i++) {
245 struct dwc3_event_buffer *evt;
246
247 evt = dwc3_alloc_one_event_buffer(dwc, length);
248 if (IS_ERR(evt)) {
249 dev_err(dwc->dev, "can't allocate event buffer\n");
250 return PTR_ERR(evt);
251 }
252 dwc->ev_buffs[i] = evt;
253 }
254
255 return 0;
256}
257
258/**
259 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800260 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300261 *
262 * Returns 0 on success otherwise negative errno.
263 */
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300264static int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300265{
266 struct dwc3_event_buffer *evt;
267 int n;
268
Felipe Balbi9f622b22011-10-12 10:31:04 +0300269 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300270 evt = dwc->ev_buffs[n];
271 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
272 evt->buf, (unsigned long long) evt->dma,
273 evt->length);
274
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300275 evt->lpos = 0;
276
Felipe Balbi72246da2011-08-19 18:10:58 +0300277 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
278 lower_32_bits(evt->dma));
279 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
280 upper_32_bits(evt->dma));
281 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
282 evt->length & 0xffff);
283 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
284 }
285
286 return 0;
287}
288
289static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
290{
291 struct dwc3_event_buffer *evt;
292 int n;
293
Felipe Balbi9f622b22011-10-12 10:31:04 +0300294 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300295 evt = dwc->ev_buffs[n];
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300296
297 evt->lpos = 0;
298
Felipe Balbi72246da2011-08-19 18:10:58 +0300299 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
300 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
301 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
302 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
303 }
304}
305
Felipe Balbi26ceca92011-09-30 10:58:49 +0300306static void __devinit dwc3_cache_hwparams(struct dwc3 *dwc)
307{
308 struct dwc3_hwparams *parms = &dwc->hwparams;
309
310 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
311 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
312 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
313 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
314 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
315 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
316 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
317 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
318 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
319}
320
Felipe Balbi72246da2011-08-19 18:10:58 +0300321/**
322 * dwc3_core_init - Low-level initialization of DWC3 Core
323 * @dwc: Pointer to our controller context structure
324 *
325 * Returns 0 on success otherwise negative errno.
326 */
327static int __devinit dwc3_core_init(struct dwc3 *dwc)
328{
329 unsigned long timeout;
330 u32 reg;
331 int ret;
332
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200333 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
334 /* This should read as U3 followed by revision number */
335 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
336 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
337 ret = -ENODEV;
338 goto err0;
339 }
Felipe Balbi248b1222011-12-14 21:59:30 +0200340 dwc->revision = reg;
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200341
Felipe Balbi72246da2011-08-19 18:10:58 +0300342 /* issue device SoftReset too */
343 timeout = jiffies + msecs_to_jiffies(500);
344 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
345 do {
346 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
347 if (!(reg & DWC3_DCTL_CSFTRST))
348 break;
349
350 if (time_after(jiffies, timeout)) {
351 dev_err(dwc->dev, "Reset Timed Out\n");
352 ret = -ETIMEDOUT;
353 goto err0;
354 }
355
356 cpu_relax();
357 } while (true);
358
Pratyush Anand58a0f232012-06-21 17:44:29 +0530359 dwc3_core_soft_reset(dwc);
360
Felipe Balbi9f622b22011-10-12 10:31:04 +0300361 dwc3_cache_hwparams(dwc);
362
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100363 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800364 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100365 reg &= ~DWC3_GCTL_DISSCRAMBLE;
366
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100367 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100368 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
369 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
370 break;
371 default:
372 dev_dbg(dwc->dev, "No power optimization available\n");
373 }
374
375 /*
376 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800377 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100378 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800379 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100380 */
381 if (dwc->revision < DWC3_REVISION_190A)
382 reg |= DWC3_GCTL_U2RSTECN;
383
384 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
385
Felipe Balbi9f622b22011-10-12 10:31:04 +0300386 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300387 if (ret) {
388 dev_err(dwc->dev, "failed to allocate event buffers\n");
389 ret = -ENOMEM;
390 goto err1;
391 }
392
393 ret = dwc3_event_buffers_setup(dwc);
394 if (ret) {
395 dev_err(dwc->dev, "failed to setup event buffers\n");
396 goto err1;
397 }
398
Felipe Balbi72246da2011-08-19 18:10:58 +0300399 return 0;
400
401err1:
402 dwc3_free_event_buffers(dwc);
403
404err0:
405 return ret;
406}
407
408static void dwc3_core_exit(struct dwc3 *dwc)
409{
410 dwc3_event_buffers_cleanup(dwc);
411 dwc3_free_event_buffers(dwc);
Vivek Gautam01b8daf2012-10-13 19:20:18 +0530412
413 usb_phy_shutdown(dwc->usb2_phy);
414 usb_phy_shutdown(dwc->usb3_phy);
415
Felipe Balbi72246da2011-08-19 18:10:58 +0300416}
417
418#define DWC3_ALIGN_MASK (16 - 1)
419
420static int __devinit dwc3_probe(struct platform_device *pdev)
421{
Felipe Balbi457e84b2012-01-18 18:04:09 +0200422 struct device_node *node = pdev->dev.of_node;
Felipe Balbi72246da2011-08-19 18:10:58 +0300423 struct resource *res;
424 struct dwc3 *dwc;
Chanho Park802ca852012-02-15 18:27:55 +0900425 struct device *dev = &pdev->dev;
Felipe Balbi0949e992011-10-12 10:44:56 +0300426
Felipe Balbi72246da2011-08-19 18:10:58 +0300427 int ret = -ENOMEM;
Felipe Balbi0949e992011-10-12 10:44:56 +0300428
429 void __iomem *regs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300430 void *mem;
431
Felipe Balbi0949e992011-10-12 10:44:56 +0300432 u8 mode;
433
Chanho Park802ca852012-02-15 18:27:55 +0900434 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300435 if (!mem) {
Chanho Park802ca852012-02-15 18:27:55 +0900436 dev_err(dev, "not enough memory\n");
437 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300438 }
439 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
440 dwc->mem = mem;
441
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300442 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300443 if (!res) {
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300444 dev_err(dev, "missing IRQ\n");
Chanho Park802ca852012-02-15 18:27:55 +0900445 return -ENODEV;
Felipe Balbi72246da2011-08-19 18:10:58 +0300446 }
Kishon Vijay Abraham I066618b2012-08-21 14:56:16 +0530447 dwc->xhci_resources[1].start = res->start;
448 dwc->xhci_resources[1].end = res->end;
449 dwc->xhci_resources[1].flags = res->flags;
450 dwc->xhci_resources[1].name = res->name;
Felipe Balbi72246da2011-08-19 18:10:58 +0300451
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300452 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
453 if (!res) {
454 dev_err(dev, "missing memory resource\n");
455 return -ENODEV;
456 }
Kishon Vijay Abraham I066618b2012-08-21 14:56:16 +0530457 dwc->xhci_resources[0].start = res->start;
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300458 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
459 DWC3_XHCI_REGS_END;
Kishon Vijay Abraham I066618b2012-08-21 14:56:16 +0530460 dwc->xhci_resources[0].flags = res->flags;
461 dwc->xhci_resources[0].name = res->name;
Felipe Balbid07e8812011-10-12 14:08:26 +0300462
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300463 /*
464 * Request memory region but exclude xHCI regs,
465 * since it will be requested by the xhci-plat driver.
466 */
467 res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START,
468 resource_size(res) - DWC3_GLOBALS_REGS_START,
Chanho Park802ca852012-02-15 18:27:55 +0900469 dev_name(dev));
Felipe Balbi72246da2011-08-19 18:10:58 +0300470 if (!res) {
Chanho Park802ca852012-02-15 18:27:55 +0900471 dev_err(dev, "can't request mem region\n");
472 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300473 }
474
Felipe Balbib7e38aa2012-08-10 09:16:43 +0300475 regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
Felipe Balbi72246da2011-08-19 18:10:58 +0300476 if (!regs) {
Chanho Park802ca852012-02-15 18:27:55 +0900477 dev_err(dev, "ioremap failed\n");
478 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300479 }
480
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300481 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
482 if (IS_ERR_OR_NULL(dwc->usb2_phy)) {
483 dev_err(dev, "no usb2 phy configured\n");
484 return -EPROBE_DEFER;
485 }
486
487 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
488 if (IS_ERR_OR_NULL(dwc->usb3_phy)) {
489 dev_err(dev, "no usb3 phy configured\n");
490 return -EPROBE_DEFER;
491 }
492
Felipe Balbi72246da2011-08-19 18:10:58 +0300493 spin_lock_init(&dwc->lock);
494 platform_set_drvdata(pdev, dwc);
495
496 dwc->regs = regs;
497 dwc->regs_size = resource_size(res);
Chanho Park802ca852012-02-15 18:27:55 +0900498 dwc->dev = dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300499
Felipe Balbi6c167fc2011-10-07 22:55:04 +0300500 if (!strncmp("super", maximum_speed, 5))
501 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
502 else if (!strncmp("high", maximum_speed, 4))
503 dwc->maximum_speed = DWC3_DCFG_HIGHSPEED;
504 else if (!strncmp("full", maximum_speed, 4))
505 dwc->maximum_speed = DWC3_DCFG_FULLSPEED1;
506 else if (!strncmp("low", maximum_speed, 3))
507 dwc->maximum_speed = DWC3_DCFG_LOWSPEED;
508 else
509 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
510
Felipe Balbi457e84b2012-01-18 18:04:09 +0200511 if (of_get_property(node, "tx-fifo-resize", NULL))
512 dwc->needs_fifo_resize = true;
513
Chanho Park802ca852012-02-15 18:27:55 +0900514 pm_runtime_enable(dev);
515 pm_runtime_get_sync(dev);
516 pm_runtime_forbid(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300517
518 ret = dwc3_core_init(dwc);
519 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900520 dev_err(dev, "failed to initialize core\n");
521 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300522 }
523
Felipe Balbi0949e992011-10-12 10:44:56 +0300524 mode = DWC3_MODE(dwc->hwparams.hwparams0);
525
526 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300527 case DWC3_MODE_DEVICE:
Sebastian Andrzej Siewior3140e8cb2011-10-31 22:25:40 +0100528 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300529 ret = dwc3_gadget_init(dwc);
530 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900531 dev_err(dev, "failed to initialize gadget\n");
532 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300533 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300534 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300535 case DWC3_MODE_HOST:
Sebastian Andrzej Siewior3140e8cb2011-10-31 22:25:40 +0100536 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
Felipe Balbid07e8812011-10-12 14:08:26 +0300537 ret = dwc3_host_init(dwc);
538 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900539 dev_err(dev, "failed to initialize host\n");
540 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300541 }
542 break;
543 case DWC3_MODE_DRD:
Sebastian Andrzej Siewior3140e8cb2011-10-31 22:25:40 +0100544 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
Felipe Balbid07e8812011-10-12 14:08:26 +0300545 ret = dwc3_host_init(dwc);
546 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900547 dev_err(dev, "failed to initialize host\n");
548 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300549 }
550
551 ret = dwc3_gadget_init(dwc);
552 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900553 dev_err(dev, "failed to initialize gadget\n");
554 goto err1;
Felipe Balbid07e8812011-10-12 14:08:26 +0300555 }
556 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300557 default:
Chanho Park802ca852012-02-15 18:27:55 +0900558 dev_err(dev, "Unsupported mode of operation %d\n", mode);
559 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300560 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300561 dwc->mode = mode;
Felipe Balbi72246da2011-08-19 18:10:58 +0300562
563 ret = dwc3_debugfs_init(dwc);
564 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900565 dev_err(dev, "failed to initialize debugfs\n");
566 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +0300567 }
568
Chanho Park802ca852012-02-15 18:27:55 +0900569 pm_runtime_allow(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300570
571 return 0;
572
Chanho Park802ca852012-02-15 18:27:55 +0900573err2:
Felipe Balbi0949e992011-10-12 10:44:56 +0300574 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300575 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300576 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300577 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300578 case DWC3_MODE_HOST:
579 dwc3_host_exit(dwc);
580 break;
581 case DWC3_MODE_DRD:
582 dwc3_host_exit(dwc);
583 dwc3_gadget_exit(dwc);
584 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300585 default:
586 /* do nothing */
587 break;
588 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300589
Chanho Park802ca852012-02-15 18:27:55 +0900590err1:
Felipe Balbi72246da2011-08-19 18:10:58 +0300591 dwc3_core_exit(dwc);
592
Felipe Balbi72246da2011-08-19 18:10:58 +0300593 return ret;
594}
595
596static int __devexit dwc3_remove(struct platform_device *pdev)
597{
Felipe Balbi72246da2011-08-19 18:10:58 +0300598 struct dwc3 *dwc = platform_get_drvdata(pdev);
599 struct resource *res;
Felipe Balbi72246da2011-08-19 18:10:58 +0300600
601 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
602
603 pm_runtime_put(&pdev->dev);
604 pm_runtime_disable(&pdev->dev);
605
606 dwc3_debugfs_exit(dwc);
607
Felipe Balbi0949e992011-10-12 10:44:56 +0300608 switch (dwc->mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300609 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300610 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300611 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300612 case DWC3_MODE_HOST:
613 dwc3_host_exit(dwc);
614 break;
615 case DWC3_MODE_DRD:
616 dwc3_host_exit(dwc);
617 dwc3_gadget_exit(dwc);
618 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300619 default:
620 /* do nothing */
621 break;
622 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300623
624 dwc3_core_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300625
626 return 0;
627}
628
Felipe Balbi72246da2011-08-19 18:10:58 +0300629static struct platform_driver dwc3_driver = {
630 .probe = dwc3_probe,
631 .remove = __devexit_p(dwc3_remove),
632 .driver = {
633 .name = "dwc3",
634 },
Felipe Balbi72246da2011-08-19 18:10:58 +0300635};
636
Tobias Klauserb1116dc2012-02-28 12:57:20 +0100637module_platform_driver(dwc3_driver);
638
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +0200639MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +0300640MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
641MODULE_LICENSE("Dual BSD/GPL");
642MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");