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 * SPDX-License-Identifier: GPL-2.0
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/init.h>
17 #include <linux/list.h>
19 #include <linux/platform_device.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/err.h>
23 #include <linux/usb/musb-omap.h>
26 #include <asm/omap_musb.h>
28 #include "linux-compat.h"
31 #include "musb_core.h"
35 struct omap2430_glue {
37 struct platform_device *musb;
38 enum omap_musb_vbus_id_status status;
39 struct work_struct omap_musb_mailbox_work;
41 #define glue_to_musb(g) platform_get_drvdata(g->musb)
43 struct omap2430_glue *_glue;
45 static struct timer_list musb_idle_timer;
47 static void musb_do_idle(unsigned long _musb)
49 struct musb *musb = (void *)_musb;
54 spin_lock_irqsave(&musb->lock, flags);
56 switch (musb->xceiv->state) {
57 case OTG_STATE_A_WAIT_BCON:
59 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
60 if (devctl & MUSB_DEVCTL_BDEVICE) {
61 musb->xceiv->state = OTG_STATE_B_IDLE;
64 musb->xceiv->state = OTG_STATE_A_IDLE;
68 case OTG_STATE_A_SUSPEND:
69 /* finish RESUME signaling? */
70 if (musb->port1_status & MUSB_PORT_STAT_RESUME) {
71 power = musb_readb(musb->mregs, MUSB_POWER);
72 power &= ~MUSB_POWER_RESUME;
73 dev_dbg(musb->controller, "root port resume stopped, power %02x\n", power);
74 musb_writeb(musb->mregs, MUSB_POWER, power);
76 musb->port1_status &= ~(USB_PORT_STAT_SUSPEND
77 | MUSB_PORT_STAT_RESUME);
78 musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
79 usb_hcd_poll_rh_status(musb_to_hcd(musb));
80 /* NOTE: it might really be A_WAIT_BCON ... */
81 musb->xceiv->state = OTG_STATE_A_HOST;
84 case OTG_STATE_A_HOST:
85 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
86 if (devctl & MUSB_DEVCTL_BDEVICE)
87 musb->xceiv->state = OTG_STATE_B_IDLE;
89 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
93 spin_unlock_irqrestore(&musb->lock, flags);
97 static void omap2430_musb_try_idle(struct musb *musb, unsigned long timeout)
99 unsigned long default_timeout = jiffies + msecs_to_jiffies(3);
100 static unsigned long last_timer;
103 timeout = default_timeout;
105 /* Never idle if active, or when VBUS timeout is not set as host */
106 if (musb->is_active || ((musb->a_wait_bcon == 0)
107 && (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
108 dev_dbg(musb->controller, "%s active, deleting timer\n",
109 otg_state_string(musb->xceiv->state));
110 del_timer(&musb_idle_timer);
111 last_timer = jiffies;
115 if (time_after(last_timer, timeout)) {
116 if (!timer_pending(&musb_idle_timer))
117 last_timer = timeout;
119 dev_dbg(musb->controller, "Longer idle timer already pending, ignoring\n");
123 last_timer = timeout;
125 dev_dbg(musb->controller, "%s inactive, for idle timer for %lu ms\n",
126 otg_state_string(musb->xceiv->state),
127 (unsigned long)jiffies_to_msecs(timeout - jiffies));
128 mod_timer(&musb_idle_timer, timeout);
131 static void omap2430_musb_set_vbus(struct musb *musb, int is_on)
133 struct usb_otg *otg = musb->xceiv->otg;
135 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
137 /* HDRC controls CPEN, but beware current surges during device
138 * connect. They can trigger transient overcurrent conditions
139 * that must be ignored.
142 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
145 if (musb->xceiv->state == OTG_STATE_A_IDLE) {
146 /* start the session */
147 devctl |= MUSB_DEVCTL_SESSION;
148 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
150 * Wait for the musb to set as A device to enable the
153 while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
157 if (time_after(jiffies, timeout)) {
158 dev_err(musb->controller,
159 "configured as A device timeout");
165 if (ret && otg->set_vbus)
166 otg_set_vbus(otg, 1);
170 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
171 devctl |= MUSB_DEVCTL_SESSION;
177 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and
178 * jumping right to B_IDLE...
182 musb->xceiv->state = OTG_STATE_B_IDLE;
183 devctl &= ~MUSB_DEVCTL_SESSION;
187 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
189 dev_dbg(musb->controller, "VBUS %s, devctl %02x "
190 /* otg %3x conf %08x prcm %08x */ "\n",
191 otg_state_string(musb->xceiv->state),
192 musb_readb(musb->mregs, MUSB_DEVCTL));
195 static int omap2430_musb_set_mode(struct musb *musb, u8 musb_mode)
197 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
199 devctl |= MUSB_DEVCTL_SESSION;
200 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
206 static inline void omap2430_low_level_exit(struct musb *musb)
211 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
212 l |= ENABLEFORCE; /* enable MSTANDBY */
213 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
216 static inline void omap2430_low_level_init(struct musb *musb)
220 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
221 l &= ~ENABLEFORCE; /* disable MSTANDBY */
222 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
226 void omap_musb_mailbox(enum omap_musb_vbus_id_status status)
228 struct omap2430_glue *glue = _glue;
229 struct musb *musb = glue_to_musb(glue);
231 glue->status = status;
233 dev_err(glue->dev, "musb core is not yet ready\n");
237 schedule_work(&glue->omap_musb_mailbox_work);
239 EXPORT_SYMBOL_GPL(omap_musb_mailbox);
241 static void omap_musb_set_mailbox(struct omap2430_glue *glue)
243 struct musb *musb = glue_to_musb(glue);
244 struct device *dev = musb->controller;
245 struct musb_hdrc_platform_data *pdata = dev->platform_data;
246 struct omap_musb_board_data *data = pdata->board_data;
247 struct usb_otg *otg = musb->xceiv->otg;
249 switch (glue->status) {
250 case OMAP_MUSB_ID_GROUND:
251 dev_dbg(dev, "ID GND\n");
253 otg->default_a = true;
254 musb->xceiv->state = OTG_STATE_A_IDLE;
255 musb->xceiv->last_event = USB_EVENT_ID;
256 if (!is_otg_enabled(musb) || musb->gadget_driver) {
257 pm_runtime_get_sync(dev);
258 usb_phy_init(musb->xceiv);
259 omap2430_musb_set_vbus(musb, 1);
263 case OMAP_MUSB_VBUS_VALID:
264 dev_dbg(dev, "VBUS Connect\n");
266 otg->default_a = false;
267 musb->xceiv->state = OTG_STATE_B_IDLE;
268 musb->xceiv->last_event = USB_EVENT_VBUS;
269 if (musb->gadget_driver)
270 pm_runtime_get_sync(dev);
271 usb_phy_init(musb->xceiv);
274 case OMAP_MUSB_ID_FLOAT:
275 case OMAP_MUSB_VBUS_OFF:
276 dev_dbg(dev, "VBUS Disconnect\n");
278 musb->xceiv->last_event = USB_EVENT_NONE;
279 if (is_otg_enabled(musb) || is_peripheral_enabled(musb))
280 if (musb->gadget_driver) {
281 pm_runtime_mark_last_busy(dev);
282 pm_runtime_put_autosuspend(dev);
285 if (data->interface_type == MUSB_INTERFACE_UTMI) {
286 if (musb->xceiv->otg->set_vbus)
287 otg_set_vbus(musb->xceiv->otg, 0);
289 usb_phy_shutdown(musb->xceiv);
292 dev_dbg(dev, "ID float\n");
297 static void omap_musb_mailbox_work(struct work_struct *mailbox_work)
299 struct omap2430_glue *glue = container_of(mailbox_work,
300 struct omap2430_glue, omap_musb_mailbox_work);
301 omap_musb_set_mailbox(glue);
305 static int omap2430_musb_init(struct musb *musb)
309 unsigned long int start;
311 struct device *dev = musb->controller;
312 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
313 struct musb_hdrc_platform_data *plat = dev->platform_data;
314 struct omap_musb_board_data *data = plat->board_data;
316 struct omap_musb_board_data *data =
317 (struct omap_musb_board_data *)musb->controller;
320 /* Reset the controller */
321 musb_writel(musb->mregs, OTG_SYSCONFIG, SOFTRST);
323 start = get_timer(0);
326 l = musb_readl(musb->mregs, OTG_SYSCONFIG);
327 if ((l & SOFTRST) == 0)
330 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
331 dev_err(musb->controller, "MUSB reset is taking too long\n");
337 /* We require some kind of external transceiver, hooked
338 * up through ULPI. TWL4030-family PMICs include one,
339 * which needs a driver, drivers aren't always needed.
341 musb->xceiv = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
342 if (IS_ERR_OR_NULL(musb->xceiv)) {
343 pr_err("HS USB OTG: no transceiver configured\n");
347 status = pm_runtime_get_sync(dev);
349 dev_err(dev, "pm_runtime_get_sync FAILED %d\n", status);
354 l = musb_readl(musb->mregs, OTG_INTERFSEL);
356 if (data->interface_type == MUSB_INTERFACE_UTMI) {
357 /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
358 l &= ~ULPI_12PIN; /* Disable ULPI */
359 l |= UTMI_8BIT; /* Enable UTMI */
364 musb_writel(musb->mregs, OTG_INTERFSEL, l);
366 pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
367 "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
368 musb_readl(musb->mregs, OTG_REVISION),
369 musb_readl(musb->mregs, OTG_SYSCONFIG),
370 musb_readl(musb->mregs, OTG_SYSSTATUS),
371 musb_readl(musb->mregs, OTG_INTERFSEL),
372 musb_readl(musb->mregs, OTG_SIMENABLE));
375 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
377 if (glue->status != OMAP_MUSB_UNKNOWN)
378 omap_musb_set_mailbox(glue);
380 pm_runtime_put_noidle(musb->controller);
389 static void omap2430_musb_enable(struct musb *musb)
391 static int omap2430_musb_enable(struct musb *musb)
396 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
397 struct device *dev = musb->controller;
398 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
399 struct musb_hdrc_platform_data *pdata = dev->platform_data;
400 struct omap_musb_board_data *data = pdata->board_data;
402 switch (glue->status) {
404 case OMAP_MUSB_ID_GROUND:
405 usb_phy_init(musb->xceiv);
406 if (data->interface_type != MUSB_INTERFACE_UTMI)
408 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
409 /* start the session */
410 devctl |= MUSB_DEVCTL_SESSION;
411 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
412 while (musb_readb(musb->mregs, MUSB_DEVCTL) &
413 MUSB_DEVCTL_BDEVICE) {
416 if (time_after(jiffies, timeout)) {
417 dev_err(dev, "configured as A device timeout");
423 case OMAP_MUSB_VBUS_VALID:
424 usb_phy_init(musb->xceiv);
431 #ifdef CONFIG_TWL4030_USB
432 if (twl4030_usb_ulpi_init()) {
433 serial_printf("ERROR: %s Could not initialize PHY\n",
434 __PRETTY_FUNCTION__);
441 static void omap2430_musb_disable(struct musb *musb)
444 struct device *dev = musb->controller;
445 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
447 if (glue->status != OMAP_MUSB_UNKNOWN)
448 usb_phy_shutdown(musb->xceiv);
452 static int omap2430_musb_exit(struct musb *musb)
454 del_timer_sync(&musb_idle_timer);
456 omap2430_low_level_exit(musb);
462 static const struct musb_platform_ops omap2430_ops = {
464 const struct musb_platform_ops omap2430_ops = {
466 .init = omap2430_musb_init,
467 .exit = omap2430_musb_exit,
470 .set_mode = omap2430_musb_set_mode,
471 .try_idle = omap2430_musb_try_idle,
473 .set_vbus = omap2430_musb_set_vbus,
476 .enable = omap2430_musb_enable,
477 .disable = omap2430_musb_disable,
481 static u64 omap2430_dmamask = DMA_BIT_MASK(32);
483 static int __devinit omap2430_probe(struct platform_device *pdev)
485 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
486 struct platform_device *musb;
487 struct omap2430_glue *glue;
490 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
492 dev_err(&pdev->dev, "failed to allocate glue context\n");
496 musb = platform_device_alloc("musb-hdrc", -1);
498 dev_err(&pdev->dev, "failed to allocate musb device\n");
502 musb->dev.parent = &pdev->dev;
503 musb->dev.dma_mask = &omap2430_dmamask;
504 musb->dev.coherent_dma_mask = omap2430_dmamask;
506 glue->dev = &pdev->dev;
508 glue->status = OMAP_MUSB_UNKNOWN;
510 pdata->platform_ops = &omap2430_ops;
512 platform_set_drvdata(pdev, glue);
515 * REVISIT if we ever have two instances of the wrapper, we will be
520 INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work);
522 ret = platform_device_add_resources(musb, pdev->resource,
523 pdev->num_resources);
525 dev_err(&pdev->dev, "failed to add resources\n");
529 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
531 dev_err(&pdev->dev, "failed to add platform_data\n");
535 pm_runtime_enable(&pdev->dev);
537 ret = platform_device_add(musb);
539 dev_err(&pdev->dev, "failed to register musb device\n");
546 platform_device_put(musb);
552 static int __devexit omap2430_remove(struct platform_device *pdev)
554 struct omap2430_glue *glue = platform_get_drvdata(pdev);
556 cancel_work_sync(&glue->omap_musb_mailbox_work);
557 platform_device_del(glue->musb);
558 platform_device_put(glue->musb);
565 static int omap2430_runtime_suspend(struct device *dev)
567 struct omap2430_glue *glue = dev_get_drvdata(dev);
568 struct musb *musb = glue_to_musb(glue);
571 musb->context.otg_interfsel = musb_readl(musb->mregs,
574 omap2430_low_level_exit(musb);
575 usb_phy_set_suspend(musb->xceiv, 1);
581 static int omap2430_runtime_resume(struct device *dev)
583 struct omap2430_glue *glue = dev_get_drvdata(dev);
584 struct musb *musb = glue_to_musb(glue);
587 omap2430_low_level_init(musb);
588 musb_writel(musb->mregs, OTG_INTERFSEL,
589 musb->context.otg_interfsel);
591 usb_phy_set_suspend(musb->xceiv, 0);
597 static struct dev_pm_ops omap2430_pm_ops = {
598 .runtime_suspend = omap2430_runtime_suspend,
599 .runtime_resume = omap2430_runtime_resume,
602 #define DEV_PM_OPS (&omap2430_pm_ops)
604 #define DEV_PM_OPS NULL
607 static struct platform_driver omap2430_driver = {
608 .probe = omap2430_probe,
609 .remove = __devexit_p(omap2430_remove),
611 .name = "musb-omap2430",
616 MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
617 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
618 MODULE_LICENSE("GPL v2");
620 static int __init omap2430_init(void)
622 return platform_driver_register(&omap2430_driver);
624 subsys_initcall(omap2430_init);
626 static void __exit omap2430_exit(void)
628 platform_driver_unregister(&omap2430_driver);
630 module_exit(omap2430_exit);