1 // Code for handling OHCI USB controllers.
3 // Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
7 #include "util.h" // dprintf
8 #include "pci.h" // pci_bdf_to_bus
9 #include "config.h" // CONFIG_*
10 #include "usb-ohci.h" // struct ohci_hcca
11 #include "pci_regs.h" // PCI_BASE_ADDRESS_0
12 #include "usb.h" // struct usb_s
13 #include "farptr.h" // GET_FLATPTR
19 struct ohci_regs *regs;
23 /****************************************************************
25 ****************************************************************/
27 // Check if device attached to port
29 ohci_hub_detect(struct usbhub_s *hub, u32 port)
31 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
32 u32 sts = readl(&cntl->regs->roothub_portstatus[port]);
33 if (!(sts & RH_PS_CCS))
37 // XXX - need to wait for USB_TIME_ATTDB if just powered up?
44 ohci_hub_disconnect(struct usbhub_s *hub, u32 port)
46 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
47 writel(&cntl->regs->roothub_portstatus[port], RH_PS_CCS|RH_PS_LSDA);
50 // Reset device on port
52 ohci_hub_reset(struct usbhub_s *hub, u32 port)
54 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
55 writel(&cntl->regs->roothub_portstatus[port], RH_PS_PRS);
57 u64 end = calc_future_tsc(USB_TIME_DRSTR * 2);
59 sts = readl(&cntl->regs->roothub_portstatus[port]);
60 if (!(sts & RH_PS_PRS))
61 // XXX - need to ensure USB_TIME_DRSTR time in reset?
66 ohci_hub_disconnect(hub, port);
72 if ((sts & (RH_PS_CCS|RH_PS_PES)) != (RH_PS_CCS|RH_PS_PES))
73 // Device no longer present
76 return !!(sts & RH_PS_LSDA);
79 static struct usbhub_op_s ohci_HubOp = {
80 .detect = ohci_hub_detect,
81 .reset = ohci_hub_reset,
82 .disconnect = ohci_hub_disconnect,
85 // Find any devices connected to the root hub.
87 check_ohci_ports(struct usb_ohci_s *cntl)
90 // Turn on power for all devices on roothub.
91 u32 rha = readl(&cntl->regs->roothub_a);
92 rha &= ~(RH_A_PSM | RH_A_OCPM);
93 writel(&cntl->regs->roothub_status, RH_HS_LPSC);
94 writel(&cntl->regs->roothub_b, RH_B_PPCM);
95 msleep((rha >> 24) * 2);
96 // XXX - need to sleep for USB_TIME_SIGATT if just powered up?
99 memset(&hub, 0, sizeof(hub));
100 hub.cntl = &cntl->usb;
101 hub.portcount = rha & RH_A_NDP;
102 hub.op = &ohci_HubOp;
108 /****************************************************************
110 ****************************************************************/
113 start_ohci(struct usb_ohci_s *cntl, struct ohci_hcca *hcca)
115 u32 oldfminterval = readl(&cntl->regs->fminterval);
116 u32 oldrwc = readl(&cntl->regs->control) & OHCI_CTRL_RWC;
118 // XXX - check if already running?
121 writel(&cntl->regs->control, OHCI_USB_RESET | oldrwc);
122 readl(&cntl->regs->control); // flush writes
123 msleep(USB_TIME_DRSTR);
125 // Do software init (min 10us, max 2ms)
126 u64 end = calc_future_tsc_usec(10);
127 writel(&cntl->regs->cmdstatus, OHCI_HCR);
129 u32 status = readl(&cntl->regs->cmdstatus);
130 if (! status & OHCI_HCR)
132 if (check_tsc(end)) {
139 writel(&cntl->regs->ed_controlhead, 0);
140 writel(&cntl->regs->ed_bulkhead, 0);
141 writel(&cntl->regs->hcca, (u32)hcca);
144 u32 fi = oldfminterval & 0x3fff;
145 writel(&cntl->regs->fminterval
146 , (((oldfminterval & FIT) ^ FIT)
147 | fi | (((6 * (fi - 210)) / 7) << 16)));
148 writel(&cntl->regs->periodicstart, ((9 * fi) / 10) & 0x3fff);
149 readl(&cntl->regs->control); // flush writes
151 // XXX - verify that fminterval was setup correctly.
153 // Go into operational state
154 writel(&cntl->regs->control
155 , (OHCI_CTRL_CBSR | OHCI_CTRL_CLE | OHCI_CTRL_PLE
156 | OHCI_USB_OPER | oldrwc));
157 readl(&cntl->regs->control); // flush writes
163 stop_ohci(struct usb_ohci_s *cntl)
165 u32 oldrwc = readl(&cntl->regs->control) & OHCI_CTRL_RWC;
166 writel(&cntl->regs->control, oldrwc);
167 readl(&cntl->regs->control); // flush writes
171 configure_ohci(void *data)
173 struct usb_ohci_s *cntl = data;
176 struct ohci_hcca *hcca = memalign_high(256, sizeof(*hcca));
177 struct ohci_ed *intr_ed = malloc_high(sizeof(*intr_ed));
178 if (!hcca || !intr_ed) {
182 memset(hcca, 0, sizeof(*hcca));
183 memset(intr_ed, 0, sizeof(*intr_ed));
184 intr_ed->hwINFO = ED_SKIP;
186 for (i=0; i<ARRAY_SIZE(hcca->int_table); i++)
187 hcca->int_table[i] = (u32)intr_ed;
189 int ret = start_ohci(cntl, hcca);
193 int count = check_ohci_ports(cntl);
194 free_pipe(cntl->usb.defaultpipe);
207 ohci_init(u16 bdf, int busid)
209 if (! CONFIG_USB_OHCI)
211 struct usb_ohci_s *cntl = malloc_tmphigh(sizeof(*cntl));
212 memset(cntl, 0, sizeof(*cntl));
213 cntl->usb.busid = busid;
214 cntl->usb.type = USB_TYPE_OHCI;
216 u32 baseaddr = pci_config_readl(bdf, PCI_BASE_ADDRESS_0);
217 cntl->regs = (void*)(baseaddr & PCI_BASE_ADDRESS_MEM_MASK);
219 dprintf(1, "OHCI init on dev %02x:%02x.%x (regs=%p)\n"
220 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
221 , pci_bdf_to_fn(bdf), cntl->regs);
223 // Enable bus mastering and memory access.
224 pci_config_maskw(bdf, PCI_COMMAND
225 , 0, PCI_COMMAND_MASTER|PCI_COMMAND_MEMORY);
227 // XXX - check for and disable SMM control?
229 // Disable interrupts
230 writel(&cntl->regs->intrdisable, ~0);
231 writel(&cntl->regs->intrstatus, ~0);
233 run_thread(configure_ohci, cntl);
237 /****************************************************************
238 * End point communication
239 ****************************************************************/
242 wait_ed(struct ohci_ed *ed)
244 // XXX - 500ms just a guess
245 u64 end = calc_future_tsc(500);
247 if (ed->hwHeadP == ed->hwTailP)
249 if (check_tsc(end)) {
257 // Wait for next USB frame to start - for ensuring safe memory release.
259 ohci_waittick(struct usb_ohci_s *cntl)
262 struct ohci_hcca *hcca = (void*)cntl->regs->hcca;
263 u32 startframe = hcca->frame_no;
264 u64 end = calc_future_tsc(1000 * 5);
266 if (hcca->frame_no != startframe)
268 if (check_tsc(end)) {
277 signal_freelist(struct usb_ohci_s *cntl)
279 u32 v = readl(&cntl->regs->control);
280 if (v & OHCI_CTRL_CLE) {
281 writel(&cntl->regs->control, v & ~(OHCI_CTRL_CLE|OHCI_CTRL_BLE));
283 writel(&cntl->regs->ed_controlcurrent, 0);
284 writel(&cntl->regs->ed_bulkcurrent, 0);
285 writel(&cntl->regs->control, v);
293 struct usb_pipe pipe;
300 ohci_free_pipe(struct usb_pipe *p)
302 if (! CONFIG_USB_OHCI)
304 dprintf(7, "ohci_free_pipe %p\n", p);
305 struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
306 struct usb_ohci_s *cntl = container_of(
307 pipe->pipe.cntl, struct usb_ohci_s, usb);
309 u32 *pos = &cntl->regs->ed_controlhead;
311 struct ohci_ed *next = (void*)*pos;
313 // Not found?! Exit without freeing.
314 warn_internalerror();
317 if (next == &pipe->ed) {
318 *pos = next->hwNextED;
319 signal_freelist(cntl);
323 pos = &next->hwNextED;
328 ohci_alloc_control_pipe(struct usb_pipe *dummy)
330 if (! CONFIG_USB_OHCI)
332 struct usb_ohci_s *cntl = container_of(
333 dummy->cntl, struct usb_ohci_s, usb);
334 dprintf(7, "ohci_alloc_control_pipe %p\n", &cntl->usb);
336 // Allocate a queue head.
337 struct ohci_pipe *pipe = malloc_tmphigh(sizeof(*pipe));
342 memset(pipe, 0, sizeof(*pipe));
343 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
344 pipe->ed.hwINFO = ED_SKIP;
346 // Add queue head to controller list.
347 pipe->ed.hwNextED = cntl->regs->ed_controlhead;
349 cntl->regs->ed_controlhead = (u32)&pipe->ed;
354 ohci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
355 , void *data, int datasize)
357 if (! CONFIG_USB_OHCI)
359 dprintf(5, "ohci_control %p\n", p);
360 if (datasize > 4096) {
361 // XXX - should support larger sizes.
365 struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
366 struct usb_ohci_s *cntl = container_of(
367 pipe->pipe.cntl, struct usb_ohci_s, usb);
368 int maxpacket = pipe->pipe.maxpacket;
369 int lowspeed = pipe->pipe.speed;
370 int devaddr = pipe->pipe.devaddr | (pipe->pipe.ep << 7);
372 // Setup transfer descriptors
373 struct ohci_td *tds = malloc_tmphigh(sizeof(*tds) * 3);
378 struct ohci_td *td = tds;
379 td->hwINFO = TD_DP_SETUP | TD_T_DATA0 | TD_CC;
380 td->hwCBP = (u32)cmd;
381 td->hwNextTD = (u32)&td[1];
382 td->hwBE = (u32)cmd + cmdsize - 1;
385 td->hwINFO = (dir ? TD_DP_IN : TD_DP_OUT) | TD_T_DATA1 | TD_CC;
386 td->hwCBP = (u32)data;
387 td->hwNextTD = (u32)&td[1];
388 td->hwBE = (u32)data + datasize - 1;
391 td->hwINFO = (dir ? TD_DP_OUT : TD_DP_IN) | TD_T_DATA1 | TD_CC;
393 td->hwNextTD = (u32)&td[1];
398 pipe->ed.hwINFO = ED_SKIP;
400 pipe->ed.hwHeadP = (u32)tds;
401 pipe->ed.hwTailP = (u32)td;
403 pipe->ed.hwINFO = devaddr | (maxpacket << 16) | (lowspeed ? ED_LOWSPEED : 0);
404 writel(&cntl->regs->cmdstatus, OHCI_CLF);
406 int ret = wait_ed(&pipe->ed);
407 pipe->ed.hwINFO = ED_SKIP;
415 ohci_alloc_intr_pipe(struct usb_pipe *dummy, int frameexp)
417 if (! CONFIG_USB_OHCI)
419 struct usb_ohci_s *cntl = container_of(
420 dummy->cntl, struct usb_ohci_s, usb);
421 dprintf(7, "ohci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
425 int maxpacket = dummy->maxpacket;
426 int lowspeed = dummy->speed;
427 int devaddr = dummy->devaddr | (dummy->ep << 7);
428 // Determine number of entries needed for 2 timer ticks.
429 int ms = 1<<frameexp;
430 int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms)+1;
431 struct ohci_pipe *pipe = malloc_low(sizeof(*pipe));
432 struct ohci_td *tds = malloc_low(sizeof(*tds) * count);
433 void *data = malloc_low(maxpacket * count);
434 if (!pipe || !tds || !data)
436 memset(pipe, 0, sizeof(*pipe));
437 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
442 struct ohci_ed *ed = &pipe->ed;
443 ed->hwHeadP = (u32)&tds[0];
444 ed->hwTailP = (u32)&tds[count-1];
445 ed->hwINFO = devaddr | (maxpacket << 16) | (lowspeed ? ED_LOWSPEED : 0);
448 for (i=0; i<count-1; i++) {
449 tds[i].hwINFO = TD_DP_IN | TD_T_TOGGLE | TD_CC;
450 tds[i].hwCBP = (u32)data + maxpacket * i;
451 tds[i].hwNextTD = (u32)&tds[i+1];
452 tds[i].hwBE = tds[i].hwCBP + maxpacket - 1;
455 // Add to interrupt schedule.
457 struct ohci_hcca *hcca = (void*)cntl->regs->hcca;
459 // Add to existing interrupt entry.
460 struct ohci_ed *intr_ed = (void*)hcca->int_table[0];
461 ed->hwNextED = intr_ed->hwNextED;
462 intr_ed->hwNextED = (u32)ed;
464 int startpos = 1<<(frameexp-1);
465 ed->hwNextED = hcca->int_table[startpos];
466 for (i=startpos; i<ARRAY_SIZE(hcca->int_table); i+=ms)
467 hcca->int_table[i] = (u32)ed;
480 ohci_poll_intr(struct usb_pipe *p, void *data)
483 if (! CONFIG_USB_OHCI)
486 struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
487 struct ohci_td *tds = GET_FLATPTR(pipe->tds);
488 struct ohci_td *head = (void*)GET_FLATPTR(pipe->ed.hwHeadP);
489 struct ohci_td *tail = (void*)GET_FLATPTR(pipe->ed.hwTailP);
490 int count = GET_FLATPTR(pipe->count);
491 int pos = (tail - tds + 1) % count;
492 struct ohci_td *next = &tds[pos];
496 // XXX - check for errors.
499 int maxpacket = GET_FLATPTR(pipe->pipe.maxpacket);
500 void *pipedata = GET_FLATPTR(pipe->data);
501 void *intrdata = pipedata + maxpacket * pos;
502 memcpy_far(GET_SEG(SS), data
503 , FLATPTR_TO_SEG(intrdata), (void*)FLATPTR_TO_OFFSET(intrdata)
507 SET_FLATPTR(tail->hwINFO, TD_DP_IN | TD_T_TOGGLE | TD_CC);
508 intrdata = pipedata + maxpacket * (tail-tds);
509 SET_FLATPTR(tail->hwCBP, (u32)intrdata);
510 SET_FLATPTR(tail->hwNextTD, (u32)next);
511 SET_FLATPTR(tail->hwBE, (u32)intrdata + maxpacket - 1);
513 SET_FLATPTR(pipe->ed.hwTailP, (u32)next);