libpayload: Add timeouts in the EHCI USB driver

We should always have some timeout when we wait for the hardware. This adds
missing timeouts to the EHCI driver.

Change-Id: I13ba532a6daf47510b16b8fdbe572a21f1d8b09c
Signed-off-by: Nico Huber <nico.huber@secunet.com>
Reviewed-on: http://review.coreboot.org/1077
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
This commit is contained in:
Nico Huber 2012-05-21 14:53:43 +02:00 committed by Stefan Reinauer
parent afe86c0b74
commit 0421dc84df
2 changed files with 91 additions and 31 deletions

View File

@ -167,7 +167,21 @@ static int wait_for_tds(qtd_t *head)
qtd_t *cur = head; qtd_t *cur = head;
while (1) { while (1) {
if (0) dump_td(virt_to_phys(cur)); if (0) dump_td(virt_to_phys(cur));
while ((cur->token & QTD_ACTIVE) && !(cur->token & QTD_HALTED)) udelay(60);
/* wait for results */
/* TOTEST: how long to wait?
* tested with some USB2.0 flash sticks:
* slowest took around 180ms
*/
int timeout = 10000; /* time out after 10000 * 50us == 500ms */
while ((cur->token & QTD_ACTIVE) && !(cur->token & QTD_HALTED)
&& timeout--)
udelay(50);
if (timeout < 0) {
printf("Error: ehci: queue transfer "
"processing timed out.\n");
return 1;
}
if (cur->token & QTD_HALTED) { if (cur->token & QTD_HALTED) {
printf("ERROR with packet\n"); printf("ERROR with packet\n");
dump_td(virt_to_phys(cur)); dump_td(virt_to_phys(cur));
@ -185,6 +199,51 @@ static int wait_for_tds(qtd_t *head)
return result; return result;
} }
static int ehci_set_async_schedule(ehci_t *ehcic, int enable)
{
/* Set async schedule status. */
if (enable)
ehcic->operation->usbcmd |= HC_OP_ASYNC_SCHED_EN;
else
ehcic->operation->usbcmd &= ~HC_OP_ASYNC_SCHED_EN;
/* Wait for the controller to accept async schedule status.
* This shouldn't take too long, but we should timeout nevertheless.
*/
enable = enable ? HC_OP_ASYNC_SCHED_STAT : 0;
int timeout = 100; /* time out after 100ms */
while (((ehcic->operation->usbsts & HC_OP_ASYNC_SCHED_STAT) != enable)
&& timeout--)
mdelay(1);
if (timeout < 0) {
debug("ehci async schedule status change timed out.\n");
return 1;
}
return 0;
}
static int ehci_process_async_schedule(
ehci_t *ehcic, ehci_qh_t *qhead, qtd_t *head)
{
int result;
/* make sure async schedule is disabled */
if (ehci_set_async_schedule(ehcic, 0)) return 1;
/* hook up QH */
ehcic->operation->asynclistaddr = virt_to_phys(qhead);
/* start async schedule */
if (ehci_set_async_schedule(ehcic, 1)) return 1;
/* wait for result */
result = wait_for_tds(head);
/* disable async schedule */
ehci_set_async_schedule(ehcic, 0);
return result;
}
static int ehci_bulk (endpoint_t *ep, int size, u8 *data, int finalize) static int ehci_bulk (endpoint_t *ep, int size, u8 *data, int finalize)
{ {
int result = 0; int result = 0;
@ -239,19 +298,8 @@ static int ehci_bulk (endpoint_t *ep, int size, u8 *data, int finalize)
qh->td.token |= (ep->toggle?QTD_TOGGLE_DATA1:0); qh->td.token |= (ep->toggle?QTD_TOGGLE_DATA1:0);
head->token |= (ep->toggle?QTD_TOGGLE_DATA1:0); head->token |= (ep->toggle?QTD_TOGGLE_DATA1:0);
/* hook up QH */ result = ehci_process_async_schedule(
EHCI_INST(ep->dev->controller)->operation->asynclistaddr = virt_to_phys(qh); EHCI_INST(ep->dev->controller), qh, head);
/* start async schedule */
EHCI_INST(ep->dev->controller)->operation->usbcmd |= HC_OP_ASYNC_SCHED_EN;
while (!(EHCI_INST(ep->dev->controller)->operation->usbsts & HC_OP_ASYNC_SCHED_STAT)) ; /* wait */
/* wait for result */
result = wait_for_tds(head);
/* disable async schedule */
EHCI_INST(ep->dev->controller)->operation->usbcmd &= ~HC_OP_ASYNC_SCHED_EN;
while (EHCI_INST(ep->dev->controller)->operation->usbsts & HC_OP_ASYNC_SCHED_STAT) ; /* wait */
ep->toggle = (cur->token & QTD_TOGGLE_MASK) >> QTD_TOGGLE_SHIFT; ep->toggle = (cur->token & QTD_TOGGLE_MASK) >> QTD_TOGGLE_SHIFT;
@ -338,18 +386,8 @@ static int ehci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq
(hubaddr << QH_HUB_ADDRESS_SHIFT); (hubaddr << QH_HUB_ADDRESS_SHIFT);
qh->td.next_qtd = virt_to_phys(head); qh->td.next_qtd = virt_to_phys(head);
/* hook up QH */ result = ehci_process_async_schedule(
EHCI_INST(dev->controller)->operation->asynclistaddr = virt_to_phys(qh); EHCI_INST(dev->controller), qh, head);
/* start async schedule */
EHCI_INST(dev->controller)->operation->usbcmd |= HC_OP_ASYNC_SCHED_EN;
while (!(EHCI_INST(dev->controller)->operation->usbsts & HC_OP_ASYNC_SCHED_STAT)) ; /* wait */
result = wait_for_tds(head);
/* disable async schedule */
EHCI_INST(dev->controller)->operation->usbcmd &= ~HC_OP_ASYNC_SCHED_EN;
while (EHCI_INST(dev->controller)->operation->usbsts & HC_OP_ASYNC_SCHED_STAT) ; /* wait */
free_qh_and_tds(qh, head); free_qh_and_tds(qh, head);
return result; return result;

View File

@ -55,9 +55,21 @@ ehci_rh_hand_over_port (usbdev_t *dev, int port)
{ {
debug("giving up port %x, it's USB1\n", port+1); debug("giving up port %x, it's USB1\n", port+1);
/* Clear ConnectStatusChange before evaluation */
/* RW/C register, so clear it by writing 1 */
RH_INST(dev)->ports[port] |= P_CONN_STATUS_CHANGE;
/* Lowspeed device. Hand over to companion */ /* Lowspeed device. Hand over to companion */
RH_INST(dev)->ports[port] |= P_PORT_OWNER; RH_INST(dev)->ports[port] |= P_PORT_OWNER;
do {} while (!(RH_INST(dev)->ports[port] & P_CONN_STATUS_CHANGE));
/* TOTEST: how long to wait? trying 100ms for now */
int timeout = 10; /* timeout after 10 * 10ms == 100ms */
while (!(RH_INST(dev)->ports[port] & P_CONN_STATUS_CHANGE) && timeout--)
mdelay(10);
if (!(RH_INST(dev)->ports[port] & P_CONN_STATUS_CHANGE)) {
debug("Warning: Handing port over to companion timed out.\n");
}
/* RW/C register, so clear it by writing 1 */ /* RW/C register, so clear it by writing 1 */
RH_INST(dev)->ports[port] |= P_CONN_STATUS_CHANGE; RH_INST(dev)->ports[port] |= P_CONN_STATUS_CHANGE;
return; return;
@ -73,7 +85,7 @@ ehci_rh_scanport (usbdev_t *dev, int port)
} }
/* device connected, handle */ /* device connected, handle */
if (RH_INST(dev)->ports[port] & P_CURR_CONN_STATUS) { if (RH_INST(dev)->ports[port] & P_CURR_CONN_STATUS) {
mdelay(100); mdelay(100); // usb20 spec 9.1.2
if ((RH_INST(dev)->ports[port] & P_LINE_STATUS) == P_LINE_STATUS_LOWSPEED) { if ((RH_INST(dev)->ports[port] & P_LINE_STATUS) == P_LINE_STATUS_LOWSPEED) {
ehci_rh_hand_over_port(dev, port); ehci_rh_hand_over_port(dev, port);
return; return;
@ -85,13 +97,23 @@ ehci_rh_scanport (usbdev_t *dev, int port)
RH_INST(dev)->ports[port] = (RH_INST(dev)->ports[port] & ~P_PORT_ENABLE) | P_PORT_RESET; RH_INST(dev)->ports[port] = (RH_INST(dev)->ports[port] & ~P_PORT_ENABLE) | P_PORT_RESET;
/* Wait a bit while reset is active. */ /* Wait a bit while reset is active. */
mdelay(50); mdelay(50); // usb20 spec 7.1.7.5 (TDRSTR)
/* Deassert reset. */ /* Deassert reset. */
RH_INST(dev)->ports[port] &= ~P_PORT_RESET; RH_INST(dev)->ports[port] &= ~P_PORT_RESET;
/* Wait for flag change to finish. The controller might take a while */ /* Wait max. 2ms (ehci spec 2.3.9) for flag change to finish. */
while (RH_INST(dev)->ports[port] & P_PORT_RESET) ; int timeout = 20; /* time out after 20 * 100us == 2ms */
while ((RH_INST(dev)->ports[port] & P_PORT_RESET) && timeout--)
udelay(100);
if (RH_INST(dev)->ports[port] & P_PORT_RESET) {
printf("Error: ehci_rh: port reset timed out.\n");
return;
}
/* If the host controller enabled the port, it's a high-speed
* device, otherwise it's full-speed.
*/
if (!(RH_INST(dev)->ports[port] & P_PORT_ENABLE)) { if (!(RH_INST(dev)->ports[port] & P_PORT_ENABLE)) {
ehci_rh_hand_over_port(dev, port); ehci_rh_hand_over_port(dev, port);
return; return;