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