xref: /openbmc/linux/drivers/usb/host/ehci-hub.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
21da177e4SLinus Torvalds /*
3d49d4317SDavid Brownell  * Copyright (C) 2001-2004 by David Brownell
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds /* this file is part of ehci-hcd.c */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds /*
111da177e4SLinus Torvalds  * EHCI Root Hub ... the nonsharable stuff
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * Registers don't need cpu_to_le32, that happens transparently
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
171da177e4SLinus Torvalds 
1858a97ffeSAlan Stern #define	PORT_WAKE_BITS	(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
1958a97ffeSAlan Stern 
20aff6d18fSAlan Stern #ifdef	CONFIG_PM
21aff6d18fSAlan Stern 
221586ba0cSAlan Stern static void unlink_empty_async_suspended(struct ehci_hcd *ehci);
231586ba0cSAlan Stern 
persist_enabled_on_companion(struct usb_device * udev,void * unused)249b790915SJulius Werner static int persist_enabled_on_companion(struct usb_device *udev, void *unused)
259b790915SJulius Werner {
269b790915SJulius Werner 	return !udev->maxchild && udev->persist_enabled &&
279b790915SJulius Werner 		udev->bus->root_hub->speed < USB_SPEED_HIGH;
289b790915SJulius Werner }
299b790915SJulius Werner 
30383975d7SAlan Stern /* After a power loss, ports that were owned by the companion must be
31383975d7SAlan Stern  * reset so that the companion can still own them.
32383975d7SAlan Stern  */
ehci_handover_companion_ports(struct ehci_hcd * ehci)33383975d7SAlan Stern static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
34383975d7SAlan Stern {
35383975d7SAlan Stern 	u32 __iomem	*reg;
36383975d7SAlan Stern 	u32		status;
37383975d7SAlan Stern 	int		port;
38383975d7SAlan Stern 	__le32		buf;
39383975d7SAlan Stern 	struct usb_hcd	*hcd = ehci_to_hcd(ehci);
40383975d7SAlan Stern 
41383975d7SAlan Stern 	if (!ehci->owned_ports)
42383975d7SAlan Stern 		return;
43383975d7SAlan Stern 
449b790915SJulius Werner 	/*
459b790915SJulius Werner 	 * USB 1.1 devices are mostly HIDs, which don't need to persist across
469b790915SJulius Werner 	 * suspends. If we ensure that none of our companion's devices have
479b790915SJulius Werner 	 * persist_enabled (by looking through all USB 1.1 buses in the system),
489b790915SJulius Werner 	 * we can skip this and avoid slowing resume down. Devices without
499b790915SJulius Werner 	 * persist will just get reenumerated shortly after resume anyway.
509b790915SJulius Werner 	 */
519b790915SJulius Werner 	if (!usb_for_each_dev(NULL, persist_enabled_on_companion))
529b790915SJulius Werner 		return;
539b790915SJulius Werner 
54c73cee71SAlan Stern 	/* Make sure the ports are powered */
55c73cee71SAlan Stern 	port = HCS_N_PORTS(ehci->hcs_params);
56c73cee71SAlan Stern 	while (port--) {
57c73cee71SAlan Stern 		if (test_bit(port, &ehci->owned_ports)) {
58c73cee71SAlan Stern 			reg = &ehci->regs->port_status[port];
59c73cee71SAlan Stern 			status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
6011a7e594SMichael Grzeschik 			if (!(status & PORT_POWER))
6111a7e594SMichael Grzeschik 				ehci_port_power(ehci, port, true);
62c73cee71SAlan Stern 		}
63c73cee71SAlan Stern 	}
64c73cee71SAlan Stern 
65383975d7SAlan Stern 	/* Give the connections some time to appear */
66383975d7SAlan Stern 	msleep(20);
67383975d7SAlan Stern 
68c4f34764SAlan Stern 	spin_lock_irq(&ehci->lock);
69383975d7SAlan Stern 	port = HCS_N_PORTS(ehci->hcs_params);
70383975d7SAlan Stern 	while (port--) {
71383975d7SAlan Stern 		if (test_bit(port, &ehci->owned_ports)) {
72383975d7SAlan Stern 			reg = &ehci->regs->port_status[port];
733c519b84SAlan Stern 			status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
74383975d7SAlan Stern 
75383975d7SAlan Stern 			/* Port already owned by companion? */
76383975d7SAlan Stern 			if (status & PORT_OWNER)
77383975d7SAlan Stern 				clear_bit(port, &ehci->owned_ports);
783c519b84SAlan Stern 			else if (test_bit(port, &ehci->companion_ports))
793c519b84SAlan Stern 				ehci_writel(ehci, status & ~PORT_PE, reg);
80c4f34764SAlan Stern 			else {
81c4f34764SAlan Stern 				spin_unlock_irq(&ehci->lock);
82383975d7SAlan Stern 				ehci_hub_control(hcd, SetPortFeature,
83383975d7SAlan Stern 						USB_PORT_FEAT_RESET, port + 1,
84383975d7SAlan Stern 						NULL, 0);
85c4f34764SAlan Stern 				spin_lock_irq(&ehci->lock);
86383975d7SAlan Stern 			}
87383975d7SAlan Stern 		}
88c4f34764SAlan Stern 	}
89c4f34764SAlan Stern 	spin_unlock_irq(&ehci->lock);
90383975d7SAlan Stern 
91383975d7SAlan Stern 	if (!ehci->owned_ports)
92383975d7SAlan Stern 		return;
93383975d7SAlan Stern 	msleep(90);		/* Wait for resets to complete */
94383975d7SAlan Stern 
95c4f34764SAlan Stern 	spin_lock_irq(&ehci->lock);
96383975d7SAlan Stern 	port = HCS_N_PORTS(ehci->hcs_params);
97383975d7SAlan Stern 	while (port--) {
98383975d7SAlan Stern 		if (test_bit(port, &ehci->owned_ports)) {
99c4f34764SAlan Stern 			spin_unlock_irq(&ehci->lock);
100383975d7SAlan Stern 			ehci_hub_control(hcd, GetPortStatus,
101383975d7SAlan Stern 					0, port + 1,
102383975d7SAlan Stern 					(char *) &buf, sizeof(buf));
103c4f34764SAlan Stern 			spin_lock_irq(&ehci->lock);
104383975d7SAlan Stern 
105383975d7SAlan Stern 			/* The companion should now own the port,
106383975d7SAlan Stern 			 * but if something went wrong the port must not
107383975d7SAlan Stern 			 * remain enabled.
108383975d7SAlan Stern 			 */
109383975d7SAlan Stern 			reg = &ehci->regs->port_status[port];
110383975d7SAlan Stern 			status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
111383975d7SAlan Stern 			if (status & PORT_OWNER)
112383975d7SAlan Stern 				ehci_writel(ehci, status | PORT_CSC, reg);
113383975d7SAlan Stern 			else {
114383975d7SAlan Stern 				ehci_dbg(ehci, "failed handover port %d: %x\n",
115383975d7SAlan Stern 						port + 1, status);
116383975d7SAlan Stern 				ehci_writel(ehci, status & ~PORT_PE, reg);
117383975d7SAlan Stern 			}
118383975d7SAlan Stern 		}
119383975d7SAlan Stern 	}
120383975d7SAlan Stern 
121383975d7SAlan Stern 	ehci->owned_ports = 0;
122c4f34764SAlan Stern 	spin_unlock_irq(&ehci->lock);
123383975d7SAlan Stern }
124383975d7SAlan Stern 
ehci_port_change(struct ehci_hcd * ehci)125c5cf9212SAlan Stern static int ehci_port_change(struct ehci_hcd *ehci)
126294d95f2SMatthew Garrett {
127294d95f2SMatthew Garrett 	int i = HCS_N_PORTS(ehci->hcs_params);
128294d95f2SMatthew Garrett 
129294d95f2SMatthew Garrett 	/* First check if the controller indicates a change event */
130294d95f2SMatthew Garrett 
131294d95f2SMatthew Garrett 	if (ehci_readl(ehci, &ehci->regs->status) & STS_PCD)
132294d95f2SMatthew Garrett 		return 1;
133294d95f2SMatthew Garrett 
134294d95f2SMatthew Garrett 	/*
135294d95f2SMatthew Garrett 	 * Not all controllers appear to update this while going from D3 to D0,
136294d95f2SMatthew Garrett 	 * so check the individual port status registers as well
137294d95f2SMatthew Garrett 	 */
138294d95f2SMatthew Garrett 
139294d95f2SMatthew Garrett 	while (i--)
140294d95f2SMatthew Garrett 		if (ehci_readl(ehci, &ehci->regs->port_status[i]) & PORT_CSC)
141294d95f2SMatthew Garrett 			return 1;
142294d95f2SMatthew Garrett 
143294d95f2SMatthew Garrett 	return 0;
144294d95f2SMatthew Garrett }
145294d95f2SMatthew Garrett 
ehci_adjust_port_wakeup_flags(struct ehci_hcd * ehci,bool suspending,bool do_wakeup)14674db22cbSRamneek Mehresh void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
1474147200dSAlan Stern 		bool suspending, bool do_wakeup)
14816032c4fSAlan Stern {
14916032c4fSAlan Stern 	int		port;
15016032c4fSAlan Stern 	u32		temp;
15116032c4fSAlan Stern 
15216032c4fSAlan Stern 	/* If remote wakeup is enabled for the root hub but disabled
15316032c4fSAlan Stern 	 * for the controller, we must adjust all the port wakeup flags
15416032c4fSAlan Stern 	 * when the controller is suspended or resumed.  In all other
15516032c4fSAlan Stern 	 * cases they don't need to be changed.
15616032c4fSAlan Stern 	 */
1574147200dSAlan Stern 	if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup)
15816032c4fSAlan Stern 		return;
15916032c4fSAlan Stern 
160c4f34764SAlan Stern 	spin_lock_irq(&ehci->lock);
161148fc55fSYin Kangkai 
16216032c4fSAlan Stern 	/* clear phy low-power mode before changing wakeup flags */
1632cdcec4fSTuomas Tynkkynen 	if (ehci->has_tdi_phy_lpm) {
16416032c4fSAlan Stern 		port = HCS_N_PORTS(ehci->hcs_params);
16516032c4fSAlan Stern 		while (port--) {
166a46af4ebSAlan Stern 			u32 __iomem	*hostpc_reg = &ehci->regs->hostpc[port];
16716032c4fSAlan Stern 
16816032c4fSAlan Stern 			temp = ehci_readl(ehci, hostpc_reg);
16916032c4fSAlan Stern 			ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg);
17016032c4fSAlan Stern 		}
171c4f34764SAlan Stern 		spin_unlock_irq(&ehci->lock);
17216032c4fSAlan Stern 		msleep(5);
173c4f34764SAlan Stern 		spin_lock_irq(&ehci->lock);
17416032c4fSAlan Stern 	}
17516032c4fSAlan Stern 
17616032c4fSAlan Stern 	port = HCS_N_PORTS(ehci->hcs_params);
17716032c4fSAlan Stern 	while (port--) {
17816032c4fSAlan Stern 		u32 __iomem	*reg = &ehci->regs->port_status[port];
17916032c4fSAlan Stern 		u32		t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
18016032c4fSAlan Stern 		u32		t2 = t1 & ~PORT_WAKE_BITS;
18116032c4fSAlan Stern 
18216032c4fSAlan Stern 		/* If we are suspending the controller, clear the flags.
18316032c4fSAlan Stern 		 * If we are resuming the controller, set the wakeup flags.
18416032c4fSAlan Stern 		 */
18516032c4fSAlan Stern 		if (!suspending) {
18616032c4fSAlan Stern 			if (t1 & PORT_CONNECT)
18716032c4fSAlan Stern 				t2 |= PORT_WKOC_E | PORT_WKDISC_E;
18816032c4fSAlan Stern 			else
18916032c4fSAlan Stern 				t2 |= PORT_WKOC_E | PORT_WKCONN_E;
19016032c4fSAlan Stern 		}
19116032c4fSAlan Stern 		ehci_writel(ehci, t2, reg);
19216032c4fSAlan Stern 	}
19316032c4fSAlan Stern 
19416032c4fSAlan Stern 	/* enter phy low-power mode again */
1952cdcec4fSTuomas Tynkkynen 	if (ehci->has_tdi_phy_lpm) {
19616032c4fSAlan Stern 		port = HCS_N_PORTS(ehci->hcs_params);
19716032c4fSAlan Stern 		while (port--) {
198a46af4ebSAlan Stern 			u32 __iomem	*hostpc_reg = &ehci->regs->hostpc[port];
19916032c4fSAlan Stern 
20016032c4fSAlan Stern 			temp = ehci_readl(ehci, hostpc_reg);
20116032c4fSAlan Stern 			ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg);
20216032c4fSAlan Stern 		}
20316032c4fSAlan Stern 	}
204ee0b9be8SAlan Stern 
205ee0b9be8SAlan Stern 	/* Does the root hub have a port wakeup pending? */
206294d95f2SMatthew Garrett 	if (!suspending && ehci_port_change(ehci))
207ee0b9be8SAlan Stern 		usb_hcd_resume_root_hub(ehci_to_hcd(ehci));
208148fc55fSYin Kangkai 
209c4f34764SAlan Stern 	spin_unlock_irq(&ehci->lock);
21016032c4fSAlan Stern }
21174db22cbSRamneek Mehresh EXPORT_SYMBOL_GPL(ehci_adjust_port_wakeup_flags);
21216032c4fSAlan Stern 
ehci_bus_suspend(struct usb_hcd * hcd)2130c0382e3SAlan Stern static int ehci_bus_suspend (struct usb_hcd *hcd)
2141da177e4SLinus Torvalds {
2151da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
2161da177e4SLinus Torvalds 	int			port;
2178c03356aSAlan Stern 	int			mask;
21816032c4fSAlan Stern 	int			changed;
2193e8d6d85SAlan Stern 	bool			fs_idle_delay;
2201da177e4SLinus Torvalds 
2218c774fe8SAlan Stern 	ehci_dbg(ehci, "suspend root hub\n");
2228c774fe8SAlan Stern 
2231da177e4SLinus Torvalds 	if (time_before (jiffies, ehci->next_statechange))
2241da177e4SLinus Torvalds 		msleep(5);
2251da177e4SLinus Torvalds 
226c4f34764SAlan Stern 	/* stop the schedules */
227c4f34764SAlan Stern 	ehci_quiesce(ehci);
228c4f34764SAlan Stern 
2291da177e4SLinus Torvalds 	spin_lock_irq (&ehci->lock);
23043fe3a99SAlan Stern 	if (ehci->rh_state < EHCI_RH_RUNNING)
23143fe3a99SAlan Stern 		goto done;
2321da177e4SLinus Torvalds 
233cec3a53cSAlan Stern 	/* Once the controller is stopped, port resumes that are already
234cec3a53cSAlan Stern 	 * in progress won't complete.  Hence if remote wakeup is enabled
235cec3a53cSAlan Stern 	 * for the root hub and any ports are in the middle of a resume or
236cec3a53cSAlan Stern 	 * remote wakeup, we must fail the suspend.
237cec3a53cSAlan Stern 	 */
238cec3a53cSAlan Stern 	if (hcd->self.root_hub->do_remote_wakeup) {
239a448e4dcSAlan Stern 		if (ehci->resuming_ports) {
240cec3a53cSAlan Stern 			spin_unlock_irq(&ehci->lock);
241a448e4dcSAlan Stern 			ehci_dbg(ehci, "suspend failed because a port is resuming\n");
242cec3a53cSAlan Stern 			return -EBUSY;
243cec3a53cSAlan Stern 		}
244cec3a53cSAlan Stern 	}
245cec3a53cSAlan Stern 
2468c03356aSAlan Stern 	/* Unlike other USB host controller types, EHCI doesn't have
2478c03356aSAlan Stern 	 * any notion of "global" or bus-wide suspend.  The driver has
2488c03356aSAlan Stern 	 * to manually suspend all the active unsuspended ports, and
2498c03356aSAlan Stern 	 * then manually resume them in the bus_resume() routine.
2508c03356aSAlan Stern 	 */
2518c03356aSAlan Stern 	ehci->bus_suspended = 0;
252383975d7SAlan Stern 	ehci->owned_ports = 0;
25316032c4fSAlan Stern 	changed = 0;
2543e8d6d85SAlan Stern 	fs_idle_delay = false;
255cec3a53cSAlan Stern 	port = HCS_N_PORTS(ehci->hcs_params);
2561da177e4SLinus Torvalds 	while (port--) {
2571da177e4SLinus Torvalds 		u32 __iomem	*reg = &ehci->regs->port_status [port];
258083522d7SBenjamin Herrenschmidt 		u32		t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
25916032c4fSAlan Stern 		u32		t2 = t1 & ~PORT_WAKE_BITS;
2601da177e4SLinus Torvalds 
2618c03356aSAlan Stern 		/* keep track of which ports we suspend */
262383975d7SAlan Stern 		if (t1 & PORT_OWNER)
263383975d7SAlan Stern 			set_bit(port, &ehci->owned_ports);
264383975d7SAlan Stern 		else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
2651da177e4SLinus Torvalds 			t2 |= PORT_SUSPEND;
2668c03356aSAlan Stern 			set_bit(port, &ehci->bus_suspended);
2678c03356aSAlan Stern 		}
2688c03356aSAlan Stern 
26916032c4fSAlan Stern 		/* enable remote wakeup on all ports, if told to do so */
270331ac6b2SAlek Du 		if (hcd->self.root_hub->do_remote_wakeup) {
271331ac6b2SAlek Du 			/* only enable appropriate wake bits, otherwise the
272331ac6b2SAlek Du 			 * hardware can not go phy low power mode. If a race
273331ac6b2SAlek Du 			 * condition happens here(connection change during bits
274331ac6b2SAlek Du 			 * set), the port change detection will finally fix it.
275331ac6b2SAlek Du 			 */
27616032c4fSAlan Stern 			if (t1 & PORT_CONNECT)
277331ac6b2SAlek Du 				t2 |= PORT_WKOC_E | PORT_WKDISC_E;
27816032c4fSAlan Stern 			else
279331ac6b2SAlek Du 				t2 |= PORT_WKOC_E | PORT_WKCONN_E;
280331ac6b2SAlek Du 		}
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 		if (t1 != t2) {
2833e8d6d85SAlan Stern 			/*
2843e8d6d85SAlan Stern 			 * On some controllers, Wake-On-Disconnect will
2853e8d6d85SAlan Stern 			 * generate false wakeup signals until the bus
2863e8d6d85SAlan Stern 			 * switches over to full-speed idle.  For their
2873e8d6d85SAlan Stern 			 * sake, add a delay if we need one.
2883e8d6d85SAlan Stern 			 */
2893e8d6d85SAlan Stern 			if ((t2 & PORT_WKDISC_E) &&
2903e8d6d85SAlan Stern 					ehci_port_speed(ehci, t2) ==
2913e8d6d85SAlan Stern 						USB_PORT_STAT_HIGH_SPEED)
2923e8d6d85SAlan Stern 				fs_idle_delay = true;
293083522d7SBenjamin Herrenschmidt 			ehci_writel(ehci, t2, reg);
29416032c4fSAlan Stern 			changed = 1;
29516032c4fSAlan Stern 		}
29616032c4fSAlan Stern 	}
2973e8d6d85SAlan Stern 	spin_unlock_irq(&ehci->lock);
2983e8d6d85SAlan Stern 
2999d4b8270SChangming Huang 	if (changed && ehci_has_fsl_susp_errata(ehci))
3009d4b8270SChangming Huang 		/*
3019d4b8270SChangming Huang 		 * Wait for at least 10 millisecondes to ensure the controller
3029d4b8270SChangming Huang 		 * enter the suspend status before initiating a port resume
3039d4b8270SChangming Huang 		 * using the Force Port Resume bit (Not-EHCI compatible).
3049d4b8270SChangming Huang 		 */
3059d4b8270SChangming Huang 		usleep_range(10000, 20000);
3069d4b8270SChangming Huang 
3073e8d6d85SAlan Stern 	if ((changed && ehci->has_tdi_phy_lpm) || fs_idle_delay) {
3083e8d6d85SAlan Stern 		/*
3093e8d6d85SAlan Stern 		 * Wait for HCD to enter low-power mode or for the bus
3103e8d6d85SAlan Stern 		 * to switch to full-speed idle.
3113e8d6d85SAlan Stern 		 */
3123e8d6d85SAlan Stern 		usleep_range(5000, 5500);
3133e8d6d85SAlan Stern 	}
31416032c4fSAlan Stern 
3152cdcec4fSTuomas Tynkkynen 	if (changed && ehci->has_tdi_phy_lpm) {
31616032c4fSAlan Stern 		spin_lock_irq(&ehci->lock);
31716032c4fSAlan Stern 		port = HCS_N_PORTS(ehci->hcs_params);
31816032c4fSAlan Stern 		while (port--) {
319a46af4ebSAlan Stern 			u32 __iomem	*hostpc_reg = &ehci->regs->hostpc[port];
320331ac6b2SAlek Du 			u32		t3;
321331ac6b2SAlek Du 
322331ac6b2SAlek Du 			t3 = ehci_readl(ehci, hostpc_reg);
323331ac6b2SAlek Du 			ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
324331ac6b2SAlek Du 			t3 = ehci_readl(ehci, hostpc_reg);
32516032c4fSAlan Stern 			ehci_dbg(ehci, "Port %d phy low-power mode %s\n",
326331ac6b2SAlek Du 					port, (t3 & HOSTPC_PHCD) ?
327331ac6b2SAlek Du 					"succeeded" : "failed");
328331ac6b2SAlek Du 		}
329c4f34764SAlan Stern 		spin_unlock_irq(&ehci->lock);
3303e8d6d85SAlan Stern 	}
3311da177e4SLinus Torvalds 
332cd930c93SAlan Stern 	/* Apparently some devices need a >= 1-uframe delay here */
333cd930c93SAlan Stern 	if (ehci->bus_suspended)
334cd930c93SAlan Stern 		udelay(150);
335cd930c93SAlan Stern 
3361da177e4SLinus Torvalds 	/* turn off now-idle HC */
3371da177e4SLinus Torvalds 	ehci_halt (ehci);
338c4f34764SAlan Stern 
339c4f34764SAlan Stern 	spin_lock_irq(&ehci->lock);
34043fe3a99SAlan Stern 	if (ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_POLL_DEAD))
34143fe3a99SAlan Stern 		ehci_handle_controller_death(ehci);
34243fe3a99SAlan Stern 	if (ehci->rh_state != EHCI_RH_RUNNING)
34343fe3a99SAlan Stern 		goto done;
344e8799906SAlan Stern 	ehci->rh_state = EHCI_RH_SUSPENDED;
3451da177e4SLinus Torvalds 
3468df0d77dSAlan Stern 	unlink_empty_async_suspended(ehci);
3478df0d77dSAlan Stern 
348643a4df7SLongfang Liu 	/* Some Synopsys controllers mistakenly leave IAA turned on */
349643a4df7SLongfang Liu 	ehci_writel(ehci, STS_IAA, &ehci->regs->status);
350643a4df7SLongfang Liu 
351f96fba0dSAlan Stern 	/* Any IAA cycle that started before the suspend is now invalid */
352f96fba0dSAlan Stern 	end_iaa_cycle(ehci);
3539118f9ebSMing Lei 	ehci_handle_start_intr_unlinks(ehci);
354df202255SAlan Stern 	ehci_handle_intr_unlinks(ehci);
35555934eb3SAlan Stern 	end_free_itds(ehci);
356cdc647a9SDavid Brownell 
3578c03356aSAlan Stern 	/* allow remote wakeup */
3588c03356aSAlan Stern 	mask = INTR_MASK;
35958a97ffeSAlan Stern 	if (!hcd->self.root_hub->do_remote_wakeup)
3608c03356aSAlan Stern 		mask &= ~STS_PCD;
361083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, mask, &ehci->regs->intr_enable);
362083522d7SBenjamin Herrenschmidt 	ehci_readl(ehci, &ehci->regs->intr_enable);
3638c03356aSAlan Stern 
36443fe3a99SAlan Stern  done:
3651da177e4SLinus Torvalds 	ehci->next_statechange = jiffies + msecs_to_jiffies(10);
366d58b4bccSAlan Stern 	ehci->enabled_hrtimer_events = 0;
367d58b4bccSAlan Stern 	ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
3681da177e4SLinus Torvalds 	spin_unlock_irq (&ehci->lock);
369015798b2SJon Hunter 
370d58b4bccSAlan Stern 	hrtimer_cancel(&ehci->hrtimer);
3711da177e4SLinus Torvalds 	return 0;
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds /* caller has locked the root hub, and should reset/reinit on error */
ehci_bus_resume(struct usb_hcd * hcd)3760c0382e3SAlan Stern static int ehci_bus_resume (struct usb_hcd *hcd)
3771da177e4SLinus Torvalds {
3781da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
3791da177e4SLinus Torvalds 	u32			temp;
380383975d7SAlan Stern 	u32			power_okay;
3811da177e4SLinus Torvalds 	int			i;
382d0f2fb25SWang Zhi 	unsigned long		resume_needed = 0;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	if (time_before (jiffies, ehci->next_statechange))
3851da177e4SLinus Torvalds 		msleep(5);
3861da177e4SLinus Torvalds 	spin_lock_irq (&ehci->lock);
38743fe3a99SAlan Stern 	if (!HCD_HW_ACCESSIBLE(hcd) || ehci->shutdown)
38843fe3a99SAlan Stern 		goto shutdown;
3891da177e4SLinus Torvalds 
390ad45f1dcSJason Wessel 	if (unlikely(ehci->debug)) {
3919fa5780bSJan Beulich 		if (!dbgp_reset_prep(hcd))
392ad45f1dcSJason Wessel 			ehci->debug = NULL;
393ad45f1dcSJason Wessel 		else
3949fa5780bSJan Beulich 			dbgp_external_startup(hcd);
395ad45f1dcSJason Wessel 	}
396ad45f1dcSJason Wessel 
397f03c17fcSDavid Brownell 	/* Ideally and we've got a real resume here, and no port's power
398f03c17fcSDavid Brownell 	 * was lost.  (For PCI, that means Vaux was maintained.)  But we
399f03c17fcSDavid Brownell 	 * could instead be restoring a swsusp snapshot -- so that BIOS was
400f03c17fcSDavid Brownell 	 * the last user of the controller, not reset/pm hardware keeping
401f03c17fcSDavid Brownell 	 * state we gave to it.
402f03c17fcSDavid Brownell 	 */
403383975d7SAlan Stern 	power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
404383975d7SAlan Stern 	ehci_dbg(ehci, "resume root hub%s\n",
405383975d7SAlan Stern 			power_okay ? "" : " after power loss");
406f03c17fcSDavid Brownell 
4071da177e4SLinus Torvalds 	/* at least some APM implementations will try to deliver
4081da177e4SLinus Torvalds 	 * IRQs right away, so delay them until we're ready.
4091da177e4SLinus Torvalds 	 */
410083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, 0, &ehci->regs->intr_enable);
4118c03356aSAlan Stern 
4128c03356aSAlan Stern 	/* re-init operational registers */
413083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, 0, &ehci->regs->segment);
414083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
415083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	/* restore CMD_RUN, framelist size, and irq threshold */
4183d9545ccSAlan Stern 	ehci->command |= CMD_RUN;
419083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, ehci->command, &ehci->regs->command);
420e8799906SAlan Stern 	ehci->rh_state = EHCI_RH_RUNNING;
4211da177e4SLinus Torvalds 
4226273f181SPeter Chen 	/*
4236273f181SPeter Chen 	 * According to Bugzilla #8190, the port status for some controllers
4246273f181SPeter Chen 	 * will be wrong without a delay. At their wrong status, the port
4256273f181SPeter Chen 	 * is enabled, but not suspended neither resumed.
4266273f181SPeter Chen 	 */
4276273f181SPeter Chen 	i = HCS_N_PORTS(ehci->hcs_params);
4286273f181SPeter Chen 	while (i--) {
4296273f181SPeter Chen 		temp = ehci_readl(ehci, &ehci->regs->port_status[i]);
4306273f181SPeter Chen 		if ((temp & PORT_PE) &&
4316273f181SPeter Chen 				!(temp & (PORT_SUSPEND | PORT_RESUME))) {
4326273f181SPeter Chen 			ehci_dbg(ehci, "Port status(0x%x) is wrong\n", temp);
4333a4e72cbSVikram Pandita 			spin_unlock_irq(&ehci->lock);
4343a4e72cbSVikram Pandita 			msleep(8);
4353a4e72cbSVikram Pandita 			spin_lock_irq(&ehci->lock);
4366273f181SPeter Chen 			break;
4376273f181SPeter Chen 		}
4386273f181SPeter Chen 	}
4396273f181SPeter Chen 
44043fe3a99SAlan Stern 	if (ehci->shutdown)
44143fe3a99SAlan Stern 		goto shutdown;
442e198a314SAlan Stern 
44316032c4fSAlan Stern 	/* clear phy low-power mode before resume */
4442cdcec4fSTuomas Tynkkynen 	if (ehci->bus_suspended && ehci->has_tdi_phy_lpm) {
4451da177e4SLinus Torvalds 		i = HCS_N_PORTS(ehci->hcs_params);
4461da177e4SLinus Torvalds 		while (i--) {
44716032c4fSAlan Stern 			if (test_bit(i, &ehci->bus_suspended)) {
448a46af4ebSAlan Stern 				u32 __iomem	*hostpc_reg =
449a46af4ebSAlan Stern 							&ehci->regs->hostpc[i];
45016032c4fSAlan Stern 
451eab80de0SAlek Du 				temp = ehci_readl(ehci, hostpc_reg);
452eab80de0SAlek Du 				ehci_writel(ehci, temp & ~HOSTPC_PHCD,
453eab80de0SAlek Du 						hostpc_reg);
454eab80de0SAlek Du 			}
45516032c4fSAlan Stern 		}
45616032c4fSAlan Stern 		spin_unlock_irq(&ehci->lock);
45716032c4fSAlan Stern 		msleep(5);
45816032c4fSAlan Stern 		spin_lock_irq(&ehci->lock);
45943fe3a99SAlan Stern 		if (ehci->shutdown)
46043fe3a99SAlan Stern 			goto shutdown;
46116032c4fSAlan Stern 	}
46216032c4fSAlan Stern 
46316032c4fSAlan Stern 	/* manually resume the ports we suspended during bus_suspend() */
46416032c4fSAlan Stern 	i = HCS_N_PORTS (ehci->hcs_params);
46516032c4fSAlan Stern 	while (i--) {
466083522d7SBenjamin Herrenschmidt 		temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
46758a97ffeSAlan Stern 		temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
4688c03356aSAlan Stern 		if (test_bit(i, &ehci->bus_suspended) &&
4693a4e72cbSVikram Pandita 				(temp & PORT_SUSPEND)) {
4701da177e4SLinus Torvalds 			temp |= PORT_RESUME;
471d0f2fb25SWang Zhi 			set_bit(i, &resume_needed);
4723a4e72cbSVikram Pandita 		}
473083522d7SBenjamin Herrenschmidt 		ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
4741da177e4SLinus Torvalds 	}
4753a4e72cbSVikram Pandita 
476ea16328fSFelipe Balbi 	/*
477ea16328fSFelipe Balbi 	 * msleep for USB_RESUME_TIMEOUT ms only if code is trying to resume
478ea16328fSFelipe Balbi 	 * port
479ea16328fSFelipe Balbi 	 */
4803a4e72cbSVikram Pandita 	if (resume_needed) {
4813a4e72cbSVikram Pandita 		spin_unlock_irq(&ehci->lock);
482ea16328fSFelipe Balbi 		msleep(USB_RESUME_TIMEOUT);
4833a4e72cbSVikram Pandita 		spin_lock_irq(&ehci->lock);
48443fe3a99SAlan Stern 		if (ehci->shutdown)
48543fe3a99SAlan Stern 			goto shutdown;
4863a4e72cbSVikram Pandita 	}
4873a4e72cbSVikram Pandita 
4881da177e4SLinus Torvalds 	i = HCS_N_PORTS (ehci->hcs_params);
4891da177e4SLinus Torvalds 	while (i--) {
490083522d7SBenjamin Herrenschmidt 		temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
491d0f2fb25SWang Zhi 		if (test_bit(i, &resume_needed)) {
49224b90814SAlan Stern 			temp &= ~(PORT_RWC_BITS | PORT_SUSPEND | PORT_RESUME);
493083522d7SBenjamin Herrenschmidt 			ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
4941da177e4SLinus Torvalds 		}
4958c03356aSAlan Stern 	}
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	ehci->next_statechange = jiffies + msecs_to_jiffies(5);
498c4f34764SAlan Stern 	spin_unlock_irq(&ehci->lock);
499c4f34764SAlan Stern 
500c4f34764SAlan Stern 	ehci_handover_companion_ports(ehci);
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	/* Now we can safely re-enable irqs */
50343fe3a99SAlan Stern 	spin_lock_irq(&ehci->lock);
50443fe3a99SAlan Stern 	if (ehci->shutdown)
50543fe3a99SAlan Stern 		goto shutdown;
506083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
50715be105bSAlan Stern 	(void) ehci_readl(ehci, &ehci->regs->intr_enable);
50843fe3a99SAlan Stern 	spin_unlock_irq(&ehci->lock);
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	return 0;
51143fe3a99SAlan Stern 
51243fe3a99SAlan Stern  shutdown:
51343fe3a99SAlan Stern 	spin_unlock_irq(&ehci->lock);
51443fe3a99SAlan Stern 	return -ESHUTDOWN;
5151da177e4SLinus Torvalds }
5161da177e4SLinus Torvalds 
ehci_get_resuming_ports(struct usb_hcd * hcd)51700d423c8SAlan Stern static unsigned long ehci_get_resuming_ports(struct usb_hcd *hcd)
51800d423c8SAlan Stern {
51900d423c8SAlan Stern 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
52000d423c8SAlan Stern 
52100d423c8SAlan Stern 	return ehci->resuming_ports;
52200d423c8SAlan Stern }
52300d423c8SAlan Stern 
5241da177e4SLinus Torvalds #else
5251da177e4SLinus Torvalds 
5260c0382e3SAlan Stern #define ehci_bus_suspend	NULL
5270c0382e3SAlan Stern #define ehci_bus_resume		NULL
52800d423c8SAlan Stern #define ehci_get_resuming_ports	NULL
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds #endif	/* CONFIG_PM */
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
5331da177e4SLinus Torvalds 
53457e06c11SAlan Stern /*
53590da096eSBalaji Rao  * Sets the owner of a port
53657e06c11SAlan Stern  */
set_owner(struct ehci_hcd * ehci,int portnum,int new_owner)53790da096eSBalaji Rao static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
53857e06c11SAlan Stern {
53957e06c11SAlan Stern 	u32 __iomem		*status_reg;
54057e06c11SAlan Stern 	u32			port_status;
54190da096eSBalaji Rao 	int 			try;
54257e06c11SAlan Stern 
54390da096eSBalaji Rao 	status_reg = &ehci->regs->port_status[portnum];
54457e06c11SAlan Stern 
54557e06c11SAlan Stern 	/*
54657e06c11SAlan Stern 	 * The controller won't set the OWNER bit if the port is
54757e06c11SAlan Stern 	 * enabled, so this loop will sometimes require at least two
54857e06c11SAlan Stern 	 * iterations: one to disable the port and one to set OWNER.
54957e06c11SAlan Stern 	 */
55057e06c11SAlan Stern 	for (try = 4; try > 0; --try) {
55157e06c11SAlan Stern 		spin_lock_irq(&ehci->lock);
55257e06c11SAlan Stern 		port_status = ehci_readl(ehci, status_reg);
55357e06c11SAlan Stern 		if ((port_status & PORT_OWNER) == new_owner
55457e06c11SAlan Stern 				|| (port_status & (PORT_OWNER | PORT_CONNECT))
55557e06c11SAlan Stern 					== 0)
55657e06c11SAlan Stern 			try = 0;
55757e06c11SAlan Stern 		else {
55857e06c11SAlan Stern 			port_status ^= PORT_OWNER;
55957e06c11SAlan Stern 			port_status &= ~(PORT_PE | PORT_RWC_BITS);
56057e06c11SAlan Stern 			ehci_writel(ehci, port_status, status_reg);
56157e06c11SAlan Stern 		}
56257e06c11SAlan Stern 		spin_unlock_irq(&ehci->lock);
56357e06c11SAlan Stern 		if (try > 1)
56457e06c11SAlan Stern 			msleep(5);
56557e06c11SAlan Stern 	}
56690da096eSBalaji Rao }
56790da096eSBalaji Rao 
56857e06c11SAlan Stern /*-------------------------------------------------------------------------*/
56957e06c11SAlan Stern 
check_reset_complete(struct ehci_hcd * ehci,int index,u32 __iomem * status_reg,int port_status)5701da177e4SLinus Torvalds static int check_reset_complete (
5711da177e4SLinus Torvalds 	struct ehci_hcd	*ehci,
5721da177e4SLinus Torvalds 	int		index,
573e6316565SAlan Stern 	u32 __iomem	*status_reg,
5741da177e4SLinus Torvalds 	int		port_status
5751da177e4SLinus Torvalds ) {
576cd4cdc93SDavid Brownell 	if (!(port_status & PORT_CONNECT))
5771da177e4SLinus Torvalds 		return port_status;
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	/* if reset finished and it's still not enabled -- handoff */
5801da177e4SLinus Torvalds 	if (!(port_status & PORT_PE)) {
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 		/* with integrated TT, there's nobody to hand it to! */
5831da177e4SLinus Torvalds 		if (ehci_is_TDI(ehci)) {
5841da177e4SLinus Torvalds 			ehci_dbg (ehci,
5851da177e4SLinus Torvalds 				"Failed to enable port %d on root hub TT\n",
5861da177e4SLinus Torvalds 				index+1);
5871da177e4SLinus Torvalds 			return port_status;
5881da177e4SLinus Torvalds 		}
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 		ehci_dbg (ehci, "port %d full speed --> companion\n",
5911da177e4SLinus Torvalds 			index + 1);
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 		// what happens if HCS_N_CC(params) == 0 ?
5941da177e4SLinus Torvalds 		port_status |= PORT_OWNER;
59510f6524aSDavid Brownell 		port_status &= ~PORT_RWC_BITS;
596e6316565SAlan Stern 		ehci_writel(ehci, port_status, status_reg);
5971da177e4SLinus Torvalds 
598796bcae7SVitaly Bordug 		/* ensure 440EPX ohci controller state is operational */
599796bcae7SVitaly Bordug 		if (ehci->has_amcc_usb23)
600796bcae7SVitaly Bordug 			set_ohci_hcfs(ehci, 1);
601796bcae7SVitaly Bordug 	} else {
6020ca7eb23SPeter Chen 		ehci_dbg(ehci, "port %d reset complete, port enabled\n",
6030ca7eb23SPeter Chen 			index + 1);
604796bcae7SVitaly Bordug 		/* ensure 440EPx ohci controller state is suspended */
605796bcae7SVitaly Bordug 		if (ehci->has_amcc_usb23)
606796bcae7SVitaly Bordug 			set_ohci_hcfs(ehci, 0);
607796bcae7SVitaly Bordug 	}
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 	return port_status;
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds /* build "status change" packet (one or two bytes) from HC registers */
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds static int
ehci_hub_status_data(struct usb_hcd * hcd,char * buf)6181da177e4SLinus Torvalds ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
6191da177e4SLinus Torvalds {
6201da177e4SLinus Torvalds 	struct ehci_hcd	*ehci = hcd_to_ehci (hcd);
621a448e4dcSAlan Stern 	u32		temp, status;
62293f1a47cSDavid Brownell 	u32		mask;
6231da177e4SLinus Torvalds 	int		ports, i, retval = 1;
6241da177e4SLinus Torvalds 	unsigned long	flags;
6254dd405a4SAlan Stern 	u32		ppcd = ~0;
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	/* init status to no-changes */
6281da177e4SLinus Torvalds 	buf [0] = 0;
6291da177e4SLinus Torvalds 	ports = HCS_N_PORTS (ehci->hcs_params);
6301da177e4SLinus Torvalds 	if (ports > 7) {
6311da177e4SLinus Torvalds 		buf [1] = 0;
6321da177e4SLinus Torvalds 		retval++;
6331da177e4SLinus Torvalds 	}
6341da177e4SLinus Torvalds 
635a448e4dcSAlan Stern 	/* Inform the core about resumes-in-progress by returning
636a448e4dcSAlan Stern 	 * a non-zero value even if there are no status changes.
637a448e4dcSAlan Stern 	 */
638a448e4dcSAlan Stern 	status = ehci->resuming_ports;
639a448e4dcSAlan Stern 
64093f1a47cSDavid Brownell 	/* Some boards (mostly VIA?) report bogus overcurrent indications,
64193f1a47cSDavid Brownell 	 * causing massive log spam unless we completely ignore them.  It
6423a4fa0a2SRobert P. J. Day 	 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
64393f1a47cSDavid Brownell 	 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
64493f1a47cSDavid Brownell 	 * PORT_POWER; that's surprising, but maybe within-spec.
64593f1a47cSDavid Brownell 	 */
6462d5ba374SFlorian Fainelli 	if (!ignore_oc && !ehci->spurious_oc)
64793f1a47cSDavid Brownell 		mask = PORT_CSC | PORT_PEC | PORT_OCC;
64893f1a47cSDavid Brownell 	else
64993f1a47cSDavid Brownell 		mask = PORT_CSC | PORT_PEC;
65093f1a47cSDavid Brownell 	// PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
65193f1a47cSDavid Brownell 
6521da177e4SLinus Torvalds 	/* no hub change reports (bit 0) for now (power, ...) */
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	/* port N changes (bit N)? */
6551da177e4SLinus Torvalds 	spin_lock_irqsave (&ehci->lock, flags);
6565a9cdf33SAlek Du 
6575a9cdf33SAlek Du 	/* get per-port change detect bits */
6585a9cdf33SAlek Du 	if (ehci->has_ppcd)
6595a9cdf33SAlek Du 		ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
6605a9cdf33SAlek Du 
6611da177e4SLinus Torvalds 	for (i = 0; i < ports; i++) {
6625a9cdf33SAlek Du 		/* leverage per-port change bits feature */
6634dd405a4SAlan Stern 		if (ppcd & (1 << i))
664083522d7SBenjamin Herrenschmidt 			temp = ehci_readl(ehci, &ehci->regs->port_status[i]);
6654dd405a4SAlan Stern 		else
6664dd405a4SAlan Stern 			temp = 0;
667625b5c9aSAlan Stern 
668625b5c9aSAlan Stern 		/*
669625b5c9aSAlan Stern 		 * Return status information even for ports with OWNER set.
67037ebb549SPetr Mladek 		 * Otherwise hub_wq wouldn't see the disconnect event when a
671625b5c9aSAlan Stern 		 * high-speed device is switched over to the companion
672625b5c9aSAlan Stern 		 * controller by the user.
673625b5c9aSAlan Stern 		 */
674625b5c9aSAlan Stern 
675eafe5b99SAlan Stern 		if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
676eafe5b99SAlan Stern 				|| (ehci->reset_done[i] && time_after_eq(
677*dda4b60eSXu Yang 					jiffies, ehci->reset_done[i]))
678*dda4b60eSXu Yang 				|| ehci_has_ci_pec_bug(ehci, temp)) {
6791da177e4SLinus Torvalds 			if (i < 7)
6801da177e4SLinus Torvalds 			    buf [0] |= 1 << (i + 1);
6811da177e4SLinus Torvalds 			else
6821da177e4SLinus Torvalds 			    buf [1] |= 1 << (i - 7);
6831da177e4SLinus Torvalds 			status = STS_PCD;
6841da177e4SLinus Torvalds 		}
6851da177e4SLinus Torvalds 	}
686ee74290bSAlan Stern 
687ee74290bSAlan Stern 	/* If a resume is in progress, make sure it can finish */
688ee74290bSAlan Stern 	if (ehci->resuming_ports)
689ee74290bSAlan Stern 		mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(25));
690ee74290bSAlan Stern 
6911da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ehci->lock, flags);
6921da177e4SLinus Torvalds 	return status ? retval : 0;
6931da177e4SLinus Torvalds }
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds static void
ehci_hub_descriptor(struct ehci_hcd * ehci,struct usb_hub_descriptor * desc)6981da177e4SLinus Torvalds ehci_hub_descriptor (
6991da177e4SLinus Torvalds 	struct ehci_hcd			*ehci,
7001da177e4SLinus Torvalds 	struct usb_hub_descriptor	*desc
7011da177e4SLinus Torvalds ) {
7021da177e4SLinus Torvalds 	int		ports = HCS_N_PORTS (ehci->hcs_params);
7031da177e4SLinus Torvalds 	u16		temp;
7041da177e4SLinus Torvalds 
705625a4c59SSergei Shtylyov 	desc->bDescriptorType = USB_DT_HUB;
7061da177e4SLinus Torvalds 	desc->bPwrOn2PwrGood = 10;	/* ehci 1.0, 2.3.9 says 20ms max */
7071da177e4SLinus Torvalds 	desc->bHubContrCurrent = 0;
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	desc->bNbrPorts = ports;
7101da177e4SLinus Torvalds 	temp = 1 + (ports / 8);
7111da177e4SLinus Torvalds 	desc->bDescLength = 7 + 2 * temp;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	/* two bitmaps:  ports removable, and usb 1.0 legacy PortPwrCtrlMask */
714dbe79bbeSJohn Youn 	memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
715dbe79bbeSJohn Youn 	memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
7161da177e4SLinus Torvalds 
7173a2359eeSSergei Shtylyov 	temp = HUB_CHAR_INDV_PORT_OCPM;	/* per-port overcurrent reporting */
7181da177e4SLinus Torvalds 	if (HCS_PPC (ehci->hcs_params))
7193a2359eeSSergei Shtylyov 		temp |= HUB_CHAR_INDV_PORT_LPSM; /* per-port power control */
72056c1e26dSDavid Brownell 	else
7213a2359eeSSergei Shtylyov 		temp |= HUB_CHAR_NO_LPSM; /* no power switching */
7221da177e4SLinus Torvalds #if 0
7231da177e4SLinus Torvalds // re-enable when we support USB_PORT_FEAT_INDICATOR below.
7241da177e4SLinus Torvalds 	if (HCS_INDICATOR (ehci->hcs_params))
7253a2359eeSSergei Shtylyov 		temp |= HUB_CHAR_PORTIND; /* per-port indicators (LEDs) */
7261da177e4SLinus Torvalds #endif
727fd05e720SAl Viro 	desc->wHubCharacteristics = cpu_to_le16(temp);
7281da177e4SLinus Torvalds }
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
7311da177e4SLinus Torvalds 
ehci_hub_control(struct usb_hcd * hcd,u16 typeReq,u16 wValue,u16 wIndex,char * buf,u16 wLength)73237769939SLaurent Pinchart int ehci_hub_control(
7331da177e4SLinus Torvalds 	struct usb_hcd	*hcd,
7341da177e4SLinus Torvalds 	u16		typeReq,
7351da177e4SLinus Torvalds 	u16		wValue,
7361da177e4SLinus Torvalds 	u16		wIndex,
7371da177e4SLinus Torvalds 	char		*buf,
7381da177e4SLinus Torvalds 	u16		wLength
7391da177e4SLinus Torvalds ) {
7401da177e4SLinus Torvalds 	struct ehci_hcd	*ehci = hcd_to_ehci (hcd);
7411da177e4SLinus Torvalds 	int		ports = HCS_N_PORTS (ehci->hcs_params);
74285e3990bSAlan Stern 	u32 __iomem	*status_reg, *hostpc_reg;
743331ac6b2SAlek Du 	u32		temp, temp1, status;
7441da177e4SLinus Torvalds 	unsigned long	flags;
7451da177e4SLinus Torvalds 	int		retval = 0;
746f0d7f273SDavid Brownell 	unsigned	selector;
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	/*
749846cbf98SAlan Stern 	 * Avoid out-of-bounds values while calculating the port index
750846cbf98SAlan Stern 	 * from wIndex.  The compiler doesn't like pointers to invalid
751846cbf98SAlan Stern 	 * addresses, even if they are never used.
75285e3990bSAlan Stern 	 */
753846cbf98SAlan Stern 	temp = (wIndex - 1) & 0xff;
754846cbf98SAlan Stern 	if (temp >= HCS_N_PORTS_MAX)
755846cbf98SAlan Stern 		temp = 0;
75685e3990bSAlan Stern 	status_reg = &ehci->regs->port_status[temp];
75785e3990bSAlan Stern 	hostpc_reg = &ehci->regs->hostpc[temp];
75885e3990bSAlan Stern 
75985e3990bSAlan Stern 	/*
7601da177e4SLinus Torvalds 	 * FIXME:  support SetPortFeatures USB_PORT_FEAT_INDICATOR.
7611da177e4SLinus Torvalds 	 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
7621da177e4SLinus Torvalds 	 * (track current state ourselves) ... blink for diagnostics,
7631da177e4SLinus Torvalds 	 * power, "this is the one", etc.  EHCI spec supports this.
7641da177e4SLinus Torvalds 	 */
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 	spin_lock_irqsave (&ehci->lock, flags);
7671da177e4SLinus Torvalds 	switch (typeReq) {
7681da177e4SLinus Torvalds 	case ClearHubFeature:
7691da177e4SLinus Torvalds 		switch (wValue) {
7701da177e4SLinus Torvalds 		case C_HUB_LOCAL_POWER:
7711da177e4SLinus Torvalds 		case C_HUB_OVER_CURRENT:
7721da177e4SLinus Torvalds 			/* no hub-wide feature/status flags */
7731da177e4SLinus Torvalds 			break;
7741da177e4SLinus Torvalds 		default:
7751da177e4SLinus Torvalds 			goto error;
7761da177e4SLinus Torvalds 		}
7771da177e4SLinus Torvalds 		break;
7781da177e4SLinus Torvalds 	case ClearPortFeature:
7791da177e4SLinus Torvalds 		if (!wIndex || wIndex > ports)
7801da177e4SLinus Torvalds 			goto error;
7811da177e4SLinus Torvalds 		wIndex--;
782e6316565SAlan Stern 		temp = ehci_readl(ehci, status_reg);
7836d5f89c7SStephen Warren 		temp &= ~PORT_RWC_BITS;
784625b5c9aSAlan Stern 
785625b5c9aSAlan Stern 		/*
786625b5c9aSAlan Stern 		 * Even if OWNER is set, so the port is owned by the
78737ebb549SPetr Mladek 		 * companion controller, hub_wq needs to be able to clear
788625b5c9aSAlan Stern 		 * the port-change status bits (especially
789749da5f8SAlan Stern 		 * USB_PORT_STAT_C_CONNECTION).
790625b5c9aSAlan Stern 		 */
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 		switch (wValue) {
7931da177e4SLinus Torvalds 		case USB_PORT_FEAT_ENABLE:
794e6316565SAlan Stern 			ehci_writel(ehci, temp & ~PORT_PE, status_reg);
7951da177e4SLinus Torvalds 			break;
7961da177e4SLinus Torvalds 		case USB_PORT_FEAT_C_ENABLE:
7976d5f89c7SStephen Warren 			ehci_writel(ehci, temp | PORT_PEC, status_reg);
7981da177e4SLinus Torvalds 			break;
7991da177e4SLinus Torvalds 		case USB_PORT_FEAT_SUSPEND:
8001da177e4SLinus Torvalds 			if (temp & PORT_RESET)
8011da177e4SLinus Torvalds 				goto error;
802f8aeb3bbSDavid Brownell 			if (ehci->no_selective_suspend)
803f8aeb3bbSDavid Brownell 				break;
80483722bc9SAnatolij Gustschin #ifdef CONFIG_USB_OTG
80583722bc9SAnatolij Gustschin 			if ((hcd->self.otg_port == (wIndex + 1))
80683722bc9SAnatolij Gustschin 			    && hcd->self.b_hnp_enable) {
8073d46e73dSAntoine Tenart 				otg_start_hnp(hcd->usb_phy->otg);
80883722bc9SAnatolij Gustschin 				break;
80983722bc9SAnatolij Gustschin 			}
81083722bc9SAnatolij Gustschin #endif
81116032c4fSAlan Stern 			if (!(temp & PORT_SUSPEND))
81216032c4fSAlan Stern 				break;
8131da177e4SLinus Torvalds 			if ((temp & PORT_PE) == 0)
8141da177e4SLinus Torvalds 				goto error;
81516032c4fSAlan Stern 
81616032c4fSAlan Stern 			/* clear phy low-power mode before resume */
8172cdcec4fSTuomas Tynkkynen 			if (ehci->has_tdi_phy_lpm) {
818eab80de0SAlek Du 				temp1 = ehci_readl(ehci, hostpc_reg);
819eab80de0SAlek Du 				ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
820eab80de0SAlek Du 						hostpc_reg);
82116032c4fSAlan Stern 				spin_unlock_irqrestore(&ehci->lock, flags);
82216032c4fSAlan Stern 				msleep(5);/* wait to leave low-power mode */
82316032c4fSAlan Stern 				spin_lock_irqsave(&ehci->lock, flags);
824eab80de0SAlek Du 			}
8251da177e4SLinus Torvalds 			/* resume signaling for 20 msec */
8266d5f89c7SStephen Warren 			temp &= ~PORT_WAKE_BITS;
82716032c4fSAlan Stern 			ehci_writel(ehci, temp | PORT_RESUME, status_reg);
8281da177e4SLinus Torvalds 			ehci->reset_done[wIndex] = jiffies
829ea16328fSFelipe Balbi 					+ msecs_to_jiffies(USB_RESUME_TIMEOUT);
8303a20446fSAlan Stern 			set_bit(wIndex, &ehci->resuming_ports);
8313a20446fSAlan Stern 			usb_hcd_start_port_resume(&hcd->self, wIndex);
8321da177e4SLinus Torvalds 			break;
8331da177e4SLinus Torvalds 		case USB_PORT_FEAT_C_SUSPEND:
834d1f114d1SAlan Stern 			clear_bit(wIndex, &ehci->port_c_suspend);
8351da177e4SLinus Torvalds 			break;
8361da177e4SLinus Torvalds 		case USB_PORT_FEAT_POWER:
83711a7e594SMichael Grzeschik 			if (HCS_PPC(ehci->hcs_params)) {
83811a7e594SMichael Grzeschik 				spin_unlock_irqrestore(&ehci->lock, flags);
83911a7e594SMichael Grzeschik 				ehci_port_power(ehci, wIndex, false);
84011a7e594SMichael Grzeschik 				spin_lock_irqsave(&ehci->lock, flags);
84111a7e594SMichael Grzeschik 			}
8421da177e4SLinus Torvalds 			break;
8431da177e4SLinus Torvalds 		case USB_PORT_FEAT_C_CONNECTION:
8446d5f89c7SStephen Warren 			ehci_writel(ehci, temp | PORT_CSC, status_reg);
8451da177e4SLinus Torvalds 			break;
8461da177e4SLinus Torvalds 		case USB_PORT_FEAT_C_OVER_CURRENT:
8476d5f89c7SStephen Warren 			ehci_writel(ehci, temp | PORT_OCC, status_reg);
8481da177e4SLinus Torvalds 			break;
8491da177e4SLinus Torvalds 		case USB_PORT_FEAT_C_RESET:
8501da177e4SLinus Torvalds 			/* GetPortStatus clears reset */
8511da177e4SLinus Torvalds 			break;
8521da177e4SLinus Torvalds 		default:
8531da177e4SLinus Torvalds 			goto error;
8541da177e4SLinus Torvalds 		}
855083522d7SBenjamin Herrenschmidt 		ehci_readl(ehci, &ehci->regs->command);	/* unblock posted write */
8561da177e4SLinus Torvalds 		break;
8571da177e4SLinus Torvalds 	case GetHubDescriptor:
8581da177e4SLinus Torvalds 		ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
8591da177e4SLinus Torvalds 			buf);
8601da177e4SLinus Torvalds 		break;
8611da177e4SLinus Torvalds 	case GetHubStatus:
8621da177e4SLinus Torvalds 		/* no hub-wide feature/status flags */
8631da177e4SLinus Torvalds 		memset (buf, 0, 4);
8641da177e4SLinus Torvalds 		//cpu_to_le32s ((u32 *) buf);
8651da177e4SLinus Torvalds 		break;
8661da177e4SLinus Torvalds 	case GetPortStatus:
8671da177e4SLinus Torvalds 		if (!wIndex || wIndex > ports)
8681da177e4SLinus Torvalds 			goto error;
8691da177e4SLinus Torvalds 		wIndex--;
8701da177e4SLinus Torvalds 		status = 0;
871e6316565SAlan Stern 		temp = ehci_readl(ehci, status_reg);
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 		// wPortChange bits
8741da177e4SLinus Torvalds 		if (temp & PORT_CSC)
875749da5f8SAlan Stern 			status |= USB_PORT_STAT_C_CONNECTION << 16;
8761da177e4SLinus Torvalds 		if (temp & PORT_PEC)
877749da5f8SAlan Stern 			status |= USB_PORT_STAT_C_ENABLE << 16;
878756aa6b3SChristian Engelmayer 
879*dda4b60eSXu Yang 		if (ehci_has_ci_pec_bug(ehci, temp)) {
880*dda4b60eSXu Yang 			status |= USB_PORT_STAT_C_ENABLE << 16;
881*dda4b60eSXu Yang 			ehci_info(ehci,
882*dda4b60eSXu Yang 				"PE is cleared by HW port:%d PORTSC:%08x\n",
883*dda4b60eSXu Yang 				wIndex + 1, temp);
884*dda4b60eSXu Yang 		}
885*dda4b60eSXu Yang 
8862d5ba374SFlorian Fainelli 		if ((temp & PORT_OCC) && (!ignore_oc && !ehci->spurious_oc)){
887749da5f8SAlan Stern 			status |= USB_PORT_STAT_C_OVERCURRENT << 16;
8881da177e4SLinus Torvalds 
889756aa6b3SChristian Engelmayer 			/*
890756aa6b3SChristian Engelmayer 			 * Hubs should disable port power on over-current.
891756aa6b3SChristian Engelmayer 			 * However, not all EHCI implementations do this
892756aa6b3SChristian Engelmayer 			 * automatically, even if they _do_ support per-port
893756aa6b3SChristian Engelmayer 			 * power switching; they're allowed to just limit the
89437ebb549SPetr Mladek 			 * current.  hub_wq will turn the power back on.
895756aa6b3SChristian Engelmayer 			 */
896e6604a7fSChristian Engelmayer 			if (((temp & PORT_OC) || (ehci->need_oc_pp_cycle))
897e6604a7fSChristian Engelmayer 					&& HCS_PPC(ehci->hcs_params)) {
89811a7e594SMichael Grzeschik 				spin_unlock_irqrestore(&ehci->lock, flags);
89911a7e594SMichael Grzeschik 				ehci_port_power(ehci, wIndex, false);
90011a7e594SMichael Grzeschik 				spin_lock_irqsave(&ehci->lock, flags);
90181463c1dSSergei Shtylyov 				temp = ehci_readl(ehci, status_reg);
902756aa6b3SChristian Engelmayer 			}
903756aa6b3SChristian Engelmayer 		}
904756aa6b3SChristian Engelmayer 
9056753f4cfSAlan Stern 		/* no reset or resume pending */
9066753f4cfSAlan Stern 		if (!ehci->reset_done[wIndex]) {
907629e4427SAlan Stern 
908629e4427SAlan Stern 			/* Remote Wakeup received? */
9096753f4cfSAlan Stern 			if (temp & PORT_RESUME) {
910629e4427SAlan Stern 				/* resume signaling for 20 msec */
911629e4427SAlan Stern 				ehci->reset_done[wIndex] = jiffies
912629e4427SAlan Stern 						+ msecs_to_jiffies(20);
913f292e7f9SAlan Stern 				usb_hcd_start_port_resume(&hcd->self, wIndex);
91447a64a13SRoger Quadros 				set_bit(wIndex, &ehci->resuming_ports);
915629e4427SAlan Stern 				/* check the port again */
916629e4427SAlan Stern 				mod_timer(&ehci_to_hcd(ehci)->rh_timer,
917629e4427SAlan Stern 						ehci->reset_done[wIndex]);
918629e4427SAlan Stern 			}
919629e4427SAlan Stern 
9206753f4cfSAlan Stern 		/* reset or resume not yet complete */
9216753f4cfSAlan Stern 		} else if (!time_after_eq(jiffies, ehci->reset_done[wIndex])) {
9226753f4cfSAlan Stern 			;	/* wait until it is complete */
9236753f4cfSAlan Stern 
9246753f4cfSAlan Stern 		/* resume completed */
9256753f4cfSAlan Stern 		} else if (test_bit(wIndex, &ehci->resuming_ports)) {
926eafe5b99SAlan Stern 			clear_bit(wIndex, &ehci->suspended_ports);
927d1f114d1SAlan Stern 			set_bit(wIndex, &ehci->port_c_suspend);
9281da177e4SLinus Torvalds 			ehci->reset_done[wIndex] = 0;
929f292e7f9SAlan Stern 			usb_hcd_end_port_resume(&hcd->self, wIndex);
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 			/* stop resume signaling */
9326753f4cfSAlan Stern 			temp &= ~(PORT_RWC_BITS | PORT_SUSPEND | PORT_RESUME);
93324b90814SAlan Stern 			ehci_writel(ehci, temp, status_reg);
934a448e4dcSAlan Stern 			clear_bit(wIndex, &ehci->resuming_ports);
9352f3a6b86SManjunath Goudar 			retval = ehci_handshake(ehci, status_reg,
9361da177e4SLinus Torvalds 					PORT_RESUME, 0, 2000 /* 2msec */);
9371da177e4SLinus Torvalds 			if (retval != 0) {
9386753f4cfSAlan Stern 				ehci_err(ehci, "port %d resume error %d\n",
9391da177e4SLinus Torvalds 						wIndex + 1, retval);
9401da177e4SLinus Torvalds 				goto error;
9411da177e4SLinus Torvalds 			}
94224b90814SAlan Stern 			temp = ehci_readl(ehci, status_reg);
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds 		/* whoever resets must GetPortStatus to complete it!! */
9456753f4cfSAlan Stern 		} else {
946749da5f8SAlan Stern 			status |= USB_PORT_STAT_C_RESET << 16;
9471da177e4SLinus Torvalds 			ehci->reset_done [wIndex] = 0;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 			/* force reset to complete */
950083522d7SBenjamin Herrenschmidt 			ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
951e6316565SAlan Stern 					status_reg);
952c22fa3acSDavid Brownell 			/* REVISIT:  some hardware needs 550+ usec to clear
953c22fa3acSDavid Brownell 			 * this bit; seems too long to spin routinely...
954c22fa3acSDavid Brownell 			 */
9552f3a6b86SManjunath Goudar 			retval = ehci_handshake(ehci, status_reg,
9566307e096SDinh Nguyen 					PORT_RESET, 0, 1000);
9571da177e4SLinus Torvalds 			if (retval != 0) {
9581da177e4SLinus Torvalds 				ehci_err (ehci, "port %d reset error %d\n",
9591da177e4SLinus Torvalds 					wIndex + 1, retval);
9601da177e4SLinus Torvalds 				goto error;
9611da177e4SLinus Torvalds 			}
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 			/* see what we found out */
964e6316565SAlan Stern 			temp = check_reset_complete (ehci, wIndex, status_reg,
965e6316565SAlan Stern 					ehci_readl(ehci, status_reg));
9661da177e4SLinus Torvalds 		}
9671da177e4SLinus Torvalds 
96857e06c11SAlan Stern 		/* transfer dedicated ports to the companion hc */
96957e06c11SAlan Stern 		if ((temp & PORT_CONNECT) &&
97057e06c11SAlan Stern 				test_bit(wIndex, &ehci->companion_ports)) {
97157e06c11SAlan Stern 			temp &= ~PORT_RWC_BITS;
97257e06c11SAlan Stern 			temp |= PORT_OWNER;
97357e06c11SAlan Stern 			ehci_writel(ehci, temp, status_reg);
97457e06c11SAlan Stern 			ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
97557e06c11SAlan Stern 			temp = ehci_readl(ehci, status_reg);
97657e06c11SAlan Stern 		}
97757e06c11SAlan Stern 
978625b5c9aSAlan Stern 		/*
97937ebb549SPetr Mladek 		 * Even if OWNER is set, there's no harm letting hub_wq
980625b5c9aSAlan Stern 		 * see the wPortStatus values (they should all be 0 except
981625b5c9aSAlan Stern 		 * for PORT_POWER anyway).
982625b5c9aSAlan Stern 		 */
983625b5c9aSAlan Stern 
9841da177e4SLinus Torvalds 		if (temp & PORT_CONNECT) {
985749da5f8SAlan Stern 			status |= USB_PORT_STAT_CONNECTION;
9861da177e4SLinus Torvalds 			// status may be from integrated TT
987331ac6b2SAlek Du 			if (ehci->has_hostpc) {
988331ac6b2SAlek Du 				temp1 = ehci_readl(ehci, hostpc_reg);
989331ac6b2SAlek Du 				status |= ehci_port_speed(ehci, temp1);
990331ac6b2SAlek Du 			} else
9911da177e4SLinus Torvalds 				status |= ehci_port_speed(ehci, temp);
9921da177e4SLinus Torvalds 		}
9931da177e4SLinus Torvalds 		if (temp & PORT_PE)
994749da5f8SAlan Stern 			status |= USB_PORT_STAT_ENABLE;
995eafe5b99SAlan Stern 
996eafe5b99SAlan Stern 		/* maybe the port was unsuspended without our knowledge */
997eafe5b99SAlan Stern 		if (temp & (PORT_SUSPEND|PORT_RESUME)) {
998749da5f8SAlan Stern 			status |= USB_PORT_STAT_SUSPEND;
999eafe5b99SAlan Stern 		} else if (test_bit(wIndex, &ehci->suspended_ports)) {
1000eafe5b99SAlan Stern 			clear_bit(wIndex, &ehci->suspended_ports);
1001a448e4dcSAlan Stern 			clear_bit(wIndex, &ehci->resuming_ports);
1002eafe5b99SAlan Stern 			ehci->reset_done[wIndex] = 0;
1003eafe5b99SAlan Stern 			if (temp & PORT_PE)
1004eafe5b99SAlan Stern 				set_bit(wIndex, &ehci->port_c_suspend);
1005f292e7f9SAlan Stern 			usb_hcd_end_port_resume(&hcd->self, wIndex);
1006eafe5b99SAlan Stern 		}
1007eafe5b99SAlan Stern 
10081da177e4SLinus Torvalds 		if (temp & PORT_OC)
1009749da5f8SAlan Stern 			status |= USB_PORT_STAT_OVERCURRENT;
10101da177e4SLinus Torvalds 		if (temp & PORT_RESET)
1011749da5f8SAlan Stern 			status |= USB_PORT_STAT_RESET;
10121da177e4SLinus Torvalds 		if (temp & PORT_POWER)
1013749da5f8SAlan Stern 			status |= USB_PORT_STAT_POWER;
1014d1f114d1SAlan Stern 		if (test_bit(wIndex, &ehci->port_c_suspend))
1015749da5f8SAlan Stern 			status |= USB_PORT_STAT_C_SUSPEND << 16;
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 		if (status & ~0xffff)	/* only if wPortChange is interesting */
10181da177e4SLinus Torvalds 			dbg_port(ehci, "GetStatus", wIndex + 1, temp);
1019a5abdeafSHarvey Harrison 		put_unaligned_le32(status, buf);
10201da177e4SLinus Torvalds 		break;
10211da177e4SLinus Torvalds 	case SetHubFeature:
10221da177e4SLinus Torvalds 		switch (wValue) {
10231da177e4SLinus Torvalds 		case C_HUB_LOCAL_POWER:
10241da177e4SLinus Torvalds 		case C_HUB_OVER_CURRENT:
10251da177e4SLinus Torvalds 			/* no hub-wide feature/status flags */
10261da177e4SLinus Torvalds 			break;
10271da177e4SLinus Torvalds 		default:
10281da177e4SLinus Torvalds 			goto error;
10291da177e4SLinus Torvalds 		}
10301da177e4SLinus Torvalds 		break;
10311da177e4SLinus Torvalds 	case SetPortFeature:
1032f0d7f273SDavid Brownell 		selector = wIndex >> 8;
1033f0d7f273SDavid Brownell 		wIndex &= 0xff;
10348d053c79SJason Wessel 		if (unlikely(ehci->debug)) {
10358d053c79SJason Wessel 			/* If the debug port is active any port
10368d053c79SJason Wessel 			 * feature requests should get denied */
10378d053c79SJason Wessel 			if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
10388d053c79SJason Wessel 			    (readl(&ehci->debug->control) & DBGP_ENABLED)) {
10398d053c79SJason Wessel 				retval = -ENODEV;
10408d053c79SJason Wessel 				goto error_exit;
10418d053c79SJason Wessel 			}
10428d053c79SJason Wessel 		}
10431da177e4SLinus Torvalds 		if (!wIndex || wIndex > ports)
10441da177e4SLinus Torvalds 			goto error;
10451da177e4SLinus Torvalds 		wIndex--;
1046e6316565SAlan Stern 		temp = ehci_readl(ehci, status_reg);
10471da177e4SLinus Torvalds 		if (temp & PORT_OWNER)
10481da177e4SLinus Torvalds 			break;
10491da177e4SLinus Torvalds 
105010f6524aSDavid Brownell 		temp &= ~PORT_RWC_BITS;
10511da177e4SLinus Torvalds 		switch (wValue) {
10521da177e4SLinus Torvalds 		case USB_PORT_FEAT_SUSPEND:
1053f8aeb3bbSDavid Brownell 			if (ehci->no_selective_suspend)
1054f8aeb3bbSDavid Brownell 				break;
10551da177e4SLinus Torvalds 			if ((temp & PORT_PE) == 0
10561da177e4SLinus Torvalds 					|| (temp & PORT_RESET) != 0)
10571da177e4SLinus Torvalds 				goto error;
1058b9df7942SAlek Du 
1059331ac6b2SAlek Du 			/* After above check the port must be connected.
1060331ac6b2SAlek Du 			 * Set appropriate bit thus could put phy into low power
10612cdcec4fSTuomas Tynkkynen 			 * mode if we have tdi_phy_lpm feature
1062331ac6b2SAlek Du 			 */
1063331ac6b2SAlek Du 			temp &= ~PORT_WKCONN_E;
1064b9df7942SAlek Du 			temp |= PORT_WKDISC_E | PORT_WKOC_E;
1065b9df7942SAlek Du 			ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
10662cdcec4fSTuomas Tynkkynen 			if (ehci->has_tdi_phy_lpm) {
1067b9df7942SAlek Du 				spin_unlock_irqrestore(&ehci->lock, flags);
1068331ac6b2SAlek Du 				msleep(5);/* 5ms for HCD enter low pwr mode */
1069b9df7942SAlek Du 				spin_lock_irqsave(&ehci->lock, flags);
1070331ac6b2SAlek Du 				temp1 = ehci_readl(ehci, hostpc_reg);
1071331ac6b2SAlek Du 				ehci_writel(ehci, temp1 | HOSTPC_PHCD,
1072331ac6b2SAlek Du 					hostpc_reg);
1073331ac6b2SAlek Du 				temp1 = ehci_readl(ehci, hostpc_reg);
1074331ac6b2SAlek Du 				ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
1075331ac6b2SAlek Du 					wIndex, (temp1 & HOSTPC_PHCD) ?
1076331ac6b2SAlek Du 					"succeeded" : "failed");
1077331ac6b2SAlek Du 			}
10789d4b8270SChangming Huang 			if (ehci_has_fsl_susp_errata(ehci)) {
10799d4b8270SChangming Huang 				/* 10ms for HCD enter suspend */
10809d4b8270SChangming Huang 				spin_unlock_irqrestore(&ehci->lock, flags);
10819d4b8270SChangming Huang 				usleep_range(10000, 20000);
10829d4b8270SChangming Huang 				spin_lock_irqsave(&ehci->lock, flags);
10839d4b8270SChangming Huang 			}
1084eafe5b99SAlan Stern 			set_bit(wIndex, &ehci->suspended_ports);
10851da177e4SLinus Torvalds 			break;
10861da177e4SLinus Torvalds 		case USB_PORT_FEAT_POWER:
108711a7e594SMichael Grzeschik 			if (HCS_PPC(ehci->hcs_params)) {
108811a7e594SMichael Grzeschik 				spin_unlock_irqrestore(&ehci->lock, flags);
108911a7e594SMichael Grzeschik 				ehci_port_power(ehci, wIndex, true);
109011a7e594SMichael Grzeschik 				spin_lock_irqsave(&ehci->lock, flags);
109111a7e594SMichael Grzeschik 			}
10921da177e4SLinus Torvalds 			break;
10931da177e4SLinus Torvalds 		case USB_PORT_FEAT_RESET:
10946753f4cfSAlan Stern 			if (temp & (PORT_SUSPEND|PORT_RESUME))
10951da177e4SLinus Torvalds 				goto error;
10961da177e4SLinus Torvalds 			/* line status bits may report this as low speed,
10971da177e4SLinus Torvalds 			 * which can be fine if this root hub has a
10981da177e4SLinus Torvalds 			 * transaction translator built in.
10991da177e4SLinus Torvalds 			 */
11001da177e4SLinus Torvalds 			if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
11011da177e4SLinus Torvalds 					&& !ehci_is_TDI(ehci)
11021da177e4SLinus Torvalds 					&& PORT_USB11 (temp)) {
11031da177e4SLinus Torvalds 				ehci_dbg (ehci,
11041da177e4SLinus Torvalds 					"port %d low speed --> companion\n",
11051da177e4SLinus Torvalds 					wIndex + 1);
11061da177e4SLinus Torvalds 				temp |= PORT_OWNER;
11071da177e4SLinus Torvalds 			} else {
11081da177e4SLinus Torvalds 				temp |= PORT_RESET;
11091da177e4SLinus Torvalds 				temp &= ~PORT_PE;
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 				/*
11121da177e4SLinus Torvalds 				 * caller must wait, then call GetPortStatus
11131da177e4SLinus Torvalds 				 * usb 2.0 spec says 50 ms resets on root
11141da177e4SLinus Torvalds 				 */
11151da177e4SLinus Torvalds 				ehci->reset_done [wIndex] = jiffies
11161da177e4SLinus Torvalds 						+ msecs_to_jiffies (50);
1117f8786a91SNikhil Badola 
1118f8786a91SNikhil Badola 				/*
1119f8786a91SNikhil Badola 				 * Force full-speed connect for FSL high-speed
1120f8786a91SNikhil Badola 				 * erratum; disable HS Chirp by setting PFSC bit
1121f8786a91SNikhil Badola 				 */
1122f8786a91SNikhil Badola 				if (ehci_has_fsl_hs_errata(ehci))
1123f8786a91SNikhil Badola 					temp |= (1 << PORTSC_FSL_PFSC);
11241da177e4SLinus Torvalds 			}
1125e6316565SAlan Stern 			ehci_writel(ehci, temp, status_reg);
11261da177e4SLinus Torvalds 			break;
1127f0d7f273SDavid Brownell 
1128f0d7f273SDavid Brownell 		/* For downstream facing ports (these):  one hub port is put
1129f0d7f273SDavid Brownell 		 * into test mode according to USB2 11.24.2.13, then the hub
1130f0d7f273SDavid Brownell 		 * must be reset (which for root hub now means rmmod+modprobe,
1131f0d7f273SDavid Brownell 		 * or else system reboot).  See EHCI 2.3.9 and 4.14 for info
1132f0d7f273SDavid Brownell 		 * about the EHCI-specific stuff.
1133f0d7f273SDavid Brownell 		 */
1134f0d7f273SDavid Brownell 		case USB_PORT_FEAT_TEST:
1135726a85caSJack Pham #ifdef CONFIG_USB_HCD_TEST_MODE
11369841f37aSManu Gautam 			if (selector == EHSET_TEST_SINGLE_STEP_SET_FEATURE) {
11379841f37aSManu Gautam 				spin_unlock_irqrestore(&ehci->lock, flags);
11389841f37aSManu Gautam 				retval = ehset_single_step_set_feature(hcd,
11395cbcc35eSPeter Chen 								wIndex + 1);
11409841f37aSManu Gautam 				spin_lock_irqsave(&ehci->lock, flags);
11419841f37aSManu Gautam 				break;
1142726a85caSJack Pham 			}
1143726a85caSJack Pham #endif
1144726a85caSJack Pham 			if (!selector || selector > 5)
1145f0d7f273SDavid Brownell 				goto error;
1146c4f34764SAlan Stern 			spin_unlock_irqrestore(&ehci->lock, flags);
1147f0d7f273SDavid Brownell 			ehci_quiesce(ehci);
1148c4f34764SAlan Stern 			spin_lock_irqsave(&ehci->lock, flags);
114977636c86SBoris Todorov 
115077636c86SBoris Todorov 			/* Put all enabled ports into suspend */
115177636c86SBoris Todorov 			while (ports--) {
115277636c86SBoris Todorov 				u32 __iomem *sreg =
115377636c86SBoris Todorov 						&ehci->regs->port_status[ports];
115477636c86SBoris Todorov 
115577636c86SBoris Todorov 				temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS;
115677636c86SBoris Todorov 				if (temp & PORT_PE)
115777636c86SBoris Todorov 					ehci_writel(ehci, temp | PORT_SUSPEND,
115877636c86SBoris Todorov 							sreg);
115977636c86SBoris Todorov 			}
1160c4f34764SAlan Stern 
1161c4f34764SAlan Stern 			spin_unlock_irqrestore(&ehci->lock, flags);
1162f0d7f273SDavid Brownell 			ehci_halt(ehci);
1163c4f34764SAlan Stern 			spin_lock_irqsave(&ehci->lock, flags);
1164c4f34764SAlan Stern 
116577636c86SBoris Todorov 			temp = ehci_readl(ehci, status_reg);
1166f0d7f273SDavid Brownell 			temp |= selector << 16;
1167e6316565SAlan Stern 			ehci_writel(ehci, temp, status_reg);
1168f0d7f273SDavid Brownell 			break;
1169f0d7f273SDavid Brownell 
11701da177e4SLinus Torvalds 		default:
11711da177e4SLinus Torvalds 			goto error;
11721da177e4SLinus Torvalds 		}
1173083522d7SBenjamin Herrenschmidt 		ehci_readl(ehci, &ehci->regs->command);	/* unblock posted writes */
11741da177e4SLinus Torvalds 		break;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 	default:
11771da177e4SLinus Torvalds error:
11781da177e4SLinus Torvalds 		/* "stall" on error */
11791da177e4SLinus Torvalds 		retval = -EPIPE;
11801da177e4SLinus Torvalds 	}
11818d053c79SJason Wessel error_exit:
11821da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ehci->lock, flags);
11831da177e4SLinus Torvalds 	return retval;
11841da177e4SLinus Torvalds }
118537769939SLaurent Pinchart EXPORT_SYMBOL_GPL(ehci_hub_control);
118690da096eSBalaji Rao 
ehci_relinquish_port(struct usb_hcd * hcd,int portnum)11873e023203SAlan Stern static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
118890da096eSBalaji Rao {
118990da096eSBalaji Rao 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
119090da096eSBalaji Rao 
119190da096eSBalaji Rao 	if (ehci_is_TDI(ehci))
119290da096eSBalaji Rao 		return;
119390da096eSBalaji Rao 	set_owner(ehci, --portnum, PORT_OWNER);
119490da096eSBalaji Rao }
119590da096eSBalaji Rao 
ehci_port_handed_over(struct usb_hcd * hcd,int portnum)11963e023203SAlan Stern static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
11973a31155cSAlan Stern {
11983a31155cSAlan Stern 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
11993a31155cSAlan Stern 	u32 __iomem		*reg;
12003a31155cSAlan Stern 
12013a31155cSAlan Stern 	if (ehci_is_TDI(ehci))
12023a31155cSAlan Stern 		return 0;
12033a31155cSAlan Stern 	reg = &ehci->regs->port_status[portnum - 1];
12043a31155cSAlan Stern 	return ehci_readl(ehci, reg) & PORT_OWNER;
12053a31155cSAlan Stern }
120611a7e594SMichael Grzeschik 
ehci_port_power(struct ehci_hcd * ehci,int portnum,bool enable)120711a7e594SMichael Grzeschik static int ehci_port_power(struct ehci_hcd *ehci, int portnum, bool enable)
120811a7e594SMichael Grzeschik {
120911a7e594SMichael Grzeschik 	struct usb_hcd *hcd = ehci_to_hcd(ehci);
121011a7e594SMichael Grzeschik 	u32 __iomem *status_reg = &ehci->regs->port_status[portnum];
121111a7e594SMichael Grzeschik 	u32 temp = ehci_readl(ehci, status_reg) & ~PORT_RWC_BITS;
121211a7e594SMichael Grzeschik 
121311a7e594SMichael Grzeschik 	if (enable)
121411a7e594SMichael Grzeschik 		ehci_writel(ehci, temp | PORT_POWER, status_reg);
121511a7e594SMichael Grzeschik 	else
121611a7e594SMichael Grzeschik 		ehci_writel(ehci, temp & ~PORT_POWER, status_reg);
121711a7e594SMichael Grzeschik 
121811a7e594SMichael Grzeschik 	if (hcd->driver->port_power)
121911a7e594SMichael Grzeschik 		hcd->driver->port_power(hcd, portnum, enable);
122011a7e594SMichael Grzeschik 
122111a7e594SMichael Grzeschik 	return 0;
122211a7e594SMichael Grzeschik }
1223