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>
25 #include "musb_core.h"
27 static void tusb_source_power(struct musb *musb, int is_on);
29 #define TUSB_REV_MAJOR(reg_val) ((reg_val >> 4) & 0xf)
30 #define TUSB_REV_MINOR(reg_val) (reg_val & 0xf)
33 * Checks the revision. We need to use the DMA register as 3.0 does not
34 * have correct versions for TUSB_PRCM_REV or TUSB_INT_CTRL_REV.
36 u8 tusb_get_revision(struct musb *musb)
38 void __iomem *tbase = musb->ctrl_base;
42 rev = musb_readl(tbase, TUSB_DMA_CTRL_REV) & 0xff;
43 if (TUSB_REV_MAJOR(rev) == 3) {
44 die_id = TUSB_DIDR1_HI_CHIP_REV(musb_readl(tbase,
46 if (die_id >= TUSB_DIDR1_HI_REV_31)
53 static int __init tusb_print_revision(struct musb *musb)
55 void __iomem *tbase = musb->ctrl_base;
58 rev = tusb_get_revision(musb);
60 pr_info("tusb: %s%i.%i %s%i.%i %s%i.%i %s%i.%i %s%i %s%i.%i\n",
62 TUSB_REV_MAJOR(musb_readl(tbase, TUSB_PRCM_REV)),
63 TUSB_REV_MINOR(musb_readl(tbase, TUSB_PRCM_REV)),
65 TUSB_REV_MAJOR(musb_readl(tbase, TUSB_INT_CTRL_REV)),
66 TUSB_REV_MINOR(musb_readl(tbase, TUSB_INT_CTRL_REV)),
68 TUSB_REV_MAJOR(musb_readl(tbase, TUSB_GPIO_REV)),
69 TUSB_REV_MINOR(musb_readl(tbase, TUSB_GPIO_REV)),
71 TUSB_REV_MAJOR(musb_readl(tbase, TUSB_DMA_CTRL_REV)),
72 TUSB_REV_MINOR(musb_readl(tbase, TUSB_DMA_CTRL_REV)),
74 TUSB_DIDR1_HI_CHIP_REV(musb_readl(tbase, TUSB_DIDR1_HI)),
76 TUSB_REV_MAJOR(rev), TUSB_REV_MINOR(rev));
78 return tusb_get_revision(musb);
81 #define WBUS_QUIRK_MASK (TUSB_PHY_OTG_CTRL_TESTM2 | TUSB_PHY_OTG_CTRL_TESTM1 \
82 | TUSB_PHY_OTG_CTRL_TESTM0)
85 * Workaround for spontaneous WBUS wake-up issue #2 for tusb3.0.
86 * Disables power detection in PHY for the duration of idle.
88 static void tusb_wbus_quirk(struct musb *musb, int enabled)
90 void __iomem *tbase = musb->ctrl_base;
91 static u32 phy_otg_ctrl, phy_otg_ena;
95 phy_otg_ctrl = musb_readl(tbase, TUSB_PHY_OTG_CTRL);
96 phy_otg_ena = musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE);
97 tmp = TUSB_PHY_OTG_CTRL_WRPROTECT
98 | phy_otg_ena | WBUS_QUIRK_MASK;
99 musb_writel(tbase, TUSB_PHY_OTG_CTRL, tmp);
100 tmp = phy_otg_ena & ~WBUS_QUIRK_MASK;
101 tmp |= TUSB_PHY_OTG_CTRL_WRPROTECT | TUSB_PHY_OTG_CTRL_TESTM2;
102 musb_writel(tbase, TUSB_PHY_OTG_CTRL_ENABLE, tmp);
103 DBG(2, "Enabled tusb wbus quirk ctrl %08x ena %08x\n",
104 musb_readl(tbase, TUSB_PHY_OTG_CTRL),
105 musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE));
106 } else if (musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE)
107 & TUSB_PHY_OTG_CTRL_TESTM2) {
108 tmp = TUSB_PHY_OTG_CTRL_WRPROTECT | phy_otg_ctrl;
109 musb_writel(tbase, TUSB_PHY_OTG_CTRL, tmp);
110 tmp = TUSB_PHY_OTG_CTRL_WRPROTECT | phy_otg_ena;
111 musb_writel(tbase, TUSB_PHY_OTG_CTRL_ENABLE, tmp);
112 DBG(2, "Disabled tusb wbus quirk ctrl %08x ena %08x\n",
113 musb_readl(tbase, TUSB_PHY_OTG_CTRL),
114 musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE));
121 * TUSB 6010 may use a parallel bus that doesn't support byte ops;
122 * so both loading and unloading FIFOs need explicit byte counts.
126 tusb_fifo_write_unaligned(void __iomem *fifo, const u8 *buf, u16 len)
132 for (i = 0; i < (len >> 2); i++) {
133 memcpy(&val, buf, 4);
134 musb_writel(fifo, 0, val);
140 /* Write the rest 1 - 3 bytes to FIFO */
141 memcpy(&val, buf, len);
142 musb_writel(fifo, 0, val);
146 static inline void tusb_fifo_read_unaligned(void __iomem *fifo,
147 void __iomem *buf, u16 len)
153 for (i = 0; i < (len >> 2); i++) {
154 val = musb_readl(fifo, 0);
155 memcpy(buf, &val, 4);
161 /* Read the rest 1 - 3 bytes from FIFO */
162 val = musb_readl(fifo, 0);
163 memcpy(buf, &val, len);
167 void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *buf)
169 void __iomem *ep_conf = hw_ep->conf;
170 void __iomem *fifo = hw_ep->fifo;
171 u8 epnum = hw_ep->epnum;
175 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
176 'T', epnum, fifo, len, buf);
179 musb_writel(ep_conf, TUSB_EP_TX_OFFSET,
180 TUSB_EP_CONFIG_XFR_SIZE(len));
182 musb_writel(ep_conf, 0, TUSB_EP0_CONFIG_DIR_TX |
183 TUSB_EP0_CONFIG_XFR_SIZE(len));
185 if (likely((0x01 & (unsigned long) buf) == 0)) {
187 /* Best case is 32bit-aligned destination address */
188 if ((0x02 & (unsigned long) buf) == 0) {
190 writesl(fifo, buf, len >> 2);
191 buf += (len & ~0x03);
199 /* Cannot use writesw, fifo is 32-bit */
200 for (i = 0; i < (len >> 2); i++) {
201 val = (u32)(*(u16 *)buf);
203 val |= (*(u16 *)buf) << 16;
205 musb_writel(fifo, 0, val);
213 tusb_fifo_write_unaligned(fifo, buf, len);
216 void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *buf)
218 void __iomem *ep_conf = hw_ep->conf;
219 void __iomem *fifo = hw_ep->fifo;
220 u8 epnum = hw_ep->epnum;
222 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
223 'R', epnum, fifo, len, buf);
226 musb_writel(ep_conf, TUSB_EP_RX_OFFSET,
227 TUSB_EP_CONFIG_XFR_SIZE(len));
229 musb_writel(ep_conf, 0, TUSB_EP0_CONFIG_XFR_SIZE(len));
231 if (likely((0x01 & (unsigned long) buf) == 0)) {
233 /* Best case is 32bit-aligned destination address */
234 if ((0x02 & (unsigned long) buf) == 0) {
236 readsl(fifo, buf, len >> 2);
237 buf += (len & ~0x03);
245 /* Cannot use readsw, fifo is 32-bit */
246 for (i = 0; i < (len >> 2); i++) {
247 val = musb_readl(fifo, 0);
248 *(u16 *)buf = (u16)(val & 0xffff);
250 *(u16 *)buf = (u16)(val >> 16);
259 tusb_fifo_read_unaligned(fifo, buf, len);
262 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
264 /* This is used by gadget drivers, and OTG transceiver logic, allowing
265 * at most mA current to be drawn from VBUS during a Default-B session
266 * (that is, while VBUS exceeds 4.4V). In Default-A (including pure host
267 * mode), or low power Default-B sessions, something else supplies power.
268 * Caller must take care of locking.
270 static int tusb_draw_power(struct otg_transceiver *x, unsigned mA)
272 struct musb *musb = container_of(x, struct musb, xceiv);
273 void __iomem *tbase = musb->ctrl_base;
277 * Keep clock active when enabled. Note that this is not tied to
278 * drawing VBUS, as with OTG mA can be less than musb->min_power.
280 if (musb->set_clock) {
282 musb->set_clock(musb->clock, 1);
284 musb->set_clock(musb->clock, 0);
287 /* tps65030 seems to consume max 100mA, with maybe 60mA available
288 * (measured on one board) for things other than tps and tusb.
290 * Boards sharing the CPU clock with CLKIN will need to prevent
291 * certain idle sleep states while the USB link is active.
293 * REVISIT we could use VBUS to supply only _one_ of { 1.5V, 3.3V }.
294 * The actual current usage would be very board-specific. For now,
295 * it's simpler to just use an aggregate (also board-specific).
297 if (x->default_a || mA < (musb->min_power << 1))
300 reg = musb_readl(tbase, TUSB_PRCM_MNGMT);
302 musb->is_bus_powered = 1;
303 reg |= TUSB_PRCM_MNGMT_15_SW_EN | TUSB_PRCM_MNGMT_33_SW_EN;
305 musb->is_bus_powered = 0;
306 reg &= ~(TUSB_PRCM_MNGMT_15_SW_EN | TUSB_PRCM_MNGMT_33_SW_EN);
308 musb_writel(tbase, TUSB_PRCM_MNGMT, reg);
310 DBG(2, "draw max %d mA VBUS\n", mA);
315 #define tusb_draw_power NULL
318 /* workaround for issue 13: change clock during chip idle
319 * (to be fixed in rev3 silicon) ... symptoms include disconnect
320 * or looping suspend/resume cycles
322 static void tusb_set_clock_source(struct musb *musb, unsigned mode)
324 void __iomem *tbase = musb->ctrl_base;
327 reg = musb_readl(tbase, TUSB_PRCM_CONF);
328 reg &= ~TUSB_PRCM_CONF_SYS_CLKSEL(0x3);
330 /* 0 = refclk (clkin, XI)
331 * 1 = PHY 60 MHz (internal PLL)
336 reg |= TUSB_PRCM_CONF_SYS_CLKSEL(mode & 0x3);
338 musb_writel(tbase, TUSB_PRCM_CONF, reg);
340 /* FIXME tusb6010_platform_retime(mode == 0); */
344 * Idle TUSB6010 until next wake-up event; NOR access always wakes.
345 * Other code ensures that we idle unless we're connected _and_ the
346 * USB link is not suspended ... and tells us the relevant wakeup
347 * events. SW_EN for voltage is handled separately.
349 void tusb_allow_idle(struct musb *musb, u32 wakeup_enables)
351 void __iomem *tbase = musb->ctrl_base;
354 if ((wakeup_enables & TUSB_PRCM_WBUS)
355 && (tusb_get_revision(musb) == TUSB_REV_30))
356 tusb_wbus_quirk(musb, 1);
358 tusb_set_clock_source(musb, 0);
360 wakeup_enables |= TUSB_PRCM_WNORCS;
361 musb_writel(tbase, TUSB_PRCM_WAKEUP_MASK, ~wakeup_enables);
363 /* REVISIT writeup of WID implies that if WID set and ID is grounded,
364 * TUSB_PHY_OTG_CTRL.TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP must be cleared.
365 * Presumably that's mostly to save power, hence WID is immaterial ...
368 reg = musb_readl(tbase, TUSB_PRCM_MNGMT);
369 /* issue 4: when driving vbus, use hipower (vbus_det) comparator */
370 if (is_host_active(musb)) {
371 reg |= TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN;
372 reg &= ~TUSB_PRCM_MNGMT_OTG_SESS_END_EN;
374 reg |= TUSB_PRCM_MNGMT_OTG_SESS_END_EN;
375 reg &= ~TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN;
377 reg |= TUSB_PRCM_MNGMT_PM_IDLE | TUSB_PRCM_MNGMT_DEV_IDLE;
378 musb_writel(tbase, TUSB_PRCM_MNGMT, reg);
380 DBG(6, "idle, wake on %02x\n", wakeup_enables);
384 * Updates cable VBUS status. Caller must take care of locking.
386 int musb_platform_get_vbus_status(struct musb *musb)
388 void __iomem *tbase = musb->ctrl_base;
389 u32 otg_stat, prcm_mngmt;
392 otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
393 prcm_mngmt = musb_readl(tbase, TUSB_PRCM_MNGMT);
395 /* Temporarily enable VBUS detection if it was disabled for
396 * suspend mode. Unless it's enabled otg_stat and devctl will
397 * not show correct VBUS state.
399 if (!(prcm_mngmt & TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN)) {
400 u32 tmp = prcm_mngmt;
401 tmp |= TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN;
402 musb_writel(tbase, TUSB_PRCM_MNGMT, tmp);
403 otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
404 musb_writel(tbase, TUSB_PRCM_MNGMT, prcm_mngmt);
407 if (otg_stat & TUSB_DEV_OTG_STAT_VBUS_VALID)
413 static struct timer_list musb_idle_timer;
415 static void musb_do_idle(unsigned long _musb)
417 struct musb *musb = (void *)_musb;
420 spin_lock_irqsave(&musb->lock, flags);
422 switch (musb->xceiv.state) {
423 case OTG_STATE_A_WAIT_BCON:
424 if ((musb->a_wait_bcon != 0)
425 && (musb->idle_timeout == 0
426 || time_after(jiffies, musb->idle_timeout))) {
427 DBG(4, "Nothing connected %s, turning off VBUS\n",
428 otg_state_string(musb));
431 case OTG_STATE_A_IDLE:
432 tusb_source_power(musb, 0);
437 if (!musb->is_active) {
440 /* wait until khubd handles port change status */
441 if (is_host_active(musb) && (musb->port1_status >> 16))
444 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
445 if (is_peripheral_enabled(musb) && !musb->gadget_driver)
448 wakeups = TUSB_PRCM_WHOSTDISCON
451 if (is_otg_enabled(musb))
452 wakeups |= TUSB_PRCM_WID;
455 wakeups = TUSB_PRCM_WHOSTDISCON | TUSB_PRCM_WBUS;
457 tusb_allow_idle(musb, wakeups);
460 spin_unlock_irqrestore(&musb->lock, flags);
464 * Maybe put TUSB6010 into idle mode mode depending on USB link status,
465 * like "disconnected" or "suspended". We'll be woken out of it by
466 * connect, resume, or disconnect.
468 * Needs to be called as the last function everywhere where there is
469 * register access to TUSB6010 because of NOR flash wake-up.
470 * Caller should own controller spinlock.
472 * Delay because peripheral enables D+ pullup 3msec after SE0, and
473 * we don't want to treat that full speed J as a wakeup event.
474 * ... peripherals must draw only suspend current after 10 msec.
476 void musb_platform_try_idle(struct musb *musb, unsigned long timeout)
478 unsigned long default_timeout = jiffies + msecs_to_jiffies(3);
479 static unsigned long last_timer;
482 timeout = default_timeout;
484 /* Never idle if active, or when VBUS timeout is not set as host */
485 if (musb->is_active || ((musb->a_wait_bcon == 0)
486 && (musb->xceiv.state == OTG_STATE_A_WAIT_BCON))) {
487 DBG(4, "%s active, deleting timer\n", otg_state_string(musb));
488 del_timer(&musb_idle_timer);
489 last_timer = jiffies;
493 if (time_after(last_timer, timeout)) {
494 if (!timer_pending(&musb_idle_timer))
495 last_timer = timeout;
497 DBG(4, "Longer idle timer already pending, ignoring\n");
501 last_timer = timeout;
503 DBG(4, "%s inactive, for idle timer for %lu ms\n",
504 otg_state_string(musb),
505 (unsigned long)jiffies_to_msecs(timeout - jiffies));
506 mod_timer(&musb_idle_timer, timeout);
509 /* ticks of 60 MHz clock */
510 #define DEVCLOCK 60000000
511 #define OTG_TIMER_MS(msecs) ((msecs) \
512 ? (TUSB_DEV_OTG_TIMER_VAL((DEVCLOCK/1000)*(msecs)) \
513 | TUSB_DEV_OTG_TIMER_ENABLE) \
516 static void tusb_source_power(struct musb *musb, int is_on)
518 void __iomem *tbase = musb->ctrl_base;
519 u32 conf, prcm, timer;
522 /* HDRC controls CPEN, but beware current surges during device
523 * connect. They can trigger transient overcurrent conditions
524 * that must be ignored.
527 prcm = musb_readl(tbase, TUSB_PRCM_MNGMT);
528 conf = musb_readl(tbase, TUSB_DEV_CONF);
529 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
533 musb->set_clock(musb->clock, 1);
534 timer = OTG_TIMER_MS(OTG_TIME_A_WAIT_VRISE);
535 musb->xceiv.default_a = 1;
536 musb->xceiv.state = OTG_STATE_A_WAIT_VRISE;
537 devctl |= MUSB_DEVCTL_SESSION;
539 conf |= TUSB_DEV_CONF_USB_HOST_MODE;
546 /* If ID pin is grounded, we want to be a_idle */
547 otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
548 if (!(otg_stat & TUSB_DEV_OTG_STAT_ID_STATUS)) {
549 switch (musb->xceiv.state) {
550 case OTG_STATE_A_WAIT_VRISE:
551 case OTG_STATE_A_WAIT_BCON:
552 musb->xceiv.state = OTG_STATE_A_WAIT_VFALL;
554 case OTG_STATE_A_WAIT_VFALL:
555 musb->xceiv.state = OTG_STATE_A_IDLE;
558 musb->xceiv.state = OTG_STATE_A_IDLE;
561 musb->xceiv.default_a = 1;
565 musb->xceiv.default_a = 0;
566 musb->xceiv.state = OTG_STATE_B_IDLE;
570 devctl &= ~MUSB_DEVCTL_SESSION;
571 conf &= ~TUSB_DEV_CONF_USB_HOST_MODE;
573 musb->set_clock(musb->clock, 0);
575 prcm &= ~(TUSB_PRCM_MNGMT_15_SW_EN | TUSB_PRCM_MNGMT_33_SW_EN);
577 musb_writel(tbase, TUSB_PRCM_MNGMT, prcm);
578 musb_writel(tbase, TUSB_DEV_OTG_TIMER, timer);
579 musb_writel(tbase, TUSB_DEV_CONF, conf);
580 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
582 DBG(1, "VBUS %s, devctl %02x otg %3x conf %08x prcm %08x\n",
583 otg_state_string(musb),
584 musb_readb(musb->mregs, MUSB_DEVCTL),
585 musb_readl(tbase, TUSB_DEV_OTG_STAT),
590 * Sets the mode to OTG, peripheral or host by changing the ID detection.
591 * Caller must take care of locking.
593 * Note that if a mini-A cable is plugged in the ID line will stay down as
594 * the weak ID pull-up is not able to pull the ID up.
596 * REVISIT: It would be possible to add support for changing between host
597 * and peripheral modes in non-OTG configurations by reconfiguring hardware
598 * and then setting musb->board_mode. For now, only support OTG mode.
600 int musb_platform_set_mode(struct musb *musb, u8 musb_mode)
602 void __iomem *tbase = musb->ctrl_base;
603 u32 otg_stat, phy_otg_ctrl, phy_otg_ena, dev_conf;
605 if (musb->board_mode != MUSB_OTG) {
606 ERR("Changing mode currently only supported in OTG mode\n");
610 otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
611 phy_otg_ctrl = musb_readl(tbase, TUSB_PHY_OTG_CTRL);
612 phy_otg_ena = musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE);
613 dev_conf = musb_readl(tbase, TUSB_DEV_CONF);
617 #ifdef CONFIG_USB_MUSB_HDRC_HCD
618 case MUSB_HOST: /* Disable PHY ID detect, ground ID */
619 phy_otg_ctrl &= ~TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
620 phy_otg_ena |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
621 dev_conf |= TUSB_DEV_CONF_ID_SEL;
622 dev_conf &= ~TUSB_DEV_CONF_SOFT_ID;
626 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
627 case MUSB_PERIPHERAL: /* Disable PHY ID detect, keep ID pull-up on */
628 phy_otg_ctrl |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
629 phy_otg_ena |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
630 dev_conf |= (TUSB_DEV_CONF_ID_SEL | TUSB_DEV_CONF_SOFT_ID);
634 #ifdef CONFIG_USB_MUSB_OTG
635 case MUSB_OTG: /* Use PHY ID detection */
636 phy_otg_ctrl |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
637 phy_otg_ena |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
638 dev_conf &= ~(TUSB_DEV_CONF_ID_SEL | TUSB_DEV_CONF_SOFT_ID);
643 DBG(2, "Trying to set mode %i\n", musb_mode);
647 musb_writel(tbase, TUSB_PHY_OTG_CTRL,
648 TUSB_PHY_OTG_CTRL_WRPROTECT | phy_otg_ctrl);
649 musb_writel(tbase, TUSB_PHY_OTG_CTRL_ENABLE,
650 TUSB_PHY_OTG_CTRL_WRPROTECT | phy_otg_ena);
651 musb_writel(tbase, TUSB_DEV_CONF, dev_conf);
653 otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
654 if ((musb_mode == MUSB_PERIPHERAL) &&
655 !(otg_stat & TUSB_DEV_OTG_STAT_ID_STATUS))
656 INFO("Cannot be peripheral with mini-A cable "
657 "otg_stat: %08x\n", otg_stat);
662 static inline unsigned long
663 tusb_otg_ints(struct musb *musb, u32 int_src, void __iomem *tbase)
665 u32 otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
666 unsigned long idle_timeout = 0;
669 if ((int_src & TUSB_INT_SRC_ID_STATUS_CHNG)) {
672 if (is_otg_enabled(musb))
673 default_a = !(otg_stat & TUSB_DEV_OTG_STAT_ID_STATUS);
675 default_a = is_host_enabled(musb);
676 DBG(2, "Default-%c\n", default_a ? 'A' : 'B');
677 musb->xceiv.default_a = default_a;
678 tusb_source_power(musb, default_a);
680 /* Don't allow idling immediately */
682 idle_timeout = jiffies + (HZ * 3);
685 /* VBUS state change */
686 if (int_src & TUSB_INT_SRC_VBUS_SENSE_CHNG) {
688 /* B-dev state machine: no vbus ~= disconnect */
689 if ((is_otg_enabled(musb) && !musb->xceiv.default_a)
690 || !is_host_enabled(musb)) {
691 #ifdef CONFIG_USB_MUSB_HDRC_HCD
692 /* ? musb_root_disconnect(musb); */
693 musb->port1_status &=
694 ~(USB_PORT_STAT_CONNECTION
695 | USB_PORT_STAT_ENABLE
696 | USB_PORT_STAT_LOW_SPEED
697 | USB_PORT_STAT_HIGH_SPEED
702 if (otg_stat & TUSB_DEV_OTG_STAT_SESS_END) {
703 DBG(1, "Forcing disconnect (no interrupt)\n");
704 if (musb->xceiv.state != OTG_STATE_B_IDLE) {
705 /* INTR_DISCONNECT can hide... */
706 musb->xceiv.state = OTG_STATE_B_IDLE;
707 musb->int_usb |= MUSB_INTR_DISCONNECT;
711 DBG(2, "vbus change, %s, otg %03x\n",
712 otg_state_string(musb), otg_stat);
713 idle_timeout = jiffies + (1 * HZ);
714 schedule_work(&musb->irq_work);
716 } else /* A-dev state machine */ {
717 DBG(2, "vbus change, %s, otg %03x\n",
718 otg_state_string(musb), otg_stat);
720 switch (musb->xceiv.state) {
721 case OTG_STATE_A_IDLE:
722 DBG(2, "Got SRP, turning on VBUS\n");
723 musb_set_vbus(musb, 1);
725 /* CONNECT can wake if a_wait_bcon is set */
726 if (musb->a_wait_bcon != 0)
732 * OPT FS A TD.4.6 needs few seconds for
735 idle_timeout = jiffies + (2 * HZ);
738 case OTG_STATE_A_WAIT_VRISE:
739 /* ignore; A-session-valid < VBUS_VALID/2,
740 * we monitor this with the timer
743 case OTG_STATE_A_WAIT_VFALL:
744 /* REVISIT this irq triggers during short
745 * spikes caused by enumeration ...
747 if (musb->vbuserr_retry) {
748 musb->vbuserr_retry--;
749 tusb_source_power(musb, 1);
752 = VBUSERR_RETRY_COUNT;
753 tusb_source_power(musb, 0);
762 /* OTG timer expiration */
763 if (int_src & TUSB_INT_SRC_OTG_TIMEOUT) {
766 DBG(4, "%s timer, %03x\n", otg_state_string(musb), otg_stat);
768 switch (musb->xceiv.state) {
769 case OTG_STATE_A_WAIT_VRISE:
770 /* VBUS has probably been valid for a while now,
771 * but may well have bounced out of range a bit
773 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
774 if (otg_stat & TUSB_DEV_OTG_STAT_VBUS_VALID) {
775 if ((devctl & MUSB_DEVCTL_VBUS)
776 != MUSB_DEVCTL_VBUS) {
777 DBG(2, "devctl %02x\n", devctl);
780 musb->xceiv.state = OTG_STATE_A_WAIT_BCON;
782 idle_timeout = jiffies
783 + msecs_to_jiffies(musb->a_wait_bcon);
785 /* REVISIT report overcurrent to hub? */
786 ERR("vbus too slow, devctl %02x\n", devctl);
787 tusb_source_power(musb, 0);
790 case OTG_STATE_A_WAIT_BCON:
791 if (musb->a_wait_bcon != 0)
792 idle_timeout = jiffies
793 + msecs_to_jiffies(musb->a_wait_bcon);
795 case OTG_STATE_A_SUSPEND:
797 case OTG_STATE_B_WAIT_ACON:
803 schedule_work(&musb->irq_work);
808 static irqreturn_t tusb_interrupt(int irq, void *__hci)
810 struct musb *musb = __hci;
811 void __iomem *tbase = musb->ctrl_base;
812 unsigned long flags, idle_timeout = 0;
813 u32 int_mask, int_src;
815 spin_lock_irqsave(&musb->lock, flags);
817 /* Mask all interrupts to allow using both edge and level GPIO irq */
818 int_mask = musb_readl(tbase, TUSB_INT_MASK);
819 musb_writel(tbase, TUSB_INT_MASK, ~TUSB_INT_MASK_RESERVED_BITS);
821 int_src = musb_readl(tbase, TUSB_INT_SRC) & ~TUSB_INT_SRC_RESERVED_BITS;
822 DBG(3, "TUSB IRQ %08x\n", int_src);
824 musb->int_usb = (u8) int_src;
826 /* Acknowledge wake-up source interrupts */
827 if (int_src & TUSB_INT_SRC_DEV_WAKEUP) {
831 if (tusb_get_revision(musb) == TUSB_REV_30)
832 tusb_wbus_quirk(musb, 0);
834 /* there are issues re-locking the PLL on wakeup ... */
836 /* work around issue 8 */
837 for (i = 0xf7f7f7; i > 0xf7f7f7 - 1000; i--) {
838 musb_writel(tbase, TUSB_SCRATCH_PAD, 0);
839 musb_writel(tbase, TUSB_SCRATCH_PAD, i);
840 reg = musb_readl(tbase, TUSB_SCRATCH_PAD);
843 DBG(6, "TUSB NOR not ready\n");
846 /* work around issue 13 (2nd half) */
847 tusb_set_clock_source(musb, 1);
849 reg = musb_readl(tbase, TUSB_PRCM_WAKEUP_SOURCE);
850 musb_writel(tbase, TUSB_PRCM_WAKEUP_CLEAR, reg);
851 if (reg & ~TUSB_PRCM_WNORCS) {
853 schedule_work(&musb->irq_work);
855 DBG(3, "wake %sactive %02x\n",
856 musb->is_active ? "" : "in", reg);
858 /* REVISIT host side TUSB_PRCM_WHOSTDISCON, TUSB_PRCM_WBUS */
861 if (int_src & TUSB_INT_SRC_USB_IP_CONN)
862 del_timer(&musb_idle_timer);
864 /* OTG state change reports (annoyingly) not issued by Mentor core */
865 if (int_src & (TUSB_INT_SRC_VBUS_SENSE_CHNG
866 | TUSB_INT_SRC_OTG_TIMEOUT
867 | TUSB_INT_SRC_ID_STATUS_CHNG))
868 idle_timeout = tusb_otg_ints(musb, int_src, tbase);
870 /* TX dma callback must be handled here, RX dma callback is
871 * handled in tusb_omap_dma_cb.
873 if ((int_src & TUSB_INT_SRC_TXRX_DMA_DONE)) {
874 u32 dma_src = musb_readl(tbase, TUSB_DMA_INT_SRC);
875 u32 real_dma_src = musb_readl(tbase, TUSB_DMA_INT_MASK);
877 DBG(3, "DMA IRQ %08x\n", dma_src);
878 real_dma_src = ~real_dma_src & dma_src;
879 if (tusb_dma_omap() && real_dma_src) {
880 int tx_source = (real_dma_src & 0xffff);
883 for (i = 1; i <= 15; i++) {
884 if (tx_source & (1 << i)) {
885 DBG(3, "completing ep%i %s\n", i, "tx");
886 musb_dma_completion(musb, i, 1);
890 musb_writel(tbase, TUSB_DMA_INT_CLEAR, dma_src);
893 /* EP interrupts. In OCP mode tusb6010 mirrors the MUSB interrupts */
894 if (int_src & (TUSB_INT_SRC_USB_IP_TX | TUSB_INT_SRC_USB_IP_RX)) {
895 u32 musb_src = musb_readl(tbase, TUSB_USBIP_INT_SRC);
897 musb_writel(tbase, TUSB_USBIP_INT_CLEAR, musb_src);
898 musb->int_rx = (((musb_src >> 16) & 0xffff) << 1);
899 musb->int_tx = (musb_src & 0xffff);
905 if (int_src & (TUSB_INT_SRC_USB_IP_TX | TUSB_INT_SRC_USB_IP_RX | 0xff))
906 musb_interrupt(musb);
908 /* Acknowledge TUSB interrupts. Clear only non-reserved bits */
909 musb_writel(tbase, TUSB_INT_SRC_CLEAR,
910 int_src & ~TUSB_INT_MASK_RESERVED_BITS);
912 musb_platform_try_idle(musb, idle_timeout);
914 musb_writel(tbase, TUSB_INT_MASK, int_mask);
915 spin_unlock_irqrestore(&musb->lock, flags);
923 * Enables TUSB6010. Caller must take care of locking.
925 * - Check what is unnecessary in MGC_HdrcStart()
927 void musb_platform_enable(struct musb *musb)
929 void __iomem *tbase = musb->ctrl_base;
931 /* Setup TUSB6010 main interrupt mask. Enable all interrupts except SOF.
932 * REVISIT: Enable and deal with TUSB_INT_SRC_USB_IP_SOF */
933 musb_writel(tbase, TUSB_INT_MASK, TUSB_INT_SRC_USB_IP_SOF);
935 /* Setup TUSB interrupt, disable DMA and GPIO interrupts */
936 musb_writel(tbase, TUSB_USBIP_INT_MASK, 0);
937 musb_writel(tbase, TUSB_DMA_INT_MASK, 0x7fffffff);
938 musb_writel(tbase, TUSB_GPIO_INT_MASK, 0x1ff);
940 /* Clear all subsystem interrups */
941 musb_writel(tbase, TUSB_USBIP_INT_CLEAR, 0x7fffffff);
942 musb_writel(tbase, TUSB_DMA_INT_CLEAR, 0x7fffffff);
943 musb_writel(tbase, TUSB_GPIO_INT_CLEAR, 0x1ff);
945 /* Acknowledge pending interrupt(s) */
946 musb_writel(tbase, TUSB_INT_SRC_CLEAR, ~TUSB_INT_MASK_RESERVED_BITS);
948 /* Only 0 clock cycles for minimum interrupt de-assertion time and
949 * interrupt polarity active low seems to work reliably here */
950 musb_writel(tbase, TUSB_INT_CTRL_CONF,
951 TUSB_INT_CTRL_CONF_INT_RELCYC(0));
953 set_irq_type(musb->nIrq, IRQ_TYPE_LEVEL_LOW);
955 /* maybe force into the Default-A OTG state machine */
956 if (!(musb_readl(tbase, TUSB_DEV_OTG_STAT)
957 & TUSB_DEV_OTG_STAT_ID_STATUS))
958 musb_writel(tbase, TUSB_INT_SRC_SET,
959 TUSB_INT_SRC_ID_STATUS_CHNG);
961 if (is_dma_capable() && dma_off)
962 printk(KERN_WARNING "%s %s: dma not reactivated\n",
969 * Disables TUSB6010. Caller must take care of locking.
971 void musb_platform_disable(struct musb *musb)
973 void __iomem *tbase = musb->ctrl_base;
975 /* FIXME stop DMA, IRQs, timers, ... */
977 /* disable all IRQs */
978 musb_writel(tbase, TUSB_INT_MASK, ~TUSB_INT_MASK_RESERVED_BITS);
979 musb_writel(tbase, TUSB_USBIP_INT_MASK, 0x7fffffff);
980 musb_writel(tbase, TUSB_DMA_INT_MASK, 0x7fffffff);
981 musb_writel(tbase, TUSB_GPIO_INT_MASK, 0x1ff);
983 del_timer(&musb_idle_timer);
985 if (is_dma_capable() && !dma_off) {
986 printk(KERN_WARNING "%s %s: dma still active\n",
993 * Sets up TUSB6010 CPU interface specific signals and registers
994 * Note: Settings optimized for OMAP24xx
996 static void __init tusb_setup_cpu_interface(struct musb *musb)
998 void __iomem *tbase = musb->ctrl_base;
1001 * Disable GPIO[5:0] pullups (used as output DMA requests)
1002 * Don't disable GPIO[7:6] as they are needed for wake-up.
1004 musb_writel(tbase, TUSB_PULLUP_1_CTRL, 0x0000003F);
1006 /* Disable all pullups on NOR IF, DMAREQ0 and DMAREQ1 */
1007 musb_writel(tbase, TUSB_PULLUP_2_CTRL, 0x01FFFFFF);
1009 /* Turn GPIO[5:0] to DMAREQ[5:0] signals */
1010 musb_writel(tbase, TUSB_GPIO_CONF, TUSB_GPIO_CONF_DMAREQ(0x3f));
1012 /* Burst size 16x16 bits, all six DMA requests enabled, DMA request
1013 * de-assertion time 2 system clocks p 62 */
1014 musb_writel(tbase, TUSB_DMA_REQ_CONF,
1015 TUSB_DMA_REQ_CONF_BURST_SIZE(2) |
1016 TUSB_DMA_REQ_CONF_DMA_REQ_EN(0x3f) |
1017 TUSB_DMA_REQ_CONF_DMA_REQ_ASSER(2));
1019 /* Set 0 wait count for synchronous burst access */
1020 musb_writel(tbase, TUSB_WAIT_COUNT, 1);
1023 static int __init tusb_start(struct musb *musb)
1025 void __iomem *tbase = musb->ctrl_base;
1027 unsigned long flags;
1030 if (musb->board_set_power)
1031 ret = musb->board_set_power(1);
1033 printk(KERN_ERR "tusb: Cannot enable TUSB6010\n");
1037 spin_lock_irqsave(&musb->lock, flags);
1039 if (musb_readl(tbase, TUSB_PROD_TEST_RESET) !=
1040 TUSB_PROD_TEST_RESET_VAL) {
1041 printk(KERN_ERR "tusb: Unable to detect TUSB6010\n");
1045 ret = tusb_print_revision(musb);
1047 printk(KERN_ERR "tusb: Unsupported TUSB6010 revision %i\n",
1052 /* The uint bit for "USB non-PDR interrupt enable" has to be 1 when
1053 * NOR FLASH interface is used */
1054 musb_writel(tbase, TUSB_VLYNQ_CTRL, 8);
1056 /* Select PHY free running 60MHz as a system clock */
1057 tusb_set_clock_source(musb, 1);
1059 /* VBus valid timer 1us, disable DFT/Debug and VLYNQ clocks for
1060 * power saving, enable VBus detect and session end comparators,
1061 * enable IDpullup, enable VBus charging */
1062 musb_writel(tbase, TUSB_PRCM_MNGMT,
1063 TUSB_PRCM_MNGMT_VBUS_VALID_TIMER(0xa) |
1064 TUSB_PRCM_MNGMT_VBUS_VALID_FLT_EN |
1065 TUSB_PRCM_MNGMT_OTG_SESS_END_EN |
1066 TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN |
1067 TUSB_PRCM_MNGMT_OTG_ID_PULLUP);
1068 tusb_setup_cpu_interface(musb);
1070 /* simplify: always sense/pullup ID pins, as if in OTG mode */
1071 reg = musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE);
1072 reg |= TUSB_PHY_OTG_CTRL_WRPROTECT | TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
1073 musb_writel(tbase, TUSB_PHY_OTG_CTRL_ENABLE, reg);
1075 reg = musb_readl(tbase, TUSB_PHY_OTG_CTRL);
1076 reg |= TUSB_PHY_OTG_CTRL_WRPROTECT | TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
1077 musb_writel(tbase, TUSB_PHY_OTG_CTRL, reg);
1079 spin_unlock_irqrestore(&musb->lock, flags);
1084 spin_unlock_irqrestore(&musb->lock, flags);
1086 if (musb->board_set_power)
1087 musb->board_set_power(0);
1092 int __init musb_platform_init(struct musb *musb)
1094 struct platform_device *pdev;
1095 struct resource *mem;
1099 pdev = to_platform_device(musb->controller);
1101 /* dma address for async dma */
1102 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1103 musb->async = mem->start;
1105 /* dma address for sync dma */
1106 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1108 pr_debug("no sync dma resource?\n");
1111 musb->sync = mem->start;
1113 sync = ioremap(mem->start, mem->end - mem->start + 1);
1115 pr_debug("ioremap for sync failed\n");
1118 musb->sync_va = sync;
1120 /* Offsets from base: VLYNQ at 0x000, MUSB regs at 0x400,
1121 * FIFOs at 0x600, TUSB at 0x800
1123 musb->mregs += TUSB_BASE_OFFSET;
1125 ret = tusb_start(musb);
1127 printk(KERN_ERR "Could not start tusb6010 (%d)\n",
1131 musb->isr = tusb_interrupt;
1133 if (is_host_enabled(musb))
1134 musb->board_set_vbus = tusb_source_power;
1135 if (is_peripheral_enabled(musb))
1136 musb->xceiv.set_power = tusb_draw_power;
1138 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
1143 int musb_platform_exit(struct musb *musb)
1145 del_timer_sync(&musb_idle_timer);
1147 if (musb->board_set_power)
1148 musb->board_set_power(0);
1150 iounmap(musb->sync_va);