blob: e4a120120dc23d068686d0e69e6d8248f9b35308 [file] [log] [blame]
Marek Vasut6da5af32012-02-13 18:58:17 +00001/*
2 *
3 * Most of this source has been derived from the Linux USB
4 * project:
5 * (C) Copyright Linus Torvalds 1999
6 * (C) Copyright Johannes Erdfelt 1999-2001
7 * (C) Copyright Andreas Gal 1999
8 * (C) Copyright Gregory P. Smith 1999
9 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
10 * (C) Copyright Randy Dunlap 2000
11 * (C) Copyright David Brownell 2000 (kernel hotplug, usb_device_id)
12 * (C) Copyright Yggdrasil Computing, Inc. 2000
13 * (usb_device_id matching changes by Adam J. Richter)
14 *
15 * Adapted for U-Boot:
16 * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
17 *
18 * See file CREDITS for list of people who contributed to this
19 * project.
20 *
21 * This program is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU General Public License as
23 * published by the Free Software Foundation; either version 2 of
24 * the License, or (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 * MA 02111-1307 USA
35 *
36 */
37
38/****************************************************************************
39 * HUB "Driver"
40 * Probes device for being a hub and configurate it
41 */
42
43#include <common.h>
44#include <command.h>
45#include <asm/processor.h>
Lucas Stach835e11e2012-09-06 08:00:13 +020046#include <asm/unaligned.h>
Marek Vasut6da5af32012-02-13 18:58:17 +000047#include <linux/ctype.h>
48#include <asm/byteorder.h>
49#include <asm/unaligned.h>
50
51#include <usb.h>
52#ifdef CONFIG_4xx
53#include <asm/4xx_pci.h>
54#endif
55
56#ifdef DEBUG
57#define USB_DEBUG 1
58#define USB_HUB_DEBUG 1
59#else
60#define USB_DEBUG 0
61#define USB_HUB_DEBUG 0
62#endif
63
64#define USB_PRINTF(fmt, args...) debug_cond(USB_DEBUG, fmt, ##args)
65#define USB_HUB_PRINTF(fmt, args...) debug_cond(USB_HUB_DEBUG, fmt, ##args)
66
67#define USB_BUFSIZ 512
68
69static struct usb_hub_device hub_dev[USB_MAX_HUB];
70static int usb_hub_index;
71
72
73static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
74{
75 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
76 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
77 USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
78}
79
80static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
81{
82 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
83 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
84 port, NULL, 0, USB_CNTL_TIMEOUT);
85}
86
87static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
88{
89 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
90 USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
91 port, NULL, 0, USB_CNTL_TIMEOUT);
92}
93
94static int usb_get_hub_status(struct usb_device *dev, void *data)
95{
96 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
97 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
98 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
99}
100
101static int usb_get_port_status(struct usb_device *dev, int port, void *data)
102{
103 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
104 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
105 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
106}
107
108
109static void usb_hub_power_on(struct usb_hub_device *hub)
110{
111 int i;
112 struct usb_device *dev;
Wolfgang Grandegger9bb46e02011-12-21 00:01:09 +0000113 unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
Marek Vasut6da5af32012-02-13 18:58:17 +0000114
115 dev = hub->pusb_dev;
116 /* Enable power to the ports */
117 USB_HUB_PRINTF("enabling power on all ports\n");
118 for (i = 0; i < dev->maxchild; i++) {
119 usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
120 USB_HUB_PRINTF("port %d returns %lX\n", i + 1, dev->status);
Marek Vasut6da5af32012-02-13 18:58:17 +0000121 }
Wolfgang Grandegger9bb46e02011-12-21 00:01:09 +0000122
123 /* Wait at least 100 msec for power to become stable */
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000124 mdelay(max(pgood_delay, (unsigned)100));
Marek Vasut6da5af32012-02-13 18:58:17 +0000125}
126
127void usb_hub_reset(void)
128{
129 usb_hub_index = 0;
130}
131
132static struct usb_hub_device *usb_hub_allocate(void)
133{
134 if (usb_hub_index < USB_MAX_HUB)
135 return &hub_dev[usb_hub_index++];
136
137 printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
138 return NULL;
139}
140
141#define MAX_TRIES 5
142
143static inline char *portspeed(int portstatus)
144{
145 if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
146 return "480 Mb/s";
147 else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
148 return "1.5 Mb/s";
149 else
150 return "12 Mb/s";
151}
152
153int hub_port_reset(struct usb_device *dev, int port,
154 unsigned short *portstat)
155{
156 int tries;
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530157 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
Marek Vasut6da5af32012-02-13 18:58:17 +0000158 unsigned short portstatus, portchange;
159
160 USB_HUB_PRINTF("hub_port_reset: resetting port %d...\n", port);
161 for (tries = 0; tries < MAX_TRIES; tries++) {
162
163 usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000164 mdelay(200);
Marek Vasut6da5af32012-02-13 18:58:17 +0000165
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530166 if (usb_get_port_status(dev, port + 1, portsts) < 0) {
Marek Vasut6da5af32012-02-13 18:58:17 +0000167 USB_HUB_PRINTF("get_port_status failed status %lX\n",
168 dev->status);
169 return -1;
170 }
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530171 portstatus = le16_to_cpu(portsts->wPortStatus);
172 portchange = le16_to_cpu(portsts->wPortChange);
Marek Vasut6da5af32012-02-13 18:58:17 +0000173
174 USB_HUB_PRINTF("portstatus %x, change %x, %s\n",
175 portstatus, portchange,
176 portspeed(portstatus));
177
178 USB_HUB_PRINTF("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
179 " USB_PORT_STAT_ENABLE %d\n",
180 (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
181 (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
182 (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
183
184 if ((portchange & USB_PORT_STAT_C_CONNECTION) ||
185 !(portstatus & USB_PORT_STAT_CONNECTION))
186 return -1;
187
188 if (portstatus & USB_PORT_STAT_ENABLE)
189 break;
190
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000191 mdelay(200);
Marek Vasut6da5af32012-02-13 18:58:17 +0000192 }
193
194 if (tries == MAX_TRIES) {
195 USB_HUB_PRINTF("Cannot enable port %i after %i retries, " \
196 "disabling port.\n", port + 1, MAX_TRIES);
197 USB_HUB_PRINTF("Maybe the USB cable is bad?\n");
198 return -1;
199 }
200
201 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
202 *portstat = portstatus;
203 return 0;
204}
205
206
207void usb_hub_port_connect_change(struct usb_device *dev, int port)
208{
209 struct usb_device *usb;
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530210 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
Marek Vasut6da5af32012-02-13 18:58:17 +0000211 unsigned short portstatus;
212
213 /* Check status */
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530214 if (usb_get_port_status(dev, port + 1, portsts) < 0) {
Marek Vasut6da5af32012-02-13 18:58:17 +0000215 USB_HUB_PRINTF("get_port_status failed\n");
216 return;
217 }
218
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530219 portstatus = le16_to_cpu(portsts->wPortStatus);
Marek Vasut6da5af32012-02-13 18:58:17 +0000220 USB_HUB_PRINTF("portstatus %x, change %x, %s\n",
221 portstatus,
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530222 le16_to_cpu(portsts->wPortChange),
Marek Vasut6da5af32012-02-13 18:58:17 +0000223 portspeed(portstatus));
224
225 /* Clear the connection change status */
226 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
227
228 /* Disconnect any existing devices under this port */
229 if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
230 (!(portstatus & USB_PORT_STAT_ENABLE))) || (dev->children[port])) {
231 USB_HUB_PRINTF("usb_disconnect(&hub->children[port]);\n");
232 /* Return now if nothing is connected */
233 if (!(portstatus & USB_PORT_STAT_CONNECTION))
234 return;
235 }
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000236 mdelay(200);
Marek Vasut6da5af32012-02-13 18:58:17 +0000237
238 /* Reset the port */
239 if (hub_port_reset(dev, port, &portstatus) < 0) {
240 printf("cannot reset port %i!?\n", port + 1);
241 return;
242 }
243
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000244 mdelay(200);
Marek Vasut6da5af32012-02-13 18:58:17 +0000245
246 /* Allocate a new device struct for it */
Lucas Stacha3231282012-09-26 00:14:34 +0200247 usb = usb_alloc_new_device(dev->controller);
Marek Vasut6da5af32012-02-13 18:58:17 +0000248
249 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
250 usb->speed = USB_SPEED_HIGH;
251 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
252 usb->speed = USB_SPEED_LOW;
253 else
254 usb->speed = USB_SPEED_FULL;
255
256 dev->children[port] = usb;
257 usb->parent = dev;
258 usb->portnr = port + 1;
259 /* Run it through the hoops (find a driver, etc) */
260 if (usb_new_device(usb)) {
261 /* Woops, disable the port */
262 USB_HUB_PRINTF("hub: disabling port %d\n", port + 1);
263 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
264 }
265}
266
267
268static int usb_hub_configure(struct usb_device *dev)
269{
270 int i;
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530271 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
272 unsigned char *bitmap;
Lucas Stach835e11e2012-09-06 08:00:13 +0200273 short hubCharacteristics;
Marek Vasut6da5af32012-02-13 18:58:17 +0000274 struct usb_hub_descriptor *descriptor;
275 struct usb_hub_device *hub;
276#ifdef USB_HUB_DEBUG
277 struct usb_hub_status *hubsts;
278#endif
279
280 /* "allocate" Hub device */
281 hub = usb_hub_allocate();
282 if (hub == NULL)
283 return -1;
284 hub->pusb_dev = dev;
285 /* Get the the hub descriptor */
286 if (usb_get_hub_descriptor(dev, buffer, 4) < 0) {
287 USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
288 "descriptor, giving up %lX\n", dev->status);
289 return -1;
290 }
291 descriptor = (struct usb_hub_descriptor *)buffer;
292
293 /* silence compiler warning if USB_BUFSIZ is > 256 [= sizeof(char)] */
294 i = descriptor->bLength;
295 if (i > USB_BUFSIZ) {
296 USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
297 "descriptor - too long: %d\n",
298 descriptor->bLength);
299 return -1;
300 }
301
302 if (usb_get_hub_descriptor(dev, buffer, descriptor->bLength) < 0) {
303 USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
304 "descriptor 2nd giving up %lX\n", dev->status);
305 return -1;
306 }
307 memcpy((unsigned char *)&hub->desc, buffer, descriptor->bLength);
308 /* adjust 16bit values */
Lucas Stach835e11e2012-09-06 08:00:13 +0200309 put_unaligned(le16_to_cpu(get_unaligned(
310 &descriptor->wHubCharacteristics)),
311 &hub->desc.wHubCharacteristics);
Marek Vasut6da5af32012-02-13 18:58:17 +0000312 /* set the bitmap */
313 bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
314 /* devices not removable by default */
315 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
316 bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
317 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
318
319 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
320 hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
321
322 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
323 hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i];
324
325 dev->maxchild = descriptor->bNbrPorts;
326 USB_HUB_PRINTF("%d ports detected\n", dev->maxchild);
327
Lucas Stach835e11e2012-09-06 08:00:13 +0200328 hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics);
329 switch (hubCharacteristics & HUB_CHAR_LPSM) {
Marek Vasut6da5af32012-02-13 18:58:17 +0000330 case 0x00:
331 USB_HUB_PRINTF("ganged power switching\n");
332 break;
333 case 0x01:
334 USB_HUB_PRINTF("individual port power switching\n");
335 break;
336 case 0x02:
337 case 0x03:
338 USB_HUB_PRINTF("unknown reserved power switching mode\n");
339 break;
340 }
341
Lucas Stach835e11e2012-09-06 08:00:13 +0200342 if (hubCharacteristics & HUB_CHAR_COMPOUND)
Marek Vasut6da5af32012-02-13 18:58:17 +0000343 USB_HUB_PRINTF("part of a compound device\n");
344 else
345 USB_HUB_PRINTF("standalone hub\n");
346
Lucas Stach835e11e2012-09-06 08:00:13 +0200347 switch (hubCharacteristics & HUB_CHAR_OCPM) {
Marek Vasut6da5af32012-02-13 18:58:17 +0000348 case 0x00:
349 USB_HUB_PRINTF("global over-current protection\n");
350 break;
351 case 0x08:
352 USB_HUB_PRINTF("individual port over-current protection\n");
353 break;
354 case 0x10:
355 case 0x18:
356 USB_HUB_PRINTF("no over-current protection\n");
357 break;
358 }
359
360 USB_HUB_PRINTF("power on to power good time: %dms\n",
361 descriptor->bPwrOn2PwrGood * 2);
362 USB_HUB_PRINTF("hub controller current requirement: %dmA\n",
363 descriptor->bHubContrCurrent);
364
365 for (i = 0; i < dev->maxchild; i++)
366 USB_HUB_PRINTF("port %d is%s removable\n", i + 1,
367 hub->desc.DeviceRemovable[(i + 1) / 8] & \
368 (1 << ((i + 1) % 8)) ? " not" : "");
369
370 if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
371 USB_HUB_PRINTF("usb_hub_configure: failed to get Status - " \
372 "too long: %d\n", descriptor->bLength);
373 return -1;
374 }
375
376 if (usb_get_hub_status(dev, buffer) < 0) {
377 USB_HUB_PRINTF("usb_hub_configure: failed to get Status %lX\n",
378 dev->status);
379 return -1;
380 }
381
382#ifdef USB_HUB_DEBUG
383 hubsts = (struct usb_hub_status *)buffer;
384#endif
385 USB_HUB_PRINTF("get_hub_status returned status %X, change %X\n",
386 le16_to_cpu(hubsts->wHubStatus),
387 le16_to_cpu(hubsts->wHubChange));
388 USB_HUB_PRINTF("local power source is %s\n",
389 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
390 "lost (inactive)" : "good");
391 USB_HUB_PRINTF("%sover-current condition exists\n",
392 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
393 "" : "no ");
394 usb_hub_power_on(hub);
395
396 for (i = 0; i < dev->maxchild; i++) {
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530397 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
Marek Vasut6da5af32012-02-13 18:58:17 +0000398 unsigned short portstatus, portchange;
399
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530400 if (usb_get_port_status(dev, i + 1, portsts) < 0) {
Marek Vasut6da5af32012-02-13 18:58:17 +0000401 USB_HUB_PRINTF("get_port_status failed\n");
402 continue;
403 }
404
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530405 portstatus = le16_to_cpu(portsts->wPortStatus);
406 portchange = le16_to_cpu(portsts->wPortChange);
Marek Vasut6da5af32012-02-13 18:58:17 +0000407 USB_HUB_PRINTF("Port %d Status %X Change %X\n",
408 i + 1, portstatus, portchange);
409
410 if (portchange & USB_PORT_STAT_C_CONNECTION) {
411 USB_HUB_PRINTF("port %d connection change\n", i + 1);
412 usb_hub_port_connect_change(dev, i);
413 }
414 if (portchange & USB_PORT_STAT_C_ENABLE) {
415 USB_HUB_PRINTF("port %d enable change, status %x\n",
416 i + 1, portstatus);
417 usb_clear_port_feature(dev, i + 1,
418 USB_PORT_FEAT_C_ENABLE);
419
420 /* EM interference sometimes causes bad shielded USB
421 * devices to be shutdown by the hub, this hack enables
422 * them again. Works at least with mouse driver */
423 if (!(portstatus & USB_PORT_STAT_ENABLE) &&
424 (portstatus & USB_PORT_STAT_CONNECTION) &&
425 ((dev->children[i]))) {
426 USB_HUB_PRINTF("already running port %i " \
427 "disabled by hub (EMI?), " \
428 "re-enabling...\n", i + 1);
429 usb_hub_port_connect_change(dev, i);
430 }
431 }
432 if (portstatus & USB_PORT_STAT_SUSPEND) {
433 USB_HUB_PRINTF("port %d suspend change\n", i + 1);
434 usb_clear_port_feature(dev, i + 1,
435 USB_PORT_FEAT_SUSPEND);
436 }
437
438 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
439 USB_HUB_PRINTF("port %d over-current change\n", i + 1);
440 usb_clear_port_feature(dev, i + 1,
441 USB_PORT_FEAT_C_OVER_CURRENT);
442 usb_hub_power_on(hub);
443 }
444
445 if (portchange & USB_PORT_STAT_C_RESET) {
446 USB_HUB_PRINTF("port %d reset change\n", i + 1);
447 usb_clear_port_feature(dev, i + 1,
448 USB_PORT_FEAT_C_RESET);
449 }
450 } /* end for i all ports */
451
452 return 0;
453}
454
455int usb_hub_probe(struct usb_device *dev, int ifnum)
456{
457 struct usb_interface *iface;
458 struct usb_endpoint_descriptor *ep;
459 int ret;
460
461 iface = &dev->config.if_desc[ifnum];
462 /* Is it a hub? */
463 if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
464 return 0;
465 /* Some hubs have a subclass of 1, which AFAICT according to the */
466 /* specs is not defined, but it works */
467 if ((iface->desc.bInterfaceSubClass != 0) &&
468 (iface->desc.bInterfaceSubClass != 1))
469 return 0;
470 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
471 if (iface->desc.bNumEndpoints != 1)
472 return 0;
473 ep = &iface->ep_desc[0];
474 /* Output endpoint? Curiousier and curiousier.. */
475 if (!(ep->bEndpointAddress & USB_DIR_IN))
476 return 0;
477 /* If it's not an interrupt endpoint, we'd better punt! */
478 if ((ep->bmAttributes & 3) != 3)
479 return 0;
480 /* We found a hub */
481 USB_HUB_PRINTF("USB hub found\n");
482 ret = usb_hub_configure(dev);
483 return ret;
484}