blob: b269dbd47fc41edef682380bcd3637a20371b9ee [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
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 */
18
Felipe Balbia72e6582011-09-05 13:37:28 +030019#include <linux/module.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030020#include <linux/kernel.h>
21#include <linux/slab.h>
22#include <linux/interrupt.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030023#include <linux/platform_device.h>
Felipe Balbi99624442011-09-01 22:26:25 +030024#include <linux/platform_data/dwc3-omap.h>
Kishon Vijay Abraham Iaf310e92013-01-25 08:30:47 +053025#include <linux/pm_runtime.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030026#include <linux/dma-mapping.h>
27#include <linux/ioport.h>
28#include <linux/io.h>
Felipe Balbi45b3cd4a2012-01-25 11:07:03 +020029#include <linux/of.h>
Kishon Vijay Abraham Ib4bfe6a2013-01-25 08:30:46 +053030#include <linux/of_platform.h>
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +090031#include <linux/extcon.h>
32#include <linux/extcon/of_extcon.h>
33#include <linux/regulator/consumer.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030034
Felipe Balbia418cc42012-07-19 13:56:07 +030035#include <linux/usb/otg.h>
Felipe Balbia418cc42012-07-19 13:56:07 +030036
Felipe Balbi72246da2011-08-19 18:10:58 +030037/*
38 * All these registers belong to OMAP's Wrapper around the
39 * DesignWare USB3 Core.
40 */
41
42#define USBOTGSS_REVISION 0x0000
43#define USBOTGSS_SYSCONFIG 0x0010
44#define USBOTGSS_IRQ_EOI 0x0020
George Cherianff7307b2013-06-12 14:53:46 +053045#define USBOTGSS_EOI_OFFSET 0x0008
Felipe Balbi72246da2011-08-19 18:10:58 +030046#define USBOTGSS_IRQSTATUS_RAW_0 0x0024
47#define USBOTGSS_IRQSTATUS_0 0x0028
48#define USBOTGSS_IRQENABLE_SET_0 0x002c
49#define USBOTGSS_IRQENABLE_CLR_0 0x0030
George Cherianff7307b2013-06-12 14:53:46 +053050#define USBOTGSS_IRQ0_OFFSET 0x0004
George Cherianb1fd6cb2013-06-12 14:53:47 +053051#define USBOTGSS_IRQSTATUS_RAW_1 0x0030
52#define USBOTGSS_IRQSTATUS_1 0x0034
53#define USBOTGSS_IRQENABLE_SET_1 0x0038
54#define USBOTGSS_IRQENABLE_CLR_1 0x003c
55#define USBOTGSS_IRQSTATUS_RAW_2 0x0040
56#define USBOTGSS_IRQSTATUS_2 0x0044
57#define USBOTGSS_IRQENABLE_SET_2 0x0048
58#define USBOTGSS_IRQENABLE_CLR_2 0x004c
59#define USBOTGSS_IRQSTATUS_RAW_3 0x0050
60#define USBOTGSS_IRQSTATUS_3 0x0054
61#define USBOTGSS_IRQENABLE_SET_3 0x0058
62#define USBOTGSS_IRQENABLE_CLR_3 0x005c
George Cherianff7307b2013-06-12 14:53:46 +053063#define USBOTGSS_IRQSTATUS_EOI_MISC 0x0030
64#define USBOTGSS_IRQSTATUS_RAW_MISC 0x0034
65#define USBOTGSS_IRQSTATUS_MISC 0x0038
66#define USBOTGSS_IRQENABLE_SET_MISC 0x003c
67#define USBOTGSS_IRQENABLE_CLR_MISC 0x0040
68#define USBOTGSS_IRQMISC_OFFSET 0x03fc
Felipe Balbi72246da2011-08-19 18:10:58 +030069#define USBOTGSS_UTMI_OTG_CTRL 0x0080
70#define USBOTGSS_UTMI_OTG_STATUS 0x0084
George Cherianff7307b2013-06-12 14:53:46 +053071#define USBOTGSS_UTMI_OTG_OFFSET 0x0480
72#define USBOTGSS_TXFIFO_DEPTH 0x0508
73#define USBOTGSS_RXFIFO_DEPTH 0x050c
Felipe Balbi72246da2011-08-19 18:10:58 +030074#define USBOTGSS_MMRAM_OFFSET 0x0100
75#define USBOTGSS_FLADJ 0x0104
76#define USBOTGSS_DEBUG_CFG 0x0108
77#define USBOTGSS_DEBUG_DATA 0x010c
George Cherianff7307b2013-06-12 14:53:46 +053078#define USBOTGSS_DEV_EBC_EN 0x0110
79#define USBOTGSS_DEBUG_OFFSET 0x0600
Felipe Balbi72246da2011-08-19 18:10:58 +030080
George Cherianff7307b2013-06-12 14:53:46 +053081/* REVISION REGISTER */
82#define USBOTGSS_REVISION_XMAJOR(reg) ((reg >> 8) & 0x7)
83#define USBOTGSS_REVISION_XMAJOR1 1
84#define USBOTGSS_REVISION_XMAJOR2 2
Felipe Balbi72246da2011-08-19 18:10:58 +030085/* SYSCONFIG REGISTER */
86#define USBOTGSS_SYSCONFIG_DMADISABLE (1 << 16)
Felipe Balbi4b5faa7a2011-09-06 10:56:51 +030087
Felipe Balbi72246da2011-08-19 18:10:58 +030088/* IRQ_EOI REGISTER */
89#define USBOTGSS_IRQ_EOI_LINE_NUMBER (1 << 0)
90
91/* IRQS0 BITS */
92#define USBOTGSS_IRQO_COREIRQ_ST (1 << 0)
93
George Cherianb1fd6cb2013-06-12 14:53:47 +053094/* IRQMISC BITS */
95#define USBOTGSS_IRQMISC_DMADISABLECLR (1 << 17)
96#define USBOTGSS_IRQMISC_OEVT (1 << 16)
97#define USBOTGSS_IRQMISC_DRVVBUS_RISE (1 << 13)
98#define USBOTGSS_IRQMISC_CHRGVBUS_RISE (1 << 12)
99#define USBOTGSS_IRQMISC_DISCHRGVBUS_RISE (1 << 11)
100#define USBOTGSS_IRQMISC_IDPULLUP_RISE (1 << 8)
101#define USBOTGSS_IRQMISC_DRVVBUS_FALL (1 << 5)
102#define USBOTGSS_IRQMISC_CHRGVBUS_FALL (1 << 4)
103#define USBOTGSS_IRQMISC_DISCHRGVBUS_FALL (1 << 3)
104#define USBOTGSS_IRQMISC_IDPULLUP_FALL (1 << 0)
Felipe Balbi72246da2011-08-19 18:10:58 +0300105
106/* UTMI_OTG_CTRL REGISTER */
107#define USBOTGSS_UTMI_OTG_CTRL_DRVVBUS (1 << 5)
108#define USBOTGSS_UTMI_OTG_CTRL_CHRGVBUS (1 << 4)
109#define USBOTGSS_UTMI_OTG_CTRL_DISCHRGVBUS (1 << 3)
110#define USBOTGSS_UTMI_OTG_CTRL_IDPULLUP (1 << 0)
111
112/* UTMI_OTG_STATUS REGISTER */
113#define USBOTGSS_UTMI_OTG_STATUS_SW_MODE (1 << 31)
114#define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT (1 << 9)
115#define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE (1 << 8)
116#define USBOTGSS_UTMI_OTG_STATUS_IDDIG (1 << 4)
117#define USBOTGSS_UTMI_OTG_STATUS_SESSEND (1 << 3)
118#define USBOTGSS_UTMI_OTG_STATUS_SESSVALID (1 << 2)
119#define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID (1 << 1)
120
121struct dwc3_omap {
Felipe Balbi72246da2011-08-19 18:10:58 +0300122 struct device *dev;
123
124 int irq;
125 void __iomem *base;
126
Felipe Balbif3e117f2013-02-11 11:12:02 +0200127 u32 utmi_otg_status;
George Cherian1e2a0642013-06-12 14:53:45 +0530128 u32 utmi_otg_offset;
129 u32 irqmisc_offset;
130 u32 irq_eoi_offset;
131 u32 debug_offset;
132 u32 irq0_offset;
133 u32 revision;
Felipe Balbif3e117f2013-02-11 11:12:02 +0200134
Felipe Balbi72246da2011-08-19 18:10:58 +0300135 u32 dma_status:1;
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900136
137 struct extcon_specific_cable_nb extcon_vbus_dev;
138 struct extcon_specific_cable_nb extcon_id_dev;
139 struct notifier_block vbus_nb;
140 struct notifier_block id_nb;
141
142 struct regulator *vbus_reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300143};
144
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900145enum omap_dwc3_vbus_id_status {
146 OMAP_DWC3_ID_FLOAT,
147 OMAP_DWC3_ID_GROUND,
148 OMAP_DWC3_VBUS_OFF,
149 OMAP_DWC3_VBUS_VALID,
150};
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530151
Ido Shayevitzab5e59d2012-04-24 14:18:38 +0300152static inline u32 dwc3_omap_readl(void __iomem *base, u32 offset)
153{
154 return readl(base + offset);
155}
156
157static inline void dwc3_omap_writel(void __iomem *base, u32 offset, u32 value)
158{
159 writel(value, base + offset);
160}
161
George Cherianb1fd6cb2013-06-12 14:53:47 +0530162static u32 dwc3_omap_read_utmi_status(struct dwc3_omap *omap)
163{
164 return dwc3_omap_readl(omap->base, USBOTGSS_UTMI_OTG_STATUS +
165 omap->utmi_otg_offset);
166}
167
168static void dwc3_omap_write_utmi_status(struct dwc3_omap *omap, u32 value)
169{
170 dwc3_omap_writel(omap->base, USBOTGSS_UTMI_OTG_STATUS +
171 omap->utmi_otg_offset, value);
172
173}
174
175static u32 dwc3_omap_read_irq0_status(struct dwc3_omap *omap)
176{
177 return dwc3_omap_readl(omap->base, USBOTGSS_IRQSTATUS_0 -
178 omap->irq0_offset);
179}
180
181static void dwc3_omap_write_irq0_status(struct dwc3_omap *omap, u32 value)
182{
183 dwc3_omap_writel(omap->base, USBOTGSS_IRQSTATUS_0 -
184 omap->irq0_offset, value);
185
186}
187
188static u32 dwc3_omap_read_irqmisc_status(struct dwc3_omap *omap)
189{
190 return dwc3_omap_readl(omap->base, USBOTGSS_IRQSTATUS_MISC +
191 omap->irqmisc_offset);
192}
193
194static void dwc3_omap_write_irqmisc_status(struct dwc3_omap *omap, u32 value)
195{
196 dwc3_omap_writel(omap->base, USBOTGSS_IRQSTATUS_MISC +
197 omap->irqmisc_offset, value);
198
199}
200
201static void dwc3_omap_write_irqmisc_set(struct dwc3_omap *omap, u32 value)
202{
203 dwc3_omap_writel(omap->base, USBOTGSS_IRQENABLE_SET_MISC +
204 omap->irqmisc_offset, value);
205
206}
207
208static void dwc3_omap_write_irq0_set(struct dwc3_omap *omap, u32 value)
209{
210 dwc3_omap_writel(omap->base, USBOTGSS_IRQENABLE_SET_0 -
211 omap->irq0_offset, value);
212}
213
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900214static void dwc3_omap_set_mailbox(struct dwc3_omap *omap,
215 enum omap_dwc3_vbus_id_status status)
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530216{
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900217 int ret;
218 u32 val;
Kishon Vijay Abraham I2ba79432013-03-07 18:51:44 +0530219
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530220 switch (status) {
221 case OMAP_DWC3_ID_GROUND:
222 dev_dbg(omap->dev, "ID GND\n");
223
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900224 if (omap->vbus_reg) {
225 ret = regulator_enable(omap->vbus_reg);
226 if (ret) {
227 dev_dbg(omap->dev, "regulator enable failed\n");
228 return;
229 }
230 }
231
George Cherianb1fd6cb2013-06-12 14:53:47 +0530232 val = dwc3_omap_read_utmi_status(omap);
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530233 val &= ~(USBOTGSS_UTMI_OTG_STATUS_IDDIG
234 | USBOTGSS_UTMI_OTG_STATUS_VBUSVALID
235 | USBOTGSS_UTMI_OTG_STATUS_SESSEND);
236 val |= USBOTGSS_UTMI_OTG_STATUS_SESSVALID
237 | USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT;
George Cherianb1fd6cb2013-06-12 14:53:47 +0530238 dwc3_omap_write_utmi_status(omap, val);
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530239 break;
240
241 case OMAP_DWC3_VBUS_VALID:
242 dev_dbg(omap->dev, "VBUS Connect\n");
243
George Cherianb1fd6cb2013-06-12 14:53:47 +0530244 val = dwc3_omap_read_utmi_status(omap);
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530245 val &= ~USBOTGSS_UTMI_OTG_STATUS_SESSEND;
246 val |= USBOTGSS_UTMI_OTG_STATUS_IDDIG
247 | USBOTGSS_UTMI_OTG_STATUS_VBUSVALID
248 | USBOTGSS_UTMI_OTG_STATUS_SESSVALID
249 | USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT;
George Cherianb1fd6cb2013-06-12 14:53:47 +0530250 dwc3_omap_write_utmi_status(omap, val);
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530251 break;
252
253 case OMAP_DWC3_ID_FLOAT:
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900254 if (omap->vbus_reg)
255 regulator_disable(omap->vbus_reg);
256
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530257 case OMAP_DWC3_VBUS_OFF:
258 dev_dbg(omap->dev, "VBUS Disconnect\n");
259
George Cherianb1fd6cb2013-06-12 14:53:47 +0530260 val = dwc3_omap_read_utmi_status(omap);
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530261 val &= ~(USBOTGSS_UTMI_OTG_STATUS_SESSVALID
262 | USBOTGSS_UTMI_OTG_STATUS_VBUSVALID
263 | USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT);
264 val |= USBOTGSS_UTMI_OTG_STATUS_SESSEND
265 | USBOTGSS_UTMI_OTG_STATUS_IDDIG;
George Cherianb1fd6cb2013-06-12 14:53:47 +0530266 dwc3_omap_write_utmi_status(omap, val);
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530267 break;
268
269 default:
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900270 dev_dbg(omap->dev, "invalid state\n");
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530271 }
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530272}
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530273
Felipe Balbi72246da2011-08-19 18:10:58 +0300274static irqreturn_t dwc3_omap_interrupt(int irq, void *_omap)
275{
276 struct dwc3_omap *omap = _omap;
277 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300278
George Cherianb1fd6cb2013-06-12 14:53:47 +0530279 reg = dwc3_omap_read_irqmisc_status(omap);
Felipe Balbi72246da2011-08-19 18:10:58 +0300280
George Cherianb1fd6cb2013-06-12 14:53:47 +0530281 if (reg & USBOTGSS_IRQMISC_DMADISABLECLR) {
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300282 dev_dbg(omap->dev, "DMA Disable was Cleared\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300283 omap->dma_status = false;
284 }
285
George Cherianb1fd6cb2013-06-12 14:53:47 +0530286 if (reg & USBOTGSS_IRQMISC_OEVT)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300287 dev_dbg(omap->dev, "OTG Event\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300288
George Cherianb1fd6cb2013-06-12 14:53:47 +0530289 if (reg & USBOTGSS_IRQMISC_DRVVBUS_RISE)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300290 dev_dbg(omap->dev, "DRVVBUS Rise\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300291
George Cherianb1fd6cb2013-06-12 14:53:47 +0530292 if (reg & USBOTGSS_IRQMISC_CHRGVBUS_RISE)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300293 dev_dbg(omap->dev, "CHRGVBUS Rise\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300294
George Cherianb1fd6cb2013-06-12 14:53:47 +0530295 if (reg & USBOTGSS_IRQMISC_DISCHRGVBUS_RISE)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300296 dev_dbg(omap->dev, "DISCHRGVBUS Rise\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300297
George Cherianb1fd6cb2013-06-12 14:53:47 +0530298 if (reg & USBOTGSS_IRQMISC_IDPULLUP_RISE)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300299 dev_dbg(omap->dev, "IDPULLUP Rise\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300300
George Cherianb1fd6cb2013-06-12 14:53:47 +0530301 if (reg & USBOTGSS_IRQMISC_DRVVBUS_FALL)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300302 dev_dbg(omap->dev, "DRVVBUS Fall\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300303
George Cherianb1fd6cb2013-06-12 14:53:47 +0530304 if (reg & USBOTGSS_IRQMISC_CHRGVBUS_FALL)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300305 dev_dbg(omap->dev, "CHRGVBUS Fall\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300306
George Cherianb1fd6cb2013-06-12 14:53:47 +0530307 if (reg & USBOTGSS_IRQMISC_DISCHRGVBUS_FALL)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300308 dev_dbg(omap->dev, "DISCHRGVBUS Fall\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300309
George Cherianb1fd6cb2013-06-12 14:53:47 +0530310 if (reg & USBOTGSS_IRQMISC_IDPULLUP_FALL)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300311 dev_dbg(omap->dev, "IDPULLUP Fall\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300312
George Cherianb1fd6cb2013-06-12 14:53:47 +0530313 dwc3_omap_write_irqmisc_status(omap, reg);
Felipe Balbi42077b02011-09-06 12:00:39 +0300314
George Cherianb1fd6cb2013-06-12 14:53:47 +0530315 reg = dwc3_omap_read_irq0_status(omap);
316
317 dwc3_omap_write_irq0_status(omap, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300318
Felipe Balbi72246da2011-08-19 18:10:58 +0300319 return IRQ_HANDLED;
320}
321
Kishon Vijay Abraham I94c6a432013-01-25 08:30:45 +0530322static int dwc3_omap_remove_core(struct device *dev, void *c)
323{
324 struct platform_device *pdev = to_platform_device(dev);
325
326 platform_device_unregister(pdev);
327
328 return 0;
329}
330
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200331static void dwc3_omap_enable_irqs(struct dwc3_omap *omap)
332{
333 u32 reg;
334
335 /* enable all IRQs */
336 reg = USBOTGSS_IRQO_COREIRQ_ST;
George Cherianb1fd6cb2013-06-12 14:53:47 +0530337 dwc3_omap_write_irq0_set(omap, reg);
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200338
George Cherianb1fd6cb2013-06-12 14:53:47 +0530339 reg = (USBOTGSS_IRQMISC_OEVT |
340 USBOTGSS_IRQMISC_DRVVBUS_RISE |
341 USBOTGSS_IRQMISC_CHRGVBUS_RISE |
342 USBOTGSS_IRQMISC_DISCHRGVBUS_RISE |
343 USBOTGSS_IRQMISC_IDPULLUP_RISE |
344 USBOTGSS_IRQMISC_DRVVBUS_FALL |
345 USBOTGSS_IRQMISC_CHRGVBUS_FALL |
346 USBOTGSS_IRQMISC_DISCHRGVBUS_FALL |
347 USBOTGSS_IRQMISC_IDPULLUP_FALL);
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200348
George Cherianb1fd6cb2013-06-12 14:53:47 +0530349 dwc3_omap_write_irqmisc_set(omap, reg);
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200350}
351
352static void dwc3_omap_disable_irqs(struct dwc3_omap *omap)
353{
354 /* disable all IRQs */
George Cherianb1fd6cb2013-06-12 14:53:47 +0530355 dwc3_omap_write_irqmisc_set(omap, 0x00);
356 dwc3_omap_write_irq0_set(omap, 0x00);
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200357}
358
Kishon Vijay Abraham Iddff14f2013-03-07 18:51:43 +0530359static u64 dwc3_omap_dma_mask = DMA_BIT_MASK(32);
360
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900361static int dwc3_omap_id_notifier(struct notifier_block *nb,
362 unsigned long event, void *ptr)
363{
364 struct dwc3_omap *omap = container_of(nb, struct dwc3_omap, id_nb);
365
366 if (event)
367 dwc3_omap_set_mailbox(omap, OMAP_DWC3_ID_GROUND);
368 else
369 dwc3_omap_set_mailbox(omap, OMAP_DWC3_ID_FLOAT);
370
371 return NOTIFY_DONE;
372}
373
374static int dwc3_omap_vbus_notifier(struct notifier_block *nb,
375 unsigned long event, void *ptr)
376{
377 struct dwc3_omap *omap = container_of(nb, struct dwc3_omap, vbus_nb);
378
379 if (event)
380 dwc3_omap_set_mailbox(omap, OMAP_DWC3_VBUS_VALID);
381 else
382 dwc3_omap_set_mailbox(omap, OMAP_DWC3_VBUS_OFF);
383
384 return NOTIFY_DONE;
385}
386
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500387static int dwc3_omap_probe(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +0300388{
Felipe Balbi45b3cd4a2012-01-25 11:07:03 +0200389 struct device_node *node = pdev->dev.of_node;
390
Felipe Balbi72246da2011-08-19 18:10:58 +0300391 struct dwc3_omap *omap;
392 struct resource *res;
Chanho Park802ca852012-02-15 18:27:55 +0900393 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900394 struct extcon_dev *edev;
395 struct regulator *vbus_reg = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300396
397 int ret = -ENOMEM;
398 int irq;
399
Kishon Vijay Abraham Ie36a0c82013-02-26 20:03:27 +0530400 int utmi_mode = 0;
George Cherianff7307b2013-06-12 14:53:46 +0530401 int x_major;
Kishon Vijay Abraham Ie36a0c82013-02-26 20:03:27 +0530402
Felipe Balbi72246da2011-08-19 18:10:58 +0300403 u32 reg;
404
405 void __iomem *base;
Felipe Balbi72246da2011-08-19 18:10:58 +0300406
Kishon Vijay Abraham I4495afc2013-02-26 20:03:28 +0530407 if (!node) {
408 dev_err(dev, "device node not found\n");
409 return -EINVAL;
410 }
411
Chanho Park802ca852012-02-15 18:27:55 +0900412 omap = devm_kzalloc(dev, sizeof(*omap), GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300413 if (!omap) {
Chanho Park802ca852012-02-15 18:27:55 +0900414 dev_err(dev, "not enough memory\n");
415 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300416 }
417
418 platform_set_drvdata(pdev, omap);
419
Kishon Vijay Abraham Ie36a0c82013-02-26 20:03:27 +0530420 irq = platform_get_irq(pdev, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300421 if (irq < 0) {
Chanho Park802ca852012-02-15 18:27:55 +0900422 dev_err(dev, "missing IRQ resource\n");
423 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300424 }
425
Kishon Vijay Abraham Ie36a0c82013-02-26 20:03:27 +0530426 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300427 if (!res) {
Chanho Park802ca852012-02-15 18:27:55 +0900428 dev_err(dev, "missing memory base resource\n");
429 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300430 }
431
Felipe Balbi8bbcd172013-07-12 15:33:35 +0300432 base = devm_ioremap_resource(dev, res);
433 if (IS_ERR(base))
434 return PTR_ERR(base);
Felipe Balbi72246da2011-08-19 18:10:58 +0300435
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900436 if (of_property_read_bool(node, "vbus-supply")) {
437 vbus_reg = devm_regulator_get(dev, "vbus");
438 if (IS_ERR(vbus_reg)) {
439 dev_err(dev, "vbus init failed\n");
440 return PTR_ERR(vbus_reg);
441 }
442 }
443
Chanho Park802ca852012-02-15 18:27:55 +0900444 omap->dev = dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300445 omap->irq = irq;
446 omap->base = base;
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900447 omap->vbus_reg = vbus_reg;
Kishon Vijay Abraham Iddff14f2013-03-07 18:51:43 +0530448 dev->dma_mask = &dwc3_omap_dma_mask;
Felipe Balbi72246da2011-08-19 18:10:58 +0300449
Kishon Vijay Abraham Iaf310e92013-01-25 08:30:47 +0530450 pm_runtime_enable(dev);
451 ret = pm_runtime_get_sync(dev);
452 if (ret < 0) {
453 dev_err(dev, "get_sync failed with err %d\n", ret);
Kishon Vijay Abraham I594daba2013-06-03 21:43:39 +0530454 goto err0;
Kishon Vijay Abraham Iaf310e92013-01-25 08:30:47 +0530455 }
456
George Cherianff7307b2013-06-12 14:53:46 +0530457 reg = dwc3_omap_readl(omap->base, USBOTGSS_REVISION);
458 omap->revision = reg;
459 x_major = USBOTGSS_REVISION_XMAJOR(reg);
460
George Cherianb1fd6cb2013-06-12 14:53:47 +0530461 /* Differentiate between OMAP5 and AM437x */
George Cherianff7307b2013-06-12 14:53:46 +0530462 switch (x_major) {
463 case USBOTGSS_REVISION_XMAJOR1:
464 case USBOTGSS_REVISION_XMAJOR2:
465 omap->irq_eoi_offset = 0;
466 omap->irq0_offset = 0;
467 omap->irqmisc_offset = 0;
468 omap->utmi_otg_offset = 0;
469 omap->debug_offset = 0;
470 break;
471 default:
472 /* Default to the latest revision */
473 omap->irq_eoi_offset = USBOTGSS_EOI_OFFSET;
474 omap->irq0_offset = USBOTGSS_IRQ0_OFFSET;
475 omap->irqmisc_offset = USBOTGSS_IRQMISC_OFFSET;
476 omap->utmi_otg_offset = USBOTGSS_UTMI_OTG_OFFSET;
477 omap->debug_offset = USBOTGSS_DEBUG_OFFSET;
478 break;
479 }
480
481 /* For OMAP5(ES2.0) and AM437x x_major is 2 even though there are
482 * changes in wrapper registers, Using dt compatible for aegis
483 */
484
485 if (of_device_is_compatible(node, "ti,am437x-dwc3")) {
486 omap->irq_eoi_offset = USBOTGSS_EOI_OFFSET;
487 omap->irq0_offset = USBOTGSS_IRQ0_OFFSET;
488 omap->irqmisc_offset = USBOTGSS_IRQMISC_OFFSET;
489 omap->utmi_otg_offset = USBOTGSS_UTMI_OTG_OFFSET;
490 omap->debug_offset = USBOTGSS_DEBUG_OFFSET;
491 }
492
George Cherianb1fd6cb2013-06-12 14:53:47 +0530493 reg = dwc3_omap_read_utmi_status(omap);
Felipe Balbi99624442011-09-01 22:26:25 +0300494
Kishon Vijay Abraham I4495afc2013-02-26 20:03:28 +0530495 of_property_read_u32(node, "utmi-mode", &utmi_mode);
Kishon Vijay Abraham Ie36a0c82013-02-26 20:03:27 +0530496
497 switch (utmi_mode) {
498 case DWC3_OMAP_UTMI_MODE_SW:
499 reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
500 break;
501 case DWC3_OMAP_UTMI_MODE_HW:
502 reg &= ~USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
503 break;
504 default:
505 dev_dbg(dev, "UNKNOWN utmi mode %d\n", utmi_mode);
Felipe Balbi99624442011-09-01 22:26:25 +0300506 }
507
George Cherianb1fd6cb2013-06-12 14:53:47 +0530508 dwc3_omap_write_utmi_status(omap, reg);
Felipe Balbi99624442011-09-01 22:26:25 +0300509
Felipe Balbi72246da2011-08-19 18:10:58 +0300510 /* check the DMA Status */
Ido Shayevitzab5e59d2012-04-24 14:18:38 +0300511 reg = dwc3_omap_readl(omap->base, USBOTGSS_SYSCONFIG);
Felipe Balbi72246da2011-08-19 18:10:58 +0300512 omap->dma_status = !!(reg & USBOTGSS_SYSCONFIG_DMADISABLE);
513
Chanho Park802ca852012-02-15 18:27:55 +0900514 ret = devm_request_irq(dev, omap->irq, dwc3_omap_interrupt, 0,
Felipe Balbidd17a6b2011-09-06 10:57:41 +0300515 "dwc3-omap", omap);
Felipe Balbi72246da2011-08-19 18:10:58 +0300516 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900517 dev_err(dev, "failed to request IRQ #%d --> %d\n",
Felipe Balbi72246da2011-08-19 18:10:58 +0300518 omap->irq, ret);
Kishon Vijay Abraham I594daba2013-06-03 21:43:39 +0530519 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300520 }
521
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200522 dwc3_omap_enable_irqs(omap);
Felipe Balbi72246da2011-08-19 18:10:58 +0300523
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900524 if (of_property_read_bool(node, "extcon")) {
525 edev = of_extcon_get_extcon_dev(dev, 0);
526 if (IS_ERR(edev)) {
527 dev_vdbg(dev, "couldn't get extcon device\n");
George Cheriane0a61042013-09-25 11:59:43 +0530528 ret = -EPROBE_DEFER;
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900529 goto err2;
530 }
531
532 omap->vbus_nb.notifier_call = dwc3_omap_vbus_notifier;
533 ret = extcon_register_interest(&omap->extcon_vbus_dev,
534 edev->name, "USB", &omap->vbus_nb);
535 if (ret < 0)
536 dev_vdbg(dev, "failed to register notifier for USB\n");
537 omap->id_nb.notifier_call = dwc3_omap_id_notifier;
538 ret = extcon_register_interest(&omap->extcon_id_dev, edev->name,
539 "USB-HOST", &omap->id_nb);
540 if (ret < 0)
541 dev_vdbg(dev,
542 "failed to register notifier for USB-HOST\n");
543
544 if (extcon_get_cable_state(edev, "USB") == true)
545 dwc3_omap_set_mailbox(omap, OMAP_DWC3_VBUS_VALID);
546 if (extcon_get_cable_state(edev, "USB-HOST") == true)
547 dwc3_omap_set_mailbox(omap, OMAP_DWC3_ID_GROUND);
548 }
549
Kishon Vijay Abraham I4495afc2013-02-26 20:03:28 +0530550 ret = of_platform_populate(node, NULL, NULL, dev);
551 if (ret) {
552 dev_err(&pdev->dev, "failed to create dwc3 core\n");
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900553 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +0300554 }
555
556 return 0;
Kishon Vijay Abraham I594daba2013-06-03 21:43:39 +0530557
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900558err3:
559 if (omap->extcon_vbus_dev.edev)
560 extcon_unregister_interest(&omap->extcon_vbus_dev);
561 if (omap->extcon_id_dev.edev)
562 extcon_unregister_interest(&omap->extcon_id_dev);
563
Kishon Vijay Abraham I594daba2013-06-03 21:43:39 +0530564err2:
565 dwc3_omap_disable_irqs(omap);
566
567err1:
568 pm_runtime_put_sync(dev);
569
570err0:
571 pm_runtime_disable(dev);
572
573 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300574}
575
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500576static int dwc3_omap_remove(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +0300577{
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200578 struct dwc3_omap *omap = platform_get_drvdata(pdev);
579
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900580 if (omap->extcon_vbus_dev.edev)
581 extcon_unregister_interest(&omap->extcon_vbus_dev);
582 if (omap->extcon_id_dev.edev)
583 extcon_unregister_interest(&omap->extcon_id_dev);
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200584 dwc3_omap_disable_irqs(omap);
Kishon Vijay Abraham Iaf310e92013-01-25 08:30:47 +0530585 pm_runtime_put_sync(&pdev->dev);
586 pm_runtime_disable(&pdev->dev);
Kishon Vijay Abraham I94c6a432013-01-25 08:30:45 +0530587 device_for_each_child(&pdev->dev, NULL, dwc3_omap_remove_core);
588
Felipe Balbi72246da2011-08-19 18:10:58 +0300589 return 0;
590}
591
Felipe Balbi2c2dc892013-02-11 10:31:15 +0200592static const struct of_device_id of_dwc3_match[] = {
Felipe Balbi72246da2011-08-19 18:10:58 +0300593 {
Kishon Vijay Abraham Ie36a0c82013-02-26 20:03:27 +0530594 .compatible = "ti,dwc3"
Felipe Balbi72246da2011-08-19 18:10:58 +0300595 },
George Cherianff7307b2013-06-12 14:53:46 +0530596 {
597 .compatible = "ti,am437x-dwc3"
598 },
Felipe Balbi72246da2011-08-19 18:10:58 +0300599 { },
600};
Felipe Balbi2c2dc892013-02-11 10:31:15 +0200601MODULE_DEVICE_TABLE(of, of_dwc3_match);
Felipe Balbi72246da2011-08-19 18:10:58 +0300602
Jingoo Han19fda7c2013-03-26 01:52:48 +0000603#ifdef CONFIG_PM_SLEEP
Felipe Balbif3e117f2013-02-11 11:12:02 +0200604static int dwc3_omap_prepare(struct device *dev)
605{
606 struct dwc3_omap *omap = dev_get_drvdata(dev);
607
608 dwc3_omap_disable_irqs(omap);
609
610 return 0;
611}
612
613static void dwc3_omap_complete(struct device *dev)
614{
615 struct dwc3_omap *omap = dev_get_drvdata(dev);
616
617 dwc3_omap_enable_irqs(omap);
618}
619
620static int dwc3_omap_suspend(struct device *dev)
621{
622 struct dwc3_omap *omap = dev_get_drvdata(dev);
623
George Cherianb1fd6cb2013-06-12 14:53:47 +0530624 omap->utmi_otg_status = dwc3_omap_read_utmi_status(omap);
Felipe Balbif3e117f2013-02-11 11:12:02 +0200625
626 return 0;
627}
628
629static int dwc3_omap_resume(struct device *dev)
630{
631 struct dwc3_omap *omap = dev_get_drvdata(dev);
632
George Cherianb1fd6cb2013-06-12 14:53:47 +0530633 dwc3_omap_write_utmi_status(omap, omap->utmi_otg_status);
Felipe Balbif3e117f2013-02-11 11:12:02 +0200634
635 pm_runtime_disable(dev);
636 pm_runtime_set_active(dev);
637 pm_runtime_enable(dev);
638
639 return 0;
640}
641
642static const struct dev_pm_ops dwc3_omap_dev_pm_ops = {
643 .prepare = dwc3_omap_prepare,
644 .complete = dwc3_omap_complete,
645
646 SET_SYSTEM_SLEEP_PM_OPS(dwc3_omap_suspend, dwc3_omap_resume)
647};
648
649#define DEV_PM_OPS (&dwc3_omap_dev_pm_ops)
650#else
651#define DEV_PM_OPS NULL
Jingoo Han19fda7c2013-03-26 01:52:48 +0000652#endif /* CONFIG_PM_SLEEP */
Felipe Balbif3e117f2013-02-11 11:12:02 +0200653
Felipe Balbi72246da2011-08-19 18:10:58 +0300654static struct platform_driver dwc3_omap_driver = {
655 .probe = dwc3_omap_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500656 .remove = dwc3_omap_remove,
Felipe Balbi72246da2011-08-19 18:10:58 +0300657 .driver = {
658 .name = "omap-dwc3",
Felipe Balbi2c2dc892013-02-11 10:31:15 +0200659 .of_match_table = of_dwc3_match,
Felipe Balbif3e117f2013-02-11 11:12:02 +0200660 .pm = DEV_PM_OPS,
Felipe Balbi72246da2011-08-19 18:10:58 +0300661 },
662};
663
Axel Lincc27c962011-11-27 20:16:27 +0800664module_platform_driver(dwc3_omap_driver);
665
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +0200666MODULE_ALIAS("platform:omap-dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +0300667MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
Felipe Balbi5945f782013-06-30 14:15:11 +0300668MODULE_LICENSE("GPL v2");
Felipe Balbi72246da2011-08-19 18:10:58 +0300669MODULE_DESCRIPTION("DesignWare USB3 OMAP Glue Layer");