blob: d0fc4d9c7b80fa0646ba6179a4a12c0ece0afb93 [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;
Jingoo Hanc1a7d672013-07-30 17:03:12 +0900258 struct musb_hdrc_platform_data *pdata = dev_get_platdata(dev);
Hema HK594632e2010-12-10 18:10:51 +0530259 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);
Jingoo Hanc1a7d672013-07-30 17:03:12 +0900344 struct musb_hdrc_platform_data *plat = dev_get_platdata(dev);
Hema Kalliguddiea65df52010-09-22 19:27:40 -0500345 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 I3e3101d2013-09-27 11:53:30 +0530351 if (dev->parent->of_node) {
352 musb->phy = devm_phy_get(dev->parent, "usb2-phy");
353
354 /* We can't totally remove musb->xceiv as of now because
355 * musb core uses xceiv.state and xceiv.otg. Once we have
356 * a separate state machine to handle otg, these can be moved
357 * out of xceiv and then we can start using the generic PHY
358 * framework
359 */
Kishon Vijay Abraham Ib16604f2013-01-25 08:03:26 +0530360 musb->xceiv = devm_usb_get_phy_by_phandle(dev->parent,
361 "usb-phy", 0);
Kishon Vijay Abraham I3e3101d2013-09-27 11:53:30 +0530362 } else {
Kishon Vijay Abraham Ib16604f2013-01-25 08:03:26 +0530363 musb->xceiv = devm_usb_get_phy_dev(dev, 0);
Kishon Vijay Abraham I3e3101d2013-09-27 11:53:30 +0530364 musb->phy = devm_phy_get(dev, "usb");
365 }
Kishon Vijay Abraham Ib16604f2013-01-25 08:03:26 +0530366
Felipe Balbia90199b2013-03-15 10:57:40 +0200367 if (IS_ERR(musb->xceiv)) {
368 status = PTR_ERR(musb->xceiv);
369
370 if (status == -ENXIO)
371 return status;
372
David Brownell84e250f2009-03-31 12:30:04 -0700373 pr_err("HS USB OTG: no transceiver configured\n");
Ming Lei25736e02013-01-04 23:13:58 +0800374 return -EPROBE_DEFER;
David Brownell84e250f2009-03-31 12:30:04 -0700375 }
376
Kishon Vijay Abraham I3e3101d2013-09-27 11:53:30 +0530377 if (IS_ERR(musb->phy)) {
378 pr_err("HS USB OTG: no PHY configured\n");
379 return PTR_ERR(musb->phy);
380 }
Philippe De Swertbaef6532012-11-06 15:32:13 +0200381 musb->isr = omap2430_musb_interrupt;
382
Hema HK7acc6192011-02-28 14:19:34 +0530383 status = pm_runtime_get_sync(dev);
384 if (status < 0) {
Shubhrajyoti Dad579692012-03-22 12:48:06 +0530385 dev_err(dev, "pm_runtime_get_sync FAILED %d\n", status);
Hema HK7acc6192011-02-28 14:19:34 +0530386 goto err1;
387 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300388
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200389 l = musb_readl(musb->mregs, OTG_INTERFSEL);
Maulik Mankadde2e1b02010-03-12 10:29:07 +0200390
391 if (data->interface_type == MUSB_INTERFACE_UTMI) {
392 /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
393 l &= ~ULPI_12PIN; /* Disable ULPI */
394 l |= UTMI_8BIT; /* Enable UTMI */
395 } else {
396 l |= ULPI_12PIN;
397 }
398
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200399 musb_writel(musb->mregs, OTG_INTERFSEL, l);
Felipe Balbi550a7372008-07-24 12:27:36 +0300400
401 pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
402 "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200403 musb_readl(musb->mregs, OTG_REVISION),
404 musb_readl(musb->mregs, OTG_SYSCONFIG),
405 musb_readl(musb->mregs, OTG_SYSSTATUS),
406 musb_readl(musb->mregs, OTG_INTERFSEL),
407 musb_readl(musb->mregs, OTG_SIMENABLE));
Felipe Balbi550a7372008-07-24 12:27:36 +0300408
Felipe Balbi550a7372008-07-24 12:27:36 +0300409 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
410
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530411 if (glue->status != OMAP_MUSB_UNKNOWN)
412 omap_musb_set_mailbox(glue);
413
Kishon Vijay Abraham I3e3101d2013-09-27 11:53:30 +0530414 phy_init(musb->phy);
Kishon Vijay Abraham I3bf6db9b2013-03-14 11:53:58 +0530415
Grazvydas Ignotasc04352a2012-02-04 19:43:51 +0200416 pm_runtime_put_noidle(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +0300417 return 0;
Hema HK7acc6192011-02-28 14:19:34 +0530418
419err1:
Hema HK7acc6192011-02-28 14:19:34 +0530420 return status;
Felipe Balbi550a7372008-07-24 12:27:36 +0300421}
422
Hema HK002eda12011-02-17 12:06:10 +0530423static void omap2430_musb_enable(struct musb *musb)
424{
425 u8 devctl;
426 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
427 struct device *dev = musb->controller;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530428 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
Jingoo Hanc1a7d672013-07-30 17:03:12 +0900429 struct musb_hdrc_platform_data *pdata = dev_get_platdata(dev);
Hema HK002eda12011-02-17 12:06:10 +0530430 struct omap_musb_board_data *data = pdata->board_data;
431
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530432 switch (glue->status) {
Hema HK002eda12011-02-17 12:06:10 +0530433
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530434 case OMAP_MUSB_ID_GROUND:
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530435 omap_control_usb_set_mode(glue->control_otghs, USB_MODE_HOST);
Felipe Contreras08dec562011-12-19 22:17:50 +0200436 if (data->interface_type != MUSB_INTERFACE_UTMI)
437 break;
438 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
439 /* start the session */
440 devctl |= MUSB_DEVCTL_SESSION;
441 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
442 while (musb_readb(musb->mregs, MUSB_DEVCTL) &
443 MUSB_DEVCTL_BDEVICE) {
444 cpu_relax();
Hema HK002eda12011-02-17 12:06:10 +0530445
Felipe Contreras08dec562011-12-19 22:17:50 +0200446 if (time_after(jiffies, timeout)) {
447 dev_err(dev, "configured as A device timeout");
448 break;
Hema HK002eda12011-02-17 12:06:10 +0530449 }
450 }
451 break;
452
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530453 case OMAP_MUSB_VBUS_VALID:
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530454 omap_control_usb_set_mode(glue->control_otghs, USB_MODE_DEVICE);
Hema HK002eda12011-02-17 12:06:10 +0530455 break;
456
457 default:
458 break;
459 }
460}
461
462static void omap2430_musb_disable(struct musb *musb)
463{
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530464 struct device *dev = musb->controller;
465 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
466
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530467 if (glue->status != OMAP_MUSB_UNKNOWN)
468 omap_control_usb_set_mode(glue->control_otghs,
469 USB_MODE_DISCONNECT);
Felipe Balbi550a7372008-07-24 12:27:36 +0300470}
471
Felipe Balbi743411b2010-12-01 13:22:05 +0200472static int omap2430_musb_exit(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300473{
Johan Hovold19b9a832011-02-11 16:57:08 +0100474 del_timer_sync(&musb_idle_timer);
Felipe Balbi550a7372008-07-24 12:27:36 +0300475
Felipe Balbic20aebb2010-12-02 12:44:40 +0200476 omap2430_low_level_exit(musb);
Kishon Vijay Abraham I3e3101d2013-09-27 11:53:30 +0530477 phy_exit(musb->phy);
Felipe Balbic20aebb2010-12-02 12:44:40 +0200478
Felipe Balbi550a7372008-07-24 12:27:36 +0300479 return 0;
480}
Felipe Balbi743411b2010-12-01 13:22:05 +0200481
Felipe Balbif7ec9432010-12-02 09:48:58 +0200482static const struct musb_platform_ops omap2430_ops = {
Felipe Balbi743411b2010-12-01 13:22:05 +0200483 .init = omap2430_musb_init,
484 .exit = omap2430_musb_exit,
485
Felipe Balbi743411b2010-12-01 13:22:05 +0200486 .set_mode = omap2430_musb_set_mode,
487 .try_idle = omap2430_musb_try_idle,
488
489 .set_vbus = omap2430_musb_set_vbus,
Hema HK002eda12011-02-17 12:06:10 +0530490
491 .enable = omap2430_musb_enable,
492 .disable = omap2430_musb_disable,
Felipe Balbi743411b2010-12-01 13:22:05 +0200493};
Felipe Balbidc098862010-12-01 15:01:11 +0200494
495static u64 omap2430_dmamask = DMA_BIT_MASK(32);
496
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500497static int omap2430_probe(struct platform_device *pdev)
Felipe Balbidc098862010-12-01 15:01:11 +0200498{
Kishon Vijay Abraham Ic1f01be2013-07-17 10:51:22 +0300499 struct resource musb_resources[3];
Jingoo Hanc1a7d672013-07-30 17:03:12 +0900500 struct musb_hdrc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530501 struct omap_musb_board_data *data;
Felipe Balbidc098862010-12-01 15:01:11 +0200502 struct platform_device *musb;
Felipe Balbia3cee122010-12-02 09:27:29 +0200503 struct omap2430_glue *glue;
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530504 struct device_node *np = pdev->dev.of_node;
505 struct musb_hdrc_config *config;
Felipe Balbidc098862010-12-01 15:01:11 +0200506 int ret = -ENOMEM;
507
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530508 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
Felipe Balbia3cee122010-12-02 09:27:29 +0200509 if (!glue) {
510 dev_err(&pdev->dev, "failed to allocate glue context\n");
511 goto err0;
512 }
513
Sebastian Andrzej Siewior2f771162012-10-31 16:12:43 +0100514 musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
515 if (!musb) {
516 dev_err(&pdev->dev, "failed to allocate musb device\n");
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530517 goto err0;
Felipe Balbidc098862010-12-01 15:01:11 +0200518 }
519
520 musb->dev.parent = &pdev->dev;
521 musb->dev.dma_mask = &omap2430_dmamask;
522 musb->dev.coherent_dma_mask = omap2430_dmamask;
523
Felipe Balbia3cee122010-12-02 09:27:29 +0200524 glue->dev = &pdev->dev;
525 glue->musb = musb;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530526 glue->status = OMAP_MUSB_UNKNOWN;
Felipe Balbia3cee122010-12-02 09:27:29 +0200527
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530528 if (np) {
529 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
530 if (!pdata) {
531 dev_err(&pdev->dev,
Masanari Iida8b513d02013-05-21 23:13:12 +0900532 "failed to allocate musb platform data\n");
Sebastian Andrzej Siewior2f771162012-10-31 16:12:43 +0100533 goto err2;
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530534 }
535
536 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
537 if (!data) {
538 dev_err(&pdev->dev,
Sergei Shtylyov8df4ce72013-01-08 22:13:40 +0300539 "failed to allocate musb board data\n");
Sebastian Andrzej Siewior2f771162012-10-31 16:12:43 +0100540 goto err2;
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530541 }
542
543 config = devm_kzalloc(&pdev->dev, sizeof(*config), GFP_KERNEL);
Sergei Shtylyovb37457d2013-01-08 22:11:14 +0300544 if (!config) {
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530545 dev_err(&pdev->dev,
546 "failed to allocate musb hdrc config\n");
Sebastian Andrzej Siewior2f771162012-10-31 16:12:43 +0100547 goto err2;
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530548 }
549
550 of_property_read_u32(np, "mode", (u32 *)&pdata->mode);
Kishon Vijay Abraham Ieee44da2013-03-07 18:51:46 +0530551 of_property_read_u32(np, "interface-type",
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530552 (u32 *)&data->interface_type);
Kishon Vijay Abraham Ieee44da2013-03-07 18:51:46 +0530553 of_property_read_u32(np, "num-eps", (u32 *)&config->num_eps);
554 of_property_read_u32(np, "ram-bits", (u32 *)&config->ram_bits);
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530555 of_property_read_u32(np, "power", (u32 *)&pdata->power);
556 config->multipoint = of_property_read_bool(np, "multipoint");
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530557 pdata->has_mailbox = of_property_read_bool(np,
558 "ti,has-mailbox");
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530559
560 pdata->board_data = data;
561 pdata->config = config;
562 }
563
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530564 if (pdata->has_mailbox) {
565 glue->control_otghs = omap_get_control_dev();
566 if (IS_ERR(glue->control_otghs)) {
567 dev_vdbg(&pdev->dev, "Failed to get control device\n");
Wei Yongjun4b7e4502013-04-26 14:27:09 +0800568 ret = PTR_ERR(glue->control_otghs);
569 goto err2;
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530570 }
571 } else {
572 glue->control_otghs = ERR_PTR(-ENODEV);
573 }
Felipe Balbif7ec9432010-12-02 09:48:58 +0200574 pdata->platform_ops = &omap2430_ops;
575
Felipe Balbia3cee122010-12-02 09:27:29 +0200576 platform_set_drvdata(pdev, glue);
Felipe Balbidc098862010-12-01 15:01:11 +0200577
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530578 /*
579 * REVISIT if we ever have two instances of the wrapper, we will be
580 * in big trouble
581 */
582 _glue = glue;
583
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530584 INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work);
585
Sebastian Andrzej Siewior3a0ddc72013-06-25 09:41:02 +0200586 memset(musb_resources, 0x00, sizeof(*musb_resources) *
Felipe Balbi09fc7d22013-04-24 17:21:42 +0300587 ARRAY_SIZE(musb_resources));
588
589 musb_resources[0].name = pdev->resource[0].name;
590 musb_resources[0].start = pdev->resource[0].start;
591 musb_resources[0].end = pdev->resource[0].end;
592 musb_resources[0].flags = pdev->resource[0].flags;
593
594 musb_resources[1].name = pdev->resource[1].name;
595 musb_resources[1].start = pdev->resource[1].start;
596 musb_resources[1].end = pdev->resource[1].end;
597 musb_resources[1].flags = pdev->resource[1].flags;
598
Kishon Vijay Abraham Ic1f01be2013-07-17 10:51:22 +0300599 musb_resources[2].name = pdev->resource[2].name;
600 musb_resources[2].start = pdev->resource[2].start;
601 musb_resources[2].end = pdev->resource[2].end;
602 musb_resources[2].flags = pdev->resource[2].flags;
603
Felipe Balbi09fc7d22013-04-24 17:21:42 +0300604 ret = platform_device_add_resources(musb, musb_resources,
605 ARRAY_SIZE(musb_resources));
Felipe Balbidc098862010-12-01 15:01:11 +0200606 if (ret) {
607 dev_err(&pdev->dev, "failed to add resources\n");
B, Ravi65b3d522012-08-31 11:09:49 +0000608 goto err2;
Felipe Balbidc098862010-12-01 15:01:11 +0200609 }
610
611 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
612 if (ret) {
613 dev_err(&pdev->dev, "failed to add platform_data\n");
B, Ravi65b3d522012-08-31 11:09:49 +0000614 goto err2;
Felipe Balbidc098862010-12-01 15:01:11 +0200615 }
616
Kishon Vijay Abraham I3006dc82012-03-21 21:30:20 +0530617 pm_runtime_enable(&pdev->dev);
618
Felipe Balbidc098862010-12-01 15:01:11 +0200619 ret = platform_device_add(musb);
620 if (ret) {
621 dev_err(&pdev->dev, "failed to register musb device\n");
B, Ravi65b3d522012-08-31 11:09:49 +0000622 goto err2;
Felipe Balbidc098862010-12-01 15:01:11 +0200623 }
624
625 return 0;
626
B, Ravi65b3d522012-08-31 11:09:49 +0000627err2:
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530628 platform_device_put(musb);
Felipe Balbia3cee122010-12-02 09:27:29 +0200629
Felipe Balbidc098862010-12-01 15:01:11 +0200630err0:
631 return ret;
632}
633
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500634static int omap2430_remove(struct platform_device *pdev)
Felipe Balbidc098862010-12-01 15:01:11 +0200635{
Felipe Balbia3cee122010-12-02 09:27:29 +0200636 struct omap2430_glue *glue = platform_get_drvdata(pdev);
Felipe Balbidc098862010-12-01 15:01:11 +0200637
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530638 cancel_work_sync(&glue->omap_musb_mailbox_work);
Kishon Vijay Abraham If039df52012-08-02 15:30:07 +0530639 platform_device_unregister(glue->musb);
Felipe Balbidc098862010-12-01 15:01:11 +0200640
641 return 0;
642}
643
Felipe Balbic20aebb2010-12-02 12:44:40 +0200644#ifdef CONFIG_PM
Felipe Balbic20aebb2010-12-02 12:44:40 +0200645
Hema HK7acc6192011-02-28 14:19:34 +0530646static int omap2430_runtime_suspend(struct device *dev)
Felipe Balbic20aebb2010-12-02 12:44:40 +0200647{
648 struct omap2430_glue *glue = dev_get_drvdata(dev);
649 struct musb *musb = glue_to_musb(glue);
650
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200651 if (musb) {
652 musb->context.otg_interfsel = musb_readl(musb->mregs,
653 OTG_INTERFSEL);
Hema HKe25bec12011-09-07 09:19:24 -0700654
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200655 omap2430_low_level_exit(musb);
Kishon Vijay Abraham I3e3101d2013-09-27 11:53:30 +0530656 phy_power_off(musb->phy);
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200657 }
Felipe Balbic20aebb2010-12-02 12:44:40 +0200658
659 return 0;
660}
661
Hema HK7acc6192011-02-28 14:19:34 +0530662static int omap2430_runtime_resume(struct device *dev)
Felipe Balbic20aebb2010-12-02 12:44:40 +0200663{
664 struct omap2430_glue *glue = dev_get_drvdata(dev);
665 struct musb *musb = glue_to_musb(glue);
Felipe Balbic20aebb2010-12-02 12:44:40 +0200666
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200667 if (musb) {
668 omap2430_low_level_init(musb);
669 musb_writel(musb->mregs, OTG_INTERFSEL,
670 musb->context.otg_interfsel);
Kishon Vijay Abraham I3e3101d2013-09-27 11:53:30 +0530671 phy_power_on(musb->phy);
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200672 }
Felipe Balbic20aebb2010-12-02 12:44:40 +0200673
674 return 0;
675}
676
677static struct dev_pm_ops omap2430_pm_ops = {
Hema HK7acc6192011-02-28 14:19:34 +0530678 .runtime_suspend = omap2430_runtime_suspend,
679 .runtime_resume = omap2430_runtime_resume,
Felipe Balbic20aebb2010-12-02 12:44:40 +0200680};
681
682#define DEV_PM_OPS (&omap2430_pm_ops)
683#else
684#define DEV_PM_OPS NULL
685#endif
686
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530687#ifdef CONFIG_OF
688static const struct of_device_id omap2430_id_table[] = {
689 {
690 .compatible = "ti,omap4-musb"
691 },
692 {
693 .compatible = "ti,omap3-musb"
694 },
695 {},
696};
697MODULE_DEVICE_TABLE(of, omap2430_id_table);
698#endif
699
Felipe Balbidc098862010-12-01 15:01:11 +0200700static struct platform_driver omap2430_driver = {
Felipe Balbie9e8c852012-01-26 12:40:23 +0200701 .probe = omap2430_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500702 .remove = omap2430_remove,
Felipe Balbidc098862010-12-01 15:01:11 +0200703 .driver = {
704 .name = "musb-omap2430",
Felipe Balbic20aebb2010-12-02 12:44:40 +0200705 .pm = DEV_PM_OPS,
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530706 .of_match_table = of_match_ptr(omap2430_id_table),
Felipe Balbidc098862010-12-01 15:01:11 +0200707 },
708};
709
710MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
711MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
712MODULE_LICENSE("GPL v2");
713
714static int __init omap2430_init(void)
715{
Felipe Balbie9e8c852012-01-26 12:40:23 +0200716 return platform_driver_register(&omap2430_driver);
Felipe Balbidc098862010-12-01 15:01:11 +0200717}
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530718subsys_initcall(omap2430_init);
Felipe Balbidc098862010-12-01 15:01:11 +0200719
720static void __exit omap2430_exit(void)
721{
722 platform_driver_unregister(&omap2430_driver);
723}
724module_exit(omap2430_exit);