blob: 64ba0979a34955bcea341b6182b2f0f797053301 [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
5 * All rights reserved.
6 *
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The names of the above-listed copyright holders may not be used
20 * to endorse or promote products derived from this software without
21 * specific prior written permission.
22 *
23 * ALTERNATIVELY, this software may be distributed under the terms of the
24 * GNU General Public License ("GPL") version 2, as published by the Free
25 * Software Foundation.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
Felipe Balbia72e6582011-09-05 13:37:28 +030040#include <linux/module.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030041#include <linux/kernel.h>
42#include <linux/slab.h>
43#include <linux/spinlock.h>
44#include <linux/platform_device.h>
45#include <linux/pm_runtime.h>
46#include <linux/interrupt.h>
47#include <linux/ioport.h>
48#include <linux/io.h>
49#include <linux/list.h>
50#include <linux/delay.h>
51#include <linux/dma-mapping.h>
52
53#include <linux/usb/ch9.h>
54#include <linux/usb/gadget.h>
55
56#include "core.h"
57#include "gadget.h"
58#include "io.h"
59
60#include "debug.h"
61
62/**
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
85 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
97 /* After PHYs are stable we can take Core out of reset state */
98 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
99 reg &= ~DWC3_GCTL_CORESOFTRESET;
100 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
101}
102
103/**
104 * dwc3_free_one_event_buffer - Frees one event buffer
105 * @dwc: Pointer to our controller context structure
106 * @evt: Pointer to event buffer to be freed
107 */
108static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
109 struct dwc3_event_buffer *evt)
110{
111 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
112 kfree(evt);
113}
114
115/**
116 * dwc3_alloc_one_event_buffer - Allocated one event buffer structure
117 * @dwc: Pointer to our controller context structure
118 * @length: size of the event buffer
119 *
120 * Returns a pointer to the allocated event buffer structure on succes
121 * otherwise ERR_PTR(errno).
122 */
123static struct dwc3_event_buffer *__devinit
124dwc3_alloc_one_event_buffer(struct dwc3 *dwc, unsigned length)
125{
126 struct dwc3_event_buffer *evt;
127
128 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
129 if (!evt)
130 return ERR_PTR(-ENOMEM);
131
132 evt->dwc = dwc;
133 evt->length = length;
134 evt->buf = dma_alloc_coherent(dwc->dev, length,
135 &evt->dma, GFP_KERNEL);
136 if (!evt->buf) {
137 kfree(evt);
138 return ERR_PTR(-ENOMEM);
139 }
140
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
153 for (i = 0; i < DWC3_EVENT_BUFFERS_NUM; i++) {
154 evt = dwc->ev_buffs[i];
155 if (evt) {
156 dwc3_free_one_event_buffer(dwc, evt);
157 dwc->ev_buffs[i] = NULL;
158 }
159 }
160}
161
162/**
163 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
164 * @dwc: Pointer to out controller context structure
165 * @num: number of event buffers to allocate
166 * @length: size of event buffer
167 *
168 * Returns 0 on success otherwise negative errno. In error the case, dwc
169 * may contain some buffers allocated but not all which were requested.
170 */
171static int __devinit dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned num,
172 unsigned length)
173{
174 int i;
175
176 for (i = 0; i < num; i++) {
177 struct dwc3_event_buffer *evt;
178
179 evt = dwc3_alloc_one_event_buffer(dwc, length);
180 if (IS_ERR(evt)) {
181 dev_err(dwc->dev, "can't allocate event buffer\n");
182 return PTR_ERR(evt);
183 }
184 dwc->ev_buffs[i] = evt;
185 }
186
187 return 0;
188}
189
190/**
191 * dwc3_event_buffers_setup - setup our allocated event buffers
192 * @dwc: Pointer to out controller context structure
193 *
194 * Returns 0 on success otherwise negative errno.
195 */
196static int __devinit dwc3_event_buffers_setup(struct dwc3 *dwc)
197{
198 struct dwc3_event_buffer *evt;
199 int n;
200
201 for (n = 0; n < DWC3_EVENT_BUFFERS_NUM; n++) {
202 evt = dwc->ev_buffs[n];
203 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
204 evt->buf, (unsigned long long) evt->dma,
205 evt->length);
206
207 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
208 lower_32_bits(evt->dma));
209 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
210 upper_32_bits(evt->dma));
211 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
212 evt->length & 0xffff);
213 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
214 }
215
216 return 0;
217}
218
219static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
220{
221 struct dwc3_event_buffer *evt;
222 int n;
223
224 for (n = 0; n < DWC3_EVENT_BUFFERS_NUM; n++) {
225 evt = dwc->ev_buffs[n];
226 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
227 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
228 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
229 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
230 }
231}
232
233/**
234 * dwc3_core_init - Low-level initialization of DWC3 Core
235 * @dwc: Pointer to our controller context structure
236 *
237 * Returns 0 on success otherwise negative errno.
238 */
239static int __devinit dwc3_core_init(struct dwc3 *dwc)
240{
241 unsigned long timeout;
242 u32 reg;
243 int ret;
244
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200245 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
246 /* This should read as U3 followed by revision number */
247 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
248 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
249 ret = -ENODEV;
250 goto err0;
251 }
252 dwc->revision = reg & DWC3_GSNPSREV_MASK;
253
Felipe Balbi72246da2011-08-19 18:10:58 +0300254 dwc3_core_soft_reset(dwc);
255
256 /* issue device SoftReset too */
257 timeout = jiffies + msecs_to_jiffies(500);
258 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
259 do {
260 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
261 if (!(reg & DWC3_DCTL_CSFTRST))
262 break;
263
264 if (time_after(jiffies, timeout)) {
265 dev_err(dwc->dev, "Reset Timed Out\n");
266 ret = -ETIMEDOUT;
267 goto err0;
268 }
269
270 cpu_relax();
271 } while (true);
272
Felipe Balbi72246da2011-08-19 18:10:58 +0300273 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_NUM,
274 DWC3_EVENT_BUFFERS_SIZE);
275 if (ret) {
276 dev_err(dwc->dev, "failed to allocate event buffers\n");
277 ret = -ENOMEM;
278 goto err1;
279 }
280
281 ret = dwc3_event_buffers_setup(dwc);
282 if (ret) {
283 dev_err(dwc->dev, "failed to setup event buffers\n");
284 goto err1;
285 }
286
287 return 0;
288
289err1:
290 dwc3_free_event_buffers(dwc);
291
292err0:
293 return ret;
294}
295
296static void dwc3_core_exit(struct dwc3 *dwc)
297{
298 dwc3_event_buffers_cleanup(dwc);
299 dwc3_free_event_buffers(dwc);
300}
301
302#define DWC3_ALIGN_MASK (16 - 1)
303
304static int __devinit dwc3_probe(struct platform_device *pdev)
305{
306 const struct platform_device_id *id = platform_get_device_id(pdev);
307 struct resource *res;
308 struct dwc3 *dwc;
309 void __iomem *regs;
310 unsigned int features = id->driver_data;
311 int ret = -ENOMEM;
312 int irq;
313 void *mem;
314
315 mem = kzalloc(sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
316 if (!mem) {
317 dev_err(&pdev->dev, "not enough memory\n");
318 goto err0;
319 }
320 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
321 dwc->mem = mem;
322
323 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
324 if (!res) {
325 dev_err(&pdev->dev, "missing resource\n");
326 goto err1;
327 }
328
329 res = request_mem_region(res->start, resource_size(res),
330 dev_name(&pdev->dev));
331 if (!res) {
332 dev_err(&pdev->dev, "can't request mem region\n");
333 goto err1;
334 }
335
336 regs = ioremap(res->start, resource_size(res));
337 if (!regs) {
338 dev_err(&pdev->dev, "ioremap failed\n");
339 goto err2;
340 }
341
342 irq = platform_get_irq(pdev, 0);
343 if (irq < 0) {
344 dev_err(&pdev->dev, "missing IRQ\n");
345 goto err3;
346 }
347
348 spin_lock_init(&dwc->lock);
349 platform_set_drvdata(pdev, dwc);
350
351 dwc->regs = regs;
352 dwc->regs_size = resource_size(res);
353 dwc->dev = &pdev->dev;
354 dwc->irq = irq;
355
356 pm_runtime_enable(&pdev->dev);
357 pm_runtime_get_sync(&pdev->dev);
358 pm_runtime_forbid(&pdev->dev);
359
360 ret = dwc3_core_init(dwc);
361 if (ret) {
362 dev_err(&pdev->dev, "failed to initialize core\n");
363 goto err3;
364 }
365
366 if (features & DWC3_HAS_PERIPHERAL) {
367 ret = dwc3_gadget_init(dwc);
368 if (ret) {
369 dev_err(&pdev->dev, "failed to initialized gadget\n");
370 goto err4;
371 }
372 }
373
374 ret = dwc3_debugfs_init(dwc);
375 if (ret) {
376 dev_err(&pdev->dev, "failed to initialize debugfs\n");
377 goto err5;
378 }
379
380 pm_runtime_allow(&pdev->dev);
381
382 return 0;
383
384err5:
385 if (features & DWC3_HAS_PERIPHERAL)
386 dwc3_gadget_exit(dwc);
387
388err4:
389 dwc3_core_exit(dwc);
390
391err3:
392 iounmap(regs);
393
394err2:
395 release_mem_region(res->start, resource_size(res));
396
397err1:
398 kfree(dwc->mem);
399
400err0:
401 return ret;
402}
403
404static int __devexit dwc3_remove(struct platform_device *pdev)
405{
406 const struct platform_device_id *id = platform_get_device_id(pdev);
407 struct dwc3 *dwc = platform_get_drvdata(pdev);
408 struct resource *res;
409 unsigned int features = id->driver_data;
410
411 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
412
413 pm_runtime_put(&pdev->dev);
414 pm_runtime_disable(&pdev->dev);
415
416 dwc3_debugfs_exit(dwc);
417
418 if (features & DWC3_HAS_PERIPHERAL)
419 dwc3_gadget_exit(dwc);
420
421 dwc3_core_exit(dwc);
422 release_mem_region(res->start, resource_size(res));
423 iounmap(dwc->regs);
424 kfree(dwc->mem);
425
426 return 0;
427}
428
429static const struct platform_device_id dwc3_id_table[] __devinitconst = {
430 {
431 .name = "dwc3-omap",
432 .driver_data = (DWC3_HAS_PERIPHERAL
433 | DWC3_HAS_XHCI
434 | DWC3_HAS_OTG),
435 },
436 {
437 .name = "dwc3-pci",
438 .driver_data = DWC3_HAS_PERIPHERAL,
439 },
440 { }, /* Terminating Entry */
441};
442MODULE_DEVICE_TABLE(platform, dwc3_id_table);
443
444static struct platform_driver dwc3_driver = {
445 .probe = dwc3_probe,
446 .remove = __devexit_p(dwc3_remove),
447 .driver = {
448 .name = "dwc3",
449 },
450 .id_table = dwc3_id_table,
451};
452
453MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
454MODULE_LICENSE("Dual BSD/GPL");
455MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");
456
457static int __devinit dwc3_init(void)
458{
459 return platform_driver_register(&dwc3_driver);
460}
461module_init(dwc3_init);
462
463static void __exit dwc3_exit(void)
464{
465 platform_driver_unregister(&dwc3_driver);
466}
467module_exit(dwc3_exit);