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
6 * Tony Lindgren <tony@atomide.com>
8 * This file is part of the Inventra Controller Driver for Linux.
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
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.
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
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/init.h>
31 #include <linux/list.h>
33 #include <linux/platform_device.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/pm_runtime.h>
36 #include <linux/err.h>
37 #include <linux/usb/musb-omap.h>
39 #include "musb_core.h"
42 struct omap2430_glue {
44 struct platform_device *musb;
45 enum omap_musb_vbus_id_status status;
46 struct work_struct omap_musb_mailbox_work;
48 #define glue_to_musb(g) platform_get_drvdata(g->musb)
50 struct omap2430_glue *_glue;
52 static struct timer_list musb_idle_timer;
54 static void musb_do_idle(unsigned long _musb)
56 struct musb *musb = (void *)_musb;
61 spin_lock_irqsave(&musb->lock, flags);
63 switch (musb->xceiv->state) {
64 case OTG_STATE_A_WAIT_BCON:
66 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
67 if (devctl & MUSB_DEVCTL_BDEVICE) {
68 musb->xceiv->state = OTG_STATE_B_IDLE;
71 musb->xceiv->state = OTG_STATE_A_IDLE;
75 case OTG_STATE_A_SUSPEND:
76 /* finish RESUME signaling? */
77 if (musb->port1_status & MUSB_PORT_STAT_RESUME) {
78 power = musb_readb(musb->mregs, MUSB_POWER);
79 power &= ~MUSB_POWER_RESUME;
80 dev_dbg(musb->controller, "root port resume stopped, power %02x\n", power);
81 musb_writeb(musb->mregs, MUSB_POWER, power);
83 musb->port1_status &= ~(USB_PORT_STAT_SUSPEND
84 | MUSB_PORT_STAT_RESUME);
85 musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
86 usb_hcd_poll_rh_status(musb_to_hcd(musb));
87 /* NOTE: it might really be A_WAIT_BCON ... */
88 musb->xceiv->state = OTG_STATE_A_HOST;
91 case OTG_STATE_A_HOST:
92 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
93 if (devctl & MUSB_DEVCTL_BDEVICE)
94 musb->xceiv->state = OTG_STATE_B_IDLE;
96 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
100 spin_unlock_irqrestore(&musb->lock, flags);
104 static void omap2430_musb_try_idle(struct musb *musb, unsigned long timeout)
106 unsigned long default_timeout = jiffies + msecs_to_jiffies(3);
107 static unsigned long last_timer;
110 timeout = default_timeout;
112 /* Never idle if active, or when VBUS timeout is not set as host */
113 if (musb->is_active || ((musb->a_wait_bcon == 0)
114 && (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
115 dev_dbg(musb->controller, "%s active, deleting timer\n",
116 otg_state_string(musb->xceiv->state));
117 del_timer(&musb_idle_timer);
118 last_timer = jiffies;
122 if (time_after(last_timer, timeout)) {
123 if (!timer_pending(&musb_idle_timer))
124 last_timer = timeout;
126 dev_dbg(musb->controller, "Longer idle timer already pending, ignoring\n");
130 last_timer = timeout;
132 dev_dbg(musb->controller, "%s inactive, for idle timer for %lu ms\n",
133 otg_state_string(musb->xceiv->state),
134 (unsigned long)jiffies_to_msecs(timeout - jiffies));
135 mod_timer(&musb_idle_timer, timeout);
138 static void omap2430_musb_set_vbus(struct musb *musb, int is_on)
140 struct usb_otg *otg = musb->xceiv->otg;
142 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
144 /* HDRC controls CPEN, but beware current surges during device
145 * connect. They can trigger transient overcurrent conditions
146 * that must be ignored.
149 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
152 if (musb->xceiv->state == OTG_STATE_A_IDLE) {
153 /* start the session */
154 devctl |= MUSB_DEVCTL_SESSION;
155 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
157 * Wait for the musb to set as A device to enable the
160 while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
164 if (time_after(jiffies, timeout)) {
165 dev_err(musb->controller,
166 "configured as A device timeout");
172 if (ret && otg->set_vbus)
173 otg_set_vbus(otg, 1);
177 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
178 devctl |= MUSB_DEVCTL_SESSION;
184 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and
185 * jumping right to B_IDLE...
189 musb->xceiv->state = OTG_STATE_B_IDLE;
190 devctl &= ~MUSB_DEVCTL_SESSION;
194 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
196 dev_dbg(musb->controller, "VBUS %s, devctl %02x "
197 /* otg %3x conf %08x prcm %08x */ "\n",
198 otg_state_string(musb->xceiv->state),
199 musb_readb(musb->mregs, MUSB_DEVCTL));
202 static int omap2430_musb_set_mode(struct musb *musb, u8 musb_mode)
204 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
206 devctl |= MUSB_DEVCTL_SESSION;
207 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
212 static inline void omap2430_low_level_exit(struct musb *musb)
217 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
218 l |= ENABLEFORCE; /* enable MSTANDBY */
219 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
222 static inline void omap2430_low_level_init(struct musb *musb)
226 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
227 l &= ~ENABLEFORCE; /* disable MSTANDBY */
228 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
231 void omap_musb_mailbox(enum omap_musb_vbus_id_status status)
233 struct omap2430_glue *glue = _glue;
234 struct musb *musb = glue_to_musb(glue);
236 glue->status = status;
238 dev_err(glue->dev, "musb core is not yet ready\n");
242 schedule_work(&glue->omap_musb_mailbox_work);
244 EXPORT_SYMBOL_GPL(omap_musb_mailbox);
246 static void omap_musb_set_mailbox(struct omap2430_glue *glue)
248 struct musb *musb = glue_to_musb(glue);
249 struct device *dev = musb->controller;
250 struct musb_hdrc_platform_data *pdata = dev->platform_data;
251 struct omap_musb_board_data *data = pdata->board_data;
252 struct usb_otg *otg = musb->xceiv->otg;
254 switch (glue->status) {
255 case OMAP_MUSB_ID_GROUND:
256 dev_dbg(dev, "ID GND\n");
258 otg->default_a = true;
259 musb->xceiv->state = OTG_STATE_A_IDLE;
260 musb->xceiv->last_event = USB_EVENT_ID;
261 if (!is_otg_enabled(musb) || musb->gadget_driver) {
262 pm_runtime_get_sync(dev);
263 usb_phy_init(musb->xceiv);
264 omap2430_musb_set_vbus(musb, 1);
268 case OMAP_MUSB_VBUS_VALID:
269 dev_dbg(dev, "VBUS Connect\n");
271 otg->default_a = false;
272 musb->xceiv->state = OTG_STATE_B_IDLE;
273 musb->xceiv->last_event = USB_EVENT_VBUS;
274 if (musb->gadget_driver)
275 pm_runtime_get_sync(dev);
276 usb_phy_init(musb->xceiv);
279 case OMAP_MUSB_ID_FLOAT:
280 case OMAP_MUSB_VBUS_OFF:
281 dev_dbg(dev, "VBUS Disconnect\n");
283 musb->xceiv->last_event = USB_EVENT_NONE;
284 if (is_otg_enabled(musb) || is_peripheral_enabled(musb))
285 if (musb->gadget_driver) {
286 pm_runtime_mark_last_busy(dev);
287 pm_runtime_put_autosuspend(dev);
290 if (data->interface_type == MUSB_INTERFACE_UTMI) {
291 if (musb->xceiv->otg->set_vbus)
292 otg_set_vbus(musb->xceiv->otg, 0);
294 usb_phy_shutdown(musb->xceiv);
297 dev_dbg(dev, "ID float\n");
302 static void omap_musb_mailbox_work(struct work_struct *mailbox_work)
304 struct omap2430_glue *glue = container_of(mailbox_work,
305 struct omap2430_glue, omap_musb_mailbox_work);
306 omap_musb_set_mailbox(glue);
309 static int omap2430_musb_init(struct musb *musb)
313 struct device *dev = musb->controller;
314 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
315 struct musb_hdrc_platform_data *plat = dev->platform_data;
316 struct omap_musb_board_data *data = plat->board_data;
318 /* We require some kind of external transceiver, hooked
319 * up through ULPI. TWL4030-family PMICs include one,
320 * which needs a driver, drivers aren't always needed.
322 musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
324 pr_err("HS USB OTG: no transceiver configured\n");
328 status = pm_runtime_get_sync(dev);
330 dev_err(dev, "pm_runtime_get_sync FAILED %d\n", status);
334 l = musb_readl(musb->mregs, OTG_INTERFSEL);
336 if (data->interface_type == MUSB_INTERFACE_UTMI) {
337 /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
338 l &= ~ULPI_12PIN; /* Disable ULPI */
339 l |= UTMI_8BIT; /* Enable UTMI */
344 musb_writel(musb->mregs, OTG_INTERFSEL, l);
346 pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
347 "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
348 musb_readl(musb->mregs, OTG_REVISION),
349 musb_readl(musb->mregs, OTG_SYSCONFIG),
350 musb_readl(musb->mregs, OTG_SYSSTATUS),
351 musb_readl(musb->mregs, OTG_INTERFSEL),
352 musb_readl(musb->mregs, OTG_SIMENABLE));
354 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
356 if (glue->status != OMAP_MUSB_UNKNOWN)
357 omap_musb_set_mailbox(glue);
359 pm_runtime_put_noidle(musb->controller);
366 static void omap2430_musb_enable(struct musb *musb)
369 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
370 struct device *dev = musb->controller;
371 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
372 struct musb_hdrc_platform_data *pdata = dev->platform_data;
373 struct omap_musb_board_data *data = pdata->board_data;
375 switch (glue->status) {
377 case OMAP_MUSB_ID_GROUND:
378 usb_phy_init(musb->xceiv);
379 if (data->interface_type != MUSB_INTERFACE_UTMI)
381 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
382 /* start the session */
383 devctl |= MUSB_DEVCTL_SESSION;
384 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
385 while (musb_readb(musb->mregs, MUSB_DEVCTL) &
386 MUSB_DEVCTL_BDEVICE) {
389 if (time_after(jiffies, timeout)) {
390 dev_err(dev, "configured as A device timeout");
396 case OMAP_MUSB_VBUS_VALID:
397 usb_phy_init(musb->xceiv);
405 static void omap2430_musb_disable(struct musb *musb)
407 struct device *dev = musb->controller;
408 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
410 if (glue->status != OMAP_MUSB_UNKNOWN)
411 usb_phy_shutdown(musb->xceiv);
414 static int omap2430_musb_exit(struct musb *musb)
416 del_timer_sync(&musb_idle_timer);
418 omap2430_low_level_exit(musb);
419 usb_put_phy(musb->xceiv);
424 static const struct musb_platform_ops omap2430_ops = {
425 .init = omap2430_musb_init,
426 .exit = omap2430_musb_exit,
428 .set_mode = omap2430_musb_set_mode,
429 .try_idle = omap2430_musb_try_idle,
431 .set_vbus = omap2430_musb_set_vbus,
433 .enable = omap2430_musb_enable,
434 .disable = omap2430_musb_disable,
437 static u64 omap2430_dmamask = DMA_BIT_MASK(32);
439 static int __devinit omap2430_probe(struct platform_device *pdev)
441 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
442 struct platform_device *musb;
443 struct omap2430_glue *glue;
446 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
448 dev_err(&pdev->dev, "failed to allocate glue context\n");
452 musb = platform_device_alloc("musb-hdrc", -1);
454 dev_err(&pdev->dev, "failed to allocate musb device\n");
458 musb->dev.parent = &pdev->dev;
459 musb->dev.dma_mask = &omap2430_dmamask;
460 musb->dev.coherent_dma_mask = omap2430_dmamask;
462 glue->dev = &pdev->dev;
464 glue->status = OMAP_MUSB_UNKNOWN;
466 pdata->platform_ops = &omap2430_ops;
468 platform_set_drvdata(pdev, glue);
471 * REVISIT if we ever have two instances of the wrapper, we will be
476 INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work);
478 ret = platform_device_add_resources(musb, pdev->resource,
479 pdev->num_resources);
481 dev_err(&pdev->dev, "failed to add resources\n");
485 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
487 dev_err(&pdev->dev, "failed to add platform_data\n");
491 pm_runtime_enable(&pdev->dev);
493 ret = platform_device_add(musb);
495 dev_err(&pdev->dev, "failed to register musb device\n");
502 platform_device_put(musb);
511 static int __devexit omap2430_remove(struct platform_device *pdev)
513 struct omap2430_glue *glue = platform_get_drvdata(pdev);
515 cancel_work_sync(&glue->omap_musb_mailbox_work);
516 platform_device_del(glue->musb);
517 platform_device_put(glue->musb);
525 static int omap2430_runtime_suspend(struct device *dev)
527 struct omap2430_glue *glue = dev_get_drvdata(dev);
528 struct musb *musb = glue_to_musb(glue);
531 musb->context.otg_interfsel = musb_readl(musb->mregs,
534 omap2430_low_level_exit(musb);
535 usb_phy_set_suspend(musb->xceiv, 1);
541 static int omap2430_runtime_resume(struct device *dev)
543 struct omap2430_glue *glue = dev_get_drvdata(dev);
544 struct musb *musb = glue_to_musb(glue);
547 omap2430_low_level_init(musb);
548 musb_writel(musb->mregs, OTG_INTERFSEL,
549 musb->context.otg_interfsel);
551 usb_phy_set_suspend(musb->xceiv, 0);
557 static struct dev_pm_ops omap2430_pm_ops = {
558 .runtime_suspend = omap2430_runtime_suspend,
559 .runtime_resume = omap2430_runtime_resume,
562 #define DEV_PM_OPS (&omap2430_pm_ops)
564 #define DEV_PM_OPS NULL
567 static struct platform_driver omap2430_driver = {
568 .probe = omap2430_probe,
569 .remove = __devexit_p(omap2430_remove),
571 .name = "musb-omap2430",
576 MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
577 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
578 MODULE_LICENSE("GPL v2");
580 static int __init omap2430_init(void)
582 return platform_driver_register(&omap2430_driver);
584 subsys_initcall(omap2430_init);
586 static void __exit omap2430_exit(void)
588 platform_driver_unregister(&omap2430_driver);
590 module_exit(omap2430_exit);