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