xref: /linux/drivers/usb/host/ehci-hcd.c (revision ad75a41085d80c8ce5e885962c15779935f8267e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (c) 2000-2004 by David Brownell
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify it
51da177e4SLinus Torvalds  * under the terms of the GNU General Public License as published by the
61da177e4SLinus Torvalds  * Free Software Foundation; either version 2 of the License, or (at your
71da177e4SLinus Torvalds  * option) any later version.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful, but
101da177e4SLinus Torvalds  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
111da177e4SLinus Torvalds  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
121da177e4SLinus Torvalds  * for more details.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
151da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software Foundation,
161da177e4SLinus Torvalds  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <linux/module.h>
201da177e4SLinus Torvalds #include <linux/pci.h>
211da177e4SLinus Torvalds #include <linux/dmapool.h>
221da177e4SLinus Torvalds #include <linux/kernel.h>
231da177e4SLinus Torvalds #include <linux/delay.h>
241da177e4SLinus Torvalds #include <linux/ioport.h>
251da177e4SLinus Torvalds #include <linux/sched.h>
261da177e4SLinus Torvalds #include <linux/slab.h>
271da177e4SLinus Torvalds #include <linux/smp_lock.h>
281da177e4SLinus Torvalds #include <linux/errno.h>
291da177e4SLinus Torvalds #include <linux/init.h>
301da177e4SLinus Torvalds #include <linux/timer.h>
311da177e4SLinus Torvalds #include <linux/list.h>
321da177e4SLinus Torvalds #include <linux/interrupt.h>
331da177e4SLinus Torvalds #include <linux/reboot.h>
341da177e4SLinus Torvalds #include <linux/usb.h>
351da177e4SLinus Torvalds #include <linux/moduleparam.h>
361da177e4SLinus Torvalds #include <linux/dma-mapping.h>
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #include "../core/hcd.h"
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #include <asm/byteorder.h>
411da177e4SLinus Torvalds #include <asm/io.h>
421da177e4SLinus Torvalds #include <asm/irq.h>
431da177e4SLinus Torvalds #include <asm/system.h>
441da177e4SLinus Torvalds #include <asm/unaligned.h>
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds /*
501da177e4SLinus Torvalds  * EHCI hc_driver implementation ... experimental, incomplete.
511da177e4SLinus Torvalds  * Based on the final 1.0 register interface specification.
521da177e4SLinus Torvalds  *
531da177e4SLinus Torvalds  * USB 2.0 shows up in upcoming www.pcmcia.org technology.
541da177e4SLinus Torvalds  * First was PCMCIA, like ISA; then CardBus, which is PCI.
551da177e4SLinus Torvalds  * Next comes "CardBay", using USB 2.0 signals.
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  * Contains additional contributions by Brad Hards, Rory Bolt, and others.
581da177e4SLinus Torvalds  * Special thanks to Intel and VIA for providing host controllers to
591da177e4SLinus Torvalds  * test this driver on, and Cypress (including In-System Design) for
601da177e4SLinus Torvalds  * providing early devices for those host controllers to talk to!
611da177e4SLinus Torvalds  *
621da177e4SLinus Torvalds  * HISTORY:
631da177e4SLinus Torvalds  *
641da177e4SLinus Torvalds  * 2004-05-10 Root hub and PCI suspend/resume support; remote wakeup. (db)
651da177e4SLinus Torvalds  * 2004-02-24 Replace pci_* with generic dma_* API calls (dsaxena@plexity.net)
661da177e4SLinus Torvalds  * 2003-12-29 Rewritten high speed iso transfer support (by Michal Sojka,
671da177e4SLinus Torvalds  *	<sojkam@centrum.cz>, updates by DB).
681da177e4SLinus Torvalds  *
691da177e4SLinus Torvalds  * 2002-11-29	Correct handling for hw async_next register.
701da177e4SLinus Torvalds  * 2002-08-06	Handling for bulk and interrupt transfers is mostly shared;
711da177e4SLinus Torvalds  *	only scheduling is different, no arbitrary limitations.
721da177e4SLinus Torvalds  * 2002-07-25	Sanity check PCI reads, mostly for better cardbus support,
731da177e4SLinus Torvalds  *	clean up HC run state handshaking.
741da177e4SLinus Torvalds  * 2002-05-24	Preliminary FS/LS interrupts, using scheduling shortcuts
751da177e4SLinus Torvalds  * 2002-05-11	Clear TT errors for FS/LS ctrl/bulk.  Fill in some other
761da177e4SLinus Torvalds  *	missing pieces:  enabling 64bit dma, handoff from BIOS/SMM.
771da177e4SLinus Torvalds  * 2002-05-07	Some error path cleanups to report better errors; wmb();
781da177e4SLinus Torvalds  *	use non-CVS version id; better iso bandwidth claim.
791da177e4SLinus Torvalds  * 2002-04-19	Control/bulk/interrupt submit no longer uses giveback() on
801da177e4SLinus Torvalds  *	errors in submit path.  Bugfixes to interrupt scheduling/processing.
811da177e4SLinus Torvalds  * 2002-03-05	Initial high-speed ISO support; reduce ITD memory; shift
821da177e4SLinus Torvalds  *	more checking to generic hcd framework (db).  Make it work with
831da177e4SLinus Torvalds  *	Philips EHCI; reduce PCI traffic; shorten IRQ path (Rory Bolt).
841da177e4SLinus Torvalds  * 2002-01-14	Minor cleanup; version synch.
851da177e4SLinus Torvalds  * 2002-01-08	Fix roothub handoff of FS/LS to companion controllers.
861da177e4SLinus Torvalds  * 2002-01-04	Control/Bulk queuing behaves.
871da177e4SLinus Torvalds  *
881da177e4SLinus Torvalds  * 2001-12-12	Initial patch version for Linux 2.5.1 kernel.
891da177e4SLinus Torvalds  * 2001-June	Works with usb-storage and NEC EHCI on 2.4
901da177e4SLinus Torvalds  */
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds #define DRIVER_VERSION "10 Dec 2004"
931da177e4SLinus Torvalds #define DRIVER_AUTHOR "David Brownell"
941da177e4SLinus Torvalds #define DRIVER_DESC "USB 2.0 'Enhanced' Host Controller (EHCI) Driver"
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds static const char	hcd_name [] = "ehci_hcd";
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds #undef EHCI_VERBOSE_DEBUG
1001da177e4SLinus Torvalds #undef EHCI_URB_TRACE
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds #ifdef DEBUG
1031da177e4SLinus Torvalds #define EHCI_STATS
1041da177e4SLinus Torvalds #endif
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /* magic numbers that can affect system performance */
1071da177e4SLinus Torvalds #define	EHCI_TUNE_CERR		3	/* 0-3 qtd retries; 0 == don't stop */
1081da177e4SLinus Torvalds #define	EHCI_TUNE_RL_HS		4	/* nak throttle; see 4.9 */
1091da177e4SLinus Torvalds #define	EHCI_TUNE_RL_TT		0
1101da177e4SLinus Torvalds #define	EHCI_TUNE_MULT_HS	1	/* 1-3 transactions/uframe; 4.10.3 */
1111da177e4SLinus Torvalds #define	EHCI_TUNE_MULT_TT	1
1121da177e4SLinus Torvalds #define	EHCI_TUNE_FLS		2	/* (small) 256 frame schedule */
1131da177e4SLinus Torvalds 
11464f89798SGreg Kroah-Hartman #define EHCI_IAA_JIFFIES	(HZ/100)	/* arbitrary; ~10 msec */
1151da177e4SLinus Torvalds #define EHCI_IO_JIFFIES		(HZ/10)		/* io watchdog > irq_thresh */
1161da177e4SLinus Torvalds #define EHCI_ASYNC_JIFFIES	(HZ/20)		/* async idle timeout */
1171da177e4SLinus Torvalds #define EHCI_SHRINK_JIFFIES	(HZ/200)	/* async qh unlink delay */
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds /* Initial IRQ latency:  faster than hw default */
1201da177e4SLinus Torvalds static int log2_irq_thresh = 0;		// 0 to 6
1211da177e4SLinus Torvalds module_param (log2_irq_thresh, int, S_IRUGO);
1221da177e4SLinus Torvalds MODULE_PARM_DESC (log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds /* initial park setting:  slower than hw default */
1251da177e4SLinus Torvalds static unsigned park = 0;
1261da177e4SLinus Torvalds module_param (park, uint, S_IRUGO);
1271da177e4SLinus Torvalds MODULE_PARM_DESC (park, "park setting; 1-3 back-to-back async packets");
1281da177e4SLinus Torvalds 
12993f1a47cSDavid Brownell /* for flakey hardware, ignore overcurrent indicators */
13093f1a47cSDavid Brownell static int ignore_oc = 0;
13193f1a47cSDavid Brownell module_param (ignore_oc, bool, S_IRUGO);
13293f1a47cSDavid Brownell MODULE_PARM_DESC (ignore_oc, "ignore bogus hardware overcurrent indications");
13393f1a47cSDavid Brownell 
1341da177e4SLinus Torvalds #define	INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds #include "ehci.h"
1391da177e4SLinus Torvalds #include "ehci-dbg.c"
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds /*
1441da177e4SLinus Torvalds  * handshake - spin reading hc until handshake completes or fails
1451da177e4SLinus Torvalds  * @ptr: address of hc register to be read
1461da177e4SLinus Torvalds  * @mask: bits to look at in result of read
1471da177e4SLinus Torvalds  * @done: value of those bits when handshake succeeds
1481da177e4SLinus Torvalds  * @usec: timeout in microseconds
1491da177e4SLinus Torvalds  *
1501da177e4SLinus Torvalds  * Returns negative errno, or zero on success
1511da177e4SLinus Torvalds  *
1521da177e4SLinus Torvalds  * Success happens when the "mask" bits have the specified value (hardware
1531da177e4SLinus Torvalds  * handshake done).  There are two failure modes:  "usec" have passed (major
1541da177e4SLinus Torvalds  * hardware flakeout), or the register reads as all-ones (hardware removed).
1551da177e4SLinus Torvalds  *
1561da177e4SLinus Torvalds  * That last failure should_only happen in cases like physical cardbus eject
1571da177e4SLinus Torvalds  * before driver shutdown. But it also seems to be caused by bugs in cardbus
1581da177e4SLinus Torvalds  * bridge shutdown:  shutting down the bridge before the devices using it.
1591da177e4SLinus Torvalds  */
160083522d7SBenjamin Herrenschmidt static int handshake (struct ehci_hcd *ehci, void __iomem *ptr,
161083522d7SBenjamin Herrenschmidt 		      u32 mask, u32 done, int usec)
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds 	u32	result;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	do {
166083522d7SBenjamin Herrenschmidt 		result = ehci_readl(ehci, ptr);
1671da177e4SLinus Torvalds 		if (result == ~(u32)0)		/* card removed */
1681da177e4SLinus Torvalds 			return -ENODEV;
1691da177e4SLinus Torvalds 		result &= mask;
1701da177e4SLinus Torvalds 		if (result == done)
1711da177e4SLinus Torvalds 			return 0;
1721da177e4SLinus Torvalds 		udelay (1);
1731da177e4SLinus Torvalds 		usec--;
1741da177e4SLinus Torvalds 	} while (usec > 0);
1751da177e4SLinus Torvalds 	return -ETIMEDOUT;
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /* force HC to halt state from unknown (EHCI spec section 2.3) */
1791da177e4SLinus Torvalds static int ehci_halt (struct ehci_hcd *ehci)
1801da177e4SLinus Torvalds {
181083522d7SBenjamin Herrenschmidt 	u32	temp = ehci_readl(ehci, &ehci->regs->status);
1821da177e4SLinus Torvalds 
18372f30b6fSDavid Brownell 	/* disable any irqs left enabled by previous code */
184083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, 0, &ehci->regs->intr_enable);
18572f30b6fSDavid Brownell 
1861da177e4SLinus Torvalds 	if ((temp & STS_HALT) != 0)
1871da177e4SLinus Torvalds 		return 0;
1881da177e4SLinus Torvalds 
189083522d7SBenjamin Herrenschmidt 	temp = ehci_readl(ehci, &ehci->regs->command);
1901da177e4SLinus Torvalds 	temp &= ~CMD_RUN;
191083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, temp, &ehci->regs->command);
192083522d7SBenjamin Herrenschmidt 	return handshake (ehci, &ehci->regs->status,
193083522d7SBenjamin Herrenschmidt 			  STS_HALT, STS_HALT, 16 * 125);
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds /* put TDI/ARC silicon into EHCI mode */
1971da177e4SLinus Torvalds static void tdi_reset (struct ehci_hcd *ehci)
1981da177e4SLinus Torvalds {
1991da177e4SLinus Torvalds 	u32 __iomem	*reg_ptr;
2001da177e4SLinus Torvalds 	u32		tmp;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	reg_ptr = (u32 __iomem *)(((u8 __iomem *)ehci->regs) + 0x68);
203083522d7SBenjamin Herrenschmidt 	tmp = ehci_readl(ehci, reg_ptr);
2041da177e4SLinus Torvalds 	tmp |= 0x3;
205083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, tmp, reg_ptr);
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds /* reset a non-running (STS_HALT == 1) controller */
2091da177e4SLinus Torvalds static int ehci_reset (struct ehci_hcd *ehci)
2101da177e4SLinus Torvalds {
2111da177e4SLinus Torvalds 	int	retval;
212083522d7SBenjamin Herrenschmidt 	u32	command = ehci_readl(ehci, &ehci->regs->command);
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	command |= CMD_RESET;
2151da177e4SLinus Torvalds 	dbg_cmd (ehci, "reset", command);
216083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, command, &ehci->regs->command);
2171da177e4SLinus Torvalds 	ehci_to_hcd(ehci)->state = HC_STATE_HALT;
2181da177e4SLinus Torvalds 	ehci->next_statechange = jiffies;
219083522d7SBenjamin Herrenschmidt 	retval = handshake (ehci, &ehci->regs->command,
220083522d7SBenjamin Herrenschmidt 			    CMD_RESET, 0, 250 * 1000);
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	if (retval)
2231da177e4SLinus Torvalds 		return retval;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	if (ehci_is_TDI(ehci))
2261da177e4SLinus Torvalds 		tdi_reset (ehci);
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	return retval;
2291da177e4SLinus Torvalds }
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds /* idle the controller (from running) */
2321da177e4SLinus Torvalds static void ehci_quiesce (struct ehci_hcd *ehci)
2331da177e4SLinus Torvalds {
2341da177e4SLinus Torvalds 	u32	temp;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds #ifdef DEBUG
2371da177e4SLinus Torvalds 	if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
2381da177e4SLinus Torvalds 		BUG ();
2391da177e4SLinus Torvalds #endif
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	/* wait for any schedule enables/disables to take effect */
242083522d7SBenjamin Herrenschmidt 	temp = ehci_readl(ehci, &ehci->regs->command) << 10;
2431da177e4SLinus Torvalds 	temp &= STS_ASS | STS_PSS;
244083522d7SBenjamin Herrenschmidt 	if (handshake (ehci, &ehci->regs->status, STS_ASS | STS_PSS,
2451da177e4SLinus Torvalds 				temp, 16 * 125) != 0) {
2461da177e4SLinus Torvalds 		ehci_to_hcd(ehci)->state = HC_STATE_HALT;
2471da177e4SLinus Torvalds 		return;
2481da177e4SLinus Torvalds 	}
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	/* then disable anything that's still active */
251083522d7SBenjamin Herrenschmidt 	temp = ehci_readl(ehci, &ehci->regs->command);
2521da177e4SLinus Torvalds 	temp &= ~(CMD_ASE | CMD_IAAD | CMD_PSE);
253083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, temp, &ehci->regs->command);
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	/* hardware can take 16 microframes to turn off ... */
256083522d7SBenjamin Herrenschmidt 	if (handshake (ehci, &ehci->regs->status, STS_ASS | STS_PSS,
2571da177e4SLinus Torvalds 				0, 16 * 125) != 0) {
2581da177e4SLinus Torvalds 		ehci_to_hcd(ehci)->state = HC_STATE_HALT;
2591da177e4SLinus Torvalds 		return;
2601da177e4SLinus Torvalds 	}
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
2641da177e4SLinus Torvalds 
2657d12e780SDavid Howells static void ehci_work(struct ehci_hcd *ehci);
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds #include "ehci-hub.c"
2681da177e4SLinus Torvalds #include "ehci-mem.c"
2691da177e4SLinus Torvalds #include "ehci-q.c"
2701da177e4SLinus Torvalds #include "ehci-sched.c"
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds static void ehci_watchdog (unsigned long param)
2751da177e4SLinus Torvalds {
2761da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = (struct ehci_hcd *) param;
2771da177e4SLinus Torvalds 	unsigned long		flags;
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	spin_lock_irqsave (&ehci->lock, flags);
2801da177e4SLinus Torvalds 
28164f89798SGreg Kroah-Hartman 	/* lost IAA irqs wedge things badly; seen with a vt8235 */
28264f89798SGreg Kroah-Hartman 	if (ehci->reclaim) {
283083522d7SBenjamin Herrenschmidt 		u32		status = ehci_readl(ehci, &ehci->regs->status);
28464f89798SGreg Kroah-Hartman 		if (status & STS_IAA) {
28564f89798SGreg Kroah-Hartman 			ehci_vdbg (ehci, "lost IAA\n");
28664f89798SGreg Kroah-Hartman 			COUNT (ehci->stats.lost_iaa);
287083522d7SBenjamin Herrenschmidt 			ehci_writel(ehci, STS_IAA, &ehci->regs->status);
28864f89798SGreg Kroah-Hartman 			ehci->reclaim_ready = 1;
28964f89798SGreg Kroah-Hartman 		}
29064f89798SGreg Kroah-Hartman 	}
29164f89798SGreg Kroah-Hartman 
2921da177e4SLinus Torvalds  	/* stop async processing after it's idled a bit */
2931da177e4SLinus Torvalds 	if (test_bit (TIMER_ASYNC_OFF, &ehci->actions))
2941da177e4SLinus Torvalds 		start_unlink_async (ehci, ehci->async);
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	/* ehci could run by timer, without IRQs ... */
2977d12e780SDavid Howells 	ehci_work (ehci);
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ehci->lock, flags);
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
30264a21d02SAleksey Gorelov /* ehci_shutdown kick in for silicon on any bus (not just pci, etc).
30372f30b6fSDavid Brownell  * This forcibly disables dma and IRQs, helping kexec and other cases
30472f30b6fSDavid Brownell  * where the next system software may expect clean state.
30572f30b6fSDavid Brownell  */
30664a21d02SAleksey Gorelov static void
30764a21d02SAleksey Gorelov ehci_shutdown (struct usb_hcd *hcd)
3081da177e4SLinus Torvalds {
3091da177e4SLinus Torvalds 	struct ehci_hcd	*ehci;
3101da177e4SLinus Torvalds 
31164a21d02SAleksey Gorelov 	ehci = hcd_to_ehci (hcd);
31272f30b6fSDavid Brownell 	(void) ehci_halt (ehci);
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	/* make BIOS/etc use companion controller during reboot */
315083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, 0, &ehci->regs->configured_flag);
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
31856c1e26dSDavid Brownell static void ehci_port_power (struct ehci_hcd *ehci, int is_on)
31956c1e26dSDavid Brownell {
32056c1e26dSDavid Brownell 	unsigned port;
32156c1e26dSDavid Brownell 
32256c1e26dSDavid Brownell 	if (!HCS_PPC (ehci->hcs_params))
32356c1e26dSDavid Brownell 		return;
32456c1e26dSDavid Brownell 
32556c1e26dSDavid Brownell 	ehci_dbg (ehci, "...power%s ports...\n", is_on ? "up" : "down");
32656c1e26dSDavid Brownell 	for (port = HCS_N_PORTS (ehci->hcs_params); port > 0; )
32756c1e26dSDavid Brownell 		(void) ehci_hub_control(ehci_to_hcd(ehci),
32856c1e26dSDavid Brownell 				is_on ? SetPortFeature : ClearPortFeature,
32956c1e26dSDavid Brownell 				USB_PORT_FEAT_POWER,
33056c1e26dSDavid Brownell 				port--, NULL, 0);
33156c1e26dSDavid Brownell 	msleep(20);
33256c1e26dSDavid Brownell }
33356c1e26dSDavid Brownell 
3347ff71d6aSMatt Porter /*-------------------------------------------------------------------------*/
3351da177e4SLinus Torvalds 
3367ff71d6aSMatt Porter /*
3377ff71d6aSMatt Porter  * ehci_work is called from some interrupts, timers, and so on.
3387ff71d6aSMatt Porter  * it calls driver completion functions, after dropping ehci->lock.
3397ff71d6aSMatt Porter  */
3407d12e780SDavid Howells static void ehci_work (struct ehci_hcd *ehci)
3417ff71d6aSMatt Porter {
3427ff71d6aSMatt Porter 	timer_action_done (ehci, TIMER_IO_WATCHDOG);
34364f89798SGreg Kroah-Hartman 	if (ehci->reclaim_ready)
34464f89798SGreg Kroah-Hartman 		end_unlink_async (ehci);
3451da177e4SLinus Torvalds 
3467ff71d6aSMatt Porter 	/* another CPU may drop ehci->lock during a schedule scan while
3477ff71d6aSMatt Porter 	 * it reports urb completions.  this flag guards against bogus
3487ff71d6aSMatt Porter 	 * attempts at re-entrant schedule scanning.
3497ff71d6aSMatt Porter 	 */
3507ff71d6aSMatt Porter 	if (ehci->scanning)
3517ff71d6aSMatt Porter 		return;
3527ff71d6aSMatt Porter 	ehci->scanning = 1;
3537d12e780SDavid Howells 	scan_async (ehci);
3547ff71d6aSMatt Porter 	if (ehci->next_uframe != -1)
3557d12e780SDavid Howells 		scan_periodic (ehci);
3567ff71d6aSMatt Porter 	ehci->scanning = 0;
3577ff71d6aSMatt Porter 
3587ff71d6aSMatt Porter 	/* the IO watchdog guards against hardware or driver bugs that
3597ff71d6aSMatt Porter 	 * misplace IRQs, and should let us run completely without IRQs.
3607ff71d6aSMatt Porter 	 * such lossage has been observed on both VT6202 and VT8235.
3617ff71d6aSMatt Porter 	 */
3627ff71d6aSMatt Porter 	if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state) &&
3637ff71d6aSMatt Porter 			(ehci->async->qh_next.ptr != NULL ||
3647ff71d6aSMatt Porter 			 ehci->periodic_sched != 0))
3657ff71d6aSMatt Porter 		timer_action (ehci, TIMER_IO_WATCHDOG);
3667ff71d6aSMatt Porter }
3677ff71d6aSMatt Porter 
3687ff71d6aSMatt Porter static void ehci_stop (struct usb_hcd *hcd)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
3711da177e4SLinus Torvalds 
3727ff71d6aSMatt Porter 	ehci_dbg (ehci, "stop\n");
3731da177e4SLinus Torvalds 
3747ff71d6aSMatt Porter 	/* Turn off port power on all root hub ports. */
37556c1e26dSDavid Brownell 	ehci_port_power (ehci, 0);
3761da177e4SLinus Torvalds 
3777ff71d6aSMatt Porter 	/* no more interrupts ... */
3787ff71d6aSMatt Porter 	del_timer_sync (&ehci->watchdog);
3791da177e4SLinus Torvalds 
3807ff71d6aSMatt Porter 	spin_lock_irq(&ehci->lock);
3817ff71d6aSMatt Porter 	if (HC_IS_RUNNING (hcd->state))
3827ff71d6aSMatt Porter 		ehci_quiesce (ehci);
3831da177e4SLinus Torvalds 
3847ff71d6aSMatt Porter 	ehci_reset (ehci);
385083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, 0, &ehci->regs->intr_enable);
3867ff71d6aSMatt Porter 	spin_unlock_irq(&ehci->lock);
3877ff71d6aSMatt Porter 
3887ff71d6aSMatt Porter 	/* let companion controllers work when we aren't */
389083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, 0, &ehci->regs->configured_flag);
3907ff71d6aSMatt Porter 
3917ff71d6aSMatt Porter 	remove_debug_files (ehci);
3927ff71d6aSMatt Porter 
3937ff71d6aSMatt Porter 	/* root hub is shut down separately (first, when possible) */
3947ff71d6aSMatt Porter 	spin_lock_irq (&ehci->lock);
3957ff71d6aSMatt Porter 	if (ehci->async)
3967d12e780SDavid Howells 		ehci_work (ehci);
3977ff71d6aSMatt Porter 	spin_unlock_irq (&ehci->lock);
3987ff71d6aSMatt Porter 	ehci_mem_cleanup (ehci);
3997ff71d6aSMatt Porter 
4007ff71d6aSMatt Porter #ifdef	EHCI_STATS
4017ff71d6aSMatt Porter 	ehci_dbg (ehci, "irq normal %ld err %ld reclaim %ld (lost %ld)\n",
4027ff71d6aSMatt Porter 		ehci->stats.normal, ehci->stats.error, ehci->stats.reclaim,
4037ff71d6aSMatt Porter 		ehci->stats.lost_iaa);
4047ff71d6aSMatt Porter 	ehci_dbg (ehci, "complete %ld unlink %ld\n",
4057ff71d6aSMatt Porter 		ehci->stats.complete, ehci->stats.unlink);
4061da177e4SLinus Torvalds #endif
4077ff71d6aSMatt Porter 
408083522d7SBenjamin Herrenschmidt 	dbg_status (ehci, "ehci_stop completed",
409083522d7SBenjamin Herrenschmidt 		    ehci_readl(ehci, &ehci->regs->status));
4101da177e4SLinus Torvalds }
4111da177e4SLinus Torvalds 
41218807521SDavid Brownell /* one-time init, only for memory state */
41318807521SDavid Brownell static int ehci_init(struct usb_hcd *hcd)
4141da177e4SLinus Torvalds {
4151da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
4161da177e4SLinus Torvalds 	u32			temp;
4171da177e4SLinus Torvalds 	int			retval;
4181da177e4SLinus Torvalds 	u32			hcc_params;
4191da177e4SLinus Torvalds 
42018807521SDavid Brownell 	spin_lock_init(&ehci->lock);
42118807521SDavid Brownell 
4221da177e4SLinus Torvalds 	init_timer(&ehci->watchdog);
4231da177e4SLinus Torvalds 	ehci->watchdog.function = ehci_watchdog;
4241da177e4SLinus Torvalds 	ehci->watchdog.data = (unsigned long) ehci;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	/*
4271da177e4SLinus Torvalds 	 * hw default: 1K periodic list heads, one per frame.
4281da177e4SLinus Torvalds 	 * periodic_size can shrink by USBCMD update if hcc_params allows.
4291da177e4SLinus Torvalds 	 */
4301da177e4SLinus Torvalds 	ehci->periodic_size = DEFAULT_I_TDPS;
43118807521SDavid Brownell 	if ((retval = ehci_mem_init(ehci, GFP_KERNEL)) < 0)
4321da177e4SLinus Torvalds 		return retval;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	/* controllers may cache some of the periodic schedule ... */
435083522d7SBenjamin Herrenschmidt 	hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
4361da177e4SLinus Torvalds 	if (HCC_ISOC_CACHE(hcc_params))		// full frame cache
4371da177e4SLinus Torvalds 		ehci->i_thresh = 8;
4381da177e4SLinus Torvalds 	else					// N microframes cached
4391da177e4SLinus Torvalds 		ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	ehci->reclaim = NULL;
44264f89798SGreg Kroah-Hartman 	ehci->reclaim_ready = 0;
4431da177e4SLinus Torvalds 	ehci->next_uframe = -1;
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	/*
4461da177e4SLinus Torvalds 	 * dedicate a qh for the async ring head, since we couldn't unlink
4471da177e4SLinus Torvalds 	 * a 'real' qh without stopping the async schedule [4.8].  use it
4481da177e4SLinus Torvalds 	 * as the 'reclamation list head' too.
4491da177e4SLinus Torvalds 	 * its dummy is used in hw_alt_next of many tds, to prevent the qh
4501da177e4SLinus Torvalds 	 * from automatically advancing to the next td after short reads.
4511da177e4SLinus Torvalds 	 */
4521da177e4SLinus Torvalds 	ehci->async->qh_next.qh = NULL;
4531da177e4SLinus Torvalds 	ehci->async->hw_next = QH_NEXT(ehci->async->qh_dma);
4541da177e4SLinus Torvalds 	ehci->async->hw_info1 = cpu_to_le32(QH_HEAD);
4551da177e4SLinus Torvalds 	ehci->async->hw_token = cpu_to_le32(QTD_STS_HALT);
4561da177e4SLinus Torvalds 	ehci->async->hw_qtd_next = EHCI_LIST_END;
4571da177e4SLinus Torvalds 	ehci->async->qh_state = QH_STATE_LINKED;
4581da177e4SLinus Torvalds 	ehci->async->hw_alt_next = QTD_NEXT(ehci->async->dummy->qtd_dma);
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	/* clear interrupt enables, set irq latency */
4611da177e4SLinus Torvalds 	if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
4621da177e4SLinus Torvalds 		log2_irq_thresh = 0;
4631da177e4SLinus Torvalds 	temp = 1 << (16 + log2_irq_thresh);
4641da177e4SLinus Torvalds 	if (HCC_CANPARK(hcc_params)) {
4651da177e4SLinus Torvalds 		/* HW default park == 3, on hardware that supports it (like
4661da177e4SLinus Torvalds 		 * NVidia and ALI silicon), maximizes throughput on the async
4671da177e4SLinus Torvalds 		 * schedule by avoiding QH fetches between transfers.
4681da177e4SLinus Torvalds 		 *
4691da177e4SLinus Torvalds 		 * With fast usb storage devices and NForce2, "park" seems to
4701da177e4SLinus Torvalds 		 * make problems:  throughput reduction (!), data errors...
4711da177e4SLinus Torvalds 		 */
4721da177e4SLinus Torvalds 		if (park) {
4731da177e4SLinus Torvalds 			park = min(park, (unsigned) 3);
4741da177e4SLinus Torvalds 			temp |= CMD_PARK;
4751da177e4SLinus Torvalds 			temp |= park << 8;
4761da177e4SLinus Torvalds 		}
47718807521SDavid Brownell 		ehci_dbg(ehci, "park %d\n", park);
4781da177e4SLinus Torvalds 	}
4791da177e4SLinus Torvalds 	if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
4801da177e4SLinus Torvalds 		/* periodic schedule size can be smaller than default */
4811da177e4SLinus Torvalds 		temp &= ~(3 << 2);
4821da177e4SLinus Torvalds 		temp |= (EHCI_TUNE_FLS << 2);
4831da177e4SLinus Torvalds 		switch (EHCI_TUNE_FLS) {
4841da177e4SLinus Torvalds 		case 0: ehci->periodic_size = 1024; break;
4851da177e4SLinus Torvalds 		case 1: ehci->periodic_size = 512; break;
4861da177e4SLinus Torvalds 		case 2: ehci->periodic_size = 256; break;
4871da177e4SLinus Torvalds 		default:	BUG();
4881da177e4SLinus Torvalds 		}
4891da177e4SLinus Torvalds 	}
49018807521SDavid Brownell 	ehci->command = temp;
49118807521SDavid Brownell 
49218807521SDavid Brownell 	return 0;
49318807521SDavid Brownell }
49418807521SDavid Brownell 
49518807521SDavid Brownell /* start HC running; it's halted, ehci_init() has been run (once) */
49618807521SDavid Brownell static int ehci_run (struct usb_hcd *hcd)
49718807521SDavid Brownell {
49818807521SDavid Brownell 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
49918807521SDavid Brownell 	int			retval;
50018807521SDavid Brownell 	u32			temp;
50118807521SDavid Brownell 	u32			hcc_params;
50218807521SDavid Brownell 
50318807521SDavid Brownell 	/* EHCI spec section 4.1 */
50418807521SDavid Brownell 	if ((retval = ehci_reset(ehci)) != 0) {
50518807521SDavid Brownell 		ehci_mem_cleanup(ehci);
50618807521SDavid Brownell 		return retval;
50718807521SDavid Brownell 	}
508083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
509083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
51018807521SDavid Brownell 
51118807521SDavid Brownell 	/*
51218807521SDavid Brownell 	 * hcc_params controls whether ehci->regs->segment must (!!!)
51318807521SDavid Brownell 	 * be used; it constrains QH/ITD/SITD and QTD locations.
51418807521SDavid Brownell 	 * pci_pool consistent memory always uses segment zero.
51518807521SDavid Brownell 	 * streaming mappings for I/O buffers, like pci_map_single(),
51618807521SDavid Brownell 	 * can return segments above 4GB, if the device allows.
51718807521SDavid Brownell 	 *
51818807521SDavid Brownell 	 * NOTE:  the dma mask is visible through dma_supported(), so
51918807521SDavid Brownell 	 * drivers can pass this info along ... like NETIF_F_HIGHDMA,
52018807521SDavid Brownell 	 * Scsi_Host.highmem_io, and so forth.  It's readonly to all
52118807521SDavid Brownell 	 * host side drivers though.
52218807521SDavid Brownell 	 */
523083522d7SBenjamin Herrenschmidt 	hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
52418807521SDavid Brownell 	if (HCC_64BIT_ADDR(hcc_params)) {
525083522d7SBenjamin Herrenschmidt 		ehci_writel(ehci, 0, &ehci->regs->segment);
52618807521SDavid Brownell #if 0
52718807521SDavid Brownell // this is deeply broken on almost all architectures
52818807521SDavid Brownell 		if (!dma_set_mask(hcd->self.controller, DMA_64BIT_MASK))
52918807521SDavid Brownell 			ehci_info(ehci, "enabled 64bit DMA\n");
53018807521SDavid Brownell #endif
53118807521SDavid Brownell 	}
53218807521SDavid Brownell 
53318807521SDavid Brownell 
5341da177e4SLinus Torvalds 	// Philips, Intel, and maybe others need CMD_RUN before the
5351da177e4SLinus Torvalds 	// root hub will detect new devices (why?); NEC doesn't
53618807521SDavid Brownell 	ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
53718807521SDavid Brownell 	ehci->command |= CMD_RUN;
538083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, ehci->command, &ehci->regs->command);
53918807521SDavid Brownell 	dbg_cmd (ehci, "init", ehci->command);
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	/*
5421da177e4SLinus Torvalds 	 * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
5431da177e4SLinus Torvalds 	 * are explicitly handed to companion controller(s), so no TT is
5441da177e4SLinus Torvalds 	 * involved with the root hub.  (Except where one is integrated,
5451da177e4SLinus Torvalds 	 * and there's no companion controller unless maybe for USB OTG.)
5461da177e4SLinus Torvalds 	 */
5471da177e4SLinus Torvalds 	hcd->state = HC_STATE_RUNNING;
548083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
549083522d7SBenjamin Herrenschmidt 	ehci_readl(ehci, &ehci->regs->command);	/* unblock posted writes */
5501da177e4SLinus Torvalds 
551083522d7SBenjamin Herrenschmidt 	temp = HC_VERSION(ehci_readl(ehci, &ehci->caps->hc_capbase));
5521da177e4SLinus Torvalds 	ehci_info (ehci,
55393f1a47cSDavid Brownell 		"USB %x.%x started, EHCI %x.%02x, driver %s%s\n",
5547ff71d6aSMatt Porter 		((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f),
55593f1a47cSDavid Brownell 		temp >> 8, temp & 0xff, DRIVER_VERSION,
55693f1a47cSDavid Brownell 		ignore_oc ? ", overcurrent ignored" : "");
5571da177e4SLinus Torvalds 
558083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, INTR_MASK,
559083522d7SBenjamin Herrenschmidt 		    &ehci->regs->intr_enable); /* Turn On Interrupts */
5601da177e4SLinus Torvalds 
56118807521SDavid Brownell 	/* GRR this is run-once init(), being done every time the HC starts.
56218807521SDavid Brownell 	 * So long as they're part of class devices, we can't do it init()
56318807521SDavid Brownell 	 * since the class device isn't created that early.
56418807521SDavid Brownell 	 */
5651da177e4SLinus Torvalds 	create_debug_files(ehci);
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	return 0;
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
5711da177e4SLinus Torvalds 
5727d12e780SDavid Howells static irqreturn_t ehci_irq (struct usb_hcd *hcd)
5731da177e4SLinus Torvalds {
5741da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
5751da177e4SLinus Torvalds 	u32			status;
5761da177e4SLinus Torvalds 	int			bh;
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	spin_lock (&ehci->lock);
5791da177e4SLinus Torvalds 
580083522d7SBenjamin Herrenschmidt 	status = ehci_readl(ehci, &ehci->regs->status);
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	/* e.g. cardbus physical eject */
5831da177e4SLinus Torvalds 	if (status == ~(u32) 0) {
5841da177e4SLinus Torvalds 		ehci_dbg (ehci, "device removed\n");
5851da177e4SLinus Torvalds 		goto dead;
5861da177e4SLinus Torvalds 	}
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	status &= INTR_MASK;
5891da177e4SLinus Torvalds 	if (!status) {			/* irq sharing? */
5901da177e4SLinus Torvalds 		spin_unlock(&ehci->lock);
5911da177e4SLinus Torvalds 		return IRQ_NONE;
5921da177e4SLinus Torvalds 	}
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	/* clear (just) interrupts */
595083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, status, &ehci->regs->status);
596083522d7SBenjamin Herrenschmidt 	ehci_readl(ehci, &ehci->regs->command);	/* unblock posted write */
5971da177e4SLinus Torvalds 	bh = 0;
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds #ifdef	EHCI_VERBOSE_DEBUG
6001da177e4SLinus Torvalds 	/* unrequested/ignored: Frame List Rollover */
6011da177e4SLinus Torvalds 	dbg_status (ehci, "irq", status);
6021da177e4SLinus Torvalds #endif
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	/* INT, ERR, and IAA interrupt rates can be throttled */
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	/* normal [4.15.1.2] or error [4.15.1.1] completion */
6071da177e4SLinus Torvalds 	if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
6081da177e4SLinus Torvalds 		if (likely ((status & STS_ERR) == 0))
6091da177e4SLinus Torvalds 			COUNT (ehci->stats.normal);
6101da177e4SLinus Torvalds 		else
6111da177e4SLinus Torvalds 			COUNT (ehci->stats.error);
6121da177e4SLinus Torvalds 		bh = 1;
6131da177e4SLinus Torvalds 	}
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 	/* complete the unlinking of some qh [4.15.2.3] */
6161da177e4SLinus Torvalds 	if (status & STS_IAA) {
6171da177e4SLinus Torvalds 		COUNT (ehci->stats.reclaim);
61864f89798SGreg Kroah-Hartman 		ehci->reclaim_ready = 1;
6191da177e4SLinus Torvalds 		bh = 1;
6201da177e4SLinus Torvalds 	}
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	/* remote wakeup [4.3.1] */
623d97cc2f2SDavid Brownell 	if (status & STS_PCD) {
6241da177e4SLinus Torvalds 		unsigned	i = HCS_N_PORTS (ehci->hcs_params);
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 		/* resume root hub? */
627083522d7SBenjamin Herrenschmidt 		if (!(ehci_readl(ehci, &ehci->regs->command) & CMD_RUN))
6288c03356aSAlan Stern 			usb_hcd_resume_root_hub(hcd);
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 		while (i--) {
631083522d7SBenjamin Herrenschmidt 			int pstatus = ehci_readl(ehci,
632083522d7SBenjamin Herrenschmidt 						 &ehci->regs->port_status [i]);
633b972b68cSDavid Brownell 
634b972b68cSDavid Brownell 			if (pstatus & PORT_OWNER)
6351da177e4SLinus Torvalds 				continue;
636b972b68cSDavid Brownell 			if (!(pstatus & PORT_RESUME)
6371da177e4SLinus Torvalds 					|| ehci->reset_done [i] != 0)
6381da177e4SLinus Torvalds 				continue;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 			/* start 20 msec resume signaling from this port,
6411da177e4SLinus Torvalds 			 * and make khubd collect PORT_STAT_C_SUSPEND to
6421da177e4SLinus Torvalds 			 * stop that signaling.
6431da177e4SLinus Torvalds 			 */
6441da177e4SLinus Torvalds 			ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
6451da177e4SLinus Torvalds 			ehci_dbg (ehci, "port %d remote wakeup\n", i + 1);
6461da177e4SLinus Torvalds 		}
6471da177e4SLinus Torvalds 	}
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	/* PCI errors [4.15.2.4] */
6501da177e4SLinus Torvalds 	if (unlikely ((status & STS_FATAL) != 0)) {
6511da177e4SLinus Torvalds 		/* bogus "fatal" IRQs appear on some chips... why?  */
652083522d7SBenjamin Herrenschmidt 		status = ehci_readl(ehci, &ehci->regs->status);
653083522d7SBenjamin Herrenschmidt 		dbg_cmd (ehci, "fatal", ehci_readl(ehci,
654083522d7SBenjamin Herrenschmidt 						   &ehci->regs->command));
6551da177e4SLinus Torvalds 		dbg_status (ehci, "fatal", status);
6561da177e4SLinus Torvalds 		if (status & STS_HALT) {
6571da177e4SLinus Torvalds 			ehci_err (ehci, "fatal error\n");
6581da177e4SLinus Torvalds dead:
6591da177e4SLinus Torvalds 			ehci_reset (ehci);
660083522d7SBenjamin Herrenschmidt 			ehci_writel(ehci, 0, &ehci->regs->configured_flag);
6611da177e4SLinus Torvalds 			/* generic layer kills/unlinks all urbs, then
6621da177e4SLinus Torvalds 			 * uses ehci_stop to clean up the rest
6631da177e4SLinus Torvalds 			 */
6641da177e4SLinus Torvalds 			bh = 1;
6651da177e4SLinus Torvalds 		}
6661da177e4SLinus Torvalds 	}
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	if (bh)
6697d12e780SDavid Howells 		ehci_work (ehci);
6701da177e4SLinus Torvalds 	spin_unlock (&ehci->lock);
6711da177e4SLinus Torvalds 	return IRQ_HANDLED;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds /*
6771da177e4SLinus Torvalds  * non-error returns are a promise to giveback() the urb later
6781da177e4SLinus Torvalds  * we drop ownership so next owner (or urb unlink) can get it
6791da177e4SLinus Torvalds  *
6801da177e4SLinus Torvalds  * urb + dev is in hcd.self.controller.urb_list
6811da177e4SLinus Torvalds  * we're queueing TDs onto software and hardware lists
6821da177e4SLinus Torvalds  *
6831da177e4SLinus Torvalds  * hcd-specific init for hcpriv hasn't been done yet
6841da177e4SLinus Torvalds  *
6851da177e4SLinus Torvalds  * NOTE:  control, bulk, and interrupt share the same code to append TDs
6861da177e4SLinus Torvalds  * to a (possibly active) QH, and the same QH scanning code.
6871da177e4SLinus Torvalds  */
6881da177e4SLinus Torvalds static int ehci_urb_enqueue (
6891da177e4SLinus Torvalds 	struct usb_hcd	*hcd,
6901da177e4SLinus Torvalds 	struct usb_host_endpoint *ep,
6911da177e4SLinus Torvalds 	struct urb	*urb,
69255016f10SAl Viro 	gfp_t		mem_flags
6931da177e4SLinus Torvalds ) {
6941da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
6951da177e4SLinus Torvalds 	struct list_head	qtd_list;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	INIT_LIST_HEAD (&qtd_list);
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 	switch (usb_pipetype (urb->pipe)) {
7001da177e4SLinus Torvalds 	// case PIPE_CONTROL:
7011da177e4SLinus Torvalds 	// case PIPE_BULK:
7021da177e4SLinus Torvalds 	default:
7031da177e4SLinus Torvalds 		if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
7041da177e4SLinus Torvalds 			return -ENOMEM;
7051da177e4SLinus Torvalds 		return submit_async (ehci, ep, urb, &qtd_list, mem_flags);
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	case PIPE_INTERRUPT:
7081da177e4SLinus Torvalds 		if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
7091da177e4SLinus Torvalds 			return -ENOMEM;
7101da177e4SLinus Torvalds 		return intr_submit (ehci, ep, urb, &qtd_list, mem_flags);
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	case PIPE_ISOCHRONOUS:
7131da177e4SLinus Torvalds 		if (urb->dev->speed == USB_SPEED_HIGH)
7141da177e4SLinus Torvalds 			return itd_submit (ehci, urb, mem_flags);
7151da177e4SLinus Torvalds 		else
7161da177e4SLinus Torvalds 			return sitd_submit (ehci, urb, mem_flags);
7171da177e4SLinus Torvalds 	}
7181da177e4SLinus Torvalds }
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds static void unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
7211da177e4SLinus Torvalds {
72264f89798SGreg Kroah-Hartman 	/* if we need to use IAA and it's busy, defer */
72364f89798SGreg Kroah-Hartman 	if (qh->qh_state == QH_STATE_LINKED
72464f89798SGreg Kroah-Hartman 			&& ehci->reclaim
72564f89798SGreg Kroah-Hartman 			&& HC_IS_RUNNING (ehci_to_hcd(ehci)->state)) {
7261da177e4SLinus Torvalds 		struct ehci_qh		*last;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 		for (last = ehci->reclaim;
7291da177e4SLinus Torvalds 				last->reclaim;
7301da177e4SLinus Torvalds 				last = last->reclaim)
7311da177e4SLinus Torvalds 			continue;
7321da177e4SLinus Torvalds 		qh->qh_state = QH_STATE_UNLINK_WAIT;
7331da177e4SLinus Torvalds 		last->reclaim = qh;
7341da177e4SLinus Torvalds 
73564f89798SGreg Kroah-Hartman 	/* bypass IAA if the hc can't care */
73664f89798SGreg Kroah-Hartman 	} else if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state) && ehci->reclaim)
73764f89798SGreg Kroah-Hartman 		end_unlink_async (ehci);
73864f89798SGreg Kroah-Hartman 
73964f89798SGreg Kroah-Hartman 	/* something else might have unlinked the qh by now */
74064f89798SGreg Kroah-Hartman 	if (qh->qh_state == QH_STATE_LINKED)
7411da177e4SLinus Torvalds 		start_unlink_async (ehci, qh);
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds /* remove from hardware lists
7451da177e4SLinus Torvalds  * completions normally happen asynchronously
7461da177e4SLinus Torvalds  */
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds static int ehci_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
7491da177e4SLinus Torvalds {
7501da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
7511da177e4SLinus Torvalds 	struct ehci_qh		*qh;
7521da177e4SLinus Torvalds 	unsigned long		flags;
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	spin_lock_irqsave (&ehci->lock, flags);
7551da177e4SLinus Torvalds 	switch (usb_pipetype (urb->pipe)) {
7561da177e4SLinus Torvalds 	// case PIPE_CONTROL:
7571da177e4SLinus Torvalds 	// case PIPE_BULK:
7581da177e4SLinus Torvalds 	default:
7591da177e4SLinus Torvalds 		qh = (struct ehci_qh *) urb->hcpriv;
7601da177e4SLinus Torvalds 		if (!qh)
7611da177e4SLinus Torvalds 			break;
7621da177e4SLinus Torvalds 		unlink_async (ehci, qh);
7631da177e4SLinus Torvalds 		break;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	case PIPE_INTERRUPT:
7661da177e4SLinus Torvalds 		qh = (struct ehci_qh *) urb->hcpriv;
7671da177e4SLinus Torvalds 		if (!qh)
7681da177e4SLinus Torvalds 			break;
7691da177e4SLinus Torvalds 		switch (qh->qh_state) {
7701da177e4SLinus Torvalds 		case QH_STATE_LINKED:
7711da177e4SLinus Torvalds 			intr_deschedule (ehci, qh);
7721da177e4SLinus Torvalds 			/* FALL THROUGH */
7731da177e4SLinus Torvalds 		case QH_STATE_IDLE:
7747d12e780SDavid Howells 			qh_completions (ehci, qh);
7751da177e4SLinus Torvalds 			break;
7761da177e4SLinus Torvalds 		default:
7771da177e4SLinus Torvalds 			ehci_dbg (ehci, "bogus qh %p state %d\n",
7781da177e4SLinus Torvalds 					qh, qh->qh_state);
7791da177e4SLinus Torvalds 			goto done;
7801da177e4SLinus Torvalds 		}
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 		/* reschedule QH iff another request is queued */
7831da177e4SLinus Torvalds 		if (!list_empty (&qh->qtd_list)
7841da177e4SLinus Torvalds 				&& HC_IS_RUNNING (hcd->state)) {
7851da177e4SLinus Torvalds 			int status;
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 			status = qh_schedule (ehci, qh);
7881da177e4SLinus Torvalds 			spin_unlock_irqrestore (&ehci->lock, flags);
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 			if (status != 0) {
7911da177e4SLinus Torvalds 				// shouldn't happen often, but ...
7921da177e4SLinus Torvalds 				// FIXME kill those tds' urbs
7931da177e4SLinus Torvalds 				err ("can't reschedule qh %p, err %d",
7941da177e4SLinus Torvalds 					qh, status);
7951da177e4SLinus Torvalds 			}
7961da177e4SLinus Torvalds 			return status;
7971da177e4SLinus Torvalds 		}
7981da177e4SLinus Torvalds 		break;
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	case PIPE_ISOCHRONOUS:
8011da177e4SLinus Torvalds 		// itd or sitd ...
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 		// wait till next completion, do it then.
8041da177e4SLinus Torvalds 		// completion irqs can wait up to 1024 msec,
8051da177e4SLinus Torvalds 		break;
8061da177e4SLinus Torvalds 	}
8071da177e4SLinus Torvalds done:
8081da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ehci->lock, flags);
8091da177e4SLinus Torvalds 	return 0;
8101da177e4SLinus Torvalds }
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds // bulk qh holds the data toggle
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds static void
8171da177e4SLinus Torvalds ehci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
8181da177e4SLinus Torvalds {
8191da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
8201da177e4SLinus Torvalds 	unsigned long		flags;
8211da177e4SLinus Torvalds 	struct ehci_qh		*qh, *tmp;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	/* ASSERT:  any requests/urbs are being unlinked */
8241da177e4SLinus Torvalds 	/* ASSERT:  nobody can be submitting urbs for this any more */
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds rescan:
8271da177e4SLinus Torvalds 	spin_lock_irqsave (&ehci->lock, flags);
8281da177e4SLinus Torvalds 	qh = ep->hcpriv;
8291da177e4SLinus Torvalds 	if (!qh)
8301da177e4SLinus Torvalds 		goto done;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	/* endpoints can be iso streams.  for now, we don't
8331da177e4SLinus Torvalds 	 * accelerate iso completions ... so spin a while.
8341da177e4SLinus Torvalds 	 */
8351da177e4SLinus Torvalds 	if (qh->hw_info1 == 0) {
8361da177e4SLinus Torvalds 		ehci_vdbg (ehci, "iso delay\n");
8371da177e4SLinus Torvalds 		goto idle_timeout;
8381da177e4SLinus Torvalds 	}
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	if (!HC_IS_RUNNING (hcd->state))
8411da177e4SLinus Torvalds 		qh->qh_state = QH_STATE_IDLE;
8421da177e4SLinus Torvalds 	switch (qh->qh_state) {
8431da177e4SLinus Torvalds 	case QH_STATE_LINKED:
8441da177e4SLinus Torvalds 		for (tmp = ehci->async->qh_next.qh;
8451da177e4SLinus Torvalds 				tmp && tmp != qh;
8461da177e4SLinus Torvalds 				tmp = tmp->qh_next.qh)
8471da177e4SLinus Torvalds 			continue;
8481da177e4SLinus Torvalds 		/* periodic qh self-unlinks on empty */
8491da177e4SLinus Torvalds 		if (!tmp)
8501da177e4SLinus Torvalds 			goto nogood;
8511da177e4SLinus Torvalds 		unlink_async (ehci, qh);
8521da177e4SLinus Torvalds 		/* FALL THROUGH */
8531da177e4SLinus Torvalds 	case QH_STATE_UNLINK:		/* wait for hw to finish? */
8541da177e4SLinus Torvalds idle_timeout:
8551da177e4SLinus Torvalds 		spin_unlock_irqrestore (&ehci->lock, flags);
85622c43863SNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
8571da177e4SLinus Torvalds 		goto rescan;
8581da177e4SLinus Torvalds 	case QH_STATE_IDLE:		/* fully unlinked */
8591da177e4SLinus Torvalds 		if (list_empty (&qh->qtd_list)) {
8601da177e4SLinus Torvalds 			qh_put (qh);
8611da177e4SLinus Torvalds 			break;
8621da177e4SLinus Torvalds 		}
8631da177e4SLinus Torvalds 		/* else FALL THROUGH */
8641da177e4SLinus Torvalds 	default:
8651da177e4SLinus Torvalds nogood:
8661da177e4SLinus Torvalds 		/* caller was supposed to have unlinked any requests;
8671da177e4SLinus Torvalds 		 * that's not our job.  just leak this memory.
8681da177e4SLinus Torvalds 		 */
8691da177e4SLinus Torvalds 		ehci_err (ehci, "qh %p (#%02x) state %d%s\n",
8701da177e4SLinus Torvalds 			qh, ep->desc.bEndpointAddress, qh->qh_state,
8711da177e4SLinus Torvalds 			list_empty (&qh->qtd_list) ? "" : "(has tds)");
8721da177e4SLinus Torvalds 		break;
8731da177e4SLinus Torvalds 	}
8741da177e4SLinus Torvalds 	ep->hcpriv = NULL;
8751da177e4SLinus Torvalds done:
8761da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ehci->lock, flags);
8771da177e4SLinus Torvalds 	return;
8781da177e4SLinus Torvalds }
8791da177e4SLinus Torvalds 
8807ff71d6aSMatt Porter static int ehci_get_frame (struct usb_hcd *hcd)
8817ff71d6aSMatt Porter {
8827ff71d6aSMatt Porter 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
883083522d7SBenjamin Herrenschmidt 	return (ehci_readl(ehci, &ehci->regs->frame_index) >> 3) %
884083522d7SBenjamin Herrenschmidt 		ehci->periodic_size;
8857ff71d6aSMatt Porter }
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds #define DRIVER_INFO DRIVER_VERSION " " DRIVER_DESC
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds MODULE_DESCRIPTION (DRIVER_INFO);
8921da177e4SLinus Torvalds MODULE_AUTHOR (DRIVER_AUTHOR);
8931da177e4SLinus Torvalds MODULE_LICENSE ("GPL");
8941da177e4SLinus Torvalds 
8957ff71d6aSMatt Porter #ifdef CONFIG_PCI
8967ff71d6aSMatt Porter #include "ehci-pci.c"
89701cced25SKumar Gala #define	PCI_DRIVER		ehci_pci_driver
8987ff71d6aSMatt Porter #endif
8991da177e4SLinus Torvalds 
900a11570f2SLi Yang #ifdef CONFIG_MPC834x
90180cb9aeeSRandy Vinson #include "ehci-fsl.c"
90201cced25SKumar Gala #define	PLATFORM_DRIVER		ehci_fsl_driver
90380cb9aeeSRandy Vinson #endif
90480cb9aeeSRandy Vinson 
905dfbaa7d8SRalf Baechle #ifdef CONFIG_SOC_AU1200
90676fa9a24SJordan Crouse #include "ehci-au1xxx.c"
90701cced25SKumar Gala #define	PLATFORM_DRIVER		ehci_hcd_au1xxx_driver
90876fa9a24SJordan Crouse #endif
90976fa9a24SJordan Crouse 
910*ad75a410SGeoff Levand #ifdef CONFIG_PPC_PS3
911*ad75a410SGeoff Levand #include "ehci-ps3.c"
912*ad75a410SGeoff Levand #define	PS3_SYSTEM_BUS_DRIVER	ps3_ehci_sb_driver
913*ad75a410SGeoff Levand #endif
914*ad75a410SGeoff Levand 
915*ad75a410SGeoff Levand #if !defined(PCI_DRIVER) && !defined(PLATFORM_DRIVER) && \
916*ad75a410SGeoff Levand     !defined(PS3_SYSTEM_BUS_DRIVER)
9177ff71d6aSMatt Porter #error "missing bus glue for ehci-hcd"
9187ff71d6aSMatt Porter #endif
91901cced25SKumar Gala 
92001cced25SKumar Gala static int __init ehci_hcd_init(void)
92101cced25SKumar Gala {
92201cced25SKumar Gala 	int retval = 0;
92301cced25SKumar Gala 
92401cced25SKumar Gala 	pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
92501cced25SKumar Gala 		 hcd_name,
92601cced25SKumar Gala 		 sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
92701cced25SKumar Gala 		 sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
92801cced25SKumar Gala 
92901cced25SKumar Gala #ifdef PLATFORM_DRIVER
93001cced25SKumar Gala 	retval = platform_driver_register(&PLATFORM_DRIVER);
93101cced25SKumar Gala 	if (retval < 0)
93201cced25SKumar Gala 		return retval;
93301cced25SKumar Gala #endif
93401cced25SKumar Gala 
93501cced25SKumar Gala #ifdef PCI_DRIVER
93601cced25SKumar Gala 	retval = pci_register_driver(&PCI_DRIVER);
93701cced25SKumar Gala 	if (retval < 0) {
93801cced25SKumar Gala #ifdef PLATFORM_DRIVER
93901cced25SKumar Gala 		platform_driver_unregister(&PLATFORM_DRIVER);
94001cced25SKumar Gala #endif
941*ad75a410SGeoff Levand 		return retval;
942*ad75a410SGeoff Levand 	}
943*ad75a410SGeoff Levand #endif
944*ad75a410SGeoff Levand 
945*ad75a410SGeoff Levand #ifdef PS3_SYSTEM_BUS_DRIVER
946*ad75a410SGeoff Levand 	retval = ps3_system_bus_driver_register(&PS3_SYSTEM_BUS_DRIVER);
947*ad75a410SGeoff Levand 	if (retval < 0) {
948*ad75a410SGeoff Levand #ifdef PLATFORM_DRIVER
949*ad75a410SGeoff Levand 		platform_driver_unregister(&PLATFORM_DRIVER);
950*ad75a410SGeoff Levand #endif
951*ad75a410SGeoff Levand #ifdef PCI_DRIVER
952*ad75a410SGeoff Levand 		pci_unregister_driver(&PCI_DRIVER);
953*ad75a410SGeoff Levand #endif
954*ad75a410SGeoff Levand 		return retval;
95501cced25SKumar Gala 	}
95601cced25SKumar Gala #endif
95701cced25SKumar Gala 
95801cced25SKumar Gala 	return retval;
95901cced25SKumar Gala }
96001cced25SKumar Gala module_init(ehci_hcd_init);
96101cced25SKumar Gala 
96201cced25SKumar Gala static void __exit ehci_hcd_cleanup(void)
96301cced25SKumar Gala {
96401cced25SKumar Gala #ifdef PLATFORM_DRIVER
96501cced25SKumar Gala 	platform_driver_unregister(&PLATFORM_DRIVER);
96601cced25SKumar Gala #endif
96701cced25SKumar Gala #ifdef PCI_DRIVER
96801cced25SKumar Gala 	pci_unregister_driver(&PCI_DRIVER);
96901cced25SKumar Gala #endif
970*ad75a410SGeoff Levand #ifdef PS3_SYSTEM_BUS_DRIVER
971*ad75a410SGeoff Levand 	ps3_system_bus_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
972*ad75a410SGeoff Levand #endif
97301cced25SKumar Gala }
97401cced25SKumar Gala module_exit(ehci_hcd_cleanup);
97501cced25SKumar Gala 
976