blob: 577af1b237f2c3b279384e2323e9ae80e97234db [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>
Felipe Balbif7e846f2013-06-30 14:29:51 +030039#include <linux/usb/of.h>
Ruchika Kharwara45c82b82013-07-06 07:52:49 -050040#include <linux/usb/otg.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030041
Felipe Balbi6462cbd2013-06-30 14:19:33 +030042#include "platform_data.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030043#include "core.h"
44#include "gadget.h"
45#include "io.h"
46
47#include "debug.h"
48
Felipe Balbi8300dd22011-10-18 13:54:01 +030049/* -------------------------------------------------------------------------- */
50
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +010051void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
52{
53 u32 reg;
54
55 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
56 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
57 reg |= DWC3_GCTL_PRTCAPDIR(mode);
58 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
59}
Felipe Balbi8300dd22011-10-18 13:54:01 +030060
Felipe Balbi72246da2011-08-19 18:10:58 +030061/**
62 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
63 * @dwc: pointer to our context structure
64 */
65static void dwc3_core_soft_reset(struct dwc3 *dwc)
66{
67 u32 reg;
68
69 /* Before Resetting PHY, put Core in Reset */
70 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
71 reg |= DWC3_GCTL_CORESOFTRESET;
72 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
73
74 /* Assert USB3 PHY reset */
75 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
76 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
77 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
78
79 /* Assert USB2 PHY reset */
80 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
81 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
82 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
83
Felipe Balbi51e1e7b2012-07-19 14:09:48 +030084 usb_phy_init(dwc->usb2_phy);
85 usb_phy_init(dwc->usb3_phy);
Felipe Balbi72246da2011-08-19 18:10:58 +030086 mdelay(100);
87
88 /* Clear USB3 PHY reset */
89 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
90 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
91 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
92
93 /* Clear USB2 PHY reset */
94 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
95 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
96 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
97
Pratyush Anand45627ac2012-06-21 17:44:28 +053098 mdelay(100);
99
Felipe Balbi72246da2011-08-19 18:10:58 +0300100 /* After PHYs are stable we can take Core out of reset state */
101 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
102 reg &= ~DWC3_GCTL_CORESOFTRESET;
103 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
104}
105
106/**
107 * dwc3_free_one_event_buffer - Frees one event buffer
108 * @dwc: Pointer to our controller context structure
109 * @evt: Pointer to event buffer to be freed
110 */
111static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
112 struct dwc3_event_buffer *evt)
113{
114 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300115}
116
117/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800118 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300119 * @dwc: Pointer to our controller context structure
120 * @length: size of the event buffer
121 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800122 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300123 * otherwise ERR_PTR(errno).
124 */
Felipe Balbi67d0b502013-02-22 16:31:07 +0200125static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
126 unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300127{
128 struct dwc3_event_buffer *evt;
129
Felipe Balbi380f0d22012-10-11 13:48:36 +0300130 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300131 if (!evt)
132 return ERR_PTR(-ENOMEM);
133
134 evt->dwc = dwc;
135 evt->length = length;
136 evt->buf = dma_alloc_coherent(dwc->dev, length,
137 &evt->dma, GFP_KERNEL);
Felipe Balbie32672f2012-11-08 15:26:41 +0200138 if (!evt->buf)
Felipe Balbi72246da2011-08-19 18:10:58 +0300139 return ERR_PTR(-ENOMEM);
Felipe Balbi72246da2011-08-19 18:10:58 +0300140
141 return evt;
142}
143
144/**
145 * dwc3_free_event_buffers - frees all allocated event buffers
146 * @dwc: Pointer to our controller context structure
147 */
148static void dwc3_free_event_buffers(struct dwc3 *dwc)
149{
150 struct dwc3_event_buffer *evt;
151 int i;
152
Felipe Balbi9f622b22011-10-12 10:31:04 +0300153 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300154 evt = dwc->ev_buffs[i];
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900155 if (evt)
Felipe Balbi72246da2011-08-19 18:10:58 +0300156 dwc3_free_one_event_buffer(dwc, evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300157 }
158}
159
160/**
161 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800162 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300163 * @length: size of event buffer
164 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800165 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300166 * may contain some buffers allocated but not all which were requested.
167 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500168static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300169{
Felipe Balbi9f622b22011-10-12 10:31:04 +0300170 int num;
Felipe Balbi72246da2011-08-19 18:10:58 +0300171 int i;
172
Felipe Balbi9f622b22011-10-12 10:31:04 +0300173 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
174 dwc->num_event_buffers = num;
175
Felipe Balbi380f0d22012-10-11 13:48:36 +0300176 dwc->ev_buffs = devm_kzalloc(dwc->dev, sizeof(*dwc->ev_buffs) * num,
177 GFP_KERNEL);
Felipe Balbi457d3f22011-10-24 12:03:13 +0300178 if (!dwc->ev_buffs) {
179 dev_err(dwc->dev, "can't allocate event buffers array\n");
180 return -ENOMEM;
181 }
182
Felipe Balbi72246da2011-08-19 18:10:58 +0300183 for (i = 0; i < num; i++) {
184 struct dwc3_event_buffer *evt;
185
186 evt = dwc3_alloc_one_event_buffer(dwc, length);
187 if (IS_ERR(evt)) {
188 dev_err(dwc->dev, "can't allocate event buffer\n");
189 return PTR_ERR(evt);
190 }
191 dwc->ev_buffs[i] = evt;
192 }
193
194 return 0;
195}
196
197/**
198 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800199 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300200 *
201 * Returns 0 on success otherwise negative errno.
202 */
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300203static int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300204{
205 struct dwc3_event_buffer *evt;
206 int n;
207
Felipe Balbi9f622b22011-10-12 10:31:04 +0300208 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300209 evt = dwc->ev_buffs[n];
210 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
211 evt->buf, (unsigned long long) evt->dma,
212 evt->length);
213
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300214 evt->lpos = 0;
215
Felipe Balbi72246da2011-08-19 18:10:58 +0300216 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
217 lower_32_bits(evt->dma));
218 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
219 upper_32_bits(evt->dma));
220 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
Felipe Balbi68d6a012013-06-12 21:09:26 +0300221 DWC3_GEVNTSIZ_SIZE(evt->length));
Felipe Balbi72246da2011-08-19 18:10:58 +0300222 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
223 }
224
225 return 0;
226}
227
228static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
229{
230 struct dwc3_event_buffer *evt;
231 int n;
232
Felipe Balbi9f622b22011-10-12 10:31:04 +0300233 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300234 evt = dwc->ev_buffs[n];
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300235
236 evt->lpos = 0;
237
Felipe Balbi72246da2011-08-19 18:10:58 +0300238 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
239 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
Felipe Balbi68d6a012013-06-12 21:09:26 +0300240 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK
241 | DWC3_GEVNTSIZ_SIZE(0));
Felipe Balbi72246da2011-08-19 18:10:58 +0300242 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
243 }
244}
245
Felipe Balbi789451f62011-05-05 15:53:10 +0300246static void dwc3_core_num_eps(struct dwc3 *dwc)
247{
248 struct dwc3_hwparams *parms = &dwc->hwparams;
249
250 dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
251 dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
252
253 dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n",
254 dwc->num_in_eps, dwc->num_out_eps);
255}
256
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500257static void dwc3_cache_hwparams(struct dwc3 *dwc)
Felipe Balbi26ceca92011-09-30 10:58:49 +0300258{
259 struct dwc3_hwparams *parms = &dwc->hwparams;
260
261 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
262 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
263 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
264 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
265 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
266 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
267 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
268 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
269 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
270}
271
Felipe Balbi72246da2011-08-19 18:10:58 +0300272/**
273 * dwc3_core_init - Low-level initialization of DWC3 Core
274 * @dwc: Pointer to our controller context structure
275 *
276 * Returns 0 on success otherwise negative errno.
277 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500278static int dwc3_core_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300279{
280 unsigned long timeout;
281 u32 reg;
282 int ret;
283
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200284 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
285 /* This should read as U3 followed by revision number */
286 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
287 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
288 ret = -ENODEV;
289 goto err0;
290 }
Felipe Balbi248b1222011-12-14 21:59:30 +0200291 dwc->revision = reg;
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200292
Felipe Balbi72246da2011-08-19 18:10:58 +0300293 /* issue device SoftReset too */
294 timeout = jiffies + msecs_to_jiffies(500);
295 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
296 do {
297 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
298 if (!(reg & DWC3_DCTL_CSFTRST))
299 break;
300
301 if (time_after(jiffies, timeout)) {
302 dev_err(dwc->dev, "Reset Timed Out\n");
303 ret = -ETIMEDOUT;
304 goto err0;
305 }
306
307 cpu_relax();
308 } while (true);
309
Pratyush Anand58a0f232012-06-21 17:44:29 +0530310 dwc3_core_soft_reset(dwc);
311
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100312 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800313 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100314 reg &= ~DWC3_GCTL_DISSCRAMBLE;
315
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100316 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100317 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
318 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
319 break;
320 default:
321 dev_dbg(dwc->dev, "No power optimization available\n");
322 }
323
324 /*
325 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800326 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100327 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800328 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100329 */
330 if (dwc->revision < DWC3_REVISION_190A)
331 reg |= DWC3_GCTL_U2RSTECN;
332
Felipe Balbi789451f62011-05-05 15:53:10 +0300333 dwc3_core_num_eps(dwc);
334
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100335 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
336
Felipe Balbi72246da2011-08-19 18:10:58 +0300337 return 0;
338
Felipe Balbi72246da2011-08-19 18:10:58 +0300339err0:
340 return ret;
341}
342
343static void dwc3_core_exit(struct dwc3 *dwc)
344{
Vivek Gautam01b8daf2012-10-13 19:20:18 +0530345 usb_phy_shutdown(dwc->usb2_phy);
346 usb_phy_shutdown(dwc->usb3_phy);
Felipe Balbi72246da2011-08-19 18:10:58 +0300347}
348
349#define DWC3_ALIGN_MASK (16 - 1)
350
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500351static int dwc3_probe(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +0300352{
Felipe Balbi941ea362013-07-31 09:21:25 +0300353 struct device *dev = &pdev->dev;
354 struct dwc3_platform_data *pdata = dev_get_platdata(dev);
355 struct device_node *node = dev->of_node;
Felipe Balbi72246da2011-08-19 18:10:58 +0300356 struct resource *res;
357 struct dwc3 *dwc;
Felipe Balbi0949e992011-10-12 10:44:56 +0300358
Felipe Balbi72246da2011-08-19 18:10:58 +0300359 int ret = -ENOMEM;
Felipe Balbi0949e992011-10-12 10:44:56 +0300360
361 void __iomem *regs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300362 void *mem;
363
Chanho Park802ca852012-02-15 18:27:55 +0900364 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300365 if (!mem) {
Chanho Park802ca852012-02-15 18:27:55 +0900366 dev_err(dev, "not enough memory\n");
367 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300368 }
369 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
370 dwc->mem = mem;
371
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300372 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300373 if (!res) {
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300374 dev_err(dev, "missing IRQ\n");
Chanho Park802ca852012-02-15 18:27:55 +0900375 return -ENODEV;
Felipe Balbi72246da2011-08-19 18:10:58 +0300376 }
Kishon Vijay Abraham I066618b2012-08-21 14:56:16 +0530377 dwc->xhci_resources[1].start = res->start;
378 dwc->xhci_resources[1].end = res->end;
379 dwc->xhci_resources[1].flags = res->flags;
380 dwc->xhci_resources[1].name = res->name;
Felipe Balbi72246da2011-08-19 18:10:58 +0300381
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300382 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
383 if (!res) {
384 dev_err(dev, "missing memory resource\n");
385 return -ENODEV;
386 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300387
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530388 if (node) {
Felipe Balbif7e846f2013-06-30 14:29:51 +0300389 dwc->maximum_speed = of_usb_get_maximum_speed(node);
390
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530391 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
392 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
Felipe Balbi6462cbd2013-06-30 14:19:33 +0300393
394 dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize");
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500395 dwc->dr_mode = of_usb_get_dr_mode(node);
Felipe Balbibb674902013-08-14 13:21:23 -0500396 } else if (pdata) {
Felipe Balbif7e846f2013-06-30 14:29:51 +0300397 dwc->maximum_speed = pdata->maximum_speed;
398
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530399 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
400 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
Felipe Balbi6462cbd2013-06-30 14:19:33 +0300401
402 dwc->needs_fifo_resize = pdata->tx_fifo_resize;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500403 dwc->dr_mode = pdata->dr_mode;
Felipe Balbibb674902013-08-14 13:21:23 -0500404 } else {
405 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
406 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530407 }
408
Felipe Balbif7e846f2013-06-30 14:29:51 +0300409 /* default to superspeed if no maximum_speed passed */
410 if (dwc->maximum_speed == USB_SPEED_UNKNOWN)
411 dwc->maximum_speed = USB_SPEED_SUPER;
412
Felipe Balbid105e7f2013-03-15 10:52:08 +0200413 if (IS_ERR(dwc->usb2_phy)) {
414 ret = PTR_ERR(dwc->usb2_phy);
415
416 /*
417 * if -ENXIO is returned, it means PHY layer wasn't
418 * enabled, so it makes no sense to return -EPROBE_DEFER
419 * in that case, since no PHY driver will ever probe.
420 */
421 if (ret == -ENXIO)
422 return ret;
423
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300424 dev_err(dev, "no usb2 phy configured\n");
425 return -EPROBE_DEFER;
426 }
427
Felipe Balbid105e7f2013-03-15 10:52:08 +0200428 if (IS_ERR(dwc->usb3_phy)) {
Ruchika Kharwar315955d72013-07-04 00:59:34 -0500429 ret = PTR_ERR(dwc->usb3_phy);
Felipe Balbid105e7f2013-03-15 10:52:08 +0200430
431 /*
432 * if -ENXIO is returned, it means PHY layer wasn't
433 * enabled, so it makes no sense to return -EPROBE_DEFER
434 * in that case, since no PHY driver will ever probe.
435 */
436 if (ret == -ENXIO)
437 return ret;
438
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300439 dev_err(dev, "no usb3 phy configured\n");
440 return -EPROBE_DEFER;
441 }
442
Ivan T. Ivanov2e112342013-07-29 10:27:02 +0300443 dwc->xhci_resources[0].start = res->start;
444 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
445 DWC3_XHCI_REGS_END;
446 dwc->xhci_resources[0].flags = res->flags;
447 dwc->xhci_resources[0].name = res->name;
448
449 res->start += DWC3_GLOBALS_REGS_START;
450
451 /*
452 * Request memory region but exclude xHCI regs,
453 * since it will be requested by the xhci-plat driver.
454 */
455 regs = devm_ioremap_resource(dev, res);
456 if (IS_ERR(regs))
457 return PTR_ERR(regs);
458
Kishon Vijay Abraham I8ba007a2013-01-25 08:30:54 +0530459 usb_phy_set_suspend(dwc->usb2_phy, 0);
460 usb_phy_set_suspend(dwc->usb3_phy, 0);
461
Felipe Balbi72246da2011-08-19 18:10:58 +0300462 spin_lock_init(&dwc->lock);
463 platform_set_drvdata(pdev, dwc);
464
465 dwc->regs = regs;
466 dwc->regs_size = resource_size(res);
Chanho Park802ca852012-02-15 18:27:55 +0900467 dwc->dev = dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300468
Kishon Vijay Abraham Iddff14f2013-03-07 18:51:43 +0530469 dev->dma_mask = dev->parent->dma_mask;
470 dev->dma_parms = dev->parent->dma_parms;
471 dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
472
Chanho Park802ca852012-02-15 18:27:55 +0900473 pm_runtime_enable(dev);
474 pm_runtime_get_sync(dev);
475 pm_runtime_forbid(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300476
Kishon Vijay Abraham I4fd24482012-11-16 12:07:54 +0530477 dwc3_cache_hwparams(dwc);
478
Felipe Balbi39214262012-10-11 13:54:36 +0300479 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
480 if (ret) {
481 dev_err(dwc->dev, "failed to allocate event buffers\n");
482 ret = -ENOMEM;
483 goto err0;
484 }
485
Felipe Balbi72246da2011-08-19 18:10:58 +0300486 ret = dwc3_core_init(dwc);
487 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900488 dev_err(dev, "failed to initialize core\n");
Felipe Balbi39214262012-10-11 13:54:36 +0300489 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300490 }
491
Felipe Balbif122d332013-02-08 15:15:11 +0200492 ret = dwc3_event_buffers_setup(dwc);
493 if (ret) {
494 dev_err(dwc->dev, "failed to setup event buffers\n");
495 goto err1;
496 }
497
Vivek Gautamcd051da2013-03-02 18:55:24 +0530498 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500499 dwc->dr_mode = USB_DR_MODE_HOST;
Vivek Gautamcd051da2013-03-02 18:55:24 +0530500 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500501 dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
Felipe Balbi0949e992011-10-12 10:44:56 +0300502
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500503 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
504 dwc->dr_mode = USB_DR_MODE_OTG;
505
506 switch (dwc->dr_mode) {
507 case USB_DR_MODE_PERIPHERAL:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100508 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300509 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 Balbi72246da2011-08-19 18:10:58 +0300513 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300514 break;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500515 case USB_DR_MODE_HOST:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100516 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
Felipe Balbid07e8812011-10-12 14:08:26 +0300517 ret = dwc3_host_init(dwc);
518 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900519 dev_err(dev, "failed to initialize host\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200520 goto err2;
Felipe Balbid07e8812011-10-12 14:08:26 +0300521 }
522 break;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500523 case USB_DR_MODE_OTG:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100524 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
Felipe Balbid07e8812011-10-12 14:08:26 +0300525 ret = dwc3_host_init(dwc);
526 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900527 dev_err(dev, "failed to initialize host\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200528 goto err2;
Felipe Balbid07e8812011-10-12 14:08:26 +0300529 }
530
531 ret = dwc3_gadget_init(dwc);
532 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900533 dev_err(dev, "failed to initialize gadget\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200534 goto err2;
Felipe Balbid07e8812011-10-12 14:08:26 +0300535 }
536 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300537 default:
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500538 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
Felipe Balbif122d332013-02-08 15:15:11 +0200539 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +0300540 }
541
542 ret = dwc3_debugfs_init(dwc);
543 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900544 dev_err(dev, "failed to initialize debugfs\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200545 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +0300546 }
547
Chanho Park802ca852012-02-15 18:27:55 +0900548 pm_runtime_allow(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300549
550 return 0;
551
Felipe Balbif122d332013-02-08 15:15:11 +0200552err3:
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500553 switch (dwc->dr_mode) {
554 case USB_DR_MODE_PERIPHERAL:
Felipe Balbi72246da2011-08-19 18:10:58 +0300555 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300556 break;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500557 case USB_DR_MODE_HOST:
Felipe Balbid07e8812011-10-12 14:08:26 +0300558 dwc3_host_exit(dwc);
559 break;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500560 case USB_DR_MODE_OTG:
Felipe Balbid07e8812011-10-12 14:08:26 +0300561 dwc3_host_exit(dwc);
562 dwc3_gadget_exit(dwc);
563 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300564 default:
565 /* do nothing */
566 break;
567 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300568
Felipe Balbif122d332013-02-08 15:15:11 +0200569err2:
570 dwc3_event_buffers_cleanup(dwc);
571
Chanho Park802ca852012-02-15 18:27:55 +0900572err1:
Felipe Balbi72246da2011-08-19 18:10:58 +0300573 dwc3_core_exit(dwc);
574
Felipe Balbi39214262012-10-11 13:54:36 +0300575err0:
576 dwc3_free_event_buffers(dwc);
577
Felipe Balbi72246da2011-08-19 18:10:58 +0300578 return ret;
579}
580
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500581static int dwc3_remove(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +0300582{
Felipe Balbi72246da2011-08-19 18:10:58 +0300583 struct dwc3 *dwc = platform_get_drvdata(pdev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300584
Kishon Vijay Abraham I8ba007a2013-01-25 08:30:54 +0530585 usb_phy_set_suspend(dwc->usb2_phy, 1);
586 usb_phy_set_suspend(dwc->usb3_phy, 1);
587
Felipe Balbi72246da2011-08-19 18:10:58 +0300588 pm_runtime_put(&pdev->dev);
589 pm_runtime_disable(&pdev->dev);
590
591 dwc3_debugfs_exit(dwc);
592
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500593 switch (dwc->dr_mode) {
594 case USB_DR_MODE_PERIPHERAL:
Felipe Balbi72246da2011-08-19 18:10:58 +0300595 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300596 break;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500597 case USB_DR_MODE_HOST:
Felipe Balbid07e8812011-10-12 14:08:26 +0300598 dwc3_host_exit(dwc);
599 break;
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500600 case USB_DR_MODE_OTG:
Felipe Balbid07e8812011-10-12 14:08:26 +0300601 dwc3_host_exit(dwc);
602 dwc3_gadget_exit(dwc);
603 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300604 default:
605 /* do nothing */
606 break;
607 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300608
Felipe Balbif122d332013-02-08 15:15:11 +0200609 dwc3_event_buffers_cleanup(dwc);
Felipe Balbid9b43302013-02-08 15:14:16 +0200610 dwc3_free_event_buffers(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300611 dwc3_core_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300612
613 return 0;
614}
615
Jingoo Han19fda7c2013-03-26 01:52:48 +0000616#ifdef CONFIG_PM_SLEEP
Felipe Balbi7415f172012-04-30 14:56:33 +0300617static int dwc3_prepare(struct device *dev)
618{
619 struct dwc3 *dwc = dev_get_drvdata(dev);
620 unsigned long flags;
621
622 spin_lock_irqsave(&dwc->lock, flags);
623
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500624 switch (dwc->dr_mode) {
625 case USB_DR_MODE_PERIPHERAL:
626 case USB_DR_MODE_OTG:
Felipe Balbi7415f172012-04-30 14:56:33 +0300627 dwc3_gadget_prepare(dwc);
628 /* FALLTHROUGH */
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500629 case USB_DR_MODE_HOST:
Felipe Balbi7415f172012-04-30 14:56:33 +0300630 default:
631 dwc3_event_buffers_cleanup(dwc);
632 break;
633 }
634
635 spin_unlock_irqrestore(&dwc->lock, flags);
636
637 return 0;
638}
639
640static void dwc3_complete(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
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500647 switch (dwc->dr_mode) {
648 case USB_DR_MODE_PERIPHERAL:
649 case USB_DR_MODE_OTG:
Felipe Balbi7415f172012-04-30 14:56:33 +0300650 dwc3_gadget_complete(dwc);
651 /* FALLTHROUGH */
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500652 case USB_DR_MODE_HOST:
Felipe Balbi7415f172012-04-30 14:56:33 +0300653 default:
654 dwc3_event_buffers_setup(dwc);
655 break;
656 }
657
658 spin_unlock_irqrestore(&dwc->lock, flags);
659}
660
661static int dwc3_suspend(struct device *dev)
662{
663 struct dwc3 *dwc = dev_get_drvdata(dev);
664 unsigned long flags;
665
666 spin_lock_irqsave(&dwc->lock, flags);
667
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500668 switch (dwc->dr_mode) {
669 case USB_DR_MODE_PERIPHERAL:
670 case USB_DR_MODE_OTG:
Felipe Balbi7415f172012-04-30 14:56:33 +0300671 dwc3_gadget_suspend(dwc);
672 /* FALLTHROUGH */
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500673 case USB_DR_MODE_HOST:
Felipe Balbi7415f172012-04-30 14:56:33 +0300674 default:
675 /* do nothing */
676 break;
677 }
678
679 dwc->gctl = dwc3_readl(dwc->regs, DWC3_GCTL);
680 spin_unlock_irqrestore(&dwc->lock, flags);
681
682 usb_phy_shutdown(dwc->usb3_phy);
683 usb_phy_shutdown(dwc->usb2_phy);
684
685 return 0;
686}
687
688static int dwc3_resume(struct device *dev)
689{
690 struct dwc3 *dwc = dev_get_drvdata(dev);
691 unsigned long flags;
692
693 usb_phy_init(dwc->usb3_phy);
694 usb_phy_init(dwc->usb2_phy);
695 msleep(100);
696
697 spin_lock_irqsave(&dwc->lock, flags);
698
699 dwc3_writel(dwc->regs, DWC3_GCTL, dwc->gctl);
700
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500701 switch (dwc->dr_mode) {
702 case USB_DR_MODE_PERIPHERAL:
703 case USB_DR_MODE_OTG:
Felipe Balbi7415f172012-04-30 14:56:33 +0300704 dwc3_gadget_resume(dwc);
705 /* FALLTHROUGH */
Ruchika Kharwara45c82b82013-07-06 07:52:49 -0500706 case USB_DR_MODE_HOST:
Felipe Balbi7415f172012-04-30 14:56:33 +0300707 default:
708 /* do nothing */
709 break;
710 }
711
712 spin_unlock_irqrestore(&dwc->lock, flags);
713
714 pm_runtime_disable(dev);
715 pm_runtime_set_active(dev);
716 pm_runtime_enable(dev);
717
718 return 0;
719}
720
721static const struct dev_pm_ops dwc3_dev_pm_ops = {
722 .prepare = dwc3_prepare,
723 .complete = dwc3_complete,
724
725 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
726};
727
728#define DWC3_PM_OPS &(dwc3_dev_pm_ops)
729#else
730#define DWC3_PM_OPS NULL
731#endif
732
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530733#ifdef CONFIG_OF
734static const struct of_device_id of_dwc3_match[] = {
735 {
Felipe Balbi22a5aa12013-07-02 21:20:24 +0300736 .compatible = "snps,dwc3"
737 },
738 {
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530739 .compatible = "synopsys,dwc3"
740 },
741 { },
742};
743MODULE_DEVICE_TABLE(of, of_dwc3_match);
744#endif
745
Felipe Balbi72246da2011-08-19 18:10:58 +0300746static struct platform_driver dwc3_driver = {
747 .probe = dwc3_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500748 .remove = dwc3_remove,
Felipe Balbi72246da2011-08-19 18:10:58 +0300749 .driver = {
750 .name = "dwc3",
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530751 .of_match_table = of_match_ptr(of_dwc3_match),
Felipe Balbi7415f172012-04-30 14:56:33 +0300752 .pm = DWC3_PM_OPS,
Felipe Balbi72246da2011-08-19 18:10:58 +0300753 },
Felipe Balbi72246da2011-08-19 18:10:58 +0300754};
755
Tobias Klauserb1116dc2012-02-28 12:57:20 +0100756module_platform_driver(dwc3_driver);
757
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +0200758MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +0300759MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
Felipe Balbi5945f782013-06-30 14:15:11 +0300760MODULE_LICENSE("GPL v2");
Felipe Balbi72246da2011-08-19 18:10:58 +0300761MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");