blob: c9be530d0bfab29d87e042ce72d00d982bff55c1 [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>
Simon Glass054fe482015-03-25 12:22:10 -060027#include <dm.h>
Simon Glass79b58882015-03-25 12:22:01 -060028#include <errno.h>
Marek Vasut23faf2b2012-02-13 18:58:17 +000029#include <asm/processor.h>
Lucas Stach93ad9082012-09-06 08:00:13 +020030#include <asm/unaligned.h>
Marek Vasut23faf2b2012-02-13 18:58:17 +000031#include <linux/ctype.h>
32#include <asm/byteorder.h>
33#include <asm/unaligned.h>
Simon Glass054fe482015-03-25 12:22:10 -060034#include <dm/root.h>
35
36DECLARE_GLOBAL_DATA_PTR;
Marek Vasut23faf2b2012-02-13 18:58:17 +000037
38#include <usb.h>
39#ifdef CONFIG_4xx
40#include <asm/4xx_pci.h>
41#endif
42
Marek Vasut23faf2b2012-02-13 18:58:17 +000043#define USB_BUFSIZ 512
44
Simon Glass054fe482015-03-25 12:22:10 -060045/* TODO(sjg@chromium.org): Remove this when CONFIG_DM_USB is defined */
Marek Vasut23faf2b2012-02-13 18:58:17 +000046static struct usb_hub_device hub_dev[USB_MAX_HUB];
47static int usb_hub_index;
48
Dan Murphy3615a992013-08-01 14:06:01 -050049__weak void usb_hub_reset_devices(int port)
50{
51 return;
52}
Marek Vasut23faf2b2012-02-13 18:58:17 +000053
54static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
55{
56 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
57 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
58 USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
59}
60
61static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
62{
63 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
64 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
65 port, NULL, 0, USB_CNTL_TIMEOUT);
66}
67
68static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
69{
70 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
71 USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
72 port, NULL, 0, USB_CNTL_TIMEOUT);
73}
74
75static int usb_get_hub_status(struct usb_device *dev, void *data)
76{
77 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
78 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
79 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
80}
81
82static int usb_get_port_status(struct usb_device *dev, int port, void *data)
83{
84 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
85 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
86 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
87}
88
89
90static void usb_hub_power_on(struct usb_hub_device *hub)
91{
92 int i;
93 struct usb_device *dev;
Wolfgang Grandeggera1a28c62011-12-21 00:01:09 +000094 unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
Tim Harvey319418c2015-04-08 12:21:12 -070095 const char *env;
Marek Vasut23faf2b2012-02-13 18:58:17 +000096
97 dev = hub->pusb_dev;
Vivek Gautam0bf796f2013-04-24 02:50:11 +000098
Vivek Gautamceb49722013-04-12 16:34:33 +053099 debug("enabling power on all ports\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000100 for (i = 0; i < dev->maxchild; i++) {
101 usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
Vivek Gautamceb49722013-04-12 16:34:33 +0530102 debug("port %d returns %lX\n", i + 1, dev->status);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000103 }
Wolfgang Grandeggera1a28c62011-12-21 00:01:09 +0000104
Stephen Warren0d437bc2014-05-19 14:21:17 -0600105 /*
106 * Wait for power to become stable,
107 * plus spec-defined max time for device to connect
Tim Harvey319418c2015-04-08 12:21:12 -0700108 * but allow this time to be increased via env variable as some
109 * devices break the spec and require longer warm-up times
Stephen Warren0d437bc2014-05-19 14:21:17 -0600110 */
Tim Harvey319418c2015-04-08 12:21:12 -0700111 env = getenv("usb_pgood_delay");
112 if (env)
113 pgood_delay = max(pgood_delay,
114 (unsigned)simple_strtol(env, NULL, 0));
115 debug("pgood_delay=%dms\n", pgood_delay);
Stephen Warren77b83e62014-05-19 14:21:18 -0600116 mdelay(pgood_delay + 1000);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000117}
118
119void usb_hub_reset(void)
120{
121 usb_hub_index = 0;
122}
123
124static struct usb_hub_device *usb_hub_allocate(void)
125{
126 if (usb_hub_index < USB_MAX_HUB)
127 return &hub_dev[usb_hub_index++];
128
129 printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
130 return NULL;
131}
132
133#define MAX_TRIES 5
134
135static inline char *portspeed(int portstatus)
136{
Vivek Gautam55f4b572013-04-24 02:50:12 +0000137 char *speed_str;
138
139 switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
140 case USB_PORT_STAT_SUPER_SPEED:
141 speed_str = "5 Gb/s";
142 break;
143 case USB_PORT_STAT_HIGH_SPEED:
144 speed_str = "480 Mb/s";
145 break;
146 case USB_PORT_STAT_LOW_SPEED:
147 speed_str = "1.5 Mb/s";
148 break;
149 default:
150 speed_str = "12 Mb/s";
151 break;
152 }
153
154 return speed_str;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000155}
156
Simon Glass862e75c2015-03-25 12:22:04 -0600157int legacy_hub_port_reset(struct usb_device *dev, int port,
Marek Vasut23faf2b2012-02-13 18:58:17 +0000158 unsigned short *portstat)
159{
160 int tries;
Puneet Saxenaf5766132012-04-03 14:56:06 +0530161 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000162 unsigned short portstatus, portchange;
163
Simon Glass054fe482015-03-25 12:22:10 -0600164#ifdef CONFIG_DM_USB
165 debug("%s: resetting '%s' port %d...\n", __func__, dev->dev->name,
166 port + 1);
167#else
168 debug("%s: resetting port %d...\n", __func__, port + 1);
169#endif
Marek Vasut23faf2b2012-02-13 18:58:17 +0000170 for (tries = 0; tries < MAX_TRIES; tries++) {
171
172 usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000173 mdelay(200);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000174
Puneet Saxenaf5766132012-04-03 14:56:06 +0530175 if (usb_get_port_status(dev, port + 1, portsts) < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530176 debug("get_port_status failed status %lX\n",
177 dev->status);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000178 return -1;
179 }
Puneet Saxenaf5766132012-04-03 14:56:06 +0530180 portstatus = le16_to_cpu(portsts->wPortStatus);
181 portchange = le16_to_cpu(portsts->wPortChange);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000182
Vivek Gautamceb49722013-04-12 16:34:33 +0530183 debug("portstatus %x, change %x, %s\n", portstatus, portchange,
184 portspeed(portstatus));
Marek Vasut23faf2b2012-02-13 18:58:17 +0000185
Vivek Gautamceb49722013-04-12 16:34:33 +0530186 debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
187 " USB_PORT_STAT_ENABLE %d\n",
188 (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
189 (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
190 (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000191
Stephen Warren74c0d752014-08-07 17:07:59 -0600192 /*
193 * Perhaps we should check for the following here:
194 * - C_CONNECTION hasn't been set.
195 * - CONNECTION is still set.
196 *
197 * Doing so would ensure that the device is still connected
198 * to the bus, and hasn't been unplugged or replaced while the
199 * USB bus reset was going on.
200 *
201 * However, if we do that, then (at least) a San Disk Ultra
202 * USB 3.0 16GB device fails to reset on (at least) an NVIDIA
203 * Tegra Jetson TK1 board. For some reason, the device appears
204 * to briefly drop off the bus when this second bus reset is
205 * executed, yet if we retry this loop, it'll eventually come
206 * back after another reset or two.
207 */
Marek Vasut23faf2b2012-02-13 18:58:17 +0000208
209 if (portstatus & USB_PORT_STAT_ENABLE)
210 break;
211
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000212 mdelay(200);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000213 }
214
215 if (tries == MAX_TRIES) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530216 debug("Cannot enable port %i after %i retries, " \
217 "disabling port.\n", port + 1, MAX_TRIES);
218 debug("Maybe the USB cable is bad?\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000219 return -1;
220 }
221
222 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
223 *portstat = portstatus;
224 return 0;
225}
226
Simon Glass054fe482015-03-25 12:22:10 -0600227#ifdef CONFIG_DM_USB
228int hub_port_reset(struct udevice *dev, int port, unsigned short *portstat)
229{
230 struct usb_device *udev = dev_get_parentdata(dev);
231
232 return legacy_hub_port_reset(udev, port, portstat);
233}
234#endif
Marek Vasut23faf2b2012-02-13 18:58:17 +0000235
Simon Glass79b58882015-03-25 12:22:01 -0600236int usb_hub_port_connect_change(struct usb_device *dev, int port)
Marek Vasut23faf2b2012-02-13 18:58:17 +0000237{
Puneet Saxenaf5766132012-04-03 14:56:06 +0530238 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000239 unsigned short portstatus;
Simon Glass79b58882015-03-25 12:22:01 -0600240 int ret, speed;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000241
242 /* Check status */
Simon Glass79b58882015-03-25 12:22:01 -0600243 ret = usb_get_port_status(dev, port + 1, portsts);
244 if (ret < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530245 debug("get_port_status failed\n");
Simon Glass79b58882015-03-25 12:22:01 -0600246 return ret;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000247 }
248
Puneet Saxenaf5766132012-04-03 14:56:06 +0530249 portstatus = le16_to_cpu(portsts->wPortStatus);
Vivek Gautamceb49722013-04-12 16:34:33 +0530250 debug("portstatus %x, change %x, %s\n",
251 portstatus,
252 le16_to_cpu(portsts->wPortChange),
253 portspeed(portstatus));
Marek Vasut23faf2b2012-02-13 18:58:17 +0000254
255 /* Clear the connection change status */
256 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
257
258 /* Disconnect any existing devices under this port */
259 if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
Simon Glass054fe482015-03-25 12:22:10 -0600260 (!(portstatus & USB_PORT_STAT_ENABLE))) ||
261 usb_device_has_child_on_port(dev, port)) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530262 debug("usb_disconnect(&hub->children[port]);\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000263 /* Return now if nothing is connected */
264 if (!(portstatus & USB_PORT_STAT_CONNECTION))
Simon Glass79b58882015-03-25 12:22:01 -0600265 return -ENOTCONN;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000266 }
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000267 mdelay(200);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000268
269 /* Reset the port */
Simon Glass862e75c2015-03-25 12:22:04 -0600270 ret = legacy_hub_port_reset(dev, port, &portstatus);
Simon Glass79b58882015-03-25 12:22:01 -0600271 if (ret < 0) {
Marek Vasut23faf2b2012-02-13 18:58:17 +0000272 printf("cannot reset port %i!?\n", port + 1);
Simon Glass79b58882015-03-25 12:22:01 -0600273 return ret;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000274 }
275
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000276 mdelay(200);
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:
Simon Glass79b58882015-03-25 12:22:01 -0600280 speed = USB_SPEED_SUPER;
Vivek Gautam55f4b572013-04-24 02:50:12 +0000281 break;
282 case USB_PORT_STAT_HIGH_SPEED:
Simon Glass79b58882015-03-25 12:22:01 -0600283 speed = USB_SPEED_HIGH;
Vivek Gautam55f4b572013-04-24 02:50:12 +0000284 break;
285 case USB_PORT_STAT_LOW_SPEED:
Simon Glass79b58882015-03-25 12:22:01 -0600286 speed = USB_SPEED_LOW;
Vivek Gautam55f4b572013-04-24 02:50:12 +0000287 break;
288 default:
Simon Glass79b58882015-03-25 12:22:01 -0600289 speed = USB_SPEED_FULL;
Vivek Gautam55f4b572013-04-24 02:50:12 +0000290 break;
291 }
Marek Vasut23faf2b2012-02-13 18:58:17 +0000292
Simon Glass054fe482015-03-25 12:22:10 -0600293#ifdef CONFIG_DM_USB
294 struct udevice *child;
295
296 ret = usb_scan_device(dev->dev, port + 1, speed, &child);
297#else
298 struct usb_device *usb;
299
Simon Glass79b58882015-03-25 12:22:01 -0600300 ret = usb_alloc_new_device(dev->controller, &usb);
301 if (ret) {
302 printf("cannot create new device: ret=%d", ret);
303 return ret;
304 }
305
Marek Vasut23faf2b2012-02-13 18:58:17 +0000306 dev->children[port] = usb;
Simon Glass79b58882015-03-25 12:22:01 -0600307 usb->speed = speed;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000308 usb->parent = dev;
309 usb->portnr = port + 1;
310 /* Run it through the hoops (find a driver, etc) */
Simon Glass79b58882015-03-25 12:22:01 -0600311 ret = usb_new_device(usb);
312 if (ret < 0) {
Marek Vasut23faf2b2012-02-13 18:58:17 +0000313 /* Woops, disable the port */
Simon Glass79b58882015-03-25 12:22:01 -0600314 usb_free_device(dev->controller);
Milind Choudhary359439d2012-12-12 17:55:28 -0800315 dev->children[port] = NULL;
Simon Glass054fe482015-03-25 12:22:10 -0600316 }
317#endif
318 if (ret < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530319 debug("hub: disabling port %d\n", port + 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000320 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
321 }
Simon Glass79b58882015-03-25 12:22:01 -0600322
323 return ret;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000324}
325
326
327static int usb_hub_configure(struct usb_device *dev)
328{
Julius Wernereaf3e612013-07-19 13:12:08 -0700329 int i, length;
Puneet Saxenaf5766132012-04-03 14:56:06 +0530330 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
331 unsigned char *bitmap;
Lucas Stach93ad9082012-09-06 08:00:13 +0200332 short hubCharacteristics;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000333 struct usb_hub_descriptor *descriptor;
334 struct usb_hub_device *hub;
Vivek Gautamceb49722013-04-12 16:34:33 +0530335 __maybe_unused struct usb_hub_status *hubsts;
Simon Glass361ad6a2015-03-25 12:22:09 -0600336 int ret;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000337
338 /* "allocate" Hub device */
339 hub = usb_hub_allocate();
340 if (hub == NULL)
Simon Glass361ad6a2015-03-25 12:22:09 -0600341 return -ENOMEM;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000342 hub->pusb_dev = dev;
343 /* Get the the hub descriptor */
Simon Glass361ad6a2015-03-25 12:22:09 -0600344 ret = usb_get_hub_descriptor(dev, buffer, 4);
345 if (ret < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530346 debug("usb_hub_configure: failed to get hub " \
347 "descriptor, giving up %lX\n", dev->status);
Simon Glass361ad6a2015-03-25 12:22:09 -0600348 return ret;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000349 }
350 descriptor = (struct usb_hub_descriptor *)buffer;
351
Masahiro Yamadab4141192014-11-07 03:03:31 +0900352 length = min_t(int, descriptor->bLength,
353 sizeof(struct usb_hub_descriptor));
Marek Vasut23faf2b2012-02-13 18:58:17 +0000354
Simon Glass361ad6a2015-03-25 12:22:09 -0600355 ret = usb_get_hub_descriptor(dev, buffer, length);
356 if (ret < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530357 debug("usb_hub_configure: failed to get hub " \
358 "descriptor 2nd giving up %lX\n", dev->status);
Simon Glass361ad6a2015-03-25 12:22:09 -0600359 return ret;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000360 }
Julius Wernereaf3e612013-07-19 13:12:08 -0700361 memcpy((unsigned char *)&hub->desc, buffer, length);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000362 /* adjust 16bit values */
Lucas Stach93ad9082012-09-06 08:00:13 +0200363 put_unaligned(le16_to_cpu(get_unaligned(
364 &descriptor->wHubCharacteristics)),
365 &hub->desc.wHubCharacteristics);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000366 /* set the bitmap */
367 bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
368 /* devices not removable by default */
369 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
370 bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
371 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
372
373 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
374 hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
375
376 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
377 hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i];
378
379 dev->maxchild = descriptor->bNbrPorts;
Vivek Gautamceb49722013-04-12 16:34:33 +0530380 debug("%d ports detected\n", dev->maxchild);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000381
Lucas Stach93ad9082012-09-06 08:00:13 +0200382 hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics);
383 switch (hubCharacteristics & HUB_CHAR_LPSM) {
Marek Vasut23faf2b2012-02-13 18:58:17 +0000384 case 0x00:
Vivek Gautamceb49722013-04-12 16:34:33 +0530385 debug("ganged power switching\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000386 break;
387 case 0x01:
Vivek Gautamceb49722013-04-12 16:34:33 +0530388 debug("individual port power switching\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000389 break;
390 case 0x02:
391 case 0x03:
Vivek Gautamceb49722013-04-12 16:34:33 +0530392 debug("unknown reserved power switching mode\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000393 break;
394 }
395
Lucas Stach93ad9082012-09-06 08:00:13 +0200396 if (hubCharacteristics & HUB_CHAR_COMPOUND)
Vivek Gautamceb49722013-04-12 16:34:33 +0530397 debug("part of a compound device\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000398 else
Vivek Gautamceb49722013-04-12 16:34:33 +0530399 debug("standalone hub\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000400
Lucas Stach93ad9082012-09-06 08:00:13 +0200401 switch (hubCharacteristics & HUB_CHAR_OCPM) {
Marek Vasut23faf2b2012-02-13 18:58:17 +0000402 case 0x00:
Vivek Gautamceb49722013-04-12 16:34:33 +0530403 debug("global over-current protection\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000404 break;
405 case 0x08:
Vivek Gautamceb49722013-04-12 16:34:33 +0530406 debug("individual port over-current protection\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000407 break;
408 case 0x10:
409 case 0x18:
Vivek Gautamceb49722013-04-12 16:34:33 +0530410 debug("no over-current protection\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000411 break;
412 }
413
Vivek Gautamceb49722013-04-12 16:34:33 +0530414 debug("power on to power good time: %dms\n",
415 descriptor->bPwrOn2PwrGood * 2);
416 debug("hub controller current requirement: %dmA\n",
417 descriptor->bHubContrCurrent);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000418
419 for (i = 0; i < dev->maxchild; i++)
Vivek Gautamceb49722013-04-12 16:34:33 +0530420 debug("port %d is%s removable\n", i + 1,
421 hub->desc.DeviceRemovable[(i + 1) / 8] & \
422 (1 << ((i + 1) % 8)) ? " not" : "");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000423
424 if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530425 debug("usb_hub_configure: failed to get Status - " \
426 "too long: %d\n", descriptor->bLength);
Simon Glass361ad6a2015-03-25 12:22:09 -0600427 return -EFBIG;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000428 }
429
Simon Glass361ad6a2015-03-25 12:22:09 -0600430 ret = usb_get_hub_status(dev, buffer);
431 if (ret < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530432 debug("usb_hub_configure: failed to get Status %lX\n",
433 dev->status);
Simon Glass361ad6a2015-03-25 12:22:09 -0600434 return ret;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000435 }
436
Vivek Gautamceb49722013-04-12 16:34:33 +0530437#ifdef DEBUG
Marek Vasut23faf2b2012-02-13 18:58:17 +0000438 hubsts = (struct usb_hub_status *)buffer;
439#endif
Vivek Gautamceb49722013-04-12 16:34:33 +0530440
441 debug("get_hub_status returned status %X, change %X\n",
442 le16_to_cpu(hubsts->wHubStatus),
443 le16_to_cpu(hubsts->wHubChange));
444 debug("local power source is %s\n",
445 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
446 "lost (inactive)" : "good");
447 debug("%sover-current condition exists\n",
448 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
449 "" : "no ");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000450 usb_hub_power_on(hub);
451
Dan Murphy3615a992013-08-01 14:06:01 -0500452 /*
453 * Reset any devices that may be in a bad state when applying
454 * the power. This is a __weak function. Resetting of the devices
455 * should occur in the board file of the device.
456 */
457 for (i = 0; i < dev->maxchild; i++)
458 usb_hub_reset_devices(i + 1);
459
Marek Vasut23faf2b2012-02-13 18:58:17 +0000460 for (i = 0; i < dev->maxchild; i++) {
Puneet Saxenaf5766132012-04-03 14:56:06 +0530461 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000462 unsigned short portstatus, portchange;
Vipin Kumarb6d78522012-12-13 16:25:53 +0530463 int ret;
464 ulong start = get_timer(0);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000465
Simon Glass054fe482015-03-25 12:22:10 -0600466#ifdef CONFIG_DM_USB
467 debug("\n\nScanning '%s' port %d\n", dev->dev->name, i + 1);
468#else
Simon Glass361ad6a2015-03-25 12:22:09 -0600469 debug("\n\nScanning port %d\n", i + 1);
Simon Glass054fe482015-03-25 12:22:10 -0600470#endif
Vipin Kumarb6d78522012-12-13 16:25:53 +0530471 /*
472 * Wait for (whichever finishes first)
473 * - A maximum of 10 seconds
474 * This is a purely observational value driven by connecting
475 * a few broken pen drives and taking the max * 1.5 approach
476 * - connection_change and connection state to report same
477 * state
478 */
479 do {
480 ret = usb_get_port_status(dev, i + 1, portsts);
481 if (ret < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530482 debug("get_port_status failed\n");
Vipin Kumarb6d78522012-12-13 16:25:53 +0530483 break;
484 }
485
486 portstatus = le16_to_cpu(portsts->wPortStatus);
487 portchange = le16_to_cpu(portsts->wPortChange);
488
489 if ((portchange & USB_PORT_STAT_C_CONNECTION) ==
490 (portstatus & USB_PORT_STAT_CONNECTION))
491 break;
492
Vipin Kumarb6d78522012-12-13 16:25:53 +0530493 } while (get_timer(start) < CONFIG_SYS_HZ * 10);
494
495 if (ret < 0)
Marek Vasut23faf2b2012-02-13 18:58:17 +0000496 continue;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000497
Vivek Gautamceb49722013-04-12 16:34:33 +0530498 debug("Port %d Status %X Change %X\n",
499 i + 1, portstatus, portchange);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000500
501 if (portchange & USB_PORT_STAT_C_CONNECTION) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530502 debug("port %d connection change\n", i + 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000503 usb_hub_port_connect_change(dev, i);
504 }
505 if (portchange & USB_PORT_STAT_C_ENABLE) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530506 debug("port %d enable change, status %x\n",
507 i + 1, portstatus);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000508 usb_clear_port_feature(dev, i + 1,
509 USB_PORT_FEAT_C_ENABLE);
Kuo-Jung Sue82a3162013-05-15 15:29:23 +0800510 /*
511 * The following hack causes a ghost device problem
512 * to Faraday EHCI
513 */
514#ifndef CONFIG_USB_EHCI_FARADAY
Marek Vasut23faf2b2012-02-13 18:58:17 +0000515 /* EM interference sometimes causes bad shielded USB
516 * devices to be shutdown by the hub, this hack enables
517 * them again. Works at least with mouse driver */
518 if (!(portstatus & USB_PORT_STAT_ENABLE) &&
519 (portstatus & USB_PORT_STAT_CONNECTION) &&
Simon Glass054fe482015-03-25 12:22:10 -0600520 usb_device_has_child_on_port(dev, i)) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530521 debug("already running port %i " \
522 "disabled by hub (EMI?), " \
523 "re-enabling...\n", i + 1);
524 usb_hub_port_connect_change(dev, i);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000525 }
Kuo-Jung Sue82a3162013-05-15 15:29:23 +0800526#endif
Marek Vasut23faf2b2012-02-13 18:58:17 +0000527 }
528 if (portstatus & USB_PORT_STAT_SUSPEND) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530529 debug("port %d suspend change\n", i + 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000530 usb_clear_port_feature(dev, i + 1,
531 USB_PORT_FEAT_SUSPEND);
532 }
533
534 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530535 debug("port %d over-current change\n", i + 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000536 usb_clear_port_feature(dev, i + 1,
537 USB_PORT_FEAT_C_OVER_CURRENT);
538 usb_hub_power_on(hub);
539 }
540
541 if (portchange & USB_PORT_STAT_C_RESET) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530542 debug("port %d reset change\n", i + 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000543 usb_clear_port_feature(dev, i + 1,
544 USB_PORT_FEAT_C_RESET);
545 }
546 } /* end for i all ports */
547
548 return 0;
549}
550
Simon Glass361ad6a2015-03-25 12:22:09 -0600551static int usb_hub_check(struct usb_device *dev, int ifnum)
Marek Vasut23faf2b2012-02-13 18:58:17 +0000552{
553 struct usb_interface *iface;
Simon Glass361ad6a2015-03-25 12:22:09 -0600554 struct usb_endpoint_descriptor *ep = NULL;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000555
556 iface = &dev->config.if_desc[ifnum];
557 /* Is it a hub? */
558 if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
Simon Glass361ad6a2015-03-25 12:22:09 -0600559 goto err;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000560 /* Some hubs have a subclass of 1, which AFAICT according to the */
561 /* specs is not defined, but it works */
562 if ((iface->desc.bInterfaceSubClass != 0) &&
563 (iface->desc.bInterfaceSubClass != 1))
Simon Glass361ad6a2015-03-25 12:22:09 -0600564 goto err;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000565 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
566 if (iface->desc.bNumEndpoints != 1)
Simon Glass361ad6a2015-03-25 12:22:09 -0600567 goto err;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000568 ep = &iface->ep_desc[0];
569 /* Output endpoint? Curiousier and curiousier.. */
570 if (!(ep->bEndpointAddress & USB_DIR_IN))
Simon Glass361ad6a2015-03-25 12:22:09 -0600571 goto err;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000572 /* If it's not an interrupt endpoint, we'd better punt! */
573 if ((ep->bmAttributes & 3) != 3)
Simon Glass361ad6a2015-03-25 12:22:09 -0600574 goto err;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000575 /* We found a hub */
Vivek Gautamceb49722013-04-12 16:34:33 +0530576 debug("USB hub found\n");
Simon Glass361ad6a2015-03-25 12:22:09 -0600577 return 0;
578
579err:
580 debug("USB hub not found: bInterfaceClass=%d, bInterfaceSubClass=%d, bNumEndpoints=%d\n",
581 iface->desc.bInterfaceClass, iface->desc.bInterfaceSubClass,
582 iface->desc.bNumEndpoints);
583 if (ep) {
584 debug(" bEndpointAddress=%#x, bmAttributes=%d",
585 ep->bEndpointAddress, ep->bmAttributes);
586 }
587
588 return -ENOENT;
589}
590
591int usb_hub_probe(struct usb_device *dev, int ifnum)
592{
593 int ret;
594
595 ret = usb_hub_check(dev, ifnum);
596 if (ret)
597 return 0;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000598 ret = usb_hub_configure(dev);
599 return ret;
600}
Simon Glass054fe482015-03-25 12:22:10 -0600601
602#ifdef CONFIG_DM_USB
603int usb_hub_scan(struct udevice *hub)
604{
605 struct usb_device *udev = dev_get_parentdata(hub);
606
607 return usb_hub_configure(udev);
608}
609
610static int usb_hub_post_bind(struct udevice *dev)
611{
612 /* Scan the bus for devices */
613 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
614}
615
616static int usb_hub_post_probe(struct udevice *dev)
617{
618 debug("%s\n", __func__);
619 return usb_hub_scan(dev);
620}
621
622static const struct udevice_id usb_hub_ids[] = {
623 { .compatible = "usb-hub" },
624 { }
625};
626
627U_BOOT_DRIVER(usb_generic_hub) = {
628 .name = "usb_hub",
629 .id = UCLASS_USB_HUB,
630 .of_match = usb_hub_ids,
631 .flags = DM_FLAG_ALLOC_PRIV_DMA,
632};
633
634UCLASS_DRIVER(usb_hub) = {
635 .id = UCLASS_USB_HUB,
636 .name = "usb_hub",
637 .post_bind = usb_hub_post_bind,
638 .post_probe = usb_hub_post_probe,
639 .child_pre_probe = usb_child_pre_probe,
640 .per_child_auto_alloc_size = sizeof(struct usb_device),
641 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
642};
643
644static const struct usb_device_id hub_id_table[] = {
645 {
646 .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
647 .bDeviceClass = USB_CLASS_HUB
648 },
649 { } /* Terminating entry */
650};
651
652USB_DEVICE(usb_generic_hub, hub_id_table);
653
654#endif