1 // Code for handling UHCI 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 "ioport.h" // outw
11 #include "usb-uhci.h" // USBLEGSUP
12 #include "pci_regs.h" // PCI_BASE_ADDRESS_4
13 #include "usb.h" // struct usb_s
14 #include "farptr.h" // GET_FLATPTR
15 #include "biosvar.h" // GET_GLOBAL
18 /****************************************************************
20 ****************************************************************/
23 reset_uhci(struct usb_s *cntl)
25 // XXX - don't reset if not needed.
28 pci_config_writew(cntl->bdf, USBLEGSUP, USBLEGSUP_RWC);
31 outw(USBCMD_HCRESET, cntl->uhci.iobase + USBCMD);
34 // Disable interrupts and commands (just to be safe).
35 outw(0, cntl->uhci.iobase + USBINTR);
36 outw(0, cntl->uhci.iobase + USBCMD);
40 configure_uhci(struct usb_s *cntl)
42 // Allocate ram for schedule storage
43 struct uhci_td *term_td = malloc_high(sizeof(*term_td));
44 struct uhci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl));
45 struct uhci_qh *intr_qh = malloc_high(sizeof(*intr_qh));
46 struct uhci_qh *term_qh = malloc_high(sizeof(*term_qh));
47 if (!term_td || !fl || !intr_qh || !term_qh) {
56 // Work around for PIIX errata
57 memset(term_td, 0, sizeof(*term_td));
58 term_td->link = UHCI_PTR_TERM;
59 term_td->token = (uhci_explen(0) | (0x7f << TD_TOKEN_DEVADDR_SHIFT)
61 memset(term_qh, 0, sizeof(*term_qh));
62 term_qh->element = (u32)term_td;
63 term_qh->link = UHCI_PTR_TERM;
65 // Set schedule to point to primary intr queue head
66 memset(intr_qh, 0, sizeof(*intr_qh));
67 intr_qh->element = UHCI_PTR_TERM;
68 intr_qh->link = (u32)term_qh | UHCI_PTR_QH;
70 for (i=0; i<ARRAY_SIZE(fl->links); i++)
71 fl->links[i] = (u32)intr_qh | UHCI_PTR_QH;
72 cntl->uhci.framelist = fl;
73 cntl->uhci.control_qh = cntl->uhci.bulk_qh = intr_qh;
76 // Set the frame length to the default: 1 ms exactly
77 outb(USBSOF_DEFAULT, cntl->uhci.iobase + USBSOF);
79 // Store the frame list base address
80 outl((u32)fl->links, cntl->uhci.iobase + USBFLBASEADD);
82 // Set the current frame number
83 outw(0, cntl->uhci.iobase + USBFRNUM);
87 start_uhci(struct usb_s *cntl)
89 // Mark as configured and running with a 64-byte max packet.
90 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, cntl->uhci.iobase + USBCMD);
93 // Find any devices connected to the root hub.
95 check_ports(struct usb_s *cntl)
97 // XXX - if just powered up, need to wait for USB_TIME_SIGATT?
98 u16 port1 = inw(cntl->uhci.iobase + USBPORTSC1);
99 u16 port2 = inw(cntl->uhci.iobase + USBPORTSC2);
101 if (!((port1 & USBPORTSC_CCS) || (port2 & USBPORTSC_CCS)))
105 // XXX - if just powered up, need to wait for USB_TIME_ATTDB?
108 if (port1 & USBPORTSC_CCS)
109 outw(USBPORTSC_PR, cntl->uhci.iobase + USBPORTSC1);
110 if (port2 & USBPORTSC_CCS)
111 outw(USBPORTSC_PR, cntl->uhci.iobase + USBPORTSC2);
112 msleep(USB_TIME_DRSTR);
116 outw(0, cntl->uhci.iobase + USBPORTSC1);
117 udelay(6); // 64 high-speed bit times
118 port1 = inw(cntl->uhci.iobase + USBPORTSC1);
119 if (port1 & USBPORTSC_CCS) {
120 outw(USBPORTSC_PE, cntl->uhci.iobase + USBPORTSC1);
121 msleep(USB_TIME_RSTRCY);
122 int count = configure_usb_device(cntl, !!(port1 & USBPORTSC_LSDA));
124 outw(0, cntl->uhci.iobase + USBPORTSC1);
127 outw(0, cntl->uhci.iobase + USBPORTSC2);
129 port2 = inw(cntl->uhci.iobase + USBPORTSC2);
130 if (port2 & USBPORTSC_CCS) {
131 outw(USBPORTSC_PE, cntl->uhci.iobase + USBPORTSC2);
132 msleep(USB_TIME_RSTRCY);
133 int count = configure_usb_device(cntl, !!(port2 & USBPORTSC_LSDA));
135 outw(0, cntl->uhci.iobase + USBPORTSC2);
142 uhci_init(void *data)
144 if (! CONFIG_USB_UHCI)
146 struct usb_s *cntl = data;
148 // XXX - don't call pci_config_XXX from a thread
149 cntl->type = USB_TYPE_UHCI;
150 cntl->uhci.iobase = (pci_config_readl(cntl->bdf, PCI_BASE_ADDRESS_4)
151 & PCI_BASE_ADDRESS_IO_MASK);
153 dprintf(3, "UHCI init on dev %02x:%02x.%x (io=%x)\n"
154 , pci_bdf_to_bus(cntl->bdf), pci_bdf_to_dev(cntl->bdf)
155 , pci_bdf_to_fn(cntl->bdf), cntl->uhci.iobase);
157 pci_config_maskw(cntl->bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
160 configure_uhci(cntl);
163 int count = check_ports(cntl);
164 free_pipe(cntl->defaultpipe);
166 // XXX - no devices; free data structures.
171 /****************************************************************
172 * End point communication
173 ****************************************************************/
176 wait_qh(struct usb_s *cntl, struct uhci_qh *qh)
178 // XXX - 500ms just a guess
179 u64 end = calc_future_tsc(500);
181 if (qh->element & UHCI_PTR_TERM)
183 if (check_time(end)) {
185 struct uhci_td *td = (void*)(qh->element & ~UHCI_PTR_BITS);
186 dprintf(1, "Timeout on wait_qh %p (td=%p s=%x c=%x/%x)\n"
188 , inw(cntl->uhci.iobase + USBCMD)
189 , inw(cntl->uhci.iobase + USBSTS));
196 // Wait for next USB frame to start - for ensuring safe memory release.
198 uhci_waittick(struct usb_s *cntl)
201 u16 iobase = GET_GLOBAL(cntl->uhci.iobase);
202 u16 startframe = inw(iobase + USBFRNUM);
203 u64 end = calc_future_tsc(1000 * 5);
205 if (inw(iobase + USBFRNUM) != startframe)
207 if (check_time(end)) {
217 struct uhci_td *next_td;
218 struct usb_pipe pipe;
222 uhci_free_pipe(struct usb_pipe *p)
224 if (! CONFIG_USB_UHCI)
226 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
227 u32 endp = pipe->pipe.endp;
228 dprintf(7, "uhci_free_pipe %x\n", endp);
229 struct usb_s *cntl = endp2cntl(endp);
231 struct uhci_framelist *fl = cntl->uhci.framelist;
232 struct uhci_qh *pos = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
234 u32 link = pos->link;
235 if (link == UHCI_PTR_TERM) {
236 // Not found?! Exit without freeing.
237 warn_internalerror();
240 struct uhci_qh *next = (void*)(link & ~UHCI_PTR_BITS);
241 if (next == &pipe->qh) {
242 pos->link = next->link;
243 if (cntl->uhci.control_qh == next)
244 cntl->uhci.control_qh = pos;
245 if (cntl->uhci.bulk_qh == next)
246 cntl->uhci.bulk_qh = pos;
256 uhci_alloc_control_pipe(u32 endp)
258 if (! CONFIG_USB_UHCI)
260 struct usb_s *cntl = endp2cntl(endp);
261 dprintf(7, "uhci_alloc_control_pipe %x\n", endp);
263 // Allocate a queue head.
264 struct uhci_pipe *pipe = malloc_tmphigh(sizeof(*pipe));
269 pipe->qh.element = UHCI_PTR_TERM;
271 pipe->pipe.endp = endp;
273 // Add queue head to controller list.
274 struct uhci_qh *control_qh = cntl->uhci.control_qh;
275 pipe->qh.link = control_qh->link;
277 control_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
278 if (cntl->uhci.bulk_qh == control_qh)
279 cntl->uhci.bulk_qh = &pipe->qh;
284 uhci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
285 , void *data, int datasize)
288 if (! CONFIG_USB_UHCI)
290 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
291 u32 endp = pipe->pipe.endp;
293 dprintf(5, "uhci_control %x\n", endp);
294 struct usb_s *cntl = endp2cntl(endp);
295 int maxpacket = endp2maxsize(endp);
296 int lowspeed = endp2speed(endp);
297 int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
299 // Setup transfer descriptors
300 int count = 2 + DIV_ROUND_UP(datasize, maxpacket);
301 struct uhci_td *tds = malloc_tmphigh(sizeof(*tds) * count);
303 tds[0].link = (u32)&tds[1] | UHCI_PTR_DEPTH;
304 tds[0].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
306 tds[0].token = (uhci_explen(cmdsize) | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
308 tds[0].buffer = (void*)cmd;
309 int toggle = TD_TOKEN_TOGGLE;
311 for (i=1; i<count-1; i++) {
312 tds[i].link = (u32)&tds[i+1] | UHCI_PTR_DEPTH;
313 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
315 int len = (i == count-2 ? (datasize - (i-1)*maxpacket) : maxpacket);
316 tds[i].token = (uhci_explen(len) | toggle
317 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
318 | (dir ? USB_PID_IN : USB_PID_OUT));
319 tds[i].buffer = data + (i-1) * maxpacket;
320 toggle ^= TD_TOKEN_TOGGLE;
322 tds[i].link = UHCI_PTR_TERM;
323 tds[i].status = (uhci_maxerr(0) | (lowspeed ? TD_CTRL_LS : 0)
325 tds[i].token = (uhci_explen(0) | TD_TOKEN_TOGGLE
326 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
327 | (dir ? USB_PID_OUT : USB_PID_IN));
332 pipe->qh.element = (u32)&tds[0];
333 int ret = wait_qh(cntl, &pipe->qh);
335 pipe->qh.element = UHCI_PTR_TERM;
343 uhci_alloc_bulk_pipe(u32 endp)
345 if (! CONFIG_USB_UHCI)
347 struct usb_s *cntl = endp2cntl(endp);
348 dprintf(7, "uhci_alloc_bulk_pipe %x\n", endp);
350 // Allocate a queue head.
351 struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
356 pipe->qh.element = UHCI_PTR_TERM;
358 pipe->pipe.endp = endp;
360 // Add queue head to controller list.
361 struct uhci_qh *bulk_qh = cntl->uhci.bulk_qh;
362 pipe->qh.link = bulk_qh->link;
364 bulk_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
370 wait_td(struct uhci_td *td)
372 u64 end = calc_future_tsc(5000); // XXX - lookup real time.
376 if (!(status & TD_CTRL_ACTIVE))
378 if (check_time(end)) {
384 if (status & TD_CTRL_ANY_ERROR) {
385 dprintf(1, "wait_td error - status=%x\n", status);
395 uhci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
397 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
398 u32 endp = GET_FLATPTR(pipe->pipe.endp);
399 dprintf(7, "uhci_send_bulk qh=%p endp=%x dir=%d data=%p size=%d\n"
400 , &pipe->qh, endp, dir, data, datasize);
401 int maxpacket = endp2maxsize(endp);
402 int lowspeed = endp2speed(endp);
403 int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
404 int toggle = (u32)GET_FLATPTR(pipe->next_td); // XXX
406 // Allocate 4 tds on stack (16byte aligned)
407 u8 tdsbuf[sizeof(struct uhci_td) * STACKTDS + TDALIGN - 1];
408 struct uhci_td *tds = (void*)ALIGN((u32)tdsbuf, TDALIGN);
409 memset(tds, 0, sizeof(*tds) * STACKTDS);
412 SET_FLATPTR(pipe->qh.element, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
416 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
417 int ret = wait_td(td);
421 int transfer = datasize;
422 if (transfer > maxpacket)
423 transfer = maxpacket;
424 struct uhci_td *nexttd_fl = MAKE_FLATPTR(GET_SEG(SS)
425 , &tds[tdpos % STACKTDS]);
426 td->link = (transfer==datasize ? UHCI_PTR_TERM : (u32)nexttd_fl);
427 td->token = (uhci_explen(transfer) | toggle
428 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
429 | (dir ? USB_PID_IN : USB_PID_OUT));
432 td->status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
434 toggle ^= TD_TOKEN_TOGGLE;
437 datasize -= transfer;
440 for (i=0; i<STACKTDS; i++) {
441 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
442 int ret = wait_td(td);
447 SET_FLATPTR(pipe->next_td, (void*)toggle); // XXX
450 dprintf(1, "uhci_send_bulk failed\n");
451 SET_FLATPTR(pipe->qh.element, UHCI_PTR_TERM);
452 uhci_waittick(endp2cntl(endp));
457 uhci_alloc_intr_pipe(u32 endp, int frameexp)
459 if (! CONFIG_USB_UHCI)
462 dprintf(7, "uhci_alloc_intr_pipe %x %d\n", endp, frameexp);
465 struct usb_s *cntl = endp2cntl(endp);
466 int maxpacket = endp2maxsize(endp);
467 int lowspeed = endp2speed(endp);
468 int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
469 // Determine number of entries needed for 2 timer ticks.
470 int ms = 1<<frameexp;
471 int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms);
472 struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
473 struct uhci_td *tds = malloc_low(sizeof(*tds) * count);
478 if (maxpacket > sizeof(tds[0].data))
480 pipe->qh.element = (u32)tds;
483 for (i=0; i<count; i++) {
484 tds[i].link = (i==count-1 ? (u32)&tds[0] : (u32)&tds[i+1]);
485 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
487 tds[i].token = (uhci_explen(maxpacket) | toggle
488 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
490 tds[i].buffer = &tds[i].data;
491 toggle ^= TD_TOKEN_TOGGLE;
494 pipe->next_td = &tds[0];
495 pipe->pipe.endp = endp;
497 // Add to interrupt schedule.
498 struct uhci_framelist *fl = cntl->uhci.framelist;
500 // Add to existing interrupt entry.
501 struct uhci_qh *intr_qh = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
502 pipe->qh.link = intr_qh->link;
504 intr_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
505 if (cntl->uhci.control_qh == intr_qh)
506 cntl->uhci.control_qh = &pipe->qh;
507 if (cntl->uhci.bulk_qh == intr_qh)
508 cntl->uhci.bulk_qh = &pipe->qh;
510 int startpos = 1<<(frameexp-1);
511 pipe->qh.link = fl->links[startpos];
513 for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
514 fl->links[i] = (u32)&pipe->qh | UHCI_PTR_QH;
525 uhci_poll_intr(struct usb_pipe *p, void *data)
528 if (! CONFIG_USB_UHCI)
531 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
532 struct uhci_td *td = GET_FLATPTR(pipe->next_td);
533 u32 status = GET_FLATPTR(td->status);
534 u32 token = GET_FLATPTR(td->token);
535 if (status & TD_CTRL_ACTIVE)
538 // XXX - check for errors.
541 memcpy_far(GET_SEG(SS), data
542 , FLATPTR_TO_SEG(td->data), (void*)FLATPTR_TO_OFFSET(td->data)
543 , uhci_expected_length(token));
546 u32 next = GET_FLATPTR(td->link);
548 SET_FLATPTR(td->status, (uhci_maxerr(0) | (status & TD_CTRL_LS)
550 SET_FLATPTR(pipe->next_td, (void*)(next & ~UHCI_PTR_BITS));