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