musb_core.c 68.0 KB
Newer Older
F
Felipe Balbi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/*
 * MUSB OTG driver core code
 *
 * Copyright 2005 Mentor Graphics Corporation
 * Copyright (C) 2005-2006 by Texas Instruments
 * Copyright (C) 2006-2007 Nokia Corporation
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

/*
 * 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
 *	- platform_data is mostly for board-specific informarion
D
David Brownell 已提交
87
 *	  (plus recentrly, SOC or family details)
F
Felipe Balbi 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
 *
 * 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/init.h>
#include <linux/list.h>
#include <linux/kobject.h>
#include <linux/platform_device.h>
#include <linux/io.h>

#include "musb_core.h"

D
David Brownell 已提交
104
#define TA_WAIT_BCON(m) max_t(int, (m)->a_wait_bcon, OTG_TIME_A_WAIT_BCON)
F
Felipe Balbi 已提交
105 106 107 108 109


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

110
#define MUSB_VERSION "6.0"
F
Felipe Balbi 已提交
111 112 113

#define DRIVER_INFO DRIVER_DESC ", v" MUSB_VERSION

114
#define MUSB_DRIVER_NAME "musb-hdrc"
F
Felipe Balbi 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
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);
}

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

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
#ifndef CONFIG_BLACKFIN
static int musb_ulpi_read(struct otg_transceiver *otg, u32 offset)
{
	void __iomem *addr = otg->io_priv;
	int	i = 0;
	u8	r;
	u8	power;

	/* 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.
	 */

	musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)offset);
	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++;
156
		if (i == 10000)
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
			return -ETIMEDOUT;

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

	return musb_readb(addr, MUSB_ULPI_REG_DATA);
}

static int musb_ulpi_write(struct otg_transceiver *otg,
		u32 offset, u32 data)
{
	void __iomem *addr = otg->io_priv;
	int	i = 0;
	u8	r = 0;
	u8	power;

	/* 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);

	musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)offset);
	musb_writeb(addr, MUSB_ULPI_REG_DATA, (u8)data);
	musb_writeb(addr, MUSB_ULPI_REG_CONTROL, MUSB_ULPI_REG_REQ);

	while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
				& MUSB_ULPI_REG_CMPLT)) {
		i++;
187
		if (i == 10000)
188 189 190 191 192 193 194 195 196 197
			return -ETIMEDOUT;
	}

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

	return 0;
}
#else
198 199
#define musb_ulpi_read		NULL
#define musb_ulpi_write		NULL
200 201 202 203 204 205 206 207 208
#endif

static struct otg_io_access_ops musb_ulpi_access = {
	.read = musb_ulpi_read,
	.write = musb_ulpi_write,
};

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

209
#if !defined(CONFIG_USB_MUSB_TUSB6010) && !defined(CONFIG_USB_MUSB_BLACKFIN)
210

F
Felipe Balbi 已提交
211 212 213 214 215
/*
 * Load an endpoint's FIFO
 */
void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
{
216
	struct musb *musb = hw_ep->musb;
F
Felipe Balbi 已提交
217 218 219 220
	void __iomem *fifo = hw_ep->fifo;

	prefetch((u8 *)src);

221
	dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
F
Felipe Balbi 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
			'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) {
				writesl(fifo, src + index, len >> 2);
				index += len & ~0x03;
			}
			if (len & 0x02) {
				musb_writew(fifo, 0, *(u16 *)&src[index]);
				index += 2;
			}
		} else {
			if (len >= 2) {
				writesw(fifo, src + index, len >> 1);
				index += len & ~0x01;
			}
		}
		if (len & 0x01)
			musb_writeb(fifo, 0, src[index]);
	} else  {
		/* byte aligned */
		writesb(fifo, src, len);
	}
}

252
#if !defined(CONFIG_USB_MUSB_AM35X)
F
Felipe Balbi 已提交
253 254 255 256 257
/*
 * Unload an endpoint's FIFO
 */
void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
{
258
	struct musb *musb = hw_ep->musb;
F
Felipe Balbi 已提交
259 260
	void __iomem *fifo = hw_ep->fifo;

261
	dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
F
Felipe Balbi 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
			'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) {
				readsl(fifo, dst, len >> 2);
				index = len & ~0x03;
			}
			if (len & 0x02) {
				*(u16 *)&dst[index] = musb_readw(fifo, 0);
				index += 2;
			}
		} else {
			if (len >= 2) {
				readsw(fifo, dst, len >> 1);
				index = len & ~0x01;
			}
		}
		if (len & 0x01)
			dst[index] = musb_readb(fifo, 0);
	} else  {
		/* byte aligned */
		readsb(fifo, dst, len);
	}
}
291
#endif
F
Felipe Balbi 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

#endif	/* normal PIO */


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

/* 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);
}

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

#ifdef	CONFIG_USB_MUSB_OTG

/*
 * Handles OTG hnp timeouts, such as b_ase0_brst
 */
void musb_otg_timer_func(unsigned long data)
{
	struct musb	*musb = (struct musb *)data;
	unsigned long	flags;

	spin_lock_irqsave(&musb->lock, flags);
341
	switch (musb->xceiv->state) {
F
Felipe Balbi 已提交
342
	case OTG_STATE_B_WAIT_ACON:
343
		dev_dbg(musb->controller, "HNP: b_wait_acon timeout; back to b_peripheral\n");
F
Felipe Balbi 已提交
344
		musb_g_disconnect(musb);
345
		musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
F
Felipe Balbi 已提交
346 347
		musb->is_active = 0;
		break;
348
	case OTG_STATE_A_SUSPEND:
F
Felipe Balbi 已提交
349
	case OTG_STATE_A_WAIT_BCON:
350
		dev_dbg(musb->controller, "HNP: %s timeout\n",
351
			otg_state_string(musb->xceiv->state));
352
		musb_platform_set_vbus(musb, 0);
353
		musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
F
Felipe Balbi 已提交
354 355
		break;
	default:
356
		dev_dbg(musb->controller, "HNP: Unhandled mode %s\n",
357
			otg_state_string(musb->xceiv->state));
F
Felipe Balbi 已提交
358 359 360 361 362 363
	}
	musb->ignore_disconnect = 0;
	spin_unlock_irqrestore(&musb->lock, flags);
}

/*
D
David Brownell 已提交
364
 * Stops the HNP transition. Caller must take care of locking.
F
Felipe Balbi 已提交
365 366 367 368 369 370 371
 */
void musb_hnp_stop(struct musb *musb)
{
	struct usb_hcd	*hcd = musb_to_hcd(musb);
	void __iomem	*mbase = musb->mregs;
	u8	reg;

372
	dev_dbg(musb->controller, "HNP: stop from %s\n", otg_state_string(musb->xceiv->state));
373

374
	switch (musb->xceiv->state) {
F
Felipe Balbi 已提交
375 376
	case OTG_STATE_A_PERIPHERAL:
		musb_g_disconnect(musb);
377
		dev_dbg(musb->controller, "HNP: back to %s\n",
378
			otg_state_string(musb->xceiv->state));
F
Felipe Balbi 已提交
379 380
		break;
	case OTG_STATE_B_HOST:
381
		dev_dbg(musb->controller, "HNP: Disabling HR\n");
F
Felipe Balbi 已提交
382
		hcd->self.is_b_host = 0;
383
		musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
F
Felipe Balbi 已提交
384 385 386 387 388 389 390
		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:
391
		dev_dbg(musb->controller, "HNP: Stopping in unknown state %s\n",
392
			otg_state_string(musb->xceiv->state));
F
Felipe Balbi 已提交
393 394 395 396 397 398 399
	}

	/*
	 * When returning to A state after HNP, avoid hub_port_rebounce(),
	 * which cause occasional OPT A "Did not receive reset after connect"
	 * errors.
	 */
400
	musb->port1_status &= ~(USB_PORT_STAT_C_CONNECTION << 16);
F
Felipe Balbi 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
}

#endif

/*
 * 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,
				u8 devctl, u8 power)
{
	irqreturn_t handled = IRQ_NONE;

422
	dev_dbg(musb->controller, "<== Power=%02x, DevCtl=%02x, int_usb=0x%x\n", power, devctl,
F
Felipe Balbi 已提交
423 424 425 426 427 428 429 430
		int_usb);

	/* 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;
431
		dev_dbg(musb->controller, "RESUME (%s)\n", otg_state_string(musb->xceiv->state));
F
Felipe Balbi 已提交
432 433 434

		if (devctl & MUSB_DEVCTL_HM) {
#ifdef CONFIG_USB_MUSB_HDRC_HCD
435 436
			void __iomem *mbase = musb->mregs;

437
			switch (musb->xceiv->state) {
F
Felipe Balbi 已提交
438 439 440 441 442 443 444 445
			case OTG_STATE_A_SUSPEND:
				/* remote wakeup?  later, GetPortStatus
				 * will stop RESUME signaling
				 */

				if (power & MUSB_POWER_SUSPENDM) {
					/* spurious */
					musb->int_usb &= ~MUSB_INTR_SUSPEND;
446
					dev_dbg(musb->controller, "Spurious SUSPENDM\n");
F
Felipe Balbi 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459
					break;
				}

				power &= ~MUSB_POWER_SUSPENDM;
				musb_writeb(mbase, MUSB_POWER,
						power | MUSB_POWER_RESUME);

				musb->port1_status |=
						(USB_PORT_STAT_C_SUSPEND << 16)
						| MUSB_PORT_STAT_RESUME;
				musb->rh_timer = jiffies
						+ msecs_to_jiffies(20);

460
				musb->xceiv->state = OTG_STATE_A_HOST;
F
Felipe Balbi 已提交
461 462 463 464
				musb->is_active = 1;
				usb_hcd_resume_root_hub(musb_to_hcd(musb));
				break;
			case OTG_STATE_B_WAIT_ACON:
465
				musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
F
Felipe Balbi 已提交
466 467 468 469 470 471
				musb->is_active = 1;
				MUSB_DEV_MODE(musb);
				break;
			default:
				WARNING("bogus %s RESUME (%s)\n",
					"host",
472
					otg_state_string(musb->xceiv->state));
F
Felipe Balbi 已提交
473 474 475
			}
#endif
		} else {
476
			switch (musb->xceiv->state) {
F
Felipe Balbi 已提交
477 478 479
#ifdef CONFIG_USB_MUSB_HDRC_HCD
			case OTG_STATE_A_SUSPEND:
				/* possibly DISCONNECT is upcoming */
480
				musb->xceiv->state = OTG_STATE_A_HOST;
F
Felipe Balbi 已提交
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
				usb_hcd_resume_root_hub(musb_to_hcd(musb));
				break;
#endif
#ifdef CONFIG_USB_GADGET_MUSB_HDRC
			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;
#endif
			default:
				WARNING("bogus %s RESUME (%s)\n",
					"peripheral",
506
					otg_state_string(musb->xceiv->state));
F
Felipe Balbi 已提交
507 508 509 510 511 512 513
			}
		}
	}

#ifdef CONFIG_USB_MUSB_HDRC_HCD
	/* see manual for the order of the tests */
	if (int_usb & MUSB_INTR_SESSREQ) {
514 515
		void __iomem *mbase = musb->mregs;

516 517
		if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS
				&& (devctl & MUSB_DEVCTL_BDEVICE)) {
518
			dev_dbg(musb->controller, "SessReq while on B state\n");
519 520 521
			return IRQ_HANDLED;
		}

522
		dev_dbg(musb->controller, "SESSION_REQUEST (%s)\n",
523
			otg_state_string(musb->xceiv->state));
F
Felipe Balbi 已提交
524 525 526 527 528 529 530 531 532 533

		/* 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;
534
		musb->xceiv->state = OTG_STATE_A_IDLE;
F
Felipe Balbi 已提交
535
		MUSB_HST_MODE(musb);
536
		musb_platform_set_vbus(musb, 1);
F
Felipe Balbi 已提交
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559

		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 ?
		 */
560
		switch (musb->xceiv->state) {
F
Felipe Balbi 已提交
561 562 563 564 565 566 567 568 569 570
		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) {
571 572
				void __iomem *mbase = musb->mregs;

F
Felipe Balbi 已提交
573 574 575 576 577 578
				musb->vbuserr_retry--;
				ignore = 1;
				devctl |= MUSB_DEVCTL_SESSION;
				musb_writeb(mbase, MUSB_DEVCTL, devctl);
			} else {
				musb->port1_status |=
579 580
					  USB_PORT_STAT_OVERCURRENT
					| (USB_PORT_STAT_C_OVERCURRENT << 16);
F
Felipe Balbi 已提交
581 582 583 584 585 586
			}
			break;
		default:
			break;
		}

587
		dev_dbg(musb->controller, "VBUS_ERROR in %s (%02x, %s), retry #%d, port1 %08x\n",
588
				otg_state_string(musb->xceiv->state),
F
Felipe Balbi 已提交
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
				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;
				}; s; }),
				VBUSERR_RETRY_COUNT - musb->vbuserr_retry,
				musb->port1_status);

		/* go through A_WAIT_VFALL then start a new session */
		if (!ignore)
607
			musb_platform_set_vbus(musb, 0);
F
Felipe Balbi 已提交
608 609 610
		handled = IRQ_HANDLED;
	}

611
#endif
612
	if (int_usb & MUSB_INTR_SUSPEND) {
613
		dev_dbg(musb->controller, "SUSPEND (%s) devctl %02x power %02x\n",
614
			otg_state_string(musb->xceiv->state), devctl, power);
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
		handled = IRQ_HANDLED;

		switch (musb->xceiv->state) {
#ifdef	CONFIG_USB_MUSB_OTG
		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);
			usb_hcd_resume_root_hub(musb_to_hcd(musb));
			musb_root_disconnect(musb);
			musb_platform_try_idle(musb, jiffies
					+ msecs_to_jiffies(musb->a_wait_bcon
						? : OTG_TIME_A_WAIT_BCON));

			break;
#endif
		case OTG_STATE_B_IDLE:
			if (!musb->is_active)
				break;
		case OTG_STATE_B_PERIPHERAL:
			musb_g_suspend(musb);
			musb->is_active = is_otg_enabled(musb)
					&& musb->xceiv->gadget->b_hnp_enable;
			if (musb->is_active) {
#ifdef	CONFIG_USB_MUSB_OTG
				musb->xceiv->state = OTG_STATE_B_WAIT_ACON;
646
				dev_dbg(musb->controller, "HNP: Setting timer for b_ase0_brst\n");
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
				mod_timer(&musb->otg_timer, jiffies
					+ msecs_to_jiffies(
							OTG_TIME_B_ASE0_BRST));
#endif
			}
			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:
			musb->xceiv->state = OTG_STATE_A_SUSPEND;
			musb->is_active = is_otg_enabled(musb)
					&& musb->xceiv->host->b_hnp_enable;
			break;
		case OTG_STATE_B_HOST:
			/* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
665
			dev_dbg(musb->controller, "REVISIT: SUSPEND as B_HOST\n");
666 667 668 669 670 671 672 673
			break;
		default:
			/* "should not happen" */
			musb->is_active = 0;
			break;
		}
	}

674
#ifdef CONFIG_USB_MUSB_HDRC_HCD
F
Felipe Balbi 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688
	if (int_usb & MUSB_INTR_CONNECT) {
		struct usb_hcd *hcd = musb_to_hcd(musb);

		handled = IRQ_HANDLED;
		musb->is_active = 1;
		set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);

		musb->ep0_stage = MUSB_EP0_START;

#ifdef CONFIG_USB_MUSB_OTG
		/* flush endpoints when transitioning from Device Mode */
		if (is_peripheral_active(musb)) {
			/* REVISIT HNP; just force disconnect */
		}
689 690 691
		musb_writew(musb->mregs, MUSB_INTRTXE, musb->epmask);
		musb_writew(musb->mregs, MUSB_INTRRXE, musb->epmask & 0xfffe);
		musb_writeb(musb->mregs, MUSB_INTRUSBE, 0xf7);
F
Felipe Balbi 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704
#endif
		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 */
705
		switch (musb->xceiv->state) {
F
Felipe Balbi 已提交
706 707
		case OTG_STATE_B_PERIPHERAL:
			if (int_usb & MUSB_INTR_SUSPEND) {
708
				dev_dbg(musb->controller, "HNP: SUSPEND+CONNECT, now b_host\n");
F
Felipe Balbi 已提交
709
				int_usb &= ~MUSB_INTR_SUSPEND;
710
				goto b_host;
F
Felipe Balbi 已提交
711
			} else
712
				dev_dbg(musb->controller, "CONNECT as b_peripheral???\n");
F
Felipe Balbi 已提交
713 714
			break;
		case OTG_STATE_B_WAIT_ACON:
715
			dev_dbg(musb->controller, "HNP: CONNECT, now b_host\n");
716
b_host:
717
			musb->xceiv->state = OTG_STATE_B_HOST;
F
Felipe Balbi 已提交
718
			hcd->self.is_b_host = 1;
719 720
			musb->ignore_disconnect = 0;
			del_timer(&musb->otg_timer);
F
Felipe Balbi 已提交
721 722 723 724
			break;
		default:
			if ((devctl & MUSB_DEVCTL_VBUS)
					== (3 << MUSB_DEVCTL_VBUS_SHIFT)) {
725
				musb->xceiv->state = OTG_STATE_A_HOST;
F
Felipe Balbi 已提交
726 727 728 729
				hcd->self.is_b_host = 0;
			}
			break;
		}
730 731 732 733 734 735 736 737

		/* poke the root hub */
		MUSB_HST_MODE(musb);
		if (hcd->status_urb)
			usb_hcd_poll_rh_status(hcd);
		else
			usb_hcd_resume_root_hub(hcd);

738
		dev_dbg(musb->controller, "CONNECT (%s) devctl %02x\n",
739
				otg_state_string(musb->xceiv->state), devctl);
F
Felipe Balbi 已提交
740 741 742
	}
#endif	/* CONFIG_USB_MUSB_HDRC_HCD */

743
	if ((int_usb & MUSB_INTR_DISCONNECT) && !musb->ignore_disconnect) {
744
		dev_dbg(musb->controller, "DISCONNECT (%s) as %s, devctl %02x\n",
745
				otg_state_string(musb->xceiv->state),
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
				MUSB_MODE(musb), devctl);
		handled = IRQ_HANDLED;

		switch (musb->xceiv->state) {
#ifdef CONFIG_USB_MUSB_HDRC_HCD
		case OTG_STATE_A_HOST:
		case OTG_STATE_A_SUSPEND:
			usb_hcd_resume_root_hub(musb_to_hcd(musb));
			musb_root_disconnect(musb);
			if (musb->a_wait_bcon != 0 && is_otg_enabled(musb))
				musb_platform_try_idle(musb, jiffies
					+ msecs_to_jiffies(musb->a_wait_bcon));
			break;
#endif	/* HOST */
#ifdef CONFIG_USB_MUSB_OTG
		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);
			musb_to_hcd(musb)->self.is_b_host = 0;
			musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
			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 */
#endif	/* OTG */
#ifdef CONFIG_USB_GADGET_MUSB_HDRC
		case OTG_STATE_B_PERIPHERAL:
		case OTG_STATE_B_IDLE:
			musb_g_disconnect(musb);
			break;
#endif	/* GADGET */
		default:
			WARNING("unhandled DISCONNECT transition (%s)\n",
788
				otg_state_string(musb->xceiv->state));
789 790 791 792
			break;
		}
	}

F
Felipe Balbi 已提交
793 794 795 796
	/* 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) {
797
		handled = IRQ_HANDLED;
F
Felipe Balbi 已提交
798 799 800 801 802 803 804 805 806
		if (is_host_capable() && (devctl & MUSB_DEVCTL_HM) != 0) {
			/*
			 * Looks like non-HS BABBLE can be ignored, but
			 * HS BABBLE is an error condition. For HS the solution
			 * is to avoid babble in the first place and fix what
			 * caused BABBLE. When HS BABBLE happens we can only
			 * stop the session.
			 */
			if (devctl & (MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV))
807
				dev_dbg(musb->controller, "BABBLE devctl: %02x\n", devctl);
F
Felipe Balbi 已提交
808 809
			else {
				ERR("Stopping host session -- babble\n");
810
				musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
F
Felipe Balbi 已提交
811 812
			}
		} else if (is_peripheral_capable()) {
813
			dev_dbg(musb->controller, "BUS RESET as %s\n",
814
				otg_state_string(musb->xceiv->state));
815
			switch (musb->xceiv->state) {
F
Felipe Balbi 已提交
816 817 818 819 820 821 822 823 824 825
#ifdef CONFIG_USB_OTG
			case OTG_STATE_A_SUSPEND:
				/* We need to ignore disconnect on suspend
				 * otherwise tusb 2.0 won't reconnect after a
				 * power cycle, which breaks otg compliance.
				 */
				musb->ignore_disconnect = 1;
				musb_g_reset(musb);
				/* FALLTHROUGH */
			case OTG_STATE_A_WAIT_BCON:	/* OPT TD.4.7-900ms */
D
David Brownell 已提交
826
				/* never use invalid T(a_wait_bcon) */
827
				dev_dbg(musb->controller, "HNP: in %s, %d msec timeout\n",
828 829
					otg_state_string(musb->xceiv->state),
					TA_WAIT_BCON(musb));
D
David Brownell 已提交
830 831
				mod_timer(&musb->otg_timer, jiffies
					+ msecs_to_jiffies(TA_WAIT_BCON(musb)));
F
Felipe Balbi 已提交
832 833
				break;
			case OTG_STATE_A_PERIPHERAL:
834 835 836
				musb->ignore_disconnect = 0;
				del_timer(&musb->otg_timer);
				musb_g_reset(musb);
F
Felipe Balbi 已提交
837 838
				break;
			case OTG_STATE_B_WAIT_ACON:
839
				dev_dbg(musb->controller, "HNP: RESET (%s), to b_peripheral\n",
840
					otg_state_string(musb->xceiv->state));
841
				musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
F
Felipe Balbi 已提交
842 843 844 845
				musb_g_reset(musb);
				break;
#endif
			case OTG_STATE_B_IDLE:
846
				musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
F
Felipe Balbi 已提交
847 848 849 850 851
				/* FALLTHROUGH */
			case OTG_STATE_B_PERIPHERAL:
				musb_g_reset(musb);
				break;
			default:
852
				dev_dbg(musb->controller, "Unhandled BUS RESET as %s\n",
853
					otg_state_string(musb->xceiv->state));
F
Felipe Balbi 已提交
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
			}
		}
	}

#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;

876
		dev_dbg(musb->controller, "START_OF_FRAME\n");
F
Felipe Balbi 已提交
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
		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

903
	schedule_work(&musb->irq_work);
F
Felipe Balbi 已提交
904 905 906 907 908 909 910 911 912 913 914 915 916 917

	return handled;
}

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

/*
* 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);

918
	dev_dbg(musb->controller, "<== devctl %02x\n", devctl);
F
Felipe Balbi 已提交
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 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 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994

	/*  Set INT enable registers, enable interrupts */
	musb_writew(regs, MUSB_INTRTXE, musb->epmask);
	musb_writew(regs, MUSB_INTRRXE, musb->epmask & 0xfffe);
	musb_writeb(regs, MUSB_INTRUSBE, 0xf7);

	musb_writeb(regs, MUSB_TESTMODE, 0);

	/* put into basic highspeed mode and start session */
	musb_writeb(regs, MUSB_POWER, MUSB_POWER_ISOUPDATE
						| MUSB_POWER_SOFTCONN
						| MUSB_POWER_HSENAB
						/* ENSUSPEND wedges tusb */
						/* | MUSB_POWER_ENSUSPEND */
						);

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

	if (is_otg_enabled(musb)) {
		/* session started after:
		 * (a) ID-grounded irq, host mode;
		 * (b) vbus present/connect IRQ, peripheral mode;
		 * (c) peripheral initiates, using SRP
		 */
		if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
			musb->is_active = 1;
		else
			devctl |= MUSB_DEVCTL_SESSION;

	} else if (is_host_enabled(musb)) {
		/* assume ID pin is hard-wired to ground */
		devctl |= MUSB_DEVCTL_SESSION;

	} else /* peripheral is enabled */ {
		if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
			musb->is_active = 1;
	}
	musb_platform_enable(musb);
	musb_writeb(regs, MUSB_DEVCTL, devctl);
}


static void musb_generic_disable(struct musb *musb)
{
	void __iomem	*mbase = musb->mregs;
	u16	temp;

	/* disable interrupts */
	musb_writeb(mbase, MUSB_INTRUSBE, 0);
	musb_writew(mbase, MUSB_INTRTXE, 0);
	musb_writew(mbase, MUSB_INTRRXE, 0);

	/* off */
	musb_writeb(mbase, MUSB_DEVCTL, 0);

	/*  flush pending interrupts */
	temp = musb_readb(mbase, MUSB_INTRUSB);
	temp = musb_readw(mbase, MUSB_INTRTX);
	temp = musb_readw(mbase, MUSB_INTRRX);

}

/*
 * 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);
	musb_generic_disable(musb);
995
	dev_dbg(musb->controller, "HDRC disabled\n");
F
Felipe Balbi 已提交
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011

	/* 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);
}

static void musb_shutdown(struct platform_device *pdev)
{
	struct musb	*musb = dev_to_musb(&pdev->dev);
	unsigned long	flags;

1012
	pm_runtime_get_sync(musb->controller);
F
Felipe Balbi 已提交
1013 1014 1015 1016 1017
	spin_lock_irqsave(&musb->lock, flags);
	musb_platform_disable(musb);
	musb_generic_disable(musb);
	spin_unlock_irqrestore(&musb->lock, flags);

1018 1019 1020 1021 1022
	if (!is_otg_enabled(musb) && is_host_enabled(musb))
		usb_remove_hcd(musb_to_hcd(musb));
	musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
	musb_platform_exit(musb);

1023
	pm_runtime_put(musb->controller);
F
Felipe Balbi 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032
	/* FIXME power down */
}


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

/*
 * 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 已提交
1033 1034 1035
 * 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 已提交
1036 1037 1038 1039
 *
 * We don't currently use dynamic fifo setup capability to do anything
 * more than selecting one of a bunch of predefined configurations.
 */
1040 1041
#if defined(CONFIG_USB_MUSB_TUSB6010) || defined(CONFIG_USB_MUSB_OMAP2PLUS) \
	|| defined(CONFIG_USB_MUSB_AM35X)
F
Felipe Balbi 已提交
1042
static ushort __initdata fifo_mode = 4;
1043 1044
#elif defined(CONFIG_USB_MUSB_UX500)
static ushort __initdata fifo_mode = 5;
F
Felipe Balbi 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
#else
static ushort __initdata fifo_mode = 2;
#endif

/* "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 */
1059
static struct musb_fifo_cfg __initdata mode_0_cfg[] = {
F
Felipe Balbi 已提交
1060 1061 1062 1063 1064 1065 1066 1067
{ .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 */
1068
static struct musb_fifo_cfg __initdata mode_1_cfg[] = {
F
Felipe Balbi 已提交
1069 1070 1071 1072 1073 1074 1075 1076
{ .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 */
1077
static struct musb_fifo_cfg __initdata mode_2_cfg[] = {
F
Felipe Balbi 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086
{ .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_RXTX, .maxpacket = 256, },
{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
};

/* mode 3 - fits in 4KB */
1087
static struct musb_fifo_cfg __initdata mode_3_cfg[] = {
F
Felipe Balbi 已提交
1088 1089 1090 1091 1092 1093 1094 1095 1096
{ .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 */
1097
static struct musb_fifo_cfg __initdata mode_4_cfg[] = {
F
Felipe Balbi 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
{ .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, },
1116 1117 1118 1119 1120 1121 1122
{ .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 已提交
1123 1124 1125 1126
{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
};

1127
/* mode 5 - fits in 8KB */
1128
static struct musb_fifo_cfg __initdata mode_5_cfg[] = {
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
{ .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 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165

/*
 * 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.
 */
static int __init
fifo_setup(struct musb *musb, struct musb_hw_ep  *hw_ep,
1166
		const struct musb_fifo_cfg *cfg, u16 offset)
F
Felipe Balbi 已提交
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
{
	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) {
1181 1182
		if ((offset + (maxpacket << 1)) >
				(1 << (musb->config->ram_bits + 2)))
F
Felipe Balbi 已提交
1183 1184 1185
			return -EMSGSIZE;
		c_size |= MUSB_FIFOSZ_DPB;
	} else {
1186
		if ((offset + maxpacket) > (1 << (musb->config->ram_bits + 2)))
F
Felipe Balbi 已提交
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
			return -EMSGSIZE;
	}

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

#ifdef CONFIG_USB_MUSB_HDRC_HCD
	/* EP0 reserved endpoint for control, bidirectional;
	 * EP1 reserved for bulk, two unidirection halves.
	 */
	if (hw_ep->epnum == 1)
		musb->bulk_ep = hw_ep;
	/* REVISIT error check:  be sure ep0 can both rx and tx ... */
#endif
	switch (cfg->style) {
	case FIFO_TX:
1203 1204
		musb_write_txfifosz(mbase, c_size);
		musb_write_txfifoadd(mbase, c_off);
F
Felipe Balbi 已提交
1205 1206 1207 1208
		hw_ep->tx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
		hw_ep->max_packet_sz_tx = maxpacket;
		break;
	case FIFO_RX:
1209 1210
		musb_write_rxfifosz(mbase, c_size);
		musb_write_rxfifoadd(mbase, c_off);
F
Felipe Balbi 已提交
1211 1212 1213 1214
		hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
		hw_ep->max_packet_sz_rx = maxpacket;
		break;
	case FIFO_RXTX:
1215 1216
		musb_write_txfifosz(mbase, c_size);
		musb_write_txfifoadd(mbase, c_off);
F
Felipe Balbi 已提交
1217 1218 1219
		hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
		hw_ep->max_packet_sz_rx = maxpacket;

1220 1221
		musb_write_rxfifosz(mbase, c_size);
		musb_write_rxfifoadd(mbase, c_off);
F
Felipe Balbi 已提交
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
		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));
}

1237
static struct musb_fifo_cfg __initdata ep0_cfg = {
F
Felipe Balbi 已提交
1238 1239 1240 1241 1242
	.style = FIFO_RXTX, .maxpacket = 64,
};

static int __init ep_config_from_table(struct musb *musb)
{
1243
	const struct musb_fifo_cfg	*cfg;
F
Felipe Balbi 已提交
1244 1245 1246 1247
	unsigned		i, n;
	int			offset;
	struct musb_hw_ep	*hw_ep = musb->endpoints;

1248 1249 1250 1251 1252 1253
	if (musb->config->fifo_cfg) {
		cfg = musb->config->fifo_cfg;
		n = musb->config->fifo_cfg_size;
		goto done;
	}

F
Felipe Balbi 已提交
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
	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;
1278 1279 1280 1281
	case 5:
		cfg = mode_5_cfg;
		n = ARRAY_SIZE(mode_5_cfg);
		break;
F
Felipe Balbi 已提交
1282 1283 1284 1285 1286 1287
	}

	printk(KERN_DEBUG "%s: setup fifo_mode %d\n",
			musb_driver_name, fifo_mode);


1288
done:
F
Felipe Balbi 已提交
1289 1290 1291 1292
	offset = fifo_setup(musb, hw_ep, &ep0_cfg, 0);
	/* assert(offset > 0) */

	/* NOTE:  for RTL versions >= 1.400 EPINFO and RAMINFO would
1293
	 * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
F
Felipe Balbi 已提交
1294 1295 1296 1297 1298
	 */

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

1299
		if (epn >= musb->config->num_eps) {
F
Felipe Balbi 已提交
1300 1301
			pr_debug("%s: invalid ep %d\n",
					musb_driver_name, epn);
1302
			return -EINVAL;
F
Felipe Balbi 已提交
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
		}
		offset = fifo_setup(musb, hw_ep + epn, cfg++, offset);
		if (offset < 0) {
			pr_debug("%s: mem overrun, ep %d\n",
					musb_driver_name, epn);
			return -EINVAL;
		}
		epn++;
		musb->nr_endpoints = max(epn, musb->nr_endpoints);
	}

	printk(KERN_DEBUG "%s: %d/%d max ep, %d/%d memory\n",
			musb_driver_name,
1316 1317
			n + 1, musb->config->num_eps * 2 - 1,
			offset, (1 << (musb->config->ram_bits + 2)));
F
Felipe Balbi 已提交
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335

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

	return 0;
}


/*
 * ep_config_from_hw - when MUSB_C_DYNFIFO_DEF is false
 * @param musb the controller
 */
static int __init ep_config_from_hw(struct musb *musb)
{
1336
	u8 epnum = 0;
F
Felipe Balbi 已提交
1337 1338
	struct musb_hw_ep *hw_ep;
	void *mbase = musb->mregs;
1339
	int ret = 0;
F
Felipe Balbi 已提交
1340

1341
	dev_dbg(musb->controller, "<== static silicon ep config\n");
F
Felipe Balbi 已提交
1342 1343 1344

	/* FIXME pick up ep0 maxpacket size */

1345
	for (epnum = 1; epnum < musb->config->num_eps; epnum++) {
F
Felipe Balbi 已提交
1346 1347 1348
		musb_ep_select(mbase, epnum);
		hw_ep = musb->endpoints + epnum;

1349 1350
		ret = musb_read_fifosize(musb, hw_ep, epnum);
		if (ret < 0)
F
Felipe Balbi 已提交
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
			break;

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

#ifdef CONFIG_USB_MUSB_HDRC_HCD
		/* 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;
#endif
	}

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

	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
 */
static int __init musb_core_init(u16 musb_type, struct musb *musb)
{
	u8 reg;
	char *type;
1389
	char aInfo[90], aRevision[32], aDate[12];
F
Felipe Balbi 已提交
1390 1391 1392 1393 1394
	void __iomem	*mbase = musb->mregs;
	int		status = 0;
	int		i;

	/* log core options (read using indexed model) */
1395
	reg = musb_read_configdata(mbase);
F
Felipe Balbi 已提交
1396 1397

	strcpy(aInfo, (reg & MUSB_CONFIGDATA_UTMIDW) ? "UTMI-16" : "UTMI-8");
1398
	if (reg & MUSB_CONFIGDATA_DYNFIFO) {
F
Felipe Balbi 已提交
1399
		strcat(aInfo, ", dyn FIFOs");
1400 1401
		musb->dyn_fifo = true;
	}
F
Felipe Balbi 已提交
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
	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");
1412
		musb->hb_iso_rx = true;
F
Felipe Balbi 已提交
1413 1414 1415
	}
	if (reg & MUSB_CONFIGDATA_HBTXE) {
		strcat(aInfo, ", HB-ISO Tx");
1416
		musb->hb_iso_tx = true;
F
Felipe Balbi 已提交
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
	}
	if (reg & MUSB_CONFIGDATA_SOFTCONE)
		strcat(aInfo, ", SoftConn");

	printk(KERN_DEBUG "%s: ConfigData=0x%02x (%s)\n",
			musb_driver_name, reg, aInfo);

	aDate[0] = 0;
	if (MUSB_CONTROLLER_MHDRC == musb_type) {
		musb->is_multipoint = 1;
		type = "M";
	} else {
		musb->is_multipoint = 0;
		type = "";
#ifdef CONFIG_USB_MUSB_HDRC_HCD
#ifndef	CONFIG_USB_OTG_BLACKLIST_HUB
		printk(KERN_ERR
			"%s: kernel must blacklist external hubs\n",
			musb_driver_name);
#endif
#endif
	}

	/* log release info */
1441 1442 1443 1444
	musb->hwvers = musb_read_hwvers(mbase);
	snprintf(aRevision, 32, "%d.%d%s", MUSB_HWVERS_MAJOR(musb->hwvers),
		MUSB_HWVERS_MINOR(musb->hwvers),
		(musb->hwvers & MUSB_HWVERS_RC) ? "RC" : "");
F
Felipe Balbi 已提交
1445 1446 1447 1448
	printk(KERN_DEBUG "%s: %sHDRC RTL version %s %s\n",
			musb_driver_name, type, aRevision, aDate);

	/* configure ep0 */
1449
	musb_configure_ep0(musb);
F
Felipe Balbi 已提交
1450 1451 1452 1453 1454

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

1455 1456 1457 1458
	if (musb->dyn_fifo)
		status = ep_config_from_table(musb);
	else
		status = ep_config_from_hw(musb);
F
Felipe Balbi 已提交
1459 1460 1461 1462 1463 1464 1465 1466 1467

	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;

		hw_ep->fifo = MUSB_FIFO_OFFSET(i) + mbase;
1468
#ifdef CONFIG_USB_MUSB_TUSB6010
F
Felipe Balbi 已提交
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
		hw_ep->fifo_async = musb->async + 0x400 + MUSB_FIFO_OFFSET(i);
		hw_ep->fifo_sync = musb->sync + 0x400 + MUSB_FIFO_OFFSET(i);
		hw_ep->fifo_sync_va =
			musb->sync_va + 0x400 + MUSB_FIFO_OFFSET(i);

		if (i == 0)
			hw_ep->conf = mbase - 0x400 + TUSB_EP0_CONF;
		else
			hw_ep->conf = mbase + 0x400 + (((i - 1) & 0xf) << 2);
#endif

		hw_ep->regs = MUSB_EP_OFFSET(i, 0) + mbase;
#ifdef CONFIG_USB_MUSB_HDRC_HCD
1482
		hw_ep->target_regs = musb_read_target_reg_base(i, mbase);
F
Felipe Balbi 已提交
1483 1484 1485 1486 1487
		hw_ep->rx_reinit = 1;
		hw_ep->tx_reinit = 1;
#endif

		if (hw_ep->max_packet_sz_tx) {
1488
			dev_dbg(musb->controller,
F
Felipe Balbi 已提交
1489 1490 1491 1492 1493 1494 1495 1496
				"%s: hw_ep %d%s, %smax %d\n",
				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) {
1497
			dev_dbg(musb->controller,
F
Felipe Balbi 已提交
1498 1499 1500 1501 1502 1503 1504 1505
				"%s: hw_ep %d%s, %smax %d\n",
				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))
1506
			dev_dbg(musb->controller, "hw_ep %d not configured\n", i);
F
Felipe Balbi 已提交
1507 1508 1509 1510 1511 1512 1513
	}

	return 0;
}

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

1514
#if defined(CONFIG_SOC_OMAP2430) || defined(CONFIG_SOC_OMAP3430) || \
1515 1516
	defined(CONFIG_ARCH_OMAP4) || defined(CONFIG_ARCH_U8500) || \
	defined(CONFIG_ARCH_U5500)
F
Felipe Balbi 已提交
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534

static irqreturn_t generic_interrupt(int irq, void *__hci)
{
	unsigned long	flags;
	irqreturn_t	retval = IRQ_NONE;
	struct musb	*musb = __hci;

	spin_lock_irqsave(&musb->lock, flags);

	musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
	musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
	musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);

	if (musb->int_usb || musb->int_tx || musb->int_rx)
		retval = musb_interrupt(musb);

	spin_unlock_irqrestore(&musb->lock, flags);

1535
	return retval;
F
Felipe Balbi 已提交
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
}

#else
#define generic_interrupt	NULL
#endif

/*
 * 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;
	u8		devctl, power;
	int		ep_num;
	u32		reg;

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

1559
	dev_dbg(musb->controller, "** IRQ %s usb%04x tx%04x rx%04x\n",
F
Felipe Balbi 已提交
1560 1561 1562
		(devctl & MUSB_DEVCTL_HM) ? "host" : "peripheral",
		musb->int_usb, musb->int_tx, musb->int_rx);

1563 1564 1565
#ifdef CONFIG_USB_GADGET_MUSB_HDRC
	if (is_otg_enabled(musb) || is_peripheral_enabled(musb))
		if (!musb->gadget_driver) {
1566
			dev_dbg(musb->controller, "No gadget driver loaded\n");
1567 1568 1569 1570
			return IRQ_HANDLED;
		}
#endif

F
Felipe Balbi 已提交
1571 1572 1573
	/* the core can interrupt us for multiple reasons; docs have
	 * a generic interrupt flowchart to follow
	 */
1574
	if (musb->int_usb)
F
Felipe Balbi 已提交
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
		retval |= musb_stage0_irq(musb, musb->int_usb,
				devctl, power);

	/* "stage 1" is handling endpoint irqs */

	/* handle endpoint 0 first */
	if (musb->int_tx & 1) {
		if (devctl & MUSB_DEVCTL_HM)
			retval |= musb_h_ep0_irq(musb);
		else
			retval |= musb_g_ep0_irq(musb);
	}

	/* RX on endpoints 1-15 */
	reg = musb->int_rx >> 1;
	ep_num = 1;
	while (reg) {
		if (reg & 1) {
			/* musb_ep_select(musb->mregs, ep_num); */
			/* REVISIT just retval = ep->rx_irq(...) */
			retval = IRQ_HANDLED;
			if (devctl & MUSB_DEVCTL_HM) {
				if (is_host_capable())
					musb_host_rx(musb, ep_num);
			} else {
				if (is_peripheral_capable())
					musb_g_rx(musb, ep_num);
			}
		}

		reg >>= 1;
		ep_num++;
	}

	/* TX on endpoints 1-15 */
	reg = musb->int_tx >> 1;
	ep_num = 1;
	while (reg) {
		if (reg & 1) {
			/* musb_ep_select(musb->mregs, ep_num); */
			/* REVISIT just retval |= ep->tx_irq(...) */
			retval = IRQ_HANDLED;
			if (devctl & MUSB_DEVCTL_HM) {
				if (is_host_capable())
					musb_host_tx(musb, ep_num);
			} else {
				if (is_peripheral_capable())
					musb_g_tx(musb, ep_num);
			}
		}
		reg >>= 1;
		ep_num++;
	}

	return retval;
}
1631
EXPORT_SYMBOL_GPL(musb_interrupt);
F
Felipe Balbi 已提交
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694

#ifndef CONFIG_MUSB_PIO_ONLY
static int __initdata use_dma = 1;

/* "modprobe ... use_dma=0" etc */
module_param(use_dma, bool, 0);
MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");

void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
{
	u8	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);

	/* called with controller lock already held */

	if (!epnum) {
#ifndef CONFIG_USB_TUSB_OMAP_DMA
		if (!is_cppi_enabled()) {
			/* endpoint 0 */
			if (devctl & MUSB_DEVCTL_HM)
				musb_h_ep0_irq(musb);
			else
				musb_g_ep0_irq(musb);
		}
#endif
	} else {
		/* endpoints 1..15 */
		if (transmit) {
			if (devctl & MUSB_DEVCTL_HM) {
				if (is_host_capable())
					musb_host_tx(musb, epnum);
			} else {
				if (is_peripheral_capable())
					musb_g_tx(musb, epnum);
			}
		} else {
			/* receive */
			if (devctl & MUSB_DEVCTL_HM) {
				if (is_host_capable())
					musb_host_rx(musb, epnum);
			} else {
				if (is_peripheral_capable())
					musb_g_rx(musb, epnum);
			}
		}
	}
}

#else
#define use_dma			0
#endif

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

#ifdef CONFIG_SYSFS

static ssize_t
musb_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct musb *musb = dev_to_musb(dev);
	unsigned long flags;
	int ret = -EINVAL;

	spin_lock_irqsave(&musb->lock, flags);
1695
	ret = sprintf(buf, "%s\n", otg_state_string(musb->xceiv->state));
F
Felipe Balbi 已提交
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
	spin_unlock_irqrestore(&musb->lock, flags);

	return ret;
}

static ssize_t
musb_mode_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t n)
{
	struct musb	*musb = dev_to_musb(dev);
	unsigned long	flags;
D
David Brownell 已提交
1707
	int		status;
F
Felipe Balbi 已提交
1708 1709

	spin_lock_irqsave(&musb->lock, flags);
D
David Brownell 已提交
1710 1711 1712 1713 1714 1715 1716 1717
	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 已提交
1718 1719
	spin_unlock_irqrestore(&musb->lock, flags);

D
David Brownell 已提交
1720
	return (status == 0) ? n : status;
F
Felipe Balbi 已提交
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
}
static DEVICE_ATTR(mode, 0644, musb_mode_show, musb_mode_store);

static ssize_t
musb_vbus_store(struct device *dev, struct device_attribute *attr,
		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) {
1733
		dev_err(dev, "Invalid VBUS timeout ms value\n");
F
Felipe Balbi 已提交
1734 1735 1736 1737
		return -EINVAL;
	}

	spin_lock_irqsave(&musb->lock, flags);
D
David Brownell 已提交
1738 1739
	/* 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 ;
1740
	if (musb->xceiv->state == OTG_STATE_A_WAIT_BCON)
F
Felipe Balbi 已提交
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757
		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
musb_vbus_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct musb	*musb = dev_to_musb(dev);
	unsigned long	flags;
	unsigned long	val;
	int		vbus;

	spin_lock_irqsave(&musb->lock, flags);
	val = musb->a_wait_bcon;
D
David Brownell 已提交
1758 1759 1760
	/* FIXME get_vbus_status() is normally #defined as false...
	 * and is effectively TUSB-specific.
	 */
F
Felipe Balbi 已提交
1761 1762 1763
	vbus = musb_platform_get_vbus_status(musb);
	spin_unlock_irqrestore(&musb->lock, flags);

D
David Brownell 已提交
1764
	return sprintf(buf, "Vbus %s, timeout %lu msec\n",
F
Felipe Balbi 已提交
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
			vbus ? "on" : "off", val);
}
static DEVICE_ATTR(vbus, 0644, musb_vbus_show, musb_vbus_store);

#ifdef CONFIG_USB_GADGET_MUSB_HDRC

/* 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.
 */
static ssize_t
musb_srp_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t n)
{
	struct musb	*musb = dev_to_musb(dev);
	unsigned short	srp;

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

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

	return n;
}
static DEVICE_ATTR(srp, 0644, NULL, musb_srp_store);

#endif /* CONFIG_USB_GADGET_MUSB_HDRC */

F
Felipe Balbi 已提交
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808
static struct attribute *musb_attributes[] = {
	&dev_attr_mode.attr,
	&dev_attr_vbus.attr,
#ifdef CONFIG_USB_GADGET_MUSB_HDRC
	&dev_attr_srp.attr,
#endif
	NULL
};

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

F
Felipe Balbi 已提交
1809 1810 1811 1812 1813 1814 1815 1816
#endif	/* sysfs */

/* Only used to provide driver mode change events */
static void musb_irq_work(struct work_struct *data)
{
	struct musb *musb = container_of(data, struct musb, irq_work);
	static int old_state;

1817 1818
	if (musb->xceiv->state != old_state) {
		old_state = musb->xceiv->state;
F
Felipe Balbi 已提交
1819 1820 1821 1822 1823 1824 1825 1826 1827
		sysfs_notify(&musb->controller->kobj, NULL, "mode");
	}
}

/* --------------------------------------------------------------------------
 * Init support
 */

static struct musb *__init
1828 1829
allocate_instance(struct device *dev,
		struct musb_hdrc_config *config, void __iomem *mbase)
F
Felipe Balbi 已提交
1830 1831 1832 1833 1834 1835 1836
{
	struct musb		*musb;
	struct musb_hw_ep	*ep;
	int			epnum;
#ifdef CONFIG_USB_MUSB_HDRC_HCD
	struct usb_hcd	*hcd;

1837
	hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
F
Felipe Balbi 已提交
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
	if (!hcd)
		return NULL;
	/* usbcore sets dev->driver_data to hcd, and sometimes uses that... */

	musb = hcd_to_musb(hcd);
	INIT_LIST_HEAD(&musb->control);
	INIT_LIST_HEAD(&musb->in_bulk);
	INIT_LIST_HEAD(&musb->out_bulk);

	hcd->uses_new_polling = 1;
F
Felipe Balbi 已提交
1848
	hcd->has_tt = 1;
F
Felipe Balbi 已提交
1849 1850

	musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
D
David Brownell 已提交
1851
	musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
F
Felipe Balbi 已提交
1852 1853 1854 1855 1856 1857
#else
	musb = kzalloc(sizeof *musb, GFP_KERNEL);
	if (!musb)
		return NULL;

#endif
1858
	dev_set_drvdata(dev, musb);
F
Felipe Balbi 已提交
1859 1860 1861
	musb->mregs = mbase;
	musb->ctrl_base = mbase;
	musb->nIrq = -ENODEV;
1862
	musb->config = config;
1863
	BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
F
Felipe Balbi 已提交
1864
	for (epnum = 0, ep = musb->endpoints;
1865
			epnum < musb->config->num_eps;
F
Felipe Balbi 已提交
1866 1867 1868 1869 1870 1871
			epnum++, ep++) {
		ep->musb = musb;
		ep->epnum = epnum;
	}

	musb->controller = dev;
1872

F
Felipe Balbi 已提交
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
	return musb;
}

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 已提交
1884
	sysfs_remove_group(&musb->controller->kobj, &musb_attr_group);
F
Felipe Balbi 已提交
1885 1886 1887 1888 1889 1890
#endif

#ifdef CONFIG_USB_GADGET_MUSB_HDRC
	musb_gadget_cleanup(musb);
#endif

A
Ajay Kumar Gupta 已提交
1891 1892 1893
	if (musb->nIrq >= 0) {
		if (musb->irq_wake)
			disable_irq_wake(musb->nIrq);
F
Felipe Balbi 已提交
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929
		free_irq(musb->nIrq, musb);
	}
	if (is_dma_capable() && musb->dma_controller) {
		struct dma_controller	*c = musb->dma_controller;

		(void) c->stop(c);
		dma_controller_destroy(c);
	}

#ifdef CONFIG_USB_MUSB_HDRC_HCD
	usb_put_hcd(musb_to_hcd(musb));
#else
	kfree(musb);
#endif
}

/*
 * Perform generic per-controller initialization.
 *
 * @pDevice: the controller (already clocked, etc)
 * @nIrq: irq
 * @mregs: virtual address of controller registers,
 *	not yet corrected for platform-specific offsets
 */
static int __init
musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
{
	int			status;
	struct musb		*musb;
	struct musb_hdrc_platform_data *plat = dev->platform_data;

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

F
Felipe Balbi 已提交
1934
	/* allocate */
1935
	musb = allocate_instance(dev, plat->config, ctrl);
1936 1937 1938 1939
	if (!musb) {
		status = -ENOMEM;
		goto fail0;
	}
F
Felipe Balbi 已提交
1940

1941 1942 1943 1944
	pm_runtime_use_autosuspend(musb->controller);
	pm_runtime_set_autosuspend_delay(musb->controller, 200);
	pm_runtime_enable(musb->controller);

F
Felipe Balbi 已提交
1945 1946 1947 1948
	spin_lock_init(&musb->lock);
	musb->board_mode = plat->mode;
	musb->board_set_power = plat->set_power;
	musb->min_power = plat->min_power;
1949
	musb->ops = plat->platform_ops;
F
Felipe Balbi 已提交
1950

1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
	/* The musb_platform_init() call:
	 *   - adjusts musb->mregs and musb->isr if needed,
	 *   - may initialize an integrated tranceiver
	 *   - initializes musb->xceiv, usually by otg_get_transceiver()
	 *   - stops powering VBUS
	 *
	 * There are various transciever configurations.  Blackfin,
	 * 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 已提交
1961 1962
	 */
	musb->isr = generic_interrupt;
1963
	status = musb_platform_init(musb);
F
Felipe Balbi 已提交
1964
	if (status < 0)
1965
		goto fail1;
1966

F
Felipe Balbi 已提交
1967 1968
	if (!musb->isr) {
		status = -ENODEV;
1969
		goto fail3;
F
Felipe Balbi 已提交
1970 1971
	}

1972 1973 1974 1975 1976
	if (!musb->xceiv->io_ops) {
		musb->xceiv->io_priv = musb->mregs;
		musb->xceiv->io_ops = &musb_ulpi_access;
	}

F
Felipe Balbi 已提交
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
#ifndef CONFIG_MUSB_PIO_ONLY
	if (use_dma && dev->dma_mask) {
		struct dma_controller	*c;

		c = dma_controller_create(musb, musb->mregs);
		musb->dma_controller = c;
		if (c)
			(void) c->start(c);
	}
#endif
	/* ideally this would be abstracted in platform setup */
	if (!is_dma_capable() || !musb->dma_controller)
		dev->dma_mask = NULL;

	/* be sure interrupts are disabled before connecting ISR */
	musb_platform_disable(musb);
	musb_generic_disable(musb);

	/* setup musb parts of the core (especially endpoints) */
1996
	status = musb_core_init(plat->config->multipoint
F
Felipe Balbi 已提交
1997 1998 1999
			? MUSB_CONTROLLER_MHDRC
			: MUSB_CONTROLLER_HDRC, musb);
	if (status < 0)
2000
		goto fail3;
F
Felipe Balbi 已提交
2001

2002
#ifdef CONFIG_USB_MUSB_OTG
D
David Brownell 已提交
2003 2004 2005
	setup_timer(&musb->otg_timer, musb_otg_timer_func, (unsigned long) musb);
#endif

F
Felipe Balbi 已提交
2006 2007 2008 2009
	/* Init IRQ workqueue before request_irq */
	INIT_WORK(&musb->irq_work, musb_irq_work);

	/* attach to the IRQ */
2010
	if (request_irq(nIrq, musb->isr, 0, dev_name(dev), musb)) {
F
Felipe Balbi 已提交
2011 2012
		dev_err(dev, "request_irq %d failed!\n", nIrq);
		status = -ENODEV;
2013
		goto fail3;
F
Felipe Balbi 已提交
2014 2015 2016
	}
	musb->nIrq = nIrq;
/* FIXME this handles wakeup irqs wrong */
2017 2018
	if (enable_irq_wake(nIrq) == 0) {
		musb->irq_wake = 1;
F
Felipe Balbi 已提交
2019
		device_init_wakeup(dev, 1);
2020 2021 2022
	} else {
		musb->irq_wake = 0;
	}
F
Felipe Balbi 已提交
2023

2024 2025
	/* host side needs more setup */
	if (is_host_enabled(musb)) {
F
Felipe Balbi 已提交
2026 2027
		struct usb_hcd	*hcd = musb_to_hcd(musb);

2028 2029 2030
		otg_set_host(musb->xceiv, &hcd->self);

		if (is_otg_enabled(musb))
F
Felipe Balbi 已提交
2031
			hcd->self.otg_port = 1;
2032
		musb->xceiv->host = &hcd->self;
F
Felipe Balbi 已提交
2033
		hcd->power_budget = 2 * (plat->power ? : 250);
2034 2035 2036

		/* program PHY to use external vBus if required */
		if (plat->extvbus) {
2037
			u8 busctl = musb_read_ulpi_buscontrol(musb->mregs);
2038
			busctl |= MUSB_ULPI_USE_EXTVBUS;
2039
			musb_write_ulpi_buscontrol(musb->mregs, busctl);
2040
		}
F
Felipe Balbi 已提交
2041 2042 2043 2044 2045 2046 2047
	}

	/* For the host-only role, we can activate right away.
	 * (We expect the ID pin to be forcibly grounded!!)
	 * Otherwise, wait till the gadget driver hooks up.
	 */
	if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
2048 2049
		struct usb_hcd	*hcd = musb_to_hcd(musb);

F
Felipe Balbi 已提交
2050
		MUSB_HST_MODE(musb);
2051 2052
		musb->xceiv->default_a = 1;
		musb->xceiv->state = OTG_STATE_A_IDLE;
F
Felipe Balbi 已提交
2053 2054 2055

		status = usb_add_hcd(musb_to_hcd(musb), -1, 0);

2056
		hcd->self.uses_pio_for_control = 1;
2057
		dev_dbg(musb->controller, "%s mode, status %d, devctl %02x %c\n",
F
Felipe Balbi 已提交
2058 2059 2060 2061 2062 2063 2064 2065
			"HOST", status,
			musb_readb(musb->mregs, MUSB_DEVCTL),
			(musb_readb(musb->mregs, MUSB_DEVCTL)
					& MUSB_DEVCTL_BDEVICE
				? 'B' : 'A'));

	} else /* peripheral is enabled */ {
		MUSB_DEV_MODE(musb);
2066 2067
		musb->xceiv->default_a = 0;
		musb->xceiv->state = OTG_STATE_B_IDLE;
F
Felipe Balbi 已提交
2068 2069 2070

		status = musb_gadget_setup(musb);

2071
		dev_dbg(musb->controller, "%s mode, status %d, dev%02x\n",
F
Felipe Balbi 已提交
2072 2073 2074 2075 2076
			is_otg_enabled(musb) ? "OTG" : "PERIPHERAL",
			status,
			musb_readb(musb->mregs, MUSB_DEVCTL));

	}
2077
	if (status < 0)
2078
		goto fail3;
F
Felipe Balbi 已提交
2079

2080 2081
	pm_runtime_put(musb->controller);

F
Felipe Balbi 已提交
2082 2083
	status = musb_init_debugfs(musb);
	if (status < 0)
2084
		goto fail4;
F
Felipe Balbi 已提交
2085

F
Felipe Balbi 已提交
2086
#ifdef CONFIG_SYSFS
F
Felipe Balbi 已提交
2087
	status = sysfs_create_group(&musb->controller->kobj, &musb_attr_group);
2088
	if (status)
2089
		goto fail5;
2090
#endif
F
Felipe Balbi 已提交
2091

2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
	dev_info(dev, "USB %s mode controller at %p using %s, IRQ %d\n",
			({char *s;
			 switch (musb->board_mode) {
			 case MUSB_HOST:		s = "Host"; break;
			 case MUSB_PERIPHERAL:	s = "Peripheral"; break;
			 default:		s = "OTG"; break;
			 }; s; }),
			ctrl,
			(is_dma_capable() && musb->dma_controller)
			? "DMA" : "PIO",
			musb->nIrq);

2104
	return 0;
F
Felipe Balbi 已提交
2105

2106 2107 2108
fail5:
	musb_exit_debugfs(musb);

2109 2110 2111 2112 2113 2114 2115 2116 2117
fail4:
	if (!is_otg_enabled(musb) && is_host_enabled(musb))
		usb_remove_hcd(musb_to_hcd(musb));
	else
		musb_gadget_cleanup(musb);

fail3:
	if (musb->irq_wake)
		device_init_wakeup(dev, 0);
F
Felipe Balbi 已提交
2118
	musb_platform_exit(musb);
2119

2120 2121 2122 2123
fail1:
	dev_err(musb->controller,
		"musb_init_controller failed with status %d\n", status);

2124 2125
	musb_free(musb);

2126 2127
fail0:

2128 2129
	return status;

F
Felipe Balbi 已提交
2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
}

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

/* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
 * bridge to a platform device; this driver then suffices.
 */

#ifndef CONFIG_MUSB_PIO_ONLY
static u64	*orig_dma_mask;
#endif

static int __init musb_probe(struct platform_device *pdev)
{
	struct device	*dev = &pdev->dev;
2145
	int		irq = platform_get_irq_byname(pdev, "mc");
2146
	int		status;
F
Felipe Balbi 已提交
2147 2148 2149 2150
	struct resource	*iomem;
	void __iomem	*base;

	iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
S
Sergei Shtylyov 已提交
2151
	if (!iomem || irq <= 0)
F
Felipe Balbi 已提交
2152 2153
		return -ENODEV;

F
Felipe Balbi 已提交
2154
	base = ioremap(iomem->start, resource_size(iomem));
F
Felipe Balbi 已提交
2155 2156 2157 2158 2159 2160 2161 2162 2163
	if (!base) {
		dev_err(dev, "ioremap failed\n");
		return -ENOMEM;
	}

#ifndef CONFIG_MUSB_PIO_ONLY
	/* clobbered by use_dma=n */
	orig_dma_mask = dev->dma_mask;
#endif
2164 2165 2166 2167 2168
	status = musb_init_controller(dev, irq, base);
	if (status < 0)
		iounmap(base);

	return status;
F
Felipe Balbi 已提交
2169 2170
}

2171
static int __exit musb_remove(struct platform_device *pdev)
F
Felipe Balbi 已提交
2172 2173 2174 2175 2176 2177 2178 2179 2180
{
	struct musb	*musb = dev_to_musb(&pdev->dev);
	void __iomem	*ctrl_base = musb->ctrl_base;

	/* 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)
	 */
2181
	pm_runtime_get_sync(musb->controller);
F
Felipe Balbi 已提交
2182
	musb_exit_debugfs(musb);
F
Felipe Balbi 已提交
2183
	musb_shutdown(pdev);
2184

2185
	pm_runtime_put(musb->controller);
F
Felipe Balbi 已提交
2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
	musb_free(musb);
	iounmap(ctrl_base);
	device_init_wakeup(&pdev->dev, 0);
#ifndef CONFIG_MUSB_PIO_ONLY
	pdev->dev.dma_mask = orig_dma_mask;
#endif
	return 0;
}

#ifdef	CONFIG_PM

2197
static void musb_save_context(struct musb *musb)
2198 2199 2200
{
	int i;
	void __iomem *musb_base = musb->mregs;
2201
	void __iomem *epio;
2202 2203

	if (is_host_enabled(musb)) {
2204 2205 2206
		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);
2207
	}
2208 2209 2210 2211 2212 2213
	musb->context.power = musb_readb(musb_base, MUSB_POWER);
	musb->context.intrtxe = musb_readw(musb_base, MUSB_INTRTXE);
	musb->context.intrrxe = musb_readw(musb_base, MUSB_INTRRXE);
	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);
2214

2215 2216
	for (i = 0; i < musb->config->num_eps; ++i) {
		epio = musb->endpoints[i].regs;
2217
		musb->context.index_regs[i].txmaxp =
2218
			musb_readw(epio, MUSB_TXMAXP);
2219
		musb->context.index_regs[i].txcsr =
2220
			musb_readw(epio, MUSB_TXCSR);
2221
		musb->context.index_regs[i].rxmaxp =
2222
			musb_readw(epio, MUSB_RXMAXP);
2223
		musb->context.index_regs[i].rxcsr =
2224
			musb_readw(epio, MUSB_RXCSR);
2225 2226

		if (musb->dyn_fifo) {
2227
			musb->context.index_regs[i].txfifoadd =
2228
					musb_read_txfifoadd(musb_base);
2229
			musb->context.index_regs[i].rxfifoadd =
2230
					musb_read_rxfifoadd(musb_base);
2231
			musb->context.index_regs[i].txfifosz =
2232
					musb_read_txfifosz(musb_base);
2233
			musb->context.index_regs[i].rxfifosz =
2234 2235 2236
					musb_read_rxfifosz(musb_base);
		}
		if (is_host_enabled(musb)) {
2237
			musb->context.index_regs[i].txtype =
2238
				musb_readb(epio, MUSB_TXTYPE);
2239
			musb->context.index_regs[i].txinterval =
2240
				musb_readb(epio, MUSB_TXINTERVAL);
2241
			musb->context.index_regs[i].rxtype =
2242
				musb_readb(epio, MUSB_RXTYPE);
2243
			musb->context.index_regs[i].rxinterval =
2244
				musb_readb(epio, MUSB_RXINTERVAL);
2245

2246
			musb->context.index_regs[i].txfunaddr =
2247
				musb_read_txfunaddr(musb_base, i);
2248
			musb->context.index_regs[i].txhubaddr =
2249
				musb_read_txhubaddr(musb_base, i);
2250
			musb->context.index_regs[i].txhubport =
2251 2252
				musb_read_txhubport(musb_base, i);

2253
			musb->context.index_regs[i].rxfunaddr =
2254
				musb_read_rxfunaddr(musb_base, i);
2255
			musb->context.index_regs[i].rxhubaddr =
2256
				musb_read_rxhubaddr(musb_base, i);
2257
			musb->context.index_regs[i].rxhubport =
2258 2259 2260 2261 2262
				musb_read_rxhubport(musb_base, i);
		}
	}
}

2263
static void musb_restore_context(struct musb *musb)
2264 2265 2266 2267
{
	int i;
	void __iomem *musb_base = musb->mregs;
	void __iomem *ep_target_regs;
2268
	void __iomem *epio;
2269 2270

	if (is_host_enabled(musb)) {
2271 2272 2273
		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);
2274
	}
2275 2276 2277 2278 2279
	musb_writeb(musb_base, MUSB_POWER, musb->context.power);
	musb_writew(musb_base, MUSB_INTRTXE, musb->context.intrtxe);
	musb_writew(musb_base, MUSB_INTRRXE, musb->context.intrrxe);
	musb_writeb(musb_base, MUSB_INTRUSBE, musb->context.intrusbe);
	musb_writeb(musb_base, MUSB_DEVCTL, musb->context.devctl);
2280

2281 2282 2283
	for (i = 0; i < musb->config->num_eps; ++i) {
		epio = musb->endpoints[i].regs;
		musb_writew(epio, MUSB_TXMAXP,
2284
			musb->context.index_regs[i].txmaxp);
2285
		musb_writew(epio, MUSB_TXCSR,
2286
			musb->context.index_regs[i].txcsr);
2287
		musb_writew(epio, MUSB_RXMAXP,
2288
			musb->context.index_regs[i].rxmaxp);
2289
		musb_writew(epio, MUSB_RXCSR,
2290
			musb->context.index_regs[i].rxcsr);
2291 2292 2293

		if (musb->dyn_fifo) {
			musb_write_txfifosz(musb_base,
2294
				musb->context.index_regs[i].txfifosz);
2295
			musb_write_rxfifosz(musb_base,
2296
				musb->context.index_regs[i].rxfifosz);
2297
			musb_write_txfifoadd(musb_base,
2298
				musb->context.index_regs[i].txfifoadd);
2299
			musb_write_rxfifoadd(musb_base,
2300
				musb->context.index_regs[i].rxfifoadd);
2301 2302 2303
		}

		if (is_host_enabled(musb)) {
2304
			musb_writeb(epio, MUSB_TXTYPE,
2305
				musb->context.index_regs[i].txtype);
2306
			musb_writeb(epio, MUSB_TXINTERVAL,
2307
				musb->context.index_regs[i].txinterval);
2308
			musb_writeb(epio, MUSB_RXTYPE,
2309
				musb->context.index_regs[i].rxtype);
2310
			musb_writeb(epio, MUSB_RXINTERVAL,
2311

2312
			musb->context.index_regs[i].rxinterval);
2313
			musb_write_txfunaddr(musb_base, i,
2314
				musb->context.index_regs[i].txfunaddr);
2315
			musb_write_txhubaddr(musb_base, i,
2316
				musb->context.index_regs[i].txhubaddr);
2317
			musb_write_txhubport(musb_base, i,
2318
				musb->context.index_regs[i].txhubport);
2319 2320 2321 2322 2323

			ep_target_regs =
				musb_read_target_reg_base(i, musb_base);

			musb_write_rxfunaddr(ep_target_regs,
2324
				musb->context.index_regs[i].rxfunaddr);
2325
			musb_write_rxhubaddr(ep_target_regs,
2326
				musb->context.index_regs[i].rxhubaddr);
2327
			musb_write_rxhubport(ep_target_regs,
2328
				musb->context.index_regs[i].rxhubport);
2329 2330 2331 2332
		}
	}
}

2333
static int musb_suspend(struct device *dev)
F
Felipe Balbi 已提交
2334
{
2335
	struct platform_device *pdev = to_platform_device(dev);
F
Felipe Balbi 已提交
2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350
	unsigned long	flags;
	struct musb	*musb = dev_to_musb(&pdev->dev);

	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.
		 */
	}

2351 2352
	musb_save_context(musb);

F
Felipe Balbi 已提交
2353 2354 2355 2356
	spin_unlock_irqrestore(&musb->lock, flags);
	return 0;
}

2357
static int musb_resume_noirq(struct device *dev)
F
Felipe Balbi 已提交
2358
{
2359
	struct platform_device *pdev = to_platform_device(dev);
F
Felipe Balbi 已提交
2360 2361
	struct musb	*musb = dev_to_musb(&pdev->dev);

2362 2363
	musb_restore_context(musb);

F
Felipe Balbi 已提交
2364
	/* for static cmos like DaVinci, register values were preserved
2365 2366
	 * unless for some reason the whole soc powered down or the USB
	 * module got reset through the PSC (vs just being disabled).
F
Felipe Balbi 已提交
2367 2368 2369 2370
	 */
	return 0;
}

2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400
static int musb_runtime_suspend(struct device *dev)
{
	struct musb	*musb = dev_to_musb(dev);

	musb_save_context(musb);

	return 0;
}

static int musb_runtime_resume(struct device *dev)
{
	struct musb	*musb = dev_to_musb(dev);
	static int	first = 1;

	/*
	 * 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
	 */
	if (!first)
		musb_restore_context(musb);
	first = 0;

	return 0;
}

2401
static const struct dev_pm_ops musb_dev_pm_ops = {
2402 2403
	.suspend	= musb_suspend,
	.resume_noirq	= musb_resume_noirq,
2404 2405
	.runtime_suspend = musb_runtime_suspend,
	.runtime_resume = musb_runtime_resume,
2406 2407 2408
};

#define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
F
Felipe Balbi 已提交
2409
#else
2410
#define	MUSB_DEV_PM_OPS	NULL
F
Felipe Balbi 已提交
2411 2412 2413 2414 2415 2416 2417
#endif

static struct platform_driver musb_driver = {
	.driver = {
		.name		= (char *)musb_driver_name,
		.bus		= &platform_bus_type,
		.owner		= THIS_MODULE,
2418
		.pm		= MUSB_DEV_PM_OPS,
F
Felipe Balbi 已提交
2419
	},
2420
	.remove		= __exit_p(musb_remove),
F
Felipe Balbi 已提交
2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441
	.shutdown	= musb_shutdown,
};

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

static int __init musb_init(void)
{
#ifdef CONFIG_USB_MUSB_HDRC_HCD
	if (usb_disabled())
		return 0;
#endif

	pr_info("%s: version " MUSB_VERSION ", "
#ifdef CONFIG_MUSB_PIO_ONLY
		"pio"
#elif defined(CONFIG_USB_TI_CPPI_DMA)
		"cppi-dma"
#elif defined(CONFIG_USB_INVENTRA_DMA)
		"musb-dma"
#elif defined(CONFIG_USB_TUSB_OMAP_DMA)
		"tusb-omap-dma"
2442 2443
#elif defined(CONFIG_USB_UX500_DMA)
		"ux500-dma"
F
Felipe Balbi 已提交
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454
#else
		"?dma?"
#endif
		", "
#ifdef CONFIG_USB_MUSB_OTG
		"otg (peripheral+host)"
#elif defined(CONFIG_USB_GADGET_MUSB_HDRC)
		"peripheral"
#elif defined(CONFIG_USB_MUSB_HDRC_HCD)
		"host"
#endif
2455 2456
		,
		musb_driver_name);
F
Felipe Balbi 已提交
2457 2458 2459
	return platform_driver_probe(&musb_driver, musb_probe);
}

2460 2461
/* make us init after usbcore and i2c (transceivers, regulators, etc)
 * and before usb gadget and host-side drivers start to register
F
Felipe Balbi 已提交
2462
 */
2463
fs_initcall(musb_init);
F
Felipe Balbi 已提交
2464 2465 2466 2467 2468 2469

static void __exit musb_cleanup(void)
{
	platform_driver_unregister(&musb_driver);
}
module_exit(musb_cleanup);