1 /* 2 * Most of this source has been derived from the Linux USB 3 * project: 4 * (C) Copyright Linus Torvalds 1999 5 * (C) Copyright Johannes Erdfelt 1999-2001 6 * (C) Copyright Andreas Gal 1999 7 * (C) Copyright Gregory P. Smith 1999 8 * (C) Copyright Deti Fliegl 1999 (new USB architecture) 9 * (C) Copyright Randy Dunlap 2000 10 * (C) Copyright David Brownell 2000 (kernel hotplug, usb_device_id) 11 * (C) Copyright Yggdrasil Computing, Inc. 2000 12 * (usb_device_id matching changes by Adam J. Richter) 13 * 14 * Adapted for U-Boot: 15 * (C) Copyright 2001 Denis Peter, MPL AG Switzerland 16 * 17 * SPDX-License-Identifier: GPL-2.0+ 18 */ 19 20 /**************************************************************************** 21 * HUB "Driver" 22 * Probes device for being a hub and configurate it 23 */ 24 25 #include <common.h> 26 #include <command.h> 27 #include <dm.h> 28 #include <errno.h> 29 #include <asm/processor.h> 30 #include <asm/unaligned.h> 31 #include <linux/ctype.h> 32 #include <asm/byteorder.h> 33 #include <asm/unaligned.h> 34 #include <dm/root.h> 35 36 DECLARE_GLOBAL_DATA_PTR; 37 38 #include <usb.h> 39 #ifdef CONFIG_4xx 40 #include <asm/4xx_pci.h> 41 #endif 42 43 #define USB_BUFSIZ 512 44 45 /* TODO(sjg@chromium.org): Remove this when CONFIG_DM_USB is defined */ 46 static struct usb_hub_device hub_dev[USB_MAX_HUB]; 47 static int usb_hub_index; 48 49 __weak void usb_hub_reset_devices(int port) 50 { 51 return; 52 } 53 54 static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size) 55 { 56 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 57 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, 58 USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT); 59 } 60 61 static int usb_clear_port_feature(struct usb_device *dev, int port, int feature) 62 { 63 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 64 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, 65 port, NULL, 0, USB_CNTL_TIMEOUT); 66 } 67 68 static int usb_set_port_feature(struct usb_device *dev, int port, int feature) 69 { 70 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 71 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, 72 port, NULL, 0, USB_CNTL_TIMEOUT); 73 } 74 75 static int usb_get_hub_status(struct usb_device *dev, void *data) 76 { 77 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 78 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0, 79 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT); 80 } 81 82 static int usb_get_port_status(struct usb_device *dev, int port, void *data) 83 { 84 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 85 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port, 86 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT); 87 } 88 89 90 static void usb_hub_power_on(struct usb_hub_device *hub) 91 { 92 int i; 93 struct usb_device *dev; 94 unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2; 95 const char *env; 96 97 dev = hub->pusb_dev; 98 99 debug("enabling power on all ports\n"); 100 for (i = 0; i < dev->maxchild; i++) { 101 usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER); 102 debug("port %d returns %lX\n", i + 1, dev->status); 103 } 104 105 /* 106 * Wait for power to become stable, 107 * plus spec-defined max time for device to connect 108 * but allow this time to be increased via env variable as some 109 * devices break the spec and require longer warm-up times 110 */ 111 env = getenv("usb_pgood_delay"); 112 if (env) 113 pgood_delay = max(pgood_delay, 114 (unsigned)simple_strtol(env, NULL, 0)); 115 debug("pgood_delay=%dms\n", pgood_delay); 116 mdelay(pgood_delay + 1000); 117 } 118 119 void usb_hub_reset(void) 120 { 121 usb_hub_index = 0; 122 } 123 124 static struct usb_hub_device *usb_hub_allocate(void) 125 { 126 if (usb_hub_index < USB_MAX_HUB) 127 return &hub_dev[usb_hub_index++]; 128 129 printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB); 130 return NULL; 131 } 132 133 #define MAX_TRIES 5 134 135 static inline char *portspeed(int portstatus) 136 { 137 char *speed_str; 138 139 switch (portstatus & USB_PORT_STAT_SPEED_MASK) { 140 case USB_PORT_STAT_SUPER_SPEED: 141 speed_str = "5 Gb/s"; 142 break; 143 case USB_PORT_STAT_HIGH_SPEED: 144 speed_str = "480 Mb/s"; 145 break; 146 case USB_PORT_STAT_LOW_SPEED: 147 speed_str = "1.5 Mb/s"; 148 break; 149 default: 150 speed_str = "12 Mb/s"; 151 break; 152 } 153 154 return speed_str; 155 } 156 157 int legacy_hub_port_reset(struct usb_device *dev, int port, 158 unsigned short *portstat) 159 { 160 int err, tries; 161 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1); 162 unsigned short portstatus, portchange; 163 164 #ifdef CONFIG_DM_USB 165 debug("%s: resetting '%s' port %d...\n", __func__, dev->dev->name, 166 port + 1); 167 #else 168 debug("%s: resetting port %d...\n", __func__, port + 1); 169 #endif 170 for (tries = 0; tries < MAX_TRIES; tries++) { 171 err = usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET); 172 if (err < 0) 173 return err; 174 175 mdelay(200); 176 177 if (usb_get_port_status(dev, port + 1, portsts) < 0) { 178 debug("get_port_status failed status %lX\n", 179 dev->status); 180 return -1; 181 } 182 portstatus = le16_to_cpu(portsts->wPortStatus); 183 portchange = le16_to_cpu(portsts->wPortChange); 184 185 debug("portstatus %x, change %x, %s\n", portstatus, portchange, 186 portspeed(portstatus)); 187 188 debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \ 189 " USB_PORT_STAT_ENABLE %d\n", 190 (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0, 191 (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0, 192 (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0); 193 194 /* 195 * Perhaps we should check for the following here: 196 * - C_CONNECTION hasn't been set. 197 * - CONNECTION is still set. 198 * 199 * Doing so would ensure that the device is still connected 200 * to the bus, and hasn't been unplugged or replaced while the 201 * USB bus reset was going on. 202 * 203 * However, if we do that, then (at least) a San Disk Ultra 204 * USB 3.0 16GB device fails to reset on (at least) an NVIDIA 205 * Tegra Jetson TK1 board. For some reason, the device appears 206 * to briefly drop off the bus when this second bus reset is 207 * executed, yet if we retry this loop, it'll eventually come 208 * back after another reset or two. 209 */ 210 211 if (portstatus & USB_PORT_STAT_ENABLE) 212 break; 213 214 mdelay(200); 215 } 216 217 if (tries == MAX_TRIES) { 218 debug("Cannot enable port %i after %i retries, " \ 219 "disabling port.\n", port + 1, MAX_TRIES); 220 debug("Maybe the USB cable is bad?\n"); 221 return -1; 222 } 223 224 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET); 225 *portstat = portstatus; 226 return 0; 227 } 228 229 #ifdef CONFIG_DM_USB 230 int hub_port_reset(struct udevice *dev, int port, unsigned short *portstat) 231 { 232 struct usb_device *udev = dev_get_parentdata(dev); 233 234 return legacy_hub_port_reset(udev, port, portstat); 235 } 236 #endif 237 238 int usb_hub_port_connect_change(struct usb_device *dev, int port) 239 { 240 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1); 241 unsigned short portstatus; 242 int ret, speed; 243 244 /* Check status */ 245 ret = usb_get_port_status(dev, port + 1, portsts); 246 if (ret < 0) { 247 debug("get_port_status failed\n"); 248 return ret; 249 } 250 251 portstatus = le16_to_cpu(portsts->wPortStatus); 252 debug("portstatus %x, change %x, %s\n", 253 portstatus, 254 le16_to_cpu(portsts->wPortChange), 255 portspeed(portstatus)); 256 257 /* Clear the connection change status */ 258 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION); 259 260 /* Disconnect any existing devices under this port */ 261 if (((!(portstatus & USB_PORT_STAT_CONNECTION)) && 262 (!(portstatus & USB_PORT_STAT_ENABLE))) || 263 usb_device_has_child_on_port(dev, port)) { 264 debug("usb_disconnect(&hub->children[port]);\n"); 265 /* Return now if nothing is connected */ 266 if (!(portstatus & USB_PORT_STAT_CONNECTION)) 267 return -ENOTCONN; 268 } 269 mdelay(200); 270 271 /* Reset the port */ 272 ret = legacy_hub_port_reset(dev, port, &portstatus); 273 if (ret < 0) { 274 printf("cannot reset port %i!?\n", port + 1); 275 return ret; 276 } 277 278 mdelay(200); 279 280 switch (portstatus & USB_PORT_STAT_SPEED_MASK) { 281 case USB_PORT_STAT_SUPER_SPEED: 282 speed = USB_SPEED_SUPER; 283 break; 284 case USB_PORT_STAT_HIGH_SPEED: 285 speed = USB_SPEED_HIGH; 286 break; 287 case USB_PORT_STAT_LOW_SPEED: 288 speed = USB_SPEED_LOW; 289 break; 290 default: 291 speed = USB_SPEED_FULL; 292 break; 293 } 294 295 #ifdef CONFIG_DM_USB 296 struct udevice *child; 297 298 ret = usb_scan_device(dev->dev, port + 1, speed, &child); 299 #else 300 struct usb_device *usb; 301 302 ret = usb_alloc_new_device(dev->controller, &usb); 303 if (ret) { 304 printf("cannot create new device: ret=%d", ret); 305 return ret; 306 } 307 308 dev->children[port] = usb; 309 usb->speed = speed; 310 usb->parent = dev; 311 usb->portnr = port + 1; 312 /* Run it through the hoops (find a driver, etc) */ 313 ret = usb_new_device(usb); 314 if (ret < 0) { 315 /* Woops, disable the port */ 316 usb_free_device(dev->controller); 317 dev->children[port] = NULL; 318 } 319 #endif 320 if (ret < 0) { 321 debug("hub: disabling port %d\n", port + 1); 322 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE); 323 } 324 325 return ret; 326 } 327 328 329 static int usb_hub_configure(struct usb_device *dev) 330 { 331 int i, length; 332 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ); 333 unsigned char *bitmap; 334 short hubCharacteristics; 335 struct usb_hub_descriptor *descriptor; 336 struct usb_hub_device *hub; 337 __maybe_unused struct usb_hub_status *hubsts; 338 int ret; 339 340 /* "allocate" Hub device */ 341 hub = usb_hub_allocate(); 342 if (hub == NULL) 343 return -ENOMEM; 344 hub->pusb_dev = dev; 345 /* Get the the hub descriptor */ 346 ret = usb_get_hub_descriptor(dev, buffer, 4); 347 if (ret < 0) { 348 debug("usb_hub_configure: failed to get hub " \ 349 "descriptor, giving up %lX\n", dev->status); 350 return ret; 351 } 352 descriptor = (struct usb_hub_descriptor *)buffer; 353 354 length = min_t(int, descriptor->bLength, 355 sizeof(struct usb_hub_descriptor)); 356 357 ret = usb_get_hub_descriptor(dev, buffer, length); 358 if (ret < 0) { 359 debug("usb_hub_configure: failed to get hub " \ 360 "descriptor 2nd giving up %lX\n", dev->status); 361 return ret; 362 } 363 memcpy((unsigned char *)&hub->desc, buffer, length); 364 /* adjust 16bit values */ 365 put_unaligned(le16_to_cpu(get_unaligned( 366 &descriptor->wHubCharacteristics)), 367 &hub->desc.wHubCharacteristics); 368 /* set the bitmap */ 369 bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0]; 370 /* devices not removable by default */ 371 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); 372 bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0]; 373 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */ 374 375 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++) 376 hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i]; 377 378 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++) 379 hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i]; 380 381 dev->maxchild = descriptor->bNbrPorts; 382 debug("%d ports detected\n", dev->maxchild); 383 384 hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics); 385 switch (hubCharacteristics & HUB_CHAR_LPSM) { 386 case 0x00: 387 debug("ganged power switching\n"); 388 break; 389 case 0x01: 390 debug("individual port power switching\n"); 391 break; 392 case 0x02: 393 case 0x03: 394 debug("unknown reserved power switching mode\n"); 395 break; 396 } 397 398 if (hubCharacteristics & HUB_CHAR_COMPOUND) 399 debug("part of a compound device\n"); 400 else 401 debug("standalone hub\n"); 402 403 switch (hubCharacteristics & HUB_CHAR_OCPM) { 404 case 0x00: 405 debug("global over-current protection\n"); 406 break; 407 case 0x08: 408 debug("individual port over-current protection\n"); 409 break; 410 case 0x10: 411 case 0x18: 412 debug("no over-current protection\n"); 413 break; 414 } 415 416 debug("power on to power good time: %dms\n", 417 descriptor->bPwrOn2PwrGood * 2); 418 debug("hub controller current requirement: %dmA\n", 419 descriptor->bHubContrCurrent); 420 421 for (i = 0; i < dev->maxchild; i++) 422 debug("port %d is%s removable\n", i + 1, 423 hub->desc.DeviceRemovable[(i + 1) / 8] & \ 424 (1 << ((i + 1) % 8)) ? " not" : ""); 425 426 if (sizeof(struct usb_hub_status) > USB_BUFSIZ) { 427 debug("usb_hub_configure: failed to get Status - " \ 428 "too long: %d\n", descriptor->bLength); 429 return -EFBIG; 430 } 431 432 ret = usb_get_hub_status(dev, buffer); 433 if (ret < 0) { 434 debug("usb_hub_configure: failed to get Status %lX\n", 435 dev->status); 436 return ret; 437 } 438 439 #ifdef DEBUG 440 hubsts = (struct usb_hub_status *)buffer; 441 #endif 442 443 debug("get_hub_status returned status %X, change %X\n", 444 le16_to_cpu(hubsts->wHubStatus), 445 le16_to_cpu(hubsts->wHubChange)); 446 debug("local power source is %s\n", 447 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \ 448 "lost (inactive)" : "good"); 449 debug("%sover-current condition exists\n", 450 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \ 451 "" : "no "); 452 usb_hub_power_on(hub); 453 454 /* 455 * Reset any devices that may be in a bad state when applying 456 * the power. This is a __weak function. Resetting of the devices 457 * should occur in the board file of the device. 458 */ 459 for (i = 0; i < dev->maxchild; i++) 460 usb_hub_reset_devices(i + 1); 461 462 for (i = 0; i < dev->maxchild; i++) { 463 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1); 464 unsigned short portstatus, portchange; 465 int ret; 466 ulong start = get_timer(0); 467 468 #ifdef CONFIG_DM_USB 469 debug("\n\nScanning '%s' port %d\n", dev->dev->name, i + 1); 470 #else 471 debug("\n\nScanning port %d\n", i + 1); 472 #endif 473 /* 474 * Wait for (whichever finishes first) 475 * - A maximum of 10 seconds 476 * This is a purely observational value driven by connecting 477 * a few broken pen drives and taking the max * 1.5 approach 478 * - connection_change and connection state to report same 479 * state 480 */ 481 do { 482 ret = usb_get_port_status(dev, i + 1, portsts); 483 if (ret < 0) { 484 debug("get_port_status failed\n"); 485 break; 486 } 487 488 portstatus = le16_to_cpu(portsts->wPortStatus); 489 portchange = le16_to_cpu(portsts->wPortChange); 490 491 if ((portchange & USB_PORT_STAT_C_CONNECTION) == 492 (portstatus & USB_PORT_STAT_CONNECTION)) 493 break; 494 495 } while (get_timer(start) < CONFIG_SYS_HZ * 10); 496 497 if (ret < 0) 498 continue; 499 500 debug("Port %d Status %X Change %X\n", 501 i + 1, portstatus, portchange); 502 503 if (portchange & USB_PORT_STAT_C_CONNECTION) { 504 debug("port %d connection change\n", i + 1); 505 usb_hub_port_connect_change(dev, i); 506 } 507 if (portchange & USB_PORT_STAT_C_ENABLE) { 508 debug("port %d enable change, status %x\n", 509 i + 1, portstatus); 510 usb_clear_port_feature(dev, i + 1, 511 USB_PORT_FEAT_C_ENABLE); 512 /* 513 * The following hack causes a ghost device problem 514 * to Faraday EHCI 515 */ 516 #ifndef CONFIG_USB_EHCI_FARADAY 517 /* EM interference sometimes causes bad shielded USB 518 * devices to be shutdown by the hub, this hack enables 519 * them again. Works at least with mouse driver */ 520 if (!(portstatus & USB_PORT_STAT_ENABLE) && 521 (portstatus & USB_PORT_STAT_CONNECTION) && 522 usb_device_has_child_on_port(dev, i)) { 523 debug("already running port %i " \ 524 "disabled by hub (EMI?), " \ 525 "re-enabling...\n", i + 1); 526 usb_hub_port_connect_change(dev, i); 527 } 528 #endif 529 } 530 if (portstatus & USB_PORT_STAT_SUSPEND) { 531 debug("port %d suspend change\n", i + 1); 532 usb_clear_port_feature(dev, i + 1, 533 USB_PORT_FEAT_SUSPEND); 534 } 535 536 if (portchange & USB_PORT_STAT_C_OVERCURRENT) { 537 debug("port %d over-current change\n", i + 1); 538 usb_clear_port_feature(dev, i + 1, 539 USB_PORT_FEAT_C_OVER_CURRENT); 540 usb_hub_power_on(hub); 541 } 542 543 if (portchange & USB_PORT_STAT_C_RESET) { 544 debug("port %d reset change\n", i + 1); 545 usb_clear_port_feature(dev, i + 1, 546 USB_PORT_FEAT_C_RESET); 547 } 548 } /* end for i all ports */ 549 550 return 0; 551 } 552 553 static int usb_hub_check(struct usb_device *dev, int ifnum) 554 { 555 struct usb_interface *iface; 556 struct usb_endpoint_descriptor *ep = NULL; 557 558 iface = &dev->config.if_desc[ifnum]; 559 /* Is it a hub? */ 560 if (iface->desc.bInterfaceClass != USB_CLASS_HUB) 561 goto err; 562 /* Some hubs have a subclass of 1, which AFAICT according to the */ 563 /* specs is not defined, but it works */ 564 if ((iface->desc.bInterfaceSubClass != 0) && 565 (iface->desc.bInterfaceSubClass != 1)) 566 goto err; 567 /* Multiple endpoints? What kind of mutant ninja-hub is this? */ 568 if (iface->desc.bNumEndpoints != 1) 569 goto err; 570 ep = &iface->ep_desc[0]; 571 /* Output endpoint? Curiousier and curiousier.. */ 572 if (!(ep->bEndpointAddress & USB_DIR_IN)) 573 goto err; 574 /* If it's not an interrupt endpoint, we'd better punt! */ 575 if ((ep->bmAttributes & 3) != 3) 576 goto err; 577 /* We found a hub */ 578 debug("USB hub found\n"); 579 return 0; 580 581 err: 582 debug("USB hub not found: bInterfaceClass=%d, bInterfaceSubClass=%d, bNumEndpoints=%d\n", 583 iface->desc.bInterfaceClass, iface->desc.bInterfaceSubClass, 584 iface->desc.bNumEndpoints); 585 if (ep) { 586 debug(" bEndpointAddress=%#x, bmAttributes=%d", 587 ep->bEndpointAddress, ep->bmAttributes); 588 } 589 590 return -ENOENT; 591 } 592 593 int usb_hub_probe(struct usb_device *dev, int ifnum) 594 { 595 int ret; 596 597 ret = usb_hub_check(dev, ifnum); 598 if (ret) 599 return 0; 600 ret = usb_hub_configure(dev); 601 return ret; 602 } 603 604 #ifdef CONFIG_DM_USB 605 int usb_hub_scan(struct udevice *hub) 606 { 607 struct usb_device *udev = dev_get_parentdata(hub); 608 609 return usb_hub_configure(udev); 610 } 611 612 static int usb_hub_post_bind(struct udevice *dev) 613 { 614 /* Scan the bus for devices */ 615 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false); 616 } 617 618 static int usb_hub_post_probe(struct udevice *dev) 619 { 620 debug("%s\n", __func__); 621 return usb_hub_scan(dev); 622 } 623 624 static const struct udevice_id usb_hub_ids[] = { 625 { .compatible = "usb-hub" }, 626 { } 627 }; 628 629 U_BOOT_DRIVER(usb_generic_hub) = { 630 .name = "usb_hub", 631 .id = UCLASS_USB_HUB, 632 .of_match = usb_hub_ids, 633 .flags = DM_FLAG_ALLOC_PRIV_DMA, 634 }; 635 636 UCLASS_DRIVER(usb_hub) = { 637 .id = UCLASS_USB_HUB, 638 .name = "usb_hub", 639 .post_bind = usb_hub_post_bind, 640 .post_probe = usb_hub_post_probe, 641 .child_pre_probe = usb_child_pre_probe, 642 .per_child_auto_alloc_size = sizeof(struct usb_device), 643 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata), 644 }; 645 646 static const struct usb_device_id hub_id_table[] = { 647 { 648 .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS, 649 .bDeviceClass = USB_CLASS_HUB 650 }, 651 { } /* Terminating entry */ 652 }; 653 654 USB_DEVICE(usb_generic_hub, hub_id_table); 655 656 #endif 657