/* xHCI (USB 3.0) host controller driver. * * Scope: one controller, USB2 and USB3 root ports and devices plugged * directly into them. Control transfers, one interrupt-IN endpoint per HID * interface and the bulk endpoints used by ASIX and usbmsc are implemented. * SuperSpeed devices use a 512-byte EP0 and their endpoint descriptors may * carry a companion descriptor, which is ignored because only the bulk/HID * endpoint itself is needed. * * The schedule is polled, like the rest of the kernel. Rings and contexts * are page-allocated so the driver does not grow the kernel BSS. */ #include "usb.h" #include "mm.h" #include "paging.h" #include "pci.h" #include "serial.h" #include "spinlock.h" #include "timer.h" #include "usb_hcd.h" #include "xhci.h" #define XHCI_DEBUG 0 /* ---- controller limits ------------------------------------------------- */ #define XHCI_MAX_SLOTS 4 #define XHCI_MAX_PORTS 32 #define XHCI_EVT_TRBS 256 #define XHCI_CMD_TRBS 32 #define XHCI_EP0_TRBS 16 #define XHCI_INT_TRBS 16 #define XHCI_BULK_TRBS 32 /* ---- capability / operational / runtime offsets (bytes) --------------- */ #define XHCI_CAP_HCSPARAMS1 0x04 #define XHCI_CAP_HCSPARAMS2 0x08 #define XHCI_CAP_HCCPARAMS1 0x10 #define XHCI_CAP_DBOFF 0x14 #define XHCI_CAP_RTSOFF 0x18 #define XHCI_OP_USBCMD 0x00 #define XHCI_OP_USBSTS 0x04 #define XHCI_OP_PAGESIZE 0x08 #define XHCI_OP_CRCR 0x18 #define XHCI_OP_DCBAAP 0x30 #define XHCI_OP_CONFIG 0x38 #define XHCI_OP_PORTSC 0x400 #define XHCI_RT_IMAN 0x20 #define XHCI_RT_IMOD 0x24 #define XHCI_RT_ERSTSZ 0x28 #define XHCI_RT_ERSTBA 0x30 #define XHCI_RT_ERDP 0x38 /* USBCMD / USBSTS */ #define XHCI_CMD_RS 0x0001u #define XHCI_CMD_HCRST 0x0002u #define XHCI_STS_HCH 0x0001u #define XHCI_STS_CNR 0x0800u #define XHCI_STS_HCE 0x1000u /* PORTSC */ #define PORTSC_CCS 0x00000001u #define PORTSC_PED 0x00000002u #define PORTSC_PR 0x00000010u #define PORTSC_PP 0x00000200u #define PORTSC_SPEED_SHIFT 10 #define PORTSC_SPEED_MASK 0xFu #define PORTSC_CSC 0x00020000u #define PORTSC_PEC 0x00040000u #define PORTSC_WRC 0x00080000u #define PORTSC_OCC 0x00100000u #define PORTSC_PRC 0x00200000u #define PORTSC_PLC 0x00400000u #define PORTSC_CEC 0x00800000u #define PORTSC_RWC (PORTSC_CSC | PORTSC_PEC | PORTSC_WRC | \ PORTSC_OCC | PORTSC_PRC | PORTSC_PLC | \ PORTSC_CEC | PORTSC_PED) /* Bits that may be written back while preserving the port state: * read-only status (CCS, OCA, speed, device-removable) and the * read/write-state fields (PLS, PP, PIC, wake enables). Everything else * -- especially PR/WPR (RW1S) and PED (RW1CS) -- must be written as 0 or * the write changes the port state. Linux's * xhci_port_state_to_neutral() does the same. */ #define PORTSC_RO_MASK (PORTSC_CCS | 0x00000008u | \ (PORTSC_SPEED_MASK << PORTSC_SPEED_SHIFT) | \ 0x40000000u) #define PORTSC_RWS_MASK (0x000001E0u | PORTSC_PP | 0x0000C000u | \ 0x0E000000u) static unsigned int portsc_neutral(unsigned int ps) { return (ps & PORTSC_RO_MASK) | (ps & PORTSC_RWS_MASK); } /* TRB types */ #define TRB_NORMAL 1 #define TRB_SETUP 2 #define TRB_DATA 3 #define TRB_STATUS 4 #define TRB_LINK 6 #define TRB_ENABLE_SLOT 9 #define TRB_DISABLE_SLOT 10 #define TRB_ADDR_DEV 11 #define TRB_CONFIG_EP 12 #define TRB_EVAL_CTX 13 #define TRB_STOP_EP 15 #define TRB_TRANSFER 32 #define TRB_COMPLETION 33 /* TRB control bits */ #define TRB_CYCLE 0x00000001u #define TRB_TC 0x00000002u #define TRB_ISP 0x00000004u #define TRB_CHAIN 0x00000010u #define TRB_IOC 0x00000020u #define TRB_IDT 0x00000040u #define TRB_BSR 0x00000200u #define TRB_DIR_IN 0x00010000u #define TRB_TYPE(t) ((t) << 10) /* completion codes */ #define CC_SUCCESS 1 #define CC_SHORT_PACKET 13 /* slot/EP context flags and endpoint types */ #define SLOT_FLAG 0x00000001u #define EP0_FLAG 0x00000002u #define EP_CTRL 4 #define EP_BULK_OUT 2 #define EP_BULK_IN 6 #define EP_INT_IN 7 struct xhci_trb { unsigned int p0; unsigned int p1; unsigned int status; unsigned int control; }; struct xhci_slot { int used; unsigned int slot_id; unsigned int addr; /* logical address used by usb.c */ unsigned int dev_addr; /* address assigned by the xHC */ unsigned int port; unsigned int speed; unsigned int dc; /* device context page */ unsigned int ep0; /* EP0 transfer ring page */ unsigned int ep0_enq; unsigned int ep0_cycle; unsigned int ep0_mps; /* max packet size programmed into EP0 */ unsigned int ep_mask; /* configured endpoint flags */ }; struct xhci_int { int active; unsigned int slot; /* hardware slot id */ unsigned int ep_id; /* xHCI endpoint id (2n+dir) */ unsigned int mps; unsigned int ring; unsigned int enq; unsigned int cycle; unsigned int trb_len; unsigned int pending_len; int pending; int error; unsigned char buf[8] __attribute__((aligned(16))); }; struct xhci_bulk { int active; int armed; unsigned int slot; unsigned int ep; /* endpoint number */ unsigned int ep_id; unsigned int mps; unsigned int ring; unsigned int enq; unsigned int cycle; unsigned int buf; unsigned int len; unsigned int trb_len; unsigned int actual; int pending; int error; }; static unsigned int mmio_base; /* kernel mapping of BAR0 */ static unsigned int mmio_phys; /* BAR0 itself (logged, reported) */ static unsigned int cap_len; static unsigned int rt_off; static unsigned int db_off; static unsigned int max_slots; static unsigned int max_ports; static unsigned int ctx_size; static unsigned int port_mask; static unsigned int hcc_params; static unsigned int pci_bus, pci_dev, pci_func; static unsigned int pci_id; static unsigned int hci_version; static int found; static int last_port = -1; static unsigned int dcbaa; static unsigned int input_ctx; static unsigned int cmd_ring; static unsigned int evt_ring; static unsigned int erst; static unsigned int cmd_enq; static unsigned int cmd_cycle; static unsigned int evt_idx; static unsigned int evt_cycle; static unsigned int cmd_busy; static unsigned int cmd_done; static unsigned int cmd_status; static unsigned int cmd_slot; static unsigned int cmd_trb_addr; static unsigned int ctl_busy; static unsigned int ctl_done; static unsigned int ctl_slot; static unsigned int ctl_status; static unsigned int ctl_req_len; static unsigned int ctl_actual; static unsigned int ctl_data_seen; static unsigned int ctl_setup_trb; static unsigned int ctl_data_trb; static unsigned int ctl_status_trb; static struct xhci_bulk bulk_in; static struct xhci_bulk bulk_out; static unsigned int out_busy; static unsigned int out_done; static unsigned int out_status; static unsigned int out_slot; static unsigned int out_trb; static struct xhci_slot slots[XHCI_MAX_SLOTS + 1]; static unsigned int addr_slot[128]; static unsigned int port_slot[XHCI_MAX_PORTS]; static struct xhci_int ints[USB_HCD_INT_SLOTS]; static spinlock_t xhci_lock; /* ---- debug ------------------------------------------------------------- */ #if XHCI_DEBUG static void dbg(const char *s) { serial_puts(SERIAL_COM1, s); } static void dbg_hex(const char *s, unsigned int v) { serial_puts(SERIAL_COM1, s); serial_puthex(SERIAL_COM1, v); serial_putc(SERIAL_COM1, '\n'); } static void dbg_dec(const char *s, unsigned int v) { serial_puts(SERIAL_COM1, s); serial_putdec(SERIAL_COM1, v); serial_putc(SERIAL_COM1, '\n'); } #else static void dbg(const char *s) { (void)s; } static void dbg_hex(const char *s, unsigned int v) { (void)s; (void)v; } static void dbg_dec(const char *s, unsigned int v) { (void)s; (void)v; } #endif /* Always-on one-line diagnostics (COM1, mirrored to netmon). Only used on * the init line and on failures, so a working machine stays quiet. */ static void xlog_hex(const char *s, unsigned int v) { serial_puts(SERIAL_COM1, s); serial_puthex(SERIAL_COM1, v); } static void delay_ms(unsigned int ms) { usb_delay_ms(ms); /* sleeps inside a sleepable scan */ } /* ---- MMIO helpers ------------------------------------------------------ */ static unsigned int mmio_rd(unsigned int off) { return *(volatile unsigned int *)(mmio_base + off); } static void mmio_wr(unsigned int off, unsigned int val) { *(volatile unsigned int *)(mmio_base + off) = val; } static void mmio_wr64(unsigned int off, unsigned int lo) { mmio_wr(off, lo); mmio_wr(off + 4, 0); } /* Normal RAM (DCBAA, ERST, scratchpad array) is not MMIO. */ static void ram_wr64(unsigned int addr, unsigned int lo) { *(volatile unsigned int *)addr = lo; *(volatile unsigned int *)(addr + 4) = 0; } static void zero_page(unsigned int addr, unsigned int len) { unsigned int *p = (unsigned int *)addr; while (len >= 4) { *p++ = 0; len -= 4; } } static unsigned int op(unsigned int reg) { return cap_len + reg; } static unsigned int rt(unsigned int reg) { return rt_off + reg; } /* ---- rings ------------------------------------------------------------- */ static void ring_init(unsigned int base, unsigned int size, unsigned int *enq, unsigned int *cycle) { struct xhci_trb *link = (struct xhci_trb *)(base + (size - 1) * 16); link->p0 = base; link->p1 = 0; link->status = 0; link->control = TRB_TYPE(TRB_LINK) | TRB_TC; *enq = 0; *cycle = 1; } static void ring_queue(unsigned int base, unsigned int size, unsigned int *enq, unsigned int *cycle, unsigned int p0, unsigned int p1, unsigned int status, unsigned int control) { struct xhci_trb *trb = (struct xhci_trb *)(base + *enq * 16); trb->p0 = p0; trb->p1 = p1; trb->status = status; __asm__ volatile ("" ::: "memory"); trb->control = control | *cycle; __asm__ volatile ("" ::: "memory"); (*enq)++; if (*enq == size - 1) { struct xhci_trb *link = (struct xhci_trb *)(base + (size - 1) * 16); __asm__ volatile ("" ::: "memory"); link->control ^= TRB_CYCLE; *enq = 0; *cycle ^= 1; } } /* ---- event handling ---------------------------------------------------- */ static void xhci_poll_locked(void); static void stop_int(unsigned int slot); static void bulk_reset(struct xhci_bulk *b); static void xhci_int_event(unsigned int slot, unsigned int ep, unsigned int code, unsigned int residual) { unsigned int i; for (i = 0; i < USB_HCD_INT_SLOTS; i++) { if (!ints[i].active || ints[i].slot != slot || ints[i].ep_id != ep) continue; if (!ints[i].pending) { if (code == CC_SUCCESS || code == CC_SHORT_PACKET) { ints[i].pending_len = (ints[i].trb_len >= residual) ? ints[i].trb_len - residual : 0; ints[i].pending = 1; } else { ints[i].pending = 1; ints[i].error = 1; } } break; } } static void xhci_poll_locked(void) { unsigned int n = 0; while (n++ < 512) { struct xhci_trb *ev = (struct xhci_trb *)(evt_ring + evt_idx * 16); unsigned int type; if ((ev->control & TRB_CYCLE) != evt_cycle) break; type = (ev->control >> 10) & 0x3F; if (type == TRB_COMPLETION) { unsigned int code = (ev->status >> 24) & 0xFF; unsigned int slot = (ev->control >> 24) & 0xFF; if (cmd_busy && ev->p0 == cmd_trb_addr) { cmd_status = code; cmd_slot = slot; cmd_done = 1; } } else if (type == TRB_TRANSFER) { unsigned int code = (ev->status >> 24) & 0xFF; unsigned int residual = ev->status & 0xFFFFFF; unsigned int slot = (ev->control >> 24) & 0xFF; unsigned int ep = (ev->control >> 16) & 0x1F; if (ctl_busy && slot == ctl_slot && ep == 1) { if (ev->p0 == ctl_setup_trb) { /* The setup stage is its own TD and * completes first; only an error stops * the transfer. */ if (code != CC_SUCCESS && code != CC_SHORT_PACKET) { ctl_status = code; ctl_done = 1; } } else if (ev->p0 == ctl_data_trb) { ctl_actual = (ctl_req_len >= residual) ? ctl_req_len - residual : 0; ctl_data_seen = 1; if (code != CC_SUCCESS && code != CC_SHORT_PACKET) { ctl_status = code; ctl_done = 1; } } else if (ev->p0 == ctl_status_trb) { if (!ctl_data_seen) ctl_actual = (code == CC_SUCCESS) ? ctl_req_len : 0; ctl_status = code; ctl_done = 1; } } else if (out_busy && slot == out_slot && ep == bulk_out.ep_id && ev->p0 == out_trb) { out_status = code; out_done = 1; } else if (bulk_in.active && slot == bulk_in.slot && ep == bulk_in.ep_id) { bulk_in.armed = 0; if (!bulk_in.pending) { if (code == CC_SUCCESS || code == CC_SHORT_PACKET) { bulk_in.actual = (bulk_in.trb_len >= residual) ? bulk_in.trb_len - residual : 0; bulk_in.pending = 1; } else { bulk_in.pending = 1; bulk_in.error = 1; } } } else { xhci_int_event(slot, ep, code, residual); } } evt_idx++; if (evt_idx >= XHCI_EVT_TRBS) { evt_idx = 0; evt_cycle ^= 1; } } mmio_wr64(rt(XHCI_RT_ERDP), (evt_ring + evt_idx * 16) | 8); } /* ---- commands ---------------------------------------------------------- */ static int xhci_cmd(unsigned int p0, unsigned int p1, unsigned int control, unsigned int *out_slot) { unsigned int flags, t0, rc; if (!found) return -1; spin_lock_irqsave(&xhci_lock, &flags); if (cmd_busy) { spin_unlock_irqrestore(&xhci_lock, flags); return -1; } cmd_busy = 1; cmd_done = 0; cmd_status = 0; cmd_slot = 0; cmd_trb_addr = cmd_ring + cmd_enq * 16; ring_queue(cmd_ring, XHCI_CMD_TRBS, &cmd_enq, &cmd_cycle, p0, p1, 0, control); __asm__ volatile ("" ::: "memory"); mmio_wr(db_off, 0); spin_unlock_irqrestore(&xhci_lock, flags); t0 = get_ticks_ms(); while (get_ticks_ms() - t0 < 1000) { spin_lock_irqsave(&xhci_lock, &flags); xhci_poll_locked(); rc = cmd_done; spin_unlock_irqrestore(&xhci_lock, flags); if (rc) break; usb_poll_relax(t0); __asm__ volatile ("pause"); } spin_lock_irqsave(&xhci_lock, &flags); cmd_busy = 0; if (!cmd_done) { spin_unlock_irqrestore(&xhci_lock, flags); dbg("xhci: command timeout\n"); return -1; } if (out_slot) *out_slot = cmd_slot; rc = cmd_status; spin_unlock_irqrestore(&xhci_lock, flags); if (rc != CC_SUCCESS) { dbg_hex("xhci: command status ", rc); return -1; } return 0; } /* ---- contexts ---------------------------------------------------------- */ static void fill_slot_ctx(unsigned int addr, unsigned int port, unsigned int speed, unsigned int last_ctx) { unsigned int *p = (unsigned int *)(input_ctx + ctx_size); p[0] = (speed << 20) | (last_ctx << 27); p[1] = (port + 1) << 16; p[2] = 0; p[3] = addr; } static void fill_ep0_ctx(unsigned int mps, unsigned int ring, unsigned int enq, unsigned int cycle) { unsigned int *p = (unsigned int *)(input_ctx + 2 * ctx_size); p[0] = 0; p[1] = (EP_CTRL << 3) | (3 << 1) | (mps << 16); p[2] = (ring + enq * 16) | cycle; p[3] = 0; p[4] = 8; } static void fill_ep_ctx(unsigned int ep_index, unsigned int type, unsigned int mps, unsigned int interval, unsigned int ring) { unsigned int *p = (unsigned int *)(input_ctx + (ep_index + 2) * ctx_size); p[0] = interval << 16; p[1] = (type << 3) | (3 << 1) | (mps << 16); p[2] = ring | 1; p[3] = 0; p[4] = mps; } static struct xhci_slot *slot_by_addr(unsigned int addr) { unsigned int id; if (addr > 127) return 0; id = addr_slot[addr]; if (id < 1 || id > max_slots || !slots[id].used) return 0; return &slots[id]; } static unsigned int port_speed(unsigned int port) { unsigned int ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10); unsigned int sp = (ps >> PORTSC_SPEED_SHIFT) & PORTSC_SPEED_MASK; return sp ? sp : 1; } /* Default control-pipe max packet size for the BSR Address Device command. * High speed must be 64; full speed uses the USB core's 64-byte guess (the * descriptor read then corrects it); low speed is 8. Using 8 for a * high/full-speed slot leaves EP0 unusable on real xHCs. */ static unsigned int ep0_mps_for_speed(unsigned int speed) { if (speed == 2) /* low speed */ return 8; if (speed >= 4) /* SuperSpeed / SuperSpeedPlus */ return 512; return 64; /* full speed (guess) and high speed */ } /* EP0 max packet size for Address Device. Low, high and SuperSpeed have * one legal value, so the USB core's 8-byte first-read guess is replaced * by it; only full speed (8/16/32/64) takes the caller's value and is * corrected later by ep0_set_mps(). */ static unsigned int ep0_mps_initial(unsigned int speed, unsigned int mps) { if (!mps || speed != 1) return ep0_mps_for_speed(speed); return mps; } /* Evaluate Context: tell the xHC the device's real bMaxPacketSize0 once * the first descriptor read has returned it. Without this the VL805 on * the 970A-DS3P kept the 8-byte EP0 that SET_ADDRESS created, and the * 18-byte device descriptor read of a high-speed stick failed with * Babble Detected (the device answers in one 18-byte packet). */ static int ep0_set_mps(struct xhci_slot *s, unsigned int mps) { unsigned int flags; int rc; spin_lock_irqsave(&xhci_lock, &flags); zero_page(input_ctx, 4096); *(unsigned int *)input_ctx = 0; *(unsigned int *)(input_ctx + 4) = EP0_FLAG; fill_ep0_ctx(mps, s->ep0, s->ep0_enq, s->ep0_cycle); spin_unlock_irqrestore(&xhci_lock, flags); rc = xhci_cmd(input_ctx, 0, TRB_TYPE(TRB_EVAL_CTX) | (s->slot_id << 24), 0); if (rc == 0) { s->ep0_mps = mps; } else { xlog_hex("xhci: evaluate context failed slot ", s->slot_id); xlog_hex(" mps ", mps); xlog_hex(" cc ", cmd_status); serial_putc(SERIAL_COM1, '\n'); } return rc; } static int xhci_config_ep(struct xhci_slot *s, unsigned int ep_index, unsigned int type, unsigned int mps, unsigned int interval, unsigned int ring) { unsigned int ep_flag = 1u << (ep_index + 1); unsigned int last = ep_index + 1; unsigned int i, flags; int rc; for (i = 1; i < 32; i++) if (s->ep_mask & (1u << i)) last = i; spin_lock_irqsave(&xhci_lock, &flags); zero_page(input_ctx, 4096); *(unsigned int *)input_ctx = 0; *(unsigned int *)(input_ctx + 4) = SLOT_FLAG | ep_flag; fill_slot_ctx(s->dev_addr, s->port, s->speed, last); fill_ep_ctx(ep_index, type, mps, interval, ring); spin_unlock_irqrestore(&xhci_lock, flags); rc = xhci_cmd(input_ctx, 0, TRB_TYPE(TRB_CONFIG_EP) | (s->slot_id << 24), 0); if (rc == 0) s->ep_mask |= ep_flag; return rc; } /* ---- slots ------------------------------------------------------------- */ /* Create a slot. bsr=1: Address Device with Block Set Address Request, * leaving the slot in the Default state at device address 0 (new * enumeration scheme). bsr=0: the command carries target_addr and moves * Enabled -> Addressed directly (traditional scheme), which is what Linux * does when the port is addressed before the first descriptor read. * mps == 0 (or a SuperSpeed port) derives it from the port speed. */ static struct xhci_slot *new_slot_common(unsigned int target_addr, unsigned int mps, int bsr) { struct xhci_slot *s; unsigned int slot_id, dc, ep0, flags; int rc; if (last_port < 0 || last_port >= (int)max_ports) return 0; if (port_slot[last_port]) return 0; slot_id = 0; rc = xhci_cmd(0, 0, TRB_TYPE(TRB_ENABLE_SLOT), &slot_id); if (rc != 0 || slot_id == 0 || slot_id > max_slots) { dbg("xhci: enable slot failed\n"); return 0; } dc = mm_alloc_page(); ep0 = mm_alloc_page(); if (!dc || !ep0) { if (dc) mm_free_page(dc); if (ep0) mm_free_page(ep0); return 0; } zero_page(dc, 4096); zero_page(ep0, 4096); s = &slots[slot_id]; s->used = 1; s->slot_id = slot_id; s->addr = 0; s->port = (unsigned int)last_port; s->speed = port_speed((unsigned int)last_port); s->dc = dc; s->ep0 = ep0; s->ep0_enq = 0; s->ep0_cycle = 1; s->ep_mask = 0; s->dev_addr = 0; ring_init(ep0, XHCI_EP0_TRBS, &s->ep0_enq, &s->ep0_cycle); mps = ep0_mps_initial(s->speed, mps); s->ep0_mps = mps; ram_wr64(dcbaa + slot_id * 8, dc); spin_lock_irqsave(&xhci_lock, &flags); zero_page(input_ctx, 4096); *(unsigned int *)input_ctx = 0; *(unsigned int *)(input_ctx + 4) = SLOT_FLAG | EP0_FLAG; fill_slot_ctx(0, s->port, s->speed, 1); fill_ep0_ctx(mps, ep0, s->ep0_enq, s->ep0_cycle); spin_unlock_irqrestore(&xhci_lock, flags); rc = xhci_cmd(input_ctx, 0, TRB_TYPE(TRB_ADDR_DEV) | (slot_id << 24) | (bsr ? TRB_BSR : 0), 0); if (rc != 0) { xlog_hex(bsr ? "xhci: address default failed port " : "xhci: address device failed port ", s->port); xlog_hex(" spd ", s->speed); xlog_hex(" cc ", cmd_status); xlog_hex(" portsc ", mmio_rd(op(XHCI_OP_PORTSC) + s->port * 0x10)); serial_putc(SERIAL_COM1, '\n'); ram_wr64(dcbaa + slot_id * 8, 0); xhci_cmd(0, 0, TRB_TYPE(TRB_DISABLE_SLOT) | (slot_id << 24), 0); mm_free_page(dc); mm_free_page(ep0); s->used = 0; return 0; } /* The xHC assigns the USB device address (the input slot context * address is 0); keep it for later Configure Endpoint commands. */ s->dev_addr = ((unsigned int *)s->dc)[3] & 0xFF; s->addr = bsr ? 0 : target_addr; addr_slot[s->addr] = slot_id; port_slot[s->port] = slot_id; return s; } /* New-scheme slot: Enabled -> Default, at device address 0. */ static struct xhci_slot *new_slot(void) { return new_slot_common(0, 0, 1); } static void free_slot(struct xhci_slot *s) { unsigned int i, flags; if (!s || !s->used) return; xhci_cmd(0, 0, TRB_TYPE(TRB_DISABLE_SLOT) | (s->slot_id << 24), 0); ram_wr64(dcbaa + s->slot_id * 8, 0); spin_lock_irqsave(&xhci_lock, &flags); for (i = 0; i < USB_HCD_INT_SLOTS; i++) { if (ints[i].slot == s->slot_id) { ints[i].active = 0; ints[i].pending = 0; ints[i].error = 0; if (ints[i].ring) { mm_free_page(ints[i].ring); ints[i].ring = 0; } ints[i].slot = 0; } } if (s->dc) { mm_free_page(s->dc); s->dc = 0; } if (s->ep0) { mm_free_page(s->ep0); s->ep0 = 0; } if (s->addr < 128) addr_slot[s->addr] = 0; if (bulk_in.active && bulk_in.slot == s->slot_id) bulk_reset(&bulk_in); if (bulk_out.active && bulk_out.slot == s->slot_id) bulk_reset(&bulk_out); if (s->port < XHCI_MAX_PORTS) port_slot[s->port] = 0; s->used = 0; s->addr = 0; s->ep_mask = 0; spin_unlock_irqrestore(&xhci_lock, flags); } static int set_address(unsigned int new_addr, unsigned int mps) { struct xhci_slot *s = slot_by_addr(0); unsigned int flags; int rc; if (!s || new_addr == 0 || new_addr > 127) return -1; mps = ep0_mps_initial(s->speed, mps); spin_lock_irqsave(&xhci_lock, &flags); zero_page(input_ctx, 4096); *(unsigned int *)input_ctx = 0; *(unsigned int *)(input_ctx + 4) = SLOT_FLAG | EP0_FLAG; fill_slot_ctx(0, s->port, s->speed, 1); fill_ep0_ctx(mps, s->ep0, s->ep0_enq, s->ep0_cycle); spin_unlock_irqrestore(&xhci_lock, flags); rc = xhci_cmd(input_ctx, 0, TRB_TYPE(TRB_ADDR_DEV) | (s->slot_id << 24), 0); if (rc != 0) { dbg("xhci: set address failed\n"); return -1; } spin_lock_irqsave(&xhci_lock, &flags); s->dev_addr = ((unsigned int *)s->dc)[3] & 0xFF; s->ep0_mps = mps; addr_slot[0] = 0; s->addr = new_addr; addr_slot[new_addr] = s->slot_id; spin_unlock_irqrestore(&xhci_lock, flags); return 0; } /* ---- control transfers ------------------------------------------------- */ /* One line per failed control transfer: the request, the completion code * (0 = no event within the timeout), the root port's PORTSC, and the slot / * EP0 states from the output device context (slot state 2 = Addressed, * EP state 2 = Halted, 4 = Error). */ static void ctl_fail_log(const struct xhci_slot *s, const unsigned char *setup, unsigned int cc, unsigned int data_seen) { const unsigned int *dc = (const unsigned int *)s->dc; const unsigned int *ep0 = (const unsigned int *)(s->dc + ctx_size); serial_puts(SERIAL_COM1, cc ? "xhci: ctl err" : "xhci: ctl timeout"); xlog_hex(" req ", (unsigned int)setup[0] | ((unsigned int)setup[1] << 8) | ((unsigned int)setup[2] << 16) | ((unsigned int)setup[3] << 24)); xlog_hex(" cc ", cc); xlog_hex(" data ", data_seen); xlog_hex(" slot ", s->slot_id); xlog_hex(" spd ", s->speed); xlog_hex(" portsc ", mmio_rd(op(XHCI_OP_PORTSC) + s->port * 0x10)); xlog_hex(" sstate ", dc[3] >> 27); xlog_hex(" dev ", dc[3] & 0xFF); xlog_hex(" ep0 ", ep0[0] & 7); xlog_hex(" mps ", ep0[1] >> 16); xlog_hex(" sts ", mmio_rd(op(XHCI_OP_USBSTS))); serial_putc(SERIAL_COM1, '\n'); } static int control_transfer(unsigned int ctrl, unsigned int addr, unsigned int mps, int low_speed, const unsigned char *setup, unsigned char *data, unsigned int len, int dir_in) { struct xhci_slot *s; unsigned int flags, t0, rc, st; unsigned int setup_c, data_c, status_c; unsigned int last_bell; (void)ctrl; (void)low_speed; if (!found) return -1; /* xHCI owns SET_ADDRESS: it is an Address Device command, not a * control request on the wire. If a Default-state slot exists (new * scheme), move it to Addressed. Otherwise this is the traditional * scheme: create the slot Enabled and address it in one command, * without ever using the Default state. */ if (setup[0] == 0x00 && setup[1] == 0x05 && addr == 0) { unsigned int new_addr = (unsigned int)setup[2] | ((unsigned int)setup[3] << 8); s = slot_by_addr(0); if (s) return set_address(new_addr, mps); s = new_slot_common(new_addr, mps, 0); return s ? 0 : -1; } s = slot_by_addr(addr); if (s && addr == 0 && (int)s->port != last_port) { /* A failed enumeration on another port left a slot at * address 0; it must not be reused for this device. */ free_slot(s); s = 0; } if (!s) { if (addr != 0) return -1; s = new_slot(); if (!s) return -1; } /* The core learned the real bMaxPacketSize0 from the first 8 bytes * of the device descriptor; reprogram EP0 before using it. Only a * full-speed EP0 can legally differ from the speed's fixed value. */ if (s->speed == 1 && (mps == 8 || mps == 16 || mps == 32 || mps == 64) && mps != s->ep0_mps && ep0_set_mps(s, mps) != 0) return -1; spin_lock_irqsave(&xhci_lock, &flags); if (ctl_busy) { spin_unlock_irqrestore(&xhci_lock, flags); dbg("xhci: control busy\n"); return -1; } ctl_busy = 1; ctl_done = 0; ctl_status = 0; ctl_actual = 0; ctl_data_seen = 0; ctl_slot = s->slot_id; ctl_req_len = len; /* Encode the three stages the way EDK2's XhciDxe does: every stage * is its own TD (no CHAIN) and carries IOC. This Braswell xHC never * completes a chained multi-TRB control TD: with Linux's chaining * (CHAIN on setup+data, IOC on status) the data stage hangs and no * transfer event is ever posted, while the per-stage encoding * completes. */ setup_c = TRB_TYPE(TRB_SETUP) | TRB_IDT | TRB_IOC; /* Transfer Type (TRT) only exists from xHCI 1.0 on; in a 0.96 * controller (Etron EJ168, early VIA/Renesas parts) bits 17:16 are * reserved. Linux sets it only for hci_version >= 0x100 too. */ if (len && hci_version >= 0x100) setup_c |= (dir_in ? 3u : 2u) << 16; ctl_setup_trb = s->ep0 + s->ep0_enq * 16; ring_queue(s->ep0, XHCI_EP0_TRBS, &s->ep0_enq, &s->ep0_cycle, (unsigned int)setup[0] | ((unsigned int)setup[1] << 8) | ((unsigned int)setup[2] << 16) | ((unsigned int)setup[3] << 24), (unsigned int)setup[4] | ((unsigned int)setup[5] << 8) | ((unsigned int)setup[6] << 16) | ((unsigned int)setup[7] << 24), 8, setup_c); ctl_data_trb = 0; if (len) { ctl_data_trb = s->ep0 + s->ep0_enq * 16; data_c = TRB_TYPE(TRB_DATA) | TRB_IOC; if (dir_in) data_c |= TRB_DIR_IN | TRB_ISP; ring_queue(s->ep0, XHCI_EP0_TRBS, &s->ep0_enq, &s->ep0_cycle, (unsigned int)data, 0, len, data_c); } ctl_status_trb = s->ep0 + s->ep0_enq * 16; status_c = TRB_TYPE(TRB_STATUS) | TRB_IOC; if (!len || !dir_in) status_c |= TRB_DIR_IN; ring_queue(s->ep0, XHCI_EP0_TRBS, &s->ep0_enq, &s->ep0_cycle, 0, 0, 0, status_c); __asm__ volatile ("" ::: "memory"); mmio_wr(db_off + 4 * s->slot_id, 1); spin_unlock_irqrestore(&xhci_lock, flags); t0 = get_ticks_ms(); last_bell = t0; while (get_ticks_ms() - t0 < 1000) { spin_lock_irqsave(&xhci_lock, &flags); xhci_poll_locked(); rc = ctl_done; if (!rc && get_ticks_ms() - last_bell >= 100) { /* Re-ring while the endpoint is still busy; a posted * doorbell can be lost on some real xHCs. */ mmio_wr(db_off + 4 * s->slot_id, 1); last_bell = get_ticks_ms(); } spin_unlock_irqrestore(&xhci_lock, flags); if (rc) break; usb_poll_relax(t0); __asm__ volatile ("pause"); } spin_lock_irqsave(&xhci_lock, &flags); if (!ctl_done) { ctl_busy = 0; spin_unlock_irqrestore(&xhci_lock, flags); ctl_fail_log(s, setup, 0, ctl_data_seen); return -1; } rc = ctl_actual; st = ctl_status; ctl_busy = 0; spin_unlock_irqrestore(&xhci_lock, flags); if (st != CC_SUCCESS && st != CC_SHORT_PACKET) { ctl_fail_log(s, setup, st, ctl_data_seen); return -1; } return (int)rc; } /* ---- HID interrupt endpoint -------------------------------------------- */ static int int_in_start(unsigned int ctrl, unsigned int slot, unsigned int addr, unsigned int ep, unsigned int mps, int low_speed) { struct xhci_slot *s = slot_by_addr(addr); struct xhci_int *in; unsigned int ep_index, ep_id, ring, flags; int rc; (void)ctrl; (void)low_speed; if (!found || !s || slot >= USB_HCD_INT_SLOTS || ep < 1 || ep > 15) return -1; if (mps < 1) mps = 8; if (mps > 8) mps = 8; if (ints[slot].active) stop_int(slot); ep_index = (ep - 1) * 2 + 2; /* EP index 2 for EP1 IN, 4 for EP2 IN */ ep_id = 2 * ep + 1; ring = mm_alloc_page(); if (!ring) return -1; zero_page(ring, 4096); in = &ints[slot]; in->slot = s->slot_id; in->ep_id = ep_id; in->mps = mps; in->ring = ring; in->enq = 0; in->cycle = 1; in->trb_len = mps; in->pending = 0; in->error = 0; in->active = 0; ring_init(ring, XHCI_INT_TRBS, &in->enq, &in->cycle); rc = xhci_config_ep(s, ep_index, EP_INT_IN, mps, 1, ring); if (rc != 0) { dbg_hex("xhci: configure int ep failed ", ep_index); mm_free_page(ring); in->ring = 0; return -1; } in->active = 1; spin_lock_irqsave(&xhci_lock, &flags); ring_queue(ring, XHCI_INT_TRBS, &in->enq, &in->cycle, (unsigned int)in->buf, 0, in->trb_len, TRB_TYPE(TRB_NORMAL) | TRB_IOC); __asm__ volatile ("" ::: "memory"); mmio_wr(db_off + 4 * s->slot_id, ep_id); spin_unlock_irqrestore(&xhci_lock, flags); return 0; } static int int_in_poll(unsigned int slot, unsigned char *buf, unsigned int len) { struct xhci_int *in; unsigned int flags, n, i; if (!found || slot >= USB_HCD_INT_SLOTS) return 0; in = &ints[slot]; if (!in->active) return 0; spin_lock_irqsave(&xhci_lock, &flags); xhci_poll_locked(); spin_unlock_irqrestore(&xhci_lock, flags); if (!in->pending) return 0; if (in->error) { in->pending = 0; in->error = 0; return -1; } n = in->pending_len; if (n > len) n = len; for (i = 0; i < n; i++) buf[i] = in->buf[i]; in->pending = 0; spin_lock_irqsave(&xhci_lock, &flags); if (in->active) { ring_queue(in->ring, XHCI_INT_TRBS, &in->enq, &in->cycle, (unsigned int)in->buf, 0, in->trb_len, TRB_TYPE(TRB_NORMAL) | TRB_IOC); __asm__ volatile ("" ::: "memory"); mmio_wr(db_off + 4 * in->slot, in->ep_id); } spin_unlock_irqrestore(&xhci_lock, flags); return (int)in->pending_len; } static void stop_int(unsigned int slot) { struct xhci_int *in; unsigned int flags; if (slot >= USB_HCD_INT_SLOTS) return; in = &ints[slot]; spin_lock_irqsave(&xhci_lock, &flags); in->active = 0; in->pending = 0; in->error = 0; spin_unlock_irqrestore(&xhci_lock, flags); } /* ---- bulk transfers ---------------------------------------------------- */ static void bulk_reset(struct xhci_bulk *b) { if (b->ring) { mm_free_page(b->ring); b->ring = 0; } b->active = 0; b->armed = 0; b->slot = 0; b->pending = 0; b->error = 0; b->actual = 0; } static int xhci_bulk_in(unsigned int ctrl, unsigned int addr, unsigned int ep, unsigned int mps, unsigned char *buf, unsigned int len) { struct xhci_slot *s = slot_by_addr(addr); struct xhci_bulk *b = &bulk_in; unsigned int ep_index, ep_id, ring, flags, n, err; int rc; (void)ctrl; if (!found || !s || ep < 1 || ep > 15 || !buf || !len) return -1; if (mps < 1) mps = 512; ep_index = (ep - 1) * 2 + 2; ep_id = 2 * ep + 1; if (!b->active || b->slot != s->slot_id || b->ep != ep) { ring = mm_alloc_page(); if (!ring) return -1; zero_page(ring, 4096); bulk_reset(b); b->slot = s->slot_id; b->ep = ep; b->ep_id = ep_id; b->mps = mps; b->ring = ring; b->enq = 0; b->cycle = 1; ring_init(ring, XHCI_BULK_TRBS, &b->enq, &b->cycle); rc = xhci_config_ep(s, ep_index, EP_BULK_IN, mps, 0, ring); if (rc != 0) { bulk_reset(b); return -1; } b->active = 1; } b->buf = (unsigned int)buf; b->len = len; spin_lock_irqsave(&xhci_lock, &flags); xhci_poll_locked(); if (b->pending) { n = b->actual; err = (unsigned int)b->error; b->pending = 0; b->error = 0; /* Do not re-arm here: the next call may use a different * buffer/length (the mass-storage driver alternates CBW, * data and CSW buffers). The caller's next usb_bulk_in() * arms the new transfer with its own buffer. */ b->armed = 0; spin_unlock_irqrestore(&xhci_lock, flags); return err ? -1 : (int)n; } if (!b->armed) { b->trb_len = len; ring_queue(b->ring, XHCI_BULK_TRBS, &b->enq, &b->cycle, (unsigned int)buf, 0, len, TRB_TYPE(TRB_NORMAL) | TRB_IOC); b->armed = 1; __asm__ volatile ("" ::: "memory"); mmio_wr(db_off + 4 * b->slot, b->ep_id); } spin_unlock_irqrestore(&xhci_lock, flags); return 0; } /* Drop a bulk transfer after a timeout or a BOT reset. Stop the endpoint * first so the xHC no longer references the old ring page, then clear the * software state; the next bulk call configures a fresh ring. */ static void xhci_bulk_reset(unsigned int ctrl, unsigned int addr, unsigned int ep, int dir_in) { struct xhci_slot *s = slot_by_addr(addr); struct xhci_bulk *b = dir_in ? &bulk_in : &bulk_out; unsigned int flags, slot, ep_id; (void)ctrl; if (!found || !s) return; spin_lock_irqsave(&xhci_lock, &flags); if (!b->active || b->slot != s->slot_id || b->ep != ep) { spin_unlock_irqrestore(&xhci_lock, flags); return; } slot = b->slot; ep_id = b->ep_id; spin_unlock_irqrestore(&xhci_lock, flags); xhci_cmd(0, 0, TRB_TYPE(TRB_STOP_EP) | (slot << 24) | (ep_id << 16), 0); spin_lock_irqsave(&xhci_lock, &flags); if (b->active && b->slot == slot && b->ep == ep) bulk_reset(b); spin_unlock_irqrestore(&xhci_lock, flags); } static int xhci_bulk_out(unsigned int ctrl, unsigned int addr, unsigned int ep, unsigned int mps, const unsigned char *buf, unsigned int len) { struct xhci_slot *s = slot_by_addr(addr); struct xhci_bulk *b = &bulk_out; unsigned int ep_index, ep_id, ring, flags, t0, done, status; int rc; (void)ctrl; if (!found || !s || ep < 1 || ep > 15 || !buf || !len) return -1; if (mps < 1) mps = 512; ep_index = (ep - 1) * 2 + 1; ep_id = 2 * ep; if (!b->active || b->slot != s->slot_id || b->ep != ep) { ring = mm_alloc_page(); if (!ring) return -1; zero_page(ring, 4096); bulk_reset(b); b->slot = s->slot_id; b->ep = ep; b->ep_id = ep_id; b->mps = mps; b->ring = ring; b->enq = 0; b->cycle = 1; ring_init(ring, XHCI_BULK_TRBS, &b->enq, &b->cycle); rc = xhci_config_ep(s, ep_index, EP_BULK_OUT, mps, 0, ring); if (rc != 0) { bulk_reset(b); return -1; } b->active = 1; } spin_lock_irqsave(&xhci_lock, &flags); if (out_busy) { spin_unlock_irqrestore(&xhci_lock, flags); return -1; } out_busy = 1; out_done = 0; out_status = 0; out_slot = s->slot_id; out_trb = b->ring + b->enq * 16; b->trb_len = len; ring_queue(b->ring, XHCI_BULK_TRBS, &b->enq, &b->cycle, (unsigned int)buf, 0, len, TRB_TYPE(TRB_NORMAL) | TRB_IOC); __asm__ volatile ("" ::: "memory"); mmio_wr(db_off + 4 * s->slot_id, ep_id); spin_unlock_irqrestore(&xhci_lock, flags); t0 = get_ticks_ms(); while (get_ticks_ms() - t0 < 1000) { spin_lock_irqsave(&xhci_lock, &flags); xhci_poll_locked(); done = out_done; spin_unlock_irqrestore(&xhci_lock, flags); if (done) break; usb_poll_relax(t0); __asm__ volatile ("pause"); } spin_lock_irqsave(&xhci_lock, &flags); done = out_done; status = out_status; out_busy = 0; spin_unlock_irqrestore(&xhci_lock, flags); if (!done) { dbg("xhci: bulk out timeout\n"); return -1; } if (status != CC_SUCCESS) { dbg_hex("xhci: bulk out status ", status); return -1; } return 0; } /* ---- root hub ports ---------------------------------------------------- */ static int port_reset(unsigned int ctrl, unsigned int port) { unsigned int ps, t0; (void)ctrl; if (!found || port >= max_ports || !(port_mask & (1u << port))) return -1; if (port_slot[port]) free_slot(&slots[port_slot[port]]); ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10); if (!(ps & PORTSC_CCS)) return -1; /* Force the port into the Disabled state first: some xHCs ignore * PR while the port is already enabled, leaving the device at its * firmware-assigned address instead of address 0. USB2 ports only: * on a SuperSpeed port PED=1 puts the link into SS.Disabled, which a * hot reset (PR) does not leave -- software would have to write * PLS=RxDetect -- so the BIOS-booted stick stayed unreachable. */ if ((ps & PORTSC_PED) && ((ps >> PORTSC_SPEED_SHIFT) & PORTSC_SPEED_MASK) < 4) { mmio_wr(op(XHCI_OP_PORTSC) + port * 0x10, portsc_neutral(ps) | PORTSC_PED); t0 = get_ticks_ms(); while (get_ticks_ms() - t0 < 100) { ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10); if (!(ps & PORTSC_PED)) break; usb_poll_relax(t0); } } /* Acknowledge stale change latches first; a leftover PRC would * otherwise let the wait below succeed before this reset has even * started (the firmware may have left one behind). */ mmio_wr(op(XHCI_OP_PORTSC) + port * 0x10, portsc_neutral(ps) | PORTSC_CSC | PORTSC_PEC | PORTSC_PRC | PORTSC_PLC); ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10); mmio_wr(op(XHCI_OP_PORTSC) + port * 0x10, portsc_neutral(ps) | PORTSC_PR); t0 = get_ticks_ms(); while (get_ticks_ms() - t0 < 500) { ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10); if (ps & PORTSC_PRC) break; usb_poll_relax(t0); } if (!(ps & PORTSC_PRC)) { xlog_hex("xhci: port reset timeout port ", port); xlog_hex(" portsc ", ps); serial_putc(SERIAL_COM1, '\n'); return -1; } mmio_wr(op(XHCI_OP_PORTSC) + port * 0x10, portsc_neutral(ps) | PORTSC_PRC); t0 = get_ticks_ms(); while (get_ticks_ms() - t0 < 500) { ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10); if (ps & PORTSC_PED) break; usb_poll_relax(t0); } if (!(ps & PORTSC_PED)) { xlog_hex("xhci: port not enabled port ", port); xlog_hex(" portsc ", ps); serial_putc(SERIAL_COM1, '\n'); return -1; } /* Reset recovery: USB2 devices need TRSTRCY (10 ms) before the first * control request; the delay is harmless for SuperSpeed. */ delay_ms(10); last_port = (int)port; return 0; } static int port_connected(unsigned int ctrl, unsigned int port) { unsigned int ps; (void)ctrl; if (!found || port >= max_ports || !(port_mask & (1u << port))) return 0; ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10); return (ps & PORTSC_CCS) ? 1 : 0; } static int port_low_speed(unsigned int ctrl, unsigned int port) { unsigned int ps; (void)ctrl; if (!found || port >= max_ports) return 0; ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10); return ((ps >> PORTSC_SPEED_SHIFT) & PORTSC_SPEED_MASK) == 2; } static int port_take_change(unsigned int ctrl, unsigned int port) { unsigned int ps; (void)ctrl; if (!found || port >= max_ports) return 0; ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10); if (!(ps & PORTSC_CSC)) return 0; mmio_wr(op(XHCI_OP_PORTSC) + port * 0x10, portsc_neutral(ps) | PORTSC_CSC); return 1; } /* ---- controller discovery ---------------------------------------------- */ /* xHCI USB Legacy Support: take ownership from the firmware before the * reset and stop its SMI from firing on port changes once we own the * controller (the same idea as the PCI 0xC0 write in uhci.c/ohci.c, but * xHCI keeps it in an extended capability). * * The BIOS-owned semaphore must actually clear before HCRST: while the * firmware still owns the controller its SMI handler can interfere with * the reset and the command ring. Linux waits for it too. */ static void legacy_handoff(unsigned int off) { unsigned int sup = mmio_rd(off); unsigned int ctl, t0; if ((sup & 0xFF) != 1) return; if (sup & (1u << 16)) { /* BIOS still owns it */ mmio_wr(off, sup | (1u << 24)); t0 = get_ticks_ms(); while (get_ticks_ms() - t0 < 1000) { if (!(mmio_rd(off) & (1u << 16))) break; } } ctl = mmio_rd(off + 4); ctl &= ~((0x7u << 1) | (0xFFu << 5) | (0x7u << 17)); ctl |= 0x7u << 29; mmio_wr(off + 4, ctl); } /* Walk the extended capabilities for the legacy handoff. Runs before the * controller reset; scan_protocols() parses the port list afterwards. */ static void ext_caps_handoff(void) { unsigned int off, guard = 0; off = ((hcc_params >> 16) & 0xFFFF) << 2; while (off && guard++ < 32) { unsigned int cap = mmio_rd(off); unsigned int next = (cap >> 8) & 0xFF; if ((cap & 0xFF) == 1) legacy_handoff(off); if (!next) break; off += next << 2; } } static void scan_protocols(void) { unsigned int off, guard = 0; port_mask = 0; off = ((hcc_params >> 16) & 0xFFFF) << 2; while (off && guard++ < 32) { unsigned int cap = mmio_rd(off); unsigned int id = cap & 0xFF; unsigned int next = (cap >> 8) & 0xFF; if (id == 2) { /* Supported Protocol */ unsigned int major = (cap >> 24) & 0xFF; unsigned int pi = mmio_rd(off + 8); unsigned int po = pi & 0xFF; unsigned int pc = (pi >> 8) & 0xFF; unsigned int i; /* The controller may expose separate USB2 and USB3 * protocol entries for the same physical connectors; * include both so a SuperSpeed device is not skipped. */ if ((major == 2 || major == 3) && po >= 1) { for (i = 0; i < pc && po + i <= 32; i++) port_mask |= 1u << (po - 1 + i); } } if (!next) break; off += next << 2; } if (!port_mask) port_mask = (max_ports >= 32) ? 0xFFFFFFFFu : ((1u << max_ports) - 1); } static int xhci_init(void) { unsigned int bus, dev, func, id, class_rev, bar, bar_hi, cmd; unsigned int hcs1, hcs2, t0, i, n, scratch; if (found) return 1; for (bus = 0; bus < 256; bus++) { for (dev = 0; dev < 32; dev++) { for (func = 0; func < 8; func++) { id = pci_cfg_read32(bus, dev, func, 0x00); if (id == 0xFFFFFFFFu || id == 0) continue; class_rev = pci_cfg_read32(bus, dev, func, 0x08); if ((class_rev >> 16) != 0x0C03) continue; if (((class_rev >> 8) & 0xFF) != 0x30) continue; bar = pci_cfg_read32(bus, dev, func, 0x10); if (bar & 1) continue; bar_hi = 0; if ((bar & 0x6) == 0x4) bar_hi = pci_cfg_read32(bus, dev, func, 0x14); bar &= 0xFFFFFFF0u; if (!bar || bar_hi) continue; pci_bus = bus; pci_dev = dev; pci_func = func; goto found_xhci; } } } return 0; found_xhci: cmd = pci_cfg_read32(pci_bus, pci_dev, pci_func, 0x04); pci_cfg_write32(pci_bus, pci_dev, pci_func, 0x04, cmd | 0x0006); mmio_phys = bar; mmio_base = (unsigned int)ioremap(bar, 0x10000, IOREMAP_MMIO); if (!mmio_base) return -1; pci_id = pci_cfg_read32(pci_bus, pci_dev, pci_func, 0x00); cap_len = mmio_rd(0) & 0xFF; hci_version = mmio_rd(0) >> 16; hcs1 = mmio_rd(XHCI_CAP_HCSPARAMS1); hcs2 = mmio_rd(XHCI_CAP_HCSPARAMS2); hcc_params = mmio_rd(XHCI_CAP_HCCPARAMS1); rt_off = mmio_rd(XHCI_CAP_RTSOFF) & ~0x1Fu; db_off = mmio_rd(XHCI_CAP_DBOFF) & ~0x3u; max_slots = hcs1 & 0xFF; if (max_slots > XHCI_MAX_SLOTS) max_slots = XHCI_MAX_SLOTS; if (!max_slots) max_slots = 1; max_ports = (hcs1 >> 24) & 0xFF; if (max_ports > XHCI_MAX_PORTS) max_ports = XHCI_MAX_PORTS; if (!max_ports) max_ports = 1; ctx_size = (hcc_params & 0x0004u) ? 64 : 32; dbg("xhci: found\n"); dbg_hex("xhci: mmio ", mmio_phys); dbg_dec("xhci: slots ", max_slots); dbg_dec("xhci: ports ", max_ports); dbg_dec("xhci: cap len ", cap_len); dcbaa = mm_alloc_page(); input_ctx = mm_alloc_page(); cmd_ring = mm_alloc_page(); evt_ring = mm_alloc_page(); erst = mm_alloc_page(); if (!dcbaa || !input_ctx || !cmd_ring || !evt_ring || !erst) { dbg("xhci: out of pages\n"); return 0; } zero_page(dcbaa, 4096); zero_page(input_ctx, 4096); zero_page(cmd_ring, 4096); zero_page(evt_ring, 4096); zero_page(erst, 4096); /* Firmware handoff before the reset: while the BIOS still owns the * controller its SMI handler can interfere with HCRST and the * command ring. */ ext_caps_handoff(); /* Wait for the controller to become ready before touching USBCMD. */ t0 = get_ticks_ms(); while (get_ticks_ms() - t0 < 1000) { if (!(mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_CNR)) break; } /* Reset. The xHC may ignore HCRST while it is running, so halt it * and wait for HCH first (xHCI 4.2; Linux does the same). QEMU * halts instantly, real silicon does not. */ mmio_wr(op(XHCI_OP_USBCMD), 0); t0 = get_ticks_ms(); while (get_ticks_ms() - t0 < 100) { if (mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_HCH) break; } if (!(mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_HCH)) dbg("xhci: halt timeout\n"); mmio_wr(op(XHCI_OP_USBCMD), XHCI_CMD_HCRST); /* Intel xHCI (CherryView/Braswell included) needs 1 ms after the * HCRST write before any other HC register is accessed; polling * USBCMD too early can leave the controller partially reset. */ delay_ms(2); t0 = get_ticks_ms(); while (get_ticks_ms() - t0 < 1000) { if (!(mmio_rd(op(XHCI_OP_USBCMD)) & XHCI_CMD_HCRST)) break; } t0 = get_ticks_ms(); while (get_ticks_ms() - t0 < 1000) { if (!(mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_CNR)) break; } if (mmio_rd(op(XHCI_OP_USBSTS)) & (XHCI_STS_CNR | XHCI_STS_HCE)) { dbg_hex("xhci: reset failed ", mmio_rd(op(XHCI_OP_USBSTS))); return 0; } /* After HCRST the controller must be halted before the ring * registers are written; otherwise the writes can be dropped and * every command times out. */ if (!(mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_HCH)) { mmio_wr(op(XHCI_OP_USBCMD), 0); t0 = get_ticks_ms(); while (get_ticks_ms() - t0 < 100) { if (mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_HCH) break; } } /* A working HCRST clears DCBAAP/CRCR/CONFIG. If firmware values * survive, the reset did not take effect: the ports keep their old * device addresses and every control transfer to address 0 fails. */ if (mmio_rd(op(XHCI_OP_DCBAAP)) != 0 || (mmio_rd(op(XHCI_OP_CRCR)) & ~0xFu) != 0 || (mmio_rd(op(XHCI_OP_CONFIG)) & 0xFFu) != 0) dbg("xhci: hcreset not applied\n"); /* Clear stale status latches from the firmware's session. */ mmio_wr(op(XHCI_OP_USBSTS), (1u << 2) | (1u << 3) | (1u << 4)); mmio_wr(op(XHCI_OP_PAGESIZE), 1); mmio_wr(op(XHCI_OP_CONFIG), max_slots); mmio_wr64(op(XHCI_OP_DCBAAP), dcbaa); /* Scratchpad buffers, if the controller asks for any. */ n = (((hcs2 >> 21) & 0x1F) << 5) | ((hcs2 >> 27) & 0x1F); if (n) { unsigned int arr; if (n > 512) n = 512; arr = mm_alloc_page(); if (!arr) { dbg("xhci: no scratchpad array\n"); return 0; } zero_page(arr, 4096); for (i = 0; i < n; i++) { scratch = mm_alloc_page(); if (!scratch) { dbg("xhci: no scratchpad page\n"); return 0; } zero_page(scratch, 4096); ram_wr64(arr + i * 8, scratch); } ram_wr64(dcbaa, arr); dbg_dec("xhci: scratchpads ", n); } /* Command ring and event ring. */ ring_init(cmd_ring, XHCI_CMD_TRBS, &cmd_enq, &cmd_cycle); mmio_wr64(op(XHCI_OP_CRCR), cmd_ring | 1); evt_idx = 0; evt_cycle = 1; *(volatile unsigned int *)(erst + 0) = evt_ring; *(volatile unsigned int *)(erst + 4) = 0; *(volatile unsigned int *)(erst + 8) = XHCI_EVT_TRBS; *(volatile unsigned int *)(erst + 12) = 0; mmio_wr(rt(XHCI_RT_ERSTSZ), 1); mmio_wr64(rt(XHCI_RT_ERSTBA), erst); mmio_wr64(rt(XHCI_RT_ERDP), evt_ring); mmio_wr(rt(XHCI_RT_IMAN), 0); mmio_wr(rt(XHCI_RT_IMOD), 0); /* Run. */ mmio_wr(op(XHCI_OP_USBCMD), XHCI_CMD_RS); t0 = get_ticks_ms(); while (get_ticks_ms() - t0 < 1000) { if (!(mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_HCH)) break; } if (mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_HCH) { dbg("xhci: controller did not start\n"); return 0; } /* The first command completion (or timeout) tells whether the ring is * actually serviced. Don't gate on CRR: QEMU leaves it clear even * while commands complete normally. */ scan_protocols(); xlog_hex("xhci: ", pci_id); xlog_hex(" hci ", hci_version); xlog_hex(" mmio ", mmio_phys); xlog_hex(" ports ", port_mask); xlog_hex(" csz ", ctx_size); xlog_hex(" scratch ", n); serial_putc(SERIAL_COM1, '\n'); /* Power every root port. VBus is supposed to come up after HCRST, * but some firmware/PPC combinations leave PP clear; the USB3 * entries may share a switch with a USB2 connector, so write all of * them, not just the USB2 range. */ for (i = 0; i < max_ports; i++) { unsigned int ps = mmio_rd(op(XHCI_OP_PORTSC) + i * 0x10); if (!(ps & PORTSC_PP)) mmio_wr(op(XHCI_OP_PORTSC) + i * 0x10, portsc_neutral(ps) | PORTSC_PP); } delay_ms(20); found = 1; return 1; } static unsigned int xhci_count(void) { return found ? 1u : 0u; } static void xhci_info(unsigned int ctrl, unsigned int *bus, unsigned int *dev, unsigned int *func, unsigned int *base, int *mmio) { (void)ctrl; *bus = pci_bus; *dev = pci_dev; *func = pci_func; *base = mmio_phys; *mmio = 1; } static unsigned int xhci_port_count(unsigned int ctrl) { (void)ctrl; return found ? max_ports : 0; } const struct usb_hcd_ops usb_xhci_ops = { "xHCI", xhci_init, xhci_count, xhci_info, control_transfer, xhci_port_count, port_connected, port_low_speed, port_reset, port_take_change, int_in_start, int_in_poll, stop_int, xhci_bulk_out, xhci_bulk_in, xhci_bulk_reset, 0, };