bd8b591771b08de2921e6b7da81a410fa4cb0a09
[firefly-linux-kernel-4.4.55.git] / drivers / usb / host / ehci-timer.c
1 /*
2  * Copyright (C) 2012 by Alan Stern
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  */
14
15 /* This file is part of ehci-hcd.c */
16
17 /*-------------------------------------------------------------------------*/
18
19 /* Set a bit in the USBCMD register */
20 static void ehci_set_command_bit(struct ehci_hcd *ehci, u32 bit)
21 {
22         ehci->command |= bit;
23         ehci_writel(ehci, ehci->command, &ehci->regs->command);
24
25         /* unblock posted write */
26         ehci_readl(ehci, &ehci->regs->command);
27 }
28
29 /* Clear a bit in the USBCMD register */
30 static void ehci_clear_command_bit(struct ehci_hcd *ehci, u32 bit)
31 {
32         ehci->command &= ~bit;
33         ehci_writel(ehci, ehci->command, &ehci->regs->command);
34
35         /* unblock posted write */
36         ehci_readl(ehci, &ehci->regs->command);
37 }
38
39 /*-------------------------------------------------------------------------*/
40
41 /*
42  * EHCI timer support...  Now using hrtimers.
43  *
44  * Lots of different events are triggered from ehci->hrtimer.  Whenever
45  * the timer routine runs, it checks each possible event; events that are
46  * currently enabled and whose expiration time has passed get handled.
47  * The set of enabled events is stored as a collection of bitflags in
48  * ehci->enabled_hrtimer_events, and they are numbered in order of
49  * increasing delay values (ranging between 1 ms and 100 ms).
50  *
51  * Rather than implementing a sorted list or tree of all pending events,
52  * we keep track only of the lowest-numbered pending event, in
53  * ehci->next_hrtimer_event.  Whenever ehci->hrtimer gets restarted, its
54  * expiration time is set to the timeout value for this event.
55  *
56  * As a result, events might not get handled right away; the actual delay
57  * could be anywhere up to twice the requested delay.  This doesn't
58  * matter, because none of the events are especially time-critical.  The
59  * ones that matter most all have a delay of 1 ms, so they will be
60  * handled after 2 ms at most, which is okay.  In addition to this, we
61  * allow for an expiration range of 1 ms.
62  */
63
64 /*
65  * Delay lengths for the hrtimer event types.
66  * Keep this list sorted by delay length, in the same order as
67  * the event types indexed by enum ehci_hrtimer_event in ehci.h.
68  */
69 static unsigned event_delays_ns[] = {
70         1 * NSEC_PER_MSEC,      /* EHCI_HRTIMER_POLL_ASS */
71         1 * NSEC_PER_MSEC,      /* EHCI_HRTIMER_POLL_PSS */
72         1125 * NSEC_PER_USEC,   /* EHCI_HRTIMER_UNLINK_INTR */
73         10 * NSEC_PER_MSEC,     /* EHCI_HRTIMER_DISABLE_PERIODIC */
74         15 * NSEC_PER_MSEC,     /* EHCI_HRTIMER_DISABLE_ASYNC */
75 };
76
77 /* Enable a pending hrtimer event */
78 static void ehci_enable_event(struct ehci_hcd *ehci, unsigned event,
79                 bool resched)
80 {
81         ktime_t         *timeout = &ehci->hr_timeouts[event];
82
83         if (resched)
84                 *timeout = ktime_add(ktime_get(),
85                                 ktime_set(0, event_delays_ns[event]));
86         ehci->enabled_hrtimer_events |= (1 << event);
87
88         /* Track only the lowest-numbered pending event */
89         if (event < ehci->next_hrtimer_event) {
90                 ehci->next_hrtimer_event = event;
91                 hrtimer_start_range_ns(&ehci->hrtimer, *timeout,
92                                 NSEC_PER_MSEC, HRTIMER_MODE_ABS);
93         }
94 }
95
96
97 /* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
98 static void ehci_poll_ASS(struct ehci_hcd *ehci)
99 {
100         unsigned        actual, want;
101
102         /* Don't enable anything if the controller isn't running (e.g., died) */
103         if (ehci->rh_state != EHCI_RH_RUNNING)
104                 return;
105
106         want = (ehci->command & CMD_ASE) ? STS_ASS : 0;
107         actual = ehci_readl(ehci, &ehci->regs->status) & STS_ASS;
108
109         if (want != actual) {
110
111                 /* Poll again later, but give up after about 20 ms */
112                 if (ehci->ASS_poll_count++ < 20) {
113                         ehci_enable_event(ehci, EHCI_HRTIMER_POLL_ASS, true);
114                         return;
115                 }
116                 ehci_warn(ehci, "Waited too long for the async schedule status, giving up\n");
117         }
118         ehci->ASS_poll_count = 0;
119
120         /* The status is up-to-date; restart or stop the schedule as needed */
121         if (want == 0) {        /* Stopped */
122                 if (ehci->async_count > 0)
123                         ehci_set_command_bit(ehci, CMD_ASE);
124
125         } else {                /* Running */
126                 if (ehci->async_count == 0) {
127
128                         /* Turn off the schedule after a while */
129                         ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_ASYNC,
130                                         true);
131                 }
132         }
133 }
134
135 /* Turn off the async schedule after a brief delay */
136 static void ehci_disable_ASE(struct ehci_hcd *ehci)
137 {
138         ehci_clear_command_bit(ehci, CMD_ASE);
139 }
140
141
142 /* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
143 static void ehci_poll_PSS(struct ehci_hcd *ehci)
144 {
145         unsigned        actual, want;
146
147         /* Don't do anything if the controller isn't running (e.g., died) */
148         if (ehci->rh_state != EHCI_RH_RUNNING)
149                 return;
150
151         want = (ehci->command & CMD_PSE) ? STS_PSS : 0;
152         actual = ehci_readl(ehci, &ehci->regs->status) & STS_PSS;
153
154         if (want != actual) {
155
156                 /* Poll again later, but give up after about 20 ms */
157                 if (ehci->PSS_poll_count++ < 20) {
158                         ehci_enable_event(ehci, EHCI_HRTIMER_POLL_PSS, true);
159                         return;
160                 }
161                 ehci_warn(ehci, "Waited too long for the periodic schedule status, giving up\n");
162         }
163         ehci->PSS_poll_count = 0;
164
165         /* The status is up-to-date; restart or stop the schedule as needed */
166         if (want == 0) {        /* Stopped */
167                 free_cached_lists(ehci);
168                 if (ehci->periodic_count > 0) {
169
170                         /* make sure ehci_work scans these */
171                         ehci->next_uframe = ehci_read_frame_index(ehci)
172                                         & ((ehci->periodic_size << 3) - 1);
173                         ehci_set_command_bit(ehci, CMD_PSE);
174                 }
175
176         } else {                /* Running */
177                 if (ehci->periodic_count == 0) {
178
179                         /* Turn off the schedule after a while */
180                         ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_PERIODIC,
181                                         true);
182                 }
183         }
184 }
185
186 /* Turn off the periodic schedule after a brief delay */
187 static void ehci_disable_PSE(struct ehci_hcd *ehci)
188 {
189         ehci_clear_command_bit(ehci, CMD_PSE);
190
191         /* Poll to see when it actually stops */
192         ehci_enable_event(ehci, EHCI_HRTIMER_POLL_PSS, true);
193 }
194
195
196 /* Handle unlinked interrupt QHs once they are gone from the hardware */
197 static void ehci_handle_intr_unlinks(struct ehci_hcd *ehci)
198 {
199         bool            stopped = (ehci->rh_state < EHCI_RH_RUNNING);
200
201         /*
202          * Process all the QHs on the intr_unlink list that were added
203          * before the current unlink cycle began.  The list is in
204          * temporal order, so stop when we reach the first entry in the
205          * current cycle.  But if the root hub isn't running then
206          * process all the QHs on the list.
207          */
208         ehci->intr_unlinking = true;
209         while (ehci->intr_unlink) {
210                 struct ehci_qh  *qh = ehci->intr_unlink;
211
212                 if (!stopped && qh->unlink_cycle == ehci->intr_unlink_cycle)
213                         break;
214                 ehci->intr_unlink = qh->unlink_next;
215                 qh->unlink_next = NULL;
216                 end_unlink_intr(ehci, qh);
217         }
218
219         /* Handle remaining entries later */
220         if (ehci->intr_unlink) {
221                 ehci_enable_event(ehci, EHCI_HRTIMER_UNLINK_INTR, true);
222                 ++ehci->intr_unlink_cycle;
223         }
224         ehci->intr_unlinking = false;
225 }
226
227
228 /*
229  * Handler functions for the hrtimer event types.
230  * Keep this array in the same order as the event types indexed by
231  * enum ehci_hrtimer_event in ehci.h.
232  */
233 static void (*event_handlers[])(struct ehci_hcd *) = {
234         ehci_poll_ASS,                  /* EHCI_HRTIMER_POLL_ASS */
235         ehci_poll_PSS,                  /* EHCI_HRTIMER_POLL_PSS */
236         ehci_handle_intr_unlinks,       /* EHCI_HRTIMER_UNLINK_INTR */
237         ehci_disable_PSE,               /* EHCI_HRTIMER_DISABLE_PERIODIC */
238         ehci_disable_ASE,               /* EHCI_HRTIMER_DISABLE_ASYNC */
239 };
240
241 static enum hrtimer_restart ehci_hrtimer_func(struct hrtimer *t)
242 {
243         struct ehci_hcd *ehci = container_of(t, struct ehci_hcd, hrtimer);
244         ktime_t         now;
245         unsigned long   events;
246         unsigned long   flags;
247         unsigned        e;
248
249         spin_lock_irqsave(&ehci->lock, flags);
250
251         events = ehci->enabled_hrtimer_events;
252         ehci->enabled_hrtimer_events = 0;
253         ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
254
255         /*
256          * Check each pending event.  If its time has expired, handle
257          * the event; otherwise re-enable it.
258          */
259         now = ktime_get();
260         for_each_set_bit(e, &events, EHCI_HRTIMER_NUM_EVENTS) {
261                 if (now.tv64 >= ehci->hr_timeouts[e].tv64)
262                         event_handlers[e](ehci);
263                 else
264                         ehci_enable_event(ehci, e, false);
265         }
266
267         spin_unlock_irqrestore(&ehci->lock, flags);
268         return HRTIMER_NORESTART;
269 }