[adm5120] USB driver fixes
[openwrt/svn-archive/archive.git] / target / linux / adm5120 / files / drivers / usb / host / adm5120-pm.c
1 /*
2 * OHCI HCD (Host Controller Driver) for USB.
3 *
4 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5 * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
6 *
7 * This file is licenced under GPL
8 */
9
10 #define OHCI_SCHED_ENABLES \
11 (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
12
13 #ifdef CONFIG_PM
14 static int admhc_restart(struct admhcd *ahcd);
15
16 static int admhc_rh_suspend(struct admhcd *ahcd, int autostop)
17 __releases(ahcd->lock)
18 __acquires(ahcd->lock)
19 {
20 int status = 0;
21
22 ahcd->hc_control = admhc_readl(ahcd, &ahcd->regs->control);
23 switch (ahcd->hc_control & OHCI_CTRL_HCFS) {
24 case OHCI_USB_RESUME:
25 admhc_dbg(ahcd, "resume/suspend?\n");
26 ahcd->hc_control &= ~OHCI_CTRL_HCFS;
27 ahcd->hc_control |= OHCI_USB_RESET;
28 admhc_writel(ahcd, ahcd->hc_control, &ahcd->ahcd->regs->control);
29 (void) admhc_readl(ahcd, &ahcd->regs->control);
30 /* FALL THROUGH */
31 case OHCI_USB_RESET:
32 status = -EBUSY;
33 admhc_dbg(ahcd, "needs reinit!\n");
34 goto done;
35 case OHCI_USB_SUSPEND:
36 if (!ahcd->autostop) {
37 admhc_dbg(ahcd, "already suspended\n");
38 goto done;
39 }
40 }
41 admhc_dbg(ahcd, "%s root hub\n",
42 autostop ? "auto-stop" : "suspend");
43
44 /* First stop any processing */
45 if (!autostop && (ahcd->hc_control & OHCI_SCHED_ENABLES)) {
46 ahcd->hc_control &= ~OHCI_SCHED_ENABLES;
47 admhc_writel(ahcd, ahcd->hc_control, &ahcd->ahcd->regs->control);
48 ahcd->hc_control = admhc_readl(ahcd, &ahcd->regs->control);
49 admhc_writel(ahcd, OHCI_INTR_SF, &ahcd->regs->intrstatus);
50
51 /* sched disables take effect on the next frame,
52 * then the last WDH could take 6+ msec
53 */
54 admhc_dbg(ahcd, "stopping schedules ...\n");
55 ahcd->autostop = 0;
56 spin_unlock_irq (&ahcd->lock);
57 msleep (8);
58 spin_lock_irq(&ahcd->lock);
59 }
60 dl_done_list (ahcd);
61 finish_unlinks (ahcd, admhc_frame_no(ahcd));
62
63 /* maybe resume can wake root hub */
64 if (device_may_wakeup(&admhcd_to_hcd(ahcd)->self.root_hub->dev) ||
65 autostop)
66 ahcd->hc_control |= OHCI_CTRL_RWE;
67 else {
68 admhc_writel(ahcd, OHCI_INTR_RHSC, &ahcd->regs->intrdisable);
69 ahcd->hc_control &= ~OHCI_CTRL_RWE;
70 }
71
72 /* Suspend hub ... this is the "global (to this bus) suspend" mode,
73 * which doesn't imply ports will first be individually suspended.
74 */
75 ahcd->hc_control &= ~OHCI_CTRL_HCFS;
76 ahcd->hc_control |= OHCI_USB_SUSPEND;
77 admhc_writel(ahcd, ahcd->hc_control, &ahcd->ahcd->regs->control);
78 (void) admhc_readl(ahcd, &ahcd->regs->control);
79
80 /* no resumes until devices finish suspending */
81 if (!autostop) {
82 ahcd->next_statechange = jiffies + msecs_to_jiffies (5);
83 ahcd->autostop = 0;
84 }
85
86 done:
87 return status;
88 }
89
90 static inline struct ed *find_head(struct ed *ed)
91 {
92 /* for bulk and control lists */
93 while (ed->ed_prev)
94 ed = ed->ed_prev;
95 return ed;
96 }
97
98 /* caller has locked the root hub */
99 static int admhc_rh_resume(struct admhcd *ahcd)
100 __releases(ahcd->lock)
101 __acquires(ahcd->lock)
102 {
103 struct usb_hcd *hcd = admhcd_to_hcd (ahcd);
104 u32 temp, enables;
105 int status = -EINPROGRESS;
106 int autostopped = ahcd->autostop;
107
108 ahcd->autostop = 0;
109 ahcd->hc_control = admhc_readl(ahcd, &ahcd->regs->control);
110
111 if (ahcd->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
112 /* this can happen after resuming a swsusp snapshot */
113 if (hcd->state == HC_STATE_RESUMING) {
114 admhc_dbg(ahcd, "BIOS/SMM active, control %03x\n",
115 ahcd->hc_control);
116 status = -EBUSY;
117 /* this happens when pmcore resumes HC then root */
118 } else {
119 admhc_dbg(ahcd, "duplicate resume\n");
120 status = 0;
121 }
122 } else switch (ahcd->hc_control & OHCI_CTRL_HCFS) {
123 case OHCI_USB_SUSPEND:
124 ahcd->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
125 ahcd->hc_control |= OHCI_USB_RESUME;
126 admhc_writel(ahcd, ahcd->hc_control, &ahcd->ahcd->regs->control);
127 (void) admhc_readl(ahcd, &ahcd->regs->control);
128 admhc_dbg(ahcd, "%s root hub\n",
129 autostopped ? "auto-start" : "resume");
130 break;
131 case OHCI_USB_RESUME:
132 /* HCFS changes sometime after INTR_RD */
133 admhc_dbg(ahcd, "%swakeup root hub\n",
134 autostopped ? "auto-" : "");
135 break;
136 case OHCI_USB_OPER:
137 /* this can happen after resuming a swsusp snapshot */
138 admhc_dbg(ahcd, "snapshot resume? reinit\n");
139 status = -EBUSY;
140 break;
141 default: /* RESET, we lost power */
142 admhc_dbg(ahcd, "lost power\n");
143 status = -EBUSY;
144 }
145 if (status == -EBUSY) {
146 if (!autostopped) {
147 spin_unlock_irq (&ahcd->lock);
148 (void) ahcd_init (ahcd);
149 status = admhc_restart (ahcd);
150 spin_lock_irq(&ahcd->lock);
151 }
152 return status;
153 }
154 if (status != -EINPROGRESS)
155 return status;
156 if (autostopped)
157 goto skip_resume;
158 spin_unlock_irq (&ahcd->lock);
159
160 /* Some controllers (lucent erratum) need extra-long delays */
161 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
162
163 temp = admhc_readl(ahcd, &ahcd->regs->control);
164 temp &= OHCI_CTRL_HCFS;
165 if (temp != OHCI_USB_RESUME) {
166 admhc_err (ahcd, "controller won't resume\n");
167 spin_lock_irq(&ahcd->lock);
168 return -EBUSY;
169 }
170
171 /* disable old schedule state, reinit from scratch */
172 admhc_writel(ahcd, 0, &ahcd->regs->ed_controlhead);
173 admhc_writel(ahcd, 0, &ahcd->regs->ed_controlcurrent);
174 admhc_writel(ahcd, 0, &ahcd->regs->ed_bulkhead);
175 admhc_writel(ahcd, 0, &ahcd->regs->ed_bulkcurrent);
176 admhc_writel(ahcd, 0, &ahcd->regs->ed_periodcurrent);
177 admhc_writel(ahcd, (u32) ahcd->hcca_dma, &ahcd->ahcd->regs->hcca);
178
179 /* Sometimes PCI D3 suspend trashes frame timings ... */
180 periodic_reinit(ahcd);
181
182 /* the following code is executed with ahcd->lock held and
183 * irqs disabled if and only if autostopped is true
184 */
185
186 skip_resume:
187 /* interrupts might have been disabled */
188 admhc_writel(ahcd, OHCI_INTR_INIT, &ahcd->regs->int_enable);
189 if (ahcd->ed_rm_list)
190 admhc_writel(ahcd, OHCI_INTR_SF, &ahcd->regs->int_enable);
191
192 /* Then re-enable operations */
193 admhc_writel(ahcd, OHCI_USB_OPER, &ahcd->regs->control);
194 (void) admhc_readl(ahcd, &ahcd->regs->control);
195 if (!autostopped)
196 msleep (3);
197
198 temp = ahcd->hc_control;
199 temp &= OHCI_CTRL_RWC;
200 temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
201 ahcd->hc_control = temp;
202 admhc_writel(ahcd, temp, &ahcd->regs->control);
203 (void) admhc_readl(ahcd, &ahcd->regs->control);
204
205 /* TRSMRCY */
206 if (!autostopped) {
207 msleep (10);
208 spin_lock_irq(&ahcd->lock);
209 }
210 /* now ahcd->lock is always held and irqs are always disabled */
211
212 /* keep it alive for more than ~5x suspend + resume costs */
213 ahcd->next_statechange = jiffies + STATECHANGE_DELAY;
214
215 /* maybe turn schedules back on */
216 enables = 0;
217 temp = 0;
218 if (!ahcd->ed_rm_list) {
219 if (ahcd->ed_controltail) {
220 admhc_writel(ahcd,
221 find_head (ahcd->ed_controltail)->dma,
222 &ahcd->regs->ed_controlhead);
223 enables |= OHCI_CTRL_CLE;
224 temp |= OHCI_CLF;
225 }
226 if (ahcd->ed_bulktail) {
227 admhc_writel(ahcd, find_head (ahcd->ed_bulktail)->dma,
228 &ahcd->regs->ed_bulkhead);
229 enables |= OHCI_CTRL_BLE;
230 temp |= OHCI_BLF;
231 }
232 }
233 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
234 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
235 if (enables) {
236 admhc_dbg(ahcd, "restarting schedules ... %08x\n", enables);
237 ahcd->hc_control |= enables;
238 admhc_writel(ahcd, ahcd->hc_control, &ahcd->ahcd->regs->control);
239 if (temp)
240 admhc_writel(ahcd, temp, &ahcd->regs->cmdstatus);
241 (void) admhc_readl(ahcd, &ahcd->regs->control);
242 }
243
244 return 0;
245 }
246
247 static int admhc_bus_suspend(struct usb_hcd *hcd)
248 {
249 struct admhcd *ahcd = hcd_to_admhcd(hcd);
250 int rc;
251
252 spin_lock_irq(&ahcd->lock);
253
254 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
255 rc = -ESHUTDOWN;
256 else
257 rc = admhc_rh_suspend(ahcd, 0);
258 spin_unlock_irq(&ahcd->lock);
259 return rc;
260 }
261
262 static int admhc_bus_resume(struct usb_hcd *hcd)
263 {
264 struct admhcd *ahcd = hcd_to_admhcd(hcd);
265 int rc;
266
267 if (time_before(jiffies, ahcd->next_statechange))
268 msleep(5);
269
270 spin_lock_irq(&ahcd->lock);
271
272 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
273 rc = -ESHUTDOWN;
274 else
275 rc = admhc_rh_resume(ahcd);
276 spin_unlock_irq(&ahcd->lock);
277
278 /* poll until we know a device is connected or we autostop */
279 if (rc == 0)
280 usb_hcd_poll_rh_status(hcd);
281 return rc;
282 }
283
284 /* Carry out polling-, autostop-, and autoresume-related state changes */
285 static int admhc_root_hub_state_changes(struct admhcd *ahcd, int changed,
286 int any_connected)
287 {
288 int poll_rh = 1;
289
290 switch (ahcd->hc_control & OHCI_CTRL_HCFS) {
291
292 case OHCI_USB_OPER:
293 /* keep on polling until we know a device is connected
294 * and RHSC is enabled */
295 if (!ahcd->autostop) {
296 if (any_connected ||
297 !device_may_wakeup(&admhcd_to_hcd(ahcd)
298 ->self.root_hub->dev)) {
299 if (admhc_readl(ahcd, &ahcd->regs->int_enable) &
300 OHCI_INTR_RHSC)
301 poll_rh = 0;
302 } else {
303 ahcd->autostop = 1;
304 ahcd->next_statechange = jiffies + HZ;
305 }
306
307 /* if no devices have been attached for one second, autostop */
308 } else {
309 if (changed || any_connected) {
310 ahcd->autostop = 0;
311 ahcd->next_statechange = jiffies +
312 STATECHANGE_DELAY;
313 } else if (time_after_eq(jiffies,
314 ahcd->next_statechange)
315 && !ahcd->ed_rm_list
316 && !(ahcd->hc_control &
317 OHCI_SCHED_ENABLES)) {
318 ahcd_rh_suspend(ahcd, 1);
319 }
320 }
321 break;
322
323 /* if there is a port change, autostart or ask to be resumed */
324 case OHCI_USB_SUSPEND:
325 case OHCI_USB_RESUME:
326 if (changed) {
327 if (ahcd->autostop)
328 admhc_rh_resume(ahcd);
329 else
330 usb_hcd_resume_root_hub(admhcd_to_hcd(ahcd));
331 } else {
332 /* everything is idle, no need for polling */
333 poll_rh = 0;
334 }
335 break;
336 }
337 return poll_rh;
338 }
339
340 /*-------------------------------------------------------------------------*/
341
342 /* must not be called from interrupt context */
343 static int admhc_restart(struct admhcd *ahcd)
344 {
345 int temp;
346 int i;
347 struct urb_priv *priv;
348
349 /* mark any devices gone, so they do nothing till khubd disconnects.
350 * recycle any "live" eds/tds (and urbs) right away.
351 * later, khubd disconnect processing will recycle the other state,
352 * (either as disconnect/reconnect, or maybe someday as a reset).
353 */
354 spin_lock_irq(&ahcd->lock);
355 admhc_disable(ahcd);
356 usb_root_hub_lost_power(admhcd_to_hcd(ahcd)->self.root_hub);
357 if (!list_empty(&ahcd->pending))
358 admhc_dbg(ahcd, "abort schedule...\n");
359 list_for_each_entry(priv, &ahcd->pending, pending) {
360 struct urb *urb = priv->td[0]->urb;
361 struct ed *ed = priv->ed;
362
363 switch (ed->state) {
364 case ED_OPER:
365 ed->state = ED_UNLINK;
366 ed->hwINFO |= cpu_to_hc32(ahcd, ED_DEQUEUE);
367 ed_deschedule (ahcd, ed);
368
369 ed->ed_next = ahcd->ed_rm_list;
370 ed->ed_prev = NULL;
371 ahcd->ed_rm_list = ed;
372 /* FALLTHROUGH */
373 case ED_UNLINK:
374 break;
375 default:
376 admhc_dbg(ahcd, "bogus ed %p state %d\n",
377 ed, ed->state);
378 }
379
380 spin_lock(&urb->lock);
381 urb->status = -ESHUTDOWN;
382 spin_unlock(&urb->lock);
383 }
384 finish_unlinks(ahcd, 0);
385 spin_unlock_irq(&ahcd->lock);
386
387 /* paranoia, in case that didn't work: */
388
389 /* empty the interrupt branches */
390 for (i = 0; i < NUM_INTS; i++) ahcd->load[i] = 0;
391 for (i = 0; i < NUM_INTS; i++) ahcd->hcca->int_table[i] = 0;
392
393 /* no EDs to remove */
394 ahcd->ed_rm_list = NULL;
395
396 /* empty control and bulk lists */
397 ahcd->ed_controltail = NULL;
398 ahcd->ed_bulktail = NULL;
399
400 if ((temp = admhc_run(ahcd)) < 0) {
401 admhc_err(ahcd, "can't restart, %d\n", temp);
402 return temp;
403 } else {
404 /* here we "know" root ports should always stay powered,
405 * and that if we try to turn them back on the root hub
406 * will respond to CSC processing.
407 */
408 i = ahcd->num_ports;
409 while (i--)
410 admhc_writel(ahcd, RH_PS_PSS,
411 &ahcd->regs->portstatus[i]);
412 admhc_dbg(ahcd, "restart complete\n");
413 }
414 return 0;
415 }
416
417 #else /* CONFIG_PM */
418
419 static inline int admhc_rh_resume(struct admhcd *ahcd)
420 {
421 return 0;
422 }
423
424 /* Carry out polling-related state changes.
425 * autostop isn't used when CONFIG_PM is turned off.
426 */
427 static int admhc_root_hub_state_changes(struct admhcd *ahcd, int changed,
428 int any_connected)
429 {
430 int poll_rh = 1;
431
432 /* keep on polling until RHSC is enabled */
433 if (admhc_readl(ahcd, &ahcd->regs->int_enable) & ADMHC_INTR_INSM)
434 poll_rh = 0;
435
436 return poll_rh;
437 }
438
439 #endif /* CONFIG_PM */
440