blob: 4875a51aceaf14347a75924fb6225d6151732039 [file] [log] [blame]
Marek Vasut6da5af32012-02-13 18:58:17 +00001/*
Marek Vasut6da5af32012-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 Denkd79de1d2013-07-08 09:37:19 +020017 * SPDX-License-Identifier: GPL-2.0+
Marek Vasut6da5af32012-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 Stach835e11e2012-09-06 08:00:13 +020028#include <asm/unaligned.h>
Marek Vasut6da5af32012-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 Su4e5923f2013-05-15 15:29:22 +080038#ifndef CONFIG_USB_HUB_MIN_POWER_ON_DELAY
Stephen Warren62da17c2014-05-19 14:21:17 -060039#define CONFIG_USB_HUB_MIN_POWER_ON_DELAY 1000
Kuo-Jung Su4e5923f2013-05-15 15:29:22 +080040#endif
41
Marek Vasut6da5af32012-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 Murphy2666bb12013-08-01 14:06:01 -050047__weak void usb_hub_reset_devices(int port)
48{
49 return;
50}
Marek Vasut6da5af32012-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 Grandegger9bb46e02011-12-21 00:01:09 +000092 unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
Vivek Gautam15e49a62013-04-12 16:34:35 +053093 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
94 unsigned short portstatus;
95 int ret;
Marek Vasut6da5af32012-02-13 18:58:17 +000096
97 dev = hub->pusb_dev;
Vivek Gautamc9b13d62013-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 Gautamf94c95d2013-04-12 16:34:33 +0530104 debug("enabling power on all ports\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000105 for (i = 0; i < dev->maxchild; i++) {
Vivek Gautam15e49a62013-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 Gautamc9b13d62013-04-24 02:50:11 +0000108 }
Vivek Gautam15e49a62013-04-12 16:34:35 +0530109
Vivek Gautamc9b13d62013-04-24 02:50:11 +0000110 /* Wait at least 2*bPwrOn2PwrGood for PP to change */
111 mdelay(pgood_delay);
Vivek Gautam15e49a62013-04-12 16:34:35 +0530112
Vivek Gautamc9b13d62013-04-24 02:50:11 +0000113 for (i = 0; i < dev->maxchild; i++) {
Vivek Gautam15e49a62013-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 Kiryanov749c6d82013-07-29 13:27:39 +0300117 continue;
Vivek Gautam15e49a62013-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 Kiryanov749c6d82013-07-29 13:27:39 +0300132 continue;
Vivek Gautam15e49a62013-04-12 16:34:35 +0530133 }
Vivek Gautamc9b13d62013-04-24 02:50:11 +0000134 }
Vivek Gautam15e49a62013-04-12 16:34:35 +0530135
Vivek Gautamc9b13d62013-04-24 02:50:11 +0000136 for (i = 0; i < dev->maxchild; i++) {
Marek Vasut6da5af32012-02-13 18:58:17 +0000137 usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530138 debug("port %d returns %lX\n", i + 1, dev->status);
Marek Vasut6da5af32012-02-13 18:58:17 +0000139 }
Wolfgang Grandegger9bb46e02011-12-21 00:01:09 +0000140
Stephen Warren62da17c2014-05-19 14:21:17 -0600141 /*
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);
Marek Vasut6da5af32012-02-13 18:58:17 +0000146}
147
148void usb_hub_reset(void)
149{
150 usb_hub_index = 0;
151}
152
153static 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
164static inline char *portspeed(int portstatus)
165{
Vivek Gautam86c35be2013-04-24 02:50:12 +0000166 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;
Marek Vasut6da5af32012-02-13 18:58:17 +0000184}
185
186int hub_port_reset(struct usb_device *dev, int port,
187 unsigned short *portstat)
188{
189 int tries;
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530190 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
Marek Vasut6da5af32012-02-13 18:58:17 +0000191 unsigned short portstatus, portchange;
192
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530193 debug("hub_port_reset: resetting port %d...\n", port);
Marek Vasut6da5af32012-02-13 18:58:17 +0000194 for (tries = 0; tries < MAX_TRIES; tries++) {
195
196 usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000197 mdelay(200);
Marek Vasut6da5af32012-02-13 18:58:17 +0000198
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530199 if (usb_get_port_status(dev, port + 1, portsts) < 0) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530200 debug("get_port_status failed status %lX\n",
201 dev->status);
Marek Vasut6da5af32012-02-13 18:58:17 +0000202 return -1;
203 }
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530204 portstatus = le16_to_cpu(portsts->wPortStatus);
205 portchange = le16_to_cpu(portsts->wPortChange);
Marek Vasut6da5af32012-02-13 18:58:17 +0000206
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530207 debug("portstatus %x, change %x, %s\n", portstatus, portchange,
208 portspeed(portstatus));
Marek Vasut6da5af32012-02-13 18:58:17 +0000209
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530210 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);
Marek Vasut6da5af32012-02-13 18:58:17 +0000215
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
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000223 mdelay(200);
Marek Vasut6da5af32012-02-13 18:58:17 +0000224 }
225
226 if (tries == MAX_TRIES) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530227 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");
Marek Vasut6da5af32012-02-13 18:58:17 +0000230 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
239void usb_hub_port_connect_change(struct usb_device *dev, int port)
240{
241 struct usb_device *usb;
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530242 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
Marek Vasut6da5af32012-02-13 18:58:17 +0000243 unsigned short portstatus;
244
245 /* Check status */
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530246 if (usb_get_port_status(dev, port + 1, portsts) < 0) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530247 debug("get_port_status failed\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000248 return;
249 }
250
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530251 portstatus = le16_to_cpu(portsts->wPortStatus);
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530252 debug("portstatus %x, change %x, %s\n",
253 portstatus,
254 le16_to_cpu(portsts->wPortChange),
255 portspeed(portstatus));
Marek Vasut6da5af32012-02-13 18:58:17 +0000256
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])) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530263 debug("usb_disconnect(&hub->children[port]);\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000264 /* Return now if nothing is connected */
265 if (!(portstatus & USB_PORT_STAT_CONNECTION))
266 return;
267 }
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000268 mdelay(200);
Marek Vasut6da5af32012-02-13 18:58:17 +0000269
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
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000276 mdelay(200);
Marek Vasut6da5af32012-02-13 18:58:17 +0000277
278 /* Allocate a new device struct for it */
Lucas Stacha3231282012-09-26 00:14:34 +0200279 usb = usb_alloc_new_device(dev->controller);
Marek Vasut6da5af32012-02-13 18:58:17 +0000280
Vivek Gautam86c35be2013-04-24 02:50:12 +0000281 switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
282 case USB_PORT_STAT_SUPER_SPEED:
Vivek Gautamdf3f2212013-04-12 16:34:38 +0530283 usb->speed = USB_SPEED_SUPER;
Vivek Gautam86c35be2013-04-24 02:50:12 +0000284 break;
285 case USB_PORT_STAT_HIGH_SPEED:
Marek Vasut6da5af32012-02-13 18:58:17 +0000286 usb->speed = USB_SPEED_HIGH;
Vivek Gautam86c35be2013-04-24 02:50:12 +0000287 break;
288 case USB_PORT_STAT_LOW_SPEED:
Marek Vasut6da5af32012-02-13 18:58:17 +0000289 usb->speed = USB_SPEED_LOW;
Vivek Gautam86c35be2013-04-24 02:50:12 +0000290 break;
291 default:
Marek Vasut6da5af32012-02-13 18:58:17 +0000292 usb->speed = USB_SPEED_FULL;
Vivek Gautam86c35be2013-04-24 02:50:12 +0000293 break;
294 }
Marek Vasut6da5af32012-02-13 18:58:17 +0000295
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 */
Milind Choudharyebb24782012-12-12 17:55:28 -0800302 usb_free_device();
303 dev->children[port] = NULL;
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530304 debug("hub: disabling port %d\n", port + 1);
Marek Vasut6da5af32012-02-13 18:58:17 +0000305 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
306 }
307}
308
309
310static int usb_hub_configure(struct usb_device *dev)
311{
Julius Wernerfc4ec492013-07-19 13:12:08 -0700312 int i, length;
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530313 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
314 unsigned char *bitmap;
Lucas Stach835e11e2012-09-06 08:00:13 +0200315 short hubCharacteristics;
Marek Vasut6da5af32012-02-13 18:58:17 +0000316 struct usb_hub_descriptor *descriptor;
317 struct usb_hub_device *hub;
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530318 __maybe_unused struct usb_hub_status *hubsts;
Marek Vasut6da5af32012-02-13 18:58:17 +0000319
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) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530327 debug("usb_hub_configure: failed to get hub " \
328 "descriptor, giving up %lX\n", dev->status);
Marek Vasut6da5af32012-02-13 18:58:17 +0000329 return -1;
330 }
331 descriptor = (struct usb_hub_descriptor *)buffer;
332
Julius Wernerfc4ec492013-07-19 13:12:08 -0700333 length = min(descriptor->bLength, sizeof(struct usb_hub_descriptor));
Marek Vasut6da5af32012-02-13 18:58:17 +0000334
Julius Wernerfc4ec492013-07-19 13:12:08 -0700335 if (usb_get_hub_descriptor(dev, buffer, length) < 0) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530336 debug("usb_hub_configure: failed to get hub " \
337 "descriptor 2nd giving up %lX\n", dev->status);
Marek Vasut6da5af32012-02-13 18:58:17 +0000338 return -1;
339 }
Julius Wernerfc4ec492013-07-19 13:12:08 -0700340 memcpy((unsigned char *)&hub->desc, buffer, length);
Marek Vasut6da5af32012-02-13 18:58:17 +0000341 /* adjust 16bit values */
Lucas Stach835e11e2012-09-06 08:00:13 +0200342 put_unaligned(le16_to_cpu(get_unaligned(
343 &descriptor->wHubCharacteristics)),
344 &hub->desc.wHubCharacteristics);
Marek Vasut6da5af32012-02-13 18:58:17 +0000345 /* 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;
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530359 debug("%d ports detected\n", dev->maxchild);
Marek Vasut6da5af32012-02-13 18:58:17 +0000360
Lucas Stach835e11e2012-09-06 08:00:13 +0200361 hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics);
362 switch (hubCharacteristics & HUB_CHAR_LPSM) {
Marek Vasut6da5af32012-02-13 18:58:17 +0000363 case 0x00:
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530364 debug("ganged power switching\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000365 break;
366 case 0x01:
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530367 debug("individual port power switching\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000368 break;
369 case 0x02:
370 case 0x03:
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530371 debug("unknown reserved power switching mode\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000372 break;
373 }
374
Lucas Stach835e11e2012-09-06 08:00:13 +0200375 if (hubCharacteristics & HUB_CHAR_COMPOUND)
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530376 debug("part of a compound device\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000377 else
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530378 debug("standalone hub\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000379
Lucas Stach835e11e2012-09-06 08:00:13 +0200380 switch (hubCharacteristics & HUB_CHAR_OCPM) {
Marek Vasut6da5af32012-02-13 18:58:17 +0000381 case 0x00:
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530382 debug("global over-current protection\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000383 break;
384 case 0x08:
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530385 debug("individual port over-current protection\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000386 break;
387 case 0x10:
388 case 0x18:
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530389 debug("no over-current protection\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000390 break;
391 }
392
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530393 debug("power on to power good time: %dms\n",
394 descriptor->bPwrOn2PwrGood * 2);
395 debug("hub controller current requirement: %dmA\n",
396 descriptor->bHubContrCurrent);
Marek Vasut6da5af32012-02-13 18:58:17 +0000397
398 for (i = 0; i < dev->maxchild; i++)
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530399 debug("port %d is%s removable\n", i + 1,
400 hub->desc.DeviceRemovable[(i + 1) / 8] & \
401 (1 << ((i + 1) % 8)) ? " not" : "");
Marek Vasut6da5af32012-02-13 18:58:17 +0000402
403 if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530404 debug("usb_hub_configure: failed to get Status - " \
405 "too long: %d\n", descriptor->bLength);
Marek Vasut6da5af32012-02-13 18:58:17 +0000406 return -1;
407 }
408
409 if (usb_get_hub_status(dev, buffer) < 0) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530410 debug("usb_hub_configure: failed to get Status %lX\n",
411 dev->status);
Marek Vasut6da5af32012-02-13 18:58:17 +0000412 return -1;
413 }
414
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530415#ifdef DEBUG
Marek Vasut6da5af32012-02-13 18:58:17 +0000416 hubsts = (struct usb_hub_status *)buffer;
417#endif
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530418
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 ");
Marek Vasut6da5af32012-02-13 18:58:17 +0000428 usb_hub_power_on(hub);
429
Dan Murphy2666bb12013-08-01 14:06:01 -0500430 /*
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
Marek Vasut6da5af32012-02-13 18:58:17 +0000438 for (i = 0; i < dev->maxchild; i++) {
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530439 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
Marek Vasut6da5af32012-02-13 18:58:17 +0000440 unsigned short portstatus, portchange;
Vipin Kumara2612822012-12-13 16:25:53 +0530441 int ret;
442 ulong start = get_timer(0);
Marek Vasut6da5af32012-02-13 18:58:17 +0000443
Vipin Kumara2612822012-12-13 16:25:53 +0530444 /*
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) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530455 debug("get_port_status failed\n");
Vipin Kumara2612822012-12-13 16:25:53 +0530456 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
Vipin Kumara2612822012-12-13 16:25:53 +0530466 } while (get_timer(start) < CONFIG_SYS_HZ * 10);
467
468 if (ret < 0)
Marek Vasut6da5af32012-02-13 18:58:17 +0000469 continue;
Marek Vasut6da5af32012-02-13 18:58:17 +0000470
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530471 debug("Port %d Status %X Change %X\n",
472 i + 1, portstatus, portchange);
Marek Vasut6da5af32012-02-13 18:58:17 +0000473
474 if (portchange & USB_PORT_STAT_C_CONNECTION) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530475 debug("port %d connection change\n", i + 1);
Marek Vasut6da5af32012-02-13 18:58:17 +0000476 usb_hub_port_connect_change(dev, i);
477 }
478 if (portchange & USB_PORT_STAT_C_ENABLE) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530479 debug("port %d enable change, status %x\n",
480 i + 1, portstatus);
Marek Vasut6da5af32012-02-13 18:58:17 +0000481 usb_clear_port_feature(dev, i + 1,
482 USB_PORT_FEAT_C_ENABLE);
Kuo-Jung Sub5d59de2013-05-15 15:29:23 +0800483 /*
484 * The following hack causes a ghost device problem
485 * to Faraday EHCI
486 */
487#ifndef CONFIG_USB_EHCI_FARADAY
Marek Vasut6da5af32012-02-13 18:58:17 +0000488 /* 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]))) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530494 debug("already running port %i " \
495 "disabled by hub (EMI?), " \
496 "re-enabling...\n", i + 1);
497 usb_hub_port_connect_change(dev, i);
Marek Vasut6da5af32012-02-13 18:58:17 +0000498 }
Kuo-Jung Sub5d59de2013-05-15 15:29:23 +0800499#endif
Marek Vasut6da5af32012-02-13 18:58:17 +0000500 }
501 if (portstatus & USB_PORT_STAT_SUSPEND) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530502 debug("port %d suspend change\n", i + 1);
Marek Vasut6da5af32012-02-13 18:58:17 +0000503 usb_clear_port_feature(dev, i + 1,
504 USB_PORT_FEAT_SUSPEND);
505 }
506
507 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530508 debug("port %d over-current change\n", i + 1);
Marek Vasut6da5af32012-02-13 18:58:17 +0000509 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) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530515 debug("port %d reset change\n", i + 1);
Marek Vasut6da5af32012-02-13 18:58:17 +0000516 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
524int 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 */
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530550 debug("USB hub found\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000551 ret = usb_hub_configure(dev);
552 return ret;
553}