1 // Code for handling EHCI USB controllers.
3 // Copyright (C) 2010 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-ehci.h" // struct ehci_qh
12 #include "pci_ids.h" // PCI_CLASS_SERIAL_USB_UHCI
13 #include "pci_regs.h" // PCI_BASE_ADDRESS_0
14 #include "usb.h" // struct usb_s
15 #include "farptr.h" // GET_FLATPTR
16 #include "usb-hub.h" // struct usbhub_s
17 #include "usb-uhci.h" // init_uhci
18 #include "usb-ohci.h" // init_ohci
27 struct ehci_caps *caps;
28 struct ehci_regs *regs;
29 struct ehci_qh *async_qh;
30 struct companion_s companion[8];
36 /****************************************************************
38 ****************************************************************/
40 #define EHCI_TIME_POSTPOWER 20
41 #define EHCI_TIME_POSTRESET 2
43 // Start processing of companion controllers for full/low speed devices
45 ehci_startcompanion(struct usb_ehci_s *cntl)
47 if (! cntl->legacycount)
48 // No full/low speed devices found.
51 for (i=0; i<ARRAY_SIZE(cntl->companion); i++) {
52 u16 type = cntl->companion[i].type;
53 if (type == USB_TYPE_UHCI)
54 uhci_init(cntl->companion[i].bdf, cntl->usb.busid + i);
55 else if (type == USB_TYPE_OHCI)
56 ohci_init(cntl->companion[i].bdf, cntl->usb.busid + i);
63 init_ehci_port(void *data)
65 struct usbhub_s *hub = data;
66 u32 port = hub->port; // XXX - find better way to pass port
67 struct usb_ehci_s *cntl = container_of(hub->cntl, struct usb_ehci_s, usb);
69 u32 *portreg = &cntl->regs->portsc[port];
70 u32 portsc = readl(portreg);
73 if (!(portsc & PORT_POWER)) {
75 writel(portreg, portsc);
76 msleep(EHCI_TIME_POSTPOWER);
77 portsc = readl(portreg);
80 if (!(portsc & PORT_CONNECT))
84 if ((portsc & PORT_LINESTATUS_MASK) == PORT_LINESTATUS_KSTATE) {
87 writel(portreg, portsc | PORT_OWNER);
91 // XXX - if just powered up, need to wait for USB_TIME_ATTDB?
94 portsc = (portsc & ~PORT_PE) | PORT_RESET;
95 writel(portreg, portsc);
96 msleep(USB_TIME_DRSTR);
97 mutex_lock(&cntl->usb.resetlock);
98 portsc &= ~PORT_RESET;
99 writel(portreg, portsc);
100 msleep(EHCI_TIME_POSTRESET);
102 portsc = readl(portreg);
103 if (!(portsc & PORT_CONNECT))
104 // No longer connected
106 if (!(portsc & PORT_PE)) {
109 writel(portreg, portsc | PORT_OWNER);
112 struct usb_pipe *pipe = usb_set_address(hub, port, USB_HIGHSPEED);
115 mutex_unlock(&cntl->usb.resetlock);
118 int count = configure_usb_device(pipe);
122 writel(portreg, portsc & ~PORT_PE);
123 hub->devcount += count;
125 if (! --cntl->checkports)
126 ehci_startcompanion(cntl);
131 mutex_unlock(&cntl->usb.resetlock);
135 // Find any devices connected to the root hub.
137 check_ehci_ports(struct usb_ehci_s *cntl)
141 // Launch a thread for every port.
143 memset(&hub, 0, sizeof(hub));
144 hub.cntl = &cntl->usb;
145 int ports = cntl->checkports;
148 for (i=0; i<ports; i++) {
150 run_thread(init_ehci_port, &hub);
153 // Wait for threads to complete.
161 /****************************************************************
163 ****************************************************************/
166 configure_ehci(void *data)
168 struct usb_ehci_s *cntl = data;
170 // Allocate ram for schedule storage
171 struct ehci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl));
172 struct ehci_qh *intr_qh = memalign_high(EHCI_QH_ALIGN, sizeof(*intr_qh));
173 struct ehci_qh *async_qh = memalign_high(EHCI_QH_ALIGN, sizeof(*async_qh));
174 if (!fl || !intr_qh || !async_qh) {
179 // XXX - check for halted?
182 u32 cmd = readl(&cntl->regs->usbcmd);
183 writel(&cntl->regs->usbcmd, (cmd & ~(CMD_ASE | CMD_PSE)) | CMD_HCRESET);
184 u64 end = calc_future_tsc(250);
186 cmd = readl(&cntl->regs->usbcmd);
187 if (!(cmd & CMD_HCRESET))
189 if (check_time(end)) {
195 // Disable interrupts (just to be safe).
196 writel(&cntl->regs->usbintr, 0);
198 // Set schedule to point to primary intr queue head
199 memset(intr_qh, 0, sizeof(*intr_qh));
200 intr_qh->next = EHCI_PTR_TERM;
201 intr_qh->info2 = (0x01 << QH_SMASK_SHIFT);
202 intr_qh->token = QTD_STS_HALT;
203 intr_qh->qtd_next = intr_qh->alt_next = EHCI_PTR_TERM;
205 for (i=0; i<ARRAY_SIZE(fl->links); i++)
206 fl->links[i] = (u32)intr_qh | EHCI_PTR_QH;
207 writel(&cntl->regs->periodiclistbase, (u32)fl);
209 // Set async list to point to primary async queue head
210 memset(async_qh, 0, sizeof(*async_qh));
211 async_qh->next = (u32)async_qh | EHCI_PTR_QH;
212 async_qh->info1 = QH_HEAD;
213 async_qh->token = QTD_STS_HALT;
214 async_qh->qtd_next = async_qh->alt_next = EHCI_PTR_TERM;
215 cntl->async_qh = async_qh;
216 writel(&cntl->regs->asynclistbase, (u32)async_qh);
219 writel(&cntl->regs->usbcmd, cmd | CMD_ASE | CMD_PSE | CMD_RUN);
221 // Set default of high speed for root hub.
222 writel(&cntl->regs->configflag, 1);
223 cntl->checkports = readl(&cntl->caps->hcsparams) & HCS_N_PORTS_MASK;
226 int count = check_ehci_ports(cntl);
227 free_pipe(cntl->usb.defaultpipe);
232 // No devices found - shutdown and free controller.
233 writel(&cntl->regs->usbcmd, cmd & ~CMD_RUN);
234 msleep(4); // 2ms to stop reading memory - XXX
243 ehci_init(u16 bdf, int busid, int compbdf)
245 if (! CONFIG_USB_EHCI)
248 u32 baseaddr = pci_config_readl(bdf, PCI_BASE_ADDRESS_0);
249 struct ehci_caps *caps = (void*)(baseaddr & PCI_BASE_ADDRESS_MEM_MASK);
250 u32 hcc_params = readl(&caps->hccparams);
251 if (hcc_params & HCC_64BIT_ADDR) {
252 dprintf(1, "No support for 64bit EHCI\n");
256 struct usb_ehci_s *cntl = malloc_tmphigh(sizeof(*cntl));
257 memset(cntl, 0, sizeof(*cntl));
258 cntl->usb.busid = busid;
259 cntl->usb.type = USB_TYPE_EHCI;
261 cntl->regs = (void*)caps + readb(&caps->caplength);
263 dprintf(3, "EHCI init on dev %02x:%02x.%x (regs=%p)\n"
264 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
265 , pci_bdf_to_fn(bdf), cntl->regs);
267 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
269 // XXX - check for and disable SMM control?
271 // Find companion controllers.
273 int max = pci_to_bdf(pci_bdf_to_bus(bdf) + 1, 0, 0);
275 if (compbdf < 0 || compbdf >= bdf)
277 u32 code = pci_config_readl(compbdf, PCI_CLASS_REVISION) >> 8;
278 if (code == PCI_CLASS_SERIAL_USB_UHCI) {
279 cntl->companion[count].bdf = compbdf;
280 cntl->companion[count].type = USB_TYPE_UHCI;
282 } else if (code == PCI_CLASS_SERIAL_USB_OHCI) {
283 cntl->companion[count].bdf = compbdf;
284 cntl->companion[count].type = USB_TYPE_OHCI;
287 compbdf = pci_next(compbdf+1, &max);
290 run_thread(configure_ehci, cntl);
295 /****************************************************************
296 * End point communication
297 ****************************************************************/
300 ehci_wait_qh(struct usb_ehci_s *cntl, struct ehci_qh *qh)
302 // XXX - 500ms just a guess
303 u64 end = calc_future_tsc(500);
305 if (qh->qtd_next & EHCI_PTR_TERM)
308 if (check_time(end)) {
316 // Wait for next USB async frame to start - for ensuring safe memory release.
318 ehci_waittick(struct usb_ehci_s *cntl)
324 // Wait for access to "doorbell"
327 u64 end = calc_future_tsc(100);
329 sts = readl(&cntl->regs->usbsts);
330 if (!(sts & STS_IAA)) {
331 cmd = readl(&cntl->regs->usbcmd);
332 if (!(cmd & CMD_IAAD))
335 if (check_time(end)) {
342 writel(&cntl->regs->usbcmd, cmd | CMD_IAAD);
343 // Wait for completion
345 sts = readl(&cntl->regs->usbsts);
348 if (check_time(end)) {
355 writel(&cntl->regs->usbsts, STS_IAA);
360 struct ehci_qtd *next_td, *tds;
362 struct usb_pipe pipe;
366 ehci_free_pipe(struct usb_pipe *p)
368 if (! CONFIG_USB_EHCI)
370 dprintf(7, "ehci_free_pipe %p\n", p);
371 struct ehci_pipe *pipe = container_of(p, struct ehci_pipe, pipe);
372 struct usb_ehci_s *cntl = container_of(
373 pipe->pipe.cntl, struct usb_ehci_s, usb);
375 struct ehci_qh *start = cntl->async_qh;
376 struct ehci_qh *pos = start;
378 struct ehci_qh *next = (void*)(pos->next & ~EHCI_PTR_BITS);
380 // Not found?! Exit without freeing.
381 warn_internalerror();
384 if (next == &pipe->qh) {
385 pos->next = next->next;
395 ehci_alloc_control_pipe(struct usb_pipe *dummy)
397 if (! CONFIG_USB_EHCI)
399 struct usb_ehci_s *cntl = container_of(
400 dummy->cntl, struct usb_ehci_s, usb);
401 dprintf(7, "ehci_alloc_control_pipe %p\n", &cntl->usb);
403 // Allocate a queue head.
404 struct ehci_pipe *pipe = memalign_tmphigh(EHCI_QH_ALIGN, sizeof(*pipe));
409 memset(pipe, 0, sizeof(*pipe));
410 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
411 pipe->qh.qtd_next = pipe->qh.alt_next = EHCI_PTR_TERM;
412 pipe->qh.token = QTD_STS_HALT;
414 // Add queue head to controller list.
415 struct ehci_qh *async_qh = cntl->async_qh;
416 pipe->qh.next = async_qh->next;
418 async_qh->next = (u32)&pipe->qh | EHCI_PTR_QH;
423 fillTDbuffer(struct ehci_qtd *td, u16 maxpacket, const void *buf, int bytes)
428 if (pos >= &td->buf[ARRAY_SIZE(td->buf)])
429 // More data than can transfer in a single qtd - only use
430 // full packets to prevent a babble error.
431 return ALIGN_DOWN(dest - (u32)buf, maxpacket);
433 u32 max = 0x1000 - (dest & 0xfff);
441 return dest - (u32)buf;
445 ehci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
446 , void *data, int datasize)
449 if (! CONFIG_USB_EHCI)
451 dprintf(5, "ehci_control %p\n", p);
452 if (datasize > 4*4096 || cmdsize > 4*4096) {
453 // XXX - should support larger sizes.
457 struct ehci_pipe *pipe = container_of(p, struct ehci_pipe, pipe);
458 struct usb_ehci_s *cntl = container_of(
459 pipe->pipe.cntl, struct usb_ehci_s, usb);
461 u16 maxpacket = pipe->pipe.maxpacket;
462 int speed = pipe->pipe.speed;
464 // Setup fields in qh
466 (1 << QH_MULT_SHIFT) | (speed != USB_HIGHSPEED ? QH_CONTROL : 0)
467 | (maxpacket << QH_MAXPACKET_SHIFT)
469 | (speed << QH_SPEED_SHIFT)
470 | (pipe->pipe.ep << QH_EP_SHIFT)
471 | (pipe->pipe.devaddr << QH_DEVADDR_SHIFT));
472 pipe->qh.info2 = ((1 << QH_MULT_SHIFT)
473 | (pipe->pipe.tt_port << QH_HUBPORT_SHIFT)
474 | (pipe->pipe.tt_devaddr << QH_HUBADDR_SHIFT));
476 // Setup transfer descriptors
477 struct ehci_qtd *tds = memalign_tmphigh(EHCI_QTD_ALIGN, sizeof(*tds) * 3);
482 memset(tds, 0, sizeof(*tds) * 3);
483 struct ehci_qtd *td = tds;
485 td->qtd_next = (u32)&td[1];
486 td->alt_next = EHCI_PTR_TERM;
487 td->token = (ehci_explen(cmdsize) | QTD_STS_ACTIVE
488 | QTD_PID_SETUP | ehci_maxerr(3));
489 fillTDbuffer(td, maxpacket, cmd, cmdsize);
493 td->qtd_next = (u32)&td[1];
494 td->alt_next = EHCI_PTR_TERM;
495 td->token = (QTD_TOGGLE | ehci_explen(datasize) | QTD_STS_ACTIVE
496 | (dir ? QTD_PID_IN : QTD_PID_OUT) | ehci_maxerr(3));
497 fillTDbuffer(td, maxpacket, data, datasize);
501 td->qtd_next = EHCI_PTR_TERM;
502 td->alt_next = EHCI_PTR_TERM;
503 td->token = (QTD_TOGGLE | QTD_STS_ACTIVE
504 | (dir ? QTD_PID_OUT : QTD_PID_IN) | ehci_maxerr(3));
508 pipe->qh.qtd_next = (u32)tds;
511 int ret = ehci_wait_qh(cntl, &pipe->qh);
512 pipe->qh.token = QTD_STS_HALT;
514 pipe->qh.qtd_next = pipe->qh.alt_next = EHCI_PTR_TERM;
523 ehci_alloc_bulk_pipe(struct usb_pipe *dummy)
525 // XXX - this func is same as alloc_control except for malloc_low
526 if (! CONFIG_USB_EHCI)
528 struct usb_ehci_s *cntl = container_of(
529 dummy->cntl, struct usb_ehci_s, usb);
530 dprintf(7, "ehci_alloc_bulk_pipe %p\n", &cntl->usb);
532 // Allocate a queue head.
533 struct ehci_pipe *pipe = memalign_low(EHCI_QH_ALIGN, sizeof(*pipe));
538 memset(pipe, 0, sizeof(*pipe));
539 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
540 pipe->qh.qtd_next = pipe->qh.alt_next = EHCI_PTR_TERM;
541 pipe->qh.token = QTD_STS_HALT;
543 // Add queue head to controller list.
544 struct ehci_qh *async_qh = cntl->async_qh;
545 pipe->qh.next = async_qh->next;
547 async_qh->next = (u32)&pipe->qh | EHCI_PTR_QH;
552 ehci_wait_td(struct ehci_qtd *td)
554 u64 end = calc_future_tsc(5000); // XXX - lookup real time.
558 if (!(status & QTD_STS_ACTIVE))
560 if (check_time(end)) {
566 if (status & QTD_STS_HALT) {
567 dprintf(1, "ehci_wait_td error - status=%x\n", status);
576 ehci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
578 if (! CONFIG_USB_EHCI)
580 struct ehci_pipe *pipe = container_of(p, struct ehci_pipe, pipe);
581 dprintf(7, "ehci_send_bulk qh=%p dir=%d data=%p size=%d\n"
582 , &pipe->qh, dir, data, datasize);
584 // Allocate 4 tds on stack (16byte aligned)
585 u8 tdsbuf[sizeof(struct ehci_qtd) * STACKQTDS + EHCI_QTD_ALIGN - 1];
586 struct ehci_qtd *tds = (void*)ALIGN((u32)tdsbuf, EHCI_QTD_ALIGN);
587 memset(tds, 0, sizeof(*tds) * STACKQTDS);
589 // Setup fields in qh
590 u16 maxpacket = GET_FLATPTR(pipe->pipe.maxpacket);
591 SET_FLATPTR(pipe->qh.info1
592 , ((1 << QH_MULT_SHIFT)
593 | (maxpacket << QH_MAXPACKET_SHIFT)
594 | (GET_FLATPTR(pipe->pipe.speed) << QH_SPEED_SHIFT)
595 | (GET_FLATPTR(pipe->pipe.ep) << QH_EP_SHIFT)
596 | (GET_FLATPTR(pipe->pipe.devaddr) << QH_DEVADDR_SHIFT)));
597 SET_FLATPTR(pipe->qh.info2
598 , ((1 << QH_MULT_SHIFT)
599 | (GET_FLATPTR(pipe->pipe.tt_port) << QH_HUBPORT_SHIFT)
600 | (GET_FLATPTR(pipe->pipe.tt_devaddr) << QH_HUBADDR_SHIFT)));
602 SET_FLATPTR(pipe->qh.qtd_next, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
604 SET_FLATPTR(pipe->qh.token, GET_FLATPTR(pipe->qh.token) & QTD_TOGGLE);
608 struct ehci_qtd *td = &tds[tdpos++ % STACKQTDS];
609 int ret = ehci_wait_td(td);
613 struct ehci_qtd *nexttd_fl = MAKE_FLATPTR(GET_SEG(SS)
614 , &tds[tdpos % STACKQTDS]);
616 int transfer = fillTDbuffer(td, maxpacket, data, datasize);
617 td->qtd_next = (transfer==datasize ? EHCI_PTR_TERM : (u32)nexttd_fl);
618 td->alt_next = EHCI_PTR_TERM;
620 td->token = (ehci_explen(transfer) | QTD_STS_ACTIVE
621 | (dir ? QTD_PID_IN : QTD_PID_OUT) | ehci_maxerr(3));
624 datasize -= transfer;
627 for (i=0; i<STACKQTDS; i++) {
628 struct ehci_qtd *td = &tds[tdpos++ % STACKQTDS];
629 int ret = ehci_wait_td(td);
636 dprintf(1, "ehci_send_bulk failed\n");
637 SET_FLATPTR(pipe->qh.qtd_next, EHCI_PTR_TERM);
638 SET_FLATPTR(pipe->qh.alt_next, EHCI_PTR_TERM);
640 struct usb_ehci_s *cntl = container_of(
641 GET_FLATPTR(pipe->pipe.cntl), struct usb_ehci_s, usb);
647 ehci_alloc_intr_pipe(struct usb_pipe *dummy, int frameexp)
649 if (! CONFIG_USB_EHCI)
651 struct usb_ehci_s *cntl = container_of(
652 dummy->cntl, struct usb_ehci_s, usb);
653 dprintf(7, "ehci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
657 int maxpacket = dummy->maxpacket;
658 // Determine number of entries needed for 2 timer ticks.
659 int ms = 1<<frameexp;
660 int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms);
661 struct ehci_pipe *pipe = memalign_low(EHCI_QH_ALIGN, sizeof(*pipe));
662 struct ehci_qtd *tds = memalign_low(EHCI_QTD_ALIGN, sizeof(*tds) * count);
663 void *data = malloc_low(maxpacket * count);
664 if (!pipe || !tds || !data) {
668 memset(pipe, 0, sizeof(*pipe));
669 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
670 pipe->next_td = pipe->tds = tds;
675 | (maxpacket << QH_MAXPACKET_SHIFT)
676 | (pipe->pipe.speed << QH_SPEED_SHIFT)
677 | (pipe->pipe.ep << QH_EP_SHIFT)
678 | (pipe->pipe.devaddr << QH_DEVADDR_SHIFT));
679 pipe->qh.info2 = ((1 << QH_MULT_SHIFT)
680 | (pipe->pipe.tt_port << QH_HUBPORT_SHIFT)
681 | (pipe->pipe.tt_devaddr << QH_HUBADDR_SHIFT)
682 | (0x01 << QH_SMASK_SHIFT)
683 | (0x1c << QH_CMASK_SHIFT));
684 pipe->qh.qtd_next = (u32)tds;
687 for (i=0; i<count; i++) {
688 struct ehci_qtd *td = &tds[i];
689 td->qtd_next = (i==count-1 ? (u32)tds : (u32)&td[1]);
690 td->alt_next = EHCI_PTR_TERM;
691 td->token = (ehci_explen(maxpacket) | QTD_STS_ACTIVE
692 | QTD_PID_IN | ehci_maxerr(3));
693 td->buf[0] = (u32)data + maxpacket * i;
696 // Add to interrupt schedule.
697 struct ehci_framelist *fl = (void*)readl(&cntl->regs->periodiclistbase);
699 // Add to existing interrupt entry.
700 struct ehci_qh *intr_qh = (void*)(fl->links[0] & ~EHCI_PTR_BITS);
701 pipe->qh.next = intr_qh->next;
703 intr_qh->next = (u32)&pipe->qh | EHCI_PTR_QH;
705 int startpos = 1<<(frameexp-1);
706 pipe->qh.next = fl->links[startpos];
708 for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
709 fl->links[i] = (u32)&pipe->qh | EHCI_PTR_QH;
721 ehci_poll_intr(struct usb_pipe *p, void *data)
724 if (! CONFIG_USB_EHCI)
726 struct ehci_pipe *pipe = container_of(p, struct ehci_pipe, pipe);
727 struct ehci_qtd *td = GET_FLATPTR(pipe->next_td);
728 u32 token = GET_FLATPTR(td->token);
729 if (token & QTD_STS_ACTIVE)
732 // XXX - check for errors.
735 int maxpacket = GET_FLATPTR(pipe->pipe.maxpacket);
736 int pos = td - GET_FLATPTR(pipe->tds);
737 void *tddata = GET_FLATPTR(pipe->data) + maxpacket * pos;
738 memcpy_far(GET_SEG(SS), data
739 , FLATPTR_TO_SEG(tddata), (void*)FLATPTR_TO_OFFSET(tddata)
743 struct ehci_qtd *next = (void*)(GET_FLATPTR(td->qtd_next) & ~EHCI_PTR_BITS);
744 SET_FLATPTR(pipe->next_td, next);
745 SET_FLATPTR(td->buf[0], (u32)tddata);
747 SET_FLATPTR(td->token, (ehci_explen(maxpacket) | QTD_STS_ACTIVE
748 | QTD_PID_IN | ehci_maxerr(3)));