xref: /linux/drivers/usb/host/ehci-hcd.c (revision bf6387bcd16975ba8952b094f262a359d74e1c8a)
11da177e4SLinus Torvalds /*
2578333abSAlan Stern  * Enhanced Host Controller Interface (EHCI) driver for USB.
3578333abSAlan Stern  *
4578333abSAlan Stern  * Maintainer: Alan Stern <stern@rowland.harvard.edu>
5578333abSAlan Stern  *
61da177e4SLinus Torvalds  * Copyright (c) 2000-2004 by David Brownell
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify it
91da177e4SLinus Torvalds  * under the terms of the GNU General Public License as published by the
101da177e4SLinus Torvalds  * Free Software Foundation; either version 2 of the License, or (at your
111da177e4SLinus Torvalds  * option) any later version.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful, but
141da177e4SLinus Torvalds  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
151da177e4SLinus Torvalds  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
161da177e4SLinus Torvalds  * for more details.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
191da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software Foundation,
201da177e4SLinus Torvalds  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <linux/module.h>
241da177e4SLinus Torvalds #include <linux/pci.h>
251da177e4SLinus Torvalds #include <linux/dmapool.h>
261da177e4SLinus Torvalds #include <linux/kernel.h>
271da177e4SLinus Torvalds #include <linux/delay.h>
281da177e4SLinus Torvalds #include <linux/ioport.h>
291da177e4SLinus Torvalds #include <linux/sched.h>
303c04e20eSMing Lei #include <linux/vmalloc.h>
311da177e4SLinus Torvalds #include <linux/errno.h>
321da177e4SLinus Torvalds #include <linux/init.h>
33d58b4bccSAlan Stern #include <linux/hrtimer.h>
341da177e4SLinus Torvalds #include <linux/list.h>
351da177e4SLinus Torvalds #include <linux/interrupt.h>
361da177e4SLinus Torvalds #include <linux/usb.h>
3727729aadSEric Lescouet #include <linux/usb/hcd.h>
381da177e4SLinus Torvalds #include <linux/moduleparam.h>
391da177e4SLinus Torvalds #include <linux/dma-mapping.h>
40694cc208STony Jones #include <linux/debugfs.h>
415a0e3ad6STejun Heo #include <linux/slab.h>
42aa4d8342SAlek Du #include <linux/uaccess.h>
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include <asm/byteorder.h>
451da177e4SLinus Torvalds #include <asm/io.h>
461da177e4SLinus Torvalds #include <asm/irq.h>
471da177e4SLinus Torvalds #include <asm/unaligned.h>
481da177e4SLinus Torvalds 
49df7c1ca2SGeoff Levand #if defined(CONFIG_PPC_PS3)
50df7c1ca2SGeoff Levand #include <asm/firmware.h>
51df7c1ca2SGeoff Levand #endif
52df7c1ca2SGeoff Levand 
531da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds /*
561da177e4SLinus Torvalds  * EHCI hc_driver implementation ... experimental, incomplete.
571da177e4SLinus Torvalds  * Based on the final 1.0 register interface specification.
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  * USB 2.0 shows up in upcoming www.pcmcia.org technology.
601da177e4SLinus Torvalds  * First was PCMCIA, like ISA; then CardBus, which is PCI.
611da177e4SLinus Torvalds  * Next comes "CardBay", using USB 2.0 signals.
621da177e4SLinus Torvalds  *
631da177e4SLinus Torvalds  * Contains additional contributions by Brad Hards, Rory Bolt, and others.
641da177e4SLinus Torvalds  * Special thanks to Intel and VIA for providing host controllers to
651da177e4SLinus Torvalds  * test this driver on, and Cypress (including In-System Design) for
661da177e4SLinus Torvalds  * providing early devices for those host controllers to talk to!
671da177e4SLinus Torvalds  */
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #define DRIVER_AUTHOR "David Brownell"
701da177e4SLinus Torvalds #define DRIVER_DESC "USB 2.0 'Enhanced' Host Controller (EHCI) Driver"
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds static const char	hcd_name [] = "ehci_hcd";
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 
759776afc8SDavid Brownell #undef VERBOSE_DEBUG
761da177e4SLinus Torvalds #undef EHCI_URB_TRACE
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds #ifdef DEBUG
791da177e4SLinus Torvalds #define EHCI_STATS
801da177e4SLinus Torvalds #endif
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds /* magic numbers that can affect system performance */
831da177e4SLinus Torvalds #define	EHCI_TUNE_CERR		3	/* 0-3 qtd retries; 0 == don't stop */
841da177e4SLinus Torvalds #define	EHCI_TUNE_RL_HS		4	/* nak throttle; see 4.9 */
851da177e4SLinus Torvalds #define	EHCI_TUNE_RL_TT		0
861da177e4SLinus Torvalds #define	EHCI_TUNE_MULT_HS	1	/* 1-3 transactions/uframe; 4.10.3 */
871da177e4SLinus Torvalds #define	EHCI_TUNE_MULT_TT	1
88ffda0803SAlan Stern /*
89ffda0803SAlan Stern  * Some drivers think it's safe to schedule isochronous transfers more than
90ffda0803SAlan Stern  * 256 ms into the future (partly as a result of an old bug in the scheduling
91ffda0803SAlan Stern  * code).  In an attempt to avoid trouble, we will use a minimum scheduling
92ffda0803SAlan Stern  * length of 512 frames instead of 256.
93ffda0803SAlan Stern  */
94ffda0803SAlan Stern #define	EHCI_TUNE_FLS		1	/* (medium) 512-frame schedule */
951da177e4SLinus Torvalds 
9607d29b63SAlan Stern #define EHCI_IAA_MSECS		10		/* arbitrary */
971da177e4SLinus Torvalds #define EHCI_IO_JIFFIES		(HZ/10)		/* io watchdog > irq_thresh */
98004c1968SAlan Stern #define EHCI_SHRINK_JIFFIES	(DIV_ROUND_UP(HZ, 200) + 1)
99fcda37cbSMing Lei 						/* 5-ms async qh unlink delay */
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds /* Initial IRQ latency:  faster than hw default */
1021da177e4SLinus Torvalds static int log2_irq_thresh = 0;		// 0 to 6
1031da177e4SLinus Torvalds module_param (log2_irq_thresh, int, S_IRUGO);
1041da177e4SLinus Torvalds MODULE_PARM_DESC (log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /* initial park setting:  slower than hw default */
1071da177e4SLinus Torvalds static unsigned park = 0;
1081da177e4SLinus Torvalds module_param (park, uint, S_IRUGO);
1091da177e4SLinus Torvalds MODULE_PARM_DESC (park, "park setting; 1-3 back-to-back async packets");
1101da177e4SLinus Torvalds 
11193f1a47cSDavid Brownell /* for flakey hardware, ignore overcurrent indicators */
11290ab5ee9SRusty Russell static bool ignore_oc = 0;
11393f1a47cSDavid Brownell module_param (ignore_oc, bool, S_IRUGO);
11493f1a47cSDavid Brownell MODULE_PARM_DESC (ignore_oc, "ignore bogus hardware overcurrent indications");
11593f1a47cSDavid Brownell 
11648f24970SAlek Du /* for link power management(LPM) feature */
11748f24970SAlek Du static unsigned int hird;
11848f24970SAlek Du module_param(hird, int, S_IRUGO);
119cc556871SNiels de Vos MODULE_PARM_DESC(hird, "host initiated resume duration, +1 for each 75us");
12048f24970SAlek Du 
1211da177e4SLinus Torvalds #define	INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds #include "ehci.h"
1261da177e4SLinus Torvalds #include "ehci-dbg.c"
127ad93562bSAndiry Xu #include "pci-quirks.h"
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1301da177e4SLinus Torvalds 
131bc29847eSAlan Stern static void
132bc29847eSAlan Stern timer_action(struct ehci_hcd *ehci, enum ehci_timer_action action)
133bc29847eSAlan Stern {
134bc29847eSAlan Stern 	/* Don't override timeouts which shrink or (later) disable
135bc29847eSAlan Stern 	 * the async ring; just the I/O watchdog.  Note that if a
136bc29847eSAlan Stern 	 * SHRINK were pending, OFF would never be requested.
137bc29847eSAlan Stern 	 */
138bc29847eSAlan Stern 	if (timer_pending(&ehci->watchdog)
13931446610SAlan Stern 			&& (BIT(TIMER_ASYNC_SHRINK)
140bc29847eSAlan Stern 				& ehci->actions))
141bc29847eSAlan Stern 		return;
142bc29847eSAlan Stern 
143bc29847eSAlan Stern 	if (!test_and_set_bit(action, &ehci->actions)) {
144bc29847eSAlan Stern 		unsigned long t;
145bc29847eSAlan Stern 
146bc29847eSAlan Stern 		switch (action) {
147bc29847eSAlan Stern 		case TIMER_IO_WATCHDOG:
148403dbd36SAlek Du 			if (!ehci->need_io_watchdog)
149403dbd36SAlek Du 				return;
150bc29847eSAlan Stern 			t = EHCI_IO_JIFFIES;
151bc29847eSAlan Stern 			break;
152bc29847eSAlan Stern 		/* case TIMER_ASYNC_SHRINK: */
153bc29847eSAlan Stern 		default:
154004c1968SAlan Stern 			t = EHCI_SHRINK_JIFFIES;
155bc29847eSAlan Stern 			break;
156bc29847eSAlan Stern 		}
157bc29847eSAlan Stern 		mod_timer(&ehci->watchdog, t + jiffies);
158bc29847eSAlan Stern 	}
159bc29847eSAlan Stern }
160bc29847eSAlan Stern 
161bc29847eSAlan Stern /*-------------------------------------------------------------------------*/
162bc29847eSAlan Stern 
1631da177e4SLinus Torvalds /*
1641da177e4SLinus Torvalds  * handshake - spin reading hc until handshake completes or fails
1651da177e4SLinus Torvalds  * @ptr: address of hc register to be read
1661da177e4SLinus Torvalds  * @mask: bits to look at in result of read
1671da177e4SLinus Torvalds  * @done: value of those bits when handshake succeeds
1681da177e4SLinus Torvalds  * @usec: timeout in microseconds
1691da177e4SLinus Torvalds  *
1701da177e4SLinus Torvalds  * Returns negative errno, or zero on success
1711da177e4SLinus Torvalds  *
1721da177e4SLinus Torvalds  * Success happens when the "mask" bits have the specified value (hardware
1731da177e4SLinus Torvalds  * handshake done).  There are two failure modes:  "usec" have passed (major
1741da177e4SLinus Torvalds  * hardware flakeout), or the register reads as all-ones (hardware removed).
1751da177e4SLinus Torvalds  *
1761da177e4SLinus Torvalds  * That last failure should_only happen in cases like physical cardbus eject
1771da177e4SLinus Torvalds  * before driver shutdown. But it also seems to be caused by bugs in cardbus
1781da177e4SLinus Torvalds  * bridge shutdown:  shutting down the bridge before the devices using it.
1791da177e4SLinus Torvalds  */
180083522d7SBenjamin Herrenschmidt static int handshake (struct ehci_hcd *ehci, void __iomem *ptr,
181083522d7SBenjamin Herrenschmidt 		      u32 mask, u32 done, int usec)
1821da177e4SLinus Torvalds {
1831da177e4SLinus Torvalds 	u32	result;
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	do {
186083522d7SBenjamin Herrenschmidt 		result = ehci_readl(ehci, ptr);
1871da177e4SLinus Torvalds 		if (result == ~(u32)0)		/* card removed */
1881da177e4SLinus Torvalds 			return -ENODEV;
1891da177e4SLinus Torvalds 		result &= mask;
1901da177e4SLinus Torvalds 		if (result == done)
1911da177e4SLinus Torvalds 			return 0;
1921da177e4SLinus Torvalds 		udelay (1);
1931da177e4SLinus Torvalds 		usec--;
1941da177e4SLinus Torvalds 	} while (usec > 0);
1951da177e4SLinus Torvalds 	return -ETIMEDOUT;
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
19865fd4272SMatthieu CASTET /* check TDI/ARC silicon is in host mode */
19965fd4272SMatthieu CASTET static int tdi_in_host_mode (struct ehci_hcd *ehci)
20065fd4272SMatthieu CASTET {
20165fd4272SMatthieu CASTET 	u32		tmp;
20265fd4272SMatthieu CASTET 
203a46af4ebSAlan Stern 	tmp = ehci_readl(ehci, &ehci->regs->usbmode);
20465fd4272SMatthieu CASTET 	return (tmp & 3) == USBMODE_CM_HC;
20565fd4272SMatthieu CASTET }
20665fd4272SMatthieu CASTET 
2071da177e4SLinus Torvalds /* force HC to halt state from unknown (EHCI spec section 2.3) */
2081da177e4SLinus Torvalds static int ehci_halt (struct ehci_hcd *ehci)
2091da177e4SLinus Torvalds {
210083522d7SBenjamin Herrenschmidt 	u32	temp = ehci_readl(ehci, &ehci->regs->status);
2111da177e4SLinus Torvalds 
21272f30b6fSDavid Brownell 	/* disable any irqs left enabled by previous code */
213083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, 0, &ehci->regs->intr_enable);
21472f30b6fSDavid Brownell 
21565fd4272SMatthieu CASTET 	if (ehci_is_TDI(ehci) && tdi_in_host_mode(ehci) == 0) {
21665fd4272SMatthieu CASTET 		return 0;
21765fd4272SMatthieu CASTET 	}
21865fd4272SMatthieu CASTET 
2191da177e4SLinus Torvalds 	if ((temp & STS_HALT) != 0)
2201da177e4SLinus Torvalds 		return 0;
2211da177e4SLinus Torvalds 
2223d9545ccSAlan Stern 	/*
2233d9545ccSAlan Stern 	 * This routine gets called during probe before ehci->command
2243d9545ccSAlan Stern 	 * has been initialized, so we can't rely on its value.
2253d9545ccSAlan Stern 	 */
2263d9545ccSAlan Stern 	ehci->command &= ~CMD_RUN;
227083522d7SBenjamin Herrenschmidt 	temp = ehci_readl(ehci, &ehci->regs->command);
2283d9545ccSAlan Stern 	temp &= ~(CMD_RUN | CMD_IAAD);
229083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, temp, &ehci->regs->command);
230083522d7SBenjamin Herrenschmidt 	return handshake (ehci, &ehci->regs->status,
231083522d7SBenjamin Herrenschmidt 			  STS_HALT, STS_HALT, 16 * 125);
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds /* put TDI/ARC silicon into EHCI mode */
2351da177e4SLinus Torvalds static void tdi_reset (struct ehci_hcd *ehci)
2361da177e4SLinus Torvalds {
2371da177e4SLinus Torvalds 	u32		tmp;
2381da177e4SLinus Torvalds 
239a46af4ebSAlan Stern 	tmp = ehci_readl(ehci, &ehci->regs->usbmode);
240d23a1377SVladimir Barinov 	tmp |= USBMODE_CM_HC;
241d23a1377SVladimir Barinov 	/* The default byte access to MMR space is LE after
242d23a1377SVladimir Barinov 	 * controller reset. Set the required endian mode
243d23a1377SVladimir Barinov 	 * for transfer buffers to match the host microprocessor
244d23a1377SVladimir Barinov 	 */
245d23a1377SVladimir Barinov 	if (ehci_big_endian_mmio(ehci))
246d23a1377SVladimir Barinov 		tmp |= USBMODE_BE;
247a46af4ebSAlan Stern 	ehci_writel(ehci, tmp, &ehci->regs->usbmode);
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds /* reset a non-running (STS_HALT == 1) controller */
2511da177e4SLinus Torvalds static int ehci_reset (struct ehci_hcd *ehci)
2521da177e4SLinus Torvalds {
2531da177e4SLinus Torvalds 	int	retval;
254083522d7SBenjamin Herrenschmidt 	u32	command = ehci_readl(ehci, &ehci->regs->command);
2551da177e4SLinus Torvalds 
2568d053c79SJason Wessel 	/* If the EHCI debug controller is active, special care must be
2578d053c79SJason Wessel 	 * taken before and after a host controller reset */
2588d053c79SJason Wessel 	if (ehci->debug && !dbgp_reset_prep())
2598d053c79SJason Wessel 		ehci->debug = NULL;
2608d053c79SJason Wessel 
2611da177e4SLinus Torvalds 	command |= CMD_RESET;
2621da177e4SLinus Torvalds 	dbg_cmd (ehci, "reset", command);
263083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, command, &ehci->regs->command);
264e8799906SAlan Stern 	ehci->rh_state = EHCI_RH_HALTED;
2651da177e4SLinus Torvalds 	ehci->next_statechange = jiffies;
266083522d7SBenjamin Herrenschmidt 	retval = handshake (ehci, &ehci->regs->command,
267083522d7SBenjamin Herrenschmidt 			    CMD_RESET, 0, 250 * 1000);
2681da177e4SLinus Torvalds 
269331ac6b2SAlek Du 	if (ehci->has_hostpc) {
270331ac6b2SAlek Du 		ehci_writel(ehci, USBMODE_EX_HC | USBMODE_EX_VBPS,
271a46af4ebSAlan Stern 				&ehci->regs->usbmode_ex);
272a46af4ebSAlan Stern 		ehci_writel(ehci, TXFIFO_DEFAULT, &ehci->regs->txfill_tuning);
273331ac6b2SAlek Du 	}
2741da177e4SLinus Torvalds 	if (retval)
2751da177e4SLinus Torvalds 		return retval;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	if (ehci_is_TDI(ehci))
2781da177e4SLinus Torvalds 		tdi_reset (ehci);
2791da177e4SLinus Torvalds 
2808d053c79SJason Wessel 	if (ehci->debug)
2818d053c79SJason Wessel 		dbgp_external_startup();
2828d053c79SJason Wessel 
283a448e4dcSAlan Stern 	ehci->port_c_suspend = ehci->suspended_ports =
284a448e4dcSAlan Stern 			ehci->resuming_ports = 0;
2851da177e4SLinus Torvalds 	return retval;
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds /* idle the controller (from running) */
2891da177e4SLinus Torvalds static void ehci_quiesce (struct ehci_hcd *ehci)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	u32	temp;
2921da177e4SLinus Torvalds 
293e8799906SAlan Stern 	if (ehci->rh_state != EHCI_RH_RUNNING)
294c0c53dbcSAlan Stern 		return;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	/* wait for any schedule enables/disables to take effect */
2973d9545ccSAlan Stern 	temp = (ehci->command << 10) & (STS_ASS | STS_PSS);
2989671cd7aSAlan Stern 	handshake(ehci, &ehci->regs->status, STS_ASS | STS_PSS, temp, 16 * 125);
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	/* then disable anything that's still active */
3013d9545ccSAlan Stern 	ehci->command &= ~(CMD_ASE | CMD_PSE);
3023d9545ccSAlan Stern 	ehci_writel(ehci, ehci->command, &ehci->regs->command);
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	/* hardware can take 16 microframes to turn off ... */
3059671cd7aSAlan Stern 	handshake(ehci, &ehci->regs->status, STS_ASS | STS_PSS, 0, 16 * 125);
3061da177e4SLinus Torvalds }
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
3091da177e4SLinus Torvalds 
31007d29b63SAlan Stern static void end_unlink_async(struct ehci_hcd *ehci);
3117d12e780SDavid Howells static void ehci_work(struct ehci_hcd *ehci);
312df202255SAlan Stern static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh);
313df202255SAlan Stern static void end_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh);
3141da177e4SLinus Torvalds 
315d58b4bccSAlan Stern #include "ehci-timer.c"
3161da177e4SLinus Torvalds #include "ehci-hub.c"
31748f24970SAlek Du #include "ehci-lpm.c"
3181da177e4SLinus Torvalds #include "ehci-mem.c"
3191da177e4SLinus Torvalds #include "ehci-q.c"
3201da177e4SLinus Torvalds #include "ehci-sched.c"
3214c67045bSKirill Smelkov #include "ehci-sysfs.c"
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
3241da177e4SLinus Torvalds 
32507d29b63SAlan Stern static void ehci_iaa_watchdog(unsigned long param)
32607d29b63SAlan Stern {
32707d29b63SAlan Stern 	struct ehci_hcd		*ehci = (struct ehci_hcd *) param;
32807d29b63SAlan Stern 	unsigned long		flags;
32907d29b63SAlan Stern 
33007d29b63SAlan Stern 	spin_lock_irqsave (&ehci->lock, flags);
33107d29b63SAlan Stern 
332e82cc128SDavid Brownell 	/* Lost IAA irqs wedge things badly; seen first with a vt8235.
333e82cc128SDavid Brownell 	 * So we need this watchdog, but must protect it against both
334e82cc128SDavid Brownell 	 * (a) SMP races against real IAA firing and retriggering, and
335e82cc128SDavid Brownell 	 * (b) clean HC shutdown, when IAA watchdog was pending.
336e82cc128SDavid Brownell 	 */
33799ac5b1eSAlan Stern 	if (ehci->async_unlink
338e82cc128SDavid Brownell 			&& !timer_pending(&ehci->iaa_watchdog)
339e8799906SAlan Stern 			&& ehci->rh_state == EHCI_RH_RUNNING) {
340e82cc128SDavid Brownell 		u32 cmd, status;
341e82cc128SDavid Brownell 
342e82cc128SDavid Brownell 		/* If we get here, IAA is *REALLY* late.  It's barely
343e82cc128SDavid Brownell 		 * conceivable that the system is so busy that CMD_IAAD
344e82cc128SDavid Brownell 		 * is still legitimately set, so let's be sure it's
345e82cc128SDavid Brownell 		 * clear before we read STS_IAA.  (The HC should clear
346e82cc128SDavid Brownell 		 * CMD_IAAD when it sets STS_IAA.)
347e82cc128SDavid Brownell 		 */
34807d29b63SAlan Stern 		cmd = ehci_readl(ehci, &ehci->regs->command);
34907d29b63SAlan Stern 
350e82cc128SDavid Brownell 		/* If IAA is set here it either legitimately triggered
351e82cc128SDavid Brownell 		 * before we cleared IAAD above (but _way_ late, so we'll
352e82cc128SDavid Brownell 		 * still count it as lost) ... or a silicon erratum:
353e82cc128SDavid Brownell 		 * - VIA seems to set IAA without triggering the IRQ;
354e82cc128SDavid Brownell 		 * - IAAD potentially cleared without setting IAA.
355e82cc128SDavid Brownell 		 */
356e82cc128SDavid Brownell 		status = ehci_readl(ehci, &ehci->regs->status);
357e82cc128SDavid Brownell 		if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
35807d29b63SAlan Stern 			COUNT (ehci->stats.lost_iaa);
35907d29b63SAlan Stern 			ehci_writel(ehci, STS_IAA, &ehci->regs->status);
36007d29b63SAlan Stern 		}
361e82cc128SDavid Brownell 
362e82cc128SDavid Brownell 		ehci_vdbg(ehci, "IAA watchdog: status %x cmd %x\n",
363e82cc128SDavid Brownell 				status, cmd);
36407d29b63SAlan Stern 		end_unlink_async(ehci);
36507d29b63SAlan Stern 	}
36607d29b63SAlan Stern 
36707d29b63SAlan Stern 	spin_unlock_irqrestore(&ehci->lock, flags);
36807d29b63SAlan Stern }
36907d29b63SAlan Stern 
3701da177e4SLinus Torvalds static void ehci_watchdog(unsigned long param)
3711da177e4SLinus Torvalds {
3721da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = (struct ehci_hcd *) param;
3731da177e4SLinus Torvalds 	unsigned long		flags;
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	spin_lock_irqsave(&ehci->lock, flags);
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	/* ehci could run by timer, without IRQs ... */
3787d12e780SDavid Howells 	ehci_work (ehci);
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ehci->lock, flags);
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
3838903795aSAlan Stern /* On some systems, leaving remote wakeup enabled prevents system shutdown.
3848903795aSAlan Stern  * The firmware seems to think that powering off is a wakeup event!
3858903795aSAlan Stern  * This routine turns off remote wakeup and everything else, on all ports.
3868903795aSAlan Stern  */
3878903795aSAlan Stern static void ehci_turn_off_all_ports(struct ehci_hcd *ehci)
3888903795aSAlan Stern {
3898903795aSAlan Stern 	int	port = HCS_N_PORTS(ehci->hcs_params);
3908903795aSAlan Stern 
3918903795aSAlan Stern 	while (port--)
3928903795aSAlan Stern 		ehci_writel(ehci, PORT_RWC_BITS,
3938903795aSAlan Stern 				&ehci->regs->port_status[port]);
3948903795aSAlan Stern }
3958903795aSAlan Stern 
39621da84a8SSarah Sharp /*
39721da84a8SSarah Sharp  * Halt HC, turn off all ports, and let the BIOS use the companion controllers.
39821da84a8SSarah Sharp  * Should be called with ehci->lock held.
39972f30b6fSDavid Brownell  */
40021da84a8SSarah Sharp static void ehci_silence_controller(struct ehci_hcd *ehci)
4011da177e4SLinus Torvalds {
40221da84a8SSarah Sharp 	ehci_halt(ehci);
4038903795aSAlan Stern 	ehci_turn_off_all_ports(ehci);
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	/* make BIOS/etc use companion controller during reboot */
406083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, 0, &ehci->regs->configured_flag);
4078903795aSAlan Stern 
4088903795aSAlan Stern 	/* unblock posted writes */
4098903795aSAlan Stern 	ehci_readl(ehci, &ehci->regs->configured_flag);
4101da177e4SLinus Torvalds }
4111da177e4SLinus Torvalds 
41221da84a8SSarah Sharp /* ehci_shutdown kick in for silicon on any bus (not just pci, etc).
41321da84a8SSarah Sharp  * This forcibly disables dma and IRQs, helping kexec and other cases
41421da84a8SSarah Sharp  * where the next system software may expect clean state.
41521da84a8SSarah Sharp  */
41621da84a8SSarah Sharp static void ehci_shutdown(struct usb_hcd *hcd)
41721da84a8SSarah Sharp {
41821da84a8SSarah Sharp 	struct ehci_hcd	*ehci = hcd_to_ehci(hcd);
41921da84a8SSarah Sharp 
42021da84a8SSarah Sharp 	del_timer_sync(&ehci->watchdog);
42121da84a8SSarah Sharp 	del_timer_sync(&ehci->iaa_watchdog);
42221da84a8SSarah Sharp 
42321da84a8SSarah Sharp 	spin_lock_irq(&ehci->lock);
424c0c53dbcSAlan Stern 	ehci->rh_state = EHCI_RH_STOPPING;
42521da84a8SSarah Sharp 	ehci_silence_controller(ehci);
426d58b4bccSAlan Stern 	ehci->enabled_hrtimer_events = 0;
42721da84a8SSarah Sharp 	spin_unlock_irq(&ehci->lock);
428d58b4bccSAlan Stern 
429d58b4bccSAlan Stern 	hrtimer_cancel(&ehci->hrtimer);
43021da84a8SSarah Sharp }
43121da84a8SSarah Sharp 
43256c1e26dSDavid Brownell static void ehci_port_power (struct ehci_hcd *ehci, int is_on)
43356c1e26dSDavid Brownell {
43456c1e26dSDavid Brownell 	unsigned port;
43556c1e26dSDavid Brownell 
43656c1e26dSDavid Brownell 	if (!HCS_PPC (ehci->hcs_params))
43756c1e26dSDavid Brownell 		return;
43856c1e26dSDavid Brownell 
43956c1e26dSDavid Brownell 	ehci_dbg (ehci, "...power%s ports...\n", is_on ? "up" : "down");
44056c1e26dSDavid Brownell 	for (port = HCS_N_PORTS (ehci->hcs_params); port > 0; )
44156c1e26dSDavid Brownell 		(void) ehci_hub_control(ehci_to_hcd(ehci),
44256c1e26dSDavid Brownell 				is_on ? SetPortFeature : ClearPortFeature,
44356c1e26dSDavid Brownell 				USB_PORT_FEAT_POWER,
44456c1e26dSDavid Brownell 				port--, NULL, 0);
445383975d7SAlan Stern 	/* Flush those writes */
446383975d7SAlan Stern 	ehci_readl(ehci, &ehci->regs->command);
44756c1e26dSDavid Brownell 	msleep(20);
44856c1e26dSDavid Brownell }
44956c1e26dSDavid Brownell 
4507ff71d6aSMatt Porter /*-------------------------------------------------------------------------*/
4511da177e4SLinus Torvalds 
4527ff71d6aSMatt Porter /*
4537ff71d6aSMatt Porter  * ehci_work is called from some interrupts, timers, and so on.
4547ff71d6aSMatt Porter  * it calls driver completion functions, after dropping ehci->lock.
4557ff71d6aSMatt Porter  */
4567d12e780SDavid Howells static void ehci_work (struct ehci_hcd *ehci)
4577ff71d6aSMatt Porter {
4587ff71d6aSMatt Porter 	timer_action_done (ehci, TIMER_IO_WATCHDOG);
4591da177e4SLinus Torvalds 
4607ff71d6aSMatt Porter 	/* another CPU may drop ehci->lock during a schedule scan while
4617ff71d6aSMatt Porter 	 * it reports urb completions.  this flag guards against bogus
4627ff71d6aSMatt Porter 	 * attempts at re-entrant schedule scanning.
4637ff71d6aSMatt Porter 	 */
4647ff71d6aSMatt Porter 	if (ehci->scanning)
4657ff71d6aSMatt Porter 		return;
4667ff71d6aSMatt Porter 	ehci->scanning = 1;
46731446610SAlan Stern 	if (ehci->async_count)
4687d12e780SDavid Howells 		scan_async(ehci);
4697ff71d6aSMatt Porter 	if (ehci->next_uframe != -1)
4707d12e780SDavid Howells 		scan_periodic (ehci);
4717ff71d6aSMatt Porter 	ehci->scanning = 0;
4727ff71d6aSMatt Porter 
4737ff71d6aSMatt Porter 	/* the IO watchdog guards against hardware or driver bugs that
4747ff71d6aSMatt Porter 	 * misplace IRQs, and should let us run completely without IRQs.
4757ff71d6aSMatt Porter 	 * such lossage has been observed on both VT6202 and VT8235.
4767ff71d6aSMatt Porter 	 */
477e8799906SAlan Stern 	if (ehci->rh_state == EHCI_RH_RUNNING &&
4787ff71d6aSMatt Porter 			(ehci->async->qh_next.ptr != NULL ||
4793ca9aebaSAlan Stern 			 ehci->periodic_count != 0))
4807ff71d6aSMatt Porter 		timer_action (ehci, TIMER_IO_WATCHDOG);
4817ff71d6aSMatt Porter }
4827ff71d6aSMatt Porter 
48321da84a8SSarah Sharp /*
48421da84a8SSarah Sharp  * Called when the ehci_hcd module is removed.
48521da84a8SSarah Sharp  */
4867ff71d6aSMatt Porter static void ehci_stop (struct usb_hcd *hcd)
4871da177e4SLinus Torvalds {
4881da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
4891da177e4SLinus Torvalds 
4907ff71d6aSMatt Porter 	ehci_dbg (ehci, "stop\n");
4911da177e4SLinus Torvalds 
4927ff71d6aSMatt Porter 	/* no more interrupts ... */
4937ff71d6aSMatt Porter 	del_timer_sync (&ehci->watchdog);
49407d29b63SAlan Stern 	del_timer_sync(&ehci->iaa_watchdog);
4951da177e4SLinus Torvalds 
4967ff71d6aSMatt Porter 	spin_lock_irq(&ehci->lock);
497d58b4bccSAlan Stern 	ehci->enabled_hrtimer_events = 0;
4987ff71d6aSMatt Porter 	ehci_quiesce(ehci);
4991da177e4SLinus Torvalds 
50021da84a8SSarah Sharp 	ehci_silence_controller(ehci);
5017ff71d6aSMatt Porter 	ehci_reset (ehci);
5027ff71d6aSMatt Porter 	spin_unlock_irq(&ehci->lock);
5037ff71d6aSMatt Porter 
504d58b4bccSAlan Stern 	hrtimer_cancel(&ehci->hrtimer);
5054c67045bSKirill Smelkov 	remove_sysfs_files(ehci);
5067ff71d6aSMatt Porter 	remove_debug_files (ehci);
5077ff71d6aSMatt Porter 
5087ff71d6aSMatt Porter 	/* root hub is shut down separately (first, when possible) */
5097ff71d6aSMatt Porter 	spin_lock_irq (&ehci->lock);
5107ff71d6aSMatt Porter 	if (ehci->async)
5117d12e780SDavid Howells 		ehci_work (ehci);
5127ff71d6aSMatt Porter 	spin_unlock_irq (&ehci->lock);
5137ff71d6aSMatt Porter 	ehci_mem_cleanup (ehci);
5147ff71d6aSMatt Porter 
515ad93562bSAndiry Xu 	if (ehci->amd_pll_fix == 1)
516ad93562bSAndiry Xu 		usb_amd_dev_put();
51705570297SAlex He 
5187ff71d6aSMatt Porter #ifdef	EHCI_STATS
51999ac5b1eSAlan Stern 	ehci_dbg(ehci, "irq normal %ld err %ld iaa %ld (lost %ld)\n",
52099ac5b1eSAlan Stern 		ehci->stats.normal, ehci->stats.error, ehci->stats.iaa,
5217ff71d6aSMatt Porter 		ehci->stats.lost_iaa);
5227ff71d6aSMatt Porter 	ehci_dbg (ehci, "complete %ld unlink %ld\n",
5237ff71d6aSMatt Porter 		ehci->stats.complete, ehci->stats.unlink);
5241da177e4SLinus Torvalds #endif
5257ff71d6aSMatt Porter 
526083522d7SBenjamin Herrenschmidt 	dbg_status (ehci, "ehci_stop completed",
527083522d7SBenjamin Herrenschmidt 		    ehci_readl(ehci, &ehci->regs->status));
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
53018807521SDavid Brownell /* one-time init, only for memory state */
53118807521SDavid Brownell static int ehci_init(struct usb_hcd *hcd)
5321da177e4SLinus Torvalds {
5331da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
5341da177e4SLinus Torvalds 	u32			temp;
5351da177e4SLinus Torvalds 	int			retval;
5361da177e4SLinus Torvalds 	u32			hcc_params;
5373807e26dSAlek Du 	struct ehci_qh_hw	*hw;
5381da177e4SLinus Torvalds 
53918807521SDavid Brownell 	spin_lock_init(&ehci->lock);
54018807521SDavid Brownell 
541403dbd36SAlek Du 	/*
542403dbd36SAlek Du 	 * keep io watchdog by default, those good HCDs could turn off it later
543403dbd36SAlek Du 	 */
544403dbd36SAlek Du 	ehci->need_io_watchdog = 1;
5451da177e4SLinus Torvalds 	init_timer(&ehci->watchdog);
5461da177e4SLinus Torvalds 	ehci->watchdog.function = ehci_watchdog;
5471da177e4SLinus Torvalds 	ehci->watchdog.data = (unsigned long) ehci;
5481da177e4SLinus Torvalds 
54907d29b63SAlan Stern 	init_timer(&ehci->iaa_watchdog);
55007d29b63SAlan Stern 	ehci->iaa_watchdog.function = ehci_iaa_watchdog;
55107d29b63SAlan Stern 	ehci->iaa_watchdog.data = (unsigned long) ehci;
55207d29b63SAlan Stern 
553d58b4bccSAlan Stern 	hrtimer_init(&ehci->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
554d58b4bccSAlan Stern 	ehci->hrtimer.function = ehci_hrtimer_func;
555d58b4bccSAlan Stern 	ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
556d58b4bccSAlan Stern 
557f75593ceSAlan Stern 	hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
558f75593ceSAlan Stern 
5591da177e4SLinus Torvalds 	/*
560cc62a7ebSKirill Smelkov 	 * by default set standard 80% (== 100 usec/uframe) max periodic
561cc62a7ebSKirill Smelkov 	 * bandwidth as required by USB 2.0
562cc62a7ebSKirill Smelkov 	 */
563cc62a7ebSKirill Smelkov 	ehci->uframe_periodic_max = 100;
564cc62a7ebSKirill Smelkov 
565cc62a7ebSKirill Smelkov 	/*
5661da177e4SLinus Torvalds 	 * hw default: 1K periodic list heads, one per frame.
5671da177e4SLinus Torvalds 	 * periodic_size can shrink by USBCMD update if hcc_params allows.
5681da177e4SLinus Torvalds 	 */
5691da177e4SLinus Torvalds 	ehci->periodic_size = DEFAULT_I_TDPS;
5709aa09d2fSKarsten Wiese 	INIT_LIST_HEAD(&ehci->cached_itd_list);
5710e5f231bSAlan Stern 	INIT_LIST_HEAD(&ehci->cached_sitd_list);
572f75593ceSAlan Stern 
5738e192910SGreg Kroah-Hartman 	if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
574f75593ceSAlan Stern 		/* periodic schedule size can be smaller than default */
575f75593ceSAlan Stern 		switch (EHCI_TUNE_FLS) {
576f75593ceSAlan Stern 		case 0: ehci->periodic_size = 1024; break;
577f75593ceSAlan Stern 		case 1: ehci->periodic_size = 512; break;
578f75593ceSAlan Stern 		case 2: ehci->periodic_size = 256; break;
579f75593ceSAlan Stern 		default:	BUG();
580f75593ceSAlan Stern 		}
581f75593ceSAlan Stern 	}
58218807521SDavid Brownell 	if ((retval = ehci_mem_init(ehci, GFP_KERNEL)) < 0)
5831da177e4SLinus Torvalds 		return retval;
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	/* controllers may cache some of the periodic schedule ... */
5861da177e4SLinus Torvalds 	if (HCC_ISOC_CACHE(hcc_params))		// full frame cache
587dccd574cSSarah Sharp 		ehci->i_thresh = 2 + 8;
5881da177e4SLinus Torvalds 	else					// N microframes cached
5891da177e4SLinus Torvalds 		ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	ehci->next_uframe = -1;
5929aa09d2fSKarsten Wiese 	ehci->clock_frame = -1;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	/*
5951da177e4SLinus Torvalds 	 * dedicate a qh for the async ring head, since we couldn't unlink
5961da177e4SLinus Torvalds 	 * a 'real' qh without stopping the async schedule [4.8].  use it
5971da177e4SLinus Torvalds 	 * as the 'reclamation list head' too.
5981da177e4SLinus Torvalds 	 * its dummy is used in hw_alt_next of many tds, to prevent the qh
5991da177e4SLinus Torvalds 	 * from automatically advancing to the next td after short reads.
6001da177e4SLinus Torvalds 	 */
6011da177e4SLinus Torvalds 	ehci->async->qh_next.qh = NULL;
6023807e26dSAlek Du 	hw = ehci->async->hw;
6033807e26dSAlek Du 	hw->hw_next = QH_NEXT(ehci, ehci->async->qh_dma);
6043807e26dSAlek Du 	hw->hw_info1 = cpu_to_hc32(ehci, QH_HEAD);
6054f7a67e2SRicardo Martins #if defined(CONFIG_PPC_PS3)
6064c53de72SAlan Stern 	hw->hw_info1 |= cpu_to_hc32(ehci, QH_INACTIVATE);
6074f7a67e2SRicardo Martins #endif
6083807e26dSAlek Du 	hw->hw_token = cpu_to_hc32(ehci, QTD_STS_HALT);
6093807e26dSAlek Du 	hw->hw_qtd_next = EHCI_LIST_END(ehci);
6101da177e4SLinus Torvalds 	ehci->async->qh_state = QH_STATE_LINKED;
6113807e26dSAlek Du 	hw->hw_alt_next = QTD_NEXT(ehci, ehci->async->dummy->qtd_dma);
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	/* clear interrupt enables, set irq latency */
6141da177e4SLinus Torvalds 	if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
6151da177e4SLinus Torvalds 		log2_irq_thresh = 0;
6161da177e4SLinus Torvalds 	temp = 1 << (16 + log2_irq_thresh);
6175a9cdf33SAlek Du 	if (HCC_PER_PORT_CHANGE_EVENT(hcc_params)) {
6185a9cdf33SAlek Du 		ehci->has_ppcd = 1;
6195a9cdf33SAlek Du 		ehci_dbg(ehci, "enable per-port change event\n");
6205a9cdf33SAlek Du 		temp |= CMD_PPCEE;
6215a9cdf33SAlek Du 	}
6221da177e4SLinus Torvalds 	if (HCC_CANPARK(hcc_params)) {
6231da177e4SLinus Torvalds 		/* HW default park == 3, on hardware that supports it (like
6241da177e4SLinus Torvalds 		 * NVidia and ALI silicon), maximizes throughput on the async
6251da177e4SLinus Torvalds 		 * schedule by avoiding QH fetches between transfers.
6261da177e4SLinus Torvalds 		 *
6271da177e4SLinus Torvalds 		 * With fast usb storage devices and NForce2, "park" seems to
6281da177e4SLinus Torvalds 		 * make problems:  throughput reduction (!), data errors...
6291da177e4SLinus Torvalds 		 */
6301da177e4SLinus Torvalds 		if (park) {
6311da177e4SLinus Torvalds 			park = min(park, (unsigned) 3);
6321da177e4SLinus Torvalds 			temp |= CMD_PARK;
6331da177e4SLinus Torvalds 			temp |= park << 8;
6341da177e4SLinus Torvalds 		}
63518807521SDavid Brownell 		ehci_dbg(ehci, "park %d\n", park);
6361da177e4SLinus Torvalds 	}
6371da177e4SLinus Torvalds 	if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
6381da177e4SLinus Torvalds 		/* periodic schedule size can be smaller than default */
6391da177e4SLinus Torvalds 		temp &= ~(3 << 2);
6401da177e4SLinus Torvalds 		temp |= (EHCI_TUNE_FLS << 2);
6411da177e4SLinus Torvalds 	}
64248f24970SAlek Du 	if (HCC_LPM(hcc_params)) {
64348f24970SAlek Du 		/* support link power management EHCI 1.1 addendum */
64448f24970SAlek Du 		ehci_dbg(ehci, "support lpm\n");
64548f24970SAlek Du 		ehci->has_lpm = 1;
64648f24970SAlek Du 		if (hird > 0xf) {
64748f24970SAlek Du 			ehci_dbg(ehci, "hird %d invalid, use default 0",
64848f24970SAlek Du 			hird);
64948f24970SAlek Du 			hird = 0;
65048f24970SAlek Du 		}
65148f24970SAlek Du 		temp |= hird << 24;
65248f24970SAlek Du 	}
65318807521SDavid Brownell 	ehci->command = temp;
65418807521SDavid Brownell 
65540f8db8fSAlan Stern 	/* Accept arbitrarily long scatter-gather lists */
6564307a28eSAndrea Righi 	if (!(hcd->driver->flags & HCD_LOCAL_MEM))
65740f8db8fSAlan Stern 		hcd->self.sg_tablesize = ~0;
65818807521SDavid Brownell 	return 0;
65918807521SDavid Brownell }
66018807521SDavid Brownell 
66118807521SDavid Brownell /* start HC running; it's halted, ehci_init() has been run (once) */
66218807521SDavid Brownell static int ehci_run (struct usb_hcd *hcd)
66318807521SDavid Brownell {
66418807521SDavid Brownell 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
66518807521SDavid Brownell 	u32			temp;
66618807521SDavid Brownell 	u32			hcc_params;
66718807521SDavid Brownell 
6681d619f12SMarcelo Tosatti 	hcd->uses_new_polling = 1;
6691d619f12SMarcelo Tosatti 
67018807521SDavid Brownell 	/* EHCI spec section 4.1 */
671876e0df9SGeoff Levand 
672083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
673083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
67418807521SDavid Brownell 
67518807521SDavid Brownell 	/*
67618807521SDavid Brownell 	 * hcc_params controls whether ehci->regs->segment must (!!!)
67718807521SDavid Brownell 	 * be used; it constrains QH/ITD/SITD and QTD locations.
67818807521SDavid Brownell 	 * pci_pool consistent memory always uses segment zero.
67918807521SDavid Brownell 	 * streaming mappings for I/O buffers, like pci_map_single(),
68018807521SDavid Brownell 	 * can return segments above 4GB, if the device allows.
68118807521SDavid Brownell 	 *
68218807521SDavid Brownell 	 * NOTE:  the dma mask is visible through dma_supported(), so
68318807521SDavid Brownell 	 * drivers can pass this info along ... like NETIF_F_HIGHDMA,
68418807521SDavid Brownell 	 * Scsi_Host.highmem_io, and so forth.  It's readonly to all
68518807521SDavid Brownell 	 * host side drivers though.
68618807521SDavid Brownell 	 */
687083522d7SBenjamin Herrenschmidt 	hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
68818807521SDavid Brownell 	if (HCC_64BIT_ADDR(hcc_params)) {
689083522d7SBenjamin Herrenschmidt 		ehci_writel(ehci, 0, &ehci->regs->segment);
69018807521SDavid Brownell #if 0
69118807521SDavid Brownell // this is deeply broken on almost all architectures
6926a35528aSYang Hongyang 		if (!dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64)))
69318807521SDavid Brownell 			ehci_info(ehci, "enabled 64bit DMA\n");
69418807521SDavid Brownell #endif
69518807521SDavid Brownell 	}
69618807521SDavid Brownell 
69718807521SDavid Brownell 
6981da177e4SLinus Torvalds 	// Philips, Intel, and maybe others need CMD_RUN before the
6991da177e4SLinus Torvalds 	// root hub will detect new devices (why?); NEC doesn't
70018807521SDavid Brownell 	ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
70118807521SDavid Brownell 	ehci->command |= CMD_RUN;
702083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, ehci->command, &ehci->regs->command);
70318807521SDavid Brownell 	dbg_cmd (ehci, "init", ehci->command);
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	/*
7061da177e4SLinus Torvalds 	 * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
7071da177e4SLinus Torvalds 	 * are explicitly handed to companion controller(s), so no TT is
7081da177e4SLinus Torvalds 	 * involved with the root hub.  (Except where one is integrated,
7091da177e4SLinus Torvalds 	 * and there's no companion controller unless maybe for USB OTG.)
71032fe0198SAlan Stern 	 *
71132fe0198SAlan Stern 	 * Turning on the CF flag will transfer ownership of all ports
71232fe0198SAlan Stern 	 * from the companions to the EHCI controller.  If any of the
71332fe0198SAlan Stern 	 * companions are in the middle of a port reset at the time, it
71432fe0198SAlan Stern 	 * could cause trouble.  Write-locking ehci_cf_port_reset_rwsem
7151cb52658SDavid Brownell 	 * guarantees that no resets are in progress.  After we set CF,
7161cb52658SDavid Brownell 	 * a short delay lets the hardware catch up; new resets shouldn't
7171cb52658SDavid Brownell 	 * be started before the port switching actions could complete.
7181da177e4SLinus Torvalds 	 */
71932fe0198SAlan Stern 	down_write(&ehci_cf_port_reset_rwsem);
720e8799906SAlan Stern 	ehci->rh_state = EHCI_RH_RUNNING;
721083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
722083522d7SBenjamin Herrenschmidt 	ehci_readl(ehci, &ehci->regs->command);	/* unblock posted writes */
7231cb52658SDavid Brownell 	msleep(5);
72432fe0198SAlan Stern 	up_write(&ehci_cf_port_reset_rwsem);
725ee4ecb8aSOliver Neukum 	ehci->last_periodic_enable = ktime_get_real();
7261da177e4SLinus Torvalds 
727c430131aSJan Andersson 	temp = HC_VERSION(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
7281da177e4SLinus Torvalds 	ehci_info (ehci,
7292b70f073SAlan Stern 		"USB %x.%x started, EHCI %x.%02x%s\n",
7307ff71d6aSMatt Porter 		((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f),
7312b70f073SAlan Stern 		temp >> 8, temp & 0xff,
73293f1a47cSDavid Brownell 		ignore_oc ? ", overcurrent ignored" : "");
7331da177e4SLinus Torvalds 
734083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, INTR_MASK,
735083522d7SBenjamin Herrenschmidt 		    &ehci->regs->intr_enable); /* Turn On Interrupts */
7361da177e4SLinus Torvalds 
73718807521SDavid Brownell 	/* GRR this is run-once init(), being done every time the HC starts.
73818807521SDavid Brownell 	 * So long as they're part of class devices, we can't do it init()
73918807521SDavid Brownell 	 * since the class device isn't created that early.
74018807521SDavid Brownell 	 */
7411da177e4SLinus Torvalds 	create_debug_files(ehci);
7424c67045bSKirill Smelkov 	create_sysfs_files(ehci);
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	return 0;
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
7471a49e2acSAlan Stern static int ehci_setup(struct usb_hcd *hcd)
7482093c6b4SMatthieu CASTET {
7492093c6b4SMatthieu CASTET 	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
7502093c6b4SMatthieu CASTET 	int retval;
7512093c6b4SMatthieu CASTET 
7522093c6b4SMatthieu CASTET 	ehci->regs = (void __iomem *)ehci->caps +
7532093c6b4SMatthieu CASTET 	    HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
7542093c6b4SMatthieu CASTET 	dbg_hcs_params(ehci, "reset");
7552093c6b4SMatthieu CASTET 	dbg_hcc_params(ehci, "reset");
7562093c6b4SMatthieu CASTET 
7572093c6b4SMatthieu CASTET 	/* cache this readonly data; minimize chip reads */
7582093c6b4SMatthieu CASTET 	ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
7592093c6b4SMatthieu CASTET 
7602093c6b4SMatthieu CASTET 	ehci->sbrn = HCD_USB2;
7612093c6b4SMatthieu CASTET 
762631fe9d9SAlan Stern 	/* data structure init */
763631fe9d9SAlan Stern 	retval = ehci_init(hcd);
7642093c6b4SMatthieu CASTET 	if (retval)
7652093c6b4SMatthieu CASTET 		return retval;
7662093c6b4SMatthieu CASTET 
767631fe9d9SAlan Stern 	retval = ehci_halt(ehci);
7682093c6b4SMatthieu CASTET 	if (retval)
7692093c6b4SMatthieu CASTET 		return retval;
7702093c6b4SMatthieu CASTET 
7711a49e2acSAlan Stern 	if (ehci_is_TDI(ehci))
7721a49e2acSAlan Stern 		tdi_reset(ehci);
7731a49e2acSAlan Stern 
7742093c6b4SMatthieu CASTET 	ehci_reset(ehci);
7752093c6b4SMatthieu CASTET 
7762093c6b4SMatthieu CASTET 	return 0;
7772093c6b4SMatthieu CASTET }
7782093c6b4SMatthieu CASTET 
7791da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
7801da177e4SLinus Torvalds 
7817d12e780SDavid Howells static irqreturn_t ehci_irq (struct usb_hcd *hcd)
7821da177e4SLinus Torvalds {
7831da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
78467b2e029SAlan Stern 	u32			status, masked_status, pcd_status = 0, cmd;
7851da177e4SLinus Torvalds 	int			bh;
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	spin_lock (&ehci->lock);
7881da177e4SLinus Torvalds 
789083522d7SBenjamin Herrenschmidt 	status = ehci_readl(ehci, &ehci->regs->status);
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	/* e.g. cardbus physical eject */
7921da177e4SLinus Torvalds 	if (status == ~(u32) 0) {
7931da177e4SLinus Torvalds 		ehci_dbg (ehci, "device removed\n");
7941da177e4SLinus Torvalds 		goto dead;
7951da177e4SLinus Torvalds 	}
7961da177e4SLinus Torvalds 
7972fbe2bf1SAlan Stern 	/*
7982fbe2bf1SAlan Stern 	 * We don't use STS_FLR, but some controllers don't like it to
7992fbe2bf1SAlan Stern 	 * remain on, so mask it out along with the other status bits.
8002fbe2bf1SAlan Stern 	 */
8012fbe2bf1SAlan Stern 	masked_status = status & (INTR_MASK | STS_FLR);
8022fbe2bf1SAlan Stern 
80369fff59dSAlan Stern 	/* Shared IRQ? */
804e8799906SAlan Stern 	if (!masked_status || unlikely(ehci->rh_state == EHCI_RH_HALTED)) {
8051da177e4SLinus Torvalds 		spin_unlock(&ehci->lock);
8061da177e4SLinus Torvalds 		return IRQ_NONE;
8071da177e4SLinus Torvalds 	}
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	/* clear (just) interrupts */
81067b2e029SAlan Stern 	ehci_writel(ehci, masked_status, &ehci->regs->status);
811e82cc128SDavid Brownell 	cmd = ehci_readl(ehci, &ehci->regs->command);
8121da177e4SLinus Torvalds 	bh = 0;
8131da177e4SLinus Torvalds 
8149776afc8SDavid Brownell #ifdef	VERBOSE_DEBUG
8151da177e4SLinus Torvalds 	/* unrequested/ignored: Frame List Rollover */
8161da177e4SLinus Torvalds 	dbg_status (ehci, "irq", status);
8171da177e4SLinus Torvalds #endif
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	/* INT, ERR, and IAA interrupt rates can be throttled */
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	/* normal [4.15.1.2] or error [4.15.1.1] completion */
8221da177e4SLinus Torvalds 	if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
8231da177e4SLinus Torvalds 		if (likely ((status & STS_ERR) == 0))
8241da177e4SLinus Torvalds 			COUNT (ehci->stats.normal);
8251da177e4SLinus Torvalds 		else
8261da177e4SLinus Torvalds 			COUNT (ehci->stats.error);
8271da177e4SLinus Torvalds 		bh = 1;
8281da177e4SLinus Torvalds 	}
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	/* complete the unlinking of some qh [4.15.2.3] */
8311da177e4SLinus Torvalds 	if (status & STS_IAA) {
832e82cc128SDavid Brownell 		/* guard against (alleged) silicon errata */
8336feff1b9SAlan Stern 		if (cmd & CMD_IAAD)
834e82cc128SDavid Brownell 			ehci_dbg(ehci, "IAA with IAAD still set?\n");
83599ac5b1eSAlan Stern 		if (ehci->async_unlink) {
83699ac5b1eSAlan Stern 			COUNT(ehci->stats.iaa);
83707d29b63SAlan Stern 			end_unlink_async(ehci);
838e82cc128SDavid Brownell 		} else
83999ac5b1eSAlan Stern 			ehci_dbg(ehci, "IAA with nothing unlinked?\n");
8401da177e4SLinus Torvalds 	}
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	/* remote wakeup [4.3.1] */
843d97cc2f2SDavid Brownell 	if (status & STS_PCD) {
8441da177e4SLinus Torvalds 		unsigned	i = HCS_N_PORTS (ehci->hcs_params);
8455a9cdf33SAlek Du 		u32		ppcd = 0;
846d1b1842cSDavid Brownell 
847d1b1842cSDavid Brownell 		/* kick root hub later */
8481d619f12SMarcelo Tosatti 		pcd_status = status;
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 		/* resume root hub? */
851dc75ce9dSAlan Stern 		if (ehci->rh_state == EHCI_RH_SUSPENDED)
8528c03356aSAlan Stern 			usb_hcd_resume_root_hub(hcd);
8531da177e4SLinus Torvalds 
8545a9cdf33SAlek Du 		/* get per-port change detect bits */
8555a9cdf33SAlek Du 		if (ehci->has_ppcd)
8565a9cdf33SAlek Du 			ppcd = status >> 16;
8575a9cdf33SAlek Du 
8581da177e4SLinus Torvalds 		while (i--) {
8595a9cdf33SAlek Du 			int pstatus;
8605a9cdf33SAlek Du 
8615a9cdf33SAlek Du 			/* leverage per-port change bits feature */
8625a9cdf33SAlek Du 			if (ehci->has_ppcd && !(ppcd & (1 << i)))
8635a9cdf33SAlek Du 				continue;
8645a9cdf33SAlek Du 			pstatus = ehci_readl(ehci,
865083522d7SBenjamin Herrenschmidt 					 &ehci->regs->port_status[i]);
866b972b68cSDavid Brownell 
867b972b68cSDavid Brownell 			if (pstatus & PORT_OWNER)
8681da177e4SLinus Torvalds 				continue;
869eafe5b99SAlan Stern 			if (!(test_bit(i, &ehci->suspended_ports) &&
870eafe5b99SAlan Stern 					((pstatus & PORT_RESUME) ||
871eafe5b99SAlan Stern 						!(pstatus & PORT_SUSPEND)) &&
872eafe5b99SAlan Stern 					(pstatus & PORT_PE) &&
873eafe5b99SAlan Stern 					ehci->reset_done[i] == 0))
8741da177e4SLinus Torvalds 				continue;
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 			/* start 20 msec resume signaling from this port,
8771da177e4SLinus Torvalds 			 * and make khubd collect PORT_STAT_C_SUSPEND to
87849d0f078SAlan Stern 			 * stop that signaling.  Use 5 ms extra for safety,
87949d0f078SAlan Stern 			 * like usb_port_resume() does.
8801da177e4SLinus Torvalds 			 */
88149d0f078SAlan Stern 			ehci->reset_done[i] = jiffies + msecs_to_jiffies(25);
882a448e4dcSAlan Stern 			set_bit(i, &ehci->resuming_ports);
8831da177e4SLinus Torvalds 			ehci_dbg (ehci, "port %d remote wakeup\n", i + 1);
88461e8b858SAlan Stern 			mod_timer(&hcd->rh_timer, ehci->reset_done[i]);
8851da177e4SLinus Torvalds 		}
8861da177e4SLinus Torvalds 	}
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	/* PCI errors [4.15.2.4] */
8891da177e4SLinus Torvalds 	if (unlikely ((status & STS_FATAL) != 0)) {
89067b2e029SAlan Stern 		ehci_err(ehci, "fatal error\n");
891eafe5b99SAlan Stern 		dbg_cmd(ehci, "fatal", cmd);
8921da177e4SLinus Torvalds 		dbg_status(ehci, "fatal", status);
8931da177e4SLinus Torvalds dead:
89469fff59dSAlan Stern 		usb_hc_died(hcd);
895*bf6387bcSAlan Stern 
896*bf6387bcSAlan Stern 		/* Don't let the controller do anything more */
897*bf6387bcSAlan Stern 		ehci->rh_state = EHCI_RH_STOPPING;
898*bf6387bcSAlan Stern 		ehci->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE);
899*bf6387bcSAlan Stern 		ehci_writel(ehci, ehci->command, &ehci->regs->command);
900*bf6387bcSAlan Stern 		ehci_writel(ehci, 0, &ehci->regs->intr_enable);
901*bf6387bcSAlan Stern 		ehci_handle_controller_death(ehci);
902*bf6387bcSAlan Stern 
903*bf6387bcSAlan Stern 		/* Handle completions when the controller stops */
904*bf6387bcSAlan Stern 		bh = 0;
9051da177e4SLinus Torvalds 	}
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	if (bh)
9087d12e780SDavid Howells 		ehci_work (ehci);
9091da177e4SLinus Torvalds 	spin_unlock (&ehci->lock);
910d1b1842cSDavid Brownell 	if (pcd_status)
9111d619f12SMarcelo Tosatti 		usb_hcd_poll_rh_status(hcd);
9121da177e4SLinus Torvalds 	return IRQ_HANDLED;
9131da177e4SLinus Torvalds }
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds /*
9181da177e4SLinus Torvalds  * non-error returns are a promise to giveback() the urb later
9191da177e4SLinus Torvalds  * we drop ownership so next owner (or urb unlink) can get it
9201da177e4SLinus Torvalds  *
9211da177e4SLinus Torvalds  * urb + dev is in hcd.self.controller.urb_list
9221da177e4SLinus Torvalds  * we're queueing TDs onto software and hardware lists
9231da177e4SLinus Torvalds  *
9241da177e4SLinus Torvalds  * hcd-specific init for hcpriv hasn't been done yet
9251da177e4SLinus Torvalds  *
9261da177e4SLinus Torvalds  * NOTE:  control, bulk, and interrupt share the same code to append TDs
9271da177e4SLinus Torvalds  * to a (possibly active) QH, and the same QH scanning code.
9281da177e4SLinus Torvalds  */
9291da177e4SLinus Torvalds static int ehci_urb_enqueue (
9301da177e4SLinus Torvalds 	struct usb_hcd	*hcd,
9311da177e4SLinus Torvalds 	struct urb	*urb,
93255016f10SAl Viro 	gfp_t		mem_flags
9331da177e4SLinus Torvalds ) {
9341da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
9351da177e4SLinus Torvalds 	struct list_head	qtd_list;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	INIT_LIST_HEAD (&qtd_list);
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	switch (usb_pipetype (urb->pipe)) {
94025b70a86SDavid Brownell 	case PIPE_CONTROL:
94125b70a86SDavid Brownell 		/* qh_completions() code doesn't handle all the fault cases
94225b70a86SDavid Brownell 		 * in multi-TD control transfers.  Even 1KB is rare anyway.
94325b70a86SDavid Brownell 		 */
94425b70a86SDavid Brownell 		if (urb->transfer_buffer_length > (16 * 1024))
94525b70a86SDavid Brownell 			return -EMSGSIZE;
94625b70a86SDavid Brownell 		/* FALLTHROUGH */
94725b70a86SDavid Brownell 	/* case PIPE_BULK: */
9481da177e4SLinus Torvalds 	default:
9491da177e4SLinus Torvalds 		if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
9501da177e4SLinus Torvalds 			return -ENOMEM;
951e9df41c5SAlan Stern 		return submit_async(ehci, urb, &qtd_list, mem_flags);
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	case PIPE_INTERRUPT:
9541da177e4SLinus Torvalds 		if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
9551da177e4SLinus Torvalds 			return -ENOMEM;
956e9df41c5SAlan Stern 		return intr_submit(ehci, urb, &qtd_list, mem_flags);
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	case PIPE_ISOCHRONOUS:
9591da177e4SLinus Torvalds 		if (urb->dev->speed == USB_SPEED_HIGH)
9601da177e4SLinus Torvalds 			return itd_submit (ehci, urb, mem_flags);
9611da177e4SLinus Torvalds 		else
9621da177e4SLinus Torvalds 			return sitd_submit (ehci, urb, mem_flags);
9631da177e4SLinus Torvalds 	}
9641da177e4SLinus Torvalds }
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds static void unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
9671da177e4SLinus Torvalds {
96807d29b63SAlan Stern 	/* failfast */
969c0c53dbcSAlan Stern 	if (ehci->rh_state < EHCI_RH_RUNNING && ehci->async_unlink)
97007d29b63SAlan Stern 		end_unlink_async(ehci);
97107d29b63SAlan Stern 
9723a44494eSAlan Stern 	/* If the QH isn't linked then there's nothing we can do
9733a44494eSAlan Stern 	 * unless we were called during a giveback, in which case
9743a44494eSAlan Stern 	 * qh_completions() has to deal with it.
9753a44494eSAlan Stern 	 */
9763a44494eSAlan Stern 	if (qh->qh_state != QH_STATE_LINKED) {
9773a44494eSAlan Stern 		if (qh->qh_state == QH_STATE_COMPLETING)
9783a44494eSAlan Stern 			qh->needs_rescan = 1;
9793a44494eSAlan Stern 		return;
9803a44494eSAlan Stern 	}
98107d29b63SAlan Stern 
98207d29b63SAlan Stern 	/* defer till later if busy */
98399ac5b1eSAlan Stern 	if (ehci->async_unlink) {
9841da177e4SLinus Torvalds 		qh->qh_state = QH_STATE_UNLINK_WAIT;
9852f5bb665SAlan Stern 		ehci->async_unlink_last->unlink_next = qh;
9862f5bb665SAlan Stern 		ehci->async_unlink_last = qh;
9871da177e4SLinus Torvalds 
98807d29b63SAlan Stern 	/* start IAA cycle */
98907d29b63SAlan Stern 	} else
9901da177e4SLinus Torvalds 		start_unlink_async (ehci, qh);
9911da177e4SLinus Torvalds }
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds /* remove from hardware lists
9941da177e4SLinus Torvalds  * completions normally happen asynchronously
9951da177e4SLinus Torvalds  */
9961da177e4SLinus Torvalds 
997e9df41c5SAlan Stern static int ehci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
9981da177e4SLinus Torvalds {
9991da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
10001da177e4SLinus Torvalds 	struct ehci_qh		*qh;
10011da177e4SLinus Torvalds 	unsigned long		flags;
1002e9df41c5SAlan Stern 	int			rc;
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	spin_lock_irqsave (&ehci->lock, flags);
1005e9df41c5SAlan Stern 	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1006e9df41c5SAlan Stern 	if (rc)
1007e9df41c5SAlan Stern 		goto done;
1008e9df41c5SAlan Stern 
10091da177e4SLinus Torvalds 	switch (usb_pipetype (urb->pipe)) {
10101da177e4SLinus Torvalds 	// case PIPE_CONTROL:
10111da177e4SLinus Torvalds 	// case PIPE_BULK:
10121da177e4SLinus Torvalds 	default:
10131da177e4SLinus Torvalds 		qh = (struct ehci_qh *) urb->hcpriv;
10141da177e4SLinus Torvalds 		if (!qh)
10151da177e4SLinus Torvalds 			break;
101607d29b63SAlan Stern 		switch (qh->qh_state) {
101707d29b63SAlan Stern 		case QH_STATE_LINKED:
101807d29b63SAlan Stern 		case QH_STATE_COMPLETING:
10191da177e4SLinus Torvalds 			unlink_async(ehci, qh);
10201da177e4SLinus Torvalds 			break;
102107d29b63SAlan Stern 		case QH_STATE_UNLINK:
102207d29b63SAlan Stern 		case QH_STATE_UNLINK_WAIT:
102307d29b63SAlan Stern 			/* already started */
102407d29b63SAlan Stern 			break;
102507d29b63SAlan Stern 		case QH_STATE_IDLE:
10267a0f0d95SAlan Stern 			/* QH might be waiting for a Clear-TT-Buffer */
10277a0f0d95SAlan Stern 			qh_completions(ehci, qh);
102807d29b63SAlan Stern 			break;
102907d29b63SAlan Stern 		}
103007d29b63SAlan Stern 		break;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	case PIPE_INTERRUPT:
10331da177e4SLinus Torvalds 		qh = (struct ehci_qh *) urb->hcpriv;
10341da177e4SLinus Torvalds 		if (!qh)
10351da177e4SLinus Torvalds 			break;
10361da177e4SLinus Torvalds 		switch (qh->qh_state) {
10371da177e4SLinus Torvalds 		case QH_STATE_LINKED:
1038a448c9d8SAlan Stern 		case QH_STATE_COMPLETING:
1039df202255SAlan Stern 			start_unlink_intr(ehci, qh);
1040a448c9d8SAlan Stern 			break;
10411da177e4SLinus Torvalds 		case QH_STATE_IDLE:
10427d12e780SDavid Howells 			qh_completions (ehci, qh);
10431da177e4SLinus Torvalds 			break;
10441da177e4SLinus Torvalds 		default:
10451da177e4SLinus Torvalds 			ehci_dbg (ehci, "bogus qh %p state %d\n",
10461da177e4SLinus Torvalds 					qh, qh->qh_state);
10471da177e4SLinus Torvalds 			goto done;
10481da177e4SLinus Torvalds 		}
10491da177e4SLinus Torvalds 		break;
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	case PIPE_ISOCHRONOUS:
10521da177e4SLinus Torvalds 		// itd or sitd ...
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 		// wait till next completion, do it then.
10551da177e4SLinus Torvalds 		// completion irqs can wait up to 1024 msec,
10561da177e4SLinus Torvalds 		break;
10571da177e4SLinus Torvalds 	}
10581da177e4SLinus Torvalds done:
10591da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ehci->lock, flags);
1060e9df41c5SAlan Stern 	return rc;
10611da177e4SLinus Torvalds }
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds // bulk qh holds the data toggle
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds static void
10681da177e4SLinus Torvalds ehci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
10691da177e4SLinus Torvalds {
10701da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
10711da177e4SLinus Torvalds 	unsigned long		flags;
10721da177e4SLinus Torvalds 	struct ehci_qh		*qh, *tmp;
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 	/* ASSERT:  any requests/urbs are being unlinked */
10751da177e4SLinus Torvalds 	/* ASSERT:  nobody can be submitting urbs for this any more */
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds rescan:
10781da177e4SLinus Torvalds 	spin_lock_irqsave (&ehci->lock, flags);
10791da177e4SLinus Torvalds 	qh = ep->hcpriv;
10801da177e4SLinus Torvalds 	if (!qh)
10811da177e4SLinus Torvalds 		goto done;
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 	/* endpoints can be iso streams.  for now, we don't
10841da177e4SLinus Torvalds 	 * accelerate iso completions ... so spin a while.
10851da177e4SLinus Torvalds 	 */
10861082f57aSClemens Ladisch 	if (qh->hw == NULL) {
10871da177e4SLinus Torvalds 		ehci_vdbg (ehci, "iso delay\n");
10881da177e4SLinus Torvalds 		goto idle_timeout;
10891da177e4SLinus Torvalds 	}
10901da177e4SLinus Torvalds 
1091c0c53dbcSAlan Stern 	if (ehci->rh_state < EHCI_RH_RUNNING)
10921da177e4SLinus Torvalds 		qh->qh_state = QH_STATE_IDLE;
10931da177e4SLinus Torvalds 	switch (qh->qh_state) {
10941da177e4SLinus Torvalds 	case QH_STATE_LINKED:
10953a44494eSAlan Stern 	case QH_STATE_COMPLETING:
10961da177e4SLinus Torvalds 		for (tmp = ehci->async->qh_next.qh;
10971da177e4SLinus Torvalds 				tmp && tmp != qh;
10981da177e4SLinus Torvalds 				tmp = tmp->qh_next.qh)
10991da177e4SLinus Torvalds 			continue;
110002e2c51bSAlan Stern 		/* periodic qh self-unlinks on empty, and a COMPLETING qh
110102e2c51bSAlan Stern 		 * may already be unlinked.
110202e2c51bSAlan Stern 		 */
110302e2c51bSAlan Stern 		if (tmp)
11041da177e4SLinus Torvalds 			unlink_async(ehci, qh);
11051da177e4SLinus Torvalds 		/* FALL THROUGH */
11061da177e4SLinus Torvalds 	case QH_STATE_UNLINK:		/* wait for hw to finish? */
110707d29b63SAlan Stern 	case QH_STATE_UNLINK_WAIT:
11081da177e4SLinus Torvalds idle_timeout:
11091da177e4SLinus Torvalds 		spin_unlock_irqrestore (&ehci->lock, flags);
111022c43863SNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
11111da177e4SLinus Torvalds 		goto rescan;
11121da177e4SLinus Torvalds 	case QH_STATE_IDLE:		/* fully unlinked */
1113914b7012SAlan Stern 		if (qh->clearing_tt)
1114914b7012SAlan Stern 			goto idle_timeout;
11151da177e4SLinus Torvalds 		if (list_empty (&qh->qtd_list)) {
1116c83e1a9fSAlan Stern 			qh_destroy(ehci, qh);
11171da177e4SLinus Torvalds 			break;
11181da177e4SLinus Torvalds 		}
11191da177e4SLinus Torvalds 		/* else FALL THROUGH */
11201da177e4SLinus Torvalds 	default:
11211da177e4SLinus Torvalds 		/* caller was supposed to have unlinked any requests;
11221da177e4SLinus Torvalds 		 * that's not our job.  just leak this memory.
11231da177e4SLinus Torvalds 		 */
11241da177e4SLinus Torvalds 		ehci_err (ehci, "qh %p (#%02x) state %d%s\n",
11251da177e4SLinus Torvalds 			qh, ep->desc.bEndpointAddress, qh->qh_state,
11261da177e4SLinus Torvalds 			list_empty (&qh->qtd_list) ? "" : "(has tds)");
11271da177e4SLinus Torvalds 		break;
11281da177e4SLinus Torvalds 	}
11291da177e4SLinus Torvalds 	ep->hcpriv = NULL;
11301da177e4SLinus Torvalds done:
11311da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ehci->lock, flags);
11321da177e4SLinus Torvalds }
11331da177e4SLinus Torvalds 
1134b18ffd49SAlan Stern static void
1135b18ffd49SAlan Stern ehci_endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1136b18ffd49SAlan Stern {
1137b18ffd49SAlan Stern 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
1138b18ffd49SAlan Stern 	struct ehci_qh		*qh;
1139b18ffd49SAlan Stern 	int			eptype = usb_endpoint_type(&ep->desc);
1140a455212dSAlan Stern 	int			epnum = usb_endpoint_num(&ep->desc);
1141a455212dSAlan Stern 	int			is_out = usb_endpoint_dir_out(&ep->desc);
1142a455212dSAlan Stern 	unsigned long		flags;
1143b18ffd49SAlan Stern 
1144b18ffd49SAlan Stern 	if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
1145b18ffd49SAlan Stern 		return;
1146b18ffd49SAlan Stern 
1147a455212dSAlan Stern 	spin_lock_irqsave(&ehci->lock, flags);
1148b18ffd49SAlan Stern 	qh = ep->hcpriv;
1149b18ffd49SAlan Stern 
1150b18ffd49SAlan Stern 	/* For Bulk and Interrupt endpoints we maintain the toggle state
1151b18ffd49SAlan Stern 	 * in the hardware; the toggle bits in udev aren't used at all.
1152b18ffd49SAlan Stern 	 * When an endpoint is reset by usb_clear_halt() we must reset
1153b18ffd49SAlan Stern 	 * the toggle bit in the QH.
1154b18ffd49SAlan Stern 	 */
1155b18ffd49SAlan Stern 	if (qh) {
1156a455212dSAlan Stern 		usb_settoggle(qh->dev, epnum, is_out, 0);
1157b18ffd49SAlan Stern 		if (!list_empty(&qh->qtd_list)) {
1158b18ffd49SAlan Stern 			WARN_ONCE(1, "clear_halt for a busy endpoint\n");
11593a44494eSAlan Stern 		} else if (qh->qh_state == QH_STATE_LINKED ||
11603a44494eSAlan Stern 				qh->qh_state == QH_STATE_COMPLETING) {
1161a455212dSAlan Stern 
1162a455212dSAlan Stern 			/* The toggle value in the QH can't be updated
1163a455212dSAlan Stern 			 * while the QH is active.  Unlink it now;
1164a455212dSAlan Stern 			 * re-linking will call qh_refresh().
1165b18ffd49SAlan Stern 			 */
1166a448c9d8SAlan Stern 			if (eptype == USB_ENDPOINT_XFER_BULK)
1167b18ffd49SAlan Stern 				unlink_async(ehci, qh);
1168a448c9d8SAlan Stern 			else
1169df202255SAlan Stern 				start_unlink_intr(ehci, qh);
1170b18ffd49SAlan Stern 		}
1171b18ffd49SAlan Stern 	}
1172a455212dSAlan Stern 	spin_unlock_irqrestore(&ehci->lock, flags);
1173b18ffd49SAlan Stern }
1174b18ffd49SAlan Stern 
11757ff71d6aSMatt Porter static int ehci_get_frame (struct usb_hcd *hcd)
11767ff71d6aSMatt Porter {
11777ff71d6aSMatt Porter 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
117868aa95d5SAlan Stern 	return (ehci_read_frame_index(ehci) >> 3) % ehci->periodic_size;
11797ff71d6aSMatt Porter }
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1182c5cf9212SAlan Stern 
1183c5cf9212SAlan Stern #ifdef	CONFIG_PM
1184c5cf9212SAlan Stern 
1185c5cf9212SAlan Stern /* suspend/resume, section 4.3 */
1186c5cf9212SAlan Stern 
1187c5cf9212SAlan Stern /* These routines handle the generic parts of controller suspend/resume */
1188c5cf9212SAlan Stern 
1189c5cf9212SAlan Stern static int __maybe_unused ehci_suspend(struct usb_hcd *hcd, bool do_wakeup)
1190c5cf9212SAlan Stern {
1191c5cf9212SAlan Stern 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
1192c5cf9212SAlan Stern 
1193c5cf9212SAlan Stern 	if (time_before(jiffies, ehci->next_statechange))
1194c5cf9212SAlan Stern 		msleep(10);
1195c5cf9212SAlan Stern 
1196c5cf9212SAlan Stern 	/*
1197c5cf9212SAlan Stern 	 * Root hub was already suspended.  Disable IRQ emission and
1198c5cf9212SAlan Stern 	 * mark HW unaccessible.  The PM and USB cores make sure that
1199c5cf9212SAlan Stern 	 * the root hub is either suspended or stopped.
1200c5cf9212SAlan Stern 	 */
1201c5cf9212SAlan Stern 	ehci_prepare_ports_for_controller_suspend(ehci, do_wakeup);
1202c5cf9212SAlan Stern 
1203c5cf9212SAlan Stern 	spin_lock_irq(&ehci->lock);
1204c5cf9212SAlan Stern 	ehci_writel(ehci, 0, &ehci->regs->intr_enable);
1205c5cf9212SAlan Stern 	(void) ehci_readl(ehci, &ehci->regs->intr_enable);
1206c5cf9212SAlan Stern 
1207c5cf9212SAlan Stern 	clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1208c5cf9212SAlan Stern 	spin_unlock_irq(&ehci->lock);
1209c5cf9212SAlan Stern 
1210c5cf9212SAlan Stern 	return 0;
1211c5cf9212SAlan Stern }
1212c5cf9212SAlan Stern 
1213c5cf9212SAlan Stern /* Returns 0 if power was preserved, 1 if power was lost */
1214c5cf9212SAlan Stern static int __maybe_unused ehci_resume(struct usb_hcd *hcd, bool hibernated)
1215c5cf9212SAlan Stern {
1216c5cf9212SAlan Stern 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
1217c5cf9212SAlan Stern 
1218c5cf9212SAlan Stern 	if (time_before(jiffies, ehci->next_statechange))
1219c5cf9212SAlan Stern 		msleep(100);
1220c5cf9212SAlan Stern 
1221c5cf9212SAlan Stern 	/* Mark hardware accessible again as we are back to full power by now */
1222c5cf9212SAlan Stern 	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1223c5cf9212SAlan Stern 
1224c5cf9212SAlan Stern 	/*
1225c5cf9212SAlan Stern 	 * If CF is still set and we aren't resuming from hibernation
1226c5cf9212SAlan Stern 	 * then we maintained suspend power.
1227c5cf9212SAlan Stern 	 * Just undo the effect of ehci_suspend().
1228c5cf9212SAlan Stern 	 */
1229c5cf9212SAlan Stern 	if (ehci_readl(ehci, &ehci->regs->configured_flag) == FLAG_CF &&
1230c5cf9212SAlan Stern 			!hibernated) {
1231c5cf9212SAlan Stern 		int	mask = INTR_MASK;
1232c5cf9212SAlan Stern 
1233c5cf9212SAlan Stern 		ehci_prepare_ports_for_controller_resume(ehci);
1234c5cf9212SAlan Stern 		if (!hcd->self.root_hub->do_remote_wakeup)
1235c5cf9212SAlan Stern 			mask &= ~STS_PCD;
1236c5cf9212SAlan Stern 		ehci_writel(ehci, mask, &ehci->regs->intr_enable);
1237c5cf9212SAlan Stern 		ehci_readl(ehci, &ehci->regs->intr_enable);
1238c5cf9212SAlan Stern 		return 0;
1239c5cf9212SAlan Stern 	}
1240c5cf9212SAlan Stern 
1241c5cf9212SAlan Stern 	/*
1242c5cf9212SAlan Stern 	 * Else reset, to cope with power loss or resume from hibernation
1243c5cf9212SAlan Stern 	 * having let the firmware kick in during reboot.
1244c5cf9212SAlan Stern 	 */
1245c5cf9212SAlan Stern 	usb_root_hub_lost_power(hcd->self.root_hub);
1246c5cf9212SAlan Stern 	(void) ehci_halt(ehci);
1247c5cf9212SAlan Stern 	(void) ehci_reset(ehci);
1248c5cf9212SAlan Stern 
1249c5cf9212SAlan Stern 	ehci_writel(ehci, ehci->command, &ehci->regs->command);
1250c5cf9212SAlan Stern 	ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
1251c5cf9212SAlan Stern 	ehci_readl(ehci, &ehci->regs->command);	/* unblock posted writes */
1252c5cf9212SAlan Stern 
1253c5cf9212SAlan Stern 	/* here we "know" root ports should always stay powered */
1254c5cf9212SAlan Stern 	ehci_port_power(ehci, 1);
1255c5cf9212SAlan Stern 
1256c5cf9212SAlan Stern 	ehci->rh_state = EHCI_RH_SUSPENDED;
1257c5cf9212SAlan Stern 	return 1;
1258c5cf9212SAlan Stern }
1259c5cf9212SAlan Stern 
1260c5cf9212SAlan Stern #endif
1261c5cf9212SAlan Stern 
1262c5cf9212SAlan Stern /*-------------------------------------------------------------------------*/
1263c5cf9212SAlan Stern 
1264eb70e5abSAlexander Shishkin /*
1265eb70e5abSAlexander Shishkin  * The EHCI in ChipIdea HDRC cannot be a separate module or device,
1266eb70e5abSAlexander Shishkin  * because its registers (and irq) are shared between host/gadget/otg
1267eb70e5abSAlexander Shishkin  * functions  and in order to facilitate role switching we cannot
1268eb70e5abSAlexander Shishkin  * give the ehci driver exclusive access to those.
1269eb70e5abSAlexander Shishkin  */
1270eb70e5abSAlexander Shishkin #ifndef CHIPIDEA_EHCI
12711da177e4SLinus Torvalds 
12722b70f073SAlan Stern MODULE_DESCRIPTION(DRIVER_DESC);
12731da177e4SLinus Torvalds MODULE_AUTHOR (DRIVER_AUTHOR);
12741da177e4SLinus Torvalds MODULE_LICENSE ("GPL");
12751da177e4SLinus Torvalds 
12767ff71d6aSMatt Porter #ifdef CONFIG_PCI
12777ff71d6aSMatt Porter #include "ehci-pci.c"
127801cced25SKumar Gala #define	PCI_DRIVER		ehci_pci_driver
12797ff71d6aSMatt Porter #endif
12801da177e4SLinus Torvalds 
1281ba02978aSLi Yang #ifdef CONFIG_USB_EHCI_FSL
128280cb9aeeSRandy Vinson #include "ehci-fsl.c"
128301cced25SKumar Gala #define	PLATFORM_DRIVER		ehci_fsl_driver
128480cb9aeeSRandy Vinson #endif
128580cb9aeeSRandy Vinson 
12867e8d5cd9SDaniel Mack #ifdef CONFIG_USB_EHCI_MXC
12877e8d5cd9SDaniel Mack #include "ehci-mxc.c"
12887e8d5cd9SDaniel Mack #define PLATFORM_DRIVER		ehci_mxc_driver
12897e8d5cd9SDaniel Mack #endif
12907e8d5cd9SDaniel Mack 
129160b0bf0fSYoshihiro Shimoda #ifdef CONFIG_USB_EHCI_SH
129263c84552SPaul Mundt #include "ehci-sh.c"
129363c84552SPaul Mundt #define PLATFORM_DRIVER		ehci_hcd_sh_driver
129463c84552SPaul Mundt #endif
129563c84552SPaul Mundt 
129637663860SManuel Lauss #ifdef CONFIG_MIPS_ALCHEMY
129776fa9a24SJordan Crouse #include "ehci-au1xxx.c"
129801cced25SKumar Gala #define	PLATFORM_DRIVER		ehci_hcd_au1xxx_driver
129976fa9a24SJordan Crouse #endif
130076fa9a24SJordan Crouse 
13017f124f4bSKeshava Munegowda #ifdef CONFIG_USB_EHCI_HCD_OMAP
130254ab2b02SFelipe Balbi #include "ehci-omap.c"
130354ab2b02SFelipe Balbi #define        PLATFORM_DRIVER         ehci_hcd_omap_driver
130454ab2b02SFelipe Balbi #endif
130554ab2b02SFelipe Balbi 
1306ad75a410SGeoff Levand #ifdef CONFIG_PPC_PS3
1307ad75a410SGeoff Levand #include "ehci-ps3.c"
13087a4eb7fdSGeoff Levand #define	PS3_SYSTEM_BUS_DRIVER	ps3_ehci_driver
1309ad75a410SGeoff Levand #endif
1310ad75a410SGeoff Levand 
1311da0e8fb0SValentine Barshak #ifdef CONFIG_USB_EHCI_HCD_PPC_OF
1312da0e8fb0SValentine Barshak #include "ehci-ppc-of.c"
1313da0e8fb0SValentine Barshak #define OF_PLATFORM_DRIVER	ehci_hcd_ppc_of_driver
1314da0e8fb0SValentine Barshak #endif
1315da0e8fb0SValentine Barshak 
131608d3c18eSJulie Zhu #ifdef CONFIG_XPS_USB_HCD_XILINX
131708d3c18eSJulie Zhu #include "ehci-xilinx-of.c"
13181f23b2d9SGrant Likely #define XILINX_OF_PLATFORM_DRIVER	ehci_hcd_xilinx_of_driver
131908d3c18eSJulie Zhu #endif
132008d3c18eSJulie Zhu 
1321705a7521SLennert Buytenhek #ifdef CONFIG_PLAT_ORION
1322e96ffe2fSTzachi Perelstein #include "ehci-orion.c"
1323e96ffe2fSTzachi Perelstein #define	PLATFORM_DRIVER		ehci_orion_driver
1324e96ffe2fSTzachi Perelstein #endif
1325e96ffe2fSTzachi Perelstein 
132691bc4d31SVladimir Barinov #ifdef CONFIG_ARCH_IXP4XX
132791bc4d31SVladimir Barinov #include "ehci-ixp4xx.c"
132891bc4d31SVladimir Barinov #define	PLATFORM_DRIVER		ixp4xx_ehci_driver
132991bc4d31SVladimir Barinov #endif
133091bc4d31SVladimir Barinov 
1331586dfc8cSWan ZongShun #ifdef CONFIG_USB_W90X900_EHCI
1332586dfc8cSWan ZongShun #include "ehci-w90x900.c"
1333586dfc8cSWan ZongShun #define	PLATFORM_DRIVER		ehci_hcd_w90x900_driver
1334586dfc8cSWan ZongShun #endif
1335586dfc8cSWan ZongShun 
1336501c9c08SNicolas Ferre #ifdef CONFIG_ARCH_AT91
1337501c9c08SNicolas Ferre #include "ehci-atmel.c"
1338501c9c08SNicolas Ferre #define	PLATFORM_DRIVER		ehci_atmel_driver
1339501c9c08SNicolas Ferre #endif
1340501c9c08SNicolas Ferre 
13411643accdSDavid Daney #ifdef CONFIG_USB_OCTEON_EHCI
13421643accdSDavid Daney #include "ehci-octeon.c"
13431643accdSDavid Daney #define PLATFORM_DRIVER		ehci_octeon_driver
13441643accdSDavid Daney #endif
13451643accdSDavid Daney 
1346760efe69SMac Lin #ifdef CONFIG_USB_CNS3XXX_EHCI
1347760efe69SMac Lin #include "ehci-cns3xxx.c"
1348760efe69SMac Lin #define PLATFORM_DRIVER		cns3xxx_ehci_driver
1349760efe69SMac Lin #endif
1350760efe69SMac Lin 
1351ad78acafSAlexey Charkov #ifdef CONFIG_ARCH_VT8500
1352ad78acafSAlexey Charkov #include "ehci-vt8500.c"
1353ad78acafSAlexey Charkov #define	PLATFORM_DRIVER		vt8500_ehci_driver
1354ad78acafSAlexey Charkov #endif
1355ad78acafSAlexey Charkov 
1356c8c38de9SDeepak Sikri #ifdef CONFIG_PLAT_SPEAR
1357c8c38de9SDeepak Sikri #include "ehci-spear.c"
1358c8c38de9SDeepak Sikri #define PLATFORM_DRIVER		spear_ehci_hcd_driver
1359c8c38de9SDeepak Sikri #endif
1360c8c38de9SDeepak Sikri 
1361b0848aeaSPavankumar Kondeti #ifdef CONFIG_USB_EHCI_MSM
1362b0848aeaSPavankumar Kondeti #include "ehci-msm.c"
1363b0848aeaSPavankumar Kondeti #define PLATFORM_DRIVER		ehci_msm_driver
1364b0848aeaSPavankumar Kondeti #endif
1365b0848aeaSPavankumar Kondeti 
136622ced687SAnoop #ifdef CONFIG_USB_EHCI_HCD_PMC_MSP
136722ced687SAnoop #include "ehci-pmcmsp.c"
136822ced687SAnoop #define	PLATFORM_DRIVER		ehci_hcd_msp_driver
136922ced687SAnoop #endif
137022ced687SAnoop 
137179ad3b5aSBenoit Goby #ifdef CONFIG_USB_EHCI_TEGRA
137279ad3b5aSBenoit Goby #include "ehci-tegra.c"
137379ad3b5aSBenoit Goby #define PLATFORM_DRIVER		tegra_ehci_driver
137479ad3b5aSBenoit Goby #endif
137579ad3b5aSBenoit Goby 
13761bcc5aa8SJoonyoung Shim #ifdef CONFIG_USB_EHCI_S5P
13771bcc5aa8SJoonyoung Shim #include "ehci-s5p.c"
13781bcc5aa8SJoonyoung Shim #define PLATFORM_DRIVER		s5p_ehci_driver
13791bcc5aa8SJoonyoung Shim #endif
13801bcc5aa8SJoonyoung Shim 
13819be03929SJan Andersson #ifdef CONFIG_SPARC_LEON
13829be03929SJan Andersson #include "ehci-grlib.c"
13839be03929SJan Andersson #define PLATFORM_DRIVER		ehci_grlib_driver
13849be03929SJan Andersson #endif
13859be03929SJan Andersson 
13863af5154aSJayachandran C #ifdef CONFIG_CPU_XLR
138723106343SJayachandran C #include "ehci-xls.c"
138823106343SJayachandran C #define PLATFORM_DRIVER		ehci_xls_driver
138923106343SJayachandran C #endif
139023106343SJayachandran C 
13913a082ec9SNeil Zhang #ifdef CONFIG_USB_EHCI_MV
13923a082ec9SNeil Zhang #include "ehci-mv.c"
13933a082ec9SNeil Zhang #define        PLATFORM_DRIVER         ehci_mv_driver
13943a082ec9SNeil Zhang #endif
13953a082ec9SNeil Zhang 
1396f30cdbcbSKelvin Cheung #ifdef CONFIG_MACH_LOONGSON1
1397f30cdbcbSKelvin Cheung #include "ehci-ls1x.c"
1398f30cdbcbSKelvin Cheung #define PLATFORM_DRIVER		ehci_ls1x_driver
1399f30cdbcbSKelvin Cheung #endif
1400f30cdbcbSKelvin Cheung 
1401c256667fSSteven J. Hill #ifdef CONFIG_MIPS_SEAD3
1402c256667fSSteven J. Hill #include "ehci-sead3.c"
1403c256667fSSteven J. Hill #define	PLATFORM_DRIVER		ehci_hcd_sead3_driver
1404c256667fSSteven J. Hill #endif
1405c256667fSSteven J. Hill 
14067a7a4a59SHauke Mehrtens #ifdef CONFIG_USB_EHCI_HCD_PLATFORM
14077a7a4a59SHauke Mehrtens #include "ehci-platform.c"
14087a7a4a59SHauke Mehrtens #define PLATFORM_DRIVER		ehci_platform_driver
14097a7a4a59SHauke Mehrtens #endif
14107a7a4a59SHauke Mehrtens 
1411ad75a410SGeoff Levand #if !defined(PCI_DRIVER) && !defined(PLATFORM_DRIVER) && \
14121f23b2d9SGrant Likely     !defined(PS3_SYSTEM_BUS_DRIVER) && !defined(OF_PLATFORM_DRIVER) && \
14131f23b2d9SGrant Likely     !defined(XILINX_OF_PLATFORM_DRIVER)
14147ff71d6aSMatt Porter #error "missing bus glue for ehci-hcd"
14157ff71d6aSMatt Porter #endif
141601cced25SKumar Gala 
141701cced25SKumar Gala static int __init ehci_hcd_init(void)
141801cced25SKumar Gala {
141901cced25SKumar Gala 	int retval = 0;
142001cced25SKumar Gala 
14212b70f073SAlan Stern 	if (usb_disabled())
14222b70f073SAlan Stern 		return -ENODEV;
14232b70f073SAlan Stern 
14242b70f073SAlan Stern 	printk(KERN_INFO "%s: " DRIVER_DESC "\n", hcd_name);
14259beeee65SAlan Stern 	set_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
14269beeee65SAlan Stern 	if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) ||
14279beeee65SAlan Stern 			test_bit(USB_OHCI_LOADED, &usb_hcds_loaded))
14289beeee65SAlan Stern 		printk(KERN_WARNING "Warning! ehci_hcd should always be loaded"
14299beeee65SAlan Stern 				" before uhci_hcd and ohci_hcd, not after\n");
14309beeee65SAlan Stern 
143101cced25SKumar Gala 	pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
143201cced25SKumar Gala 		 hcd_name,
143301cced25SKumar Gala 		 sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
143401cced25SKumar Gala 		 sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
143501cced25SKumar Gala 
1436694cc208STony Jones #ifdef DEBUG
143708f4e586SGreg Kroah-Hartman 	ehci_debug_root = debugfs_create_dir("ehci", usb_debug_root);
14389beeee65SAlan Stern 	if (!ehci_debug_root) {
14399beeee65SAlan Stern 		retval = -ENOENT;
14409beeee65SAlan Stern 		goto err_debug;
14419beeee65SAlan Stern 	}
1442694cc208STony Jones #endif
1443694cc208STony Jones 
144401cced25SKumar Gala #ifdef PLATFORM_DRIVER
144501cced25SKumar Gala 	retval = platform_driver_register(&PLATFORM_DRIVER);
1446da0e8fb0SValentine Barshak 	if (retval < 0)
1447da0e8fb0SValentine Barshak 		goto clean0;
144801cced25SKumar Gala #endif
144901cced25SKumar Gala 
145001cced25SKumar Gala #ifdef PCI_DRIVER
145101cced25SKumar Gala 	retval = pci_register_driver(&PCI_DRIVER);
1452da0e8fb0SValentine Barshak 	if (retval < 0)
1453da0e8fb0SValentine Barshak 		goto clean1;
1454ad75a410SGeoff Levand #endif
1455ad75a410SGeoff Levand 
1456ad75a410SGeoff Levand #ifdef PS3_SYSTEM_BUS_DRIVER
14577a4eb7fdSGeoff Levand 	retval = ps3_ehci_driver_register(&PS3_SYSTEM_BUS_DRIVER);
1458da0e8fb0SValentine Barshak 	if (retval < 0)
1459da0e8fb0SValentine Barshak 		goto clean2;
1460da0e8fb0SValentine Barshak #endif
1461da0e8fb0SValentine Barshak 
1462da0e8fb0SValentine Barshak #ifdef OF_PLATFORM_DRIVER
1463d35fb641SGrant Likely 	retval = platform_driver_register(&OF_PLATFORM_DRIVER);
1464da0e8fb0SValentine Barshak 	if (retval < 0)
1465da0e8fb0SValentine Barshak 		goto clean3;
1466da0e8fb0SValentine Barshak #endif
14671f23b2d9SGrant Likely 
14681f23b2d9SGrant Likely #ifdef XILINX_OF_PLATFORM_DRIVER
1469d35fb641SGrant Likely 	retval = platform_driver_register(&XILINX_OF_PLATFORM_DRIVER);
14701f23b2d9SGrant Likely 	if (retval < 0)
14711f23b2d9SGrant Likely 		goto clean4;
14721f23b2d9SGrant Likely #endif
1473da0e8fb0SValentine Barshak 	return retval;
1474da0e8fb0SValentine Barshak 
14751f23b2d9SGrant Likely #ifdef XILINX_OF_PLATFORM_DRIVER
1476d35fb641SGrant Likely 	/* platform_driver_unregister(&XILINX_OF_PLATFORM_DRIVER); */
14771f23b2d9SGrant Likely clean4:
14781f23b2d9SGrant Likely #endif
1479da0e8fb0SValentine Barshak #ifdef OF_PLATFORM_DRIVER
1480d35fb641SGrant Likely 	platform_driver_unregister(&OF_PLATFORM_DRIVER);
1481da0e8fb0SValentine Barshak clean3:
1482da0e8fb0SValentine Barshak #endif
1483da0e8fb0SValentine Barshak #ifdef PS3_SYSTEM_BUS_DRIVER
1484da0e8fb0SValentine Barshak 	ps3_ehci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
1485da0e8fb0SValentine Barshak clean2:
1486da0e8fb0SValentine Barshak #endif
1487da0e8fb0SValentine Barshak #ifdef PCI_DRIVER
1488da0e8fb0SValentine Barshak 	pci_unregister_driver(&PCI_DRIVER);
1489da0e8fb0SValentine Barshak clean1:
1490da0e8fb0SValentine Barshak #endif
1491da0e8fb0SValentine Barshak #ifdef PLATFORM_DRIVER
1492da0e8fb0SValentine Barshak 	platform_driver_unregister(&PLATFORM_DRIVER);
1493da0e8fb0SValentine Barshak clean0:
1494da0e8fb0SValentine Barshak #endif
1495694cc208STony Jones #ifdef DEBUG
1496694cc208STony Jones 	debugfs_remove(ehci_debug_root);
1497694cc208STony Jones 	ehci_debug_root = NULL;
14989beeee65SAlan Stern err_debug:
1499a9b6148dSLinus Torvalds #endif
15009beeee65SAlan Stern 	clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
150101cced25SKumar Gala 	return retval;
150201cced25SKumar Gala }
150301cced25SKumar Gala module_init(ehci_hcd_init);
150401cced25SKumar Gala 
150501cced25SKumar Gala static void __exit ehci_hcd_cleanup(void)
150601cced25SKumar Gala {
15071f23b2d9SGrant Likely #ifdef XILINX_OF_PLATFORM_DRIVER
1508d35fb641SGrant Likely 	platform_driver_unregister(&XILINX_OF_PLATFORM_DRIVER);
15091f23b2d9SGrant Likely #endif
1510da0e8fb0SValentine Barshak #ifdef OF_PLATFORM_DRIVER
1511d35fb641SGrant Likely 	platform_driver_unregister(&OF_PLATFORM_DRIVER);
1512da0e8fb0SValentine Barshak #endif
151301cced25SKumar Gala #ifdef PLATFORM_DRIVER
151401cced25SKumar Gala 	platform_driver_unregister(&PLATFORM_DRIVER);
151501cced25SKumar Gala #endif
151601cced25SKumar Gala #ifdef PCI_DRIVER
151701cced25SKumar Gala 	pci_unregister_driver(&PCI_DRIVER);
151801cced25SKumar Gala #endif
1519ad75a410SGeoff Levand #ifdef PS3_SYSTEM_BUS_DRIVER
15207a4eb7fdSGeoff Levand 	ps3_ehci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
1521ad75a410SGeoff Levand #endif
1522694cc208STony Jones #ifdef DEBUG
1523694cc208STony Jones 	debugfs_remove(ehci_debug_root);
1524694cc208STony Jones #endif
15259beeee65SAlan Stern 	clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
152601cced25SKumar Gala }
152701cced25SKumar Gala module_exit(ehci_hcd_cleanup);
152801cced25SKumar Gala 
1529eb70e5abSAlexander Shishkin #endif /* CHIPIDEA_EHCI */
1530