2 * core.c - DesignWare USB3 DRD Controller Core file
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/module.h>
40 #include <linux/kernel.h>
41 #include <linux/slab.h>
42 #include <linux/spinlock.h>
43 #include <linux/platform_device.h>
44 #include <linux/pm_runtime.h>
45 #include <linux/interrupt.h>
46 #include <linux/ioport.h>
48 #include <linux/list.h>
49 #include <linux/delay.h>
50 #include <linux/dma-mapping.h>
52 #include <linux/usb/ch9.h>
53 #include <linux/usb/gadget.h>
54 #include <linux/module.h>
62 static char *maximum_speed = "super";
63 module_param(maximum_speed, charp, 0);
64 MODULE_PARM_DESC(maximum_speed, "Maximum supported speed.");
66 /* -------------------------------------------------------------------------- */
68 #define DWC3_DEVS_POSSIBLE 32
70 static DECLARE_BITMAP(dwc3_devs, DWC3_DEVS_POSSIBLE);
72 int dwc3_get_device_id(void)
77 id = find_first_zero_bit(dwc3_devs, DWC3_DEVS_POSSIBLE);
78 if (id < DWC3_DEVS_POSSIBLE) {
81 old = test_and_set_bit(id, dwc3_devs);
85 pr_err("dwc3: no space for new device\n");
91 EXPORT_SYMBOL_GPL(dwc3_get_device_id);
93 void dwc3_put_device_id(int id)
100 ret = test_bit(id, dwc3_devs);
101 WARN(!ret, "dwc3: ID %d not in use\n", id);
102 clear_bit(id, dwc3_devs);
104 EXPORT_SYMBOL_GPL(dwc3_put_device_id);
106 /* -------------------------------------------------------------------------- */
109 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
110 * @dwc: pointer to our context structure
112 static void dwc3_core_soft_reset(struct dwc3 *dwc)
116 /* Before Resetting PHY, put Core in Reset */
117 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
118 reg |= DWC3_GCTL_CORESOFTRESET;
119 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
121 /* Assert USB3 PHY reset */
122 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
123 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
124 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
126 /* Assert USB2 PHY reset */
127 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
128 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
129 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
133 /* Clear USB3 PHY reset */
134 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
135 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
136 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
138 /* Clear USB2 PHY reset */
139 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
140 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
141 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
143 /* After PHYs are stable we can take Core out of reset state */
144 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
145 reg &= ~DWC3_GCTL_CORESOFTRESET;
146 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
150 * dwc3_free_one_event_buffer - Frees one event buffer
151 * @dwc: Pointer to our controller context structure
152 * @evt: Pointer to event buffer to be freed
154 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
155 struct dwc3_event_buffer *evt)
157 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
162 * dwc3_alloc_one_event_buffer - Allocated one event buffer structure
163 * @dwc: Pointer to our controller context structure
164 * @length: size of the event buffer
166 * Returns a pointer to the allocated event buffer structure on succes
167 * otherwise ERR_PTR(errno).
169 static struct dwc3_event_buffer *__devinit
170 dwc3_alloc_one_event_buffer(struct dwc3 *dwc, unsigned length)
172 struct dwc3_event_buffer *evt;
174 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
176 return ERR_PTR(-ENOMEM);
179 evt->length = length;
180 evt->buf = dma_alloc_coherent(dwc->dev, length,
181 &evt->dma, GFP_KERNEL);
184 return ERR_PTR(-ENOMEM);
191 * dwc3_free_event_buffers - frees all allocated event buffers
192 * @dwc: Pointer to our controller context structure
194 static void dwc3_free_event_buffers(struct dwc3 *dwc)
196 struct dwc3_event_buffer *evt;
199 for (i = 0; i < dwc->num_event_buffers; i++) {
200 evt = dwc->ev_buffs[i];
202 dwc3_free_one_event_buffer(dwc, evt);
203 dwc->ev_buffs[i] = NULL;
209 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
210 * @dwc: Pointer to out controller context structure
211 * @length: size of event buffer
213 * Returns 0 on success otherwise negative errno. In error the case, dwc
214 * may contain some buffers allocated but not all which were requested.
216 static int __devinit dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
221 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
222 dwc->num_event_buffers = num;
224 dwc->ev_buffs = kzalloc(sizeof(*dwc->ev_buffs) * num, GFP_KERNEL);
225 if (!dwc->ev_buffs) {
226 dev_err(dwc->dev, "can't allocate event buffers array\n");
230 for (i = 0; i < num; i++) {
231 struct dwc3_event_buffer *evt;
233 evt = dwc3_alloc_one_event_buffer(dwc, length);
235 dev_err(dwc->dev, "can't allocate event buffer\n");
238 dwc->ev_buffs[i] = evt;
245 * dwc3_event_buffers_setup - setup our allocated event buffers
246 * @dwc: Pointer to out controller context structure
248 * Returns 0 on success otherwise negative errno.
250 static int __devinit dwc3_event_buffers_setup(struct dwc3 *dwc)
252 struct dwc3_event_buffer *evt;
255 for (n = 0; n < dwc->num_event_buffers; n++) {
256 evt = dwc->ev_buffs[n];
257 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
258 evt->buf, (unsigned long long) evt->dma,
261 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
262 lower_32_bits(evt->dma));
263 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
264 upper_32_bits(evt->dma));
265 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
266 evt->length & 0xffff);
267 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
273 static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
275 struct dwc3_event_buffer *evt;
278 for (n = 0; n < dwc->num_event_buffers; n++) {
279 evt = dwc->ev_buffs[n];
280 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
281 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
282 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
283 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
287 static void __devinit dwc3_cache_hwparams(struct dwc3 *dwc)
289 struct dwc3_hwparams *parms = &dwc->hwparams;
291 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
292 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
293 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
294 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
295 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
296 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
297 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
298 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
299 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
303 * dwc3_core_init - Low-level initialization of DWC3 Core
304 * @dwc: Pointer to our controller context structure
306 * Returns 0 on success otherwise negative errno.
308 static int __devinit dwc3_core_init(struct dwc3 *dwc)
310 unsigned long timeout;
314 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
315 /* This should read as U3 followed by revision number */
316 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
317 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
321 dwc->revision = reg & DWC3_GSNPSREV_MASK;
323 dwc3_core_soft_reset(dwc);
325 /* issue device SoftReset too */
326 timeout = jiffies + msecs_to_jiffies(500);
327 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
329 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
330 if (!(reg & DWC3_DCTL_CSFTRST))
333 if (time_after(jiffies, timeout)) {
334 dev_err(dwc->dev, "Reset Timed Out\n");
342 dwc3_cache_hwparams(dwc);
344 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
346 dev_err(dwc->dev, "failed to allocate event buffers\n");
351 ret = dwc3_event_buffers_setup(dwc);
353 dev_err(dwc->dev, "failed to setup event buffers\n");
360 dwc3_free_event_buffers(dwc);
366 static void dwc3_core_exit(struct dwc3 *dwc)
368 dwc3_event_buffers_cleanup(dwc);
369 dwc3_free_event_buffers(dwc);
372 #define DWC3_ALIGN_MASK (16 - 1)
374 static int __devinit dwc3_probe(struct platform_device *pdev)
376 struct resource *res;
387 mem = kzalloc(sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
389 dev_err(&pdev->dev, "not enough memory\n");
392 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
395 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
397 dev_err(&pdev->dev, "missing resource\n");
403 res = request_mem_region(res->start, resource_size(res),
404 dev_name(&pdev->dev));
406 dev_err(&pdev->dev, "can't request mem region\n");
410 regs = ioremap(res->start, resource_size(res));
412 dev_err(&pdev->dev, "ioremap failed\n");
416 irq = platform_get_irq(pdev, 0);
418 dev_err(&pdev->dev, "missing IRQ\n");
422 spin_lock_init(&dwc->lock);
423 platform_set_drvdata(pdev, dwc);
426 dwc->regs_size = resource_size(res);
427 dwc->dev = &pdev->dev;
430 if (!strncmp("super", maximum_speed, 5))
431 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
432 else if (!strncmp("high", maximum_speed, 4))
433 dwc->maximum_speed = DWC3_DCFG_HIGHSPEED;
434 else if (!strncmp("full", maximum_speed, 4))
435 dwc->maximum_speed = DWC3_DCFG_FULLSPEED1;
436 else if (!strncmp("low", maximum_speed, 3))
437 dwc->maximum_speed = DWC3_DCFG_LOWSPEED;
439 dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
441 pm_runtime_enable(&pdev->dev);
442 pm_runtime_get_sync(&pdev->dev);
443 pm_runtime_forbid(&pdev->dev);
445 ret = dwc3_core_init(dwc);
447 dev_err(&pdev->dev, "failed to initialize core\n");
451 mode = DWC3_MODE(dwc->hwparams.hwparams0);
454 case DWC3_MODE_DEVICE:
455 ret = dwc3_gadget_init(dwc);
457 dev_err(&pdev->dev, "failed to initialize gadget\n");
462 ret = dwc3_host_init(dwc);
464 dev_err(&pdev->dev, "failed to initialize host\n");
469 ret = dwc3_host_init(dwc);
471 dev_err(&pdev->dev, "failed to initialize host\n");
475 ret = dwc3_gadget_init(dwc);
477 dev_err(&pdev->dev, "failed to initialize gadget\n");
482 dev_err(&pdev->dev, "Unsupported mode of operation %d\n", mode);
487 ret = dwc3_debugfs_init(dwc);
489 dev_err(&pdev->dev, "failed to initialize debugfs\n");
493 pm_runtime_allow(&pdev->dev);
499 case DWC3_MODE_DEVICE:
500 dwc3_gadget_exit(dwc);
507 dwc3_gadget_exit(dwc);
521 release_mem_region(res->start, resource_size(res));
530 static int __devexit dwc3_remove(struct platform_device *pdev)
532 struct dwc3 *dwc = platform_get_drvdata(pdev);
533 struct resource *res;
535 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
537 pm_runtime_put(&pdev->dev);
538 pm_runtime_disable(&pdev->dev);
540 dwc3_debugfs_exit(dwc);
543 case DWC3_MODE_DEVICE:
544 dwc3_gadget_exit(dwc);
551 dwc3_gadget_exit(dwc);
559 release_mem_region(res->start, resource_size(res));
566 static struct platform_driver dwc3_driver = {
568 .remove = __devexit_p(dwc3_remove),
574 MODULE_ALIAS("platform:dwc3");
575 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
576 MODULE_LICENSE("Dual BSD/GPL");
577 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");
579 static int __devinit dwc3_init(void)
581 return platform_driver_register(&dwc3_driver);
583 module_init(dwc3_init);
585 static void __exit dwc3_exit(void)
587 platform_driver_unregister(&dwc3_driver);
589 module_exit(dwc3_exit);