blob: fd2b4ed4f4a8b71a68877d3877ea58a8893327a0 [file] [log] [blame]
Marek Vasut23faf2b2012-02-13 18:58:17 +00001/*
Marek Vasut23faf2b2012-02-13 18:58:17 +00002 * 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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020017 * SPDX-License-Identifier: GPL-2.0+
Marek Vasut23faf2b2012-02-13 18:58:17 +000018 */
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>
Lucas Stach93ad9082012-09-06 08:00:13 +020028#include <asm/unaligned.h>
Marek Vasut23faf2b2012-02-13 18:58:17 +000029#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
Kuo-Jung Suaa155052013-05-15 15:29:22 +080038#ifndef CONFIG_USB_HUB_MIN_POWER_ON_DELAY
39#define CONFIG_USB_HUB_MIN_POWER_ON_DELAY 100
40#endif
41
Marek Vasut23faf2b2012-02-13 18:58:17 +000042#define USB_BUFSIZ 512
43
44static struct usb_hub_device hub_dev[USB_MAX_HUB];
45static int usb_hub_index;
46
Dan Murphy3615a992013-08-01 14:06:01 -050047__weak void usb_hub_reset_devices(int port)
48{
49 return;
50}
Marek Vasut23faf2b2012-02-13 18:58:17 +000051
52static 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
59static 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
66static 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
73static 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
80static 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
88static void usb_hub_power_on(struct usb_hub_device *hub)
89{
90 int i;
91 struct usb_device *dev;
Wolfgang Grandeggera1a28c62011-12-21 00:01:09 +000092 unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
Vivek Gautam020bbcb2013-04-12 16:34:35 +053093 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
94 unsigned short portstatus;
95 int ret;
Marek Vasut23faf2b2012-02-13 18:58:17 +000096
97 dev = hub->pusb_dev;
Vivek Gautam0bf796f2013-04-24 02:50:11 +000098
99 /*
100 * Enable power to the ports:
101 * Here we Power-cycle the ports: aka,
102 * turning them off and turning on again.
103 */
Vivek Gautamceb49722013-04-12 16:34:33 +0530104 debug("enabling power on all ports\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000105 for (i = 0; i < dev->maxchild; i++) {
Vivek Gautam020bbcb2013-04-12 16:34:35 +0530106 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
107 debug("port %d returns %lX\n", i + 1, dev->status);
Vivek Gautam0bf796f2013-04-24 02:50:11 +0000108 }
Vivek Gautam020bbcb2013-04-12 16:34:35 +0530109
Vivek Gautam0bf796f2013-04-24 02:50:11 +0000110 /* Wait at least 2*bPwrOn2PwrGood for PP to change */
111 mdelay(pgood_delay);
Vivek Gautam020bbcb2013-04-12 16:34:35 +0530112
Vivek Gautam0bf796f2013-04-24 02:50:11 +0000113 for (i = 0; i < dev->maxchild; i++) {
Vivek Gautam020bbcb2013-04-12 16:34:35 +0530114 ret = usb_get_port_status(dev, i + 1, portsts);
115 if (ret < 0) {
116 debug("port %d: get_port_status failed\n", i + 1);
Nikita Kiryanov0adc3312013-07-29 13:27:39 +0300117 continue;
Vivek Gautam020bbcb2013-04-12 16:34:35 +0530118 }
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);
Nikita Kiryanov0adc3312013-07-29 13:27:39 +0300132 continue;
Vivek Gautam020bbcb2013-04-12 16:34:35 +0530133 }
Vivek Gautam0bf796f2013-04-24 02:50:11 +0000134 }
Vivek Gautam020bbcb2013-04-12 16:34:35 +0530135
Vivek Gautam0bf796f2013-04-24 02:50:11 +0000136 for (i = 0; i < dev->maxchild; i++) {
Marek Vasut23faf2b2012-02-13 18:58:17 +0000137 usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
Vivek Gautamceb49722013-04-12 16:34:33 +0530138 debug("port %d returns %lX\n", i + 1, dev->status);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000139 }
Wolfgang Grandeggera1a28c62011-12-21 00:01:09 +0000140
Kuo-Jung Suaa155052013-05-15 15:29:22 +0800141 /* Wait for power to become stable */
142 mdelay(max(pgood_delay, CONFIG_USB_HUB_MIN_POWER_ON_DELAY));
Marek Vasut23faf2b2012-02-13 18:58:17 +0000143}
144
145void usb_hub_reset(void)
146{
147 usb_hub_index = 0;
148}
149
150static struct usb_hub_device *usb_hub_allocate(void)
151{
152 if (usb_hub_index < USB_MAX_HUB)
153 return &hub_dev[usb_hub_index++];
154
155 printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
156 return NULL;
157}
158
159#define MAX_TRIES 5
160
161static inline char *portspeed(int portstatus)
162{
Vivek Gautam55f4b572013-04-24 02:50:12 +0000163 char *speed_str;
164
165 switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
166 case USB_PORT_STAT_SUPER_SPEED:
167 speed_str = "5 Gb/s";
168 break;
169 case USB_PORT_STAT_HIGH_SPEED:
170 speed_str = "480 Mb/s";
171 break;
172 case USB_PORT_STAT_LOW_SPEED:
173 speed_str = "1.5 Mb/s";
174 break;
175 default:
176 speed_str = "12 Mb/s";
177 break;
178 }
179
180 return speed_str;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000181}
182
183int hub_port_reset(struct usb_device *dev, int port,
184 unsigned short *portstat)
185{
186 int tries;
Puneet Saxenaf5766132012-04-03 14:56:06 +0530187 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000188 unsigned short portstatus, portchange;
189
Vivek Gautamceb49722013-04-12 16:34:33 +0530190 debug("hub_port_reset: resetting port %d...\n", port);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000191 for (tries = 0; tries < MAX_TRIES; tries++) {
192
193 usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000194 mdelay(200);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000195
Puneet Saxenaf5766132012-04-03 14:56:06 +0530196 if (usb_get_port_status(dev, port + 1, portsts) < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530197 debug("get_port_status failed status %lX\n",
198 dev->status);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000199 return -1;
200 }
Puneet Saxenaf5766132012-04-03 14:56:06 +0530201 portstatus = le16_to_cpu(portsts->wPortStatus);
202 portchange = le16_to_cpu(portsts->wPortChange);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000203
Vivek Gautamceb49722013-04-12 16:34:33 +0530204 debug("portstatus %x, change %x, %s\n", portstatus, portchange,
205 portspeed(portstatus));
Marek Vasut23faf2b2012-02-13 18:58:17 +0000206
Vivek Gautamceb49722013-04-12 16:34:33 +0530207 debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
208 " USB_PORT_STAT_ENABLE %d\n",
209 (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
210 (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
211 (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000212
213 if ((portchange & USB_PORT_STAT_C_CONNECTION) ||
214 !(portstatus & USB_PORT_STAT_CONNECTION))
215 return -1;
216
217 if (portstatus & USB_PORT_STAT_ENABLE)
218 break;
219
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000220 mdelay(200);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000221 }
222
223 if (tries == MAX_TRIES) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530224 debug("Cannot enable port %i after %i retries, " \
225 "disabling port.\n", port + 1, MAX_TRIES);
226 debug("Maybe the USB cable is bad?\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000227 return -1;
228 }
229
230 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
231 *portstat = portstatus;
232 return 0;
233}
234
235
236void usb_hub_port_connect_change(struct usb_device *dev, int port)
237{
238 struct usb_device *usb;
Puneet Saxenaf5766132012-04-03 14:56:06 +0530239 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000240 unsigned short portstatus;
241
242 /* Check status */
Puneet Saxenaf5766132012-04-03 14:56:06 +0530243 if (usb_get_port_status(dev, port + 1, portsts) < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530244 debug("get_port_status failed\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000245 return;
246 }
247
Puneet Saxenaf5766132012-04-03 14:56:06 +0530248 portstatus = le16_to_cpu(portsts->wPortStatus);
Vivek Gautamceb49722013-04-12 16:34:33 +0530249 debug("portstatus %x, change %x, %s\n",
250 portstatus,
251 le16_to_cpu(portsts->wPortChange),
252 portspeed(portstatus));
Marek Vasut23faf2b2012-02-13 18:58:17 +0000253
254 /* Clear the connection change status */
255 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
256
257 /* Disconnect any existing devices under this port */
258 if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
259 (!(portstatus & USB_PORT_STAT_ENABLE))) || (dev->children[port])) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530260 debug("usb_disconnect(&hub->children[port]);\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000261 /* Return now if nothing is connected */
262 if (!(portstatus & USB_PORT_STAT_CONNECTION))
263 return;
264 }
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000265 mdelay(200);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000266
267 /* Reset the port */
268 if (hub_port_reset(dev, port, &portstatus) < 0) {
269 printf("cannot reset port %i!?\n", port + 1);
270 return;
271 }
272
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000273 mdelay(200);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000274
275 /* Allocate a new device struct for it */
Lucas Stachc7e3b2b2012-09-26 00:14:34 +0200276 usb = usb_alloc_new_device(dev->controller);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000277
Vivek Gautam55f4b572013-04-24 02:50:12 +0000278 switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
279 case USB_PORT_STAT_SUPER_SPEED:
Vivek Gautam6497c662013-04-12 16:34:38 +0530280 usb->speed = USB_SPEED_SUPER;
Vivek Gautam55f4b572013-04-24 02:50:12 +0000281 break;
282 case USB_PORT_STAT_HIGH_SPEED:
Marek Vasut23faf2b2012-02-13 18:58:17 +0000283 usb->speed = USB_SPEED_HIGH;
Vivek Gautam55f4b572013-04-24 02:50:12 +0000284 break;
285 case USB_PORT_STAT_LOW_SPEED:
Marek Vasut23faf2b2012-02-13 18:58:17 +0000286 usb->speed = USB_SPEED_LOW;
Vivek Gautam55f4b572013-04-24 02:50:12 +0000287 break;
288 default:
Marek Vasut23faf2b2012-02-13 18:58:17 +0000289 usb->speed = USB_SPEED_FULL;
Vivek Gautam55f4b572013-04-24 02:50:12 +0000290 break;
291 }
Marek Vasut23faf2b2012-02-13 18:58:17 +0000292
293 dev->children[port] = usb;
294 usb->parent = dev;
295 usb->portnr = port + 1;
296 /* Run it through the hoops (find a driver, etc) */
297 if (usb_new_device(usb)) {
298 /* Woops, disable the port */
Milind Choudhary359439d2012-12-12 17:55:28 -0800299 usb_free_device();
300 dev->children[port] = NULL;
Vivek Gautamceb49722013-04-12 16:34:33 +0530301 debug("hub: disabling port %d\n", port + 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000302 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
303 }
304}
305
306
307static int usb_hub_configure(struct usb_device *dev)
308{
309 int i;
Puneet Saxenaf5766132012-04-03 14:56:06 +0530310 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
311 unsigned char *bitmap;
Lucas Stach93ad9082012-09-06 08:00:13 +0200312 short hubCharacteristics;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000313 struct usb_hub_descriptor *descriptor;
314 struct usb_hub_device *hub;
Vivek Gautamceb49722013-04-12 16:34:33 +0530315 __maybe_unused struct usb_hub_status *hubsts;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000316
317 /* "allocate" Hub device */
318 hub = usb_hub_allocate();
319 if (hub == NULL)
320 return -1;
321 hub->pusb_dev = dev;
322 /* Get the the hub descriptor */
323 if (usb_get_hub_descriptor(dev, buffer, 4) < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530324 debug("usb_hub_configure: failed to get hub " \
325 "descriptor, giving up %lX\n", dev->status);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000326 return -1;
327 }
328 descriptor = (struct usb_hub_descriptor *)buffer;
329
330 /* silence compiler warning if USB_BUFSIZ is > 256 [= sizeof(char)] */
331 i = descriptor->bLength;
332 if (i > USB_BUFSIZ) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530333 debug("usb_hub_configure: failed to get hub " \
334 "descriptor - too long: %d\n", descriptor->bLength);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000335 return -1;
336 }
337
338 if (usb_get_hub_descriptor(dev, buffer, descriptor->bLength) < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530339 debug("usb_hub_configure: failed to get hub " \
340 "descriptor 2nd giving up %lX\n", dev->status);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000341 return -1;
342 }
343 memcpy((unsigned char *)&hub->desc, buffer, descriptor->bLength);
344 /* adjust 16bit values */
Lucas Stach93ad9082012-09-06 08:00:13 +0200345 put_unaligned(le16_to_cpu(get_unaligned(
346 &descriptor->wHubCharacteristics)),
347 &hub->desc.wHubCharacteristics);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000348 /* set the bitmap */
349 bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
350 /* devices not removable by default */
351 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
352 bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
353 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
354
355 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
356 hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
357
358 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
359 hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i];
360
361 dev->maxchild = descriptor->bNbrPorts;
Vivek Gautamceb49722013-04-12 16:34:33 +0530362 debug("%d ports detected\n", dev->maxchild);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000363
Lucas Stach93ad9082012-09-06 08:00:13 +0200364 hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics);
365 switch (hubCharacteristics & HUB_CHAR_LPSM) {
Marek Vasut23faf2b2012-02-13 18:58:17 +0000366 case 0x00:
Vivek Gautamceb49722013-04-12 16:34:33 +0530367 debug("ganged power switching\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000368 break;
369 case 0x01:
Vivek Gautamceb49722013-04-12 16:34:33 +0530370 debug("individual port power switching\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000371 break;
372 case 0x02:
373 case 0x03:
Vivek Gautamceb49722013-04-12 16:34:33 +0530374 debug("unknown reserved power switching mode\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000375 break;
376 }
377
Lucas Stach93ad9082012-09-06 08:00:13 +0200378 if (hubCharacteristics & HUB_CHAR_COMPOUND)
Vivek Gautamceb49722013-04-12 16:34:33 +0530379 debug("part of a compound device\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000380 else
Vivek Gautamceb49722013-04-12 16:34:33 +0530381 debug("standalone hub\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000382
Lucas Stach93ad9082012-09-06 08:00:13 +0200383 switch (hubCharacteristics & HUB_CHAR_OCPM) {
Marek Vasut23faf2b2012-02-13 18:58:17 +0000384 case 0x00:
Vivek Gautamceb49722013-04-12 16:34:33 +0530385 debug("global over-current protection\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000386 break;
387 case 0x08:
Vivek Gautamceb49722013-04-12 16:34:33 +0530388 debug("individual port over-current protection\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000389 break;
390 case 0x10:
391 case 0x18:
Vivek Gautamceb49722013-04-12 16:34:33 +0530392 debug("no over-current protection\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000393 break;
394 }
395
Vivek Gautamceb49722013-04-12 16:34:33 +0530396 debug("power on to power good time: %dms\n",
397 descriptor->bPwrOn2PwrGood * 2);
398 debug("hub controller current requirement: %dmA\n",
399 descriptor->bHubContrCurrent);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000400
401 for (i = 0; i < dev->maxchild; i++)
Vivek Gautamceb49722013-04-12 16:34:33 +0530402 debug("port %d is%s removable\n", i + 1,
403 hub->desc.DeviceRemovable[(i + 1) / 8] & \
404 (1 << ((i + 1) % 8)) ? " not" : "");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000405
406 if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530407 debug("usb_hub_configure: failed to get Status - " \
408 "too long: %d\n", descriptor->bLength);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000409 return -1;
410 }
411
412 if (usb_get_hub_status(dev, buffer) < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530413 debug("usb_hub_configure: failed to get Status %lX\n",
414 dev->status);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000415 return -1;
416 }
417
Vivek Gautamceb49722013-04-12 16:34:33 +0530418#ifdef DEBUG
Marek Vasut23faf2b2012-02-13 18:58:17 +0000419 hubsts = (struct usb_hub_status *)buffer;
420#endif
Vivek Gautamceb49722013-04-12 16:34:33 +0530421
422 debug("get_hub_status returned status %X, change %X\n",
423 le16_to_cpu(hubsts->wHubStatus),
424 le16_to_cpu(hubsts->wHubChange));
425 debug("local power source is %s\n",
426 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
427 "lost (inactive)" : "good");
428 debug("%sover-current condition exists\n",
429 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
430 "" : "no ");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000431 usb_hub_power_on(hub);
432
Dan Murphy3615a992013-08-01 14:06:01 -0500433 /*
434 * Reset any devices that may be in a bad state when applying
435 * the power. This is a __weak function. Resetting of the devices
436 * should occur in the board file of the device.
437 */
438 for (i = 0; i < dev->maxchild; i++)
439 usb_hub_reset_devices(i + 1);
440
Marek Vasut23faf2b2012-02-13 18:58:17 +0000441 for (i = 0; i < dev->maxchild; i++) {
Puneet Saxenaf5766132012-04-03 14:56:06 +0530442 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000443 unsigned short portstatus, portchange;
Vipin Kumarb6d78522012-12-13 16:25:53 +0530444 int ret;
445 ulong start = get_timer(0);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000446
Vipin Kumarb6d78522012-12-13 16:25:53 +0530447 /*
448 * Wait for (whichever finishes first)
449 * - A maximum of 10 seconds
450 * This is a purely observational value driven by connecting
451 * a few broken pen drives and taking the max * 1.5 approach
452 * - connection_change and connection state to report same
453 * state
454 */
455 do {
456 ret = usb_get_port_status(dev, i + 1, portsts);
457 if (ret < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530458 debug("get_port_status failed\n");
Vipin Kumarb6d78522012-12-13 16:25:53 +0530459 break;
460 }
461
462 portstatus = le16_to_cpu(portsts->wPortStatus);
463 portchange = le16_to_cpu(portsts->wPortChange);
464
465 if ((portchange & USB_PORT_STAT_C_CONNECTION) ==
466 (portstatus & USB_PORT_STAT_CONNECTION))
467 break;
468
Vipin Kumarb6d78522012-12-13 16:25:53 +0530469 } while (get_timer(start) < CONFIG_SYS_HZ * 10);
470
471 if (ret < 0)
Marek Vasut23faf2b2012-02-13 18:58:17 +0000472 continue;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000473
Vivek Gautamceb49722013-04-12 16:34:33 +0530474 debug("Port %d Status %X Change %X\n",
475 i + 1, portstatus, portchange);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000476
477 if (portchange & USB_PORT_STAT_C_CONNECTION) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530478 debug("port %d connection change\n", i + 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000479 usb_hub_port_connect_change(dev, i);
480 }
481 if (portchange & USB_PORT_STAT_C_ENABLE) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530482 debug("port %d enable change, status %x\n",
483 i + 1, portstatus);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000484 usb_clear_port_feature(dev, i + 1,
485 USB_PORT_FEAT_C_ENABLE);
Kuo-Jung Sue82a3162013-05-15 15:29:23 +0800486 /*
487 * The following hack causes a ghost device problem
488 * to Faraday EHCI
489 */
490#ifndef CONFIG_USB_EHCI_FARADAY
Marek Vasut23faf2b2012-02-13 18:58:17 +0000491 /* EM interference sometimes causes bad shielded USB
492 * devices to be shutdown by the hub, this hack enables
493 * them again. Works at least with mouse driver */
494 if (!(portstatus & USB_PORT_STAT_ENABLE) &&
495 (portstatus & USB_PORT_STAT_CONNECTION) &&
496 ((dev->children[i]))) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530497 debug("already running port %i " \
498 "disabled by hub (EMI?), " \
499 "re-enabling...\n", i + 1);
500 usb_hub_port_connect_change(dev, i);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000501 }
Kuo-Jung Sue82a3162013-05-15 15:29:23 +0800502#endif
Marek Vasut23faf2b2012-02-13 18:58:17 +0000503 }
504 if (portstatus & USB_PORT_STAT_SUSPEND) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530505 debug("port %d suspend change\n", i + 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000506 usb_clear_port_feature(dev, i + 1,
507 USB_PORT_FEAT_SUSPEND);
508 }
509
510 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530511 debug("port %d over-current change\n", i + 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000512 usb_clear_port_feature(dev, i + 1,
513 USB_PORT_FEAT_C_OVER_CURRENT);
514 usb_hub_power_on(hub);
515 }
516
517 if (portchange & USB_PORT_STAT_C_RESET) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530518 debug("port %d reset change\n", i + 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000519 usb_clear_port_feature(dev, i + 1,
520 USB_PORT_FEAT_C_RESET);
521 }
522 } /* end for i all ports */
523
524 return 0;
525}
526
527int usb_hub_probe(struct usb_device *dev, int ifnum)
528{
529 struct usb_interface *iface;
530 struct usb_endpoint_descriptor *ep;
531 int ret;
532
533 iface = &dev->config.if_desc[ifnum];
534 /* Is it a hub? */
535 if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
536 return 0;
537 /* Some hubs have a subclass of 1, which AFAICT according to the */
538 /* specs is not defined, but it works */
539 if ((iface->desc.bInterfaceSubClass != 0) &&
540 (iface->desc.bInterfaceSubClass != 1))
541 return 0;
542 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
543 if (iface->desc.bNumEndpoints != 1)
544 return 0;
545 ep = &iface->ep_desc[0];
546 /* Output endpoint? Curiousier and curiousier.. */
547 if (!(ep->bEndpointAddress & USB_DIR_IN))
548 return 0;
549 /* If it's not an interrupt endpoint, we'd better punt! */
550 if ((ep->bmAttributes & 3) != 3)
551 return 0;
552 /* We found a hub */
Vivek Gautamceb49722013-04-12 16:34:33 +0530553 debug("USB hub found\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000554 ret = usb_hub_configure(dev);
555 return ret;
556}