162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2001-2004 by David Brownell
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci/* this file is part of ehci-hcd.c */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci/*-------------------------------------------------------------------------*/
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci/*
1162306a36Sopenharmony_ci * EHCI Root Hub ... the nonsharable stuff
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * Registers don't need cpu_to_le32, that happens transparently
1462306a36Sopenharmony_ci */
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/*-------------------------------------------------------------------------*/
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#define	PORT_WAKE_BITS	(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#ifdef	CONFIG_PM
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cistatic void unlink_empty_async_suspended(struct ehci_hcd *ehci);
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cistatic int persist_enabled_on_companion(struct usb_device *udev, void *unused)
2562306a36Sopenharmony_ci{
2662306a36Sopenharmony_ci	return !udev->maxchild && udev->persist_enabled &&
2762306a36Sopenharmony_ci		udev->bus->root_hub->speed < USB_SPEED_HIGH;
2862306a36Sopenharmony_ci}
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci/* After a power loss, ports that were owned by the companion must be
3162306a36Sopenharmony_ci * reset so that the companion can still own them.
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_cistatic void ehci_handover_companion_ports(struct ehci_hcd *ehci)
3462306a36Sopenharmony_ci{
3562306a36Sopenharmony_ci	u32 __iomem	*reg;
3662306a36Sopenharmony_ci	u32		status;
3762306a36Sopenharmony_ci	int		port;
3862306a36Sopenharmony_ci	__le32		buf;
3962306a36Sopenharmony_ci	struct usb_hcd	*hcd = ehci_to_hcd(ehci);
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci	if (!ehci->owned_ports)
4262306a36Sopenharmony_ci		return;
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	/*
4562306a36Sopenharmony_ci	 * USB 1.1 devices are mostly HIDs, which don't need to persist across
4662306a36Sopenharmony_ci	 * suspends. If we ensure that none of our companion's devices have
4762306a36Sopenharmony_ci	 * persist_enabled (by looking through all USB 1.1 buses in the system),
4862306a36Sopenharmony_ci	 * we can skip this and avoid slowing resume down. Devices without
4962306a36Sopenharmony_ci	 * persist will just get reenumerated shortly after resume anyway.
5062306a36Sopenharmony_ci	 */
5162306a36Sopenharmony_ci	if (!usb_for_each_dev(NULL, persist_enabled_on_companion))
5262306a36Sopenharmony_ci		return;
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	/* Make sure the ports are powered */
5562306a36Sopenharmony_ci	port = HCS_N_PORTS(ehci->hcs_params);
5662306a36Sopenharmony_ci	while (port--) {
5762306a36Sopenharmony_ci		if (test_bit(port, &ehci->owned_ports)) {
5862306a36Sopenharmony_ci			reg = &ehci->regs->port_status[port];
5962306a36Sopenharmony_ci			status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
6062306a36Sopenharmony_ci			if (!(status & PORT_POWER))
6162306a36Sopenharmony_ci				ehci_port_power(ehci, port, true);
6262306a36Sopenharmony_ci		}
6362306a36Sopenharmony_ci	}
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	/* Give the connections some time to appear */
6662306a36Sopenharmony_ci	msleep(20);
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	spin_lock_irq(&ehci->lock);
6962306a36Sopenharmony_ci	port = HCS_N_PORTS(ehci->hcs_params);
7062306a36Sopenharmony_ci	while (port--) {
7162306a36Sopenharmony_ci		if (test_bit(port, &ehci->owned_ports)) {
7262306a36Sopenharmony_ci			reg = &ehci->regs->port_status[port];
7362306a36Sopenharmony_ci			status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci			/* Port already owned by companion? */
7662306a36Sopenharmony_ci			if (status & PORT_OWNER)
7762306a36Sopenharmony_ci				clear_bit(port, &ehci->owned_ports);
7862306a36Sopenharmony_ci			else if (test_bit(port, &ehci->companion_ports))
7962306a36Sopenharmony_ci				ehci_writel(ehci, status & ~PORT_PE, reg);
8062306a36Sopenharmony_ci			else {
8162306a36Sopenharmony_ci				spin_unlock_irq(&ehci->lock);
8262306a36Sopenharmony_ci				ehci_hub_control(hcd, SetPortFeature,
8362306a36Sopenharmony_ci						USB_PORT_FEAT_RESET, port + 1,
8462306a36Sopenharmony_ci						NULL, 0);
8562306a36Sopenharmony_ci				spin_lock_irq(&ehci->lock);
8662306a36Sopenharmony_ci			}
8762306a36Sopenharmony_ci		}
8862306a36Sopenharmony_ci	}
8962306a36Sopenharmony_ci	spin_unlock_irq(&ehci->lock);
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	if (!ehci->owned_ports)
9262306a36Sopenharmony_ci		return;
9362306a36Sopenharmony_ci	msleep(90);		/* Wait for resets to complete */
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	spin_lock_irq(&ehci->lock);
9662306a36Sopenharmony_ci	port = HCS_N_PORTS(ehci->hcs_params);
9762306a36Sopenharmony_ci	while (port--) {
9862306a36Sopenharmony_ci		if (test_bit(port, &ehci->owned_ports)) {
9962306a36Sopenharmony_ci			spin_unlock_irq(&ehci->lock);
10062306a36Sopenharmony_ci			ehci_hub_control(hcd, GetPortStatus,
10162306a36Sopenharmony_ci					0, port + 1,
10262306a36Sopenharmony_ci					(char *) &buf, sizeof(buf));
10362306a36Sopenharmony_ci			spin_lock_irq(&ehci->lock);
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci			/* The companion should now own the port,
10662306a36Sopenharmony_ci			 * but if something went wrong the port must not
10762306a36Sopenharmony_ci			 * remain enabled.
10862306a36Sopenharmony_ci			 */
10962306a36Sopenharmony_ci			reg = &ehci->regs->port_status[port];
11062306a36Sopenharmony_ci			status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
11162306a36Sopenharmony_ci			if (status & PORT_OWNER)
11262306a36Sopenharmony_ci				ehci_writel(ehci, status | PORT_CSC, reg);
11362306a36Sopenharmony_ci			else {
11462306a36Sopenharmony_ci				ehci_dbg(ehci, "failed handover port %d: %x\n",
11562306a36Sopenharmony_ci						port + 1, status);
11662306a36Sopenharmony_ci				ehci_writel(ehci, status & ~PORT_PE, reg);
11762306a36Sopenharmony_ci			}
11862306a36Sopenharmony_ci		}
11962306a36Sopenharmony_ci	}
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	ehci->owned_ports = 0;
12262306a36Sopenharmony_ci	spin_unlock_irq(&ehci->lock);
12362306a36Sopenharmony_ci}
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_cistatic int ehci_port_change(struct ehci_hcd *ehci)
12662306a36Sopenharmony_ci{
12762306a36Sopenharmony_ci	int i = HCS_N_PORTS(ehci->hcs_params);
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	/* First check if the controller indicates a change event */
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	if (ehci_readl(ehci, &ehci->regs->status) & STS_PCD)
13262306a36Sopenharmony_ci		return 1;
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	/*
13562306a36Sopenharmony_ci	 * Not all controllers appear to update this while going from D3 to D0,
13662306a36Sopenharmony_ci	 * so check the individual port status registers as well
13762306a36Sopenharmony_ci	 */
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	while (i--)
14062306a36Sopenharmony_ci		if (ehci_readl(ehci, &ehci->regs->port_status[i]) & PORT_CSC)
14162306a36Sopenharmony_ci			return 1;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	return 0;
14462306a36Sopenharmony_ci}
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_civoid ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
14762306a36Sopenharmony_ci		bool suspending, bool do_wakeup)
14862306a36Sopenharmony_ci{
14962306a36Sopenharmony_ci	int		port;
15062306a36Sopenharmony_ci	u32		temp;
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci	/* If remote wakeup is enabled for the root hub but disabled
15362306a36Sopenharmony_ci	 * for the controller, we must adjust all the port wakeup flags
15462306a36Sopenharmony_ci	 * when the controller is suspended or resumed.  In all other
15562306a36Sopenharmony_ci	 * cases they don't need to be changed.
15662306a36Sopenharmony_ci	 */
15762306a36Sopenharmony_ci	if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup)
15862306a36Sopenharmony_ci		return;
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	spin_lock_irq(&ehci->lock);
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	/* clear phy low-power mode before changing wakeup flags */
16362306a36Sopenharmony_ci	if (ehci->has_tdi_phy_lpm) {
16462306a36Sopenharmony_ci		port = HCS_N_PORTS(ehci->hcs_params);
16562306a36Sopenharmony_ci		while (port--) {
16662306a36Sopenharmony_ci			u32 __iomem	*hostpc_reg = &ehci->regs->hostpc[port];
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci			temp = ehci_readl(ehci, hostpc_reg);
16962306a36Sopenharmony_ci			ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg);
17062306a36Sopenharmony_ci		}
17162306a36Sopenharmony_ci		spin_unlock_irq(&ehci->lock);
17262306a36Sopenharmony_ci		msleep(5);
17362306a36Sopenharmony_ci		spin_lock_irq(&ehci->lock);
17462306a36Sopenharmony_ci	}
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	port = HCS_N_PORTS(ehci->hcs_params);
17762306a36Sopenharmony_ci	while (port--) {
17862306a36Sopenharmony_ci		u32 __iomem	*reg = &ehci->regs->port_status[port];
17962306a36Sopenharmony_ci		u32		t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
18062306a36Sopenharmony_ci		u32		t2 = t1 & ~PORT_WAKE_BITS;
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci		/* If we are suspending the controller, clear the flags.
18362306a36Sopenharmony_ci		 * If we are resuming the controller, set the wakeup flags.
18462306a36Sopenharmony_ci		 */
18562306a36Sopenharmony_ci		if (!suspending) {
18662306a36Sopenharmony_ci			if (t1 & PORT_CONNECT)
18762306a36Sopenharmony_ci				t2 |= PORT_WKOC_E | PORT_WKDISC_E;
18862306a36Sopenharmony_ci			else
18962306a36Sopenharmony_ci				t2 |= PORT_WKOC_E | PORT_WKCONN_E;
19062306a36Sopenharmony_ci		}
19162306a36Sopenharmony_ci		ehci_writel(ehci, t2, reg);
19262306a36Sopenharmony_ci	}
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci	/* enter phy low-power mode again */
19562306a36Sopenharmony_ci	if (ehci->has_tdi_phy_lpm) {
19662306a36Sopenharmony_ci		port = HCS_N_PORTS(ehci->hcs_params);
19762306a36Sopenharmony_ci		while (port--) {
19862306a36Sopenharmony_ci			u32 __iomem	*hostpc_reg = &ehci->regs->hostpc[port];
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci			temp = ehci_readl(ehci, hostpc_reg);
20162306a36Sopenharmony_ci			ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg);
20262306a36Sopenharmony_ci		}
20362306a36Sopenharmony_ci	}
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	/* Does the root hub have a port wakeup pending? */
20662306a36Sopenharmony_ci	if (!suspending && ehci_port_change(ehci))
20762306a36Sopenharmony_ci		usb_hcd_resume_root_hub(ehci_to_hcd(ehci));
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci	spin_unlock_irq(&ehci->lock);
21062306a36Sopenharmony_ci}
21162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(ehci_adjust_port_wakeup_flags);
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_cistatic int ehci_bus_suspend (struct usb_hcd *hcd)
21462306a36Sopenharmony_ci{
21562306a36Sopenharmony_ci	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
21662306a36Sopenharmony_ci	int			port;
21762306a36Sopenharmony_ci	int			mask;
21862306a36Sopenharmony_ci	int			changed;
21962306a36Sopenharmony_ci	bool			fs_idle_delay;
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	ehci_dbg(ehci, "suspend root hub\n");
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	if (time_before (jiffies, ehci->next_statechange))
22462306a36Sopenharmony_ci		msleep(5);
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci	/* stop the schedules */
22762306a36Sopenharmony_ci	ehci_quiesce(ehci);
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci	spin_lock_irq (&ehci->lock);
23062306a36Sopenharmony_ci	if (ehci->rh_state < EHCI_RH_RUNNING)
23162306a36Sopenharmony_ci		goto done;
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci	/* Once the controller is stopped, port resumes that are already
23462306a36Sopenharmony_ci	 * in progress won't complete.  Hence if remote wakeup is enabled
23562306a36Sopenharmony_ci	 * for the root hub and any ports are in the middle of a resume or
23662306a36Sopenharmony_ci	 * remote wakeup, we must fail the suspend.
23762306a36Sopenharmony_ci	 */
23862306a36Sopenharmony_ci	if (hcd->self.root_hub->do_remote_wakeup) {
23962306a36Sopenharmony_ci		if (ehci->resuming_ports) {
24062306a36Sopenharmony_ci			spin_unlock_irq(&ehci->lock);
24162306a36Sopenharmony_ci			ehci_dbg(ehci, "suspend failed because a port is resuming\n");
24262306a36Sopenharmony_ci			return -EBUSY;
24362306a36Sopenharmony_ci		}
24462306a36Sopenharmony_ci	}
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci	/* Unlike other USB host controller types, EHCI doesn't have
24762306a36Sopenharmony_ci	 * any notion of "global" or bus-wide suspend.  The driver has
24862306a36Sopenharmony_ci	 * to manually suspend all the active unsuspended ports, and
24962306a36Sopenharmony_ci	 * then manually resume them in the bus_resume() routine.
25062306a36Sopenharmony_ci	 */
25162306a36Sopenharmony_ci	ehci->bus_suspended = 0;
25262306a36Sopenharmony_ci	ehci->owned_ports = 0;
25362306a36Sopenharmony_ci	changed = 0;
25462306a36Sopenharmony_ci	fs_idle_delay = false;
25562306a36Sopenharmony_ci	port = HCS_N_PORTS(ehci->hcs_params);
25662306a36Sopenharmony_ci	while (port--) {
25762306a36Sopenharmony_ci		u32 __iomem	*reg = &ehci->regs->port_status [port];
25862306a36Sopenharmony_ci		u32		t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
25962306a36Sopenharmony_ci		u32		t2 = t1 & ~PORT_WAKE_BITS;
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci		/* keep track of which ports we suspend */
26262306a36Sopenharmony_ci		if (t1 & PORT_OWNER)
26362306a36Sopenharmony_ci			set_bit(port, &ehci->owned_ports);
26462306a36Sopenharmony_ci		else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
26562306a36Sopenharmony_ci			t2 |= PORT_SUSPEND;
26662306a36Sopenharmony_ci			set_bit(port, &ehci->bus_suspended);
26762306a36Sopenharmony_ci		}
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_ci		/* enable remote wakeup on all ports, if told to do so */
27062306a36Sopenharmony_ci		if (hcd->self.root_hub->do_remote_wakeup) {
27162306a36Sopenharmony_ci			/* only enable appropriate wake bits, otherwise the
27262306a36Sopenharmony_ci			 * hardware can not go phy low power mode. If a race
27362306a36Sopenharmony_ci			 * condition happens here(connection change during bits
27462306a36Sopenharmony_ci			 * set), the port change detection will finally fix it.
27562306a36Sopenharmony_ci			 */
27662306a36Sopenharmony_ci			if (t1 & PORT_CONNECT)
27762306a36Sopenharmony_ci				t2 |= PORT_WKOC_E | PORT_WKDISC_E;
27862306a36Sopenharmony_ci			else
27962306a36Sopenharmony_ci				t2 |= PORT_WKOC_E | PORT_WKCONN_E;
28062306a36Sopenharmony_ci		}
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci		if (t1 != t2) {
28362306a36Sopenharmony_ci			/*
28462306a36Sopenharmony_ci			 * On some controllers, Wake-On-Disconnect will
28562306a36Sopenharmony_ci			 * generate false wakeup signals until the bus
28662306a36Sopenharmony_ci			 * switches over to full-speed idle.  For their
28762306a36Sopenharmony_ci			 * sake, add a delay if we need one.
28862306a36Sopenharmony_ci			 */
28962306a36Sopenharmony_ci			if ((t2 & PORT_WKDISC_E) &&
29062306a36Sopenharmony_ci					ehci_port_speed(ehci, t2) ==
29162306a36Sopenharmony_ci						USB_PORT_STAT_HIGH_SPEED)
29262306a36Sopenharmony_ci				fs_idle_delay = true;
29362306a36Sopenharmony_ci			ehci_writel(ehci, t2, reg);
29462306a36Sopenharmony_ci			changed = 1;
29562306a36Sopenharmony_ci		}
29662306a36Sopenharmony_ci	}
29762306a36Sopenharmony_ci	spin_unlock_irq(&ehci->lock);
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci	if (changed && ehci_has_fsl_susp_errata(ehci))
30062306a36Sopenharmony_ci		/*
30162306a36Sopenharmony_ci		 * Wait for at least 10 millisecondes to ensure the controller
30262306a36Sopenharmony_ci		 * enter the suspend status before initiating a port resume
30362306a36Sopenharmony_ci		 * using the Force Port Resume bit (Not-EHCI compatible).
30462306a36Sopenharmony_ci		 */
30562306a36Sopenharmony_ci		usleep_range(10000, 20000);
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci	if ((changed && ehci->has_tdi_phy_lpm) || fs_idle_delay) {
30862306a36Sopenharmony_ci		/*
30962306a36Sopenharmony_ci		 * Wait for HCD to enter low-power mode or for the bus
31062306a36Sopenharmony_ci		 * to switch to full-speed idle.
31162306a36Sopenharmony_ci		 */
31262306a36Sopenharmony_ci		usleep_range(5000, 5500);
31362306a36Sopenharmony_ci	}
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci	if (changed && ehci->has_tdi_phy_lpm) {
31662306a36Sopenharmony_ci		spin_lock_irq(&ehci->lock);
31762306a36Sopenharmony_ci		port = HCS_N_PORTS(ehci->hcs_params);
31862306a36Sopenharmony_ci		while (port--) {
31962306a36Sopenharmony_ci			u32 __iomem	*hostpc_reg = &ehci->regs->hostpc[port];
32062306a36Sopenharmony_ci			u32		t3;
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci			t3 = ehci_readl(ehci, hostpc_reg);
32362306a36Sopenharmony_ci			ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
32462306a36Sopenharmony_ci			t3 = ehci_readl(ehci, hostpc_reg);
32562306a36Sopenharmony_ci			ehci_dbg(ehci, "Port %d phy low-power mode %s\n",
32662306a36Sopenharmony_ci					port, (t3 & HOSTPC_PHCD) ?
32762306a36Sopenharmony_ci					"succeeded" : "failed");
32862306a36Sopenharmony_ci		}
32962306a36Sopenharmony_ci		spin_unlock_irq(&ehci->lock);
33062306a36Sopenharmony_ci	}
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci	/* Apparently some devices need a >= 1-uframe delay here */
33362306a36Sopenharmony_ci	if (ehci->bus_suspended)
33462306a36Sopenharmony_ci		udelay(150);
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci	/* turn off now-idle HC */
33762306a36Sopenharmony_ci	ehci_halt (ehci);
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci	spin_lock_irq(&ehci->lock);
34062306a36Sopenharmony_ci	if (ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_POLL_DEAD))
34162306a36Sopenharmony_ci		ehci_handle_controller_death(ehci);
34262306a36Sopenharmony_ci	if (ehci->rh_state != EHCI_RH_RUNNING)
34362306a36Sopenharmony_ci		goto done;
34462306a36Sopenharmony_ci	ehci->rh_state = EHCI_RH_SUSPENDED;
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci	unlink_empty_async_suspended(ehci);
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci	/* Some Synopsys controllers mistakenly leave IAA turned on */
34962306a36Sopenharmony_ci	ehci_writel(ehci, STS_IAA, &ehci->regs->status);
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci	/* Any IAA cycle that started before the suspend is now invalid */
35262306a36Sopenharmony_ci	end_iaa_cycle(ehci);
35362306a36Sopenharmony_ci	ehci_handle_start_intr_unlinks(ehci);
35462306a36Sopenharmony_ci	ehci_handle_intr_unlinks(ehci);
35562306a36Sopenharmony_ci	end_free_itds(ehci);
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ci	/* allow remote wakeup */
35862306a36Sopenharmony_ci	mask = INTR_MASK;
35962306a36Sopenharmony_ci	if (!hcd->self.root_hub->do_remote_wakeup)
36062306a36Sopenharmony_ci		mask &= ~STS_PCD;
36162306a36Sopenharmony_ci	ehci_writel(ehci, mask, &ehci->regs->intr_enable);
36262306a36Sopenharmony_ci	ehci_readl(ehci, &ehci->regs->intr_enable);
36362306a36Sopenharmony_ci
36462306a36Sopenharmony_ci done:
36562306a36Sopenharmony_ci	ehci->next_statechange = jiffies + msecs_to_jiffies(10);
36662306a36Sopenharmony_ci	ehci->enabled_hrtimer_events = 0;
36762306a36Sopenharmony_ci	ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
36862306a36Sopenharmony_ci	spin_unlock_irq (&ehci->lock);
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci	hrtimer_cancel(&ehci->hrtimer);
37162306a36Sopenharmony_ci	return 0;
37262306a36Sopenharmony_ci}
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci/* caller has locked the root hub, and should reset/reinit on error */
37662306a36Sopenharmony_cistatic int ehci_bus_resume (struct usb_hcd *hcd)
37762306a36Sopenharmony_ci{
37862306a36Sopenharmony_ci	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
37962306a36Sopenharmony_ci	u32			temp;
38062306a36Sopenharmony_ci	u32			power_okay;
38162306a36Sopenharmony_ci	int			i;
38262306a36Sopenharmony_ci	unsigned long		resume_needed = 0;
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci	if (time_before (jiffies, ehci->next_statechange))
38562306a36Sopenharmony_ci		msleep(5);
38662306a36Sopenharmony_ci	spin_lock_irq (&ehci->lock);
38762306a36Sopenharmony_ci	if (!HCD_HW_ACCESSIBLE(hcd) || ehci->shutdown)
38862306a36Sopenharmony_ci		goto shutdown;
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci	if (unlikely(ehci->debug)) {
39162306a36Sopenharmony_ci		if (!dbgp_reset_prep(hcd))
39262306a36Sopenharmony_ci			ehci->debug = NULL;
39362306a36Sopenharmony_ci		else
39462306a36Sopenharmony_ci			dbgp_external_startup(hcd);
39562306a36Sopenharmony_ci	}
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_ci	/* Ideally and we've got a real resume here, and no port's power
39862306a36Sopenharmony_ci	 * was lost.  (For PCI, that means Vaux was maintained.)  But we
39962306a36Sopenharmony_ci	 * could instead be restoring a swsusp snapshot -- so that BIOS was
40062306a36Sopenharmony_ci	 * the last user of the controller, not reset/pm hardware keeping
40162306a36Sopenharmony_ci	 * state we gave to it.
40262306a36Sopenharmony_ci	 */
40362306a36Sopenharmony_ci	power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
40462306a36Sopenharmony_ci	ehci_dbg(ehci, "resume root hub%s\n",
40562306a36Sopenharmony_ci			power_okay ? "" : " after power loss");
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_ci	/* at least some APM implementations will try to deliver
40862306a36Sopenharmony_ci	 * IRQs right away, so delay them until we're ready.
40962306a36Sopenharmony_ci	 */
41062306a36Sopenharmony_ci	ehci_writel(ehci, 0, &ehci->regs->intr_enable);
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci	/* re-init operational registers */
41362306a36Sopenharmony_ci	ehci_writel(ehci, 0, &ehci->regs->segment);
41462306a36Sopenharmony_ci	ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
41562306a36Sopenharmony_ci	ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_ci	/* restore CMD_RUN, framelist size, and irq threshold */
41862306a36Sopenharmony_ci	ehci->command |= CMD_RUN;
41962306a36Sopenharmony_ci	ehci_writel(ehci, ehci->command, &ehci->regs->command);
42062306a36Sopenharmony_ci	ehci->rh_state = EHCI_RH_RUNNING;
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_ci	/*
42362306a36Sopenharmony_ci	 * According to Bugzilla #8190, the port status for some controllers
42462306a36Sopenharmony_ci	 * will be wrong without a delay. At their wrong status, the port
42562306a36Sopenharmony_ci	 * is enabled, but not suspended neither resumed.
42662306a36Sopenharmony_ci	 */
42762306a36Sopenharmony_ci	i = HCS_N_PORTS(ehci->hcs_params);
42862306a36Sopenharmony_ci	while (i--) {
42962306a36Sopenharmony_ci		temp = ehci_readl(ehci, &ehci->regs->port_status[i]);
43062306a36Sopenharmony_ci		if ((temp & PORT_PE) &&
43162306a36Sopenharmony_ci				!(temp & (PORT_SUSPEND | PORT_RESUME))) {
43262306a36Sopenharmony_ci			ehci_dbg(ehci, "Port status(0x%x) is wrong\n", temp);
43362306a36Sopenharmony_ci			spin_unlock_irq(&ehci->lock);
43462306a36Sopenharmony_ci			msleep(8);
43562306a36Sopenharmony_ci			spin_lock_irq(&ehci->lock);
43662306a36Sopenharmony_ci			break;
43762306a36Sopenharmony_ci		}
43862306a36Sopenharmony_ci	}
43962306a36Sopenharmony_ci
44062306a36Sopenharmony_ci	if (ehci->shutdown)
44162306a36Sopenharmony_ci		goto shutdown;
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci	/* clear phy low-power mode before resume */
44462306a36Sopenharmony_ci	if (ehci->bus_suspended && ehci->has_tdi_phy_lpm) {
44562306a36Sopenharmony_ci		i = HCS_N_PORTS(ehci->hcs_params);
44662306a36Sopenharmony_ci		while (i--) {
44762306a36Sopenharmony_ci			if (test_bit(i, &ehci->bus_suspended)) {
44862306a36Sopenharmony_ci				u32 __iomem	*hostpc_reg =
44962306a36Sopenharmony_ci							&ehci->regs->hostpc[i];
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_ci				temp = ehci_readl(ehci, hostpc_reg);
45262306a36Sopenharmony_ci				ehci_writel(ehci, temp & ~HOSTPC_PHCD,
45362306a36Sopenharmony_ci						hostpc_reg);
45462306a36Sopenharmony_ci			}
45562306a36Sopenharmony_ci		}
45662306a36Sopenharmony_ci		spin_unlock_irq(&ehci->lock);
45762306a36Sopenharmony_ci		msleep(5);
45862306a36Sopenharmony_ci		spin_lock_irq(&ehci->lock);
45962306a36Sopenharmony_ci		if (ehci->shutdown)
46062306a36Sopenharmony_ci			goto shutdown;
46162306a36Sopenharmony_ci	}
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ci	/* manually resume the ports we suspended during bus_suspend() */
46462306a36Sopenharmony_ci	i = HCS_N_PORTS (ehci->hcs_params);
46562306a36Sopenharmony_ci	while (i--) {
46662306a36Sopenharmony_ci		temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
46762306a36Sopenharmony_ci		temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
46862306a36Sopenharmony_ci		if (test_bit(i, &ehci->bus_suspended) &&
46962306a36Sopenharmony_ci				(temp & PORT_SUSPEND)) {
47062306a36Sopenharmony_ci			temp |= PORT_RESUME;
47162306a36Sopenharmony_ci			set_bit(i, &resume_needed);
47262306a36Sopenharmony_ci		}
47362306a36Sopenharmony_ci		ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
47462306a36Sopenharmony_ci	}
47562306a36Sopenharmony_ci
47662306a36Sopenharmony_ci	/*
47762306a36Sopenharmony_ci	 * msleep for USB_RESUME_TIMEOUT ms only if code is trying to resume
47862306a36Sopenharmony_ci	 * port
47962306a36Sopenharmony_ci	 */
48062306a36Sopenharmony_ci	if (resume_needed) {
48162306a36Sopenharmony_ci		spin_unlock_irq(&ehci->lock);
48262306a36Sopenharmony_ci		msleep(USB_RESUME_TIMEOUT);
48362306a36Sopenharmony_ci		spin_lock_irq(&ehci->lock);
48462306a36Sopenharmony_ci		if (ehci->shutdown)
48562306a36Sopenharmony_ci			goto shutdown;
48662306a36Sopenharmony_ci	}
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci	i = HCS_N_PORTS (ehci->hcs_params);
48962306a36Sopenharmony_ci	while (i--) {
49062306a36Sopenharmony_ci		temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
49162306a36Sopenharmony_ci		if (test_bit(i, &resume_needed)) {
49262306a36Sopenharmony_ci			temp &= ~(PORT_RWC_BITS | PORT_SUSPEND | PORT_RESUME);
49362306a36Sopenharmony_ci			ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
49462306a36Sopenharmony_ci		}
49562306a36Sopenharmony_ci	}
49662306a36Sopenharmony_ci
49762306a36Sopenharmony_ci	ehci->next_statechange = jiffies + msecs_to_jiffies(5);
49862306a36Sopenharmony_ci	spin_unlock_irq(&ehci->lock);
49962306a36Sopenharmony_ci
50062306a36Sopenharmony_ci	ehci_handover_companion_ports(ehci);
50162306a36Sopenharmony_ci
50262306a36Sopenharmony_ci	/* Now we can safely re-enable irqs */
50362306a36Sopenharmony_ci	spin_lock_irq(&ehci->lock);
50462306a36Sopenharmony_ci	if (ehci->shutdown)
50562306a36Sopenharmony_ci		goto shutdown;
50662306a36Sopenharmony_ci	ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
50762306a36Sopenharmony_ci	(void) ehci_readl(ehci, &ehci->regs->intr_enable);
50862306a36Sopenharmony_ci	spin_unlock_irq(&ehci->lock);
50962306a36Sopenharmony_ci
51062306a36Sopenharmony_ci	return 0;
51162306a36Sopenharmony_ci
51262306a36Sopenharmony_ci shutdown:
51362306a36Sopenharmony_ci	spin_unlock_irq(&ehci->lock);
51462306a36Sopenharmony_ci	return -ESHUTDOWN;
51562306a36Sopenharmony_ci}
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_cistatic unsigned long ehci_get_resuming_ports(struct usb_hcd *hcd)
51862306a36Sopenharmony_ci{
51962306a36Sopenharmony_ci	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
52062306a36Sopenharmony_ci
52162306a36Sopenharmony_ci	return ehci->resuming_ports;
52262306a36Sopenharmony_ci}
52362306a36Sopenharmony_ci
52462306a36Sopenharmony_ci#else
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ci#define ehci_bus_suspend	NULL
52762306a36Sopenharmony_ci#define ehci_bus_resume		NULL
52862306a36Sopenharmony_ci#define ehci_get_resuming_ports	NULL
52962306a36Sopenharmony_ci
53062306a36Sopenharmony_ci#endif	/* CONFIG_PM */
53162306a36Sopenharmony_ci
53262306a36Sopenharmony_ci/*-------------------------------------------------------------------------*/
53362306a36Sopenharmony_ci
53462306a36Sopenharmony_ci/*
53562306a36Sopenharmony_ci * Sets the owner of a port
53662306a36Sopenharmony_ci */
53762306a36Sopenharmony_cistatic void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
53862306a36Sopenharmony_ci{
53962306a36Sopenharmony_ci	u32 __iomem		*status_reg;
54062306a36Sopenharmony_ci	u32			port_status;
54162306a36Sopenharmony_ci	int 			try;
54262306a36Sopenharmony_ci
54362306a36Sopenharmony_ci	status_reg = &ehci->regs->port_status[portnum];
54462306a36Sopenharmony_ci
54562306a36Sopenharmony_ci	/*
54662306a36Sopenharmony_ci	 * The controller won't set the OWNER bit if the port is
54762306a36Sopenharmony_ci	 * enabled, so this loop will sometimes require at least two
54862306a36Sopenharmony_ci	 * iterations: one to disable the port and one to set OWNER.
54962306a36Sopenharmony_ci	 */
55062306a36Sopenharmony_ci	for (try = 4; try > 0; --try) {
55162306a36Sopenharmony_ci		spin_lock_irq(&ehci->lock);
55262306a36Sopenharmony_ci		port_status = ehci_readl(ehci, status_reg);
55362306a36Sopenharmony_ci		if ((port_status & PORT_OWNER) == new_owner
55462306a36Sopenharmony_ci				|| (port_status & (PORT_OWNER | PORT_CONNECT))
55562306a36Sopenharmony_ci					== 0)
55662306a36Sopenharmony_ci			try = 0;
55762306a36Sopenharmony_ci		else {
55862306a36Sopenharmony_ci			port_status ^= PORT_OWNER;
55962306a36Sopenharmony_ci			port_status &= ~(PORT_PE | PORT_RWC_BITS);
56062306a36Sopenharmony_ci			ehci_writel(ehci, port_status, status_reg);
56162306a36Sopenharmony_ci		}
56262306a36Sopenharmony_ci		spin_unlock_irq(&ehci->lock);
56362306a36Sopenharmony_ci		if (try > 1)
56462306a36Sopenharmony_ci			msleep(5);
56562306a36Sopenharmony_ci	}
56662306a36Sopenharmony_ci}
56762306a36Sopenharmony_ci
56862306a36Sopenharmony_ci/*-------------------------------------------------------------------------*/
56962306a36Sopenharmony_ci
57062306a36Sopenharmony_cistatic int check_reset_complete (
57162306a36Sopenharmony_ci	struct ehci_hcd	*ehci,
57262306a36Sopenharmony_ci	int		index,
57362306a36Sopenharmony_ci	u32 __iomem	*status_reg,
57462306a36Sopenharmony_ci	int		port_status
57562306a36Sopenharmony_ci) {
57662306a36Sopenharmony_ci	if (!(port_status & PORT_CONNECT))
57762306a36Sopenharmony_ci		return port_status;
57862306a36Sopenharmony_ci
57962306a36Sopenharmony_ci	/* if reset finished and it's still not enabled -- handoff */
58062306a36Sopenharmony_ci	if (!(port_status & PORT_PE)) {
58162306a36Sopenharmony_ci
58262306a36Sopenharmony_ci		/* with integrated TT, there's nobody to hand it to! */
58362306a36Sopenharmony_ci		if (ehci_is_TDI(ehci)) {
58462306a36Sopenharmony_ci			ehci_dbg (ehci,
58562306a36Sopenharmony_ci				"Failed to enable port %d on root hub TT\n",
58662306a36Sopenharmony_ci				index+1);
58762306a36Sopenharmony_ci			return port_status;
58862306a36Sopenharmony_ci		}
58962306a36Sopenharmony_ci
59062306a36Sopenharmony_ci		ehci_dbg (ehci, "port %d full speed --> companion\n",
59162306a36Sopenharmony_ci			index + 1);
59262306a36Sopenharmony_ci
59362306a36Sopenharmony_ci		// what happens if HCS_N_CC(params) == 0 ?
59462306a36Sopenharmony_ci		port_status |= PORT_OWNER;
59562306a36Sopenharmony_ci		port_status &= ~PORT_RWC_BITS;
59662306a36Sopenharmony_ci		ehci_writel(ehci, port_status, status_reg);
59762306a36Sopenharmony_ci
59862306a36Sopenharmony_ci		/* ensure 440EPX ohci controller state is operational */
59962306a36Sopenharmony_ci		if (ehci->has_amcc_usb23)
60062306a36Sopenharmony_ci			set_ohci_hcfs(ehci, 1);
60162306a36Sopenharmony_ci	} else {
60262306a36Sopenharmony_ci		ehci_dbg(ehci, "port %d reset complete, port enabled\n",
60362306a36Sopenharmony_ci			index + 1);
60462306a36Sopenharmony_ci		/* ensure 440EPx ohci controller state is suspended */
60562306a36Sopenharmony_ci		if (ehci->has_amcc_usb23)
60662306a36Sopenharmony_ci			set_ohci_hcfs(ehci, 0);
60762306a36Sopenharmony_ci	}
60862306a36Sopenharmony_ci
60962306a36Sopenharmony_ci	return port_status;
61062306a36Sopenharmony_ci}
61162306a36Sopenharmony_ci
61262306a36Sopenharmony_ci/*-------------------------------------------------------------------------*/
61362306a36Sopenharmony_ci
61462306a36Sopenharmony_ci
61562306a36Sopenharmony_ci/* build "status change" packet (one or two bytes) from HC registers */
61662306a36Sopenharmony_ci
61762306a36Sopenharmony_cistatic int
61862306a36Sopenharmony_ciehci_hub_status_data (struct usb_hcd *hcd, char *buf)
61962306a36Sopenharmony_ci{
62062306a36Sopenharmony_ci	struct ehci_hcd	*ehci = hcd_to_ehci (hcd);
62162306a36Sopenharmony_ci	u32		temp, status;
62262306a36Sopenharmony_ci	u32		mask;
62362306a36Sopenharmony_ci	int		ports, i, retval = 1;
62462306a36Sopenharmony_ci	unsigned long	flags;
62562306a36Sopenharmony_ci	u32		ppcd = ~0;
62662306a36Sopenharmony_ci
62762306a36Sopenharmony_ci	/* init status to no-changes */
62862306a36Sopenharmony_ci	buf [0] = 0;
62962306a36Sopenharmony_ci	ports = HCS_N_PORTS (ehci->hcs_params);
63062306a36Sopenharmony_ci	if (ports > 7) {
63162306a36Sopenharmony_ci		buf [1] = 0;
63262306a36Sopenharmony_ci		retval++;
63362306a36Sopenharmony_ci	}
63462306a36Sopenharmony_ci
63562306a36Sopenharmony_ci	/* Inform the core about resumes-in-progress by returning
63662306a36Sopenharmony_ci	 * a non-zero value even if there are no status changes.
63762306a36Sopenharmony_ci	 */
63862306a36Sopenharmony_ci	status = ehci->resuming_ports;
63962306a36Sopenharmony_ci
64062306a36Sopenharmony_ci	/* Some boards (mostly VIA?) report bogus overcurrent indications,
64162306a36Sopenharmony_ci	 * causing massive log spam unless we completely ignore them.  It
64262306a36Sopenharmony_ci	 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
64362306a36Sopenharmony_ci	 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
64462306a36Sopenharmony_ci	 * PORT_POWER; that's surprising, but maybe within-spec.
64562306a36Sopenharmony_ci	 */
64662306a36Sopenharmony_ci	if (!ignore_oc && !ehci->spurious_oc)
64762306a36Sopenharmony_ci		mask = PORT_CSC | PORT_PEC | PORT_OCC;
64862306a36Sopenharmony_ci	else
64962306a36Sopenharmony_ci		mask = PORT_CSC | PORT_PEC;
65062306a36Sopenharmony_ci	// PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
65162306a36Sopenharmony_ci
65262306a36Sopenharmony_ci	/* no hub change reports (bit 0) for now (power, ...) */
65362306a36Sopenharmony_ci
65462306a36Sopenharmony_ci	/* port N changes (bit N)? */
65562306a36Sopenharmony_ci	spin_lock_irqsave (&ehci->lock, flags);
65662306a36Sopenharmony_ci
65762306a36Sopenharmony_ci	/* get per-port change detect bits */
65862306a36Sopenharmony_ci	if (ehci->has_ppcd)
65962306a36Sopenharmony_ci		ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
66062306a36Sopenharmony_ci
66162306a36Sopenharmony_ci	for (i = 0; i < ports; i++) {
66262306a36Sopenharmony_ci		/* leverage per-port change bits feature */
66362306a36Sopenharmony_ci		if (ppcd & (1 << i))
66462306a36Sopenharmony_ci			temp = ehci_readl(ehci, &ehci->regs->port_status[i]);
66562306a36Sopenharmony_ci		else
66662306a36Sopenharmony_ci			temp = 0;
66762306a36Sopenharmony_ci
66862306a36Sopenharmony_ci		/*
66962306a36Sopenharmony_ci		 * Return status information even for ports with OWNER set.
67062306a36Sopenharmony_ci		 * Otherwise hub_wq wouldn't see the disconnect event when a
67162306a36Sopenharmony_ci		 * high-speed device is switched over to the companion
67262306a36Sopenharmony_ci		 * controller by the user.
67362306a36Sopenharmony_ci		 */
67462306a36Sopenharmony_ci
67562306a36Sopenharmony_ci		if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
67662306a36Sopenharmony_ci				|| (ehci->reset_done[i] && time_after_eq(
67762306a36Sopenharmony_ci					jiffies, ehci->reset_done[i]))
67862306a36Sopenharmony_ci				|| ehci_has_ci_pec_bug(ehci, temp)) {
67962306a36Sopenharmony_ci			if (i < 7)
68062306a36Sopenharmony_ci			    buf [0] |= 1 << (i + 1);
68162306a36Sopenharmony_ci			else
68262306a36Sopenharmony_ci			    buf [1] |= 1 << (i - 7);
68362306a36Sopenharmony_ci			status = STS_PCD;
68462306a36Sopenharmony_ci		}
68562306a36Sopenharmony_ci	}
68662306a36Sopenharmony_ci
68762306a36Sopenharmony_ci	/* If a resume is in progress, make sure it can finish */
68862306a36Sopenharmony_ci	if (ehci->resuming_ports)
68962306a36Sopenharmony_ci		mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(25));
69062306a36Sopenharmony_ci
69162306a36Sopenharmony_ci	spin_unlock_irqrestore (&ehci->lock, flags);
69262306a36Sopenharmony_ci	return status ? retval : 0;
69362306a36Sopenharmony_ci}
69462306a36Sopenharmony_ci
69562306a36Sopenharmony_ci/*-------------------------------------------------------------------------*/
69662306a36Sopenharmony_ci
69762306a36Sopenharmony_cistatic void
69862306a36Sopenharmony_ciehci_hub_descriptor (
69962306a36Sopenharmony_ci	struct ehci_hcd			*ehci,
70062306a36Sopenharmony_ci	struct usb_hub_descriptor	*desc
70162306a36Sopenharmony_ci) {
70262306a36Sopenharmony_ci	int		ports = HCS_N_PORTS (ehci->hcs_params);
70362306a36Sopenharmony_ci	u16		temp;
70462306a36Sopenharmony_ci
70562306a36Sopenharmony_ci	desc->bDescriptorType = USB_DT_HUB;
70662306a36Sopenharmony_ci	desc->bPwrOn2PwrGood = 10;	/* ehci 1.0, 2.3.9 says 20ms max */
70762306a36Sopenharmony_ci	desc->bHubContrCurrent = 0;
70862306a36Sopenharmony_ci
70962306a36Sopenharmony_ci	desc->bNbrPorts = ports;
71062306a36Sopenharmony_ci	temp = 1 + (ports / 8);
71162306a36Sopenharmony_ci	desc->bDescLength = 7 + 2 * temp;
71262306a36Sopenharmony_ci
71362306a36Sopenharmony_ci	/* two bitmaps:  ports removable, and usb 1.0 legacy PortPwrCtrlMask */
71462306a36Sopenharmony_ci	memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
71562306a36Sopenharmony_ci	memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
71662306a36Sopenharmony_ci
71762306a36Sopenharmony_ci	temp = HUB_CHAR_INDV_PORT_OCPM;	/* per-port overcurrent reporting */
71862306a36Sopenharmony_ci	if (HCS_PPC (ehci->hcs_params))
71962306a36Sopenharmony_ci		temp |= HUB_CHAR_INDV_PORT_LPSM; /* per-port power control */
72062306a36Sopenharmony_ci	else
72162306a36Sopenharmony_ci		temp |= HUB_CHAR_NO_LPSM; /* no power switching */
72262306a36Sopenharmony_ci#if 0
72362306a36Sopenharmony_ci// re-enable when we support USB_PORT_FEAT_INDICATOR below.
72462306a36Sopenharmony_ci	if (HCS_INDICATOR (ehci->hcs_params))
72562306a36Sopenharmony_ci		temp |= HUB_CHAR_PORTIND; /* per-port indicators (LEDs) */
72662306a36Sopenharmony_ci#endif
72762306a36Sopenharmony_ci	desc->wHubCharacteristics = cpu_to_le16(temp);
72862306a36Sopenharmony_ci}
72962306a36Sopenharmony_ci
73062306a36Sopenharmony_ci/*-------------------------------------------------------------------------*/
73162306a36Sopenharmony_ci
73262306a36Sopenharmony_ciint ehci_hub_control(
73362306a36Sopenharmony_ci	struct usb_hcd	*hcd,
73462306a36Sopenharmony_ci	u16		typeReq,
73562306a36Sopenharmony_ci	u16		wValue,
73662306a36Sopenharmony_ci	u16		wIndex,
73762306a36Sopenharmony_ci	char		*buf,
73862306a36Sopenharmony_ci	u16		wLength
73962306a36Sopenharmony_ci) {
74062306a36Sopenharmony_ci	struct ehci_hcd	*ehci = hcd_to_ehci (hcd);
74162306a36Sopenharmony_ci	int		ports = HCS_N_PORTS (ehci->hcs_params);
74262306a36Sopenharmony_ci	u32 __iomem	*status_reg, *hostpc_reg;
74362306a36Sopenharmony_ci	u32		temp, temp1, status;
74462306a36Sopenharmony_ci	unsigned long	flags;
74562306a36Sopenharmony_ci	int		retval = 0;
74662306a36Sopenharmony_ci	unsigned	selector;
74762306a36Sopenharmony_ci
74862306a36Sopenharmony_ci	/*
74962306a36Sopenharmony_ci	 * Avoid out-of-bounds values while calculating the port index
75062306a36Sopenharmony_ci	 * from wIndex.  The compiler doesn't like pointers to invalid
75162306a36Sopenharmony_ci	 * addresses, even if they are never used.
75262306a36Sopenharmony_ci	 */
75362306a36Sopenharmony_ci	temp = (wIndex - 1) & 0xff;
75462306a36Sopenharmony_ci	if (temp >= HCS_N_PORTS_MAX)
75562306a36Sopenharmony_ci		temp = 0;
75662306a36Sopenharmony_ci	status_reg = &ehci->regs->port_status[temp];
75762306a36Sopenharmony_ci	hostpc_reg = &ehci->regs->hostpc[temp];
75862306a36Sopenharmony_ci
75962306a36Sopenharmony_ci	/*
76062306a36Sopenharmony_ci	 * FIXME:  support SetPortFeatures USB_PORT_FEAT_INDICATOR.
76162306a36Sopenharmony_ci	 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
76262306a36Sopenharmony_ci	 * (track current state ourselves) ... blink for diagnostics,
76362306a36Sopenharmony_ci	 * power, "this is the one", etc.  EHCI spec supports this.
76462306a36Sopenharmony_ci	 */
76562306a36Sopenharmony_ci
76662306a36Sopenharmony_ci	spin_lock_irqsave (&ehci->lock, flags);
76762306a36Sopenharmony_ci	switch (typeReq) {
76862306a36Sopenharmony_ci	case ClearHubFeature:
76962306a36Sopenharmony_ci		switch (wValue) {
77062306a36Sopenharmony_ci		case C_HUB_LOCAL_POWER:
77162306a36Sopenharmony_ci		case C_HUB_OVER_CURRENT:
77262306a36Sopenharmony_ci			/* no hub-wide feature/status flags */
77362306a36Sopenharmony_ci			break;
77462306a36Sopenharmony_ci		default:
77562306a36Sopenharmony_ci			goto error;
77662306a36Sopenharmony_ci		}
77762306a36Sopenharmony_ci		break;
77862306a36Sopenharmony_ci	case ClearPortFeature:
77962306a36Sopenharmony_ci		if (!wIndex || wIndex > ports)
78062306a36Sopenharmony_ci			goto error;
78162306a36Sopenharmony_ci		wIndex--;
78262306a36Sopenharmony_ci		temp = ehci_readl(ehci, status_reg);
78362306a36Sopenharmony_ci		temp &= ~PORT_RWC_BITS;
78462306a36Sopenharmony_ci
78562306a36Sopenharmony_ci		/*
78662306a36Sopenharmony_ci		 * Even if OWNER is set, so the port is owned by the
78762306a36Sopenharmony_ci		 * companion controller, hub_wq needs to be able to clear
78862306a36Sopenharmony_ci		 * the port-change status bits (especially
78962306a36Sopenharmony_ci		 * USB_PORT_STAT_C_CONNECTION).
79062306a36Sopenharmony_ci		 */
79162306a36Sopenharmony_ci
79262306a36Sopenharmony_ci		switch (wValue) {
79362306a36Sopenharmony_ci		case USB_PORT_FEAT_ENABLE:
79462306a36Sopenharmony_ci			ehci_writel(ehci, temp & ~PORT_PE, status_reg);
79562306a36Sopenharmony_ci			break;
79662306a36Sopenharmony_ci		case USB_PORT_FEAT_C_ENABLE:
79762306a36Sopenharmony_ci			ehci_writel(ehci, temp | PORT_PEC, status_reg);
79862306a36Sopenharmony_ci			break;
79962306a36Sopenharmony_ci		case USB_PORT_FEAT_SUSPEND:
80062306a36Sopenharmony_ci			if (temp & PORT_RESET)
80162306a36Sopenharmony_ci				goto error;
80262306a36Sopenharmony_ci			if (ehci->no_selective_suspend)
80362306a36Sopenharmony_ci				break;
80462306a36Sopenharmony_ci#ifdef CONFIG_USB_OTG
80562306a36Sopenharmony_ci			if ((hcd->self.otg_port == (wIndex + 1))
80662306a36Sopenharmony_ci			    && hcd->self.b_hnp_enable) {
80762306a36Sopenharmony_ci				otg_start_hnp(hcd->usb_phy->otg);
80862306a36Sopenharmony_ci				break;
80962306a36Sopenharmony_ci			}
81062306a36Sopenharmony_ci#endif
81162306a36Sopenharmony_ci			if (!(temp & PORT_SUSPEND))
81262306a36Sopenharmony_ci				break;
81362306a36Sopenharmony_ci			if ((temp & PORT_PE) == 0)
81462306a36Sopenharmony_ci				goto error;
81562306a36Sopenharmony_ci
81662306a36Sopenharmony_ci			/* clear phy low-power mode before resume */
81762306a36Sopenharmony_ci			if (ehci->has_tdi_phy_lpm) {
81862306a36Sopenharmony_ci				temp1 = ehci_readl(ehci, hostpc_reg);
81962306a36Sopenharmony_ci				ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
82062306a36Sopenharmony_ci						hostpc_reg);
82162306a36Sopenharmony_ci				spin_unlock_irqrestore(&ehci->lock, flags);
82262306a36Sopenharmony_ci				msleep(5);/* wait to leave low-power mode */
82362306a36Sopenharmony_ci				spin_lock_irqsave(&ehci->lock, flags);
82462306a36Sopenharmony_ci			}
82562306a36Sopenharmony_ci			/* resume signaling for 20 msec */
82662306a36Sopenharmony_ci			temp &= ~PORT_WAKE_BITS;
82762306a36Sopenharmony_ci			ehci_writel(ehci, temp | PORT_RESUME, status_reg);
82862306a36Sopenharmony_ci			ehci->reset_done[wIndex] = jiffies
82962306a36Sopenharmony_ci					+ msecs_to_jiffies(USB_RESUME_TIMEOUT);
83062306a36Sopenharmony_ci			set_bit(wIndex, &ehci->resuming_ports);
83162306a36Sopenharmony_ci			usb_hcd_start_port_resume(&hcd->self, wIndex);
83262306a36Sopenharmony_ci			break;
83362306a36Sopenharmony_ci		case USB_PORT_FEAT_C_SUSPEND:
83462306a36Sopenharmony_ci			clear_bit(wIndex, &ehci->port_c_suspend);
83562306a36Sopenharmony_ci			break;
83662306a36Sopenharmony_ci		case USB_PORT_FEAT_POWER:
83762306a36Sopenharmony_ci			if (HCS_PPC(ehci->hcs_params)) {
83862306a36Sopenharmony_ci				spin_unlock_irqrestore(&ehci->lock, flags);
83962306a36Sopenharmony_ci				ehci_port_power(ehci, wIndex, false);
84062306a36Sopenharmony_ci				spin_lock_irqsave(&ehci->lock, flags);
84162306a36Sopenharmony_ci			}
84262306a36Sopenharmony_ci			break;
84362306a36Sopenharmony_ci		case USB_PORT_FEAT_C_CONNECTION:
84462306a36Sopenharmony_ci			ehci_writel(ehci, temp | PORT_CSC, status_reg);
84562306a36Sopenharmony_ci			break;
84662306a36Sopenharmony_ci		case USB_PORT_FEAT_C_OVER_CURRENT:
84762306a36Sopenharmony_ci			ehci_writel(ehci, temp | PORT_OCC, status_reg);
84862306a36Sopenharmony_ci			break;
84962306a36Sopenharmony_ci		case USB_PORT_FEAT_C_RESET:
85062306a36Sopenharmony_ci			/* GetPortStatus clears reset */
85162306a36Sopenharmony_ci			break;
85262306a36Sopenharmony_ci		default:
85362306a36Sopenharmony_ci			goto error;
85462306a36Sopenharmony_ci		}
85562306a36Sopenharmony_ci		ehci_readl(ehci, &ehci->regs->command);	/* unblock posted write */
85662306a36Sopenharmony_ci		break;
85762306a36Sopenharmony_ci	case GetHubDescriptor:
85862306a36Sopenharmony_ci		ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
85962306a36Sopenharmony_ci			buf);
86062306a36Sopenharmony_ci		break;
86162306a36Sopenharmony_ci	case GetHubStatus:
86262306a36Sopenharmony_ci		/* no hub-wide feature/status flags */
86362306a36Sopenharmony_ci		memset (buf, 0, 4);
86462306a36Sopenharmony_ci		//cpu_to_le32s ((u32 *) buf);
86562306a36Sopenharmony_ci		break;
86662306a36Sopenharmony_ci	case GetPortStatus:
86762306a36Sopenharmony_ci		if (!wIndex || wIndex > ports)
86862306a36Sopenharmony_ci			goto error;
86962306a36Sopenharmony_ci		wIndex--;
87062306a36Sopenharmony_ci		status = 0;
87162306a36Sopenharmony_ci		temp = ehci_readl(ehci, status_reg);
87262306a36Sopenharmony_ci
87362306a36Sopenharmony_ci		// wPortChange bits
87462306a36Sopenharmony_ci		if (temp & PORT_CSC)
87562306a36Sopenharmony_ci			status |= USB_PORT_STAT_C_CONNECTION << 16;
87662306a36Sopenharmony_ci		if (temp & PORT_PEC)
87762306a36Sopenharmony_ci			status |= USB_PORT_STAT_C_ENABLE << 16;
87862306a36Sopenharmony_ci
87962306a36Sopenharmony_ci		if (ehci_has_ci_pec_bug(ehci, temp)) {
88062306a36Sopenharmony_ci			status |= USB_PORT_STAT_C_ENABLE << 16;
88162306a36Sopenharmony_ci			ehci_info(ehci,
88262306a36Sopenharmony_ci				"PE is cleared by HW port:%d PORTSC:%08x\n",
88362306a36Sopenharmony_ci				wIndex + 1, temp);
88462306a36Sopenharmony_ci		}
88562306a36Sopenharmony_ci
88662306a36Sopenharmony_ci		if ((temp & PORT_OCC) && (!ignore_oc && !ehci->spurious_oc)){
88762306a36Sopenharmony_ci			status |= USB_PORT_STAT_C_OVERCURRENT << 16;
88862306a36Sopenharmony_ci
88962306a36Sopenharmony_ci			/*
89062306a36Sopenharmony_ci			 * Hubs should disable port power on over-current.
89162306a36Sopenharmony_ci			 * However, not all EHCI implementations do this
89262306a36Sopenharmony_ci			 * automatically, even if they _do_ support per-port
89362306a36Sopenharmony_ci			 * power switching; they're allowed to just limit the
89462306a36Sopenharmony_ci			 * current.  hub_wq will turn the power back on.
89562306a36Sopenharmony_ci			 */
89662306a36Sopenharmony_ci			if (((temp & PORT_OC) || (ehci->need_oc_pp_cycle))
89762306a36Sopenharmony_ci					&& HCS_PPC(ehci->hcs_params)) {
89862306a36Sopenharmony_ci				spin_unlock_irqrestore(&ehci->lock, flags);
89962306a36Sopenharmony_ci				ehci_port_power(ehci, wIndex, false);
90062306a36Sopenharmony_ci				spin_lock_irqsave(&ehci->lock, flags);
90162306a36Sopenharmony_ci				temp = ehci_readl(ehci, status_reg);
90262306a36Sopenharmony_ci			}
90362306a36Sopenharmony_ci		}
90462306a36Sopenharmony_ci
90562306a36Sopenharmony_ci		/* no reset or resume pending */
90662306a36Sopenharmony_ci		if (!ehci->reset_done[wIndex]) {
90762306a36Sopenharmony_ci
90862306a36Sopenharmony_ci			/* Remote Wakeup received? */
90962306a36Sopenharmony_ci			if (temp & PORT_RESUME) {
91062306a36Sopenharmony_ci				/* resume signaling for 20 msec */
91162306a36Sopenharmony_ci				ehci->reset_done[wIndex] = jiffies
91262306a36Sopenharmony_ci						+ msecs_to_jiffies(20);
91362306a36Sopenharmony_ci				usb_hcd_start_port_resume(&hcd->self, wIndex);
91462306a36Sopenharmony_ci				set_bit(wIndex, &ehci->resuming_ports);
91562306a36Sopenharmony_ci				/* check the port again */
91662306a36Sopenharmony_ci				mod_timer(&ehci_to_hcd(ehci)->rh_timer,
91762306a36Sopenharmony_ci						ehci->reset_done[wIndex]);
91862306a36Sopenharmony_ci			}
91962306a36Sopenharmony_ci
92062306a36Sopenharmony_ci		/* reset or resume not yet complete */
92162306a36Sopenharmony_ci		} else if (!time_after_eq(jiffies, ehci->reset_done[wIndex])) {
92262306a36Sopenharmony_ci			;	/* wait until it is complete */
92362306a36Sopenharmony_ci
92462306a36Sopenharmony_ci		/* resume completed */
92562306a36Sopenharmony_ci		} else if (test_bit(wIndex, &ehci->resuming_ports)) {
92662306a36Sopenharmony_ci			clear_bit(wIndex, &ehci->suspended_ports);
92762306a36Sopenharmony_ci			set_bit(wIndex, &ehci->port_c_suspend);
92862306a36Sopenharmony_ci			ehci->reset_done[wIndex] = 0;
92962306a36Sopenharmony_ci			usb_hcd_end_port_resume(&hcd->self, wIndex);
93062306a36Sopenharmony_ci
93162306a36Sopenharmony_ci			/* stop resume signaling */
93262306a36Sopenharmony_ci			temp &= ~(PORT_RWC_BITS | PORT_SUSPEND | PORT_RESUME);
93362306a36Sopenharmony_ci			ehci_writel(ehci, temp, status_reg);
93462306a36Sopenharmony_ci			clear_bit(wIndex, &ehci->resuming_ports);
93562306a36Sopenharmony_ci			retval = ehci_handshake(ehci, status_reg,
93662306a36Sopenharmony_ci					PORT_RESUME, 0, 2000 /* 2msec */);
93762306a36Sopenharmony_ci			if (retval != 0) {
93862306a36Sopenharmony_ci				ehci_err(ehci, "port %d resume error %d\n",
93962306a36Sopenharmony_ci						wIndex + 1, retval);
94062306a36Sopenharmony_ci				goto error;
94162306a36Sopenharmony_ci			}
94262306a36Sopenharmony_ci			temp = ehci_readl(ehci, status_reg);
94362306a36Sopenharmony_ci
94462306a36Sopenharmony_ci		/* whoever resets must GetPortStatus to complete it!! */
94562306a36Sopenharmony_ci		} else {
94662306a36Sopenharmony_ci			status |= USB_PORT_STAT_C_RESET << 16;
94762306a36Sopenharmony_ci			ehci->reset_done [wIndex] = 0;
94862306a36Sopenharmony_ci
94962306a36Sopenharmony_ci			/* force reset to complete */
95062306a36Sopenharmony_ci			ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
95162306a36Sopenharmony_ci					status_reg);
95262306a36Sopenharmony_ci			/* REVISIT:  some hardware needs 550+ usec to clear
95362306a36Sopenharmony_ci			 * this bit; seems too long to spin routinely...
95462306a36Sopenharmony_ci			 */
95562306a36Sopenharmony_ci			retval = ehci_handshake(ehci, status_reg,
95662306a36Sopenharmony_ci					PORT_RESET, 0, 1000);
95762306a36Sopenharmony_ci			if (retval != 0) {
95862306a36Sopenharmony_ci				ehci_err (ehci, "port %d reset error %d\n",
95962306a36Sopenharmony_ci					wIndex + 1, retval);
96062306a36Sopenharmony_ci				goto error;
96162306a36Sopenharmony_ci			}
96262306a36Sopenharmony_ci
96362306a36Sopenharmony_ci			/* see what we found out */
96462306a36Sopenharmony_ci			temp = check_reset_complete (ehci, wIndex, status_reg,
96562306a36Sopenharmony_ci					ehci_readl(ehci, status_reg));
96662306a36Sopenharmony_ci		}
96762306a36Sopenharmony_ci
96862306a36Sopenharmony_ci		/* transfer dedicated ports to the companion hc */
96962306a36Sopenharmony_ci		if ((temp & PORT_CONNECT) &&
97062306a36Sopenharmony_ci				test_bit(wIndex, &ehci->companion_ports)) {
97162306a36Sopenharmony_ci			temp &= ~PORT_RWC_BITS;
97262306a36Sopenharmony_ci			temp |= PORT_OWNER;
97362306a36Sopenharmony_ci			ehci_writel(ehci, temp, status_reg);
97462306a36Sopenharmony_ci			ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
97562306a36Sopenharmony_ci			temp = ehci_readl(ehci, status_reg);
97662306a36Sopenharmony_ci		}
97762306a36Sopenharmony_ci
97862306a36Sopenharmony_ci		/*
97962306a36Sopenharmony_ci		 * Even if OWNER is set, there's no harm letting hub_wq
98062306a36Sopenharmony_ci		 * see the wPortStatus values (they should all be 0 except
98162306a36Sopenharmony_ci		 * for PORT_POWER anyway).
98262306a36Sopenharmony_ci		 */
98362306a36Sopenharmony_ci
98462306a36Sopenharmony_ci		if (temp & PORT_CONNECT) {
98562306a36Sopenharmony_ci			status |= USB_PORT_STAT_CONNECTION;
98662306a36Sopenharmony_ci			// status may be from integrated TT
98762306a36Sopenharmony_ci			if (ehci->has_hostpc) {
98862306a36Sopenharmony_ci				temp1 = ehci_readl(ehci, hostpc_reg);
98962306a36Sopenharmony_ci				status |= ehci_port_speed(ehci, temp1);
99062306a36Sopenharmony_ci			} else
99162306a36Sopenharmony_ci				status |= ehci_port_speed(ehci, temp);
99262306a36Sopenharmony_ci		}
99362306a36Sopenharmony_ci		if (temp & PORT_PE)
99462306a36Sopenharmony_ci			status |= USB_PORT_STAT_ENABLE;
99562306a36Sopenharmony_ci
99662306a36Sopenharmony_ci		/* maybe the port was unsuspended without our knowledge */
99762306a36Sopenharmony_ci		if (temp & (PORT_SUSPEND|PORT_RESUME)) {
99862306a36Sopenharmony_ci			status |= USB_PORT_STAT_SUSPEND;
99962306a36Sopenharmony_ci		} else if (test_bit(wIndex, &ehci->suspended_ports)) {
100062306a36Sopenharmony_ci			clear_bit(wIndex, &ehci->suspended_ports);
100162306a36Sopenharmony_ci			clear_bit(wIndex, &ehci->resuming_ports);
100262306a36Sopenharmony_ci			ehci->reset_done[wIndex] = 0;
100362306a36Sopenharmony_ci			if (temp & PORT_PE)
100462306a36Sopenharmony_ci				set_bit(wIndex, &ehci->port_c_suspend);
100562306a36Sopenharmony_ci			usb_hcd_end_port_resume(&hcd->self, wIndex);
100662306a36Sopenharmony_ci		}
100762306a36Sopenharmony_ci
100862306a36Sopenharmony_ci		if (temp & PORT_OC)
100962306a36Sopenharmony_ci			status |= USB_PORT_STAT_OVERCURRENT;
101062306a36Sopenharmony_ci		if (temp & PORT_RESET)
101162306a36Sopenharmony_ci			status |= USB_PORT_STAT_RESET;
101262306a36Sopenharmony_ci		if (temp & PORT_POWER)
101362306a36Sopenharmony_ci			status |= USB_PORT_STAT_POWER;
101462306a36Sopenharmony_ci		if (test_bit(wIndex, &ehci->port_c_suspend))
101562306a36Sopenharmony_ci			status |= USB_PORT_STAT_C_SUSPEND << 16;
101662306a36Sopenharmony_ci
101762306a36Sopenharmony_ci		if (status & ~0xffff)	/* only if wPortChange is interesting */
101862306a36Sopenharmony_ci			dbg_port(ehci, "GetStatus", wIndex + 1, temp);
101962306a36Sopenharmony_ci		put_unaligned_le32(status, buf);
102062306a36Sopenharmony_ci		break;
102162306a36Sopenharmony_ci	case SetHubFeature:
102262306a36Sopenharmony_ci		switch (wValue) {
102362306a36Sopenharmony_ci		case C_HUB_LOCAL_POWER:
102462306a36Sopenharmony_ci		case C_HUB_OVER_CURRENT:
102562306a36Sopenharmony_ci			/* no hub-wide feature/status flags */
102662306a36Sopenharmony_ci			break;
102762306a36Sopenharmony_ci		default:
102862306a36Sopenharmony_ci			goto error;
102962306a36Sopenharmony_ci		}
103062306a36Sopenharmony_ci		break;
103162306a36Sopenharmony_ci	case SetPortFeature:
103262306a36Sopenharmony_ci		selector = wIndex >> 8;
103362306a36Sopenharmony_ci		wIndex &= 0xff;
103462306a36Sopenharmony_ci		if (unlikely(ehci->debug)) {
103562306a36Sopenharmony_ci			/* If the debug port is active any port
103662306a36Sopenharmony_ci			 * feature requests should get denied */
103762306a36Sopenharmony_ci			if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
103862306a36Sopenharmony_ci			    (readl(&ehci->debug->control) & DBGP_ENABLED)) {
103962306a36Sopenharmony_ci				retval = -ENODEV;
104062306a36Sopenharmony_ci				goto error_exit;
104162306a36Sopenharmony_ci			}
104262306a36Sopenharmony_ci		}
104362306a36Sopenharmony_ci		if (!wIndex || wIndex > ports)
104462306a36Sopenharmony_ci			goto error;
104562306a36Sopenharmony_ci		wIndex--;
104662306a36Sopenharmony_ci		temp = ehci_readl(ehci, status_reg);
104762306a36Sopenharmony_ci		if (temp & PORT_OWNER)
104862306a36Sopenharmony_ci			break;
104962306a36Sopenharmony_ci
105062306a36Sopenharmony_ci		temp &= ~PORT_RWC_BITS;
105162306a36Sopenharmony_ci		switch (wValue) {
105262306a36Sopenharmony_ci		case USB_PORT_FEAT_SUSPEND:
105362306a36Sopenharmony_ci			if (ehci->no_selective_suspend)
105462306a36Sopenharmony_ci				break;
105562306a36Sopenharmony_ci			if ((temp & PORT_PE) == 0
105662306a36Sopenharmony_ci					|| (temp & PORT_RESET) != 0)
105762306a36Sopenharmony_ci				goto error;
105862306a36Sopenharmony_ci
105962306a36Sopenharmony_ci			/* After above check the port must be connected.
106062306a36Sopenharmony_ci			 * Set appropriate bit thus could put phy into low power
106162306a36Sopenharmony_ci			 * mode if we have tdi_phy_lpm feature
106262306a36Sopenharmony_ci			 */
106362306a36Sopenharmony_ci			temp &= ~PORT_WKCONN_E;
106462306a36Sopenharmony_ci			temp |= PORT_WKDISC_E | PORT_WKOC_E;
106562306a36Sopenharmony_ci			ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
106662306a36Sopenharmony_ci			if (ehci->has_tdi_phy_lpm) {
106762306a36Sopenharmony_ci				spin_unlock_irqrestore(&ehci->lock, flags);
106862306a36Sopenharmony_ci				msleep(5);/* 5ms for HCD enter low pwr mode */
106962306a36Sopenharmony_ci				spin_lock_irqsave(&ehci->lock, flags);
107062306a36Sopenharmony_ci				temp1 = ehci_readl(ehci, hostpc_reg);
107162306a36Sopenharmony_ci				ehci_writel(ehci, temp1 | HOSTPC_PHCD,
107262306a36Sopenharmony_ci					hostpc_reg);
107362306a36Sopenharmony_ci				temp1 = ehci_readl(ehci, hostpc_reg);
107462306a36Sopenharmony_ci				ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
107562306a36Sopenharmony_ci					wIndex, (temp1 & HOSTPC_PHCD) ?
107662306a36Sopenharmony_ci					"succeeded" : "failed");
107762306a36Sopenharmony_ci			}
107862306a36Sopenharmony_ci			if (ehci_has_fsl_susp_errata(ehci)) {
107962306a36Sopenharmony_ci				/* 10ms for HCD enter suspend */
108062306a36Sopenharmony_ci				spin_unlock_irqrestore(&ehci->lock, flags);
108162306a36Sopenharmony_ci				usleep_range(10000, 20000);
108262306a36Sopenharmony_ci				spin_lock_irqsave(&ehci->lock, flags);
108362306a36Sopenharmony_ci			}
108462306a36Sopenharmony_ci			set_bit(wIndex, &ehci->suspended_ports);
108562306a36Sopenharmony_ci			break;
108662306a36Sopenharmony_ci		case USB_PORT_FEAT_POWER:
108762306a36Sopenharmony_ci			if (HCS_PPC(ehci->hcs_params)) {
108862306a36Sopenharmony_ci				spin_unlock_irqrestore(&ehci->lock, flags);
108962306a36Sopenharmony_ci				ehci_port_power(ehci, wIndex, true);
109062306a36Sopenharmony_ci				spin_lock_irqsave(&ehci->lock, flags);
109162306a36Sopenharmony_ci			}
109262306a36Sopenharmony_ci			break;
109362306a36Sopenharmony_ci		case USB_PORT_FEAT_RESET:
109462306a36Sopenharmony_ci			if (temp & (PORT_SUSPEND|PORT_RESUME))
109562306a36Sopenharmony_ci				goto error;
109662306a36Sopenharmony_ci			/* line status bits may report this as low speed,
109762306a36Sopenharmony_ci			 * which can be fine if this root hub has a
109862306a36Sopenharmony_ci			 * transaction translator built in.
109962306a36Sopenharmony_ci			 */
110062306a36Sopenharmony_ci			if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
110162306a36Sopenharmony_ci					&& !ehci_is_TDI(ehci)
110262306a36Sopenharmony_ci					&& PORT_USB11 (temp)) {
110362306a36Sopenharmony_ci				ehci_dbg (ehci,
110462306a36Sopenharmony_ci					"port %d low speed --> companion\n",
110562306a36Sopenharmony_ci					wIndex + 1);
110662306a36Sopenharmony_ci				temp |= PORT_OWNER;
110762306a36Sopenharmony_ci			} else {
110862306a36Sopenharmony_ci				temp |= PORT_RESET;
110962306a36Sopenharmony_ci				temp &= ~PORT_PE;
111062306a36Sopenharmony_ci
111162306a36Sopenharmony_ci				/*
111262306a36Sopenharmony_ci				 * caller must wait, then call GetPortStatus
111362306a36Sopenharmony_ci				 * usb 2.0 spec says 50 ms resets on root
111462306a36Sopenharmony_ci				 */
111562306a36Sopenharmony_ci				ehci->reset_done [wIndex] = jiffies
111662306a36Sopenharmony_ci						+ msecs_to_jiffies (50);
111762306a36Sopenharmony_ci
111862306a36Sopenharmony_ci				/*
111962306a36Sopenharmony_ci				 * Force full-speed connect for FSL high-speed
112062306a36Sopenharmony_ci				 * erratum; disable HS Chirp by setting PFSC bit
112162306a36Sopenharmony_ci				 */
112262306a36Sopenharmony_ci				if (ehci_has_fsl_hs_errata(ehci))
112362306a36Sopenharmony_ci					temp |= (1 << PORTSC_FSL_PFSC);
112462306a36Sopenharmony_ci			}
112562306a36Sopenharmony_ci			ehci_writel(ehci, temp, status_reg);
112662306a36Sopenharmony_ci			break;
112762306a36Sopenharmony_ci
112862306a36Sopenharmony_ci		/* For downstream facing ports (these):  one hub port is put
112962306a36Sopenharmony_ci		 * into test mode according to USB2 11.24.2.13, then the hub
113062306a36Sopenharmony_ci		 * must be reset (which for root hub now means rmmod+modprobe,
113162306a36Sopenharmony_ci		 * or else system reboot).  See EHCI 2.3.9 and 4.14 for info
113262306a36Sopenharmony_ci		 * about the EHCI-specific stuff.
113362306a36Sopenharmony_ci		 */
113462306a36Sopenharmony_ci		case USB_PORT_FEAT_TEST:
113562306a36Sopenharmony_ci#ifdef CONFIG_USB_HCD_TEST_MODE
113662306a36Sopenharmony_ci			if (selector == EHSET_TEST_SINGLE_STEP_SET_FEATURE) {
113762306a36Sopenharmony_ci				spin_unlock_irqrestore(&ehci->lock, flags);
113862306a36Sopenharmony_ci				retval = ehset_single_step_set_feature(hcd,
113962306a36Sopenharmony_ci								wIndex + 1);
114062306a36Sopenharmony_ci				spin_lock_irqsave(&ehci->lock, flags);
114162306a36Sopenharmony_ci				break;
114262306a36Sopenharmony_ci			}
114362306a36Sopenharmony_ci#endif
114462306a36Sopenharmony_ci			if (!selector || selector > 5)
114562306a36Sopenharmony_ci				goto error;
114662306a36Sopenharmony_ci			spin_unlock_irqrestore(&ehci->lock, flags);
114762306a36Sopenharmony_ci			ehci_quiesce(ehci);
114862306a36Sopenharmony_ci			spin_lock_irqsave(&ehci->lock, flags);
114962306a36Sopenharmony_ci
115062306a36Sopenharmony_ci			/* Put all enabled ports into suspend */
115162306a36Sopenharmony_ci			while (ports--) {
115262306a36Sopenharmony_ci				u32 __iomem *sreg =
115362306a36Sopenharmony_ci						&ehci->regs->port_status[ports];
115462306a36Sopenharmony_ci
115562306a36Sopenharmony_ci				temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS;
115662306a36Sopenharmony_ci				if (temp & PORT_PE)
115762306a36Sopenharmony_ci					ehci_writel(ehci, temp | PORT_SUSPEND,
115862306a36Sopenharmony_ci							sreg);
115962306a36Sopenharmony_ci			}
116062306a36Sopenharmony_ci
116162306a36Sopenharmony_ci			spin_unlock_irqrestore(&ehci->lock, flags);
116262306a36Sopenharmony_ci			ehci_halt(ehci);
116362306a36Sopenharmony_ci			spin_lock_irqsave(&ehci->lock, flags);
116462306a36Sopenharmony_ci
116562306a36Sopenharmony_ci			temp = ehci_readl(ehci, status_reg);
116662306a36Sopenharmony_ci			temp |= selector << 16;
116762306a36Sopenharmony_ci			ehci_writel(ehci, temp, status_reg);
116862306a36Sopenharmony_ci			break;
116962306a36Sopenharmony_ci
117062306a36Sopenharmony_ci		default:
117162306a36Sopenharmony_ci			goto error;
117262306a36Sopenharmony_ci		}
117362306a36Sopenharmony_ci		ehci_readl(ehci, &ehci->regs->command);	/* unblock posted writes */
117462306a36Sopenharmony_ci		break;
117562306a36Sopenharmony_ci
117662306a36Sopenharmony_ci	default:
117762306a36Sopenharmony_cierror:
117862306a36Sopenharmony_ci		/* "stall" on error */
117962306a36Sopenharmony_ci		retval = -EPIPE;
118062306a36Sopenharmony_ci	}
118162306a36Sopenharmony_cierror_exit:
118262306a36Sopenharmony_ci	spin_unlock_irqrestore (&ehci->lock, flags);
118362306a36Sopenharmony_ci	return retval;
118462306a36Sopenharmony_ci}
118562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(ehci_hub_control);
118662306a36Sopenharmony_ci
118762306a36Sopenharmony_cistatic void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
118862306a36Sopenharmony_ci{
118962306a36Sopenharmony_ci	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
119062306a36Sopenharmony_ci
119162306a36Sopenharmony_ci	if (ehci_is_TDI(ehci))
119262306a36Sopenharmony_ci		return;
119362306a36Sopenharmony_ci	set_owner(ehci, --portnum, PORT_OWNER);
119462306a36Sopenharmony_ci}
119562306a36Sopenharmony_ci
119662306a36Sopenharmony_cistatic int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
119762306a36Sopenharmony_ci{
119862306a36Sopenharmony_ci	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
119962306a36Sopenharmony_ci	u32 __iomem		*reg;
120062306a36Sopenharmony_ci
120162306a36Sopenharmony_ci	if (ehci_is_TDI(ehci))
120262306a36Sopenharmony_ci		return 0;
120362306a36Sopenharmony_ci	reg = &ehci->regs->port_status[portnum - 1];
120462306a36Sopenharmony_ci	return ehci_readl(ehci, reg) & PORT_OWNER;
120562306a36Sopenharmony_ci}
120662306a36Sopenharmony_ci
120762306a36Sopenharmony_cistatic int ehci_port_power(struct ehci_hcd *ehci, int portnum, bool enable)
120862306a36Sopenharmony_ci{
120962306a36Sopenharmony_ci	struct usb_hcd *hcd = ehci_to_hcd(ehci);
121062306a36Sopenharmony_ci	u32 __iomem *status_reg = &ehci->regs->port_status[portnum];
121162306a36Sopenharmony_ci	u32 temp = ehci_readl(ehci, status_reg) & ~PORT_RWC_BITS;
121262306a36Sopenharmony_ci
121362306a36Sopenharmony_ci	if (enable)
121462306a36Sopenharmony_ci		ehci_writel(ehci, temp | PORT_POWER, status_reg);
121562306a36Sopenharmony_ci	else
121662306a36Sopenharmony_ci		ehci_writel(ehci, temp & ~PORT_POWER, status_reg);
121762306a36Sopenharmony_ci
121862306a36Sopenharmony_ci	if (hcd->driver->port_power)
121962306a36Sopenharmony_ci		hcd->driver->port_power(hcd, portnum, enable);
122062306a36Sopenharmony_ci
122162306a36Sopenharmony_ci	return 0;
122262306a36Sopenharmony_ci}
1223