blob: b81b3357f0074b8c1e706dc2713136fbaf912e0a [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
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +010069void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
70{
71 u32 reg;
72
73 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
74 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
75 reg |= DWC3_GCTL_PRTCAPDIR(mode);
76 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
77}
Felipe Balbi8300dd22011-10-18 13:54:01 +030078
Felipe Balbi72246da2011-08-19 18:10:58 +030079/**
80 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
81 * @dwc: pointer to our context structure
82 */
83static void dwc3_core_soft_reset(struct dwc3 *dwc)
84{
85 u32 reg;
86
87 /* Before Resetting PHY, put Core in Reset */
88 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
89 reg |= DWC3_GCTL_CORESOFTRESET;
90 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
91
92 /* Assert USB3 PHY reset */
93 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
94 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
95 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
96
97 /* Assert USB2 PHY reset */
98 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
99 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
100 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
101
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300102 usb_phy_init(dwc->usb2_phy);
103 usb_phy_init(dwc->usb3_phy);
Felipe Balbi72246da2011-08-19 18:10:58 +0300104 mdelay(100);
105
106 /* Clear USB3 PHY reset */
107 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
108 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
109 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
110
111 /* Clear USB2 PHY reset */
112 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
113 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
114 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
115
Pratyush Anand45627ac2012-06-21 17:44:28 +0530116 mdelay(100);
117
Felipe Balbi72246da2011-08-19 18:10:58 +0300118 /* After PHYs are stable we can take Core out of reset state */
119 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
120 reg &= ~DWC3_GCTL_CORESOFTRESET;
121 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
122}
123
124/**
125 * dwc3_free_one_event_buffer - Frees one event buffer
126 * @dwc: Pointer to our controller context structure
127 * @evt: Pointer to event buffer to be freed
128 */
129static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
130 struct dwc3_event_buffer *evt)
131{
132 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300133}
134
135/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800136 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300137 * @dwc: Pointer to our controller context structure
138 * @length: size of the event buffer
139 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800140 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300141 * otherwise ERR_PTR(errno).
142 */
Felipe Balbi67d0b502013-02-22 16:31:07 +0200143static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
144 unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300145{
146 struct dwc3_event_buffer *evt;
147
Felipe Balbi380f0d22012-10-11 13:48:36 +0300148 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300149 if (!evt)
150 return ERR_PTR(-ENOMEM);
151
152 evt->dwc = dwc;
153 evt->length = length;
154 evt->buf = dma_alloc_coherent(dwc->dev, length,
155 &evt->dma, GFP_KERNEL);
Felipe Balbie32672f2012-11-08 15:26:41 +0200156 if (!evt->buf)
Felipe Balbi72246da2011-08-19 18:10:58 +0300157 return ERR_PTR(-ENOMEM);
Felipe Balbi72246da2011-08-19 18:10:58 +0300158
159 return evt;
160}
161
162/**
163 * dwc3_free_event_buffers - frees all allocated event buffers
164 * @dwc: Pointer to our controller context structure
165 */
166static void dwc3_free_event_buffers(struct dwc3 *dwc)
167{
168 struct dwc3_event_buffer *evt;
169 int i;
170
Felipe Balbi9f622b22011-10-12 10:31:04 +0300171 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300172 evt = dwc->ev_buffs[i];
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900173 if (evt)
Felipe Balbi72246da2011-08-19 18:10:58 +0300174 dwc3_free_one_event_buffer(dwc, evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300175 }
176}
177
178/**
179 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800180 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300181 * @length: size of event buffer
182 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800183 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300184 * may contain some buffers allocated but not all which were requested.
185 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500186static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300187{
Felipe Balbi9f622b22011-10-12 10:31:04 +0300188 int num;
Felipe Balbi72246da2011-08-19 18:10:58 +0300189 int i;
190
Felipe Balbi9f622b22011-10-12 10:31:04 +0300191 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
192 dwc->num_event_buffers = num;
193
Felipe Balbi380f0d22012-10-11 13:48:36 +0300194 dwc->ev_buffs = devm_kzalloc(dwc->dev, sizeof(*dwc->ev_buffs) * num,
195 GFP_KERNEL);
Felipe Balbi457d3f22011-10-24 12:03:13 +0300196 if (!dwc->ev_buffs) {
197 dev_err(dwc->dev, "can't allocate event buffers array\n");
198 return -ENOMEM;
199 }
200
Felipe Balbi72246da2011-08-19 18:10:58 +0300201 for (i = 0; i < num; i++) {
202 struct dwc3_event_buffer *evt;
203
204 evt = dwc3_alloc_one_event_buffer(dwc, length);
205 if (IS_ERR(evt)) {
206 dev_err(dwc->dev, "can't allocate event buffer\n");
207 return PTR_ERR(evt);
208 }
209 dwc->ev_buffs[i] = evt;
210 }
211
212 return 0;
213}
214
215/**
216 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800217 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300218 *
219 * Returns 0 on success otherwise negative errno.
220 */
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300221static int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300222{
223 struct dwc3_event_buffer *evt;
224 int n;
225
Felipe Balbi9f622b22011-10-12 10:31:04 +0300226 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300227 evt = dwc->ev_buffs[n];
228 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
229 evt->buf, (unsigned long long) evt->dma,
230 evt->length);
231
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300232 evt->lpos = 0;
233
Felipe Balbi72246da2011-08-19 18:10:58 +0300234 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
235 lower_32_bits(evt->dma));
236 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
237 upper_32_bits(evt->dma));
238 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
239 evt->length & 0xffff);
240 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
241 }
242
243 return 0;
244}
245
246static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
247{
248 struct dwc3_event_buffer *evt;
249 int n;
250
Felipe Balbi9f622b22011-10-12 10:31:04 +0300251 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300252 evt = dwc->ev_buffs[n];
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300253
254 evt->lpos = 0;
255
Felipe Balbi72246da2011-08-19 18:10:58 +0300256 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
257 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
258 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
259 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
260 }
261}
262
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500263static void dwc3_cache_hwparams(struct dwc3 *dwc)
Felipe Balbi26ceca92011-09-30 10:58:49 +0300264{
265 struct dwc3_hwparams *parms = &dwc->hwparams;
266
267 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
268 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
269 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
270 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
271 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
272 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
273 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
274 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
275 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
276}
277
Felipe Balbi72246da2011-08-19 18:10:58 +0300278/**
279 * dwc3_core_init - Low-level initialization of DWC3 Core
280 * @dwc: Pointer to our controller context structure
281 *
282 * Returns 0 on success otherwise negative errno.
283 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500284static int dwc3_core_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300285{
286 unsigned long timeout;
287 u32 reg;
288 int ret;
289
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200290 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
291 /* This should read as U3 followed by revision number */
292 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
293 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
294 ret = -ENODEV;
295 goto err0;
296 }
Felipe Balbi248b1222011-12-14 21:59:30 +0200297 dwc->revision = reg;
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200298
Felipe Balbi72246da2011-08-19 18:10:58 +0300299 /* issue device SoftReset too */
300 timeout = jiffies + msecs_to_jiffies(500);
301 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
302 do {
303 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
304 if (!(reg & DWC3_DCTL_CSFTRST))
305 break;
306
307 if (time_after(jiffies, timeout)) {
308 dev_err(dwc->dev, "Reset Timed Out\n");
309 ret = -ETIMEDOUT;
310 goto err0;
311 }
312
313 cpu_relax();
314 } while (true);
315
Pratyush Anand58a0f232012-06-21 17:44:29 +0530316 dwc3_core_soft_reset(dwc);
317
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100318 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800319 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100320 reg &= ~DWC3_GCTL_DISSCRAMBLE;
321
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100322 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100323 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
324 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
325 break;
326 default:
327 dev_dbg(dwc->dev, "No power optimization available\n");
328 }
329
330 /*
331 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800332 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100333 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800334 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100335 */
336 if (dwc->revision < DWC3_REVISION_190A)
337 reg |= DWC3_GCTL_U2RSTECN;
338
339 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
340
Felipe Balbi72246da2011-08-19 18:10:58 +0300341 return 0;
342
Felipe Balbi72246da2011-08-19 18:10:58 +0300343err0:
344 return ret;
345}
346
347static void dwc3_core_exit(struct dwc3 *dwc)
348{
Vivek Gautam01b8daf2012-10-13 19:20:18 +0530349 usb_phy_shutdown(dwc->usb2_phy);
350 usb_phy_shutdown(dwc->usb3_phy);
Felipe Balbi72246da2011-08-19 18:10:58 +0300351}
352
353#define DWC3_ALIGN_MASK (16 - 1)
354
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500355static int dwc3_probe(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +0300356{
Felipe Balbi457e84b2012-01-18 18:04:09 +0200357 struct device_node *node = pdev->dev.of_node;
Felipe Balbi72246da2011-08-19 18:10:58 +0300358 struct resource *res;
359 struct dwc3 *dwc;
Chanho Park802ca852012-02-15 18:27:55 +0900360 struct device *dev = &pdev->dev;
Felipe Balbi0949e992011-10-12 10:44:56 +0300361
Felipe Balbi72246da2011-08-19 18:10:58 +0300362 int ret = -ENOMEM;
Felipe Balbi0949e992011-10-12 10:44:56 +0300363
364 void __iomem *regs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300365 void *mem;
366
Felipe Balbi0949e992011-10-12 10:44:56 +0300367 u8 mode;
368
Chanho Park802ca852012-02-15 18:27:55 +0900369 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300370 if (!mem) {
Chanho Park802ca852012-02-15 18:27:55 +0900371 dev_err(dev, "not enough memory\n");
372 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300373 }
374 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
375 dwc->mem = mem;
376
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300377 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300378 if (!res) {
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300379 dev_err(dev, "missing IRQ\n");
Chanho Park802ca852012-02-15 18:27:55 +0900380 return -ENODEV;
Felipe Balbi72246da2011-08-19 18:10:58 +0300381 }
Kishon Vijay Abraham I066618b2012-08-21 14:56:16 +0530382 dwc->xhci_resources[1].start = res->start;
383 dwc->xhci_resources[1].end = res->end;
384 dwc->xhci_resources[1].flags = res->flags;
385 dwc->xhci_resources[1].name = res->name;
Felipe Balbi72246da2011-08-19 18:10:58 +0300386
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300387 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
388 if (!res) {
389 dev_err(dev, "missing memory resource\n");
390 return -ENODEV;
391 }
Kishon Vijay Abraham I066618b2012-08-21 14:56:16 +0530392 dwc->xhci_resources[0].start = res->start;
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300393 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
394 DWC3_XHCI_REGS_END;
Kishon Vijay Abraham I066618b2012-08-21 14:56:16 +0530395 dwc->xhci_resources[0].flags = res->flags;
396 dwc->xhci_resources[0].name = res->name;
Felipe Balbid07e8812011-10-12 14:08:26 +0300397
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300398 /*
399 * Request memory region but exclude xHCI regs,
400 * since it will be requested by the xhci-plat driver.
401 */
402 res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START,
403 resource_size(res) - DWC3_GLOBALS_REGS_START,
Chanho Park802ca852012-02-15 18:27:55 +0900404 dev_name(dev));
Felipe Balbi72246da2011-08-19 18:10:58 +0300405 if (!res) {
Chanho Park802ca852012-02-15 18:27:55 +0900406 dev_err(dev, "can't request mem region\n");
407 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300408 }
409
Felipe Balbib7e38aa2012-08-10 09:16:43 +0300410 regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
Felipe Balbi72246da2011-08-19 18:10:58 +0300411 if (!regs) {
Chanho Park802ca852012-02-15 18:27:55 +0900412 dev_err(dev, "ioremap failed\n");
413 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300414 }
415
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530416 if (node) {
417 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
418 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
419 } else {
420 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
421 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
422 }
423
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300424 if (IS_ERR_OR_NULL(dwc->usb2_phy)) {
425 dev_err(dev, "no usb2 phy configured\n");
426 return -EPROBE_DEFER;
427 }
428
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300429 if (IS_ERR_OR_NULL(dwc->usb3_phy)) {
430 dev_err(dev, "no usb3 phy configured\n");
431 return -EPROBE_DEFER;
432 }
433
Kishon Vijay Abraham I8ba007a2013-01-25 08:30:54 +0530434 usb_phy_set_suspend(dwc->usb2_phy, 0);
435 usb_phy_set_suspend(dwc->usb3_phy, 0);
436
Felipe Balbi72246da2011-08-19 18:10:58 +0300437 spin_lock_init(&dwc->lock);
438 platform_set_drvdata(pdev, dwc);
439
440 dwc->regs = regs;
441 dwc->regs_size = resource_size(res);
Chanho Park802ca852012-02-15 18:27:55 +0900442 dwc->dev = dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300443
Felipe Balbi6c167fc2011-10-07 22:55:04 +0300444 if (!strncmp("super", maximum_speed, 5))
445 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
446 else if (!strncmp("high", maximum_speed, 4))
447 dwc->maximum_speed = DWC3_DCFG_HIGHSPEED;
448 else if (!strncmp("full", maximum_speed, 4))
449 dwc->maximum_speed = DWC3_DCFG_FULLSPEED1;
450 else if (!strncmp("low", maximum_speed, 3))
451 dwc->maximum_speed = DWC3_DCFG_LOWSPEED;
452 else
453 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
454
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530455 dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize");
Felipe Balbi457e84b2012-01-18 18:04:09 +0200456
Chanho Park802ca852012-02-15 18:27:55 +0900457 pm_runtime_enable(dev);
458 pm_runtime_get_sync(dev);
459 pm_runtime_forbid(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300460
Kishon Vijay Abraham I4fd24482012-11-16 12:07:54 +0530461 dwc3_cache_hwparams(dwc);
462
Felipe Balbi39214262012-10-11 13:54:36 +0300463 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
464 if (ret) {
465 dev_err(dwc->dev, "failed to allocate event buffers\n");
466 ret = -ENOMEM;
467 goto err0;
468 }
469
Felipe Balbi72246da2011-08-19 18:10:58 +0300470 ret = dwc3_core_init(dwc);
471 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900472 dev_err(dev, "failed to initialize core\n");
Felipe Balbi39214262012-10-11 13:54:36 +0300473 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300474 }
475
Felipe Balbif122d332013-02-08 15:15:11 +0200476 ret = dwc3_event_buffers_setup(dwc);
477 if (ret) {
478 dev_err(dwc->dev, "failed to setup event buffers\n");
479 goto err1;
480 }
481
Felipe Balbi0949e992011-10-12 10:44:56 +0300482 mode = DWC3_MODE(dwc->hwparams.hwparams0);
483
484 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300485 case DWC3_MODE_DEVICE:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100486 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300487 ret = dwc3_gadget_init(dwc);
488 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900489 dev_err(dev, "failed to initialize gadget\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200490 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +0300491 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300492 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300493 case DWC3_MODE_HOST:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100494 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
Felipe Balbid07e8812011-10-12 14:08:26 +0300495 ret = dwc3_host_init(dwc);
496 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900497 dev_err(dev, "failed to initialize host\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200498 goto err2;
Felipe Balbid07e8812011-10-12 14:08:26 +0300499 }
500 break;
501 case DWC3_MODE_DRD:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100502 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
Felipe Balbid07e8812011-10-12 14:08:26 +0300503 ret = dwc3_host_init(dwc);
504 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900505 dev_err(dev, "failed to initialize host\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200506 goto err2;
Felipe Balbid07e8812011-10-12 14:08:26 +0300507 }
508
509 ret = dwc3_gadget_init(dwc);
510 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900511 dev_err(dev, "failed to initialize gadget\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200512 goto err2;
Felipe Balbid07e8812011-10-12 14:08:26 +0300513 }
514 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300515 default:
Chanho Park802ca852012-02-15 18:27:55 +0900516 dev_err(dev, "Unsupported mode of operation %d\n", mode);
Felipe Balbif122d332013-02-08 15:15:11 +0200517 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +0300518 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300519 dwc->mode = mode;
Felipe Balbi72246da2011-08-19 18:10:58 +0300520
521 ret = dwc3_debugfs_init(dwc);
522 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900523 dev_err(dev, "failed to initialize debugfs\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200524 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +0300525 }
526
Chanho Park802ca852012-02-15 18:27:55 +0900527 pm_runtime_allow(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300528
529 return 0;
530
Felipe Balbif122d332013-02-08 15:15:11 +0200531err3:
Felipe Balbi0949e992011-10-12 10:44:56 +0300532 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300533 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300534 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300535 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300536 case DWC3_MODE_HOST:
537 dwc3_host_exit(dwc);
538 break;
539 case DWC3_MODE_DRD:
540 dwc3_host_exit(dwc);
541 dwc3_gadget_exit(dwc);
542 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300543 default:
544 /* do nothing */
545 break;
546 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300547
Felipe Balbif122d332013-02-08 15:15:11 +0200548err2:
549 dwc3_event_buffers_cleanup(dwc);
550
Chanho Park802ca852012-02-15 18:27:55 +0900551err1:
Felipe Balbi72246da2011-08-19 18:10:58 +0300552 dwc3_core_exit(dwc);
553
Felipe Balbi39214262012-10-11 13:54:36 +0300554err0:
555 dwc3_free_event_buffers(dwc);
556
Felipe Balbi72246da2011-08-19 18:10:58 +0300557 return ret;
558}
559
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500560static int dwc3_remove(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +0300561{
Felipe Balbi72246da2011-08-19 18:10:58 +0300562 struct dwc3 *dwc = platform_get_drvdata(pdev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300563
Kishon Vijay Abraham I8ba007a2013-01-25 08:30:54 +0530564 usb_phy_set_suspend(dwc->usb2_phy, 1);
565 usb_phy_set_suspend(dwc->usb3_phy, 1);
566
Felipe Balbi72246da2011-08-19 18:10:58 +0300567 pm_runtime_put(&pdev->dev);
568 pm_runtime_disable(&pdev->dev);
569
570 dwc3_debugfs_exit(dwc);
571
Felipe Balbi0949e992011-10-12 10:44:56 +0300572 switch (dwc->mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300573 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300574 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300575 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300576 case DWC3_MODE_HOST:
577 dwc3_host_exit(dwc);
578 break;
579 case DWC3_MODE_DRD:
580 dwc3_host_exit(dwc);
581 dwc3_gadget_exit(dwc);
582 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300583 default:
584 /* do nothing */
585 break;
586 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300587
Felipe Balbif122d332013-02-08 15:15:11 +0200588 dwc3_event_buffers_cleanup(dwc);
Felipe Balbid9b43302013-02-08 15:14:16 +0200589 dwc3_free_event_buffers(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300590 dwc3_core_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300591
592 return 0;
593}
594
Felipe Balbi7415f172012-04-30 14:56:33 +0300595#ifdef CONFIG_PM
596static int dwc3_prepare(struct device *dev)
597{
598 struct dwc3 *dwc = dev_get_drvdata(dev);
599 unsigned long flags;
600
601 spin_lock_irqsave(&dwc->lock, flags);
602
603 switch (dwc->mode) {
604 case DWC3_MODE_DEVICE:
605 case DWC3_MODE_DRD:
606 dwc3_gadget_prepare(dwc);
607 /* FALLTHROUGH */
608 case DWC3_MODE_HOST:
609 default:
610 dwc3_event_buffers_cleanup(dwc);
611 break;
612 }
613
614 spin_unlock_irqrestore(&dwc->lock, flags);
615
616 return 0;
617}
618
619static void dwc3_complete(struct device *dev)
620{
621 struct dwc3 *dwc = dev_get_drvdata(dev);
622 unsigned long flags;
623
624 spin_lock_irqsave(&dwc->lock, flags);
625
626 switch (dwc->mode) {
627 case DWC3_MODE_DEVICE:
628 case DWC3_MODE_DRD:
629 dwc3_gadget_complete(dwc);
630 /* FALLTHROUGH */
631 case DWC3_MODE_HOST:
632 default:
633 dwc3_event_buffers_setup(dwc);
634 break;
635 }
636
637 spin_unlock_irqrestore(&dwc->lock, flags);
638}
639
640static int dwc3_suspend(struct device *dev)
641{
642 struct dwc3 *dwc = dev_get_drvdata(dev);
643 unsigned long flags;
644
645 spin_lock_irqsave(&dwc->lock, flags);
646
647 switch (dwc->mode) {
648 case DWC3_MODE_DEVICE:
649 case DWC3_MODE_DRD:
650 dwc3_gadget_suspend(dwc);
651 /* FALLTHROUGH */
652 case DWC3_MODE_HOST:
653 default:
654 /* do nothing */
655 break;
656 }
657
658 dwc->gctl = dwc3_readl(dwc->regs, DWC3_GCTL);
659 spin_unlock_irqrestore(&dwc->lock, flags);
660
661 usb_phy_shutdown(dwc->usb3_phy);
662 usb_phy_shutdown(dwc->usb2_phy);
663
664 return 0;
665}
666
667static int dwc3_resume(struct device *dev)
668{
669 struct dwc3 *dwc = dev_get_drvdata(dev);
670 unsigned long flags;
671
672 usb_phy_init(dwc->usb3_phy);
673 usb_phy_init(dwc->usb2_phy);
674 msleep(100);
675
676 spin_lock_irqsave(&dwc->lock, flags);
677
678 dwc3_writel(dwc->regs, DWC3_GCTL, dwc->gctl);
679
680 switch (dwc->mode) {
681 case DWC3_MODE_DEVICE:
682 case DWC3_MODE_DRD:
683 dwc3_gadget_resume(dwc);
684 /* FALLTHROUGH */
685 case DWC3_MODE_HOST:
686 default:
687 /* do nothing */
688 break;
689 }
690
691 spin_unlock_irqrestore(&dwc->lock, flags);
692
693 pm_runtime_disable(dev);
694 pm_runtime_set_active(dev);
695 pm_runtime_enable(dev);
696
697 return 0;
698}
699
700static const struct dev_pm_ops dwc3_dev_pm_ops = {
701 .prepare = dwc3_prepare,
702 .complete = dwc3_complete,
703
704 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
705};
706
707#define DWC3_PM_OPS &(dwc3_dev_pm_ops)
708#else
709#define DWC3_PM_OPS NULL
710#endif
711
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530712#ifdef CONFIG_OF
713static const struct of_device_id of_dwc3_match[] = {
714 {
715 .compatible = "synopsys,dwc3"
716 },
717 { },
718};
719MODULE_DEVICE_TABLE(of, of_dwc3_match);
720#endif
721
Felipe Balbi72246da2011-08-19 18:10:58 +0300722static struct platform_driver dwc3_driver = {
723 .probe = dwc3_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500724 .remove = dwc3_remove,
Felipe Balbi72246da2011-08-19 18:10:58 +0300725 .driver = {
726 .name = "dwc3",
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530727 .of_match_table = of_match_ptr(of_dwc3_match),
Felipe Balbi7415f172012-04-30 14:56:33 +0300728 .pm = DWC3_PM_OPS,
Felipe Balbi72246da2011-08-19 18:10:58 +0300729 },
Felipe Balbi72246da2011-08-19 18:10:58 +0300730};
731
Tobias Klauserb1116dc2012-02-28 12:57:20 +0100732module_platform_driver(dwc3_driver);
733
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +0200734MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +0300735MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
736MODULE_LICENSE("Dual BSD/GPL");
737MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");