#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
#define FIT (1 << 31)
+
+/****************************************************************
+ * Root hub
+ ****************************************************************/
+
+static void
+init_ohci_port(void *data)
+{
+ struct usbhub_s *hub = data;
+ u32 port = hub->port; // XXX - find better way to pass port
+
+ u32 sts = readl(&hub->cntl->ohci.regs->roothub_portstatus[port]);
+ if (!(sts & RH_PS_CCS))
+ // No device.
+ goto done;
+
+ // XXX - need to wait for USB_TIME_ATTDB if just powered up?
+
+ // Signal reset
+ mutex_lock(&hub->cntl->resetlock);
+ writel(&hub->cntl->ohci.regs->roothub_portstatus[port], RH_PS_PRS);
+ u64 end = calc_future_tsc(USB_TIME_DRSTR * 2);
+ for (;;) {
+ sts = readl(&hub->cntl->ohci.regs->roothub_portstatus[port]);
+ if (!(sts & RH_PS_PRS))
+ // XXX - need to ensure USB_TIME_DRSTR time in reset?
+ break;
+ if (check_time(end)) {
+ // Timeout.
+ warn_timeout();
+ goto resetfail;
+ }
+ yield();
+ }
+
+ if ((sts & (RH_PS_CCS|RH_PS_PES)) != (RH_PS_CCS|RH_PS_PES))
+ // Device no longer present
+ goto resetfail;
+
+ // Set address of port
+ struct usb_pipe *pipe = usb_set_address(hub->cntl, !!(sts & RH_PS_LSDA));
+ if (!pipe)
+ goto resetfail;
+ mutex_unlock(&hub->cntl->resetlock);
+
+ // Configure the device
+ int count = configure_usb_device(pipe);
+ free_pipe(pipe);
+ if (! count)
+ // Shutdown port
+ writel(&hub->cntl->ohci.regs->roothub_portstatus[port]
+ , RH_PS_CCS|RH_PS_LSDA);
+ hub->devcount += count;
+done:
+ hub->threads--;
+ return;
+
+resetfail:
+ // Shutdown port
+ writel(&hub->cntl->ohci.regs->roothub_portstatus[port]
+ , RH_PS_CCS|RH_PS_LSDA);
+ mutex_unlock(&hub->cntl->resetlock);
+ goto done;
+}
+
+// Find any devices connected to the root hub.
+static int
+check_ohci_ports(struct usb_s *cntl)
+{
+ ASSERT32FLAT();
+ // Turn on power for all devices on roothub.
+ u32 rha = readl(&cntl->ohci.regs->roothub_a);
+ rha &= ~(RH_A_PSM | RH_A_OCPM);
+ writel(&cntl->ohci.regs->roothub_status, RH_HS_LPSC);
+ writel(&cntl->ohci.regs->roothub_b, RH_B_PPCM);
+ msleep((rha >> 24) * 2);
+ // XXX - need to sleep for USB_TIME_SIGATT if just powered up?
+
+ // Lanuch a thread per port.
+ struct usbhub_s hub;
+ memset(&hub, 0, sizeof(hub));
+ hub.cntl = cntl;
+ int ports = rha & RH_A_NDP;
+ hub.threads = ports;
+ int i;
+ for (i=0; i<ports; i++) {
+ hub.port = i;
+ run_thread(init_ohci_port, &hub);
+ }
+
+ // Wait for threads to complete.
+ while (hub.threads)
+ yield();
+
+ return hub.devcount;
+}
+
+
+/****************************************************************
+ * Setup
+ ****************************************************************/
+
static int
start_ohci(struct usb_s *cntl, struct ohci_hcca *hcca)
{
// Do reset
writel(&cntl->ohci.regs->control, OHCI_USB_RESET | oldrwc);
readl(&cntl->ohci.regs->control); // flush writes
- mdelay(50);
+ msleep(USB_TIME_DRSTR);
// Do software init (min 10us, max 2ms)
u64 end = calc_future_tsc_usec(10);
if (! status & OHCI_HCR)
break;
if (check_time(end)) {
- dprintf(1, "Timeout on ohci software reset\n");
+ warn_timeout();
return -1;
}
}
// Init memory
- writel(&cntl->ohci.regs->ed_controlhead, (u32)cntl->ohci.control_ed);
+ writel(&cntl->ohci.regs->ed_controlhead, 0);
writel(&cntl->ohci.regs->ed_bulkhead, 0);
writel(&cntl->ohci.regs->hcca, (u32)hcca);
readl(&cntl->ohci.regs->control); // flush writes
}
-// Find any devices connected to the root hub.
-static int
-check_ohci_ports(struct usb_s *cntl)
-{
- // Turn on power for all devices on roothub.
- u32 rha = readl(&cntl->ohci.regs->roothub_a);
- rha &= ~(RH_A_PSM | RH_A_OCPM);
- writel(&cntl->ohci.regs->roothub_status, RH_HS_LPSC);
- writel(&cntl->ohci.regs->roothub_b, RH_B_PPCM);
- mdelay((rha >> 24) * 2);
-
- // Count and reset connected devices
- int ports = rha & RH_A_NDP;
- int totalcount = 0;
- int i;
- for (i=0; i<ports; i++)
- if (readl(&cntl->ohci.regs->roothub_portstatus[i]) & RH_PS_CCS) {
- writel(&cntl->ohci.regs->roothub_portstatus[i], RH_PS_PRS);
- totalcount++;
- }
- if (!totalcount)
- // No devices connected
- goto shutdown;
-
- mdelay(60); // XXX - should poll instead of using timer.
-
- totalcount = 0;
- for (i=0; i<ports; i++) {
- u32 sts = readl(&cntl->ohci.regs->roothub_portstatus[i]);
- if ((sts & (RH_PS_CCS|RH_PS_PES)) == (RH_PS_CCS|RH_PS_PES)) {
- int count = configure_usb_device(cntl, !!(sts & RH_PS_LSDA));
- if (! count)
- // Shutdown port
- writel(&cntl->ohci.regs->roothub_portstatus[i]
- , RH_PS_CCS|RH_PS_LSDA);
- totalcount += count;
- }
- }
- if (!totalcount)
- goto shutdown;
-
- return totalcount;
-
-shutdown:
- // Turn off power to all ports
- writel(&cntl->ohci.regs->roothub_status, RH_HS_LPS);
- return 0;
-}
-
-int
-ohci_init(struct usb_s *cntl)
+void
+ohci_init(void *data)
{
if (! CONFIG_USB_OHCI)
- return 0;
+ return;
+ struct usb_s *cntl = data;
+ // XXX - don't call pci_config_XXX from a thread
cntl->type = USB_TYPE_OHCI;
u32 baseaddr = pci_config_readl(cntl->bdf, PCI_BASE_ADDRESS_0);
cntl->ohci.regs = (void*)(baseaddr & PCI_BASE_ADDRESS_MEM_MASK);
// Allocate memory
struct ohci_hcca *hcca = memalign_high(256, sizeof(*hcca));
- struct ohci_ed *control_ed = malloc_high(sizeof(*control_ed));
- if (!hcca || !control_ed) {
- dprintf(1, "No ram for ohci init\n");
- return 0;
+ struct ohci_ed *intr_ed = malloc_high(sizeof(*intr_ed));
+ if (!hcca || !intr_ed) {
+ warn_noalloc();
+ goto free;
}
memset(hcca, 0, sizeof(*hcca));
- memset(control_ed, 0, sizeof(*control_ed));
- control_ed->hwINFO = ED_SKIP;
- cntl->ohci.control_ed = control_ed;
+ memset(intr_ed, 0, sizeof(*intr_ed));
+ intr_ed->hwINFO = ED_SKIP;
+ int i;
+ for (i=0; i<ARRAY_SIZE(hcca->int_table); i++)
+ hcca->int_table[i] = (u32)intr_ed;
int ret = start_ohci(cntl, hcca);
if (ret)
goto err;
int count = check_ohci_ports(cntl);
+ free_pipe(cntl->defaultpipe);
if (! count)
goto err;
- return count;
+ return;
err:
stop_ohci(cntl);
+free:
free(hcca);
- free(control_ed);
- return 0;
+ free(intr_ed);
}
+
+/****************************************************************
+ * End point communication
+ ****************************************************************/
+
static int
wait_ed(struct ohci_ed *ed)
{
if (ed->hwHeadP == ed->hwTailP)
return 0;
if (check_time(end)) {
- dprintf(1, "Timeout on wait_ed %p\n", ed);
+ warn_timeout();
return -1;
}
- cpu_relax();
+ yield();
+ }
+}
+
+// Wait for next USB frame to start - for ensuring safe memory release.
+static void
+ohci_waittick(struct usb_s *cntl)
+{
+ barrier();
+ struct ohci_hcca *hcca = (void*)cntl->ohci.regs->hcca;
+ u32 startframe = hcca->frame_no;
+ u64 end = calc_future_tsc(1000 * 5);
+ for (;;) {
+ if (hcca->frame_no != startframe)
+ break;
+ if (check_time(end)) {
+ warn_timeout();
+ return;
+ }
+ yield();
+ }
+}
+
+static void
+signal_freelist(struct usb_s *cntl)
+{
+ u32 v = readl(&cntl->ohci.regs->control);
+ if (v & OHCI_CTRL_CLE) {
+ writel(&cntl->ohci.regs->control, v & ~(OHCI_CTRL_CLE|OHCI_CTRL_BLE));
+ ohci_waittick(cntl);
+ writel(&cntl->ohci.regs->ed_controlcurrent, 0);
+ writel(&cntl->ohci.regs->ed_bulkcurrent, 0);
+ writel(&cntl->ohci.regs->control, v);
+ } else {
+ ohci_waittick(cntl);
}
}
+struct ohci_pipe {
+ struct ohci_ed ed;
+ struct usb_pipe pipe;
+ void *data;
+ int count;
+ struct ohci_td *tds;
+};
+
+void
+ohci_free_pipe(struct usb_pipe *p)
+{
+ if (! CONFIG_USB_OHCI)
+ return;
+ struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
+ u32 endp = pipe->pipe.endp;
+ dprintf(7, "ohci_free_pipe %x\n", endp);
+ struct usb_s *cntl = endp2cntl(endp);
+
+ u32 *pos = &cntl->ohci.regs->ed_controlhead;
+ for (;;) {
+ struct ohci_ed *next = (void*)*pos;
+ if (!next) {
+ // Not found?! Exit without freeing.
+ warn_internalerror();
+ return;
+ }
+ if (next == &pipe->ed) {
+ *pos = next->hwNextED;
+ signal_freelist(cntl);
+ free(pipe);
+ return;
+ }
+ pos = &next->hwNextED;
+ }
+}
+
+struct usb_pipe *
+ohci_alloc_control_pipe(u32 endp)
+{
+ if (! CONFIG_USB_OHCI)
+ return NULL;
+ struct usb_s *cntl = endp2cntl(endp);
+ dprintf(7, "ohci_alloc_control_pipe %x\n", endp);
+
+ // Allocate a queue head.
+ struct ohci_pipe *pipe = malloc_tmphigh(sizeof(*pipe));
+ if (!pipe) {
+ warn_noalloc();
+ return NULL;
+ }
+ memset(pipe, 0, sizeof(*pipe));
+ pipe->ed.hwINFO = ED_SKIP;
+ pipe->pipe.endp = endp;
+
+ // Add queue head to controller list.
+ pipe->ed.hwNextED = cntl->ohci.regs->ed_controlhead;
+ barrier();
+ cntl->ohci.regs->ed_controlhead = (u32)&pipe->ed;
+ return &pipe->pipe;
+}
+
int
-ohci_control(u32 endp, int dir, const void *cmd, int cmdsize
+ohci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
, void *data, int datasize)
{
if (! CONFIG_USB_OHCI)
return -1;
-
+ if (datasize > 4096) {
+ // XXX - should support larger sizes.
+ warn_noalloc();
+ return -1;
+ }
+ struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
+ u32 endp = pipe->pipe.endp;
dprintf(5, "ohci_control %x\n", endp);
struct usb_s *cntl = endp2cntl(endp);
int maxpacket = endp2maxsize(endp);
tds[2].hwBE = 0;
// Transfer data
- struct ohci_ed *ed = cntl->ohci.control_ed;
- ed->hwINFO = ED_SKIP;
+ pipe->ed.hwINFO = ED_SKIP;
barrier();
- ed->hwHeadP = (u32)&tds[0];
- ed->hwTailP = (u32)&tds[3];
+ pipe->ed.hwHeadP = (u32)&tds[0];
+ pipe->ed.hwTailP = (u32)&tds[3];
barrier();
- ed->hwINFO = devaddr | (maxpacket << 16) | (lowspeed ? ED_LOWSPEED : 0);
+ pipe->ed.hwINFO = devaddr | (maxpacket << 16) | (lowspeed ? ED_LOWSPEED : 0);
writel(&cntl->ohci.regs->cmdstatus, OHCI_CLF);
- int ret = wait_ed(ed);
- ed->hwINFO = ED_SKIP;
- udelay(1); // XXX - in case controller still accessing tds
+ int ret = wait_ed(&pipe->ed);
+ pipe->ed.hwINFO = ED_SKIP;
+ if (ret)
+ ohci_waittick(cntl);
free(tds);
return ret;
}
-struct ohci_pipe {
- struct ohci_ed ed;
- struct usb_pipe pipe;
- void *data;
- int count;
- struct ohci_td *tds;
-};
-
struct usb_pipe *
-ohci_alloc_intr_pipe(u32 endp, int period)
+ohci_alloc_intr_pipe(u32 endp, int frameexp)
{
if (! CONFIG_USB_OHCI)
return NULL;
- dprintf(7, "ohci_alloc_intr_pipe %x %d\n", endp, period);
+ dprintf(7, "ohci_alloc_intr_pipe %x %d\n", endp, frameexp);
+ if (frameexp > 5)
+ frameexp = 5;
struct usb_s *cntl = endp2cntl(endp);
int maxpacket = endp2maxsize(endp);
int lowspeed = endp2speed(endp);
int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
- // XXX - just grab 20 for now.
- int count = 20;
+ // Determine number of entries needed for 2 timer ticks.
+ int ms = 1<<frameexp;
+ int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms);
struct ohci_pipe *pipe = malloc_low(sizeof(*pipe));
struct ohci_td *tds = malloc_low(sizeof(*tds) * count);
void *data = malloc_low(maxpacket * count);
ed->hwHeadP = (u32)&tds[0];
ed->hwTailP = (u32)&tds[count-1];
ed->hwINFO = devaddr | (maxpacket << 16) | (lowspeed ? ED_LOWSPEED : 0);
- ed->hwNextED = 0;
int i;
for (i=0; i<count-1; i++) {
tds[i].hwBE = tds[i].hwCBP + maxpacket - 1;
}
- // XXX - need schedule - just add to primary list for now.
+ // Add to interrupt schedule.
barrier();
struct ohci_hcca *hcca = (void*)cntl->ohci.regs->hcca;
- for (i=0; i<ARRAY_SIZE(hcca->int_table); i++)
- hcca->int_table[i] = (u32)ed;
+ if (frameexp == 0) {
+ // Add to existing interrupt entry.
+ struct ohci_ed *intr_ed = (void*)hcca->int_table[0];
+ ed->hwNextED = intr_ed->hwNextED;
+ intr_ed->hwNextED = (u32)ed;
+ } else {
+ int startpos = 1<<(frameexp-1);
+ ed->hwNextED = hcca->int_table[startpos];
+ for (i=startpos; i<ARRAY_SIZE(hcca->int_table); i+=ms)
+ hcca->int_table[i] = (u32)ed;
+ }
pipe->data = data;
pipe->count = count;
}
int
-ohci_poll_intr(struct usb_pipe *pipe, void *data)
+ohci_poll_intr(struct usb_pipe *p, void *data)
{
ASSERT16();
if (! CONFIG_USB_OHCI)
return -1;
- struct ohci_pipe *p = container_of(pipe, struct ohci_pipe, pipe);
- struct ohci_td *tds = GET_FLATPTR(p->tds);
- struct ohci_td *head = (void*)GET_FLATPTR(p->ed.hwHeadP);
- struct ohci_td *tail = (void*)GET_FLATPTR(p->ed.hwTailP);
- int count = GET_FLATPTR(p->count);
+ struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
+ struct ohci_td *tds = GET_FLATPTR(pipe->tds);
+ struct ohci_td *head = (void*)GET_FLATPTR(pipe->ed.hwHeadP);
+ struct ohci_td *tail = (void*)GET_FLATPTR(pipe->ed.hwTailP);
+ int count = GET_FLATPTR(pipe->count);
int pos = (tail - tds + 1) % count;
struct ohci_td *next = &tds[pos];
if (head == next)
// XXX - check for errors.
// Copy data.
- u32 endp = GET_FLATPTR(p->pipe.endp);
+ u32 endp = GET_FLATPTR(pipe->pipe.endp);
int maxpacket = endp2maxsize(endp);
- void *pipedata = GET_FLATPTR(p->data);
+ void *pipedata = GET_FLATPTR(pipe->data);
void *intrdata = pipedata + maxpacket * pos;
memcpy_far(GET_SEG(SS), data
, FLATPTR_TO_SEG(intrdata), (void*)FLATPTR_TO_OFFSET(intrdata)
SET_FLATPTR(tail->hwNextTD, (u32)next);
SET_FLATPTR(tail->hwBE, (u32)intrdata + maxpacket - 1);
- SET_FLATPTR(p->ed.hwTailP, (u32)next);
+ SET_FLATPTR(pipe->ed.hwTailP, (u32)next);
return 0;
}