1 // Code for handling XHCI "Super speed" USB controllers.
3 // Copyright (C) 2013 Gerd Hoffmann <kraxel@redhat.com>
4 // Copyright (C) 2014 Kevin O'Connor <kevin@koconnor.net>
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
8 #include "config.h" // CONFIG_*
9 #include "malloc.h" // memalign_low
10 #include "memmap.h" // PAGE_SIZE
11 #include "output.h" // dprintf
12 #include "pci.h" // pci_bdf_to_bus
13 #include "pci_ids.h" // PCI_CLASS_SERIAL_USB_XHCI
14 #include "pci_regs.h" // PCI_BASE_ADDRESS_0
15 #include "string.h" // memcpy
16 #include "usb.h" // struct usb_s
17 #include "usb-xhci.h" // struct ehci_qh
18 #include "util.h" // timer_calc
19 #include "x86.h" // readl
21 // --------------------------------------------------------------
24 #define XHCI_RING_ITEMS 16
25 #define XHCI_RING_SIZE (XHCI_RING_ITEMS*sizeof(struct xhci_trb))
28 * xhci_ring structs are allocated with XHCI_RING_SIZE alignment,
29 * then we can get it from a trb pointer (provided by evt ring).
31 #define XHCI_RING(_trb) \
32 ((struct xhci_ring*)((u32)(_trb) & ~(XHCI_RING_SIZE-1)))
34 // --------------------------------------------------------------
37 #define XHCI_CMD_RS (1<<0)
38 #define XHCI_CMD_HCRST (1<<1)
39 #define XHCI_CMD_INTE (1<<2)
40 #define XHCI_CMD_HSEE (1<<3)
41 #define XHCI_CMD_LHCRST (1<<7)
42 #define XHCI_CMD_CSS (1<<8)
43 #define XHCI_CMD_CRS (1<<9)
44 #define XHCI_CMD_EWE (1<<10)
45 #define XHCI_CMD_EU3S (1<<11)
47 #define XHCI_STS_HCH (1<<0)
48 #define XHCI_STS_HSE (1<<2)
49 #define XHCI_STS_EINT (1<<3)
50 #define XHCI_STS_PCD (1<<4)
51 #define XHCI_STS_SSS (1<<8)
52 #define XHCI_STS_RSS (1<<9)
53 #define XHCI_STS_SRE (1<<10)
54 #define XHCI_STS_CNR (1<<11)
55 #define XHCI_STS_HCE (1<<12)
57 #define XHCI_PORTSC_CCS (1<<0)
58 #define XHCI_PORTSC_PED (1<<1)
59 #define XHCI_PORTSC_OCA (1<<3)
60 #define XHCI_PORTSC_PR (1<<4)
61 #define XHCI_PORTSC_PLS_SHIFT 5
62 #define XHCI_PORTSC_PLS_MASK 0xf
63 #define XHCI_PORTSC_PP (1<<9)
64 #define XHCI_PORTSC_SPEED_SHIFT 10
65 #define XHCI_PORTSC_SPEED_MASK 0xf
66 #define XHCI_PORTSC_SPEED_FULL (1<<10)
67 #define XHCI_PORTSC_SPEED_LOW (2<<10)
68 #define XHCI_PORTSC_SPEED_HIGH (3<<10)
69 #define XHCI_PORTSC_SPEED_SUPER (4<<10)
70 #define XHCI_PORTSC_PIC_SHIFT 14
71 #define XHCI_PORTSC_PIC_MASK 0x3
72 #define XHCI_PORTSC_LWS (1<<16)
73 #define XHCI_PORTSC_CSC (1<<17)
74 #define XHCI_PORTSC_PEC (1<<18)
75 #define XHCI_PORTSC_WRC (1<<19)
76 #define XHCI_PORTSC_OCC (1<<20)
77 #define XHCI_PORTSC_PRC (1<<21)
78 #define XHCI_PORTSC_PLC (1<<22)
79 #define XHCI_PORTSC_CEC (1<<23)
80 #define XHCI_PORTSC_CAS (1<<24)
81 #define XHCI_PORTSC_WCE (1<<25)
82 #define XHCI_PORTSC_WDE (1<<26)
83 #define XHCI_PORTSC_WOE (1<<27)
84 #define XHCI_PORTSC_DR (1<<30)
85 #define XHCI_PORTSC_WPR (1<<31)
88 #define TRB_TYPE_SHIFT 10
89 #define TRB_TYPE_MASK 0x3f
90 #define TRB_TYPE(t) (((t) >> TRB_TYPE_SHIFT) & TRB_TYPE_MASK)
92 #define TRB_EV_ED (1<<2)
94 #define TRB_TR_ENT (1<<1)
95 #define TRB_TR_ISP (1<<2)
96 #define TRB_TR_NS (1<<3)
97 #define TRB_TR_CH (1<<4)
98 #define TRB_TR_IOC (1<<5)
99 #define TRB_TR_IDT (1<<6)
100 #define TRB_TR_TBC_SHIFT 7
101 #define TRB_TR_TBC_MASK 0x3
102 #define TRB_TR_BEI (1<<9)
103 #define TRB_TR_TLBPC_SHIFT 16
104 #define TRB_TR_TLBPC_MASK 0xf
105 #define TRB_TR_FRAMEID_SHIFT 20
106 #define TRB_TR_FRAMEID_MASK 0x7ff
107 #define TRB_TR_SIA (1<<31)
109 #define TRB_TR_DIR (1<<16)
111 #define TRB_CR_SLOTID_SHIFT 24
112 #define TRB_CR_SLOTID_MASK 0xff
113 #define TRB_CR_EPID_SHIFT 16
114 #define TRB_CR_EPID_MASK 0x1f
116 #define TRB_CR_BSR (1<<9)
117 #define TRB_CR_DC (1<<9)
119 #define TRB_LK_TC (1<<1)
121 #define TRB_INTR_SHIFT 22
122 #define TRB_INTR_MASK 0x3ff
123 #define TRB_INTR(t) (((t).status >> TRB_INTR_SHIFT) & TRB_INTR_MASK)
125 typedef enum TRBType {
138 CR_CONFIGURE_ENDPOINT,
146 CR_SET_LATENCY_TOLERANCE,
147 CR_GET_PORT_BANDWIDTH,
152 ER_PORT_STATUS_CHANGE,
153 ER_BANDWIDTH_REQUEST,
156 ER_DEVICE_NOTIFICATION,
160 typedef enum TRBCCode {
163 CC_DATA_BUFFER_ERROR,
165 CC_USB_TRANSACTION_ERROR,
171 CC_INVALID_STREAM_TYPE_ERROR,
172 CC_SLOT_NOT_ENABLED_ERROR,
173 CC_EP_NOT_ENABLED_ERROR,
179 CC_BANDWIDTH_OVERRUN,
180 CC_CONTEXT_STATE_ERROR,
181 CC_NO_PING_RESPONSE_ERROR,
182 CC_EVENT_RING_FULL_ERROR,
183 CC_INCOMPATIBLE_DEVICE_ERROR,
184 CC_MISSED_SERVICE_ERROR,
185 CC_COMMAND_RING_STOPPED,
188 CC_STOPPED_LENGTH_INVALID,
189 CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR = 29,
190 CC_ISOCH_BUFFER_OVERRUN = 31,
193 CC_INVALID_STREAM_ID_ERROR,
194 CC_SECONDARY_BANDWIDTH_ERROR,
195 CC_SPLIT_TRANSACTION_ERROR
209 PLS_COMPILANCE_MODE = 10,
214 #define xhci_get_field(data, field) \
215 (((data) >> field##_SHIFT) & field##_MASK)
217 // --------------------------------------------------------------
221 struct xhci_trb ring[XHCI_RING_ITEMS];
240 struct xhci_caps *caps;
246 /* xhci data structures */
247 struct xhci_devlist *devs;
248 struct xhci_ring *cmds;
249 struct xhci_ring *evts;
250 struct xhci_er_seg *eseg;
254 struct xhci_ring reqs;
256 struct usb_pipe pipe;
263 // --------------------------------------------------------------
266 static const char *speed_name[16] = {
274 static const int speed_from_xhci[16] = {
276 [ 1 ] = USB_FULLSPEED,
277 [ 2 ] = USB_LOWSPEED,
278 [ 3 ] = USB_HIGHSPEED,
279 [ 4 ] = USB_SUPERSPEED,
283 static const int speed_to_xhci[] = {
284 [ USB_FULLSPEED ] = 1,
285 [ USB_LOWSPEED ] = 2,
286 [ USB_HIGHSPEED ] = 3,
287 [ USB_SUPERSPEED ] = 4,
290 static const int eptype_to_xhci_in[] = {
291 [ USB_ENDPOINT_XFER_CONTROL] = 4,
292 [ USB_ENDPOINT_XFER_ISOC ] = 5,
293 [ USB_ENDPOINT_XFER_BULK ] = 6,
294 [ USB_ENDPOINT_XFER_INT ] = 7,
297 static const int eptype_to_xhci_out[] = {
298 [ USB_ENDPOINT_XFER_CONTROL] = 4,
299 [ USB_ENDPOINT_XFER_ISOC ] = 1,
300 [ USB_ENDPOINT_XFER_BULK ] = 2,
301 [ USB_ENDPOINT_XFER_INT ] = 3,
304 static int wait_bit(u32 *reg, u32 mask, int value, u32 timeout)
306 u32 end = timer_calc(timeout);
308 while ((readl(reg) & mask) != value) {
309 if (timer_check(end)) {
319 /****************************************************************
321 ****************************************************************/
323 #define XHCI_TIME_POSTPOWER 20
325 // Check if device attached to port
327 xhci_print_port_state(int loglevel, const char *prefix, u32 port, u32 portsc)
329 u32 pls = xhci_get_field(portsc, XHCI_PORTSC_PLS);
330 u32 speed = xhci_get_field(portsc, XHCI_PORTSC_SPEED);
332 dprintf(loglevel, "%s port #%d: 0x%08x,%s%s pls %d, speed %d [%s]\n",
333 prefix, port + 1, portsc,
334 (portsc & XHCI_PORTSC_PP) ? " powered," : "",
335 (portsc & XHCI_PORTSC_PED) ? " enabled," : "",
336 pls, speed, speed_name[speed]);
340 xhci_hub_detect(struct usbhub_s *hub, u32 port)
342 struct usb_xhci_s *xhci = container_of(hub->cntl, struct usb_xhci_s, usb);
343 u32 portsc = readl(&xhci->pr[port].portsc);
344 return (portsc & XHCI_PORTSC_CCS) ? 1 : 0;
347 // Reset device on port
349 xhci_hub_reset(struct usbhub_s *hub, u32 port)
351 struct usb_xhci_s *xhci = container_of(hub->cntl, struct usb_xhci_s, usb);
352 u32 portsc = readl(&xhci->pr[port].portsc);
355 switch (xhci_get_field(portsc, XHCI_PORTSC_PLS)) {
357 rc = speed_from_xhci[xhci_get_field(portsc, XHCI_PORTSC_SPEED)];
360 xhci_print_port_state(3, __func__, port, portsc);
361 portsc |= XHCI_PORTSC_PR;
362 writel(&xhci->pr[port].portsc, portsc);
363 if (wait_bit(&xhci->pr[port].portsc, XHCI_PORTSC_PED, XHCI_PORTSC_PED, 100) != 0)
365 portsc = readl(&xhci->pr[port].portsc);
366 rc = speed_from_xhci[xhci_get_field(portsc, XHCI_PORTSC_SPEED)];
373 xhci_print_port_state(1, "XHCI", port, portsc);
378 xhci_hub_disconnect(struct usbhub_s *hub, u32 port)
380 // XXX - should turn the port power off.
383 static struct usbhub_op_s xhci_hub_ops = {
384 .detect = xhci_hub_detect,
385 .reset = xhci_hub_reset,
386 .disconnect = xhci_hub_disconnect,
389 // Find any devices connected to the root hub.
391 xhci_check_ports(struct usb_xhci_s *xhci)
393 // Wait for port power to stabilize.
394 msleep(XHCI_TIME_POSTPOWER);
397 memset(&hub, 0, sizeof(hub));
398 hub.cntl = &xhci->usb;
399 hub.portcount = xhci->ports;
400 hub.op = &xhci_hub_ops;
406 /****************************************************************
408 ****************************************************************/
411 xhci_free_pipes(struct usb_xhci_s *xhci)
413 // XXX - should walk list of pipes and free unused pipes.
417 configure_xhci(void *data)
419 struct usb_xhci_s *xhci = data;
422 xhci->devs = memalign_high(64, sizeof(*xhci->devs) * (xhci->slots + 1));
423 xhci->eseg = memalign_high(64, sizeof(*xhci->eseg));
424 xhci->cmds = memalign_high(XHCI_RING_SIZE, sizeof(*xhci->cmds));
425 xhci->evts = memalign_high(XHCI_RING_SIZE, sizeof(*xhci->evts));
426 if (!xhci->devs || !xhci->cmds || !xhci->evts || !xhci->eseg) {
430 memset(xhci->devs, 0, sizeof(*xhci->devs) * (xhci->slots + 1));
431 memset(xhci->cmds, 0, sizeof(*xhci->cmds));
432 memset(xhci->evts, 0, sizeof(*xhci->evts));
433 memset(xhci->eseg, 0, sizeof(*xhci->eseg));
435 reg = readl(&xhci->op->usbcmd);
436 if (reg & XHCI_CMD_RS) {
438 writel(&xhci->op->usbcmd, reg);
439 if (wait_bit(&xhci->op->usbsts, XHCI_STS_HCH, XHCI_STS_HCH, 32) != 0)
443 dprintf(3, "%s: resetting\n", __func__);
444 writel(&xhci->op->usbcmd, XHCI_CMD_HCRST);
445 if (wait_bit(&xhci->op->usbcmd, XHCI_CMD_HCRST, 0, 100) != 0)
447 if (wait_bit(&xhci->op->usbsts, XHCI_STS_CNR, 0, 100) != 0)
450 writel(&xhci->op->config, xhci->slots);
451 writel(&xhci->op->dcbaap_low, (u32)xhci->devs);
452 writel(&xhci->op->dcbaap_high, 0);
453 writel(&xhci->op->crcr_low, (u32)xhci->cmds | 1);
454 writel(&xhci->op->crcr_high, 0);
457 xhci->eseg->ptr_low = (u32)xhci->evts;
458 xhci->eseg->ptr_high = 0;
459 xhci->eseg->size = XHCI_RING_ITEMS;
460 writel(&xhci->ir->erstsz, 1);
461 writel(&xhci->ir->erdp_low, (u32)xhci->evts);
462 writel(&xhci->ir->erdp_high, 0);
463 writel(&xhci->ir->erstba_low, (u32)xhci->eseg);
464 writel(&xhci->ir->erstba_high, 0);
467 reg = readl(&xhci->caps->hcsparams2);
470 dprintf(3, "%s: setup %d scratch pad buffers\n", __func__, spb);
471 u64 *spba = memalign_high(64, sizeof(*spba) * spb);
472 void *pad = memalign_high(PAGE_SIZE, PAGE_SIZE * spb);
480 for (i = 0; i < spb; i++)
481 spba[i] = (u32)pad + (i * PAGE_SIZE);
482 xhci->devs[0].ptr_low = (u32)spba;
483 xhci->devs[0].ptr_high = 0;
486 reg = readl(&xhci->op->usbcmd);
488 writel(&xhci->op->usbcmd, reg);
491 int count = xhci_check_ports(xhci);
492 xhci_free_pipes(xhci);
497 // No devices found - shutdown and free controller.
498 dprintf(1, "XHCI no devices found\n");
499 reg = readl(&xhci->op->usbcmd);
501 writel(&xhci->op->usbcmd, reg);
502 wait_bit(&xhci->op->usbsts, XHCI_STS_HCH, XHCI_STS_HCH, 32);
513 xhci_controller_setup(struct pci_device *pci)
515 struct usb_xhci_s *xhci = malloc_high(sizeof(*xhci));
520 memset(xhci, 0, sizeof(*xhci));
522 wait_preempt(); // Avoid pci_config_readl when preempting
523 xhci->baseaddr = pci_config_readl(pci->bdf, PCI_BASE_ADDRESS_0)
524 & PCI_BASE_ADDRESS_MEM_MASK;
525 xhci->caps = (void*)(xhci->baseaddr);
526 xhci->op = (void*)(xhci->baseaddr + readb(&xhci->caps->caplength));
527 xhci->pr = (void*)(xhci->baseaddr + readb(&xhci->caps->caplength) + 0x400);
528 xhci->db = (void*)(xhci->baseaddr + readl(&xhci->caps->dboff));
529 xhci->ir = (void*)(xhci->baseaddr + readl(&xhci->caps->rtsoff) + 0x20);
531 u32 hcs1 = readl(&xhci->caps->hcsparams1);
532 u32 hcc = readl(&xhci->caps->hccparams);
533 xhci->ports = (hcs1 >> 24) & 0xff;
534 xhci->slots = hcs1 & 0xff;
535 xhci->xcap = ((hcc >> 16) & 0xffff) << 2;
536 xhci->context64 = (hcc & 0x04) ? 1 : 0;
539 xhci->usb.type = USB_TYPE_XHCI;
541 dprintf(1, "XHCI init on dev %02x:%02x.%x: regs @ %p, %d ports, %d slots"
542 ", %d byte contexts\n"
543 , pci_bdf_to_bus(pci->bdf), pci_bdf_to_dev(pci->bdf)
544 , pci_bdf_to_fn(pci->bdf), xhci->caps
545 , xhci->ports, xhci->slots, xhci->context64 ? 64 : 32);
548 u32 off, addr = xhci->baseaddr + xhci->xcap;
550 struct xhci_xcap *xcap = (void*)addr;
551 u32 ports, name, cap = readl(&xcap->cap);
552 switch (cap & 0xff) {
554 name = readl(&xcap->data[0]);
555 ports = readl(&xcap->data[1]);
556 dprintf(1, "XHCI protocol %c%c%c%c %x.%02x"
557 ", %d ports (offset %d), def %x\n"
560 , (name >> 16) & 0xff
561 , (name >> 24) & 0xff
564 , (ports >> 8) & 0xff
565 , (ports >> 0) & 0xff
569 dprintf(1, "XHCI extcap 0x%x @ %x\n", cap & 0xff, addr);
572 off = (cap >> 8) & 0xff;
577 u32 pagesize = readl(&xhci->op->pagesize);
578 if (PAGE_SIZE != (pagesize<<12)) {
579 dprintf(1, "XHCI driver does not support page size code %d\n"
585 pci_config_maskw(pci->bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
587 run_thread(configure_xhci, xhci);
593 if (! CONFIG_USB_XHCI)
595 struct pci_device *pci;
597 if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_XHCI)
598 xhci_controller_setup(pci);
603 /****************************************************************
604 * End point communication
605 ****************************************************************/
607 static void xhci_doorbell(struct usb_xhci_s *xhci, u32 slotid, u32 value)
609 struct xhci_db *db = xhci->db;
610 void *addr = &db[slotid].doorbell;
614 static void xhci_process_events(struct usb_xhci_s *xhci)
616 struct xhci_ring *evts = xhci->evts;
619 /* check for event */
620 u32 nidx = evts->nidx;
622 struct xhci_trb *etrb = evts->ring + nidx;
623 u32 control = etrb->control;
624 if ((control & TRB_C) != (cs ? 1 : 0))
628 u32 evt_type = TRB_TYPE(control);
629 u32 evt_cc = (etrb->status >> 24) & 0xff;
632 case ER_COMMAND_COMPLETE:
634 struct xhci_trb *rtrb = (void*)etrb->ptr_low;
635 struct xhci_ring *ring = XHCI_RING(rtrb);
636 struct xhci_trb *evt = &ring->evt;
637 u32 eidx = rtrb - ring->ring + 1;
638 dprintf(5, "%s: ring %p [trb %p, evt %p, type %d, eidx %d, cc %d]\n",
639 __func__, ring, rtrb, evt, evt_type, eidx, evt_cc);
640 memcpy(evt, etrb, sizeof(*etrb));
644 case ER_PORT_STATUS_CHANGE:
646 u32 portid = (etrb->ptr_low >> 24) & 0xff;
647 dprintf(3, "%s: status change port #%d\n",
652 dprintf(1, "%s: unknown event, type %d, cc %d\n",
653 __func__, evt_type, evt_cc);
657 /* move ring index, notify xhci */
659 if (nidx == XHCI_RING_ITEMS) {
665 struct xhci_ir *ir = xhci->ir;
666 u32 erdp = (u32)(evts->ring + nidx);
667 writel(&ir->erdp_low, erdp);
668 writel(&ir->erdp_high, 0);
672 static int xhci_ring_busy(struct xhci_ring *ring)
674 u32 eidx = ring->eidx;
675 u32 nidx = ring->nidx;
676 return (eidx != nidx);
679 static int xhci_event_wait(struct usb_xhci_s *xhci,
680 struct xhci_ring *ring,
683 u32 end = timer_calc(timeout);
686 xhci_process_events(xhci);
687 if (!xhci_ring_busy(ring)) {
688 u32 status = ring->evt.status;
689 return (status >> 24) & 0xff;
691 if (timer_check(end)) {
699 static void xhci_trb_queue(struct xhci_ring *ring,
700 struct xhci_trb *trb)
702 u32 nidx = ring->nidx;
704 struct xhci_trb *dst;
707 if (nidx == XHCI_RING_ITEMS-1) {
708 dst = ring->ring + nidx;
709 control = (TR_LINK << 10); // trb type
710 control |= TRB_LK_TC;
711 control |= (cs ? TRB_C : 0);
712 dst->ptr_low = (u32)&ring[0];
715 dst->control = control;
721 dprintf(5, "%s: ring %p [linked]\n", __func__, ring);
724 dst = ring->ring + nidx;
725 control = trb->control | (cs ? TRB_C : 0);
727 dst->ptr_low = trb->ptr_low;
728 dst->ptr_high = trb->ptr_high;
729 dst->status = trb->status;
730 dst->control = control;
734 dprintf(5, "%s: ring %p [nidx %d, len %d]\n",
735 __func__, ring, nidx,
736 trb->status & 0xffff);
739 static int xhci_cmd_submit(struct usb_xhci_s *xhci,
740 struct xhci_trb *cmd)
744 mutex_lock(&xhci->cmds->lock);
745 xhci_trb_queue(xhci->cmds, cmd);
746 xhci_doorbell(xhci, 0, 0);
747 rc = xhci_event_wait(xhci, xhci->cmds, 1000);
748 mutex_unlock(&xhci->cmds->lock);
752 static int xhci_cmd_enable_slot(struct usb_xhci_s *xhci)
754 struct xhci_trb cmd = {
758 .control = (CR_ENABLE_SLOT << 10)
760 dprintf(3, "%s:\n", __func__);
761 int cc = xhci_cmd_submit(xhci, &cmd);
762 if (cc != CC_SUCCESS)
764 return (xhci->cmds->evt.control >> 24) & 0xff;
768 static int xhci_cmd_disable_slot(struct usb_xhci_s *xhci, u32 slotid)
770 struct xhci_trb cmd = {
774 .control = (slotid << 24) | (CR_DISABLE_SLOT << 10)
776 dprintf(3, "%s: slotid %d\n", __func__, slotid);
777 return xhci_cmd_submit(xhci, &cmd);
781 static int xhci_cmd_address_device(struct usb_xhci_s *xhci, u32 slotid
782 , struct xhci_inctx *inctx)
784 struct xhci_trb cmd = {
785 .ptr_low = (u32)inctx,
788 .control = (slotid << 24) | (CR_ADDRESS_DEVICE << 10)
790 dprintf(3, "%s: slotid %d\n", __func__, slotid);
791 return xhci_cmd_submit(xhci, &cmd);
794 static int xhci_cmd_configure_endpoint(struct usb_xhci_s *xhci, u32 slotid
795 , struct xhci_inctx *inctx)
797 struct xhci_trb cmd = {
798 .ptr_low = (u32)inctx,
801 .control = (slotid << 24) | (CR_CONFIGURE_ENDPOINT << 10)
803 dprintf(3, "%s: slotid %d, add 0x%x, del 0x%x\n", __func__,
804 slotid, inctx->add, inctx->del);
805 return xhci_cmd_submit(xhci, &cmd);
808 static int xhci_cmd_evaluate_context(struct usb_xhci_s *xhci, u32 slotid
809 , struct xhci_inctx *inctx)
811 struct xhci_trb cmd = {
812 .ptr_low = (u32)inctx,
815 .control = (slotid << 24) | (CR_EVALUATE_CONTEXT << 10)
817 dprintf(3, "%s: slotid %d, add 0x%x, del 0x%x\n", __func__,
818 slotid, inctx->add, inctx->del);
819 return xhci_cmd_submit(xhci, &cmd);
822 static struct xhci_inctx *
823 xhci_alloc_inctx(struct usbdevice_s *usbdev, int maxepid)
825 struct usb_xhci_s *xhci = container_of(
826 usbdev->hub->cntl, struct usb_xhci_s, usb);
827 int size = (sizeof(struct xhci_inctx) * 33) << xhci->context64;
828 struct xhci_inctx *in = memalign_tmphigh(2048 << xhci->context64, size);
835 struct xhci_slotctx *slot = (void*)&in[1 << xhci->context64];
836 slot->ctx[0] |= maxepid << 27; // context entries
837 slot->ctx[0] |= speed_to_xhci[usbdev->speed] << 20;
839 // Set high-speed hub flags.
840 struct usbdevice_s *hubdev = usbdev->hub->usbdev;
842 if (usbdev->speed == USB_LOWSPEED || usbdev->speed == USB_FULLSPEED) {
843 struct xhci_pipe *hpipe = container_of(
844 hubdev->defpipe, struct xhci_pipe, pipe);
845 if (hubdev->speed == USB_HIGHSPEED) {
846 slot->ctx[2] |= hpipe->slotid;
847 slot->ctx[2] |= (usbdev->port+1) << 8;
849 struct xhci_slotctx *hslot = (void*)xhci->devs[hpipe->slotid].ptr_low;
850 slot->ctx[2] = hslot->ctx[2];
854 while (usbdev->hub->usbdev) {
856 route |= (usbdev->port+1) & 0xf;
857 usbdev = usbdev->hub->usbdev;
859 slot->ctx[0] |= route;
862 slot->ctx[1] |= (usbdev->port+1) << 16;
867 static int xhci_config_hub(struct usbhub_s *hub)
869 struct usb_xhci_s *xhci = container_of(
870 hub->cntl, struct usb_xhci_s, usb);
871 struct xhci_pipe *pipe = container_of(
872 hub->usbdev->defpipe, struct xhci_pipe, pipe);
873 struct xhci_slotctx *hdslot = (void*)xhci->devs[pipe->slotid].ptr_low;
874 if ((hdslot->ctx[3] >> 27) == 3)
875 // Already configured
877 struct xhci_inctx *in = xhci_alloc_inctx(hub->usbdev, 1);
881 struct xhci_slotctx *slot = (void*)&in[1 << xhci->context64];
882 slot->ctx[0] |= 1 << 26;
883 slot->ctx[1] |= hub->portcount << 24;
885 int cc = xhci_cmd_configure_endpoint(xhci, pipe->slotid, in);
887 if (cc != CC_SUCCESS) {
888 dprintf(1, "%s: configure hub: failed (cc %d)\n", __func__, cc);
894 static struct usb_pipe *
895 xhci_alloc_pipe(struct usbdevice_s *usbdev
896 , struct usb_endpoint_descriptor *epdesc)
898 u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
899 struct usb_xhci_s *xhci = container_of(
900 usbdev->hub->cntl, struct usb_xhci_s, usb);
901 struct xhci_pipe *pipe;
904 if (epdesc->bEndpointAddress == 0) {
907 epid = (epdesc->bEndpointAddress & 0x0f) * 2;
908 epid += (epdesc->bEndpointAddress & USB_DIR_IN) ? 1 : 0;
911 if (eptype == USB_ENDPOINT_XFER_CONTROL)
912 pipe = memalign_high(XHCI_RING_SIZE, sizeof(*pipe));
914 pipe = memalign_low(XHCI_RING_SIZE, sizeof(*pipe));
919 memset(pipe, 0, sizeof(*pipe));
921 usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
924 if (eptype == USB_ENDPOINT_XFER_INT)
925 pipe->buf = malloc_high(pipe->pipe.maxpacket);
927 // Allocate input context and initialize endpoint info.
928 struct xhci_inctx *in = xhci_alloc_inctx(usbdev, epid);
931 in->add = 0x01 | (1 << epid);
932 struct xhci_epctx *ep = (void*)&in[(pipe->epid+1) << xhci->context64];
933 if (eptype == USB_ENDPOINT_XFER_INT)
934 ep->ctx[0] = (usb_get_period(usbdev, epdesc) + 3) << 16;
935 ep->ctx[1] |= eptype << 3;
936 if (epdesc->bEndpointAddress & USB_DIR_IN
937 || eptype == USB_ENDPOINT_XFER_CONTROL)
938 ep->ctx[1] |= 1 << 5;
939 ep->ctx[1] |= pipe->pipe.maxpacket << 16;
940 ep->deq_low = (u32)&pipe->reqs.ring[0];
941 ep->deq_low |= 1; // dcs
942 ep->length = pipe->pipe.maxpacket;
944 dprintf(3, "%s: usbdev %p, ring %p, slotid %d, epid %d\n", __func__,
945 usbdev, &pipe->reqs, pipe->slotid, pipe->epid);
946 if (pipe->epid == 1) {
947 if (usbdev->hub->usbdev) {
948 // Make sure parent hub is configured.
949 int ret = xhci_config_hub(usbdev->hub);
954 u32 size = (sizeof(struct xhci_slotctx) * 32) << xhci->context64;
955 struct xhci_slotctx *dev = memalign_high(1024 << xhci->context64, size);
960 int slotid = xhci_cmd_enable_slot(xhci);
962 dprintf(1, "%s: enable slot: failed\n", __func__);
966 dprintf(3, "%s: enable slot: got slotid %d\n", __func__, slotid);
967 memset(dev, 0, size);
968 pipe->slotid = usbdev->slotid = slotid;
969 xhci->devs[slotid].ptr_low = (u32)dev;
970 xhci->devs[slotid].ptr_high = 0;
972 // Send set_address command.
973 int cc = xhci_cmd_address_device(xhci, slotid, in);
974 if (cc != CC_SUCCESS) {
975 dprintf(1, "%s: address device: failed (cc %d)\n", __func__, cc);
979 pipe->slotid = usbdev->slotid;
980 // Send configure command.
981 int cc = xhci_cmd_configure_endpoint(xhci, pipe->slotid, in);
982 if (cc != CC_SUCCESS) {
983 dprintf(1, "%s: configure endpoint: failed (cc %d)\n", __func__, cc);
997 xhci_realloc_pipe(struct usbdevice_s *usbdev, struct usb_pipe *upipe
998 , struct usb_endpoint_descriptor *epdesc)
1000 if (!CONFIG_USB_XHCI)
1003 usb_add_freelist(upipe);
1007 return xhci_alloc_pipe(usbdev, epdesc);
1008 u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
1009 int oldmaxpacket = upipe->maxpacket;
1010 usb_desc2pipe(upipe, usbdev, epdesc);
1011 struct xhci_pipe *pipe = container_of(upipe, struct xhci_pipe, pipe);
1012 struct usb_xhci_s *xhci = container_of(
1013 pipe->pipe.cntl, struct usb_xhci_s, usb);
1014 dprintf(3, "%s: usbdev %p, ring %p, slotid %d, epid %d\n", __func__,
1015 usbdev, &pipe->reqs, pipe->slotid, pipe->epid);
1016 if (eptype != USB_ENDPOINT_XFER_CONTROL || upipe->maxpacket == oldmaxpacket)
1019 // maxpacket has changed on control endpoint - update controller.
1020 dprintf(1, "%s: reconf ctl endpoint pkt size: %d -> %d\n",
1021 __func__, oldmaxpacket, pipe->pipe.maxpacket);
1022 struct xhci_inctx *in = xhci_alloc_inctx(usbdev, 1);
1026 struct xhci_epctx *ep = (void*)&in[2 << xhci->context64];
1027 ep->ctx[1] |= (pipe->pipe.maxpacket << 16);
1028 int cc = xhci_cmd_evaluate_context(xhci, pipe->slotid, in);
1029 if (cc != CC_SUCCESS) {
1030 dprintf(1, "%s: reconf ctl endpoint: failed (cc %d)\n",
1038 static void xhci_xfer_queue(struct xhci_pipe *pipe,
1039 void *data, int datalen, u32 flags)
1041 struct xhci_trb trb;
1042 memset(&trb, 0, sizeof(trb));
1043 if (flags & TRB_TR_IDT)
1044 memcpy(&trb.ptr_low, data, datalen);
1046 trb.ptr_low = (u32)data;
1047 trb.status = datalen;
1048 trb.control = flags;
1049 xhci_trb_queue(&pipe->reqs, &trb);
1052 static void xhci_xfer_kick(struct xhci_pipe *pipe)
1054 struct usb_xhci_s *xhci = container_of(
1055 pipe->pipe.cntl, struct usb_xhci_s, usb);
1056 u32 slotid = pipe->slotid;
1057 u32 epid = pipe->epid;
1059 dprintf(5, "%s: ring %p, slotid %d, epid %d\n",
1060 __func__, &pipe->reqs, slotid, epid);
1061 xhci_doorbell(xhci, slotid, epid);
1064 static void xhci_xfer_normal(struct xhci_pipe *pipe,
1065 void *data, int datalen)
1067 xhci_xfer_queue(pipe, data, datalen, (TR_NORMAL << 10) | TRB_TR_IOC);
1068 xhci_xfer_kick(pipe);
1072 xhci_send_pipe(struct usb_pipe *p, int dir, const void *cmd
1073 , void *data, int datalen)
1075 if (!CONFIG_USB_XHCI)
1077 struct xhci_pipe *pipe = container_of(p, struct xhci_pipe, pipe);
1078 struct usb_xhci_s *xhci = container_of(
1079 pipe->pipe.cntl, struct usb_xhci_s, usb);
1082 const struct usb_ctrlrequest *req = cmd;
1083 if (req->bRequest == USB_REQ_SET_ADDRESS)
1084 // Set address command sent during xhci_alloc_pipe.
1087 xhci_xfer_queue(pipe, (void*)req, USB_CONTROL_SETUP_SIZE
1088 , (TR_SETUP << 10) | TRB_TR_IDT
1089 | ((datalen ? (dir ? 3 : 2) : 0) << 16));
1091 xhci_xfer_queue(pipe, data, datalen, (TR_DATA << 10)
1092 | ((dir ? 1 : 0) << 16));
1093 xhci_xfer_queue(pipe, NULL, 0, (TR_STATUS << 10) | TRB_TR_IOC
1094 | ((dir ? 0 : 1) << 16));
1095 xhci_xfer_kick(pipe);
1097 xhci_xfer_normal(pipe, data, datalen);
1100 int cc = xhci_event_wait(xhci, &pipe->reqs, usb_xfer_time(p, datalen));
1101 if (cc != CC_SUCCESS) {
1102 dprintf(1, "%s: xfer failed (cc %d)\n", __func__, cc);
1110 xhci_poll_intr(struct usb_pipe *p, void *data)
1112 if (!CONFIG_USB_XHCI)
1115 struct xhci_pipe *pipe = container_of(p, struct xhci_pipe, pipe);
1116 struct usb_xhci_s *xhci = container_of(
1117 pipe->pipe.cntl, struct usb_xhci_s, usb);
1118 u32 len = pipe->pipe.maxpacket;
1119 void *buf = pipe->buf;
1120 int bufused = pipe->bufused;
1123 xhci_xfer_normal(pipe, buf, len);
1125 pipe->bufused = bufused;
1129 xhci_process_events(xhci);
1130 if (xhci_ring_busy(&pipe->reqs))
1132 dprintf(5, "%s: st %x ct %x [ %p <= %p / %d ]\n", __func__,
1133 pipe->reqs.evt.status,
1134 pipe->reqs.evt.control,
1136 memcpy(data, buf, len);
1137 xhci_xfer_normal(pipe, buf, len);