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-omap.h>
41 #include "musb_core.h"
44 struct omap2430_glue
{
46 struct platform_device
*musb
;
47 enum omap_musb_vbus_id_status status
;
48 struct work_struct omap_musb_mailbox_work
;
49 u32 __iomem
*control_otghs
;
51 #define glue_to_musb(g) platform_get_drvdata(g->musb)
53 struct omap2430_glue
*_glue
;
55 static struct timer_list musb_idle_timer
;
58 * omap4_usb_phy_mailbox - write to usb otg mailbox
59 * @glue: struct omap2430_glue *
60 * @val: the value to be written to the mailbox
62 * On detection of a device (ID pin is grounded), this API should be called
63 * to set AVALID, VBUSVALID and ID pin is grounded.
65 * When OMAP is connected to a host (OMAP in device mode), this API
66 * is called to set AVALID, VBUSVALID and ID pin in high impedance.
68 * XXX: This function will be removed once we have a seperate driver for
71 static void omap4_usb_phy_mailbox(struct omap2430_glue
*glue
, u32 val
)
73 if (glue
->control_otghs
)
74 writel(val
, glue
->control_otghs
);
77 static void musb_do_idle(unsigned long _musb
)
79 struct musb
*musb
= (void *)_musb
;
84 spin_lock_irqsave(&musb
->lock
, flags
);
86 switch (musb
->xceiv
->state
) {
87 case OTG_STATE_A_WAIT_BCON
:
89 devctl
= musb_readb(musb
->mregs
, MUSB_DEVCTL
);
90 if (devctl
& MUSB_DEVCTL_BDEVICE
) {
91 musb
->xceiv
->state
= OTG_STATE_B_IDLE
;
94 musb
->xceiv
->state
= OTG_STATE_A_IDLE
;
98 case OTG_STATE_A_SUSPEND
:
99 /* finish RESUME signaling? */
100 if (musb
->port1_status
& MUSB_PORT_STAT_RESUME
) {
101 power
= musb_readb(musb
->mregs
, MUSB_POWER
);
102 power
&= ~MUSB_POWER_RESUME
;
103 dev_dbg(musb
->controller
, "root port resume stopped, power %02x\n", power
);
104 musb_writeb(musb
->mregs
, MUSB_POWER
, power
);
106 musb
->port1_status
&= ~(USB_PORT_STAT_SUSPEND
107 | MUSB_PORT_STAT_RESUME
);
108 musb
->port1_status
|= USB_PORT_STAT_C_SUSPEND
<< 16;
109 usb_hcd_poll_rh_status(musb_to_hcd(musb
));
110 /* NOTE: it might really be A_WAIT_BCON ... */
111 musb
->xceiv
->state
= OTG_STATE_A_HOST
;
114 case OTG_STATE_A_HOST
:
115 devctl
= musb_readb(musb
->mregs
, MUSB_DEVCTL
);
116 if (devctl
& MUSB_DEVCTL_BDEVICE
)
117 musb
->xceiv
->state
= OTG_STATE_B_IDLE
;
119 musb
->xceiv
->state
= OTG_STATE_A_WAIT_BCON
;
123 spin_unlock_irqrestore(&musb
->lock
, flags
);
127 static void omap2430_musb_try_idle(struct musb
*musb
, unsigned long timeout
)
129 unsigned long default_timeout
= jiffies
+ msecs_to_jiffies(3);
130 static unsigned long last_timer
;
133 timeout
= default_timeout
;
135 /* Never idle if active, or when VBUS timeout is not set as host */
136 if (musb
->is_active
|| ((musb
->a_wait_bcon
== 0)
137 && (musb
->xceiv
->state
== OTG_STATE_A_WAIT_BCON
))) {
138 dev_dbg(musb
->controller
, "%s active, deleting timer\n",
139 otg_state_string(musb
->xceiv
->state
));
140 del_timer(&musb_idle_timer
);
141 last_timer
= jiffies
;
145 if (time_after(last_timer
, timeout
)) {
146 if (!timer_pending(&musb_idle_timer
))
147 last_timer
= timeout
;
149 dev_dbg(musb
->controller
, "Longer idle timer already pending, ignoring\n");
153 last_timer
= timeout
;
155 dev_dbg(musb
->controller
, "%s inactive, for idle timer for %lu ms\n",
156 otg_state_string(musb
->xceiv
->state
),
157 (unsigned long)jiffies_to_msecs(timeout
- jiffies
));
158 mod_timer(&musb_idle_timer
, timeout
);
161 static void omap2430_musb_set_vbus(struct musb
*musb
, int is_on
)
163 struct usb_otg
*otg
= musb
->xceiv
->otg
;
165 unsigned long timeout
= jiffies
+ msecs_to_jiffies(1000);
166 /* HDRC controls CPEN, but beware current surges during device
167 * connect. They can trigger transient overcurrent conditions
168 * that must be ignored.
171 devctl
= musb_readb(musb
->mregs
, MUSB_DEVCTL
);
174 if (musb
->xceiv
->state
== OTG_STATE_A_IDLE
) {
176 /* start the session */
177 devctl
|= MUSB_DEVCTL_SESSION
;
178 musb_writeb(musb
->mregs
, MUSB_DEVCTL
, devctl
);
180 * Wait for the musb to set as A device to enable the
183 while (musb_readb(musb
->mregs
, MUSB_DEVCTL
) & 0x80) {
188 if (time_after(jiffies
, timeout
)
190 dev_err(musb
->controller
,
191 "configured as A device timeout");
197 otg_set_vbus(otg
, 1);
201 musb
->xceiv
->state
= OTG_STATE_A_WAIT_VRISE
;
202 devctl
|= MUSB_DEVCTL_SESSION
;
208 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and
209 * jumping right to B_IDLE...
213 musb
->xceiv
->state
= OTG_STATE_B_IDLE
;
214 devctl
&= ~MUSB_DEVCTL_SESSION
;
218 musb_writeb(musb
->mregs
, MUSB_DEVCTL
, devctl
);
220 dev_dbg(musb
->controller
, "VBUS %s, devctl %02x "
221 /* otg %3x conf %08x prcm %08x */ "\n",
222 otg_state_string(musb
->xceiv
->state
),
223 musb_readb(musb
->mregs
, MUSB_DEVCTL
));
226 static int omap2430_musb_set_mode(struct musb
*musb
, u8 musb_mode
)
228 u8 devctl
= musb_readb(musb
->mregs
, MUSB_DEVCTL
);
230 devctl
|= MUSB_DEVCTL_SESSION
;
231 musb_writeb(musb
->mregs
, MUSB_DEVCTL
, devctl
);
236 static inline void omap2430_low_level_exit(struct musb
*musb
)
241 l
= musb_readl(musb
->mregs
, OTG_FORCESTDBY
);
242 l
|= ENABLEFORCE
; /* enable MSTANDBY */
243 musb_writel(musb
->mregs
, OTG_FORCESTDBY
, l
);
246 static inline void omap2430_low_level_init(struct musb
*musb
)
250 l
= musb_readl(musb
->mregs
, OTG_FORCESTDBY
);
251 l
&= ~ENABLEFORCE
; /* disable MSTANDBY */
252 musb_writel(musb
->mregs
, OTG_FORCESTDBY
, l
);
255 void omap_musb_mailbox(enum omap_musb_vbus_id_status status
)
257 struct omap2430_glue
*glue
= _glue
;
258 struct musb
*musb
= glue_to_musb(glue
);
260 glue
->status
= status
;
262 dev_err(glue
->dev
, "musb core is not yet ready\n");
266 schedule_work(&glue
->omap_musb_mailbox_work
);
268 EXPORT_SYMBOL_GPL(omap_musb_mailbox
);
270 static void omap_musb_set_mailbox(struct omap2430_glue
*glue
)
273 struct musb
*musb
= glue_to_musb(glue
);
274 struct device
*dev
= musb
->controller
;
275 struct musb_hdrc_platform_data
*pdata
= dev
->platform_data
;
276 struct omap_musb_board_data
*data
= pdata
->board_data
;
277 struct usb_otg
*otg
= musb
->xceiv
->otg
;
279 switch (glue
->status
) {
280 case OMAP_MUSB_ID_GROUND
:
281 dev_dbg(dev
, "ID GND\n");
283 otg
->default_a
= true;
284 musb
->xceiv
->state
= OTG_STATE_A_IDLE
;
285 musb
->xceiv
->last_event
= USB_EVENT_ID
;
286 if (musb
->gadget_driver
) {
287 pm_runtime_get_sync(dev
);
288 val
= AVALID
| VBUSVALID
;
289 omap4_usb_phy_mailbox(glue
, val
);
290 omap2430_musb_set_vbus(musb
, 1);
294 case OMAP_MUSB_VBUS_VALID
:
295 dev_dbg(dev
, "VBUS Connect\n");
297 otg
->default_a
= false;
298 musb
->xceiv
->state
= OTG_STATE_B_IDLE
;
299 musb
->xceiv
->last_event
= USB_EVENT_VBUS
;
300 if (musb
->gadget_driver
)
301 pm_runtime_get_sync(dev
);
302 val
= IDDIG
| AVALID
| VBUSVALID
;
303 omap4_usb_phy_mailbox(glue
, val
);
306 case OMAP_MUSB_ID_FLOAT
:
307 case OMAP_MUSB_VBUS_OFF
:
308 dev_dbg(dev
, "VBUS Disconnect\n");
310 musb
->xceiv
->last_event
= USB_EVENT_NONE
;
311 if (musb
->gadget_driver
) {
312 pm_runtime_mark_last_busy(dev
);
313 pm_runtime_put_autosuspend(dev
);
316 if (data
->interface_type
== MUSB_INTERFACE_UTMI
) {
317 if (musb
->xceiv
->otg
->set_vbus
)
318 otg_set_vbus(musb
->xceiv
->otg
, 0);
320 val
= SESSEND
| IDDIG
;
321 omap4_usb_phy_mailbox(glue
, val
);
324 dev_dbg(dev
, "ID float\n");
329 static void omap_musb_mailbox_work(struct work_struct
*mailbox_work
)
331 struct omap2430_glue
*glue
= container_of(mailbox_work
,
332 struct omap2430_glue
, omap_musb_mailbox_work
);
333 omap_musb_set_mailbox(glue
);
336 static irqreturn_t
omap2430_musb_interrupt(int irq
, void *__hci
)
339 irqreturn_t retval
= IRQ_NONE
;
340 struct musb
*musb
= __hci
;
342 spin_lock_irqsave(&musb
->lock
, flags
);
344 musb
->int_usb
= musb_readb(musb
->mregs
, MUSB_INTRUSB
);
345 musb
->int_tx
= musb_readw(musb
->mregs
, MUSB_INTRTX
);
346 musb
->int_rx
= musb_readw(musb
->mregs
, MUSB_INTRRX
);
348 if (musb
->int_usb
|| musb
->int_tx
|| musb
->int_rx
)
349 retval
= musb_interrupt(musb
);
351 spin_unlock_irqrestore(&musb
->lock
, flags
);
356 static int omap2430_musb_init(struct musb
*musb
)
360 struct device
*dev
= musb
->controller
;
361 struct omap2430_glue
*glue
= dev_get_drvdata(dev
->parent
);
362 struct musb_hdrc_platform_data
*plat
= dev
->platform_data
;
363 struct omap_musb_board_data
*data
= plat
->board_data
;
365 /* We require some kind of external transceiver, hooked
366 * up through ULPI. TWL4030-family PMICs include one,
367 * which needs a driver, drivers aren't always needed.
369 musb
->xceiv
= devm_usb_get_phy(dev
, USB_PHY_TYPE_USB2
);
370 if (IS_ERR_OR_NULL(musb
->xceiv
)) {
371 pr_err("HS USB OTG: no transceiver configured\n");
375 musb
->isr
= omap2430_musb_interrupt
;
377 status
= pm_runtime_get_sync(dev
);
379 dev_err(dev
, "pm_runtime_get_sync FAILED %d\n", status
);
383 l
= musb_readl(musb
->mregs
, OTG_INTERFSEL
);
385 if (data
->interface_type
== MUSB_INTERFACE_UTMI
) {
386 /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
387 l
&= ~ULPI_12PIN
; /* Disable ULPI */
388 l
|= UTMI_8BIT
; /* Enable UTMI */
393 musb_writel(musb
->mregs
, OTG_INTERFSEL
, l
);
395 pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
396 "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
397 musb_readl(musb
->mregs
, OTG_REVISION
),
398 musb_readl(musb
->mregs
, OTG_SYSCONFIG
),
399 musb_readl(musb
->mregs
, OTG_SYSSTATUS
),
400 musb_readl(musb
->mregs
, OTG_INTERFSEL
),
401 musb_readl(musb
->mregs
, OTG_SIMENABLE
));
403 setup_timer(&musb_idle_timer
, musb_do_idle
, (unsigned long) musb
);
405 if (glue
->status
!= OMAP_MUSB_UNKNOWN
)
406 omap_musb_set_mailbox(glue
);
408 pm_runtime_put_noidle(musb
->controller
);
415 static void omap2430_musb_enable(struct musb
*musb
)
419 unsigned long timeout
= jiffies
+ msecs_to_jiffies(1000);
420 struct device
*dev
= musb
->controller
;
421 struct omap2430_glue
*glue
= dev_get_drvdata(dev
->parent
);
422 struct musb_hdrc_platform_data
*pdata
= dev
->platform_data
;
423 struct omap_musb_board_data
*data
= pdata
->board_data
;
425 switch (glue
->status
) {
427 case OMAP_MUSB_ID_GROUND
:
428 val
= AVALID
| VBUSVALID
;
429 omap4_usb_phy_mailbox(glue
, val
);
430 if (data
->interface_type
!= MUSB_INTERFACE_UTMI
)
432 devctl
= musb_readb(musb
->mregs
, MUSB_DEVCTL
);
433 /* start the session */
434 devctl
|= MUSB_DEVCTL_SESSION
;
435 musb_writeb(musb
->mregs
, MUSB_DEVCTL
, devctl
);
436 while (musb_readb(musb
->mregs
, MUSB_DEVCTL
) &
437 MUSB_DEVCTL_BDEVICE
) {
440 if (time_after(jiffies
, timeout
)) {
441 dev_err(dev
, "configured as A device timeout");
447 case OMAP_MUSB_VBUS_VALID
:
448 val
= IDDIG
| AVALID
| VBUSVALID
;
449 omap4_usb_phy_mailbox(glue
, val
);
457 static void omap2430_musb_disable(struct musb
*musb
)
460 struct device
*dev
= musb
->controller
;
461 struct omap2430_glue
*glue
= dev_get_drvdata(dev
->parent
);
463 if (glue
->status
!= OMAP_MUSB_UNKNOWN
) {
464 val
= SESSEND
| IDDIG
;
465 omap4_usb_phy_mailbox(glue
, val
);
469 static int omap2430_musb_exit(struct musb
*musb
)
471 del_timer_sync(&musb_idle_timer
);
473 omap2430_low_level_exit(musb
);
478 static const struct musb_platform_ops omap2430_ops
= {
479 .init
= omap2430_musb_init
,
480 .exit
= omap2430_musb_exit
,
482 .set_mode
= omap2430_musb_set_mode
,
483 .try_idle
= omap2430_musb_try_idle
,
485 .set_vbus
= omap2430_musb_set_vbus
,
487 .enable
= omap2430_musb_enable
,
488 .disable
= omap2430_musb_disable
,
491 static u64 omap2430_dmamask
= DMA_BIT_MASK(32);
493 static int omap2430_probe(struct platform_device
*pdev
)
495 struct musb_hdrc_platform_data
*pdata
= pdev
->dev
.platform_data
;
496 struct omap_musb_board_data
*data
;
497 struct platform_device
*musb
;
498 struct omap2430_glue
*glue
;
499 struct device_node
*np
= pdev
->dev
.of_node
;
500 struct musb_hdrc_config
*config
;
501 struct resource
*res
;
504 glue
= devm_kzalloc(&pdev
->dev
, sizeof(*glue
), GFP_KERNEL
);
506 dev_err(&pdev
->dev
, "failed to allocate glue context\n");
510 musb
= platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO
);
512 dev_err(&pdev
->dev
, "failed to allocate musb device\n");
516 musb
->dev
.parent
= &pdev
->dev
;
517 musb
->dev
.dma_mask
= &omap2430_dmamask
;
518 musb
->dev
.coherent_dma_mask
= omap2430_dmamask
;
520 glue
->dev
= &pdev
->dev
;
522 glue
->status
= OMAP_MUSB_UNKNOWN
;
524 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
526 glue
->control_otghs
= devm_ioremap_resource(&pdev
->dev
, res
);
529 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
532 "failed to allocate musb platfrom data\n");
537 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
540 "failed to allocate musb board data\n");
545 config
= devm_kzalloc(&pdev
->dev
, sizeof(*config
), GFP_KERNEL
);
548 "failed to allocate musb hdrc config\n");
552 of_property_read_u32(np
, "mode", (u32
*)&pdata
->mode
);
553 of_property_read_u32(np
, "interface_type",
554 (u32
*)&data
->interface_type
);
555 of_property_read_u32(np
, "num_eps", (u32
*)&config
->num_eps
);
556 of_property_read_u32(np
, "ram_bits", (u32
*)&config
->ram_bits
);
557 of_property_read_u32(np
, "power", (u32
*)&pdata
->power
);
558 config
->multipoint
= of_property_read_bool(np
, "multipoint");
560 pdata
->board_data
= data
;
561 pdata
->config
= config
;
564 pdata
->platform_ops
= &omap2430_ops
;
566 platform_set_drvdata(pdev
, glue
);
569 * REVISIT if we ever have two instances of the wrapper, we will be
574 INIT_WORK(&glue
->omap_musb_mailbox_work
, omap_musb_mailbox_work
);
576 ret
= platform_device_add_resources(musb
, pdev
->resource
,
577 pdev
->num_resources
);
579 dev_err(&pdev
->dev
, "failed to add resources\n");
583 ret
= platform_device_add_data(musb
, pdata
, sizeof(*pdata
));
585 dev_err(&pdev
->dev
, "failed to add platform_data\n");
589 pm_runtime_enable(&pdev
->dev
);
591 ret
= platform_device_add(musb
);
593 dev_err(&pdev
->dev
, "failed to register musb device\n");
600 platform_device_put(musb
);
606 static int omap2430_remove(struct platform_device
*pdev
)
608 struct omap2430_glue
*glue
= platform_get_drvdata(pdev
);
610 cancel_work_sync(&glue
->omap_musb_mailbox_work
);
611 platform_device_unregister(glue
->musb
);
618 static int omap2430_runtime_suspend(struct device
*dev
)
620 struct omap2430_glue
*glue
= dev_get_drvdata(dev
);
621 struct musb
*musb
= glue_to_musb(glue
);
624 musb
->context
.otg_interfsel
= musb_readl(musb
->mregs
,
627 omap2430_low_level_exit(musb
);
628 usb_phy_set_suspend(musb
->xceiv
, 1);
634 static int omap2430_runtime_resume(struct device
*dev
)
636 struct omap2430_glue
*glue
= dev_get_drvdata(dev
);
637 struct musb
*musb
= glue_to_musb(glue
);
640 omap2430_low_level_init(musb
);
641 musb_writel(musb
->mregs
, OTG_INTERFSEL
,
642 musb
->context
.otg_interfsel
);
644 usb_phy_set_suspend(musb
->xceiv
, 0);
650 static struct dev_pm_ops omap2430_pm_ops
= {
651 .runtime_suspend
= omap2430_runtime_suspend
,
652 .runtime_resume
= omap2430_runtime_resume
,
655 #define DEV_PM_OPS (&omap2430_pm_ops)
657 #define DEV_PM_OPS NULL
661 static const struct of_device_id omap2430_id_table
[] = {
663 .compatible
= "ti,omap4-musb"
666 .compatible
= "ti,omap3-musb"
670 MODULE_DEVICE_TABLE(of
, omap2430_id_table
);
673 static struct platform_driver omap2430_driver
= {
674 .probe
= omap2430_probe
,
675 .remove
= omap2430_remove
,
677 .name
= "musb-omap2430",
679 .of_match_table
= of_match_ptr(omap2430_id_table
),
683 MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
684 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
685 MODULE_LICENSE("GPL v2");
687 static int __init
omap2430_init(void)
689 return platform_driver_register(&omap2430_driver
);
691 subsys_initcall(omap2430_init
);
693 static void __exit
omap2430_exit(void)
695 platform_driver_unregister(&omap2430_driver
);
697 module_exit(omap2430_exit
);