xref: /OK3568_Linux_fs/kernel/drivers/usb/host/ehci-timer.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2012 by Alan Stern
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun /* This file is part of ehci-hcd.c */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun /* Set a bit in the USBCMD register */
ehci_set_command_bit(struct ehci_hcd * ehci,u32 bit)11*4882a593Smuzhiyun static void ehci_set_command_bit(struct ehci_hcd *ehci, u32 bit)
12*4882a593Smuzhiyun {
13*4882a593Smuzhiyun 	ehci->command |= bit;
14*4882a593Smuzhiyun 	ehci_writel(ehci, ehci->command, &ehci->regs->command);
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun 	/* unblock posted write */
17*4882a593Smuzhiyun 	ehci_readl(ehci, &ehci->regs->command);
18*4882a593Smuzhiyun }
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun /* Clear a bit in the USBCMD register */
ehci_clear_command_bit(struct ehci_hcd * ehci,u32 bit)21*4882a593Smuzhiyun static void ehci_clear_command_bit(struct ehci_hcd *ehci, u32 bit)
22*4882a593Smuzhiyun {
23*4882a593Smuzhiyun 	ehci->command &= ~bit;
24*4882a593Smuzhiyun 	ehci_writel(ehci, ehci->command, &ehci->regs->command);
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun 	/* unblock posted write */
27*4882a593Smuzhiyun 	ehci_readl(ehci, &ehci->regs->command);
28*4882a593Smuzhiyun }
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun  * EHCI timer support...  Now using hrtimers.
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * Lots of different events are triggered from ehci->hrtimer.  Whenever
36*4882a593Smuzhiyun  * the timer routine runs, it checks each possible event; events that are
37*4882a593Smuzhiyun  * currently enabled and whose expiration time has passed get handled.
38*4882a593Smuzhiyun  * The set of enabled events is stored as a collection of bitflags in
39*4882a593Smuzhiyun  * ehci->enabled_hrtimer_events, and they are numbered in order of
40*4882a593Smuzhiyun  * increasing delay values (ranging between 1 ms and 100 ms).
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  * Rather than implementing a sorted list or tree of all pending events,
43*4882a593Smuzhiyun  * we keep track only of the lowest-numbered pending event, in
44*4882a593Smuzhiyun  * ehci->next_hrtimer_event.  Whenever ehci->hrtimer gets restarted, its
45*4882a593Smuzhiyun  * expiration time is set to the timeout value for this event.
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * As a result, events might not get handled right away; the actual delay
48*4882a593Smuzhiyun  * could be anywhere up to twice the requested delay.  This doesn't
49*4882a593Smuzhiyun  * matter, because none of the events are especially time-critical.  The
50*4882a593Smuzhiyun  * ones that matter most all have a delay of 1 ms, so they will be
51*4882a593Smuzhiyun  * handled after 2 ms at most, which is okay.  In addition to this, we
52*4882a593Smuzhiyun  * allow for an expiration range of 1 ms.
53*4882a593Smuzhiyun  */
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /*
56*4882a593Smuzhiyun  * Delay lengths for the hrtimer event types.
57*4882a593Smuzhiyun  * Keep this list sorted by delay length, in the same order as
58*4882a593Smuzhiyun  * the event types indexed by enum ehci_hrtimer_event in ehci.h.
59*4882a593Smuzhiyun  */
60*4882a593Smuzhiyun static unsigned event_delays_ns[] = {
61*4882a593Smuzhiyun 	1 * NSEC_PER_MSEC,	/* EHCI_HRTIMER_POLL_ASS */
62*4882a593Smuzhiyun 	1 * NSEC_PER_MSEC,	/* EHCI_HRTIMER_POLL_PSS */
63*4882a593Smuzhiyun 	1 * NSEC_PER_MSEC,	/* EHCI_HRTIMER_POLL_DEAD */
64*4882a593Smuzhiyun 	1125 * NSEC_PER_USEC,	/* EHCI_HRTIMER_UNLINK_INTR */
65*4882a593Smuzhiyun 	2 * NSEC_PER_MSEC,	/* EHCI_HRTIMER_FREE_ITDS */
66*4882a593Smuzhiyun 	2 * NSEC_PER_MSEC,	/* EHCI_HRTIMER_ACTIVE_UNLINK */
67*4882a593Smuzhiyun 	5 * NSEC_PER_MSEC,	/* EHCI_HRTIMER_START_UNLINK_INTR */
68*4882a593Smuzhiyun 	6 * NSEC_PER_MSEC,	/* EHCI_HRTIMER_ASYNC_UNLINKS */
69*4882a593Smuzhiyun 	10 * NSEC_PER_MSEC,	/* EHCI_HRTIMER_IAA_WATCHDOG */
70*4882a593Smuzhiyun 	10 * NSEC_PER_MSEC,	/* EHCI_HRTIMER_DISABLE_PERIODIC */
71*4882a593Smuzhiyun 	15 * NSEC_PER_MSEC,	/* EHCI_HRTIMER_DISABLE_ASYNC */
72*4882a593Smuzhiyun 	100 * NSEC_PER_MSEC,	/* EHCI_HRTIMER_IO_WATCHDOG */
73*4882a593Smuzhiyun };
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun /* Enable a pending hrtimer event */
ehci_enable_event(struct ehci_hcd * ehci,unsigned event,bool resched)76*4882a593Smuzhiyun static void ehci_enable_event(struct ehci_hcd *ehci, unsigned event,
77*4882a593Smuzhiyun 		bool resched)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	ktime_t		*timeout = &ehci->hr_timeouts[event];
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	if (resched)
82*4882a593Smuzhiyun 		*timeout = ktime_add(ktime_get(), event_delays_ns[event]);
83*4882a593Smuzhiyun 	ehci->enabled_hrtimer_events |= (1 << event);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	/* Track only the lowest-numbered pending event */
86*4882a593Smuzhiyun 	if (event < ehci->next_hrtimer_event) {
87*4882a593Smuzhiyun 		ehci->next_hrtimer_event = event;
88*4882a593Smuzhiyun 		hrtimer_start_range_ns(&ehci->hrtimer, *timeout,
89*4882a593Smuzhiyun 				NSEC_PER_MSEC, HRTIMER_MODE_ABS);
90*4882a593Smuzhiyun 	}
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
ehci_poll_ASS(struct ehci_hcd * ehci)95*4882a593Smuzhiyun static void ehci_poll_ASS(struct ehci_hcd *ehci)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	unsigned	actual, want;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	/* Don't enable anything if the controller isn't running (e.g., died) */
100*4882a593Smuzhiyun 	if (ehci->rh_state != EHCI_RH_RUNNING)
101*4882a593Smuzhiyun 		return;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	want = (ehci->command & CMD_ASE) ? STS_ASS : 0;
104*4882a593Smuzhiyun 	actual = ehci_readl(ehci, &ehci->regs->status) & STS_ASS;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	if (want != actual) {
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 		/* Poll again later, but give up after about 2-4 ms */
109*4882a593Smuzhiyun 		if (ehci->ASS_poll_count++ < 2) {
110*4882a593Smuzhiyun 			ehci_enable_event(ehci, EHCI_HRTIMER_POLL_ASS, true);
111*4882a593Smuzhiyun 			return;
112*4882a593Smuzhiyun 		}
113*4882a593Smuzhiyun 		ehci_dbg(ehci, "Waited too long for the async schedule status (%x/%x), giving up\n",
114*4882a593Smuzhiyun 				want, actual);
115*4882a593Smuzhiyun 	}
116*4882a593Smuzhiyun 	ehci->ASS_poll_count = 0;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	/* The status is up-to-date; restart or stop the schedule as needed */
119*4882a593Smuzhiyun 	if (want == 0) {	/* Stopped */
120*4882a593Smuzhiyun 		if (ehci->async_count > 0)
121*4882a593Smuzhiyun 			ehci_set_command_bit(ehci, CMD_ASE);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	} else {		/* Running */
124*4882a593Smuzhiyun 		if (ehci->async_count == 0) {
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 			/* Turn off the schedule after a while */
127*4882a593Smuzhiyun 			ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_ASYNC,
128*4882a593Smuzhiyun 					true);
129*4882a593Smuzhiyun 		}
130*4882a593Smuzhiyun 	}
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun /* Turn off the async schedule after a brief delay */
ehci_disable_ASE(struct ehci_hcd * ehci)134*4882a593Smuzhiyun static void ehci_disable_ASE(struct ehci_hcd *ehci)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	ehci_clear_command_bit(ehci, CMD_ASE);
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun /* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
ehci_poll_PSS(struct ehci_hcd * ehci)141*4882a593Smuzhiyun static void ehci_poll_PSS(struct ehci_hcd *ehci)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	unsigned	actual, want;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	/* Don't do anything if the controller isn't running (e.g., died) */
146*4882a593Smuzhiyun 	if (ehci->rh_state != EHCI_RH_RUNNING)
147*4882a593Smuzhiyun 		return;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	want = (ehci->command & CMD_PSE) ? STS_PSS : 0;
150*4882a593Smuzhiyun 	actual = ehci_readl(ehci, &ehci->regs->status) & STS_PSS;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	if (want != actual) {
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 		/* Poll again later, but give up after about 2-4 ms */
155*4882a593Smuzhiyun 		if (ehci->PSS_poll_count++ < 2) {
156*4882a593Smuzhiyun 			ehci_enable_event(ehci, EHCI_HRTIMER_POLL_PSS, true);
157*4882a593Smuzhiyun 			return;
158*4882a593Smuzhiyun 		}
159*4882a593Smuzhiyun 		ehci_dbg(ehci, "Waited too long for the periodic schedule status (%x/%x), giving up\n",
160*4882a593Smuzhiyun 				want, actual);
161*4882a593Smuzhiyun 	}
162*4882a593Smuzhiyun 	ehci->PSS_poll_count = 0;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	/* The status is up-to-date; restart or stop the schedule as needed */
165*4882a593Smuzhiyun 	if (want == 0) {	/* Stopped */
166*4882a593Smuzhiyun 		if (ehci->periodic_count > 0)
167*4882a593Smuzhiyun 			ehci_set_command_bit(ehci, CMD_PSE);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	} else {		/* Running */
170*4882a593Smuzhiyun 		if (ehci->periodic_count == 0) {
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 			/* Turn off the schedule after a while */
173*4882a593Smuzhiyun 			ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_PERIODIC,
174*4882a593Smuzhiyun 					true);
175*4882a593Smuzhiyun 		}
176*4882a593Smuzhiyun 	}
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun /* Turn off the periodic schedule after a brief delay */
ehci_disable_PSE(struct ehci_hcd * ehci)180*4882a593Smuzhiyun static void ehci_disable_PSE(struct ehci_hcd *ehci)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun 	ehci_clear_command_bit(ehci, CMD_PSE);
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun /* Poll the STS_HALT status bit; see when a dead controller stops */
ehci_handle_controller_death(struct ehci_hcd * ehci)187*4882a593Smuzhiyun static void ehci_handle_controller_death(struct ehci_hcd *ehci)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun 	if (!(ehci_readl(ehci, &ehci->regs->status) & STS_HALT)) {
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 		/* Give up after a few milliseconds */
192*4882a593Smuzhiyun 		if (ehci->died_poll_count++ < 5) {
193*4882a593Smuzhiyun 			/* Try again later */
194*4882a593Smuzhiyun 			ehci_enable_event(ehci, EHCI_HRTIMER_POLL_DEAD, true);
195*4882a593Smuzhiyun 			return;
196*4882a593Smuzhiyun 		}
197*4882a593Smuzhiyun 		ehci_warn(ehci, "Waited too long for the controller to stop, giving up\n");
198*4882a593Smuzhiyun 	}
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	/* Clean up the mess */
201*4882a593Smuzhiyun 	ehci->rh_state = EHCI_RH_HALTED;
202*4882a593Smuzhiyun 	ehci_writel(ehci, 0, &ehci->regs->configured_flag);
203*4882a593Smuzhiyun 	ehci_writel(ehci, 0, &ehci->regs->intr_enable);
204*4882a593Smuzhiyun 	ehci_work(ehci);
205*4882a593Smuzhiyun 	end_unlink_async(ehci);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	/* Not in process context, so don't try to reset the controller */
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun /* start to unlink interrupt QHs  */
ehci_handle_start_intr_unlinks(struct ehci_hcd * ehci)211*4882a593Smuzhiyun static void ehci_handle_start_intr_unlinks(struct ehci_hcd *ehci)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun 	bool		stopped = (ehci->rh_state < EHCI_RH_RUNNING);
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	/*
216*4882a593Smuzhiyun 	 * Process all the QHs on the intr_unlink list that were added
217*4882a593Smuzhiyun 	 * before the current unlink cycle began.  The list is in
218*4882a593Smuzhiyun 	 * temporal order, so stop when we reach the first entry in the
219*4882a593Smuzhiyun 	 * current cycle.  But if the root hub isn't running then
220*4882a593Smuzhiyun 	 * process all the QHs on the list.
221*4882a593Smuzhiyun 	 */
222*4882a593Smuzhiyun 	while (!list_empty(&ehci->intr_unlink_wait)) {
223*4882a593Smuzhiyun 		struct ehci_qh	*qh;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 		qh = list_first_entry(&ehci->intr_unlink_wait,
226*4882a593Smuzhiyun 				struct ehci_qh, unlink_node);
227*4882a593Smuzhiyun 		if (!stopped && (qh->unlink_cycle ==
228*4882a593Smuzhiyun 				ehci->intr_unlink_wait_cycle))
229*4882a593Smuzhiyun 			break;
230*4882a593Smuzhiyun 		list_del_init(&qh->unlink_node);
231*4882a593Smuzhiyun 		qh->unlink_reason |= QH_UNLINK_QUEUE_EMPTY;
232*4882a593Smuzhiyun 		start_unlink_intr(ehci, qh);
233*4882a593Smuzhiyun 	}
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	/* Handle remaining entries later */
236*4882a593Smuzhiyun 	if (!list_empty(&ehci->intr_unlink_wait)) {
237*4882a593Smuzhiyun 		ehci_enable_event(ehci, EHCI_HRTIMER_START_UNLINK_INTR, true);
238*4882a593Smuzhiyun 		++ehci->intr_unlink_wait_cycle;
239*4882a593Smuzhiyun 	}
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun /* Handle unlinked interrupt QHs once they are gone from the hardware */
ehci_handle_intr_unlinks(struct ehci_hcd * ehci)243*4882a593Smuzhiyun static void ehci_handle_intr_unlinks(struct ehci_hcd *ehci)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun 	bool		stopped = (ehci->rh_state < EHCI_RH_RUNNING);
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	/*
248*4882a593Smuzhiyun 	 * Process all the QHs on the intr_unlink list that were added
249*4882a593Smuzhiyun 	 * before the current unlink cycle began.  The list is in
250*4882a593Smuzhiyun 	 * temporal order, so stop when we reach the first entry in the
251*4882a593Smuzhiyun 	 * current cycle.  But if the root hub isn't running then
252*4882a593Smuzhiyun 	 * process all the QHs on the list.
253*4882a593Smuzhiyun 	 */
254*4882a593Smuzhiyun 	ehci->intr_unlinking = true;
255*4882a593Smuzhiyun 	while (!list_empty(&ehci->intr_unlink)) {
256*4882a593Smuzhiyun 		struct ehci_qh	*qh;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 		qh = list_first_entry(&ehci->intr_unlink, struct ehci_qh,
259*4882a593Smuzhiyun 				unlink_node);
260*4882a593Smuzhiyun 		if (!stopped && qh->unlink_cycle == ehci->intr_unlink_cycle)
261*4882a593Smuzhiyun 			break;
262*4882a593Smuzhiyun 		list_del_init(&qh->unlink_node);
263*4882a593Smuzhiyun 		end_unlink_intr(ehci, qh);
264*4882a593Smuzhiyun 	}
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	/* Handle remaining entries later */
267*4882a593Smuzhiyun 	if (!list_empty(&ehci->intr_unlink)) {
268*4882a593Smuzhiyun 		ehci_enable_event(ehci, EHCI_HRTIMER_UNLINK_INTR, true);
269*4882a593Smuzhiyun 		++ehci->intr_unlink_cycle;
270*4882a593Smuzhiyun 	}
271*4882a593Smuzhiyun 	ehci->intr_unlinking = false;
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun /* Start another free-iTDs/siTDs cycle */
start_free_itds(struct ehci_hcd * ehci)276*4882a593Smuzhiyun static void start_free_itds(struct ehci_hcd *ehci)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun 	if (!(ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_FREE_ITDS))) {
279*4882a593Smuzhiyun 		ehci->last_itd_to_free = list_entry(
280*4882a593Smuzhiyun 				ehci->cached_itd_list.prev,
281*4882a593Smuzhiyun 				struct ehci_itd, itd_list);
282*4882a593Smuzhiyun 		ehci->last_sitd_to_free = list_entry(
283*4882a593Smuzhiyun 				ehci->cached_sitd_list.prev,
284*4882a593Smuzhiyun 				struct ehci_sitd, sitd_list);
285*4882a593Smuzhiyun 		ehci_enable_event(ehci, EHCI_HRTIMER_FREE_ITDS, true);
286*4882a593Smuzhiyun 	}
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun /* Wait for controller to stop using old iTDs and siTDs */
end_free_itds(struct ehci_hcd * ehci)290*4882a593Smuzhiyun static void end_free_itds(struct ehci_hcd *ehci)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun 	struct ehci_itd		*itd, *n;
293*4882a593Smuzhiyun 	struct ehci_sitd	*sitd, *sn;
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	if (ehci->rh_state < EHCI_RH_RUNNING) {
296*4882a593Smuzhiyun 		ehci->last_itd_to_free = NULL;
297*4882a593Smuzhiyun 		ehci->last_sitd_to_free = NULL;
298*4882a593Smuzhiyun 	}
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	list_for_each_entry_safe(itd, n, &ehci->cached_itd_list, itd_list) {
301*4882a593Smuzhiyun 		list_del(&itd->itd_list);
302*4882a593Smuzhiyun 		dma_pool_free(ehci->itd_pool, itd, itd->itd_dma);
303*4882a593Smuzhiyun 		if (itd == ehci->last_itd_to_free)
304*4882a593Smuzhiyun 			break;
305*4882a593Smuzhiyun 	}
306*4882a593Smuzhiyun 	list_for_each_entry_safe(sitd, sn, &ehci->cached_sitd_list, sitd_list) {
307*4882a593Smuzhiyun 		list_del(&sitd->sitd_list);
308*4882a593Smuzhiyun 		dma_pool_free(ehci->sitd_pool, sitd, sitd->sitd_dma);
309*4882a593Smuzhiyun 		if (sitd == ehci->last_sitd_to_free)
310*4882a593Smuzhiyun 			break;
311*4882a593Smuzhiyun 	}
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	if (!list_empty(&ehci->cached_itd_list) ||
314*4882a593Smuzhiyun 			!list_empty(&ehci->cached_sitd_list))
315*4882a593Smuzhiyun 		start_free_itds(ehci);
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun /* Handle lost (or very late) IAA interrupts */
ehci_iaa_watchdog(struct ehci_hcd * ehci)320*4882a593Smuzhiyun static void ehci_iaa_watchdog(struct ehci_hcd *ehci)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun 	u32 cmd, status;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	/*
325*4882a593Smuzhiyun 	 * Lost IAA irqs wedge things badly; seen first with a vt8235.
326*4882a593Smuzhiyun 	 * So we need this watchdog, but must protect it against both
327*4882a593Smuzhiyun 	 * (a) SMP races against real IAA firing and retriggering, and
328*4882a593Smuzhiyun 	 * (b) clean HC shutdown, when IAA watchdog was pending.
329*4882a593Smuzhiyun 	 */
330*4882a593Smuzhiyun 	if (!ehci->iaa_in_progress || ehci->rh_state != EHCI_RH_RUNNING)
331*4882a593Smuzhiyun 		return;
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	/* If we get here, IAA is *REALLY* late.  It's barely
334*4882a593Smuzhiyun 	 * conceivable that the system is so busy that CMD_IAAD
335*4882a593Smuzhiyun 	 * is still legitimately set, so let's be sure it's
336*4882a593Smuzhiyun 	 * clear before we read STS_IAA.  (The HC should clear
337*4882a593Smuzhiyun 	 * CMD_IAAD when it sets STS_IAA.)
338*4882a593Smuzhiyun 	 */
339*4882a593Smuzhiyun 	cmd = ehci_readl(ehci, &ehci->regs->command);
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	/*
342*4882a593Smuzhiyun 	 * If IAA is set here it either legitimately triggered
343*4882a593Smuzhiyun 	 * after the watchdog timer expired (_way_ late, so we'll
344*4882a593Smuzhiyun 	 * still count it as lost) ... or a silicon erratum:
345*4882a593Smuzhiyun 	 * - VIA seems to set IAA without triggering the IRQ;
346*4882a593Smuzhiyun 	 * - IAAD potentially cleared without setting IAA.
347*4882a593Smuzhiyun 	 */
348*4882a593Smuzhiyun 	status = ehci_readl(ehci, &ehci->regs->status);
349*4882a593Smuzhiyun 	if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
350*4882a593Smuzhiyun 		INCR(ehci->stats.lost_iaa);
351*4882a593Smuzhiyun 		ehci_writel(ehci, STS_IAA, &ehci->regs->status);
352*4882a593Smuzhiyun 	}
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	ehci_dbg(ehci, "IAA watchdog: status %x cmd %x\n", status, cmd);
355*4882a593Smuzhiyun 	end_iaa_cycle(ehci);
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun /* Enable the I/O watchdog, if appropriate */
turn_on_io_watchdog(struct ehci_hcd * ehci)360*4882a593Smuzhiyun static void turn_on_io_watchdog(struct ehci_hcd *ehci)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun 	/* Not needed if the controller isn't running or it's already enabled */
363*4882a593Smuzhiyun 	if (ehci->rh_state != EHCI_RH_RUNNING ||
364*4882a593Smuzhiyun 			(ehci->enabled_hrtimer_events &
365*4882a593Smuzhiyun 				BIT(EHCI_HRTIMER_IO_WATCHDOG)))
366*4882a593Smuzhiyun 		return;
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	/*
369*4882a593Smuzhiyun 	 * Isochronous transfers always need the watchdog.
370*4882a593Smuzhiyun 	 * For other sorts we use it only if the flag is set.
371*4882a593Smuzhiyun 	 */
372*4882a593Smuzhiyun 	if (ehci->isoc_count > 0 || (ehci->need_io_watchdog &&
373*4882a593Smuzhiyun 			ehci->async_count + ehci->intr_count > 0))
374*4882a593Smuzhiyun 		ehci_enable_event(ehci, EHCI_HRTIMER_IO_WATCHDOG, true);
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun /*
379*4882a593Smuzhiyun  * Handler functions for the hrtimer event types.
380*4882a593Smuzhiyun  * Keep this array in the same order as the event types indexed by
381*4882a593Smuzhiyun  * enum ehci_hrtimer_event in ehci.h.
382*4882a593Smuzhiyun  */
383*4882a593Smuzhiyun static void (*event_handlers[])(struct ehci_hcd *) = {
384*4882a593Smuzhiyun 	ehci_poll_ASS,			/* EHCI_HRTIMER_POLL_ASS */
385*4882a593Smuzhiyun 	ehci_poll_PSS,			/* EHCI_HRTIMER_POLL_PSS */
386*4882a593Smuzhiyun 	ehci_handle_controller_death,	/* EHCI_HRTIMER_POLL_DEAD */
387*4882a593Smuzhiyun 	ehci_handle_intr_unlinks,	/* EHCI_HRTIMER_UNLINK_INTR */
388*4882a593Smuzhiyun 	end_free_itds,			/* EHCI_HRTIMER_FREE_ITDS */
389*4882a593Smuzhiyun 	end_unlink_async,		/* EHCI_HRTIMER_ACTIVE_UNLINK */
390*4882a593Smuzhiyun 	ehci_handle_start_intr_unlinks,	/* EHCI_HRTIMER_START_UNLINK_INTR */
391*4882a593Smuzhiyun 	unlink_empty_async,		/* EHCI_HRTIMER_ASYNC_UNLINKS */
392*4882a593Smuzhiyun 	ehci_iaa_watchdog,		/* EHCI_HRTIMER_IAA_WATCHDOG */
393*4882a593Smuzhiyun 	ehci_disable_PSE,		/* EHCI_HRTIMER_DISABLE_PERIODIC */
394*4882a593Smuzhiyun 	ehci_disable_ASE,		/* EHCI_HRTIMER_DISABLE_ASYNC */
395*4882a593Smuzhiyun 	ehci_work,			/* EHCI_HRTIMER_IO_WATCHDOG */
396*4882a593Smuzhiyun };
397*4882a593Smuzhiyun 
ehci_hrtimer_func(struct hrtimer * t)398*4882a593Smuzhiyun static enum hrtimer_restart ehci_hrtimer_func(struct hrtimer *t)
399*4882a593Smuzhiyun {
400*4882a593Smuzhiyun 	struct ehci_hcd	*ehci = container_of(t, struct ehci_hcd, hrtimer);
401*4882a593Smuzhiyun 	ktime_t		now;
402*4882a593Smuzhiyun 	unsigned long	events;
403*4882a593Smuzhiyun 	unsigned long	flags;
404*4882a593Smuzhiyun 	unsigned	e;
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	spin_lock_irqsave(&ehci->lock, flags);
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	events = ehci->enabled_hrtimer_events;
409*4882a593Smuzhiyun 	ehci->enabled_hrtimer_events = 0;
410*4882a593Smuzhiyun 	ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	/*
413*4882a593Smuzhiyun 	 * Check each pending event.  If its time has expired, handle
414*4882a593Smuzhiyun 	 * the event; otherwise re-enable it.
415*4882a593Smuzhiyun 	 */
416*4882a593Smuzhiyun 	now = ktime_get();
417*4882a593Smuzhiyun 	for_each_set_bit(e, &events, EHCI_HRTIMER_NUM_EVENTS) {
418*4882a593Smuzhiyun 		if (ktime_compare(now, ehci->hr_timeouts[e]) >= 0)
419*4882a593Smuzhiyun 			event_handlers[e](ehci);
420*4882a593Smuzhiyun 		else
421*4882a593Smuzhiyun 			ehci_enable_event(ehci, e, false);
422*4882a593Smuzhiyun 	}
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ehci->lock, flags);
425*4882a593Smuzhiyun 	return HRTIMER_NORESTART;
426*4882a593Smuzhiyun }
427