blob: f4d95037db5d352a1c6381a55cc0c9f07ddccff4 [file] [log] [blame]
Felipe Balbi550a7372008-07-24 12:27:36 +03001/*
2 * Copyright (C) 2005-2007 by Texas Instruments
3 * Some code has been taken from tusb6010.c
4 * Copyrights for that are attributable to:
5 * Copyright (C) 2006 Nokia Corporation
Felipe Balbi550a7372008-07-24 12:27:36 +03006 * Tony Lindgren <tony@atomide.com>
7 *
8 * This file is part of the Inventra Controller Driver for Linux.
9 *
10 * The Inventra Controller Driver for Linux is free software; you
11 * can redistribute it and/or modify it under the terms of the GNU
12 * General Public License version 2 as published by the Free Software
13 * Foundation.
14 *
15 * The Inventra Controller Driver for Linux is distributed in
16 * the hope that it will be useful, but WITHOUT ANY WARRANTY;
17 * without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 * License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with The Inventra Controller Driver for Linux ; if not,
23 * write to the Free Software Foundation, Inc., 59 Temple Place,
24 * Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/sched.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030030#include <linux/init.h>
31#include <linux/list.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030032#include <linux/io.h>
Felipe Balbidc098862010-12-01 15:01:11 +020033#include <linux/platform_device.h>
34#include <linux/dma-mapping.h>
Hema HK207b0e12011-02-17 12:07:22 +053035#include <linux/pm_runtime.h>
36#include <linux/err.h>
NeilBrown12a19b52012-08-13 12:32:58 +100037#include <linux/delay.h>
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +053038#include <linux/usb/musb-omap.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030039
Felipe Balbi550a7372008-07-24 12:27:36 +030040#include "musb_core.h"
41#include "omap2430.h"
42
Felipe Balbia3cee122010-12-02 09:27:29 +020043struct omap2430_glue {
44 struct device *dev;
45 struct platform_device *musb;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +053046 enum omap_musb_vbus_id_status status;
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +053047 struct work_struct omap_musb_mailbox_work;
Kishon Vijay Abraham I5ec40592012-09-11 14:39:39 +053048 u32 __iomem *control_otghs;
Felipe Balbia3cee122010-12-02 09:27:29 +020049};
Felipe Balbic20aebb2010-12-02 12:44:40 +020050#define glue_to_musb(g) platform_get_drvdata(g->musb)
Felipe Balbia3cee122010-12-02 09:27:29 +020051
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +053052struct omap2430_glue *_glue;
53
Felipe Balbi550a7372008-07-24 12:27:36 +030054static struct timer_list musb_idle_timer;
55
Kishon Vijay Abraham I5ec40592012-09-11 14:39:39 +053056/**
57 * omap4_usb_phy_mailbox - write to usb otg mailbox
58 * @glue: struct omap2430_glue *
59 * @val: the value to be written to the mailbox
60 *
61 * On detection of a device (ID pin is grounded), this API should be called
62 * to set AVALID, VBUSVALID and ID pin is grounded.
63 *
64 * When OMAP is connected to a host (OMAP in device mode), this API
65 * is called to set AVALID, VBUSVALID and ID pin in high impedance.
66 *
67 * XXX: This function will be removed once we have a seperate driver for
68 * control module
69 */
70static void omap4_usb_phy_mailbox(struct omap2430_glue *glue, u32 val)
71{
72 if (glue->control_otghs)
73 writel(val, glue->control_otghs);
74}
75
Felipe Balbi550a7372008-07-24 12:27:36 +030076static void musb_do_idle(unsigned long _musb)
77{
78 struct musb *musb = (void *)_musb;
79 unsigned long flags;
80 u8 power;
Felipe Balbi550a7372008-07-24 12:27:36 +030081 u8 devctl;
82
Felipe Balbi550a7372008-07-24 12:27:36 +030083 spin_lock_irqsave(&musb->lock, flags);
84
David Brownell84e250f2009-03-31 12:30:04 -070085 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +030086 case OTG_STATE_A_WAIT_BCON:
Felipe Balbi550a7372008-07-24 12:27:36 +030087
88 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
89 if (devctl & MUSB_DEVCTL_BDEVICE) {
David Brownell84e250f2009-03-31 12:30:04 -070090 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +030091 MUSB_DEV_MODE(musb);
92 } else {
David Brownell84e250f2009-03-31 12:30:04 -070093 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +030094 MUSB_HST_MODE(musb);
95 }
96 break;
Felipe Balbi550a7372008-07-24 12:27:36 +030097 case OTG_STATE_A_SUSPEND:
98 /* finish RESUME signaling? */
99 if (musb->port1_status & MUSB_PORT_STAT_RESUME) {
100 power = musb_readb(musb->mregs, MUSB_POWER);
101 power &= ~MUSB_POWER_RESUME;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300102 dev_dbg(musb->controller, "root port resume stopped, power %02x\n", power);
Felipe Balbi550a7372008-07-24 12:27:36 +0300103 musb_writeb(musb->mregs, MUSB_POWER, power);
104 musb->is_active = 1;
105 musb->port1_status &= ~(USB_PORT_STAT_SUSPEND
106 | MUSB_PORT_STAT_RESUME);
107 musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
108 usb_hcd_poll_rh_status(musb_to_hcd(musb));
109 /* NOTE: it might really be A_WAIT_BCON ... */
David Brownell84e250f2009-03-31 12:30:04 -0700110 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300111 }
112 break;
Felipe Balbi550a7372008-07-24 12:27:36 +0300113 case OTG_STATE_A_HOST:
114 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
115 if (devctl & MUSB_DEVCTL_BDEVICE)
David Brownell84e250f2009-03-31 12:30:04 -0700116 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300117 else
David Brownell84e250f2009-03-31 12:30:04 -0700118 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
Felipe Balbi550a7372008-07-24 12:27:36 +0300119 default:
120 break;
121 }
122 spin_unlock_irqrestore(&musb->lock, flags);
123}
124
125
Felipe Balbi743411b2010-12-01 13:22:05 +0200126static void omap2430_musb_try_idle(struct musb *musb, unsigned long timeout)
Felipe Balbi550a7372008-07-24 12:27:36 +0300127{
128 unsigned long default_timeout = jiffies + msecs_to_jiffies(3);
129 static unsigned long last_timer;
130
131 if (timeout == 0)
132 timeout = default_timeout;
133
134 /* Never idle if active, or when VBUS timeout is not set as host */
135 if (musb->is_active || ((musb->a_wait_bcon == 0)
David Brownell84e250f2009-03-31 12:30:04 -0700136 && (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300137 dev_dbg(musb->controller, "%s active, deleting timer\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200138 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300139 del_timer(&musb_idle_timer);
140 last_timer = jiffies;
141 return;
142 }
143
144 if (time_after(last_timer, timeout)) {
145 if (!timer_pending(&musb_idle_timer))
146 last_timer = timeout;
147 else {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300148 dev_dbg(musb->controller, "Longer idle timer already pending, ignoring\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300149 return;
150 }
151 }
152 last_timer = timeout;
153
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300154 dev_dbg(musb->controller, "%s inactive, for idle timer for %lu ms\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200155 otg_state_string(musb->xceiv->state),
Felipe Balbi550a7372008-07-24 12:27:36 +0300156 (unsigned long)jiffies_to_msecs(timeout - jiffies));
157 mod_timer(&musb_idle_timer, timeout);
158}
159
Felipe Balbi743411b2010-12-01 13:22:05 +0200160static void omap2430_musb_set_vbus(struct musb *musb, int is_on)
Felipe Balbi550a7372008-07-24 12:27:36 +0300161{
Heikki Krogerusd445b6d2012-02-13 13:24:15 +0200162 struct usb_otg *otg = musb->xceiv->otg;
Felipe Balbi550a7372008-07-24 12:27:36 +0300163 u8 devctl;
Hema HK594632e2010-12-10 18:10:51 +0530164 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Felipe Balbi550a7372008-07-24 12:27:36 +0300165 /* HDRC controls CPEN, but beware current surges during device
166 * connect. They can trigger transient overcurrent conditions
167 * that must be ignored.
168 */
169
170 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
171
172 if (is_on) {
Hema HK594632e2010-12-10 18:10:51 +0530173 if (musb->xceiv->state == OTG_STATE_A_IDLE) {
NeilBrown12a19b52012-08-13 12:32:58 +1000174 int loops = 100;
Hema HK594632e2010-12-10 18:10:51 +0530175 /* start the session */
176 devctl |= MUSB_DEVCTL_SESSION;
177 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
178 /*
179 * Wait for the musb to set as A device to enable the
180 * VBUS
181 */
182 while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300183
NeilBrown12a19b52012-08-13 12:32:58 +1000184 mdelay(5);
Hema HK594632e2010-12-10 18:10:51 +0530185 cpu_relax();
186
NeilBrown12a19b52012-08-13 12:32:58 +1000187 if (time_after(jiffies, timeout)
188 || loops-- <= 0) {
Hema HK594632e2010-12-10 18:10:51 +0530189 dev_err(musb->controller,
190 "configured as A device timeout");
Hema HK594632e2010-12-10 18:10:51 +0530191 break;
192 }
193 }
194
Matthias Brugger10770c52012-08-16 13:00:25 +0200195 if (otg->set_vbus)
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200196 otg_set_vbus(otg, 1);
Hema HK594632e2010-12-10 18:10:51 +0530197 } else {
198 musb->is_active = 1;
Heikki Krogerusd445b6d2012-02-13 13:24:15 +0200199 otg->default_a = 1;
Hema HK594632e2010-12-10 18:10:51 +0530200 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
201 devctl |= MUSB_DEVCTL_SESSION;
202 MUSB_HST_MODE(musb);
203 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300204 } else {
205 musb->is_active = 0;
206
207 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and
208 * jumping right to B_IDLE...
209 */
210
Heikki Krogerusd445b6d2012-02-13 13:24:15 +0200211 otg->default_a = 0;
David Brownell84e250f2009-03-31 12:30:04 -0700212 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300213 devctl &= ~MUSB_DEVCTL_SESSION;
214
215 MUSB_DEV_MODE(musb);
216 }
217 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
218
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300219 dev_dbg(musb->controller, "VBUS %s, devctl %02x "
Felipe Balbi550a7372008-07-24 12:27:36 +0300220 /* otg %3x conf %08x prcm %08x */ "\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200221 otg_state_string(musb->xceiv->state),
Felipe Balbi550a7372008-07-24 12:27:36 +0300222 musb_readb(musb->mregs, MUSB_DEVCTL));
223}
Felipe Balbi550a7372008-07-24 12:27:36 +0300224
Felipe Balbi743411b2010-12-01 13:22:05 +0200225static int omap2430_musb_set_mode(struct musb *musb, u8 musb_mode)
Felipe Balbi550a7372008-07-24 12:27:36 +0300226{
227 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
228
229 devctl |= MUSB_DEVCTL_SESSION;
230 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
231
David Brownell96a274d2008-11-24 13:06:47 +0200232 return 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300233}
234
Felipe Balbic20aebb2010-12-02 12:44:40 +0200235static inline void omap2430_low_level_exit(struct musb *musb)
236{
237 u32 l;
238
239 /* in any role */
240 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
241 l |= ENABLEFORCE; /* enable MSTANDBY */
242 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
Felipe Balbic20aebb2010-12-02 12:44:40 +0200243}
244
245static inline void omap2430_low_level_init(struct musb *musb)
246{
247 u32 l;
248
Felipe Balbic20aebb2010-12-02 12:44:40 +0200249 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
250 l &= ~ENABLEFORCE; /* disable MSTANDBY */
251 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
252}
253
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530254void omap_musb_mailbox(enum omap_musb_vbus_id_status status)
Hema HK594632e2010-12-10 18:10:51 +0530255{
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530256 struct omap2430_glue *glue = _glue;
257 struct musb *musb = glue_to_musb(glue);
Vikram Pandita712d8ef2011-08-12 07:38:51 -0700258
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530259 glue->status = status;
260 if (!musb) {
261 dev_err(glue->dev, "musb core is not yet ready\n");
262 return;
263 }
264
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530265 schedule_work(&glue->omap_musb_mailbox_work);
Vikram Pandita712d8ef2011-08-12 07:38:51 -0700266}
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530267EXPORT_SYMBOL_GPL(omap_musb_mailbox);
Vikram Pandita712d8ef2011-08-12 07:38:51 -0700268
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530269static void omap_musb_set_mailbox(struct omap2430_glue *glue)
Vikram Pandita712d8ef2011-08-12 07:38:51 -0700270{
Kishon Vijay Abraham I5ec40592012-09-11 14:39:39 +0530271 u32 val;
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530272 struct musb *musb = glue_to_musb(glue);
Hema HK594632e2010-12-10 18:10:51 +0530273 struct device *dev = musb->controller;
274 struct musb_hdrc_platform_data *pdata = dev->platform_data;
275 struct omap_musb_board_data *data = pdata->board_data;
Kishon Vijay Abraham Ic83a8542012-06-22 17:40:53 +0530276 struct usb_otg *otg = musb->xceiv->otg;
Hema HK594632e2010-12-10 18:10:51 +0530277
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530278 switch (glue->status) {
279 case OMAP_MUSB_ID_GROUND:
280 dev_dbg(dev, "ID GND\n");
Hema HK594632e2010-12-10 18:10:51 +0530281
Kishon Vijay Abraham Ic83a8542012-06-22 17:40:53 +0530282 otg->default_a = true;
283 musb->xceiv->state = OTG_STATE_A_IDLE;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530284 musb->xceiv->last_event = USB_EVENT_ID;
Felipe Balbi032ec492011-11-24 15:46:26 +0200285 if (musb->gadget_driver) {
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530286 pm_runtime_get_sync(dev);
Kishon Vijay Abraham I5ec40592012-09-11 14:39:39 +0530287 val = AVALID | VBUSVALID;
288 omap4_usb_phy_mailbox(glue, val);
Hema HK70045c52011-02-28 15:05:29 +0530289 omap2430_musb_set_vbus(musb, 1);
Hema HK594632e2010-12-10 18:10:51 +0530290 }
291 break;
292
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530293 case OMAP_MUSB_VBUS_VALID:
294 dev_dbg(dev, "VBUS Connect\n");
Hema HK594632e2010-12-10 18:10:51 +0530295
Kishon Vijay Abraham Ic83a8542012-06-22 17:40:53 +0530296 otg->default_a = false;
297 musb->xceiv->state = OTG_STATE_B_IDLE;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530298 musb->xceiv->last_event = USB_EVENT_VBUS;
Hema HK7acc6192011-02-28 14:19:34 +0530299 if (musb->gadget_driver)
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530300 pm_runtime_get_sync(dev);
Kishon Vijay Abraham I5ec40592012-09-11 14:39:39 +0530301 val = IDDIG | AVALID | VBUSVALID;
302 omap4_usb_phy_mailbox(glue, val);
Hema HK594632e2010-12-10 18:10:51 +0530303 break;
304
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530305 case OMAP_MUSB_ID_FLOAT:
306 case OMAP_MUSB_VBUS_OFF:
307 dev_dbg(dev, "VBUS Disconnect\n");
Hema HK594632e2010-12-10 18:10:51 +0530308
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530309 musb->xceiv->last_event = USB_EVENT_NONE;
Felipe Balbi032ec492011-11-24 15:46:26 +0200310 if (musb->gadget_driver) {
311 pm_runtime_mark_last_busy(dev);
312 pm_runtime_put_autosuspend(dev);
313 }
Hema HK7acc6192011-02-28 14:19:34 +0530314
Hema HK594632e2010-12-10 18:10:51 +0530315 if (data->interface_type == MUSB_INTERFACE_UTMI) {
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200316 if (musb->xceiv->otg->set_vbus)
317 otg_set_vbus(musb->xceiv->otg, 0);
Hema HK594632e2010-12-10 18:10:51 +0530318 }
Kishon Vijay Abraham I5ec40592012-09-11 14:39:39 +0530319 val = SESSEND | IDDIG;
320 omap4_usb_phy_mailbox(glue, val);
Hema HK594632e2010-12-10 18:10:51 +0530321 break;
322 default:
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530323 dev_dbg(dev, "ID float\n");
Hema HK594632e2010-12-10 18:10:51 +0530324 }
Hema HK594632e2010-12-10 18:10:51 +0530325}
326
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530327
328static void omap_musb_mailbox_work(struct work_struct *mailbox_work)
329{
330 struct omap2430_glue *glue = container_of(mailbox_work,
331 struct omap2430_glue, omap_musb_mailbox_work);
332 omap_musb_set_mailbox(glue);
333}
334
Felipe Balbi743411b2010-12-01 13:22:05 +0200335static int omap2430_musb_init(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300336{
Shubhrajyoti Dad579692012-03-22 12:48:06 +0530337 u32 l;
338 int status = 0;
Hema Kalliguddiea65df52010-09-22 19:27:40 -0500339 struct device *dev = musb->controller;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530340 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
Hema Kalliguddiea65df52010-09-22 19:27:40 -0500341 struct musb_hdrc_platform_data *plat = dev->platform_data;
342 struct omap_musb_board_data *data = plat->board_data;
Felipe Balbi550a7372008-07-24 12:27:36 +0300343
David Brownell84e250f2009-03-31 12:30:04 -0700344 /* We require some kind of external transceiver, hooked
345 * up through ULPI. TWL4030-family PMICs include one,
346 * which needs a driver, drivers aren't always needed.
347 */
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530348 musb->xceiv = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530349 if (IS_ERR_OR_NULL(musb->xceiv)) {
David Brownell84e250f2009-03-31 12:30:04 -0700350 pr_err("HS USB OTG: no transceiver configured\n");
351 return -ENODEV;
352 }
353
Hema HK7acc6192011-02-28 14:19:34 +0530354 status = pm_runtime_get_sync(dev);
355 if (status < 0) {
Shubhrajyoti Dad579692012-03-22 12:48:06 +0530356 dev_err(dev, "pm_runtime_get_sync FAILED %d\n", status);
Hema HK7acc6192011-02-28 14:19:34 +0530357 goto err1;
358 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300359
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200360 l = musb_readl(musb->mregs, OTG_INTERFSEL);
Maulik Mankadde2e1b02010-03-12 10:29:07 +0200361
362 if (data->interface_type == MUSB_INTERFACE_UTMI) {
363 /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
364 l &= ~ULPI_12PIN; /* Disable ULPI */
365 l |= UTMI_8BIT; /* Enable UTMI */
366 } else {
367 l |= ULPI_12PIN;
368 }
369
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200370 musb_writel(musb->mregs, OTG_INTERFSEL, l);
Felipe Balbi550a7372008-07-24 12:27:36 +0300371
372 pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
373 "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200374 musb_readl(musb->mregs, OTG_REVISION),
375 musb_readl(musb->mregs, OTG_SYSCONFIG),
376 musb_readl(musb->mregs, OTG_SYSSTATUS),
377 musb_readl(musb->mregs, OTG_INTERFSEL),
378 musb_readl(musb->mregs, OTG_SIMENABLE));
Felipe Balbi550a7372008-07-24 12:27:36 +0300379
Felipe Balbi550a7372008-07-24 12:27:36 +0300380 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
381
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530382 if (glue->status != OMAP_MUSB_UNKNOWN)
383 omap_musb_set_mailbox(glue);
384
Grazvydas Ignotasc04352a2012-02-04 19:43:51 +0200385 pm_runtime_put_noidle(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +0300386 return 0;
Hema HK7acc6192011-02-28 14:19:34 +0530387
388err1:
Hema HK7acc6192011-02-28 14:19:34 +0530389 return status;
Felipe Balbi550a7372008-07-24 12:27:36 +0300390}
391
Hema HK002eda12011-02-17 12:06:10 +0530392static void omap2430_musb_enable(struct musb *musb)
393{
394 u8 devctl;
Kishon Vijay Abraham I5ec40592012-09-11 14:39:39 +0530395 u32 val;
Hema HK002eda12011-02-17 12:06:10 +0530396 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
397 struct device *dev = musb->controller;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530398 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
Hema HK002eda12011-02-17 12:06:10 +0530399 struct musb_hdrc_platform_data *pdata = dev->platform_data;
400 struct omap_musb_board_data *data = pdata->board_data;
401
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530402 switch (glue->status) {
Hema HK002eda12011-02-17 12:06:10 +0530403
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530404 case OMAP_MUSB_ID_GROUND:
Kishon Vijay Abraham I5ec40592012-09-11 14:39:39 +0530405 val = AVALID | VBUSVALID;
406 omap4_usb_phy_mailbox(glue, val);
Felipe Contreras08dec562011-12-19 22:17:50 +0200407 if (data->interface_type != MUSB_INTERFACE_UTMI)
408 break;
409 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
410 /* start the session */
411 devctl |= MUSB_DEVCTL_SESSION;
412 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
413 while (musb_readb(musb->mregs, MUSB_DEVCTL) &
414 MUSB_DEVCTL_BDEVICE) {
415 cpu_relax();
Hema HK002eda12011-02-17 12:06:10 +0530416
Felipe Contreras08dec562011-12-19 22:17:50 +0200417 if (time_after(jiffies, timeout)) {
418 dev_err(dev, "configured as A device timeout");
419 break;
Hema HK002eda12011-02-17 12:06:10 +0530420 }
421 }
422 break;
423
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530424 case OMAP_MUSB_VBUS_VALID:
Kishon Vijay Abraham I5ec40592012-09-11 14:39:39 +0530425 val = IDDIG | AVALID | VBUSVALID;
426 omap4_usb_phy_mailbox(glue, val);
Hema HK002eda12011-02-17 12:06:10 +0530427 break;
428
429 default:
430 break;
431 }
432}
433
434static void omap2430_musb_disable(struct musb *musb)
435{
Kishon Vijay Abraham I5ec40592012-09-11 14:39:39 +0530436 u32 val;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530437 struct device *dev = musb->controller;
438 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
439
Kishon Vijay Abraham I5ec40592012-09-11 14:39:39 +0530440 if (glue->status != OMAP_MUSB_UNKNOWN) {
441 val = SESSEND | IDDIG;
442 omap4_usb_phy_mailbox(glue, val);
443 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300444}
445
Felipe Balbi743411b2010-12-01 13:22:05 +0200446static int omap2430_musb_exit(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300447{
Johan Hovold19b9a832011-02-11 16:57:08 +0100448 del_timer_sync(&musb_idle_timer);
Felipe Balbi550a7372008-07-24 12:27:36 +0300449
Felipe Balbic20aebb2010-12-02 12:44:40 +0200450 omap2430_low_level_exit(musb);
Felipe Balbic20aebb2010-12-02 12:44:40 +0200451
Felipe Balbi550a7372008-07-24 12:27:36 +0300452 return 0;
453}
Felipe Balbi743411b2010-12-01 13:22:05 +0200454
Felipe Balbif7ec9432010-12-02 09:48:58 +0200455static const struct musb_platform_ops omap2430_ops = {
Felipe Balbi743411b2010-12-01 13:22:05 +0200456 .init = omap2430_musb_init,
457 .exit = omap2430_musb_exit,
458
Felipe Balbi743411b2010-12-01 13:22:05 +0200459 .set_mode = omap2430_musb_set_mode,
460 .try_idle = omap2430_musb_try_idle,
461
462 .set_vbus = omap2430_musb_set_vbus,
Hema HK002eda12011-02-17 12:06:10 +0530463
464 .enable = omap2430_musb_enable,
465 .disable = omap2430_musb_disable,
Felipe Balbi743411b2010-12-01 13:22:05 +0200466};
Felipe Balbidc098862010-12-01 15:01:11 +0200467
468static u64 omap2430_dmamask = DMA_BIT_MASK(32);
469
Felipe Balbie9e8c852012-01-26 12:40:23 +0200470static int __devinit omap2430_probe(struct platform_device *pdev)
Felipe Balbidc098862010-12-01 15:01:11 +0200471{
472 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
473 struct platform_device *musb;
Felipe Balbia3cee122010-12-02 09:27:29 +0200474 struct omap2430_glue *glue;
Kishon Vijay Abraham I5ec40592012-09-11 14:39:39 +0530475 struct resource *res;
Felipe Balbidc098862010-12-01 15:01:11 +0200476 int ret = -ENOMEM;
477
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530478 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
Felipe Balbia3cee122010-12-02 09:27:29 +0200479 if (!glue) {
480 dev_err(&pdev->dev, "failed to allocate glue context\n");
481 goto err0;
482 }
483
Felipe Balbidc098862010-12-01 15:01:11 +0200484 musb = platform_device_alloc("musb-hdrc", -1);
485 if (!musb) {
486 dev_err(&pdev->dev, "failed to allocate musb device\n");
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530487 goto err0;
Felipe Balbidc098862010-12-01 15:01:11 +0200488 }
489
490 musb->dev.parent = &pdev->dev;
491 musb->dev.dma_mask = &omap2430_dmamask;
492 musb->dev.coherent_dma_mask = omap2430_dmamask;
493
Felipe Balbia3cee122010-12-02 09:27:29 +0200494 glue->dev = &pdev->dev;
495 glue->musb = musb;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530496 glue->status = OMAP_MUSB_UNKNOWN;
Felipe Balbia3cee122010-12-02 09:27:29 +0200497
Kishon Vijay Abraham I5ec40592012-09-11 14:39:39 +0530498 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
499
500 glue->control_otghs = devm_request_and_ioremap(&pdev->dev, res);
501 if (glue->control_otghs == NULL)
502 dev_dbg(&pdev->dev, "Failed to obtain control memory\n");
503
Felipe Balbif7ec9432010-12-02 09:48:58 +0200504 pdata->platform_ops = &omap2430_ops;
505
Felipe Balbia3cee122010-12-02 09:27:29 +0200506 platform_set_drvdata(pdev, glue);
Felipe Balbidc098862010-12-01 15:01:11 +0200507
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530508 /*
509 * REVISIT if we ever have two instances of the wrapper, we will be
510 * in big trouble
511 */
512 _glue = glue;
513
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530514 INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work);
515
Felipe Balbidc098862010-12-01 15:01:11 +0200516 ret = platform_device_add_resources(musb, pdev->resource,
517 pdev->num_resources);
518 if (ret) {
519 dev_err(&pdev->dev, "failed to add resources\n");
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530520 goto err1;
Felipe Balbidc098862010-12-01 15:01:11 +0200521 }
522
523 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
524 if (ret) {
525 dev_err(&pdev->dev, "failed to add platform_data\n");
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530526 goto err1;
Felipe Balbidc098862010-12-01 15:01:11 +0200527 }
528
Kishon Vijay Abraham I3006dc82012-03-21 21:30:20 +0530529 pm_runtime_enable(&pdev->dev);
530
Felipe Balbidc098862010-12-01 15:01:11 +0200531 ret = platform_device_add(musb);
532 if (ret) {
533 dev_err(&pdev->dev, "failed to register musb device\n");
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530534 goto err1;
Felipe Balbidc098862010-12-01 15:01:11 +0200535 }
536
537 return 0;
538
Felipe Balbia3cee122010-12-02 09:27:29 +0200539err1:
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530540 platform_device_put(musb);
Felipe Balbia3cee122010-12-02 09:27:29 +0200541
Felipe Balbidc098862010-12-01 15:01:11 +0200542err0:
543 return ret;
544}
545
Felipe Balbie9e8c852012-01-26 12:40:23 +0200546static int __devexit omap2430_remove(struct platform_device *pdev)
Felipe Balbidc098862010-12-01 15:01:11 +0200547{
Felipe Balbia3cee122010-12-02 09:27:29 +0200548 struct omap2430_glue *glue = platform_get_drvdata(pdev);
Felipe Balbidc098862010-12-01 15:01:11 +0200549
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530550 cancel_work_sync(&glue->omap_musb_mailbox_work);
Kishon Vijay Abraham If039df52012-08-02 15:30:07 +0530551 platform_device_unregister(glue->musb);
Felipe Balbidc098862010-12-01 15:01:11 +0200552
553 return 0;
554}
555
Felipe Balbic20aebb2010-12-02 12:44:40 +0200556#ifdef CONFIG_PM
Felipe Balbic20aebb2010-12-02 12:44:40 +0200557
Hema HK7acc6192011-02-28 14:19:34 +0530558static int omap2430_runtime_suspend(struct device *dev)
Felipe Balbic20aebb2010-12-02 12:44:40 +0200559{
560 struct omap2430_glue *glue = dev_get_drvdata(dev);
561 struct musb *musb = glue_to_musb(glue);
562
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200563 if (musb) {
564 musb->context.otg_interfsel = musb_readl(musb->mregs,
565 OTG_INTERFSEL);
Hema HKe25bec12011-09-07 09:19:24 -0700566
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200567 omap2430_low_level_exit(musb);
568 usb_phy_set_suspend(musb->xceiv, 1);
569 }
Felipe Balbic20aebb2010-12-02 12:44:40 +0200570
571 return 0;
572}
573
Hema HK7acc6192011-02-28 14:19:34 +0530574static int omap2430_runtime_resume(struct device *dev)
Felipe Balbic20aebb2010-12-02 12:44:40 +0200575{
576 struct omap2430_glue *glue = dev_get_drvdata(dev);
577 struct musb *musb = glue_to_musb(glue);
Felipe Balbic20aebb2010-12-02 12:44:40 +0200578
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200579 if (musb) {
580 omap2430_low_level_init(musb);
581 musb_writel(musb->mregs, OTG_INTERFSEL,
582 musb->context.otg_interfsel);
Hema HKe25bec12011-09-07 09:19:24 -0700583
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200584 usb_phy_set_suspend(musb->xceiv, 0);
585 }
Felipe Balbic20aebb2010-12-02 12:44:40 +0200586
587 return 0;
588}
589
590static struct dev_pm_ops omap2430_pm_ops = {
Hema HK7acc6192011-02-28 14:19:34 +0530591 .runtime_suspend = omap2430_runtime_suspend,
592 .runtime_resume = omap2430_runtime_resume,
Felipe Balbic20aebb2010-12-02 12:44:40 +0200593};
594
595#define DEV_PM_OPS (&omap2430_pm_ops)
596#else
597#define DEV_PM_OPS NULL
598#endif
599
Felipe Balbidc098862010-12-01 15:01:11 +0200600static struct platform_driver omap2430_driver = {
Felipe Balbie9e8c852012-01-26 12:40:23 +0200601 .probe = omap2430_probe,
602 .remove = __devexit_p(omap2430_remove),
Felipe Balbidc098862010-12-01 15:01:11 +0200603 .driver = {
604 .name = "musb-omap2430",
Felipe Balbic20aebb2010-12-02 12:44:40 +0200605 .pm = DEV_PM_OPS,
Felipe Balbidc098862010-12-01 15:01:11 +0200606 },
607};
608
609MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
610MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
611MODULE_LICENSE("GPL v2");
612
613static int __init omap2430_init(void)
614{
Felipe Balbie9e8c852012-01-26 12:40:23 +0200615 return platform_driver_register(&omap2430_driver);
Felipe Balbidc098862010-12-01 15:01:11 +0200616}
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530617subsys_initcall(omap2430_init);
Felipe Balbidc098862010-12-01 15:01:11 +0200618
619static void __exit omap2430_exit(void)
620{
621 platform_driver_unregister(&omap2430_driver);
622}
623module_exit(omap2430_exit);