1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2001-2004 by David Brownell
4 */
5
6/* this file is part of ehci-hcd.c */
7
8/*-------------------------------------------------------------------------*/
9
10/*
11 * EHCI Root Hub ... the nonsharable stuff
12 *
13 * Registers don't need cpu_to_le32, that happens transparently
14 */
15
16/*-------------------------------------------------------------------------*/
17
18#define	PORT_WAKE_BITS	(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
19
20#ifdef	CONFIG_PM
21
22static void unlink_empty_async_suspended(struct ehci_hcd *ehci);
23
24static int persist_enabled_on_companion(struct usb_device *udev, void *unused)
25{
26	return !udev->maxchild && udev->persist_enabled &&
27		udev->bus->root_hub->speed < USB_SPEED_HIGH;
28}
29
30/* After a power loss, ports that were owned by the companion must be
31 * reset so that the companion can still own them.
32 */
33static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
34{
35	u32 __iomem	*reg;
36	u32		status;
37	int		port;
38	__le32		buf;
39	struct usb_hcd	*hcd = ehci_to_hcd(ehci);
40
41	if (!ehci->owned_ports)
42		return;
43
44	/*
45	 * USB 1.1 devices are mostly HIDs, which don't need to persist across
46	 * suspends. If we ensure that none of our companion's devices have
47	 * persist_enabled (by looking through all USB 1.1 buses in the system),
48	 * we can skip this and avoid slowing resume down. Devices without
49	 * persist will just get reenumerated shortly after resume anyway.
50	 */
51	if (!usb_for_each_dev(NULL, persist_enabled_on_companion))
52		return;
53
54	/* Make sure the ports are powered */
55	port = HCS_N_PORTS(ehci->hcs_params);
56	while (port--) {
57		if (test_bit(port, &ehci->owned_ports)) {
58			reg = &ehci->regs->port_status[port];
59			status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
60			if (!(status & PORT_POWER))
61				ehci_port_power(ehci, port, true);
62		}
63	}
64
65	/* Give the connections some time to appear */
66	msleep(20);
67
68	spin_lock_irq(&ehci->lock);
69	port = HCS_N_PORTS(ehci->hcs_params);
70	while (port--) {
71		if (test_bit(port, &ehci->owned_ports)) {
72			reg = &ehci->regs->port_status[port];
73			status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
74
75			/* Port already owned by companion? */
76			if (status & PORT_OWNER)
77				clear_bit(port, &ehci->owned_ports);
78			else if (test_bit(port, &ehci->companion_ports))
79				ehci_writel(ehci, status & ~PORT_PE, reg);
80			else {
81				spin_unlock_irq(&ehci->lock);
82				ehci_hub_control(hcd, SetPortFeature,
83						USB_PORT_FEAT_RESET, port + 1,
84						NULL, 0);
85				spin_lock_irq(&ehci->lock);
86			}
87		}
88	}
89	spin_unlock_irq(&ehci->lock);
90
91	if (!ehci->owned_ports)
92		return;
93	msleep(90);		/* Wait for resets to complete */
94
95	spin_lock_irq(&ehci->lock);
96	port = HCS_N_PORTS(ehci->hcs_params);
97	while (port--) {
98		if (test_bit(port, &ehci->owned_ports)) {
99			spin_unlock_irq(&ehci->lock);
100			ehci_hub_control(hcd, GetPortStatus,
101					0, port + 1,
102					(char *) &buf, sizeof(buf));
103			spin_lock_irq(&ehci->lock);
104
105			/* The companion should now own the port,
106			 * but if something went wrong the port must not
107			 * remain enabled.
108			 */
109			reg = &ehci->regs->port_status[port];
110			status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
111			if (status & PORT_OWNER)
112				ehci_writel(ehci, status | PORT_CSC, reg);
113			else {
114				ehci_dbg(ehci, "failed handover port %d: %x\n",
115						port + 1, status);
116				ehci_writel(ehci, status & ~PORT_PE, reg);
117			}
118		}
119	}
120
121	ehci->owned_ports = 0;
122	spin_unlock_irq(&ehci->lock);
123}
124
125static int ehci_port_change(struct ehci_hcd *ehci)
126{
127	int i = HCS_N_PORTS(ehci->hcs_params);
128
129	/* First check if the controller indicates a change event */
130
131	if (ehci_readl(ehci, &ehci->regs->status) & STS_PCD)
132		return 1;
133
134	/*
135	 * Not all controllers appear to update this while going from D3 to D0,
136	 * so check the individual port status registers as well
137	 */
138
139	while (i--)
140		if (ehci_readl(ehci, &ehci->regs->port_status[i]) & PORT_CSC)
141			return 1;
142
143	return 0;
144}
145
146void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
147		bool suspending, bool do_wakeup)
148{
149	int		port;
150	u32		temp;
151
152	/* If remote wakeup is enabled for the root hub but disabled
153	 * for the controller, we must adjust all the port wakeup flags
154	 * when the controller is suspended or resumed.  In all other
155	 * cases they don't need to be changed.
156	 */
157	if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup)
158		return;
159
160	spin_lock_irq(&ehci->lock);
161
162	/* clear phy low-power mode before changing wakeup flags */
163	if (ehci->has_tdi_phy_lpm) {
164		port = HCS_N_PORTS(ehci->hcs_params);
165		while (port--) {
166			u32 __iomem	*hostpc_reg = &ehci->regs->hostpc[port];
167
168			temp = ehci_readl(ehci, hostpc_reg);
169			ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg);
170		}
171		spin_unlock_irq(&ehci->lock);
172		msleep(5);
173		spin_lock_irq(&ehci->lock);
174	}
175
176	port = HCS_N_PORTS(ehci->hcs_params);
177	while (port--) {
178		u32 __iomem	*reg = &ehci->regs->port_status[port];
179		u32		t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
180		u32		t2 = t1 & ~PORT_WAKE_BITS;
181
182		/* If we are suspending the controller, clear the flags.
183		 * If we are resuming the controller, set the wakeup flags.
184		 */
185		if (!suspending) {
186			if (t1 & PORT_CONNECT)
187				t2 |= PORT_WKOC_E | PORT_WKDISC_E;
188			else
189				t2 |= PORT_WKOC_E | PORT_WKCONN_E;
190		}
191		ehci_writel(ehci, t2, reg);
192	}
193
194	/* enter phy low-power mode again */
195	if (ehci->has_tdi_phy_lpm) {
196		port = HCS_N_PORTS(ehci->hcs_params);
197		while (port--) {
198			u32 __iomem	*hostpc_reg = &ehci->regs->hostpc[port];
199
200			temp = ehci_readl(ehci, hostpc_reg);
201			ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg);
202		}
203	}
204
205	/* Does the root hub have a port wakeup pending? */
206	if (!suspending && ehci_port_change(ehci))
207		usb_hcd_resume_root_hub(ehci_to_hcd(ehci));
208
209	spin_unlock_irq(&ehci->lock);
210}
211EXPORT_SYMBOL_GPL(ehci_adjust_port_wakeup_flags);
212
213static int ehci_bus_suspend (struct usb_hcd *hcd)
214{
215	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
216	int			port;
217	int			mask;
218	int			changed;
219	bool			fs_idle_delay;
220
221	ehci_dbg(ehci, "suspend root hub\n");
222
223	if (time_before (jiffies, ehci->next_statechange))
224		msleep(5);
225
226	/* stop the schedules */
227	ehci_quiesce(ehci);
228
229	spin_lock_irq (&ehci->lock);
230	if (ehci->rh_state < EHCI_RH_RUNNING)
231		goto done;
232
233	/* Once the controller is stopped, port resumes that are already
234	 * in progress won't complete.  Hence if remote wakeup is enabled
235	 * for the root hub and any ports are in the middle of a resume or
236	 * remote wakeup, we must fail the suspend.
237	 */
238	if (hcd->self.root_hub->do_remote_wakeup) {
239		if (ehci->resuming_ports) {
240			spin_unlock_irq(&ehci->lock);
241			ehci_dbg(ehci, "suspend failed because a port is resuming\n");
242			return -EBUSY;
243		}
244	}
245
246	/* Unlike other USB host controller types, EHCI doesn't have
247	 * any notion of "global" or bus-wide suspend.  The driver has
248	 * to manually suspend all the active unsuspended ports, and
249	 * then manually resume them in the bus_resume() routine.
250	 */
251	ehci->bus_suspended = 0;
252	ehci->owned_ports = 0;
253	changed = 0;
254	fs_idle_delay = false;
255	port = HCS_N_PORTS(ehci->hcs_params);
256	while (port--) {
257		u32 __iomem	*reg = &ehci->regs->port_status [port];
258		u32		t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
259		u32		t2 = t1 & ~PORT_WAKE_BITS;
260
261		/* keep track of which ports we suspend */
262		if (t1 & PORT_OWNER)
263			set_bit(port, &ehci->owned_ports);
264		else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
265			t2 |= PORT_SUSPEND;
266			set_bit(port, &ehci->bus_suspended);
267		}
268
269		/* enable remote wakeup on all ports, if told to do so */
270		if (hcd->self.root_hub->do_remote_wakeup) {
271			/* only enable appropriate wake bits, otherwise the
272			 * hardware can not go phy low power mode. If a race
273			 * condition happens here(connection change during bits
274			 * set), the port change detection will finally fix it.
275			 */
276			if (t1 & PORT_CONNECT)
277				t2 |= PORT_WKOC_E | PORT_WKDISC_E;
278			else
279				t2 |= PORT_WKOC_E | PORT_WKCONN_E;
280		}
281
282		if (t1 != t2) {
283			/*
284			 * On some controllers, Wake-On-Disconnect will
285			 * generate false wakeup signals until the bus
286			 * switches over to full-speed idle.  For their
287			 * sake, add a delay if we need one.
288			 */
289			if ((t2 & PORT_WKDISC_E) &&
290					ehci_port_speed(ehci, t2) ==
291						USB_PORT_STAT_HIGH_SPEED)
292				fs_idle_delay = true;
293			ehci_writel(ehci, t2, reg);
294			changed = 1;
295		}
296	}
297	spin_unlock_irq(&ehci->lock);
298
299	if (changed && ehci_has_fsl_susp_errata(ehci))
300		/*
301		 * Wait for at least 10 millisecondes to ensure the controller
302		 * enter the suspend status before initiating a port resume
303		 * using the Force Port Resume bit (Not-EHCI compatible).
304		 */
305		usleep_range(10000, 20000);
306
307	if ((changed && ehci->has_tdi_phy_lpm) || fs_idle_delay) {
308		/*
309		 * Wait for HCD to enter low-power mode or for the bus
310		 * to switch to full-speed idle.
311		 */
312		usleep_range(5000, 5500);
313	}
314
315	if (changed && ehci->has_tdi_phy_lpm) {
316		spin_lock_irq(&ehci->lock);
317		port = HCS_N_PORTS(ehci->hcs_params);
318		while (port--) {
319			u32 __iomem	*hostpc_reg = &ehci->regs->hostpc[port];
320			u32		t3;
321
322			t3 = ehci_readl(ehci, hostpc_reg);
323			ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
324			t3 = ehci_readl(ehci, hostpc_reg);
325			ehci_dbg(ehci, "Port %d phy low-power mode %s\n",
326					port, (t3 & HOSTPC_PHCD) ?
327					"succeeded" : "failed");
328		}
329		spin_unlock_irq(&ehci->lock);
330	}
331
332	/* Apparently some devices need a >= 1-uframe delay here */
333	if (ehci->bus_suspended)
334		udelay(150);
335
336	/* turn off now-idle HC */
337	ehci_halt (ehci);
338
339	spin_lock_irq(&ehci->lock);
340	if (ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_POLL_DEAD))
341		ehci_handle_controller_death(ehci);
342	if (ehci->rh_state != EHCI_RH_RUNNING)
343		goto done;
344	ehci->rh_state = EHCI_RH_SUSPENDED;
345
346	unlink_empty_async_suspended(ehci);
347
348	/* Some Synopsys controllers mistakenly leave IAA turned on */
349	ehci_writel(ehci, STS_IAA, &ehci->regs->status);
350
351	/* Any IAA cycle that started before the suspend is now invalid */
352	end_iaa_cycle(ehci);
353	ehci_handle_start_intr_unlinks(ehci);
354	ehci_handle_intr_unlinks(ehci);
355	end_free_itds(ehci);
356
357	/* allow remote wakeup */
358	mask = INTR_MASK;
359	if (!hcd->self.root_hub->do_remote_wakeup)
360		mask &= ~STS_PCD;
361	ehci_writel(ehci, mask, &ehci->regs->intr_enable);
362	ehci_readl(ehci, &ehci->regs->intr_enable);
363
364 done:
365	ehci->next_statechange = jiffies + msecs_to_jiffies(10);
366	ehci->enabled_hrtimer_events = 0;
367	ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
368	spin_unlock_irq (&ehci->lock);
369
370	hrtimer_cancel(&ehci->hrtimer);
371	return 0;
372}
373
374
375/* caller has locked the root hub, and should reset/reinit on error */
376static int ehci_bus_resume (struct usb_hcd *hcd)
377{
378	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
379	u32			temp;
380	u32			power_okay;
381	int			i;
382	unsigned long		resume_needed = 0;
383
384	if (time_before (jiffies, ehci->next_statechange))
385		msleep(5);
386	spin_lock_irq (&ehci->lock);
387	if (!HCD_HW_ACCESSIBLE(hcd) || ehci->shutdown)
388		goto shutdown;
389
390	if (unlikely(ehci->debug)) {
391		if (!dbgp_reset_prep(hcd))
392			ehci->debug = NULL;
393		else
394			dbgp_external_startup(hcd);
395	}
396
397	/* Ideally and we've got a real resume here, and no port's power
398	 * was lost.  (For PCI, that means Vaux was maintained.)  But we
399	 * could instead be restoring a swsusp snapshot -- so that BIOS was
400	 * the last user of the controller, not reset/pm hardware keeping
401	 * state we gave to it.
402	 */
403	power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
404	ehci_dbg(ehci, "resume root hub%s\n",
405			power_okay ? "" : " after power loss");
406
407	/* at least some APM implementations will try to deliver
408	 * IRQs right away, so delay them until we're ready.
409	 */
410	ehci_writel(ehci, 0, &ehci->regs->intr_enable);
411
412	/* re-init operational registers */
413	ehci_writel(ehci, 0, &ehci->regs->segment);
414	ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
415	ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
416
417	/* restore CMD_RUN, framelist size, and irq threshold */
418	ehci->command |= CMD_RUN;
419	ehci_writel(ehci, ehci->command, &ehci->regs->command);
420	ehci->rh_state = EHCI_RH_RUNNING;
421
422	/*
423	 * According to Bugzilla #8190, the port status for some controllers
424	 * will be wrong without a delay. At their wrong status, the port
425	 * is enabled, but not suspended neither resumed.
426	 */
427	i = HCS_N_PORTS(ehci->hcs_params);
428	while (i--) {
429		temp = ehci_readl(ehci, &ehci->regs->port_status[i]);
430		if ((temp & PORT_PE) &&
431				!(temp & (PORT_SUSPEND | PORT_RESUME))) {
432			ehci_dbg(ehci, "Port status(0x%x) is wrong\n", temp);
433			spin_unlock_irq(&ehci->lock);
434			msleep(8);
435			spin_lock_irq(&ehci->lock);
436			break;
437		}
438	}
439
440	if (ehci->shutdown)
441		goto shutdown;
442
443	/* clear phy low-power mode before resume */
444	if (ehci->bus_suspended && ehci->has_tdi_phy_lpm) {
445		i = HCS_N_PORTS(ehci->hcs_params);
446		while (i--) {
447			if (test_bit(i, &ehci->bus_suspended)) {
448				u32 __iomem	*hostpc_reg =
449							&ehci->regs->hostpc[i];
450
451				temp = ehci_readl(ehci, hostpc_reg);
452				ehci_writel(ehci, temp & ~HOSTPC_PHCD,
453						hostpc_reg);
454			}
455		}
456		spin_unlock_irq(&ehci->lock);
457		msleep(5);
458		spin_lock_irq(&ehci->lock);
459		if (ehci->shutdown)
460			goto shutdown;
461	}
462
463	/* manually resume the ports we suspended during bus_suspend() */
464	i = HCS_N_PORTS (ehci->hcs_params);
465	while (i--) {
466		temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
467		temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
468		if (test_bit(i, &ehci->bus_suspended) &&
469				(temp & PORT_SUSPEND)) {
470			temp |= PORT_RESUME;
471			set_bit(i, &resume_needed);
472		}
473		ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
474	}
475
476	/*
477	 * msleep for USB_RESUME_TIMEOUT ms only if code is trying to resume
478	 * port
479	 */
480	if (resume_needed) {
481		spin_unlock_irq(&ehci->lock);
482		msleep(USB_RESUME_TIMEOUT);
483		spin_lock_irq(&ehci->lock);
484		if (ehci->shutdown)
485			goto shutdown;
486	}
487
488	i = HCS_N_PORTS (ehci->hcs_params);
489	while (i--) {
490		temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
491		if (test_bit(i, &resume_needed)) {
492			temp &= ~(PORT_RWC_BITS | PORT_SUSPEND | PORT_RESUME);
493			ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
494		}
495	}
496
497	ehci->next_statechange = jiffies + msecs_to_jiffies(5);
498	spin_unlock_irq(&ehci->lock);
499
500	ehci_handover_companion_ports(ehci);
501
502	/* Now we can safely re-enable irqs */
503	spin_lock_irq(&ehci->lock);
504	if (ehci->shutdown)
505		goto shutdown;
506	ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
507	(void) ehci_readl(ehci, &ehci->regs->intr_enable);
508	spin_unlock_irq(&ehci->lock);
509
510	return 0;
511
512 shutdown:
513	spin_unlock_irq(&ehci->lock);
514	return -ESHUTDOWN;
515}
516
517static unsigned long ehci_get_resuming_ports(struct usb_hcd *hcd)
518{
519	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
520
521	return ehci->resuming_ports;
522}
523
524#else
525
526#define ehci_bus_suspend	NULL
527#define ehci_bus_resume		NULL
528#define ehci_get_resuming_ports	NULL
529
530#endif	/* CONFIG_PM */
531
532/*-------------------------------------------------------------------------*/
533
534/*
535 * Sets the owner of a port
536 */
537static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
538{
539	u32 __iomem		*status_reg;
540	u32			port_status;
541	int 			try;
542
543	status_reg = &ehci->regs->port_status[portnum];
544
545	/*
546	 * The controller won't set the OWNER bit if the port is
547	 * enabled, so this loop will sometimes require at least two
548	 * iterations: one to disable the port and one to set OWNER.
549	 */
550	for (try = 4; try > 0; --try) {
551		spin_lock_irq(&ehci->lock);
552		port_status = ehci_readl(ehci, status_reg);
553		if ((port_status & PORT_OWNER) == new_owner
554				|| (port_status & (PORT_OWNER | PORT_CONNECT))
555					== 0)
556			try = 0;
557		else {
558			port_status ^= PORT_OWNER;
559			port_status &= ~(PORT_PE | PORT_RWC_BITS);
560			ehci_writel(ehci, port_status, status_reg);
561		}
562		spin_unlock_irq(&ehci->lock);
563		if (try > 1)
564			msleep(5);
565	}
566}
567
568/*-------------------------------------------------------------------------*/
569
570static int check_reset_complete (
571	struct ehci_hcd	*ehci,
572	int		index,
573	u32 __iomem	*status_reg,
574	int		port_status
575) {
576	if (!(port_status & PORT_CONNECT))
577		return port_status;
578
579	/* if reset finished and it's still not enabled -- handoff */
580	if (!(port_status & PORT_PE)) {
581
582		/* with integrated TT, there's nobody to hand it to! */
583		if (ehci_is_TDI(ehci)) {
584			ehci_dbg (ehci,
585				"Failed to enable port %d on root hub TT\n",
586				index+1);
587			return port_status;
588		}
589
590		ehci_dbg (ehci, "port %d full speed --> companion\n",
591			index + 1);
592
593		// what happens if HCS_N_CC(params) == 0 ?
594		port_status |= PORT_OWNER;
595		port_status &= ~PORT_RWC_BITS;
596		ehci_writel(ehci, port_status, status_reg);
597
598		/* ensure 440EPX ohci controller state is operational */
599		if (ehci->has_amcc_usb23)
600			set_ohci_hcfs(ehci, 1);
601	} else {
602		ehci_dbg(ehci, "port %d reset complete, port enabled\n",
603			index + 1);
604		/* ensure 440EPx ohci controller state is suspended */
605		if (ehci->has_amcc_usb23)
606			set_ohci_hcfs(ehci, 0);
607	}
608
609	return port_status;
610}
611
612/*-------------------------------------------------------------------------*/
613
614
615/* build "status change" packet (one or two bytes) from HC registers */
616
617static int
618ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
619{
620	struct ehci_hcd	*ehci = hcd_to_ehci (hcd);
621	u32		temp, status;
622	u32		mask;
623	int		ports, i, retval = 1;
624	unsigned long	flags;
625	u32		ppcd = ~0;
626
627	/* init status to no-changes */
628	buf [0] = 0;
629	ports = HCS_N_PORTS (ehci->hcs_params);
630	if (ports > 7) {
631		buf [1] = 0;
632		retval++;
633	}
634
635	/* Inform the core about resumes-in-progress by returning
636	 * a non-zero value even if there are no status changes.
637	 */
638	status = ehci->resuming_ports;
639
640	/* Some boards (mostly VIA?) report bogus overcurrent indications,
641	 * causing massive log spam unless we completely ignore them.  It
642	 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
643	 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
644	 * PORT_POWER; that's surprising, but maybe within-spec.
645	 */
646	if (!ignore_oc && !ehci->spurious_oc)
647		mask = PORT_CSC | PORT_PEC | PORT_OCC;
648	else
649		mask = PORT_CSC | PORT_PEC;
650	// PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
651
652	/* no hub change reports (bit 0) for now (power, ...) */
653
654	/* port N changes (bit N)? */
655	spin_lock_irqsave (&ehci->lock, flags);
656
657	/* get per-port change detect bits */
658	if (ehci->has_ppcd)
659		ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
660
661	for (i = 0; i < ports; i++) {
662		/* leverage per-port change bits feature */
663		if (ppcd & (1 << i))
664			temp = ehci_readl(ehci, &ehci->regs->port_status[i]);
665		else
666			temp = 0;
667
668		/*
669		 * Return status information even for ports with OWNER set.
670		 * Otherwise hub_wq wouldn't see the disconnect event when a
671		 * high-speed device is switched over to the companion
672		 * controller by the user.
673		 */
674
675		if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
676				|| (ehci->reset_done[i] && time_after_eq(
677					jiffies, ehci->reset_done[i]))
678				|| ehci_has_ci_pec_bug(ehci, temp)) {
679			if (i < 7)
680			    buf [0] |= 1 << (i + 1);
681			else
682			    buf [1] |= 1 << (i - 7);
683			status = STS_PCD;
684		}
685	}
686
687	/* If a resume is in progress, make sure it can finish */
688	if (ehci->resuming_ports)
689		mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(25));
690
691	spin_unlock_irqrestore (&ehci->lock, flags);
692	return status ? retval : 0;
693}
694
695/*-------------------------------------------------------------------------*/
696
697static void
698ehci_hub_descriptor (
699	struct ehci_hcd			*ehci,
700	struct usb_hub_descriptor	*desc
701) {
702	int		ports = HCS_N_PORTS (ehci->hcs_params);
703	u16		temp;
704
705	desc->bDescriptorType = USB_DT_HUB;
706	desc->bPwrOn2PwrGood = 10;	/* ehci 1.0, 2.3.9 says 20ms max */
707	desc->bHubContrCurrent = 0;
708
709	desc->bNbrPorts = ports;
710	temp = 1 + (ports / 8);
711	desc->bDescLength = 7 + 2 * temp;
712
713	/* two bitmaps:  ports removable, and usb 1.0 legacy PortPwrCtrlMask */
714	memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
715	memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
716
717	temp = HUB_CHAR_INDV_PORT_OCPM;	/* per-port overcurrent reporting */
718	if (HCS_PPC (ehci->hcs_params))
719		temp |= HUB_CHAR_INDV_PORT_LPSM; /* per-port power control */
720	else
721		temp |= HUB_CHAR_NO_LPSM; /* no power switching */
722#if 0
723// re-enable when we support USB_PORT_FEAT_INDICATOR below.
724	if (HCS_INDICATOR (ehci->hcs_params))
725		temp |= HUB_CHAR_PORTIND; /* per-port indicators (LEDs) */
726#endif
727	desc->wHubCharacteristics = cpu_to_le16(temp);
728}
729
730/*-------------------------------------------------------------------------*/
731
732int ehci_hub_control(
733	struct usb_hcd	*hcd,
734	u16		typeReq,
735	u16		wValue,
736	u16		wIndex,
737	char		*buf,
738	u16		wLength
739) {
740	struct ehci_hcd	*ehci = hcd_to_ehci (hcd);
741	int		ports = HCS_N_PORTS (ehci->hcs_params);
742	u32 __iomem	*status_reg, *hostpc_reg;
743	u32		temp, temp1, status;
744	unsigned long	flags;
745	int		retval = 0;
746	unsigned	selector;
747
748	/*
749	 * Avoid out-of-bounds values while calculating the port index
750	 * from wIndex.  The compiler doesn't like pointers to invalid
751	 * addresses, even if they are never used.
752	 */
753	temp = (wIndex - 1) & 0xff;
754	if (temp >= HCS_N_PORTS_MAX)
755		temp = 0;
756	status_reg = &ehci->regs->port_status[temp];
757	hostpc_reg = &ehci->regs->hostpc[temp];
758
759	/*
760	 * FIXME:  support SetPortFeatures USB_PORT_FEAT_INDICATOR.
761	 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
762	 * (track current state ourselves) ... blink for diagnostics,
763	 * power, "this is the one", etc.  EHCI spec supports this.
764	 */
765
766	spin_lock_irqsave (&ehci->lock, flags);
767	switch (typeReq) {
768	case ClearHubFeature:
769		switch (wValue) {
770		case C_HUB_LOCAL_POWER:
771		case C_HUB_OVER_CURRENT:
772			/* no hub-wide feature/status flags */
773			break;
774		default:
775			goto error;
776		}
777		break;
778	case ClearPortFeature:
779		if (!wIndex || wIndex > ports)
780			goto error;
781		wIndex--;
782		temp = ehci_readl(ehci, status_reg);
783		temp &= ~PORT_RWC_BITS;
784
785		/*
786		 * Even if OWNER is set, so the port is owned by the
787		 * companion controller, hub_wq needs to be able to clear
788		 * the port-change status bits (especially
789		 * USB_PORT_STAT_C_CONNECTION).
790		 */
791
792		switch (wValue) {
793		case USB_PORT_FEAT_ENABLE:
794			ehci_writel(ehci, temp & ~PORT_PE, status_reg);
795			break;
796		case USB_PORT_FEAT_C_ENABLE:
797			ehci_writel(ehci, temp | PORT_PEC, status_reg);
798			break;
799		case USB_PORT_FEAT_SUSPEND:
800			if (temp & PORT_RESET)
801				goto error;
802			if (ehci->no_selective_suspend)
803				break;
804#ifdef CONFIG_USB_OTG
805			if ((hcd->self.otg_port == (wIndex + 1))
806			    && hcd->self.b_hnp_enable) {
807				otg_start_hnp(hcd->usb_phy->otg);
808				break;
809			}
810#endif
811			if (!(temp & PORT_SUSPEND))
812				break;
813			if ((temp & PORT_PE) == 0)
814				goto error;
815
816			/* clear phy low-power mode before resume */
817			if (ehci->has_tdi_phy_lpm) {
818				temp1 = ehci_readl(ehci, hostpc_reg);
819				ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
820						hostpc_reg);
821				spin_unlock_irqrestore(&ehci->lock, flags);
822				msleep(5);/* wait to leave low-power mode */
823				spin_lock_irqsave(&ehci->lock, flags);
824			}
825			/* resume signaling for 20 msec */
826			temp &= ~PORT_WAKE_BITS;
827			ehci_writel(ehci, temp | PORT_RESUME, status_reg);
828			ehci->reset_done[wIndex] = jiffies
829					+ msecs_to_jiffies(USB_RESUME_TIMEOUT);
830			set_bit(wIndex, &ehci->resuming_ports);
831			usb_hcd_start_port_resume(&hcd->self, wIndex);
832			break;
833		case USB_PORT_FEAT_C_SUSPEND:
834			clear_bit(wIndex, &ehci->port_c_suspend);
835			break;
836		case USB_PORT_FEAT_POWER:
837			if (HCS_PPC(ehci->hcs_params)) {
838				spin_unlock_irqrestore(&ehci->lock, flags);
839				ehci_port_power(ehci, wIndex, false);
840				spin_lock_irqsave(&ehci->lock, flags);
841			}
842			break;
843		case USB_PORT_FEAT_C_CONNECTION:
844			ehci_writel(ehci, temp | PORT_CSC, status_reg);
845			break;
846		case USB_PORT_FEAT_C_OVER_CURRENT:
847			ehci_writel(ehci, temp | PORT_OCC, status_reg);
848			break;
849		case USB_PORT_FEAT_C_RESET:
850			/* GetPortStatus clears reset */
851			break;
852		default:
853			goto error;
854		}
855		ehci_readl(ehci, &ehci->regs->command);	/* unblock posted write */
856		break;
857	case GetHubDescriptor:
858		ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
859			buf);
860		break;
861	case GetHubStatus:
862		/* no hub-wide feature/status flags */
863		memset (buf, 0, 4);
864		//cpu_to_le32s ((u32 *) buf);
865		break;
866	case GetPortStatus:
867		if (!wIndex || wIndex > ports)
868			goto error;
869		wIndex--;
870		status = 0;
871		temp = ehci_readl(ehci, status_reg);
872
873		// wPortChange bits
874		if (temp & PORT_CSC)
875			status |= USB_PORT_STAT_C_CONNECTION << 16;
876		if (temp & PORT_PEC)
877			status |= USB_PORT_STAT_C_ENABLE << 16;
878
879		if (ehci_has_ci_pec_bug(ehci, temp)) {
880			status |= USB_PORT_STAT_C_ENABLE << 16;
881			ehci_info(ehci,
882				"PE is cleared by HW port:%d PORTSC:%08x\n",
883				wIndex + 1, temp);
884		}
885
886		if ((temp & PORT_OCC) && (!ignore_oc && !ehci->spurious_oc)){
887			status |= USB_PORT_STAT_C_OVERCURRENT << 16;
888
889			/*
890			 * Hubs should disable port power on over-current.
891			 * However, not all EHCI implementations do this
892			 * automatically, even if they _do_ support per-port
893			 * power switching; they're allowed to just limit the
894			 * current.  hub_wq will turn the power back on.
895			 */
896			if (((temp & PORT_OC) || (ehci->need_oc_pp_cycle))
897					&& HCS_PPC(ehci->hcs_params)) {
898				spin_unlock_irqrestore(&ehci->lock, flags);
899				ehci_port_power(ehci, wIndex, false);
900				spin_lock_irqsave(&ehci->lock, flags);
901				temp = ehci_readl(ehci, status_reg);
902			}
903		}
904
905		/* no reset or resume pending */
906		if (!ehci->reset_done[wIndex]) {
907
908			/* Remote Wakeup received? */
909			if (temp & PORT_RESUME) {
910				/* resume signaling for 20 msec */
911				ehci->reset_done[wIndex] = jiffies
912						+ msecs_to_jiffies(20);
913				usb_hcd_start_port_resume(&hcd->self, wIndex);
914				set_bit(wIndex, &ehci->resuming_ports);
915				/* check the port again */
916				mod_timer(&ehci_to_hcd(ehci)->rh_timer,
917						ehci->reset_done[wIndex]);
918			}
919
920		/* reset or resume not yet complete */
921		} else if (!time_after_eq(jiffies, ehci->reset_done[wIndex])) {
922			;	/* wait until it is complete */
923
924		/* resume completed */
925		} else if (test_bit(wIndex, &ehci->resuming_ports)) {
926			clear_bit(wIndex, &ehci->suspended_ports);
927			set_bit(wIndex, &ehci->port_c_suspend);
928			ehci->reset_done[wIndex] = 0;
929			usb_hcd_end_port_resume(&hcd->self, wIndex);
930
931			/* stop resume signaling */
932			temp &= ~(PORT_RWC_BITS | PORT_SUSPEND | PORT_RESUME);
933			ehci_writel(ehci, temp, status_reg);
934			clear_bit(wIndex, &ehci->resuming_ports);
935			retval = ehci_handshake(ehci, status_reg,
936					PORT_RESUME, 0, 2000 /* 2msec */);
937			if (retval != 0) {
938				ehci_err(ehci, "port %d resume error %d\n",
939						wIndex + 1, retval);
940				goto error;
941			}
942			temp = ehci_readl(ehci, status_reg);
943
944		/* whoever resets must GetPortStatus to complete it!! */
945		} else {
946			status |= USB_PORT_STAT_C_RESET << 16;
947			ehci->reset_done [wIndex] = 0;
948
949			/* force reset to complete */
950			ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
951					status_reg);
952			/* REVISIT:  some hardware needs 550+ usec to clear
953			 * this bit; seems too long to spin routinely...
954			 */
955			retval = ehci_handshake(ehci, status_reg,
956					PORT_RESET, 0, 1000);
957			if (retval != 0) {
958				ehci_err (ehci, "port %d reset error %d\n",
959					wIndex + 1, retval);
960				goto error;
961			}
962
963			/* see what we found out */
964			temp = check_reset_complete (ehci, wIndex, status_reg,
965					ehci_readl(ehci, status_reg));
966		}
967
968		/* transfer dedicated ports to the companion hc */
969		if ((temp & PORT_CONNECT) &&
970				test_bit(wIndex, &ehci->companion_ports)) {
971			temp &= ~PORT_RWC_BITS;
972			temp |= PORT_OWNER;
973			ehci_writel(ehci, temp, status_reg);
974			ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
975			temp = ehci_readl(ehci, status_reg);
976		}
977
978		/*
979		 * Even if OWNER is set, there's no harm letting hub_wq
980		 * see the wPortStatus values (they should all be 0 except
981		 * for PORT_POWER anyway).
982		 */
983
984		if (temp & PORT_CONNECT) {
985			status |= USB_PORT_STAT_CONNECTION;
986			// status may be from integrated TT
987			if (ehci->has_hostpc) {
988				temp1 = ehci_readl(ehci, hostpc_reg);
989				status |= ehci_port_speed(ehci, temp1);
990			} else
991				status |= ehci_port_speed(ehci, temp);
992		}
993		if (temp & PORT_PE)
994			status |= USB_PORT_STAT_ENABLE;
995
996		/* maybe the port was unsuspended without our knowledge */
997		if (temp & (PORT_SUSPEND|PORT_RESUME)) {
998			status |= USB_PORT_STAT_SUSPEND;
999		} else if (test_bit(wIndex, &ehci->suspended_ports)) {
1000			clear_bit(wIndex, &ehci->suspended_ports);
1001			clear_bit(wIndex, &ehci->resuming_ports);
1002			ehci->reset_done[wIndex] = 0;
1003			if (temp & PORT_PE)
1004				set_bit(wIndex, &ehci->port_c_suspend);
1005			usb_hcd_end_port_resume(&hcd->self, wIndex);
1006		}
1007
1008		if (temp & PORT_OC)
1009			status |= USB_PORT_STAT_OVERCURRENT;
1010		if (temp & PORT_RESET)
1011			status |= USB_PORT_STAT_RESET;
1012		if (temp & PORT_POWER)
1013			status |= USB_PORT_STAT_POWER;
1014		if (test_bit(wIndex, &ehci->port_c_suspend))
1015			status |= USB_PORT_STAT_C_SUSPEND << 16;
1016
1017		if (status & ~0xffff)	/* only if wPortChange is interesting */
1018			dbg_port(ehci, "GetStatus", wIndex + 1, temp);
1019		put_unaligned_le32(status, buf);
1020		break;
1021	case SetHubFeature:
1022		switch (wValue) {
1023		case C_HUB_LOCAL_POWER:
1024		case C_HUB_OVER_CURRENT:
1025			/* no hub-wide feature/status flags */
1026			break;
1027		default:
1028			goto error;
1029		}
1030		break;
1031	case SetPortFeature:
1032		selector = wIndex >> 8;
1033		wIndex &= 0xff;
1034		if (unlikely(ehci->debug)) {
1035			/* If the debug port is active any port
1036			 * feature requests should get denied */
1037			if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
1038			    (readl(&ehci->debug->control) & DBGP_ENABLED)) {
1039				retval = -ENODEV;
1040				goto error_exit;
1041			}
1042		}
1043		if (!wIndex || wIndex > ports)
1044			goto error;
1045		wIndex--;
1046		temp = ehci_readl(ehci, status_reg);
1047		if (temp & PORT_OWNER)
1048			break;
1049
1050		temp &= ~PORT_RWC_BITS;
1051		switch (wValue) {
1052		case USB_PORT_FEAT_SUSPEND:
1053			if (ehci->no_selective_suspend)
1054				break;
1055			if ((temp & PORT_PE) == 0
1056					|| (temp & PORT_RESET) != 0)
1057				goto error;
1058
1059			/* After above check the port must be connected.
1060			 * Set appropriate bit thus could put phy into low power
1061			 * mode if we have tdi_phy_lpm feature
1062			 */
1063			temp &= ~PORT_WKCONN_E;
1064			temp |= PORT_WKDISC_E | PORT_WKOC_E;
1065			ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
1066			if (ehci->has_tdi_phy_lpm) {
1067				spin_unlock_irqrestore(&ehci->lock, flags);
1068				msleep(5);/* 5ms for HCD enter low pwr mode */
1069				spin_lock_irqsave(&ehci->lock, flags);
1070				temp1 = ehci_readl(ehci, hostpc_reg);
1071				ehci_writel(ehci, temp1 | HOSTPC_PHCD,
1072					hostpc_reg);
1073				temp1 = ehci_readl(ehci, hostpc_reg);
1074				ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
1075					wIndex, (temp1 & HOSTPC_PHCD) ?
1076					"succeeded" : "failed");
1077			}
1078			if (ehci_has_fsl_susp_errata(ehci)) {
1079				/* 10ms for HCD enter suspend */
1080				spin_unlock_irqrestore(&ehci->lock, flags);
1081				usleep_range(10000, 20000);
1082				spin_lock_irqsave(&ehci->lock, flags);
1083			}
1084			set_bit(wIndex, &ehci->suspended_ports);
1085			break;
1086		case USB_PORT_FEAT_POWER:
1087			if (HCS_PPC(ehci->hcs_params)) {
1088				spin_unlock_irqrestore(&ehci->lock, flags);
1089				ehci_port_power(ehci, wIndex, true);
1090				spin_lock_irqsave(&ehci->lock, flags);
1091			}
1092			break;
1093		case USB_PORT_FEAT_RESET:
1094			if (temp & (PORT_SUSPEND|PORT_RESUME))
1095				goto error;
1096			/* line status bits may report this as low speed,
1097			 * which can be fine if this root hub has a
1098			 * transaction translator built in.
1099			 */
1100			if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
1101					&& !ehci_is_TDI(ehci)
1102					&& PORT_USB11 (temp)) {
1103				ehci_dbg (ehci,
1104					"port %d low speed --> companion\n",
1105					wIndex + 1);
1106				temp |= PORT_OWNER;
1107			} else {
1108				temp |= PORT_RESET;
1109				temp &= ~PORT_PE;
1110
1111				/*
1112				 * caller must wait, then call GetPortStatus
1113				 * usb 2.0 spec says 50 ms resets on root
1114				 */
1115				ehci->reset_done [wIndex] = jiffies
1116						+ msecs_to_jiffies (50);
1117
1118				/*
1119				 * Force full-speed connect for FSL high-speed
1120				 * erratum; disable HS Chirp by setting PFSC bit
1121				 */
1122				if (ehci_has_fsl_hs_errata(ehci))
1123					temp |= (1 << PORTSC_FSL_PFSC);
1124			}
1125			ehci_writel(ehci, temp, status_reg);
1126			break;
1127
1128		/* For downstream facing ports (these):  one hub port is put
1129		 * into test mode according to USB2 11.24.2.13, then the hub
1130		 * must be reset (which for root hub now means rmmod+modprobe,
1131		 * or else system reboot).  See EHCI 2.3.9 and 4.14 for info
1132		 * about the EHCI-specific stuff.
1133		 */
1134		case USB_PORT_FEAT_TEST:
1135#ifdef CONFIG_USB_HCD_TEST_MODE
1136			if (selector == EHSET_TEST_SINGLE_STEP_SET_FEATURE) {
1137				spin_unlock_irqrestore(&ehci->lock, flags);
1138				retval = ehset_single_step_set_feature(hcd,
1139								wIndex + 1);
1140				spin_lock_irqsave(&ehci->lock, flags);
1141				break;
1142			}
1143#endif
1144			if (!selector || selector > 5)
1145				goto error;
1146			spin_unlock_irqrestore(&ehci->lock, flags);
1147			ehci_quiesce(ehci);
1148			spin_lock_irqsave(&ehci->lock, flags);
1149
1150			/* Put all enabled ports into suspend */
1151			while (ports--) {
1152				u32 __iomem *sreg =
1153						&ehci->regs->port_status[ports];
1154
1155				temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS;
1156				if (temp & PORT_PE)
1157					ehci_writel(ehci, temp | PORT_SUSPEND,
1158							sreg);
1159			}
1160
1161			spin_unlock_irqrestore(&ehci->lock, flags);
1162			ehci_halt(ehci);
1163			spin_lock_irqsave(&ehci->lock, flags);
1164
1165			temp = ehci_readl(ehci, status_reg);
1166			temp |= selector << 16;
1167			ehci_writel(ehci, temp, status_reg);
1168			break;
1169
1170		default:
1171			goto error;
1172		}
1173		ehci_readl(ehci, &ehci->regs->command);	/* unblock posted writes */
1174		break;
1175
1176	default:
1177error:
1178		/* "stall" on error */
1179		retval = -EPIPE;
1180	}
1181error_exit:
1182	spin_unlock_irqrestore (&ehci->lock, flags);
1183	return retval;
1184}
1185EXPORT_SYMBOL_GPL(ehci_hub_control);
1186
1187static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
1188{
1189	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
1190
1191	if (ehci_is_TDI(ehci))
1192		return;
1193	set_owner(ehci, --portnum, PORT_OWNER);
1194}
1195
1196static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
1197{
1198	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
1199	u32 __iomem		*reg;
1200
1201	if (ehci_is_TDI(ehci))
1202		return 0;
1203	reg = &ehci->regs->port_status[portnum - 1];
1204	return ehci_readl(ehci, reg) & PORT_OWNER;
1205}
1206
1207static int ehci_port_power(struct ehci_hcd *ehci, int portnum, bool enable)
1208{
1209	struct usb_hcd *hcd = ehci_to_hcd(ehci);
1210	u32 __iomem *status_reg = &ehci->regs->port_status[portnum];
1211	u32 temp = ehci_readl(ehci, status_reg) & ~PORT_RWC_BITS;
1212
1213	if (enable)
1214		ehci_writel(ehci, temp | PORT_POWER, status_reg);
1215	else
1216		ehci_writel(ehci, temp & ~PORT_POWER, status_reg);
1217
1218	if (hcd->driver->port_power)
1219		hcd->driver->port_power(hcd, portnum, enable);
1220
1221	return 0;
1222}
1223