blob: 1160ff41bed4972fb2c11ea576ddecfc9a9f2548 [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>
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +090032#include <linux/regulator/consumer.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030033
Felipe Balbia418cc42012-07-19 13:56:07 +030034#include <linux/usb/otg.h>
Felipe Balbia418cc42012-07-19 13:56:07 +030035
Felipe Balbi72246da2011-08-19 18:10:58 +030036/*
37 * All these registers belong to OMAP's Wrapper around the
38 * DesignWare USB3 Core.
39 */
40
41#define USBOTGSS_REVISION 0x0000
42#define USBOTGSS_SYSCONFIG 0x0010
43#define USBOTGSS_IRQ_EOI 0x0020
George Cherianff7307b2013-06-12 14:53:46 +053044#define USBOTGSS_EOI_OFFSET 0x0008
Felipe Balbi72246da2011-08-19 18:10:58 +030045#define USBOTGSS_IRQSTATUS_RAW_0 0x0024
46#define USBOTGSS_IRQSTATUS_0 0x0028
47#define USBOTGSS_IRQENABLE_SET_0 0x002c
48#define USBOTGSS_IRQENABLE_CLR_0 0x0030
George Cherianff7307b2013-06-12 14:53:46 +053049#define USBOTGSS_IRQ0_OFFSET 0x0004
George Cherianb1fd6cb2013-06-12 14:53:47 +053050#define USBOTGSS_IRQSTATUS_RAW_1 0x0030
51#define USBOTGSS_IRQSTATUS_1 0x0034
52#define USBOTGSS_IRQENABLE_SET_1 0x0038
53#define USBOTGSS_IRQENABLE_CLR_1 0x003c
54#define USBOTGSS_IRQSTATUS_RAW_2 0x0040
55#define USBOTGSS_IRQSTATUS_2 0x0044
56#define USBOTGSS_IRQENABLE_SET_2 0x0048
57#define USBOTGSS_IRQENABLE_CLR_2 0x004c
58#define USBOTGSS_IRQSTATUS_RAW_3 0x0050
59#define USBOTGSS_IRQSTATUS_3 0x0054
60#define USBOTGSS_IRQENABLE_SET_3 0x0058
61#define USBOTGSS_IRQENABLE_CLR_3 0x005c
George Cherianff7307b2013-06-12 14:53:46 +053062#define USBOTGSS_IRQSTATUS_EOI_MISC 0x0030
63#define USBOTGSS_IRQSTATUS_RAW_MISC 0x0034
64#define USBOTGSS_IRQSTATUS_MISC 0x0038
65#define USBOTGSS_IRQENABLE_SET_MISC 0x003c
66#define USBOTGSS_IRQENABLE_CLR_MISC 0x0040
67#define USBOTGSS_IRQMISC_OFFSET 0x03fc
Felipe Balbi72246da2011-08-19 18:10:58 +030068#define USBOTGSS_UTMI_OTG_CTRL 0x0080
69#define USBOTGSS_UTMI_OTG_STATUS 0x0084
George Cherianff7307b2013-06-12 14:53:46 +053070#define USBOTGSS_UTMI_OTG_OFFSET 0x0480
71#define USBOTGSS_TXFIFO_DEPTH 0x0508
72#define USBOTGSS_RXFIFO_DEPTH 0x050c
Felipe Balbi72246da2011-08-19 18:10:58 +030073#define USBOTGSS_MMRAM_OFFSET 0x0100
74#define USBOTGSS_FLADJ 0x0104
75#define USBOTGSS_DEBUG_CFG 0x0108
76#define USBOTGSS_DEBUG_DATA 0x010c
George Cherianff7307b2013-06-12 14:53:46 +053077#define USBOTGSS_DEV_EBC_EN 0x0110
78#define USBOTGSS_DEBUG_OFFSET 0x0600
Felipe Balbi72246da2011-08-19 18:10:58 +030079
George Cherianff7307b2013-06-12 14:53:46 +053080/* REVISION REGISTER */
81#define USBOTGSS_REVISION_XMAJOR(reg) ((reg >> 8) & 0x7)
82#define USBOTGSS_REVISION_XMAJOR1 1
83#define USBOTGSS_REVISION_XMAJOR2 2
Felipe Balbi72246da2011-08-19 18:10:58 +030084/* SYSCONFIG REGISTER */
85#define USBOTGSS_SYSCONFIG_DMADISABLE (1 << 16)
Felipe Balbi4b5faa7a2011-09-06 10:56:51 +030086
Felipe Balbi72246da2011-08-19 18:10:58 +030087/* IRQ_EOI REGISTER */
88#define USBOTGSS_IRQ_EOI_LINE_NUMBER (1 << 0)
89
90/* IRQS0 BITS */
91#define USBOTGSS_IRQO_COREIRQ_ST (1 << 0)
92
George Cherianb1fd6cb2013-06-12 14:53:47 +053093/* IRQMISC BITS */
94#define USBOTGSS_IRQMISC_DMADISABLECLR (1 << 17)
95#define USBOTGSS_IRQMISC_OEVT (1 << 16)
96#define USBOTGSS_IRQMISC_DRVVBUS_RISE (1 << 13)
97#define USBOTGSS_IRQMISC_CHRGVBUS_RISE (1 << 12)
98#define USBOTGSS_IRQMISC_DISCHRGVBUS_RISE (1 << 11)
99#define USBOTGSS_IRQMISC_IDPULLUP_RISE (1 << 8)
100#define USBOTGSS_IRQMISC_DRVVBUS_FALL (1 << 5)
101#define USBOTGSS_IRQMISC_CHRGVBUS_FALL (1 << 4)
102#define USBOTGSS_IRQMISC_DISCHRGVBUS_FALL (1 << 3)
103#define USBOTGSS_IRQMISC_IDPULLUP_FALL (1 << 0)
Felipe Balbi72246da2011-08-19 18:10:58 +0300104
105/* UTMI_OTG_CTRL REGISTER */
106#define USBOTGSS_UTMI_OTG_CTRL_DRVVBUS (1 << 5)
107#define USBOTGSS_UTMI_OTG_CTRL_CHRGVBUS (1 << 4)
108#define USBOTGSS_UTMI_OTG_CTRL_DISCHRGVBUS (1 << 3)
109#define USBOTGSS_UTMI_OTG_CTRL_IDPULLUP (1 << 0)
110
111/* UTMI_OTG_STATUS REGISTER */
112#define USBOTGSS_UTMI_OTG_STATUS_SW_MODE (1 << 31)
113#define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT (1 << 9)
114#define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE (1 << 8)
115#define USBOTGSS_UTMI_OTG_STATUS_IDDIG (1 << 4)
116#define USBOTGSS_UTMI_OTG_STATUS_SESSEND (1 << 3)
117#define USBOTGSS_UTMI_OTG_STATUS_SESSVALID (1 << 2)
118#define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID (1 << 1)
119
120struct dwc3_omap {
Felipe Balbi72246da2011-08-19 18:10:58 +0300121 struct device *dev;
122
123 int irq;
124 void __iomem *base;
125
Felipe Balbif3e117f2013-02-11 11:12:02 +0200126 u32 utmi_otg_status;
George Cherian1e2a0642013-06-12 14:53:45 +0530127 u32 utmi_otg_offset;
128 u32 irqmisc_offset;
129 u32 irq_eoi_offset;
130 u32 debug_offset;
131 u32 irq0_offset;
132 u32 revision;
Felipe Balbif3e117f2013-02-11 11:12:02 +0200133
Felipe Balbi72246da2011-08-19 18:10:58 +0300134 u32 dma_status:1;
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900135
136 struct extcon_specific_cable_nb extcon_vbus_dev;
137 struct extcon_specific_cable_nb extcon_id_dev;
138 struct notifier_block vbus_nb;
139 struct notifier_block id_nb;
140
141 struct regulator *vbus_reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300142};
143
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900144enum omap_dwc3_vbus_id_status {
145 OMAP_DWC3_ID_FLOAT,
146 OMAP_DWC3_ID_GROUND,
147 OMAP_DWC3_VBUS_OFF,
148 OMAP_DWC3_VBUS_VALID,
149};
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530150
Ido Shayevitzab5e59d2012-04-24 14:18:38 +0300151static inline u32 dwc3_omap_readl(void __iomem *base, u32 offset)
152{
153 return readl(base + offset);
154}
155
156static inline void dwc3_omap_writel(void __iomem *base, u32 offset, u32 value)
157{
158 writel(value, base + offset);
159}
160
George Cherianb1fd6cb2013-06-12 14:53:47 +0530161static u32 dwc3_omap_read_utmi_status(struct dwc3_omap *omap)
162{
163 return dwc3_omap_readl(omap->base, USBOTGSS_UTMI_OTG_STATUS +
164 omap->utmi_otg_offset);
165}
166
167static void dwc3_omap_write_utmi_status(struct dwc3_omap *omap, u32 value)
168{
169 dwc3_omap_writel(omap->base, USBOTGSS_UTMI_OTG_STATUS +
170 omap->utmi_otg_offset, value);
171
172}
173
174static u32 dwc3_omap_read_irq0_status(struct dwc3_omap *omap)
175{
176 return dwc3_omap_readl(omap->base, USBOTGSS_IRQSTATUS_0 -
177 omap->irq0_offset);
178}
179
180static void dwc3_omap_write_irq0_status(struct dwc3_omap *omap, u32 value)
181{
182 dwc3_omap_writel(omap->base, USBOTGSS_IRQSTATUS_0 -
183 omap->irq0_offset, value);
184
185}
186
187static u32 dwc3_omap_read_irqmisc_status(struct dwc3_omap *omap)
188{
189 return dwc3_omap_readl(omap->base, USBOTGSS_IRQSTATUS_MISC +
190 omap->irqmisc_offset);
191}
192
193static void dwc3_omap_write_irqmisc_status(struct dwc3_omap *omap, u32 value)
194{
195 dwc3_omap_writel(omap->base, USBOTGSS_IRQSTATUS_MISC +
196 omap->irqmisc_offset, value);
197
198}
199
200static void dwc3_omap_write_irqmisc_set(struct dwc3_omap *omap, u32 value)
201{
202 dwc3_omap_writel(omap->base, USBOTGSS_IRQENABLE_SET_MISC +
203 omap->irqmisc_offset, value);
204
205}
206
207static void dwc3_omap_write_irq0_set(struct dwc3_omap *omap, u32 value)
208{
209 dwc3_omap_writel(omap->base, USBOTGSS_IRQENABLE_SET_0 -
210 omap->irq0_offset, value);
211}
212
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900213static void dwc3_omap_set_mailbox(struct dwc3_omap *omap,
214 enum omap_dwc3_vbus_id_status status)
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530215{
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900216 int ret;
217 u32 val;
Kishon Vijay Abraham I2ba79432013-03-07 18:51:44 +0530218
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530219 switch (status) {
220 case OMAP_DWC3_ID_GROUND:
221 dev_dbg(omap->dev, "ID GND\n");
222
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900223 if (omap->vbus_reg) {
224 ret = regulator_enable(omap->vbus_reg);
225 if (ret) {
226 dev_dbg(omap->dev, "regulator enable failed\n");
227 return;
228 }
229 }
230
George Cherianb1fd6cb2013-06-12 14:53:47 +0530231 val = dwc3_omap_read_utmi_status(omap);
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530232 val &= ~(USBOTGSS_UTMI_OTG_STATUS_IDDIG
233 | USBOTGSS_UTMI_OTG_STATUS_VBUSVALID
234 | USBOTGSS_UTMI_OTG_STATUS_SESSEND);
235 val |= USBOTGSS_UTMI_OTG_STATUS_SESSVALID
236 | USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT;
George Cherianb1fd6cb2013-06-12 14:53:47 +0530237 dwc3_omap_write_utmi_status(omap, val);
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530238 break;
239
240 case OMAP_DWC3_VBUS_VALID:
241 dev_dbg(omap->dev, "VBUS Connect\n");
242
George Cherianb1fd6cb2013-06-12 14:53:47 +0530243 val = dwc3_omap_read_utmi_status(omap);
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530244 val &= ~USBOTGSS_UTMI_OTG_STATUS_SESSEND;
245 val |= USBOTGSS_UTMI_OTG_STATUS_IDDIG
246 | USBOTGSS_UTMI_OTG_STATUS_VBUSVALID
247 | USBOTGSS_UTMI_OTG_STATUS_SESSVALID
248 | USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT;
George Cherianb1fd6cb2013-06-12 14:53:47 +0530249 dwc3_omap_write_utmi_status(omap, val);
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530250 break;
251
252 case OMAP_DWC3_ID_FLOAT:
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900253 if (omap->vbus_reg)
254 regulator_disable(omap->vbus_reg);
255
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530256 case OMAP_DWC3_VBUS_OFF:
257 dev_dbg(omap->dev, "VBUS Disconnect\n");
258
George Cherianb1fd6cb2013-06-12 14:53:47 +0530259 val = dwc3_omap_read_utmi_status(omap);
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530260 val &= ~(USBOTGSS_UTMI_OTG_STATUS_SESSVALID
261 | USBOTGSS_UTMI_OTG_STATUS_VBUSVALID
262 | USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT);
263 val |= USBOTGSS_UTMI_OTG_STATUS_SESSEND
264 | USBOTGSS_UTMI_OTG_STATUS_IDDIG;
George Cherianb1fd6cb2013-06-12 14:53:47 +0530265 dwc3_omap_write_utmi_status(omap, val);
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530266 break;
267
268 default:
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900269 dev_dbg(omap->dev, "invalid state\n");
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530270 }
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530271}
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530272
Felipe Balbi72246da2011-08-19 18:10:58 +0300273static irqreturn_t dwc3_omap_interrupt(int irq, void *_omap)
274{
275 struct dwc3_omap *omap = _omap;
276 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300277
George Cherianb1fd6cb2013-06-12 14:53:47 +0530278 reg = dwc3_omap_read_irqmisc_status(omap);
Felipe Balbi72246da2011-08-19 18:10:58 +0300279
George Cherianb1fd6cb2013-06-12 14:53:47 +0530280 if (reg & USBOTGSS_IRQMISC_DMADISABLECLR) {
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300281 dev_dbg(omap->dev, "DMA Disable was Cleared\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300282 omap->dma_status = false;
283 }
284
George Cherianb1fd6cb2013-06-12 14:53:47 +0530285 if (reg & USBOTGSS_IRQMISC_OEVT)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300286 dev_dbg(omap->dev, "OTG Event\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300287
George Cherianb1fd6cb2013-06-12 14:53:47 +0530288 if (reg & USBOTGSS_IRQMISC_DRVVBUS_RISE)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300289 dev_dbg(omap->dev, "DRVVBUS Rise\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300290
George Cherianb1fd6cb2013-06-12 14:53:47 +0530291 if (reg & USBOTGSS_IRQMISC_CHRGVBUS_RISE)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300292 dev_dbg(omap->dev, "CHRGVBUS Rise\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300293
George Cherianb1fd6cb2013-06-12 14:53:47 +0530294 if (reg & USBOTGSS_IRQMISC_DISCHRGVBUS_RISE)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300295 dev_dbg(omap->dev, "DISCHRGVBUS Rise\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300296
George Cherianb1fd6cb2013-06-12 14:53:47 +0530297 if (reg & USBOTGSS_IRQMISC_IDPULLUP_RISE)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300298 dev_dbg(omap->dev, "IDPULLUP Rise\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300299
George Cherianb1fd6cb2013-06-12 14:53:47 +0530300 if (reg & USBOTGSS_IRQMISC_DRVVBUS_FALL)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300301 dev_dbg(omap->dev, "DRVVBUS Fall\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300302
George Cherianb1fd6cb2013-06-12 14:53:47 +0530303 if (reg & USBOTGSS_IRQMISC_CHRGVBUS_FALL)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300304 dev_dbg(omap->dev, "CHRGVBUS Fall\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300305
George Cherianb1fd6cb2013-06-12 14:53:47 +0530306 if (reg & USBOTGSS_IRQMISC_DISCHRGVBUS_FALL)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300307 dev_dbg(omap->dev, "DISCHRGVBUS Fall\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300308
George Cherianb1fd6cb2013-06-12 14:53:47 +0530309 if (reg & USBOTGSS_IRQMISC_IDPULLUP_FALL)
Felipe Balbiccba3bc2011-09-01 14:46:16 +0300310 dev_dbg(omap->dev, "IDPULLUP Fall\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300311
George Cherianb1fd6cb2013-06-12 14:53:47 +0530312 dwc3_omap_write_irqmisc_status(omap, reg);
Felipe Balbi42077b02011-09-06 12:00:39 +0300313
George Cherianb1fd6cb2013-06-12 14:53:47 +0530314 reg = dwc3_omap_read_irq0_status(omap);
315
316 dwc3_omap_write_irq0_status(omap, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300317
Felipe Balbi72246da2011-08-19 18:10:58 +0300318 return IRQ_HANDLED;
319}
320
Kishon Vijay Abraham I94c6a432013-01-25 08:30:45 +0530321static int dwc3_omap_remove_core(struct device *dev, void *c)
322{
323 struct platform_device *pdev = to_platform_device(dev);
324
325 platform_device_unregister(pdev);
326
327 return 0;
328}
329
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200330static void dwc3_omap_enable_irqs(struct dwc3_omap *omap)
331{
332 u32 reg;
333
334 /* enable all IRQs */
335 reg = USBOTGSS_IRQO_COREIRQ_ST;
George Cherianb1fd6cb2013-06-12 14:53:47 +0530336 dwc3_omap_write_irq0_set(omap, reg);
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200337
George Cherianb1fd6cb2013-06-12 14:53:47 +0530338 reg = (USBOTGSS_IRQMISC_OEVT |
339 USBOTGSS_IRQMISC_DRVVBUS_RISE |
340 USBOTGSS_IRQMISC_CHRGVBUS_RISE |
341 USBOTGSS_IRQMISC_DISCHRGVBUS_RISE |
342 USBOTGSS_IRQMISC_IDPULLUP_RISE |
343 USBOTGSS_IRQMISC_DRVVBUS_FALL |
344 USBOTGSS_IRQMISC_CHRGVBUS_FALL |
345 USBOTGSS_IRQMISC_DISCHRGVBUS_FALL |
346 USBOTGSS_IRQMISC_IDPULLUP_FALL);
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200347
George Cherianb1fd6cb2013-06-12 14:53:47 +0530348 dwc3_omap_write_irqmisc_set(omap, reg);
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200349}
350
351static void dwc3_omap_disable_irqs(struct dwc3_omap *omap)
352{
353 /* disable all IRQs */
George Cherianb1fd6cb2013-06-12 14:53:47 +0530354 dwc3_omap_write_irqmisc_set(omap, 0x00);
355 dwc3_omap_write_irq0_set(omap, 0x00);
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200356}
357
Kishon Vijay Abraham Iddff14f2013-03-07 18:51:43 +0530358static u64 dwc3_omap_dma_mask = DMA_BIT_MASK(32);
359
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900360static int dwc3_omap_id_notifier(struct notifier_block *nb,
361 unsigned long event, void *ptr)
362{
363 struct dwc3_omap *omap = container_of(nb, struct dwc3_omap, id_nb);
364
365 if (event)
366 dwc3_omap_set_mailbox(omap, OMAP_DWC3_ID_GROUND);
367 else
368 dwc3_omap_set_mailbox(omap, OMAP_DWC3_ID_FLOAT);
369
370 return NOTIFY_DONE;
371}
372
373static int dwc3_omap_vbus_notifier(struct notifier_block *nb,
374 unsigned long event, void *ptr)
375{
376 struct dwc3_omap *omap = container_of(nb, struct dwc3_omap, vbus_nb);
377
378 if (event)
379 dwc3_omap_set_mailbox(omap, OMAP_DWC3_VBUS_VALID);
380 else
381 dwc3_omap_set_mailbox(omap, OMAP_DWC3_VBUS_OFF);
382
383 return NOTIFY_DONE;
384}
385
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500386static int dwc3_omap_probe(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +0300387{
Felipe Balbi45b3cd4a2012-01-25 11:07:03 +0200388 struct device_node *node = pdev->dev.of_node;
389
Felipe Balbi72246da2011-08-19 18:10:58 +0300390 struct dwc3_omap *omap;
391 struct resource *res;
Chanho Park802ca852012-02-15 18:27:55 +0900392 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900393 struct extcon_dev *edev;
394 struct regulator *vbus_reg = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300395
396 int ret = -ENOMEM;
397 int irq;
398
Kishon Vijay Abraham Ie36a0c82013-02-26 20:03:27 +0530399 int utmi_mode = 0;
George Cherianff7307b2013-06-12 14:53:46 +0530400 int x_major;
Kishon Vijay Abraham Ie36a0c82013-02-26 20:03:27 +0530401
Felipe Balbi72246da2011-08-19 18:10:58 +0300402 u32 reg;
403
404 void __iomem *base;
Felipe Balbi72246da2011-08-19 18:10:58 +0300405
Kishon Vijay Abraham I4495afc2013-02-26 20:03:28 +0530406 if (!node) {
407 dev_err(dev, "device node not found\n");
408 return -EINVAL;
409 }
410
Chanho Park802ca852012-02-15 18:27:55 +0900411 omap = devm_kzalloc(dev, sizeof(*omap), GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300412 if (!omap) {
Chanho Park802ca852012-02-15 18:27:55 +0900413 dev_err(dev, "not enough memory\n");
414 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300415 }
416
417 platform_set_drvdata(pdev, omap);
418
Kishon Vijay Abraham Ie36a0c82013-02-26 20:03:27 +0530419 irq = platform_get_irq(pdev, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300420 if (irq < 0) {
Chanho Park802ca852012-02-15 18:27:55 +0900421 dev_err(dev, "missing IRQ resource\n");
422 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300423 }
424
Kishon Vijay Abraham Ie36a0c82013-02-26 20:03:27 +0530425 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Felipe Balbi8bbcd172013-07-12 15:33:35 +0300426 base = devm_ioremap_resource(dev, res);
427 if (IS_ERR(base))
428 return PTR_ERR(base);
Felipe Balbi72246da2011-08-19 18:10:58 +0300429
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900430 if (of_property_read_bool(node, "vbus-supply")) {
431 vbus_reg = devm_regulator_get(dev, "vbus");
432 if (IS_ERR(vbus_reg)) {
433 dev_err(dev, "vbus init failed\n");
434 return PTR_ERR(vbus_reg);
435 }
436 }
437
Chanho Park802ca852012-02-15 18:27:55 +0900438 omap->dev = dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300439 omap->irq = irq;
440 omap->base = base;
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900441 omap->vbus_reg = vbus_reg;
Kishon Vijay Abraham Iddff14f2013-03-07 18:51:43 +0530442 dev->dma_mask = &dwc3_omap_dma_mask;
Felipe Balbi72246da2011-08-19 18:10:58 +0300443
Kishon Vijay Abraham Iaf310e92013-01-25 08:30:47 +0530444 pm_runtime_enable(dev);
445 ret = pm_runtime_get_sync(dev);
446 if (ret < 0) {
447 dev_err(dev, "get_sync failed with err %d\n", ret);
Kishon Vijay Abraham I594daba2013-06-03 21:43:39 +0530448 goto err0;
Kishon Vijay Abraham Iaf310e92013-01-25 08:30:47 +0530449 }
450
George Cherianff7307b2013-06-12 14:53:46 +0530451 reg = dwc3_omap_readl(omap->base, USBOTGSS_REVISION);
452 omap->revision = reg;
453 x_major = USBOTGSS_REVISION_XMAJOR(reg);
454
George Cherianb1fd6cb2013-06-12 14:53:47 +0530455 /* Differentiate between OMAP5 and AM437x */
George Cherianff7307b2013-06-12 14:53:46 +0530456 switch (x_major) {
457 case USBOTGSS_REVISION_XMAJOR1:
458 case USBOTGSS_REVISION_XMAJOR2:
459 omap->irq_eoi_offset = 0;
460 omap->irq0_offset = 0;
461 omap->irqmisc_offset = 0;
462 omap->utmi_otg_offset = 0;
463 omap->debug_offset = 0;
464 break;
465 default:
466 /* Default to the latest revision */
467 omap->irq_eoi_offset = USBOTGSS_EOI_OFFSET;
468 omap->irq0_offset = USBOTGSS_IRQ0_OFFSET;
469 omap->irqmisc_offset = USBOTGSS_IRQMISC_OFFSET;
470 omap->utmi_otg_offset = USBOTGSS_UTMI_OTG_OFFSET;
471 omap->debug_offset = USBOTGSS_DEBUG_OFFSET;
472 break;
473 }
474
475 /* For OMAP5(ES2.0) and AM437x x_major is 2 even though there are
476 * changes in wrapper registers, Using dt compatible for aegis
477 */
478
479 if (of_device_is_compatible(node, "ti,am437x-dwc3")) {
480 omap->irq_eoi_offset = USBOTGSS_EOI_OFFSET;
481 omap->irq0_offset = USBOTGSS_IRQ0_OFFSET;
482 omap->irqmisc_offset = USBOTGSS_IRQMISC_OFFSET;
483 omap->utmi_otg_offset = USBOTGSS_UTMI_OTG_OFFSET;
484 omap->debug_offset = USBOTGSS_DEBUG_OFFSET;
485 }
486
George Cherianb1fd6cb2013-06-12 14:53:47 +0530487 reg = dwc3_omap_read_utmi_status(omap);
Felipe Balbi99624442011-09-01 22:26:25 +0300488
Kishon Vijay Abraham I4495afc2013-02-26 20:03:28 +0530489 of_property_read_u32(node, "utmi-mode", &utmi_mode);
Kishon Vijay Abraham Ie36a0c82013-02-26 20:03:27 +0530490
491 switch (utmi_mode) {
492 case DWC3_OMAP_UTMI_MODE_SW:
493 reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
494 break;
495 case DWC3_OMAP_UTMI_MODE_HW:
496 reg &= ~USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
497 break;
498 default:
499 dev_dbg(dev, "UNKNOWN utmi mode %d\n", utmi_mode);
Felipe Balbi99624442011-09-01 22:26:25 +0300500 }
501
George Cherianb1fd6cb2013-06-12 14:53:47 +0530502 dwc3_omap_write_utmi_status(omap, reg);
Felipe Balbi99624442011-09-01 22:26:25 +0300503
Felipe Balbi72246da2011-08-19 18:10:58 +0300504 /* check the DMA Status */
Ido Shayevitzab5e59d2012-04-24 14:18:38 +0300505 reg = dwc3_omap_readl(omap->base, USBOTGSS_SYSCONFIG);
Felipe Balbi72246da2011-08-19 18:10:58 +0300506 omap->dma_status = !!(reg & USBOTGSS_SYSCONFIG_DMADISABLE);
507
Chanho Park802ca852012-02-15 18:27:55 +0900508 ret = devm_request_irq(dev, omap->irq, dwc3_omap_interrupt, 0,
Felipe Balbidd17a6b2011-09-06 10:57:41 +0300509 "dwc3-omap", omap);
Felipe Balbi72246da2011-08-19 18:10:58 +0300510 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900511 dev_err(dev, "failed to request IRQ #%d --> %d\n",
Felipe Balbi72246da2011-08-19 18:10:58 +0300512 omap->irq, ret);
Kishon Vijay Abraham I594daba2013-06-03 21:43:39 +0530513 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300514 }
515
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200516 dwc3_omap_enable_irqs(omap);
Felipe Balbi72246da2011-08-19 18:10:58 +0300517
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900518 if (of_property_read_bool(node, "extcon")) {
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +0900519 edev = extcon_get_edev_by_phandle(dev, 0);
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900520 if (IS_ERR(edev)) {
521 dev_vdbg(dev, "couldn't get extcon device\n");
George Cheriane0a61042013-09-25 11:59:43 +0530522 ret = -EPROBE_DEFER;
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900523 goto err2;
524 }
525
526 omap->vbus_nb.notifier_call = dwc3_omap_vbus_notifier;
527 ret = extcon_register_interest(&omap->extcon_vbus_dev,
528 edev->name, "USB", &omap->vbus_nb);
529 if (ret < 0)
530 dev_vdbg(dev, "failed to register notifier for USB\n");
531 omap->id_nb.notifier_call = dwc3_omap_id_notifier;
532 ret = extcon_register_interest(&omap->extcon_id_dev, edev->name,
533 "USB-HOST", &omap->id_nb);
534 if (ret < 0)
535 dev_vdbg(dev,
536 "failed to register notifier for USB-HOST\n");
537
538 if (extcon_get_cable_state(edev, "USB") == true)
539 dwc3_omap_set_mailbox(omap, OMAP_DWC3_VBUS_VALID);
540 if (extcon_get_cable_state(edev, "USB-HOST") == true)
541 dwc3_omap_set_mailbox(omap, OMAP_DWC3_ID_GROUND);
542 }
543
Kishon Vijay Abraham I4495afc2013-02-26 20:03:28 +0530544 ret = of_platform_populate(node, NULL, NULL, dev);
545 if (ret) {
546 dev_err(&pdev->dev, "failed to create dwc3 core\n");
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900547 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +0300548 }
549
550 return 0;
Kishon Vijay Abraham I594daba2013-06-03 21:43:39 +0530551
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900552err3:
553 if (omap->extcon_vbus_dev.edev)
554 extcon_unregister_interest(&omap->extcon_vbus_dev);
555 if (omap->extcon_id_dev.edev)
556 extcon_unregister_interest(&omap->extcon_id_dev);
557
Kishon Vijay Abraham I594daba2013-06-03 21:43:39 +0530558err2:
559 dwc3_omap_disable_irqs(omap);
560
561err1:
562 pm_runtime_put_sync(dev);
563
564err0:
565 pm_runtime_disable(dev);
566
567 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300568}
569
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500570static int dwc3_omap_remove(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +0300571{
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200572 struct dwc3_omap *omap = platform_get_drvdata(pdev);
573
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900574 if (omap->extcon_vbus_dev.edev)
575 extcon_unregister_interest(&omap->extcon_vbus_dev);
576 if (omap->extcon_id_dev.edev)
577 extcon_unregister_interest(&omap->extcon_id_dev);
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200578 dwc3_omap_disable_irqs(omap);
Kishon Vijay Abraham Iaf310e92013-01-25 08:30:47 +0530579 pm_runtime_put_sync(&pdev->dev);
580 pm_runtime_disable(&pdev->dev);
Kishon Vijay Abraham I94c6a432013-01-25 08:30:45 +0530581 device_for_each_child(&pdev->dev, NULL, dwc3_omap_remove_core);
582
Felipe Balbi72246da2011-08-19 18:10:58 +0300583 return 0;
584}
585
Felipe Balbi2c2dc892013-02-11 10:31:15 +0200586static const struct of_device_id of_dwc3_match[] = {
Felipe Balbi72246da2011-08-19 18:10:58 +0300587 {
Kishon Vijay Abraham Ie36a0c82013-02-26 20:03:27 +0530588 .compatible = "ti,dwc3"
Felipe Balbi72246da2011-08-19 18:10:58 +0300589 },
George Cherianff7307b2013-06-12 14:53:46 +0530590 {
591 .compatible = "ti,am437x-dwc3"
592 },
Felipe Balbi72246da2011-08-19 18:10:58 +0300593 { },
594};
Felipe Balbi2c2dc892013-02-11 10:31:15 +0200595MODULE_DEVICE_TABLE(of, of_dwc3_match);
Felipe Balbi72246da2011-08-19 18:10:58 +0300596
Jingoo Han19fda7c2013-03-26 01:52:48 +0000597#ifdef CONFIG_PM_SLEEP
Felipe Balbif3e117f2013-02-11 11:12:02 +0200598static int dwc3_omap_prepare(struct device *dev)
599{
600 struct dwc3_omap *omap = dev_get_drvdata(dev);
601
602 dwc3_omap_disable_irqs(omap);
603
604 return 0;
605}
606
607static void dwc3_omap_complete(struct device *dev)
608{
609 struct dwc3_omap *omap = dev_get_drvdata(dev);
610
611 dwc3_omap_enable_irqs(omap);
612}
613
614static int dwc3_omap_suspend(struct device *dev)
615{
616 struct dwc3_omap *omap = dev_get_drvdata(dev);
617
George Cherianb1fd6cb2013-06-12 14:53:47 +0530618 omap->utmi_otg_status = dwc3_omap_read_utmi_status(omap);
Felipe Balbif3e117f2013-02-11 11:12:02 +0200619
620 return 0;
621}
622
623static int dwc3_omap_resume(struct device *dev)
624{
625 struct dwc3_omap *omap = dev_get_drvdata(dev);
626
George Cherianb1fd6cb2013-06-12 14:53:47 +0530627 dwc3_omap_write_utmi_status(omap, omap->utmi_otg_status);
Felipe Balbif3e117f2013-02-11 11:12:02 +0200628
629 pm_runtime_disable(dev);
630 pm_runtime_set_active(dev);
631 pm_runtime_enable(dev);
632
633 return 0;
634}
635
636static const struct dev_pm_ops dwc3_omap_dev_pm_ops = {
637 .prepare = dwc3_omap_prepare,
638 .complete = dwc3_omap_complete,
639
640 SET_SYSTEM_SLEEP_PM_OPS(dwc3_omap_suspend, dwc3_omap_resume)
641};
642
643#define DEV_PM_OPS (&dwc3_omap_dev_pm_ops)
644#else
645#define DEV_PM_OPS NULL
Jingoo Han19fda7c2013-03-26 01:52:48 +0000646#endif /* CONFIG_PM_SLEEP */
Felipe Balbif3e117f2013-02-11 11:12:02 +0200647
Felipe Balbi72246da2011-08-19 18:10:58 +0300648static struct platform_driver dwc3_omap_driver = {
649 .probe = dwc3_omap_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500650 .remove = dwc3_omap_remove,
Felipe Balbi72246da2011-08-19 18:10:58 +0300651 .driver = {
652 .name = "omap-dwc3",
Felipe Balbi2c2dc892013-02-11 10:31:15 +0200653 .of_match_table = of_dwc3_match,
Felipe Balbif3e117f2013-02-11 11:12:02 +0200654 .pm = DEV_PM_OPS,
Felipe Balbi72246da2011-08-19 18:10:58 +0300655 },
656};
657
Axel Lincc27c962011-11-27 20:16:27 +0800658module_platform_driver(dwc3_omap_driver);
659
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +0200660MODULE_ALIAS("platform:omap-dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +0300661MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
Felipe Balbi5945f782013-06-30 14:15:11 +0300662MODULE_LICENSE("GPL v2");
Felipe Balbi72246da2011-08-19 18:10:58 +0300663MODULE_DESCRIPTION("DesignWare USB3 OMAP Glue Layer");