blob: 415b45c1f139451c685fd450a6477bde34859cab [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>
Simon Glasscf92e052015-09-02 17:24:58 -060029#include <memalign.h>
Marek Vasut23faf2b2012-02-13 18:58:17 +000030#include <asm/processor.h>
Lucas Stach93ad9082012-09-06 08:00:13 +020031#include <asm/unaligned.h>
Marek Vasut23faf2b2012-02-13 18:58:17 +000032#include <linux/ctype.h>
33#include <asm/byteorder.h>
34#include <asm/unaligned.h>
Simon Glass054fe482015-03-25 12:22:10 -060035#include <dm/root.h>
36
37DECLARE_GLOBAL_DATA_PTR;
Marek Vasut23faf2b2012-02-13 18:58:17 +000038
39#include <usb.h>
40#ifdef CONFIG_4xx
41#include <asm/4xx_pci.h>
42#endif
43
Marek Vasut23faf2b2012-02-13 18:58:17 +000044#define USB_BUFSIZ 512
45
Simon Glass054fe482015-03-25 12:22:10 -060046/* TODO(sjg@chromium.org): Remove this when CONFIG_DM_USB is defined */
Marek Vasut23faf2b2012-02-13 18:58:17 +000047static struct usb_hub_device hub_dev[USB_MAX_HUB];
48static int usb_hub_index;
49
Dan Murphy3615a992013-08-01 14:06:01 -050050__weak void usb_hub_reset_devices(int port)
51{
52 return;
53}
Marek Vasut23faf2b2012-02-13 18:58:17 +000054
55static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
56{
57 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
58 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
59 USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
60}
61
62static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
63{
64 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
65 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
66 port, NULL, 0, USB_CNTL_TIMEOUT);
67}
68
69static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
70{
71 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
72 USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
73 port, NULL, 0, USB_CNTL_TIMEOUT);
74}
75
76static int usb_get_hub_status(struct usb_device *dev, void *data)
77{
78 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
79 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
80 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
81}
82
Vincent Palatin08f3bb02015-05-04 11:30:54 -060083int usb_get_port_status(struct usb_device *dev, int port, void *data)
Marek Vasut23faf2b2012-02-13 18:58:17 +000084{
85 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
86 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
87 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
88}
89
90
91static void usb_hub_power_on(struct usb_hub_device *hub)
92{
93 int i;
94 struct usb_device *dev;
Wolfgang Grandeggera1a28c62011-12-21 00:01:09 +000095 unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
Tim Harvey319418c2015-04-08 12:21:12 -070096 const char *env;
Marek Vasut23faf2b2012-02-13 18:58:17 +000097
98 dev = hub->pusb_dev;
Vivek Gautam0bf796f2013-04-24 02:50:11 +000099
Vivek Gautamceb49722013-04-12 16:34:33 +0530100 debug("enabling power on all ports\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000101 for (i = 0; i < dev->maxchild; i++) {
102 usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
Vivek Gautamceb49722013-04-12 16:34:33 +0530103 debug("port %d returns %lX\n", i + 1, dev->status);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000104 }
Wolfgang Grandeggera1a28c62011-12-21 00:01:09 +0000105
Stephen Warren0d437bc2014-05-19 14:21:17 -0600106 /*
107 * Wait for power to become stable,
108 * plus spec-defined max time for device to connect
Tim Harvey319418c2015-04-08 12:21:12 -0700109 * but allow this time to be increased via env variable as some
110 * devices break the spec and require longer warm-up times
Stephen Warren0d437bc2014-05-19 14:21:17 -0600111 */
Tim Harvey319418c2015-04-08 12:21:12 -0700112 env = getenv("usb_pgood_delay");
113 if (env)
114 pgood_delay = max(pgood_delay,
115 (unsigned)simple_strtol(env, NULL, 0));
116 debug("pgood_delay=%dms\n", pgood_delay);
Stephen Warren77b83e62014-05-19 14:21:18 -0600117 mdelay(pgood_delay + 1000);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000118}
119
120void usb_hub_reset(void)
121{
122 usb_hub_index = 0;
123}
124
125static struct usb_hub_device *usb_hub_allocate(void)
126{
127 if (usb_hub_index < USB_MAX_HUB)
128 return &hub_dev[usb_hub_index++];
129
130 printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
131 return NULL;
132}
133
134#define MAX_TRIES 5
135
136static inline char *portspeed(int portstatus)
137{
Vivek Gautam55f4b572013-04-24 02:50:12 +0000138 char *speed_str;
139
140 switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
141 case USB_PORT_STAT_SUPER_SPEED:
142 speed_str = "5 Gb/s";
143 break;
144 case USB_PORT_STAT_HIGH_SPEED:
145 speed_str = "480 Mb/s";
146 break;
147 case USB_PORT_STAT_LOW_SPEED:
148 speed_str = "1.5 Mb/s";
149 break;
150 default:
151 speed_str = "12 Mb/s";
152 break;
153 }
154
155 return speed_str;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000156}
157
Simon Glass862e75c2015-03-25 12:22:04 -0600158int legacy_hub_port_reset(struct usb_device *dev, int port,
Marek Vasut23faf2b2012-02-13 18:58:17 +0000159 unsigned short *portstat)
160{
Hans de Goedead84a422015-05-10 14:10:15 +0200161 int err, tries;
Puneet Saxenaf5766132012-04-03 14:56:06 +0530162 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000163 unsigned short portstatus, portchange;
164
Simon Glass054fe482015-03-25 12:22:10 -0600165#ifdef CONFIG_DM_USB
166 debug("%s: resetting '%s' port %d...\n", __func__, dev->dev->name,
167 port + 1);
168#else
169 debug("%s: resetting port %d...\n", __func__, port + 1);
170#endif
Marek Vasut23faf2b2012-02-13 18:58:17 +0000171 for (tries = 0; tries < MAX_TRIES; tries++) {
Hans de Goedead84a422015-05-10 14:10:15 +0200172 err = usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
173 if (err < 0)
174 return err;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000175
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000176 mdelay(200);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000177
Puneet Saxenaf5766132012-04-03 14:56:06 +0530178 if (usb_get_port_status(dev, port + 1, portsts) < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530179 debug("get_port_status failed status %lX\n",
180 dev->status);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000181 return -1;
182 }
Puneet Saxenaf5766132012-04-03 14:56:06 +0530183 portstatus = le16_to_cpu(portsts->wPortStatus);
184 portchange = le16_to_cpu(portsts->wPortChange);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000185
Vivek Gautamceb49722013-04-12 16:34:33 +0530186 debug("portstatus %x, change %x, %s\n", portstatus, portchange,
187 portspeed(portstatus));
Marek Vasut23faf2b2012-02-13 18:58:17 +0000188
Vivek Gautamceb49722013-04-12 16:34:33 +0530189 debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
190 " USB_PORT_STAT_ENABLE %d\n",
191 (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
192 (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
193 (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000194
Stephen Warren74c0d752014-08-07 17:07:59 -0600195 /*
196 * Perhaps we should check for the following here:
197 * - C_CONNECTION hasn't been set.
198 * - CONNECTION is still set.
199 *
200 * Doing so would ensure that the device is still connected
201 * to the bus, and hasn't been unplugged or replaced while the
202 * USB bus reset was going on.
203 *
204 * However, if we do that, then (at least) a San Disk Ultra
205 * USB 3.0 16GB device fails to reset on (at least) an NVIDIA
206 * Tegra Jetson TK1 board. For some reason, the device appears
207 * to briefly drop off the bus when this second bus reset is
208 * executed, yet if we retry this loop, it'll eventually come
209 * back after another reset or two.
210 */
Marek Vasut23faf2b2012-02-13 18:58:17 +0000211
212 if (portstatus & USB_PORT_STAT_ENABLE)
213 break;
214
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000215 mdelay(200);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000216 }
217
218 if (tries == MAX_TRIES) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530219 debug("Cannot enable port %i after %i retries, " \
220 "disabling port.\n", port + 1, MAX_TRIES);
221 debug("Maybe the USB cable is bad?\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000222 return -1;
223 }
224
225 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
226 *portstat = portstatus;
227 return 0;
228}
229
Simon Glass054fe482015-03-25 12:22:10 -0600230#ifdef CONFIG_DM_USB
231int hub_port_reset(struct udevice *dev, int port, unsigned short *portstat)
232{
233 struct usb_device *udev = dev_get_parentdata(dev);
234
235 return legacy_hub_port_reset(udev, port, portstat);
236}
237#endif
Marek Vasut23faf2b2012-02-13 18:58:17 +0000238
Simon Glass79b58882015-03-25 12:22:01 -0600239int usb_hub_port_connect_change(struct usb_device *dev, int port)
Marek Vasut23faf2b2012-02-13 18:58:17 +0000240{
Puneet Saxenaf5766132012-04-03 14:56:06 +0530241 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000242 unsigned short portstatus;
Simon Glass79b58882015-03-25 12:22:01 -0600243 int ret, speed;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000244
245 /* Check status */
Simon Glass79b58882015-03-25 12:22:01 -0600246 ret = usb_get_port_status(dev, port + 1, portsts);
247 if (ret < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530248 debug("get_port_status failed\n");
Simon Glass79b58882015-03-25 12:22:01 -0600249 return ret;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000250 }
251
Puneet Saxenaf5766132012-04-03 14:56:06 +0530252 portstatus = le16_to_cpu(portsts->wPortStatus);
Vivek Gautamceb49722013-04-12 16:34:33 +0530253 debug("portstatus %x, change %x, %s\n",
254 portstatus,
255 le16_to_cpu(portsts->wPortChange),
256 portspeed(portstatus));
Marek Vasut23faf2b2012-02-13 18:58:17 +0000257
258 /* Clear the connection change status */
259 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
260
261 /* Disconnect any existing devices under this port */
262 if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
Simon Glass054fe482015-03-25 12:22:10 -0600263 (!(portstatus & USB_PORT_STAT_ENABLE))) ||
264 usb_device_has_child_on_port(dev, port)) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530265 debug("usb_disconnect(&hub->children[port]);\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000266 /* Return now if nothing is connected */
267 if (!(portstatus & USB_PORT_STAT_CONNECTION))
Simon Glass79b58882015-03-25 12:22:01 -0600268 return -ENOTCONN;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000269 }
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000270 mdelay(200);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000271
272 /* Reset the port */
Simon Glass862e75c2015-03-25 12:22:04 -0600273 ret = legacy_hub_port_reset(dev, port, &portstatus);
Simon Glass79b58882015-03-25 12:22:01 -0600274 if (ret < 0) {
Hans de Goede45b9ea12015-05-10 14:10:16 +0200275 if (ret != -ENXIO)
276 printf("cannot reset port %i!?\n", port + 1);
Simon Glass79b58882015-03-25 12:22:01 -0600277 return ret;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000278 }
279
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000280 mdelay(200);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000281
Vivek Gautam55f4b572013-04-24 02:50:12 +0000282 switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
283 case USB_PORT_STAT_SUPER_SPEED:
Simon Glass79b58882015-03-25 12:22:01 -0600284 speed = USB_SPEED_SUPER;
Vivek Gautam55f4b572013-04-24 02:50:12 +0000285 break;
286 case USB_PORT_STAT_HIGH_SPEED:
Simon Glass79b58882015-03-25 12:22:01 -0600287 speed = USB_SPEED_HIGH;
Vivek Gautam55f4b572013-04-24 02:50:12 +0000288 break;
289 case USB_PORT_STAT_LOW_SPEED:
Simon Glass79b58882015-03-25 12:22:01 -0600290 speed = USB_SPEED_LOW;
Vivek Gautam55f4b572013-04-24 02:50:12 +0000291 break;
292 default:
Simon Glass79b58882015-03-25 12:22:01 -0600293 speed = USB_SPEED_FULL;
Vivek Gautam55f4b572013-04-24 02:50:12 +0000294 break;
295 }
Marek Vasut23faf2b2012-02-13 18:58:17 +0000296
Simon Glass054fe482015-03-25 12:22:10 -0600297#ifdef CONFIG_DM_USB
298 struct udevice *child;
299
300 ret = usb_scan_device(dev->dev, port + 1, speed, &child);
301#else
302 struct usb_device *usb;
303
Simon Glass79b58882015-03-25 12:22:01 -0600304 ret = usb_alloc_new_device(dev->controller, &usb);
305 if (ret) {
306 printf("cannot create new device: ret=%d", ret);
307 return ret;
308 }
309
Marek Vasut23faf2b2012-02-13 18:58:17 +0000310 dev->children[port] = usb;
Simon Glass79b58882015-03-25 12:22:01 -0600311 usb->speed = speed;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000312 usb->parent = dev;
313 usb->portnr = port + 1;
314 /* Run it through the hoops (find a driver, etc) */
Simon Glass79b58882015-03-25 12:22:01 -0600315 ret = usb_new_device(usb);
316 if (ret < 0) {
Marek Vasut23faf2b2012-02-13 18:58:17 +0000317 /* Woops, disable the port */
Simon Glass79b58882015-03-25 12:22:01 -0600318 usb_free_device(dev->controller);
Milind Choudhary359439d2012-12-12 17:55:28 -0800319 dev->children[port] = NULL;
Simon Glass054fe482015-03-25 12:22:10 -0600320 }
321#endif
322 if (ret < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530323 debug("hub: disabling port %d\n", port + 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000324 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
325 }
Simon Glass79b58882015-03-25 12:22:01 -0600326
327 return ret;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000328}
329
330
331static int usb_hub_configure(struct usb_device *dev)
332{
Julius Wernereaf3e612013-07-19 13:12:08 -0700333 int i, length;
Puneet Saxenaf5766132012-04-03 14:56:06 +0530334 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
335 unsigned char *bitmap;
Lucas Stach93ad9082012-09-06 08:00:13 +0200336 short hubCharacteristics;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000337 struct usb_hub_descriptor *descriptor;
338 struct usb_hub_device *hub;
Vivek Gautamceb49722013-04-12 16:34:33 +0530339 __maybe_unused struct usb_hub_status *hubsts;
Simon Glass361ad6a2015-03-25 12:22:09 -0600340 int ret;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000341
342 /* "allocate" Hub device */
343 hub = usb_hub_allocate();
344 if (hub == NULL)
Simon Glass361ad6a2015-03-25 12:22:09 -0600345 return -ENOMEM;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000346 hub->pusb_dev = dev;
347 /* Get the the hub descriptor */
Simon Glass361ad6a2015-03-25 12:22:09 -0600348 ret = usb_get_hub_descriptor(dev, buffer, 4);
349 if (ret < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530350 debug("usb_hub_configure: failed to get hub " \
351 "descriptor, giving up %lX\n", dev->status);
Simon Glass361ad6a2015-03-25 12:22:09 -0600352 return ret;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000353 }
354 descriptor = (struct usb_hub_descriptor *)buffer;
355
Masahiro Yamadab4141192014-11-07 03:03:31 +0900356 length = min_t(int, descriptor->bLength,
357 sizeof(struct usb_hub_descriptor));
Marek Vasut23faf2b2012-02-13 18:58:17 +0000358
Simon Glass361ad6a2015-03-25 12:22:09 -0600359 ret = usb_get_hub_descriptor(dev, buffer, length);
360 if (ret < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530361 debug("usb_hub_configure: failed to get hub " \
362 "descriptor 2nd giving up %lX\n", dev->status);
Simon Glass361ad6a2015-03-25 12:22:09 -0600363 return ret;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000364 }
Julius Wernereaf3e612013-07-19 13:12:08 -0700365 memcpy((unsigned char *)&hub->desc, buffer, length);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000366 /* adjust 16bit values */
Lucas Stach93ad9082012-09-06 08:00:13 +0200367 put_unaligned(le16_to_cpu(get_unaligned(
368 &descriptor->wHubCharacteristics)),
369 &hub->desc.wHubCharacteristics);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000370 /* set the bitmap */
371 bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
372 /* devices not removable by default */
373 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
374 bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
375 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
376
377 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
378 hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
379
380 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
381 hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i];
382
383 dev->maxchild = descriptor->bNbrPorts;
Vivek Gautamceb49722013-04-12 16:34:33 +0530384 debug("%d ports detected\n", dev->maxchild);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000385
Lucas Stach93ad9082012-09-06 08:00:13 +0200386 hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics);
387 switch (hubCharacteristics & HUB_CHAR_LPSM) {
Marek Vasut23faf2b2012-02-13 18:58:17 +0000388 case 0x00:
Vivek Gautamceb49722013-04-12 16:34:33 +0530389 debug("ganged power switching\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000390 break;
391 case 0x01:
Vivek Gautamceb49722013-04-12 16:34:33 +0530392 debug("individual port power switching\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000393 break;
394 case 0x02:
395 case 0x03:
Vivek Gautamceb49722013-04-12 16:34:33 +0530396 debug("unknown reserved power switching mode\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000397 break;
398 }
399
Lucas Stach93ad9082012-09-06 08:00:13 +0200400 if (hubCharacteristics & HUB_CHAR_COMPOUND)
Vivek Gautamceb49722013-04-12 16:34:33 +0530401 debug("part of a compound device\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000402 else
Vivek Gautamceb49722013-04-12 16:34:33 +0530403 debug("standalone hub\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000404
Lucas Stach93ad9082012-09-06 08:00:13 +0200405 switch (hubCharacteristics & HUB_CHAR_OCPM) {
Marek Vasut23faf2b2012-02-13 18:58:17 +0000406 case 0x00:
Vivek Gautamceb49722013-04-12 16:34:33 +0530407 debug("global over-current protection\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000408 break;
409 case 0x08:
Vivek Gautamceb49722013-04-12 16:34:33 +0530410 debug("individual port over-current protection\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000411 break;
412 case 0x10:
413 case 0x18:
Vivek Gautamceb49722013-04-12 16:34:33 +0530414 debug("no over-current protection\n");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000415 break;
416 }
417
Vivek Gautamceb49722013-04-12 16:34:33 +0530418 debug("power on to power good time: %dms\n",
419 descriptor->bPwrOn2PwrGood * 2);
420 debug("hub controller current requirement: %dmA\n",
421 descriptor->bHubContrCurrent);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000422
423 for (i = 0; i < dev->maxchild; i++)
Vivek Gautamceb49722013-04-12 16:34:33 +0530424 debug("port %d is%s removable\n", i + 1,
425 hub->desc.DeviceRemovable[(i + 1) / 8] & \
426 (1 << ((i + 1) % 8)) ? " not" : "");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000427
428 if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530429 debug("usb_hub_configure: failed to get Status - " \
430 "too long: %d\n", descriptor->bLength);
Simon Glass361ad6a2015-03-25 12:22:09 -0600431 return -EFBIG;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000432 }
433
Simon Glass361ad6a2015-03-25 12:22:09 -0600434 ret = usb_get_hub_status(dev, buffer);
435 if (ret < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530436 debug("usb_hub_configure: failed to get Status %lX\n",
437 dev->status);
Simon Glass361ad6a2015-03-25 12:22:09 -0600438 return ret;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000439 }
440
Vivek Gautamceb49722013-04-12 16:34:33 +0530441#ifdef DEBUG
Marek Vasut23faf2b2012-02-13 18:58:17 +0000442 hubsts = (struct usb_hub_status *)buffer;
443#endif
Vivek Gautamceb49722013-04-12 16:34:33 +0530444
445 debug("get_hub_status returned status %X, change %X\n",
446 le16_to_cpu(hubsts->wHubStatus),
447 le16_to_cpu(hubsts->wHubChange));
448 debug("local power source is %s\n",
449 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
450 "lost (inactive)" : "good");
451 debug("%sover-current condition exists\n",
452 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
453 "" : "no ");
Marek Vasut23faf2b2012-02-13 18:58:17 +0000454 usb_hub_power_on(hub);
455
Dan Murphy3615a992013-08-01 14:06:01 -0500456 /*
457 * Reset any devices that may be in a bad state when applying
458 * the power. This is a __weak function. Resetting of the devices
459 * should occur in the board file of the device.
460 */
461 for (i = 0; i < dev->maxchild; i++)
462 usb_hub_reset_devices(i + 1);
463
Marek Vasut23faf2b2012-02-13 18:58:17 +0000464 for (i = 0; i < dev->maxchild; i++) {
Puneet Saxenaf5766132012-04-03 14:56:06 +0530465 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000466 unsigned short portstatus, portchange;
Vipin Kumarb6d78522012-12-13 16:25:53 +0530467 int ret;
468 ulong start = get_timer(0);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000469
Simon Glass054fe482015-03-25 12:22:10 -0600470#ifdef CONFIG_DM_USB
471 debug("\n\nScanning '%s' port %d\n", dev->dev->name, i + 1);
472#else
Simon Glass361ad6a2015-03-25 12:22:09 -0600473 debug("\n\nScanning port %d\n", i + 1);
Simon Glass054fe482015-03-25 12:22:10 -0600474#endif
Vipin Kumarb6d78522012-12-13 16:25:53 +0530475 /*
476 * Wait for (whichever finishes first)
477 * - A maximum of 10 seconds
478 * This is a purely observational value driven by connecting
479 * a few broken pen drives and taking the max * 1.5 approach
480 * - connection_change and connection state to report same
481 * state
482 */
483 do {
484 ret = usb_get_port_status(dev, i + 1, portsts);
485 if (ret < 0) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530486 debug("get_port_status failed\n");
Vipin Kumarb6d78522012-12-13 16:25:53 +0530487 break;
488 }
489
490 portstatus = le16_to_cpu(portsts->wPortStatus);
491 portchange = le16_to_cpu(portsts->wPortChange);
492
Marek Vasutdcc7dbc2015-08-05 03:19:22 +0200493 /* No connection change happened, wait a bit more. */
494 if (!(portchange & USB_PORT_STAT_C_CONNECTION))
495 continue;
496
497 /* Test if the connection came up, and if so, exit. */
498 if (portstatus & USB_PORT_STAT_CONNECTION)
Vipin Kumarb6d78522012-12-13 16:25:53 +0530499 break;
500
Marek Vasutdcc7dbc2015-08-05 03:19:22 +0200501 } while (get_timer(start) < CONFIG_SYS_HZ * 1);
Vipin Kumarb6d78522012-12-13 16:25:53 +0530502
503 if (ret < 0)
Marek Vasut23faf2b2012-02-13 18:58:17 +0000504 continue;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000505
Vivek Gautamceb49722013-04-12 16:34:33 +0530506 debug("Port %d Status %X Change %X\n",
507 i + 1, portstatus, portchange);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000508
509 if (portchange & USB_PORT_STAT_C_CONNECTION) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530510 debug("port %d connection change\n", i + 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000511 usb_hub_port_connect_change(dev, i);
512 }
513 if (portchange & USB_PORT_STAT_C_ENABLE) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530514 debug("port %d enable change, status %x\n",
515 i + 1, portstatus);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000516 usb_clear_port_feature(dev, i + 1,
517 USB_PORT_FEAT_C_ENABLE);
Kuo-Jung Sue82a3162013-05-15 15:29:23 +0800518 /*
519 * The following hack causes a ghost device problem
520 * to Faraday EHCI
521 */
522#ifndef CONFIG_USB_EHCI_FARADAY
Marek Vasut23faf2b2012-02-13 18:58:17 +0000523 /* EM interference sometimes causes bad shielded USB
524 * devices to be shutdown by the hub, this hack enables
525 * them again. Works at least with mouse driver */
526 if (!(portstatus & USB_PORT_STAT_ENABLE) &&
527 (portstatus & USB_PORT_STAT_CONNECTION) &&
Simon Glass054fe482015-03-25 12:22:10 -0600528 usb_device_has_child_on_port(dev, i)) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530529 debug("already running port %i " \
530 "disabled by hub (EMI?), " \
531 "re-enabling...\n", i + 1);
532 usb_hub_port_connect_change(dev, i);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000533 }
Kuo-Jung Sue82a3162013-05-15 15:29:23 +0800534#endif
Marek Vasut23faf2b2012-02-13 18:58:17 +0000535 }
536 if (portstatus & USB_PORT_STAT_SUSPEND) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530537 debug("port %d suspend change\n", i + 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000538 usb_clear_port_feature(dev, i + 1,
539 USB_PORT_FEAT_SUSPEND);
540 }
541
542 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530543 debug("port %d over-current change\n", i + 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000544 usb_clear_port_feature(dev, i + 1,
545 USB_PORT_FEAT_C_OVER_CURRENT);
546 usb_hub_power_on(hub);
547 }
548
549 if (portchange & USB_PORT_STAT_C_RESET) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530550 debug("port %d reset change\n", i + 1);
Marek Vasut23faf2b2012-02-13 18:58:17 +0000551 usb_clear_port_feature(dev, i + 1,
552 USB_PORT_FEAT_C_RESET);
553 }
554 } /* end for i all ports */
555
556 return 0;
557}
558
Simon Glass361ad6a2015-03-25 12:22:09 -0600559static int usb_hub_check(struct usb_device *dev, int ifnum)
Marek Vasut23faf2b2012-02-13 18:58:17 +0000560{
561 struct usb_interface *iface;
Simon Glass361ad6a2015-03-25 12:22:09 -0600562 struct usb_endpoint_descriptor *ep = NULL;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000563
564 iface = &dev->config.if_desc[ifnum];
565 /* Is it a hub? */
566 if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
Simon Glass361ad6a2015-03-25 12:22:09 -0600567 goto err;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000568 /* Some hubs have a subclass of 1, which AFAICT according to the */
569 /* specs is not defined, but it works */
570 if ((iface->desc.bInterfaceSubClass != 0) &&
571 (iface->desc.bInterfaceSubClass != 1))
Simon Glass361ad6a2015-03-25 12:22:09 -0600572 goto err;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000573 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
574 if (iface->desc.bNumEndpoints != 1)
Simon Glass361ad6a2015-03-25 12:22:09 -0600575 goto err;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000576 ep = &iface->ep_desc[0];
577 /* Output endpoint? Curiousier and curiousier.. */
578 if (!(ep->bEndpointAddress & USB_DIR_IN))
Simon Glass361ad6a2015-03-25 12:22:09 -0600579 goto err;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000580 /* If it's not an interrupt endpoint, we'd better punt! */
581 if ((ep->bmAttributes & 3) != 3)
Simon Glass361ad6a2015-03-25 12:22:09 -0600582 goto err;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000583 /* We found a hub */
Vivek Gautamceb49722013-04-12 16:34:33 +0530584 debug("USB hub found\n");
Simon Glass361ad6a2015-03-25 12:22:09 -0600585 return 0;
586
587err:
588 debug("USB hub not found: bInterfaceClass=%d, bInterfaceSubClass=%d, bNumEndpoints=%d\n",
589 iface->desc.bInterfaceClass, iface->desc.bInterfaceSubClass,
590 iface->desc.bNumEndpoints);
591 if (ep) {
592 debug(" bEndpointAddress=%#x, bmAttributes=%d",
593 ep->bEndpointAddress, ep->bmAttributes);
594 }
595
596 return -ENOENT;
597}
598
599int usb_hub_probe(struct usb_device *dev, int ifnum)
600{
601 int ret;
602
603 ret = usb_hub_check(dev, ifnum);
604 if (ret)
605 return 0;
Marek Vasut23faf2b2012-02-13 18:58:17 +0000606 ret = usb_hub_configure(dev);
607 return ret;
608}
Simon Glass054fe482015-03-25 12:22:10 -0600609
610#ifdef CONFIG_DM_USB
611int usb_hub_scan(struct udevice *hub)
612{
613 struct usb_device *udev = dev_get_parentdata(hub);
614
615 return usb_hub_configure(udev);
616}
617
618static int usb_hub_post_bind(struct udevice *dev)
619{
620 /* Scan the bus for devices */
621 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
622}
623
624static int usb_hub_post_probe(struct udevice *dev)
625{
626 debug("%s\n", __func__);
627 return usb_hub_scan(dev);
628}
629
630static const struct udevice_id usb_hub_ids[] = {
631 { .compatible = "usb-hub" },
632 { }
633};
634
635U_BOOT_DRIVER(usb_generic_hub) = {
636 .name = "usb_hub",
637 .id = UCLASS_USB_HUB,
638 .of_match = usb_hub_ids,
639 .flags = DM_FLAG_ALLOC_PRIV_DMA,
640};
641
642UCLASS_DRIVER(usb_hub) = {
643 .id = UCLASS_USB_HUB,
644 .name = "usb_hub",
645 .post_bind = usb_hub_post_bind,
646 .post_probe = usb_hub_post_probe,
647 .child_pre_probe = usb_child_pre_probe,
648 .per_child_auto_alloc_size = sizeof(struct usb_device),
649 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
650};
651
652static const struct usb_device_id hub_id_table[] = {
653 {
654 .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
655 .bDeviceClass = USB_CLASS_HUB
656 },
657 { } /* Terminating entry */
658};
659
Simon Glassabb59cf2015-07-06 16:47:51 -0600660U_BOOT_USB_DEVICE(usb_generic_hub, hub_id_table);
Simon Glass054fe482015-03-25 12:22:10 -0600661
662#endif