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>
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/delay.h>
39 #include <linux/usb/musb.h>
40 #include <linux/phy/omap_control_phy.h>
41 #include <linux/of_platform.h>
43 #include "musb_core.h"
46 struct omap2430_glue {
48 struct platform_device *musb;
49 enum musb_vbus_id_status status;
50 struct work_struct omap_musb_mailbox_work;
51 struct device *control_otghs;
56 #define glue_to_musb(g) platform_get_drvdata(g->musb)
58 static 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->otg->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->otg->state = OTG_STATE_B_IDLE;
79 musb->xceiv->otg->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->hcd);
95 /* NOTE: it might really be A_WAIT_BCON ... */
96 musb->xceiv->otg->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->otg->state = OTG_STATE_B_IDLE;
104 musb->xceiv->otg->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->otg->state == OTG_STATE_A_WAIT_BCON))) {
123 dev_dbg(musb->controller, "%s active, deleting timer\n",
124 usb_otg_state_string(musb->xceiv->otg->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 usb_otg_state_string(musb->xceiv->otg->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);
151 /* HDRC controls CPEN, but beware current surges during device
152 * connect. They can trigger transient overcurrent conditions
153 * that must be ignored.
156 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
159 if (musb->xceiv->otg->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) &
169 MUSB_DEVCTL_BDEVICE) {
174 if (time_after(jiffies, timeout)
176 dev_err(musb->controller,
177 "configured as A device timeout");
182 otg_set_vbus(otg, 1);
186 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
187 devctl |= MUSB_DEVCTL_SESSION;
193 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and
194 * jumping right to B_IDLE...
198 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
199 devctl &= ~MUSB_DEVCTL_SESSION;
203 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
205 dev_dbg(musb->controller, "VBUS %s, devctl %02x "
206 /* otg %3x conf %08x prcm %08x */ "\n",
207 usb_otg_state_string(musb->xceiv->otg->state),
208 musb_readb(musb->mregs, MUSB_DEVCTL));
211 static int omap2430_musb_set_mode(struct musb *musb, u8 musb_mode)
213 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
215 devctl |= MUSB_DEVCTL_SESSION;
216 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 * We can get multiple cable events so we need to keep track
242 * of the power state. Only keep power enabled if USB cable is
243 * connected and a gadget is started.
245 static void omap2430_set_power(struct musb *musb, bool enabled, bool cable)
247 struct device *dev = musb->controller;
248 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
252 if (glue->enabled != enabled)
253 glue->enabled = enabled;
255 if (glue->cable_connected != cable)
256 glue->cable_connected = cable;
258 power_up = glue->enabled && glue->cable_connected;
259 if (power_up == glue->powered) {
260 dev_warn(musb->controller, "power state already %i\n",
265 glue->powered = power_up;
268 res = pm_runtime_get_sync(musb->controller);
270 dev_err(musb->controller, "could not enable: %i", res);
271 glue->powered = false;
274 pm_runtime_mark_last_busy(musb->controller);
275 pm_runtime_put_autosuspend(musb->controller);
279 static void omap2430_musb_mailbox(enum musb_vbus_id_status status)
281 struct omap2430_glue *glue = _glue;
284 pr_err("%s: musb core is not yet initialized\n", __func__);
287 glue->status = status;
289 if (!glue_to_musb(glue)) {
290 pr_err("%s: musb core is not yet ready\n", __func__);
294 schedule_work(&glue->omap_musb_mailbox_work);
297 static void omap_musb_set_mailbox(struct omap2430_glue *glue)
299 struct musb *musb = glue_to_musb(glue);
300 struct device *dev = musb->controller;
301 struct musb_hdrc_platform_data *pdata = dev_get_platdata(dev);
302 struct omap_musb_board_data *data = pdata->board_data;
303 struct usb_otg *otg = musb->xceiv->otg;
304 bool cable_connected;
306 cable_connected = ((glue->status == MUSB_ID_GROUND) ||
307 (glue->status == MUSB_VBUS_VALID));
310 omap2430_set_power(musb, glue->enabled, cable_connected);
312 switch (glue->status) {
314 dev_dbg(dev, "ID GND\n");
316 otg->default_a = true;
317 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
318 musb->xceiv->last_event = USB_EVENT_ID;
319 if (musb->gadget_driver) {
320 omap_control_usb_set_mode(glue->control_otghs,
322 omap2430_musb_set_vbus(musb, 1);
326 case MUSB_VBUS_VALID:
327 dev_dbg(dev, "VBUS Connect\n");
329 otg->default_a = false;
330 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
331 musb->xceiv->last_event = USB_EVENT_VBUS;
332 omap_control_usb_set_mode(glue->control_otghs, USB_MODE_DEVICE);
337 dev_dbg(dev, "VBUS Disconnect\n");
339 musb->xceiv->last_event = USB_EVENT_NONE;
340 if (musb->gadget_driver)
341 omap2430_musb_set_vbus(musb, 0);
343 if (data->interface_type == MUSB_INTERFACE_UTMI)
344 otg_set_vbus(musb->xceiv->otg, 0);
346 omap_control_usb_set_mode(glue->control_otghs,
347 USB_MODE_DISCONNECT);
350 dev_dbg(dev, "ID float\n");
353 if (!cable_connected)
354 omap2430_set_power(musb, glue->enabled, cable_connected);
356 atomic_notifier_call_chain(&musb->xceiv->notifier,
357 musb->xceiv->last_event, NULL);
361 static void omap_musb_mailbox_work(struct work_struct *mailbox_work)
363 struct omap2430_glue *glue = container_of(mailbox_work,
364 struct omap2430_glue, omap_musb_mailbox_work);
365 struct musb *musb = glue_to_musb(glue);
366 struct device *dev = musb->controller;
368 pm_runtime_get_sync(dev);
369 omap_musb_set_mailbox(glue);
370 pm_runtime_mark_last_busy(dev);
371 pm_runtime_put_autosuspend(dev);
374 static irqreturn_t omap2430_musb_interrupt(int irq, void *__hci)
377 irqreturn_t retval = IRQ_NONE;
378 struct musb *musb = __hci;
380 spin_lock_irqsave(&musb->lock, flags);
382 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
383 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
384 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
386 if (musb->int_usb || musb->int_tx || musb->int_rx)
387 retval = musb_interrupt(musb);
389 spin_unlock_irqrestore(&musb->lock, flags);
394 static int omap2430_musb_init(struct musb *musb)
398 struct device *dev = musb->controller;
399 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
400 struct musb_hdrc_platform_data *plat = dev_get_platdata(dev);
401 struct omap_musb_board_data *data = plat->board_data;
403 /* We require some kind of external transceiver, hooked
404 * up through ULPI. TWL4030-family PMICs include one,
405 * which needs a driver, drivers aren't always needed.
407 if (dev->parent->of_node) {
408 musb->phy = devm_phy_get(dev->parent, "usb2-phy");
410 /* We can't totally remove musb->xceiv as of now because
411 * musb core uses xceiv.state and xceiv.otg. Once we have
412 * a separate state machine to handle otg, these can be moved
413 * out of xceiv and then we can start using the generic PHY
416 musb->xceiv = devm_usb_get_phy_by_phandle(dev->parent,
419 musb->xceiv = devm_usb_get_phy_dev(dev, 0);
420 musb->phy = devm_phy_get(dev, "usb");
423 if (IS_ERR(musb->xceiv)) {
424 status = PTR_ERR(musb->xceiv);
426 if (status == -ENXIO)
429 pr_err("HS USB OTG: no transceiver configured\n");
430 return -EPROBE_DEFER;
433 if (IS_ERR(musb->phy)) {
434 pr_err("HS USB OTG: no PHY configured\n");
435 return PTR_ERR(musb->phy);
437 musb->isr = omap2430_musb_interrupt;
440 * Enable runtime PM for musb parent (this driver). We can't
441 * do it earlier as struct musb is not yet allocated and we
442 * need to touch the musb registers for runtime PM.
444 pm_runtime_enable(glue->dev);
445 status = pm_runtime_get_sync(glue->dev);
449 l = musb_readl(musb->mregs, OTG_INTERFSEL);
451 if (data->interface_type == MUSB_INTERFACE_UTMI) {
452 /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
453 l &= ~ULPI_12PIN; /* Disable ULPI */
454 l |= UTMI_8BIT; /* Enable UTMI */
459 musb_writel(musb->mregs, OTG_INTERFSEL, l);
461 pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
462 "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
463 musb_readl(musb->mregs, OTG_REVISION),
464 musb_readl(musb->mregs, OTG_SYSCONFIG),
465 musb_readl(musb->mregs, OTG_SYSSTATUS),
466 musb_readl(musb->mregs, OTG_INTERFSEL),
467 musb_readl(musb->mregs, OTG_SIMENABLE));
469 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
471 if (glue->status != MUSB_UNKNOWN)
472 omap_musb_set_mailbox(glue);
475 phy_power_on(musb->phy);
476 pm_runtime_put(glue->dev);
483 static void omap2430_musb_enable(struct musb *musb)
486 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
487 struct device *dev = musb->controller;
488 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
489 struct musb_hdrc_platform_data *pdata = dev_get_platdata(dev);
490 struct omap_musb_board_data *data = pdata->board_data;
492 omap2430_set_power(musb, true, glue->cable_connected);
494 switch (glue->status) {
497 omap_control_usb_set_mode(glue->control_otghs, USB_MODE_HOST);
498 if (data->interface_type != MUSB_INTERFACE_UTMI)
500 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
501 /* start the session */
502 devctl |= MUSB_DEVCTL_SESSION;
503 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
504 while (musb_readb(musb->mregs, MUSB_DEVCTL) &
505 MUSB_DEVCTL_BDEVICE) {
508 if (time_after(jiffies, timeout)) {
509 dev_err(dev, "configured as A device timeout");
515 case MUSB_VBUS_VALID:
516 omap_control_usb_set_mode(glue->control_otghs, USB_MODE_DEVICE);
524 static void omap2430_musb_disable(struct musb *musb)
526 struct device *dev = musb->controller;
527 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
529 if (glue->status != MUSB_UNKNOWN)
530 omap_control_usb_set_mode(glue->control_otghs,
531 USB_MODE_DISCONNECT);
533 omap2430_set_power(musb, false, glue->cable_connected);
536 static int omap2430_musb_exit(struct musb *musb)
538 del_timer_sync(&musb_idle_timer);
540 omap2430_low_level_exit(musb);
541 phy_power_off(musb->phy);
547 static const struct musb_platform_ops omap2430_ops = {
548 .quirks = MUSB_DMA_INVENTRA,
549 #ifdef CONFIG_USB_INVENTRA_DMA
550 .dma_init = musbhs_dma_controller_create,
551 .dma_exit = musbhs_dma_controller_destroy,
553 .init = omap2430_musb_init,
554 .exit = omap2430_musb_exit,
556 .set_mode = omap2430_musb_set_mode,
557 .try_idle = omap2430_musb_try_idle,
559 .set_vbus = omap2430_musb_set_vbus,
561 .enable = omap2430_musb_enable,
562 .disable = omap2430_musb_disable,
564 .phy_callback = omap2430_musb_mailbox,
567 static u64 omap2430_dmamask = DMA_BIT_MASK(32);
569 static int omap2430_probe(struct platform_device *pdev)
571 struct resource musb_resources[3];
572 struct musb_hdrc_platform_data *pdata = dev_get_platdata(&pdev->dev);
573 struct omap_musb_board_data *data;
574 struct platform_device *musb;
575 struct omap2430_glue *glue;
576 struct device_node *np = pdev->dev.of_node;
577 struct musb_hdrc_config *config;
578 int ret = -ENOMEM, val;
580 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
584 musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
586 dev_err(&pdev->dev, "failed to allocate musb device\n");
590 musb->dev.parent = &pdev->dev;
591 musb->dev.dma_mask = &omap2430_dmamask;
592 musb->dev.coherent_dma_mask = omap2430_dmamask;
594 glue->dev = &pdev->dev;
596 glue->status = MUSB_UNKNOWN;
597 glue->control_otghs = ERR_PTR(-ENODEV);
600 struct device_node *control_node;
601 struct platform_device *control_pdev;
603 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
607 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
611 config = devm_kzalloc(&pdev->dev, sizeof(*config), GFP_KERNEL);
615 of_property_read_u32(np, "mode", (u32 *)&pdata->mode);
616 of_property_read_u32(np, "interface-type",
617 (u32 *)&data->interface_type);
618 of_property_read_u32(np, "num-eps", (u32 *)&config->num_eps);
619 of_property_read_u32(np, "ram-bits", (u32 *)&config->ram_bits);
620 of_property_read_u32(np, "power", (u32 *)&pdata->power);
622 ret = of_property_read_u32(np, "multipoint", &val);
624 config->multipoint = true;
626 pdata->board_data = data;
627 pdata->config = config;
629 control_node = of_parse_phandle(np, "ctrl-module", 0);
631 control_pdev = of_find_device_by_node(control_node);
633 dev_err(&pdev->dev, "Failed to get control device\n");
637 glue->control_otghs = &control_pdev->dev;
640 pdata->platform_ops = &omap2430_ops;
642 platform_set_drvdata(pdev, glue);
645 * REVISIT if we ever have two instances of the wrapper, we will be
650 INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work);
652 memset(musb_resources, 0x00, sizeof(*musb_resources) *
653 ARRAY_SIZE(musb_resources));
655 musb_resources[0].name = pdev->resource[0].name;
656 musb_resources[0].start = pdev->resource[0].start;
657 musb_resources[0].end = pdev->resource[0].end;
658 musb_resources[0].flags = pdev->resource[0].flags;
660 musb_resources[1].name = pdev->resource[1].name;
661 musb_resources[1].start = pdev->resource[1].start;
662 musb_resources[1].end = pdev->resource[1].end;
663 musb_resources[1].flags = pdev->resource[1].flags;
665 musb_resources[2].name = pdev->resource[2].name;
666 musb_resources[2].start = pdev->resource[2].start;
667 musb_resources[2].end = pdev->resource[2].end;
668 musb_resources[2].flags = pdev->resource[2].flags;
670 ret = platform_device_add_resources(musb, musb_resources,
671 ARRAY_SIZE(musb_resources));
673 dev_err(&pdev->dev, "failed to add resources\n");
677 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
679 dev_err(&pdev->dev, "failed to add platform_data\n");
684 * Note that we cannot enable PM runtime yet for this
685 * driver as we need struct musb initialized first.
686 * See omap2430_musb_init above.
689 ret = platform_device_add(musb);
691 dev_err(&pdev->dev, "failed to register musb device\n");
698 platform_device_put(musb);
704 static int omap2430_remove(struct platform_device *pdev)
706 struct omap2430_glue *glue = platform_get_drvdata(pdev);
707 struct musb *musb = glue_to_musb(glue);
709 pm_runtime_get_sync(glue->dev);
710 cancel_work_sync(&glue->omap_musb_mailbox_work);
711 platform_device_unregister(glue->musb);
712 omap2430_set_power(musb, false, false);
713 pm_runtime_put_sync(glue->dev);
714 pm_runtime_disable(glue->dev);
721 static int omap2430_runtime_suspend(struct device *dev)
723 struct omap2430_glue *glue = dev_get_drvdata(dev);
724 struct musb *musb = glue_to_musb(glue);
727 musb->context.otg_interfsel = musb_readl(musb->mregs,
730 omap2430_low_level_exit(musb);
736 static int omap2430_runtime_resume(struct device *dev)
738 struct omap2430_glue *glue = dev_get_drvdata(dev);
739 struct musb *musb = glue_to_musb(glue);
742 return -EPROBE_DEFER;
744 omap2430_low_level_init(musb);
745 musb_writel(musb->mregs, OTG_INTERFSEL,
746 musb->context.otg_interfsel);
751 static struct dev_pm_ops omap2430_pm_ops = {
752 .runtime_suspend = omap2430_runtime_suspend,
753 .runtime_resume = omap2430_runtime_resume,
756 #define DEV_PM_OPS (&omap2430_pm_ops)
758 #define DEV_PM_OPS NULL
762 static const struct of_device_id omap2430_id_table[] = {
764 .compatible = "ti,omap4-musb"
767 .compatible = "ti,omap3-musb"
771 MODULE_DEVICE_TABLE(of, omap2430_id_table);
774 static struct platform_driver omap2430_driver = {
775 .probe = omap2430_probe,
776 .remove = omap2430_remove,
778 .name = "musb-omap2430",
780 .of_match_table = of_match_ptr(omap2430_id_table),
784 MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
785 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
786 MODULE_LICENSE("GPL v2");
788 static int __init omap2430_init(void)
790 return platform_driver_register(&omap2430_driver);
792 subsys_initcall(omap2430_init);
794 static void __exit omap2430_exit(void)
796 platform_driver_unregister(&omap2430_driver);
798 module_exit(omap2430_exit);