xref: /linux/drivers/usb/host/ehci-hcd.c (revision 7ec462100ef9142344ddbf86f2c3008b97acddbe)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
21da177e4SLinus Torvalds /*
3578333abSAlan Stern  * Enhanced Host Controller Interface (EHCI) driver for USB.
4578333abSAlan Stern  *
5578333abSAlan Stern  * Maintainer: Alan Stern <stern@rowland.harvard.edu>
6578333abSAlan Stern  *
71da177e4SLinus Torvalds  * Copyright (c) 2000-2004 by David Brownell
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/module.h>
111da177e4SLinus Torvalds #include <linux/pci.h>
121da177e4SLinus Torvalds #include <linux/dmapool.h>
131da177e4SLinus Torvalds #include <linux/kernel.h>
141da177e4SLinus Torvalds #include <linux/delay.h>
151da177e4SLinus Torvalds #include <linux/ioport.h>
161da177e4SLinus Torvalds #include <linux/sched.h>
173c04e20eSMing Lei #include <linux/vmalloc.h>
181da177e4SLinus Torvalds #include <linux/errno.h>
191da177e4SLinus Torvalds #include <linux/init.h>
20d58b4bccSAlan Stern #include <linux/hrtimer.h>
211da177e4SLinus Torvalds #include <linux/list.h>
221da177e4SLinus Torvalds #include <linux/interrupt.h>
231da177e4SLinus Torvalds #include <linux/usb.h>
2427729aadSEric Lescouet #include <linux/usb/hcd.h>
2529231826SQuentin Perret #include <linux/usb/otg.h>
261da177e4SLinus Torvalds #include <linux/moduleparam.h>
271da177e4SLinus Torvalds #include <linux/dma-mapping.h>
28694cc208STony Jones #include <linux/debugfs.h>
29aad06846SGuenter Roeck #include <linux/platform_device.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include <asm/byteorder.h>
331da177e4SLinus Torvalds #include <asm/io.h>
341da177e4SLinus Torvalds #include <asm/irq.h>
35*5f60d5f6SAl Viro #include <linux/unaligned.h>
361da177e4SLinus Torvalds 
37df7c1ca2SGeoff Levand #if defined(CONFIG_PPC_PS3)
38df7c1ca2SGeoff Levand #include <asm/firmware.h>
39df7c1ca2SGeoff Levand #endif
40df7c1ca2SGeoff Levand 
411da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /*
441da177e4SLinus Torvalds  * EHCI hc_driver implementation ... experimental, incomplete.
451da177e4SLinus Torvalds  * Based on the final 1.0 register interface specification.
461da177e4SLinus Torvalds  *
471da177e4SLinus Torvalds  * USB 2.0 shows up in upcoming www.pcmcia.org technology.
481da177e4SLinus Torvalds  * First was PCMCIA, like ISA; then CardBus, which is PCI.
491da177e4SLinus Torvalds  * Next comes "CardBay", using USB 2.0 signals.
501da177e4SLinus Torvalds  *
511da177e4SLinus Torvalds  * Contains additional contributions by Brad Hards, Rory Bolt, and others.
521da177e4SLinus Torvalds  * Special thanks to Intel and VIA for providing host controllers to
531da177e4SLinus Torvalds  * test this driver on, and Cypress (including In-System Design) for
541da177e4SLinus Torvalds  * providing early devices for those host controllers to talk to!
551da177e4SLinus Torvalds  */
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds #define DRIVER_AUTHOR "David Brownell"
581da177e4SLinus Torvalds #define DRIVER_DESC "USB 2.0 'Enhanced' Host Controller (EHCI) Driver"
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds static const char	hcd_name [] = "ehci_hcd";
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds #undef EHCI_URB_TRACE
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /* magic numbers that can affect system performance */
661da177e4SLinus Torvalds #define	EHCI_TUNE_CERR		3	/* 0-3 qtd retries; 0 == don't stop */
671da177e4SLinus Torvalds #define	EHCI_TUNE_RL_HS		4	/* nak throttle; see 4.9 */
681da177e4SLinus Torvalds #define	EHCI_TUNE_RL_TT		0
691da177e4SLinus Torvalds #define	EHCI_TUNE_MULT_HS	1	/* 1-3 transactions/uframe; 4.10.3 */
701da177e4SLinus Torvalds #define	EHCI_TUNE_MULT_TT	1
71ffda0803SAlan Stern /*
72ffda0803SAlan Stern  * Some drivers think it's safe to schedule isochronous transfers more than
73ffda0803SAlan Stern  * 256 ms into the future (partly as a result of an old bug in the scheduling
74ffda0803SAlan Stern  * code).  In an attempt to avoid trouble, we will use a minimum scheduling
75ffda0803SAlan Stern  * length of 512 frames instead of 256.
76ffda0803SAlan Stern  */
77ffda0803SAlan Stern #define	EHCI_TUNE_FLS		1	/* (medium) 512-frame schedule */
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /* Initial IRQ latency:  faster than hw default */
8003a674f5SJason Wang static int log2_irq_thresh;		// 0 to 6
811da177e4SLinus Torvalds module_param (log2_irq_thresh, int, S_IRUGO);
821da177e4SLinus Torvalds MODULE_PARM_DESC (log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /* initial park setting:  slower than hw default */
8503a674f5SJason Wang static unsigned park;
861da177e4SLinus Torvalds module_param (park, uint, S_IRUGO);
871da177e4SLinus Torvalds MODULE_PARM_DESC (park, "park setting; 1-3 back-to-back async packets");
881da177e4SLinus Torvalds 
8993f1a47cSDavid Brownell /* for flakey hardware, ignore overcurrent indicators */
90900937c0SGeyslan G. Bem static bool ignore_oc;
9193f1a47cSDavid Brownell module_param (ignore_oc, bool, S_IRUGO);
9293f1a47cSDavid Brownell MODULE_PARM_DESC (ignore_oc, "ignore bogus hardware overcurrent indications");
9393f1a47cSDavid Brownell 
941da177e4SLinus Torvalds #define	INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds #include "ehci.h"
99ad93562bSAndiry Xu #include "pci-quirks.h"
1001da177e4SLinus Torvalds 
101b35c5009SAlan Stern static void compute_tt_budget(u8 budget_table[EHCI_BANDWIDTH_SIZE],
102b35c5009SAlan Stern 		struct ehci_tt *tt);
103b35c5009SAlan Stern 
104acc08503SAlan Stern /*
105acc08503SAlan Stern  * The MosChip MCS9990 controller updates its microframe counter
106acc08503SAlan Stern  * a little before the frame counter, and occasionally we will read
107acc08503SAlan Stern  * the invalid intermediate value.  Avoid problems by checking the
108acc08503SAlan Stern  * microframe number (the low-order 3 bits); if they are 0 then
109acc08503SAlan Stern  * re-read the register to get the correct value.
110acc08503SAlan Stern  */
ehci_moschip_read_frame_index(struct ehci_hcd * ehci)111acc08503SAlan Stern static unsigned ehci_moschip_read_frame_index(struct ehci_hcd *ehci)
112acc08503SAlan Stern {
113acc08503SAlan Stern 	unsigned uf;
114acc08503SAlan Stern 
115acc08503SAlan Stern 	uf = ehci_readl(ehci, &ehci->regs->frame_index);
116acc08503SAlan Stern 	if (unlikely((uf & 7) == 0))
117acc08503SAlan Stern 		uf = ehci_readl(ehci, &ehci->regs->frame_index);
118acc08503SAlan Stern 	return uf;
119acc08503SAlan Stern }
120acc08503SAlan Stern 
ehci_read_frame_index(struct ehci_hcd * ehci)121acc08503SAlan Stern static inline unsigned ehci_read_frame_index(struct ehci_hcd *ehci)
122acc08503SAlan Stern {
123acc08503SAlan Stern 	if (ehci->frame_index_bug)
124acc08503SAlan Stern 		return ehci_moschip_read_frame_index(ehci);
125acc08503SAlan Stern 	return ehci_readl(ehci, &ehci->regs->frame_index);
126acc08503SAlan Stern }
127acc08503SAlan Stern 
128acc08503SAlan Stern #include "ehci-dbg.c"
129acc08503SAlan Stern 
1301da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds /*
1332f3a6b86SManjunath Goudar  * ehci_handshake - spin reading hc until handshake completes or fails
1341da177e4SLinus Torvalds  * @ptr: address of hc register to be read
1351da177e4SLinus Torvalds  * @mask: bits to look at in result of read
1361da177e4SLinus Torvalds  * @done: value of those bits when handshake succeeds
1371da177e4SLinus Torvalds  * @usec: timeout in microseconds
1381da177e4SLinus Torvalds  *
1391da177e4SLinus Torvalds  * Returns negative errno, or zero on success
1401da177e4SLinus Torvalds  *
1411da177e4SLinus Torvalds  * Success happens when the "mask" bits have the specified value (hardware
1421da177e4SLinus Torvalds  * handshake done).  There are two failure modes:  "usec" have passed (major
1431da177e4SLinus Torvalds  * hardware flakeout), or the register reads as all-ones (hardware removed).
1441da177e4SLinus Torvalds  *
1451da177e4SLinus Torvalds  * That last failure should_only happen in cases like physical cardbus eject
1461da177e4SLinus Torvalds  * before driver shutdown. But it also seems to be caused by bugs in cardbus
1471da177e4SLinus Torvalds  * bridge shutdown:  shutting down the bridge before the devices using it.
1481da177e4SLinus Torvalds  */
ehci_handshake(struct ehci_hcd * ehci,void __iomem * ptr,u32 mask,u32 done,int usec)1492f3a6b86SManjunath Goudar int ehci_handshake(struct ehci_hcd *ehci, void __iomem *ptr,
150083522d7SBenjamin Herrenschmidt 		   u32 mask, u32 done, int usec)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds 	u32	result;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	do {
155083522d7SBenjamin Herrenschmidt 		result = ehci_readl(ehci, ptr);
1561da177e4SLinus Torvalds 		if (result == ~(u32)0)		/* card removed */
1571da177e4SLinus Torvalds 			return -ENODEV;
1581da177e4SLinus Torvalds 		result &= mask;
1591da177e4SLinus Torvalds 		if (result == done)
1601da177e4SLinus Torvalds 			return 0;
1611da177e4SLinus Torvalds 		udelay (1);
1621da177e4SLinus Torvalds 		usec--;
1631da177e4SLinus Torvalds 	} while (usec > 0);
1641da177e4SLinus Torvalds 	return -ETIMEDOUT;
1651da177e4SLinus Torvalds }
1662f3a6b86SManjunath Goudar EXPORT_SYMBOL_GPL(ehci_handshake);
1671da177e4SLinus Torvalds 
16865fd4272SMatthieu CASTET /* check TDI/ARC silicon is in host mode */
tdi_in_host_mode(struct ehci_hcd * ehci)16965fd4272SMatthieu CASTET static int tdi_in_host_mode (struct ehci_hcd *ehci)
17065fd4272SMatthieu CASTET {
17165fd4272SMatthieu CASTET 	u32		tmp;
17265fd4272SMatthieu CASTET 
173a46af4ebSAlan Stern 	tmp = ehci_readl(ehci, &ehci->regs->usbmode);
17465fd4272SMatthieu CASTET 	return (tmp & 3) == USBMODE_CM_HC;
17565fd4272SMatthieu CASTET }
17665fd4272SMatthieu CASTET 
177c4f34764SAlan Stern /*
178c4f34764SAlan Stern  * Force HC to halt state from unknown (EHCI spec section 2.3).
179c4f34764SAlan Stern  * Must be called with interrupts enabled and the lock not held.
180c4f34764SAlan Stern  */
ehci_halt(struct ehci_hcd * ehci)1811da177e4SLinus Torvalds static int ehci_halt (struct ehci_hcd *ehci)
1821da177e4SLinus Torvalds {
183c4f34764SAlan Stern 	u32	temp;
184c4f34764SAlan Stern 
185c4f34764SAlan Stern 	spin_lock_irq(&ehci->lock);
1861da177e4SLinus Torvalds 
18772f30b6fSDavid Brownell 	/* disable any irqs left enabled by previous code */
188083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, 0, &ehci->regs->intr_enable);
18972f30b6fSDavid Brownell 
190c4f34764SAlan Stern 	if (ehci_is_TDI(ehci) && !tdi_in_host_mode(ehci)) {
191c4f34764SAlan Stern 		spin_unlock_irq(&ehci->lock);
19265fd4272SMatthieu CASTET 		return 0;
19365fd4272SMatthieu CASTET 	}
19465fd4272SMatthieu CASTET 
1953d9545ccSAlan Stern 	/*
1963d9545ccSAlan Stern 	 * This routine gets called during probe before ehci->command
1973d9545ccSAlan Stern 	 * has been initialized, so we can't rely on its value.
1983d9545ccSAlan Stern 	 */
1993d9545ccSAlan Stern 	ehci->command &= ~CMD_RUN;
200083522d7SBenjamin Herrenschmidt 	temp = ehci_readl(ehci, &ehci->regs->command);
2013d9545ccSAlan Stern 	temp &= ~(CMD_RUN | CMD_IAAD);
202083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, temp, &ehci->regs->command);
203c4f34764SAlan Stern 
204c4f34764SAlan Stern 	spin_unlock_irq(&ehci->lock);
205c4f34764SAlan Stern 	synchronize_irq(ehci_to_hcd(ehci)->irq);
206c4f34764SAlan Stern 
2072f3a6b86SManjunath Goudar 	return ehci_handshake(ehci, &ehci->regs->status,
208083522d7SBenjamin Herrenschmidt 			  STS_HALT, STS_HALT, 16 * 125);
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /* put TDI/ARC silicon into EHCI mode */
tdi_reset(struct ehci_hcd * ehci)2121da177e4SLinus Torvalds static void tdi_reset (struct ehci_hcd *ehci)
2131da177e4SLinus Torvalds {
2141da177e4SLinus Torvalds 	u32		tmp;
2151da177e4SLinus Torvalds 
216a46af4ebSAlan Stern 	tmp = ehci_readl(ehci, &ehci->regs->usbmode);
217d23a1377SVladimir Barinov 	tmp |= USBMODE_CM_HC;
218d23a1377SVladimir Barinov 	/* The default byte access to MMR space is LE after
219d23a1377SVladimir Barinov 	 * controller reset. Set the required endian mode
220d23a1377SVladimir Barinov 	 * for transfer buffers to match the host microprocessor
221d23a1377SVladimir Barinov 	 */
222d23a1377SVladimir Barinov 	if (ehci_big_endian_mmio(ehci))
223d23a1377SVladimir Barinov 		tmp |= USBMODE_BE;
224a46af4ebSAlan Stern 	ehci_writel(ehci, tmp, &ehci->regs->usbmode);
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
227c4f34764SAlan Stern /*
228c4f34764SAlan Stern  * Reset a non-running (STS_HALT == 1) controller.
229c4f34764SAlan Stern  * Must be called with interrupts enabled and the lock not held.
230c4f34764SAlan Stern  */
ehci_reset(struct ehci_hcd * ehci)23174db22cbSRamneek Mehresh int ehci_reset(struct ehci_hcd *ehci)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds 	int	retval;
234083522d7SBenjamin Herrenschmidt 	u32	command = ehci_readl(ehci, &ehci->regs->command);
2351da177e4SLinus Torvalds 
2368d053c79SJason Wessel 	/* If the EHCI debug controller is active, special care must be
2378d053c79SJason Wessel 	 * taken before and after a host controller reset */
2389fa5780bSJan Beulich 	if (ehci->debug && !dbgp_reset_prep(ehci_to_hcd(ehci)))
2398d053c79SJason Wessel 		ehci->debug = NULL;
2408d053c79SJason Wessel 
2411da177e4SLinus Torvalds 	command |= CMD_RESET;
2421da177e4SLinus Torvalds 	dbg_cmd (ehci, "reset", command);
243083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, command, &ehci->regs->command);
244e8799906SAlan Stern 	ehci->rh_state = EHCI_RH_HALTED;
2451da177e4SLinus Torvalds 	ehci->next_statechange = jiffies;
2462f3a6b86SManjunath Goudar 	retval = ehci_handshake(ehci, &ehci->regs->command,
247083522d7SBenjamin Herrenschmidt 			    CMD_RESET, 0, 250 * 1000);
2481da177e4SLinus Torvalds 
249331ac6b2SAlek Du 	if (ehci->has_hostpc) {
250331ac6b2SAlek Du 		ehci_writel(ehci, USBMODE_EX_HC | USBMODE_EX_VBPS,
251a46af4ebSAlan Stern 				&ehci->regs->usbmode_ex);
252a46af4ebSAlan Stern 		ehci_writel(ehci, TXFIFO_DEFAULT, &ehci->regs->txfill_tuning);
253331ac6b2SAlek Du 	}
2541da177e4SLinus Torvalds 	if (retval)
2551da177e4SLinus Torvalds 		return retval;
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	if (ehci_is_TDI(ehci))
2581da177e4SLinus Torvalds 		tdi_reset (ehci);
2591da177e4SLinus Torvalds 
2608d053c79SJason Wessel 	if (ehci->debug)
2619fa5780bSJan Beulich 		dbgp_external_startup(ehci_to_hcd(ehci));
2628d053c79SJason Wessel 
263a448e4dcSAlan Stern 	ehci->port_c_suspend = ehci->suspended_ports =
264a448e4dcSAlan Stern 			ehci->resuming_ports = 0;
2651da177e4SLinus Torvalds 	return retval;
2661da177e4SLinus Torvalds }
26774db22cbSRamneek Mehresh EXPORT_SYMBOL_GPL(ehci_reset);
2681da177e4SLinus Torvalds 
269c4f34764SAlan Stern /*
270c4f34764SAlan Stern  * Idle the controller (turn off the schedules).
271c4f34764SAlan Stern  * Must be called with interrupts enabled and the lock not held.
272c4f34764SAlan Stern  */
ehci_quiesce(struct ehci_hcd * ehci)2731da177e4SLinus Torvalds static void ehci_quiesce (struct ehci_hcd *ehci)
2741da177e4SLinus Torvalds {
2751da177e4SLinus Torvalds 	u32	temp;
2761da177e4SLinus Torvalds 
277e8799906SAlan Stern 	if (ehci->rh_state != EHCI_RH_RUNNING)
278c0c53dbcSAlan Stern 		return;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	/* wait for any schedule enables/disables to take effect */
2813d9545ccSAlan Stern 	temp = (ehci->command << 10) & (STS_ASS | STS_PSS);
2822f3a6b86SManjunath Goudar 	ehci_handshake(ehci, &ehci->regs->status, STS_ASS | STS_PSS, temp,
2832f3a6b86SManjunath Goudar 			16 * 125);
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	/* then disable anything that's still active */
286c4f34764SAlan Stern 	spin_lock_irq(&ehci->lock);
2873d9545ccSAlan Stern 	ehci->command &= ~(CMD_ASE | CMD_PSE);
2883d9545ccSAlan Stern 	ehci_writel(ehci, ehci->command, &ehci->regs->command);
289c4f34764SAlan Stern 	spin_unlock_irq(&ehci->lock);
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	/* hardware can take 16 microframes to turn off ... */
2922f3a6b86SManjunath Goudar 	ehci_handshake(ehci, &ehci->regs->status, STS_ASS | STS_PSS, 0,
2932f3a6b86SManjunath Goudar 			16 * 125);
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
2971da177e4SLinus Torvalds 
298f96fba0dSAlan Stern static void end_iaa_cycle(struct ehci_hcd *ehci);
29907d29b63SAlan Stern static void end_unlink_async(struct ehci_hcd *ehci);
30032830f20SAlan Stern static void unlink_empty_async(struct ehci_hcd *ehci);
3017d12e780SDavid Howells static void ehci_work(struct ehci_hcd *ehci);
302df202255SAlan Stern static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh);
303df202255SAlan Stern static void end_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh);
30411a7e594SMichael Grzeschik static int ehci_port_power(struct ehci_hcd *ehci, int portnum, bool enable);
3051da177e4SLinus Torvalds 
306d58b4bccSAlan Stern #include "ehci-timer.c"
3071da177e4SLinus Torvalds #include "ehci-hub.c"
3081da177e4SLinus Torvalds #include "ehci-mem.c"
3091da177e4SLinus Torvalds #include "ehci-q.c"
3101da177e4SLinus Torvalds #include "ehci-sched.c"
3114c67045bSKirill Smelkov #include "ehci-sysfs.c"
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
3141da177e4SLinus Torvalds 
3158903795aSAlan Stern /* On some systems, leaving remote wakeup enabled prevents system shutdown.
3168903795aSAlan Stern  * The firmware seems to think that powering off is a wakeup event!
3178903795aSAlan Stern  * This routine turns off remote wakeup and everything else, on all ports.
3188903795aSAlan Stern  */
ehci_turn_off_all_ports(struct ehci_hcd * ehci)3198903795aSAlan Stern static void ehci_turn_off_all_ports(struct ehci_hcd *ehci)
3208903795aSAlan Stern {
3218903795aSAlan Stern 	int	port = HCS_N_PORTS(ehci->hcs_params);
3228903795aSAlan Stern 
32311a7e594SMichael Grzeschik 	while (port--) {
32411a7e594SMichael Grzeschik 		spin_unlock_irq(&ehci->lock);
32511a7e594SMichael Grzeschik 		ehci_port_power(ehci, port, false);
32611a7e594SMichael Grzeschik 		spin_lock_irq(&ehci->lock);
327bc337b51SMarc Ohlf 		ehci_writel(ehci, PORT_RWC_BITS,
328bc337b51SMarc Ohlf 				&ehci->regs->port_status[port]);
32911a7e594SMichael Grzeschik 	}
3308903795aSAlan Stern }
3318903795aSAlan Stern 
33221da84a8SSarah Sharp /*
33321da84a8SSarah Sharp  * Halt HC, turn off all ports, and let the BIOS use the companion controllers.
334c4f34764SAlan Stern  * Must be called with interrupts enabled and the lock not held.
33572f30b6fSDavid Brownell  */
ehci_silence_controller(struct ehci_hcd * ehci)33621da84a8SSarah Sharp static void ehci_silence_controller(struct ehci_hcd *ehci)
3371da177e4SLinus Torvalds {
33821da84a8SSarah Sharp 	ehci_halt(ehci);
339c4f34764SAlan Stern 
340c4f34764SAlan Stern 	spin_lock_irq(&ehci->lock);
341c4f34764SAlan Stern 	ehci->rh_state = EHCI_RH_HALTED;
3428903795aSAlan Stern 	ehci_turn_off_all_ports(ehci);
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	/* make BIOS/etc use companion controller during reboot */
345083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, 0, &ehci->regs->configured_flag);
3468903795aSAlan Stern 
3478903795aSAlan Stern 	/* unblock posted writes */
3488903795aSAlan Stern 	ehci_readl(ehci, &ehci->regs->configured_flag);
349c4f34764SAlan Stern 	spin_unlock_irq(&ehci->lock);
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
35221da84a8SSarah Sharp /* ehci_shutdown kick in for silicon on any bus (not just pci, etc).
35321da84a8SSarah Sharp  * This forcibly disables dma and IRQs, helping kexec and other cases
35421da84a8SSarah Sharp  * where the next system software may expect clean state.
35521da84a8SSarah Sharp  */
ehci_shutdown(struct usb_hcd * hcd)35621da84a8SSarah Sharp static void ehci_shutdown(struct usb_hcd *hcd)
35721da84a8SSarah Sharp {
35821da84a8SSarah Sharp 	struct ehci_hcd	*ehci = hcd_to_ehci(hcd);
35921da84a8SSarah Sharp 
36011c011a5SSrinivas Kandagatla 	/**
36111c011a5SSrinivas Kandagatla 	 * Protect the system from crashing at system shutdown in cases where
36211c011a5SSrinivas Kandagatla 	 * usb host is not added yet from OTG controller driver.
36311c011a5SSrinivas Kandagatla 	 * As ehci_setup() not done yet, so stop accessing registers or
36411c011a5SSrinivas Kandagatla 	 * variables initialized in ehci_setup()
36511c011a5SSrinivas Kandagatla 	 */
36611c011a5SSrinivas Kandagatla 	if (!ehci->sbrn)
36711c011a5SSrinivas Kandagatla 		return;
36811c011a5SSrinivas Kandagatla 
36921da84a8SSarah Sharp 	spin_lock_irq(&ehci->lock);
37043fe3a99SAlan Stern 	ehci->shutdown = true;
371c0c53dbcSAlan Stern 	ehci->rh_state = EHCI_RH_STOPPING;
372d58b4bccSAlan Stern 	ehci->enabled_hrtimer_events = 0;
37321da84a8SSarah Sharp 	spin_unlock_irq(&ehci->lock);
374d58b4bccSAlan Stern 
375c4f34764SAlan Stern 	ehci_silence_controller(ehci);
376c4f34764SAlan Stern 
377d58b4bccSAlan Stern 	hrtimer_cancel(&ehci->hrtimer);
37821da84a8SSarah Sharp }
37921da84a8SSarah Sharp 
3807ff71d6aSMatt Porter /*-------------------------------------------------------------------------*/
3811da177e4SLinus Torvalds 
3827ff71d6aSMatt Porter /*
3837ff71d6aSMatt Porter  * ehci_work is called from some interrupts, timers, and so on.
3847ff71d6aSMatt Porter  * it calls driver completion functions, after dropping ehci->lock.
3857ff71d6aSMatt Porter  */
ehci_work(struct ehci_hcd * ehci)3867d12e780SDavid Howells static void ehci_work (struct ehci_hcd *ehci)
3877ff71d6aSMatt Porter {
3887ff71d6aSMatt Porter 	/* another CPU may drop ehci->lock during a schedule scan while
3897ff71d6aSMatt Porter 	 * it reports urb completions.  this flag guards against bogus
3907ff71d6aSMatt Porter 	 * attempts at re-entrant schedule scanning.
3917ff71d6aSMatt Porter 	 */
392361aabf3SAlan Stern 	if (ehci->scanning) {
393361aabf3SAlan Stern 		ehci->need_rescan = true;
3947ff71d6aSMatt Porter 		return;
395361aabf3SAlan Stern 	}
396361aabf3SAlan Stern 	ehci->scanning = true;
397361aabf3SAlan Stern 
398361aabf3SAlan Stern  rescan:
399361aabf3SAlan Stern 	ehci->need_rescan = false;
40031446610SAlan Stern 	if (ehci->async_count)
4017d12e780SDavid Howells 		scan_async(ehci);
402569b394fSAlan Stern 	if (ehci->intr_count > 0)
403569b394fSAlan Stern 		scan_intr(ehci);
404569b394fSAlan Stern 	if (ehci->isoc_count > 0)
405569b394fSAlan Stern 		scan_isoc(ehci);
406361aabf3SAlan Stern 	if (ehci->need_rescan)
407361aabf3SAlan Stern 		goto rescan;
408361aabf3SAlan Stern 	ehci->scanning = false;
4097ff71d6aSMatt Porter 
4107ff71d6aSMatt Porter 	/* the IO watchdog guards against hardware or driver bugs that
4117ff71d6aSMatt Porter 	 * misplace IRQs, and should let us run completely without IRQs.
4127ff71d6aSMatt Porter 	 * such lossage has been observed on both VT6202 and VT8235.
4137ff71d6aSMatt Porter 	 */
41418aafe64SAlan Stern 	turn_on_io_watchdog(ehci);
4157ff71d6aSMatt Porter }
4167ff71d6aSMatt Porter 
41721da84a8SSarah Sharp /*
41821da84a8SSarah Sharp  * Called when the ehci_hcd module is removed.
41921da84a8SSarah Sharp  */
ehci_stop(struct usb_hcd * hcd)4207ff71d6aSMatt Porter static void ehci_stop (struct usb_hcd *hcd)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
4231da177e4SLinus Torvalds 
4247ff71d6aSMatt Porter 	ehci_dbg (ehci, "stop\n");
4251da177e4SLinus Torvalds 
4267ff71d6aSMatt Porter 	/* no more interrupts ... */
4271da177e4SLinus Torvalds 
4287ff71d6aSMatt Porter 	spin_lock_irq(&ehci->lock);
429d58b4bccSAlan Stern 	ehci->enabled_hrtimer_events = 0;
4307ff71d6aSMatt Porter 	spin_unlock_irq(&ehci->lock);
4317ff71d6aSMatt Porter 
432c4f34764SAlan Stern 	ehci_quiesce(ehci);
4337ff71d6aSMatt Porter 	ehci_silence_controller(ehci);
4347ff71d6aSMatt Porter 	ehci_reset (ehci);
4357ff71d6aSMatt Porter 
436d58b4bccSAlan Stern 	hrtimer_cancel(&ehci->hrtimer);
4374c67045bSKirill Smelkov 	remove_sysfs_files(ehci);
4387ff71d6aSMatt Porter 	remove_debug_files (ehci);
4397ff71d6aSMatt Porter 
4407ff71d6aSMatt Porter 	/* root hub is shut down separately (first, when possible) */
4417ff71d6aSMatt Porter 	spin_lock_irq (&ehci->lock);
44255934eb3SAlan Stern 	end_free_itds(ehci);
4437ff71d6aSMatt Porter 	spin_unlock_irq (&ehci->lock);
4447ff71d6aSMatt Porter 	ehci_mem_cleanup (ehci);
4457ff71d6aSMatt Porter 
446ad93562bSAndiry Xu 	if (ehci->amd_pll_fix == 1)
447ad93562bSAndiry Xu 		usb_amd_dev_put();
44805570297SAlex He 
449083522d7SBenjamin Herrenschmidt 	dbg_status (ehci, "ehci_stop completed",
450083522d7SBenjamin Herrenschmidt 		    ehci_readl(ehci, &ehci->regs->status));
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds 
45318807521SDavid Brownell /* one-time init, only for memory state */
ehci_init(struct usb_hcd * hcd)45418807521SDavid Brownell static int ehci_init(struct usb_hcd *hcd)
4551da177e4SLinus Torvalds {
4561da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
4571da177e4SLinus Torvalds 	u32			temp;
4581da177e4SLinus Torvalds 	int			retval;
4591da177e4SLinus Torvalds 	u32			hcc_params;
4603807e26dSAlek Du 	struct ehci_qh_hw	*hw;
4611da177e4SLinus Torvalds 
46218807521SDavid Brownell 	spin_lock_init(&ehci->lock);
46318807521SDavid Brownell 
464403dbd36SAlek Du 	/*
465403dbd36SAlek Du 	 * keep io watchdog by default, those good HCDs could turn off it later
466403dbd36SAlek Du 	 */
467403dbd36SAlek Du 	ehci->need_io_watchdog = 1;
4681da177e4SLinus Torvalds 
469d58b4bccSAlan Stern 	hrtimer_init(&ehci->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
470d58b4bccSAlan Stern 	ehci->hrtimer.function = ehci_hrtimer_func;
471d58b4bccSAlan Stern 	ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
47207d29b63SAlan Stern 
473f75593ceSAlan Stern 	hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
474f75593ceSAlan Stern 
4751da177e4SLinus Torvalds 	/*
476cc62a7ebSKirill Smelkov 	 * by default set standard 80% (== 100 usec/uframe) max periodic
477cc62a7ebSKirill Smelkov 	 * bandwidth as required by USB 2.0
478cc62a7ebSKirill Smelkov 	 */
479cc62a7ebSKirill Smelkov 	ehci->uframe_periodic_max = 100;
480cc62a7ebSKirill Smelkov 
481cc62a7ebSKirill Smelkov 	/*
4821da177e4SLinus Torvalds 	 * hw default: 1K periodic list heads, one per frame.
4831da177e4SLinus Torvalds 	 * periodic_size can shrink by USBCMD update if hcc_params allows.
4841da177e4SLinus Torvalds 	 */
4851da177e4SLinus Torvalds 	ehci->periodic_size = DEFAULT_I_TDPS;
4866e018751SAlan Stern 	INIT_LIST_HEAD(&ehci->async_unlink);
487214ac7a0SAlan Stern 	INIT_LIST_HEAD(&ehci->async_idle);
4889118f9ebSMing Lei 	INIT_LIST_HEAD(&ehci->intr_unlink_wait);
4896e018751SAlan Stern 	INIT_LIST_HEAD(&ehci->intr_unlink);
490569b394fSAlan Stern 	INIT_LIST_HEAD(&ehci->intr_qh_list);
4919aa09d2fSKarsten Wiese 	INIT_LIST_HEAD(&ehci->cached_itd_list);
4920e5f231bSAlan Stern 	INIT_LIST_HEAD(&ehci->cached_sitd_list);
493b35c5009SAlan Stern 	INIT_LIST_HEAD(&ehci->tt_list);
494f75593ceSAlan Stern 
4958e192910SGreg Kroah-Hartman 	if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
496f75593ceSAlan Stern 		/* periodic schedule size can be smaller than default */
497f75593ceSAlan Stern 		switch (EHCI_TUNE_FLS) {
498f75593ceSAlan Stern 		case 0: ehci->periodic_size = 1024; break;
499f75593ceSAlan Stern 		case 1: ehci->periodic_size = 512; break;
500f75593ceSAlan Stern 		case 2: ehci->periodic_size = 256; break;
501f75593ceSAlan Stern 		default:	BUG();
502f75593ceSAlan Stern 		}
503f75593ceSAlan Stern 	}
50418807521SDavid Brownell 	if ((retval = ehci_mem_init(ehci, GFP_KERNEL)) < 0)
5051da177e4SLinus Torvalds 		return retval;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	/* controllers may cache some of the periodic schedule ... */
5081da177e4SLinus Torvalds 	if (HCC_ISOC_CACHE(hcc_params))		// full frame cache
50998cae42dSAlan Stern 		ehci->i_thresh = 0;
5101da177e4SLinus Torvalds 	else					// N microframes cached
5111da177e4SLinus Torvalds 		ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	/*
5141da177e4SLinus Torvalds 	 * dedicate a qh for the async ring head, since we couldn't unlink
5151da177e4SLinus Torvalds 	 * a 'real' qh without stopping the async schedule [4.8].  use it
5161da177e4SLinus Torvalds 	 * as the 'reclamation list head' too.
5171da177e4SLinus Torvalds 	 * its dummy is used in hw_alt_next of many tds, to prevent the qh
5181da177e4SLinus Torvalds 	 * from automatically advancing to the next td after short reads.
5191da177e4SLinus Torvalds 	 */
5201da177e4SLinus Torvalds 	ehci->async->qh_next.qh = NULL;
5213807e26dSAlek Du 	hw = ehci->async->hw;
5223807e26dSAlek Du 	hw->hw_next = QH_NEXT(ehci, ehci->async->qh_dma);
5233807e26dSAlek Du 	hw->hw_info1 = cpu_to_hc32(ehci, QH_HEAD);
5244f7a67e2SRicardo Martins #if defined(CONFIG_PPC_PS3)
5254c53de72SAlan Stern 	hw->hw_info1 |= cpu_to_hc32(ehci, QH_INACTIVATE);
5264f7a67e2SRicardo Martins #endif
5273807e26dSAlek Du 	hw->hw_token = cpu_to_hc32(ehci, QTD_STS_HALT);
5283807e26dSAlek Du 	hw->hw_qtd_next = EHCI_LIST_END(ehci);
5291da177e4SLinus Torvalds 	ehci->async->qh_state = QH_STATE_LINKED;
5303807e26dSAlek Du 	hw->hw_alt_next = QTD_NEXT(ehci, ehci->async->dummy->qtd_dma);
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	/* clear interrupt enables, set irq latency */
5331da177e4SLinus Torvalds 	if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
5341da177e4SLinus Torvalds 		log2_irq_thresh = 0;
5351da177e4SLinus Torvalds 	temp = 1 << (16 + log2_irq_thresh);
5365a9cdf33SAlek Du 	if (HCC_PER_PORT_CHANGE_EVENT(hcc_params)) {
5375a9cdf33SAlek Du 		ehci->has_ppcd = 1;
5385a9cdf33SAlek Du 		ehci_dbg(ehci, "enable per-port change event\n");
5395a9cdf33SAlek Du 		temp |= CMD_PPCEE;
5405a9cdf33SAlek Du 	}
5411da177e4SLinus Torvalds 	if (HCC_CANPARK(hcc_params)) {
5421da177e4SLinus Torvalds 		/* HW default park == 3, on hardware that supports it (like
5431da177e4SLinus Torvalds 		 * NVidia and ALI silicon), maximizes throughput on the async
5441da177e4SLinus Torvalds 		 * schedule by avoiding QH fetches between transfers.
5451da177e4SLinus Torvalds 		 *
5461da177e4SLinus Torvalds 		 * With fast usb storage devices and NForce2, "park" seems to
5471da177e4SLinus Torvalds 		 * make problems:  throughput reduction (!), data errors...
5481da177e4SLinus Torvalds 		 */
5491da177e4SLinus Torvalds 		if (park) {
5501da177e4SLinus Torvalds 			park = min(park, (unsigned) 3);
5511da177e4SLinus Torvalds 			temp |= CMD_PARK;
5521da177e4SLinus Torvalds 			temp |= park << 8;
5531da177e4SLinus Torvalds 		}
55418807521SDavid Brownell 		ehci_dbg(ehci, "park %d\n", park);
5551da177e4SLinus Torvalds 	}
5561da177e4SLinus Torvalds 	if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
5571da177e4SLinus Torvalds 		/* periodic schedule size can be smaller than default */
5581da177e4SLinus Torvalds 		temp &= ~(3 << 2);
5591da177e4SLinus Torvalds 		temp |= (EHCI_TUNE_FLS << 2);
5601da177e4SLinus Torvalds 	}
56118807521SDavid Brownell 	ehci->command = temp;
56218807521SDavid Brownell 
56340f8db8fSAlan Stern 	/* Accept arbitrarily long scatter-gather lists */
5642d7a3dc3SLaurentiu Tudor 	if (!hcd->localmem_pool)
56540f8db8fSAlan Stern 		hcd->self.sg_tablesize = ~0;
56687d61912SAlan Stern 
56787d61912SAlan Stern 	/* Prepare for unlinking active QHs */
56887d61912SAlan Stern 	ehci->old_current = ~0;
56918807521SDavid Brownell 	return 0;
57018807521SDavid Brownell }
57118807521SDavid Brownell 
57218807521SDavid Brownell /* start HC running; it's halted, ehci_init() has been run (once) */
ehci_run(struct usb_hcd * hcd)57318807521SDavid Brownell static int ehci_run (struct usb_hcd *hcd)
57418807521SDavid Brownell {
57518807521SDavid Brownell 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
57618807521SDavid Brownell 	u32			temp;
57718807521SDavid Brownell 	u32			hcc_params;
578280a9045SEugene Korenevsky 	int			rc;
57918807521SDavid Brownell 
5801d619f12SMarcelo Tosatti 	hcd->uses_new_polling = 1;
5811d619f12SMarcelo Tosatti 
58218807521SDavid Brownell 	/* EHCI spec section 4.1 */
583876e0df9SGeoff Levand 
584083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
585083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
58618807521SDavid Brownell 
58718807521SDavid Brownell 	/*
58818807521SDavid Brownell 	 * hcc_params controls whether ehci->regs->segment must (!!!)
58918807521SDavid Brownell 	 * be used; it constrains QH/ITD/SITD and QTD locations.
590324c54faSRomain Perier 	 * dma_pool consistent memory always uses segment zero.
5911cd27268SCai Huoqing 	 * streaming mappings for I/O buffers, like dma_map_single(),
59218807521SDavid Brownell 	 * can return segments above 4GB, if the device allows.
59318807521SDavid Brownell 	 *
5940e77acefSChristoph Hellwig 	 * NOTE:  the dma mask is visible through dev->dma_mask, so
59518807521SDavid Brownell 	 * drivers can pass this info along ... like NETIF_F_HIGHDMA,
59618807521SDavid Brownell 	 * Scsi_Host.highmem_io, and so forth.  It's readonly to all
59718807521SDavid Brownell 	 * host side drivers though.
59818807521SDavid Brownell 	 */
599083522d7SBenjamin Herrenschmidt 	hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
60018807521SDavid Brownell 	if (HCC_64BIT_ADDR(hcc_params)) {
601083522d7SBenjamin Herrenschmidt 		ehci_writel(ehci, 0, &ehci->regs->segment);
60218807521SDavid Brownell #if 0
60318807521SDavid Brownell // this is deeply broken on almost all architectures
6046a35528aSYang Hongyang 		if (!dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64)))
60518807521SDavid Brownell 			ehci_info(ehci, "enabled 64bit DMA\n");
60618807521SDavid Brownell #endif
60718807521SDavid Brownell 	}
60818807521SDavid Brownell 
60918807521SDavid Brownell 
6101da177e4SLinus Torvalds 	// Philips, Intel, and maybe others need CMD_RUN before the
6111da177e4SLinus Torvalds 	// root hub will detect new devices (why?); NEC doesn't
61218807521SDavid Brownell 	ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
61318807521SDavid Brownell 	ehci->command |= CMD_RUN;
614083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, ehci->command, &ehci->regs->command);
61518807521SDavid Brownell 	dbg_cmd (ehci, "init", ehci->command);
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	/*
6181da177e4SLinus Torvalds 	 * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
6191da177e4SLinus Torvalds 	 * are explicitly handed to companion controller(s), so no TT is
6201da177e4SLinus Torvalds 	 * involved with the root hub.  (Except where one is integrated,
6211da177e4SLinus Torvalds 	 * and there's no companion controller unless maybe for USB OTG.)
62232fe0198SAlan Stern 	 *
62332fe0198SAlan Stern 	 * Turning on the CF flag will transfer ownership of all ports
62432fe0198SAlan Stern 	 * from the companions to the EHCI controller.  If any of the
62532fe0198SAlan Stern 	 * companions are in the middle of a port reset at the time, it
62632fe0198SAlan Stern 	 * could cause trouble.  Write-locking ehci_cf_port_reset_rwsem
6271cb52658SDavid Brownell 	 * guarantees that no resets are in progress.  After we set CF,
6281cb52658SDavid Brownell 	 * a short delay lets the hardware catch up; new resets shouldn't
6291cb52658SDavid Brownell 	 * be started before the port switching actions could complete.
6301da177e4SLinus Torvalds 	 */
63132fe0198SAlan Stern 	down_write(&ehci_cf_port_reset_rwsem);
632e8799906SAlan Stern 	ehci->rh_state = EHCI_RH_RUNNING;
633083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
634280a9045SEugene Korenevsky 
635280a9045SEugene Korenevsky 	/* Wait until HC become operational */
636083522d7SBenjamin Herrenschmidt 	ehci_readl(ehci, &ehci->regs->command);	/* unblock posted writes */
6371cb52658SDavid Brownell 	msleep(5);
6387f2d7378SNeal Liu 
6397f2d7378SNeal Liu 	/* For Aspeed, STS_HALT also depends on ASS/PSS status.
6407f2d7378SNeal Liu 	 * Check CMD_RUN instead.
6417f2d7378SNeal Liu 	 */
6427f2d7378SNeal Liu 	if (ehci->is_aspeed)
6437f2d7378SNeal Liu 		rc = ehci_handshake(ehci, &ehci->regs->command, CMD_RUN,
6447f2d7378SNeal Liu 				    1, 100 * 1000);
6457f2d7378SNeal Liu 	else
6467f2d7378SNeal Liu 		rc = ehci_handshake(ehci, &ehci->regs->status, STS_HALT,
6477f2d7378SNeal Liu 				    0, 100 * 1000);
648280a9045SEugene Korenevsky 
64932fe0198SAlan Stern 	up_write(&ehci_cf_port_reset_rwsem);
650280a9045SEugene Korenevsky 
651280a9045SEugene Korenevsky 	if (rc) {
652280a9045SEugene Korenevsky 		ehci_err(ehci, "USB %x.%x, controller refused to start: %d\n",
653280a9045SEugene Korenevsky 			 ((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f), rc);
654280a9045SEugene Korenevsky 		return rc;
655280a9045SEugene Korenevsky 	}
656280a9045SEugene Korenevsky 
657ee4ecb8aSOliver Neukum 	ehci->last_periodic_enable = ktime_get_real();
6581da177e4SLinus Torvalds 
659c430131aSJan Andersson 	temp = HC_VERSION(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
6601da177e4SLinus Torvalds 	ehci_info (ehci,
6612b70f073SAlan Stern 		"USB %x.%x started, EHCI %x.%02x%s\n",
6627ff71d6aSMatt Porter 		((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f),
6632b70f073SAlan Stern 		temp >> 8, temp & 0xff,
6642d5ba374SFlorian Fainelli 		(ignore_oc || ehci->spurious_oc) ? ", overcurrent ignored" : "");
6651da177e4SLinus Torvalds 
666083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, INTR_MASK,
667083522d7SBenjamin Herrenschmidt 		    &ehci->regs->intr_enable); /* Turn On Interrupts */
6681da177e4SLinus Torvalds 
66918807521SDavid Brownell 	/* GRR this is run-once init(), being done every time the HC starts.
67018807521SDavid Brownell 	 * So long as they're part of class devices, we can't do it init()
67118807521SDavid Brownell 	 * since the class device isn't created that early.
67218807521SDavid Brownell 	 */
6731da177e4SLinus Torvalds 	create_debug_files(ehci);
6744c67045bSKirill Smelkov 	create_sysfs_files(ehci);
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	return 0;
6771da177e4SLinus Torvalds }
6781da177e4SLinus Torvalds 
ehci_setup(struct usb_hcd * hcd)6793e023203SAlan Stern int ehci_setup(struct usb_hcd *hcd)
6802093c6b4SMatthieu CASTET {
6812093c6b4SMatthieu CASTET 	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
6822093c6b4SMatthieu CASTET 	int retval;
6832093c6b4SMatthieu CASTET 
6842093c6b4SMatthieu CASTET 	ehci->regs = (void __iomem *)ehci->caps +
6852093c6b4SMatthieu CASTET 	    HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
6862093c6b4SMatthieu CASTET 	dbg_hcs_params(ehci, "reset");
6872093c6b4SMatthieu CASTET 	dbg_hcc_params(ehci, "reset");
6882093c6b4SMatthieu CASTET 
6892093c6b4SMatthieu CASTET 	/* cache this readonly data; minimize chip reads */
6902093c6b4SMatthieu CASTET 	ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
6912093c6b4SMatthieu CASTET 
6922093c6b4SMatthieu CASTET 	ehci->sbrn = HCD_USB2;
6932093c6b4SMatthieu CASTET 
6942093c6b4SMatthieu CASTET 	/* data structure init */
6952093c6b4SMatthieu CASTET 	retval = ehci_init(hcd);
6962093c6b4SMatthieu CASTET 	if (retval)
6972093c6b4SMatthieu CASTET 		return retval;
6982093c6b4SMatthieu CASTET 
699631fe9d9SAlan Stern 	retval = ehci_halt(ehci);
700d8ff463aSJia-Ju Bai 	if (retval) {
701d8ff463aSJia-Ju Bai 		ehci_mem_cleanup(ehci);
7022093c6b4SMatthieu CASTET 		return retval;
703d8ff463aSJia-Ju Bai 	}
7042093c6b4SMatthieu CASTET 
7052093c6b4SMatthieu CASTET 	ehci_reset(ehci);
7062093c6b4SMatthieu CASTET 
7072093c6b4SMatthieu CASTET 	return 0;
7082093c6b4SMatthieu CASTET }
7093e023203SAlan Stern EXPORT_SYMBOL_GPL(ehci_setup);
7102093c6b4SMatthieu CASTET 
7111da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
7121da177e4SLinus Torvalds 
ehci_irq(struct usb_hcd * hcd)7137d12e780SDavid Howells static irqreturn_t ehci_irq (struct usb_hcd *hcd)
7141da177e4SLinus Torvalds {
7151da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
7160b605572SDavid Jeffery 	u32			status, current_status, masked_status, pcd_status = 0;
7170b605572SDavid Jeffery 	u32			cmd;
7181da177e4SLinus Torvalds 	int			bh;
7191da177e4SLinus Torvalds 
7208219ab4cSJohan Hovold 	spin_lock(&ehci->lock);
7211da177e4SLinus Torvalds 
7220b605572SDavid Jeffery 	status = 0;
7230b605572SDavid Jeffery 	current_status = ehci_readl(ehci, &ehci->regs->status);
7240b605572SDavid Jeffery restart:
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	/* e.g. cardbus physical eject */
7270b605572SDavid Jeffery 	if (current_status == ~(u32) 0) {
7281da177e4SLinus Torvalds 		ehci_dbg (ehci, "device removed\n");
7291da177e4SLinus Torvalds 		goto dead;
7301da177e4SLinus Torvalds 	}
7310b605572SDavid Jeffery 	status |= current_status;
7321da177e4SLinus Torvalds 
7332fbe2bf1SAlan Stern 	/*
7342fbe2bf1SAlan Stern 	 * We don't use STS_FLR, but some controllers don't like it to
7352fbe2bf1SAlan Stern 	 * remain on, so mask it out along with the other status bits.
7362fbe2bf1SAlan Stern 	 */
7370b605572SDavid Jeffery 	masked_status = current_status & (INTR_MASK | STS_FLR);
7382fbe2bf1SAlan Stern 
73969fff59dSAlan Stern 	/* Shared IRQ? */
740e8799906SAlan Stern 	if (!masked_status || unlikely(ehci->rh_state == EHCI_RH_HALTED)) {
7418219ab4cSJohan Hovold 		spin_unlock(&ehci->lock);
7421da177e4SLinus Torvalds 		return IRQ_NONE;
7431da177e4SLinus Torvalds 	}
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	/* clear (just) interrupts */
74667b2e029SAlan Stern 	ehci_writel(ehci, masked_status, &ehci->regs->status);
7470b605572SDavid Jeffery 
7480b605572SDavid Jeffery 	/* For edge interrupts, don't race with an interrupt bit being raised */
7490b605572SDavid Jeffery 	current_status = ehci_readl(ehci, &ehci->regs->status);
7500b605572SDavid Jeffery 	if (current_status & INTR_MASK)
7510b605572SDavid Jeffery 		goto restart;
7520b605572SDavid Jeffery 
753e82cc128SDavid Brownell 	cmd = ehci_readl(ehci, &ehci->regs->command);
7541da177e4SLinus Torvalds 	bh = 0;
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 	/* normal [4.15.1.2] or error [4.15.1.1] completion */
7571da177e4SLinus Torvalds 	if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
758dda4b60eSXu Yang 		if (likely ((status & STS_ERR) == 0)) {
759a0ef2bdfSCorentin Labbe 			INCR(ehci->stats.normal);
760dda4b60eSXu Yang 		} else {
761dda4b60eSXu Yang 			/* Force to check port status */
762dda4b60eSXu Yang 			if (ehci->has_ci_pec_bug)
763dda4b60eSXu Yang 				status |= STS_PCD;
764a0ef2bdfSCorentin Labbe 			INCR(ehci->stats.error);
765dda4b60eSXu Yang 		}
7661da177e4SLinus Torvalds 		bh = 1;
7671da177e4SLinus Torvalds 	}
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	/* complete the unlinking of some qh [4.15.2.3] */
7701da177e4SLinus Torvalds 	if (status & STS_IAA) {
7719d938747SAlan Stern 
7729d938747SAlan Stern 		/* Turn off the IAA watchdog */
7739d938747SAlan Stern 		ehci->enabled_hrtimer_events &= ~BIT(EHCI_HRTIMER_IAA_WATCHDOG);
7749d938747SAlan Stern 
7759d938747SAlan Stern 		/*
7769d938747SAlan Stern 		 * Mild optimization: Allow another IAAD to reset the
7779d938747SAlan Stern 		 * hrtimer, if one occurs before the next expiration.
7789d938747SAlan Stern 		 * In theory we could always cancel the hrtimer, but
7799d938747SAlan Stern 		 * tests show that about half the time it will be reset
7809d938747SAlan Stern 		 * for some other event anyway.
7819d938747SAlan Stern 		 */
7829d938747SAlan Stern 		if (ehci->next_hrtimer_event == EHCI_HRTIMER_IAA_WATCHDOG)
7839d938747SAlan Stern 			++ehci->next_hrtimer_event;
7849d938747SAlan Stern 
785e82cc128SDavid Brownell 		/* guard against (alleged) silicon errata */
7866feff1b9SAlan Stern 		if (cmd & CMD_IAAD)
787e82cc128SDavid Brownell 			ehci_dbg(ehci, "IAA with IAAD still set?\n");
788214ac7a0SAlan Stern 		if (ehci->iaa_in_progress)
789a0ef2bdfSCorentin Labbe 			INCR(ehci->stats.iaa);
790f96fba0dSAlan Stern 		end_iaa_cycle(ehci);
7911da177e4SLinus Torvalds 	}
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	/* remote wakeup [4.3.1] */
794d97cc2f2SDavid Brownell 	if (status & STS_PCD) {
7951da177e4SLinus Torvalds 		unsigned	i = HCS_N_PORTS (ehci->hcs_params);
7964dd405a4SAlan Stern 		u32		ppcd = ~0;
797d1b1842cSDavid Brownell 
798d1b1842cSDavid Brownell 		/* kick root hub later */
7991d619f12SMarcelo Tosatti 		pcd_status = status;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 		/* resume root hub? */
802dc75ce9dSAlan Stern 		if (ehci->rh_state == EHCI_RH_SUSPENDED)
8038c03356aSAlan Stern 			usb_hcd_resume_root_hub(hcd);
8041da177e4SLinus Torvalds 
8055a9cdf33SAlek Du 		/* get per-port change detect bits */
8065a9cdf33SAlek Du 		if (ehci->has_ppcd)
8075a9cdf33SAlek Du 			ppcd = status >> 16;
8085a9cdf33SAlek Du 
8091da177e4SLinus Torvalds 		while (i--) {
8105a9cdf33SAlek Du 			int pstatus;
8115a9cdf33SAlek Du 
8125a9cdf33SAlek Du 			/* leverage per-port change bits feature */
8134dd405a4SAlan Stern 			if (!(ppcd & (1 << i)))
8145a9cdf33SAlek Du 				continue;
8155a9cdf33SAlek Du 			pstatus = ehci_readl(ehci,
816083522d7SBenjamin Herrenschmidt 					 &ehci->regs->port_status[i]);
817b972b68cSDavid Brownell 
818b972b68cSDavid Brownell 			if (pstatus & PORT_OWNER)
8191da177e4SLinus Torvalds 				continue;
820eafe5b99SAlan Stern 			if (!(test_bit(i, &ehci->suspended_ports) &&
821eafe5b99SAlan Stern 					((pstatus & PORT_RESUME) ||
822eafe5b99SAlan Stern 						!(pstatus & PORT_SUSPEND)) &&
823eafe5b99SAlan Stern 					(pstatus & PORT_PE) &&
824eafe5b99SAlan Stern 					ehci->reset_done[i] == 0))
8251da177e4SLinus Torvalds 				continue;
8261da177e4SLinus Torvalds 
827ea16328fSFelipe Balbi 			/* start USB_RESUME_TIMEOUT msec resume signaling from
828ea16328fSFelipe Balbi 			 * this port, and make hub_wq collect
829ea16328fSFelipe Balbi 			 * PORT_STAT_C_SUSPEND to stop that signaling.
8301da177e4SLinus Torvalds 			 */
831ea16328fSFelipe Balbi 			ehci->reset_done[i] = jiffies +
832ea16328fSFelipe Balbi 				msecs_to_jiffies(USB_RESUME_TIMEOUT);
833a448e4dcSAlan Stern 			set_bit(i, &ehci->resuming_ports);
8341da177e4SLinus Torvalds 			ehci_dbg (ehci, "port %d remote wakeup\n", i + 1);
835f292e7f9SAlan Stern 			usb_hcd_start_port_resume(&hcd->self, i);
83661e8b858SAlan Stern 			mod_timer(&hcd->rh_timer, ehci->reset_done[i]);
8371da177e4SLinus Torvalds 		}
8381da177e4SLinus Torvalds 	}
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	/* PCI errors [4.15.2.4] */
8411da177e4SLinus Torvalds 	if (unlikely ((status & STS_FATAL) != 0)) {
84267b2e029SAlan Stern 		ehci_err(ehci, "fatal error\n");
843eafe5b99SAlan Stern 		dbg_cmd(ehci, "fatal", cmd);
8441da177e4SLinus Torvalds 		dbg_status(ehci, "fatal", status);
8451da177e4SLinus Torvalds dead:
84669fff59dSAlan Stern 		usb_hc_died(hcd);
847bf6387bcSAlan Stern 
848bf6387bcSAlan Stern 		/* Don't let the controller do anything more */
84943fe3a99SAlan Stern 		ehci->shutdown = true;
850bf6387bcSAlan Stern 		ehci->rh_state = EHCI_RH_STOPPING;
851bf6387bcSAlan Stern 		ehci->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE);
852bf6387bcSAlan Stern 		ehci_writel(ehci, ehci->command, &ehci->regs->command);
853bf6387bcSAlan Stern 		ehci_writel(ehci, 0, &ehci->regs->intr_enable);
854bf6387bcSAlan Stern 		ehci_handle_controller_death(ehci);
855bf6387bcSAlan Stern 
856bf6387bcSAlan Stern 		/* Handle completions when the controller stops */
857bf6387bcSAlan Stern 		bh = 0;
8581da177e4SLinus Torvalds 	}
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 	if (bh)
8617d12e780SDavid Howells 		ehci_work (ehci);
8628219ab4cSJohan Hovold 	spin_unlock(&ehci->lock);
863d1b1842cSDavid Brownell 	if (pcd_status)
8641d619f12SMarcelo Tosatti 		usb_hcd_poll_rh_status(hcd);
8651da177e4SLinus Torvalds 	return IRQ_HANDLED;
8661da177e4SLinus Torvalds }
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds /*
8711da177e4SLinus Torvalds  * non-error returns are a promise to giveback() the urb later
8721da177e4SLinus Torvalds  * we drop ownership so next owner (or urb unlink) can get it
8731da177e4SLinus Torvalds  *
8741da177e4SLinus Torvalds  * urb + dev is in hcd.self.controller.urb_list
8751da177e4SLinus Torvalds  * we're queueing TDs onto software and hardware lists
8761da177e4SLinus Torvalds  *
8771da177e4SLinus Torvalds  * hcd-specific init for hcpriv hasn't been done yet
8781da177e4SLinus Torvalds  *
8791da177e4SLinus Torvalds  * NOTE:  control, bulk, and interrupt share the same code to append TDs
8801da177e4SLinus Torvalds  * to a (possibly active) QH, and the same QH scanning code.
8811da177e4SLinus Torvalds  */
ehci_urb_enqueue(struct usb_hcd * hcd,struct urb * urb,gfp_t mem_flags)8821da177e4SLinus Torvalds static int ehci_urb_enqueue (
8831da177e4SLinus Torvalds 	struct usb_hcd	*hcd,
8841da177e4SLinus Torvalds 	struct urb	*urb,
88555016f10SAl Viro 	gfp_t		mem_flags
8861da177e4SLinus Torvalds ) {
8871da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
8881da177e4SLinus Torvalds 	struct list_head	qtd_list;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	INIT_LIST_HEAD (&qtd_list);
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	switch (usb_pipetype (urb->pipe)) {
89325b70a86SDavid Brownell 	case PIPE_CONTROL:
89425b70a86SDavid Brownell 		/* qh_completions() code doesn't handle all the fault cases
89525b70a86SDavid Brownell 		 * in multi-TD control transfers.  Even 1KB is rare anyway.
89625b70a86SDavid Brownell 		 */
89725b70a86SDavid Brownell 		if (urb->transfer_buffer_length > (16 * 1024))
89825b70a86SDavid Brownell 			return -EMSGSIZE;
8991d6903a6SNick Desaulniers 		fallthrough;
90025b70a86SDavid Brownell 	/* case PIPE_BULK: */
9011da177e4SLinus Torvalds 	default:
9021da177e4SLinus Torvalds 		if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
9031da177e4SLinus Torvalds 			return -ENOMEM;
904e9df41c5SAlan Stern 		return submit_async(ehci, urb, &qtd_list, mem_flags);
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	case PIPE_INTERRUPT:
9071da177e4SLinus Torvalds 		if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
9081da177e4SLinus Torvalds 			return -ENOMEM;
909e9df41c5SAlan Stern 		return intr_submit(ehci, urb, &qtd_list, mem_flags);
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	case PIPE_ISOCHRONOUS:
9121da177e4SLinus Torvalds 		if (urb->dev->speed == USB_SPEED_HIGH)
9131da177e4SLinus Torvalds 			return itd_submit (ehci, urb, mem_flags);
9141da177e4SLinus Torvalds 		else
9151da177e4SLinus Torvalds 			return sitd_submit (ehci, urb, mem_flags);
9161da177e4SLinus Torvalds 	}
9171da177e4SLinus Torvalds }
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds /* remove from hardware lists
9201da177e4SLinus Torvalds  * completions normally happen asynchronously
9211da177e4SLinus Torvalds  */
9221da177e4SLinus Torvalds 
ehci_urb_dequeue(struct usb_hcd * hcd,struct urb * urb,int status)923e9df41c5SAlan Stern static int ehci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
9241da177e4SLinus Torvalds {
9251da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
9261da177e4SLinus Torvalds 	struct ehci_qh		*qh;
9271da177e4SLinus Torvalds 	unsigned long		flags;
928e9df41c5SAlan Stern 	int			rc;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	spin_lock_irqsave (&ehci->lock, flags);
931e9df41c5SAlan Stern 	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
932e9df41c5SAlan Stern 	if (rc)
933e9df41c5SAlan Stern 		goto done;
934e9df41c5SAlan Stern 
9357655e316SAlan Stern 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
9367655e316SAlan Stern 		/*
9377655e316SAlan Stern 		 * We don't expedite dequeue for isochronous URBs.
9387655e316SAlan Stern 		 * Just wait until they complete normally or their
9397655e316SAlan Stern 		 * time slot expires.
9407655e316SAlan Stern 		 */
9417655e316SAlan Stern 	} else {
9421da177e4SLinus Torvalds 		qh = (struct ehci_qh *) urb->hcpriv;
943fcc5184eSAlan Stern 		qh->unlink_reason |= QH_UNLINK_REQUESTED;
94407d29b63SAlan Stern 		switch (qh->qh_state) {
94507d29b63SAlan Stern 		case QH_STATE_LINKED:
9467655e316SAlan Stern 			if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)
9477655e316SAlan Stern 				start_unlink_intr(ehci, qh);
9487655e316SAlan Stern 			else
9493c273a05SAlan Stern 				start_unlink_async(ehci, qh);
9501da177e4SLinus Torvalds 			break;
9517bc782d7SAlan Stern 		case QH_STATE_COMPLETING:
9527bc782d7SAlan Stern 			qh->dequeue_during_giveback = 1;
9537bc782d7SAlan Stern 			break;
95407d29b63SAlan Stern 		case QH_STATE_UNLINK:
95507d29b63SAlan Stern 		case QH_STATE_UNLINK_WAIT:
95607d29b63SAlan Stern 			/* already started */
95707d29b63SAlan Stern 			break;
95807d29b63SAlan Stern 		case QH_STATE_IDLE:
9597a0f0d95SAlan Stern 			/* QH might be waiting for a Clear-TT-Buffer */
9607a0f0d95SAlan Stern 			qh_completions(ehci, qh);
96107d29b63SAlan Stern 			break;
96207d29b63SAlan Stern 		}
9631da177e4SLinus Torvalds 	}
9641da177e4SLinus Torvalds done:
9651da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ehci->lock, flags);
966e9df41c5SAlan Stern 	return rc;
9671da177e4SLinus Torvalds }
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds // bulk qh holds the data toggle
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds static void
ehci_endpoint_disable(struct usb_hcd * hcd,struct usb_host_endpoint * ep)9741da177e4SLinus Torvalds ehci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
9751da177e4SLinus Torvalds {
9761da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
9771da177e4SLinus Torvalds 	unsigned long		flags;
97835371e4fSMing Lei 	struct ehci_qh		*qh;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	/* ASSERT:  any requests/urbs are being unlinked */
9811da177e4SLinus Torvalds 	/* ASSERT:  nobody can be submitting urbs for this any more */
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds rescan:
9841da177e4SLinus Torvalds 	spin_lock_irqsave (&ehci->lock, flags);
9851da177e4SLinus Torvalds 	qh = ep->hcpriv;
9861da177e4SLinus Torvalds 	if (!qh)
9871da177e4SLinus Torvalds 		goto done;
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	/* endpoints can be iso streams.  for now, we don't
9901da177e4SLinus Torvalds 	 * accelerate iso completions ... so spin a while.
9911da177e4SLinus Torvalds 	 */
9921082f57aSClemens Ladisch 	if (qh->hw == NULL) {
9938c5bf7beSAlan Stern 		struct ehci_iso_stream	*stream = ep->hcpriv;
9948c5bf7beSAlan Stern 
9958c5bf7beSAlan Stern 		if (!list_empty(&stream->td_list))
9961da177e4SLinus Torvalds 			goto idle_timeout;
9978c5bf7beSAlan Stern 
9988c5bf7beSAlan Stern 		/* BUG_ON(!list_empty(&stream->free_list)); */
999d0ce5c6bSAlan Stern 		reserve_release_iso_bandwidth(ehci, stream, -1);
10008c5bf7beSAlan Stern 		kfree(stream);
10018c5bf7beSAlan Stern 		goto done;
10021da177e4SLinus Torvalds 	}
10031da177e4SLinus Torvalds 
1004fcc5184eSAlan Stern 	qh->unlink_reason |= QH_UNLINK_REQUESTED;
10051da177e4SLinus Torvalds 	switch (qh->qh_state) {
10061da177e4SLinus Torvalds 	case QH_STATE_LINKED:
1007fcc5184eSAlan Stern 		if (list_empty(&qh->qtd_list))
1008fcc5184eSAlan Stern 			qh->unlink_reason |= QH_UNLINK_QUEUE_EMPTY;
1009fcc5184eSAlan Stern 		else
1010fcc5184eSAlan Stern 			WARN_ON(1);
101135371e4fSMing Lei 		if (usb_endpoint_type(&ep->desc) != USB_ENDPOINT_XFER_INT)
10123c273a05SAlan Stern 			start_unlink_async(ehci, qh);
101335371e4fSMing Lei 		else
101435371e4fSMing Lei 			start_unlink_intr(ehci, qh);
10158b84724eSGustavo A. R. Silva 		fallthrough;
101635371e4fSMing Lei 	case QH_STATE_COMPLETING:	/* already in unlinking */
10171da177e4SLinus Torvalds 	case QH_STATE_UNLINK:		/* wait for hw to finish? */
101807d29b63SAlan Stern 	case QH_STATE_UNLINK_WAIT:
10191da177e4SLinus Torvalds idle_timeout:
10201da177e4SLinus Torvalds 		spin_unlock_irqrestore (&ehci->lock, flags);
102122c43863SNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
10221da177e4SLinus Torvalds 		goto rescan;
10231da177e4SLinus Torvalds 	case QH_STATE_IDLE:		/* fully unlinked */
1024914b7012SAlan Stern 		if (qh->clearing_tt)
1025914b7012SAlan Stern 			goto idle_timeout;
10261da177e4SLinus Torvalds 		if (list_empty (&qh->qtd_list)) {
1027d0ce5c6bSAlan Stern 			if (qh->ps.bw_uperiod)
1028d0ce5c6bSAlan Stern 				reserve_release_intr_bandwidth(ehci, qh, -1);
1029c83e1a9fSAlan Stern 			qh_destroy(ehci, qh);
10301da177e4SLinus Torvalds 			break;
10311da177e4SLinus Torvalds 		}
10328b84724eSGustavo A. R. Silva 		fallthrough;
10331da177e4SLinus Torvalds 	default:
10341da177e4SLinus Torvalds 		/* caller was supposed to have unlinked any requests;
10351da177e4SLinus Torvalds 		 * that's not our job.  just leak this memory.
10361da177e4SLinus Torvalds 		 */
10371da177e4SLinus Torvalds 		ehci_err (ehci, "qh %p (#%02x) state %d%s\n",
10381da177e4SLinus Torvalds 			qh, ep->desc.bEndpointAddress, qh->qh_state,
10391da177e4SLinus Torvalds 			list_empty (&qh->qtd_list) ? "" : "(has tds)");
10401da177e4SLinus Torvalds 		break;
10411da177e4SLinus Torvalds 	}
10421da177e4SLinus Torvalds  done:
10438c5bf7beSAlan Stern 	ep->hcpriv = NULL;
10441da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ehci->lock, flags);
10451da177e4SLinus Torvalds }
10461da177e4SLinus Torvalds 
1047b18ffd49SAlan Stern static void
ehci_endpoint_reset(struct usb_hcd * hcd,struct usb_host_endpoint * ep)1048b18ffd49SAlan Stern ehci_endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1049b18ffd49SAlan Stern {
1050b18ffd49SAlan Stern 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
1051b18ffd49SAlan Stern 	struct ehci_qh		*qh;
1052b18ffd49SAlan Stern 	int			eptype = usb_endpoint_type(&ep->desc);
1053a455212dSAlan Stern 	int			epnum = usb_endpoint_num(&ep->desc);
1054a455212dSAlan Stern 	int			is_out = usb_endpoint_dir_out(&ep->desc);
1055a455212dSAlan Stern 	unsigned long		flags;
1056b18ffd49SAlan Stern 
1057b18ffd49SAlan Stern 	if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
1058b18ffd49SAlan Stern 		return;
1059b18ffd49SAlan Stern 
1060a455212dSAlan Stern 	spin_lock_irqsave(&ehci->lock, flags);
1061b18ffd49SAlan Stern 	qh = ep->hcpriv;
1062b18ffd49SAlan Stern 
1063b18ffd49SAlan Stern 	/* For Bulk and Interrupt endpoints we maintain the toggle state
1064b18ffd49SAlan Stern 	 * in the hardware; the toggle bits in udev aren't used at all.
1065b18ffd49SAlan Stern 	 * When an endpoint is reset by usb_clear_halt() we must reset
1066b18ffd49SAlan Stern 	 * the toggle bit in the QH.
1067b18ffd49SAlan Stern 	 */
1068b18ffd49SAlan Stern 	if (qh) {
1069b18ffd49SAlan Stern 		if (!list_empty(&qh->qtd_list)) {
1070b18ffd49SAlan Stern 			WARN_ONCE(1, "clear_halt for a busy endpoint\n");
10717bc782d7SAlan Stern 		} else {
1072a455212dSAlan Stern 			/* The toggle value in the QH can't be updated
1073a455212dSAlan Stern 			 * while the QH is active.  Unlink it now;
1074a455212dSAlan Stern 			 * re-linking will call qh_refresh().
1075b18ffd49SAlan Stern 			 */
1076ffa0248eSAlan Stern 			usb_settoggle(qh->ps.udev, epnum, is_out, 0);
1077fcc5184eSAlan Stern 			qh->unlink_reason |= QH_UNLINK_REQUESTED;
1078a448c9d8SAlan Stern 			if (eptype == USB_ENDPOINT_XFER_BULK)
10793c273a05SAlan Stern 				start_unlink_async(ehci, qh);
1080a448c9d8SAlan Stern 			else
1081df202255SAlan Stern 				start_unlink_intr(ehci, qh);
1082b18ffd49SAlan Stern 		}
1083b18ffd49SAlan Stern 	}
1084a455212dSAlan Stern 	spin_unlock_irqrestore(&ehci->lock, flags);
1085b18ffd49SAlan Stern }
1086b18ffd49SAlan Stern 
ehci_get_frame(struct usb_hcd * hcd)10877ff71d6aSMatt Porter static int ehci_get_frame (struct usb_hcd *hcd)
10887ff71d6aSMatt Porter {
10897ff71d6aSMatt Porter 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
109068aa95d5SAlan Stern 	return (ehci_read_frame_index(ehci) >> 3) % ehci->periodic_size;
10917ff71d6aSMatt Porter }
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1094c5cf9212SAlan Stern 
1095b35c5009SAlan Stern /* Device addition and removal */
1096b35c5009SAlan Stern 
ehci_remove_device(struct usb_hcd * hcd,struct usb_device * udev)1097b35c5009SAlan Stern static void ehci_remove_device(struct usb_hcd *hcd, struct usb_device *udev)
1098b35c5009SAlan Stern {
1099b35c5009SAlan Stern 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
1100b35c5009SAlan Stern 
1101b35c5009SAlan Stern 	spin_lock_irq(&ehci->lock);
1102b35c5009SAlan Stern 	drop_tt(udev);
1103b35c5009SAlan Stern 	spin_unlock_irq(&ehci->lock);
1104b35c5009SAlan Stern }
1105b35c5009SAlan Stern 
1106b35c5009SAlan Stern /*-------------------------------------------------------------------------*/
1107b35c5009SAlan Stern 
1108c5cf9212SAlan Stern #ifdef	CONFIG_PM
1109c5cf9212SAlan Stern 
1110f085bd4bSWeitao Wango /* Clear wakeup signal locked in zhaoxin platform when device plug in. */
ehci_zx_wakeup_clear(struct ehci_hcd * ehci)1111f085bd4bSWeitao Wango static void ehci_zx_wakeup_clear(struct ehci_hcd *ehci)
1112f085bd4bSWeitao Wango {
1113f085bd4bSWeitao Wango 	u32 __iomem	*reg = &ehci->regs->port_status[4];
1114f085bd4bSWeitao Wango 	u32 		t1 = ehci_readl(ehci, reg);
1115f085bd4bSWeitao Wango 
1116f085bd4bSWeitao Wango 	t1 &= (u32)~0xf0000;
1117f085bd4bSWeitao Wango 	t1 |= PORT_TEST_FORCE;
1118f085bd4bSWeitao Wango 	ehci_writel(ehci, t1, reg);
1119f085bd4bSWeitao Wango 	t1 = ehci_readl(ehci, reg);
1120f085bd4bSWeitao Wango 	msleep(1);
1121f085bd4bSWeitao Wango 	t1 &= (u32)~0xf0000;
1122f085bd4bSWeitao Wango 	ehci_writel(ehci, t1, reg);
1123f085bd4bSWeitao Wango 	ehci_readl(ehci, reg);
1124f085bd4bSWeitao Wango 	msleep(1);
1125f085bd4bSWeitao Wango 	t1 = ehci_readl(ehci, reg);
1126f085bd4bSWeitao Wango 	ehci_writel(ehci, t1 | PORT_CSC, reg);
1127f085bd4bSWeitao Wango 	ehci_readl(ehci, reg);
1128f085bd4bSWeitao Wango }
1129f085bd4bSWeitao Wango 
1130c5cf9212SAlan Stern /* suspend/resume, section 4.3 */
1131c5cf9212SAlan Stern 
1132c5cf9212SAlan Stern /* These routines handle the generic parts of controller suspend/resume */
1133c5cf9212SAlan Stern 
ehci_suspend(struct usb_hcd * hcd,bool do_wakeup)11343e023203SAlan Stern int ehci_suspend(struct usb_hcd *hcd, bool do_wakeup)
1135c5cf9212SAlan Stern {
1136c5cf9212SAlan Stern 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
1137c5cf9212SAlan Stern 
1138c5cf9212SAlan Stern 	if (time_before(jiffies, ehci->next_statechange))
1139c5cf9212SAlan Stern 		msleep(10);
1140c5cf9212SAlan Stern 
1141c5cf9212SAlan Stern 	/*
1142c5cf9212SAlan Stern 	 * Root hub was already suspended.  Disable IRQ emission and
1143c5cf9212SAlan Stern 	 * mark HW unaccessible.  The PM and USB cores make sure that
1144c5cf9212SAlan Stern 	 * the root hub is either suspended or stopped.
1145c5cf9212SAlan Stern 	 */
1146c5cf9212SAlan Stern 	ehci_prepare_ports_for_controller_suspend(ehci, do_wakeup);
1147c5cf9212SAlan Stern 
1148c5cf9212SAlan Stern 	spin_lock_irq(&ehci->lock);
1149c5cf9212SAlan Stern 	ehci_writel(ehci, 0, &ehci->regs->intr_enable);
1150c5cf9212SAlan Stern 	(void) ehci_readl(ehci, &ehci->regs->intr_enable);
1151c5cf9212SAlan Stern 
1152c5cf9212SAlan Stern 	clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1153c5cf9212SAlan Stern 	spin_unlock_irq(&ehci->lock);
1154c5cf9212SAlan Stern 
1155b8efdafbSAlan Stern 	synchronize_irq(hcd->irq);
1156b8efdafbSAlan Stern 
1157b8efdafbSAlan Stern 	/* Check for race with a wakeup request */
1158b8efdafbSAlan Stern 	if (do_wakeup && HCD_WAKEUP_PENDING(hcd)) {
1159b8efdafbSAlan Stern 		ehci_resume(hcd, false);
1160b8efdafbSAlan Stern 		return -EBUSY;
1161b8efdafbSAlan Stern 	}
1162b8efdafbSAlan Stern 
1163c5cf9212SAlan Stern 	return 0;
1164c5cf9212SAlan Stern }
11653e023203SAlan Stern EXPORT_SYMBOL_GPL(ehci_suspend);
1166c5cf9212SAlan Stern 
1167c5cf9212SAlan Stern /* Returns 0 if power was preserved, 1 if power was lost */
ehci_resume(struct usb_hcd * hcd,bool force_reset)1168314b41b1SWu Liang feng int ehci_resume(struct usb_hcd *hcd, bool force_reset)
1169c5cf9212SAlan Stern {
1170c5cf9212SAlan Stern 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
1171c5cf9212SAlan Stern 
1172c5cf9212SAlan Stern 	if (time_before(jiffies, ehci->next_statechange))
1173c5cf9212SAlan Stern 		msleep(100);
1174c5cf9212SAlan Stern 
1175c5cf9212SAlan Stern 	/* Mark hardware accessible again as we are back to full power by now */
1176c5cf9212SAlan Stern 	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1177c5cf9212SAlan Stern 
117843fe3a99SAlan Stern 	if (ehci->shutdown)
117943fe3a99SAlan Stern 		return 0;		/* Controller is dead */
118043fe3a99SAlan Stern 
1181f085bd4bSWeitao Wango 	if (ehci->zx_wakeup_clear_needed)
1182f085bd4bSWeitao Wango 		ehci_zx_wakeup_clear(ehci);
1183f085bd4bSWeitao Wango 
1184c5cf9212SAlan Stern 	/*
1185314b41b1SWu Liang feng 	 * If CF is still set and reset isn't forced
1186c5cf9212SAlan Stern 	 * then we maintained suspend power.
1187c5cf9212SAlan Stern 	 * Just undo the effect of ehci_suspend().
1188c5cf9212SAlan Stern 	 */
1189c5cf9212SAlan Stern 	if (ehci_readl(ehci, &ehci->regs->configured_flag) == FLAG_CF &&
1190314b41b1SWu Liang feng 			!force_reset) {
1191c5cf9212SAlan Stern 		int	mask = INTR_MASK;
1192c5cf9212SAlan Stern 
1193c5cf9212SAlan Stern 		ehci_prepare_ports_for_controller_resume(ehci);
119443fe3a99SAlan Stern 
119543fe3a99SAlan Stern 		spin_lock_irq(&ehci->lock);
119643fe3a99SAlan Stern 		if (ehci->shutdown)
119743fe3a99SAlan Stern 			goto skip;
119843fe3a99SAlan Stern 
1199c5cf9212SAlan Stern 		if (!hcd->self.root_hub->do_remote_wakeup)
1200c5cf9212SAlan Stern 			mask &= ~STS_PCD;
1201c5cf9212SAlan Stern 		ehci_writel(ehci, mask, &ehci->regs->intr_enable);
1202c5cf9212SAlan Stern 		ehci_readl(ehci, &ehci->regs->intr_enable);
120343fe3a99SAlan Stern  skip:
120443fe3a99SAlan Stern 		spin_unlock_irq(&ehci->lock);
1205c5cf9212SAlan Stern 		return 0;
1206c5cf9212SAlan Stern 	}
1207c5cf9212SAlan Stern 
1208c5cf9212SAlan Stern 	/*
1209c5cf9212SAlan Stern 	 * Else reset, to cope with power loss or resume from hibernation
1210c5cf9212SAlan Stern 	 * having let the firmware kick in during reboot.
1211c5cf9212SAlan Stern 	 */
1212c5cf9212SAlan Stern 	usb_root_hub_lost_power(hcd->self.root_hub);
1213c5cf9212SAlan Stern 	(void) ehci_halt(ehci);
1214c5cf9212SAlan Stern 	(void) ehci_reset(ehci);
1215c5cf9212SAlan Stern 
121643fe3a99SAlan Stern 	spin_lock_irq(&ehci->lock);
121743fe3a99SAlan Stern 	if (ehci->shutdown)
121843fe3a99SAlan Stern 		goto skip;
121943fe3a99SAlan Stern 
1220c5cf9212SAlan Stern 	ehci_writel(ehci, ehci->command, &ehci->regs->command);
1221c5cf9212SAlan Stern 	ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
1222c5cf9212SAlan Stern 	ehci_readl(ehci, &ehci->regs->command);	/* unblock posted writes */
1223c5cf9212SAlan Stern 
122443fe3a99SAlan Stern 	ehci->rh_state = EHCI_RH_SUSPENDED;
122543fe3a99SAlan Stern 	spin_unlock_irq(&ehci->lock);
122643fe3a99SAlan Stern 
1227c5cf9212SAlan Stern 	return 1;
1228c5cf9212SAlan Stern }
12293e023203SAlan Stern EXPORT_SYMBOL_GPL(ehci_resume);
1230c5cf9212SAlan Stern 
1231c5cf9212SAlan Stern #endif
1232c5cf9212SAlan Stern 
1233c5cf9212SAlan Stern /*-------------------------------------------------------------------------*/
1234c5cf9212SAlan Stern 
1235eb70e5abSAlexander Shishkin /*
12363e023203SAlan Stern  * Generic structure: This gets copied for platform drivers so that
12373e023203SAlan Stern  * individual entries can be overridden as needed.
12383e023203SAlan Stern  */
12393e023203SAlan Stern 
12403e023203SAlan Stern static const struct hc_driver ehci_hc_driver = {
12413e023203SAlan Stern 	.description =		hcd_name,
12423e023203SAlan Stern 	.product_desc =		"EHCI Host Controller",
12433e023203SAlan Stern 	.hcd_priv_size =	sizeof(struct ehci_hcd),
12443e023203SAlan Stern 
12453e023203SAlan Stern 	/*
12463e023203SAlan Stern 	 * generic hardware linkage
12473e023203SAlan Stern 	 */
12483e023203SAlan Stern 	.irq =			ehci_irq,
12497b81cb6bSChristoph Hellwig 	.flags =		HCD_MEMORY | HCD_DMA | HCD_USB2 | HCD_BH,
12503e023203SAlan Stern 
12513e023203SAlan Stern 	/*
12523e023203SAlan Stern 	 * basic lifecycle operations
12533e023203SAlan Stern 	 */
12543e023203SAlan Stern 	.reset =		ehci_setup,
12553e023203SAlan Stern 	.start =		ehci_run,
12563e023203SAlan Stern 	.stop =			ehci_stop,
12573e023203SAlan Stern 	.shutdown =		ehci_shutdown,
12583e023203SAlan Stern 
12593e023203SAlan Stern 	/*
12603e023203SAlan Stern 	 * managing i/o requests and associated device resources
12613e023203SAlan Stern 	 */
12623e023203SAlan Stern 	.urb_enqueue =		ehci_urb_enqueue,
12633e023203SAlan Stern 	.urb_dequeue =		ehci_urb_dequeue,
12643e023203SAlan Stern 	.endpoint_disable =	ehci_endpoint_disable,
12653e023203SAlan Stern 	.endpoint_reset =	ehci_endpoint_reset,
12663e023203SAlan Stern 	.clear_tt_buffer_complete =	ehci_clear_tt_buffer_complete,
12673e023203SAlan Stern 
12683e023203SAlan Stern 	/*
12693e023203SAlan Stern 	 * scheduling support
12703e023203SAlan Stern 	 */
12713e023203SAlan Stern 	.get_frame_number =	ehci_get_frame,
12723e023203SAlan Stern 
12733e023203SAlan Stern 	/*
12743e023203SAlan Stern 	 * root hub support
12753e023203SAlan Stern 	 */
12763e023203SAlan Stern 	.hub_status_data =	ehci_hub_status_data,
12773e023203SAlan Stern 	.hub_control =		ehci_hub_control,
12783e023203SAlan Stern 	.bus_suspend =		ehci_bus_suspend,
12793e023203SAlan Stern 	.bus_resume =		ehci_bus_resume,
12803e023203SAlan Stern 	.relinquish_port =	ehci_relinquish_port,
12813e023203SAlan Stern 	.port_handed_over =	ehci_port_handed_over,
128200d423c8SAlan Stern 	.get_resuming_ports =	ehci_get_resuming_ports,
1283b35c5009SAlan Stern 
1284b35c5009SAlan Stern 	/*
1285b35c5009SAlan Stern 	 * device support
1286b35c5009SAlan Stern 	 */
1287b35c5009SAlan Stern 	.free_dev =		ehci_remove_device,
1288cbbc07e1SPeter Chen #ifdef CONFIG_USB_HCD_TEST_MODE
1289cbbc07e1SPeter Chen 	/* EH SINGLE_STEP_SET_FEATURE test support */
1290cbbc07e1SPeter Chen 	.submit_single_step_set_feature	= ehci_submit_single_step_set_feature,
1291cbbc07e1SPeter Chen #endif
12923e023203SAlan Stern };
12933e023203SAlan Stern 
ehci_init_driver(struct hc_driver * drv,const struct ehci_driver_overrides * over)12943e023203SAlan Stern void ehci_init_driver(struct hc_driver *drv,
12953e023203SAlan Stern 		const struct ehci_driver_overrides *over)
12963e023203SAlan Stern {
12973e023203SAlan Stern 	/* Copy the generic table to drv and then apply the overrides */
12983e023203SAlan Stern 	*drv = ehci_hc_driver;
12993e023203SAlan Stern 
13001b36810eSAlan Stern 	if (over) {
13013e023203SAlan Stern 		drv->hcd_priv_size += over->extra_priv_size;
13023e023203SAlan Stern 		if (over->reset)
13033e023203SAlan Stern 			drv->reset = over->reset;
130411a7e594SMichael Grzeschik 		if (over->port_power)
130511a7e594SMichael Grzeschik 			drv->port_power = over->port_power;
13063e023203SAlan Stern 	}
13071b36810eSAlan Stern }
13083e023203SAlan Stern EXPORT_SYMBOL_GPL(ehci_init_driver);
13093e023203SAlan Stern 
13103e023203SAlan Stern /*-------------------------------------------------------------------------*/
13113e023203SAlan Stern 
13122b70f073SAlan Stern MODULE_DESCRIPTION(DRIVER_DESC);
13131da177e4SLinus Torvalds MODULE_AUTHOR (DRIVER_AUTHOR);
13141da177e4SLinus Torvalds MODULE_LICENSE ("GPL");
13151da177e4SLinus Torvalds 
131660b0bf0fSYoshihiro Shimoda #ifdef CONFIG_USB_EHCI_SH
131763c84552SPaul Mundt #include "ehci-sh.c"
131863c84552SPaul Mundt #endif
131963c84552SPaul Mundt 
1320ad75a410SGeoff Levand #ifdef CONFIG_PPC_PS3
1321ad75a410SGeoff Levand #include "ehci-ps3.c"
1322ad75a410SGeoff Levand #endif
1323ad75a410SGeoff Levand 
1324da0e8fb0SValentine Barshak #ifdef CONFIG_USB_EHCI_HCD_PPC_OF
1325da0e8fb0SValentine Barshak #include "ehci-ppc-of.c"
1326da0e8fb0SValentine Barshak #endif
1327da0e8fb0SValentine Barshak 
132808d3c18eSJulie Zhu #ifdef CONFIG_XPS_USB_HCD_XILINX
132908d3c18eSJulie Zhu #include "ehci-xilinx-of.c"
133008d3c18eSJulie Zhu #endif
133108d3c18eSJulie Zhu 
13329be03929SJan Andersson #ifdef CONFIG_SPARC_LEON
13339be03929SJan Andersson #include "ehci-grlib.c"
13349be03929SJan Andersson #endif
13359be03929SJan Andersson 
1336aad06846SGuenter Roeck static struct platform_driver * const platform_drivers[] = {
1337aad06846SGuenter Roeck #ifdef CONFIG_USB_EHCI_SH
1338aad06846SGuenter Roeck 	&ehci_hcd_sh_driver,
1339aad06846SGuenter Roeck #endif
1340aad06846SGuenter Roeck #ifdef CONFIG_USB_EHCI_HCD_PPC_OF
1341aad06846SGuenter Roeck 	&ehci_hcd_ppc_of_driver,
1342aad06846SGuenter Roeck #endif
1343aad06846SGuenter Roeck #ifdef CONFIG_XPS_USB_HCD_XILINX
1344aad06846SGuenter Roeck 	&ehci_hcd_xilinx_of_driver,
1345aad06846SGuenter Roeck #endif
1346aad06846SGuenter Roeck #ifdef CONFIG_SPARC_LEON
1347aad06846SGuenter Roeck 	&ehci_grlib_driver,
1348aad06846SGuenter Roeck #endif
1349aad06846SGuenter Roeck };
1350aad06846SGuenter Roeck 
ehci_hcd_init(void)135101cced25SKumar Gala static int __init ehci_hcd_init(void)
135201cced25SKumar Gala {
135301cced25SKumar Gala 	int retval = 0;
135401cced25SKumar Gala 
13552b70f073SAlan Stern 	if (usb_disabled())
13562b70f073SAlan Stern 		return -ENODEV;
13572b70f073SAlan Stern 
13589beeee65SAlan Stern 	set_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
13599beeee65SAlan Stern 	if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) ||
13609beeee65SAlan Stern 			test_bit(USB_OHCI_LOADED, &usb_hcds_loaded))
13619beeee65SAlan Stern 		printk(KERN_WARNING "Warning! ehci_hcd should always be loaded"
13629beeee65SAlan Stern 				" before uhci_hcd and ohci_hcd, not after\n");
13639beeee65SAlan Stern 
13645b5e0928SAlexey Dobriyan 	pr_debug("%s: block sizes: qh %zd qtd %zd itd %zd sitd %zd\n",
136501cced25SKumar Gala 		 hcd_name,
136601cced25SKumar Gala 		 sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
136701cced25SKumar Gala 		 sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
136801cced25SKumar Gala 
13691c20163dSOliver Neukum #ifdef CONFIG_DYNAMIC_DEBUG
137008f4e586SGreg Kroah-Hartman 	ehci_debug_root = debugfs_create_dir("ehci", usb_debug_root);
1371694cc208STony Jones #endif
1372694cc208STony Jones 
1373aad06846SGuenter Roeck 	retval = platform_register_drivers(platform_drivers, ARRAY_SIZE(platform_drivers));
1374da0e8fb0SValentine Barshak 	if (retval < 0)
1375da0e8fb0SValentine Barshak 		goto clean0;
137601cced25SKumar Gala 
1377aad06846SGuenter Roeck #ifdef CONFIG_PPC_PS3
1378aad06846SGuenter Roeck 	retval = ps3_ehci_driver_register(&ps3_ehci_driver);
1379da0e8fb0SValentine Barshak 	if (retval < 0)
1380aad06846SGuenter Roeck 		goto clean1;
1381da0e8fb0SValentine Barshak #endif
1382da0e8fb0SValentine Barshak 
1383aad06846SGuenter Roeck 	return 0;
13841f23b2d9SGrant Likely 
1385aad06846SGuenter Roeck #ifdef CONFIG_PPC_PS3
1386aad06846SGuenter Roeck clean1:
13871f23b2d9SGrant Likely #endif
1388aad06846SGuenter Roeck 	platform_unregister_drivers(platform_drivers, ARRAY_SIZE(platform_drivers));
1389da0e8fb0SValentine Barshak clean0:
13901c20163dSOliver Neukum #ifdef CONFIG_DYNAMIC_DEBUG
1391694cc208STony Jones 	debugfs_remove(ehci_debug_root);
1392694cc208STony Jones 	ehci_debug_root = NULL;
1393a9b6148dSLinus Torvalds #endif
13949beeee65SAlan Stern 	clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
139501cced25SKumar Gala 	return retval;
139601cced25SKumar Gala }
139701cced25SKumar Gala module_init(ehci_hcd_init);
139801cced25SKumar Gala 
ehci_hcd_cleanup(void)139901cced25SKumar Gala static void __exit ehci_hcd_cleanup(void)
140001cced25SKumar Gala {
1401aad06846SGuenter Roeck #ifdef CONFIG_PPC_PS3
1402aad06846SGuenter Roeck 	ps3_ehci_driver_unregister(&ps3_ehci_driver);
14031f23b2d9SGrant Likely #endif
1404aad06846SGuenter Roeck 	platform_unregister_drivers(platform_drivers, ARRAY_SIZE(platform_drivers));
14051c20163dSOliver Neukum #ifdef CONFIG_DYNAMIC_DEBUG
1406694cc208STony Jones 	debugfs_remove(ehci_debug_root);
1407694cc208STony Jones #endif
14089beeee65SAlan Stern 	clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
140901cced25SKumar Gala }
141001cced25SKumar Gala module_exit(ehci_hcd_cleanup);
1411