2 * ADM5120 HCD (Host Controller Driver) for USB
4 * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
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>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
16 #define OHCI_SCHED_ENABLES \
17 (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
20 static int admhc_restart(struct admhcd *ahcd);
22 static int admhc_rh_suspend(struct admhcd *ahcd, int autostop)
23 __releases(ahcd->lock)
24 __acquires(ahcd->lock)
28 ahcd->hc_control = admhc_readl(ahcd, &ahcd->regs->control);
29 switch (ahcd->hc_control & OHCI_CTRL_HCFS) {
31 admhc_dbg(ahcd, "resume/suspend?\n");
32 ahcd->hc_control &= ~OHCI_CTRL_HCFS;
33 ahcd->hc_control |= OHCI_USB_RESET;
34 admhc_writel(ahcd, ahcd->hc_control, &ahcd->ahcd->regs->control);
35 (void) admhc_readl(ahcd, &ahcd->regs->control);
39 admhc_dbg(ahcd, "needs reinit!\n");
41 case OHCI_USB_SUSPEND:
42 if (!ahcd->autostop) {
43 admhc_dbg(ahcd, "already suspended\n");
47 admhc_dbg(ahcd, "%s root hub\n",
48 autostop ? "auto-stop" : "suspend");
50 /* First stop any processing */
51 if (!autostop && (ahcd->hc_control & OHCI_SCHED_ENABLES)) {
52 ahcd->hc_control &= ~OHCI_SCHED_ENABLES;
53 admhc_writel(ahcd, ahcd->hc_control, &ahcd->ahcd->regs->control);
54 ahcd->hc_control = admhc_readl(ahcd, &ahcd->regs->control);
55 admhc_writel(ahcd, OHCI_INTR_SF, &ahcd->regs->intrstatus);
57 /* sched disables take effect on the next frame,
58 * then the last WDH could take 6+ msec
60 admhc_dbg(ahcd, "stopping schedules ...\n");
62 spin_unlock_irq (&ahcd->lock);
64 spin_lock_irq(&ahcd->lock);
67 finish_unlinks (ahcd, admhc_frame_no(ahcd));
69 /* maybe resume can wake root hub */
70 if (device_may_wakeup(&admhcd_to_hcd(ahcd)->self.root_hub->dev) ||
72 ahcd->hc_control |= OHCI_CTRL_RWE;
74 admhc_writel(ahcd, OHCI_INTR_RHSC, &ahcd->regs->intrdisable);
75 ahcd->hc_control &= ~OHCI_CTRL_RWE;
78 /* Suspend hub ... this is the "global (to this bus) suspend" mode,
79 * which doesn't imply ports will first be individually suspended.
81 ahcd->hc_control &= ~OHCI_CTRL_HCFS;
82 ahcd->hc_control |= OHCI_USB_SUSPEND;
83 admhc_writel(ahcd, ahcd->hc_control, &ahcd->ahcd->regs->control);
84 (void) admhc_readl(ahcd, &ahcd->regs->control);
86 /* no resumes until devices finish suspending */
88 ahcd->next_statechange = jiffies + msecs_to_jiffies (5);
96 static inline struct ed *find_head(struct ed *ed)
98 /* for bulk and control lists */
104 /* caller has locked the root hub */
105 static int admhc_rh_resume(struct admhcd *ahcd)
106 __releases(ahcd->lock)
107 __acquires(ahcd->lock)
109 struct usb_hcd *hcd = admhcd_to_hcd (ahcd);
111 int status = -EINPROGRESS;
112 int autostopped = ahcd->autostop;
115 ahcd->hc_control = admhc_readl(ahcd, &ahcd->regs->control);
117 if (ahcd->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
118 /* this can happen after resuming a swsusp snapshot */
119 if (hcd->state == HC_STATE_RESUMING) {
120 admhc_dbg(ahcd, "BIOS/SMM active, control %03x\n",
123 /* this happens when pmcore resumes HC then root */
125 admhc_dbg(ahcd, "duplicate resume\n");
128 } else switch (ahcd->hc_control & OHCI_CTRL_HCFS) {
129 case OHCI_USB_SUSPEND:
130 ahcd->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
131 ahcd->hc_control |= OHCI_USB_RESUME;
132 admhc_writel(ahcd, ahcd->hc_control, &ahcd->ahcd->regs->control);
133 (void) admhc_readl(ahcd, &ahcd->regs->control);
134 admhc_dbg(ahcd, "%s root hub\n",
135 autostopped ? "auto-start" : "resume");
137 case OHCI_USB_RESUME:
138 /* HCFS changes sometime after INTR_RD */
139 admhc_dbg(ahcd, "%swakeup root hub\n",
140 autostopped ? "auto-" : "");
143 /* this can happen after resuming a swsusp snapshot */
144 admhc_dbg(ahcd, "snapshot resume? reinit\n");
147 default: /* RESET, we lost power */
148 admhc_dbg(ahcd, "lost power\n");
151 if (status == -EBUSY) {
153 spin_unlock_irq (&ahcd->lock);
154 (void) ahcd_init (ahcd);
155 status = admhc_restart (ahcd);
156 spin_lock_irq(&ahcd->lock);
160 if (status != -EINPROGRESS)
164 spin_unlock_irq (&ahcd->lock);
166 /* Some controllers (lucent erratum) need extra-long delays */
167 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
169 temp = admhc_readl(ahcd, &ahcd->regs->control);
170 temp &= OHCI_CTRL_HCFS;
171 if (temp != OHCI_USB_RESUME) {
172 admhc_err (ahcd, "controller won't resume\n");
173 spin_lock_irq(&ahcd->lock);
177 /* disable old schedule state, reinit from scratch */
178 admhc_writel(ahcd, 0, &ahcd->regs->ed_controlhead);
179 admhc_writel(ahcd, 0, &ahcd->regs->ed_controlcurrent);
180 admhc_writel(ahcd, 0, &ahcd->regs->ed_bulkhead);
181 admhc_writel(ahcd, 0, &ahcd->regs->ed_bulkcurrent);
182 admhc_writel(ahcd, 0, &ahcd->regs->ed_periodcurrent);
183 admhc_writel(ahcd, (u32) ahcd->hcca_dma, &ahcd->ahcd->regs->hcca);
185 /* Sometimes PCI D3 suspend trashes frame timings ... */
186 periodic_reinit(ahcd);
188 /* the following code is executed with ahcd->lock held and
189 * irqs disabled if and only if autostopped is true
193 /* interrupts might have been disabled */
194 admhc_writel(ahcd, OHCI_INTR_INIT, &ahcd->regs->int_enable);
195 if (ahcd->ed_rm_list)
196 admhc_writel(ahcd, OHCI_INTR_SF, &ahcd->regs->int_enable);
198 /* Then re-enable operations */
199 admhc_writel(ahcd, OHCI_USB_OPER, &ahcd->regs->control);
200 (void) admhc_readl(ahcd, &ahcd->regs->control);
204 temp = ahcd->hc_control;
205 temp &= OHCI_CTRL_RWC;
206 temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
207 ahcd->hc_control = temp;
208 admhc_writel(ahcd, temp, &ahcd->regs->control);
209 (void) admhc_readl(ahcd, &ahcd->regs->control);
214 spin_lock_irq(&ahcd->lock);
216 /* now ahcd->lock is always held and irqs are always disabled */
218 /* keep it alive for more than ~5x suspend + resume costs */
219 ahcd->next_statechange = jiffies + STATECHANGE_DELAY;
221 /* maybe turn schedules back on */
224 if (!ahcd->ed_rm_list) {
225 if (ahcd->ed_controltail) {
227 find_head (ahcd->ed_controltail)->dma,
228 &ahcd->regs->ed_controlhead);
229 enables |= OHCI_CTRL_CLE;
232 if (ahcd->ed_bulktail) {
233 admhc_writel(ahcd, find_head (ahcd->ed_bulktail)->dma,
234 &ahcd->regs->ed_bulkhead);
235 enables |= OHCI_CTRL_BLE;
239 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
240 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
242 admhc_dbg(ahcd, "restarting schedules ... %08x\n", enables);
243 ahcd->hc_control |= enables;
244 admhc_writel(ahcd, ahcd->hc_control, &ahcd->ahcd->regs->control);
246 admhc_writel(ahcd, temp, &ahcd->regs->cmdstatus);
247 (void) admhc_readl(ahcd, &ahcd->regs->control);
253 static int admhc_bus_suspend(struct usb_hcd *hcd)
255 struct admhcd *ahcd = hcd_to_admhcd(hcd);
258 spin_lock_irq(&ahcd->lock);
260 if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
263 rc = admhc_rh_suspend(ahcd, 0);
264 spin_unlock_irq(&ahcd->lock);
268 static int admhc_bus_resume(struct usb_hcd *hcd)
270 struct admhcd *ahcd = hcd_to_admhcd(hcd);
273 if (time_before(jiffies, ahcd->next_statechange))
276 spin_lock_irq(&ahcd->lock);
278 if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
281 rc = admhc_rh_resume(ahcd);
282 spin_unlock_irq(&ahcd->lock);
284 /* poll until we know a device is connected or we autostop */
286 usb_hcd_poll_rh_status(hcd);
290 /* Carry out polling-, autostop-, and autoresume-related state changes */
291 static int admhc_root_hub_state_changes(struct admhcd *ahcd, int changed,
296 switch (ahcd->hc_control & OHCI_CTRL_HCFS) {
299 /* keep on polling until we know a device is connected
300 * and RHSC is enabled */
301 if (!ahcd->autostop) {
303 !device_may_wakeup(&admhcd_to_hcd(ahcd)
304 ->self.root_hub->dev)) {
305 if (admhc_readl(ahcd, &ahcd->regs->int_enable) &
310 ahcd->next_statechange = jiffies + HZ;
313 /* if no devices have been attached for one second, autostop */
315 if (changed || any_connected) {
317 ahcd->next_statechange = jiffies +
319 } else if (time_after_eq(jiffies,
320 ahcd->next_statechange)
322 && !(ahcd->hc_control &
323 OHCI_SCHED_ENABLES)) {
324 ahcd_rh_suspend(ahcd, 1);
329 /* if there is a port change, autostart or ask to be resumed */
330 case OHCI_USB_SUSPEND:
331 case OHCI_USB_RESUME:
334 admhc_rh_resume(ahcd);
336 usb_hcd_resume_root_hub(admhcd_to_hcd(ahcd));
338 /* everything is idle, no need for polling */
346 /*-------------------------------------------------------------------------*/
348 /* must not be called from interrupt context */
349 static int admhc_restart(struct admhcd *ahcd)
353 struct urb_priv *priv;
355 /* mark any devices gone, so they do nothing till khubd disconnects.
356 * recycle any "live" eds/tds (and urbs) right away.
357 * later, khubd disconnect processing will recycle the other state,
358 * (either as disconnect/reconnect, or maybe someday as a reset).
360 spin_lock_irq(&ahcd->lock);
362 usb_root_hub_lost_power(admhcd_to_hcd(ahcd)->self.root_hub);
363 if (!list_empty(&ahcd->pending))
364 admhc_dbg(ahcd, "abort schedule...\n");
365 list_for_each_entry(priv, &ahcd->pending, pending) {
366 struct urb *urb = priv->td[0]->urb;
367 struct ed *ed = priv->ed;
371 ed->state = ED_UNLINK;
372 ed->hwINFO |= cpu_to_hc32(ahcd, ED_DEQUEUE);
373 ed_deschedule (ahcd, ed);
375 ed->ed_next = ahcd->ed_rm_list;
377 ahcd->ed_rm_list = ed;
382 admhc_dbg(ahcd, "bogus ed %p state %d\n",
387 urb->unlinked = -ESHUTDOWN;
389 finish_unlinks(ahcd, 0);
390 spin_unlock_irq(&ahcd->lock);
392 /* paranoia, in case that didn't work: */
394 /* empty the interrupt branches */
395 for (i = 0; i < NUM_INTS; i++) ahcd->load[i] = 0;
396 for (i = 0; i < NUM_INTS; i++) ahcd->hcca->int_table[i] = 0;
398 /* no EDs to remove */
399 ahcd->ed_rm_list = NULL;
401 /* empty control and bulk lists */
402 ahcd->ed_controltail = NULL;
403 ahcd->ed_bulktail = NULL;
405 if ((temp = admhc_run(ahcd)) < 0) {
406 admhc_err(ahcd, "can't restart, %d\n", temp);
409 /* here we "know" root ports should always stay powered,
410 * and that if we try to turn them back on the root hub
411 * will respond to CSC processing.
415 admhc_writel(ahcd, RH_PS_PSS,
416 &ahcd->regs->portstatus[i]);
417 admhc_dbg(ahcd, "restart complete\n");
422 #else /* CONFIG_PM */
424 static inline int admhc_rh_resume(struct admhcd *ahcd)
429 /* Carry out polling-related state changes.
430 * autostop isn't used when CONFIG_PM is turned off.
432 static int admhc_root_hub_state_changes(struct admhcd *ahcd, int changed,
435 /* If INSM is enabled, don't poll */
436 if (admhc_readl(ahcd, &ahcd->regs->int_enable) & ADMHC_INTR_INSM)
439 /* If no status changes are pending, enable status-change interrupts */
441 admhc_intr_enable(ahcd, ADMHC_INTR_INSM);
448 #endif /* CONFIG_PM */