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