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
16 #include "usb-hub.h" // struct usbhub_s
19 /****************************************************************
21 ****************************************************************/
24 init_uhci_port(void *data)
26 struct usbhub_s *hub = data;
27 u32 port = hub->port; // XXX - find better way to pass port
28 u16 ioport = hub->cntl->uhci.iobase + USBPORTSC1 + port * 2;
30 u16 status = inw(ioport);
31 if (!(status & USBPORTSC_CCS))
35 // XXX - if just powered up, need to wait for USB_TIME_ATTDB?
38 outw(USBPORTSC_PR, ioport);
39 msleep(USB_TIME_DRSTR);
40 mutex_lock(&hub->cntl->resetlock);
42 udelay(6); // 64 high-speed bit times
44 if (!(status & USBPORTSC_CCS))
45 // No longer connected
47 outw(USBPORTSC_PE, ioport);
48 struct usb_pipe *pipe = usb_set_address(
49 hub->cntl, !!(status & USBPORTSC_LSDA));
52 mutex_unlock(&hub->cntl->resetlock);
55 int count = configure_usb_device(pipe);
60 hub->devcount += count;
67 mutex_unlock(&hub->cntl->resetlock);
71 // Find any devices connected to the root hub.
73 check_ports(struct usb_s *cntl)
77 memset(&hub, 0, sizeof(hub));
81 // Launch a thread for every port.
82 run_thread(init_uhci_port, &hub);
84 run_thread(init_uhci_port, &hub);
86 // Wait for threads to complete.
94 /****************************************************************
96 ****************************************************************/
99 reset_uhci(struct usb_s *cntl)
101 // XXX - don't reset if not needed.
103 // Reset PIRQ and SMI
104 pci_config_writew(cntl->bdf, USBLEGSUP, USBLEGSUP_RWC);
107 outw(USBCMD_HCRESET, cntl->uhci.iobase + USBCMD);
110 // Disable interrupts and commands (just to be safe).
111 outw(0, cntl->uhci.iobase + USBINTR);
112 outw(0, cntl->uhci.iobase + USBCMD);
116 configure_uhci(struct usb_s *cntl)
118 // Allocate ram for schedule storage
119 struct uhci_td *term_td = malloc_high(sizeof(*term_td));
120 struct uhci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl));
121 struct uhci_qh *intr_qh = malloc_high(sizeof(*intr_qh));
122 struct uhci_qh *term_qh = malloc_high(sizeof(*term_qh));
123 if (!term_td || !fl || !intr_qh || !term_qh) {
132 // Work around for PIIX errata
133 memset(term_td, 0, sizeof(*term_td));
134 term_td->link = UHCI_PTR_TERM;
135 term_td->token = (uhci_explen(0) | (0x7f << TD_TOKEN_DEVADDR_SHIFT)
137 memset(term_qh, 0, sizeof(*term_qh));
138 term_qh->element = (u32)term_td;
139 term_qh->link = UHCI_PTR_TERM;
141 // Set schedule to point to primary intr queue head
142 memset(intr_qh, 0, sizeof(*intr_qh));
143 intr_qh->element = UHCI_PTR_TERM;
144 intr_qh->link = (u32)term_qh | UHCI_PTR_QH;
146 for (i=0; i<ARRAY_SIZE(fl->links); i++)
147 fl->links[i] = (u32)intr_qh | UHCI_PTR_QH;
148 cntl->uhci.framelist = fl;
149 cntl->uhci.control_qh = cntl->uhci.bulk_qh = intr_qh;
152 // Set the frame length to the default: 1 ms exactly
153 outb(USBSOF_DEFAULT, cntl->uhci.iobase + USBSOF);
155 // Store the frame list base address
156 outl((u32)fl->links, cntl->uhci.iobase + USBFLBASEADD);
158 // Set the current frame number
159 outw(0, cntl->uhci.iobase + USBFRNUM);
163 start_uhci(struct usb_s *cntl)
165 // Mark as configured and running with a 64-byte max packet.
166 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, cntl->uhci.iobase + USBCMD);
170 uhci_init(void *data)
172 if (! CONFIG_USB_UHCI)
174 struct usb_s *cntl = data;
176 // XXX - don't call pci_config_XXX from a thread
177 cntl->type = USB_TYPE_UHCI;
178 cntl->uhci.iobase = (pci_config_readl(cntl->bdf, PCI_BASE_ADDRESS_4)
179 & PCI_BASE_ADDRESS_IO_MASK);
181 dprintf(3, "UHCI init on dev %02x:%02x.%x (io=%x)\n"
182 , pci_bdf_to_bus(cntl->bdf), pci_bdf_to_dev(cntl->bdf)
183 , pci_bdf_to_fn(cntl->bdf), cntl->uhci.iobase);
185 pci_config_maskw(cntl->bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
188 configure_uhci(cntl);
191 int count = check_ports(cntl);
192 free_pipe(cntl->defaultpipe);
194 // XXX - no devices; free data structures.
199 /****************************************************************
200 * End point communication
201 ****************************************************************/
204 wait_qh(struct usb_s *cntl, struct uhci_qh *qh)
206 // XXX - 500ms just a guess
207 u64 end = calc_future_tsc(500);
209 if (qh->element & UHCI_PTR_TERM)
211 if (check_time(end)) {
213 struct uhci_td *td = (void*)(qh->element & ~UHCI_PTR_BITS);
214 dprintf(1, "Timeout on wait_qh %p (td=%p s=%x c=%x/%x)\n"
216 , inw(cntl->uhci.iobase + USBCMD)
217 , inw(cntl->uhci.iobase + USBSTS));
224 // Wait for next USB frame to start - for ensuring safe memory release.
226 uhci_waittick(struct usb_s *cntl)
229 u16 iobase = GET_GLOBAL(cntl->uhci.iobase);
230 u16 startframe = inw(iobase + USBFRNUM);
231 u64 end = calc_future_tsc(1000 * 5);
233 if (inw(iobase + USBFRNUM) != startframe)
235 if (check_time(end)) {
245 struct uhci_td *next_td;
246 struct usb_pipe pipe;
250 uhci_free_pipe(struct usb_pipe *p)
252 if (! CONFIG_USB_UHCI)
254 dprintf(7, "uhci_free_pipe %p\n", p);
255 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
256 struct usb_s *cntl = pipe->pipe.cntl;
258 struct uhci_framelist *fl = cntl->uhci.framelist;
259 struct uhci_qh *pos = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
261 u32 link = pos->link;
262 if (link == UHCI_PTR_TERM) {
263 // Not found?! Exit without freeing.
264 warn_internalerror();
267 struct uhci_qh *next = (void*)(link & ~UHCI_PTR_BITS);
268 if (next == &pipe->qh) {
269 pos->link = next->link;
270 if (cntl->uhci.control_qh == next)
271 cntl->uhci.control_qh = pos;
272 if (cntl->uhci.bulk_qh == next)
273 cntl->uhci.bulk_qh = pos;
283 uhci_alloc_control_pipe(struct usb_pipe *dummy)
285 if (! CONFIG_USB_UHCI)
287 struct usb_s *cntl = dummy->cntl;
288 dprintf(7, "uhci_alloc_control_pipe %p\n", cntl);
290 // Allocate a queue head.
291 struct uhci_pipe *pipe = malloc_tmphigh(sizeof(*pipe));
296 memset(pipe, 0, sizeof(*pipe));
297 pipe->qh.element = UHCI_PTR_TERM;
298 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
300 // Add queue head to controller list.
301 struct uhci_qh *control_qh = cntl->uhci.control_qh;
302 pipe->qh.link = control_qh->link;
304 control_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
305 if (cntl->uhci.bulk_qh == control_qh)
306 cntl->uhci.bulk_qh = &pipe->qh;
311 uhci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
312 , void *data, int datasize)
315 if (! CONFIG_USB_UHCI)
317 dprintf(5, "uhci_control %p\n", p);
318 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
320 struct usb_s *cntl = pipe->pipe.cntl;
321 int maxpacket = pipe->pipe.maxpacket;
322 int lowspeed = pipe->pipe.lowspeed;
323 int devaddr = pipe->pipe.devaddr | (pipe->pipe.ep << 7);
325 // Setup transfer descriptors
326 int count = 2 + DIV_ROUND_UP(datasize, maxpacket);
327 struct uhci_td *tds = malloc_tmphigh(sizeof(*tds) * count);
329 tds[0].link = (u32)&tds[1] | UHCI_PTR_DEPTH;
330 tds[0].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
332 tds[0].token = (uhci_explen(cmdsize) | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
334 tds[0].buffer = (void*)cmd;
335 int toggle = TD_TOKEN_TOGGLE;
337 for (i=1; i<count-1; i++) {
338 tds[i].link = (u32)&tds[i+1] | UHCI_PTR_DEPTH;
339 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
341 int len = (i == count-2 ? (datasize - (i-1)*maxpacket) : maxpacket);
342 tds[i].token = (uhci_explen(len) | toggle
343 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
344 | (dir ? USB_PID_IN : USB_PID_OUT));
345 tds[i].buffer = data + (i-1) * maxpacket;
346 toggle ^= TD_TOKEN_TOGGLE;
348 tds[i].link = UHCI_PTR_TERM;
349 tds[i].status = (uhci_maxerr(0) | (lowspeed ? TD_CTRL_LS : 0)
351 tds[i].token = (uhci_explen(0) | TD_TOKEN_TOGGLE
352 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
353 | (dir ? USB_PID_OUT : USB_PID_IN));
358 pipe->qh.element = (u32)&tds[0];
359 int ret = wait_qh(cntl, &pipe->qh);
361 pipe->qh.element = UHCI_PTR_TERM;
369 uhci_alloc_bulk_pipe(struct usb_pipe *dummy)
371 if (! CONFIG_USB_UHCI)
373 struct usb_s *cntl = dummy->cntl;
374 dprintf(7, "uhci_alloc_bulk_pipe %p\n", cntl);
376 // Allocate a queue head.
377 struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
382 memset(pipe, 0, sizeof(*pipe));
383 pipe->qh.element = UHCI_PTR_TERM;
384 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
386 // Add queue head to controller list.
387 struct uhci_qh *bulk_qh = cntl->uhci.bulk_qh;
388 pipe->qh.link = bulk_qh->link;
390 bulk_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
396 wait_td(struct uhci_td *td)
398 u64 end = calc_future_tsc(5000); // XXX - lookup real time.
402 if (!(status & TD_CTRL_ACTIVE))
404 if (check_time(end)) {
410 if (status & TD_CTRL_ANY_ERROR) {
411 dprintf(1, "wait_td error - status=%x\n", status);
421 uhci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
423 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
424 dprintf(7, "uhci_send_bulk qh=%p dir=%d data=%p size=%d\n"
425 , &pipe->qh, dir, data, datasize);
426 int maxpacket = GET_FLATPTR(pipe->pipe.maxpacket);
427 int lowspeed = GET_FLATPTR(pipe->pipe.lowspeed);
428 int devaddr = (GET_FLATPTR(pipe->pipe.devaddr)
429 | (GET_FLATPTR(pipe->pipe.ep) << 7));
430 int toggle = GET_FLATPTR(pipe->pipe.toggle) ? TD_TOKEN_TOGGLE : 0;
432 // Allocate 4 tds on stack (16byte aligned)
433 u8 tdsbuf[sizeof(struct uhci_td) * STACKTDS + TDALIGN - 1];
434 struct uhci_td *tds = (void*)ALIGN((u32)tdsbuf, TDALIGN);
435 memset(tds, 0, sizeof(*tds) * STACKTDS);
438 SET_FLATPTR(pipe->qh.element, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
442 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
443 int ret = wait_td(td);
447 int transfer = datasize;
448 if (transfer > maxpacket)
449 transfer = maxpacket;
450 struct uhci_td *nexttd_fl = MAKE_FLATPTR(GET_SEG(SS)
451 , &tds[tdpos % STACKTDS]);
452 td->link = (transfer==datasize ? UHCI_PTR_TERM : (u32)nexttd_fl);
453 td->token = (uhci_explen(transfer) | toggle
454 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
455 | (dir ? USB_PID_IN : USB_PID_OUT));
458 td->status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
460 toggle ^= TD_TOKEN_TOGGLE;
463 datasize -= transfer;
466 for (i=0; i<STACKTDS; i++) {
467 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
468 int ret = wait_td(td);
473 SET_FLATPTR(pipe->pipe.toggle, !!toggle);
476 dprintf(1, "uhci_send_bulk failed\n");
477 SET_FLATPTR(pipe->qh.element, UHCI_PTR_TERM);
478 uhci_waittick(GET_FLATPTR(pipe->pipe.cntl));
483 uhci_alloc_intr_pipe(struct usb_pipe *dummy, int frameexp)
485 if (! CONFIG_USB_UHCI)
487 struct usb_s *cntl = dummy->cntl;
488 dprintf(7, "uhci_alloc_intr_pipe %p %d\n", cntl, frameexp);
492 int maxpacket = dummy->maxpacket;
493 int lowspeed = dummy->lowspeed;
494 int devaddr = dummy->devaddr | (dummy->ep << 7);
495 // Determine number of entries needed for 2 timer ticks.
496 int ms = 1<<frameexp;
497 int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms);
498 struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
499 struct uhci_td *tds = malloc_low(sizeof(*tds) * count);
504 if (maxpacket > sizeof(tds[0].data))
506 memset(pipe, 0, sizeof(*pipe));
507 pipe->qh.element = (u32)tds;
508 pipe->next_td = &tds[0];
509 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
513 for (i=0; i<count; i++) {
514 tds[i].link = (i==count-1 ? (u32)&tds[0] : (u32)&tds[i+1]);
515 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
517 tds[i].token = (uhci_explen(maxpacket) | toggle
518 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
520 tds[i].buffer = &tds[i].data;
521 toggle ^= TD_TOKEN_TOGGLE;
524 // Add to interrupt schedule.
525 struct uhci_framelist *fl = cntl->uhci.framelist;
527 // Add to existing interrupt entry.
528 struct uhci_qh *intr_qh = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
529 pipe->qh.link = intr_qh->link;
531 intr_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
532 if (cntl->uhci.control_qh == intr_qh)
533 cntl->uhci.control_qh = &pipe->qh;
534 if (cntl->uhci.bulk_qh == intr_qh)
535 cntl->uhci.bulk_qh = &pipe->qh;
537 int startpos = 1<<(frameexp-1);
538 pipe->qh.link = fl->links[startpos];
540 for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
541 fl->links[i] = (u32)&pipe->qh | UHCI_PTR_QH;
552 uhci_poll_intr(struct usb_pipe *p, void *data)
555 if (! CONFIG_USB_UHCI)
558 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
559 struct uhci_td *td = GET_FLATPTR(pipe->next_td);
560 u32 status = GET_FLATPTR(td->status);
561 u32 token = GET_FLATPTR(td->token);
562 if (status & TD_CTRL_ACTIVE)
565 // XXX - check for errors.
568 memcpy_far(GET_SEG(SS), data
569 , FLATPTR_TO_SEG(td->data), (void*)FLATPTR_TO_OFFSET(td->data)
570 , uhci_expected_length(token));
573 u32 next = GET_FLATPTR(td->link);
575 SET_FLATPTR(td->status, (uhci_maxerr(0) | (status & TD_CTRL_LS)
577 SET_FLATPTR(pipe->next_td, (void*)(next & ~UHCI_PTR_BITS));