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
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/init.h>
32 #include <linux/list.h>
34 #include <linux/platform_device.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/err.h>
38 #include <linux/usb/musb-omap.h>
41 #include <asm/omap_musb.h>
43 #include "linux-compat.h"
46 #include "musb_core.h"
50 struct omap2430_glue {
52 struct platform_device *musb;
53 enum omap_musb_vbus_id_status status;
54 struct work_struct omap_musb_mailbox_work;
56 #define glue_to_musb(g) platform_get_drvdata(g->musb)
58 struct omap2430_glue *_glue;
60 static struct timer_list musb_idle_timer;
62 static void musb_do_idle(unsigned long _musb)
64 struct musb *musb = (void *)_musb;
69 spin_lock_irqsave(&musb->lock, flags);
71 switch (musb->xceiv->state) {
72 case OTG_STATE_A_WAIT_BCON:
74 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
75 if (devctl & MUSB_DEVCTL_BDEVICE) {
76 musb->xceiv->state = OTG_STATE_B_IDLE;
79 musb->xceiv->state = OTG_STATE_A_IDLE;
83 case OTG_STATE_A_SUSPEND:
84 /* finish RESUME signaling? */
85 if (musb->port1_status & MUSB_PORT_STAT_RESUME) {
86 power = musb_readb(musb->mregs, MUSB_POWER);
87 power &= ~MUSB_POWER_RESUME;
88 dev_dbg(musb->controller, "root port resume stopped, power %02x\n", power);
89 musb_writeb(musb->mregs, MUSB_POWER, power);
91 musb->port1_status &= ~(USB_PORT_STAT_SUSPEND
92 | MUSB_PORT_STAT_RESUME);
93 musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
94 usb_hcd_poll_rh_status(musb_to_hcd(musb));
95 /* NOTE: it might really be A_WAIT_BCON ... */
96 musb->xceiv->state = OTG_STATE_A_HOST;
99 case OTG_STATE_A_HOST:
100 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
101 if (devctl & MUSB_DEVCTL_BDEVICE)
102 musb->xceiv->state = OTG_STATE_B_IDLE;
104 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
108 spin_unlock_irqrestore(&musb->lock, flags);
112 static void omap2430_musb_try_idle(struct musb *musb, unsigned long timeout)
114 unsigned long default_timeout = jiffies + msecs_to_jiffies(3);
115 static unsigned long last_timer;
118 timeout = default_timeout;
120 /* Never idle if active, or when VBUS timeout is not set as host */
121 if (musb->is_active || ((musb->a_wait_bcon == 0)
122 && (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
123 dev_dbg(musb->controller, "%s active, deleting timer\n",
124 otg_state_string(musb->xceiv->state));
125 del_timer(&musb_idle_timer);
126 last_timer = jiffies;
130 if (time_after(last_timer, timeout)) {
131 if (!timer_pending(&musb_idle_timer))
132 last_timer = timeout;
134 dev_dbg(musb->controller, "Longer idle timer already pending, ignoring\n");
138 last_timer = timeout;
140 dev_dbg(musb->controller, "%s inactive, for idle timer for %lu ms\n",
141 otg_state_string(musb->xceiv->state),
142 (unsigned long)jiffies_to_msecs(timeout - jiffies));
143 mod_timer(&musb_idle_timer, timeout);
146 static void omap2430_musb_set_vbus(struct musb *musb, int is_on)
148 struct usb_otg *otg = musb->xceiv->otg;
150 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
152 /* HDRC controls CPEN, but beware current surges during device
153 * connect. They can trigger transient overcurrent conditions
154 * that must be ignored.
157 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
160 if (musb->xceiv->state == OTG_STATE_A_IDLE) {
161 /* start the session */
162 devctl |= MUSB_DEVCTL_SESSION;
163 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
165 * Wait for the musb to set as A device to enable the
168 while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
172 if (time_after(jiffies, timeout)) {
173 dev_err(musb->controller,
174 "configured as A device timeout");
180 if (ret && otg->set_vbus)
181 otg_set_vbus(otg, 1);
185 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
186 devctl |= MUSB_DEVCTL_SESSION;
192 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and
193 * jumping right to B_IDLE...
197 musb->xceiv->state = OTG_STATE_B_IDLE;
198 devctl &= ~MUSB_DEVCTL_SESSION;
202 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
204 dev_dbg(musb->controller, "VBUS %s, devctl %02x "
205 /* otg %3x conf %08x prcm %08x */ "\n",
206 otg_state_string(musb->xceiv->state),
207 musb_readb(musb->mregs, MUSB_DEVCTL));
210 static int omap2430_musb_set_mode(struct musb *musb, u8 musb_mode)
212 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
214 devctl |= MUSB_DEVCTL_SESSION;
215 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
221 static inline void omap2430_low_level_exit(struct musb *musb)
226 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
227 l |= ENABLEFORCE; /* enable MSTANDBY */
228 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
231 static inline void omap2430_low_level_init(struct musb *musb)
235 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
236 l &= ~ENABLEFORCE; /* disable MSTANDBY */
237 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
241 void omap_musb_mailbox(enum omap_musb_vbus_id_status status)
243 struct omap2430_glue *glue = _glue;
244 struct musb *musb = glue_to_musb(glue);
246 glue->status = status;
248 dev_err(glue->dev, "musb core is not yet ready\n");
252 schedule_work(&glue->omap_musb_mailbox_work);
254 EXPORT_SYMBOL_GPL(omap_musb_mailbox);
256 static void omap_musb_set_mailbox(struct omap2430_glue *glue)
258 struct musb *musb = glue_to_musb(glue);
259 struct device *dev = musb->controller;
260 struct musb_hdrc_platform_data *pdata = dev->platform_data;
261 struct omap_musb_board_data *data = pdata->board_data;
262 struct usb_otg *otg = musb->xceiv->otg;
264 switch (glue->status) {
265 case OMAP_MUSB_ID_GROUND:
266 dev_dbg(dev, "ID GND\n");
268 otg->default_a = true;
269 musb->xceiv->state = OTG_STATE_A_IDLE;
270 musb->xceiv->last_event = USB_EVENT_ID;
271 if (!is_otg_enabled(musb) || musb->gadget_driver) {
272 pm_runtime_get_sync(dev);
273 usb_phy_init(musb->xceiv);
274 omap2430_musb_set_vbus(musb, 1);
278 case OMAP_MUSB_VBUS_VALID:
279 dev_dbg(dev, "VBUS Connect\n");
281 otg->default_a = false;
282 musb->xceiv->state = OTG_STATE_B_IDLE;
283 musb->xceiv->last_event = USB_EVENT_VBUS;
284 if (musb->gadget_driver)
285 pm_runtime_get_sync(dev);
286 usb_phy_init(musb->xceiv);
289 case OMAP_MUSB_ID_FLOAT:
290 case OMAP_MUSB_VBUS_OFF:
291 dev_dbg(dev, "VBUS Disconnect\n");
293 musb->xceiv->last_event = USB_EVENT_NONE;
294 if (is_otg_enabled(musb) || is_peripheral_enabled(musb))
295 if (musb->gadget_driver) {
296 pm_runtime_mark_last_busy(dev);
297 pm_runtime_put_autosuspend(dev);
300 if (data->interface_type == MUSB_INTERFACE_UTMI) {
301 if (musb->xceiv->otg->set_vbus)
302 otg_set_vbus(musb->xceiv->otg, 0);
304 usb_phy_shutdown(musb->xceiv);
307 dev_dbg(dev, "ID float\n");
312 static void omap_musb_mailbox_work(struct work_struct *mailbox_work)
314 struct omap2430_glue *glue = container_of(mailbox_work,
315 struct omap2430_glue, omap_musb_mailbox_work);
316 omap_musb_set_mailbox(glue);
320 static int omap2430_musb_init(struct musb *musb)
324 unsigned long int start;
326 struct device *dev = musb->controller;
327 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
328 struct musb_hdrc_platform_data *plat = dev->platform_data;
329 struct omap_musb_board_data *data = plat->board_data;
331 struct omap_musb_board_data *data =
332 (struct omap_musb_board_data *)musb->controller;
335 /* Reset the controller */
336 musb_writel(musb->mregs, OTG_SYSCONFIG, SOFTRST);
338 start = get_timer(0);
341 l = musb_readl(musb->mregs, OTG_SYSCONFIG);
342 if ((l & SOFTRST) == 0)
345 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
346 dev_err(musb->controller, "MUSB reset is taking too long\n");
352 /* We require some kind of external transceiver, hooked
353 * up through ULPI. TWL4030-family PMICs include one,
354 * which needs a driver, drivers aren't always needed.
356 musb->xceiv = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
357 if (IS_ERR_OR_NULL(musb->xceiv)) {
358 pr_err("HS USB OTG: no transceiver configured\n");
362 status = pm_runtime_get_sync(dev);
364 dev_err(dev, "pm_runtime_get_sync FAILED %d\n", status);
369 l = musb_readl(musb->mregs, OTG_INTERFSEL);
371 if (data->interface_type == MUSB_INTERFACE_UTMI) {
372 /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
373 l &= ~ULPI_12PIN; /* Disable ULPI */
374 l |= UTMI_8BIT; /* Enable UTMI */
379 musb_writel(musb->mregs, OTG_INTERFSEL, l);
381 pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
382 "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
383 musb_readl(musb->mregs, OTG_REVISION),
384 musb_readl(musb->mregs, OTG_SYSCONFIG),
385 musb_readl(musb->mregs, OTG_SYSSTATUS),
386 musb_readl(musb->mregs, OTG_INTERFSEL),
387 musb_readl(musb->mregs, OTG_SIMENABLE));
390 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
392 if (glue->status != OMAP_MUSB_UNKNOWN)
393 omap_musb_set_mailbox(glue);
395 pm_runtime_put_noidle(musb->controller);
403 static void omap2430_musb_enable(struct musb *musb)
407 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
408 struct device *dev = musb->controller;
409 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
410 struct musb_hdrc_platform_data *pdata = dev->platform_data;
411 struct omap_musb_board_data *data = pdata->board_data;
413 switch (glue->status) {
415 case OMAP_MUSB_ID_GROUND:
416 usb_phy_init(musb->xceiv);
417 if (data->interface_type != MUSB_INTERFACE_UTMI)
419 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
420 /* start the session */
421 devctl |= MUSB_DEVCTL_SESSION;
422 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
423 while (musb_readb(musb->mregs, MUSB_DEVCTL) &
424 MUSB_DEVCTL_BDEVICE) {
427 if (time_after(jiffies, timeout)) {
428 dev_err(dev, "configured as A device timeout");
434 case OMAP_MUSB_VBUS_VALID:
435 usb_phy_init(musb->xceiv);
442 #ifdef CONFIG_TWL4030_USB
443 if (twl4030_usb_ulpi_init()) {
444 serial_printf("ERROR: %s Could not initialize PHY\n",
445 __PRETTY_FUNCTION__);
451 static void omap2430_musb_disable(struct musb *musb)
454 struct device *dev = musb->controller;
455 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
457 if (glue->status != OMAP_MUSB_UNKNOWN)
458 usb_phy_shutdown(musb->xceiv);
462 static int omap2430_musb_exit(struct musb *musb)
464 del_timer_sync(&musb_idle_timer);
466 omap2430_low_level_exit(musb);
472 static const struct musb_platform_ops omap2430_ops = {
474 const struct musb_platform_ops omap2430_ops = {
476 .init = omap2430_musb_init,
477 .exit = omap2430_musb_exit,
480 .set_mode = omap2430_musb_set_mode,
481 .try_idle = omap2430_musb_try_idle,
483 .set_vbus = omap2430_musb_set_vbus,
486 .enable = omap2430_musb_enable,
487 .disable = omap2430_musb_disable,
491 static u64 omap2430_dmamask = DMA_BIT_MASK(32);
493 static int __devinit omap2430_probe(struct platform_device *pdev)
495 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
496 struct platform_device *musb;
497 struct omap2430_glue *glue;
500 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
502 dev_err(&pdev->dev, "failed to allocate glue context\n");
506 musb = platform_device_alloc("musb-hdrc", -1);
508 dev_err(&pdev->dev, "failed to allocate musb device\n");
512 musb->dev.parent = &pdev->dev;
513 musb->dev.dma_mask = &omap2430_dmamask;
514 musb->dev.coherent_dma_mask = omap2430_dmamask;
516 glue->dev = &pdev->dev;
518 glue->status = OMAP_MUSB_UNKNOWN;
520 pdata->platform_ops = &omap2430_ops;
522 platform_set_drvdata(pdev, glue);
525 * REVISIT if we ever have two instances of the wrapper, we will be
530 INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work);
532 ret = platform_device_add_resources(musb, pdev->resource,
533 pdev->num_resources);
535 dev_err(&pdev->dev, "failed to add resources\n");
539 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
541 dev_err(&pdev->dev, "failed to add platform_data\n");
545 pm_runtime_enable(&pdev->dev);
547 ret = platform_device_add(musb);
549 dev_err(&pdev->dev, "failed to register musb device\n");
556 platform_device_put(musb);
562 static int __devexit omap2430_remove(struct platform_device *pdev)
564 struct omap2430_glue *glue = platform_get_drvdata(pdev);
566 cancel_work_sync(&glue->omap_musb_mailbox_work);
567 platform_device_del(glue->musb);
568 platform_device_put(glue->musb);
575 static int omap2430_runtime_suspend(struct device *dev)
577 struct omap2430_glue *glue = dev_get_drvdata(dev);
578 struct musb *musb = glue_to_musb(glue);
581 musb->context.otg_interfsel = musb_readl(musb->mregs,
584 omap2430_low_level_exit(musb);
585 usb_phy_set_suspend(musb->xceiv, 1);
591 static int omap2430_runtime_resume(struct device *dev)
593 struct omap2430_glue *glue = dev_get_drvdata(dev);
594 struct musb *musb = glue_to_musb(glue);
597 omap2430_low_level_init(musb);
598 musb_writel(musb->mregs, OTG_INTERFSEL,
599 musb->context.otg_interfsel);
601 usb_phy_set_suspend(musb->xceiv, 0);
607 static struct dev_pm_ops omap2430_pm_ops = {
608 .runtime_suspend = omap2430_runtime_suspend,
609 .runtime_resume = omap2430_runtime_resume,
612 #define DEV_PM_OPS (&omap2430_pm_ops)
614 #define DEV_PM_OPS NULL
617 static struct platform_driver omap2430_driver = {
618 .probe = omap2430_probe,
619 .remove = __devexit_p(omap2430_remove),
621 .name = "musb-omap2430",
626 MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
627 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
628 MODULE_LICENSE("GPL v2");
630 static int __init omap2430_init(void)
632 return platform_driver_register(&omap2430_driver);
634 subsys_initcall(omap2430_init);
636 static void __exit omap2430_exit(void)
638 platform_driver_unregister(&omap2430_driver);
640 module_exit(omap2430_exit);