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 "usb-hub.h" // struct usbhub_s
20 struct uhci_qh *control_qh, *bulk_qh;
21 struct uhci_framelist *framelist;
25 /****************************************************************
27 ****************************************************************/
30 init_uhci_port(void *data)
32 struct usbhub_s *hub = data;
33 u32 port = hub->port; // XXX - find better way to pass port
34 struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
35 u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
37 u16 status = inw(ioport);
38 if (!(status & USBPORTSC_CCS))
42 // XXX - if just powered up, need to wait for USB_TIME_ATTDB?
45 outw(USBPORTSC_PR, ioport);
46 msleep(USB_TIME_DRSTR);
47 mutex_lock(&cntl->usb.resetlock);
49 udelay(6); // 64 high-speed bit times
51 if (!(status & USBPORTSC_CCS))
52 // No longer connected
54 outw(USBPORTSC_PE, ioport);
55 struct usb_pipe *pipe = usb_set_address(
56 &cntl->usb, !!(status & USBPORTSC_LSDA));
59 mutex_unlock(&cntl->usb.resetlock);
62 int count = configure_usb_device(pipe);
67 hub->devcount += count;
74 mutex_unlock(&cntl->usb.resetlock);
78 // Find any devices connected to the root hub.
80 check_uhci_ports(struct usb_uhci_s *cntl)
84 memset(&hub, 0, sizeof(hub));
85 hub.cntl = &cntl->usb;
88 // Launch a thread for every port.
89 run_thread(init_uhci_port, &hub);
91 run_thread(init_uhci_port, &hub);
93 // Wait for threads to complete.
101 /****************************************************************
103 ****************************************************************/
106 reset_uhci(struct usb_uhci_s *cntl, u16 bdf)
108 // XXX - don't reset if not needed.
110 // Reset PIRQ and SMI
111 pci_config_writew(bdf, USBLEGSUP, USBLEGSUP_RWC);
114 outw(USBCMD_HCRESET, cntl->iobase + USBCMD);
117 // Disable interrupts and commands (just to be safe).
118 outw(0, cntl->iobase + USBINTR);
119 outw(0, cntl->iobase + USBCMD);
123 configure_uhci(void *data)
125 struct usb_uhci_s *cntl = data;
127 // Allocate ram for schedule storage
128 struct uhci_td *term_td = malloc_high(sizeof(*term_td));
129 struct uhci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl));
130 struct uhci_qh *intr_qh = malloc_high(sizeof(*intr_qh));
131 struct uhci_qh *term_qh = malloc_high(sizeof(*term_qh));
132 if (!term_td || !fl || !intr_qh || !term_qh) {
137 // Work around for PIIX errata
138 memset(term_td, 0, sizeof(*term_td));
139 term_td->link = UHCI_PTR_TERM;
140 term_td->token = (uhci_explen(0) | (0x7f << TD_TOKEN_DEVADDR_SHIFT)
142 memset(term_qh, 0, sizeof(*term_qh));
143 term_qh->element = (u32)term_td;
144 term_qh->link = UHCI_PTR_TERM;
146 // Set schedule to point to primary intr queue head
147 memset(intr_qh, 0, sizeof(*intr_qh));
148 intr_qh->element = UHCI_PTR_TERM;
149 intr_qh->link = (u32)term_qh | UHCI_PTR_QH;
151 for (i=0; i<ARRAY_SIZE(fl->links); i++)
152 fl->links[i] = (u32)intr_qh | UHCI_PTR_QH;
153 cntl->framelist = fl;
154 cntl->control_qh = cntl->bulk_qh = intr_qh;
157 // Set the frame length to the default: 1 ms exactly
158 outb(USBSOF_DEFAULT, cntl->iobase + USBSOF);
160 // Store the frame list base address
161 outl((u32)fl->links, cntl->iobase + USBFLBASEADD);
163 // Set the current frame number
164 outw(0, cntl->iobase + USBFRNUM);
166 // Mark as configured and running with a 64-byte max packet.
167 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, cntl->iobase + USBCMD);
170 int count = check_uhci_ports(cntl);
171 free_pipe(cntl->usb.defaultpipe);
176 // No devices found - shutdown and free controller.
177 outw(0, cntl->iobase + USBCMD);
187 uhci_init(u16 bdf, int busid)
189 if (! CONFIG_USB_UHCI)
191 struct usb_uhci_s *cntl = malloc_tmphigh(sizeof(*cntl));
192 memset(cntl, 0, sizeof(*cntl));
193 cntl->usb.busid = busid;
194 cntl->usb.type = USB_TYPE_UHCI;
195 cntl->iobase = (pci_config_readl(bdf, PCI_BASE_ADDRESS_4)
196 & PCI_BASE_ADDRESS_IO_MASK);
198 dprintf(3, "UHCI init on dev %02x:%02x.%x (io=%x)\n"
199 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
200 , pci_bdf_to_fn(bdf), cntl->iobase);
202 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
204 reset_uhci(cntl, bdf);
206 run_thread(configure_uhci, cntl);
210 /****************************************************************
211 * End point communication
212 ****************************************************************/
215 wait_qh(struct usb_uhci_s *cntl, struct uhci_qh *qh)
217 // XXX - 500ms just a guess
218 u64 end = calc_future_tsc(500);
220 if (qh->element & UHCI_PTR_TERM)
222 if (check_time(end)) {
224 struct uhci_td *td = (void*)(qh->element & ~UHCI_PTR_BITS);
225 dprintf(1, "Timeout on wait_qh %p (td=%p s=%x c=%x/%x)\n"
227 , inw(cntl->iobase + USBCMD)
228 , inw(cntl->iobase + USBSTS));
235 // Wait for next USB frame to start - for ensuring safe memory release.
237 uhci_waittick(u16 iobase)
240 u16 startframe = inw(iobase + USBFRNUM);
241 u64 end = calc_future_tsc(1000 * 5);
243 if (inw(iobase + USBFRNUM) != startframe)
245 if (check_time(end)) {
255 struct uhci_td *next_td;
256 struct usb_pipe pipe;
262 uhci_free_pipe(struct usb_pipe *p)
264 if (! CONFIG_USB_UHCI)
266 dprintf(7, "uhci_free_pipe %p\n", p);
267 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
268 struct usb_uhci_s *cntl = container_of(
269 pipe->pipe.cntl, struct usb_uhci_s, usb);
271 struct uhci_qh *pos = (void*)(cntl->framelist->links[0] & ~UHCI_PTR_BITS);
273 u32 link = pos->link;
274 if (link == UHCI_PTR_TERM) {
275 // Not found?! Exit without freeing.
276 warn_internalerror();
279 struct uhci_qh *next = (void*)(link & ~UHCI_PTR_BITS);
280 if (next == &pipe->qh) {
281 pos->link = next->link;
282 if (cntl->control_qh == next)
283 cntl->control_qh = pos;
284 if (cntl->bulk_qh == next)
286 uhci_waittick(cntl->iobase);
295 uhci_alloc_control_pipe(struct usb_pipe *dummy)
297 if (! CONFIG_USB_UHCI)
299 struct usb_uhci_s *cntl = container_of(
300 dummy->cntl, struct usb_uhci_s, usb);
301 dprintf(7, "uhci_alloc_control_pipe %p\n", &cntl->usb);
303 // Allocate a queue head.
304 struct uhci_pipe *pipe = malloc_tmphigh(sizeof(*pipe));
309 memset(pipe, 0, sizeof(*pipe));
310 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
311 pipe->qh.element = UHCI_PTR_TERM;
312 pipe->iobase = cntl->iobase;
314 // Add queue head to controller list.
315 struct uhci_qh *control_qh = cntl->control_qh;
316 pipe->qh.link = control_qh->link;
318 control_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
319 if (cntl->bulk_qh == control_qh)
320 cntl->bulk_qh = &pipe->qh;
325 uhci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
326 , void *data, int datasize)
329 if (! CONFIG_USB_UHCI)
331 dprintf(5, "uhci_control %p\n", p);
332 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
333 struct usb_uhci_s *cntl = container_of(
334 pipe->pipe.cntl, struct usb_uhci_s, usb);
336 int maxpacket = pipe->pipe.maxpacket;
337 int lowspeed = pipe->pipe.lowspeed;
338 int devaddr = pipe->pipe.devaddr | (pipe->pipe.ep << 7);
340 // Setup transfer descriptors
341 int count = 2 + DIV_ROUND_UP(datasize, maxpacket);
342 struct uhci_td *tds = malloc_tmphigh(sizeof(*tds) * count);
348 tds[0].link = (u32)&tds[1] | UHCI_PTR_DEPTH;
349 tds[0].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
351 tds[0].token = (uhci_explen(cmdsize) | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
353 tds[0].buffer = (void*)cmd;
354 int toggle = TD_TOKEN_TOGGLE;
356 for (i=1; i<count-1; i++) {
357 tds[i].link = (u32)&tds[i+1] | UHCI_PTR_DEPTH;
358 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
360 int len = (i == count-2 ? (datasize - (i-1)*maxpacket) : maxpacket);
361 tds[i].token = (uhci_explen(len) | toggle
362 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
363 | (dir ? USB_PID_IN : USB_PID_OUT));
364 tds[i].buffer = data + (i-1) * maxpacket;
365 toggle ^= TD_TOKEN_TOGGLE;
367 tds[i].link = UHCI_PTR_TERM;
368 tds[i].status = (uhci_maxerr(0) | (lowspeed ? TD_CTRL_LS : 0)
370 tds[i].token = (uhci_explen(0) | TD_TOKEN_TOGGLE
371 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
372 | (dir ? USB_PID_OUT : USB_PID_IN));
377 pipe->qh.element = (u32)&tds[0];
378 int ret = wait_qh(cntl, &pipe->qh);
380 pipe->qh.element = UHCI_PTR_TERM;
381 uhci_waittick(pipe->iobase);
388 uhci_alloc_bulk_pipe(struct usb_pipe *dummy)
390 if (! CONFIG_USB_UHCI)
392 struct usb_uhci_s *cntl = container_of(
393 dummy->cntl, struct usb_uhci_s, usb);
394 dprintf(7, "uhci_alloc_bulk_pipe %p\n", &cntl->usb);
396 // Allocate a queue head.
397 struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
402 memset(pipe, 0, sizeof(*pipe));
403 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
404 pipe->qh.element = UHCI_PTR_TERM;
405 pipe->iobase = cntl->iobase;
407 // Add queue head to controller list.
408 struct uhci_qh *bulk_qh = cntl->bulk_qh;
409 pipe->qh.link = bulk_qh->link;
411 bulk_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
417 wait_td(struct uhci_td *td)
419 u64 end = calc_future_tsc(5000); // XXX - lookup real time.
423 if (!(status & TD_CTRL_ACTIVE))
425 if (check_time(end)) {
431 if (status & TD_CTRL_ANY_ERROR) {
432 dprintf(1, "wait_td error - status=%x\n", status);
442 uhci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
444 if (! CONFIG_USB_UHCI)
446 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
447 dprintf(7, "uhci_send_bulk qh=%p dir=%d data=%p size=%d\n"
448 , &pipe->qh, dir, data, datasize);
449 int maxpacket = GET_FLATPTR(pipe->pipe.maxpacket);
450 int lowspeed = GET_FLATPTR(pipe->pipe.lowspeed);
451 int devaddr = (GET_FLATPTR(pipe->pipe.devaddr)
452 | (GET_FLATPTR(pipe->pipe.ep) << 7));
453 int toggle = GET_FLATPTR(pipe->toggle) ? TD_TOKEN_TOGGLE : 0;
455 // Allocate 4 tds on stack (16byte aligned)
456 u8 tdsbuf[sizeof(struct uhci_td) * STACKTDS + TDALIGN - 1];
457 struct uhci_td *tds = (void*)ALIGN((u32)tdsbuf, TDALIGN);
458 memset(tds, 0, sizeof(*tds) * STACKTDS);
462 SET_FLATPTR(pipe->qh.element, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
466 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
467 int ret = wait_td(td);
471 int transfer = datasize;
472 if (transfer > maxpacket)
473 transfer = maxpacket;
474 struct uhci_td *nexttd_fl = MAKE_FLATPTR(GET_SEG(SS)
475 , &tds[tdpos % STACKTDS]);
476 td->link = (transfer==datasize ? UHCI_PTR_TERM : (u32)nexttd_fl);
477 td->token = (uhci_explen(transfer) | toggle
478 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
479 | (dir ? USB_PID_IN : USB_PID_OUT));
482 td->status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
484 toggle ^= TD_TOKEN_TOGGLE;
487 datasize -= transfer;
490 for (i=0; i<STACKTDS; i++) {
491 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
492 int ret = wait_td(td);
497 SET_FLATPTR(pipe->toggle, !!toggle);
500 dprintf(1, "uhci_send_bulk failed\n");
501 SET_FLATPTR(pipe->qh.element, UHCI_PTR_TERM);
502 uhci_waittick(GET_FLATPTR(pipe->iobase));
507 uhci_alloc_intr_pipe(struct usb_pipe *dummy, int frameexp)
509 if (! CONFIG_USB_UHCI)
511 struct usb_uhci_s *cntl = container_of(
512 dummy->cntl, struct usb_uhci_s, usb);
513 dprintf(7, "uhci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
517 int maxpacket = dummy->maxpacket;
518 int lowspeed = dummy->lowspeed;
519 int devaddr = dummy->devaddr | (dummy->ep << 7);
520 // Determine number of entries needed for 2 timer ticks.
521 int ms = 1<<frameexp;
522 int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms);
523 count = ALIGN(count, 2);
524 struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
525 struct uhci_td *tds = malloc_low(sizeof(*tds) * count);
526 void *data = malloc_low(maxpacket * count);
527 if (!pipe || !tds || !data) {
531 memset(pipe, 0, sizeof(*pipe));
532 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
533 pipe->qh.element = (u32)tds;
534 pipe->next_td = &tds[0];
535 pipe->iobase = cntl->iobase;
539 for (i=0; i<count; i++) {
540 tds[i].link = (i==count-1 ? (u32)&tds[0] : (u32)&tds[i+1]);
541 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
543 tds[i].token = (uhci_explen(maxpacket) | toggle
544 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
546 tds[i].buffer = data + maxpacket * i;
547 toggle ^= TD_TOKEN_TOGGLE;
550 // Add to interrupt schedule.
551 struct uhci_framelist *fl = cntl->framelist;
553 // Add to existing interrupt entry.
554 struct uhci_qh *intr_qh = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
555 pipe->qh.link = intr_qh->link;
557 intr_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
558 if (cntl->control_qh == intr_qh)
559 cntl->control_qh = &pipe->qh;
560 if (cntl->bulk_qh == intr_qh)
561 cntl->bulk_qh = &pipe->qh;
563 int startpos = 1<<(frameexp-1);
564 pipe->qh.link = fl->links[startpos];
566 for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
567 fl->links[i] = (u32)&pipe->qh | UHCI_PTR_QH;
579 uhci_poll_intr(struct usb_pipe *p, void *data)
582 if (! CONFIG_USB_UHCI)
585 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
586 struct uhci_td *td = GET_FLATPTR(pipe->next_td);
587 u32 status = GET_FLATPTR(td->status);
588 u32 token = GET_FLATPTR(td->token);
589 if (status & TD_CTRL_ACTIVE)
592 // XXX - check for errors.
595 void *tddata = GET_FLATPTR(td->buffer);
596 memcpy_far(GET_SEG(SS), data
597 , FLATPTR_TO_SEG(tddata), (void*)FLATPTR_TO_OFFSET(tddata)
598 , uhci_expected_length(token));
601 struct uhci_td *next = (void*)(GET_FLATPTR(td->link) & ~UHCI_PTR_BITS);
602 SET_FLATPTR(pipe->next_td, next);
604 SET_FLATPTR(td->status, (uhci_maxerr(0) | (status & TD_CTRL_LS)