2 * OHCI HCD (Host Controller Driver) for USB.
4 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5 * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
7 * This file is licenced under GPL
10 /*-------------------------------------------------------------------------*/
13 * OHCI Root Hub ... the nonsharable stuff
15 * Registers don't need cpu_to_le32, that happens transparently
18 /* AMD-756 (D2 rev) reports corrupt register contents in some cases.
19 * The erratum (#4) description is incorrect. AMD's workaround waits
20 * till some bits (mostly reserved) are clear; ok for all revs.
22 #define read_roothub(hc, register, mask) ({ \
23 u32 temp = ohci_readl (&hc->regs->roothub.register); \
26 else if (hc->flags & OHCI_QUIRK_AMD756) \
28 temp = ohci_readl (&hc->regs->roothub.register); \
31 static u32 roothub_a (struct ohci_hcd *hc)
32 { return read_roothub (hc, a, 0xfc0fe000); }
33 static inline u32 roothub_b (struct ohci_hcd *hc)
34 { return ohci_readl (&hc->regs->roothub.b); }
35 static inline u32 roothub_status (struct ohci_hcd *hc)
36 { return ohci_readl (&hc->regs->roothub.status); }
37 static u32 roothub_portstatus (struct ohci_hcd *hc, int i)
38 { return read_roothub (hc, portstatus [i], 0xffe0fce0); }
40 /*-------------------------------------------------------------------------*/
42 #define dbg_port(hc,label,num,value) \
44 "%s roothub.portstatus [%d] " \
45 "= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
47 (temp & RH_PS_PRSC) ? " PRSC" : "", \
48 (temp & RH_PS_OCIC) ? " OCIC" : "", \
49 (temp & RH_PS_PSSC) ? " PSSC" : "", \
50 (temp & RH_PS_PESC) ? " PESC" : "", \
51 (temp & RH_PS_CSC) ? " CSC" : "", \
53 (temp & RH_PS_LSDA) ? " LSDA" : "", \
54 (temp & RH_PS_PPS) ? " PPS" : "", \
55 (temp & RH_PS_PRS) ? " PRS" : "", \
56 (temp & RH_PS_POCI) ? " POCI" : "", \
57 (temp & RH_PS_PSS) ? " PSS" : "", \
59 (temp & RH_PS_PES) ? " PES" : "", \
60 (temp & RH_PS_CCS) ? " CCS" : "" \
63 /*-------------------------------------------------------------------------*/
65 #if defined(CONFIG_USB_SUSPEND) || defined(CONFIG_PM)
67 #define OHCI_SCHED_ENABLES \
68 (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
70 static void dl_done_list (struct ohci_hcd *, struct pt_regs *);
71 static void finish_unlinks (struct ohci_hcd *, u16 , struct pt_regs *);
73 static int ohci_hub_suspend (struct usb_hcd *hcd)
75 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
76 struct usb_device *root = hcd_to_bus (&ohci->hcd)->root_hub;
79 if (root->dev.power.power_state != 0)
81 if (time_before (jiffies, ohci->next_statechange))
84 spin_lock_irq (&ohci->lock);
86 ohci->hc_control = ohci_readl (&ohci->regs->control);
87 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
89 ohci_dbg (ohci, "resume/suspend?\n");
90 ohci->hc_control &= ~OHCI_CTRL_HCFS;
91 ohci->hc_control |= OHCI_USB_RESET;
92 writel (ohci->hc_control, &ohci->regs->control);
93 (void) ohci_readl (&ohci->regs->control);
97 ohci_dbg (ohci, "needs reinit!\n");
99 case OHCI_USB_SUSPEND:
100 ohci_dbg (ohci, "already suspended?\n");
103 ohci_dbg (ohci, "suspend root hub\n");
105 /* First stop any processing */
106 ohci->hcd.state = USB_STATE_QUIESCING;
107 if (ohci->hc_control & OHCI_SCHED_ENABLES) {
110 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
111 writel (ohci->hc_control, &ohci->regs->control);
112 ohci->hc_control = ohci_readl (&ohci->regs->control);
113 writel (OHCI_INTR_SF, &ohci->regs->intrstatus);
115 /* sched disables take effect on the next frame,
116 * then the last WDH could take 6+ msec
118 ohci_dbg (ohci, "stopping schedules ...\n");
123 if (ohci_readl (&ohci->regs->intrstatus) & OHCI_INTR_SF)
126 dl_done_list (ohci, NULL);
129 dl_done_list (ohci, NULL);
130 finish_unlinks (ohci, OHCI_FRAME_NO(ohci->hcca), NULL);
131 writel (ohci_readl (&ohci->regs->intrstatus), &ohci->regs->intrstatus);
133 /* maybe resume can wake root hub */
134 if (ohci->hcd.remote_wakeup)
135 ohci->hc_control |= OHCI_CTRL_RWE;
137 ohci->hc_control &= ~OHCI_CTRL_RWE;
140 ohci->hc_control &= ~OHCI_CTRL_HCFS;
141 ohci->hc_control |= OHCI_USB_SUSPEND;
142 writel (ohci->hc_control, &ohci->regs->control);
143 (void) ohci_readl (&ohci->regs->control);
145 /* no resumes until devices finish suspending */
146 ohci->next_statechange = jiffies + msecs_to_jiffies (5);
149 /* it's not USB_STATE_SUSPENDED unless access to this
150 * hub from the non-usb side (PCI, SOC, etc) stopped
152 root->dev.power.power_state = 3;
154 spin_unlock_irq (&ohci->lock);
158 static inline struct ed *find_head (struct ed *ed)
160 /* for bulk and control lists */
166 static int hc_restart (struct ohci_hcd *ohci);
168 /* caller owns root->serialize */
169 static int ohci_hub_resume (struct usb_hcd *hcd)
171 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
172 struct usb_device *root = hcd_to_bus (&ohci->hcd)->root_hub;
174 int status = -EINPROGRESS;
176 if (!root->dev.power.power_state)
178 if (time_before (jiffies, ohci->next_statechange))
181 spin_lock_irq (&ohci->lock);
182 ohci->hc_control = ohci_readl (&ohci->regs->control);
183 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
184 case OHCI_USB_SUSPEND:
185 ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
186 ohci->hc_control |= OHCI_USB_RESUME;
187 writel (ohci->hc_control, &ohci->regs->control);
188 (void) ohci_readl (&ohci->regs->control);
189 ohci_dbg (ohci, "resume root hub\n");
191 case OHCI_USB_RESUME:
192 /* HCFS changes sometime after INTR_RD */
193 ohci_info (ohci, "wakeup\n");
196 ohci_dbg (ohci, "odd resume\n");
197 root->dev.power.power_state = 0;
200 default: /* RESET, we lost power */
201 ohci_dbg (ohci, "root hub hardware reset\n");
204 spin_unlock_irq (&ohci->lock);
205 if (status == -EBUSY)
206 return hc_restart (ohci);
207 if (status != -EINPROGRESS)
210 temp = roothub_a (ohci) & RH_A_NDP;
213 u32 stat = ohci_readl (&ohci->regs->roothub.portstatus [temp]);
215 /* force global, not selective, resume */
216 if (!(stat & RH_PS_PSS))
218 writel (RH_PS_POCI, &ohci->regs->roothub.portstatus [temp]);
221 /* Some controllers (lucent) need extra-long delays */
222 ohci->hcd.state = USB_STATE_RESUMING;
223 mdelay (20 /* usb 11.5.1.10 */ + 15);
225 temp = ohci_readl (&ohci->regs->control);
226 temp &= OHCI_CTRL_HCFS;
227 if (temp != OHCI_USB_RESUME) {
228 ohci_err (ohci, "controller won't resume\n");
232 /* disable old schedule state, reinit from scratch */
233 writel (0, &ohci->regs->ed_controlhead);
234 writel (0, &ohci->regs->ed_controlcurrent);
235 writel (0, &ohci->regs->ed_bulkhead);
236 writel (0, &ohci->regs->ed_bulkcurrent);
237 writel (0, &ohci->regs->ed_periodcurrent);
238 writel ((u32) ohci->hcca_dma, &ohci->regs->hcca);
240 periodic_reinit (ohci);
242 /* interrupts might have been disabled */
243 writel (OHCI_INTR_INIT, &ohci->regs->intrenable);
244 if (ohci->ed_rm_list)
245 writel (OHCI_INTR_SF, &ohci->regs->intrenable);
246 writel (ohci_readl (&ohci->regs->intrstatus), &ohci->regs->intrstatus);
248 /* Then re-enable operations */
249 writel (OHCI_USB_OPER, &ohci->regs->control);
250 (void) ohci_readl (&ohci->regs->control);
253 temp = OHCI_CONTROL_INIT | OHCI_USB_OPER;
254 if (ohci->hcd.can_wakeup)
255 temp |= OHCI_CTRL_RWC;
256 ohci->hc_control = temp;
257 writel (temp, &ohci->regs->control);
258 (void) ohci_readl (&ohci->regs->control);
262 root->dev.power.power_state = 0;
264 /* keep it alive for ~5x suspend + resume costs */
265 ohci->next_statechange = jiffies + msecs_to_jiffies (250);
267 /* maybe turn schedules back on */
270 if (!ohci->ed_rm_list) {
271 if (ohci->ed_controltail) {
272 writel (find_head (ohci->ed_controltail)->dma,
273 &ohci->regs->ed_controlhead);
274 enables |= OHCI_CTRL_CLE;
277 if (ohci->ed_bulktail) {
278 writel (find_head (ohci->ed_bulktail)->dma,
279 &ohci->regs->ed_bulkhead);
280 enables |= OHCI_CTRL_BLE;
284 if (hcd_to_bus (&ohci->hcd)->bandwidth_isoc_reqs
285 || hcd_to_bus (&ohci->hcd)->bandwidth_int_reqs)
286 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
288 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
289 ohci->hc_control |= enables;
290 writel (ohci->hc_control, &ohci->regs->control);
292 writel (status, &ohci->regs->cmdstatus);
293 (void) ohci_readl (&ohci->regs->control);
296 ohci->hcd.state = USB_STATE_RUNNING;
300 static void ohci_rh_resume (void *_hcd)
302 struct usb_hcd *hcd = _hcd;
304 down (&hcd->self.root_hub->serialize);
305 (void) ohci_hub_resume (hcd);
306 up (&hcd->self.root_hub->serialize);
311 static void ohci_rh_resume (void *_hcd)
313 struct ohci_hcd *ohci = hcd_to_ohci (_hcd);
314 ohci_dbg(ohci, "rh_resume ??\n");
317 #endif /* CONFIG_USB_SUSPEND || CONFIG_PM */
319 /*-------------------------------------------------------------------------*/
321 /* build "status change" packet (one or two bytes) from HC registers */
324 ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
326 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
327 int ports, i, changed = 0, length = 1;
330 ports = roothub_a (ohci) & RH_A_NDP;
331 if (ports > MAX_ROOT_PORTS) {
332 if (!HCD_IS_RUNNING(ohci->hcd.state))
334 ohci_err (ohci, "bogus NDP=%d, rereads as NDP=%d\n",
335 ports, ohci_readl (&ohci->regs->roothub.a) & RH_A_NDP);
336 /* retry later; "should not happen" */
341 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
342 buf [0] = changed = 1;
350 /* look at each port */
351 for (i = 0; i < ports; i++) {
352 u32 status = roothub_portstatus (ohci, i);
354 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
355 | RH_PS_OCIC | RH_PS_PRSC)) {
358 buf [0] |= 1 << (i + 1);
360 buf [1] |= 1 << (i - 7);
364 /* can suspend if no ports are enabled; or if all all
365 * enabled ports are suspended AND remote wakeup is on.
367 if (!(status & RH_PS_CCS))
369 if ((status & RH_PS_PSS) && ohci->hcd.remote_wakeup)
375 /* save power by suspending idle root hubs;
376 * INTR_RD wakes us when there's work
381 && ((OHCI_CTRL_HCFS | OHCI_SCHED_ENABLES)
384 && down_trylock (&hcd->self.root_hub->serialize) == 0
386 ohci_vdbg (ohci, "autosuspend\n");
387 (void) ohci_hub_suspend (&ohci->hcd);
388 ohci->hcd.state = USB_STATE_RUNNING;
389 up (&hcd->self.root_hub->serialize);
393 return changed ? length : 0;
396 /*-------------------------------------------------------------------------*/
399 ohci_hub_descriptor (
400 struct ohci_hcd *ohci,
401 struct usb_hub_descriptor *desc
403 u32 rh = roothub_a (ohci);
404 int ports = rh & RH_A_NDP;
407 desc->bDescriptorType = 0x29;
408 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
409 desc->bHubContrCurrent = 0;
411 desc->bNbrPorts = ports;
412 temp = 1 + (ports / 8);
413 desc->bDescLength = 7 + 2 * temp;
416 if (rh & RH_A_NPS) /* no power switching? */
418 if (rh & RH_A_PSM) /* per-port power switching? */
420 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
422 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
424 desc->wHubCharacteristics = cpu_to_le16 (temp);
426 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
427 rh = roothub_b (ohci);
428 desc->bitmap [0] = rh & RH_B_DR;
430 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
431 desc->bitmap [2] = desc->bitmap [3] = 0xff;
433 desc->bitmap [1] = 0xff;
436 /*-------------------------------------------------------------------------*/
438 #ifdef CONFIG_USB_OTG
440 static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
442 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
449 /* start port reset before HNP protocol times out */
450 status = ohci_readl(&ohci->regs->roothub.portstatus [port]);
451 if (!(status & RH_PS_CCS))
454 /* khubd will finish the reset later */
455 writel(RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
459 static void start_hnp(struct ohci_hcd *ohci);
463 #define ohci_start_port_reset NULL
467 /*-------------------------------------------------------------------------*/
470 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
471 * not necessarily continuous ... to guard against resume signaling.
472 * The short timeout is safe for non-root hubs, and is backward-compatible
473 * with earlier Linux hosts.
475 #ifdef CONFIG_USB_SUSPEND
476 #define PORT_RESET_MSEC 50
478 #define PORT_RESET_MSEC 10
481 /* this timer value might be vendor-specific ... */
482 #define PORT_RESET_HW_MSEC 10
484 /* wrap-aware logic stolen from <linux/jiffies.h> */
485 #define tick_before(t1,t2) ((((s16)(t1))-((s16)(t2))) < 0)
487 /* called from some task, normally khubd */
488 static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
490 u32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
492 u16 now = readl(&ohci->regs->fmnumber);
493 u16 reset_done = now + PORT_RESET_MSEC;
495 /* build a "continuous enough" reset signal, with up to
496 * 3msec gap between pulses. scheduler HZ==100 must work;
497 * this might need to be deadline-scheduled.
500 /* spin until any current reset finishes */
502 temp = ohci_readl (portstat);
503 if (!(temp & RH_PS_PRS))
508 if (!(temp & RH_PS_CCS))
510 if (temp & RH_PS_PRSC)
511 writel (RH_PS_PRSC, portstat);
513 /* start the next reset, sleep till it's probably done */
514 writel (RH_PS_PRS, portstat);
515 msleep(PORT_RESET_HW_MSEC);
516 now = readl(&ohci->regs->fmnumber);
517 } while (tick_before(now, reset_done));
518 /* caller synchronizes using PRSC */
521 static int ohci_hub_control (
529 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
530 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
535 case ClearHubFeature:
537 case C_HUB_OVER_CURRENT:
538 writel (RH_HS_OCIC, &ohci->regs->roothub.status);
539 case C_HUB_LOCAL_POWER:
545 case ClearPortFeature:
546 if (!wIndex || wIndex > ports)
551 case USB_PORT_FEAT_ENABLE:
554 case USB_PORT_FEAT_C_ENABLE:
557 case USB_PORT_FEAT_SUSPEND:
559 if ((ohci->hc_control & OHCI_CTRL_HCFS)
561 schedule_work (&ohci->rh_resume);
563 case USB_PORT_FEAT_C_SUSPEND:
566 case USB_PORT_FEAT_POWER:
569 case USB_PORT_FEAT_C_CONNECTION:
572 case USB_PORT_FEAT_C_OVER_CURRENT:
575 case USB_PORT_FEAT_C_RESET:
581 writel (temp, &ohci->regs->roothub.portstatus [wIndex]);
582 // ohci_readl (&ohci->regs->roothub.portstatus [wIndex]);
584 case GetHubDescriptor:
585 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
588 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
589 *(__le32 *) buf = cpu_to_le32 (temp);
592 if (!wIndex || wIndex > ports)
595 temp = roothub_portstatus (ohci, wIndex);
596 *(__le32 *) buf = cpu_to_le32 (temp);
598 #ifndef OHCI_VERBOSE_DEBUG
599 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
601 dbg_port (ohci, "GetStatus", wIndex, temp);
605 case C_HUB_OVER_CURRENT:
606 // FIXME: this can be cleared, yes?
607 case C_HUB_LOCAL_POWER:
614 if (!wIndex || wIndex > ports)
618 case USB_PORT_FEAT_SUSPEND:
619 #ifdef CONFIG_USB_OTG
620 if (ohci->hcd.self.otg_port == (wIndex + 1)
621 && ohci->hcd.self.b_hnp_enable)
626 &ohci->regs->roothub.portstatus [wIndex]);
628 case USB_PORT_FEAT_POWER:
630 &ohci->regs->roothub.portstatus [wIndex]);
632 case USB_PORT_FEAT_RESET:
633 root_port_reset (ohci, wIndex);
642 /* "protocol stall" on error */