tusb6010.c 30.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * TUSB6010 USB 2.0 OTG Dual Role controller
 *
 * Copyright (C) 2006 Nokia Corporation
 * Jarkko Nikula <jarkko.nikula@nokia.com>
 * Tony Lindgren <tony@atomide.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Notes:
 * - Driver assumes that interface to external host (main CPU) is
 *   configured for NOR FLASH interface instead of VLYNQ serial
 *   interface.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/usb.h>
#include <linux/irq.h>
#include <linux/platform_device.h>

26
#include "musb_core.h"
27

28 29
static void tusb_source_power(struct musb *musb, int is_on);

30 31 32
#define TUSB_REV_MAJOR(reg_val)		((reg_val >> 4) & 0xf)
#define TUSB_REV_MINOR(reg_val)		(reg_val & 0xf)

33 34 35 36
/*
 * Checks the revision. We need to use the DMA register as 3.0 does not
 * have correct versions for TUSB_PRCM_REV or TUSB_INT_CTRL_REV.
 */
37
u8 tusb_get_revision(struct musb *musb)
38
{
39
	void __iomem	*tbase = musb->ctrl_base;
40 41
	u32		die_id;
	u8		rev;
42

43
	rev = musb_readl(tbase, TUSB_DMA_CTRL_REV) & 0xff;
44
	if (TUSB_REV_MAJOR(rev) == 3) {
45 46
		die_id = TUSB_DIDR1_HI_CHIP_REV(musb_readl(tbase,
				TUSB_DIDR1_HI));
47
		if (die_id >= TUSB_DIDR1_HI_REV_31)
48 49
			rev |= 1;
	}
50

51 52
	return rev;
}
53 54 55

static int __init tusb_print_revision(struct musb *musb)
{
56
	void __iomem	*tbase = musb->ctrl_base;
57 58 59
	u8		rev;

	rev = tusb_get_revision(musb);
60

61
	pr_info("tusb: %s%i.%i %s%i.%i %s%i.%i %s%i.%i %s%i %s%i.%i\n",
62
		"prcm",
63 64
		TUSB_REV_MAJOR(musb_readl(tbase, TUSB_PRCM_REV)),
		TUSB_REV_MINOR(musb_readl(tbase, TUSB_PRCM_REV)),
65
		"int",
66 67
		TUSB_REV_MAJOR(musb_readl(tbase, TUSB_INT_CTRL_REV)),
		TUSB_REV_MINOR(musb_readl(tbase, TUSB_INT_CTRL_REV)),
68
		"gpio",
69 70
		TUSB_REV_MAJOR(musb_readl(tbase, TUSB_GPIO_REV)),
		TUSB_REV_MINOR(musb_readl(tbase, TUSB_GPIO_REV)),
71
		"dma",
72 73
		TUSB_REV_MAJOR(musb_readl(tbase, TUSB_DMA_CTRL_REV)),
		TUSB_REV_MINOR(musb_readl(tbase, TUSB_DMA_CTRL_REV)),
74
		"dieid",
75
		TUSB_DIDR1_HI_CHIP_REV(musb_readl(tbase, TUSB_DIDR1_HI)),
76 77
		"rev",
		TUSB_REV_MAJOR(rev), TUSB_REV_MINOR(rev));
78

79
	return tusb_get_revision(musb);
80
}
81

82 83 84 85 86 87 88 89 90
#define WBUS_QUIRK_MASK	(TUSB_PHY_OTG_CTRL_TESTM2 | TUSB_PHY_OTG_CTRL_TESTM1	\
				| TUSB_PHY_OTG_CTRL_TESTM0)

/*
 * Workaround for spontaneous WBUS wake-up issue #2 for tusb3.0.
 * Disables power detection in PHY for the duration of idle.
 */
static void tusb_wbus_quirk(struct musb *musb, int enabled)
{
91
	void __iomem	*tbase = musb->ctrl_base;
92
	static u32	phy_otg_ctrl = 0, phy_otg_ena = 0;
93
	u32		tmp;
94 95

	if (enabled) {
96 97
		phy_otg_ctrl = musb_readl(tbase, TUSB_PHY_OTG_CTRL);
		phy_otg_ena = musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE);
98 99
		tmp = TUSB_PHY_OTG_CTRL_WRPROTECT
				| phy_otg_ena | WBUS_QUIRK_MASK;
100
		musb_writel(tbase, TUSB_PHY_OTG_CTRL, tmp);
101 102
		tmp = phy_otg_ena & ~WBUS_QUIRK_MASK;
		tmp |= TUSB_PHY_OTG_CTRL_WRPROTECT | TUSB_PHY_OTG_CTRL_TESTM2;
103
		musb_writel(tbase, TUSB_PHY_OTG_CTRL_ENABLE, tmp);
104
		DBG(2, "Enabled tusb wbus quirk ctrl %08x ena %08x\n",
105 106 107
			musb_readl(tbase, TUSB_PHY_OTG_CTRL),
			musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE));
	} else if (musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE)
108 109
					& TUSB_PHY_OTG_CTRL_TESTM2) {
		tmp = TUSB_PHY_OTG_CTRL_WRPROTECT | phy_otg_ctrl;
110
		musb_writel(tbase, TUSB_PHY_OTG_CTRL, tmp);
111
		tmp = TUSB_PHY_OTG_CTRL_WRPROTECT | phy_otg_ena;
112
		musb_writel(tbase, TUSB_PHY_OTG_CTRL_ENABLE, tmp);
113
		DBG(2, "Disabled tusb wbus quirk ctrl %08x ena %08x\n",
114 115
			musb_readl(tbase, TUSB_PHY_OTG_CTRL),
			musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE));
116
		phy_otg_ctrl = 0;
117
		phy_otg_ena = 0;
118 119 120
	}
}

121 122 123 124 125
/*
 * TUSB 6010 may use a parallel bus that doesn't support byte ops;
 * so both loading and unloading FIFOs need explicit byte counts.
 */

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
static inline void
tusb_fifo_write_unaligned(void __iomem *fifo, const u8 *buf, u16 len)
{
	u32		val;
	int		i;

	if (len > 4) {
		for (i = 0; i < (len >> 2); i++) {
			memcpy(&val, buf, 4);
			musb_writel(fifo, 0, val);
			buf += 4;
		}
		len %= 4;
	}
	if (len > 0) {
		/* Write the rest 1 - 3 bytes to FIFO */
		memcpy(&val, buf, len);
		musb_writel(fifo, 0, val);
	}
}

static inline void tusb_fifo_read_unaligned(void __iomem *fifo,
						void __iomem *buf, u16 len)
{
	u32		val;
	int		i;

	if (len > 4) {
		for (i = 0; i < (len >> 2); i++) {
			val = musb_readl(fifo, 0);
			memcpy(buf, &val, 4);
			buf += 4;
		}
		len %= 4;
	}
	if (len > 0) {
		/* Read the rest 1 - 3 bytes from FIFO */
		val = musb_readl(fifo, 0);
		memcpy(buf, &val, len);
	}
}

168 169
void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *buf)
{
170
	void __iomem	*ep_conf = hw_ep->conf;
171
	void __iomem	*fifo = hw_ep->fifo;
172
	u8		epnum = hw_ep->epnum;
173

174
	prefetch(buf);
175

David Brownell's avatar
David Brownell committed
176
	DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
177
			'T', epnum, fifo, len, buf);
178 179 180

	if (epnum)
		musb_writel(ep_conf, TUSB_EP_TX_OFFSET,
181
			TUSB_EP_CONFIG_XFR_SIZE(len));
182 183
	else
		musb_writel(ep_conf, 0, TUSB_EP0_CONFIG_DIR_TX |
184
			TUSB_EP0_CONFIG_XFR_SIZE(len));
185

186 187 188 189 190 191 192 193
	if (likely((0x01 & (unsigned long) buf) == 0)) {

		/* Best case is 32bit-aligned destination address */
		if ((0x02 & (unsigned long) buf) == 0) {
			if (len >= 4) {
				writesl(fifo, buf, len >> 2);
				buf += (len & ~0x03);
				len &= 0x03;
194 195
			}
		} else {
196 197 198 199 200 201 202 203 204 205 206 207 208
			if (len >= 2) {
				u32 val;
				int i;

				/* Cannot use writesw, fifo is 32-bit */
				for (i = 0; i < (len >> 2); i++) {
					val = (u32)(*(u16 *)buf);
					buf += 2;
					val |= (*(u16 *)buf) << 16;
					buf += 2;
					musb_writel(fifo, 0, val);
				}
				len &= 0x03;
209 210 211
			}
		}
	}
212 213 214

	if (len > 0)
		tusb_fifo_write_unaligned(fifo, buf, len);
215 216 217 218
}

void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *buf)
{
219
	void __iomem	*ep_conf = hw_ep->conf;
220
	void __iomem	*fifo = hw_ep->fifo;
221
	u8		epnum = hw_ep->epnum;
222

David Brownell's avatar
David Brownell committed
223
	DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
224
			'R', epnum, fifo, len, buf);
225 226 227

	if (epnum)
		musb_writel(ep_conf, TUSB_EP_RX_OFFSET,
228
			TUSB_EP_CONFIG_XFR_SIZE(len));
229 230 231
	else
		musb_writel(ep_conf, 0, TUSB_EP0_CONFIG_XFR_SIZE(len));

232 233 234 235 236 237 238 239
	if (likely((0x01 & (unsigned long) buf) == 0)) {

		/* Best case is 32bit-aligned destination address */
		if ((0x02 & (unsigned long) buf) == 0) {
			if (len >= 4) {
				readsl(fifo, buf, len >> 2);
				buf += (len & ~0x03);
				len &= 0x03;
240 241
			}
		} else {
242 243 244 245 246 247 248 249 250 251 252 253 254
			if (len >= 2) {
				u32 val;
				int i;

				/* Cannot use readsw, fifo is 32-bit */
				for (i = 0; i < (len >> 2); i++) {
					val = musb_readl(fifo, 0);
					*(u16 *)buf = (u16)(val & 0xffff);
					buf += 2;
					*(u16 *)buf = (u16)(val >> 16);
					buf += 2;
				}
				len &= 0x03;
255 256 257
			}
		}
	}
258 259 260

	if (len > 0)
		tusb_fifo_read_unaligned(fifo, buf, len);
261 262
}

263 264
#ifdef CONFIG_USB_GADGET_MUSB_HDRC

David Brownell's avatar
David Brownell committed
265 266 267 268
/* This is used by gadget drivers, and OTG transceiver logic, allowing
 * at most mA current to be drawn from VBUS during a Default-B session
 * (that is, while VBUS exceeds 4.4V).  In Default-A (including pure host
 * mode), or low power Default-B sessions, something else supplies power.
269
 * Caller must take care of locking.
270
 */
271
static int tusb_draw_power(struct otg_transceiver *x, unsigned mA)
272
{
David Brownell's avatar
David Brownell committed
273
	struct musb	*musb = container_of(x, struct musb, xceiv);
274
	void __iomem	*tbase = musb->ctrl_base;
275 276
	u32		reg;

277 278 279 280
	/*
	 * Keep clock active when enabled. Note that this is not tied to
	 * drawing VBUS, as with OTG mA can be less than musb->min_power.
	 */
281
	if (musb->set_clock) {
282 283 284 285
		if (mA)
			musb->set_clock(musb->clock, 1);
		else
			musb->set_clock(musb->clock, 0);
286
	}
287

David Brownell's avatar
David Brownell committed
288 289 290
	/* tps65030 seems to consume max 100mA, with maybe 60mA available
	 * (measured on one board) for things other than tps and tusb.
	 *
291 292 293
	 * Boards sharing the CPU clock with CLKIN will need to prevent
	 * certain idle sleep states while the USB link is active.
	 *
David Brownell's avatar
David Brownell committed
294 295 296 297 298 299 300
	 * REVISIT we could use VBUS to supply only _one_ of { 1.5V, 3.3V }.
	 * The actual current usage would be very board-specific.  For now,
	 * it's simpler to just use an aggregate (also board-specific).
	 */
	if (x->default_a || mA < (musb->min_power << 1))
		mA = 0;

301
	reg = musb_readl(tbase, TUSB_PRCM_MNGMT);
302 303
	if (mA) {
		musb->is_bus_powered = 1;
David Brownell's avatar
David Brownell committed
304
		reg |= TUSB_PRCM_MNGMT_15_SW_EN | TUSB_PRCM_MNGMT_33_SW_EN;
305 306
	} else {
		musb->is_bus_powered = 0;
David Brownell's avatar
David Brownell committed
307
		reg &= ~(TUSB_PRCM_MNGMT_15_SW_EN | TUSB_PRCM_MNGMT_33_SW_EN);
308
	}
309
	musb_writel(tbase, TUSB_PRCM_MNGMT, reg);
David Brownell's avatar
David Brownell committed
310

David Brownell's avatar
David Brownell committed
311
	DBG(2, "draw max %d mA VBUS\n", mA);
David Brownell's avatar
David Brownell committed
312
	return 0;
313 314
}

315 316 317 318
#else
#define tusb_draw_power	NULL
#endif

319 320 321 322
/* workaround for issue 13:  change clock during chip idle
 * (to be fixed in rev3 silicon) ... symptoms include disconnect
 * or looping suspend/resume cycles
 */
323
static void tusb_set_clock_source(struct musb *musb, unsigned mode)
324
{
325
	void __iomem	*tbase = musb->ctrl_base;
326 327
	u32		reg;

328
	reg = musb_readl(tbase, TUSB_PRCM_CONF);
329 330
	reg &= ~TUSB_PRCM_CONF_SYS_CLKSEL(0x3);

331 332 333
	/* 0 = refclk (clkin, XI)
	 * 1 = PHY 60 MHz (internal PLL)
	 * 2 = not supported
334
	 * 3 = what?
335
	 */
336 337 338
	if (mode > 0)
		reg |= TUSB_PRCM_CONF_SYS_CLKSEL(mode & 0x3);

339
	musb_writel(tbase, TUSB_PRCM_CONF, reg);
340

341
	/* FIXME tusb6010_platform_retime(mode == 0); */
342 343
}

344
/*
David Brownell's avatar
David Brownell committed
345 346 347
 * Idle TUSB6010 until next wake-up event; NOR access always wakes.
 * Other code ensures that we idle unless we're connected _and_ the
 * USB link is not suspended ... and tells us the relevant wakeup
David Brownell's avatar
David Brownell committed
348
 * events.  SW_EN for voltage is handled separately.
349
 */
350
void tusb_allow_idle(struct musb *musb, u32 wakeup_enables)
351
{
352
	void __iomem	*tbase = musb->ctrl_base;
353 354
	u32		reg;

355 356 357 358
	if ((wakeup_enables & TUSB_PRCM_WBUS)
			&& (tusb_get_revision(musb) == TUSB_REV_30))
		tusb_wbus_quirk(musb, 1);

359 360
	tusb_set_clock_source(musb, 0);

David Brownell's avatar
David Brownell committed
361
	wakeup_enables |= TUSB_PRCM_WNORCS;
362
	musb_writel(tbase, TUSB_PRCM_WAKEUP_MASK, ~wakeup_enables);
David Brownell's avatar
David Brownell committed
363

David Brownell's avatar
David Brownell committed
364
	/* REVISIT writeup of WID implies that if WID set and ID is grounded,
365
	 * TUSB_PHY_OTG_CTRL.TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP must be cleared.
David Brownell's avatar
David Brownell committed
366
	 * Presumably that's mostly to save power, hence WID is immaterial ...
367 368
	 */

369
	reg = musb_readl(tbase, TUSB_PRCM_MNGMT);
David Brownell's avatar
David Brownell committed
370 371 372 373 374 375
	/* issue 4: when driving vbus, use hipower (vbus_det) comparator */
	if (is_host_active(musb)) {
		reg |= TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN;
		reg &= ~TUSB_PRCM_MNGMT_OTG_SESS_END_EN;
	} else {
		reg |= TUSB_PRCM_MNGMT_OTG_SESS_END_EN;
376
		reg &= ~TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN;
David Brownell's avatar
David Brownell committed
377 378
	}
	reg |= TUSB_PRCM_MNGMT_PM_IDLE | TUSB_PRCM_MNGMT_DEV_IDLE;
379
	musb_writel(tbase, TUSB_PRCM_MNGMT, reg);
David Brownell's avatar
David Brownell committed
380

381
	DBG(6, "idle, wake on %02x\n", wakeup_enables);
382 383 384 385 386 387 388
}

/*
 * Updates cable VBUS status. Caller must take care of locking.
 */
int musb_platform_get_vbus_status(struct musb *musb)
{
389
	void __iomem	*tbase = musb->ctrl_base;
390 391 392
	u32		otg_stat, prcm_mngmt;
	int		ret = 0;

393 394
	otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
	prcm_mngmt = musb_readl(tbase, TUSB_PRCM_MNGMT);
395 396 397 398 399 400 401 402

	/* Temporarily enable VBUS detection if it was disabled for
	 * suspend mode. Unless it's enabled otg_stat and devctl will
	 * not show correct VBUS state.
	 */
	if (!(prcm_mngmt & TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN)) {
		u32 tmp = prcm_mngmt;
		tmp |= TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN;
403 404 405
		musb_writel(tbase, TUSB_PRCM_MNGMT, tmp);
		otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
		musb_writel(tbase, TUSB_PRCM_MNGMT, prcm_mngmt);
406 407
	}

408
	if (otg_stat & TUSB_DEV_OTG_STAT_VBUS_VALID)
409 410 411 412 413
		ret = 1;

	return ret;
}

David Brownell's avatar
David Brownell committed
414 415 416 417 418 419 420
static struct timer_list musb_idle_timer;

static void musb_do_idle(unsigned long _musb)
{
	struct musb	*musb = (void *)_musb;
	unsigned long	flags;

421
	spin_lock_irqsave(&musb->lock, flags);
422 423 424 425 426 427 428 429 430

	switch (musb->xceiv.state) {
	case OTG_STATE_A_WAIT_BCON:
		if ((musb->a_wait_bcon != 0)
			&& (musb->idle_timeout == 0
				|| time_after(jiffies, musb->idle_timeout))) {
			DBG(4, "Nothing connected %s, turning off VBUS\n",
					otg_state_string(musb));
		}
431 432 433
		/* FALLTHROUGH */
	case OTG_STATE_A_IDLE:
		tusb_source_power(musb, 0);
434 435 436 437
	default:
		break;
	}

David Brownell's avatar
David Brownell committed
438 439 440
	if (!musb->is_active) {
		u32	wakeups;

441 442 443 444
		/* wait until khubd handles port change status */
		if (is_host_active(musb) && (musb->port1_status >> 16))
			goto done;

David Brownell's avatar
David Brownell committed
445
#ifdef CONFIG_USB_GADGET_MUSB_HDRC
446
		if (is_peripheral_enabled(musb) && !musb->gadget_driver)
David Brownell's avatar
David Brownell committed
447 448 449 450 451 452
			wakeups = 0;
		else {
			wakeups = TUSB_PRCM_WHOSTDISCON
					| TUSB_PRCM_WBUS
					| TUSB_PRCM_WVBUS;
			if (is_otg_enabled(musb))
David Brownell's avatar
David Brownell committed
453
				wakeups |= TUSB_PRCM_WID;
David Brownell's avatar
David Brownell committed
454 455 456 457 458 459
		}
#else
		wakeups = TUSB_PRCM_WHOSTDISCON | TUSB_PRCM_WBUS;
#endif
		tusb_allow_idle(musb, wakeups);
	}
460
done:
461
	spin_unlock_irqrestore(&musb->lock, flags);
David Brownell's avatar
David Brownell committed
462 463
}

464
/*
David Brownell's avatar
David Brownell committed
465 466 467 468 469 470 471 472 473 474 475
 * Maybe put TUSB6010 into idle mode mode depending on USB link status,
 * like "disconnected" or "suspended".  We'll be woken out of it by
 * connect, resume, or disconnect.
 *
 * Needs to be called as the last function everywhere where there is
 * register access to TUSB6010 because of NOR flash wake-up.
 * Caller should own controller spinlock.
 *
 * Delay because peripheral enables D+ pullup 3msec after SE0, and
 * we don't want to treat that full speed J as a wakeup event.
 * ... peripherals must draw only suspend current after 10 msec.
476
 */
477
void musb_platform_try_idle(struct musb *musb, unsigned long timeout)
478
{
479 480 481 482 483 484
	unsigned long		default_timeout = jiffies + msecs_to_jiffies(3);
	static unsigned long	last_timer = 0;

	if (timeout == 0)
		timeout = default_timeout;

485 486 487
	/* Never idle if active, or when VBUS timeout is not set as host */
	if (musb->is_active || ((musb->a_wait_bcon == 0)
			&& (musb->xceiv.state == OTG_STATE_A_WAIT_BCON))) {
488
		DBG(4, "%s active, deleting timer\n", otg_state_string(musb));
David Brownell's avatar
David Brownell committed
489
		del_timer(&musb_idle_timer);
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
		last_timer = jiffies;
		return;
	}

	if (time_after(last_timer, timeout)) {
		if (!timer_pending(&musb_idle_timer))
			last_timer = timeout;
		else {
			DBG(4, "Longer idle timer already pending, ignoring\n");
			return;
		}
	}
	last_timer = timeout;

	DBG(4, "%s inactive, for idle timer for %lu ms\n",
		otg_state_string(musb),
		(unsigned long)jiffies_to_msecs(timeout - jiffies));
	mod_timer(&musb_idle_timer, timeout);
508 509 510 511 512 513 514 515 516
}

/* ticks of 60 MHz clock */
#define DEVCLOCK		60000000
#define OTG_TIMER_MS(msecs)	((msecs) \
		? (TUSB_DEV_OTG_TIMER_VAL((DEVCLOCK/1000)*(msecs)) \
				| TUSB_DEV_OTG_TIMER_ENABLE) \
		: 0)

517
static void tusb_source_power(struct musb *musb, int is_on)
518
{
519
	void __iomem	*tbase = musb->ctrl_base;
520 521 522
	u32		conf, prcm, timer;
	u8		devctl;

523 524 525 526
	/* HDRC controls CPEN, but beware current surges during device
	 * connect.  They can trigger transient overcurrent conditions
	 * that must be ignored.
	 */
527

528 529
	prcm = musb_readl(tbase, TUSB_PRCM_MNGMT);
	conf = musb_readl(tbase, TUSB_DEV_CONF);
530
	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
531 532

	if (is_on) {
533 534
		if (musb->set_clock)
			musb->set_clock(musb->clock, 1);
535 536 537
		timer = OTG_TIMER_MS(OTG_TIME_A_WAIT_VRISE);
		musb->xceiv.default_a = 1;
		musb->xceiv.state = OTG_STATE_A_WAIT_VRISE;
538
		devctl |= MUSB_DEVCTL_SESSION;
539 540

		conf |= TUSB_DEV_CONF_USB_HOST_MODE;
541
		MUSB_HST_MODE(musb);
542
	} else {
543 544
		u32	otg_stat;

545 546
		timer = 0;

547
		/* If ID pin is grounded, we want to be a_idle */
548
		otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
549 550 551
		if (!(otg_stat & TUSB_DEV_OTG_STAT_ID_STATUS)) {
			switch (musb->xceiv.state) {
			case OTG_STATE_A_WAIT_VRISE:
552
			case OTG_STATE_A_WAIT_BCON:
553 554
				musb->xceiv.state = OTG_STATE_A_WAIT_VFALL;
				break;
555 556 557
			case OTG_STATE_A_WAIT_VFALL:
				musb->xceiv.state = OTG_STATE_A_IDLE;
				break;
558 559 560
			default:
				musb->xceiv.state = OTG_STATE_A_IDLE;
			}
561
			musb->is_active = 0;
562 563 564 565 566 567 568 569
			musb->xceiv.default_a = 1;
			MUSB_HST_MODE(musb);
		} else {
			musb->is_active = 0;
			musb->xceiv.default_a = 0;
			musb->xceiv.state = OTG_STATE_B_IDLE;
			MUSB_DEV_MODE(musb);
		}
570

571
		devctl &= ~MUSB_DEVCTL_SESSION;
572
		conf &= ~TUSB_DEV_CONF_USB_HOST_MODE;
573 574
		if (musb->set_clock)
			musb->set_clock(musb->clock, 0);
575 576 577
	}
	prcm &= ~(TUSB_PRCM_MNGMT_15_SW_EN | TUSB_PRCM_MNGMT_33_SW_EN);

578 579 580
	musb_writel(tbase, TUSB_PRCM_MNGMT, prcm);
	musb_writel(tbase, TUSB_DEV_OTG_TIMER, timer);
	musb_writel(tbase, TUSB_DEV_CONF, conf);
581
	musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
582 583

	DBG(1, "VBUS %s, devctl %02x otg %3x conf %08x prcm %08x\n",
David Brownell's avatar
David Brownell committed
584
		otg_state_string(musb),
585
		musb_readb(musb->mregs, MUSB_DEVCTL),
586
		musb_readl(tbase, TUSB_DEV_OTG_STAT),
587
		conf, prcm);
David Brownell's avatar
David Brownell committed
588 589
}

590 591 592 593 594 595 596 597 598 599 600 601 602
/*
 * Sets the mode to OTG, peripheral or host by changing the ID detection.
 * Caller must take care of locking.
 *
 * Note that if a mini-A cable is plugged in the ID line will stay down as
 * the weak ID pull-up is not able to pull the ID up.
 *
 * REVISIT: It would be possible to add support for changing between host
 * and peripheral modes in non-OTG configurations by reconfiguring hardware
 * and then setting musb->board_mode. For now, only support OTG mode.
 */
void musb_platform_set_mode(struct musb *musb, u8 musb_mode)
{
603
	void __iomem	*tbase = musb->ctrl_base;
604
	u32		otg_stat, phy_otg_ctrl, phy_otg_ena, dev_conf;
605 606 607 608 609 610

	if (musb->board_mode != MUSB_OTG) {
		ERR("Changing mode currently only supported in OTG mode\n");
		return;
	}

611 612 613 614
	otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
	phy_otg_ctrl = musb_readl(tbase, TUSB_PHY_OTG_CTRL);
	phy_otg_ena = musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE);
	dev_conf = musb_readl(tbase, TUSB_DEV_CONF);
615 616 617 618 619 620

	switch (musb_mode) {

#ifdef CONFIG_USB_MUSB_HDRC_HCD
	case MUSB_HOST:		/* Disable PHY ID detect, ground ID */
		phy_otg_ctrl &= ~TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
621
		phy_otg_ena |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
622 623 624 625 626 627 628 629
		dev_conf |= TUSB_DEV_CONF_ID_SEL;
		dev_conf &= ~TUSB_DEV_CONF_SOFT_ID;
		break;
#endif

#ifdef CONFIG_USB_GADGET_MUSB_HDRC
	case MUSB_PERIPHERAL:	/* Disable PHY ID detect, keep ID pull-up on */
		phy_otg_ctrl |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
630
		phy_otg_ena |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
631 632 633 634 635 636
		dev_conf |= (TUSB_DEV_CONF_ID_SEL | TUSB_DEV_CONF_SOFT_ID);
		break;
#endif

#ifdef CONFIG_USB_MUSB_OTG
	case MUSB_OTG:		/* Use PHY ID detection */
637
		phy_otg_ctrl |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
638
		phy_otg_ena |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
639 640 641 642 643 644 645 646
		dev_conf &= ~(TUSB_DEV_CONF_ID_SEL | TUSB_DEV_CONF_SOFT_ID);
		break;
#endif

	default:
		DBG(2, "Trying to set unknown 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);
652

653
	otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
654 655
	if ((musb_mode == MUSB_PERIPHERAL) &&
		!(otg_stat & TUSB_DEV_OTG_STAT_ID_STATUS))
656
			INFO("Cannot be peripheral with mini-A cable "
657 658 659
			"otg_stat: %08x\n", otg_stat);
}

660
static inline unsigned long
661
tusb_otg_ints(struct musb *musb, u32 int_src, void __iomem *tbase)
David Brownell's avatar
David Brownell committed
662
{
663
	u32		otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
664
	unsigned long	idle_timeout = 0;
David Brownell's avatar
David Brownell committed
665 666 667 668 669

	/* ID pin */
	if ((int_src & TUSB_INT_SRC_ID_STATUS_CHNG)) {
		int	default_a;

670
		if (is_otg_enabled(musb))
David Brownell's avatar
David Brownell committed
671
			default_a = !(otg_stat & TUSB_DEV_OTG_STAT_ID_STATUS);
672 673
		else
			default_a = is_host_enabled(musb);
David Brownell's avatar
David Brownell committed
674 675
		DBG(2, "Default-%c\n", default_a ? 'A' : 'B');
		musb->xceiv.default_a = default_a;
676
		tusb_source_power(musb, default_a);
677 678 679 680

		/* Don't allow idling immediately */
		if (default_a)
			idle_timeout = jiffies + (HZ * 3);
David Brownell's avatar
David Brownell committed
681
	}
682

David Brownell's avatar
David Brownell committed
683 684
	/* VBUS state change */
	if (int_src & TUSB_INT_SRC_VBUS_SENSE_CHNG) {
685 686 687 688

		/* B-dev state machine:  no vbus ~= disconnect */
		if ((is_otg_enabled(musb) && !musb->xceiv.default_a)
				|| !is_host_enabled(musb)) {
689
#ifdef CONFIG_USB_MUSB_HDRC_HCD
690
			/* ? musb_root_disconnect(musb); */
691 692 693 694 695 696 697 698
			musb->port1_status &=
				~(USB_PORT_STAT_CONNECTION
				| USB_PORT_STAT_ENABLE
				| USB_PORT_STAT_LOW_SPEED
				| USB_PORT_STAT_HIGH_SPEED
				| USB_PORT_STAT_TEST
				);
#endif
David Brownell's avatar
David Brownell committed
699

David Brownell's avatar
David Brownell committed
700
			if (otg_stat & TUSB_DEV_OTG_STAT_SESS_END) {
701
				DBG(1, "Forcing disconnect (no interrupt)\n");
David Brownell's avatar
David Brownell committed
702 703 704
				if (musb->xceiv.state != OTG_STATE_B_IDLE) {
					/* INTR_DISCONNECT can hide... */
					musb->xceiv.state = OTG_STATE_B_IDLE;
705
					musb->int_usb |= MUSB_INTR_DISCONNECT;
David Brownell's avatar
David Brownell committed
706
				}
David Brownell's avatar
David Brownell committed
707 708
				musb->is_active = 0;
			}
David Brownell's avatar
David Brownell committed
709 710
			DBG(2, "vbus change, %s, otg %03x\n",
				otg_state_string(musb), otg_stat);
711
			idle_timeout = jiffies + (1 * HZ);
712
			schedule_work(&musb->irq_work);
713 714

		} else /* A-dev state machine */ {
715
			DBG(2, "vbus change, %s, otg %03x\n",
716 717 718
				otg_state_string(musb), otg_stat);

			switch (musb->xceiv.state) {
719 720
			case OTG_STATE_A_IDLE:
				DBG(2, "Got SRP, turning on VBUS\n");
721
				musb_set_vbus(musb, 1);
722 723 724 725 726 727 728

				/* CONNECT can wake if a_wait_bcon is set */
				if (musb->a_wait_bcon != 0)
					musb->is_active = 0;
				else
					musb->is_active = 1;

729 730 731 732 733 734
				/*
				 * OPT FS A TD.4.6 needs few seconds for
				 * A_WAIT_VRISE
				 */
				idle_timeout = jiffies + (2 * HZ);

735
				break;
736 737 738 739 740 741
			case OTG_STATE_A_WAIT_VRISE:
				/* ignore; A-session-valid < VBUS_VALID/2,
				 * we monitor this with the timer
				 */
				break;
			case OTG_STATE_A_WAIT_VFALL:
742 743
				/* REVISIT this irq triggers during short
				 * spikes causet by enumeration ...
744 745 746
				 */
				if (musb->vbuserr_retry) {
					musb->vbuserr_retry--;
747
					tusb_source_power(musb, 1);
748 749 750 751
				} else {
					musb->vbuserr_retry
						= VBUSERR_RETRY_COUNT;
					tusb_source_power(musb, 0);
752 753 754 755 756
				}
				break;
			default:
				break;
			}
David Brownell's avatar
David Brownell committed
757
		}
758 759
	}

David Brownell's avatar
David Brownell committed
760 761
	/* OTG timer expiration */
	if (int_src & TUSB_INT_SRC_OTG_TIMEOUT) {
David Brownell's avatar
David Brownell committed
762 763
		u8	devctl;

764 765 766 767
		DBG(4, "%s timer, %03x\n", otg_state_string(musb), otg_stat);

		switch (musb->xceiv.state) {
		case OTG_STATE_A_WAIT_VRISE:
768 769 770
			/* VBUS has probably been valid for a while now,
			 * but may well have bounced out of range a bit
			 */
771
			devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
772
			if (otg_stat & TUSB_DEV_OTG_STAT_VBUS_VALID) {
773 774
				if ((devctl & MUSB_DEVCTL_VBUS)
						!= MUSB_DEVCTL_VBUS) {
775 776 777
					DBG(2, "devctl %02x\n", devctl);
					break;
				}
778
				musb->xceiv.state = OTG_STATE_A_WAIT_BCON;
779
				musb->is_active = 0;
780 781
				idle_timeout = jiffies
					+ msecs_to_jiffies(musb->a_wait_bcon);
782
			} else {
783
				/* REVISIT report overcurrent to hub? */
David Brownell's avatar
David Brownell committed
784
				ERR("vbus too slow, devctl %02x\n", devctl);
785
				tusb_source_power(musb, 0);
786 787 788
			}
			break;
		case OTG_STATE_A_WAIT_BCON:
789 790 791
			if (musb->a_wait_bcon != 0)
				idle_timeout = jiffies
					+ msecs_to_jiffies(musb->a_wait_bcon);
792 793 794 795 796 797 798 799
			break;
		case OTG_STATE_A_SUSPEND:
			break;
		case OTG_STATE_B_WAIT_ACON:
			break;
		default:
			break;
		}
800
	}
801
	schedule_work(&musb->irq_work);
802 803

	return idle_timeout;
804 805
}

806
static irqreturn_t tusb_interrupt(int irq, void *__hci)
807 808
{
	struct musb	*musb = __hci;
809
	void __iomem	*tbase = musb->ctrl_base;
810
	unsigned long	flags, idle_timeout = 0;
811
	u32		int_mask, int_src;
812

813
	spin_lock_irqsave(&musb->lock, flags);
814

815
	/* Mask all interrupts to allow using both edge and level GPIO irq */
816 817
	int_mask = musb_readl(tbase, TUSB_INT_MASK);
	musb_writel(tbase, TUSB_INT_MASK, ~TUSB_INT_MASK_RESERVED_BITS);
818

819
	int_src = musb_readl(tbase, TUSB_INT_SRC) & ~TUSB_INT_SRC_RESERVED_BITS;
David Brownell's avatar
David Brownell committed
820
	DBG(3, "TUSB IRQ %08x\n", int_src);
821

David Brownell's avatar
David Brownell committed
822
	musb->int_usb = (u8) int_src;
823

David Brownell's avatar
David Brownell committed
824 825
	/* Acknowledge wake-up source interrupts */
	if (int_src & TUSB_INT_SRC_DEV_WAKEUP) {
826 827 828
		u32	reg;
		u32	i;

829 830 831
		if (tusb_get_revision(musb) == TUSB_REV_30)
			tusb_wbus_quirk(musb, 0);

832 833 834 835
		/* there are issues re-locking the PLL on wakeup ... */

		/* work around issue 8 */
		for (i = 0xf7f7f7; i > 0xf7f7f7 - 1000; i--) {
836 837 838
			musb_writel(tbase, TUSB_SCRATCH_PAD, 0);
			musb_writel(tbase, TUSB_SCRATCH_PAD, i);
			reg = musb_readl(tbase, TUSB_SCRATCH_PAD);
839 840
			if (reg == i)
				break;
841
			DBG(6, "TUSB NOR not ready\n");
842 843 844 845
		}

		/* work around issue 13 (2nd half) */
		tusb_set_clock_source(musb, 1);
846

847 848
		reg = musb_readl(tbase, TUSB_PRCM_WAKEUP_SOURCE);
		musb_writel(tbase, TUSB_PRCM_WAKEUP_CLEAR, reg);
David Brownell's avatar
David Brownell committed
849 850
		if (reg & ~TUSB_PRCM_WNORCS) {
			musb->is_active = 1;
851
			schedule_work(&musb->irq_work);
852
		}
David Brownell's avatar
David Brownell committed
853 854
		DBG(3, "wake %sactive %02x\n",
				musb->is_active ? "" : "in", reg);
855

856
		/* REVISIT host side TUSB_PRCM_WHOSTDISCON, TUSB_PRCM_WBUS */
857 858
	}

859 860 861
	if (int_src & TUSB_INT_SRC_USB_IP_CONN)
		del_timer(&musb_idle_timer);

David Brownell's avatar
David Brownell committed
862 863 864 865
	/* OTG state change reports (annoyingly) not issued by Mentor core */
	if (int_src & (TUSB_INT_SRC_VBUS_SENSE_CHNG
				| TUSB_INT_SRC_OTG_TIMEOUT
				| TUSB_INT_SRC_ID_STATUS_CHNG))
866
		idle_timeout = tusb_otg_ints(musb, int_src, tbase);
867 868 869 870

	/* TX dma callback must be handled here, RX dma callback is
	 * handled in tusb_omap_dma_cb.
	 */
David Brownell's avatar
David Brownell committed
871
	if ((int_src & TUSB_INT_SRC_TXRX_DMA_DONE)) {
872 873
		u32	dma_src = musb_readl(tbase, TUSB_DMA_INT_SRC);
		u32	real_dma_src = musb_readl(tbase, TUSB_DMA_INT_MASK);
David Brownell's avatar
David Brownell committed
874

David Brownell's avatar
David Brownell committed
875
		DBG(3, "DMA IRQ %08x\n", dma_src);
876
		real_dma_src = ~real_dma_src & dma_src;
David Brownell's avatar
David Brownell committed
877
		if (tusb_dma_omap() && real_dma_src) {
878 879 880 881 882
			int	tx_source = (real_dma_src & 0xffff);
			int	i;

			for (i = 1; i <= 15; i++) {
				if (tx_source & (1 << i)) {
883
					DBG(3, "completing ep%i %s\n", i, "tx");
884 885 886 887
					musb_dma_completion(musb, i, 1);
				}
			}
		}
888
		musb_writel(tbase, TUSB_DMA_INT_CLEAR, dma_src);
889 890 891 892
	}

	/* EP interrupts. In OCP mode tusb6010 mirrors the MUSB * interrupts */
	if (int_src & (TUSB_INT_SRC_USB_IP_TX | TUSB_INT_SRC_USB_IP_RX)) {
893
		u32	musb_src = musb_readl(tbase, TUSB_USBIP_INT_SRC);
David Brownell's avatar
David Brownell committed
894

895
		musb_writel(tbase, TUSB_USBIP_INT_CLEAR, musb_src);
896 897
		musb->int_rx = (((musb_src >> 16) & 0xffff) << 1);
		musb->int_tx = (musb_src & 0xffff);
David Brownell's avatar
David Brownell committed
898 899
	} else
		musb->int_rx = musb->int_tx = 0;
900

David Brownell's avatar
David Brownell committed
901
	if (int_src & (TUSB_INT_SRC_USB_IP_TX | TUSB_INT_SRC_USB_IP_RX | 0xff))
David Brownell's avatar
David Brownell committed
902
		musb_interrupt(musb);
903 904

	/* Acknowledge TUSB interrupts. Clear only non-reserved bits */
905
	musb_writel(tbase, TUSB_INT_SRC_CLEAR,
David Brownell's avatar
David Brownell committed
906
		int_src & ~TUSB_INT_MASK_RESERVED_BITS);
907

908
	musb_platform_try_idle(musb, idle_timeout);
909

910
	musb_writel(tbase, TUSB_INT_MASK, int_mask);
911
	spin_unlock_irqrestore(&musb->lock, flags);
912 913 914 915 916 917 918 919 920 921 922 923 924

	return IRQ_HANDLED;
}

static int dma_off;

/*
 * Enables TUSB6010. Caller must take care of locking.
 * REVISIT:
 * - Check what is unnecessary in MGC_HdrcStart()
 */
void musb_platform_enable(struct musb * musb)
{
925
	void __iomem	*tbase = musb->ctrl_base;
926 927 928

	/* Setup TUSB6010 main interrupt mask. Enable all interrupts except SOF.
	 * REVISIT: Enable and deal with TUSB_INT_SRC_USB_IP_SOF */
929
	musb_writel(tbase, TUSB_INT_MASK, TUSB_INT_SRC_USB_IP_SOF);
930 931

	/* Setup TUSB interrupt, disable DMA and GPIO interrupts */
932 933 934
	musb_writel(tbase, TUSB_USBIP_INT_MASK, 0);
	musb_writel(tbase, TUSB_DMA_INT_MASK, 0x7fffffff);
	musb_writel(tbase, TUSB_GPIO_INT_MASK, 0x1ff);
935 936

	/* Clear all subsystem interrups */
937 938 939
	musb_writel(tbase, TUSB_USBIP_INT_CLEAR, 0x7fffffff);
	musb_writel(tbase, TUSB_DMA_INT_CLEAR, 0x7fffffff);
	musb_writel(tbase, TUSB_GPIO_INT_CLEAR, 0x1ff);
940 941

	/* Acknowledge pending interrupt(s) */
942
	musb_writel(tbase, TUSB_INT_SRC_CLEAR, ~TUSB_INT_MASK_RESERVED_BITS);
943 944 945

	/* Only 0 clock cycles for minimum interrupt de-assertion time and
	 * interrupt polarity active low seems to work reliably here */
946
	musb_writel(tbase, TUSB_INT_CTRL_CONF,
947
			TUSB_INT_CTRL_CONF_INT_RELCYC(0));
948 949 950

	set_irq_type(musb->nIrq, IRQ_TYPE_LEVEL_LOW);

951
	/* maybe force into the Default-A OTG state machine */
952
	if (!(musb_readl(tbase, TUSB_DEV_OTG_STAT)
953
			& TUSB_DEV_OTG_STAT_ID_STATUS))
954
		musb_writel(tbase, TUSB_INT_SRC_SET,
955
				TUSB_INT_SRC_ID_STATUS_CHNG);
David Brownell's avatar
David Brownell committed
956

957 958 959 960 961 962 963 964 965 966 967 968
	if (is_dma_capable() && dma_off)
		printk(KERN_WARNING "%s %s: dma not reactivated\n",
				__FILE__, __FUNCTION__);
	else
		dma_off = 1;
}

/*
 * Disables TUSB6010. Caller must take care of locking.
 */
void musb_platform_disable(struct musb *musb)
{
969
	void __iomem	*tbase = musb->ctrl_base;
970

971 972
	/* FIXME stop DMA, IRQs, timers, ... */

973
	/* disable all IRQs */
974 975 976 977
	musb_writel(tbase, TUSB_INT_MASK, ~TUSB_INT_MASK_RESERVED_BITS);
	musb_writel(tbase, TUSB_USBIP_INT_MASK, 0);
	musb_writel(tbase, TUSB_DMA_INT_MASK, 0x7fffffff);
	musb_writel(tbase, TUSB_GPIO_INT_MASK, 0x1ff);
978 979 980

	del_timer(&musb_idle_timer);

981
	if (is_dma_capable() && !dma_off) {
982 983 984 985 986 987 988 989 990 991
		printk(KERN_WARNING "%s %s: dma still active\n",
				__FILE__, __FUNCTION__);
		dma_off = 1;
	}
}

/*
 * Sets up TUSB6010 CPU interface specific signals and registers
 * Note: Settings optimized for OMAP24xx
 */
992
static void __init tusb_setup_cpu_interface(struct musb *musb)
993
{
994
	void __iomem	*tbase = musb->ctrl_base;
995

996 997 998 999
	/*
	 * Disable GPIO[5:0] pullups (used as output DMA requests)
	 * Don't disable GPIO[7:6] as they are needed for wake-up.
	 */
1000
	musb_writel(tbase, TUSB_PULLUP_1_CTRL, 0x0000003F);
1001

1002
	/* Disable all pullups on NOR IF, DMAREQ0 and DMAREQ1 */
1003
	musb_writel(tbase, TUSB_PULLUP_2_CTRL, 0x01FFFFFF);
1004 1005

	/* Turn GPIO[5:0] to DMAREQ[5:0] signals */
1006
	musb_writel(tbase, TUSB_GPIO_CONF, TUSB_GPIO_CONF_DMAREQ(0x3f));
1007 1008 1009

	/* Burst size 16x16 bits, all six DMA requests enabled, DMA request
	 * de-assertion time 2 system clocks p 62 */
1010
	musb_writel(tbase, TUSB_DMA_REQ_CONF,
1011 1012 1013 1014 1015
		TUSB_DMA_REQ_CONF_BURST_SIZE(2) |
		TUSB_DMA_REQ_CONF_DMA_REQ_EN(0x3f) |
		TUSB_DMA_REQ_CONF_DMA_REQ_ASSER(2));

	/* Set 0 wait count for synchronous burst access */
1016
	musb_writel(tbase, TUSB_WAIT_COUNT, 1);
1017 1018
}

1019
static int __init tusb_start(struct musb *musb)
1020
{
1021
	void __iomem	*tbase = musb->ctrl_base;
1022
	int		ret = 0;
1023
	unsigned long	flags;
1024
	u32		reg;
1025 1026 1027 1028 1029

	if (musb->board_set_power)
		ret = musb->board_set_power(1);
	if (ret != 0) {
		printk(KERN_ERR "tusb: Cannot enable TUSB6010\n");
1030
		return ret;
1031 1032
	}

1033
	spin_lock_irqsave(&musb->lock, flags);
1034

1035
	if (musb_readl(tbase, TUSB_PROD_TEST_RESET) !=
1036 1037 1038 1039 1040 1041 1042 1043
		TUSB_PROD_TEST_RESET_VAL) {
		printk(KERN_ERR "tusb: Unable to detect TUSB6010\n");
		goto err;
	}

	ret = tusb_print_revision(musb);
	if (ret < 2) {
		printk(KERN_ERR "tusb: Unsupported TUSB6010 revision %i\n",
1044
				ret);
1045 1046 1047 1048 1049
		goto err;
	}

	/* The uint bit for "USB non-PDR interrupt enable" has to be 1 when
	 * NOR FLASH interface is used */
1050
	musb_writel(tbase, TUSB_VLYNQ_CTRL, 8);
1051 1052

	/* Select PHY free running 60MHz as a system clock */
1053
	tusb_set_clock_source(musb, 1);
1054 1055 1056 1057

	/* VBus valid timer 1us, disable DFT/Debug and VLYNQ clocks for
	 * power saving, enable VBus detect and session end comparators,
	 * enable IDpullup, enable VBus charging */
1058
	musb_writel(tbase, TUSB_PRCM_MNGMT,
1059 1060 1061 1062 1063 1064 1065
		TUSB_PRCM_MNGMT_VBUS_VALID_TIMER(0xa) |
		TUSB_PRCM_MNGMT_VBUS_VALID_FLT_EN |
		TUSB_PRCM_MNGMT_OTG_SESS_END_EN |
		TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN |
		TUSB_PRCM_MNGMT_OTG_ID_PULLUP);
	tusb_setup_cpu_interface(musb);

1066
	/* simplify:  always sense/pullup ID pins, as if in OTG mode */
1067
	reg = musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE);
1068
	reg |= TUSB_PHY_OTG_CTRL_WRPROTECT | TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
1069
	musb_writel(tbase, TUSB_PHY_OTG_CTRL_ENABLE, reg);
1070

1071
	reg = musb_readl(tbase, TUSB_PHY_OTG_CTRL);
1072
	reg |= TUSB_PHY_OTG_CTRL_WRPROTECT | TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
1073
	musb_writel(tbase, TUSB_PHY_OTG_CTRL, reg);
1074

1075
	spin_unlock_irqrestore(&musb->lock, flags);
1076 1077 1078 1079

	return 0;

err:
1080
	spin_unlock_irqrestore(&musb->lock, flags);
1081

1082 1083 1084 1085 1086 1087
	if (musb->board_set_power)
		musb->board_set_power(0);

	return -ENODEV;
}

1088
int __init musb_platform_init(struct musb *musb)
1089 1090 1091
{
	struct platform_device	*pdev;
	struct resource		*mem;
1092
	void __iomem		*sync;
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
	int			ret;

	pdev = to_platform_device(musb->controller);

	/* dma address for async dma */
	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	musb->async = mem->start;

	/* dma address for sync dma */
	mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
	if (!mem) {
		pr_debug("no sync dma resource?\n");
		return -ENODEV;
	}
	musb->sync = mem->start;

1109 1110 1111 1112 1113 1114 1115
	sync = ioremap(mem->start, mem->end - mem->start + 1);
	if (!sync) {
		pr_debug("ioremap for sync failed\n");
		return -ENOMEM;
	}
	musb->sync_va = sync;

1116 1117 1118
	/* Offsets from base: VLYNQ at 0x000, MUSB regs at 0x400,
	 * FIFOs at 0x600, TUSB at 0x800
	 */
1119
	musb->mregs += TUSB_BASE_OFFSET;
1120 1121 1122 1123 1124 1125 1126 1127

	ret = tusb_start(musb);
	if (ret) {
		printk(KERN_ERR "Could not start tusb6010 (%d)\n",
				ret);
		return -ENODEV;
	}
	musb->isr = tusb_interrupt;
1128 1129

	if (is_host_enabled(musb))
1130
		musb->board_set_vbus = tusb_source_power;
1131
	if (is_peripheral_enabled(musb))
1132
		musb->xceiv.set_power = tusb_draw_power;
1133

David Brownell's avatar
David Brownell committed
1134
	setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
1135 1136 1137 1138 1139 1140

	return ret;
}

int musb_platform_exit(struct musb *musb)
{
1141 1142
	del_timer_sync(&musb_idle_timer);

1143 1144 1145
	if (musb->board_set_power)
		musb->board_set_power(0);

1146 1147
	iounmap(musb->sync_va);

1148 1149
	return 0;
}