blob: f69e45a500396ab694352ab483e9582375723134 [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
40#include "core.h"
41#include "gadget.h"
42#include "io.h"
43
44#include "debug.h"
45
Felipe Balbi6c167fc2011-10-07 22:55:04 +030046static char *maximum_speed = "super";
47module_param(maximum_speed, charp, 0);
48MODULE_PARM_DESC(maximum_speed, "Maximum supported speed.");
49
Felipe Balbi8300dd22011-10-18 13:54:01 +030050/* -------------------------------------------------------------------------- */
51
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +010052void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
53{
54 u32 reg;
55
56 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
57 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
58 reg |= DWC3_GCTL_PRTCAPDIR(mode);
59 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
60}
Felipe Balbi8300dd22011-10-18 13:54:01 +030061
Felipe Balbi72246da2011-08-19 18:10:58 +030062/**
63 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
64 * @dwc: pointer to our context structure
65 */
66static void dwc3_core_soft_reset(struct dwc3 *dwc)
67{
68 u32 reg;
69
70 /* Before Resetting PHY, put Core in Reset */
71 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
72 reg |= DWC3_GCTL_CORESOFTRESET;
73 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
74
75 /* Assert USB3 PHY reset */
76 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
77 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
78 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
79
80 /* Assert USB2 PHY reset */
81 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
82 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
83 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
84
Felipe Balbi51e1e7b2012-07-19 14:09:48 +030085 usb_phy_init(dwc->usb2_phy);
86 usb_phy_init(dwc->usb3_phy);
Felipe Balbi72246da2011-08-19 18:10:58 +030087 mdelay(100);
88
89 /* Clear USB3 PHY reset */
90 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
91 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
92 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
93
94 /* Clear USB2 PHY reset */
95 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
96 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
97 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
98
Pratyush Anand45627ac2012-06-21 17:44:28 +053099 mdelay(100);
100
Felipe Balbi72246da2011-08-19 18:10:58 +0300101 /* After PHYs are stable we can take Core out of reset state */
102 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
103 reg &= ~DWC3_GCTL_CORESOFTRESET;
104 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
105}
106
107/**
108 * dwc3_free_one_event_buffer - Frees one event buffer
109 * @dwc: Pointer to our controller context structure
110 * @evt: Pointer to event buffer to be freed
111 */
112static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
113 struct dwc3_event_buffer *evt)
114{
115 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300116}
117
118/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800119 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300120 * @dwc: Pointer to our controller context structure
121 * @length: size of the event buffer
122 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800123 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300124 * otherwise ERR_PTR(errno).
125 */
Felipe Balbi67d0b502013-02-22 16:31:07 +0200126static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
127 unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300128{
129 struct dwc3_event_buffer *evt;
130
Felipe Balbi380f0d22012-10-11 13:48:36 +0300131 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300132 if (!evt)
133 return ERR_PTR(-ENOMEM);
134
135 evt->dwc = dwc;
136 evt->length = length;
137 evt->buf = dma_alloc_coherent(dwc->dev, length,
138 &evt->dma, GFP_KERNEL);
Felipe Balbie32672f2012-11-08 15:26:41 +0200139 if (!evt->buf)
Felipe Balbi72246da2011-08-19 18:10:58 +0300140 return ERR_PTR(-ENOMEM);
Felipe Balbi72246da2011-08-19 18:10:58 +0300141
142 return evt;
143}
144
145/**
146 * dwc3_free_event_buffers - frees all allocated event buffers
147 * @dwc: Pointer to our controller context structure
148 */
149static void dwc3_free_event_buffers(struct dwc3 *dwc)
150{
151 struct dwc3_event_buffer *evt;
152 int i;
153
Felipe Balbi9f622b22011-10-12 10:31:04 +0300154 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300155 evt = dwc->ev_buffs[i];
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900156 if (evt)
Felipe Balbi72246da2011-08-19 18:10:58 +0300157 dwc3_free_one_event_buffer(dwc, evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300158 }
159}
160
161/**
162 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800163 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300164 * @length: size of event buffer
165 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800166 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300167 * may contain some buffers allocated but not all which were requested.
168 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500169static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300170{
Felipe Balbi9f622b22011-10-12 10:31:04 +0300171 int num;
Felipe Balbi72246da2011-08-19 18:10:58 +0300172 int i;
173
Felipe Balbi9f622b22011-10-12 10:31:04 +0300174 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
175 dwc->num_event_buffers = num;
176
Felipe Balbi380f0d22012-10-11 13:48:36 +0300177 dwc->ev_buffs = devm_kzalloc(dwc->dev, sizeof(*dwc->ev_buffs) * num,
178 GFP_KERNEL);
Felipe Balbi457d3f22011-10-24 12:03:13 +0300179 if (!dwc->ev_buffs) {
180 dev_err(dwc->dev, "can't allocate event buffers array\n");
181 return -ENOMEM;
182 }
183
Felipe Balbi72246da2011-08-19 18:10:58 +0300184 for (i = 0; i < num; i++) {
185 struct dwc3_event_buffer *evt;
186
187 evt = dwc3_alloc_one_event_buffer(dwc, length);
188 if (IS_ERR(evt)) {
189 dev_err(dwc->dev, "can't allocate event buffer\n");
190 return PTR_ERR(evt);
191 }
192 dwc->ev_buffs[i] = evt;
193 }
194
195 return 0;
196}
197
198/**
199 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800200 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300201 *
202 * Returns 0 on success otherwise negative errno.
203 */
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300204static int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300205{
206 struct dwc3_event_buffer *evt;
207 int n;
208
Felipe Balbi9f622b22011-10-12 10:31:04 +0300209 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300210 evt = dwc->ev_buffs[n];
211 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
212 evt->buf, (unsigned long long) evt->dma,
213 evt->length);
214
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300215 evt->lpos = 0;
216
Felipe Balbi72246da2011-08-19 18:10:58 +0300217 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
218 lower_32_bits(evt->dma));
219 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
220 upper_32_bits(evt->dma));
221 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
222 evt->length & 0xffff);
223 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
224 }
225
226 return 0;
227}
228
229static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
230{
231 struct dwc3_event_buffer *evt;
232 int n;
233
Felipe Balbi9f622b22011-10-12 10:31:04 +0300234 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300235 evt = dwc->ev_buffs[n];
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300236
237 evt->lpos = 0;
238
Felipe Balbi72246da2011-08-19 18:10:58 +0300239 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
240 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
241 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
242 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 Balbi457e84b2012-01-18 18:04:09 +0200353 struct device_node *node = pdev->dev.of_node;
Felipe Balbi72246da2011-08-19 18:10:58 +0300354 struct resource *res;
355 struct dwc3 *dwc;
Chanho Park802ca852012-02-15 18:27:55 +0900356 struct device *dev = &pdev->dev;
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
Felipe Balbi0949e992011-10-12 10:44:56 +0300363 u8 mode;
364
Chanho Park802ca852012-02-15 18:27:55 +0900365 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300366 if (!mem) {
Chanho Park802ca852012-02-15 18:27:55 +0900367 dev_err(dev, "not enough memory\n");
368 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300369 }
370 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
371 dwc->mem = mem;
372
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300373 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300374 if (!res) {
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300375 dev_err(dev, "missing IRQ\n");
Chanho Park802ca852012-02-15 18:27:55 +0900376 return -ENODEV;
Felipe Balbi72246da2011-08-19 18:10:58 +0300377 }
Kishon Vijay Abraham I066618b2012-08-21 14:56:16 +0530378 dwc->xhci_resources[1].start = res->start;
379 dwc->xhci_resources[1].end = res->end;
380 dwc->xhci_resources[1].flags = res->flags;
381 dwc->xhci_resources[1].name = res->name;
Felipe Balbi72246da2011-08-19 18:10:58 +0300382
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300383 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
384 if (!res) {
385 dev_err(dev, "missing memory resource\n");
386 return -ENODEV;
387 }
Kishon Vijay Abraham I066618b2012-08-21 14:56:16 +0530388 dwc->xhci_resources[0].start = res->start;
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300389 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
390 DWC3_XHCI_REGS_END;
Kishon Vijay Abraham I066618b2012-08-21 14:56:16 +0530391 dwc->xhci_resources[0].flags = res->flags;
392 dwc->xhci_resources[0].name = res->name;
Felipe Balbid07e8812011-10-12 14:08:26 +0300393
Ido Shayevitz51249dc2012-04-24 14:18:39 +0300394 /*
395 * Request memory region but exclude xHCI regs,
396 * since it will be requested by the xhci-plat driver.
397 */
398 res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START,
399 resource_size(res) - DWC3_GLOBALS_REGS_START,
Chanho Park802ca852012-02-15 18:27:55 +0900400 dev_name(dev));
Felipe Balbi72246da2011-08-19 18:10:58 +0300401 if (!res) {
Chanho Park802ca852012-02-15 18:27:55 +0900402 dev_err(dev, "can't request mem region\n");
403 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300404 }
405
Felipe Balbib7e38aa2012-08-10 09:16:43 +0300406 regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
Felipe Balbi72246da2011-08-19 18:10:58 +0300407 if (!regs) {
Chanho Park802ca852012-02-15 18:27:55 +0900408 dev_err(dev, "ioremap failed\n");
409 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300410 }
411
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530412 if (node) {
413 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
414 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
415 } else {
416 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
417 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
418 }
419
Felipe Balbid105e7f2013-03-15 10:52:08 +0200420 if (IS_ERR(dwc->usb2_phy)) {
421 ret = PTR_ERR(dwc->usb2_phy);
422
423 /*
424 * if -ENXIO is returned, it means PHY layer wasn't
425 * enabled, so it makes no sense to return -EPROBE_DEFER
426 * in that case, since no PHY driver will ever probe.
427 */
428 if (ret == -ENXIO)
429 return ret;
430
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300431 dev_err(dev, "no usb2 phy configured\n");
432 return -EPROBE_DEFER;
433 }
434
Felipe Balbid105e7f2013-03-15 10:52:08 +0200435 if (IS_ERR(dwc->usb3_phy)) {
Ruchika Kharwar315955d72013-07-04 00:59:34 -0500436 ret = PTR_ERR(dwc->usb3_phy);
Felipe Balbid105e7f2013-03-15 10:52:08 +0200437
438 /*
439 * if -ENXIO is returned, it means PHY layer wasn't
440 * enabled, so it makes no sense to return -EPROBE_DEFER
441 * in that case, since no PHY driver will ever probe.
442 */
443 if (ret == -ENXIO)
444 return ret;
445
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300446 dev_err(dev, "no usb3 phy configured\n");
447 return -EPROBE_DEFER;
448 }
449
Kishon Vijay Abraham I8ba007a2013-01-25 08:30:54 +0530450 usb_phy_set_suspend(dwc->usb2_phy, 0);
451 usb_phy_set_suspend(dwc->usb3_phy, 0);
452
Felipe Balbi72246da2011-08-19 18:10:58 +0300453 spin_lock_init(&dwc->lock);
454 platform_set_drvdata(pdev, dwc);
455
456 dwc->regs = regs;
457 dwc->regs_size = resource_size(res);
Chanho Park802ca852012-02-15 18:27:55 +0900458 dwc->dev = dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300459
Kishon Vijay Abraham Iddff14f2013-03-07 18:51:43 +0530460 dev->dma_mask = dev->parent->dma_mask;
461 dev->dma_parms = dev->parent->dma_parms;
462 dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
463
Felipe Balbi6c167fc2011-10-07 22:55:04 +0300464 if (!strncmp("super", maximum_speed, 5))
465 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
466 else if (!strncmp("high", maximum_speed, 4))
467 dwc->maximum_speed = DWC3_DCFG_HIGHSPEED;
468 else if (!strncmp("full", maximum_speed, 4))
469 dwc->maximum_speed = DWC3_DCFG_FULLSPEED1;
470 else if (!strncmp("low", maximum_speed, 3))
471 dwc->maximum_speed = DWC3_DCFG_LOWSPEED;
472 else
473 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
474
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530475 dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize");
Felipe Balbi457e84b2012-01-18 18:04:09 +0200476
Chanho Park802ca852012-02-15 18:27:55 +0900477 pm_runtime_enable(dev);
478 pm_runtime_get_sync(dev);
479 pm_runtime_forbid(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300480
Kishon Vijay Abraham I4fd24482012-11-16 12:07:54 +0530481 dwc3_cache_hwparams(dwc);
482
Felipe Balbi39214262012-10-11 13:54:36 +0300483 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
484 if (ret) {
485 dev_err(dwc->dev, "failed to allocate event buffers\n");
486 ret = -ENOMEM;
487 goto err0;
488 }
489
Felipe Balbi72246da2011-08-19 18:10:58 +0300490 ret = dwc3_core_init(dwc);
491 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900492 dev_err(dev, "failed to initialize core\n");
Felipe Balbi39214262012-10-11 13:54:36 +0300493 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300494 }
495
Felipe Balbif122d332013-02-08 15:15:11 +0200496 ret = dwc3_event_buffers_setup(dwc);
497 if (ret) {
498 dev_err(dwc->dev, "failed to setup event buffers\n");
499 goto err1;
500 }
501
Vivek Gautamcd051da2013-03-02 18:55:24 +0530502 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
503 mode = DWC3_MODE_HOST;
504 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
505 mode = DWC3_MODE_DEVICE;
506 else
507 mode = DWC3_MODE_DRD;
Felipe Balbi0949e992011-10-12 10:44:56 +0300508
509 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300510 case DWC3_MODE_DEVICE:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100511 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
Felipe Balbi72246da2011-08-19 18:10:58 +0300512 ret = dwc3_gadget_init(dwc);
513 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900514 dev_err(dev, "failed to initialize gadget\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200515 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +0300516 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300517 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300518 case DWC3_MODE_HOST:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100519 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
Felipe Balbid07e8812011-10-12 14:08:26 +0300520 ret = dwc3_host_init(dwc);
521 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900522 dev_err(dev, "failed to initialize host\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200523 goto err2;
Felipe Balbid07e8812011-10-12 14:08:26 +0300524 }
525 break;
526 case DWC3_MODE_DRD:
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +0100527 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
Felipe Balbid07e8812011-10-12 14:08:26 +0300528 ret = dwc3_host_init(dwc);
529 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900530 dev_err(dev, "failed to initialize host\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200531 goto err2;
Felipe Balbid07e8812011-10-12 14:08:26 +0300532 }
533
534 ret = dwc3_gadget_init(dwc);
535 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900536 dev_err(dev, "failed to initialize gadget\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200537 goto err2;
Felipe Balbid07e8812011-10-12 14:08:26 +0300538 }
539 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300540 default:
Chanho Park802ca852012-02-15 18:27:55 +0900541 dev_err(dev, "Unsupported mode of operation %d\n", mode);
Felipe Balbif122d332013-02-08 15:15:11 +0200542 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +0300543 }
Felipe Balbi0949e992011-10-12 10:44:56 +0300544 dwc->mode = mode;
Felipe Balbi72246da2011-08-19 18:10:58 +0300545
546 ret = dwc3_debugfs_init(dwc);
547 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900548 dev_err(dev, "failed to initialize debugfs\n");
Felipe Balbif122d332013-02-08 15:15:11 +0200549 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +0300550 }
551
Chanho Park802ca852012-02-15 18:27:55 +0900552 pm_runtime_allow(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300553
554 return 0;
555
Felipe Balbif122d332013-02-08 15:15:11 +0200556err3:
Felipe Balbi0949e992011-10-12 10:44:56 +0300557 switch (mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300558 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300559 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300560 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300561 case DWC3_MODE_HOST:
562 dwc3_host_exit(dwc);
563 break;
564 case DWC3_MODE_DRD:
565 dwc3_host_exit(dwc);
566 dwc3_gadget_exit(dwc);
567 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300568 default:
569 /* do nothing */
570 break;
571 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300572
Felipe Balbif122d332013-02-08 15:15:11 +0200573err2:
574 dwc3_event_buffers_cleanup(dwc);
575
Chanho Park802ca852012-02-15 18:27:55 +0900576err1:
Felipe Balbi72246da2011-08-19 18:10:58 +0300577 dwc3_core_exit(dwc);
578
Felipe Balbi39214262012-10-11 13:54:36 +0300579err0:
580 dwc3_free_event_buffers(dwc);
581
Felipe Balbi72246da2011-08-19 18:10:58 +0300582 return ret;
583}
584
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500585static int dwc3_remove(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +0300586{
Felipe Balbi72246da2011-08-19 18:10:58 +0300587 struct dwc3 *dwc = platform_get_drvdata(pdev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300588
Kishon Vijay Abraham I8ba007a2013-01-25 08:30:54 +0530589 usb_phy_set_suspend(dwc->usb2_phy, 1);
590 usb_phy_set_suspend(dwc->usb3_phy, 1);
591
Felipe Balbi72246da2011-08-19 18:10:58 +0300592 pm_runtime_put(&pdev->dev);
593 pm_runtime_disable(&pdev->dev);
594
595 dwc3_debugfs_exit(dwc);
596
Felipe Balbi0949e992011-10-12 10:44:56 +0300597 switch (dwc->mode) {
Felipe Balbi0949e992011-10-12 10:44:56 +0300598 case DWC3_MODE_DEVICE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300599 dwc3_gadget_exit(dwc);
Felipe Balbi0949e992011-10-12 10:44:56 +0300600 break;
Felipe Balbid07e8812011-10-12 14:08:26 +0300601 case DWC3_MODE_HOST:
602 dwc3_host_exit(dwc);
603 break;
604 case DWC3_MODE_DRD:
605 dwc3_host_exit(dwc);
606 dwc3_gadget_exit(dwc);
607 break;
Felipe Balbi0949e992011-10-12 10:44:56 +0300608 default:
609 /* do nothing */
610 break;
611 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300612
Felipe Balbif122d332013-02-08 15:15:11 +0200613 dwc3_event_buffers_cleanup(dwc);
Felipe Balbid9b43302013-02-08 15:14:16 +0200614 dwc3_free_event_buffers(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300615 dwc3_core_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300616
617 return 0;
618}
619
Jingoo Han19fda7c2013-03-26 01:52:48 +0000620#ifdef CONFIG_PM_SLEEP
Felipe Balbi7415f172012-04-30 14:56:33 +0300621static int dwc3_prepare(struct device *dev)
622{
623 struct dwc3 *dwc = dev_get_drvdata(dev);
624 unsigned long flags;
625
626 spin_lock_irqsave(&dwc->lock, flags);
627
628 switch (dwc->mode) {
629 case DWC3_MODE_DEVICE:
630 case DWC3_MODE_DRD:
631 dwc3_gadget_prepare(dwc);
632 /* FALLTHROUGH */
633 case DWC3_MODE_HOST:
634 default:
635 dwc3_event_buffers_cleanup(dwc);
636 break;
637 }
638
639 spin_unlock_irqrestore(&dwc->lock, flags);
640
641 return 0;
642}
643
644static void dwc3_complete(struct device *dev)
645{
646 struct dwc3 *dwc = dev_get_drvdata(dev);
647 unsigned long flags;
648
649 spin_lock_irqsave(&dwc->lock, flags);
650
651 switch (dwc->mode) {
652 case DWC3_MODE_DEVICE:
653 case DWC3_MODE_DRD:
654 dwc3_gadget_complete(dwc);
655 /* FALLTHROUGH */
656 case DWC3_MODE_HOST:
657 default:
658 dwc3_event_buffers_setup(dwc);
659 break;
660 }
661
662 spin_unlock_irqrestore(&dwc->lock, flags);
663}
664
665static int dwc3_suspend(struct device *dev)
666{
667 struct dwc3 *dwc = dev_get_drvdata(dev);
668 unsigned long flags;
669
670 spin_lock_irqsave(&dwc->lock, flags);
671
672 switch (dwc->mode) {
673 case DWC3_MODE_DEVICE:
674 case DWC3_MODE_DRD:
675 dwc3_gadget_suspend(dwc);
676 /* FALLTHROUGH */
677 case DWC3_MODE_HOST:
678 default:
679 /* do nothing */
680 break;
681 }
682
683 dwc->gctl = dwc3_readl(dwc->regs, DWC3_GCTL);
684 spin_unlock_irqrestore(&dwc->lock, flags);
685
686 usb_phy_shutdown(dwc->usb3_phy);
687 usb_phy_shutdown(dwc->usb2_phy);
688
689 return 0;
690}
691
692static int dwc3_resume(struct device *dev)
693{
694 struct dwc3 *dwc = dev_get_drvdata(dev);
695 unsigned long flags;
696
697 usb_phy_init(dwc->usb3_phy);
698 usb_phy_init(dwc->usb2_phy);
699 msleep(100);
700
701 spin_lock_irqsave(&dwc->lock, flags);
702
703 dwc3_writel(dwc->regs, DWC3_GCTL, dwc->gctl);
704
705 switch (dwc->mode) {
706 case DWC3_MODE_DEVICE:
707 case DWC3_MODE_DRD:
708 dwc3_gadget_resume(dwc);
709 /* FALLTHROUGH */
710 case DWC3_MODE_HOST:
711 default:
712 /* do nothing */
713 break;
714 }
715
716 spin_unlock_irqrestore(&dwc->lock, flags);
717
718 pm_runtime_disable(dev);
719 pm_runtime_set_active(dev);
720 pm_runtime_enable(dev);
721
722 return 0;
723}
724
725static const struct dev_pm_ops dwc3_dev_pm_ops = {
726 .prepare = dwc3_prepare,
727 .complete = dwc3_complete,
728
729 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
730};
731
732#define DWC3_PM_OPS &(dwc3_dev_pm_ops)
733#else
734#define DWC3_PM_OPS NULL
735#endif
736
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530737#ifdef CONFIG_OF
738static const struct of_device_id of_dwc3_match[] = {
739 {
740 .compatible = "synopsys,dwc3"
741 },
742 { },
743};
744MODULE_DEVICE_TABLE(of, of_dwc3_match);
745#endif
746
Felipe Balbi72246da2011-08-19 18:10:58 +0300747static struct platform_driver dwc3_driver = {
748 .probe = dwc3_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500749 .remove = dwc3_remove,
Felipe Balbi72246da2011-08-19 18:10:58 +0300750 .driver = {
751 .name = "dwc3",
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530752 .of_match_table = of_match_ptr(of_dwc3_match),
Felipe Balbi7415f172012-04-30 14:56:33 +0300753 .pm = DWC3_PM_OPS,
Felipe Balbi72246da2011-08-19 18:10:58 +0300754 },
Felipe Balbi72246da2011-08-19 18:10:58 +0300755};
756
Tobias Klauserb1116dc2012-02-28 12:57:20 +0100757module_platform_driver(dwc3_driver);
758
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +0200759MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +0300760MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
Felipe Balbi5945f782013-06-30 14:15:11 +0300761MODULE_LICENSE("GPL v2");
Felipe Balbi72246da2011-08-19 18:10:58 +0300762MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");