blob: 4315d351fc7dbb4e026a0dce65dd87b761f55561 [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>
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +053033#include <linux/of.h>
Felipe Balbidc098862010-12-01 15:01:11 +020034#include <linux/platform_device.h>
35#include <linux/dma-mapping.h>
Hema HK207b0e12011-02-17 12:07:22 +053036#include <linux/pm_runtime.h>
37#include <linux/err.h>
NeilBrown12a19b52012-08-13 12:32:58 +100038#include <linux/delay.h>
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +053039#include <linux/usb/musb-omap.h>
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +053040#include <linux/usb/omap_control_usb.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030041
Felipe Balbi550a7372008-07-24 12:27:36 +030042#include "musb_core.h"
43#include "omap2430.h"
44
Felipe Balbia3cee122010-12-02 09:27:29 +020045struct omap2430_glue {
46 struct device *dev;
47 struct platform_device *musb;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +053048 enum omap_musb_vbus_id_status status;
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +053049 struct work_struct omap_musb_mailbox_work;
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +053050 struct device *control_otghs;
Felipe Balbia3cee122010-12-02 09:27:29 +020051};
Felipe Balbic20aebb2010-12-02 12:44:40 +020052#define glue_to_musb(g) platform_get_drvdata(g->musb)
Felipe Balbia3cee122010-12-02 09:27:29 +020053
Aaro Koskinen4b58ed12013-03-05 13:04:24 +020054static struct omap2430_glue *_glue;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +053055
Felipe Balbi550a7372008-07-24 12:27:36 +030056static struct timer_list musb_idle_timer;
57
58static void musb_do_idle(unsigned long _musb)
59{
60 struct musb *musb = (void *)_musb;
61 unsigned long flags;
62 u8 power;
Felipe Balbi550a7372008-07-24 12:27:36 +030063 u8 devctl;
64
Felipe Balbi550a7372008-07-24 12:27:36 +030065 spin_lock_irqsave(&musb->lock, flags);
66
David Brownell84e250f2009-03-31 12:30:04 -070067 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +030068 case OTG_STATE_A_WAIT_BCON:
Felipe Balbi550a7372008-07-24 12:27:36 +030069
70 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
71 if (devctl & MUSB_DEVCTL_BDEVICE) {
David Brownell84e250f2009-03-31 12:30:04 -070072 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +030073 MUSB_DEV_MODE(musb);
74 } else {
David Brownell84e250f2009-03-31 12:30:04 -070075 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +030076 MUSB_HST_MODE(musb);
77 }
78 break;
Felipe Balbi550a7372008-07-24 12:27:36 +030079 case OTG_STATE_A_SUSPEND:
80 /* finish RESUME signaling? */
81 if (musb->port1_status & MUSB_PORT_STAT_RESUME) {
82 power = musb_readb(musb->mregs, MUSB_POWER);
83 power &= ~MUSB_POWER_RESUME;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +030084 dev_dbg(musb->controller, "root port resume stopped, power %02x\n", power);
Felipe Balbi550a7372008-07-24 12:27:36 +030085 musb_writeb(musb->mregs, MUSB_POWER, power);
86 musb->is_active = 1;
87 musb->port1_status &= ~(USB_PORT_STAT_SUSPEND
88 | MUSB_PORT_STAT_RESUME);
89 musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
Daniel Mack8b125df2013-04-10 21:55:50 +020090 usb_hcd_poll_rh_status(musb->hcd);
Felipe Balbi550a7372008-07-24 12:27:36 +030091 /* NOTE: it might really be A_WAIT_BCON ... */
David Brownell84e250f2009-03-31 12:30:04 -070092 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +030093 }
94 break;
Felipe Balbi550a7372008-07-24 12:27:36 +030095 case OTG_STATE_A_HOST:
96 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
97 if (devctl & MUSB_DEVCTL_BDEVICE)
David Brownell84e250f2009-03-31 12:30:04 -070098 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +030099 else
David Brownell84e250f2009-03-31 12:30:04 -0700100 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
Felipe Balbi550a7372008-07-24 12:27:36 +0300101 default:
102 break;
103 }
104 spin_unlock_irqrestore(&musb->lock, flags);
105}
106
107
Felipe Balbi743411b2010-12-01 13:22:05 +0200108static void omap2430_musb_try_idle(struct musb *musb, unsigned long timeout)
Felipe Balbi550a7372008-07-24 12:27:36 +0300109{
110 unsigned long default_timeout = jiffies + msecs_to_jiffies(3);
111 static unsigned long last_timer;
112
113 if (timeout == 0)
114 timeout = default_timeout;
115
116 /* Never idle if active, or when VBUS timeout is not set as host */
117 if (musb->is_active || ((musb->a_wait_bcon == 0)
David Brownell84e250f2009-03-31 12:30:04 -0700118 && (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300119 dev_dbg(musb->controller, "%s active, deleting timer\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200120 usb_otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300121 del_timer(&musb_idle_timer);
122 last_timer = jiffies;
123 return;
124 }
125
126 if (time_after(last_timer, timeout)) {
127 if (!timer_pending(&musb_idle_timer))
128 last_timer = timeout;
129 else {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300130 dev_dbg(musb->controller, "Longer idle timer already pending, ignoring\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300131 return;
132 }
133 }
134 last_timer = timeout;
135
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300136 dev_dbg(musb->controller, "%s inactive, for idle timer for %lu ms\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200137 usb_otg_state_string(musb->xceiv->state),
Felipe Balbi550a7372008-07-24 12:27:36 +0300138 (unsigned long)jiffies_to_msecs(timeout - jiffies));
139 mod_timer(&musb_idle_timer, timeout);
140}
141
Felipe Balbi743411b2010-12-01 13:22:05 +0200142static void omap2430_musb_set_vbus(struct musb *musb, int is_on)
Felipe Balbi550a7372008-07-24 12:27:36 +0300143{
Heikki Krogerusd445b6d2012-02-13 13:24:15 +0200144 struct usb_otg *otg = musb->xceiv->otg;
Felipe Balbi550a7372008-07-24 12:27:36 +0300145 u8 devctl;
Hema HK594632e2010-12-10 18:10:51 +0530146 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Felipe Balbi550a7372008-07-24 12:27:36 +0300147 /* HDRC controls CPEN, but beware current surges during device
148 * connect. They can trigger transient overcurrent conditions
149 * that must be ignored.
150 */
151
152 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
153
154 if (is_on) {
Hema HK594632e2010-12-10 18:10:51 +0530155 if (musb->xceiv->state == OTG_STATE_A_IDLE) {
NeilBrown12a19b52012-08-13 12:32:58 +1000156 int loops = 100;
Hema HK594632e2010-12-10 18:10:51 +0530157 /* start the session */
158 devctl |= MUSB_DEVCTL_SESSION;
159 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
160 /*
161 * Wait for the musb to set as A device to enable the
162 * VBUS
163 */
164 while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300165
NeilBrown12a19b52012-08-13 12:32:58 +1000166 mdelay(5);
Hema HK594632e2010-12-10 18:10:51 +0530167 cpu_relax();
168
NeilBrown12a19b52012-08-13 12:32:58 +1000169 if (time_after(jiffies, timeout)
170 || loops-- <= 0) {
Hema HK594632e2010-12-10 18:10:51 +0530171 dev_err(musb->controller,
172 "configured as A device timeout");
Hema HK594632e2010-12-10 18:10:51 +0530173 break;
174 }
175 }
176
Kishon Vijay Abraham Ibb467cf2013-03-14 11:53:56 +0530177 otg_set_vbus(otg, 1);
Hema HK594632e2010-12-10 18:10:51 +0530178 } else {
179 musb->is_active = 1;
Heikki Krogerusd445b6d2012-02-13 13:24:15 +0200180 otg->default_a = 1;
Hema HK594632e2010-12-10 18:10:51 +0530181 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
182 devctl |= MUSB_DEVCTL_SESSION;
183 MUSB_HST_MODE(musb);
184 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300185 } else {
186 musb->is_active = 0;
187
188 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and
189 * jumping right to B_IDLE...
190 */
191
Heikki Krogerusd445b6d2012-02-13 13:24:15 +0200192 otg->default_a = 0;
David Brownell84e250f2009-03-31 12:30:04 -0700193 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300194 devctl &= ~MUSB_DEVCTL_SESSION;
195
196 MUSB_DEV_MODE(musb);
197 }
198 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
199
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300200 dev_dbg(musb->controller, "VBUS %s, devctl %02x "
Felipe Balbi550a7372008-07-24 12:27:36 +0300201 /* otg %3x conf %08x prcm %08x */ "\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200202 usb_otg_state_string(musb->xceiv->state),
Felipe Balbi550a7372008-07-24 12:27:36 +0300203 musb_readb(musb->mregs, MUSB_DEVCTL));
204}
Felipe Balbi550a7372008-07-24 12:27:36 +0300205
Felipe Balbi743411b2010-12-01 13:22:05 +0200206static int omap2430_musb_set_mode(struct musb *musb, u8 musb_mode)
Felipe Balbi550a7372008-07-24 12:27:36 +0300207{
208 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
209
210 devctl |= MUSB_DEVCTL_SESSION;
211 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
212
David Brownell96a274d2008-11-24 13:06:47 +0200213 return 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300214}
215
Felipe Balbic20aebb2010-12-02 12:44:40 +0200216static inline void omap2430_low_level_exit(struct musb *musb)
217{
218 u32 l;
219
220 /* in any role */
221 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
222 l |= ENABLEFORCE; /* enable MSTANDBY */
223 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
Felipe Balbic20aebb2010-12-02 12:44:40 +0200224}
225
226static inline void omap2430_low_level_init(struct musb *musb)
227{
228 u32 l;
229
Felipe Balbic20aebb2010-12-02 12:44:40 +0200230 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
231 l &= ~ENABLEFORCE; /* disable MSTANDBY */
232 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
233}
234
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530235void omap_musb_mailbox(enum omap_musb_vbus_id_status status)
Hema HK594632e2010-12-10 18:10:51 +0530236{
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530237 struct omap2430_glue *glue = _glue;
Vikram Pandita712d8ef2011-08-12 07:38:51 -0700238
Aaro Koskinenf8c4b0e2013-03-05 13:04:23 +0200239 if (!glue) {
240 pr_err("%s: musb core is not yet initialized\n", __func__);
241 return;
242 }
243 glue->status = status;
244
245 if (!glue_to_musb(glue)) {
Aaro Koskinen80ab72e2012-12-23 01:24:51 +0200246 pr_err("%s: musb core is not yet ready\n", __func__);
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530247 return;
248 }
249
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530250 schedule_work(&glue->omap_musb_mailbox_work);
Vikram Pandita712d8ef2011-08-12 07:38:51 -0700251}
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530252EXPORT_SYMBOL_GPL(omap_musb_mailbox);
Vikram Pandita712d8ef2011-08-12 07:38:51 -0700253
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530254static void omap_musb_set_mailbox(struct omap2430_glue *glue)
Vikram Pandita712d8ef2011-08-12 07:38:51 -0700255{
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530256 struct musb *musb = glue_to_musb(glue);
Hema HK594632e2010-12-10 18:10:51 +0530257 struct device *dev = musb->controller;
258 struct musb_hdrc_platform_data *pdata = dev->platform_data;
259 struct omap_musb_board_data *data = pdata->board_data;
Kishon Vijay Abraham Ic83a8542012-06-22 17:40:53 +0530260 struct usb_otg *otg = musb->xceiv->otg;
Hema HK594632e2010-12-10 18:10:51 +0530261
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530262 switch (glue->status) {
263 case OMAP_MUSB_ID_GROUND:
264 dev_dbg(dev, "ID GND\n");
Hema HK594632e2010-12-10 18:10:51 +0530265
Kishon Vijay Abraham Ic83a8542012-06-22 17:40:53 +0530266 otg->default_a = true;
267 musb->xceiv->state = OTG_STATE_A_IDLE;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530268 musb->xceiv->last_event = USB_EVENT_ID;
Felipe Balbi032ec492011-11-24 15:46:26 +0200269 if (musb->gadget_driver) {
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530270 pm_runtime_get_sync(dev);
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530271 omap_control_usb_set_mode(glue->control_otghs,
272 USB_MODE_HOST);
Hema HK70045c52011-02-28 15:05:29 +0530273 omap2430_musb_set_vbus(musb, 1);
Hema HK594632e2010-12-10 18:10:51 +0530274 }
275 break;
276
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530277 case OMAP_MUSB_VBUS_VALID:
278 dev_dbg(dev, "VBUS Connect\n");
Hema HK594632e2010-12-10 18:10:51 +0530279
Kishon Vijay Abraham Ic83a8542012-06-22 17:40:53 +0530280 otg->default_a = false;
281 musb->xceiv->state = OTG_STATE_B_IDLE;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530282 musb->xceiv->last_event = USB_EVENT_VBUS;
Hema HK7acc6192011-02-28 14:19:34 +0530283 if (musb->gadget_driver)
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530284 pm_runtime_get_sync(dev);
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530285 omap_control_usb_set_mode(glue->control_otghs, USB_MODE_DEVICE);
Hema HK594632e2010-12-10 18:10:51 +0530286 break;
287
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530288 case OMAP_MUSB_ID_FLOAT:
289 case OMAP_MUSB_VBUS_OFF:
290 dev_dbg(dev, "VBUS Disconnect\n");
Hema HK594632e2010-12-10 18:10:51 +0530291
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530292 musb->xceiv->last_event = USB_EVENT_NONE;
Felipe Balbi032ec492011-11-24 15:46:26 +0200293 if (musb->gadget_driver) {
Grazvydas Ignotas2c1fe892013-03-24 17:36:54 +0200294 omap2430_musb_set_vbus(musb, 0);
Felipe Balbi032ec492011-11-24 15:46:26 +0200295 pm_runtime_mark_last_busy(dev);
296 pm_runtime_put_autosuspend(dev);
297 }
Hema HK7acc6192011-02-28 14:19:34 +0530298
Kishon Vijay Abraham Ibb467cf2013-03-14 11:53:56 +0530299 if (data->interface_type == MUSB_INTERFACE_UTMI)
300 otg_set_vbus(musb->xceiv->otg, 0);
301
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530302 omap_control_usb_set_mode(glue->control_otghs,
303 USB_MODE_DISCONNECT);
Hema HK594632e2010-12-10 18:10:51 +0530304 break;
305 default:
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530306 dev_dbg(dev, "ID float\n");
Hema HK594632e2010-12-10 18:10:51 +0530307 }
Hema HK594632e2010-12-10 18:10:51 +0530308}
309
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530310
311static void omap_musb_mailbox_work(struct work_struct *mailbox_work)
312{
313 struct omap2430_glue *glue = container_of(mailbox_work,
314 struct omap2430_glue, omap_musb_mailbox_work);
315 omap_musb_set_mailbox(glue);
316}
317
Philippe De Swertbaef6532012-11-06 15:32:13 +0200318static irqreturn_t omap2430_musb_interrupt(int irq, void *__hci)
319{
320 unsigned long flags;
321 irqreturn_t retval = IRQ_NONE;
322 struct musb *musb = __hci;
323
324 spin_lock_irqsave(&musb->lock, flags);
325
326 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
327 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
328 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
329
330 if (musb->int_usb || musb->int_tx || musb->int_rx)
331 retval = musb_interrupt(musb);
332
333 spin_unlock_irqrestore(&musb->lock, flags);
334
335 return retval;
336}
337
Felipe Balbi743411b2010-12-01 13:22:05 +0200338static int omap2430_musb_init(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300339{
Shubhrajyoti Dad579692012-03-22 12:48:06 +0530340 u32 l;
341 int status = 0;
Hema Kalliguddiea65df52010-09-22 19:27:40 -0500342 struct device *dev = musb->controller;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530343 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
Hema Kalliguddiea65df52010-09-22 19:27:40 -0500344 struct musb_hdrc_platform_data *plat = dev->platform_data;
345 struct omap_musb_board_data *data = plat->board_data;
Felipe Balbi550a7372008-07-24 12:27:36 +0300346
David Brownell84e250f2009-03-31 12:30:04 -0700347 /* We require some kind of external transceiver, hooked
348 * up through ULPI. TWL4030-family PMICs include one,
349 * which needs a driver, drivers aren't always needed.
350 */
Kishon Vijay Abraham Ib16604f2013-01-25 08:03:26 +0530351 if (dev->parent->of_node)
352 musb->xceiv = devm_usb_get_phy_by_phandle(dev->parent,
353 "usb-phy", 0);
354 else
355 musb->xceiv = devm_usb_get_phy_dev(dev, 0);
356
Felipe Balbia90199b2013-03-15 10:57:40 +0200357 if (IS_ERR(musb->xceiv)) {
358 status = PTR_ERR(musb->xceiv);
359
360 if (status == -ENXIO)
361 return status;
362
David Brownell84e250f2009-03-31 12:30:04 -0700363 pr_err("HS USB OTG: no transceiver configured\n");
Ming Lei25736e02013-01-04 23:13:58 +0800364 return -EPROBE_DEFER;
David Brownell84e250f2009-03-31 12:30:04 -0700365 }
366
Philippe De Swertbaef6532012-11-06 15:32:13 +0200367 musb->isr = omap2430_musb_interrupt;
368
Hema HK7acc6192011-02-28 14:19:34 +0530369 status = pm_runtime_get_sync(dev);
370 if (status < 0) {
Shubhrajyoti Dad579692012-03-22 12:48:06 +0530371 dev_err(dev, "pm_runtime_get_sync FAILED %d\n", status);
Hema HK7acc6192011-02-28 14:19:34 +0530372 goto err1;
373 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300374
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200375 l = musb_readl(musb->mregs, OTG_INTERFSEL);
Maulik Mankadde2e1b02010-03-12 10:29:07 +0200376
377 if (data->interface_type == MUSB_INTERFACE_UTMI) {
378 /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
379 l &= ~ULPI_12PIN; /* Disable ULPI */
380 l |= UTMI_8BIT; /* Enable UTMI */
381 } else {
382 l |= ULPI_12PIN;
383 }
384
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200385 musb_writel(musb->mregs, OTG_INTERFSEL, l);
Felipe Balbi550a7372008-07-24 12:27:36 +0300386
387 pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
388 "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200389 musb_readl(musb->mregs, OTG_REVISION),
390 musb_readl(musb->mregs, OTG_SYSCONFIG),
391 musb_readl(musb->mregs, OTG_SYSSTATUS),
392 musb_readl(musb->mregs, OTG_INTERFSEL),
393 musb_readl(musb->mregs, OTG_SIMENABLE));
Felipe Balbi550a7372008-07-24 12:27:36 +0300394
Felipe Balbi550a7372008-07-24 12:27:36 +0300395 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
396
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530397 if (glue->status != OMAP_MUSB_UNKNOWN)
398 omap_musb_set_mailbox(glue);
399
Kishon Vijay Abraham I3bf6db9b2013-03-14 11:53:58 +0530400 usb_phy_init(musb->xceiv);
401
Grazvydas Ignotasc04352a2012-02-04 19:43:51 +0200402 pm_runtime_put_noidle(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +0300403 return 0;
Hema HK7acc6192011-02-28 14:19:34 +0530404
405err1:
Hema HK7acc6192011-02-28 14:19:34 +0530406 return status;
Felipe Balbi550a7372008-07-24 12:27:36 +0300407}
408
Hema HK002eda12011-02-17 12:06:10 +0530409static void omap2430_musb_enable(struct musb *musb)
410{
411 u8 devctl;
412 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
413 struct device *dev = musb->controller;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530414 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
Hema HK002eda12011-02-17 12:06:10 +0530415 struct musb_hdrc_platform_data *pdata = dev->platform_data;
416 struct omap_musb_board_data *data = pdata->board_data;
417
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530418 switch (glue->status) {
Hema HK002eda12011-02-17 12:06:10 +0530419
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530420 case OMAP_MUSB_ID_GROUND:
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530421 omap_control_usb_set_mode(glue->control_otghs, USB_MODE_HOST);
Felipe Contreras08dec562011-12-19 22:17:50 +0200422 if (data->interface_type != MUSB_INTERFACE_UTMI)
423 break;
424 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
425 /* start the session */
426 devctl |= MUSB_DEVCTL_SESSION;
427 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
428 while (musb_readb(musb->mregs, MUSB_DEVCTL) &
429 MUSB_DEVCTL_BDEVICE) {
430 cpu_relax();
Hema HK002eda12011-02-17 12:06:10 +0530431
Felipe Contreras08dec562011-12-19 22:17:50 +0200432 if (time_after(jiffies, timeout)) {
433 dev_err(dev, "configured as A device timeout");
434 break;
Hema HK002eda12011-02-17 12:06:10 +0530435 }
436 }
437 break;
438
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530439 case OMAP_MUSB_VBUS_VALID:
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530440 omap_control_usb_set_mode(glue->control_otghs, USB_MODE_DEVICE);
Hema HK002eda12011-02-17 12:06:10 +0530441 break;
442
443 default:
444 break;
445 }
446}
447
448static void omap2430_musb_disable(struct musb *musb)
449{
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530450 struct device *dev = musb->controller;
451 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
452
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530453 if (glue->status != OMAP_MUSB_UNKNOWN)
454 omap_control_usb_set_mode(glue->control_otghs,
455 USB_MODE_DISCONNECT);
Felipe Balbi550a7372008-07-24 12:27:36 +0300456}
457
Felipe Balbi743411b2010-12-01 13:22:05 +0200458static int omap2430_musb_exit(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300459{
Johan Hovold19b9a832011-02-11 16:57:08 +0100460 del_timer_sync(&musb_idle_timer);
Felipe Balbi550a7372008-07-24 12:27:36 +0300461
Felipe Balbic20aebb2010-12-02 12:44:40 +0200462 omap2430_low_level_exit(musb);
Felipe Balbic20aebb2010-12-02 12:44:40 +0200463
Felipe Balbi550a7372008-07-24 12:27:36 +0300464 return 0;
465}
Felipe Balbi743411b2010-12-01 13:22:05 +0200466
Felipe Balbif7ec9432010-12-02 09:48:58 +0200467static const struct musb_platform_ops omap2430_ops = {
Felipe Balbi743411b2010-12-01 13:22:05 +0200468 .init = omap2430_musb_init,
469 .exit = omap2430_musb_exit,
470
Felipe Balbi743411b2010-12-01 13:22:05 +0200471 .set_mode = omap2430_musb_set_mode,
472 .try_idle = omap2430_musb_try_idle,
473
474 .set_vbus = omap2430_musb_set_vbus,
Hema HK002eda12011-02-17 12:06:10 +0530475
476 .enable = omap2430_musb_enable,
477 .disable = omap2430_musb_disable,
Felipe Balbi743411b2010-12-01 13:22:05 +0200478};
Felipe Balbidc098862010-12-01 15:01:11 +0200479
480static u64 omap2430_dmamask = DMA_BIT_MASK(32);
481
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500482static int omap2430_probe(struct platform_device *pdev)
Felipe Balbidc098862010-12-01 15:01:11 +0200483{
Sebastian Andrzej Siewior3a0ddc72013-06-25 09:41:02 +0200484 struct resource musb_resources[2];
Felipe Balbidc098862010-12-01 15:01:11 +0200485 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530486 struct omap_musb_board_data *data;
Felipe Balbidc098862010-12-01 15:01:11 +0200487 struct platform_device *musb;
Felipe Balbia3cee122010-12-02 09:27:29 +0200488 struct omap2430_glue *glue;
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530489 struct device_node *np = pdev->dev.of_node;
490 struct musb_hdrc_config *config;
Felipe Balbidc098862010-12-01 15:01:11 +0200491 int ret = -ENOMEM;
492
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530493 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
Felipe Balbia3cee122010-12-02 09:27:29 +0200494 if (!glue) {
495 dev_err(&pdev->dev, "failed to allocate glue context\n");
496 goto err0;
497 }
498
Sebastian Andrzej Siewior2f771162012-10-31 16:12:43 +0100499 musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
500 if (!musb) {
501 dev_err(&pdev->dev, "failed to allocate musb device\n");
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530502 goto err0;
Felipe Balbidc098862010-12-01 15:01:11 +0200503 }
504
505 musb->dev.parent = &pdev->dev;
506 musb->dev.dma_mask = &omap2430_dmamask;
507 musb->dev.coherent_dma_mask = omap2430_dmamask;
508
Felipe Balbia3cee122010-12-02 09:27:29 +0200509 glue->dev = &pdev->dev;
510 glue->musb = musb;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530511 glue->status = OMAP_MUSB_UNKNOWN;
Felipe Balbia3cee122010-12-02 09:27:29 +0200512
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530513 if (np) {
514 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
515 if (!pdata) {
516 dev_err(&pdev->dev,
517 "failed to allocate musb platfrom data\n");
Sebastian Andrzej Siewior2f771162012-10-31 16:12:43 +0100518 goto err2;
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530519 }
520
521 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
522 if (!data) {
523 dev_err(&pdev->dev,
Sergei Shtylyov8df4ce72013-01-08 22:13:40 +0300524 "failed to allocate musb board data\n");
Sebastian Andrzej Siewior2f771162012-10-31 16:12:43 +0100525 goto err2;
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530526 }
527
528 config = devm_kzalloc(&pdev->dev, sizeof(*config), GFP_KERNEL);
Sergei Shtylyovb37457d2013-01-08 22:11:14 +0300529 if (!config) {
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530530 dev_err(&pdev->dev,
531 "failed to allocate musb hdrc config\n");
Sebastian Andrzej Siewior2f771162012-10-31 16:12:43 +0100532 goto err2;
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530533 }
534
535 of_property_read_u32(np, "mode", (u32 *)&pdata->mode);
Kishon Vijay Abraham Ieee44da2013-03-07 18:51:46 +0530536 of_property_read_u32(np, "interface-type",
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530537 (u32 *)&data->interface_type);
Kishon Vijay Abraham Ieee44da2013-03-07 18:51:46 +0530538 of_property_read_u32(np, "num-eps", (u32 *)&config->num_eps);
539 of_property_read_u32(np, "ram-bits", (u32 *)&config->ram_bits);
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530540 of_property_read_u32(np, "power", (u32 *)&pdata->power);
541 config->multipoint = of_property_read_bool(np, "multipoint");
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530542 pdata->has_mailbox = of_property_read_bool(np,
543 "ti,has-mailbox");
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530544
545 pdata->board_data = data;
546 pdata->config = config;
547 }
548
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530549 if (pdata->has_mailbox) {
550 glue->control_otghs = omap_get_control_dev();
551 if (IS_ERR(glue->control_otghs)) {
552 dev_vdbg(&pdev->dev, "Failed to get control device\n");
Wei Yongjun4b7e4502013-04-26 14:27:09 +0800553 ret = PTR_ERR(glue->control_otghs);
554 goto err2;
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530555 }
556 } else {
557 glue->control_otghs = ERR_PTR(-ENODEV);
558 }
Felipe Balbif7ec9432010-12-02 09:48:58 +0200559 pdata->platform_ops = &omap2430_ops;
560
Felipe Balbia3cee122010-12-02 09:27:29 +0200561 platform_set_drvdata(pdev, glue);
Felipe Balbidc098862010-12-01 15:01:11 +0200562
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530563 /*
564 * REVISIT if we ever have two instances of the wrapper, we will be
565 * in big trouble
566 */
567 _glue = glue;
568
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530569 INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work);
570
Sebastian Andrzej Siewior3a0ddc72013-06-25 09:41:02 +0200571 memset(musb_resources, 0x00, sizeof(*musb_resources) *
Felipe Balbi09fc7d22013-04-24 17:21:42 +0300572 ARRAY_SIZE(musb_resources));
573
574 musb_resources[0].name = pdev->resource[0].name;
575 musb_resources[0].start = pdev->resource[0].start;
576 musb_resources[0].end = pdev->resource[0].end;
577 musb_resources[0].flags = pdev->resource[0].flags;
578
579 musb_resources[1].name = pdev->resource[1].name;
580 musb_resources[1].start = pdev->resource[1].start;
581 musb_resources[1].end = pdev->resource[1].end;
582 musb_resources[1].flags = pdev->resource[1].flags;
583
584 ret = platform_device_add_resources(musb, musb_resources,
585 ARRAY_SIZE(musb_resources));
Felipe Balbidc098862010-12-01 15:01:11 +0200586 if (ret) {
587 dev_err(&pdev->dev, "failed to add resources\n");
B, Ravi65b3d522012-08-31 11:09:49 +0000588 goto err2;
Felipe Balbidc098862010-12-01 15:01:11 +0200589 }
590
591 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
592 if (ret) {
593 dev_err(&pdev->dev, "failed to add platform_data\n");
B, Ravi65b3d522012-08-31 11:09:49 +0000594 goto err2;
Felipe Balbidc098862010-12-01 15:01:11 +0200595 }
596
Kishon Vijay Abraham I3006dc82012-03-21 21:30:20 +0530597 pm_runtime_enable(&pdev->dev);
598
Felipe Balbidc098862010-12-01 15:01:11 +0200599 ret = platform_device_add(musb);
600 if (ret) {
601 dev_err(&pdev->dev, "failed to register musb device\n");
B, Ravi65b3d522012-08-31 11:09:49 +0000602 goto err2;
Felipe Balbidc098862010-12-01 15:01:11 +0200603 }
604
605 return 0;
606
B, Ravi65b3d522012-08-31 11:09:49 +0000607err2:
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530608 platform_device_put(musb);
Felipe Balbia3cee122010-12-02 09:27:29 +0200609
Felipe Balbidc098862010-12-01 15:01:11 +0200610err0:
611 return ret;
612}
613
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500614static int omap2430_remove(struct platform_device *pdev)
Felipe Balbidc098862010-12-01 15:01:11 +0200615{
Felipe Balbia3cee122010-12-02 09:27:29 +0200616 struct omap2430_glue *glue = platform_get_drvdata(pdev);
Felipe Balbidc098862010-12-01 15:01:11 +0200617
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530618 cancel_work_sync(&glue->omap_musb_mailbox_work);
Kishon Vijay Abraham If039df52012-08-02 15:30:07 +0530619 platform_device_unregister(glue->musb);
Felipe Balbidc098862010-12-01 15:01:11 +0200620
621 return 0;
622}
623
Felipe Balbic20aebb2010-12-02 12:44:40 +0200624#ifdef CONFIG_PM
Felipe Balbic20aebb2010-12-02 12:44:40 +0200625
Hema HK7acc6192011-02-28 14:19:34 +0530626static int omap2430_runtime_suspend(struct device *dev)
Felipe Balbic20aebb2010-12-02 12:44:40 +0200627{
628 struct omap2430_glue *glue = dev_get_drvdata(dev);
629 struct musb *musb = glue_to_musb(glue);
630
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200631 if (musb) {
632 musb->context.otg_interfsel = musb_readl(musb->mregs,
633 OTG_INTERFSEL);
Hema HKe25bec12011-09-07 09:19:24 -0700634
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200635 omap2430_low_level_exit(musb);
636 usb_phy_set_suspend(musb->xceiv, 1);
637 }
Felipe Balbic20aebb2010-12-02 12:44:40 +0200638
639 return 0;
640}
641
Hema HK7acc6192011-02-28 14:19:34 +0530642static int omap2430_runtime_resume(struct device *dev)
Felipe Balbic20aebb2010-12-02 12:44:40 +0200643{
644 struct omap2430_glue *glue = dev_get_drvdata(dev);
645 struct musb *musb = glue_to_musb(glue);
Felipe Balbic20aebb2010-12-02 12:44:40 +0200646
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200647 if (musb) {
648 omap2430_low_level_init(musb);
649 musb_writel(musb->mregs, OTG_INTERFSEL,
650 musb->context.otg_interfsel);
Hema HKe25bec12011-09-07 09:19:24 -0700651
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200652 usb_phy_set_suspend(musb->xceiv, 0);
653 }
Felipe Balbic20aebb2010-12-02 12:44:40 +0200654
655 return 0;
656}
657
658static struct dev_pm_ops omap2430_pm_ops = {
Hema HK7acc6192011-02-28 14:19:34 +0530659 .runtime_suspend = omap2430_runtime_suspend,
660 .runtime_resume = omap2430_runtime_resume,
Felipe Balbic20aebb2010-12-02 12:44:40 +0200661};
662
663#define DEV_PM_OPS (&omap2430_pm_ops)
664#else
665#define DEV_PM_OPS NULL
666#endif
667
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530668#ifdef CONFIG_OF
669static const struct of_device_id omap2430_id_table[] = {
670 {
671 .compatible = "ti,omap4-musb"
672 },
673 {
674 .compatible = "ti,omap3-musb"
675 },
676 {},
677};
678MODULE_DEVICE_TABLE(of, omap2430_id_table);
679#endif
680
Felipe Balbidc098862010-12-01 15:01:11 +0200681static struct platform_driver omap2430_driver = {
Felipe Balbie9e8c852012-01-26 12:40:23 +0200682 .probe = omap2430_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500683 .remove = omap2430_remove,
Felipe Balbidc098862010-12-01 15:01:11 +0200684 .driver = {
685 .name = "musb-omap2430",
Felipe Balbic20aebb2010-12-02 12:44:40 +0200686 .pm = DEV_PM_OPS,
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530687 .of_match_table = of_match_ptr(omap2430_id_table),
Felipe Balbidc098862010-12-01 15:01:11 +0200688 },
689};
690
691MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
692MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
693MODULE_LICENSE("GPL v2");
694
695static int __init omap2430_init(void)
696{
Felipe Balbie9e8c852012-01-26 12:40:23 +0200697 return platform_driver_register(&omap2430_driver);
Felipe Balbidc098862010-12-01 15:01:11 +0200698}
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530699subsys_initcall(omap2430_init);
Felipe Balbidc098862010-12-01 15:01:11 +0200700
701static void __exit omap2430_exit(void)
702{
703 platform_driver_unregister(&omap2430_driver);
704}
705module_exit(omap2430_exit);