blob: 29e80cc9b634aab72f59e73c71667dfef9f86a45 [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
Bin Liu22832192015-03-24 15:08:49 -050068#define USBOTGSS_UTMI_OTG_STATUS 0x0080
69#define USBOTGSS_UTMI_OTG_CTRL 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
80/* SYSCONFIG REGISTER */
81#define USBOTGSS_SYSCONFIG_DMADISABLE (1 << 16)
Felipe Balbi4b5faa7a2011-09-06 10:56:51 +030082
Felipe Balbi72246da2011-08-19 18:10:58 +030083/* IRQ_EOI REGISTER */
84#define USBOTGSS_IRQ_EOI_LINE_NUMBER (1 << 0)
85
86/* IRQS0 BITS */
87#define USBOTGSS_IRQO_COREIRQ_ST (1 << 0)
88
George Cherianb1fd6cb2013-06-12 14:53:47 +053089/* IRQMISC BITS */
90#define USBOTGSS_IRQMISC_DMADISABLECLR (1 << 17)
91#define USBOTGSS_IRQMISC_OEVT (1 << 16)
92#define USBOTGSS_IRQMISC_DRVVBUS_RISE (1 << 13)
93#define USBOTGSS_IRQMISC_CHRGVBUS_RISE (1 << 12)
94#define USBOTGSS_IRQMISC_DISCHRGVBUS_RISE (1 << 11)
95#define USBOTGSS_IRQMISC_IDPULLUP_RISE (1 << 8)
96#define USBOTGSS_IRQMISC_DRVVBUS_FALL (1 << 5)
97#define USBOTGSS_IRQMISC_CHRGVBUS_FALL (1 << 4)
98#define USBOTGSS_IRQMISC_DISCHRGVBUS_FALL (1 << 3)
99#define USBOTGSS_IRQMISC_IDPULLUP_FALL (1 << 0)
Felipe Balbi72246da2011-08-19 18:10:58 +0300100
Felipe Balbi72246da2011-08-19 18:10:58 +0300101/* UTMI_OTG_STATUS REGISTER */
Bin Liu22832192015-03-24 15:08:49 -0500102#define USBOTGSS_UTMI_OTG_STATUS_DRVVBUS (1 << 5)
103#define USBOTGSS_UTMI_OTG_STATUS_CHRGVBUS (1 << 4)
104#define USBOTGSS_UTMI_OTG_STATUS_DISCHRGVBUS (1 << 3)
105#define USBOTGSS_UTMI_OTG_STATUS_IDPULLUP (1 << 0)
106
107/* UTMI_OTG_CTRL REGISTER */
108#define USBOTGSS_UTMI_OTG_CTRL_SW_MODE (1 << 31)
109#define USBOTGSS_UTMI_OTG_CTRL_POWERPRESENT (1 << 9)
110#define USBOTGSS_UTMI_OTG_CTRL_TXBITSTUFFENABLE (1 << 8)
111#define USBOTGSS_UTMI_OTG_CTRL_IDDIG (1 << 4)
112#define USBOTGSS_UTMI_OTG_CTRL_SESSEND (1 << 3)
113#define USBOTGSS_UTMI_OTG_CTRL_SESSVALID (1 << 2)
114#define USBOTGSS_UTMI_OTG_CTRL_VBUSVALID (1 << 1)
Felipe Balbi72246da2011-08-19 18:10:58 +0300115
116struct dwc3_omap {
Felipe Balbi72246da2011-08-19 18:10:58 +0300117 struct device *dev;
118
119 int irq;
120 void __iomem *base;
121
Bin Liu22832192015-03-24 15:08:49 -0500122 u32 utmi_otg_ctrl;
George Cherian1e2a0642013-06-12 14:53:45 +0530123 u32 utmi_otg_offset;
124 u32 irqmisc_offset;
125 u32 irq_eoi_offset;
126 u32 debug_offset;
127 u32 irq0_offset;
Felipe Balbif3e117f2013-02-11 11:12:02 +0200128
Chanwoo Choi59603872015-07-01 13:11:30 +0900129 struct extcon_dev *edev;
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900130 struct notifier_block vbus_nb;
131 struct notifier_block id_nb;
132
133 struct regulator *vbus_reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300134};
135
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900136enum omap_dwc3_vbus_id_status {
137 OMAP_DWC3_ID_FLOAT,
138 OMAP_DWC3_ID_GROUND,
139 OMAP_DWC3_VBUS_OFF,
140 OMAP_DWC3_VBUS_VALID,
141};
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530142
Ido Shayevitzab5e59d2012-04-24 14:18:38 +0300143static inline u32 dwc3_omap_readl(void __iomem *base, u32 offset)
144{
145 return readl(base + offset);
146}
147
148static inline void dwc3_omap_writel(void __iomem *base, u32 offset, u32 value)
149{
150 writel(value, base + offset);
151}
152
Bin Liu22832192015-03-24 15:08:49 -0500153static u32 dwc3_omap_read_utmi_ctrl(struct dwc3_omap *omap)
George Cherianb1fd6cb2013-06-12 14:53:47 +0530154{
Bin Liu22832192015-03-24 15:08:49 -0500155 return dwc3_omap_readl(omap->base, USBOTGSS_UTMI_OTG_CTRL +
George Cherianb1fd6cb2013-06-12 14:53:47 +0530156 omap->utmi_otg_offset);
157}
158
Bin Liu22832192015-03-24 15:08:49 -0500159static void dwc3_omap_write_utmi_ctrl(struct dwc3_omap *omap, u32 value)
George Cherianb1fd6cb2013-06-12 14:53:47 +0530160{
Bin Liu22832192015-03-24 15:08:49 -0500161 dwc3_omap_writel(omap->base, USBOTGSS_UTMI_OTG_CTRL +
George Cherianb1fd6cb2013-06-12 14:53:47 +0530162 omap->utmi_otg_offset, value);
163
164}
165
166static u32 dwc3_omap_read_irq0_status(struct dwc3_omap *omap)
167{
Roger Quadros3f586c92016-05-11 17:36:42 +0300168 return dwc3_omap_readl(omap->base, USBOTGSS_IRQSTATUS_RAW_0 -
George Cherianb1fd6cb2013-06-12 14:53:47 +0530169 omap->irq0_offset);
170}
171
172static void dwc3_omap_write_irq0_status(struct dwc3_omap *omap, u32 value)
173{
174 dwc3_omap_writel(omap->base, USBOTGSS_IRQSTATUS_0 -
175 omap->irq0_offset, value);
176
177}
178
179static u32 dwc3_omap_read_irqmisc_status(struct dwc3_omap *omap)
180{
Roger Quadros3f586c92016-05-11 17:36:42 +0300181 return dwc3_omap_readl(omap->base, USBOTGSS_IRQSTATUS_RAW_MISC +
George Cherianb1fd6cb2013-06-12 14:53:47 +0530182 omap->irqmisc_offset);
183}
184
185static void dwc3_omap_write_irqmisc_status(struct dwc3_omap *omap, u32 value)
186{
187 dwc3_omap_writel(omap->base, USBOTGSS_IRQSTATUS_MISC +
188 omap->irqmisc_offset, value);
189
190}
191
192static void dwc3_omap_write_irqmisc_set(struct dwc3_omap *omap, u32 value)
193{
194 dwc3_omap_writel(omap->base, USBOTGSS_IRQENABLE_SET_MISC +
195 omap->irqmisc_offset, value);
196
197}
198
199static void dwc3_omap_write_irq0_set(struct dwc3_omap *omap, u32 value)
200{
201 dwc3_omap_writel(omap->base, USBOTGSS_IRQENABLE_SET_0 -
202 omap->irq0_offset, value);
203}
204
George Cherian96e5d312015-02-13 10:13:24 +0530205static void dwc3_omap_write_irqmisc_clr(struct dwc3_omap *omap, u32 value)
206{
207 dwc3_omap_writel(omap->base, USBOTGSS_IRQENABLE_CLR_MISC +
208 omap->irqmisc_offset, value);
209}
210
211static void dwc3_omap_write_irq0_clr(struct dwc3_omap *omap, u32 value)
212{
213 dwc3_omap_writel(omap->base, USBOTGSS_IRQENABLE_CLR_0 -
214 omap->irq0_offset, value);
215}
216
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900217static void dwc3_omap_set_mailbox(struct dwc3_omap *omap,
218 enum omap_dwc3_vbus_id_status status)
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530219{
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900220 int ret;
221 u32 val;
Kishon Vijay Abraham I2ba79432013-03-07 18:51:44 +0530222
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530223 switch (status) {
224 case OMAP_DWC3_ID_GROUND:
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900225 if (omap->vbus_reg) {
226 ret = regulator_enable(omap->vbus_reg);
227 if (ret) {
Felipe Balbie4f75662015-06-30 12:46:07 -0500228 dev_err(omap->dev, "regulator enable failed\n");
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900229 return;
230 }
231 }
232
Bin Liu22832192015-03-24 15:08:49 -0500233 val = dwc3_omap_read_utmi_ctrl(omap);
Roger Quadrosd2728fb2016-05-11 17:36:45 +0300234 val &= ~USBOTGSS_UTMI_OTG_CTRL_IDDIG;
Bin Liu22832192015-03-24 15:08:49 -0500235 dwc3_omap_write_utmi_ctrl(omap, val);
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530236 break;
237
238 case OMAP_DWC3_VBUS_VALID:
Bin Liu22832192015-03-24 15:08:49 -0500239 val = dwc3_omap_read_utmi_ctrl(omap);
240 val &= ~USBOTGSS_UTMI_OTG_CTRL_SESSEND;
Roger Quadrosd2728fb2016-05-11 17:36:45 +0300241 val |= USBOTGSS_UTMI_OTG_CTRL_VBUSVALID
Roger Quadros9ab330b2016-05-11 17:36:44 +0300242 | USBOTGSS_UTMI_OTG_CTRL_SESSVALID;
Bin Liu22832192015-03-24 15:08:49 -0500243 dwc3_omap_write_utmi_ctrl(omap, val);
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530244 break;
245
246 case OMAP_DWC3_ID_FLOAT:
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900247 if (omap->vbus_reg)
248 regulator_disable(omap->vbus_reg);
Roger Quadrosd2728fb2016-05-11 17:36:45 +0300249 val = dwc3_omap_read_utmi_ctrl(omap);
250 val |= USBOTGSS_UTMI_OTG_CTRL_IDDIG;
251 dwc3_omap_write_utmi_ctrl(omap, val);
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900252
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530253 case OMAP_DWC3_VBUS_OFF:
Bin Liu22832192015-03-24 15:08:49 -0500254 val = dwc3_omap_read_utmi_ctrl(omap);
255 val &= ~(USBOTGSS_UTMI_OTG_CTRL_SESSVALID
Roger Quadros9ab330b2016-05-11 17:36:44 +0300256 | USBOTGSS_UTMI_OTG_CTRL_VBUSVALID);
Roger Quadrosd2728fb2016-05-11 17:36:45 +0300257 val |= USBOTGSS_UTMI_OTG_CTRL_SESSEND;
Bin Liu22832192015-03-24 15:08:49 -0500258 dwc3_omap_write_utmi_ctrl(omap, val);
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530259 break;
260
261 default:
Felipe Balbie4f75662015-06-30 12:46:07 -0500262 dev_WARN(omap->dev, "invalid state\n");
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530263 }
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530264}
Kishon Vijay Abraham I7e41bba2013-01-25 08:30:49 +0530265
Roger Quadros3f586c92016-05-11 17:36:42 +0300266static void dwc3_omap_enable_irqs(struct dwc3_omap *omap);
267static void dwc3_omap_disable_irqs(struct dwc3_omap *omap);
268
Felipe Balbi72246da2011-08-19 18:10:58 +0300269static irqreturn_t dwc3_omap_interrupt(int irq, void *_omap)
270{
271 struct dwc3_omap *omap = _omap;
Roger Quadros3f586c92016-05-11 17:36:42 +0300272
273 if (dwc3_omap_read_irqmisc_status(omap) ||
274 dwc3_omap_read_irq0_status(omap)) {
275 /* mask irqs */
276 dwc3_omap_disable_irqs(omap);
277 return IRQ_WAKE_THREAD;
278 }
279
280 return IRQ_NONE;
281}
282
283static irqreturn_t dwc3_omap_interrupt_thread(int irq, void *_omap)
284{
285 struct dwc3_omap *omap = _omap;
Felipe Balbi72246da2011-08-19 18:10:58 +0300286 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300287
Roger Quadros3f586c92016-05-11 17:36:42 +0300288 /* clear irq status flags */
George Cherianb1fd6cb2013-06-12 14:53:47 +0530289 reg = dwc3_omap_read_irqmisc_status(omap);
George Cherianb1fd6cb2013-06-12 14:53:47 +0530290 dwc3_omap_write_irqmisc_status(omap, reg);
Felipe Balbi42077b02011-09-06 12:00:39 +0300291
George Cherianb1fd6cb2013-06-12 14:53:47 +0530292 reg = dwc3_omap_read_irq0_status(omap);
George Cherianb1fd6cb2013-06-12 14:53:47 +0530293 dwc3_omap_write_irq0_status(omap, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300294
Roger Quadros3f586c92016-05-11 17:36:42 +0300295 /* unmask irqs */
296 dwc3_omap_enable_irqs(omap);
297
Felipe Balbi72246da2011-08-19 18:10:58 +0300298 return IRQ_HANDLED;
299}
300
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200301static void dwc3_omap_enable_irqs(struct dwc3_omap *omap)
302{
303 u32 reg;
304
305 /* enable all IRQs */
306 reg = USBOTGSS_IRQO_COREIRQ_ST;
George Cherianb1fd6cb2013-06-12 14:53:47 +0530307 dwc3_omap_write_irq0_set(omap, reg);
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200308
George Cherianb1fd6cb2013-06-12 14:53:47 +0530309 reg = (USBOTGSS_IRQMISC_OEVT |
310 USBOTGSS_IRQMISC_DRVVBUS_RISE |
311 USBOTGSS_IRQMISC_CHRGVBUS_RISE |
312 USBOTGSS_IRQMISC_DISCHRGVBUS_RISE |
313 USBOTGSS_IRQMISC_IDPULLUP_RISE |
314 USBOTGSS_IRQMISC_DRVVBUS_FALL |
315 USBOTGSS_IRQMISC_CHRGVBUS_FALL |
316 USBOTGSS_IRQMISC_DISCHRGVBUS_FALL |
317 USBOTGSS_IRQMISC_IDPULLUP_FALL);
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200318
George Cherianb1fd6cb2013-06-12 14:53:47 +0530319 dwc3_omap_write_irqmisc_set(omap, reg);
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200320}
321
322static void dwc3_omap_disable_irqs(struct dwc3_omap *omap)
323{
George Cherian96e5d312015-02-13 10:13:24 +0530324 u32 reg;
325
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200326 /* disable all IRQs */
George Cherian96e5d312015-02-13 10:13:24 +0530327 reg = USBOTGSS_IRQO_COREIRQ_ST;
328 dwc3_omap_write_irq0_clr(omap, reg);
329
330 reg = (USBOTGSS_IRQMISC_OEVT |
331 USBOTGSS_IRQMISC_DRVVBUS_RISE |
332 USBOTGSS_IRQMISC_CHRGVBUS_RISE |
333 USBOTGSS_IRQMISC_DISCHRGVBUS_RISE |
334 USBOTGSS_IRQMISC_IDPULLUP_RISE |
335 USBOTGSS_IRQMISC_DRVVBUS_FALL |
336 USBOTGSS_IRQMISC_CHRGVBUS_FALL |
337 USBOTGSS_IRQMISC_DISCHRGVBUS_FALL |
338 USBOTGSS_IRQMISC_IDPULLUP_FALL);
339
340 dwc3_omap_write_irqmisc_clr(omap, reg);
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200341}
342
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900343static int dwc3_omap_id_notifier(struct notifier_block *nb,
344 unsigned long event, void *ptr)
345{
346 struct dwc3_omap *omap = container_of(nb, struct dwc3_omap, id_nb);
347
348 if (event)
349 dwc3_omap_set_mailbox(omap, OMAP_DWC3_ID_GROUND);
350 else
351 dwc3_omap_set_mailbox(omap, OMAP_DWC3_ID_FLOAT);
352
353 return NOTIFY_DONE;
354}
355
356static int dwc3_omap_vbus_notifier(struct notifier_block *nb,
357 unsigned long event, void *ptr)
358{
359 struct dwc3_omap *omap = container_of(nb, struct dwc3_omap, vbus_nb);
360
361 if (event)
362 dwc3_omap_set_mailbox(omap, OMAP_DWC3_VBUS_VALID);
363 else
364 dwc3_omap_set_mailbox(omap, OMAP_DWC3_VBUS_OFF);
365
366 return NOTIFY_DONE;
367}
368
George Cherian30fef1a2014-07-16 18:37:06 +0530369static void dwc3_omap_map_offset(struct dwc3_omap *omap)
370{
371 struct device_node *node = omap->dev->of_node;
372
373 /*
374 * Differentiate between OMAP5 and AM437x.
375 *
376 * For OMAP5(ES2.0) and AM437x wrapper revision is same, even
377 * though there are changes in wrapper register offsets.
378 *
379 * Using dt compatible to differentiate AM437x.
380 */
381 if (of_device_is_compatible(node, "ti,am437x-dwc3")) {
382 omap->irq_eoi_offset = USBOTGSS_EOI_OFFSET;
383 omap->irq0_offset = USBOTGSS_IRQ0_OFFSET;
384 omap->irqmisc_offset = USBOTGSS_IRQMISC_OFFSET;
385 omap->utmi_otg_offset = USBOTGSS_UTMI_OTG_OFFSET;
386 omap->debug_offset = USBOTGSS_DEBUG_OFFSET;
387 }
388}
389
George Cheriand2f0cf892014-07-16 18:37:07 +0530390static void dwc3_omap_set_utmi_mode(struct dwc3_omap *omap)
391{
392 u32 reg;
393 struct device_node *node = omap->dev->of_node;
394 int utmi_mode = 0;
395
Bin Liu22832192015-03-24 15:08:49 -0500396 reg = dwc3_omap_read_utmi_ctrl(omap);
George Cheriand2f0cf892014-07-16 18:37:07 +0530397
398 of_property_read_u32(node, "utmi-mode", &utmi_mode);
399
400 switch (utmi_mode) {
401 case DWC3_OMAP_UTMI_MODE_SW:
Bin Liu22832192015-03-24 15:08:49 -0500402 reg |= USBOTGSS_UTMI_OTG_CTRL_SW_MODE;
George Cheriand2f0cf892014-07-16 18:37:07 +0530403 break;
404 case DWC3_OMAP_UTMI_MODE_HW:
Bin Liu22832192015-03-24 15:08:49 -0500405 reg &= ~USBOTGSS_UTMI_OTG_CTRL_SW_MODE;
George Cheriand2f0cf892014-07-16 18:37:07 +0530406 break;
407 default:
Felipe Balbie4f75662015-06-30 12:46:07 -0500408 dev_WARN(omap->dev, "UNKNOWN utmi mode %d\n", utmi_mode);
George Cheriand2f0cf892014-07-16 18:37:07 +0530409 }
410
Bin Liu22832192015-03-24 15:08:49 -0500411 dwc3_omap_write_utmi_ctrl(omap, reg);
George Cheriand2f0cf892014-07-16 18:37:07 +0530412}
413
George Cherian025b4312014-07-16 18:37:08 +0530414static int dwc3_omap_extcon_register(struct dwc3_omap *omap)
415{
Dan Carpenter788b0bc42014-07-31 18:30:51 +0300416 int ret;
George Cherian025b4312014-07-16 18:37:08 +0530417 struct device_node *node = omap->dev->of_node;
418 struct extcon_dev *edev;
419
420 if (of_property_read_bool(node, "extcon")) {
421 edev = extcon_get_edev_by_phandle(omap->dev, 0);
422 if (IS_ERR(edev)) {
423 dev_vdbg(omap->dev, "couldn't get extcon device\n");
424 return -EPROBE_DEFER;
425 }
426
427 omap->vbus_nb.notifier_call = dwc3_omap_vbus_notifier;
Chanwoo Choi59603872015-07-01 13:11:30 +0900428 ret = extcon_register_notifier(edev, EXTCON_USB,
429 &omap->vbus_nb);
George Cherian025b4312014-07-16 18:37:08 +0530430 if (ret < 0)
431 dev_vdbg(omap->dev, "failed to register notifier for USB\n");
432
433 omap->id_nb.notifier_call = dwc3_omap_id_notifier;
Chanwoo Choi59603872015-07-01 13:11:30 +0900434 ret = extcon_register_notifier(edev, EXTCON_USB_HOST,
435 &omap->id_nb);
George Cherian025b4312014-07-16 18:37:08 +0530436 if (ret < 0)
437 dev_vdbg(omap->dev, "failed to register notifier for USB-HOST\n");
438
Chanwoo Choi59603872015-07-01 13:11:30 +0900439 if (extcon_get_cable_state_(edev, EXTCON_USB) == true)
George Cherian025b4312014-07-16 18:37:08 +0530440 dwc3_omap_set_mailbox(omap, OMAP_DWC3_VBUS_VALID);
Chanwoo Choi59603872015-07-01 13:11:30 +0900441 if (extcon_get_cable_state_(edev, EXTCON_USB_HOST) == true)
George Cherian025b4312014-07-16 18:37:08 +0530442 dwc3_omap_set_mailbox(omap, OMAP_DWC3_ID_GROUND);
Chanwoo Choi59603872015-07-01 13:11:30 +0900443
444 omap->edev = edev;
George Cherian025b4312014-07-16 18:37:08 +0530445 }
446
447 return 0;
448}
449
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500450static int dwc3_omap_probe(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +0300451{
Felipe Balbi45b3cd4a2012-01-25 11:07:03 +0200452 struct device_node *node = pdev->dev.of_node;
453
Felipe Balbi72246da2011-08-19 18:10:58 +0300454 struct dwc3_omap *omap;
455 struct resource *res;
Chanho Park802ca852012-02-15 18:27:55 +0900456 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900457 struct regulator *vbus_reg = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300458
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300459 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300460 int irq;
461
462 u32 reg;
463
464 void __iomem *base;
Felipe Balbi72246da2011-08-19 18:10:58 +0300465
Kishon Vijay Abraham I4495afc2013-02-26 20:03:28 +0530466 if (!node) {
467 dev_err(dev, "device node not found\n");
468 return -EINVAL;
469 }
470
Chanho Park802ca852012-02-15 18:27:55 +0900471 omap = devm_kzalloc(dev, sizeof(*omap), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +0900472 if (!omap)
Chanho Park802ca852012-02-15 18:27:55 +0900473 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300474
475 platform_set_drvdata(pdev, omap);
476
Kishon Vijay Abraham Ie36a0c82013-02-26 20:03:27 +0530477 irq = platform_get_irq(pdev, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300478 if (irq < 0) {
Chanho Park802ca852012-02-15 18:27:55 +0900479 dev_err(dev, "missing IRQ resource\n");
480 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300481 }
482
Kishon Vijay Abraham Ie36a0c82013-02-26 20:03:27 +0530483 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Felipe Balbi8bbcd172013-07-12 15:33:35 +0300484 base = devm_ioremap_resource(dev, res);
485 if (IS_ERR(base))
486 return PTR_ERR(base);
Felipe Balbi72246da2011-08-19 18:10:58 +0300487
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900488 if (of_property_read_bool(node, "vbus-supply")) {
489 vbus_reg = devm_regulator_get(dev, "vbus");
490 if (IS_ERR(vbus_reg)) {
491 dev_err(dev, "vbus init failed\n");
492 return PTR_ERR(vbus_reg);
493 }
494 }
495
Chanho Park802ca852012-02-15 18:27:55 +0900496 omap->dev = dev;
Felipe Balbi72246da2011-08-19 18:10:58 +0300497 omap->irq = irq;
498 omap->base = base;
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900499 omap->vbus_reg = vbus_reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300500
Kishon Vijay Abraham Iaf310e92013-01-25 08:30:47 +0530501 pm_runtime_enable(dev);
502 ret = pm_runtime_get_sync(dev);
503 if (ret < 0) {
504 dev_err(dev, "get_sync failed with err %d\n", ret);
Felipe Balbi45d49cb2016-04-11 17:12:33 +0300505 goto err1;
Kishon Vijay Abraham Iaf310e92013-01-25 08:30:47 +0530506 }
507
George Cherian30fef1a2014-07-16 18:37:06 +0530508 dwc3_omap_map_offset(omap);
George Cheriand2f0cf892014-07-16 18:37:07 +0530509 dwc3_omap_set_utmi_mode(omap);
Felipe Balbi99624442011-09-01 22:26:25 +0300510
Felipe Balbi72246da2011-08-19 18:10:58 +0300511 /* check the DMA Status */
Ido Shayevitzab5e59d2012-04-24 14:18:38 +0300512 reg = dwc3_omap_readl(omap->base, USBOTGSS_SYSCONFIG);
Felipe Balbi72246da2011-08-19 18:10:58 +0300513
Roger Quadros3f586c92016-05-11 17:36:42 +0300514 ret = devm_request_threaded_irq(dev, omap->irq, dwc3_omap_interrupt,
Roger Quadros12da8ea2016-05-11 17:36:43 +0300515 dwc3_omap_interrupt_thread, IRQF_SHARED,
Roger Quadros3f586c92016-05-11 17:36:42 +0300516 "dwc3-omap", omap);
Felipe Balbi72246da2011-08-19 18:10:58 +0300517 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +0900518 dev_err(dev, "failed to request IRQ #%d --> %d\n",
Felipe Balbi72246da2011-08-19 18:10:58 +0300519 omap->irq, ret);
Kishon Vijay Abraham I594daba2013-06-03 21:43:39 +0530520 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300521 }
522
George Cherian025b4312014-07-16 18:37:08 +0530523 ret = dwc3_omap_extcon_register(omap);
524 if (ret < 0)
Felipe Balbi45d49cb2016-04-11 17:12:33 +0300525 goto err1;
Kishon Vijay Abraham I8061ad72013-07-08 09:54:43 +0900526
Kishon Vijay Abraham I4495afc2013-02-26 20:03:28 +0530527 ret = of_platform_populate(node, NULL, NULL, dev);
528 if (ret) {
529 dev_err(&pdev->dev, "failed to create dwc3 core\n");
Felipe Balbi45d49cb2016-04-11 17:12:33 +0300530 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +0300531 }
532
Felipe Balbie2ae0692015-08-25 12:07:45 -0500533 dwc3_omap_enable_irqs(omap);
534
Felipe Balbi72246da2011-08-19 18:10:58 +0300535 return 0;
Kishon Vijay Abraham I594daba2013-06-03 21:43:39 +0530536
Felipe Balbi45d49cb2016-04-11 17:12:33 +0300537err2:
Chanwoo Choi59603872015-07-01 13:11:30 +0900538 extcon_unregister_notifier(omap->edev, EXTCON_USB, &omap->vbus_nb);
539 extcon_unregister_notifier(omap->edev, EXTCON_USB_HOST, &omap->id_nb);
Kishon Vijay Abraham I594daba2013-06-03 21:43:39 +0530540
541err1:
542 pm_runtime_put_sync(dev);
Kishon Vijay Abraham I594daba2013-06-03 21:43:39 +0530543 pm_runtime_disable(dev);
544
545 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300546}
547
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500548static int dwc3_omap_remove(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +0300549{
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200550 struct dwc3_omap *omap = platform_get_drvdata(pdev);
551
Chanwoo Choi59603872015-07-01 13:11:30 +0900552 extcon_unregister_notifier(omap->edev, EXTCON_USB, &omap->vbus_nb);
553 extcon_unregister_notifier(omap->edev, EXTCON_USB_HOST, &omap->id_nb);
Felipe Balbi9a4b5da2013-02-11 11:03:59 +0200554 dwc3_omap_disable_irqs(omap);
Felipe Balbi3d0184d2014-09-02 14:12:26 -0500555 of_platform_depopulate(omap->dev);
Kishon Vijay Abraham Iaf310e92013-01-25 08:30:47 +0530556 pm_runtime_put_sync(&pdev->dev);
557 pm_runtime_disable(&pdev->dev);
Kishon Vijay Abraham I94c6a432013-01-25 08:30:45 +0530558
Felipe Balbi72246da2011-08-19 18:10:58 +0300559 return 0;
560}
561
Felipe Balbi2c2dc892013-02-11 10:31:15 +0200562static const struct of_device_id of_dwc3_match[] = {
Felipe Balbi72246da2011-08-19 18:10:58 +0300563 {
Kishon Vijay Abraham Ie36a0c82013-02-26 20:03:27 +0530564 .compatible = "ti,dwc3"
Felipe Balbi72246da2011-08-19 18:10:58 +0300565 },
George Cherianff7307b2013-06-12 14:53:46 +0530566 {
567 .compatible = "ti,am437x-dwc3"
568 },
Felipe Balbi72246da2011-08-19 18:10:58 +0300569 { },
570};
Felipe Balbi2c2dc892013-02-11 10:31:15 +0200571MODULE_DEVICE_TABLE(of, of_dwc3_match);
Felipe Balbi72246da2011-08-19 18:10:58 +0300572
Jingoo Han19fda7c2013-03-26 01:52:48 +0000573#ifdef CONFIG_PM_SLEEP
Felipe Balbif3e117f2013-02-11 11:12:02 +0200574static int dwc3_omap_suspend(struct device *dev)
575{
576 struct dwc3_omap *omap = dev_get_drvdata(dev);
577
Bin Liu22832192015-03-24 15:08:49 -0500578 omap->utmi_otg_ctrl = dwc3_omap_read_utmi_ctrl(omap);
George Cherian7ee25662013-12-17 18:47:54 +0530579 dwc3_omap_disable_irqs(omap);
Felipe Balbif3e117f2013-02-11 11:12:02 +0200580
581 return 0;
582}
583
584static int dwc3_omap_resume(struct device *dev)
585{
586 struct dwc3_omap *omap = dev_get_drvdata(dev);
587
Bin Liu22832192015-03-24 15:08:49 -0500588 dwc3_omap_write_utmi_ctrl(omap, omap->utmi_otg_ctrl);
George Cherian7ee25662013-12-17 18:47:54 +0530589 dwc3_omap_enable_irqs(omap);
Felipe Balbif3e117f2013-02-11 11:12:02 +0200590
591 pm_runtime_disable(dev);
592 pm_runtime_set_active(dev);
593 pm_runtime_enable(dev);
594
595 return 0;
596}
597
598static const struct dev_pm_ops dwc3_omap_dev_pm_ops = {
Felipe Balbif3e117f2013-02-11 11:12:02 +0200599
600 SET_SYSTEM_SLEEP_PM_OPS(dwc3_omap_suspend, dwc3_omap_resume)
601};
602
603#define DEV_PM_OPS (&dwc3_omap_dev_pm_ops)
604#else
605#define DEV_PM_OPS NULL
Jingoo Han19fda7c2013-03-26 01:52:48 +0000606#endif /* CONFIG_PM_SLEEP */
Felipe Balbif3e117f2013-02-11 11:12:02 +0200607
Felipe Balbi72246da2011-08-19 18:10:58 +0300608static struct platform_driver dwc3_omap_driver = {
609 .probe = dwc3_omap_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500610 .remove = dwc3_omap_remove,
Felipe Balbi72246da2011-08-19 18:10:58 +0300611 .driver = {
612 .name = "omap-dwc3",
Felipe Balbi2c2dc892013-02-11 10:31:15 +0200613 .of_match_table = of_dwc3_match,
Felipe Balbif3e117f2013-02-11 11:12:02 +0200614 .pm = DEV_PM_OPS,
Felipe Balbi72246da2011-08-19 18:10:58 +0300615 },
616};
617
Axel Lincc27c962011-11-27 20:16:27 +0800618module_platform_driver(dwc3_omap_driver);
619
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +0200620MODULE_ALIAS("platform:omap-dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +0300621MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
Felipe Balbi5945f782013-06-30 14:15:11 +0300622MODULE_LICENSE("GPL v2");
Felipe Balbi72246da2011-08-19 18:10:58 +0300623MODULE_DESCRIPTION("DesignWare USB3 OMAP Glue Layer");