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