blob: 1e907dd3bb1bc9e2e347179e6725bd85aa8e3f09 [file] [log] [blame]
Alan Sternd58b4bc2012-07-11 11:21:54 -04001/*
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
Alan Stern3ca9aeb2012-07-11 11:22:05 -040019/* Set a bit in the USBCMD register */
20static 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 */
30static 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
Alan Sternd58b4bc2012-07-11 11:21:54 -040041/*
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 */
69static unsigned event_delays_ns[] = {
Alan Stern31446612012-07-11 11:22:21 -040070 1 * NSEC_PER_MSEC, /* EHCI_HRTIMER_POLL_ASS */
Alan Stern3ca9aeb2012-07-11 11:22:05 -040071 1 * NSEC_PER_MSEC, /* EHCI_HRTIMER_POLL_PSS */
72 10 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_PERIODIC */
Alan Stern31446612012-07-11 11:22:21 -040073 15 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_ASYNC */
Alan Sternd58b4bc2012-07-11 11:21:54 -040074};
75
76/* Enable a pending hrtimer event */
77static void ehci_enable_event(struct ehci_hcd *ehci, unsigned event,
78 bool resched)
79{
80 ktime_t *timeout = &ehci->hr_timeouts[event];
81
82 if (resched)
83 *timeout = ktime_add(ktime_get(),
84 ktime_set(0, event_delays_ns[event]));
85 ehci->enabled_hrtimer_events |= (1 << event);
86
87 /* Track only the lowest-numbered pending event */
88 if (event < ehci->next_hrtimer_event) {
89 ehci->next_hrtimer_event = event;
90 hrtimer_start_range_ns(&ehci->hrtimer, *timeout,
91 NSEC_PER_MSEC, HRTIMER_MODE_ABS);
92 }
93}
94
95
Alan Stern31446612012-07-11 11:22:21 -040096/* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
97static void ehci_poll_ASS(struct ehci_hcd *ehci)
98{
99 unsigned actual, want;
100
101 /* Don't enable anything if the controller isn't running (e.g., died) */
102 if (ehci->rh_state != EHCI_RH_RUNNING)
103 return;
104
105 want = (ehci->command & CMD_ASE) ? STS_ASS : 0;
106 actual = ehci_readl(ehci, &ehci->regs->status) & STS_ASS;
107
108 if (want != actual) {
109
110 /* Poll again later, but give up after about 20 ms */
111 if (ehci->ASS_poll_count++ < 20) {
112 ehci_enable_event(ehci, EHCI_HRTIMER_POLL_ASS, true);
113 return;
114 }
115 ehci_warn(ehci, "Waited too long for the async schedule status, giving up\n");
116 }
117 ehci->ASS_poll_count = 0;
118
119 /* The status is up-to-date; restart or stop the schedule as needed */
120 if (want == 0) { /* Stopped */
121 if (ehci->async_count > 0)
122 ehci_set_command_bit(ehci, CMD_ASE);
123
124 } else { /* Running */
125 if (ehci->async_count == 0) {
126
127 /* Turn off the schedule after a while */
128 ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_ASYNC,
129 true);
130 }
131 }
132}
133
134/* Turn off the async schedule after a brief delay */
135static void ehci_disable_ASE(struct ehci_hcd *ehci)
136{
137 ehci_clear_command_bit(ehci, CMD_ASE);
138}
139
140
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400141/* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
142static void ehci_poll_PSS(struct ehci_hcd *ehci)
143{
144 unsigned actual, want;
145
146 /* Don't do anything if the controller isn't running (e.g., died) */
147 if (ehci->rh_state != EHCI_RH_RUNNING)
148 return;
149
150 want = (ehci->command & CMD_PSE) ? STS_PSS : 0;
151 actual = ehci_readl(ehci, &ehci->regs->status) & STS_PSS;
152
153 if (want != actual) {
154
155 /* Poll again later, but give up after about 20 ms */
156 if (ehci->PSS_poll_count++ < 20) {
157 ehci_enable_event(ehci, EHCI_HRTIMER_POLL_PSS, true);
158 return;
159 }
160 ehci_warn(ehci, "Waited too long for the periodic schedule status, giving up\n");
161 }
162 ehci->PSS_poll_count = 0;
163
164 /* The status is up-to-date; restart or stop the schedule as needed */
165 if (want == 0) { /* Stopped */
166 free_cached_lists(ehci);
167 if (ehci->periodic_count > 0) {
168
169 /* make sure ehci_work scans these */
170 ehci->next_uframe = ehci_read_frame_index(ehci)
171 & ((ehci->periodic_size << 3) - 1);
172 ehci_set_command_bit(ehci, CMD_PSE);
173 }
174
175 } else { /* Running */
176 if (ehci->periodic_count == 0) {
177
178 /* Turn off the schedule after a while */
179 ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_PERIODIC,
180 true);
181 }
182 }
183}
184
185/* Turn off the periodic schedule after a brief delay */
186static void ehci_disable_PSE(struct ehci_hcd *ehci)
187{
188 ehci_clear_command_bit(ehci, CMD_PSE);
189
190 /* Poll to see when it actually stops */
191 ehci_enable_event(ehci, EHCI_HRTIMER_POLL_PSS, true);
192}
193
194
Alan Sternd58b4bc2012-07-11 11:21:54 -0400195/*
196 * Handler functions for the hrtimer event types.
197 * Keep this array in the same order as the event types indexed by
198 * enum ehci_hrtimer_event in ehci.h.
199 */
200static void (*event_handlers[])(struct ehci_hcd *) = {
Alan Stern31446612012-07-11 11:22:21 -0400201 ehci_poll_ASS, /* EHCI_HRTIMER_POLL_ASS */
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400202 ehci_poll_PSS, /* EHCI_HRTIMER_POLL_PSS */
203 ehci_disable_PSE, /* EHCI_HRTIMER_DISABLE_PERIODIC */
Alan Stern31446612012-07-11 11:22:21 -0400204 ehci_disable_ASE, /* EHCI_HRTIMER_DISABLE_ASYNC */
Alan Sternd58b4bc2012-07-11 11:21:54 -0400205};
206
207static enum hrtimer_restart ehci_hrtimer_func(struct hrtimer *t)
208{
209 struct ehci_hcd *ehci = container_of(t, struct ehci_hcd, hrtimer);
210 ktime_t now;
211 unsigned long events;
212 unsigned long flags;
213 unsigned e;
214
215 spin_lock_irqsave(&ehci->lock, flags);
216
217 events = ehci->enabled_hrtimer_events;
218 ehci->enabled_hrtimer_events = 0;
219 ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
220
221 /*
222 * Check each pending event. If its time has expired, handle
223 * the event; otherwise re-enable it.
224 */
225 now = ktime_get();
226 for_each_set_bit(e, &events, EHCI_HRTIMER_NUM_EVENTS) {
227 if (now.tv64 >= ehci->hr_timeouts[e].tv64)
228 event_handlers[e](ehci);
229 else
230 ehci_enable_event(ehci, e, false);
231 }
232
233 spin_unlock_irqrestore(&ehci->lock, flags);
234 return HRTIMER_NORESTART;
235}