#include "pci_regs.h" // PCI_BASE_ADDRESS_0
#include "usb.h" // struct usb_s
#include "farptr.h" // GET_FLATPTR
-#include "usb-hub.h" // struct usbhub_s
#include "usb-uhci.h" // init_uhci
#include "usb-ohci.h" // init_ohci
-struct companion_s {
- u16 bdf;
- u16 type;
-};
-
struct usb_ehci_s {
struct usb_s usb;
struct ehci_caps *caps;
struct ehci_regs *regs;
struct ehci_qh *async_qh;
- struct companion_s companion[8];
+ struct pci_device *companion[8];
int checkports;
int legacycount;
};
#define EHCI_TIME_POSTPOWER 20
#define EHCI_TIME_POSTRESET 2
-// Start processing of companion controllers for full/low speed devices
+// Check if need companion controllers for full/low speed devices
static void
-ehci_startcompanion(struct usb_ehci_s *cntl)
+ehci_note_port(struct usb_ehci_s *cntl)
{
+ if (--cntl->checkports)
+ // Ports still being detected.
+ return;
if (! cntl->legacycount)
// No full/low speed devices found.
return;
+ // Start companion controllers.
int i;
for (i=0; i<ARRAY_SIZE(cntl->companion); i++) {
- u16 type = cntl->companion[i].type;
- if (type == USB_TYPE_UHCI)
- uhci_init(cntl->companion[i].bdf, cntl->usb.busid + i);
- else if (type == USB_TYPE_OHCI)
- ohci_init(cntl->companion[i].bdf, cntl->usb.busid + i);
- else
- return;
+ struct pci_device *pci = cntl->companion[i];
+ if (!pci)
+ break;
+
+ // ohci/uhci_init call pci_config_XXX - don't run from irq handler.
+ wait_preempt();
+
+ if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_UHCI)
+ uhci_init(pci, cntl->usb.busid + i);
+ else if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_OHCI)
+ ohci_init(pci, cntl->usb.busid + i);
}
}
-static void
-init_ehci_port(void *data)
+// Check if device attached to port
+static int
+ehci_hub_detect(struct usbhub_s *hub, u32 port)
{
- struct usbhub_s *hub = data;
- u32 port = hub->port; // XXX - find better way to pass port
struct usb_ehci_s *cntl = container_of(hub->cntl, struct usb_ehci_s, usb);
-
u32 *portreg = &cntl->regs->portsc[port];
u32 portsc = readl(portreg);
portsc |= PORT_POWER;
writel(portreg, portsc);
msleep(EHCI_TIME_POSTPOWER);
- portsc = readl(portreg);
+ } else {
+ msleep(1); // XXX - time for connect to be detected.
}
+ portsc = readl(portreg);
if (!(portsc & PORT_CONNECT))
// No device present
- goto done;
+ goto doneearly;
if ((portsc & PORT_LINESTATUS_MASK) == PORT_LINESTATUS_KSTATE) {
// low speed device
cntl->legacycount++;
writel(portreg, portsc | PORT_OWNER);
- goto done;
+ goto doneearly;
}
// XXX - if just powered up, need to wait for USB_TIME_ATTDB?
- // Reset port
+ // Begin reset on port
portsc = (portsc & ~PORT_PE) | PORT_RESET;
writel(portreg, portsc);
msleep(USB_TIME_DRSTR);
- mutex_lock(&cntl->usb.resetlock);
+ return 0;
+
+doneearly:
+ ehci_note_port(cntl);
+ return -1;
+}
+
+// Reset device on port
+static int
+ehci_hub_reset(struct usbhub_s *hub, u32 port)
+{
+ struct usb_ehci_s *cntl = container_of(hub->cntl, struct usb_ehci_s, usb);
+ u32 *portreg = &cntl->regs->portsc[port];
+ u32 portsc = readl(portreg);
+
+ // Finish reset on port
portsc &= ~PORT_RESET;
writel(portreg, portsc);
msleep(EHCI_TIME_POSTRESET);
+ int rv = -1;
portsc = readl(portreg);
if (!(portsc & PORT_CONNECT))
// No longer connected
writel(portreg, portsc | PORT_OWNER);
goto resetfail;
}
- struct usb_pipe *pipe = usb_set_address(hub, port, USB_HIGHSPEED);
- if (!pipe)
- goto resetfail;
- mutex_unlock(&cntl->usb.resetlock);
-
- // Configure port
- int count = configure_usb_device(pipe);
- free_pipe(pipe);
- if (! count)
- // Disable port
- writel(portreg, portsc & ~PORT_PE);
- hub->devcount += count;
-done:
- if (! --cntl->checkports)
- ehci_startcompanion(cntl);
- hub->threads--;
- return;
+ rv = USB_HIGHSPEED;
resetfail:
- mutex_unlock(&cntl->usb.resetlock);
- goto done;
+ ehci_note_port(cntl);
+ return rv;
}
+// Disable port
+static void
+ehci_hub_disconnect(struct usbhub_s *hub, u32 port)
+{
+ struct usb_ehci_s *cntl = container_of(hub->cntl, struct usb_ehci_s, usb);
+ u32 *portreg = &cntl->regs->portsc[port];
+ u32 portsc = readl(portreg);
+ writel(portreg, portsc & ~PORT_PE);
+}
+
+static struct usbhub_op_s ehci_HubOp = {
+ .detect = ehci_hub_detect,
+ .reset = ehci_hub_reset,
+ .disconnect = ehci_hub_disconnect,
+};
+
// Find any devices connected to the root hub.
static int
check_ehci_ports(struct usb_ehci_s *cntl)
{
ASSERT32FLAT();
-
- // Launch a thread for every port.
struct usbhub_s hub;
memset(&hub, 0, sizeof(hub));
hub.cntl = &cntl->usb;
- int ports = cntl->checkports;
- hub.threads = ports;
- int i;
- for (i=0; i<ports; i++) {
- hub.port = i;
- run_thread(init_ehci_port, &hub);
- }
-
- // Wait for threads to complete.
- while (hub.threads)
- yield();
-
+ hub.portcount = cntl->checkports;
+ hub.op = &ehci_HubOp;
+ usb_enumerate(&hub);
return hub.devcount;
}
cmd = readl(&cntl->regs->usbcmd);
if (!(cmd & CMD_HCRESET))
break;
- if (check_time(end)) {
+ if (check_tsc(end)) {
warn_timeout();
goto fail;
}
+ yield();
}
// Disable interrupts (just to be safe).
}
int
-ehci_init(u16 bdf, int busid, int compbdf)
+ehci_init(struct pci_device *pci, int busid, struct pci_device *comppci)
{
if (! CONFIG_USB_EHCI)
return -1;
+ u16 bdf = pci->bdf;
u32 baseaddr = pci_config_readl(bdf, PCI_BASE_ADDRESS_0);
struct ehci_caps *caps = (void*)(baseaddr & PCI_BASE_ADDRESS_MEM_MASK);
u32 hcc_params = readl(&caps->hccparams);
}
struct usb_ehci_s *cntl = malloc_tmphigh(sizeof(*cntl));
+ if (!cntl) {
+ warn_noalloc();
+ return -1;
+ }
memset(cntl, 0, sizeof(*cntl));
cntl->usb.busid = busid;
+ cntl->usb.pci = pci;
cntl->usb.type = USB_TYPE_EHCI;
cntl->caps = caps;
cntl->regs = (void*)caps + readb(&caps->caplength);
- dprintf(3, "EHCI init on dev %02x:%02x.%x (regs=%p)\n"
+ dprintf(1, "EHCI init on dev %02x:%02x.%x (regs=%p)\n"
, pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
, pci_bdf_to_fn(bdf), cntl->regs);
// Find companion controllers.
int count = 0;
- int max = pci_to_bdf(pci_bdf_to_bus(bdf) + 1, 0, 0);
for (;;) {
- if (compbdf < 0 || compbdf >= bdf)
+ if (!comppci || comppci == pci)
break;
- u32 code = pci_config_readl(compbdf, PCI_CLASS_REVISION) >> 8;
- if (code == PCI_CLASS_SERIAL_USB_UHCI) {
- cntl->companion[count].bdf = compbdf;
- cntl->companion[count].type = USB_TYPE_UHCI;
- count++;
- } else if (code == PCI_CLASS_SERIAL_USB_OHCI) {
- cntl->companion[count].bdf = compbdf;
- cntl->companion[count].type = USB_TYPE_OHCI;
- count++;
- }
- compbdf = pci_next(compbdf+1, &max);
+ if (pci_classprog(comppci) == PCI_CLASS_SERIAL_USB_UHCI)
+ cntl->companion[count++] = comppci;
+ else if (pci_classprog(comppci) == PCI_CLASS_SERIAL_USB_OHCI)
+ cntl->companion[count++] = comppci;
+ comppci = comppci->next;
}
run_thread(configure_ehci, cntl);
* End point communication
****************************************************************/
-static int
-ehci_wait_qh(struct usb_ehci_s *cntl, struct ehci_qh *qh)
-{
- // XXX - 500ms just a guess
- u64 end = calc_future_tsc(500);
- for (;;) {
- if (qh->qtd_next & EHCI_PTR_TERM)
- // XXX - confirm
- return 0;
- if (check_time(end)) {
- warn_timeout();
- return -1;
- }
- yield();
- }
-}
+struct ehci_pipe {
+ struct ehci_qh qh;
+ struct ehci_qtd *next_td, *tds;
+ void *data;
+ struct usb_pipe pipe;
+};
// Wait for next USB async frame to start - for ensuring safe memory release.
static void
if (!(cmd & CMD_IAAD))
break;
}
- if (check_time(end)) {
+ if (check_tsc(end)) {
warn_timeout();
return;
}
sts = readl(&cntl->regs->usbsts);
if (sts & STS_IAA)
break;
- if (check_time(end)) {
+ if (check_tsc(end)) {
warn_timeout();
return;
}
writel(&cntl->regs->usbsts, STS_IAA);
}
-struct ehci_pipe {
- struct ehci_qh qh;
- struct ehci_qtd *next_td, *tds;
- void *data;
- struct usb_pipe pipe;
-};
+static void
+ehci_reset_pipe(struct ehci_pipe *pipe)
+{
+ SET_FLATPTR(pipe->qh.qtd_next, EHCI_PTR_TERM);
+ SET_FLATPTR(pipe->qh.alt_next, EHCI_PTR_TERM);
+ barrier();
+ SET_FLATPTR(pipe->qh.token, GET_FLATPTR(pipe->qh.token) & QTD_TOGGLE);
+}
+
+static int
+ehci_wait_td(struct ehci_pipe *pipe, struct ehci_qtd *td, int timeout)
+{
+ u64 end = calc_future_tsc(timeout);
+ u32 status;
+ for (;;) {
+ status = td->token;
+ if (!(status & QTD_STS_ACTIVE))
+ break;
+ if (check_tsc(end)) {
+ u32 cur = GET_FLATPTR(pipe->qh.current);
+ u32 tok = GET_FLATPTR(pipe->qh.token);
+ u32 next = GET_FLATPTR(pipe->qh.qtd_next);
+ warn_timeout();
+ dprintf(1, "ehci pipe=%p cur=%08x tok=%08x next=%x td=%p status=%x\n"
+ , pipe, cur, tok, next, td, status);
+ ehci_reset_pipe(pipe);
+ struct usb_ehci_s *cntl = container_of(
+ GET_FLATPTR(pipe->pipe.cntl), struct usb_ehci_s, usb);
+ ehci_waittick(cntl);
+ return -1;
+ }
+ yield();
+ }
+ if (status & QTD_STS_HALT) {
+ dprintf(1, "ehci_wait_td error - status=%x\n", status);
+ ehci_reset_pipe(pipe);
+ return -2;
+ }
+ return 0;
+}
void
ehci_free_pipe(struct usb_pipe *p)
memset(pipe, 0, sizeof(*pipe));
memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
pipe->qh.qtd_next = pipe->qh.alt_next = EHCI_PTR_TERM;
- pipe->qh.token = QTD_STS_HALT;
// Add queue head to controller list.
struct ehci_qh *async_qh = cntl->async_qh;
ASSERT32FLAT();
if (! CONFIG_USB_EHCI)
return -1;
- dprintf(5, "ehci_control %p\n", p);
+ dprintf(5, "ehci_control %p (dir=%d cmd=%d data=%d)\n"
+ , p, dir, cmdsize, datasize);
if (datasize > 4*4096 || cmdsize > 4*4096) {
// XXX - should support larger sizes.
warn_noalloc();
return -1;
}
struct ehci_pipe *pipe = container_of(p, struct ehci_pipe, pipe);
- struct usb_ehci_s *cntl = container_of(
- pipe->pipe.cntl, struct usb_ehci_s, usb);
u16 maxpacket = pipe->pipe.maxpacket;
int speed = pipe->pipe.speed;
// Transfer data
barrier();
pipe->qh.qtd_next = (u32)tds;
- barrier();
- pipe->qh.token = 0;
- int ret = ehci_wait_qh(cntl, &pipe->qh);
- pipe->qh.token = QTD_STS_HALT;
- if (ret) {
- pipe->qh.qtd_next = pipe->qh.alt_next = EHCI_PTR_TERM;
- // XXX - halt qh?
- ehci_waittick(cntl);
+ int i, ret=0;
+ for (i=0; i<3; i++) {
+ struct ehci_qtd *td = &tds[i];
+ ret = ehci_wait_td(pipe, td, 500);
+ if (ret)
+ break;
}
free(tds);
return ret;
memset(pipe, 0, sizeof(*pipe));
memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
pipe->qh.qtd_next = pipe->qh.alt_next = EHCI_PTR_TERM;
- pipe->qh.token = QTD_STS_HALT;
// Add queue head to controller list.
struct ehci_qh *async_qh = cntl->async_qh;
return &pipe->pipe;
}
-static int
-ehci_wait_td(struct ehci_qtd *td)
-{
- u64 end = calc_future_tsc(5000); // XXX - lookup real time.
- u32 status;
- for (;;) {
- status = td->token;
- if (!(status & QTD_STS_ACTIVE))
- break;
- if (check_time(end)) {
- warn_timeout();
- return -1;
- }
- yield();
- }
- if (status & QTD_STS_HALT) {
- dprintf(1, "ehci_wait_td error - status=%x\n", status);
- return -2;
- }
- return 0;
-}
-
#define STACKQTDS 4
int
| (GET_FLATPTR(pipe->pipe.tt_devaddr) << QH_HUBADDR_SHIFT)));
barrier();
SET_FLATPTR(pipe->qh.qtd_next, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
- barrier();
- SET_FLATPTR(pipe->qh.token, GET_FLATPTR(pipe->qh.token) & QTD_TOGGLE);
int tdpos = 0;
while (datasize) {
struct ehci_qtd *td = &tds[tdpos++ % STACKQTDS];
- int ret = ehci_wait_td(td);
+ int ret = ehci_wait_td(pipe, td, 5000);
if (ret)
- goto fail;
+ return -1;
struct ehci_qtd *nexttd_fl = MAKE_FLATPTR(GET_SEG(SS)
, &tds[tdpos % STACKQTDS]);
int i;
for (i=0; i<STACKQTDS; i++) {
struct ehci_qtd *td = &tds[tdpos++ % STACKQTDS];
- int ret = ehci_wait_td(td);
+ int ret = ehci_wait_td(pipe, td, 5000);
if (ret)
- goto fail;
+ return -1;
}
return 0;
-fail:
- dprintf(1, "ehci_send_bulk failed\n");
- SET_FLATPTR(pipe->qh.qtd_next, EHCI_PTR_TERM);
- SET_FLATPTR(pipe->qh.alt_next, EHCI_PTR_TERM);
- // XXX - halt qh?
- struct usb_ehci_s *cntl = container_of(
- GET_FLATPTR(pipe->pipe.cntl), struct usb_ehci_s, usb);
- ehci_waittick(cntl);
- return -1;
}
struct usb_pipe *