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 /*-------------------------------------------------------------------------*/ 11 12 /* 13 * OHCI Root Hub ... the nonsharable stuff 14 */ 15 16 #define dbg_port(hc,label,num,value) \ 17 ohci_dbg (hc, \ 18 "%s roothub.portstatus [%d] " \ 19 "= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \ 20 label, num, temp, \ 21 (temp & RH_PS_PRSC) ? " PRSC" : "", \ 22 (temp & RH_PS_OCIC) ? " OCIC" : "", \ 23 (temp & RH_PS_PSSC) ? " PSSC" : "", \ 24 (temp & RH_PS_PESC) ? " PESC" : "", \ 25 (temp & RH_PS_CSC) ? " CSC" : "", \ 26 \ 27 (temp & RH_PS_LSDA) ? " LSDA" : "", \ 28 (temp & RH_PS_PPS) ? " PPS" : "", \ 29 (temp & RH_PS_PRS) ? " PRS" : "", \ 30 (temp & RH_PS_POCI) ? " POCI" : "", \ 31 (temp & RH_PS_PSS) ? " PSS" : "", \ 32 \ 33 (temp & RH_PS_PES) ? " PES" : "", \ 34 (temp & RH_PS_CCS) ? " CCS" : "" \ 35 ); 36 37 /*-------------------------------------------------------------------------*/ 38 39 #ifdef CONFIG_PM 40 41 #define OHCI_SCHED_ENABLES \ 42 (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE) 43 44 static void dl_done_list (struct ohci_hcd *, struct pt_regs *); 45 static void finish_unlinks (struct ohci_hcd *, u16 , struct pt_regs *); 46 static int ohci_restart (struct ohci_hcd *ohci); 47 48 static int ohci_bus_suspend (struct usb_hcd *hcd) 49 { 50 struct ohci_hcd *ohci = hcd_to_ohci (hcd); 51 int status = 0; 52 unsigned long flags; 53 54 spin_lock_irqsave (&ohci->lock, flags); 55 56 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control); 57 switch (ohci->hc_control & OHCI_CTRL_HCFS) { 58 case OHCI_USB_RESUME: 59 ohci_dbg (ohci, "resume/suspend?\n"); 60 ohci->hc_control &= ~OHCI_CTRL_HCFS; 61 ohci->hc_control |= OHCI_USB_RESET; 62 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); 63 (void) ohci_readl (ohci, &ohci->regs->control); 64 /* FALL THROUGH */ 65 case OHCI_USB_RESET: 66 status = -EBUSY; 67 ohci_dbg (ohci, "needs reinit!\n"); 68 goto done; 69 case OHCI_USB_SUSPEND: 70 ohci_dbg (ohci, "already suspended\n"); 71 goto done; 72 } 73 ohci_dbg (ohci, "suspend root hub\n"); 74 75 /* First stop any processing */ 76 if (ohci->hc_control & OHCI_SCHED_ENABLES) { 77 int limit; 78 79 ohci->hc_control &= ~OHCI_SCHED_ENABLES; 80 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); 81 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control); 82 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus); 83 84 /* sched disables take effect on the next frame, 85 * then the last WDH could take 6+ msec 86 */ 87 ohci_dbg (ohci, "stopping schedules ...\n"); 88 limit = 2000; 89 while (limit > 0) { 90 udelay (250); 91 limit =- 250; 92 if (ohci_readl (ohci, &ohci->regs->intrstatus) 93 & OHCI_INTR_SF) 94 break; 95 } 96 dl_done_list (ohci, NULL); 97 mdelay (7); 98 } 99 dl_done_list (ohci, NULL); 100 finish_unlinks (ohci, ohci_frame_no(ohci), NULL); 101 ohci_writel (ohci, ohci_readl (ohci, &ohci->regs->intrstatus), 102 &ohci->regs->intrstatus); 103 104 /* maybe resume can wake root hub */ 105 if (hcd->remote_wakeup) 106 ohci->hc_control |= OHCI_CTRL_RWE; 107 else 108 ohci->hc_control &= ~OHCI_CTRL_RWE; 109 110 /* Suspend hub ... this is the "global (to this bus) suspend" mode, 111 * which doesn't imply ports will first be individually suspended. 112 */ 113 ohci->hc_control &= ~OHCI_CTRL_HCFS; 114 ohci->hc_control |= OHCI_USB_SUSPEND; 115 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); 116 (void) ohci_readl (ohci, &ohci->regs->control); 117 118 /* no resumes until devices finish suspending */ 119 ohci->next_statechange = jiffies + msecs_to_jiffies (5); 120 121 done: 122 /* external suspend vs self autosuspend ... same effect */ 123 if (status == 0) 124 usb_hcd_suspend_root_hub(hcd); 125 spin_unlock_irqrestore (&ohci->lock, flags); 126 return status; 127 } 128 129 static inline struct ed *find_head (struct ed *ed) 130 { 131 /* for bulk and control lists */ 132 while (ed->ed_prev) 133 ed = ed->ed_prev; 134 return ed; 135 } 136 137 /* caller has locked the root hub */ 138 static int ohci_bus_resume (struct usb_hcd *hcd) 139 { 140 struct ohci_hcd *ohci = hcd_to_ohci (hcd); 141 u32 temp, enables; 142 int status = -EINPROGRESS; 143 144 if (time_before (jiffies, ohci->next_statechange)) 145 msleep(5); 146 147 spin_lock_irq (&ohci->lock); 148 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control); 149 150 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) { 151 /* this can happen after resuming a swsusp snapshot */ 152 if (hcd->state == HC_STATE_RESUMING) { 153 ohci_dbg (ohci, "BIOS/SMM active, control %03x\n", 154 ohci->hc_control); 155 status = -EBUSY; 156 /* this happens when pmcore resumes HC then root */ 157 } else { 158 ohci_dbg (ohci, "duplicate resume\n"); 159 status = 0; 160 } 161 } else switch (ohci->hc_control & OHCI_CTRL_HCFS) { 162 case OHCI_USB_SUSPEND: 163 ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES); 164 ohci->hc_control |= OHCI_USB_RESUME; 165 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); 166 (void) ohci_readl (ohci, &ohci->regs->control); 167 ohci_dbg (ohci, "resume root hub\n"); 168 break; 169 case OHCI_USB_RESUME: 170 /* HCFS changes sometime after INTR_RD */ 171 ohci_info (ohci, "wakeup\n"); 172 break; 173 case OHCI_USB_OPER: 174 /* this can happen after resuming a swsusp snapshot */ 175 ohci_dbg (ohci, "snapshot resume? reinit\n"); 176 status = -EBUSY; 177 break; 178 default: /* RESET, we lost power */ 179 ohci_dbg (ohci, "lost power\n"); 180 status = -EBUSY; 181 } 182 spin_unlock_irq (&ohci->lock); 183 if (status == -EBUSY) { 184 (void) ohci_init (ohci); 185 return ohci_restart (ohci); 186 } 187 if (status != -EINPROGRESS) 188 return status; 189 190 temp = ohci->num_ports; 191 enables = 0; 192 while (temp--) { 193 u32 stat = ohci_readl (ohci, 194 &ohci->regs->roothub.portstatus [temp]); 195 196 /* force global, not selective, resume */ 197 if (!(stat & RH_PS_PSS)) 198 continue; 199 ohci_writel (ohci, RH_PS_POCI, 200 &ohci->regs->roothub.portstatus [temp]); 201 } 202 203 /* Some controllers (lucent erratum) need extra-long delays */ 204 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1); 205 206 temp = ohci_readl (ohci, &ohci->regs->control); 207 temp &= OHCI_CTRL_HCFS; 208 if (temp != OHCI_USB_RESUME) { 209 ohci_err (ohci, "controller won't resume\n"); 210 return -EBUSY; 211 } 212 213 /* disable old schedule state, reinit from scratch */ 214 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead); 215 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent); 216 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead); 217 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent); 218 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent); 219 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca); 220 221 /* Sometimes PCI D3 suspend trashes frame timings ... */ 222 periodic_reinit (ohci); 223 224 /* interrupts might have been disabled */ 225 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable); 226 if (ohci->ed_rm_list) 227 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable); 228 ohci_writel (ohci, ohci_readl (ohci, &ohci->regs->intrstatus), 229 &ohci->regs->intrstatus); 230 231 /* Then re-enable operations */ 232 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control); 233 (void) ohci_readl (ohci, &ohci->regs->control); 234 msleep (3); 235 236 temp = OHCI_CONTROL_INIT | OHCI_USB_OPER; 237 if (hcd->can_wakeup) 238 temp |= OHCI_CTRL_RWC; 239 ohci->hc_control = temp; 240 ohci_writel (ohci, temp, &ohci->regs->control); 241 (void) ohci_readl (ohci, &ohci->regs->control); 242 243 /* TRSMRCY */ 244 msleep (10); 245 246 /* keep it alive for ~5x suspend + resume costs */ 247 ohci->next_statechange = jiffies + msecs_to_jiffies (250); 248 249 /* maybe turn schedules back on */ 250 enables = 0; 251 temp = 0; 252 if (!ohci->ed_rm_list) { 253 if (ohci->ed_controltail) { 254 ohci_writel (ohci, 255 find_head (ohci->ed_controltail)->dma, 256 &ohci->regs->ed_controlhead); 257 enables |= OHCI_CTRL_CLE; 258 temp |= OHCI_CLF; 259 } 260 if (ohci->ed_bulktail) { 261 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma, 262 &ohci->regs->ed_bulkhead); 263 enables |= OHCI_CTRL_BLE; 264 temp |= OHCI_BLF; 265 } 266 } 267 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs) 268 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE; 269 if (enables) { 270 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables); 271 ohci->hc_control |= enables; 272 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); 273 if (temp) 274 ohci_writel (ohci, temp, &ohci->regs->cmdstatus); 275 (void) ohci_readl (ohci, &ohci->regs->control); 276 } 277 278 return 0; 279 } 280 281 #endif /* CONFIG_PM */ 282 283 /*-------------------------------------------------------------------------*/ 284 285 /* build "status change" packet (one or two bytes) from HC registers */ 286 287 static int 288 ohci_hub_status_data (struct usb_hcd *hcd, char *buf) 289 { 290 struct ohci_hcd *ohci = hcd_to_ohci (hcd); 291 int i, changed = 0, length = 1; 292 int can_suspend = hcd->can_wakeup; 293 unsigned long flags; 294 295 spin_lock_irqsave (&ohci->lock, flags); 296 297 /* handle autosuspended root: finish resuming before 298 * letting khubd or root hub timer see state changes. 299 */ 300 if ((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER 301 || !HC_IS_RUNNING(hcd->state)) { 302 can_suspend = 0; 303 goto done; 304 } 305 306 /* undocumented erratum seen on at least rev D */ 307 if ((ohci->flags & OHCI_QUIRK_AMD756) 308 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) { 309 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n", 310 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP); 311 /* retry later; "should not happen" */ 312 goto done; 313 } 314 315 /* init status */ 316 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC)) 317 buf [0] = changed = 1; 318 else 319 buf [0] = 0; 320 if (ohci->num_ports > 7) { 321 buf [1] = 0; 322 length++; 323 } 324 325 /* look at each port */ 326 for (i = 0; i < ohci->num_ports; i++) { 327 u32 status = roothub_portstatus (ohci, i); 328 329 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC 330 | RH_PS_OCIC | RH_PS_PRSC)) { 331 changed = 1; 332 if (i < 7) 333 buf [0] |= 1 << (i + 1); 334 else 335 buf [1] |= 1 << (i - 7); 336 continue; 337 } 338 339 /* can suspend if no ports are enabled; or if all all 340 * enabled ports are suspended AND remote wakeup is on. 341 */ 342 if (!(status & RH_PS_CCS)) 343 continue; 344 if ((status & RH_PS_PSS) && hcd->remote_wakeup) 345 continue; 346 can_suspend = 0; 347 } 348 done: 349 spin_unlock_irqrestore (&ohci->lock, flags); 350 351 #ifdef CONFIG_PM 352 /* save power by suspending idle root hubs; 353 * INTR_RD wakes us when there's work 354 */ 355 if (can_suspend 356 && !changed 357 && !ohci->ed_rm_list 358 && ((OHCI_CTRL_HCFS | OHCI_SCHED_ENABLES) 359 & ohci->hc_control) 360 == OHCI_USB_OPER 361 && time_after (jiffies, ohci->next_statechange) 362 && usb_trylock_device (hcd->self.root_hub) 363 ) { 364 ohci_vdbg (ohci, "autosuspend\n"); 365 (void) ohci_bus_suspend (hcd); 366 usb_unlock_device (hcd->self.root_hub); 367 } 368 #endif 369 370 return changed ? length : 0; 371 } 372 373 /*-------------------------------------------------------------------------*/ 374 375 static void 376 ohci_hub_descriptor ( 377 struct ohci_hcd *ohci, 378 struct usb_hub_descriptor *desc 379 ) { 380 u32 rh = roothub_a (ohci); 381 u16 temp; 382 383 desc->bDescriptorType = 0x29; 384 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24; 385 desc->bHubContrCurrent = 0; 386 387 desc->bNbrPorts = ohci->num_ports; 388 temp = 1 + (ohci->num_ports / 8); 389 desc->bDescLength = 7 + 2 * temp; 390 391 temp = 0; 392 if (rh & RH_A_NPS) /* no power switching? */ 393 temp |= 0x0002; 394 if (rh & RH_A_PSM) /* per-port power switching? */ 395 temp |= 0x0001; 396 if (rh & RH_A_NOCP) /* no overcurrent reporting? */ 397 temp |= 0x0010; 398 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */ 399 temp |= 0x0008; 400 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp); 401 402 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */ 403 rh = roothub_b (ohci); 404 memset(desc->bitmap, 0xff, sizeof(desc->bitmap)); 405 desc->bitmap [0] = rh & RH_B_DR; 406 if (ohci->num_ports > 7) { 407 desc->bitmap [1] = (rh & RH_B_DR) >> 8; 408 desc->bitmap [2] = 0xff; 409 } else 410 desc->bitmap [1] = 0xff; 411 } 412 413 /*-------------------------------------------------------------------------*/ 414 415 #ifdef CONFIG_USB_OTG 416 417 static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port) 418 { 419 struct ohci_hcd *ohci = hcd_to_ohci (hcd); 420 u32 status; 421 422 if (!port) 423 return -EINVAL; 424 port--; 425 426 /* start port reset before HNP protocol times out */ 427 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]); 428 if (!(status & RH_PS_CCS)) 429 return -ENODEV; 430 431 /* khubd will finish the reset later */ 432 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]); 433 return 0; 434 } 435 436 static void start_hnp(struct ohci_hcd *ohci); 437 438 #else 439 440 #define ohci_start_port_reset NULL 441 442 #endif 443 444 /*-------------------------------------------------------------------------*/ 445 446 447 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling, 448 * not necessarily continuous ... to guard against resume signaling. 449 * The short timeout is safe for non-root hubs, and is backward-compatible 450 * with earlier Linux hosts. 451 */ 452 #ifdef CONFIG_USB_SUSPEND 453 #define PORT_RESET_MSEC 50 454 #else 455 #define PORT_RESET_MSEC 10 456 #endif 457 458 /* this timer value might be vendor-specific ... */ 459 #define PORT_RESET_HW_MSEC 10 460 461 /* wrap-aware logic morphed from <linux/jiffies.h> */ 462 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0) 463 464 /* called from some task, normally khubd */ 465 static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port) 466 { 467 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port]; 468 u32 temp; 469 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber); 470 u16 reset_done = now + PORT_RESET_MSEC; 471 472 /* build a "continuous enough" reset signal, with up to 473 * 3msec gap between pulses. scheduler HZ==100 must work; 474 * this might need to be deadline-scheduled. 475 */ 476 do { 477 /* spin until any current reset finishes */ 478 for (;;) { 479 temp = ohci_readl (ohci, portstat); 480 if (!(temp & RH_PS_PRS)) 481 break; 482 udelay (500); 483 } 484 485 if (!(temp & RH_PS_CCS)) 486 break; 487 if (temp & RH_PS_PRSC) 488 ohci_writel (ohci, RH_PS_PRSC, portstat); 489 490 /* start the next reset, sleep till it's probably done */ 491 ohci_writel (ohci, RH_PS_PRS, portstat); 492 msleep(PORT_RESET_HW_MSEC); 493 now = ohci_readl(ohci, &ohci->regs->fmnumber); 494 } while (tick_before(now, reset_done)); 495 /* caller synchronizes using PRSC */ 496 } 497 498 static int ohci_hub_control ( 499 struct usb_hcd *hcd, 500 u16 typeReq, 501 u16 wValue, 502 u16 wIndex, 503 char *buf, 504 u16 wLength 505 ) { 506 struct ohci_hcd *ohci = hcd_to_ohci (hcd); 507 int ports = hcd_to_bus (hcd)->root_hub->maxchild; 508 u32 temp; 509 int retval = 0; 510 511 switch (typeReq) { 512 case ClearHubFeature: 513 switch (wValue) { 514 case C_HUB_OVER_CURRENT: 515 ohci_writel (ohci, RH_HS_OCIC, 516 &ohci->regs->roothub.status); 517 case C_HUB_LOCAL_POWER: 518 break; 519 default: 520 goto error; 521 } 522 break; 523 case ClearPortFeature: 524 if (!wIndex || wIndex > ports) 525 goto error; 526 wIndex--; 527 528 switch (wValue) { 529 case USB_PORT_FEAT_ENABLE: 530 temp = RH_PS_CCS; 531 break; 532 case USB_PORT_FEAT_C_ENABLE: 533 temp = RH_PS_PESC; 534 break; 535 case USB_PORT_FEAT_SUSPEND: 536 temp = RH_PS_POCI; 537 if ((ohci->hc_control & OHCI_CTRL_HCFS) 538 != OHCI_USB_OPER) 539 usb_hcd_resume_root_hub(hcd); 540 break; 541 case USB_PORT_FEAT_C_SUSPEND: 542 temp = RH_PS_PSSC; 543 break; 544 case USB_PORT_FEAT_POWER: 545 temp = RH_PS_LSDA; 546 break; 547 case USB_PORT_FEAT_C_CONNECTION: 548 temp = RH_PS_CSC; 549 break; 550 case USB_PORT_FEAT_C_OVER_CURRENT: 551 temp = RH_PS_OCIC; 552 break; 553 case USB_PORT_FEAT_C_RESET: 554 temp = RH_PS_PRSC; 555 break; 556 default: 557 goto error; 558 } 559 ohci_writel (ohci, temp, 560 &ohci->regs->roothub.portstatus [wIndex]); 561 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]); 562 break; 563 case GetHubDescriptor: 564 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf); 565 break; 566 case GetHubStatus: 567 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE); 568 *(__le32 *) buf = cpu_to_le32 (temp); 569 break; 570 case GetPortStatus: 571 if (!wIndex || wIndex > ports) 572 goto error; 573 wIndex--; 574 temp = roothub_portstatus (ohci, wIndex); 575 *(__le32 *) buf = cpu_to_le32 (temp); 576 577 #ifndef OHCI_VERBOSE_DEBUG 578 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */ 579 #endif 580 dbg_port (ohci, "GetStatus", wIndex, temp); 581 break; 582 case SetHubFeature: 583 switch (wValue) { 584 case C_HUB_OVER_CURRENT: 585 // FIXME: this can be cleared, yes? 586 case C_HUB_LOCAL_POWER: 587 break; 588 default: 589 goto error; 590 } 591 break; 592 case SetPortFeature: 593 if (!wIndex || wIndex > ports) 594 goto error; 595 wIndex--; 596 switch (wValue) { 597 case USB_PORT_FEAT_SUSPEND: 598 #ifdef CONFIG_USB_OTG 599 if (hcd->self.otg_port == (wIndex + 1) 600 && hcd->self.b_hnp_enable) 601 start_hnp(ohci); 602 else 603 #endif 604 ohci_writel (ohci, RH_PS_PSS, 605 &ohci->regs->roothub.portstatus [wIndex]); 606 break; 607 case USB_PORT_FEAT_POWER: 608 ohci_writel (ohci, RH_PS_PPS, 609 &ohci->regs->roothub.portstatus [wIndex]); 610 break; 611 case USB_PORT_FEAT_RESET: 612 root_port_reset (ohci, wIndex); 613 break; 614 default: 615 goto error; 616 } 617 break; 618 619 default: 620 error: 621 /* "protocol stall" on error */ 622 retval = -EPIPE; 623 } 624 return retval; 625 } 626 627