2 * TUSB6010 USB 2.0 OTG Dual Role controller
4 * Copyright (C) 2006 Nokia Corporation
5 * Tony Lindgren <tony@atomide.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 * - Driver assumes that interface to external host (main CPU) is
13 * configured for NOR FLASH interface instead of VLYNQ serial
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/init.h>
21 #include <linux/usb.h>
22 #include <linux/irq.h>
23 #include <linux/platform_device.h>
24 #include <linux/dma-mapping.h>
26 #include "musb_core.h"
28 struct tusb6010_glue
{
30 struct platform_device
*musb
;
33 static void tusb_musb_set_vbus(struct musb
*musb
, int is_on
);
35 #define TUSB_REV_MAJOR(reg_val) ((reg_val >> 4) & 0xf)
36 #define TUSB_REV_MINOR(reg_val) (reg_val & 0xf)
39 * Checks the revision. We need to use the DMA register as 3.0 does not
40 * have correct versions for TUSB_PRCM_REV or TUSB_INT_CTRL_REV.
42 u8
tusb_get_revision(struct musb
*musb
)
44 void __iomem
*tbase
= musb
->ctrl_base
;
48 rev
= musb_readl(tbase
, TUSB_DMA_CTRL_REV
) & 0xff;
49 if (TUSB_REV_MAJOR(rev
) == 3) {
50 die_id
= TUSB_DIDR1_HI_CHIP_REV(musb_readl(tbase
,
52 if (die_id
>= TUSB_DIDR1_HI_REV_31
)
59 static int tusb_print_revision(struct musb
*musb
)
61 void __iomem
*tbase
= musb
->ctrl_base
;
64 rev
= tusb_get_revision(musb
);
66 pr_info("tusb: %s%i.%i %s%i.%i %s%i.%i %s%i.%i %s%i %s%i.%i\n",
68 TUSB_REV_MAJOR(musb_readl(tbase
, TUSB_PRCM_REV
)),
69 TUSB_REV_MINOR(musb_readl(tbase
, TUSB_PRCM_REV
)),
71 TUSB_REV_MAJOR(musb_readl(tbase
, TUSB_INT_CTRL_REV
)),
72 TUSB_REV_MINOR(musb_readl(tbase
, TUSB_INT_CTRL_REV
)),
74 TUSB_REV_MAJOR(musb_readl(tbase
, TUSB_GPIO_REV
)),
75 TUSB_REV_MINOR(musb_readl(tbase
, TUSB_GPIO_REV
)),
77 TUSB_REV_MAJOR(musb_readl(tbase
, TUSB_DMA_CTRL_REV
)),
78 TUSB_REV_MINOR(musb_readl(tbase
, TUSB_DMA_CTRL_REV
)),
80 TUSB_DIDR1_HI_CHIP_REV(musb_readl(tbase
, TUSB_DIDR1_HI
)),
82 TUSB_REV_MAJOR(rev
), TUSB_REV_MINOR(rev
));
84 return tusb_get_revision(musb
);
87 #define WBUS_QUIRK_MASK (TUSB_PHY_OTG_CTRL_TESTM2 | TUSB_PHY_OTG_CTRL_TESTM1 \
88 | TUSB_PHY_OTG_CTRL_TESTM0)
91 * Workaround for spontaneous WBUS wake-up issue #2 for tusb3.0.
92 * Disables power detection in PHY for the duration of idle.
94 static void tusb_wbus_quirk(struct musb
*musb
, int enabled
)
96 void __iomem
*tbase
= musb
->ctrl_base
;
97 static u32 phy_otg_ctrl
, phy_otg_ena
;
101 phy_otg_ctrl
= musb_readl(tbase
, TUSB_PHY_OTG_CTRL
);
102 phy_otg_ena
= musb_readl(tbase
, TUSB_PHY_OTG_CTRL_ENABLE
);
103 tmp
= TUSB_PHY_OTG_CTRL_WRPROTECT
104 | phy_otg_ena
| WBUS_QUIRK_MASK
;
105 musb_writel(tbase
, TUSB_PHY_OTG_CTRL
, tmp
);
106 tmp
= phy_otg_ena
& ~WBUS_QUIRK_MASK
;
107 tmp
|= TUSB_PHY_OTG_CTRL_WRPROTECT
| TUSB_PHY_OTG_CTRL_TESTM2
;
108 musb_writel(tbase
, TUSB_PHY_OTG_CTRL_ENABLE
, tmp
);
109 dev_dbg(musb
->controller
, "Enabled tusb wbus quirk ctrl %08x ena %08x\n",
110 musb_readl(tbase
, TUSB_PHY_OTG_CTRL
),
111 musb_readl(tbase
, TUSB_PHY_OTG_CTRL_ENABLE
));
112 } else if (musb_readl(tbase
, TUSB_PHY_OTG_CTRL_ENABLE
)
113 & TUSB_PHY_OTG_CTRL_TESTM2
) {
114 tmp
= TUSB_PHY_OTG_CTRL_WRPROTECT
| phy_otg_ctrl
;
115 musb_writel(tbase
, TUSB_PHY_OTG_CTRL
, tmp
);
116 tmp
= TUSB_PHY_OTG_CTRL_WRPROTECT
| phy_otg_ena
;
117 musb_writel(tbase
, TUSB_PHY_OTG_CTRL_ENABLE
, tmp
);
118 dev_dbg(musb
->controller
, "Disabled tusb wbus quirk ctrl %08x ena %08x\n",
119 musb_readl(tbase
, TUSB_PHY_OTG_CTRL
),
120 musb_readl(tbase
, TUSB_PHY_OTG_CTRL_ENABLE
));
127 * TUSB 6010 may use a parallel bus that doesn't support byte ops;
128 * so both loading and unloading FIFOs need explicit byte counts.
132 tusb_fifo_write_unaligned(void __iomem
*fifo
, const u8
*buf
, u16 len
)
138 for (i
= 0; i
< (len
>> 2); i
++) {
139 memcpy(&val
, buf
, 4);
140 musb_writel(fifo
, 0, val
);
146 /* Write the rest 1 - 3 bytes to FIFO */
147 memcpy(&val
, buf
, len
);
148 musb_writel(fifo
, 0, val
);
152 static inline void tusb_fifo_read_unaligned(void __iomem
*fifo
,
153 void __iomem
*buf
, u16 len
)
159 for (i
= 0; i
< (len
>> 2); i
++) {
160 val
= musb_readl(fifo
, 0);
161 memcpy(buf
, &val
, 4);
167 /* Read the rest 1 - 3 bytes from FIFO */
168 val
= musb_readl(fifo
, 0);
169 memcpy(buf
, &val
, len
);
173 void musb_write_fifo(struct musb_hw_ep
*hw_ep
, u16 len
, const u8
*buf
)
175 struct musb
*musb
= hw_ep
->musb
;
176 void __iomem
*ep_conf
= hw_ep
->conf
;
177 void __iomem
*fifo
= hw_ep
->fifo
;
178 u8 epnum
= hw_ep
->epnum
;
182 dev_dbg(musb
->controller
, "%cX ep%d fifo %p count %d buf %p\n",
183 'T', epnum
, fifo
, len
, buf
);
186 musb_writel(ep_conf
, TUSB_EP_TX_OFFSET
,
187 TUSB_EP_CONFIG_XFR_SIZE(len
));
189 musb_writel(ep_conf
, 0, TUSB_EP0_CONFIG_DIR_TX
|
190 TUSB_EP0_CONFIG_XFR_SIZE(len
));
192 if (likely((0x01 & (unsigned long) buf
) == 0)) {
194 /* Best case is 32bit-aligned destination address */
195 if ((0x02 & (unsigned long) buf
) == 0) {
197 writesl(fifo
, buf
, len
>> 2);
198 buf
+= (len
& ~0x03);
206 /* Cannot use writesw, fifo is 32-bit */
207 for (i
= 0; i
< (len
>> 2); i
++) {
208 val
= (u32
)(*(u16
*)buf
);
210 val
|= (*(u16
*)buf
) << 16;
212 musb_writel(fifo
, 0, val
);
220 tusb_fifo_write_unaligned(fifo
, buf
, len
);
223 void musb_read_fifo(struct musb_hw_ep
*hw_ep
, u16 len
, u8
*buf
)
225 struct musb
*musb
= hw_ep
->musb
;
226 void __iomem
*ep_conf
= hw_ep
->conf
;
227 void __iomem
*fifo
= hw_ep
->fifo
;
228 u8 epnum
= hw_ep
->epnum
;
230 dev_dbg(musb
->controller
, "%cX ep%d fifo %p count %d buf %p\n",
231 'R', epnum
, fifo
, len
, buf
);
234 musb_writel(ep_conf
, TUSB_EP_RX_OFFSET
,
235 TUSB_EP_CONFIG_XFR_SIZE(len
));
237 musb_writel(ep_conf
, 0, TUSB_EP0_CONFIG_XFR_SIZE(len
));
239 if (likely((0x01 & (unsigned long) buf
) == 0)) {
241 /* Best case is 32bit-aligned destination address */
242 if ((0x02 & (unsigned long) buf
) == 0) {
244 readsl(fifo
, buf
, len
>> 2);
245 buf
+= (len
& ~0x03);
253 /* Cannot use readsw, fifo is 32-bit */
254 for (i
= 0; i
< (len
>> 2); i
++) {
255 val
= musb_readl(fifo
, 0);
256 *(u16
*)buf
= (u16
)(val
& 0xffff);
258 *(u16
*)buf
= (u16
)(val
>> 16);
267 tusb_fifo_read_unaligned(fifo
, buf
, len
);
270 static struct musb
*the_musb
;
272 /* This is used by gadget drivers, and OTG transceiver logic, allowing
273 * at most mA current to be drawn from VBUS during a Default-B session
274 * (that is, while VBUS exceeds 4.4V). In Default-A (including pure host
275 * mode), or low power Default-B sessions, something else supplies power.
276 * Caller must take care of locking.
278 static int tusb_draw_power(struct otg_transceiver
*x
, unsigned mA
)
280 struct musb
*musb
= the_musb
;
281 void __iomem
*tbase
= musb
->ctrl_base
;
284 /* tps65030 seems to consume max 100mA, with maybe 60mA available
285 * (measured on one board) for things other than tps and tusb.
287 * Boards sharing the CPU clock with CLKIN will need to prevent
288 * certain idle sleep states while the USB link is active.
290 * REVISIT we could use VBUS to supply only _one_ of { 1.5V, 3.3V }.
291 * The actual current usage would be very board-specific. For now,
292 * it's simpler to just use an aggregate (also board-specific).
294 if (x
->default_a
|| mA
< (musb
->min_power
<< 1))
297 reg
= musb_readl(tbase
, TUSB_PRCM_MNGMT
);
299 musb
->is_bus_powered
= 1;
300 reg
|= TUSB_PRCM_MNGMT_15_SW_EN
| TUSB_PRCM_MNGMT_33_SW_EN
;
302 musb
->is_bus_powered
= 0;
303 reg
&= ~(TUSB_PRCM_MNGMT_15_SW_EN
| TUSB_PRCM_MNGMT_33_SW_EN
);
305 musb_writel(tbase
, TUSB_PRCM_MNGMT
, reg
);
307 dev_dbg(musb
->controller
, "draw max %d mA VBUS\n", mA
);
311 /* workaround for issue 13: change clock during chip idle
312 * (to be fixed in rev3 silicon) ... symptoms include disconnect
313 * or looping suspend/resume cycles
315 static void tusb_set_clock_source(struct musb
*musb
, unsigned mode
)
317 void __iomem
*tbase
= musb
->ctrl_base
;
320 reg
= musb_readl(tbase
, TUSB_PRCM_CONF
);
321 reg
&= ~TUSB_PRCM_CONF_SYS_CLKSEL(0x3);
323 /* 0 = refclk (clkin, XI)
324 * 1 = PHY 60 MHz (internal PLL)
329 reg
|= TUSB_PRCM_CONF_SYS_CLKSEL(mode
& 0x3);
331 musb_writel(tbase
, TUSB_PRCM_CONF
, reg
);
333 /* FIXME tusb6010_platform_retime(mode == 0); */
337 * Idle TUSB6010 until next wake-up event; NOR access always wakes.
338 * Other code ensures that we idle unless we're connected _and_ the
339 * USB link is not suspended ... and tells us the relevant wakeup
340 * events. SW_EN for voltage is handled separately.
342 static void tusb_allow_idle(struct musb
*musb
, u32 wakeup_enables
)
344 void __iomem
*tbase
= musb
->ctrl_base
;
347 if ((wakeup_enables
& TUSB_PRCM_WBUS
)
348 && (tusb_get_revision(musb
) == TUSB_REV_30
))
349 tusb_wbus_quirk(musb
, 1);
351 tusb_set_clock_source(musb
, 0);
353 wakeup_enables
|= TUSB_PRCM_WNORCS
;
354 musb_writel(tbase
, TUSB_PRCM_WAKEUP_MASK
, ~wakeup_enables
);
356 /* REVISIT writeup of WID implies that if WID set and ID is grounded,
357 * TUSB_PHY_OTG_CTRL.TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP must be cleared.
358 * Presumably that's mostly to save power, hence WID is immaterial ...
361 reg
= musb_readl(tbase
, TUSB_PRCM_MNGMT
);
362 /* issue 4: when driving vbus, use hipower (vbus_det) comparator */
363 if (is_host_active(musb
)) {
364 reg
|= TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN
;
365 reg
&= ~TUSB_PRCM_MNGMT_OTG_SESS_END_EN
;
367 reg
|= TUSB_PRCM_MNGMT_OTG_SESS_END_EN
;
368 reg
&= ~TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN
;
370 reg
|= TUSB_PRCM_MNGMT_PM_IDLE
| TUSB_PRCM_MNGMT_DEV_IDLE
;
371 musb_writel(tbase
, TUSB_PRCM_MNGMT
, reg
);
373 dev_dbg(musb
->controller
, "idle, wake on %02x\n", wakeup_enables
);
377 * Updates cable VBUS status. Caller must take care of locking.
379 static int tusb_musb_vbus_status(struct musb
*musb
)
381 void __iomem
*tbase
= musb
->ctrl_base
;
382 u32 otg_stat
, prcm_mngmt
;
385 otg_stat
= musb_readl(tbase
, TUSB_DEV_OTG_STAT
);
386 prcm_mngmt
= musb_readl(tbase
, TUSB_PRCM_MNGMT
);
388 /* Temporarily enable VBUS detection if it was disabled for
389 * suspend mode. Unless it's enabled otg_stat and devctl will
390 * not show correct VBUS state.
392 if (!(prcm_mngmt
& TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN
)) {
393 u32 tmp
= prcm_mngmt
;
394 tmp
|= TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN
;
395 musb_writel(tbase
, TUSB_PRCM_MNGMT
, tmp
);
396 otg_stat
= musb_readl(tbase
, TUSB_DEV_OTG_STAT
);
397 musb_writel(tbase
, TUSB_PRCM_MNGMT
, prcm_mngmt
);
400 if (otg_stat
& TUSB_DEV_OTG_STAT_VBUS_VALID
)
406 static struct timer_list musb_idle_timer
;
408 static void musb_do_idle(unsigned long _musb
)
410 struct musb
*musb
= (void *)_musb
;
413 spin_lock_irqsave(&musb
->lock
, flags
);
415 switch (musb
->xceiv
->state
) {
416 case OTG_STATE_A_WAIT_BCON
:
417 if ((musb
->a_wait_bcon
!= 0)
418 && (musb
->idle_timeout
== 0
419 || time_after(jiffies
, musb
->idle_timeout
))) {
420 dev_dbg(musb
->controller
, "Nothing connected %s, turning off VBUS\n",
421 otg_state_string(musb
->xceiv
->state
));
424 case OTG_STATE_A_IDLE
:
425 tusb_musb_set_vbus(musb
, 0);
430 if (!musb
->is_active
) {
433 /* wait until khubd handles port change status */
434 if (is_host_active(musb
) && (musb
->port1_status
>> 16))
437 if (is_peripheral_enabled(musb
) && !musb
->gadget_driver
) {
440 wakeups
= TUSB_PRCM_WHOSTDISCON
443 if (is_otg_enabled(musb
))
444 wakeups
|= TUSB_PRCM_WID
;
446 tusb_allow_idle(musb
, wakeups
);
449 spin_unlock_irqrestore(&musb
->lock
, flags
);
453 * Maybe put TUSB6010 into idle mode mode depending on USB link status,
454 * like "disconnected" or "suspended". We'll be woken out of it by
455 * connect, resume, or disconnect.
457 * Needs to be called as the last function everywhere where there is
458 * register access to TUSB6010 because of NOR flash wake-up.
459 * Caller should own controller spinlock.
461 * Delay because peripheral enables D+ pullup 3msec after SE0, and
462 * we don't want to treat that full speed J as a wakeup event.
463 * ... peripherals must draw only suspend current after 10 msec.
465 static void tusb_musb_try_idle(struct musb
*musb
, unsigned long timeout
)
467 unsigned long default_timeout
= jiffies
+ msecs_to_jiffies(3);
468 static unsigned long last_timer
;
471 timeout
= default_timeout
;
473 /* Never idle if active, or when VBUS timeout is not set as host */
474 if (musb
->is_active
|| ((musb
->a_wait_bcon
== 0)
475 && (musb
->xceiv
->state
== OTG_STATE_A_WAIT_BCON
))) {
476 dev_dbg(musb
->controller
, "%s active, deleting timer\n",
477 otg_state_string(musb
->xceiv
->state
));
478 del_timer(&musb_idle_timer
);
479 last_timer
= jiffies
;
483 if (time_after(last_timer
, timeout
)) {
484 if (!timer_pending(&musb_idle_timer
))
485 last_timer
= timeout
;
487 dev_dbg(musb
->controller
, "Longer idle timer already pending, ignoring\n");
491 last_timer
= timeout
;
493 dev_dbg(musb
->controller
, "%s inactive, for idle timer for %lu ms\n",
494 otg_state_string(musb
->xceiv
->state
),
495 (unsigned long)jiffies_to_msecs(timeout
- jiffies
));
496 mod_timer(&musb_idle_timer
, timeout
);
499 /* ticks of 60 MHz clock */
500 #define DEVCLOCK 60000000
501 #define OTG_TIMER_MS(msecs) ((msecs) \
502 ? (TUSB_DEV_OTG_TIMER_VAL((DEVCLOCK/1000)*(msecs)) \
503 | TUSB_DEV_OTG_TIMER_ENABLE) \
506 static void tusb_musb_set_vbus(struct musb
*musb
, int is_on
)
508 void __iomem
*tbase
= musb
->ctrl_base
;
509 u32 conf
, prcm
, timer
;
512 /* HDRC controls CPEN, but beware current surges during device
513 * connect. They can trigger transient overcurrent conditions
514 * that must be ignored.
517 prcm
= musb_readl(tbase
, TUSB_PRCM_MNGMT
);
518 conf
= musb_readl(tbase
, TUSB_DEV_CONF
);
519 devctl
= musb_readb(musb
->mregs
, MUSB_DEVCTL
);
522 timer
= OTG_TIMER_MS(OTG_TIME_A_WAIT_VRISE
);
523 musb
->xceiv
->default_a
= 1;
524 musb
->xceiv
->state
= OTG_STATE_A_WAIT_VRISE
;
525 devctl
|= MUSB_DEVCTL_SESSION
;
527 conf
|= TUSB_DEV_CONF_USB_HOST_MODE
;
534 /* If ID pin is grounded, we want to be a_idle */
535 otg_stat
= musb_readl(tbase
, TUSB_DEV_OTG_STAT
);
536 if (!(otg_stat
& TUSB_DEV_OTG_STAT_ID_STATUS
)) {
537 switch (musb
->xceiv
->state
) {
538 case OTG_STATE_A_WAIT_VRISE
:
539 case OTG_STATE_A_WAIT_BCON
:
540 musb
->xceiv
->state
= OTG_STATE_A_WAIT_VFALL
;
542 case OTG_STATE_A_WAIT_VFALL
:
543 musb
->xceiv
->state
= OTG_STATE_A_IDLE
;
546 musb
->xceiv
->state
= OTG_STATE_A_IDLE
;
549 musb
->xceiv
->default_a
= 1;
553 musb
->xceiv
->default_a
= 0;
554 musb
->xceiv
->state
= OTG_STATE_B_IDLE
;
558 devctl
&= ~MUSB_DEVCTL_SESSION
;
559 conf
&= ~TUSB_DEV_CONF_USB_HOST_MODE
;
561 prcm
&= ~(TUSB_PRCM_MNGMT_15_SW_EN
| TUSB_PRCM_MNGMT_33_SW_EN
);
563 musb_writel(tbase
, TUSB_PRCM_MNGMT
, prcm
);
564 musb_writel(tbase
, TUSB_DEV_OTG_TIMER
, timer
);
565 musb_writel(tbase
, TUSB_DEV_CONF
, conf
);
566 musb_writeb(musb
->mregs
, MUSB_DEVCTL
, devctl
);
568 dev_dbg(musb
->controller
, "VBUS %s, devctl %02x otg %3x conf %08x prcm %08x\n",
569 otg_state_string(musb
->xceiv
->state
),
570 musb_readb(musb
->mregs
, MUSB_DEVCTL
),
571 musb_readl(tbase
, TUSB_DEV_OTG_STAT
),
576 * Sets the mode to OTG, peripheral or host by changing the ID detection.
577 * Caller must take care of locking.
579 * Note that if a mini-A cable is plugged in the ID line will stay down as
580 * the weak ID pull-up is not able to pull the ID up.
582 * REVISIT: It would be possible to add support for changing between host
583 * and peripheral modes in non-OTG configurations by reconfiguring hardware
584 * and then setting musb->board_mode. For now, only support OTG mode.
586 static int tusb_musb_set_mode(struct musb
*musb
, u8 musb_mode
)
588 void __iomem
*tbase
= musb
->ctrl_base
;
589 u32 otg_stat
, phy_otg_ctrl
, phy_otg_ena
, dev_conf
;
591 if (musb
->board_mode
!= MUSB_OTG
) {
592 ERR("Changing mode currently only supported in OTG mode\n");
596 otg_stat
= musb_readl(tbase
, TUSB_DEV_OTG_STAT
);
597 phy_otg_ctrl
= musb_readl(tbase
, TUSB_PHY_OTG_CTRL
);
598 phy_otg_ena
= musb_readl(tbase
, TUSB_PHY_OTG_CTRL_ENABLE
);
599 dev_conf
= musb_readl(tbase
, TUSB_DEV_CONF
);
603 case MUSB_HOST
: /* Disable PHY ID detect, ground ID */
604 phy_otg_ctrl
&= ~TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP
;
605 phy_otg_ena
|= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP
;
606 dev_conf
|= TUSB_DEV_CONF_ID_SEL
;
607 dev_conf
&= ~TUSB_DEV_CONF_SOFT_ID
;
609 case MUSB_PERIPHERAL
: /* Disable PHY ID detect, keep ID pull-up on */
610 phy_otg_ctrl
|= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP
;
611 phy_otg_ena
|= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP
;
612 dev_conf
|= (TUSB_DEV_CONF_ID_SEL
| TUSB_DEV_CONF_SOFT_ID
);
614 case MUSB_OTG
: /* Use PHY ID detection */
615 phy_otg_ctrl
|= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP
;
616 phy_otg_ena
|= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP
;
617 dev_conf
&= ~(TUSB_DEV_CONF_ID_SEL
| TUSB_DEV_CONF_SOFT_ID
);
621 dev_dbg(musb
->controller
, "Trying to set mode %i\n", musb_mode
);
625 musb_writel(tbase
, TUSB_PHY_OTG_CTRL
,
626 TUSB_PHY_OTG_CTRL_WRPROTECT
| phy_otg_ctrl
);
627 musb_writel(tbase
, TUSB_PHY_OTG_CTRL_ENABLE
,
628 TUSB_PHY_OTG_CTRL_WRPROTECT
| phy_otg_ena
);
629 musb_writel(tbase
, TUSB_DEV_CONF
, dev_conf
);
631 otg_stat
= musb_readl(tbase
, TUSB_DEV_OTG_STAT
);
632 if ((musb_mode
== MUSB_PERIPHERAL
) &&
633 !(otg_stat
& TUSB_DEV_OTG_STAT_ID_STATUS
))
634 INFO("Cannot be peripheral with mini-A cable "
635 "otg_stat: %08x\n", otg_stat
);
640 static inline unsigned long
641 tusb_otg_ints(struct musb
*musb
, u32 int_src
, void __iomem
*tbase
)
643 u32 otg_stat
= musb_readl(tbase
, TUSB_DEV_OTG_STAT
);
644 unsigned long idle_timeout
= 0;
647 if ((int_src
& TUSB_INT_SRC_ID_STATUS_CHNG
)) {
650 if (is_otg_enabled(musb
))
651 default_a
= !(otg_stat
& TUSB_DEV_OTG_STAT_ID_STATUS
);
653 default_a
= is_host_enabled(musb
);
654 dev_dbg(musb
->controller
, "Default-%c\n", default_a
? 'A' : 'B');
655 musb
->xceiv
->default_a
= default_a
;
656 tusb_musb_set_vbus(musb
, default_a
);
658 /* Don't allow idling immediately */
660 idle_timeout
= jiffies
+ (HZ
* 3);
663 /* VBUS state change */
664 if (int_src
& TUSB_INT_SRC_VBUS_SENSE_CHNG
) {
666 /* B-dev state machine: no vbus ~= disconnect */
667 if ((is_otg_enabled(musb
) && !musb
->xceiv
->default_a
)
668 || !is_host_enabled(musb
)) {
669 /* ? musb_root_disconnect(musb); */
670 musb
->port1_status
&=
671 ~(USB_PORT_STAT_CONNECTION
672 | USB_PORT_STAT_ENABLE
673 | USB_PORT_STAT_LOW_SPEED
674 | USB_PORT_STAT_HIGH_SPEED
678 if (otg_stat
& TUSB_DEV_OTG_STAT_SESS_END
) {
679 dev_dbg(musb
->controller
, "Forcing disconnect (no interrupt)\n");
680 if (musb
->xceiv
->state
!= OTG_STATE_B_IDLE
) {
681 /* INTR_DISCONNECT can hide... */
682 musb
->xceiv
->state
= OTG_STATE_B_IDLE
;
683 musb
->int_usb
|= MUSB_INTR_DISCONNECT
;
687 dev_dbg(musb
->controller
, "vbus change, %s, otg %03x\n",
688 otg_state_string(musb
->xceiv
->state
), otg_stat
);
689 idle_timeout
= jiffies
+ (1 * HZ
);
690 schedule_work(&musb
->irq_work
);
692 } else /* A-dev state machine */ {
693 dev_dbg(musb
->controller
, "vbus change, %s, otg %03x\n",
694 otg_state_string(musb
->xceiv
->state
), otg_stat
);
696 switch (musb
->xceiv
->state
) {
697 case OTG_STATE_A_IDLE
:
698 dev_dbg(musb
->controller
, "Got SRP, turning on VBUS\n");
699 musb_platform_set_vbus(musb
, 1);
701 /* CONNECT can wake if a_wait_bcon is set */
702 if (musb
->a_wait_bcon
!= 0)
708 * OPT FS A TD.4.6 needs few seconds for
711 idle_timeout
= jiffies
+ (2 * HZ
);
714 case OTG_STATE_A_WAIT_VRISE
:
715 /* ignore; A-session-valid < VBUS_VALID/2,
716 * we monitor this with the timer
719 case OTG_STATE_A_WAIT_VFALL
:
720 /* REVISIT this irq triggers during short
721 * spikes caused by enumeration ...
723 if (musb
->vbuserr_retry
) {
724 musb
->vbuserr_retry
--;
725 tusb_musb_set_vbus(musb
, 1);
728 = VBUSERR_RETRY_COUNT
;
729 tusb_musb_set_vbus(musb
, 0);
738 /* OTG timer expiration */
739 if (int_src
& TUSB_INT_SRC_OTG_TIMEOUT
) {
742 dev_dbg(musb
->controller
, "%s timer, %03x\n",
743 otg_state_string(musb
->xceiv
->state
), otg_stat
);
745 switch (musb
->xceiv
->state
) {
746 case OTG_STATE_A_WAIT_VRISE
:
747 /* VBUS has probably been valid for a while now,
748 * but may well have bounced out of range a bit
750 devctl
= musb_readb(musb
->mregs
, MUSB_DEVCTL
);
751 if (otg_stat
& TUSB_DEV_OTG_STAT_VBUS_VALID
) {
752 if ((devctl
& MUSB_DEVCTL_VBUS
)
753 != MUSB_DEVCTL_VBUS
) {
754 dev_dbg(musb
->controller
, "devctl %02x\n", devctl
);
757 musb
->xceiv
->state
= OTG_STATE_A_WAIT_BCON
;
759 idle_timeout
= jiffies
760 + msecs_to_jiffies(musb
->a_wait_bcon
);
762 /* REVISIT report overcurrent to hub? */
763 ERR("vbus too slow, devctl %02x\n", devctl
);
764 tusb_musb_set_vbus(musb
, 0);
767 case OTG_STATE_A_WAIT_BCON
:
768 if (musb
->a_wait_bcon
!= 0)
769 idle_timeout
= jiffies
770 + msecs_to_jiffies(musb
->a_wait_bcon
);
772 case OTG_STATE_A_SUSPEND
:
774 case OTG_STATE_B_WAIT_ACON
:
780 schedule_work(&musb
->irq_work
);
785 static irqreturn_t
tusb_musb_interrupt(int irq
, void *__hci
)
787 struct musb
*musb
= __hci
;
788 void __iomem
*tbase
= musb
->ctrl_base
;
789 unsigned long flags
, idle_timeout
= 0;
790 u32 int_mask
, int_src
;
792 spin_lock_irqsave(&musb
->lock
, flags
);
794 /* Mask all interrupts to allow using both edge and level GPIO irq */
795 int_mask
= musb_readl(tbase
, TUSB_INT_MASK
);
796 musb_writel(tbase
, TUSB_INT_MASK
, ~TUSB_INT_MASK_RESERVED_BITS
);
798 int_src
= musb_readl(tbase
, TUSB_INT_SRC
) & ~TUSB_INT_SRC_RESERVED_BITS
;
799 dev_dbg(musb
->controller
, "TUSB IRQ %08x\n", int_src
);
801 musb
->int_usb
= (u8
) int_src
;
803 /* Acknowledge wake-up source interrupts */
804 if (int_src
& TUSB_INT_SRC_DEV_WAKEUP
) {
808 if (tusb_get_revision(musb
) == TUSB_REV_30
)
809 tusb_wbus_quirk(musb
, 0);
811 /* there are issues re-locking the PLL on wakeup ... */
813 /* work around issue 8 */
814 for (i
= 0xf7f7f7; i
> 0xf7f7f7 - 1000; i
--) {
815 musb_writel(tbase
, TUSB_SCRATCH_PAD
, 0);
816 musb_writel(tbase
, TUSB_SCRATCH_PAD
, i
);
817 reg
= musb_readl(tbase
, TUSB_SCRATCH_PAD
);
820 dev_dbg(musb
->controller
, "TUSB NOR not ready\n");
823 /* work around issue 13 (2nd half) */
824 tusb_set_clock_source(musb
, 1);
826 reg
= musb_readl(tbase
, TUSB_PRCM_WAKEUP_SOURCE
);
827 musb_writel(tbase
, TUSB_PRCM_WAKEUP_CLEAR
, reg
);
828 if (reg
& ~TUSB_PRCM_WNORCS
) {
830 schedule_work(&musb
->irq_work
);
832 dev_dbg(musb
->controller
, "wake %sactive %02x\n",
833 musb
->is_active
? "" : "in", reg
);
835 /* REVISIT host side TUSB_PRCM_WHOSTDISCON, TUSB_PRCM_WBUS */
838 if (int_src
& TUSB_INT_SRC_USB_IP_CONN
)
839 del_timer(&musb_idle_timer
);
841 /* OTG state change reports (annoyingly) not issued by Mentor core */
842 if (int_src
& (TUSB_INT_SRC_VBUS_SENSE_CHNG
843 | TUSB_INT_SRC_OTG_TIMEOUT
844 | TUSB_INT_SRC_ID_STATUS_CHNG
))
845 idle_timeout
= tusb_otg_ints(musb
, int_src
, tbase
);
847 /* TX dma callback must be handled here, RX dma callback is
848 * handled in tusb_omap_dma_cb.
850 if ((int_src
& TUSB_INT_SRC_TXRX_DMA_DONE
)) {
851 u32 dma_src
= musb_readl(tbase
, TUSB_DMA_INT_SRC
);
852 u32 real_dma_src
= musb_readl(tbase
, TUSB_DMA_INT_MASK
);
854 dev_dbg(musb
->controller
, "DMA IRQ %08x\n", dma_src
);
855 real_dma_src
= ~real_dma_src
& dma_src
;
856 if (tusb_dma_omap() && real_dma_src
) {
857 int tx_source
= (real_dma_src
& 0xffff);
860 for (i
= 1; i
<= 15; i
++) {
861 if (tx_source
& (1 << i
)) {
862 dev_dbg(musb
->controller
, "completing ep%i %s\n", i
, "tx");
863 musb_dma_completion(musb
, i
, 1);
867 musb_writel(tbase
, TUSB_DMA_INT_CLEAR
, dma_src
);
870 /* EP interrupts. In OCP mode tusb6010 mirrors the MUSB interrupts */
871 if (int_src
& (TUSB_INT_SRC_USB_IP_TX
| TUSB_INT_SRC_USB_IP_RX
)) {
872 u32 musb_src
= musb_readl(tbase
, TUSB_USBIP_INT_SRC
);
874 musb_writel(tbase
, TUSB_USBIP_INT_CLEAR
, musb_src
);
875 musb
->int_rx
= (((musb_src
>> 16) & 0xffff) << 1);
876 musb
->int_tx
= (musb_src
& 0xffff);
882 if (int_src
& (TUSB_INT_SRC_USB_IP_TX
| TUSB_INT_SRC_USB_IP_RX
| 0xff))
883 musb_interrupt(musb
);
885 /* Acknowledge TUSB interrupts. Clear only non-reserved bits */
886 musb_writel(tbase
, TUSB_INT_SRC_CLEAR
,
887 int_src
& ~TUSB_INT_MASK_RESERVED_BITS
);
889 tusb_musb_try_idle(musb
, idle_timeout
);
891 musb_writel(tbase
, TUSB_INT_MASK
, int_mask
);
892 spin_unlock_irqrestore(&musb
->lock
, flags
);
900 * Enables TUSB6010. Caller must take care of locking.
902 * - Check what is unnecessary in MGC_HdrcStart()
904 static void tusb_musb_enable(struct musb
*musb
)
906 void __iomem
*tbase
= musb
->ctrl_base
;
908 /* Setup TUSB6010 main interrupt mask. Enable all interrupts except SOF.
909 * REVISIT: Enable and deal with TUSB_INT_SRC_USB_IP_SOF */
910 musb_writel(tbase
, TUSB_INT_MASK
, TUSB_INT_SRC_USB_IP_SOF
);
912 /* Setup TUSB interrupt, disable DMA and GPIO interrupts */
913 musb_writel(tbase
, TUSB_USBIP_INT_MASK
, 0);
914 musb_writel(tbase
, TUSB_DMA_INT_MASK
, 0x7fffffff);
915 musb_writel(tbase
, TUSB_GPIO_INT_MASK
, 0x1ff);
917 /* Clear all subsystem interrups */
918 musb_writel(tbase
, TUSB_USBIP_INT_CLEAR
, 0x7fffffff);
919 musb_writel(tbase
, TUSB_DMA_INT_CLEAR
, 0x7fffffff);
920 musb_writel(tbase
, TUSB_GPIO_INT_CLEAR
, 0x1ff);
922 /* Acknowledge pending interrupt(s) */
923 musb_writel(tbase
, TUSB_INT_SRC_CLEAR
, ~TUSB_INT_MASK_RESERVED_BITS
);
925 /* Only 0 clock cycles for minimum interrupt de-assertion time and
926 * interrupt polarity active low seems to work reliably here */
927 musb_writel(tbase
, TUSB_INT_CTRL_CONF
,
928 TUSB_INT_CTRL_CONF_INT_RELCYC(0));
930 irq_set_irq_type(musb
->nIrq
, IRQ_TYPE_LEVEL_LOW
);
932 /* maybe force into the Default-A OTG state machine */
933 if (!(musb_readl(tbase
, TUSB_DEV_OTG_STAT
)
934 & TUSB_DEV_OTG_STAT_ID_STATUS
))
935 musb_writel(tbase
, TUSB_INT_SRC_SET
,
936 TUSB_INT_SRC_ID_STATUS_CHNG
);
938 if (is_dma_capable() && dma_off
)
939 printk(KERN_WARNING
"%s %s: dma not reactivated\n",
946 * Disables TUSB6010. Caller must take care of locking.
948 static void tusb_musb_disable(struct musb
*musb
)
950 void __iomem
*tbase
= musb
->ctrl_base
;
952 /* FIXME stop DMA, IRQs, timers, ... */
954 /* disable all IRQs */
955 musb_writel(tbase
, TUSB_INT_MASK
, ~TUSB_INT_MASK_RESERVED_BITS
);
956 musb_writel(tbase
, TUSB_USBIP_INT_MASK
, 0x7fffffff);
957 musb_writel(tbase
, TUSB_DMA_INT_MASK
, 0x7fffffff);
958 musb_writel(tbase
, TUSB_GPIO_INT_MASK
, 0x1ff);
960 del_timer(&musb_idle_timer
);
962 if (is_dma_capable() && !dma_off
) {
963 printk(KERN_WARNING
"%s %s: dma still active\n",
970 * Sets up TUSB6010 CPU interface specific signals and registers
971 * Note: Settings optimized for OMAP24xx
973 static void tusb_setup_cpu_interface(struct musb
*musb
)
975 void __iomem
*tbase
= musb
->ctrl_base
;
978 * Disable GPIO[5:0] pullups (used as output DMA requests)
979 * Don't disable GPIO[7:6] as they are needed for wake-up.
981 musb_writel(tbase
, TUSB_PULLUP_1_CTRL
, 0x0000003F);
983 /* Disable all pullups on NOR IF, DMAREQ0 and DMAREQ1 */
984 musb_writel(tbase
, TUSB_PULLUP_2_CTRL
, 0x01FFFFFF);
986 /* Turn GPIO[5:0] to DMAREQ[5:0] signals */
987 musb_writel(tbase
, TUSB_GPIO_CONF
, TUSB_GPIO_CONF_DMAREQ(0x3f));
989 /* Burst size 16x16 bits, all six DMA requests enabled, DMA request
990 * de-assertion time 2 system clocks p 62 */
991 musb_writel(tbase
, TUSB_DMA_REQ_CONF
,
992 TUSB_DMA_REQ_CONF_BURST_SIZE(2) |
993 TUSB_DMA_REQ_CONF_DMA_REQ_EN(0x3f) |
994 TUSB_DMA_REQ_CONF_DMA_REQ_ASSER(2));
996 /* Set 0 wait count for synchronous burst access */
997 musb_writel(tbase
, TUSB_WAIT_COUNT
, 1);
1000 static int tusb_musb_start(struct musb
*musb
)
1002 void __iomem
*tbase
= musb
->ctrl_base
;
1004 unsigned long flags
;
1007 if (musb
->board_set_power
)
1008 ret
= musb
->board_set_power(1);
1010 printk(KERN_ERR
"tusb: Cannot enable TUSB6010\n");
1014 spin_lock_irqsave(&musb
->lock
, flags
);
1016 if (musb_readl(tbase
, TUSB_PROD_TEST_RESET
) !=
1017 TUSB_PROD_TEST_RESET_VAL
) {
1018 printk(KERN_ERR
"tusb: Unable to detect TUSB6010\n");
1022 ret
= tusb_print_revision(musb
);
1024 printk(KERN_ERR
"tusb: Unsupported TUSB6010 revision %i\n",
1029 /* The uint bit for "USB non-PDR interrupt enable" has to be 1 when
1030 * NOR FLASH interface is used */
1031 musb_writel(tbase
, TUSB_VLYNQ_CTRL
, 8);
1033 /* Select PHY free running 60MHz as a system clock */
1034 tusb_set_clock_source(musb
, 1);
1036 /* VBus valid timer 1us, disable DFT/Debug and VLYNQ clocks for
1037 * power saving, enable VBus detect and session end comparators,
1038 * enable IDpullup, enable VBus charging */
1039 musb_writel(tbase
, TUSB_PRCM_MNGMT
,
1040 TUSB_PRCM_MNGMT_VBUS_VALID_TIMER(0xa) |
1041 TUSB_PRCM_MNGMT_VBUS_VALID_FLT_EN
|
1042 TUSB_PRCM_MNGMT_OTG_SESS_END_EN
|
1043 TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN
|
1044 TUSB_PRCM_MNGMT_OTG_ID_PULLUP
);
1045 tusb_setup_cpu_interface(musb
);
1047 /* simplify: always sense/pullup ID pins, as if in OTG mode */
1048 reg
= musb_readl(tbase
, TUSB_PHY_OTG_CTRL_ENABLE
);
1049 reg
|= TUSB_PHY_OTG_CTRL_WRPROTECT
| TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP
;
1050 musb_writel(tbase
, TUSB_PHY_OTG_CTRL_ENABLE
, reg
);
1052 reg
= musb_readl(tbase
, TUSB_PHY_OTG_CTRL
);
1053 reg
|= TUSB_PHY_OTG_CTRL_WRPROTECT
| TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP
;
1054 musb_writel(tbase
, TUSB_PHY_OTG_CTRL
, reg
);
1056 spin_unlock_irqrestore(&musb
->lock
, flags
);
1061 spin_unlock_irqrestore(&musb
->lock
, flags
);
1063 if (musb
->board_set_power
)
1064 musb
->board_set_power(0);
1069 static int tusb_musb_init(struct musb
*musb
)
1071 struct platform_device
*pdev
;
1072 struct resource
*mem
;
1073 void __iomem
*sync
= NULL
;
1076 usb_nop_xceiv_register();
1077 musb
->xceiv
= otg_get_transceiver();
1081 pdev
= to_platform_device(musb
->controller
);
1083 /* dma address for async dma */
1084 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1085 musb
->async
= mem
->start
;
1087 /* dma address for sync dma */
1088 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1090 pr_debug("no sync dma resource?\n");
1094 musb
->sync
= mem
->start
;
1096 sync
= ioremap(mem
->start
, resource_size(mem
));
1098 pr_debug("ioremap for sync failed\n");
1102 musb
->sync_va
= sync
;
1104 /* Offsets from base: VLYNQ at 0x000, MUSB regs at 0x400,
1105 * FIFOs at 0x600, TUSB at 0x800
1107 musb
->mregs
+= TUSB_BASE_OFFSET
;
1109 ret
= tusb_musb_start(musb
);
1111 printk(KERN_ERR
"Could not start tusb6010 (%d)\n",
1115 musb
->isr
= tusb_musb_interrupt
;
1117 if (is_peripheral_enabled(musb
)) {
1118 musb
->xceiv
->set_power
= tusb_draw_power
;
1122 setup_timer(&musb_idle_timer
, musb_do_idle
, (unsigned long) musb
);
1129 otg_put_transceiver(musb
->xceiv
);
1130 usb_nop_xceiv_unregister();
1135 static int tusb_musb_exit(struct musb
*musb
)
1137 del_timer_sync(&musb_idle_timer
);
1140 if (musb
->board_set_power
)
1141 musb
->board_set_power(0);
1143 iounmap(musb
->sync_va
);
1145 otg_put_transceiver(musb
->xceiv
);
1146 usb_nop_xceiv_unregister();
1150 static const struct musb_platform_ops tusb_ops
= {
1151 .init
= tusb_musb_init
,
1152 .exit
= tusb_musb_exit
,
1154 .enable
= tusb_musb_enable
,
1155 .disable
= tusb_musb_disable
,
1157 .set_mode
= tusb_musb_set_mode
,
1158 .try_idle
= tusb_musb_try_idle
,
1160 .vbus_status
= tusb_musb_vbus_status
,
1161 .set_vbus
= tusb_musb_set_vbus
,
1164 static u64 tusb_dmamask
= DMA_BIT_MASK(32);
1166 static int __init
tusb_probe(struct platform_device
*pdev
)
1168 struct musb_hdrc_platform_data
*pdata
= pdev
->dev
.platform_data
;
1169 struct platform_device
*musb
;
1170 struct tusb6010_glue
*glue
;
1174 glue
= kzalloc(sizeof(*glue
), GFP_KERNEL
);
1176 dev_err(&pdev
->dev
, "failed to allocate glue context\n");
1180 musb
= platform_device_alloc("musb-hdrc", -1);
1182 dev_err(&pdev
->dev
, "failed to allocate musb device\n");
1186 musb
->dev
.parent
= &pdev
->dev
;
1187 musb
->dev
.dma_mask
= &tusb_dmamask
;
1188 musb
->dev
.coherent_dma_mask
= tusb_dmamask
;
1190 glue
->dev
= &pdev
->dev
;
1193 pdata
->platform_ops
= &tusb_ops
;
1195 platform_set_drvdata(pdev
, glue
);
1197 ret
= platform_device_add_resources(musb
, pdev
->resource
,
1198 pdev
->num_resources
);
1200 dev_err(&pdev
->dev
, "failed to add resources\n");
1204 ret
= platform_device_add_data(musb
, pdata
, sizeof(*pdata
));
1206 dev_err(&pdev
->dev
, "failed to add platform_data\n");
1210 ret
= platform_device_add(musb
);
1212 dev_err(&pdev
->dev
, "failed to register musb device\n");
1219 platform_device_put(musb
);
1228 static int __exit
tusb_remove(struct platform_device
*pdev
)
1230 struct tusb6010_glue
*glue
= platform_get_drvdata(pdev
);
1232 platform_device_del(glue
->musb
);
1233 platform_device_put(glue
->musb
);
1239 static struct platform_driver tusb_driver
= {
1240 .remove
= __exit_p(tusb_remove
),
1242 .name
= "musb-tusb",
1246 MODULE_DESCRIPTION("TUSB6010 MUSB Glue Layer");
1247 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1248 MODULE_LICENSE("GPL v2");
1250 static int __init
tusb_init(void)
1252 return platform_driver_probe(&tusb_driver
, tusb_probe
);
1254 subsys_initcall(tusb_init
);
1256 static void __exit
tusb_exit(void)
1258 platform_driver_unregister(&tusb_driver
);
1260 module_exit(tusb_exit
);