musb_core.c 74.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
F
Felipe Balbi 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/*
 * MUSB OTG driver core code
 *
 * Copyright 2005 Mentor Graphics Corporation
 * Copyright (C) 2005-2006 by Texas Instruments
 * Copyright (C) 2006-2007 Nokia Corporation
 */

/*
 * Inventra (Multipoint) Dual-Role Controller Driver for Linux.
 *
 * This consists of a Host Controller Driver (HCD) and a peripheral
 * controller driver implementing the "Gadget" API; OTG support is
 * in the works.  These are normal Linux-USB controller drivers which
 * use IRQs and have no dedicated thread.
 *
 * This version of the driver has only been used with products from
 * Texas Instruments.  Those products integrate the Inventra logic
 * with other DMA, IRQ, and bus modules, as well as other logic that
 * needs to be reflected in this driver.
 *
 *
 * NOTE:  the original Mentor code here was pretty much a collection
 * of mechanisms that don't seem to have been fully integrated/working
 * for *any* Linux kernel version.  This version aims at Linux 2.6.now,
 * Key open issues include:
 *
 *  - Lack of host-side transaction scheduling, for all transfer types.
 *    The hardware doesn't do it; instead, software must.
 *
 *    This is not an issue for OTG devices that don't support external
 *    hubs, but for more "normal" USB hosts it's a user issue that the
 *    "multipoint" support doesn't scale in the expected ways.  That
 *    includes DaVinci EVM in a common non-OTG mode.
 *
 *      * Control and bulk use dedicated endpoints, and there's as
 *        yet no mechanism to either (a) reclaim the hardware when
 *        peripherals are NAKing, which gets complicated with bulk
 *        endpoints, or (b) use more than a single bulk endpoint in
 *        each direction.
 *
 *        RESULT:  one device may be perceived as blocking another one.
 *
 *      * Interrupt and isochronous will dynamically allocate endpoint
 *        hardware, but (a) there's no record keeping for bandwidth;
 *        (b) in the common case that few endpoints are available, there
 *        is no mechanism to reuse endpoints to talk to multiple devices.
 *
 *        RESULT:  At one extreme, bandwidth can be overcommitted in
 *        some hardware configurations, no faults will be reported.
 *        At the other extreme, the bandwidth capabilities which do
 *        exist tend to be severely undercommitted.  You can't yet hook
 *        up both a keyboard and a mouse to an external USB hub.
 */

/*
 * This gets many kinds of configuration information:
 *	- Kconfig for everything user-configurable
 *	- platform_device for addressing, irq, and platform_data
61
 *	- platform_data is mostly for board-specific information
D
David Brownell 已提交
62
 *	  (plus recentrly, SOC or family details)
F
Felipe Balbi 已提交
63 64 65 66 67 68 69 70 71 72
 *
 * Most of the conditional compilation will (someday) vanish.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/kobject.h>
73
#include <linux/prefetch.h>
F
Felipe Balbi 已提交
74 75
#include <linux/platform_device.h>
#include <linux/io.h>
76
#include <linux/dma-mapping.h>
77
#include <linux/usb.h>
78
#include <linux/usb/of.h>
F
Felipe Balbi 已提交
79 80

#include "musb_core.h"
81
#include "musb_trace.h"
F
Felipe Balbi 已提交
82

D
David Brownell 已提交
83
#define TA_WAIT_BCON(m) max_t(int, (m)->a_wait_bcon, OTG_TIME_A_WAIT_BCON)
F
Felipe Balbi 已提交
84 85 86 87 88


#define DRIVER_AUTHOR "Mentor Graphics, Texas Instruments, Nokia"
#define DRIVER_DESC "Inventra Dual-Role USB Controller Driver"

89
#define MUSB_VERSION "6.0"
F
Felipe Balbi 已提交
90 91 92

#define DRIVER_INFO DRIVER_DESC ", v" MUSB_VERSION

93
#define MUSB_DRIVER_NAME "musb-hdrc"
F
Felipe Balbi 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
const char musb_driver_name[] = MUSB_DRIVER_NAME;

MODULE_DESCRIPTION(DRIVER_INFO);
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:" MUSB_DRIVER_NAME);


/*-------------------------------------------------------------------------*/

static inline struct musb *dev_to_musb(struct device *dev)
{
	return dev_get_drvdata(dev);
}

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
enum musb_mode musb_get_mode(struct device *dev)
{
	enum usb_dr_mode mode;

	mode = usb_get_dr_mode(dev);
	switch (mode) {
	case USB_DR_MODE_HOST:
		return MUSB_HOST;
	case USB_DR_MODE_PERIPHERAL:
		return MUSB_PERIPHERAL;
	case USB_DR_MODE_OTG:
	case USB_DR_MODE_UNKNOWN:
	default:
		return MUSB_OTG;
	}
}
EXPORT_SYMBOL_GPL(musb_get_mode);

F
Felipe Balbi 已提交
127 128
/*-------------------------------------------------------------------------*/

129
static int musb_ulpi_read(struct usb_phy *phy, u32 reg)
130
{
131
	void __iomem *addr = phy->io_priv;
132 133 134
	int	i = 0;
	u8	r;
	u8	power;
135 136 137
	int	ret;

	pm_runtime_get_sync(phy->io_dev);
138 139 140 141 142 143 144 145 146 147

	/* Make sure the transceiver is not in low power mode */
	power = musb_readb(addr, MUSB_POWER);
	power &= ~MUSB_POWER_SUSPENDM;
	musb_writeb(addr, MUSB_POWER, power);

	/* REVISIT: musbhdrc_ulpi_an.pdf recommends setting the
	 * ULPICarKitControlDisableUTMI after clearing POWER_SUSPENDM.
	 */

148
	musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)reg);
149 150 151 152 153 154
	musb_writeb(addr, MUSB_ULPI_REG_CONTROL,
			MUSB_ULPI_REG_REQ | MUSB_ULPI_RDN_WR);

	while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
				& MUSB_ULPI_REG_CMPLT)) {
		i++;
155 156 157 158
		if (i == 10000) {
			ret = -ETIMEDOUT;
			goto out;
		}
159 160 161 162 163 164

	}
	r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
	r &= ~MUSB_ULPI_REG_CMPLT;
	musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);

165 166 167 168 169 170
	ret = musb_readb(addr, MUSB_ULPI_REG_DATA);

out:
	pm_runtime_put(phy->io_dev);

	return ret;
171 172
}

173
static int musb_ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
174
{
175
	void __iomem *addr = phy->io_priv;
176 177 178
	int	i = 0;
	u8	r = 0;
	u8	power;
179 180 181
	int	ret = 0;

	pm_runtime_get_sync(phy->io_dev);
182 183 184 185 186 187

	/* Make sure the transceiver is not in low power mode */
	power = musb_readb(addr, MUSB_POWER);
	power &= ~MUSB_POWER_SUSPENDM;
	musb_writeb(addr, MUSB_POWER, power);

188 189
	musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)reg);
	musb_writeb(addr, MUSB_ULPI_REG_DATA, (u8)val);
190 191 192 193 194
	musb_writeb(addr, MUSB_ULPI_REG_CONTROL, MUSB_ULPI_REG_REQ);

	while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
				& MUSB_ULPI_REG_CMPLT)) {
		i++;
195 196 197 198
		if (i == 10000) {
			ret = -ETIMEDOUT;
			goto out;
		}
199 200 201 202 203 204
	}

	r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
	r &= ~MUSB_ULPI_REG_CMPLT;
	musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);

205 206 207 208
out:
	pm_runtime_put(phy->io_dev);

	return ret;
209 210
}

211
static struct usb_phy_io_ops musb_ulpi_access = {
212 213 214 215 216 217
	.read = musb_ulpi_read,
	.write = musb_ulpi_write,
};

/*-------------------------------------------------------------------------*/

218 219 220 221 222
static u32 musb_default_fifo_offset(u8 epnum)
{
	return 0x20 + (epnum * 4);
}

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
/* "flat" mapping: each endpoint has its own i/o address */
static void musb_flat_ep_select(void __iomem *mbase, u8 epnum)
{
}

static u32 musb_flat_ep_offset(u8 epnum, u16 offset)
{
	return 0x100 + (0x10 * epnum) + offset;
}

/* "indexed" mapping: INDEX register controls register bank select */
static void musb_indexed_ep_select(void __iomem *mbase, u8 epnum)
{
	musb_writeb(mbase, MUSB_INDEX, epnum);
}

static u32 musb_indexed_ep_offset(u8 epnum, u16 offset)
{
	return 0x10 + offset;
}

244 245 246 247 248
static u32 musb_default_busctl_offset(u8 epnum, u16 offset)
{
	return 0x80 + (0x08 * epnum) + offset;
}

249 250
static u8 musb_default_readb(const void __iomem *addr, unsigned offset)
{
251 252 253 254
	u8 data =  __raw_readb(addr + offset);

	trace_musb_readb(__builtin_return_address(0), addr, offset, data);
	return data;
255 256 257 258
}

static void musb_default_writeb(void __iomem *addr, unsigned offset, u8 data)
{
259
	trace_musb_writeb(__builtin_return_address(0), addr, offset, data);
260 261 262 263 264
	__raw_writeb(data, addr + offset);
}

static u16 musb_default_readw(const void __iomem *addr, unsigned offset)
{
265 266 267 268
	u16 data = __raw_readw(addr + offset);

	trace_musb_readw(__builtin_return_address(0), addr, offset, data);
	return data;
269 270 271 272
}

static void musb_default_writew(void __iomem *addr, unsigned offset, u16 data)
{
273
	trace_musb_writew(__builtin_return_address(0), addr, offset, data);
274 275 276
	__raw_writew(data, addr + offset);
}

F
Felipe Balbi 已提交
277 278 279
/*
 * Load an endpoint's FIFO
 */
280 281
static void musb_default_write_fifo(struct musb_hw_ep *hw_ep, u16 len,
				    const u8 *src)
F
Felipe Balbi 已提交
282
{
283
	struct musb *musb = hw_ep->musb;
F
Felipe Balbi 已提交
284 285
	void __iomem *fifo = hw_ep->fifo;

286 287 288
	if (unlikely(len == 0))
		return;

F
Felipe Balbi 已提交
289 290
	prefetch((u8 *)src);

291
	dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
F
Felipe Balbi 已提交
292 293 294 295 296 297 298 299 300
			'T', hw_ep->epnum, fifo, len, src);

	/* we can't assume unaligned reads work */
	if (likely((0x01 & (unsigned long) src) == 0)) {
		u16	index = 0;

		/* best case is 32bit-aligned source address */
		if ((0x02 & (unsigned long) src) == 0) {
			if (len >= 4) {
301
				iowrite32_rep(fifo, src + index, len >> 2);
F
Felipe Balbi 已提交
302 303 304
				index += len & ~0x03;
			}
			if (len & 0x02) {
305
				__raw_writew(*(u16 *)&src[index], fifo);
F
Felipe Balbi 已提交
306 307 308 309
				index += 2;
			}
		} else {
			if (len >= 2) {
310
				iowrite16_rep(fifo, src + index, len >> 1);
F
Felipe Balbi 已提交
311 312 313 314
				index += len & ~0x01;
			}
		}
		if (len & 0x01)
315
			__raw_writeb(src[index], fifo);
F
Felipe Balbi 已提交
316 317
	} else  {
		/* byte aligned */
318
		iowrite8_rep(fifo, src, len);
F
Felipe Balbi 已提交
319 320 321 322 323 324
	}
}

/*
 * Unload an endpoint's FIFO
 */
325
static void musb_default_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
F
Felipe Balbi 已提交
326
{
327
	struct musb *musb = hw_ep->musb;
F
Felipe Balbi 已提交
328 329
	void __iomem *fifo = hw_ep->fifo;

330 331 332
	if (unlikely(len == 0))
		return;

333
	dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
F
Felipe Balbi 已提交
334 335 336 337 338 339 340 341 342
			'R', hw_ep->epnum, fifo, len, dst);

	/* we can't assume unaligned writes work */
	if (likely((0x01 & (unsigned long) dst) == 0)) {
		u16	index = 0;

		/* best case is 32bit-aligned destination address */
		if ((0x02 & (unsigned long) dst) == 0) {
			if (len >= 4) {
343
				ioread32_rep(fifo, dst, len >> 2);
F
Felipe Balbi 已提交
344 345 346
				index = len & ~0x03;
			}
			if (len & 0x02) {
347
				*(u16 *)&dst[index] = __raw_readw(fifo);
F
Felipe Balbi 已提交
348 349 350 351
				index += 2;
			}
		} else {
			if (len >= 2) {
352
				ioread16_rep(fifo, dst, len >> 1);
F
Felipe Balbi 已提交
353 354 355 356
				index = len & ~0x01;
			}
		}
		if (len & 0x01)
357
			dst[index] = __raw_readb(fifo);
F
Felipe Balbi 已提交
358 359
	} else  {
		/* byte aligned */
360
		ioread8_rep(fifo, dst, len);
F
Felipe Balbi 已提交
361 362 363
	}
}

364 365 366 367 368 369 370 371
/*
 * Old style IO functions
 */
u8 (*musb_readb)(const void __iomem *addr, unsigned offset);
EXPORT_SYMBOL_GPL(musb_readb);

void (*musb_writeb)(void __iomem *addr, unsigned offset, u8 data);
EXPORT_SYMBOL_GPL(musb_writeb);
F
Felipe Balbi 已提交
372

373 374 375 376 377 378
u16 (*musb_readw)(const void __iomem *addr, unsigned offset);
EXPORT_SYMBOL_GPL(musb_readw);

void (*musb_writew)(void __iomem *addr, unsigned offset, u16 data);
EXPORT_SYMBOL_GPL(musb_writew);

379 380 381 382 383 384 385
u32 musb_readl(const void __iomem *addr, unsigned offset)
{
	u32 data = __raw_readl(addr + offset);

	trace_musb_readl(__builtin_return_address(0), addr, offset, data);
	return data;
}
386 387
EXPORT_SYMBOL_GPL(musb_readl);

388 389 390 391 392
void musb_writel(void __iomem *addr, unsigned offset, u32 data)
{
	trace_musb_writel(__builtin_return_address(0), addr, offset, data);
	__raw_writel(data, addr + offset);
}
393 394
EXPORT_SYMBOL_GPL(musb_writel);

395 396 397 398 399 400 401 402 403
#ifndef CONFIG_MUSB_PIO_ONLY
struct dma_controller *
(*musb_dma_controller_create)(struct musb *musb, void __iomem *base);
EXPORT_SYMBOL(musb_dma_controller_create);

void (*musb_dma_controller_destroy)(struct dma_controller *c);
EXPORT_SYMBOL(musb_dma_controller_destroy);
#endif

404 405 406 407 408 409 410 411 412 413 414 415
/*
 * New style IO functions
 */
void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
{
	return hw_ep->musb->io.read_fifo(hw_ep, len, dst);
}

void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
{
	return hw_ep->musb->io.write_fifo(hw_ep, len, src);
}
F
Felipe Balbi 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

/*-------------------------------------------------------------------------*/

/* for high speed test mode; see USB 2.0 spec 7.1.20 */
static const u8 musb_test_packet[53] = {
	/* implicit SYNC then DATA0 to start */

	/* JKJKJKJK x9 */
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	/* JJKKJJKK x8 */
	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
	/* JJJJKKKK x8 */
	0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
	/* JJJJJJJKKKKKKK x8 */
	0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	/* JJJJJJJK x8 */
	0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
	/* JKKKKKKK x10, JK */
	0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e

	/* implicit CRC16 then EOP to end */
};

void musb_load_testpacket(struct musb *musb)
{
	void __iomem	*regs = musb->endpoints[0].regs;

	musb_ep_select(musb->mregs, 0);
	musb_write_fifo(musb->control_ep,
			sizeof(musb_test_packet), musb_test_packet);
	musb_writew(regs, MUSB_CSR0, MUSB_CSR0_TXPKTRDY);
}

/*-------------------------------------------------------------------------*/

/*
 * Handles OTG hnp timeouts, such as b_ase0_brst
 */
454
static void musb_otg_timer_func(struct timer_list *t)
F
Felipe Balbi 已提交
455
{
456
	struct musb	*musb = from_timer(musb, t, otg_timer);
F
Felipe Balbi 已提交
457 458 459
	unsigned long	flags;

	spin_lock_irqsave(&musb->lock, flags);
460
	switch (musb->xceiv->otg->state) {
F
Felipe Balbi 已提交
461
	case OTG_STATE_B_WAIT_ACON:
462 463
		musb_dbg(musb,
			"HNP: b_wait_acon timeout; back to b_peripheral");
F
Felipe Balbi 已提交
464
		musb_g_disconnect(musb);
465
		musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
F
Felipe Balbi 已提交
466 467
		musb->is_active = 0;
		break;
468
	case OTG_STATE_A_SUSPEND:
F
Felipe Balbi 已提交
469
	case OTG_STATE_A_WAIT_BCON:
470
		musb_dbg(musb, "HNP: %s timeout",
471
			usb_otg_state_string(musb->xceiv->otg->state));
472
		musb_platform_set_vbus(musb, 0);
473
		musb->xceiv->otg->state = OTG_STATE_A_WAIT_VFALL;
F
Felipe Balbi 已提交
474 475
		break;
	default:
476
		musb_dbg(musb, "HNP: Unhandled mode %s",
477
			usb_otg_state_string(musb->xceiv->otg->state));
F
Felipe Balbi 已提交
478 479 480 481 482
	}
	spin_unlock_irqrestore(&musb->lock, flags);
}

/*
D
David Brownell 已提交
483
 * Stops the HNP transition. Caller must take care of locking.
F
Felipe Balbi 已提交
484 485 486
 */
void musb_hnp_stop(struct musb *musb)
{
D
Daniel Mack 已提交
487
	struct usb_hcd	*hcd = musb->hcd;
F
Felipe Balbi 已提交
488 489 490
	void __iomem	*mbase = musb->mregs;
	u8	reg;

491
	musb_dbg(musb, "HNP: stop from %s",
492
			usb_otg_state_string(musb->xceiv->otg->state));
493

494
	switch (musb->xceiv->otg->state) {
F
Felipe Balbi 已提交
495 496
	case OTG_STATE_A_PERIPHERAL:
		musb_g_disconnect(musb);
497
		musb_dbg(musb, "HNP: back to %s",
498
			usb_otg_state_string(musb->xceiv->otg->state));
F
Felipe Balbi 已提交
499 500
		break;
	case OTG_STATE_B_HOST:
501
		musb_dbg(musb, "HNP: Disabling HR");
502 503
		if (hcd)
			hcd->self.is_b_host = 0;
504
		musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
F
Felipe Balbi 已提交
505 506 507 508 509 510 511
		MUSB_DEV_MODE(musb);
		reg = musb_readb(mbase, MUSB_POWER);
		reg |= MUSB_POWER_SUSPENDM;
		musb_writeb(mbase, MUSB_POWER, reg);
		/* REVISIT: Start SESSION_REQUEST here? */
		break;
	default:
512
		musb_dbg(musb, "HNP: Stopping in unknown state %s",
513
			usb_otg_state_string(musb->xceiv->otg->state));
F
Felipe Balbi 已提交
514 515 516 517 518 519 520
	}

	/*
	 * When returning to A state after HNP, avoid hub_port_rebounce(),
	 * which cause occasional OPT A "Did not receive reset after connect"
	 * errors.
	 */
521
	musb->port1_status &= ~(USB_PORT_STAT_C_CONNECTION << 16);
F
Felipe Balbi 已提交
522 523
}

524
static void musb_recover_from_babble(struct musb *musb);
525

F
Felipe Balbi 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538
/*
 * Interrupt Service Routine to record USB "global" interrupts.
 * Since these do not happen often and signify things of
 * paramount importance, it seems OK to check them individually;
 * the order of the tests is specified in the manual
 *
 * @param musb instance pointer
 * @param int_usb register contents
 * @param devctl
 * @param power
 */

static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
539
				u8 devctl)
F
Felipe Balbi 已提交
540 541 542
{
	irqreturn_t handled = IRQ_NONE;

543
	musb_dbg(musb, "<== DevCtl=%02x, int_usb=0x%x", devctl, int_usb);
F
Felipe Balbi 已提交
544 545 546 547 548 549 550

	/* in host mode, the peripheral may issue remote wakeup.
	 * in peripheral mode, the host may resume the link.
	 * spurious RESUME irqs happen too, paired with SUSPEND.
	 */
	if (int_usb & MUSB_INTR_RESUME) {
		handled = IRQ_HANDLED;
551
		musb_dbg(musb, "RESUME (%s)",
F
Felipe Balbi 已提交
552
				usb_otg_state_string(musb->xceiv->otg->state));
F
Felipe Balbi 已提交
553 554

		if (devctl & MUSB_DEVCTL_HM) {
555
			switch (musb->xceiv->otg->state) {
F
Felipe Balbi 已提交
556
			case OTG_STATE_A_SUSPEND:
557
				/* remote wakeup? */
F
Felipe Balbi 已提交
558 559 560
				musb->port1_status |=
						(USB_PORT_STAT_C_SUSPEND << 16)
						| MUSB_PORT_STAT_RESUME;
561
				musb->rh_timer = jiffies
562
					+ msecs_to_jiffies(USB_RESUME_TIMEOUT);
563
				musb->xceiv->otg->state = OTG_STATE_A_HOST;
F
Felipe Balbi 已提交
564
				musb->is_active = 1;
565
				musb_host_resume_root_hub(musb);
566 567
				schedule_delayed_work(&musb->finish_resume_work,
					msecs_to_jiffies(USB_RESUME_TIMEOUT));
F
Felipe Balbi 已提交
568 569
				break;
			case OTG_STATE_B_WAIT_ACON:
570
				musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
F
Felipe Balbi 已提交
571 572 573 574 575 576
				musb->is_active = 1;
				MUSB_DEV_MODE(musb);
				break;
			default:
				WARNING("bogus %s RESUME (%s)\n",
					"host",
577
					usb_otg_state_string(musb->xceiv->otg->state));
F
Felipe Balbi 已提交
578 579
			}
		} else {
580
			switch (musb->xceiv->otg->state) {
F
Felipe Balbi 已提交
581 582
			case OTG_STATE_A_SUSPEND:
				/* possibly DISCONNECT is upcoming */
583
				musb->xceiv->otg->state = OTG_STATE_A_HOST;
584
				musb_host_resume_root_hub(musb);
F
Felipe Balbi 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
				break;
			case OTG_STATE_B_WAIT_ACON:
			case OTG_STATE_B_PERIPHERAL:
				/* disconnect while suspended?  we may
				 * not get a disconnect irq...
				 */
				if ((devctl & MUSB_DEVCTL_VBUS)
						!= (3 << MUSB_DEVCTL_VBUS_SHIFT)
						) {
					musb->int_usb |= MUSB_INTR_DISCONNECT;
					musb->int_usb &= ~MUSB_INTR_SUSPEND;
					break;
				}
				musb_g_resume(musb);
				break;
			case OTG_STATE_B_IDLE:
				musb->int_usb &= ~MUSB_INTR_SUSPEND;
				break;
			default:
				WARNING("bogus %s RESUME (%s)\n",
					"peripheral",
606
					usb_otg_state_string(musb->xceiv->otg->state));
F
Felipe Balbi 已提交
607 608 609 610 611 612
			}
		}
	}

	/* see manual for the order of the tests */
	if (int_usb & MUSB_INTR_SESSREQ) {
613 614
		void __iomem *mbase = musb->mregs;

615 616
		if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS
				&& (devctl & MUSB_DEVCTL_BDEVICE)) {
617
			musb_dbg(musb, "SessReq while on B state");
618 619 620
			return IRQ_HANDLED;
		}

621
		musb_dbg(musb, "SESSION_REQUEST (%s)",
622
			usb_otg_state_string(musb->xceiv->otg->state));
F
Felipe Balbi 已提交
623 624 625 626 627 628 629 630 631 632

		/* IRQ arrives from ID pin sense or (later, if VBUS power
		 * is removed) SRP.  responses are time critical:
		 *  - turn on VBUS (with silicon-specific mechanism)
		 *  - go through A_WAIT_VRISE
		 *  - ... to A_WAIT_BCON.
		 * a_wait_vrise_tmout triggers VBUS_ERROR transitions
		 */
		musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
		musb->ep0_stage = MUSB_EP0_START;
633
		musb->xceiv->otg->state = OTG_STATE_A_IDLE;
F
Felipe Balbi 已提交
634
		MUSB_HST_MODE(musb);
635
		musb_platform_set_vbus(musb, 1);
F
Felipe Balbi 已提交
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658

		handled = IRQ_HANDLED;
	}

	if (int_usb & MUSB_INTR_VBUSERROR) {
		int	ignore = 0;

		/* During connection as an A-Device, we may see a short
		 * current spikes causing voltage drop, because of cable
		 * and peripheral capacitance combined with vbus draw.
		 * (So: less common with truly self-powered devices, where
		 * vbus doesn't act like a power supply.)
		 *
		 * Such spikes are short; usually less than ~500 usec, max
		 * of ~2 msec.  That is, they're not sustained overcurrent
		 * errors, though they're reported using VBUSERROR irqs.
		 *
		 * Workarounds:  (a) hardware: use self powered devices.
		 * (b) software:  ignore non-repeated VBUS errors.
		 *
		 * REVISIT:  do delays from lots of DEBUG_KERNEL checks
		 * make trouble here, keeping VBUS < 4.4V ?
		 */
659
		switch (musb->xceiv->otg->state) {
F
Felipe Balbi 已提交
660 661 662 663 664 665 666 667 668 669
		case OTG_STATE_A_HOST:
			/* recovery is dicey once we've gotten past the
			 * initial stages of enumeration, but if VBUS
			 * stayed ok at the other end of the link, and
			 * another reset is due (at least for high speed,
			 * to redo the chirp etc), it might work OK...
			 */
		case OTG_STATE_A_WAIT_BCON:
		case OTG_STATE_A_WAIT_VRISE:
			if (musb->vbuserr_retry) {
670 671
				void __iomem *mbase = musb->mregs;

F
Felipe Balbi 已提交
672 673 674 675 676 677
				musb->vbuserr_retry--;
				ignore = 1;
				devctl |= MUSB_DEVCTL_SESSION;
				musb_writeb(mbase, MUSB_DEVCTL, devctl);
			} else {
				musb->port1_status |=
678 679
					  USB_PORT_STAT_OVERCURRENT
					| (USB_PORT_STAT_C_OVERCURRENT << 16);
F
Felipe Balbi 已提交
680 681 682 683 684 685
			}
			break;
		default:
			break;
		}

686 687
		dev_printk(ignore ? KERN_DEBUG : KERN_ERR, musb->controller,
				"VBUS_ERROR in %s (%02x, %s), retry #%d, port1 %08x\n",
688
				usb_otg_state_string(musb->xceiv->otg->state),
F
Felipe Balbi 已提交
689 690 691 692 693 694 695 696 697 698 699 700
				devctl,
				({ char *s;
				switch (devctl & MUSB_DEVCTL_VBUS) {
				case 0 << MUSB_DEVCTL_VBUS_SHIFT:
					s = "<SessEnd"; break;
				case 1 << MUSB_DEVCTL_VBUS_SHIFT:
					s = "<AValid"; break;
				case 2 << MUSB_DEVCTL_VBUS_SHIFT:
					s = "<VBusValid"; break;
				/* case 3 << MUSB_DEVCTL_VBUS_SHIFT: */
				default:
					s = "VALID"; break;
J
Joe Perches 已提交
701
				} s; }),
F
Felipe Balbi 已提交
702 703 704 705 706
				VBUSERR_RETRY_COUNT - musb->vbuserr_retry,
				musb->port1_status);

		/* go through A_WAIT_VFALL then start a new session */
		if (!ignore)
707
			musb_platform_set_vbus(musb, 0);
F
Felipe Balbi 已提交
708 709 710
		handled = IRQ_HANDLED;
	}

711
	if (int_usb & MUSB_INTR_SUSPEND) {
712
		musb_dbg(musb, "SUSPEND (%s) devctl %02x",
713
			usb_otg_state_string(musb->xceiv->otg->state), devctl);
714 715
		handled = IRQ_HANDLED;

716
		switch (musb->xceiv->otg->state) {
717 718 719 720 721 722 723 724 725
		case OTG_STATE_A_PERIPHERAL:
			/* We also come here if the cable is removed, since
			 * this silicon doesn't report ID-no-longer-grounded.
			 *
			 * We depend on T(a_wait_bcon) to shut us down, and
			 * hope users don't do anything dicey during this
			 * undesired detour through A_WAIT_BCON.
			 */
			musb_hnp_stop(musb);
726
			musb_host_resume_root_hub(musb);
727 728 729 730 731 732 733 734 735
			musb_root_disconnect(musb);
			musb_platform_try_idle(musb, jiffies
					+ msecs_to_jiffies(musb->a_wait_bcon
						? : OTG_TIME_A_WAIT_BCON));

			break;
		case OTG_STATE_B_IDLE:
			if (!musb->is_active)
				break;
736
			/* fall through */
737 738
		case OTG_STATE_B_PERIPHERAL:
			musb_g_suspend(musb);
739
			musb->is_active = musb->g.b_hnp_enable;
740
			if (musb->is_active) {
741
				musb->xceiv->otg->state = OTG_STATE_B_WAIT_ACON;
742
				musb_dbg(musb, "HNP: Setting timer for b_ase0_brst");
743 744 745 746 747 748 749 750 751 752 753
				mod_timer(&musb->otg_timer, jiffies
					+ msecs_to_jiffies(
							OTG_TIME_B_ASE0_BRST));
			}
			break;
		case OTG_STATE_A_WAIT_BCON:
			if (musb->a_wait_bcon != 0)
				musb_platform_try_idle(musb, jiffies
					+ msecs_to_jiffies(musb->a_wait_bcon));
			break;
		case OTG_STATE_A_HOST:
754
			musb->xceiv->otg->state = OTG_STATE_A_SUSPEND;
755
			musb->is_active = musb->hcd->self.b_hnp_enable;
756 757 758
			break;
		case OTG_STATE_B_HOST:
			/* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
759
			musb_dbg(musb, "REVISIT: SUSPEND as B_HOST");
760 761 762 763 764 765 766 767
			break;
		default:
			/* "should not happen" */
			musb->is_active = 0;
			break;
		}
	}

F
Felipe Balbi 已提交
768
	if (int_usb & MUSB_INTR_CONNECT) {
D
Daniel Mack 已提交
769
		struct usb_hcd *hcd = musb->hcd;
F
Felipe Balbi 已提交
770 771 772 773 774 775

		handled = IRQ_HANDLED;
		musb->is_active = 1;

		musb->ep0_stage = MUSB_EP0_START;

776 777
		musb->intrtxe = musb->epmask;
		musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
778 779
		musb->intrrxe = musb->epmask & 0xfffe;
		musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
780
		musb_writeb(musb->mregs, MUSB_INTRUSBE, 0xf7);
F
Felipe Balbi 已提交
781 782 783 784 785 786 787 788 789 790 791 792
		musb->port1_status &= ~(USB_PORT_STAT_LOW_SPEED
					|USB_PORT_STAT_HIGH_SPEED
					|USB_PORT_STAT_ENABLE
					);
		musb->port1_status |= USB_PORT_STAT_CONNECTION
					|(USB_PORT_STAT_C_CONNECTION << 16);

		/* high vs full speed is just a guess until after reset */
		if (devctl & MUSB_DEVCTL_LSDEV)
			musb->port1_status |= USB_PORT_STAT_LOW_SPEED;

		/* indicate new connection to OTG machine */
793
		switch (musb->xceiv->otg->state) {
F
Felipe Balbi 已提交
794 795
		case OTG_STATE_B_PERIPHERAL:
			if (int_usb & MUSB_INTR_SUSPEND) {
796
				musb_dbg(musb, "HNP: SUSPEND+CONNECT, now b_host");
F
Felipe Balbi 已提交
797
				int_usb &= ~MUSB_INTR_SUSPEND;
798
				goto b_host;
F
Felipe Balbi 已提交
799
			} else
800
				musb_dbg(musb, "CONNECT as b_peripheral???");
F
Felipe Balbi 已提交
801 802
			break;
		case OTG_STATE_B_WAIT_ACON:
803
			musb_dbg(musb, "HNP: CONNECT, now b_host");
804
b_host:
805
			musb->xceiv->otg->state = OTG_STATE_B_HOST;
806 807
			if (musb->hcd)
				musb->hcd->self.is_b_host = 1;
808
			del_timer(&musb->otg_timer);
F
Felipe Balbi 已提交
809 810 811 812
			break;
		default:
			if ((devctl & MUSB_DEVCTL_VBUS)
					== (3 << MUSB_DEVCTL_VBUS_SHIFT)) {
813
				musb->xceiv->otg->state = OTG_STATE_A_HOST;
814 815
				if (hcd)
					hcd->self.is_b_host = 0;
F
Felipe Balbi 已提交
816 817 818
			}
			break;
		}
819

820
		musb_host_poke_root_hub(musb);
821

822
		musb_dbg(musb, "CONNECT (%s) devctl %02x",
823
				usb_otg_state_string(musb->xceiv->otg->state), devctl);
F
Felipe Balbi 已提交
824 825
	}

826
	if (int_usb & MUSB_INTR_DISCONNECT) {
827
		musb_dbg(musb, "DISCONNECT (%s) as %s, devctl %02x",
828
				usb_otg_state_string(musb->xceiv->otg->state),
829 830 831
				MUSB_MODE(musb), devctl);
		handled = IRQ_HANDLED;

832
		switch (musb->xceiv->otg->state) {
833 834
		case OTG_STATE_A_HOST:
		case OTG_STATE_A_SUSPEND:
835
			musb_host_resume_root_hub(musb);
836
			musb_root_disconnect(musb);
837
			if (musb->a_wait_bcon != 0)
838 839 840 841 842 843 844 845 846 847
				musb_platform_try_idle(musb, jiffies
					+ msecs_to_jiffies(musb->a_wait_bcon));
			break;
		case OTG_STATE_B_HOST:
			/* REVISIT this behaves for "real disconnect"
			 * cases; make sure the other transitions from
			 * from B_HOST act right too.  The B_HOST code
			 * in hnp_stop() is currently not used...
			 */
			musb_root_disconnect(musb);
848 849
			if (musb->hcd)
				musb->hcd->self.is_b_host = 0;
850
			musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
			MUSB_DEV_MODE(musb);
			musb_g_disconnect(musb);
			break;
		case OTG_STATE_A_PERIPHERAL:
			musb_hnp_stop(musb);
			musb_root_disconnect(musb);
			/* FALLTHROUGH */
		case OTG_STATE_B_WAIT_ACON:
			/* FALLTHROUGH */
		case OTG_STATE_B_PERIPHERAL:
		case OTG_STATE_B_IDLE:
			musb_g_disconnect(musb);
			break;
		default:
			WARNING("unhandled DISCONNECT transition (%s)\n",
866
				usb_otg_state_string(musb->xceiv->otg->state));
867 868 869 870
			break;
		}
	}

F
Felipe Balbi 已提交
871 872 873 874
	/* mentor saves a bit: bus reset and babble share the same irq.
	 * only host sees babble; only peripheral sees bus reset.
	 */
	if (int_usb & MUSB_INTR_RESET) {
875
		handled = IRQ_HANDLED;
876
		if (is_host_active(musb)) {
F
Felipe Balbi 已提交
877
			/*
878
			 * When BABBLE happens what we can depends on which
879 880 881
			 * platform MUSB is running, because some platforms
			 * implemented proprietary means for 'recovering' from
			 * Babble conditions. One such platform is AM335x. In
882 883
			 * most cases, however, the only thing we can do is
			 * drop the session.
F
Felipe Balbi 已提交
884
			 */
885
			dev_err(musb->controller, "Babble\n");
886
			musb_recover_from_babble(musb);
887
		} else {
888
			musb_dbg(musb, "BUS RESET as %s",
889 890
				usb_otg_state_string(musb->xceiv->otg->state));
			switch (musb->xceiv->otg->state) {
F
Felipe Balbi 已提交
891 892 893 894
			case OTG_STATE_A_SUSPEND:
				musb_g_reset(musb);
				/* FALLTHROUGH */
			case OTG_STATE_A_WAIT_BCON:	/* OPT TD.4.7-900ms */
D
David Brownell 已提交
895
				/* never use invalid T(a_wait_bcon) */
896
				musb_dbg(musb, "HNP: in %s, %d msec timeout",
897
					usb_otg_state_string(musb->xceiv->otg->state),
898
					TA_WAIT_BCON(musb));
D
David Brownell 已提交
899 900
				mod_timer(&musb->otg_timer, jiffies
					+ msecs_to_jiffies(TA_WAIT_BCON(musb)));
F
Felipe Balbi 已提交
901 902
				break;
			case OTG_STATE_A_PERIPHERAL:
903 904
				del_timer(&musb->otg_timer);
				musb_g_reset(musb);
F
Felipe Balbi 已提交
905 906
				break;
			case OTG_STATE_B_WAIT_ACON:
907
				musb_dbg(musb, "HNP: RESET (%s), to b_peripheral",
908 909
					usb_otg_state_string(musb->xceiv->otg->state));
				musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
F
Felipe Balbi 已提交
910 911 912
				musb_g_reset(musb);
				break;
			case OTG_STATE_B_IDLE:
913
				musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
F
Felipe Balbi 已提交
914 915 916 917 918
				/* FALLTHROUGH */
			case OTG_STATE_B_PERIPHERAL:
				musb_g_reset(musb);
				break;
			default:
919
				musb_dbg(musb, "Unhandled BUS RESET as %s",
920
					usb_otg_state_string(musb->xceiv->otg->state));
F
Felipe Balbi 已提交
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
			}
		}
	}

#if 0
/* REVISIT ... this would be for multiplexing periodic endpoints, or
 * supporting transfer phasing to prevent exceeding ISO bandwidth
 * limits of a given frame or microframe.
 *
 * It's not needed for peripheral side, which dedicates endpoints;
 * though it _might_ use SOF irqs for other purposes.
 *
 * And it's not currently needed for host side, which also dedicates
 * endpoints, relies on TX/RX interval registers, and isn't claimed
 * to support ISO transfers yet.
 */
	if (int_usb & MUSB_INTR_SOF) {
		void __iomem *mbase = musb->mregs;
		struct musb_hw_ep	*ep;
		u8 epnum;
		u16 frame;

943
		dev_dbg(musb->controller, "START_OF_FRAME\n");
F
Felipe Balbi 已提交
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
		handled = IRQ_HANDLED;

		/* start any periodic Tx transfers waiting for current frame */
		frame = musb_readw(mbase, MUSB_FRAME);
		ep = musb->endpoints;
		for (epnum = 1; (epnum < musb->nr_endpoints)
					&& (musb->epmask >= (1 << epnum));
				epnum++, ep++) {
			/*
			 * FIXME handle framecounter wraps (12 bits)
			 * eliminate duplicated StartUrb logic
			 */
			if (ep->dwWaitFrame >= frame) {
				ep->dwWaitFrame = 0;
				pr_debug("SOF --> periodic TX%s on %d\n",
					ep->tx_channel ? " DMA" : "",
					epnum);
				if (!ep->tx_channel)
					musb_h_tx_start(musb, epnum);
				else
					cppi_hostdma_start(musb, epnum);
			}
		}		/* end of for loop */
	}
#endif

970
	schedule_delayed_work(&musb->irq_work, 0);
F
Felipe Balbi 已提交
971 972 973 974 975 976

	return handled;
}

/*-------------------------------------------------------------------------*/

977
static void musb_disable_interrupts(struct musb *musb)
F
Felipe Balbi 已提交
978 979 980 981 982 983
{
	void __iomem	*mbase = musb->mregs;
	u16	temp;

	/* disable interrupts */
	musb_writeb(mbase, MUSB_INTRUSBE, 0);
984
	musb->intrtxe = 0;
F
Felipe Balbi 已提交
985
	musb_writew(mbase, MUSB_INTRTXE, 0);
986
	musb->intrrxe = 0;
F
Felipe Balbi 已提交
987 988 989 990 991 992
	musb_writew(mbase, MUSB_INTRRXE, 0);

	/*  flush pending interrupts */
	temp = musb_readb(mbase, MUSB_INTRUSB);
	temp = musb_readw(mbase, MUSB_INTRTX);
	temp = musb_readw(mbase, MUSB_INTRRX);
993 994 995 996 997 998 999 1000 1001 1002 1003 1004
}

static void musb_enable_interrupts(struct musb *musb)
{
	void __iomem    *regs = musb->mregs;

	/*  Set INT enable registers, enable interrupts */
	musb->intrtxe = musb->epmask;
	musb_writew(regs, MUSB_INTRTXE, musb->intrtxe);
	musb->intrrxe = musb->epmask & 0xfffe;
	musb_writew(regs, MUSB_INTRRXE, musb->intrrxe);
	musb_writeb(regs, MUSB_INTRUSBE, 0xf7);
F
Felipe Balbi 已提交
1005 1006 1007

}

1008 1009 1010 1011 1012 1013 1014
/*
 * Program the HDRC to start (enable interrupts, dma, etc.).
 */
void musb_start(struct musb *musb)
{
	void __iomem    *regs = musb->mregs;
	u8              devctl = musb_readb(regs, MUSB_DEVCTL);
1015
	u8		power;
1016

1017
	musb_dbg(musb, "<== devctl %02x", devctl);
1018

1019
	musb_enable_interrupts(musb);
1020 1021
	musb_writeb(regs, MUSB_TESTMODE, 0);

1022 1023 1024 1025 1026 1027 1028 1029 1030
	power = MUSB_POWER_ISOUPDATE;
	/*
	 * treating UNKNOWN as unspecified maximum speed, in which case
	 * we will default to high-speed.
	 */
	if (musb->config->maximum_speed == USB_SPEED_HIGH ||
			musb->config->maximum_speed == USB_SPEED_UNKNOWN)
		power |= MUSB_POWER_HSENAB;
	musb_writeb(regs, MUSB_POWER, power);
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041

	musb->is_active = 0;
	devctl = musb_readb(regs, MUSB_DEVCTL);
	devctl &= ~MUSB_DEVCTL_SESSION;

	/* session started after:
	 * (a) ID-grounded irq, host mode;
	 * (b) vbus present/connect IRQ, peripheral mode;
	 * (c) peripheral initiates, using SRP
	 */
	if (musb->port_mode != MUSB_PORT_MODE_HOST &&
1042
			musb->xceiv->otg->state != OTG_STATE_A_WAIT_BCON &&
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
			(devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS) {
		musb->is_active = 1;
	} else {
		devctl |= MUSB_DEVCTL_SESSION;
	}

	musb_platform_enable(musb);
	musb_writeb(regs, MUSB_DEVCTL, devctl);
}

F
Felipe Balbi 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
/*
 * Make the HDRC stop (disable interrupts, etc.);
 * reversible by musb_start
 * called on gadget driver unregister
 * with controller locked, irqs blocked
 * acts as a NOP unless some role activated the hardware
 */
void musb_stop(struct musb *musb)
{
	/* stop IRQs, timers, ... */
	musb_platform_disable(musb);
1064 1065
	musb_disable_interrupts(musb);
	musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
F
Felipe Balbi 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081

	/* FIXME
	 *  - mark host and/or peripheral drivers unusable/inactive
	 *  - disable DMA (and enable it in HdrcStart)
	 *  - make sure we can musb_start() after musb_stop(); with
	 *    OTG mode, gadget driver module rmmod/modprobe cycles that
	 *  - ...
	 */
	musb_platform_try_idle(musb, 0);
}

/*-------------------------------------------------------------------------*/

/*
 * The silicon either has hard-wired endpoint configurations, or else
 * "dynamic fifo" sizing.  The driver has support for both, though at this
D
David Brownell 已提交
1082 1083 1084
 * writing only the dynamic sizing is very well tested.   Since we switched
 * away from compile-time hardware parameters, we can no longer rely on
 * dead code elimination to leave only the relevant one in the object file.
F
Felipe Balbi 已提交
1085 1086 1087 1088
 *
 * We don't currently use dynamic fifo setup capability to do anything
 * more than selecting one of a bunch of predefined configurations.
 */
1089
static ushort fifo_mode;
F
Felipe Balbi 已提交
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100

/* "modprobe ... fifo_mode=1" etc */
module_param(fifo_mode, ushort, 0);
MODULE_PARM_DESC(fifo_mode, "initial endpoint configuration");

/*
 * tables defining fifo_mode values.  define more if you like.
 * for host side, make sure both halves of ep1 are set up.
 */

/* mode 0 - fits in 2KB */
B
Bill Pemberton 已提交
1101
static struct musb_fifo_cfg mode_0_cfg[] = {
F
Felipe Balbi 已提交
1102 1103 1104 1105 1106 1107 1108 1109
{ .hw_ep_num = 1, .style = FIFO_TX,   .maxpacket = 512, },
{ .hw_ep_num = 1, .style = FIFO_RX,   .maxpacket = 512, },
{ .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, },
{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
};

/* mode 1 - fits in 4KB */
B
Bill Pemberton 已提交
1110
static struct musb_fifo_cfg mode_1_cfg[] = {
F
Felipe Balbi 已提交
1111 1112 1113 1114 1115 1116 1117 1118
{ .hw_ep_num = 1, .style = FIFO_TX,   .maxpacket = 512, .mode = BUF_DOUBLE, },
{ .hw_ep_num = 1, .style = FIFO_RX,   .maxpacket = 512, .mode = BUF_DOUBLE, },
{ .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, .mode = BUF_DOUBLE, },
{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
};

/* mode 2 - fits in 4KB */
B
Bill Pemberton 已提交
1119
static struct musb_fifo_cfg mode_2_cfg[] = {
F
Felipe Balbi 已提交
1120 1121 1122 1123
{ .hw_ep_num = 1, .style = FIFO_TX,   .maxpacket = 512, },
{ .hw_ep_num = 1, .style = FIFO_RX,   .maxpacket = 512, },
{ .hw_ep_num = 2, .style = FIFO_TX,   .maxpacket = 512, },
{ .hw_ep_num = 2, .style = FIFO_RX,   .maxpacket = 512, },
1124 1125
{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 960, },
{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 1024, },
F
Felipe Balbi 已提交
1126 1127 1128
};

/* mode 3 - fits in 4KB */
B
Bill Pemberton 已提交
1129
static struct musb_fifo_cfg mode_3_cfg[] = {
F
Felipe Balbi 已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138
{ .hw_ep_num = 1, .style = FIFO_TX,   .maxpacket = 512, .mode = BUF_DOUBLE, },
{ .hw_ep_num = 1, .style = FIFO_RX,   .maxpacket = 512, .mode = BUF_DOUBLE, },
{ .hw_ep_num = 2, .style = FIFO_TX,   .maxpacket = 512, },
{ .hw_ep_num = 2, .style = FIFO_RX,   .maxpacket = 512, },
{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
};

/* mode 4 - fits in 16KB */
B
Bill Pemberton 已提交
1139
static struct musb_fifo_cfg mode_4_cfg[] = {
F
Felipe Balbi 已提交
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
{ .hw_ep_num =  1, .style = FIFO_TX,   .maxpacket = 512, },
{ .hw_ep_num =  1, .style = FIFO_RX,   .maxpacket = 512, },
{ .hw_ep_num =  2, .style = FIFO_TX,   .maxpacket = 512, },
{ .hw_ep_num =  2, .style = FIFO_RX,   .maxpacket = 512, },
{ .hw_ep_num =  3, .style = FIFO_TX,   .maxpacket = 512, },
{ .hw_ep_num =  3, .style = FIFO_RX,   .maxpacket = 512, },
{ .hw_ep_num =  4, .style = FIFO_TX,   .maxpacket = 512, },
{ .hw_ep_num =  4, .style = FIFO_RX,   .maxpacket = 512, },
{ .hw_ep_num =  5, .style = FIFO_TX,   .maxpacket = 512, },
{ .hw_ep_num =  5, .style = FIFO_RX,   .maxpacket = 512, },
{ .hw_ep_num =  6, .style = FIFO_TX,   .maxpacket = 512, },
{ .hw_ep_num =  6, .style = FIFO_RX,   .maxpacket = 512, },
{ .hw_ep_num =  7, .style = FIFO_TX,   .maxpacket = 512, },
{ .hw_ep_num =  7, .style = FIFO_RX,   .maxpacket = 512, },
{ .hw_ep_num =  8, .style = FIFO_TX,   .maxpacket = 512, },
{ .hw_ep_num =  8, .style = FIFO_RX,   .maxpacket = 512, },
{ .hw_ep_num =  9, .style = FIFO_TX,   .maxpacket = 512, },
{ .hw_ep_num =  9, .style = FIFO_RX,   .maxpacket = 512, },
1158 1159 1160 1161 1162 1163 1164
{ .hw_ep_num = 10, .style = FIFO_TX,   .maxpacket = 256, },
{ .hw_ep_num = 10, .style = FIFO_RX,   .maxpacket = 64, },
{ .hw_ep_num = 11, .style = FIFO_TX,   .maxpacket = 256, },
{ .hw_ep_num = 11, .style = FIFO_RX,   .maxpacket = 64, },
{ .hw_ep_num = 12, .style = FIFO_TX,   .maxpacket = 256, },
{ .hw_ep_num = 12, .style = FIFO_RX,   .maxpacket = 64, },
{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 4096, },
F
Felipe Balbi 已提交
1165 1166 1167 1168
{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
};

1169
/* mode 5 - fits in 8KB */
B
Bill Pemberton 已提交
1170
static struct musb_fifo_cfg mode_5_cfg[] = {
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
{ .hw_ep_num =  1, .style = FIFO_TX,   .maxpacket = 512, },
{ .hw_ep_num =  1, .style = FIFO_RX,   .maxpacket = 512, },
{ .hw_ep_num =  2, .style = FIFO_TX,   .maxpacket = 512, },
{ .hw_ep_num =  2, .style = FIFO_RX,   .maxpacket = 512, },
{ .hw_ep_num =  3, .style = FIFO_TX,   .maxpacket = 512, },
{ .hw_ep_num =  3, .style = FIFO_RX,   .maxpacket = 512, },
{ .hw_ep_num =  4, .style = FIFO_TX,   .maxpacket = 512, },
{ .hw_ep_num =  4, .style = FIFO_RX,   .maxpacket = 512, },
{ .hw_ep_num =  5, .style = FIFO_TX,   .maxpacket = 512, },
{ .hw_ep_num =  5, .style = FIFO_RX,   .maxpacket = 512, },
{ .hw_ep_num =  6, .style = FIFO_TX,   .maxpacket = 32, },
{ .hw_ep_num =  6, .style = FIFO_RX,   .maxpacket = 32, },
{ .hw_ep_num =  7, .style = FIFO_TX,   .maxpacket = 32, },
{ .hw_ep_num =  7, .style = FIFO_RX,   .maxpacket = 32, },
{ .hw_ep_num =  8, .style = FIFO_TX,   .maxpacket = 32, },
{ .hw_ep_num =  8, .style = FIFO_RX,   .maxpacket = 32, },
{ .hw_ep_num =  9, .style = FIFO_TX,   .maxpacket = 32, },
{ .hw_ep_num =  9, .style = FIFO_RX,   .maxpacket = 32, },
{ .hw_ep_num = 10, .style = FIFO_TX,   .maxpacket = 32, },
{ .hw_ep_num = 10, .style = FIFO_RX,   .maxpacket = 32, },
{ .hw_ep_num = 11, .style = FIFO_TX,   .maxpacket = 32, },
{ .hw_ep_num = 11, .style = FIFO_RX,   .maxpacket = 32, },
{ .hw_ep_num = 12, .style = FIFO_TX,   .maxpacket = 32, },
{ .hw_ep_num = 12, .style = FIFO_RX,   .maxpacket = 32, },
{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 512, },
{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
};
F
Felipe Balbi 已提交
1199 1200 1201 1202 1203 1204 1205

/*
 * configure a fifo; for non-shared endpoints, this may be called
 * once for a tx fifo and once for an rx fifo.
 *
 * returns negative errno or offset for next fifo.
 */
B
Bill Pemberton 已提交
1206
static int
F
Felipe Balbi 已提交
1207
fifo_setup(struct musb *musb, struct musb_hw_ep  *hw_ep,
1208
		const struct musb_fifo_cfg *cfg, u16 offset)
F
Felipe Balbi 已提交
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
{
	void __iomem	*mbase = musb->mregs;
	int	size = 0;
	u16	maxpacket = cfg->maxpacket;
	u16	c_off = offset >> 3;
	u8	c_size;

	/* expect hw_ep has already been zero-initialized */

	size = ffs(max(maxpacket, (u16) 8)) - 1;
	maxpacket = 1 << size;

	c_size = size - 3;
	if (cfg->mode == BUF_DOUBLE) {
1223 1224
		if ((offset + (maxpacket << 1)) >
				(1 << (musb->config->ram_bits + 2)))
F
Felipe Balbi 已提交
1225 1226 1227
			return -EMSGSIZE;
		c_size |= MUSB_FIFOSZ_DPB;
	} else {
1228
		if ((offset + maxpacket) > (1 << (musb->config->ram_bits + 2)))
F
Felipe Balbi 已提交
1229 1230 1231 1232 1233 1234 1235
			return -EMSGSIZE;
	}

	/* configure the FIFO */
	musb_writeb(mbase, MUSB_INDEX, hw_ep->epnum);

	/* EP0 reserved endpoint for control, bidirectional;
1236
	 * EP1 reserved for bulk, two unidirectional halves.
F
Felipe Balbi 已提交
1237 1238 1239 1240 1241 1242
	 */
	if (hw_ep->epnum == 1)
		musb->bulk_ep = hw_ep;
	/* REVISIT error check:  be sure ep0 can both rx and tx ... */
	switch (cfg->style) {
	case FIFO_TX:
1243 1244
		musb_write_txfifosz(mbase, c_size);
		musb_write_txfifoadd(mbase, c_off);
F
Felipe Balbi 已提交
1245 1246 1247 1248
		hw_ep->tx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
		hw_ep->max_packet_sz_tx = maxpacket;
		break;
	case FIFO_RX:
1249 1250
		musb_write_rxfifosz(mbase, c_size);
		musb_write_rxfifoadd(mbase, c_off);
F
Felipe Balbi 已提交
1251 1252 1253 1254
		hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
		hw_ep->max_packet_sz_rx = maxpacket;
		break;
	case FIFO_RXTX:
1255 1256
		musb_write_txfifosz(mbase, c_size);
		musb_write_txfifoadd(mbase, c_off);
F
Felipe Balbi 已提交
1257 1258 1259
		hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
		hw_ep->max_packet_sz_rx = maxpacket;

1260 1261
		musb_write_rxfifosz(mbase, c_size);
		musb_write_rxfifoadd(mbase, c_off);
F
Felipe Balbi 已提交
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
		hw_ep->tx_double_buffered = hw_ep->rx_double_buffered;
		hw_ep->max_packet_sz_tx = maxpacket;

		hw_ep->is_shared_fifo = true;
		break;
	}

	/* NOTE rx and tx endpoint irqs aren't managed separately,
	 * which happens to be ok
	 */
	musb->epmask |= (1 << hw_ep->epnum);

	return offset + (maxpacket << ((c_size & MUSB_FIFOSZ_DPB) ? 1 : 0));
}

B
Bill Pemberton 已提交
1277
static struct musb_fifo_cfg ep0_cfg = {
F
Felipe Balbi 已提交
1278 1279 1280
	.style = FIFO_RXTX, .maxpacket = 64,
};

B
Bill Pemberton 已提交
1281
static int ep_config_from_table(struct musb *musb)
F
Felipe Balbi 已提交
1282
{
1283
	const struct musb_fifo_cfg	*cfg;
F
Felipe Balbi 已提交
1284 1285 1286 1287
	unsigned		i, n;
	int			offset;
	struct musb_hw_ep	*hw_ep = musb->endpoints;

1288 1289 1290 1291 1292 1293
	if (musb->config->fifo_cfg) {
		cfg = musb->config->fifo_cfg;
		n = musb->config->fifo_cfg_size;
		goto done;
	}

F
Felipe Balbi 已提交
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
	switch (fifo_mode) {
	default:
		fifo_mode = 0;
		/* FALLTHROUGH */
	case 0:
		cfg = mode_0_cfg;
		n = ARRAY_SIZE(mode_0_cfg);
		break;
	case 1:
		cfg = mode_1_cfg;
		n = ARRAY_SIZE(mode_1_cfg);
		break;
	case 2:
		cfg = mode_2_cfg;
		n = ARRAY_SIZE(mode_2_cfg);
		break;
	case 3:
		cfg = mode_3_cfg;
		n = ARRAY_SIZE(mode_3_cfg);
		break;
	case 4:
		cfg = mode_4_cfg;
		n = ARRAY_SIZE(mode_4_cfg);
		break;
1318 1319 1320 1321
	case 5:
		cfg = mode_5_cfg;
		n = ARRAY_SIZE(mode_5_cfg);
		break;
F
Felipe Balbi 已提交
1322 1323
	}

1324
	pr_debug("%s: setup fifo_mode %d\n", musb_driver_name, fifo_mode);
F
Felipe Balbi 已提交
1325 1326


1327
done:
F
Felipe Balbi 已提交
1328 1329 1330 1331
	offset = fifo_setup(musb, hw_ep, &ep0_cfg, 0);
	/* assert(offset > 0) */

	/* NOTE:  for RTL versions >= 1.400 EPINFO and RAMINFO would
1332
	 * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
F
Felipe Balbi 已提交
1333 1334 1335 1336 1337
	 */

	for (i = 0; i < n; i++) {
		u8	epn = cfg->hw_ep_num;

1338
		if (epn >= musb->config->num_eps) {
F
Felipe Balbi 已提交
1339 1340
			pr_debug("%s: invalid ep %d\n",
					musb_driver_name, epn);
1341
			return -EINVAL;
F
Felipe Balbi 已提交
1342 1343 1344 1345 1346
		}
		offset = fifo_setup(musb, hw_ep + epn, cfg++, offset);
		if (offset < 0) {
			pr_debug("%s: mem overrun, ep %d\n",
					musb_driver_name, epn);
1347
			return offset;
F
Felipe Balbi 已提交
1348 1349 1350 1351 1352
		}
		epn++;
		musb->nr_endpoints = max(epn, musb->nr_endpoints);
	}

1353
	pr_debug("%s: %d/%d max ep, %d/%d memory\n",
F
Felipe Balbi 已提交
1354
			musb_driver_name,
1355 1356
			n + 1, musb->config->num_eps * 2 - 1,
			offset, (1 << (musb->config->ram_bits + 2)));
F
Felipe Balbi 已提交
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370

	if (!musb->bulk_ep) {
		pr_debug("%s: missing bulk\n", musb_driver_name);
		return -EINVAL;
	}

	return 0;
}


/*
 * ep_config_from_hw - when MUSB_C_DYNFIFO_DEF is false
 * @param musb the controller
 */
B
Bill Pemberton 已提交
1371
static int ep_config_from_hw(struct musb *musb)
F
Felipe Balbi 已提交
1372
{
1373
	u8 epnum = 0;
F
Felipe Balbi 已提交
1374
	struct musb_hw_ep *hw_ep;
F
Felipe Balbi 已提交
1375
	void __iomem *mbase = musb->mregs;
1376
	int ret = 0;
F
Felipe Balbi 已提交
1377

1378
	musb_dbg(musb, "<== static silicon ep config");
F
Felipe Balbi 已提交
1379 1380 1381

	/* FIXME pick up ep0 maxpacket size */

1382
	for (epnum = 1; epnum < musb->config->num_eps; epnum++) {
F
Felipe Balbi 已提交
1383 1384 1385
		musb_ep_select(mbase, epnum);
		hw_ep = musb->endpoints + epnum;

1386 1387
		ret = musb_read_fifosize(musb, hw_ep, epnum);
		if (ret < 0)
F
Felipe Balbi 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
			break;

		/* FIXME set up hw_ep->{rx,tx}_double_buffered */

		/* pick an RX/TX endpoint for bulk */
		if (hw_ep->max_packet_sz_tx < 512
				|| hw_ep->max_packet_sz_rx < 512)
			continue;

		/* REVISIT:  this algorithm is lazy, we should at least
		 * try to pick a double buffered endpoint.
		 */
		if (musb->bulk_ep)
			continue;
		musb->bulk_ep = hw_ep;
	}

	if (!musb->bulk_ep) {
		pr_debug("%s: missing bulk\n", musb_driver_name);
		return -EINVAL;
	}

	return 0;
}

enum { MUSB_CONTROLLER_MHDRC, MUSB_CONTROLLER_HDRC, };

/* Initialize MUSB (M)HDRC part of the USB hardware subsystem;
 * configure endpoints, or take their config from silicon
 */
B
Bill Pemberton 已提交
1418
static int musb_core_init(u16 musb_type, struct musb *musb)
F
Felipe Balbi 已提交
1419 1420 1421
{
	u8 reg;
	char *type;
1422
	char aInfo[90];
F
Felipe Balbi 已提交
1423 1424 1425 1426 1427
	void __iomem	*mbase = musb->mregs;
	int		status = 0;
	int		i;

	/* log core options (read using indexed model) */
1428
	reg = musb_read_configdata(mbase);
F
Felipe Balbi 已提交
1429 1430

	strcpy(aInfo, (reg & MUSB_CONFIGDATA_UTMIDW) ? "UTMI-16" : "UTMI-8");
1431
	if (reg & MUSB_CONFIGDATA_DYNFIFO) {
F
Felipe Balbi 已提交
1432
		strcat(aInfo, ", dyn FIFOs");
1433 1434
		musb->dyn_fifo = true;
	}
F
Felipe Balbi 已提交
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
	if (reg & MUSB_CONFIGDATA_MPRXE) {
		strcat(aInfo, ", bulk combine");
		musb->bulk_combine = true;
	}
	if (reg & MUSB_CONFIGDATA_MPTXE) {
		strcat(aInfo, ", bulk split");
		musb->bulk_split = true;
	}
	if (reg & MUSB_CONFIGDATA_HBRXE) {
		strcat(aInfo, ", HB-ISO Rx");
1445
		musb->hb_iso_rx = true;
F
Felipe Balbi 已提交
1446 1447 1448
	}
	if (reg & MUSB_CONFIGDATA_HBTXE) {
		strcat(aInfo, ", HB-ISO Tx");
1449
		musb->hb_iso_tx = true;
F
Felipe Balbi 已提交
1450 1451 1452 1453
	}
	if (reg & MUSB_CONFIGDATA_SOFTCONE)
		strcat(aInfo, ", SoftConn");

1454
	pr_debug("%s: ConfigData=0x%02x (%s)\n", musb_driver_name, reg, aInfo);
F
Felipe Balbi 已提交
1455 1456 1457 1458 1459 1460 1461 1462

	if (MUSB_CONTROLLER_MHDRC == musb_type) {
		musb->is_multipoint = 1;
		type = "M";
	} else {
		musb->is_multipoint = 0;
		type = "";
#ifndef	CONFIG_USB_OTG_BLACKLIST_HUB
1463 1464
		pr_err("%s: kernel must blacklist external hubs\n",
		       musb_driver_name);
F
Felipe Balbi 已提交
1465 1466 1467 1468
#endif
	}

	/* log release info */
1469
	musb->hwvers = musb_read_hwvers(mbase);
1470 1471 1472 1473
	pr_debug("%s: %sHDRC RTL version %d.%d%s\n",
		 musb_driver_name, type, MUSB_HWVERS_MAJOR(musb->hwvers),
		 MUSB_HWVERS_MINOR(musb->hwvers),
		 (musb->hwvers & MUSB_HWVERS_RC) ? "RC" : "");
F
Felipe Balbi 已提交
1474 1475

	/* configure ep0 */
1476
	musb_configure_ep0(musb);
F
Felipe Balbi 已提交
1477 1478 1479 1480 1481

	/* discover endpoint configuration */
	musb->nr_endpoints = 1;
	musb->epmask = 1;

1482 1483 1484 1485
	if (musb->dyn_fifo)
		status = ep_config_from_table(musb);
	else
		status = ep_config_from_hw(musb);
F
Felipe Balbi 已提交
1486 1487 1488 1489 1490 1491 1492 1493

	if (status < 0)
		return status;

	/* finish init, and print endpoint config */
	for (i = 0; i < musb->nr_endpoints; i++) {
		struct musb_hw_ep	*hw_ep = musb->endpoints + i;

1494
		hw_ep->fifo = musb->io.fifo_offset(i) + mbase;
1495
#if IS_ENABLED(CONFIG_USB_MUSB_TUSB6010)
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
		if (musb->io.quirks & MUSB_IN_TUSB) {
			hw_ep->fifo_async = musb->async + 0x400 +
				musb->io.fifo_offset(i);
			hw_ep->fifo_sync = musb->sync + 0x400 +
				musb->io.fifo_offset(i);
			hw_ep->fifo_sync_va =
				musb->sync_va + 0x400 + musb->io.fifo_offset(i);

			if (i == 0)
				hw_ep->conf = mbase - 0x400 + TUSB_EP0_CONF;
			else
				hw_ep->conf = mbase + 0x400 +
					(((i - 1) & 0xf) << 2);
		}
F
Felipe Balbi 已提交
1510 1511
#endif

1512
		hw_ep->regs = musb->io.ep_offset(i, 0) + mbase;
F
Felipe Balbi 已提交
1513 1514 1515 1516
		hw_ep->rx_reinit = 1;
		hw_ep->tx_reinit = 1;

		if (hw_ep->max_packet_sz_tx) {
1517
			musb_dbg(musb, "%s: hw_ep %d%s, %smax %d",
F
Felipe Balbi 已提交
1518 1519 1520 1521 1522 1523 1524
				musb_driver_name, i,
				hw_ep->is_shared_fifo ? "shared" : "tx",
				hw_ep->tx_double_buffered
					? "doublebuffer, " : "",
				hw_ep->max_packet_sz_tx);
		}
		if (hw_ep->max_packet_sz_rx && !hw_ep->is_shared_fifo) {
1525
			musb_dbg(musb, "%s: hw_ep %d%s, %smax %d",
F
Felipe Balbi 已提交
1526 1527 1528 1529 1530 1531 1532
				musb_driver_name, i,
				"rx",
				hw_ep->rx_double_buffered
					? "doublebuffer, " : "",
				hw_ep->max_packet_sz_rx);
		}
		if (!(hw_ep->max_packet_sz_tx || hw_ep->max_packet_sz_rx))
1533
			musb_dbg(musb, "hw_ep %d not configured", i);
F
Felipe Balbi 已提交
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
	}

	return 0;
}

/*-------------------------------------------------------------------------*/

/*
 * handle all the irqs defined by the HDRC core. for now we expect:  other
 * irq sources (phy, dma, etc) will be handled first, musb->int_* values
 * will be assigned, and the irq will already have been acked.
 *
 * called in irq context with spinlock held, irqs blocked
 */
irqreturn_t musb_interrupt(struct musb *musb)
{
	irqreturn_t	retval = IRQ_NONE;
1551 1552
	unsigned long	status;
	unsigned long	epnum;
1553
	u8		devctl;
1554 1555 1556

	if (!musb->int_usb && !musb->int_tx && !musb->int_rx)
		return IRQ_NONE;
F
Felipe Balbi 已提交
1557 1558 1559

	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);

1560
	trace_musb_isr(musb);
F
Felipe Balbi 已提交
1561

1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
	/**
	 * According to Mentor Graphics' documentation, flowchart on page 98,
	 * IRQ should be handled as follows:
	 *
	 * . Resume IRQ
	 * . Session Request IRQ
	 * . VBUS Error IRQ
	 * . Suspend IRQ
	 * . Connect IRQ
	 * . Disconnect IRQ
	 * . Reset/Babble IRQ
	 * . SOF IRQ (we're not using this one)
	 * . Endpoint 0 IRQ
	 * . TX Endpoints
	 * . RX Endpoints
	 *
	 * We will be following that flowchart in order to avoid any problems
	 * that might arise with internal Finite State Machine.
F
Felipe Balbi 已提交
1580
	 */
1581

1582
	if (musb->int_usb)
1583
		retval |= musb_stage0_irq(musb, musb->int_usb, devctl);
F
Felipe Balbi 已提交
1584 1585

	if (musb->int_tx & 1) {
1586
		if (is_host_active(musb))
F
Felipe Balbi 已提交
1587 1588 1589
			retval |= musb_h_ep0_irq(musb);
		else
			retval |= musb_g_ep0_irq(musb);
1590 1591 1592

		/* we have just handled endpoint 0 IRQ, clear it */
		musb->int_tx &= ~BIT(0);
F
Felipe Balbi 已提交
1593 1594
	}

1595 1596 1597 1598 1599 1600 1601 1602
	status = musb->int_tx;

	for_each_set_bit(epnum, &status, 16) {
		retval = IRQ_HANDLED;
		if (is_host_active(musb))
			musb_host_tx(musb, epnum);
		else
			musb_g_tx(musb, epnum);
F
Felipe Balbi 已提交
1603 1604
	}

1605
	status = musb->int_rx;
1606

1607 1608 1609 1610 1611 1612
	for_each_set_bit(epnum, &status, 16) {
		retval = IRQ_HANDLED;
		if (is_host_active(musb))
			musb_host_rx(musb, epnum);
		else
			musb_g_rx(musb, epnum);
F
Felipe Balbi 已提交
1613 1614 1615 1616
	}

	return retval;
}
1617
EXPORT_SYMBOL_GPL(musb_interrupt);
F
Felipe Balbi 已提交
1618 1619

#ifndef CONFIG_MUSB_PIO_ONLY
B
Bill Pemberton 已提交
1620
static bool use_dma = 1;
F
Felipe Balbi 已提交
1621 1622

/* "modprobe ... use_dma=0" etc */
B
Bin Liu 已提交
1623
module_param(use_dma, bool, 0644);
F
Felipe Balbi 已提交
1624 1625 1626 1627 1628 1629 1630
MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");

void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
{
	/* called with controller lock already held */

	if (!epnum) {
1631
		if (!is_cppi_enabled(musb)) {
F
Felipe Balbi 已提交
1632
			/* endpoint 0 */
1633
			if (is_host_active(musb))
F
Felipe Balbi 已提交
1634 1635 1636 1637 1638 1639 1640
				musb_h_ep0_irq(musb);
			else
				musb_g_ep0_irq(musb);
		}
	} else {
		/* endpoints 1..15 */
		if (transmit) {
1641
			if (is_host_active(musb))
1642 1643 1644
				musb_host_tx(musb, epnum);
			else
				musb_g_tx(musb, epnum);
F
Felipe Balbi 已提交
1645 1646
		} else {
			/* receive */
1647
			if (is_host_active(musb))
1648 1649 1650
				musb_host_rx(musb, epnum);
			else
				musb_g_rx(musb, epnum);
F
Felipe Balbi 已提交
1651 1652 1653
		}
	}
}
1654
EXPORT_SYMBOL_GPL(musb_dma_completion);
F
Felipe Balbi 已提交
1655 1656 1657 1658 1659

#else
#define use_dma			0
#endif

1660
static int (*musb_phy_callback)(enum musb_vbus_id_status status);
1661 1662 1663 1664 1665 1666 1667 1668

/*
 * musb_mailbox - optional phy notifier function
 * @status phy state change
 *
 * Optionally gets called from the USB PHY. Note that the USB PHY must be
 * disabled at the point the phy_callback is registered or unregistered.
 */
1669
int musb_mailbox(enum musb_vbus_id_status status)
1670 1671
{
	if (musb_phy_callback)
1672
		return musb_phy_callback(status);
1673

1674
	return -ENODEV;
1675 1676 1677
};
EXPORT_SYMBOL_GPL(musb_mailbox);

F
Felipe Balbi 已提交
1678 1679 1680
/*-------------------------------------------------------------------------*/

static ssize_t
1681
mode_show(struct device *dev, struct device_attribute *attr, char *buf)
F
Felipe Balbi 已提交
1682 1683 1684 1685 1686 1687
{
	struct musb *musb = dev_to_musb(dev);
	unsigned long flags;
	int ret = -EINVAL;

	spin_lock_irqsave(&musb->lock, flags);
1688
	ret = sprintf(buf, "%s\n", usb_otg_state_string(musb->xceiv->otg->state));
F
Felipe Balbi 已提交
1689 1690 1691 1692 1693 1694
	spin_unlock_irqrestore(&musb->lock, flags);

	return ret;
}

static ssize_t
1695
mode_store(struct device *dev, struct device_attribute *attr,
F
Felipe Balbi 已提交
1696 1697 1698 1699
		const char *buf, size_t n)
{
	struct musb	*musb = dev_to_musb(dev);
	unsigned long	flags;
D
David Brownell 已提交
1700
	int		status;
F
Felipe Balbi 已提交
1701 1702

	spin_lock_irqsave(&musb->lock, flags);
D
David Brownell 已提交
1703 1704 1705 1706 1707 1708 1709 1710
	if (sysfs_streq(buf, "host"))
		status = musb_platform_set_mode(musb, MUSB_HOST);
	else if (sysfs_streq(buf, "peripheral"))
		status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
	else if (sysfs_streq(buf, "otg"))
		status = musb_platform_set_mode(musb, MUSB_OTG);
	else
		status = -EINVAL;
F
Felipe Balbi 已提交
1711 1712
	spin_unlock_irqrestore(&musb->lock, flags);

D
David Brownell 已提交
1713
	return (status == 0) ? n : status;
F
Felipe Balbi 已提交
1714
}
1715
static DEVICE_ATTR_RW(mode);
F
Felipe Balbi 已提交
1716 1717

static ssize_t
1718
vbus_store(struct device *dev, struct device_attribute *attr,
F
Felipe Balbi 已提交
1719 1720 1721 1722 1723 1724 1725
		const char *buf, size_t n)
{
	struct musb	*musb = dev_to_musb(dev);
	unsigned long	flags;
	unsigned long	val;

	if (sscanf(buf, "%lu", &val) < 1) {
1726
		dev_err(dev, "Invalid VBUS timeout ms value\n");
F
Felipe Balbi 已提交
1727 1728 1729 1730
		return -EINVAL;
	}

	spin_lock_irqsave(&musb->lock, flags);
D
David Brownell 已提交
1731 1732
	/* force T(a_wait_bcon) to be zero/unlimited *OR* valid */
	musb->a_wait_bcon = val ? max_t(int, val, OTG_TIME_A_WAIT_BCON) : 0 ;
1733
	if (musb->xceiv->otg->state == OTG_STATE_A_WAIT_BCON)
F
Felipe Balbi 已提交
1734 1735 1736 1737 1738 1739 1740 1741
		musb->is_active = 0;
	musb_platform_try_idle(musb, jiffies + msecs_to_jiffies(val));
	spin_unlock_irqrestore(&musb->lock, flags);

	return n;
}

static ssize_t
1742
vbus_show(struct device *dev, struct device_attribute *attr, char *buf)
F
Felipe Balbi 已提交
1743 1744 1745 1746 1747
{
	struct musb	*musb = dev_to_musb(dev);
	unsigned long	flags;
	unsigned long	val;
	int		vbus;
1748
	u8		devctl;
F
Felipe Balbi 已提交
1749

1750
	pm_runtime_get_sync(dev);
F
Felipe Balbi 已提交
1751 1752 1753
	spin_lock_irqsave(&musb->lock, flags);
	val = musb->a_wait_bcon;
	vbus = musb_platform_get_vbus_status(musb);
1754 1755 1756 1757 1758 1759 1760 1761 1762
	if (vbus < 0) {
		/* Use default MUSB method by means of DEVCTL register */
		devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
		if ((devctl & MUSB_DEVCTL_VBUS)
				== (3 << MUSB_DEVCTL_VBUS_SHIFT))
			vbus = 1;
		else
			vbus = 0;
	}
F
Felipe Balbi 已提交
1763
	spin_unlock_irqrestore(&musb->lock, flags);
1764
	pm_runtime_put_sync(dev);
F
Felipe Balbi 已提交
1765

D
David Brownell 已提交
1766
	return sprintf(buf, "Vbus %s, timeout %lu msec\n",
F
Felipe Balbi 已提交
1767 1768
			vbus ? "on" : "off", val);
}
1769
static DEVICE_ATTR_RW(vbus);
F
Felipe Balbi 已提交
1770 1771 1772 1773

/* Gadget drivers can't know that a host is connected so they might want
 * to start SRP, but users can.  This allows userspace to trigger SRP.
 */
1774
static ssize_t srp_store(struct device *dev, struct device_attribute *attr,
F
Felipe Balbi 已提交
1775 1776 1777 1778 1779 1780 1781
		const char *buf, size_t n)
{
	struct musb	*musb = dev_to_musb(dev);
	unsigned short	srp;

	if (sscanf(buf, "%hu", &srp) != 1
			|| (srp != 1)) {
1782
		dev_err(dev, "SRP: Value must be 1\n");
F
Felipe Balbi 已提交
1783 1784 1785 1786 1787 1788 1789 1790
		return -EINVAL;
	}

	if (srp == 1)
		musb_g_wakeup(musb);

	return n;
}
1791
static DEVICE_ATTR_WO(srp);
F
Felipe Balbi 已提交
1792

F
Felipe Balbi 已提交
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803
static struct attribute *musb_attributes[] = {
	&dev_attr_mode.attr,
	&dev_attr_vbus.attr,
	&dev_attr_srp.attr,
	NULL
};

static const struct attribute_group musb_attr_group = {
	.attrs = musb_attributes,
};

1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
#define MUSB_QUIRK_B_INVALID_VBUS_91	(MUSB_DEVCTL_BDEVICE | \
					 (2 << MUSB_DEVCTL_VBUS_SHIFT) | \
					 MUSB_DEVCTL_SESSION)
#define MUSB_QUIRK_A_DISCONNECT_19	((3 << MUSB_DEVCTL_VBUS_SHIFT) | \
					 MUSB_DEVCTL_SESSION)

/*
 * Check the musb devctl session bit to determine if we want to
 * allow PM runtime for the device. In general, we want to keep things
 * active when the session bit is set except after host disconnect.
 *
 * Only called from musb_irq_work. If this ever needs to get called
 * elsewhere, proper locking must be implemented for musb->session.
 */
static void musb_pm_runtime_check_session(struct musb *musb)
{
	u8 devctl, s;
	int error;

	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);

	/* Handle session status quirks first */
	s = MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV |
		MUSB_DEVCTL_HR;
	switch (devctl & ~s) {
	case MUSB_QUIRK_B_INVALID_VBUS_91:
1830
		if (musb->quirk_retries && !musb->flush_irq_work) {
1831
			musb_dbg(musb,
1832 1833 1834
				 "Poll devctl on invalid vbus, assume no session");
			schedule_delayed_work(&musb->irq_work,
					      msecs_to_jiffies(1000));
1835
			musb->quirk_retries--;
1836 1837
			return;
		}
1838
		/* fall through */
1839
	case MUSB_QUIRK_A_DISCONNECT_19:
1840
		if (musb->quirk_retries && !musb->flush_irq_work) {
1841 1842 1843 1844
			musb_dbg(musb,
				 "Poll devctl on possible host mode disconnect");
			schedule_delayed_work(&musb->irq_work,
					      msecs_to_jiffies(1000));
1845
			musb->quirk_retries--;
1846 1847
			return;
		}
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
		if (!musb->session)
			break;
		musb_dbg(musb, "Allow PM on possible host mode disconnect");
		pm_runtime_mark_last_busy(musb->controller);
		pm_runtime_put_autosuspend(musb->controller);
		musb->session = false;
		return;
	default:
		break;
	}

	/* No need to do anything if session has not changed */
	s = devctl & MUSB_DEVCTL_SESSION;
	if (s == musb->session)
		return;

	/* Block PM or allow PM? */
	if (s) {
		musb_dbg(musb, "Block PM on active session: %02x", devctl);
		error = pm_runtime_get_sync(musb->controller);
		if (error < 0)
			dev_err(musb->controller, "Could not enable: %i\n",
				error);
1871
		musb->quirk_retries = 3;
1872 1873 1874 1875 1876 1877 1878 1879 1880
	} else {
		musb_dbg(musb, "Allow PM with no session: %02x", devctl);
		pm_runtime_mark_last_busy(musb->controller);
		pm_runtime_put_autosuspend(musb->controller);
	}

	musb->session = s;
}

F
Felipe Balbi 已提交
1881 1882 1883
/* Only used to provide driver mode change events */
static void musb_irq_work(struct work_struct *data)
{
1884
	struct musb *musb = container_of(data, struct musb, irq_work.work);
1885 1886 1887 1888 1889 1890 1891 1892
	int error;

	error = pm_runtime_get_sync(musb->controller);
	if (error < 0) {
		dev_err(musb->controller, "Could not enable: %i\n", error);

		return;
	}
F
Felipe Balbi 已提交
1893

1894 1895
	musb_pm_runtime_check_session(musb);

1896 1897
	if (musb->xceiv->otg->state != musb->xceiv_old_state) {
		musb->xceiv_old_state = musb->xceiv->otg->state;
F
Felipe Balbi 已提交
1898 1899
		sysfs_notify(&musb->controller->kobj, NULL, "mode");
	}
1900 1901 1902

	pm_runtime_mark_last_busy(musb->controller);
	pm_runtime_put_autosuspend(musb->controller);
F
Felipe Balbi 已提交
1903 1904
}

1905
static void musb_recover_from_babble(struct musb *musb)
1906
{
1907 1908
	int ret;
	u8 devctl;
1909

1910 1911
	musb_disable_interrupts(musb);

1912 1913 1914 1915 1916 1917
	/*
	 * wait at least 320 cycles of 60MHz clock. That's 5.3us, we will give
	 * it some slack and wait for 10us.
	 */
	udelay(10);

1918
	ret  = musb_platform_recover(musb);
1919 1920
	if (ret) {
		musb_enable_interrupts(musb);
1921
		return;
1922
	}
1923

1924 1925 1926 1927
	/* drop session bit */
	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
	devctl &= ~MUSB_DEVCTL_SESSION;
	musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
1928

1929 1930
	/* tell usbcore about it */
	musb_root_disconnect(musb);
1931 1932

	/*
1933 1934
	 * When a babble condition occurs, the musb controller
	 * removes the session bit and the endpoint config is lost.
1935 1936
	 */
	if (musb->dyn_fifo)
1937
		ret = ep_config_from_table(musb);
1938
	else
1939
		ret = ep_config_from_hw(musb);
1940

1941 1942
	/* restart session */
	if (ret == 0)
1943 1944 1945
		musb_start(musb);
}

F
Felipe Balbi 已提交
1946 1947 1948 1949
/* --------------------------------------------------------------------------
 * Init support
 */

B
Bill Pemberton 已提交
1950
static struct musb *allocate_instance(struct device *dev,
1951
		const struct musb_hdrc_config *config, void __iomem *mbase)
F
Felipe Balbi 已提交
1952 1953 1954 1955
{
	struct musb		*musb;
	struct musb_hw_ep	*ep;
	int			epnum;
1956
	int			ret;
F
Felipe Balbi 已提交
1957

1958 1959
	musb = devm_kzalloc(dev, sizeof(*musb), GFP_KERNEL);
	if (!musb)
F
Felipe Balbi 已提交
1960 1961 1962 1963 1964
		return NULL;

	INIT_LIST_HEAD(&musb->control);
	INIT_LIST_HEAD(&musb->in_bulk);
	INIT_LIST_HEAD(&musb->out_bulk);
1965
	INIT_LIST_HEAD(&musb->pending_list);
F
Felipe Balbi 已提交
1966 1967

	musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
D
David Brownell 已提交
1968
	musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
F
Felipe Balbi 已提交
1969 1970 1971
	musb->mregs = mbase;
	musb->ctrl_base = mbase;
	musb->nIrq = -ENODEV;
1972
	musb->config = config;
1973
	BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
F
Felipe Balbi 已提交
1974
	for (epnum = 0, ep = musb->endpoints;
1975
			epnum < musb->config->num_eps;
F
Felipe Balbi 已提交
1976 1977 1978 1979 1980 1981
			epnum++, ep++) {
		ep->musb = musb;
		ep->epnum = epnum;
	}

	musb->controller = dev;
1982

1983 1984 1985 1986 1987 1988
	ret = musb_host_alloc(musb);
	if (ret < 0)
		goto err_free;

	dev_set_drvdata(dev, musb);

F
Felipe Balbi 已提交
1989
	return musb;
1990 1991 1992

err_free:
	return NULL;
F
Felipe Balbi 已提交
1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
}

static void musb_free(struct musb *musb)
{
	/* this has multiple entry modes. it handles fault cleanup after
	 * probe(), where things may be partially set up, as well as rmmod
	 * cleanup after everything's been de-activated.
	 */

#ifdef CONFIG_SYSFS
F
Felipe Balbi 已提交
2003
	sysfs_remove_group(&musb->controller->kobj, &musb_attr_group);
F
Felipe Balbi 已提交
2004 2005
#endif

A
Ajay Kumar Gupta 已提交
2006 2007 2008
	if (musb->nIrq >= 0) {
		if (musb->irq_wake)
			disable_irq_wake(musb->nIrq);
F
Felipe Balbi 已提交
2009 2010 2011
		free_irq(musb->nIrq, musb);
	}

2012
	musb_host_free(musb);
F
Felipe Balbi 已提交
2013 2014
}

2015 2016 2017 2018 2019 2020
struct musb_pending_work {
	int (*callback)(struct musb *musb, void *data);
	void *data;
	struct list_head node;
};

2021
#ifdef CONFIG_PM
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
/*
 * Called from musb_runtime_resume(), musb_resume(), and
 * musb_queue_resume_work(). Callers must take musb->lock.
 */
static int musb_run_resume_work(struct musb *musb)
{
	struct musb_pending_work *w, *_w;
	unsigned long flags;
	int error = 0;

	spin_lock_irqsave(&musb->list_lock, flags);
	list_for_each_entry_safe(w, _w, &musb->pending_list, node) {
		if (w->callback) {
			error = w->callback(musb, w->data);
			if (error < 0) {
				dev_err(musb->controller,
					"resume callback %p failed: %i\n",
					w->callback, error);
			}
		}
		list_del(&w->node);
		devm_kfree(musb->controller, w);
	}
	spin_unlock_irqrestore(&musb->list_lock, flags);

	return error;
}
2049
#endif
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094

/*
 * Called to run work if device is active or else queue the work to happen
 * on resume. Caller must take musb->lock and must hold an RPM reference.
 *
 * Note that we cowardly refuse queuing work after musb PM runtime
 * resume is done calling musb_run_resume_work() and return -EINPROGRESS
 * instead.
 */
int musb_queue_resume_work(struct musb *musb,
			   int (*callback)(struct musb *musb, void *data),
			   void *data)
{
	struct musb_pending_work *w;
	unsigned long flags;
	int error;

	if (WARN_ON(!callback))
		return -EINVAL;

	if (pm_runtime_active(musb->controller))
		return callback(musb, data);

	w = devm_kzalloc(musb->controller, sizeof(*w), GFP_ATOMIC);
	if (!w)
		return -ENOMEM;

	w->callback = callback;
	w->data = data;
	spin_lock_irqsave(&musb->list_lock, flags);
	if (musb->is_runtime_suspended) {
		list_add_tail(&w->node, &musb->pending_list);
		error = 0;
	} else {
		dev_err(musb->controller, "could not add resume work %p\n",
			callback);
		devm_kfree(musb->controller, w);
		error = -EINPROGRESS;
	}
	spin_unlock_irqrestore(&musb->list_lock, flags);

	return error;
}
EXPORT_SYMBOL_GPL(musb_queue_resume_work);

2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
static void musb_deassert_reset(struct work_struct *work)
{
	struct musb *musb;
	unsigned long flags;

	musb = container_of(work, struct musb, deassert_reset_work.work);

	spin_lock_irqsave(&musb->lock, flags);

	if (musb->port1_status & USB_PORT_STAT_RESET)
		musb_port_reset(musb, false);

	spin_unlock_irqrestore(&musb->lock, flags);
}

F
Felipe Balbi 已提交
2110 2111 2112
/*
 * Perform generic per-controller initialization.
 *
2113 2114 2115
 * @dev: the controller (already clocked, etc)
 * @nIrq: IRQ number
 * @ctrl: virtual address of controller registers,
F
Felipe Balbi 已提交
2116 2117
 *	not yet corrected for platform-specific offsets
 */
B
Bill Pemberton 已提交
2118
static int
F
Felipe Balbi 已提交
2119 2120 2121 2122
musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
{
	int			status;
	struct musb		*musb;
J
Jingoo Han 已提交
2123
	struct musb_hdrc_platform_data *plat = dev_get_platdata(dev);
F
Felipe Balbi 已提交
2124 2125 2126 2127 2128

	/* The driver might handle more features than the board; OK.
	 * Fail when the board needs a feature that's not enabled.
	 */
	if (!plat) {
2129
		dev_err(dev, "no platform_data?\n");
2130 2131
		status = -ENODEV;
		goto fail0;
F
Felipe Balbi 已提交
2132
	}
2133

F
Felipe Balbi 已提交
2134
	/* allocate */
2135
	musb = allocate_instance(dev, plat->config, ctrl);
2136 2137 2138 2139
	if (!musb) {
		status = -ENOMEM;
		goto fail0;
	}
F
Felipe Balbi 已提交
2140 2141

	spin_lock_init(&musb->lock);
2142
	spin_lock_init(&musb->list_lock);
F
Felipe Balbi 已提交
2143 2144
	musb->board_set_power = plat->set_power;
	musb->min_power = plat->min_power;
2145
	musb->ops = plat->platform_ops;
2146
	musb->port_mode = plat->mode;
F
Felipe Balbi 已提交
2147

2148 2149 2150 2151 2152 2153 2154 2155 2156 2157
	/*
	 * Initialize the default IO functions. At least omap2430 needs
	 * these early. We initialize the platform specific IO functions
	 * later on.
	 */
	musb_readb = musb_default_readb;
	musb_writeb = musb_default_writeb;
	musb_readw = musb_default_readw;
	musb_writew = musb_default_writew;

2158
	/* The musb_platform_init() call:
2159 2160
	 *   - adjusts musb->mregs
	 *   - sets the musb->isr
2161
	 *   - may initialize an integrated transceiver
2162
	 *   - initializes musb->xceiv, usually by otg_get_phy()
2163 2164
	 *   - stops powering VBUS
	 *
A
Arnd Bergmann 已提交
2165
	 * There are various transceiver configurations.
2166 2167 2168
	 * DaVinci, TUSB60x0, and others integrate them.  OMAP3 uses
	 * external/discrete ones in various flavors (twl4030 family,
	 * isp1504, non-OTG, etc) mostly hooking up through ULPI.
F
Felipe Balbi 已提交
2169
	 */
2170
	status = musb_platform_init(musb);
F
Felipe Balbi 已提交
2171
	if (status < 0)
2172
		goto fail1;
2173

F
Felipe Balbi 已提交
2174 2175
	if (!musb->isr) {
		status = -ENODEV;
2176
		goto fail2;
F
Felipe Balbi 已提交
2177 2178
	}

2179 2180 2181
	if (musb->ops->quirks)
		musb->io.quirks = musb->ops->quirks;

2182
	/* Most devices use indexed offset or flat offset */
2183 2184 2185 2186 2187 2188 2189 2190
	if (musb->io.quirks & MUSB_INDEXED_EP) {
		musb->io.ep_offset = musb_indexed_ep_offset;
		musb->io.ep_select = musb_indexed_ep_select;
	} else {
		musb->io.ep_offset = musb_flat_ep_offset;
		musb->io.ep_select = musb_flat_ep_select;
	}

2191 2192 2193
	if (musb->io.quirks & MUSB_G_NO_SKB_RESERVE)
		musb->g.quirk_avoids_skb_reserve = 1;

2194 2195 2196 2197 2198 2199
	/* At least tusb6010 has its own offsets */
	if (musb->ops->ep_offset)
		musb->io.ep_offset = musb->ops->ep_offset;
	if (musb->ops->ep_select)
		musb->io.ep_select = musb->ops->ep_select;

2200 2201 2202 2203 2204
	if (musb->ops->fifo_mode)
		fifo_mode = musb->ops->fifo_mode;
	else
		fifo_mode = 4;

2205 2206 2207 2208 2209
	if (musb->ops->fifo_offset)
		musb->io.fifo_offset = musb->ops->fifo_offset;
	else
		musb->io.fifo_offset = musb_default_fifo_offset;

2210 2211 2212 2213 2214
	if (musb->ops->busctl_offset)
		musb->io.busctl_offset = musb->ops->busctl_offset;
	else
		musb->io.busctl_offset = musb_default_busctl_offset;

2215 2216 2217 2218 2219 2220 2221 2222 2223
	if (musb->ops->readb)
		musb_readb = musb->ops->readb;
	if (musb->ops->writeb)
		musb_writeb = musb->ops->writeb;
	if (musb->ops->readw)
		musb_readw = musb->ops->readw;
	if (musb->ops->writew)
		musb_writew = musb->ops->writew;

2224 2225 2226
#ifndef CONFIG_MUSB_PIO_ONLY
	if (!musb->ops->dma_init || !musb->ops->dma_exit) {
		dev_err(dev, "DMA controller not set\n");
2227
		status = -ENODEV;
2228 2229 2230 2231 2232 2233
		goto fail2;
	}
	musb_dma_controller_create = musb->ops->dma_init;
	musb_dma_controller_destroy = musb->ops->dma_exit;
#endif

2234 2235 2236 2237 2238 2239 2240 2241 2242 2243
	if (musb->ops->read_fifo)
		musb->io.read_fifo = musb->ops->read_fifo;
	else
		musb->io.read_fifo = musb_default_read_fifo;

	if (musb->ops->write_fifo)
		musb->io.write_fifo = musb->ops->write_fifo;
	else
		musb->io.write_fifo = musb_default_write_fifo;

2244
	if (!musb->xceiv->io_ops) {
2245
		musb->xceiv->io_dev = musb->controller;
2246 2247 2248 2249
		musb->xceiv->io_priv = musb->mregs;
		musb->xceiv->io_ops = &musb_ulpi_access;
	}

2250 2251 2252
	if (musb->ops->phy_callback)
		musb_phy_callback = musb->ops->phy_callback;

2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
	/*
	 * We need musb_read/write functions initialized for PM.
	 * Note that at least 2430 glue needs autosuspend delay
	 * somewhere above 300 ms for the hardware to idle properly
	 * after disconnecting the cable in host mode. Let's use
	 * 500 ms for some margin.
	 */
	pm_runtime_use_autosuspend(musb->controller);
	pm_runtime_set_autosuspend_delay(musb->controller, 500);
	pm_runtime_enable(musb->controller);
2263 2264
	pm_runtime_get_sync(musb->controller);

2265 2266 2267 2268
	status = usb_phy_init(musb->xceiv);
	if (status < 0)
		goto err_usb_phy_init;

2269
	if (use_dma && dev->dma_mask) {
2270 2271
		musb->dma_controller =
			musb_dma_controller_create(musb, musb->mregs);
2272 2273 2274 2275 2276
		if (IS_ERR(musb->dma_controller)) {
			status = PTR_ERR(musb->dma_controller);
			goto fail2_5;
		}
	}
F
Felipe Balbi 已提交
2277 2278 2279

	/* be sure interrupts are disabled before connecting ISR */
	musb_platform_disable(musb);
2280 2281
	musb_disable_interrupts(musb);
	musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
F
Felipe Balbi 已提交
2282

2283
	/* Init IRQ workqueue before request_irq */
2284
	INIT_DELAYED_WORK(&musb->irq_work, musb_irq_work);
2285 2286
	INIT_DELAYED_WORK(&musb->deassert_reset_work, musb_deassert_reset);
	INIT_DELAYED_WORK(&musb->finish_resume_work, musb_host_finish_resume);
2287

F
Felipe Balbi 已提交
2288
	/* setup musb parts of the core (especially endpoints) */
2289
	status = musb_core_init(plat->config->multipoint
F
Felipe Balbi 已提交
2290 2291 2292
			? MUSB_CONTROLLER_MHDRC
			: MUSB_CONTROLLER_HDRC, musb);
	if (status < 0)
2293
		goto fail3;
F
Felipe Balbi 已提交
2294

2295
	timer_setup(&musb->otg_timer, musb_otg_timer_func, 0);
D
David Brownell 已提交
2296

F
Felipe Balbi 已提交
2297
	/* attach to the IRQ */
A
Alexandre Bailon 已提交
2298
	if (request_irq(nIrq, musb->isr, IRQF_SHARED, dev_name(dev), musb)) {
F
Felipe Balbi 已提交
2299 2300
		dev_err(dev, "request_irq %d failed!\n", nIrq);
		status = -ENODEV;
2301
		goto fail3;
F
Felipe Balbi 已提交
2302 2303
	}
	musb->nIrq = nIrq;
2304
	/* FIXME this handles wakeup irqs wrong */
2305 2306
	if (enable_irq_wake(nIrq) == 0) {
		musb->irq_wake = 1;
F
Felipe Balbi 已提交
2307
		device_init_wakeup(dev, 1);
2308 2309 2310
	} else {
		musb->irq_wake = 0;
	}
F
Felipe Balbi 已提交
2311

2312 2313 2314 2315 2316
	/* program PHY to use external vBus if required */
	if (plat->extvbus) {
		u8 busctl = musb_read_ulpi_buscontrol(musb->mregs);
		busctl |= MUSB_ULPI_USE_EXTVBUS;
		musb_write_ulpi_buscontrol(musb->mregs, busctl);
F
Felipe Balbi 已提交
2317 2318
	}

2319 2320
	if (musb->xceiv->otg->default_a) {
		MUSB_HST_MODE(musb);
2321
		musb->xceiv->otg->state = OTG_STATE_A_IDLE;
2322 2323
	} else {
		MUSB_DEV_MODE(musb);
2324
		musb->xceiv->otg->state = OTG_STATE_B_IDLE;
2325
	}
F
Felipe Balbi 已提交
2326

D
Daniel Mack 已提交
2327 2328 2329
	switch (musb->port_mode) {
	case MUSB_PORT_MODE_HOST:
		status = musb_host_setup(musb, plat->power);
2330 2331 2332
		if (status < 0)
			goto fail3;
		status = musb_platform_set_mode(musb, MUSB_HOST);
D
Daniel Mack 已提交
2333 2334 2335
		break;
	case MUSB_PORT_MODE_GADGET:
		status = musb_gadget_setup(musb);
2336 2337 2338
		if (status < 0)
			goto fail3;
		status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
D
Daniel Mack 已提交
2339 2340 2341 2342 2343 2344
		break;
	case MUSB_PORT_MODE_DUAL_ROLE:
		status = musb_host_setup(musb, plat->power);
		if (status < 0)
			goto fail3;
		status = musb_gadget_setup(musb);
2345
		if (status) {
2346
			musb_host_cleanup(musb);
2347 2348 2349
			goto fail3;
		}
		status = musb_platform_set_mode(musb, MUSB_OTG);
D
Daniel Mack 已提交
2350 2351 2352 2353 2354
		break;
	default:
		dev_err(dev, "unsupported port mode %d\n", musb->port_mode);
		break;
	}
F
Felipe Balbi 已提交
2355

2356
	if (status < 0)
2357
		goto fail3;
F
Felipe Balbi 已提交
2358

F
Felipe Balbi 已提交
2359 2360
	status = musb_init_debugfs(musb);
	if (status < 0)
2361
		goto fail4;
F
Felipe Balbi 已提交
2362

F
Felipe Balbi 已提交
2363
	status = sysfs_create_group(&musb->controller->kobj, &musb_attr_group);
2364
	if (status)
2365
		goto fail5;
F
Felipe Balbi 已提交
2366

2367
	musb->is_initialized = 1;
2368 2369
	pm_runtime_mark_last_busy(musb->controller);
	pm_runtime_put_autosuspend(musb->controller);
2370

2371
	return 0;
F
Felipe Balbi 已提交
2372

2373 2374 2375
fail5:
	musb_exit_debugfs(musb);

2376
fail4:
2377
	musb_gadget_cleanup(musb);
2378
	musb_host_cleanup(musb);
2379 2380

fail3:
2381
	cancel_delayed_work_sync(&musb->irq_work);
2382 2383
	cancel_delayed_work_sync(&musb->finish_resume_work);
	cancel_delayed_work_sync(&musb->deassert_reset_work);
2384
	if (musb->dma_controller)
2385
		musb_dma_controller_destroy(musb->dma_controller);
2386

2387
fail2_5:
2388 2389 2390
	usb_phy_shutdown(musb->xceiv);

err_usb_phy_init:
2391
	pm_runtime_dont_use_autosuspend(musb->controller);
2392
	pm_runtime_put_sync(musb->controller);
2393
	pm_runtime_disable(musb->controller);
2394 2395

fail2:
2396 2397
	if (musb->irq_wake)
		device_init_wakeup(dev, 0);
F
Felipe Balbi 已提交
2398
	musb_platform_exit(musb);
2399

2400
fail1:
2401 2402 2403
	if (status != -EPROBE_DEFER)
		dev_err(musb->controller,
			"%s failed with status %d\n", __func__, status);
2404

2405 2406
	musb_free(musb);

2407 2408
fail0:

2409 2410
	return status;

F
Felipe Balbi 已提交
2411 2412 2413 2414 2415 2416 2417
}

/*-------------------------------------------------------------------------*/

/* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
 * bridge to a platform device; this driver then suffices.
 */
B
Bill Pemberton 已提交
2418
static int musb_probe(struct platform_device *pdev)
F
Felipe Balbi 已提交
2419 2420
{
	struct device	*dev = &pdev->dev;
2421
	int		irq = platform_get_irq_byname(pdev, "mc");
F
Felipe Balbi 已提交
2422 2423 2424
	struct resource	*iomem;
	void __iomem	*base;

2425
	if (irq <= 0)
F
Felipe Balbi 已提交
2426 2427
		return -ENODEV;

2428
	iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2429 2430 2431
	base = devm_ioremap_resource(dev, iomem);
	if (IS_ERR(base))
		return PTR_ERR(base);
F
Felipe Balbi 已提交
2432

2433
	return musb_init_controller(dev, irq, base);
F
Felipe Balbi 已提交
2434 2435
}

B
Bill Pemberton 已提交
2436
static int musb_remove(struct platform_device *pdev)
F
Felipe Balbi 已提交
2437
{
2438 2439
	struct device	*dev = &pdev->dev;
	struct musb	*musb = dev_to_musb(dev);
2440
	unsigned long	flags;
F
Felipe Balbi 已提交
2441 2442 2443 2444 2445 2446

	/* this gets called on rmmod.
	 *  - Host mode: host may still be active
	 *  - Peripheral mode: peripheral is deactivated (or never-activated)
	 *  - OTG mode: both roles are deactivated (or never-activated)
	 */
F
Felipe Balbi 已提交
2447
	musb_exit_debugfs(musb);
2448

2449
	cancel_delayed_work_sync(&musb->irq_work);
2450 2451
	cancel_delayed_work_sync(&musb->finish_resume_work);
	cancel_delayed_work_sync(&musb->deassert_reset_work);
2452 2453 2454
	pm_runtime_get_sync(musb->controller);
	musb_host_cleanup(musb);
	musb_gadget_cleanup(musb);
2455

2456
	musb_platform_disable(musb);
2457
	spin_lock_irqsave(&musb->lock, flags);
2458
	musb_disable_interrupts(musb);
2459
	musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
2460
	spin_unlock_irqrestore(&musb->lock, flags);
2461
	musb_platform_exit(musb);
2462

2463 2464 2465
	pm_runtime_dont_use_autosuspend(musb->controller);
	pm_runtime_put_sync(musb->controller);
	pm_runtime_disable(musb->controller);
2466
	musb_phy_callback = NULL;
2467
	if (musb->dma_controller)
2468
		musb_dma_controller_destroy(musb->dma_controller);
2469
	usb_phy_shutdown(musb->xceiv);
F
Felipe Balbi 已提交
2470
	musb_free(musb);
2471
	device_init_wakeup(dev, 0);
F
Felipe Balbi 已提交
2472 2473 2474 2475 2476
	return 0;
}

#ifdef	CONFIG_PM

2477
static void musb_save_context(struct musb *musb)
2478 2479 2480
{
	int i;
	void __iomem *musb_base = musb->mregs;
2481
	void __iomem *epio;
2482

2483 2484 2485
	musb->context.frame = musb_readw(musb_base, MUSB_FRAME);
	musb->context.testmode = musb_readb(musb_base, MUSB_TESTMODE);
	musb->context.busctl = musb_read_ulpi_buscontrol(musb->mregs);
2486 2487 2488 2489
	musb->context.power = musb_readb(musb_base, MUSB_POWER);
	musb->context.intrusbe = musb_readb(musb_base, MUSB_INTRUSBE);
	musb->context.index = musb_readb(musb_base, MUSB_INDEX);
	musb->context.devctl = musb_readb(musb_base, MUSB_DEVCTL);
2490

2491
	for (i = 0; i < musb->config->num_eps; ++i) {
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501
		struct musb_hw_ep	*hw_ep;

		hw_ep = &musb->endpoints[i];
		if (!hw_ep)
			continue;

		epio = hw_ep->regs;
		if (!epio)
			continue;

2502
		musb_writeb(musb_base, MUSB_INDEX, i);
2503
		musb->context.index_regs[i].txmaxp =
2504
			musb_readw(epio, MUSB_TXMAXP);
2505
		musb->context.index_regs[i].txcsr =
2506
			musb_readw(epio, MUSB_TXCSR);
2507
		musb->context.index_regs[i].rxmaxp =
2508
			musb_readw(epio, MUSB_RXMAXP);
2509
		musb->context.index_regs[i].rxcsr =
2510
			musb_readw(epio, MUSB_RXCSR);
2511 2512

		if (musb->dyn_fifo) {
2513
			musb->context.index_regs[i].txfifoadd =
2514
					musb_read_txfifoadd(musb_base);
2515
			musb->context.index_regs[i].rxfifoadd =
2516
					musb_read_rxfifoadd(musb_base);
2517
			musb->context.index_regs[i].txfifosz =
2518
					musb_read_txfifosz(musb_base);
2519
			musb->context.index_regs[i].rxfifosz =
2520 2521
					musb_read_rxfifosz(musb_base);
		}
2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532

		musb->context.index_regs[i].txtype =
			musb_readb(epio, MUSB_TXTYPE);
		musb->context.index_regs[i].txinterval =
			musb_readb(epio, MUSB_TXINTERVAL);
		musb->context.index_regs[i].rxtype =
			musb_readb(epio, MUSB_RXTYPE);
		musb->context.index_regs[i].rxinterval =
			musb_readb(epio, MUSB_RXINTERVAL);

		musb->context.index_regs[i].txfunaddr =
2533
			musb_read_txfunaddr(musb, i);
2534
		musb->context.index_regs[i].txhubaddr =
2535
			musb_read_txhubaddr(musb, i);
2536
		musb->context.index_regs[i].txhubport =
2537
			musb_read_txhubport(musb, i);
2538 2539

		musb->context.index_regs[i].rxfunaddr =
2540
			musb_read_rxfunaddr(musb, i);
2541
		musb->context.index_regs[i].rxhubaddr =
2542
			musb_read_rxhubaddr(musb, i);
2543
		musb->context.index_regs[i].rxhubport =
2544
			musb_read_rxhubport(musb, i);
2545 2546 2547
	}
}

2548
static void musb_restore_context(struct musb *musb)
2549 2550 2551
{
	int i;
	void __iomem *musb_base = musb->mregs;
2552
	void __iomem *epio;
2553
	u8 power;
2554

2555 2556 2557
	musb_writew(musb_base, MUSB_FRAME, musb->context.frame);
	musb_writeb(musb_base, MUSB_TESTMODE, musb->context.testmode);
	musb_write_ulpi_buscontrol(musb->mregs, musb->context.busctl);
2558 2559 2560 2561 2562 2563 2564 2565

	/* Don't affect SUSPENDM/RESUME bits in POWER reg */
	power = musb_readb(musb_base, MUSB_POWER);
	power &= MUSB_POWER_SUSPENDM | MUSB_POWER_RESUME;
	musb->context.power &= ~(MUSB_POWER_SUSPENDM | MUSB_POWER_RESUME);
	power |= musb->context.power;
	musb_writeb(musb_base, MUSB_POWER, power);

2566
	musb_writew(musb_base, MUSB_INTRTXE, musb->intrtxe);
2567
	musb_writew(musb_base, MUSB_INTRRXE, musb->intrrxe);
2568
	musb_writeb(musb_base, MUSB_INTRUSBE, musb->context.intrusbe);
2569 2570
	if (musb->context.devctl & MUSB_DEVCTL_SESSION)
		musb_writeb(musb_base, MUSB_DEVCTL, musb->context.devctl);
2571

2572
	for (i = 0; i < musb->config->num_eps; ++i) {
2573 2574 2575 2576 2577 2578 2579 2580 2581 2582
		struct musb_hw_ep	*hw_ep;

		hw_ep = &musb->endpoints[i];
		if (!hw_ep)
			continue;

		epio = hw_ep->regs;
		if (!epio)
			continue;

2583
		musb_writeb(musb_base, MUSB_INDEX, i);
2584
		musb_writew(epio, MUSB_TXMAXP,
2585
			musb->context.index_regs[i].txmaxp);
2586
		musb_writew(epio, MUSB_TXCSR,
2587
			musb->context.index_regs[i].txcsr);
2588
		musb_writew(epio, MUSB_RXMAXP,
2589
			musb->context.index_regs[i].rxmaxp);
2590
		musb_writew(epio, MUSB_RXCSR,
2591
			musb->context.index_regs[i].rxcsr);
2592 2593 2594

		if (musb->dyn_fifo) {
			musb_write_txfifosz(musb_base,
2595
				musb->context.index_regs[i].txfifosz);
2596
			musb_write_rxfifosz(musb_base,
2597
				musb->context.index_regs[i].rxfifosz);
2598
			musb_write_txfifoadd(musb_base,
2599
				musb->context.index_regs[i].txfifoadd);
2600
			musb_write_rxfifoadd(musb_base,
2601
				musb->context.index_regs[i].rxfifoadd);
2602 2603
		}

2604
		musb_writeb(epio, MUSB_TXTYPE,
2605
				musb->context.index_regs[i].txtype);
2606
		musb_writeb(epio, MUSB_TXINTERVAL,
2607
				musb->context.index_regs[i].txinterval);
2608
		musb_writeb(epio, MUSB_RXTYPE,
2609
				musb->context.index_regs[i].rxtype);
2610
		musb_writeb(epio, MUSB_RXINTERVAL,
2611

2612
				musb->context.index_regs[i].rxinterval);
2613
		musb_write_txfunaddr(musb, i,
2614
				musb->context.index_regs[i].txfunaddr);
2615
		musb_write_txhubaddr(musb, i,
2616
				musb->context.index_regs[i].txhubaddr);
2617
		musb_write_txhubport(musb, i,
2618
				musb->context.index_regs[i].txhubport);
2619

2620
		musb_write_rxfunaddr(musb, i,
2621
				musb->context.index_regs[i].rxfunaddr);
2622
		musb_write_rxhubaddr(musb, i,
2623
				musb->context.index_regs[i].rxhubaddr);
2624
		musb_write_rxhubport(musb, i,
2625
				musb->context.index_regs[i].rxhubport);
2626
	}
2627
	musb_writeb(musb_base, MUSB_INDEX, musb->context.index);
2628 2629
}

2630
static int musb_suspend(struct device *dev)
F
Felipe Balbi 已提交
2631
{
2632
	struct musb	*musb = dev_to_musb(dev);
F
Felipe Balbi 已提交
2633
	unsigned long	flags;
2634 2635 2636 2637 2638 2639 2640
	int ret;

	ret = pm_runtime_get_sync(dev);
	if (ret < 0) {
		pm_runtime_put_noidle(dev);
		return ret;
	}
F
Felipe Balbi 已提交
2641

2642
	musb_platform_disable(musb);
2643
	musb_disable_interrupts(musb);
2644 2645 2646 2647 2648 2649

	musb->flush_irq_work = true;
	while (flush_delayed_work(&musb->irq_work))
		;
	musb->flush_irq_work = false;

2650 2651
	if (!(musb->io.quirks & MUSB_PRESERVE_SESSION))
		musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
2652

2653
	WARN_ON(!list_empty(&musb->pending_list));
2654

F
Felipe Balbi 已提交
2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666
	spin_lock_irqsave(&musb->lock, flags);

	if (is_peripheral_active(musb)) {
		/* FIXME force disconnect unless we know USB will wake
		 * the system up quickly enough to respond ...
		 */
	} else if (is_host_active(musb)) {
		/* we know all the children are suspended; sometimes
		 * they will even be wakeup-enabled.
		 */
	}

2667 2668
	musb_save_context(musb);

F
Felipe Balbi 已提交
2669 2670 2671 2672
	spin_unlock_irqrestore(&musb->lock, flags);
	return 0;
}

2673
static int musb_resume(struct device *dev)
F
Felipe Balbi 已提交
2674
{
2675 2676 2677 2678 2679
	struct musb *musb = dev_to_musb(dev);
	unsigned long flags;
	int error;
	u8 devctl;
	u8 mask;
2680 2681 2682

	/*
	 * For static cmos like DaVinci, register values were preserved
2683 2684
	 * unless for some reason the whole soc powered down or the USB
	 * module got reset through the PSC (vs just being disabled).
2685 2686 2687 2688
	 *
	 * For the DSPS glue layer though, a full register restore has to
	 * be done. As it shouldn't harm other platforms, we do it
	 * unconditionally.
F
Felipe Balbi 已提交
2689
	 */
2690 2691 2692

	musb_restore_context(musb);

2693 2694 2695 2696
	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
	mask = MUSB_DEVCTL_BDEVICE | MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV;
	if ((devctl & mask) != (musb->context.devctl & mask))
		musb->port1_status = 0;
2697

2698 2699
	musb_enable_interrupts(musb);
	musb_platform_enable(musb);
2700

2701 2702 2703 2704 2705 2706 2707
	spin_lock_irqsave(&musb->lock, flags);
	error = musb_run_resume_work(musb);
	if (error)
		dev_err(musb->controller, "resume work failed with %i\n",
			error);
	spin_unlock_irqrestore(&musb->lock, flags);

2708 2709 2710
	pm_runtime_mark_last_busy(dev);
	pm_runtime_put_autosuspend(dev);

F
Felipe Balbi 已提交
2711 2712 2713
	return 0;
}

2714 2715 2716 2717 2718
static int musb_runtime_suspend(struct device *dev)
{
	struct musb	*musb = dev_to_musb(dev);

	musb_save_context(musb);
2719
	musb->is_runtime_suspended = 1;
2720 2721 2722 2723 2724 2725

	return 0;
}

static int musb_runtime_resume(struct device *dev)
{
2726 2727 2728
	struct musb *musb = dev_to_musb(dev);
	unsigned long flags;
	int error;
2729 2730 2731 2732 2733 2734 2735 2736 2737 2738

	/*
	 * When pm_runtime_get_sync called for the first time in driver
	 * init,  some of the structure is still not initialized which is
	 * used in restore function. But clock needs to be
	 * enabled before any register access, so
	 * pm_runtime_get_sync has to be called.
	 * Also context restore without save does not make
	 * any sense
	 */
2739 2740 2741 2742
	if (!musb->is_initialized)
		return 0;

	musb_restore_context(musb);
2743

2744 2745 2746 2747 2748 2749 2750 2751
	spin_lock_irqsave(&musb->lock, flags);
	error = musb_run_resume_work(musb);
	if (error)
		dev_err(musb->controller, "resume work failed with %i\n",
			error);
	musb->is_runtime_suspended = 0;
	spin_unlock_irqrestore(&musb->lock, flags);

2752 2753 2754
	return 0;
}

2755
static const struct dev_pm_ops musb_dev_pm_ops = {
2756
	.suspend	= musb_suspend,
2757
	.resume		= musb_resume,
2758 2759
	.runtime_suspend = musb_runtime_suspend,
	.runtime_resume = musb_runtime_resume,
2760 2761 2762
};

#define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
F
Felipe Balbi 已提交
2763
#else
2764
#define	MUSB_DEV_PM_OPS	NULL
F
Felipe Balbi 已提交
2765 2766 2767 2768 2769 2770
#endif

static struct platform_driver musb_driver = {
	.driver = {
		.name		= (char *)musb_driver_name,
		.bus		= &platform_bus_type,
2771
		.pm		= MUSB_DEV_PM_OPS,
F
Felipe Balbi 已提交
2772
	},
2773
	.probe		= musb_probe,
B
Bill Pemberton 已提交
2774
	.remove		= musb_remove,
F
Felipe Balbi 已提交
2775 2776
};

2777
module_platform_driver(musb_driver);