2 * MUSB OTG driver core code
4 * Copyright 2005 Mentor Graphics Corporation
5 * Copyright (C) 2005-2006 by Texas Instruments
6 * Copyright (C) 2006-2007 Nokia Corporation
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
25 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Inventra (Multipoint) Dual-Role Controller Driver for Linux.
38 * This consists of a Host Controller Driver (HCD) and a peripheral
39 * controller driver implementing the "Gadget" API; OTG support is
40 * in the works. These are normal Linux-USB controller drivers which
41 * use IRQs and have no dedicated thread.
43 * This version of the driver has only been used with products from
44 * Texas Instruments. Those products integrate the Inventra logic
45 * with other DMA, IRQ, and bus modules, as well as other logic that
46 * needs to be reflected in this driver.
49 * NOTE: the original Mentor code here was pretty much a collection
50 * of mechanisms that don't seem to have been fully integrated/working
51 * for *any* Linux kernel version. This version aims at Linux 2.6.now,
52 * Key open issues include:
54 * - Lack of host-side transaction scheduling, for all transfer types.
55 * The hardware doesn't do it; instead, software must.
57 * This is not an issue for OTG devices that don't support external
58 * hubs, but for more "normal" USB hosts it's a user issue that the
59 * "multipoint" support doesn't scale in the expected ways. That
60 * includes DaVinci EVM in a common non-OTG mode.
62 * * Control and bulk use dedicated endpoints, and there's as
63 * yet no mechanism to either (a) reclaim the hardware when
64 * peripherals are NAKing, which gets complicated with bulk
65 * endpoints, or (b) use more than a single bulk endpoint in
68 * RESULT: one device may be perceived as blocking another one.
70 * * Interrupt and isochronous will dynamically allocate endpoint
71 * hardware, but (a) there's no record keeping for bandwidth;
72 * (b) in the common case that few endpoints are available, there
73 * is no mechanism to reuse endpoints to talk to multiple devices.
75 * RESULT: At one extreme, bandwidth can be overcommitted in
76 * some hardware configurations, no faults will be reported.
77 * At the other extreme, the bandwidth capabilities which do
78 * exist tend to be severely undercommitted. You can't yet hook
79 * up both a keyboard and a mouse to an external USB hub.
83 * This gets many kinds of configuration information:
84 * - Kconfig for everything user-configurable
85 * - platform_device for addressing, irq, and platform_data
86 * - platform_data is mostly for board-specific informarion
87 * (plus recentrly, SOC or family details)
89 * Most of the conditional compilation will (someday) vanish.
92 #include <linux/module.h>
93 #include <linux/kernel.h>
94 #include <linux/sched.h>
95 #include <linux/slab.h>
96 #include <linux/init.h>
97 #include <linux/list.h>
98 #include <linux/kobject.h>
99 #include <linux/platform_device.h>
100 #include <linux/io.h>
102 #include "musb_core.h"
104 #define TA_WAIT_BCON(m) max_t(int, (m)->a_wait_bcon, OTG_TIME_A_WAIT_BCON)
108 module_param_named(debug
, musb_debug
, uint
, S_IRUGO
| S_IWUSR
);
109 MODULE_PARM_DESC(debug
, "Debug message level. Default = 0");
111 #define DRIVER_AUTHOR "Mentor Graphics, Texas Instruments, Nokia"
112 #define DRIVER_DESC "Inventra Dual-Role USB Controller Driver"
114 #define MUSB_VERSION "6.0"
116 #define DRIVER_INFO DRIVER_DESC ", v" MUSB_VERSION
118 #define MUSB_DRIVER_NAME "musb-hdrc"
119 const char musb_driver_name
[] = MUSB_DRIVER_NAME
;
121 MODULE_DESCRIPTION(DRIVER_INFO
);
122 MODULE_AUTHOR(DRIVER_AUTHOR
);
123 MODULE_LICENSE("GPL");
124 MODULE_ALIAS("platform:" MUSB_DRIVER_NAME
);
127 /*-------------------------------------------------------------------------*/
129 static inline struct musb
*dev_to_musb(struct device
*dev
)
131 return dev_get_drvdata(dev
);
134 /*-------------------------------------------------------------------------*/
136 #ifndef CONFIG_BLACKFIN
137 static int musb_ulpi_read(struct otg_transceiver
*otg
, u32 offset
)
139 void __iomem
*addr
= otg
->io_priv
;
144 /* Make sure the transceiver is not in low power mode */
145 power
= musb_readb(addr
, MUSB_POWER
);
146 power
&= ~MUSB_POWER_SUSPENDM
;
147 musb_writeb(addr
, MUSB_POWER
, power
);
149 /* REVISIT: musbhdrc_ulpi_an.pdf recommends setting the
150 * ULPICarKitControlDisableUTMI after clearing POWER_SUSPENDM.
153 musb_writeb(addr
, MUSB_ULPI_REG_ADDR
, (u8
)offset
);
154 musb_writeb(addr
, MUSB_ULPI_REG_CONTROL
,
155 MUSB_ULPI_REG_REQ
| MUSB_ULPI_RDN_WR
);
157 while (!(musb_readb(addr
, MUSB_ULPI_REG_CONTROL
)
158 & MUSB_ULPI_REG_CMPLT
)) {
161 DBG(3, "ULPI read timed out\n");
166 r
= musb_readb(addr
, MUSB_ULPI_REG_CONTROL
);
167 r
&= ~MUSB_ULPI_REG_CMPLT
;
168 musb_writeb(addr
, MUSB_ULPI_REG_CONTROL
, r
);
170 return musb_readb(addr
, MUSB_ULPI_REG_DATA
);
173 static int musb_ulpi_write(struct otg_transceiver
*otg
,
174 u32 offset
, u32 data
)
176 void __iomem
*addr
= otg
->io_priv
;
181 /* Make sure the transceiver is not in low power mode */
182 power
= musb_readb(addr
, MUSB_POWER
);
183 power
&= ~MUSB_POWER_SUSPENDM
;
184 musb_writeb(addr
, MUSB_POWER
, power
);
186 musb_writeb(addr
, MUSB_ULPI_REG_ADDR
, (u8
)offset
);
187 musb_writeb(addr
, MUSB_ULPI_REG_DATA
, (u8
)data
);
188 musb_writeb(addr
, MUSB_ULPI_REG_CONTROL
, MUSB_ULPI_REG_REQ
);
190 while (!(musb_readb(addr
, MUSB_ULPI_REG_CONTROL
)
191 & MUSB_ULPI_REG_CMPLT
)) {
194 DBG(3, "ULPI write timed out\n");
199 r
= musb_readb(addr
, MUSB_ULPI_REG_CONTROL
);
200 r
&= ~MUSB_ULPI_REG_CMPLT
;
201 musb_writeb(addr
, MUSB_ULPI_REG_CONTROL
, r
);
206 #define musb_ulpi_read NULL
207 #define musb_ulpi_write NULL
210 static struct otg_io_access_ops musb_ulpi_access
= {
211 .read
= musb_ulpi_read
,
212 .write
= musb_ulpi_write
,
215 /*-------------------------------------------------------------------------*/
217 #if !defined(CONFIG_USB_MUSB_TUSB6010) && !defined(CONFIG_USB_MUSB_BLACKFIN)
220 * Load an endpoint's FIFO
222 void musb_write_fifo(struct musb_hw_ep
*hw_ep
, u16 len
, const u8
*src
)
224 void __iomem
*fifo
= hw_ep
->fifo
;
228 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
229 'T', hw_ep
->epnum
, fifo
, len
, src
);
231 /* we can't assume unaligned reads work */
232 if (likely((0x01 & (unsigned long) src
) == 0)) {
235 /* best case is 32bit-aligned source address */
236 if ((0x02 & (unsigned long) src
) == 0) {
238 writesl(fifo
, src
+ index
, len
>> 2);
239 index
+= len
& ~0x03;
242 musb_writew(fifo
, 0, *(u16
*)&src
[index
]);
247 writesw(fifo
, src
+ index
, len
>> 1);
248 index
+= len
& ~0x01;
252 musb_writeb(fifo
, 0, src
[index
]);
255 writesb(fifo
, src
, len
);
259 #if !defined(CONFIG_USB_MUSB_AM35X)
261 * Unload an endpoint's FIFO
263 void musb_read_fifo(struct musb_hw_ep
*hw_ep
, u16 len
, u8
*dst
)
265 void __iomem
*fifo
= hw_ep
->fifo
;
267 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
268 'R', hw_ep
->epnum
, fifo
, len
, dst
);
270 /* we can't assume unaligned writes work */
271 if (likely((0x01 & (unsigned long) dst
) == 0)) {
274 /* best case is 32bit-aligned destination address */
275 if ((0x02 & (unsigned long) dst
) == 0) {
277 readsl(fifo
, dst
, len
>> 2);
281 *(u16
*)&dst
[index
] = musb_readw(fifo
, 0);
286 readsw(fifo
, dst
, len
>> 1);
291 dst
[index
] = musb_readb(fifo
, 0);
294 readsb(fifo
, dst
, len
);
299 #endif /* normal PIO */
302 /*-------------------------------------------------------------------------*/
304 /* for high speed test mode; see USB 2.0 spec 7.1.20 */
305 static const u8 musb_test_packet
[53] = {
306 /* implicit SYNC then DATA0 to start */
309 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
311 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
313 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
314 /* JJJJJJJKKKKKKK x8 */
315 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
317 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
318 /* JKKKKKKK x10, JK */
319 0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e
321 /* implicit CRC16 then EOP to end */
324 void musb_load_testpacket(struct musb
*musb
)
326 void __iomem
*regs
= musb
->endpoints
[0].regs
;
328 musb_ep_select(musb
->mregs
, 0);
329 musb_write_fifo(musb
->control_ep
,
330 sizeof(musb_test_packet
), musb_test_packet
);
331 musb_writew(regs
, MUSB_CSR0
, MUSB_CSR0_TXPKTRDY
);
334 /*-------------------------------------------------------------------------*/
336 const char *otg_state_string(struct musb
*musb
)
338 switch (musb
->xceiv
->state
) {
339 case OTG_STATE_A_IDLE
: return "a_idle";
340 case OTG_STATE_A_WAIT_VRISE
: return "a_wait_vrise";
341 case OTG_STATE_A_WAIT_BCON
: return "a_wait_bcon";
342 case OTG_STATE_A_HOST
: return "a_host";
343 case OTG_STATE_A_SUSPEND
: return "a_suspend";
344 case OTG_STATE_A_PERIPHERAL
: return "a_peripheral";
345 case OTG_STATE_A_WAIT_VFALL
: return "a_wait_vfall";
346 case OTG_STATE_A_VBUS_ERR
: return "a_vbus_err";
347 case OTG_STATE_B_IDLE
: return "b_idle";
348 case OTG_STATE_B_SRP_INIT
: return "b_srp_init";
349 case OTG_STATE_B_PERIPHERAL
: return "b_peripheral";
350 case OTG_STATE_B_WAIT_ACON
: return "b_wait_acon";
351 case OTG_STATE_B_HOST
: return "b_host";
352 default: return "UNDEFINED";
356 #ifdef CONFIG_USB_MUSB_OTG
359 * Handles OTG hnp timeouts, such as b_ase0_brst
361 void musb_otg_timer_func(unsigned long data
)
363 struct musb
*musb
= (struct musb
*)data
;
366 spin_lock_irqsave(&musb
->lock
, flags
);
367 switch (musb
->xceiv
->state
) {
368 case OTG_STATE_B_WAIT_ACON
:
369 DBG(1, "HNP: b_wait_acon timeout; back to b_peripheral\n");
370 musb_g_disconnect(musb
);
371 musb
->xceiv
->state
= OTG_STATE_B_PERIPHERAL
;
374 case OTG_STATE_A_SUSPEND
:
375 case OTG_STATE_A_WAIT_BCON
:
376 DBG(1, "HNP: %s timeout\n", otg_state_string(musb
));
377 musb_platform_set_vbus(musb
, 0);
378 musb
->xceiv
->state
= OTG_STATE_A_WAIT_VFALL
;
381 DBG(1, "HNP: Unhandled mode %s\n", otg_state_string(musb
));
383 musb
->ignore_disconnect
= 0;
384 spin_unlock_irqrestore(&musb
->lock
, flags
);
388 * Stops the HNP transition. Caller must take care of locking.
390 void musb_hnp_stop(struct musb
*musb
)
392 struct usb_hcd
*hcd
= musb_to_hcd(musb
);
393 void __iomem
*mbase
= musb
->mregs
;
396 DBG(1, "HNP: stop from %s\n", otg_state_string(musb
));
398 switch (musb
->xceiv
->state
) {
399 case OTG_STATE_A_PERIPHERAL
:
400 musb_g_disconnect(musb
);
401 DBG(1, "HNP: back to %s\n", otg_state_string(musb
));
403 case OTG_STATE_B_HOST
:
404 DBG(1, "HNP: Disabling HR\n");
405 hcd
->self
.is_b_host
= 0;
406 musb
->xceiv
->state
= OTG_STATE_B_PERIPHERAL
;
408 reg
= musb_readb(mbase
, MUSB_POWER
);
409 reg
|= MUSB_POWER_SUSPENDM
;
410 musb_writeb(mbase
, MUSB_POWER
, reg
);
411 /* REVISIT: Start SESSION_REQUEST here? */
414 DBG(1, "HNP: Stopping in unknown state %s\n",
415 otg_state_string(musb
));
419 * When returning to A state after HNP, avoid hub_port_rebounce(),
420 * which cause occasional OPT A "Did not receive reset after connect"
423 musb
->port1_status
&= ~(USB_PORT_STAT_C_CONNECTION
<< 16);
429 * Interrupt Service Routine to record USB "global" interrupts.
430 * Since these do not happen often and signify things of
431 * paramount importance, it seems OK to check them individually;
432 * the order of the tests is specified in the manual
434 * @param musb instance pointer
435 * @param int_usb register contents
440 static irqreturn_t
musb_stage0_irq(struct musb
*musb
, u8 int_usb
,
443 irqreturn_t handled
= IRQ_NONE
;
445 DBG(3, "<== Power=%02x, DevCtl=%02x, int_usb=0x%x\n", power
, devctl
,
448 /* in host mode, the peripheral may issue remote wakeup.
449 * in peripheral mode, the host may resume the link.
450 * spurious RESUME irqs happen too, paired with SUSPEND.
452 if (int_usb
& MUSB_INTR_RESUME
) {
453 handled
= IRQ_HANDLED
;
454 DBG(3, "RESUME (%s)\n", otg_state_string(musb
));
456 if (devctl
& MUSB_DEVCTL_HM
) {
457 #ifdef CONFIG_USB_MUSB_HDRC_HCD
458 void __iomem
*mbase
= musb
->mregs
;
460 switch (musb
->xceiv
->state
) {
461 case OTG_STATE_A_SUSPEND
:
462 /* remote wakeup? later, GetPortStatus
463 * will stop RESUME signaling
466 if (power
& MUSB_POWER_SUSPENDM
) {
468 musb
->int_usb
&= ~MUSB_INTR_SUSPEND
;
469 DBG(2, "Spurious SUSPENDM\n");
473 power
&= ~MUSB_POWER_SUSPENDM
;
474 musb_writeb(mbase
, MUSB_POWER
,
475 power
| MUSB_POWER_RESUME
);
477 musb
->port1_status
|=
478 (USB_PORT_STAT_C_SUSPEND
<< 16)
479 | MUSB_PORT_STAT_RESUME
;
480 musb
->rh_timer
= jiffies
481 + msecs_to_jiffies(20);
483 musb
->xceiv
->state
= OTG_STATE_A_HOST
;
485 usb_hcd_resume_root_hub(musb_to_hcd(musb
));
487 case OTG_STATE_B_WAIT_ACON
:
488 musb
->xceiv
->state
= OTG_STATE_B_PERIPHERAL
;
493 WARNING("bogus %s RESUME (%s)\n",
495 otg_state_string(musb
));
499 switch (musb
->xceiv
->state
) {
500 #ifdef CONFIG_USB_MUSB_HDRC_HCD
501 case OTG_STATE_A_SUSPEND
:
502 /* possibly DISCONNECT is upcoming */
503 musb
->xceiv
->state
= OTG_STATE_A_HOST
;
504 usb_hcd_resume_root_hub(musb_to_hcd(musb
));
507 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
508 case OTG_STATE_B_WAIT_ACON
:
509 case OTG_STATE_B_PERIPHERAL
:
510 /* disconnect while suspended? we may
511 * not get a disconnect irq...
513 if ((devctl
& MUSB_DEVCTL_VBUS
)
514 != (3 << MUSB_DEVCTL_VBUS_SHIFT
)
516 musb
->int_usb
|= MUSB_INTR_DISCONNECT
;
517 musb
->int_usb
&= ~MUSB_INTR_SUSPEND
;
522 case OTG_STATE_B_IDLE
:
523 musb
->int_usb
&= ~MUSB_INTR_SUSPEND
;
527 WARNING("bogus %s RESUME (%s)\n",
529 otg_state_string(musb
));
534 #ifdef CONFIG_USB_MUSB_HDRC_HCD
535 /* see manual for the order of the tests */
536 if (int_usb
& MUSB_INTR_SESSREQ
) {
537 void __iomem
*mbase
= musb
->mregs
;
539 if ((devctl
& MUSB_DEVCTL_VBUS
) == MUSB_DEVCTL_VBUS
540 && (devctl
& MUSB_DEVCTL_BDEVICE
)) {
541 DBG(3, "SessReq while on B state\n");
545 DBG(1, "SESSION_REQUEST (%s)\n", otg_state_string(musb
));
547 /* IRQ arrives from ID pin sense or (later, if VBUS power
548 * is removed) SRP. responses are time critical:
549 * - turn on VBUS (with silicon-specific mechanism)
550 * - go through A_WAIT_VRISE
551 * - ... to A_WAIT_BCON.
552 * a_wait_vrise_tmout triggers VBUS_ERROR transitions
554 musb_writeb(mbase
, MUSB_DEVCTL
, MUSB_DEVCTL_SESSION
);
555 musb
->ep0_stage
= MUSB_EP0_START
;
556 musb
->xceiv
->state
= OTG_STATE_A_IDLE
;
558 musb_platform_set_vbus(musb
, 1);
560 handled
= IRQ_HANDLED
;
563 if (int_usb
& MUSB_INTR_VBUSERROR
) {
566 /* During connection as an A-Device, we may see a short
567 * current spikes causing voltage drop, because of cable
568 * and peripheral capacitance combined with vbus draw.
569 * (So: less common with truly self-powered devices, where
570 * vbus doesn't act like a power supply.)
572 * Such spikes are short; usually less than ~500 usec, max
573 * of ~2 msec. That is, they're not sustained overcurrent
574 * errors, though they're reported using VBUSERROR irqs.
576 * Workarounds: (a) hardware: use self powered devices.
577 * (b) software: ignore non-repeated VBUS errors.
579 * REVISIT: do delays from lots of DEBUG_KERNEL checks
580 * make trouble here, keeping VBUS < 4.4V ?
582 switch (musb
->xceiv
->state
) {
583 case OTG_STATE_A_HOST
:
584 /* recovery is dicey once we've gotten past the
585 * initial stages of enumeration, but if VBUS
586 * stayed ok at the other end of the link, and
587 * another reset is due (at least for high speed,
588 * to redo the chirp etc), it might work OK...
590 case OTG_STATE_A_WAIT_BCON
:
591 case OTG_STATE_A_WAIT_VRISE
:
592 if (musb
->vbuserr_retry
) {
593 void __iomem
*mbase
= musb
->mregs
;
595 musb
->vbuserr_retry
--;
597 devctl
|= MUSB_DEVCTL_SESSION
;
598 musb_writeb(mbase
, MUSB_DEVCTL
, devctl
);
600 musb
->port1_status
|=
601 USB_PORT_STAT_OVERCURRENT
602 | (USB_PORT_STAT_C_OVERCURRENT
<< 16);
609 DBG(1, "VBUS_ERROR in %s (%02x, %s), retry #%d, port1 %08x\n",
610 otg_state_string(musb
),
613 switch (devctl
& MUSB_DEVCTL_VBUS
) {
614 case 0 << MUSB_DEVCTL_VBUS_SHIFT
:
615 s
= "<SessEnd"; break;
616 case 1 << MUSB_DEVCTL_VBUS_SHIFT
:
617 s
= "<AValid"; break;
618 case 2 << MUSB_DEVCTL_VBUS_SHIFT
:
619 s
= "<VBusValid"; break;
620 /* case 3 << MUSB_DEVCTL_VBUS_SHIFT: */
624 VBUSERR_RETRY_COUNT
- musb
->vbuserr_retry
,
627 /* go through A_WAIT_VFALL then start a new session */
629 musb_platform_set_vbus(musb
, 0);
630 handled
= IRQ_HANDLED
;
634 if (int_usb
& MUSB_INTR_SUSPEND
) {
635 DBG(1, "SUSPEND (%s) devctl %02x power %02x\n",
636 otg_state_string(musb
), devctl
, power
);
637 handled
= IRQ_HANDLED
;
639 switch (musb
->xceiv
->state
) {
640 #ifdef CONFIG_USB_MUSB_OTG
641 case OTG_STATE_A_PERIPHERAL
:
642 /* We also come here if the cable is removed, since
643 * this silicon doesn't report ID-no-longer-grounded.
645 * We depend on T(a_wait_bcon) to shut us down, and
646 * hope users don't do anything dicey during this
647 * undesired detour through A_WAIT_BCON.
650 usb_hcd_resume_root_hub(musb_to_hcd(musb
));
651 musb_root_disconnect(musb
);
652 musb_platform_try_idle(musb
, jiffies
653 + msecs_to_jiffies(musb
->a_wait_bcon
654 ? : OTG_TIME_A_WAIT_BCON
));
658 case OTG_STATE_B_IDLE
:
659 if (!musb
->is_active
)
661 case OTG_STATE_B_PERIPHERAL
:
662 musb_g_suspend(musb
);
663 musb
->is_active
= is_otg_enabled(musb
)
664 && musb
->xceiv
->gadget
->b_hnp_enable
;
665 if (musb
->is_active
) {
666 #ifdef CONFIG_USB_MUSB_OTG
667 musb
->xceiv
->state
= OTG_STATE_B_WAIT_ACON
;
668 DBG(1, "HNP: Setting timer for b_ase0_brst\n");
669 mod_timer(&musb
->otg_timer
, jiffies
671 OTG_TIME_B_ASE0_BRST
));
675 case OTG_STATE_A_WAIT_BCON
:
676 if (musb
->a_wait_bcon
!= 0)
677 musb_platform_try_idle(musb
, jiffies
678 + msecs_to_jiffies(musb
->a_wait_bcon
));
680 case OTG_STATE_A_HOST
:
681 musb
->xceiv
->state
= OTG_STATE_A_SUSPEND
;
682 musb
->is_active
= is_otg_enabled(musb
)
683 && musb
->xceiv
->host
->b_hnp_enable
;
685 case OTG_STATE_B_HOST
:
686 /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
687 DBG(1, "REVISIT: SUSPEND as B_HOST\n");
690 /* "should not happen" */
696 #ifdef CONFIG_USB_MUSB_HDRC_HCD
697 if (int_usb
& MUSB_INTR_CONNECT
) {
698 struct usb_hcd
*hcd
= musb_to_hcd(musb
);
700 handled
= IRQ_HANDLED
;
702 set_bit(HCD_FLAG_SAW_IRQ
, &hcd
->flags
);
704 musb
->ep0_stage
= MUSB_EP0_START
;
706 #ifdef CONFIG_USB_MUSB_OTG
707 /* flush endpoints when transitioning from Device Mode */
708 if (is_peripheral_active(musb
)) {
709 /* REVISIT HNP; just force disconnect */
711 musb_writew(musb
->mregs
, MUSB_INTRTXE
, musb
->epmask
);
712 musb_writew(musb
->mregs
, MUSB_INTRRXE
, musb
->epmask
& 0xfffe);
713 musb_writeb(musb
->mregs
, MUSB_INTRUSBE
, 0xf7);
715 musb
->port1_status
&= ~(USB_PORT_STAT_LOW_SPEED
716 |USB_PORT_STAT_HIGH_SPEED
717 |USB_PORT_STAT_ENABLE
719 musb
->port1_status
|= USB_PORT_STAT_CONNECTION
720 |(USB_PORT_STAT_C_CONNECTION
<< 16);
722 /* high vs full speed is just a guess until after reset */
723 if (devctl
& MUSB_DEVCTL_LSDEV
)
724 musb
->port1_status
|= USB_PORT_STAT_LOW_SPEED
;
726 /* indicate new connection to OTG machine */
727 switch (musb
->xceiv
->state
) {
728 case OTG_STATE_B_PERIPHERAL
:
729 if (int_usb
& MUSB_INTR_SUSPEND
) {
730 DBG(1, "HNP: SUSPEND+CONNECT, now b_host\n");
731 int_usb
&= ~MUSB_INTR_SUSPEND
;
734 DBG(1, "CONNECT as b_peripheral???\n");
736 case OTG_STATE_B_WAIT_ACON
:
737 DBG(1, "HNP: CONNECT, now b_host\n");
739 musb
->xceiv
->state
= OTG_STATE_B_HOST
;
740 hcd
->self
.is_b_host
= 1;
741 musb
->ignore_disconnect
= 0;
742 del_timer(&musb
->otg_timer
);
745 if ((devctl
& MUSB_DEVCTL_VBUS
)
746 == (3 << MUSB_DEVCTL_VBUS_SHIFT
)) {
747 musb
->xceiv
->state
= OTG_STATE_A_HOST
;
748 hcd
->self
.is_b_host
= 0;
753 /* poke the root hub */
756 usb_hcd_poll_rh_status(hcd
);
758 usb_hcd_resume_root_hub(hcd
);
760 DBG(1, "CONNECT (%s) devctl %02x\n",
761 otg_state_string(musb
), devctl
);
763 #endif /* CONFIG_USB_MUSB_HDRC_HCD */
765 if ((int_usb
& MUSB_INTR_DISCONNECT
) && !musb
->ignore_disconnect
) {
766 DBG(1, "DISCONNECT (%s) as %s, devctl %02x\n",
767 otg_state_string(musb
),
768 MUSB_MODE(musb
), devctl
);
769 handled
= IRQ_HANDLED
;
771 switch (musb
->xceiv
->state
) {
772 #ifdef CONFIG_USB_MUSB_HDRC_HCD
773 case OTG_STATE_A_HOST
:
774 case OTG_STATE_A_SUSPEND
:
775 usb_hcd_resume_root_hub(musb_to_hcd(musb
));
776 musb_root_disconnect(musb
);
777 if (musb
->a_wait_bcon
!= 0 && is_otg_enabled(musb
))
778 musb_platform_try_idle(musb
, jiffies
779 + msecs_to_jiffies(musb
->a_wait_bcon
));
782 #ifdef CONFIG_USB_MUSB_OTG
783 case OTG_STATE_B_HOST
:
784 /* REVISIT this behaves for "real disconnect"
785 * cases; make sure the other transitions from
786 * from B_HOST act right too. The B_HOST code
787 * in hnp_stop() is currently not used...
789 musb_root_disconnect(musb
);
790 musb_to_hcd(musb
)->self
.is_b_host
= 0;
791 musb
->xceiv
->state
= OTG_STATE_B_PERIPHERAL
;
793 musb_g_disconnect(musb
);
795 case OTG_STATE_A_PERIPHERAL
:
797 musb_root_disconnect(musb
);
799 case OTG_STATE_B_WAIT_ACON
:
802 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
803 case OTG_STATE_B_PERIPHERAL
:
804 case OTG_STATE_B_IDLE
:
805 musb_g_disconnect(musb
);
809 WARNING("unhandled DISCONNECT transition (%s)\n",
810 otg_state_string(musb
));
815 /* mentor saves a bit: bus reset and babble share the same irq.
816 * only host sees babble; only peripheral sees bus reset.
818 if (int_usb
& MUSB_INTR_RESET
) {
819 handled
= IRQ_HANDLED
;
820 if (is_host_capable() && (devctl
& MUSB_DEVCTL_HM
) != 0) {
822 * Looks like non-HS BABBLE can be ignored, but
823 * HS BABBLE is an error condition. For HS the solution
824 * is to avoid babble in the first place and fix what
825 * caused BABBLE. When HS BABBLE happens we can only
828 if (devctl
& (MUSB_DEVCTL_FSDEV
| MUSB_DEVCTL_LSDEV
))
829 DBG(1, "BABBLE devctl: %02x\n", devctl
);
831 ERR("Stopping host session -- babble\n");
832 musb_writeb(musb
->mregs
, MUSB_DEVCTL
, 0);
834 } else if (is_peripheral_capable()) {
835 DBG(1, "BUS RESET as %s\n", otg_state_string(musb
));
836 switch (musb
->xceiv
->state
) {
837 #ifdef CONFIG_USB_OTG
838 case OTG_STATE_A_SUSPEND
:
839 /* We need to ignore disconnect on suspend
840 * otherwise tusb 2.0 won't reconnect after a
841 * power cycle, which breaks otg compliance.
843 musb
->ignore_disconnect
= 1;
846 case OTG_STATE_A_WAIT_BCON
: /* OPT TD.4.7-900ms */
847 /* never use invalid T(a_wait_bcon) */
848 DBG(1, "HNP: in %s, %d msec timeout\n",
849 otg_state_string(musb
),
851 mod_timer(&musb
->otg_timer
, jiffies
852 + msecs_to_jiffies(TA_WAIT_BCON(musb
)));
854 case OTG_STATE_A_PERIPHERAL
:
855 musb
->ignore_disconnect
= 0;
856 del_timer(&musb
->otg_timer
);
859 case OTG_STATE_B_WAIT_ACON
:
860 DBG(1, "HNP: RESET (%s), to b_peripheral\n",
861 otg_state_string(musb
));
862 musb
->xceiv
->state
= OTG_STATE_B_PERIPHERAL
;
866 case OTG_STATE_B_IDLE
:
867 musb
->xceiv
->state
= OTG_STATE_B_PERIPHERAL
;
869 case OTG_STATE_B_PERIPHERAL
:
873 DBG(1, "Unhandled BUS RESET as %s\n",
874 otg_state_string(musb
));
880 /* REVISIT ... this would be for multiplexing periodic endpoints, or
881 * supporting transfer phasing to prevent exceeding ISO bandwidth
882 * limits of a given frame or microframe.
884 * It's not needed for peripheral side, which dedicates endpoints;
885 * though it _might_ use SOF irqs for other purposes.
887 * And it's not currently needed for host side, which also dedicates
888 * endpoints, relies on TX/RX interval registers, and isn't claimed
889 * to support ISO transfers yet.
891 if (int_usb
& MUSB_INTR_SOF
) {
892 void __iomem
*mbase
= musb
->mregs
;
893 struct musb_hw_ep
*ep
;
897 DBG(6, "START_OF_FRAME\n");
898 handled
= IRQ_HANDLED
;
900 /* start any periodic Tx transfers waiting for current frame */
901 frame
= musb_readw(mbase
, MUSB_FRAME
);
902 ep
= musb
->endpoints
;
903 for (epnum
= 1; (epnum
< musb
->nr_endpoints
)
904 && (musb
->epmask
>= (1 << epnum
));
907 * FIXME handle framecounter wraps (12 bits)
908 * eliminate duplicated StartUrb logic
910 if (ep
->dwWaitFrame
>= frame
) {
912 pr_debug("SOF --> periodic TX%s on %d\n",
913 ep
->tx_channel
? " DMA" : "",
916 musb_h_tx_start(musb
, epnum
);
918 cppi_hostdma_start(musb
, epnum
);
920 } /* end of for loop */
924 schedule_work(&musb
->irq_work
);
929 /*-------------------------------------------------------------------------*/
932 * Program the HDRC to start (enable interrupts, dma, etc.).
934 void musb_start(struct musb
*musb
)
936 void __iomem
*regs
= musb
->mregs
;
937 u8 devctl
= musb_readb(regs
, MUSB_DEVCTL
);
939 DBG(2, "<== devctl %02x\n", devctl
);
941 /* Set INT enable registers, enable interrupts */
942 musb_writew(regs
, MUSB_INTRTXE
, musb
->epmask
);
943 musb_writew(regs
, MUSB_INTRRXE
, musb
->epmask
& 0xfffe);
944 musb_writeb(regs
, MUSB_INTRUSBE
, 0xf7);
946 musb_writeb(regs
, MUSB_TESTMODE
, 0);
948 /* put into basic highspeed mode and start session */
949 musb_writeb(regs
, MUSB_POWER
, MUSB_POWER_ISOUPDATE
950 | MUSB_POWER_SOFTCONN
952 /* ENSUSPEND wedges tusb */
953 /* | MUSB_POWER_ENSUSPEND */
957 devctl
= musb_readb(regs
, MUSB_DEVCTL
);
958 devctl
&= ~MUSB_DEVCTL_SESSION
;
960 if (is_otg_enabled(musb
)) {
961 /* session started after:
962 * (a) ID-grounded irq, host mode;
963 * (b) vbus present/connect IRQ, peripheral mode;
964 * (c) peripheral initiates, using SRP
966 if ((devctl
& MUSB_DEVCTL_VBUS
) == MUSB_DEVCTL_VBUS
)
969 devctl
|= MUSB_DEVCTL_SESSION
;
971 } else if (is_host_enabled(musb
)) {
972 /* assume ID pin is hard-wired to ground */
973 devctl
|= MUSB_DEVCTL_SESSION
;
975 } else /* peripheral is enabled */ {
976 if ((devctl
& MUSB_DEVCTL_VBUS
) == MUSB_DEVCTL_VBUS
)
979 musb_platform_enable(musb
);
980 musb_writeb(regs
, MUSB_DEVCTL
, devctl
);
984 static void musb_generic_disable(struct musb
*musb
)
986 void __iomem
*mbase
= musb
->mregs
;
989 /* disable interrupts */
990 musb_writeb(mbase
, MUSB_INTRUSBE
, 0);
991 musb_writew(mbase
, MUSB_INTRTXE
, 0);
992 musb_writew(mbase
, MUSB_INTRRXE
, 0);
995 musb_writeb(mbase
, MUSB_DEVCTL
, 0);
997 /* flush pending interrupts */
998 temp
= musb_readb(mbase
, MUSB_INTRUSB
);
999 temp
= musb_readw(mbase
, MUSB_INTRTX
);
1000 temp
= musb_readw(mbase
, MUSB_INTRRX
);
1005 * Make the HDRC stop (disable interrupts, etc.);
1006 * reversible by musb_start
1007 * called on gadget driver unregister
1008 * with controller locked, irqs blocked
1009 * acts as a NOP unless some role activated the hardware
1011 void musb_stop(struct musb
*musb
)
1013 /* stop IRQs, timers, ... */
1014 musb_platform_disable(musb
);
1015 musb_generic_disable(musb
);
1016 DBG(3, "HDRC disabled\n");
1019 * - mark host and/or peripheral drivers unusable/inactive
1020 * - disable DMA (and enable it in HdrcStart)
1021 * - make sure we can musb_start() after musb_stop(); with
1022 * OTG mode, gadget driver module rmmod/modprobe cycles that
1025 musb_platform_try_idle(musb
, 0);
1028 static void musb_shutdown(struct platform_device
*pdev
)
1030 struct musb
*musb
= dev_to_musb(&pdev
->dev
);
1031 unsigned long flags
;
1033 pm_runtime_get_sync(musb
->controller
);
1034 spin_lock_irqsave(&musb
->lock
, flags
);
1035 musb_platform_disable(musb
);
1036 musb_generic_disable(musb
);
1037 spin_unlock_irqrestore(&musb
->lock
, flags
);
1039 if (!is_otg_enabled(musb
) && is_host_enabled(musb
))
1040 usb_remove_hcd(musb_to_hcd(musb
));
1041 musb_writeb(musb
->mregs
, MUSB_DEVCTL
, 0);
1042 musb_platform_exit(musb
);
1044 pm_runtime_put(musb
->controller
);
1045 /* FIXME power down */
1049 /*-------------------------------------------------------------------------*/
1052 * The silicon either has hard-wired endpoint configurations, or else
1053 * "dynamic fifo" sizing. The driver has support for both, though at this
1054 * writing only the dynamic sizing is very well tested. Since we switched
1055 * away from compile-time hardware parameters, we can no longer rely on
1056 * dead code elimination to leave only the relevant one in the object file.
1058 * We don't currently use dynamic fifo setup capability to do anything
1059 * more than selecting one of a bunch of predefined configurations.
1061 #if defined(CONFIG_USB_MUSB_TUSB6010) || defined(CONFIG_USB_MUSB_OMAP2PLUS) \
1062 || defined(CONFIG_USB_MUSB_AM35X)
1063 static ushort __initdata fifo_mode
= 4;
1064 #elif defined(CONFIG_USB_MUSB_UX500)
1065 static ushort __initdata fifo_mode
= 5;
1067 static ushort __initdata fifo_mode
= 2;
1070 /* "modprobe ... fifo_mode=1" etc */
1071 module_param(fifo_mode
, ushort
, 0);
1072 MODULE_PARM_DESC(fifo_mode
, "initial endpoint configuration");
1075 * tables defining fifo_mode values. define more if you like.
1076 * for host side, make sure both halves of ep1 are set up.
1079 /* mode 0 - fits in 2KB */
1080 static struct musb_fifo_cfg __initdata mode_0_cfg
[] = {
1081 { .hw_ep_num
= 1, .style
= FIFO_TX
, .maxpacket
= 512, },
1082 { .hw_ep_num
= 1, .style
= FIFO_RX
, .maxpacket
= 512, },
1083 { .hw_ep_num
= 2, .style
= FIFO_RXTX
, .maxpacket
= 512, },
1084 { .hw_ep_num
= 3, .style
= FIFO_RXTX
, .maxpacket
= 256, },
1085 { .hw_ep_num
= 4, .style
= FIFO_RXTX
, .maxpacket
= 256, },
1088 /* mode 1 - fits in 4KB */
1089 static struct musb_fifo_cfg __initdata mode_1_cfg
[] = {
1090 { .hw_ep_num
= 1, .style
= FIFO_TX
, .maxpacket
= 512, .mode
= BUF_DOUBLE
, },
1091 { .hw_ep_num
= 1, .style
= FIFO_RX
, .maxpacket
= 512, .mode
= BUF_DOUBLE
, },
1092 { .hw_ep_num
= 2, .style
= FIFO_RXTX
, .maxpacket
= 512, .mode
= BUF_DOUBLE
, },
1093 { .hw_ep_num
= 3, .style
= FIFO_RXTX
, .maxpacket
= 256, },
1094 { .hw_ep_num
= 4, .style
= FIFO_RXTX
, .maxpacket
= 256, },
1097 /* mode 2 - fits in 4KB */
1098 static struct musb_fifo_cfg __initdata mode_2_cfg
[] = {
1099 { .hw_ep_num
= 1, .style
= FIFO_TX
, .maxpacket
= 512, },
1100 { .hw_ep_num
= 1, .style
= FIFO_RX
, .maxpacket
= 512, },
1101 { .hw_ep_num
= 2, .style
= FIFO_TX
, .maxpacket
= 512, },
1102 { .hw_ep_num
= 2, .style
= FIFO_RX
, .maxpacket
= 512, },
1103 { .hw_ep_num
= 3, .style
= FIFO_RXTX
, .maxpacket
= 256, },
1104 { .hw_ep_num
= 4, .style
= FIFO_RXTX
, .maxpacket
= 256, },
1107 /* mode 3 - fits in 4KB */
1108 static struct musb_fifo_cfg __initdata mode_3_cfg
[] = {
1109 { .hw_ep_num
= 1, .style
= FIFO_TX
, .maxpacket
= 512, .mode
= BUF_DOUBLE
, },
1110 { .hw_ep_num
= 1, .style
= FIFO_RX
, .maxpacket
= 512, .mode
= BUF_DOUBLE
, },
1111 { .hw_ep_num
= 2, .style
= FIFO_TX
, .maxpacket
= 512, },
1112 { .hw_ep_num
= 2, .style
= FIFO_RX
, .maxpacket
= 512, },
1113 { .hw_ep_num
= 3, .style
= FIFO_RXTX
, .maxpacket
= 256, },
1114 { .hw_ep_num
= 4, .style
= FIFO_RXTX
, .maxpacket
= 256, },
1117 /* mode 4 - fits in 16KB */
1118 static struct musb_fifo_cfg __initdata mode_4_cfg
[] = {
1119 { .hw_ep_num
= 1, .style
= FIFO_TX
, .maxpacket
= 512, },
1120 { .hw_ep_num
= 1, .style
= FIFO_RX
, .maxpacket
= 512, },
1121 { .hw_ep_num
= 2, .style
= FIFO_TX
, .maxpacket
= 512, },
1122 { .hw_ep_num
= 2, .style
= FIFO_RX
, .maxpacket
= 512, },
1123 { .hw_ep_num
= 3, .style
= FIFO_TX
, .maxpacket
= 512, },
1124 { .hw_ep_num
= 3, .style
= FIFO_RX
, .maxpacket
= 512, },
1125 { .hw_ep_num
= 4, .style
= FIFO_TX
, .maxpacket
= 512, },
1126 { .hw_ep_num
= 4, .style
= FIFO_RX
, .maxpacket
= 512, },
1127 { .hw_ep_num
= 5, .style
= FIFO_TX
, .maxpacket
= 512, },
1128 { .hw_ep_num
= 5, .style
= FIFO_RX
, .maxpacket
= 512, },
1129 { .hw_ep_num
= 6, .style
= FIFO_TX
, .maxpacket
= 512, },
1130 { .hw_ep_num
= 6, .style
= FIFO_RX
, .maxpacket
= 512, },
1131 { .hw_ep_num
= 7, .style
= FIFO_TX
, .maxpacket
= 512, },
1132 { .hw_ep_num
= 7, .style
= FIFO_RX
, .maxpacket
= 512, },
1133 { .hw_ep_num
= 8, .style
= FIFO_TX
, .maxpacket
= 512, },
1134 { .hw_ep_num
= 8, .style
= FIFO_RX
, .maxpacket
= 512, },
1135 { .hw_ep_num
= 9, .style
= FIFO_TX
, .maxpacket
= 512, },
1136 { .hw_ep_num
= 9, .style
= FIFO_RX
, .maxpacket
= 512, },
1137 { .hw_ep_num
= 10, .style
= FIFO_TX
, .maxpacket
= 256, },
1138 { .hw_ep_num
= 10, .style
= FIFO_RX
, .maxpacket
= 64, },
1139 { .hw_ep_num
= 11, .style
= FIFO_TX
, .maxpacket
= 256, },
1140 { .hw_ep_num
= 11, .style
= FIFO_RX
, .maxpacket
= 64, },
1141 { .hw_ep_num
= 12, .style
= FIFO_TX
, .maxpacket
= 256, },
1142 { .hw_ep_num
= 12, .style
= FIFO_RX
, .maxpacket
= 64, },
1143 { .hw_ep_num
= 13, .style
= FIFO_RXTX
, .maxpacket
= 4096, },
1144 { .hw_ep_num
= 14, .style
= FIFO_RXTX
, .maxpacket
= 1024, },
1145 { .hw_ep_num
= 15, .style
= FIFO_RXTX
, .maxpacket
= 1024, },
1148 /* mode 5 - fits in 8KB */
1149 static struct musb_fifo_cfg __initdata mode_5_cfg
[] = {
1150 { .hw_ep_num
= 1, .style
= FIFO_TX
, .maxpacket
= 512, },
1151 { .hw_ep_num
= 1, .style
= FIFO_RX
, .maxpacket
= 512, },
1152 { .hw_ep_num
= 2, .style
= FIFO_TX
, .maxpacket
= 512, },
1153 { .hw_ep_num
= 2, .style
= FIFO_RX
, .maxpacket
= 512, },
1154 { .hw_ep_num
= 3, .style
= FIFO_TX
, .maxpacket
= 512, },
1155 { .hw_ep_num
= 3, .style
= FIFO_RX
, .maxpacket
= 512, },
1156 { .hw_ep_num
= 4, .style
= FIFO_TX
, .maxpacket
= 512, },
1157 { .hw_ep_num
= 4, .style
= FIFO_RX
, .maxpacket
= 512, },
1158 { .hw_ep_num
= 5, .style
= FIFO_TX
, .maxpacket
= 512, },
1159 { .hw_ep_num
= 5, .style
= FIFO_RX
, .maxpacket
= 512, },
1160 { .hw_ep_num
= 6, .style
= FIFO_TX
, .maxpacket
= 32, },
1161 { .hw_ep_num
= 6, .style
= FIFO_RX
, .maxpacket
= 32, },
1162 { .hw_ep_num
= 7, .style
= FIFO_TX
, .maxpacket
= 32, },
1163 { .hw_ep_num
= 7, .style
= FIFO_RX
, .maxpacket
= 32, },
1164 { .hw_ep_num
= 8, .style
= FIFO_TX
, .maxpacket
= 32, },
1165 { .hw_ep_num
= 8, .style
= FIFO_RX
, .maxpacket
= 32, },
1166 { .hw_ep_num
= 9, .style
= FIFO_TX
, .maxpacket
= 32, },
1167 { .hw_ep_num
= 9, .style
= FIFO_RX
, .maxpacket
= 32, },
1168 { .hw_ep_num
= 10, .style
= FIFO_TX
, .maxpacket
= 32, },
1169 { .hw_ep_num
= 10, .style
= FIFO_RX
, .maxpacket
= 32, },
1170 { .hw_ep_num
= 11, .style
= FIFO_TX
, .maxpacket
= 32, },
1171 { .hw_ep_num
= 11, .style
= FIFO_RX
, .maxpacket
= 32, },
1172 { .hw_ep_num
= 12, .style
= FIFO_TX
, .maxpacket
= 32, },
1173 { .hw_ep_num
= 12, .style
= FIFO_RX
, .maxpacket
= 32, },
1174 { .hw_ep_num
= 13, .style
= FIFO_RXTX
, .maxpacket
= 512, },
1175 { .hw_ep_num
= 14, .style
= FIFO_RXTX
, .maxpacket
= 1024, },
1176 { .hw_ep_num
= 15, .style
= FIFO_RXTX
, .maxpacket
= 1024, },
1180 * configure a fifo; for non-shared endpoints, this may be called
1181 * once for a tx fifo and once for an rx fifo.
1183 * returns negative errno or offset for next fifo.
1186 fifo_setup(struct musb
*musb
, struct musb_hw_ep
*hw_ep
,
1187 const struct musb_fifo_cfg
*cfg
, u16 offset
)
1189 void __iomem
*mbase
= musb
->mregs
;
1191 u16 maxpacket
= cfg
->maxpacket
;
1192 u16 c_off
= offset
>> 3;
1195 /* expect hw_ep has already been zero-initialized */
1197 size
= ffs(max(maxpacket
, (u16
) 8)) - 1;
1198 maxpacket
= 1 << size
;
1201 if (cfg
->mode
== BUF_DOUBLE
) {
1202 if ((offset
+ (maxpacket
<< 1)) >
1203 (1 << (musb
->config
->ram_bits
+ 2)))
1205 c_size
|= MUSB_FIFOSZ_DPB
;
1207 if ((offset
+ maxpacket
) > (1 << (musb
->config
->ram_bits
+ 2)))
1211 /* configure the FIFO */
1212 musb_writeb(mbase
, MUSB_INDEX
, hw_ep
->epnum
);
1214 #ifdef CONFIG_USB_MUSB_HDRC_HCD
1215 /* EP0 reserved endpoint for control, bidirectional;
1216 * EP1 reserved for bulk, two unidirection halves.
1218 if (hw_ep
->epnum
== 1)
1219 musb
->bulk_ep
= hw_ep
;
1220 /* REVISIT error check: be sure ep0 can both rx and tx ... */
1222 switch (cfg
->style
) {
1224 musb_write_txfifosz(mbase
, c_size
);
1225 musb_write_txfifoadd(mbase
, c_off
);
1226 hw_ep
->tx_double_buffered
= !!(c_size
& MUSB_FIFOSZ_DPB
);
1227 hw_ep
->max_packet_sz_tx
= maxpacket
;
1230 musb_write_rxfifosz(mbase
, c_size
);
1231 musb_write_rxfifoadd(mbase
, c_off
);
1232 hw_ep
->rx_double_buffered
= !!(c_size
& MUSB_FIFOSZ_DPB
);
1233 hw_ep
->max_packet_sz_rx
= maxpacket
;
1236 musb_write_txfifosz(mbase
, c_size
);
1237 musb_write_txfifoadd(mbase
, c_off
);
1238 hw_ep
->rx_double_buffered
= !!(c_size
& MUSB_FIFOSZ_DPB
);
1239 hw_ep
->max_packet_sz_rx
= maxpacket
;
1241 musb_write_rxfifosz(mbase
, c_size
);
1242 musb_write_rxfifoadd(mbase
, c_off
);
1243 hw_ep
->tx_double_buffered
= hw_ep
->rx_double_buffered
;
1244 hw_ep
->max_packet_sz_tx
= maxpacket
;
1246 hw_ep
->is_shared_fifo
= true;
1250 /* NOTE rx and tx endpoint irqs aren't managed separately,
1251 * which happens to be ok
1253 musb
->epmask
|= (1 << hw_ep
->epnum
);
1255 return offset
+ (maxpacket
<< ((c_size
& MUSB_FIFOSZ_DPB
) ? 1 : 0));
1258 static struct musb_fifo_cfg __initdata ep0_cfg
= {
1259 .style
= FIFO_RXTX
, .maxpacket
= 64,
1262 static int __init
ep_config_from_table(struct musb
*musb
)
1264 const struct musb_fifo_cfg
*cfg
;
1267 struct musb_hw_ep
*hw_ep
= musb
->endpoints
;
1269 if (musb
->config
->fifo_cfg
) {
1270 cfg
= musb
->config
->fifo_cfg
;
1271 n
= musb
->config
->fifo_cfg_size
;
1275 switch (fifo_mode
) {
1281 n
= ARRAY_SIZE(mode_0_cfg
);
1285 n
= ARRAY_SIZE(mode_1_cfg
);
1289 n
= ARRAY_SIZE(mode_2_cfg
);
1293 n
= ARRAY_SIZE(mode_3_cfg
);
1297 n
= ARRAY_SIZE(mode_4_cfg
);
1301 n
= ARRAY_SIZE(mode_5_cfg
);
1305 printk(KERN_DEBUG
"%s: setup fifo_mode %d\n",
1306 musb_driver_name
, fifo_mode
);
1310 offset
= fifo_setup(musb
, hw_ep
, &ep0_cfg
, 0);
1311 /* assert(offset > 0) */
1313 /* NOTE: for RTL versions >= 1.400 EPINFO and RAMINFO would
1314 * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
1317 for (i
= 0; i
< n
; i
++) {
1318 u8 epn
= cfg
->hw_ep_num
;
1320 if (epn
>= musb
->config
->num_eps
) {
1321 pr_debug("%s: invalid ep %d\n",
1322 musb_driver_name
, epn
);
1325 offset
= fifo_setup(musb
, hw_ep
+ epn
, cfg
++, offset
);
1327 pr_debug("%s: mem overrun, ep %d\n",
1328 musb_driver_name
, epn
);
1332 musb
->nr_endpoints
= max(epn
, musb
->nr_endpoints
);
1335 printk(KERN_DEBUG
"%s: %d/%d max ep, %d/%d memory\n",
1337 n
+ 1, musb
->config
->num_eps
* 2 - 1,
1338 offset
, (1 << (musb
->config
->ram_bits
+ 2)));
1340 #ifdef CONFIG_USB_MUSB_HDRC_HCD
1341 if (!musb
->bulk_ep
) {
1342 pr_debug("%s: missing bulk\n", musb_driver_name
);
1352 * ep_config_from_hw - when MUSB_C_DYNFIFO_DEF is false
1353 * @param musb the controller
1355 static int __init
ep_config_from_hw(struct musb
*musb
)
1358 struct musb_hw_ep
*hw_ep
;
1359 void *mbase
= musb
->mregs
;
1362 DBG(2, "<== static silicon ep config\n");
1364 /* FIXME pick up ep0 maxpacket size */
1366 for (epnum
= 1; epnum
< musb
->config
->num_eps
; epnum
++) {
1367 musb_ep_select(mbase
, epnum
);
1368 hw_ep
= musb
->endpoints
+ epnum
;
1370 ret
= musb_read_fifosize(musb
, hw_ep
, epnum
);
1374 /* FIXME set up hw_ep->{rx,tx}_double_buffered */
1376 #ifdef CONFIG_USB_MUSB_HDRC_HCD
1377 /* pick an RX/TX endpoint for bulk */
1378 if (hw_ep
->max_packet_sz_tx
< 512
1379 || hw_ep
->max_packet_sz_rx
< 512)
1382 /* REVISIT: this algorithm is lazy, we should at least
1383 * try to pick a double buffered endpoint.
1387 musb
->bulk_ep
= hw_ep
;
1391 #ifdef CONFIG_USB_MUSB_HDRC_HCD
1392 if (!musb
->bulk_ep
) {
1393 pr_debug("%s: missing bulk\n", musb_driver_name
);
1401 enum { MUSB_CONTROLLER_MHDRC
, MUSB_CONTROLLER_HDRC
, };
1403 /* Initialize MUSB (M)HDRC part of the USB hardware subsystem;
1404 * configure endpoints, or take their config from silicon
1406 static int __init
musb_core_init(u16 musb_type
, struct musb
*musb
)
1410 char aInfo
[90], aRevision
[32], aDate
[12];
1411 void __iomem
*mbase
= musb
->mregs
;
1415 /* log core options (read using indexed model) */
1416 reg
= musb_read_configdata(mbase
);
1418 strcpy(aInfo
, (reg
& MUSB_CONFIGDATA_UTMIDW
) ? "UTMI-16" : "UTMI-8");
1419 if (reg
& MUSB_CONFIGDATA_DYNFIFO
) {
1420 strcat(aInfo
, ", dyn FIFOs");
1421 musb
->dyn_fifo
= true;
1423 if (reg
& MUSB_CONFIGDATA_MPRXE
) {
1424 strcat(aInfo
, ", bulk combine");
1425 musb
->bulk_combine
= true;
1427 if (reg
& MUSB_CONFIGDATA_MPTXE
) {
1428 strcat(aInfo
, ", bulk split");
1429 musb
->bulk_split
= true;
1431 if (reg
& MUSB_CONFIGDATA_HBRXE
) {
1432 strcat(aInfo
, ", HB-ISO Rx");
1433 musb
->hb_iso_rx
= true;
1435 if (reg
& MUSB_CONFIGDATA_HBTXE
) {
1436 strcat(aInfo
, ", HB-ISO Tx");
1437 musb
->hb_iso_tx
= true;
1439 if (reg
& MUSB_CONFIGDATA_SOFTCONE
)
1440 strcat(aInfo
, ", SoftConn");
1442 printk(KERN_DEBUG
"%s: ConfigData=0x%02x (%s)\n",
1443 musb_driver_name
, reg
, aInfo
);
1446 if (MUSB_CONTROLLER_MHDRC
== musb_type
) {
1447 musb
->is_multipoint
= 1;
1450 musb
->is_multipoint
= 0;
1452 #ifdef CONFIG_USB_MUSB_HDRC_HCD
1453 #ifndef CONFIG_USB_OTG_BLACKLIST_HUB
1455 "%s: kernel must blacklist external hubs\n",
1461 /* log release info */
1462 musb
->hwvers
= musb_read_hwvers(mbase
);
1463 snprintf(aRevision
, 32, "%d.%d%s", MUSB_HWVERS_MAJOR(musb
->hwvers
),
1464 MUSB_HWVERS_MINOR(musb
->hwvers
),
1465 (musb
->hwvers
& MUSB_HWVERS_RC
) ? "RC" : "");
1466 printk(KERN_DEBUG
"%s: %sHDRC RTL version %s %s\n",
1467 musb_driver_name
, type
, aRevision
, aDate
);
1470 musb_configure_ep0(musb
);
1472 /* discover endpoint configuration */
1473 musb
->nr_endpoints
= 1;
1477 status
= ep_config_from_table(musb
);
1479 status
= ep_config_from_hw(musb
);
1484 /* finish init, and print endpoint config */
1485 for (i
= 0; i
< musb
->nr_endpoints
; i
++) {
1486 struct musb_hw_ep
*hw_ep
= musb
->endpoints
+ i
;
1488 hw_ep
->fifo
= MUSB_FIFO_OFFSET(i
) + mbase
;
1489 #ifdef CONFIG_USB_MUSB_TUSB6010
1490 hw_ep
->fifo_async
= musb
->async
+ 0x400 + MUSB_FIFO_OFFSET(i
);
1491 hw_ep
->fifo_sync
= musb
->sync
+ 0x400 + MUSB_FIFO_OFFSET(i
);
1492 hw_ep
->fifo_sync_va
=
1493 musb
->sync_va
+ 0x400 + MUSB_FIFO_OFFSET(i
);
1496 hw_ep
->conf
= mbase
- 0x400 + TUSB_EP0_CONF
;
1498 hw_ep
->conf
= mbase
+ 0x400 + (((i
- 1) & 0xf) << 2);
1501 hw_ep
->regs
= MUSB_EP_OFFSET(i
, 0) + mbase
;
1502 #ifdef CONFIG_USB_MUSB_HDRC_HCD
1503 hw_ep
->target_regs
= musb_read_target_reg_base(i
, mbase
);
1504 hw_ep
->rx_reinit
= 1;
1505 hw_ep
->tx_reinit
= 1;
1508 if (hw_ep
->max_packet_sz_tx
) {
1510 "%s: hw_ep %d%s, %smax %d\n",
1511 musb_driver_name
, i
,
1512 hw_ep
->is_shared_fifo
? "shared" : "tx",
1513 hw_ep
->tx_double_buffered
1514 ? "doublebuffer, " : "",
1515 hw_ep
->max_packet_sz_tx
);
1517 if (hw_ep
->max_packet_sz_rx
&& !hw_ep
->is_shared_fifo
) {
1519 "%s: hw_ep %d%s, %smax %d\n",
1520 musb_driver_name
, i
,
1522 hw_ep
->rx_double_buffered
1523 ? "doublebuffer, " : "",
1524 hw_ep
->max_packet_sz_rx
);
1526 if (!(hw_ep
->max_packet_sz_tx
|| hw_ep
->max_packet_sz_rx
))
1527 DBG(1, "hw_ep %d not configured\n", i
);
1533 /*-------------------------------------------------------------------------*/
1535 #if defined(CONFIG_SOC_OMAP2430) || defined(CONFIG_SOC_OMAP3430) || \
1536 defined(CONFIG_ARCH_OMAP4) || defined(CONFIG_ARCH_U8500) || \
1537 defined(CONFIG_ARCH_U5500)
1539 static irqreturn_t
generic_interrupt(int irq
, void *__hci
)
1541 unsigned long flags
;
1542 irqreturn_t retval
= IRQ_NONE
;
1543 struct musb
*musb
= __hci
;
1545 spin_lock_irqsave(&musb
->lock
, flags
);
1547 musb
->int_usb
= musb_readb(musb
->mregs
, MUSB_INTRUSB
);
1548 musb
->int_tx
= musb_readw(musb
->mregs
, MUSB_INTRTX
);
1549 musb
->int_rx
= musb_readw(musb
->mregs
, MUSB_INTRRX
);
1551 if (musb
->int_usb
|| musb
->int_tx
|| musb
->int_rx
)
1552 retval
= musb_interrupt(musb
);
1554 spin_unlock_irqrestore(&musb
->lock
, flags
);
1560 #define generic_interrupt NULL
1564 * handle all the irqs defined by the HDRC core. for now we expect: other
1565 * irq sources (phy, dma, etc) will be handled first, musb->int_* values
1566 * will be assigned, and the irq will already have been acked.
1568 * called in irq context with spinlock held, irqs blocked
1570 irqreturn_t
musb_interrupt(struct musb
*musb
)
1572 irqreturn_t retval
= IRQ_NONE
;
1577 devctl
= musb_readb(musb
->mregs
, MUSB_DEVCTL
);
1578 power
= musb_readb(musb
->mregs
, MUSB_POWER
);
1580 DBG(4, "** IRQ %s usb%04x tx%04x rx%04x\n",
1581 (devctl
& MUSB_DEVCTL_HM
) ? "host" : "peripheral",
1582 musb
->int_usb
, musb
->int_tx
, musb
->int_rx
);
1584 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
1585 if (is_otg_enabled(musb
) || is_peripheral_enabled(musb
))
1586 if (!musb
->gadget_driver
) {
1587 DBG(5, "No gadget driver loaded\n");
1592 /* the core can interrupt us for multiple reasons; docs have
1593 * a generic interrupt flowchart to follow
1596 retval
|= musb_stage0_irq(musb
, musb
->int_usb
,
1599 /* "stage 1" is handling endpoint irqs */
1601 /* handle endpoint 0 first */
1602 if (musb
->int_tx
& 1) {
1603 if (devctl
& MUSB_DEVCTL_HM
)
1604 retval
|= musb_h_ep0_irq(musb
);
1606 retval
|= musb_g_ep0_irq(musb
);
1609 /* RX on endpoints 1-15 */
1610 reg
= musb
->int_rx
>> 1;
1614 /* musb_ep_select(musb->mregs, ep_num); */
1615 /* REVISIT just retval = ep->rx_irq(...) */
1616 retval
= IRQ_HANDLED
;
1617 if (devctl
& MUSB_DEVCTL_HM
) {
1618 if (is_host_capable())
1619 musb_host_rx(musb
, ep_num
);
1621 if (is_peripheral_capable())
1622 musb_g_rx(musb
, ep_num
);
1630 /* TX on endpoints 1-15 */
1631 reg
= musb
->int_tx
>> 1;
1635 /* musb_ep_select(musb->mregs, ep_num); */
1636 /* REVISIT just retval |= ep->tx_irq(...) */
1637 retval
= IRQ_HANDLED
;
1638 if (devctl
& MUSB_DEVCTL_HM
) {
1639 if (is_host_capable())
1640 musb_host_tx(musb
, ep_num
);
1642 if (is_peripheral_capable())
1643 musb_g_tx(musb
, ep_num
);
1654 #ifndef CONFIG_MUSB_PIO_ONLY
1655 static int __initdata use_dma
= 1;
1657 /* "modprobe ... use_dma=0" etc */
1658 module_param(use_dma
, bool, 0);
1659 MODULE_PARM_DESC(use_dma
, "enable/disable use of DMA");
1661 void musb_dma_completion(struct musb
*musb
, u8 epnum
, u8 transmit
)
1663 u8 devctl
= musb_readb(musb
->mregs
, MUSB_DEVCTL
);
1665 /* called with controller lock already held */
1668 #ifndef CONFIG_USB_TUSB_OMAP_DMA
1669 if (!is_cppi_enabled()) {
1671 if (devctl
& MUSB_DEVCTL_HM
)
1672 musb_h_ep0_irq(musb
);
1674 musb_g_ep0_irq(musb
);
1678 /* endpoints 1..15 */
1680 if (devctl
& MUSB_DEVCTL_HM
) {
1681 if (is_host_capable())
1682 musb_host_tx(musb
, epnum
);
1684 if (is_peripheral_capable())
1685 musb_g_tx(musb
, epnum
);
1689 if (devctl
& MUSB_DEVCTL_HM
) {
1690 if (is_host_capable())
1691 musb_host_rx(musb
, epnum
);
1693 if (is_peripheral_capable())
1694 musb_g_rx(musb
, epnum
);
1704 /*-------------------------------------------------------------------------*/
1709 musb_mode_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1711 struct musb
*musb
= dev_to_musb(dev
);
1712 unsigned long flags
;
1715 spin_lock_irqsave(&musb
->lock
, flags
);
1716 ret
= sprintf(buf
, "%s\n", otg_state_string(musb
));
1717 spin_unlock_irqrestore(&musb
->lock
, flags
);
1723 musb_mode_store(struct device
*dev
, struct device_attribute
*attr
,
1724 const char *buf
, size_t n
)
1726 struct musb
*musb
= dev_to_musb(dev
);
1727 unsigned long flags
;
1730 spin_lock_irqsave(&musb
->lock
, flags
);
1731 if (sysfs_streq(buf
, "host"))
1732 status
= musb_platform_set_mode(musb
, MUSB_HOST
);
1733 else if (sysfs_streq(buf
, "peripheral"))
1734 status
= musb_platform_set_mode(musb
, MUSB_PERIPHERAL
);
1735 else if (sysfs_streq(buf
, "otg"))
1736 status
= musb_platform_set_mode(musb
, MUSB_OTG
);
1739 spin_unlock_irqrestore(&musb
->lock
, flags
);
1741 return (status
== 0) ? n
: status
;
1743 static DEVICE_ATTR(mode
, 0644, musb_mode_show
, musb_mode_store
);
1746 musb_vbus_store(struct device
*dev
, struct device_attribute
*attr
,
1747 const char *buf
, size_t n
)
1749 struct musb
*musb
= dev_to_musb(dev
);
1750 unsigned long flags
;
1753 if (sscanf(buf
, "%lu", &val
) < 1) {
1754 dev_err(dev
, "Invalid VBUS timeout ms value\n");
1758 spin_lock_irqsave(&musb
->lock
, flags
);
1759 /* force T(a_wait_bcon) to be zero/unlimited *OR* valid */
1760 musb
->a_wait_bcon
= val
? max_t(int, val
, OTG_TIME_A_WAIT_BCON
) : 0 ;
1761 if (musb
->xceiv
->state
== OTG_STATE_A_WAIT_BCON
)
1762 musb
->is_active
= 0;
1763 musb_platform_try_idle(musb
, jiffies
+ msecs_to_jiffies(val
));
1764 spin_unlock_irqrestore(&musb
->lock
, flags
);
1770 musb_vbus_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1772 struct musb
*musb
= dev_to_musb(dev
);
1773 unsigned long flags
;
1777 spin_lock_irqsave(&musb
->lock
, flags
);
1778 val
= musb
->a_wait_bcon
;
1779 /* FIXME get_vbus_status() is normally #defined as false...
1780 * and is effectively TUSB-specific.
1782 vbus
= musb_platform_get_vbus_status(musb
);
1783 spin_unlock_irqrestore(&musb
->lock
, flags
);
1785 return sprintf(buf
, "Vbus %s, timeout %lu msec\n",
1786 vbus
? "on" : "off", val
);
1788 static DEVICE_ATTR(vbus
, 0644, musb_vbus_show
, musb_vbus_store
);
1790 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
1792 /* Gadget drivers can't know that a host is connected so they might want
1793 * to start SRP, but users can. This allows userspace to trigger SRP.
1796 musb_srp_store(struct device
*dev
, struct device_attribute
*attr
,
1797 const char *buf
, size_t n
)
1799 struct musb
*musb
= dev_to_musb(dev
);
1802 if (sscanf(buf
, "%hu", &srp
) != 1
1804 dev_err(dev
, "SRP: Value must be 1\n");
1809 musb_g_wakeup(musb
);
1813 static DEVICE_ATTR(srp
, 0644, NULL
, musb_srp_store
);
1815 #endif /* CONFIG_USB_GADGET_MUSB_HDRC */
1817 static struct attribute
*musb_attributes
[] = {
1818 &dev_attr_mode
.attr
,
1819 &dev_attr_vbus
.attr
,
1820 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
1826 static const struct attribute_group musb_attr_group
= {
1827 .attrs
= musb_attributes
,
1832 /* Only used to provide driver mode change events */
1833 static void musb_irq_work(struct work_struct
*data
)
1835 struct musb
*musb
= container_of(data
, struct musb
, irq_work
);
1836 static int old_state
;
1838 if (musb
->xceiv
->state
!= old_state
) {
1839 old_state
= musb
->xceiv
->state
;
1840 sysfs_notify(&musb
->controller
->kobj
, NULL
, "mode");
1844 /* --------------------------------------------------------------------------
1848 static struct musb
*__init
1849 allocate_instance(struct device
*dev
,
1850 struct musb_hdrc_config
*config
, void __iomem
*mbase
)
1853 struct musb_hw_ep
*ep
;
1855 #ifdef CONFIG_USB_MUSB_HDRC_HCD
1856 struct usb_hcd
*hcd
;
1858 hcd
= usb_create_hcd(&musb_hc_driver
, dev
, dev_name(dev
));
1861 /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
1863 musb
= hcd_to_musb(hcd
);
1864 INIT_LIST_HEAD(&musb
->control
);
1865 INIT_LIST_HEAD(&musb
->in_bulk
);
1866 INIT_LIST_HEAD(&musb
->out_bulk
);
1868 hcd
->uses_new_polling
= 1;
1871 musb
->vbuserr_retry
= VBUSERR_RETRY_COUNT
;
1872 musb
->a_wait_bcon
= OTG_TIME_A_WAIT_BCON
;
1874 musb
= kzalloc(sizeof *musb
, GFP_KERNEL
);
1879 dev_set_drvdata(dev
, musb
);
1880 musb
->mregs
= mbase
;
1881 musb
->ctrl_base
= mbase
;
1882 musb
->nIrq
= -ENODEV
;
1883 musb
->config
= config
;
1884 BUG_ON(musb
->config
->num_eps
> MUSB_C_NUM_EPS
);
1885 for (epnum
= 0, ep
= musb
->endpoints
;
1886 epnum
< musb
->config
->num_eps
;
1892 musb
->controller
= dev
;
1897 static void musb_free(struct musb
*musb
)
1899 /* this has multiple entry modes. it handles fault cleanup after
1900 * probe(), where things may be partially set up, as well as rmmod
1901 * cleanup after everything's been de-activated.
1905 sysfs_remove_group(&musb
->controller
->kobj
, &musb_attr_group
);
1908 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
1909 musb_gadget_cleanup(musb
);
1912 if (musb
->nIrq
>= 0) {
1914 disable_irq_wake(musb
->nIrq
);
1915 free_irq(musb
->nIrq
, musb
);
1917 if (is_dma_capable() && musb
->dma_controller
) {
1918 struct dma_controller
*c
= musb
->dma_controller
;
1921 dma_controller_destroy(c
);
1924 #ifdef CONFIG_USB_MUSB_HDRC_HCD
1925 usb_put_hcd(musb_to_hcd(musb
));
1932 * Perform generic per-controller initialization.
1934 * @pDevice: the controller (already clocked, etc)
1936 * @mregs: virtual address of controller registers,
1937 * not yet corrected for platform-specific offsets
1940 musb_init_controller(struct device
*dev
, int nIrq
, void __iomem
*ctrl
)
1944 struct musb_hdrc_platform_data
*plat
= dev
->platform_data
;
1946 /* The driver might handle more features than the board; OK.
1947 * Fail when the board needs a feature that's not enabled.
1950 dev_dbg(dev
, "no platform_data?\n");
1956 musb
= allocate_instance(dev
, plat
->config
, ctrl
);
1962 pm_runtime_use_autosuspend(musb
->controller
);
1963 pm_runtime_set_autosuspend_delay(musb
->controller
, 200);
1964 pm_runtime_enable(musb
->controller
);
1966 spin_lock_init(&musb
->lock
);
1967 musb
->board_mode
= plat
->mode
;
1968 musb
->board_set_power
= plat
->set_power
;
1969 musb
->min_power
= plat
->min_power
;
1970 musb
->ops
= plat
->platform_ops
;
1972 /* The musb_platform_init() call:
1973 * - adjusts musb->mregs and musb->isr if needed,
1974 * - may initialize an integrated tranceiver
1975 * - initializes musb->xceiv, usually by otg_get_transceiver()
1976 * - stops powering VBUS
1978 * There are various transciever configurations. Blackfin,
1979 * DaVinci, TUSB60x0, and others integrate them. OMAP3 uses
1980 * external/discrete ones in various flavors (twl4030 family,
1981 * isp1504, non-OTG, etc) mostly hooking up through ULPI.
1983 musb
->isr
= generic_interrupt
;
1984 status
= musb_platform_init(musb
);
1993 if (!musb
->xceiv
->io_ops
) {
1994 musb
->xceiv
->io_priv
= musb
->mregs
;
1995 musb
->xceiv
->io_ops
= &musb_ulpi_access
;
1998 #ifndef CONFIG_MUSB_PIO_ONLY
1999 if (use_dma
&& dev
->dma_mask
) {
2000 struct dma_controller
*c
;
2002 c
= dma_controller_create(musb
, musb
->mregs
);
2003 musb
->dma_controller
= c
;
2008 /* ideally this would be abstracted in platform setup */
2009 if (!is_dma_capable() || !musb
->dma_controller
)
2010 dev
->dma_mask
= NULL
;
2012 /* be sure interrupts are disabled before connecting ISR */
2013 musb_platform_disable(musb
);
2014 musb_generic_disable(musb
);
2016 /* setup musb parts of the core (especially endpoints) */
2017 status
= musb_core_init(plat
->config
->multipoint
2018 ? MUSB_CONTROLLER_MHDRC
2019 : MUSB_CONTROLLER_HDRC
, musb
);
2023 #ifdef CONFIG_USB_MUSB_OTG
2024 setup_timer(&musb
->otg_timer
, musb_otg_timer_func
, (unsigned long) musb
);
2027 /* Init IRQ workqueue before request_irq */
2028 INIT_WORK(&musb
->irq_work
, musb_irq_work
);
2030 /* attach to the IRQ */
2031 if (request_irq(nIrq
, musb
->isr
, 0, dev_name(dev
), musb
)) {
2032 dev_err(dev
, "request_irq %d failed!\n", nIrq
);
2037 /* FIXME this handles wakeup irqs wrong */
2038 if (enable_irq_wake(nIrq
) == 0) {
2040 device_init_wakeup(dev
, 1);
2045 /* host side needs more setup */
2046 if (is_host_enabled(musb
)) {
2047 struct usb_hcd
*hcd
= musb_to_hcd(musb
);
2049 otg_set_host(musb
->xceiv
, &hcd
->self
);
2051 if (is_otg_enabled(musb
))
2052 hcd
->self
.otg_port
= 1;
2053 musb
->xceiv
->host
= &hcd
->self
;
2054 hcd
->power_budget
= 2 * (plat
->power
? : 250);
2056 /* program PHY to use external vBus if required */
2057 if (plat
->extvbus
) {
2058 u8 busctl
= musb_read_ulpi_buscontrol(musb
->mregs
);
2059 busctl
|= MUSB_ULPI_USE_EXTVBUS
;
2060 musb_write_ulpi_buscontrol(musb
->mregs
, busctl
);
2064 /* For the host-only role, we can activate right away.
2065 * (We expect the ID pin to be forcibly grounded!!)
2066 * Otherwise, wait till the gadget driver hooks up.
2068 if (!is_otg_enabled(musb
) && is_host_enabled(musb
)) {
2069 struct usb_hcd
*hcd
= musb_to_hcd(musb
);
2071 MUSB_HST_MODE(musb
);
2072 musb
->xceiv
->default_a
= 1;
2073 musb
->xceiv
->state
= OTG_STATE_A_IDLE
;
2075 status
= usb_add_hcd(musb_to_hcd(musb
), -1, 0);
2077 hcd
->self
.uses_pio_for_control
= 1;
2078 DBG(1, "%s mode, status %d, devctl %02x %c\n",
2080 musb_readb(musb
->mregs
, MUSB_DEVCTL
),
2081 (musb_readb(musb
->mregs
, MUSB_DEVCTL
)
2082 & MUSB_DEVCTL_BDEVICE
2085 } else /* peripheral is enabled */ {
2086 MUSB_DEV_MODE(musb
);
2087 musb
->xceiv
->default_a
= 0;
2088 musb
->xceiv
->state
= OTG_STATE_B_IDLE
;
2090 status
= musb_gadget_setup(musb
);
2092 DBG(1, "%s mode, status %d, dev%02x\n",
2093 is_otg_enabled(musb
) ? "OTG" : "PERIPHERAL",
2095 musb_readb(musb
->mregs
, MUSB_DEVCTL
));
2101 pm_runtime_put(musb
->controller
);
2103 status
= musb_init_debugfs(musb
);
2108 status
= sysfs_create_group(&musb
->controller
->kobj
, &musb_attr_group
);
2113 dev_info(dev
, "USB %s mode controller at %p using %s, IRQ %d\n",
2115 switch (musb
->board_mode
) {
2116 case MUSB_HOST
: s
= "Host"; break;
2117 case MUSB_PERIPHERAL
: s
= "Peripheral"; break;
2118 default: s
= "OTG"; break;
2121 (is_dma_capable() && musb
->dma_controller
)
2128 musb_exit_debugfs(musb
);
2131 if (!is_otg_enabled(musb
) && is_host_enabled(musb
))
2132 usb_remove_hcd(musb_to_hcd(musb
));
2134 musb_gadget_cleanup(musb
);
2138 device_init_wakeup(dev
, 0);
2139 musb_platform_exit(musb
);
2142 dev_err(musb
->controller
,
2143 "musb_init_controller failed with status %d\n", status
);
2153 /*-------------------------------------------------------------------------*/
2155 /* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
2156 * bridge to a platform device; this driver then suffices.
2159 #ifndef CONFIG_MUSB_PIO_ONLY
2160 static u64
*orig_dma_mask
;
2163 static int __init
musb_probe(struct platform_device
*pdev
)
2165 struct device
*dev
= &pdev
->dev
;
2166 int irq
= platform_get_irq_byname(pdev
, "mc");
2168 struct resource
*iomem
;
2171 iomem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2172 if (!iomem
|| irq
<= 0)
2175 base
= ioremap(iomem
->start
, resource_size(iomem
));
2177 dev_err(dev
, "ioremap failed\n");
2181 #ifndef CONFIG_MUSB_PIO_ONLY
2182 /* clobbered by use_dma=n */
2183 orig_dma_mask
= dev
->dma_mask
;
2185 status
= musb_init_controller(dev
, irq
, base
);
2192 static int __exit
musb_remove(struct platform_device
*pdev
)
2194 struct musb
*musb
= dev_to_musb(&pdev
->dev
);
2195 void __iomem
*ctrl_base
= musb
->ctrl_base
;
2197 /* this gets called on rmmod.
2198 * - Host mode: host may still be active
2199 * - Peripheral mode: peripheral is deactivated (or never-activated)
2200 * - OTG mode: both roles are deactivated (or never-activated)
2202 pm_runtime_get_sync(musb
->controller
);
2203 musb_exit_debugfs(musb
);
2204 musb_shutdown(pdev
);
2206 pm_runtime_put(musb
->controller
);
2209 device_init_wakeup(&pdev
->dev
, 0);
2210 #ifndef CONFIG_MUSB_PIO_ONLY
2211 pdev
->dev
.dma_mask
= orig_dma_mask
;
2218 static void musb_save_context(struct musb
*musb
)
2221 void __iomem
*musb_base
= musb
->mregs
;
2224 if (is_host_enabled(musb
)) {
2225 musb
->context
.frame
= musb_readw(musb_base
, MUSB_FRAME
);
2226 musb
->context
.testmode
= musb_readb(musb_base
, MUSB_TESTMODE
);
2227 musb
->context
.busctl
= musb_read_ulpi_buscontrol(musb
->mregs
);
2229 musb
->context
.power
= musb_readb(musb_base
, MUSB_POWER
);
2230 musb
->context
.intrtxe
= musb_readw(musb_base
, MUSB_INTRTXE
);
2231 musb
->context
.intrrxe
= musb_readw(musb_base
, MUSB_INTRRXE
);
2232 musb
->context
.intrusbe
= musb_readb(musb_base
, MUSB_INTRUSBE
);
2233 musb
->context
.index
= musb_readb(musb_base
, MUSB_INDEX
);
2234 musb
->context
.devctl
= musb_readb(musb_base
, MUSB_DEVCTL
);
2236 for (i
= 0; i
< musb
->config
->num_eps
; ++i
) {
2237 epio
= musb
->endpoints
[i
].regs
;
2238 musb
->context
.index_regs
[i
].txmaxp
=
2239 musb_readw(epio
, MUSB_TXMAXP
);
2240 musb
->context
.index_regs
[i
].txcsr
=
2241 musb_readw(epio
, MUSB_TXCSR
);
2242 musb
->context
.index_regs
[i
].rxmaxp
=
2243 musb_readw(epio
, MUSB_RXMAXP
);
2244 musb
->context
.index_regs
[i
].rxcsr
=
2245 musb_readw(epio
, MUSB_RXCSR
);
2247 if (musb
->dyn_fifo
) {
2248 musb
->context
.index_regs
[i
].txfifoadd
=
2249 musb_read_txfifoadd(musb_base
);
2250 musb
->context
.index_regs
[i
].rxfifoadd
=
2251 musb_read_rxfifoadd(musb_base
);
2252 musb
->context
.index_regs
[i
].txfifosz
=
2253 musb_read_txfifosz(musb_base
);
2254 musb
->context
.index_regs
[i
].rxfifosz
=
2255 musb_read_rxfifosz(musb_base
);
2257 if (is_host_enabled(musb
)) {
2258 musb
->context
.index_regs
[i
].txtype
=
2259 musb_readb(epio
, MUSB_TXTYPE
);
2260 musb
->context
.index_regs
[i
].txinterval
=
2261 musb_readb(epio
, MUSB_TXINTERVAL
);
2262 musb
->context
.index_regs
[i
].rxtype
=
2263 musb_readb(epio
, MUSB_RXTYPE
);
2264 musb
->context
.index_regs
[i
].rxinterval
=
2265 musb_readb(epio
, MUSB_RXINTERVAL
);
2267 musb
->context
.index_regs
[i
].txfunaddr
=
2268 musb_read_txfunaddr(musb_base
, i
);
2269 musb
->context
.index_regs
[i
].txhubaddr
=
2270 musb_read_txhubaddr(musb_base
, i
);
2271 musb
->context
.index_regs
[i
].txhubport
=
2272 musb_read_txhubport(musb_base
, i
);
2274 musb
->context
.index_regs
[i
].rxfunaddr
=
2275 musb_read_rxfunaddr(musb_base
, i
);
2276 musb
->context
.index_regs
[i
].rxhubaddr
=
2277 musb_read_rxhubaddr(musb_base
, i
);
2278 musb
->context
.index_regs
[i
].rxhubport
=
2279 musb_read_rxhubport(musb_base
, i
);
2284 static void musb_restore_context(struct musb
*musb
)
2287 void __iomem
*musb_base
= musb
->mregs
;
2288 void __iomem
*ep_target_regs
;
2291 if (is_host_enabled(musb
)) {
2292 musb_writew(musb_base
, MUSB_FRAME
, musb
->context
.frame
);
2293 musb_writeb(musb_base
, MUSB_TESTMODE
, musb
->context
.testmode
);
2294 musb_write_ulpi_buscontrol(musb
->mregs
, musb
->context
.busctl
);
2296 musb_writeb(musb_base
, MUSB_POWER
, musb
->context
.power
);
2297 musb_writew(musb_base
, MUSB_INTRTXE
, musb
->context
.intrtxe
);
2298 musb_writew(musb_base
, MUSB_INTRRXE
, musb
->context
.intrrxe
);
2299 musb_writeb(musb_base
, MUSB_INTRUSBE
, musb
->context
.intrusbe
);
2300 musb_writeb(musb_base
, MUSB_DEVCTL
, musb
->context
.devctl
);
2302 for (i
= 0; i
< musb
->config
->num_eps
; ++i
) {
2303 epio
= musb
->endpoints
[i
].regs
;
2304 musb_writew(epio
, MUSB_TXMAXP
,
2305 musb
->context
.index_regs
[i
].txmaxp
);
2306 musb_writew(epio
, MUSB_TXCSR
,
2307 musb
->context
.index_regs
[i
].txcsr
);
2308 musb_writew(epio
, MUSB_RXMAXP
,
2309 musb
->context
.index_regs
[i
].rxmaxp
);
2310 musb_writew(epio
, MUSB_RXCSR
,
2311 musb
->context
.index_regs
[i
].rxcsr
);
2313 if (musb
->dyn_fifo
) {
2314 musb_write_txfifosz(musb_base
,
2315 musb
->context
.index_regs
[i
].txfifosz
);
2316 musb_write_rxfifosz(musb_base
,
2317 musb
->context
.index_regs
[i
].rxfifosz
);
2318 musb_write_txfifoadd(musb_base
,
2319 musb
->context
.index_regs
[i
].txfifoadd
);
2320 musb_write_rxfifoadd(musb_base
,
2321 musb
->context
.index_regs
[i
].rxfifoadd
);
2324 if (is_host_enabled(musb
)) {
2325 musb_writeb(epio
, MUSB_TXTYPE
,
2326 musb
->context
.index_regs
[i
].txtype
);
2327 musb_writeb(epio
, MUSB_TXINTERVAL
,
2328 musb
->context
.index_regs
[i
].txinterval
);
2329 musb_writeb(epio
, MUSB_RXTYPE
,
2330 musb
->context
.index_regs
[i
].rxtype
);
2331 musb_writeb(epio
, MUSB_RXINTERVAL
,
2333 musb
->context
.index_regs
[i
].rxinterval
);
2334 musb_write_txfunaddr(musb_base
, i
,
2335 musb
->context
.index_regs
[i
].txfunaddr
);
2336 musb_write_txhubaddr(musb_base
, i
,
2337 musb
->context
.index_regs
[i
].txhubaddr
);
2338 musb_write_txhubport(musb_base
, i
,
2339 musb
->context
.index_regs
[i
].txhubport
);
2342 musb_read_target_reg_base(i
, musb_base
);
2344 musb_write_rxfunaddr(ep_target_regs
,
2345 musb
->context
.index_regs
[i
].rxfunaddr
);
2346 musb_write_rxhubaddr(ep_target_regs
,
2347 musb
->context
.index_regs
[i
].rxhubaddr
);
2348 musb_write_rxhubport(ep_target_regs
,
2349 musb
->context
.index_regs
[i
].rxhubport
);
2354 static int musb_suspend(struct device
*dev
)
2356 struct platform_device
*pdev
= to_platform_device(dev
);
2357 unsigned long flags
;
2358 struct musb
*musb
= dev_to_musb(&pdev
->dev
);
2360 spin_lock_irqsave(&musb
->lock
, flags
);
2362 if (is_peripheral_active(musb
)) {
2363 /* FIXME force disconnect unless we know USB will wake
2364 * the system up quickly enough to respond ...
2366 } else if (is_host_active(musb
)) {
2367 /* we know all the children are suspended; sometimes
2368 * they will even be wakeup-enabled.
2372 musb_save_context(musb
);
2374 spin_unlock_irqrestore(&musb
->lock
, flags
);
2378 static int musb_resume_noirq(struct device
*dev
)
2380 struct platform_device
*pdev
= to_platform_device(dev
);
2381 struct musb
*musb
= dev_to_musb(&pdev
->dev
);
2383 musb_restore_context(musb
);
2385 /* for static cmos like DaVinci, register values were preserved
2386 * unless for some reason the whole soc powered down or the USB
2387 * module got reset through the PSC (vs just being disabled).
2392 static int musb_runtime_suspend(struct device
*dev
)
2394 struct musb
*musb
= dev_to_musb(dev
);
2396 musb_save_context(musb
);
2401 static int musb_runtime_resume(struct device
*dev
)
2403 struct musb
*musb
= dev_to_musb(dev
);
2404 static int first
= 1;
2407 * When pm_runtime_get_sync called for the first time in driver
2408 * init, some of the structure is still not initialized which is
2409 * used in restore function. But clock needs to be
2410 * enabled before any register access, so
2411 * pm_runtime_get_sync has to be called.
2412 * Also context restore without save does not make
2416 musb_restore_context(musb
);
2422 static const struct dev_pm_ops musb_dev_pm_ops
= {
2423 .suspend
= musb_suspend
,
2424 .resume_noirq
= musb_resume_noirq
,
2425 .runtime_suspend
= musb_runtime_suspend
,
2426 .runtime_resume
= musb_runtime_resume
,
2429 #define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
2431 #define MUSB_DEV_PM_OPS NULL
2434 static struct platform_driver musb_driver
= {
2436 .name
= (char *)musb_driver_name
,
2437 .bus
= &platform_bus_type
,
2438 .owner
= THIS_MODULE
,
2439 .pm
= MUSB_DEV_PM_OPS
,
2441 .remove
= __exit_p(musb_remove
),
2442 .shutdown
= musb_shutdown
,
2445 /*-------------------------------------------------------------------------*/
2447 static int __init
musb_init(void)
2449 #ifdef CONFIG_USB_MUSB_HDRC_HCD
2454 pr_info("%s: version " MUSB_VERSION
", "
2455 #ifdef CONFIG_MUSB_PIO_ONLY
2457 #elif defined(CONFIG_USB_TI_CPPI_DMA)
2459 #elif defined(CONFIG_USB_INVENTRA_DMA)
2461 #elif defined(CONFIG_USB_TUSB_OMAP_DMA)
2467 #ifdef CONFIG_USB_MUSB_OTG
2468 "otg (peripheral+host)"
2469 #elif defined(CONFIG_USB_GADGET_MUSB_HDRC)
2471 #elif defined(CONFIG_USB_MUSB_HDRC_HCD)
2475 musb_driver_name
, musb_debug
);
2476 return platform_driver_probe(&musb_driver
, musb_probe
);
2479 /* make us init after usbcore and i2c (transceivers, regulators, etc)
2480 * and before usb gadget and host-side drivers start to register
2482 fs_initcall(musb_init
);
2484 static void __exit
musb_cleanup(void)
2486 platform_driver_unregister(&musb_driver
);
2488 module_exit(musb_cleanup
);