blob: 474162e9d01d2e03bfaf52402a8cfc1b4461c304 [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
36#include <linux/usb/ch9.h>
37#include <linux/usb/gadget.h>
Felipe Balbif7e846f2013-06-30 14:29:51 +030038#include <linux/usb/of.h>
Ruchika Kharwara45c82b82013-07-06 07:52:49 -050039#include <linux/usb/otg.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030040
Felipe Balbi6462cbd2013-06-30 14:19:33 +030041#include "platform_data.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030042#include "core.h"
43#include "gadget.h"
44#include "io.h"
45
46#include "debug.h"
47
Felipe Balbi8300dd22011-10-18 13:54:01 +030048/* -------------------------------------------------------------------------- */
49
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +010050void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
51{
52 u32 reg;
53
54 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
55 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
56 reg |= DWC3_GCTL_PRTCAPDIR(mode);
57 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
58}
Felipe Balbi8300dd22011-10-18 13:54:01 +030059
Felipe Balbi72246da2011-08-19 18:10:58 +030060/**
61 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
62 * @dwc: pointer to our context structure
63 */
64static void dwc3_core_soft_reset(struct dwc3 *dwc)
65{
66 u32 reg;
67
68 /* Before Resetting PHY, put Core in Reset */
69 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
70 reg |= DWC3_GCTL_CORESOFTRESET;
71 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
72
73 /* Assert USB3 PHY reset */
74 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
75 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
76 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
77
78 /* Assert USB2 PHY reset */
79 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
80 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
81 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
82
Felipe Balbi51e1e7b2012-07-19 14:09:48 +030083 usb_phy_init(dwc->usb2_phy);
84 usb_phy_init(dwc->usb3_phy);
Felipe Balbi72246da2011-08-19 18:10:58 +030085 mdelay(100);
86
87 /* Clear USB3 PHY reset */
88 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
89 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
90 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
91
92 /* Clear USB2 PHY reset */
93 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
94 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
95 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
96
Pratyush Anand45627ac2012-06-21 17:44:28 +053097 mdelay(100);
98
Felipe Balbi72246da2011-08-19 18:10:58 +030099 /* After PHYs are stable we can take Core out of reset state */
100 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
101 reg &= ~DWC3_GCTL_CORESOFTRESET;
102 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
103}
104
105/**
106 * dwc3_free_one_event_buffer - Frees one event buffer
107 * @dwc: Pointer to our controller context structure
108 * @evt: Pointer to event buffer to be freed
109 */
110static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
111 struct dwc3_event_buffer *evt)
112{
113 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300114}
115
116/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800117 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300118 * @dwc: Pointer to our controller context structure
119 * @length: size of the event buffer
120 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800121 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300122 * otherwise ERR_PTR(errno).
123 */
Felipe Balbi67d0b502013-02-22 16:31:07 +0200124static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
125 unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300126{
127 struct dwc3_event_buffer *evt;
128
Felipe Balbi380f0d22012-10-11 13:48:36 +0300129 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300130 if (!evt)
131 return ERR_PTR(-ENOMEM);
132
133 evt->dwc = dwc;
134 evt->length = length;
135 evt->buf = dma_alloc_coherent(dwc->dev, length,
136 &evt->dma, GFP_KERNEL);
Felipe Balbie32672f2012-11-08 15:26:41 +0200137 if (!evt->buf)
Felipe Balbi72246da2011-08-19 18:10:58 +0300138 return ERR_PTR(-ENOMEM);
Felipe Balbi72246da2011-08-19 18:10:58 +0300139
140 return evt;
141}
142
143/**
144 * dwc3_free_event_buffers - frees all allocated event buffers
145 * @dwc: Pointer to our controller context structure
146 */
147static void dwc3_free_event_buffers(struct dwc3 *dwc)
148{
149 struct dwc3_event_buffer *evt;
150 int i;
151
Felipe Balbi9f622b22011-10-12 10:31:04 +0300152 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300153 evt = dwc->ev_buffs[i];
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900154 if (evt)
Felipe Balbi72246da2011-08-19 18:10:58 +0300155 dwc3_free_one_event_buffer(dwc, evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300156 }
157}
158
159/**
160 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800161 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300162 * @length: size of event buffer
163 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800164 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300165 * may contain some buffers allocated but not all which were requested.
166 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500167static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300168{
Felipe Balbi9f622b22011-10-12 10:31:04 +0300169 int num;
Felipe Balbi72246da2011-08-19 18:10:58 +0300170 int i;
171
Felipe Balbi9f622b22011-10-12 10:31:04 +0300172 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
173 dwc->num_event_buffers = num;
174
Felipe Balbi380f0d22012-10-11 13:48:36 +0300175 dwc->ev_buffs = devm_kzalloc(dwc->dev, sizeof(*dwc->ev_buffs) * num,
176 GFP_KERNEL);
Felipe Balbi457d3f22011-10-24 12:03:13 +0300177 if (!dwc->ev_buffs) {
178 dev_err(dwc->dev, "can't allocate event buffers array\n");
179 return -ENOMEM;
180 }
181
Felipe Balbi72246da2011-08-19 18:10:58 +0300182 for (i = 0; i < num; i++) {
183 struct dwc3_event_buffer *evt;
184
185 evt = dwc3_alloc_one_event_buffer(dwc, length);
186 if (IS_ERR(evt)) {
187 dev_err(dwc->dev, "can't allocate event buffer\n");
188 return PTR_ERR(evt);
189 }
190 dwc->ev_buffs[i] = evt;
191 }
192
193 return 0;
194}
195
196/**
197 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800198 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300199 *
200 * Returns 0 on success otherwise negative errno.
201 */
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300202static int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300203{
204 struct dwc3_event_buffer *evt;
205 int n;
206
Felipe Balbi9f622b22011-10-12 10:31:04 +0300207 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300208 evt = dwc->ev_buffs[n];
209 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
210 evt->buf, (unsigned long long) evt->dma,
211 evt->length);
212
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300213 evt->lpos = 0;
214
Felipe Balbi72246da2011-08-19 18:10:58 +0300215 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
216 lower_32_bits(evt->dma));
217 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
218 upper_32_bits(evt->dma));
219 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
Felipe Balbi68d6a012013-06-12 21:09:26 +0300220 DWC3_GEVNTSIZ_SIZE(evt->length));
Felipe Balbi72246da2011-08-19 18:10:58 +0300221 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
222 }
223
224 return 0;
225}
226
227static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
228{
229 struct dwc3_event_buffer *evt;
230 int n;
231
Felipe Balbi9f622b22011-10-12 10:31:04 +0300232 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300233 evt = dwc->ev_buffs[n];
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300234
235 evt->lpos = 0;
236
Felipe Balbi72246da2011-08-19 18:10:58 +0300237 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
238 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
Felipe Balbi68d6a012013-06-12 21:09:26 +0300239 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK
240 | DWC3_GEVNTSIZ_SIZE(0));
Felipe Balbi72246da2011-08-19 18:10:58 +0300241 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
242 }
243}
244
Felipe Balbi789451f62011-05-05 15:53:10 +0300245static void dwc3_core_num_eps(struct dwc3 *dwc)
246{
247 struct dwc3_hwparams *parms = &dwc->hwparams;
248
249 dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
250 dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
251
252 dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n",
253 dwc->num_in_eps, dwc->num_out_eps);
254}
255
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500256static void dwc3_cache_hwparams(struct dwc3 *dwc)
Felipe Balbi26ceca92011-09-30 10:58:49 +0300257{
258 struct dwc3_hwparams *parms = &dwc->hwparams;
259
260 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
261 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
262 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
263 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
264 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
265 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
266 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
267 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
268 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
269}
270
Felipe Balbi72246da2011-08-19 18:10:58 +0300271/**
272 * dwc3_core_init - Low-level initialization of DWC3 Core
273 * @dwc: Pointer to our controller context structure
274 *
275 * Returns 0 on success otherwise negative errno.
276 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500277static int dwc3_core_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300278{
279 unsigned long timeout;
280 u32 reg;
281 int ret;
282
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200283 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
284 /* This should read as U3 followed by revision number */
285 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
286 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
287 ret = -ENODEV;
288 goto err0;
289 }
Felipe Balbi248b1222011-12-14 21:59:30 +0200290 dwc->revision = reg;
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200291
Felipe Balbi72246da2011-08-19 18:10:58 +0300292 /* issue device SoftReset too */
293 timeout = jiffies + msecs_to_jiffies(500);
294 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
295 do {
296 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
297 if (!(reg & DWC3_DCTL_CSFTRST))
298 break;
299
300 if (time_after(jiffies, timeout)) {
301 dev_err(dwc->dev, "Reset Timed Out\n");
302 ret = -ETIMEDOUT;
303 goto err0;
304 }
305
306 cpu_relax();
307 } while (true);
308
Pratyush Anand58a0f232012-06-21 17:44:29 +0530309 dwc3_core_soft_reset(dwc);
310
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100311 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800312 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100313 reg &= ~DWC3_GCTL_DISSCRAMBLE;
314
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100315 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100316 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
317 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
318 break;
319 default:
320 dev_dbg(dwc->dev, "No power optimization available\n");
321 }
322
323 /*
324 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800325 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100326 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800327 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100328 */
329 if (dwc->revision < DWC3_REVISION_190A)
330 reg |= DWC3_GCTL_U2RSTECN;
331
Felipe Balbi789451f62011-05-05 15:53:10 +0300332 dwc3_core_num_eps(dwc);
333
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100334 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
335
Felipe Balbi72246da2011-08-19 18:10:58 +0300336 return 0;
337
Felipe Balbi72246da2011-08-19 18:10:58 +0300338err0:
339 return ret;
340}
341
342static void dwc3_core_exit(struct dwc3 *dwc)
343{
Vivek Gautam01b8daf2012-10-13 19:20:18 +0530344 usb_phy_shutdown(dwc->usb2_phy);
345 usb_phy_shutdown(dwc->usb3_phy);
Felipe Balbi72246da2011-08-19 18:10:58 +0300346}
347
348#define DWC3_ALIGN_MASK (16 - 1)
349
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500350static int dwc3_probe(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +0300351{
Felipe Balbi941ea362013-07-31 09:21:25 +0300352 struct device *dev = &pdev->dev;
353 struct dwc3_platform_data *pdata = dev_get_platdata(dev);
354 struct device_node *node = dev->of_node;
Felipe Balbi72246da2011-08-19 18:10:58 +0300355 struct resource *res;
356 struct dwc3 *dwc;
Felipe Balbi0949e992011-10-12 10:44:56 +0300357
Felipe Balbi72246da2011-08-19 18:10:58 +0300358 int ret = -ENOMEM;
Felipe Balbi0949e992011-10-12 10:44:56 +0300359
360 void __iomem *regs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300361 void *mem;
362
Chanho Park802ca852012-02-15 18:27:55 +0900363 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300364 if (!mem) {
Chanho Park802ca852012-02-15 18:27:55 +0900365 dev_err(dev, "not enough memory\n");
366 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300367 }
368 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
369 dwc->mem = mem;
370
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300371 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300372 if (!res) {
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300373 dev_err(dev, "missing IRQ\n");
Chanho Park802ca852012-02-15 18:27:55 +0900374 return -ENODEV;
Felipe Balbi72246da2011-08-19 18:10:58 +0300375 }
Kishon Vijay Abraham I066618b2012-08-21 14:56:16 +0530376 dwc->xhci_resources[1].start = res->start;
377 dwc->xhci_resources[1].end = res->end;
378 dwc->xhci_resources[1].flags = res->flags;
379 dwc->xhci_resources[1].name = res->name;
Felipe Balbi72246da2011-08-19 18:10:58 +0300380
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300381 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
382 if (!res) {
383 dev_err(dev, "missing memory resource\n");
384 return -ENODEV;
385 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300386
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530387 if (node) {
Felipe Balbif7e846f2013-06-30 14:29:51 +0300388 dwc->maximum_speed = of_usb_get_maximum_speed(node);
389
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530390 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
391 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
Felipe Balbi6462cbd2013-06-30 14:19:33 +0300392
393 dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize");
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500394 dwc->dr_mode = of_usb_get_dr_mode(node);
Felipe Balbibb674902013-08-14 13:21:23 -0500395 } else if (pdata) {
Felipe Balbif7e846f2013-06-30 14:29:51 +0300396 dwc->maximum_speed = pdata->maximum_speed;
397
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530398 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
399 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
Felipe Balbi6462cbd2013-06-30 14:19:33 +0300400
401 dwc->needs_fifo_resize = pdata->tx_fifo_resize;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500402 dwc->dr_mode = pdata->dr_mode;
Felipe Balbibb674902013-08-14 13:21:23 -0500403 } else {
404 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
405 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530406 }
407
Felipe Balbif7e846f2013-06-30 14:29:51 +0300408 /* default to superspeed if no maximum_speed passed */
409 if (dwc->maximum_speed == USB_SPEED_UNKNOWN)
410 dwc->maximum_speed = USB_SPEED_SUPER;
411
Felipe Balbid105e7f2013-03-15 10:52:08 +0200412 if (IS_ERR(dwc->usb2_phy)) {
413 ret = PTR_ERR(dwc->usb2_phy);
414
415 /*
416 * if -ENXIO is returned, it means PHY layer wasn't
417 * enabled, so it makes no sense to return -EPROBE_DEFER
418 * in that case, since no PHY driver will ever probe.
419 */
420 if (ret == -ENXIO)
421 return ret;
422
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300423 dev_err(dev, "no usb2 phy configured\n");
424 return -EPROBE_DEFER;
425 }
426
Felipe Balbid105e7f2013-03-15 10:52:08 +0200427 if (IS_ERR(dwc->usb3_phy)) {
Ruchika Kharwar315955d72013-07-04 00:59:34 -0500428 ret = PTR_ERR(dwc->usb3_phy);
Felipe Balbid105e7f2013-03-15 10:52:08 +0200429
430 /*
431 * if -ENXIO is returned, it means PHY layer wasn't
432 * enabled, so it makes no sense to return -EPROBE_DEFER
433 * in that case, since no PHY driver will ever probe.
434 */
435 if (ret == -ENXIO)
436 return ret;
437
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300438 dev_err(dev, "no usb3 phy configured\n");
439 return -EPROBE_DEFER;
440 }
441
Ivan T. Ivanov2e112342013-07-29 10:27:02 +0300442 dwc->xhci_resources[0].start = res->start;
443 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
444 DWC3_XHCI_REGS_END;
445 dwc->xhci_resources[0].flags = res->flags;
446 dwc->xhci_resources[0].name = res->name;
447
448 res->start += DWC3_GLOBALS_REGS_START;
449
450 /*
451 * Request memory region but exclude xHCI regs,
452 * since it will be requested by the xhci-plat driver.
453 */
454 regs = devm_ioremap_resource(dev, res);
455 if (IS_ERR(regs))
456 return PTR_ERR(regs);
457
Kishon Vijay Abraham I8ba007a2013-01-25 08:30:54 +0530458 usb_phy_set_suspend(dwc->usb2_phy, 0);
459 usb_phy_set_suspend(dwc->usb3_phy, 0);
460
Felipe Balbi72246da2011-08-19 18:10:58 +0300461 spin_lock_init(&dwc->lock);
462 platform_set_drvdata(pdev, dwc);
463
464 dwc->regs = regs;
465 dwc->regs_size = resource_size(res);
Chanho Park802ca852012-02-15 18:27:55 +0900466 dwc->dev = dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300467
Kishon Vijay Abraham Iddff14f2013-03-07 18:51:43 +0530468 dev->dma_mask = dev->parent->dma_mask;
469 dev->dma_parms = dev->parent->dma_parms;
470 dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
471
Chanho Park802ca852012-02-15 18:27:55 +0900472 pm_runtime_enable(dev);
473 pm_runtime_get_sync(dev);
474 pm_runtime_forbid(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300475
Kishon Vijay Abraham I4fd24482012-11-16 12:07:54 +0530476 dwc3_cache_hwparams(dwc);
477
Felipe Balbi39214262012-10-11 13:54:36 +0300478 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
479 if (ret) {
480 dev_err(dwc->dev, "failed to allocate event buffers\n");
481 ret = -ENOMEM;
482 goto err0;
483 }
484
Felipe Balbi72246da2011-08-19 18:10:58 +0300485 ret = dwc3_core_init(dwc);
486 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900487 dev_err(dev, "failed to initialize core\n");
Felipe Balbi39214262012-10-11 13:54:36 +0300488 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300489 }
490
Felipe Balbif122d332013-02-08 15:15:11 +0200491 ret = dwc3_event_buffers_setup(dwc);
492 if (ret) {
493 dev_err(dwc->dev, "failed to setup event buffers\n");
494 goto err1;
495 }
496
Vivek Gautamcd051da2013-03-02 18:55:24 +0530497 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500498 dwc->dr_mode = USB_DR_MODE_HOST;
Vivek Gautamcd051da2013-03-02 18:55:24 +0530499 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500500 dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
Felipe Balbi0949e992011-10-12 10:44:56 +0300501
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500502 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
503 dwc->dr_mode = USB_DR_MODE_OTG;
504
505 switch (dwc->dr_mode) {
506 case USB_DR_MODE_PERIPHERAL:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100507 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300508 ret = dwc3_gadget_init(dwc);
509 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900510 dev_err(dev, "failed to initialize gadget\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200511 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +0300512 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300513 break;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500514 case USB_DR_MODE_HOST:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100515 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
Felipe Balbid07e8812011-10-12 14:08:26 +0300516 ret = dwc3_host_init(dwc);
517 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900518 dev_err(dev, "failed to initialize host\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200519 goto err2;
Felipe Balbid07e8812011-10-12 14:08:26 +0300520 }
521 break;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500522 case USB_DR_MODE_OTG:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100523 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
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
530 ret = dwc3_gadget_init(dwc);
531 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900532 dev_err(dev, "failed to initialize gadget\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200533 goto err2;
Felipe Balbid07e8812011-10-12 14:08:26 +0300534 }
535 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300536 default:
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500537 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
Felipe Balbif122d332013-02-08 15:15:11 +0200538 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +0300539 }
540
541 ret = dwc3_debugfs_init(dwc);
542 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900543 dev_err(dev, "failed to initialize debugfs\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200544 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +0300545 }
546
Chanho Park802ca852012-02-15 18:27:55 +0900547 pm_runtime_allow(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300548
549 return 0;
550
Felipe Balbif122d332013-02-08 15:15:11 +0200551err3:
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500552 switch (dwc->dr_mode) {
553 case USB_DR_MODE_PERIPHERAL:
Felipe Balbi72246da2011-08-19 18:10:58 +0300554 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300555 break;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500556 case USB_DR_MODE_HOST:
Felipe Balbid07e8812011-10-12 14:08:26 +0300557 dwc3_host_exit(dwc);
558 break;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500559 case USB_DR_MODE_OTG:
Felipe Balbid07e8812011-10-12 14:08:26 +0300560 dwc3_host_exit(dwc);
561 dwc3_gadget_exit(dwc);
562 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300563 default:
564 /* do nothing */
565 break;
566 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300567
Felipe Balbif122d332013-02-08 15:15:11 +0200568err2:
569 dwc3_event_buffers_cleanup(dwc);
570
Chanho Park802ca852012-02-15 18:27:55 +0900571err1:
Felipe Balbi72246da2011-08-19 18:10:58 +0300572 dwc3_core_exit(dwc);
573
Felipe Balbi39214262012-10-11 13:54:36 +0300574err0:
575 dwc3_free_event_buffers(dwc);
576
Felipe Balbi72246da2011-08-19 18:10:58 +0300577 return ret;
578}
579
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500580static int dwc3_remove(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +0300581{
Felipe Balbi72246da2011-08-19 18:10:58 +0300582 struct dwc3 *dwc = platform_get_drvdata(pdev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300583
Kishon Vijay Abraham I8ba007a2013-01-25 08:30:54 +0530584 usb_phy_set_suspend(dwc->usb2_phy, 1);
585 usb_phy_set_suspend(dwc->usb3_phy, 1);
586
Felipe Balbi72246da2011-08-19 18:10:58 +0300587 pm_runtime_put(&pdev->dev);
588 pm_runtime_disable(&pdev->dev);
589
590 dwc3_debugfs_exit(dwc);
591
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500592 switch (dwc->dr_mode) {
593 case USB_DR_MODE_PERIPHERAL:
Felipe Balbi72246da2011-08-19 18:10:58 +0300594 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300595 break;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500596 case USB_DR_MODE_HOST:
Felipe Balbid07e8812011-10-12 14:08:26 +0300597 dwc3_host_exit(dwc);
598 break;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500599 case USB_DR_MODE_OTG:
Felipe Balbid07e8812011-10-12 14:08:26 +0300600 dwc3_host_exit(dwc);
601 dwc3_gadget_exit(dwc);
602 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300603 default:
604 /* do nothing */
605 break;
606 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300607
Felipe Balbif122d332013-02-08 15:15:11 +0200608 dwc3_event_buffers_cleanup(dwc);
Felipe Balbid9b43302013-02-08 15:14:16 +0200609 dwc3_free_event_buffers(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300610 dwc3_core_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300611
612 return 0;
613}
614
Jingoo Han19fda7c2013-03-26 01:52:48 +0000615#ifdef CONFIG_PM_SLEEP
Felipe Balbi7415f172012-04-30 14:56:33 +0300616static int dwc3_prepare(struct device *dev)
617{
618 struct dwc3 *dwc = dev_get_drvdata(dev);
619 unsigned long flags;
620
621 spin_lock_irqsave(&dwc->lock, flags);
622
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500623 switch (dwc->dr_mode) {
624 case USB_DR_MODE_PERIPHERAL:
625 case USB_DR_MODE_OTG:
Felipe Balbi7415f172012-04-30 14:56:33 +0300626 dwc3_gadget_prepare(dwc);
627 /* FALLTHROUGH */
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500628 case USB_DR_MODE_HOST:
Felipe Balbi7415f172012-04-30 14:56:33 +0300629 default:
630 dwc3_event_buffers_cleanup(dwc);
631 break;
632 }
633
634 spin_unlock_irqrestore(&dwc->lock, flags);
635
636 return 0;
637}
638
639static void dwc3_complete(struct device *dev)
640{
641 struct dwc3 *dwc = dev_get_drvdata(dev);
642 unsigned long flags;
643
644 spin_lock_irqsave(&dwc->lock, flags);
645
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500646 switch (dwc->dr_mode) {
647 case USB_DR_MODE_PERIPHERAL:
648 case USB_DR_MODE_OTG:
Felipe Balbi7415f172012-04-30 14:56:33 +0300649 dwc3_gadget_complete(dwc);
650 /* FALLTHROUGH */
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500651 case USB_DR_MODE_HOST:
Felipe Balbi7415f172012-04-30 14:56:33 +0300652 default:
653 dwc3_event_buffers_setup(dwc);
654 break;
655 }
656
657 spin_unlock_irqrestore(&dwc->lock, flags);
658}
659
660static int dwc3_suspend(struct device *dev)
661{
662 struct dwc3 *dwc = dev_get_drvdata(dev);
663 unsigned long flags;
664
665 spin_lock_irqsave(&dwc->lock, flags);
666
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500667 switch (dwc->dr_mode) {
668 case USB_DR_MODE_PERIPHERAL:
669 case USB_DR_MODE_OTG:
Felipe Balbi7415f172012-04-30 14:56:33 +0300670 dwc3_gadget_suspend(dwc);
671 /* FALLTHROUGH */
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500672 case USB_DR_MODE_HOST:
Felipe Balbi7415f172012-04-30 14:56:33 +0300673 default:
674 /* do nothing */
675 break;
676 }
677
678 dwc->gctl = dwc3_readl(dwc->regs, DWC3_GCTL);
679 spin_unlock_irqrestore(&dwc->lock, flags);
680
681 usb_phy_shutdown(dwc->usb3_phy);
682 usb_phy_shutdown(dwc->usb2_phy);
683
684 return 0;
685}
686
687static int dwc3_resume(struct device *dev)
688{
689 struct dwc3 *dwc = dev_get_drvdata(dev);
690 unsigned long flags;
691
692 usb_phy_init(dwc->usb3_phy);
693 usb_phy_init(dwc->usb2_phy);
694 msleep(100);
695
696 spin_lock_irqsave(&dwc->lock, flags);
697
698 dwc3_writel(dwc->regs, DWC3_GCTL, dwc->gctl);
699
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500700 switch (dwc->dr_mode) {
701 case USB_DR_MODE_PERIPHERAL:
702 case USB_DR_MODE_OTG:
Felipe Balbi7415f172012-04-30 14:56:33 +0300703 dwc3_gadget_resume(dwc);
704 /* FALLTHROUGH */
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500705 case USB_DR_MODE_HOST:
Felipe Balbi7415f172012-04-30 14:56:33 +0300706 default:
707 /* do nothing */
708 break;
709 }
710
711 spin_unlock_irqrestore(&dwc->lock, flags);
712
713 pm_runtime_disable(dev);
714 pm_runtime_set_active(dev);
715 pm_runtime_enable(dev);
716
717 return 0;
718}
719
720static const struct dev_pm_ops dwc3_dev_pm_ops = {
721 .prepare = dwc3_prepare,
722 .complete = dwc3_complete,
723
724 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
725};
726
727#define DWC3_PM_OPS &(dwc3_dev_pm_ops)
728#else
729#define DWC3_PM_OPS NULL
730#endif
731
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530732#ifdef CONFIG_OF
733static const struct of_device_id of_dwc3_match[] = {
734 {
Felipe Balbi22a5aa12013-07-02 21:20:24 +0300735 .compatible = "snps,dwc3"
736 },
737 {
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530738 .compatible = "synopsys,dwc3"
739 },
740 { },
741};
742MODULE_DEVICE_TABLE(of, of_dwc3_match);
743#endif
744
Felipe Balbi72246da2011-08-19 18:10:58 +0300745static struct platform_driver dwc3_driver = {
746 .probe = dwc3_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500747 .remove = dwc3_remove,
Felipe Balbi72246da2011-08-19 18:10:58 +0300748 .driver = {
749 .name = "dwc3",
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530750 .of_match_table = of_match_ptr(of_dwc3_match),
Felipe Balbi7415f172012-04-30 14:56:33 +0300751 .pm = DWC3_PM_OPS,
Felipe Balbi72246da2011-08-19 18:10:58 +0300752 },
Felipe Balbi72246da2011-08-19 18:10:58 +0300753};
754
Tobias Klauserb1116dc2012-02-28 12:57:20 +0100755module_platform_driver(dwc3_driver);
756
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +0200757MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +0300758MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
Felipe Balbi5945f782013-06-30 14:15:11 +0300759MODULE_LICENSE("GPL v2");
Felipe Balbi72246da2011-08-19 18:10:58 +0300760MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");