blob: cb1eb37c6d6cf3b258c9fd6b384b01ba1033bfd8 [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 *
Felipe Balbi5945f782013-06-30 14:15:11 +03009 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
Felipe Balbi72246da2011-08-19 18:10:58 +030012 *
Felipe Balbi5945f782013-06-30 14:15:11 +030013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Felipe Balbi72246da2011-08-19 18:10:58 +030017 *
Felipe Balbi5945f782013-06-30 14:15:11 +030018 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Felipe Balbi72246da2011-08-19 18:10:58 +030020 */
21
Felipe Balbia72e6582011-09-05 13:37:28 +030022#include <linux/module.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030023#include <linux/kernel.h>
24#include <linux/slab.h>
25#include <linux/spinlock.h>
26#include <linux/platform_device.h>
27#include <linux/pm_runtime.h>
28#include <linux/interrupt.h>
29#include <linux/ioport.h>
30#include <linux/io.h>
31#include <linux/list.h>
32#include <linux/delay.h>
33#include <linux/dma-mapping.h>
Felipe Balbi457e84b2012-01-18 18:04:09 +020034#include <linux/of.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030035
Felipe Balbi51e1e7b2012-07-19 14:09:48 +030036#include <linux/usb/otg.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030037#include <linux/usb/ch9.h>
38#include <linux/usb/gadget.h>
39
Felipe Balbi6462cbd2013-06-30 14:19:33 +030040#include "platform_data.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030041#include "core.h"
42#include "gadget.h"
43#include "io.h"
44
45#include "debug.h"
46
Felipe Balbi6c167fc2011-10-07 22:55:04 +030047static char *maximum_speed = "super";
48module_param(maximum_speed, charp, 0);
49MODULE_PARM_DESC(maximum_speed, "Maximum supported speed.");
50
Felipe Balbi8300dd22011-10-18 13:54:01 +030051/* -------------------------------------------------------------------------- */
52
Sebastian Andrzej Siewior3140e8cb2011-10-31 22:25:40 +010053void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
54{
55 u32 reg;
56
57 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
58 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
59 reg |= DWC3_GCTL_PRTCAPDIR(mode);
60 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
61}
Felipe Balbi8300dd22011-10-18 13:54:01 +030062
Felipe Balbi72246da2011-08-19 18:10:58 +030063/**
64 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
65 * @dwc: pointer to our context structure
66 */
67static void dwc3_core_soft_reset(struct dwc3 *dwc)
68{
69 u32 reg;
70
71 /* Before Resetting PHY, put Core in Reset */
72 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
73 reg |= DWC3_GCTL_CORESOFTRESET;
74 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
75
76 /* Assert USB3 PHY reset */
77 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
78 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
79 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
80
81 /* Assert USB2 PHY reset */
82 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
83 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
84 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
85
Felipe Balbi51e1e7b2012-07-19 14:09:48 +030086 usb_phy_init(dwc->usb2_phy);
87 usb_phy_init(dwc->usb3_phy);
Felipe Balbi72246da2011-08-19 18:10:58 +030088 mdelay(100);
89
90 /* Clear USB3 PHY reset */
91 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
92 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
93 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
94
95 /* Clear USB2 PHY reset */
96 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
97 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
98 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
99
Pratyush Anand45627ac2012-06-21 17:44:28 +0530100 mdelay(100);
101
Felipe Balbi72246da2011-08-19 18:10:58 +0300102 /* After PHYs are stable we can take Core out of reset state */
103 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
104 reg &= ~DWC3_GCTL_CORESOFTRESET;
105 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
106}
107
108/**
109 * dwc3_free_one_event_buffer - Frees one event buffer
110 * @dwc: Pointer to our controller context structure
111 * @evt: Pointer to event buffer to be freed
112 */
113static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
114 struct dwc3_event_buffer *evt)
115{
116 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300117}
118
119/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800120 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300121 * @dwc: Pointer to our controller context structure
122 * @length: size of the event buffer
123 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800124 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300125 * otherwise ERR_PTR(errno).
126 */
Felipe Balbi67d0b502013-02-22 16:31:07 +0200127static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
128 unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300129{
130 struct dwc3_event_buffer *evt;
131
Felipe Balbi380f0d22012-10-11 13:48:36 +0300132 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300133 if (!evt)
134 return ERR_PTR(-ENOMEM);
135
136 evt->dwc = dwc;
137 evt->length = length;
138 evt->buf = dma_alloc_coherent(dwc->dev, length,
139 &evt->dma, GFP_KERNEL);
Felipe Balbie32672f2012-11-08 15:26:41 +0200140 if (!evt->buf)
Felipe Balbi72246da2011-08-19 18:10:58 +0300141 return ERR_PTR(-ENOMEM);
Felipe Balbi72246da2011-08-19 18:10:58 +0300142
143 return evt;
144}
145
146/**
147 * dwc3_free_event_buffers - frees all allocated event buffers
148 * @dwc: Pointer to our controller context structure
149 */
150static void dwc3_free_event_buffers(struct dwc3 *dwc)
151{
152 struct dwc3_event_buffer *evt;
153 int i;
154
Felipe Balbi9f622b22011-10-12 10:31:04 +0300155 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300156 evt = dwc->ev_buffs[i];
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900157 if (evt)
Felipe Balbi72246da2011-08-19 18:10:58 +0300158 dwc3_free_one_event_buffer(dwc, evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300159 }
160}
161
162/**
163 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800164 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300165 * @length: size of event buffer
166 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800167 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300168 * may contain some buffers allocated but not all which were requested.
169 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500170static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300171{
Felipe Balbi9f622b22011-10-12 10:31:04 +0300172 int num;
Felipe Balbi72246da2011-08-19 18:10:58 +0300173 int i;
174
Felipe Balbi9f622b22011-10-12 10:31:04 +0300175 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
176 dwc->num_event_buffers = num;
177
Felipe Balbi380f0d22012-10-11 13:48:36 +0300178 dwc->ev_buffs = devm_kzalloc(dwc->dev, sizeof(*dwc->ev_buffs) * num,
179 GFP_KERNEL);
Felipe Balbi457d3f22011-10-24 12:03:13 +0300180 if (!dwc->ev_buffs) {
181 dev_err(dwc->dev, "can't allocate event buffers array\n");
182 return -ENOMEM;
183 }
184
Felipe Balbi72246da2011-08-19 18:10:58 +0300185 for (i = 0; i < num; i++) {
186 struct dwc3_event_buffer *evt;
187
188 evt = dwc3_alloc_one_event_buffer(dwc, length);
189 if (IS_ERR(evt)) {
190 dev_err(dwc->dev, "can't allocate event buffer\n");
191 return PTR_ERR(evt);
192 }
193 dwc->ev_buffs[i] = evt;
194 }
195
196 return 0;
197}
198
199/**
200 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800201 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300202 *
203 * Returns 0 on success otherwise negative errno.
204 */
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300205static int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300206{
207 struct dwc3_event_buffer *evt;
208 int n;
209
Felipe Balbi9f622b22011-10-12 10:31:04 +0300210 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300211 evt = dwc->ev_buffs[n];
212 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
213 evt->buf, (unsigned long long) evt->dma,
214 evt->length);
215
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300216 evt->lpos = 0;
217
Felipe Balbi72246da2011-08-19 18:10:58 +0300218 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
219 lower_32_bits(evt->dma));
220 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
221 upper_32_bits(evt->dma));
222 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
223 evt->length & 0xffff);
224 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
225 }
226
227 return 0;
228}
229
230static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
231{
232 struct dwc3_event_buffer *evt;
233 int n;
234
Felipe Balbi9f622b22011-10-12 10:31:04 +0300235 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300236 evt = dwc->ev_buffs[n];
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300237
238 evt->lpos = 0;
239
Felipe Balbi72246da2011-08-19 18:10:58 +0300240 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
241 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
242 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
243 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
244 }
245}
246
Felipe Balbi789451f62011-05-05 15:53:10 +0300247static void dwc3_core_num_eps(struct dwc3 *dwc)
248{
249 struct dwc3_hwparams *parms = &dwc->hwparams;
250
251 dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
252 dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
253
254 dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n",
255 dwc->num_in_eps, dwc->num_out_eps);
256}
257
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500258static void dwc3_cache_hwparams(struct dwc3 *dwc)
Felipe Balbi26ceca92011-09-30 10:58:49 +0300259{
260 struct dwc3_hwparams *parms = &dwc->hwparams;
261
262 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
263 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
264 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
265 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
266 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
267 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
268 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
269 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
270 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
271}
272
Felipe Balbi72246da2011-08-19 18:10:58 +0300273/**
274 * dwc3_core_init - Low-level initialization of DWC3 Core
275 * @dwc: Pointer to our controller context structure
276 *
277 * Returns 0 on success otherwise negative errno.
278 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500279static int dwc3_core_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300280{
281 unsigned long timeout;
282 u32 reg;
283 int ret;
284
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200285 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
286 /* This should read as U3 followed by revision number */
287 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
288 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
289 ret = -ENODEV;
290 goto err0;
291 }
Felipe Balbi248b1222011-12-14 21:59:30 +0200292 dwc->revision = reg;
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200293
Felipe Balbi72246da2011-08-19 18:10:58 +0300294 /* issue device SoftReset too */
295 timeout = jiffies + msecs_to_jiffies(500);
296 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
297 do {
298 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
299 if (!(reg & DWC3_DCTL_CSFTRST))
300 break;
301
302 if (time_after(jiffies, timeout)) {
303 dev_err(dwc->dev, "Reset Timed Out\n");
304 ret = -ETIMEDOUT;
305 goto err0;
306 }
307
308 cpu_relax();
309 } while (true);
310
Pratyush Anand58a0f232012-06-21 17:44:29 +0530311 dwc3_core_soft_reset(dwc);
312
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100313 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800314 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100315 reg &= ~DWC3_GCTL_DISSCRAMBLE;
316
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100317 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100318 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
319 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
320 break;
321 default:
322 dev_dbg(dwc->dev, "No power optimization available\n");
323 }
324
325 /*
326 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800327 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100328 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800329 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100330 */
331 if (dwc->revision < DWC3_REVISION_190A)
332 reg |= DWC3_GCTL_U2RSTECN;
333
Felipe Balbi789451f62011-05-05 15:53:10 +0300334 dwc3_core_num_eps(dwc);
335
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100336 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
337
Felipe Balbi72246da2011-08-19 18:10:58 +0300338 return 0;
339
Felipe Balbi72246da2011-08-19 18:10:58 +0300340err0:
341 return ret;
342}
343
344static void dwc3_core_exit(struct dwc3 *dwc)
345{
Vivek Gautam01b8daf2012-10-13 19:20:18 +0530346 usb_phy_shutdown(dwc->usb2_phy);
347 usb_phy_shutdown(dwc->usb3_phy);
Felipe Balbi72246da2011-08-19 18:10:58 +0300348}
349
350#define DWC3_ALIGN_MASK (16 - 1)
351
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500352static int dwc3_probe(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +0300353{
Felipe Balbi6462cbd2013-06-30 14:19:33 +0300354 struct dwc3_platform_data *pdata = pdev->dev.platform_data;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200355 struct device_node *node = pdev->dev.of_node;
Felipe Balbi72246da2011-08-19 18:10:58 +0300356 struct resource *res;
357 struct dwc3 *dwc;
Chanho Park802ca852012-02-15 18:27:55 +0900358 struct device *dev = &pdev->dev;
Felipe Balbi0949e992011-10-12 10:44:56 +0300359
Felipe Balbi72246da2011-08-19 18:10:58 +0300360 int ret = -ENOMEM;
Felipe Balbi0949e992011-10-12 10:44:56 +0300361
362 void __iomem *regs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300363 void *mem;
364
Felipe Balbi0949e992011-10-12 10:44:56 +0300365 u8 mode;
366
Chanho Park802ca852012-02-15 18:27:55 +0900367 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300368 if (!mem) {
Chanho Park802ca852012-02-15 18:27:55 +0900369 dev_err(dev, "not enough memory\n");
370 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300371 }
372 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
373 dwc->mem = mem;
374
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300375 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300376 if (!res) {
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300377 dev_err(dev, "missing IRQ\n");
Chanho Park802ca852012-02-15 18:27:55 +0900378 return -ENODEV;
Felipe Balbi72246da2011-08-19 18:10:58 +0300379 }
Kishon Vijay Abraham I066618b2012-08-21 14:56:16 +0530380 dwc->xhci_resources[1].start = res->start;
381 dwc->xhci_resources[1].end = res->end;
382 dwc->xhci_resources[1].flags = res->flags;
383 dwc->xhci_resources[1].name = res->name;
Felipe Balbi72246da2011-08-19 18:10:58 +0300384
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300385 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
386 if (!res) {
387 dev_err(dev, "missing memory resource\n");
388 return -ENODEV;
389 }
Kishon Vijay Abraham I066618b2012-08-21 14:56:16 +0530390 dwc->xhci_resources[0].start = res->start;
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300391 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
392 DWC3_XHCI_REGS_END;
Kishon Vijay Abraham I066618b2012-08-21 14:56:16 +0530393 dwc->xhci_resources[0].flags = res->flags;
394 dwc->xhci_resources[0].name = res->name;
Felipe Balbid07e8812011-10-12 14:08:26 +0300395
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300396 /*
397 * Request memory region but exclude xHCI regs,
398 * since it will be requested by the xhci-plat driver.
399 */
400 res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START,
401 resource_size(res) - DWC3_GLOBALS_REGS_START,
Chanho Park802ca852012-02-15 18:27:55 +0900402 dev_name(dev));
Felipe Balbi72246da2011-08-19 18:10:58 +0300403 if (!res) {
Chanho Park802ca852012-02-15 18:27:55 +0900404 dev_err(dev, "can't request mem region\n");
405 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300406 }
407
Felipe Balbib7e38aa2012-08-10 09:16:43 +0300408 regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
Felipe Balbi72246da2011-08-19 18:10:58 +0300409 if (!regs) {
Chanho Park802ca852012-02-15 18:27:55 +0900410 dev_err(dev, "ioremap failed\n");
411 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300412 }
413
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530414 if (node) {
415 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
416 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
Felipe Balbi6462cbd2013-06-30 14:19:33 +0300417
418 dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize");
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530419 } 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);
Felipe Balbi6462cbd2013-06-30 14:19:33 +0300422
423 dwc->needs_fifo_resize = pdata->tx_fifo_resize;
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530424 }
425
Felipe Balbid105e7f2013-03-15 10:52:08 +0200426 if (IS_ERR(dwc->usb2_phy)) {
427 ret = PTR_ERR(dwc->usb2_phy);
428
429 /*
430 * if -ENXIO is returned, it means PHY layer wasn't
431 * enabled, so it makes no sense to return -EPROBE_DEFER
432 * in that case, since no PHY driver will ever probe.
433 */
434 if (ret == -ENXIO)
435 return ret;
436
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300437 dev_err(dev, "no usb2 phy configured\n");
438 return -EPROBE_DEFER;
439 }
440
Felipe Balbid105e7f2013-03-15 10:52:08 +0200441 if (IS_ERR(dwc->usb3_phy)) {
Ruchika Kharwar315955d72013-07-04 00:59:34 -0500442 ret = PTR_ERR(dwc->usb3_phy);
Felipe Balbid105e7f2013-03-15 10:52:08 +0200443
444 /*
445 * if -ENXIO is returned, it means PHY layer wasn't
446 * enabled, so it makes no sense to return -EPROBE_DEFER
447 * in that case, since no PHY driver will ever probe.
448 */
449 if (ret == -ENXIO)
450 return ret;
451
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300452 dev_err(dev, "no usb3 phy configured\n");
453 return -EPROBE_DEFER;
454 }
455
Kishon Vijay Abraham I8ba007a2013-01-25 08:30:54 +0530456 usb_phy_set_suspend(dwc->usb2_phy, 0);
457 usb_phy_set_suspend(dwc->usb3_phy, 0);
458
Felipe Balbi72246da2011-08-19 18:10:58 +0300459 spin_lock_init(&dwc->lock);
460 platform_set_drvdata(pdev, dwc);
461
462 dwc->regs = regs;
463 dwc->regs_size = resource_size(res);
Chanho Park802ca852012-02-15 18:27:55 +0900464 dwc->dev = dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300465
Kishon Vijay Abraham Iddff14f2013-03-07 18:51:43 +0530466 dev->dma_mask = dev->parent->dma_mask;
467 dev->dma_parms = dev->parent->dma_parms;
468 dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
469
Felipe Balbi6c167fc2011-10-07 22:55:04 +0300470 if (!strncmp("super", maximum_speed, 5))
471 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
472 else if (!strncmp("high", maximum_speed, 4))
473 dwc->maximum_speed = DWC3_DCFG_HIGHSPEED;
474 else if (!strncmp("full", maximum_speed, 4))
475 dwc->maximum_speed = DWC3_DCFG_FULLSPEED1;
476 else if (!strncmp("low", maximum_speed, 3))
477 dwc->maximum_speed = DWC3_DCFG_LOWSPEED;
478 else
479 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
480
Chanho Park802ca852012-02-15 18:27:55 +0900481 pm_runtime_enable(dev);
482 pm_runtime_get_sync(dev);
483 pm_runtime_forbid(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300484
Kishon Vijay Abraham I4fd24482012-11-16 12:07:54 +0530485 dwc3_cache_hwparams(dwc);
486
Felipe Balbi39214262012-10-11 13:54:36 +0300487 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
488 if (ret) {
489 dev_err(dwc->dev, "failed to allocate event buffers\n");
490 ret = -ENOMEM;
491 goto err0;
492 }
493
Felipe Balbi72246da2011-08-19 18:10:58 +0300494 ret = dwc3_core_init(dwc);
495 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900496 dev_err(dev, "failed to initialize core\n");
Felipe Balbi39214262012-10-11 13:54:36 +0300497 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300498 }
499
Felipe Balbif122d332013-02-08 15:15:11 +0200500 ret = dwc3_event_buffers_setup(dwc);
501 if (ret) {
502 dev_err(dwc->dev, "failed to setup event buffers\n");
503 goto err1;
504 }
505
Vivek Gautamcd051da2013-03-02 18:55:24 +0530506 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
507 mode = DWC3_MODE_HOST;
508 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
509 mode = DWC3_MODE_DEVICE;
510 else
511 mode = DWC3_MODE_DRD;
Felipe Balbi0949e992011-10-12 10:44:56 +0300512
513 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300514 case DWC3_MODE_DEVICE:
Sebastian Andrzej Siewior3140e8cb2011-10-31 22:25:40 +0100515 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300516 ret = dwc3_gadget_init(dwc);
517 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900518 dev_err(dev, "failed to initialize gadget\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200519 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +0300520 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300521 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300522 case DWC3_MODE_HOST:
Sebastian Andrzej Siewior3140e8cb2011-10-31 22:25:40 +0100523 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
Felipe Balbid07e8812011-10-12 14:08:26 +0300524 ret = dwc3_host_init(dwc);
525 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900526 dev_err(dev, "failed to initialize host\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200527 goto err2;
Felipe Balbid07e8812011-10-12 14:08:26 +0300528 }
529 break;
530 case DWC3_MODE_DRD:
Sebastian Andrzej Siewior3140e8cb2011-10-31 22:25:40 +0100531 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
Felipe Balbid07e8812011-10-12 14:08:26 +0300532 ret = dwc3_host_init(dwc);
533 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900534 dev_err(dev, "failed to initialize host\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200535 goto err2;
Felipe Balbid07e8812011-10-12 14:08:26 +0300536 }
537
538 ret = dwc3_gadget_init(dwc);
539 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900540 dev_err(dev, "failed to initialize gadget\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200541 goto err2;
Felipe Balbid07e8812011-10-12 14:08:26 +0300542 }
543 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300544 default:
Chanho Park802ca852012-02-15 18:27:55 +0900545 dev_err(dev, "Unsupported mode of operation %d\n", mode);
Felipe Balbif122d332013-02-08 15:15:11 +0200546 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +0300547 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300548 dwc->mode = mode;
Felipe Balbi72246da2011-08-19 18:10:58 +0300549
550 ret = dwc3_debugfs_init(dwc);
551 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900552 dev_err(dev, "failed to initialize debugfs\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200553 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +0300554 }
555
Chanho Park802ca852012-02-15 18:27:55 +0900556 pm_runtime_allow(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300557
558 return 0;
559
Felipe Balbif122d332013-02-08 15:15:11 +0200560err3:
Felipe Balbi0949e992011-10-12 10:44:56 +0300561 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300562 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300563 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300564 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300565 case DWC3_MODE_HOST:
566 dwc3_host_exit(dwc);
567 break;
568 case DWC3_MODE_DRD:
569 dwc3_host_exit(dwc);
570 dwc3_gadget_exit(dwc);
571 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300572 default:
573 /* do nothing */
574 break;
575 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300576
Felipe Balbif122d332013-02-08 15:15:11 +0200577err2:
578 dwc3_event_buffers_cleanup(dwc);
579
Chanho Park802ca852012-02-15 18:27:55 +0900580err1:
Felipe Balbi72246da2011-08-19 18:10:58 +0300581 dwc3_core_exit(dwc);
582
Felipe Balbi39214262012-10-11 13:54:36 +0300583err0:
584 dwc3_free_event_buffers(dwc);
585
Felipe Balbi72246da2011-08-19 18:10:58 +0300586 return ret;
587}
588
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500589static int dwc3_remove(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +0300590{
Felipe Balbi72246da2011-08-19 18:10:58 +0300591 struct dwc3 *dwc = platform_get_drvdata(pdev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300592
Kishon Vijay Abraham I8ba007a2013-01-25 08:30:54 +0530593 usb_phy_set_suspend(dwc->usb2_phy, 1);
594 usb_phy_set_suspend(dwc->usb3_phy, 1);
595
Felipe Balbi72246da2011-08-19 18:10:58 +0300596 pm_runtime_put(&pdev->dev);
597 pm_runtime_disable(&pdev->dev);
598
599 dwc3_debugfs_exit(dwc);
600
Felipe Balbi0949e992011-10-12 10:44:56 +0300601 switch (dwc->mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300602 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300603 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300604 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300605 case DWC3_MODE_HOST:
606 dwc3_host_exit(dwc);
607 break;
608 case DWC3_MODE_DRD:
609 dwc3_host_exit(dwc);
610 dwc3_gadget_exit(dwc);
611 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300612 default:
613 /* do nothing */
614 break;
615 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300616
Felipe Balbif122d332013-02-08 15:15:11 +0200617 dwc3_event_buffers_cleanup(dwc);
Felipe Balbid9b43302013-02-08 15:14:16 +0200618 dwc3_free_event_buffers(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300619 dwc3_core_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300620
621 return 0;
622}
623
Jingoo Han19fda7c2013-03-26 01:52:48 +0000624#ifdef CONFIG_PM_SLEEP
Felipe Balbi7415f172012-04-30 14:56:33 +0300625static int dwc3_prepare(struct device *dev)
626{
627 struct dwc3 *dwc = dev_get_drvdata(dev);
628 unsigned long flags;
629
630 spin_lock_irqsave(&dwc->lock, flags);
631
632 switch (dwc->mode) {
633 case DWC3_MODE_DEVICE:
634 case DWC3_MODE_DRD:
635 dwc3_gadget_prepare(dwc);
636 /* FALLTHROUGH */
637 case DWC3_MODE_HOST:
638 default:
639 dwc3_event_buffers_cleanup(dwc);
640 break;
641 }
642
643 spin_unlock_irqrestore(&dwc->lock, flags);
644
645 return 0;
646}
647
648static void dwc3_complete(struct device *dev)
649{
650 struct dwc3 *dwc = dev_get_drvdata(dev);
651 unsigned long flags;
652
653 spin_lock_irqsave(&dwc->lock, flags);
654
655 switch (dwc->mode) {
656 case DWC3_MODE_DEVICE:
657 case DWC3_MODE_DRD:
658 dwc3_gadget_complete(dwc);
659 /* FALLTHROUGH */
660 case DWC3_MODE_HOST:
661 default:
662 dwc3_event_buffers_setup(dwc);
663 break;
664 }
665
666 spin_unlock_irqrestore(&dwc->lock, flags);
667}
668
669static int dwc3_suspend(struct device *dev)
670{
671 struct dwc3 *dwc = dev_get_drvdata(dev);
672 unsigned long flags;
673
674 spin_lock_irqsave(&dwc->lock, flags);
675
676 switch (dwc->mode) {
677 case DWC3_MODE_DEVICE:
678 case DWC3_MODE_DRD:
679 dwc3_gadget_suspend(dwc);
680 /* FALLTHROUGH */
681 case DWC3_MODE_HOST:
682 default:
683 /* do nothing */
684 break;
685 }
686
687 dwc->gctl = dwc3_readl(dwc->regs, DWC3_GCTL);
688 spin_unlock_irqrestore(&dwc->lock, flags);
689
690 usb_phy_shutdown(dwc->usb3_phy);
691 usb_phy_shutdown(dwc->usb2_phy);
692
693 return 0;
694}
695
696static int dwc3_resume(struct device *dev)
697{
698 struct dwc3 *dwc = dev_get_drvdata(dev);
699 unsigned long flags;
700
701 usb_phy_init(dwc->usb3_phy);
702 usb_phy_init(dwc->usb2_phy);
703 msleep(100);
704
705 spin_lock_irqsave(&dwc->lock, flags);
706
707 dwc3_writel(dwc->regs, DWC3_GCTL, dwc->gctl);
708
709 switch (dwc->mode) {
710 case DWC3_MODE_DEVICE:
711 case DWC3_MODE_DRD:
712 dwc3_gadget_resume(dwc);
713 /* FALLTHROUGH */
714 case DWC3_MODE_HOST:
715 default:
716 /* do nothing */
717 break;
718 }
719
720 spin_unlock_irqrestore(&dwc->lock, flags);
721
722 pm_runtime_disable(dev);
723 pm_runtime_set_active(dev);
724 pm_runtime_enable(dev);
725
726 return 0;
727}
728
729static const struct dev_pm_ops dwc3_dev_pm_ops = {
730 .prepare = dwc3_prepare,
731 .complete = dwc3_complete,
732
733 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
734};
735
736#define DWC3_PM_OPS &(dwc3_dev_pm_ops)
737#else
738#define DWC3_PM_OPS NULL
739#endif
740
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530741#ifdef CONFIG_OF
742static const struct of_device_id of_dwc3_match[] = {
743 {
744 .compatible = "synopsys,dwc3"
745 },
746 { },
747};
748MODULE_DEVICE_TABLE(of, of_dwc3_match);
749#endif
750
Felipe Balbi72246da2011-08-19 18:10:58 +0300751static struct platform_driver dwc3_driver = {
752 .probe = dwc3_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500753 .remove = dwc3_remove,
Felipe Balbi72246da2011-08-19 18:10:58 +0300754 .driver = {
755 .name = "dwc3",
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530756 .of_match_table = of_match_ptr(of_dwc3_match),
Felipe Balbi7415f172012-04-30 14:56:33 +0300757 .pm = DWC3_PM_OPS,
Felipe Balbi72246da2011-08-19 18:10:58 +0300758 },
Felipe Balbi72246da2011-08-19 18:10:58 +0300759};
760
Tobias Klauserb1116dc2012-02-28 12:57:20 +0100761module_platform_driver(dwc3_driver);
762
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +0200763MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +0300764MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
Felipe Balbi5945f782013-06-30 14:15:11 +0300765MODULE_LICENSE("GPL v2");
Felipe Balbi72246da2011-08-19 18:10:58 +0300766MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");