| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718 |
- /* 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_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 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;
- 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 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
- 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 */
- }
- 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);
- if (!mps || s->speed >= 4)
- mps = ep0_mps_for_speed(s->speed);
- 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) {
- dbg(bsr ? "xhci: address default failed\n" :
- "xhci: address device failed\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;
- /* The USB core's first SET_ADDRESS uses its high/full-speed 64-byte
- * guess; a SuperSpeed slot's EP0 is always 512 bytes. */
- if (s->speed >= 4)
- mps = ep0_mps_for_speed(s->speed);
- 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;
- 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 ------------------------------------------------- */
- 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;
- }
- 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;
- if (len)
- 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);
- dbg("xhci: control timeout\n");
- 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) {
- dbg_hex("xhci: control status ", st);
- 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;
- }
- 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. */
- if (ps & PORTSC_PED) {
- 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)) {
- dbg_hex("xhci: port reset timeout ", port);
- 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)) {
- dbg_hex("xhci: port not enabled ", port);
- 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_base = bar;
- paging_map_mmio(mmio_base, 0x10000);
- cap_len = mmio_rd(0) & 0xFF;
- 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_base);
- 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();
- dbg_hex("xhci: ports ", port_mask);
- /* 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_base;
- *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,
- 0,
- };
|