blob: 17d1822f768708d9a203b4f53a75c31e6ec71eab [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * dwc3-omap.c - OMAP Specific Glue layer
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
40#include <linux/kernel.h>
41#include <linux/slab.h>
42#include <linux/interrupt.h>
43#include <linux/spinlock.h>
44#include <linux/platform_device.h>
45#include <linux/dma-mapping.h>
46#include <linux/ioport.h>
47#include <linux/io.h>
48
49#include "io.h"
50
51/*
52 * All these registers belong to OMAP's Wrapper around the
53 * DesignWare USB3 Core.
54 */
55
56#define USBOTGSS_REVISION 0x0000
57#define USBOTGSS_SYSCONFIG 0x0010
58#define USBOTGSS_IRQ_EOI 0x0020
59#define USBOTGSS_IRQSTATUS_RAW_0 0x0024
60#define USBOTGSS_IRQSTATUS_0 0x0028
61#define USBOTGSS_IRQENABLE_SET_0 0x002c
62#define USBOTGSS_IRQENABLE_CLR_0 0x0030
63#define USBOTGSS_IRQSTATUS_RAW_1 0x0034
64#define USBOTGSS_IRQSTATUS_1 0x0038
65#define USBOTGSS_IRQENABLE_SET_1 0x003c
66#define USBOTGSS_IRQENABLE_CLR_1 0x0040
67#define USBOTGSS_UTMI_OTG_CTRL 0x0080
68#define USBOTGSS_UTMI_OTG_STATUS 0x0084
69#define USBOTGSS_MMRAM_OFFSET 0x0100
70#define USBOTGSS_FLADJ 0x0104
71#define USBOTGSS_DEBUG_CFG 0x0108
72#define USBOTGSS_DEBUG_DATA 0x010c
73
74/* SYSCONFIG REGISTER */
75#define USBOTGSS_SYSCONFIG_DMADISABLE (1 << 16)
76#define USBOTGSS_SYSCONFIG_STANDBYMODE(x) ((x) << 4)
77#define USBOTGSS_SYSCONFIG_IDLEMODE(x) ((x) << 2)
78
79/* IRQ_EOI REGISTER */
80#define USBOTGSS_IRQ_EOI_LINE_NUMBER (1 << 0)
81
82/* IRQS0 BITS */
83#define USBOTGSS_IRQO_COREIRQ_ST (1 << 0)
84
85/* IRQ1 BITS */
86#define USBOTGSS_IRQ1_DMADISABLECLR (1 << 17)
87#define USBOTGSS_IRQ1_OEVT (1 << 16)
88#define USBOTGSS_IRQ1_DRVVBUS_RISE (1 << 13)
89#define USBOTGSS_IRQ1_CHRGVBUS_RISE (1 << 12)
90#define USBOTGSS_IRQ1_DISCHRGVBUS_RISE (1 << 11)
91#define USBOTGSS_IRQ1_IDPULLUP_RISE (1 << 8)
92#define USBOTGSS_IRQ1_DRVVBUS_FALL (1 << 5)
93#define USBOTGSS_IRQ1_CHRGVBUS_FALL (1 << 4)
94#define USBOTGSS_IRQ1_DISCHRGVBUS_FALL (1 << 3)
95#define USBOTGSS_IRQ1_IDPULLUP_FALL (1 << 0)
96
97/* UTMI_OTG_CTRL REGISTER */
98#define USBOTGSS_UTMI_OTG_CTRL_DRVVBUS (1 << 5)
99#define USBOTGSS_UTMI_OTG_CTRL_CHRGVBUS (1 << 4)
100#define USBOTGSS_UTMI_OTG_CTRL_DISCHRGVBUS (1 << 3)
101#define USBOTGSS_UTMI_OTG_CTRL_IDPULLUP (1 << 0)
102
103/* UTMI_OTG_STATUS REGISTER */
104#define USBOTGSS_UTMI_OTG_STATUS_SW_MODE (1 << 31)
105#define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT (1 << 9)
106#define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE (1 << 8)
107#define USBOTGSS_UTMI_OTG_STATUS_IDDIG (1 << 4)
108#define USBOTGSS_UTMI_OTG_STATUS_SESSEND (1 << 3)
109#define USBOTGSS_UTMI_OTG_STATUS_SESSVALID (1 << 2)
110#define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID (1 << 1)
111
112struct dwc3_omap {
113 /* device lock */
114 spinlock_t lock;
115
116 struct platform_device *dwc3;
117 struct device *dev;
118
119 int irq;
120 void __iomem *base;
121
122 void *context;
123 u32 resource_size;
124
125 u32 dma_status:1;
126};
127
Felipe Balbi72246da2011-08-19 18:10:58 +0300128static irqreturn_t dwc3_omap_interrupt(int irq, void *_omap)
129{
130 struct dwc3_omap *omap = _omap;
131 u32 reg;
132 u32 ctrl;
133
134 spin_lock(&omap->lock);
135
136 reg = dwc3_readl(omap->base, USBOTGSS_IRQSTATUS_1);
137 ctrl = dwc3_readl(omap->base, USBOTGSS_UTMI_OTG_CTRL);
138
139 if (reg & USBOTGSS_IRQ1_DMADISABLECLR) {
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300140 dev_dbg(omap->dev, "DMA Disable was Cleared\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300141 omap->dma_status = false;
142 }
143
144 if (reg & USBOTGSS_IRQ1_OEVT)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300145 dev_dbg(omap->dev, "OTG Event\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300146
147 if (reg & USBOTGSS_IRQ1_DRVVBUS_RISE) {
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300148 dev_dbg(omap->dev, "DRVVBUS Rise\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300149 ctrl |= USBOTGSS_UTMI_OTG_CTRL_DRVVBUS;
150 }
151
152 if (reg & USBOTGSS_IRQ1_CHRGVBUS_RISE) {
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300153 dev_dbg(omap->dev, "CHRGVBUS Rise\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300154 ctrl |= USBOTGSS_UTMI_OTG_CTRL_CHRGVBUS;
155 }
156
157 if (reg & USBOTGSS_IRQ1_DISCHRGVBUS_RISE) {
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300158 dev_dbg(omap->dev, "DISCHRGVBUS Rise\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300159 ctrl |= USBOTGSS_UTMI_OTG_CTRL_DISCHRGVBUS;
160 }
161
162 if (reg & USBOTGSS_IRQ1_IDPULLUP_RISE) {
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300163 dev_dbg(omap->dev, "IDPULLUP Rise\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300164 ctrl |= USBOTGSS_UTMI_OTG_CTRL_IDPULLUP;
165 }
166
167 if (reg & USBOTGSS_IRQ1_DRVVBUS_FALL) {
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300168 dev_dbg(omap->dev, "DRVVBUS Fall\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300169 ctrl &= ~USBOTGSS_UTMI_OTG_CTRL_DRVVBUS;
170 }
171
172 if (reg & USBOTGSS_IRQ1_CHRGVBUS_FALL) {
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300173 dev_dbg(omap->dev, "CHRGVBUS Fall\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300174 ctrl &= ~USBOTGSS_UTMI_OTG_CTRL_CHRGVBUS;
175 }
176
177 if (reg & USBOTGSS_IRQ1_DISCHRGVBUS_FALL) {
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300178 dev_dbg(omap->dev, "DISCHRGVBUS Fall\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300179 ctrl &= ~USBOTGSS_UTMI_OTG_CTRL_DISCHRGVBUS;
180 }
181
182 if (reg & USBOTGSS_IRQ1_IDPULLUP_FALL) {
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300183 dev_dbg(omap->dev, "IDPULLUP Fall\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300184 ctrl &= ~USBOTGSS_UTMI_OTG_CTRL_IDPULLUP;
185 }
186
187 dwc3_writel(omap->base, USBOTGSS_UTMI_OTG_CTRL, ctrl);
188
189 spin_unlock(&omap->lock);
190
191 return IRQ_HANDLED;
192}
193
194static int __devinit dwc3_omap_probe(struct platform_device *pdev)
195{
196 struct platform_device *dwc3;
197 struct dwc3_omap *omap;
198 struct resource *res;
199
200 int ret = -ENOMEM;
201 int irq;
202
203 u32 reg;
204
205 void __iomem *base;
206 void *context;
207
208 omap = kzalloc(sizeof(*omap), GFP_KERNEL);
209 if (!omap) {
210 dev_err(&pdev->dev, "not enough memory\n");
211 goto err0;
212 }
213
214 platform_set_drvdata(pdev, omap);
215
216 irq = platform_get_irq(pdev, 1);
217 if (irq < 0) {
218 dev_err(&pdev->dev, "missing IRQ resource\n");
219 ret = -EINVAL;
220 goto err1;
221 }
222
223 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
224 if (!res) {
225 dev_err(&pdev->dev, "missing memory base resource\n");
226 ret = -EINVAL;
227 goto err1;
228 }
229
230 base = ioremap_nocache(res->start, resource_size(res));
231 if (!base) {
232 dev_err(&pdev->dev, "ioremap failed\n");
233 goto err1;
234 }
235
236 dwc3 = platform_device_alloc("dwc3-omap", -1);
237 if (!dwc3) {
238 dev_err(&pdev->dev, "couldn't allocate dwc3 device\n");
239 goto err2;
240 }
241
242 context = kzalloc(resource_size(res), GFP_KERNEL);
243 if (!context) {
244 dev_err(&pdev->dev, "couldn't allocate dwc3 context memory\n");
245 goto err3;
246 }
247
248 spin_lock_init(&omap->lock);
249 dma_set_coherent_mask(&dwc3->dev, pdev->dev.coherent_dma_mask);
250
251 dwc3->dev.parent = &pdev->dev;
252 dwc3->dev.dma_mask = pdev->dev.dma_mask;
253 dwc3->dev.dma_parms = pdev->dev.dma_parms;
254 omap->resource_size = resource_size(res);
255 omap->context = context;
256 omap->dev = &pdev->dev;
257 omap->irq = irq;
258 omap->base = base;
259 omap->dwc3 = dwc3;
260
261 /* check the DMA Status */
262 reg = dwc3_readl(omap->base, USBOTGSS_SYSCONFIG);
263 omap->dma_status = !!(reg & USBOTGSS_SYSCONFIG_DMADISABLE);
264
265 ret = request_irq(omap->irq, dwc3_omap_interrupt, 0,
266 "dwc3-wrapper", omap);
267 if (ret) {
268 dev_err(&pdev->dev, "failed to request IRQ #%d --> %d\n",
269 omap->irq, ret);
270 goto err4;
271 }
272
273 /* enable all IRQs */
Felipe Balbidf01c612011-09-01 18:22:01 +0300274 reg = USBOTGSS_IRQO_COREIRQ_ST;
275 dwc3_writel(omap->base, USBOTGSS_IRQENABLE_SET_0, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300276
Felipe Balbi324e5482011-09-01 14:52:52 +0300277 reg = (USBOTGSS_IRQ1_OEVT |
Felipe Balbi72246da2011-08-19 18:10:58 +0300278 USBOTGSS_IRQ1_DRVVBUS_RISE |
279 USBOTGSS_IRQ1_CHRGVBUS_RISE |
280 USBOTGSS_IRQ1_DISCHRGVBUS_RISE |
281 USBOTGSS_IRQ1_IDPULLUP_RISE |
282 USBOTGSS_IRQ1_DRVVBUS_FALL |
283 USBOTGSS_IRQ1_CHRGVBUS_FALL |
284 USBOTGSS_IRQ1_DISCHRGVBUS_FALL |
285 USBOTGSS_IRQ1_IDPULLUP_FALL);
286
287 dwc3_writel(omap->base, USBOTGSS_IRQENABLE_SET_1, reg);
288
289 ret = platform_device_add_resources(dwc3, pdev->resource,
290 pdev->num_resources);
291 if (ret) {
292 dev_err(&pdev->dev, "couldn't add resources to dwc3 device\n");
293 goto err5;
294 }
295
296 ret = platform_device_add(dwc3);
297 if (ret) {
298 dev_err(&pdev->dev, "failed to register dwc3 device\n");
299 goto err5;
300 }
301
302 return 0;
303
304err5:
305 free_irq(omap->irq, omap);
306
307err4:
308 kfree(omap->context);
309
310err3:
311 platform_device_put(dwc3);
312
313err2:
314 iounmap(base);
315
316err1:
317 kfree(omap);
318
319err0:
320 return ret;
321}
322
323static int __devexit dwc3_omap_remove(struct platform_device *pdev)
324{
325 struct dwc3_omap *omap = platform_get_drvdata(pdev);
326
327 platform_device_unregister(omap->dwc3);
328
329 free_irq(omap->irq, omap);
330 iounmap(omap->base);
331
332 kfree(omap->context);
333 kfree(omap);
334
335 return 0;
336}
337
338static const struct of_device_id of_dwc3_matach[] = {
339 {
340 "ti,dwc3",
341 },
342 { },
343};
344MODULE_DEVICE_TABLE(of, of_dwc3_matach);
345
346static struct platform_driver dwc3_omap_driver = {
347 .probe = dwc3_omap_probe,
348 .remove = __devexit_p(dwc3_omap_remove),
349 .driver = {
350 .name = "omap-dwc3",
Felipe Balbi72246da2011-08-19 18:10:58 +0300351 .of_match_table = of_dwc3_matach,
352 },
353};
354
355MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
356MODULE_LICENSE("Dual BSD/GPL");
357MODULE_DESCRIPTION("DesignWare USB3 OMAP Glue Layer");
358
359static int __devinit dwc3_omap_init(void)
360{
361 return platform_driver_register(&dwc3_omap_driver);
362}
363module_init(dwc3_omap_init);
364
365static void __exit dwc3_omap_exit(void)
366{
367 platform_driver_unregister(&dwc3_omap_driver);
368}
369module_exit(dwc3_omap_exit);