xhci.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  1. /* xHCI (USB 3.0) host controller driver.
  2. *
  3. * Scope: one controller, USB2 and USB3 root ports and devices plugged
  4. * directly into them. Control transfers, one interrupt-IN endpoint per HID
  5. * interface and the bulk endpoints used by ASIX and usbmsc are implemented.
  6. * SuperSpeed devices use a 512-byte EP0 and their endpoint descriptors may
  7. * carry a companion descriptor, which is ignored because only the bulk/HID
  8. * endpoint itself is needed.
  9. *
  10. * The schedule is polled, like the rest of the kernel. Rings and contexts
  11. * are page-allocated so the driver does not grow the kernel BSS. */
  12. #include "usb.h"
  13. #include "mm.h"
  14. #include "paging.h"
  15. #include "pci.h"
  16. #include "serial.h"
  17. #include "spinlock.h"
  18. #include "timer.h"
  19. #include "usb_hcd.h"
  20. #include "xhci.h"
  21. #define XHCI_DEBUG 0
  22. /* ---- controller limits ------------------------------------------------- */
  23. #define XHCI_MAX_SLOTS 4
  24. #define XHCI_MAX_PORTS 32
  25. #define XHCI_EVT_TRBS 256
  26. #define XHCI_CMD_TRBS 32
  27. #define XHCI_EP0_TRBS 16
  28. #define XHCI_INT_TRBS 16
  29. #define XHCI_BULK_TRBS 32
  30. /* ---- capability / operational / runtime offsets (bytes) --------------- */
  31. #define XHCI_CAP_HCSPARAMS1 0x04
  32. #define XHCI_CAP_HCSPARAMS2 0x08
  33. #define XHCI_CAP_HCCPARAMS1 0x10
  34. #define XHCI_CAP_DBOFF 0x14
  35. #define XHCI_CAP_RTSOFF 0x18
  36. #define XHCI_OP_USBCMD 0x00
  37. #define XHCI_OP_USBSTS 0x04
  38. #define XHCI_OP_PAGESIZE 0x08
  39. #define XHCI_OP_CRCR 0x18
  40. #define XHCI_OP_DCBAAP 0x30
  41. #define XHCI_OP_CONFIG 0x38
  42. #define XHCI_OP_PORTSC 0x400
  43. #define XHCI_RT_IMAN 0x20
  44. #define XHCI_RT_IMOD 0x24
  45. #define XHCI_RT_ERSTSZ 0x28
  46. #define XHCI_RT_ERSTBA 0x30
  47. #define XHCI_RT_ERDP 0x38
  48. /* USBCMD / USBSTS */
  49. #define XHCI_CMD_RS 0x0001u
  50. #define XHCI_CMD_HCRST 0x0002u
  51. #define XHCI_STS_HCH 0x0001u
  52. #define XHCI_STS_CNR 0x0800u
  53. #define XHCI_STS_HCE 0x1000u
  54. /* PORTSC */
  55. #define PORTSC_CCS 0x00000001u
  56. #define PORTSC_PED 0x00000002u
  57. #define PORTSC_PR 0x00000010u
  58. #define PORTSC_PP 0x00000200u
  59. #define PORTSC_SPEED_SHIFT 10
  60. #define PORTSC_SPEED_MASK 0xFu
  61. #define PORTSC_CSC 0x00020000u
  62. #define PORTSC_PEC 0x00040000u
  63. #define PORTSC_WRC 0x00080000u
  64. #define PORTSC_OCC 0x00100000u
  65. #define PORTSC_PRC 0x00200000u
  66. #define PORTSC_PLC 0x00400000u
  67. #define PORTSC_CEC 0x00800000u
  68. #define PORTSC_RWC (PORTSC_CSC | PORTSC_PEC | PORTSC_WRC | \
  69. PORTSC_OCC | PORTSC_PRC | PORTSC_PLC | \
  70. PORTSC_CEC | PORTSC_PED)
  71. /* Bits that may be written back while preserving the port state:
  72. * read-only status (CCS, OCA, speed, device-removable) and the
  73. * read/write-state fields (PLS, PP, PIC, wake enables). Everything else
  74. * -- especially PR/WPR (RW1S) and PED (RW1CS) -- must be written as 0 or
  75. * the write changes the port state. Linux's
  76. * xhci_port_state_to_neutral() does the same. */
  77. #define PORTSC_RO_MASK (PORTSC_CCS | 0x00000008u | \
  78. (PORTSC_SPEED_MASK << PORTSC_SPEED_SHIFT) | \
  79. 0x40000000u)
  80. #define PORTSC_RWS_MASK (0x000001E0u | PORTSC_PP | 0x0000C000u | \
  81. 0x0E000000u)
  82. static unsigned int portsc_neutral(unsigned int ps)
  83. {
  84. return (ps & PORTSC_RO_MASK) | (ps & PORTSC_RWS_MASK);
  85. }
  86. /* TRB types */
  87. #define TRB_NORMAL 1
  88. #define TRB_SETUP 2
  89. #define TRB_DATA 3
  90. #define TRB_STATUS 4
  91. #define TRB_LINK 6
  92. #define TRB_ENABLE_SLOT 9
  93. #define TRB_DISABLE_SLOT 10
  94. #define TRB_ADDR_DEV 11
  95. #define TRB_CONFIG_EP 12
  96. #define TRB_TRANSFER 32
  97. #define TRB_COMPLETION 33
  98. /* TRB control bits */
  99. #define TRB_CYCLE 0x00000001u
  100. #define TRB_TC 0x00000002u
  101. #define TRB_ISP 0x00000004u
  102. #define TRB_CHAIN 0x00000010u
  103. #define TRB_IOC 0x00000020u
  104. #define TRB_IDT 0x00000040u
  105. #define TRB_BSR 0x00000200u
  106. #define TRB_DIR_IN 0x00010000u
  107. #define TRB_TYPE(t) ((t) << 10)
  108. /* completion codes */
  109. #define CC_SUCCESS 1
  110. #define CC_SHORT_PACKET 13
  111. /* slot/EP context flags and endpoint types */
  112. #define SLOT_FLAG 0x00000001u
  113. #define EP0_FLAG 0x00000002u
  114. #define EP_CTRL 4
  115. #define EP_BULK_OUT 2
  116. #define EP_BULK_IN 6
  117. #define EP_INT_IN 7
  118. struct xhci_trb {
  119. unsigned int p0;
  120. unsigned int p1;
  121. unsigned int status;
  122. unsigned int control;
  123. };
  124. struct xhci_slot {
  125. int used;
  126. unsigned int slot_id;
  127. unsigned int addr; /* logical address used by usb.c */
  128. unsigned int dev_addr; /* address assigned by the xHC */
  129. unsigned int port;
  130. unsigned int speed;
  131. unsigned int dc; /* device context page */
  132. unsigned int ep0; /* EP0 transfer ring page */
  133. unsigned int ep0_enq;
  134. unsigned int ep0_cycle;
  135. unsigned int ep_mask; /* configured endpoint flags */
  136. };
  137. struct xhci_int {
  138. int active;
  139. unsigned int slot; /* hardware slot id */
  140. unsigned int ep_id; /* xHCI endpoint id (2n+dir) */
  141. unsigned int mps;
  142. unsigned int ring;
  143. unsigned int enq;
  144. unsigned int cycle;
  145. unsigned int trb_len;
  146. unsigned int pending_len;
  147. int pending;
  148. int error;
  149. unsigned char buf[8] __attribute__((aligned(16)));
  150. };
  151. struct xhci_bulk {
  152. int active;
  153. int armed;
  154. unsigned int slot;
  155. unsigned int ep; /* endpoint number */
  156. unsigned int ep_id;
  157. unsigned int mps;
  158. unsigned int ring;
  159. unsigned int enq;
  160. unsigned int cycle;
  161. unsigned int buf;
  162. unsigned int len;
  163. unsigned int trb_len;
  164. unsigned int actual;
  165. int pending;
  166. int error;
  167. };
  168. static unsigned int mmio_base;
  169. static unsigned int cap_len;
  170. static unsigned int rt_off;
  171. static unsigned int db_off;
  172. static unsigned int max_slots;
  173. static unsigned int max_ports;
  174. static unsigned int ctx_size;
  175. static unsigned int port_mask;
  176. static unsigned int hcc_params;
  177. static unsigned int pci_bus, pci_dev, pci_func;
  178. static int found;
  179. static int last_port = -1;
  180. static unsigned int dcbaa;
  181. static unsigned int input_ctx;
  182. static unsigned int cmd_ring;
  183. static unsigned int evt_ring;
  184. static unsigned int erst;
  185. static unsigned int cmd_enq;
  186. static unsigned int cmd_cycle;
  187. static unsigned int evt_idx;
  188. static unsigned int evt_cycle;
  189. static unsigned int cmd_busy;
  190. static unsigned int cmd_done;
  191. static unsigned int cmd_status;
  192. static unsigned int cmd_slot;
  193. static unsigned int cmd_trb_addr;
  194. static unsigned int ctl_busy;
  195. static unsigned int ctl_done;
  196. static unsigned int ctl_slot;
  197. static unsigned int ctl_status;
  198. static unsigned int ctl_req_len;
  199. static unsigned int ctl_actual;
  200. static unsigned int ctl_data_seen;
  201. static unsigned int ctl_setup_trb;
  202. static unsigned int ctl_data_trb;
  203. static unsigned int ctl_status_trb;
  204. static struct xhci_bulk bulk_in;
  205. static struct xhci_bulk bulk_out;
  206. static unsigned int out_busy;
  207. static unsigned int out_done;
  208. static unsigned int out_status;
  209. static unsigned int out_slot;
  210. static unsigned int out_trb;
  211. static struct xhci_slot slots[XHCI_MAX_SLOTS + 1];
  212. static unsigned int addr_slot[128];
  213. static unsigned int port_slot[XHCI_MAX_PORTS];
  214. static struct xhci_int ints[USB_HCD_INT_SLOTS];
  215. static spinlock_t xhci_lock;
  216. /* ---- debug ------------------------------------------------------------- */
  217. #if XHCI_DEBUG
  218. static void dbg(const char *s)
  219. {
  220. serial_puts(SERIAL_COM1, s);
  221. }
  222. static void dbg_hex(const char *s, unsigned int v)
  223. {
  224. serial_puts(SERIAL_COM1, s);
  225. serial_puthex(SERIAL_COM1, v);
  226. serial_putc(SERIAL_COM1, '\n');
  227. }
  228. static void dbg_dec(const char *s, unsigned int v)
  229. {
  230. serial_puts(SERIAL_COM1, s);
  231. serial_putdec(SERIAL_COM1, v);
  232. serial_putc(SERIAL_COM1, '\n');
  233. }
  234. #else
  235. static void dbg(const char *s) { (void)s; }
  236. static void dbg_hex(const char *s, unsigned int v) { (void)s; (void)v; }
  237. static void dbg_dec(const char *s, unsigned int v) { (void)s; (void)v; }
  238. #endif
  239. static void delay_ms(unsigned int ms)
  240. {
  241. usb_delay_ms(ms); /* sleeps inside a sleepable scan */
  242. }
  243. /* ---- MMIO helpers ------------------------------------------------------ */
  244. static unsigned int mmio_rd(unsigned int off)
  245. {
  246. return *(volatile unsigned int *)(mmio_base + off);
  247. }
  248. static void mmio_wr(unsigned int off, unsigned int val)
  249. {
  250. *(volatile unsigned int *)(mmio_base + off) = val;
  251. }
  252. static void mmio_wr64(unsigned int off, unsigned int lo)
  253. {
  254. mmio_wr(off, lo);
  255. mmio_wr(off + 4, 0);
  256. }
  257. /* Normal RAM (DCBAA, ERST, scratchpad array) is not MMIO. */
  258. static void ram_wr64(unsigned int addr, unsigned int lo)
  259. {
  260. *(volatile unsigned int *)addr = lo;
  261. *(volatile unsigned int *)(addr + 4) = 0;
  262. }
  263. static void zero_page(unsigned int addr, unsigned int len)
  264. {
  265. unsigned int *p = (unsigned int *)addr;
  266. while (len >= 4) {
  267. *p++ = 0;
  268. len -= 4;
  269. }
  270. }
  271. static unsigned int op(unsigned int reg)
  272. {
  273. return cap_len + reg;
  274. }
  275. static unsigned int rt(unsigned int reg)
  276. {
  277. return rt_off + reg;
  278. }
  279. /* ---- rings ------------------------------------------------------------- */
  280. static void ring_init(unsigned int base, unsigned int size,
  281. unsigned int *enq, unsigned int *cycle)
  282. {
  283. struct xhci_trb *link = (struct xhci_trb *)(base + (size - 1) * 16);
  284. link->p0 = base;
  285. link->p1 = 0;
  286. link->status = 0;
  287. link->control = TRB_TYPE(TRB_LINK) | TRB_TC;
  288. *enq = 0;
  289. *cycle = 1;
  290. }
  291. static void ring_queue(unsigned int base, unsigned int size,
  292. unsigned int *enq, unsigned int *cycle,
  293. unsigned int p0, unsigned int p1,
  294. unsigned int status, unsigned int control)
  295. {
  296. struct xhci_trb *trb = (struct xhci_trb *)(base + *enq * 16);
  297. trb->p0 = p0;
  298. trb->p1 = p1;
  299. trb->status = status;
  300. __asm__ volatile ("" ::: "memory");
  301. trb->control = control | *cycle;
  302. __asm__ volatile ("" ::: "memory");
  303. (*enq)++;
  304. if (*enq == size - 1) {
  305. struct xhci_trb *link =
  306. (struct xhci_trb *)(base + (size - 1) * 16);
  307. __asm__ volatile ("" ::: "memory");
  308. link->control ^= TRB_CYCLE;
  309. *enq = 0;
  310. *cycle ^= 1;
  311. }
  312. }
  313. /* ---- event handling ---------------------------------------------------- */
  314. static void xhci_poll_locked(void);
  315. static void stop_int(unsigned int slot);
  316. static void bulk_reset(struct xhci_bulk *b);
  317. static void xhci_int_event(unsigned int slot, unsigned int ep,
  318. unsigned int code, unsigned int residual)
  319. {
  320. unsigned int i;
  321. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  322. if (!ints[i].active || ints[i].slot != slot ||
  323. ints[i].ep_id != ep)
  324. continue;
  325. if (!ints[i].pending) {
  326. if (code == CC_SUCCESS || code == CC_SHORT_PACKET) {
  327. ints[i].pending_len =
  328. (ints[i].trb_len >= residual) ?
  329. ints[i].trb_len - residual : 0;
  330. ints[i].pending = 1;
  331. } else {
  332. ints[i].pending = 1;
  333. ints[i].error = 1;
  334. }
  335. }
  336. break;
  337. }
  338. }
  339. static void xhci_poll_locked(void)
  340. {
  341. unsigned int n = 0;
  342. while (n++ < 512) {
  343. struct xhci_trb *ev =
  344. (struct xhci_trb *)(evt_ring + evt_idx * 16);
  345. unsigned int type;
  346. if ((ev->control & TRB_CYCLE) != evt_cycle)
  347. break;
  348. type = (ev->control >> 10) & 0x3F;
  349. if (type == TRB_COMPLETION) {
  350. unsigned int code = (ev->status >> 24) & 0xFF;
  351. unsigned int slot = (ev->control >> 24) & 0xFF;
  352. if (cmd_busy && ev->p0 == cmd_trb_addr) {
  353. cmd_status = code;
  354. cmd_slot = slot;
  355. cmd_done = 1;
  356. }
  357. } else if (type == TRB_TRANSFER) {
  358. unsigned int code = (ev->status >> 24) & 0xFF;
  359. unsigned int residual = ev->status & 0xFFFFFF;
  360. unsigned int slot = (ev->control >> 24) & 0xFF;
  361. unsigned int ep = (ev->control >> 16) & 0x1F;
  362. if (ctl_busy && slot == ctl_slot && ep == 1) {
  363. if (ev->p0 == ctl_setup_trb) {
  364. /* The setup stage is its own TD and
  365. * completes first; only an error stops
  366. * the transfer. */
  367. if (code != CC_SUCCESS &&
  368. code != CC_SHORT_PACKET) {
  369. ctl_status = code;
  370. ctl_done = 1;
  371. }
  372. } else if (ev->p0 == ctl_data_trb) {
  373. ctl_actual = (ctl_req_len >= residual) ?
  374. ctl_req_len - residual : 0;
  375. ctl_data_seen = 1;
  376. if (code != CC_SUCCESS &&
  377. code != CC_SHORT_PACKET) {
  378. ctl_status = code;
  379. ctl_done = 1;
  380. }
  381. } else if (ev->p0 == ctl_status_trb) {
  382. if (!ctl_data_seen)
  383. ctl_actual =
  384. (code == CC_SUCCESS) ?
  385. ctl_req_len : 0;
  386. ctl_status = code;
  387. ctl_done = 1;
  388. }
  389. } else if (out_busy && slot == out_slot &&
  390. ep == bulk_out.ep_id &&
  391. ev->p0 == out_trb) {
  392. out_status = code;
  393. out_done = 1;
  394. } else if (bulk_in.active && slot == bulk_in.slot &&
  395. ep == bulk_in.ep_id) {
  396. bulk_in.armed = 0;
  397. if (!bulk_in.pending) {
  398. if (code == CC_SUCCESS ||
  399. code == CC_SHORT_PACKET) {
  400. bulk_in.actual =
  401. (bulk_in.trb_len >=
  402. residual) ?
  403. bulk_in.trb_len -
  404. residual : 0;
  405. bulk_in.pending = 1;
  406. } else {
  407. bulk_in.pending = 1;
  408. bulk_in.error = 1;
  409. }
  410. }
  411. } else {
  412. xhci_int_event(slot, ep, code, residual);
  413. }
  414. }
  415. evt_idx++;
  416. if (evt_idx >= XHCI_EVT_TRBS) {
  417. evt_idx = 0;
  418. evt_cycle ^= 1;
  419. }
  420. }
  421. mmio_wr64(rt(XHCI_RT_ERDP),
  422. (evt_ring + evt_idx * 16) | 8);
  423. }
  424. /* ---- commands ---------------------------------------------------------- */
  425. static int xhci_cmd(unsigned int p0, unsigned int p1, unsigned int control,
  426. unsigned int *out_slot)
  427. {
  428. unsigned int flags, t0, rc;
  429. if (!found)
  430. return -1;
  431. spin_lock_irqsave(&xhci_lock, &flags);
  432. if (cmd_busy) {
  433. spin_unlock_irqrestore(&xhci_lock, flags);
  434. return -1;
  435. }
  436. cmd_busy = 1;
  437. cmd_done = 0;
  438. cmd_status = 0;
  439. cmd_slot = 0;
  440. cmd_trb_addr = cmd_ring + cmd_enq * 16;
  441. ring_queue(cmd_ring, XHCI_CMD_TRBS, &cmd_enq, &cmd_cycle,
  442. p0, p1, 0, control);
  443. __asm__ volatile ("" ::: "memory");
  444. mmio_wr(db_off, 0);
  445. spin_unlock_irqrestore(&xhci_lock, flags);
  446. t0 = get_ticks_ms();
  447. while (get_ticks_ms() - t0 < 1000) {
  448. spin_lock_irqsave(&xhci_lock, &flags);
  449. xhci_poll_locked();
  450. rc = cmd_done;
  451. spin_unlock_irqrestore(&xhci_lock, flags);
  452. if (rc)
  453. break;
  454. usb_poll_relax(t0);
  455. __asm__ volatile ("pause");
  456. }
  457. spin_lock_irqsave(&xhci_lock, &flags);
  458. cmd_busy = 0;
  459. if (!cmd_done) {
  460. spin_unlock_irqrestore(&xhci_lock, flags);
  461. dbg("xhci: command timeout\n");
  462. return -1;
  463. }
  464. if (out_slot)
  465. *out_slot = cmd_slot;
  466. rc = cmd_status;
  467. spin_unlock_irqrestore(&xhci_lock, flags);
  468. if (rc != CC_SUCCESS) {
  469. dbg_hex("xhci: command status ", rc);
  470. return -1;
  471. }
  472. return 0;
  473. }
  474. /* ---- contexts ---------------------------------------------------------- */
  475. static void fill_slot_ctx(unsigned int addr, unsigned int port,
  476. unsigned int speed, unsigned int last_ctx)
  477. {
  478. unsigned int *p = (unsigned int *)(input_ctx + ctx_size);
  479. p[0] = (speed << 20) | (last_ctx << 27);
  480. p[1] = (port + 1) << 16;
  481. p[2] = 0;
  482. p[3] = addr;
  483. }
  484. static void fill_ep0_ctx(unsigned int mps, unsigned int ring,
  485. unsigned int enq, unsigned int cycle)
  486. {
  487. unsigned int *p = (unsigned int *)(input_ctx + 2 * ctx_size);
  488. p[0] = 0;
  489. p[1] = (EP_CTRL << 3) | (3 << 1) | (mps << 16);
  490. p[2] = (ring + enq * 16) | cycle;
  491. p[3] = 0;
  492. p[4] = 8;
  493. }
  494. static void fill_ep_ctx(unsigned int ep_index, unsigned int type,
  495. unsigned int mps, unsigned int interval,
  496. unsigned int ring)
  497. {
  498. unsigned int *p =
  499. (unsigned int *)(input_ctx + (ep_index + 2) * ctx_size);
  500. p[0] = interval << 16;
  501. p[1] = (type << 3) | (3 << 1) | (mps << 16);
  502. p[2] = ring | 1;
  503. p[3] = 0;
  504. p[4] = mps;
  505. }
  506. static struct xhci_slot *slot_by_addr(unsigned int addr)
  507. {
  508. unsigned int id;
  509. if (addr > 127)
  510. return 0;
  511. id = addr_slot[addr];
  512. if (id < 1 || id > max_slots || !slots[id].used)
  513. return 0;
  514. return &slots[id];
  515. }
  516. static unsigned int port_speed(unsigned int port)
  517. {
  518. unsigned int ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10);
  519. unsigned int sp = (ps >> PORTSC_SPEED_SHIFT) & PORTSC_SPEED_MASK;
  520. return sp ? sp : 1;
  521. }
  522. /* Default control-pipe max packet size for the BSR Address Device command.
  523. * High speed must be 64; full speed uses the USB core's 64-byte guess (the
  524. * descriptor read then corrects it); low speed is 8. Using 8 for a
  525. * high/full-speed slot leaves EP0 unusable on real xHCs. */
  526. static unsigned int ep0_mps_for_speed(unsigned int speed)
  527. {
  528. if (speed == 2) /* low speed */
  529. return 8;
  530. if (speed >= 4) /* SuperSpeed / SuperSpeedPlus */
  531. return 512;
  532. return 64; /* full speed (guess) and high speed */
  533. }
  534. static int xhci_config_ep(struct xhci_slot *s, unsigned int ep_index,
  535. unsigned int type, unsigned int mps,
  536. unsigned int interval, unsigned int ring)
  537. {
  538. unsigned int ep_flag = 1u << (ep_index + 1);
  539. unsigned int last = ep_index + 1;
  540. unsigned int i, flags;
  541. int rc;
  542. for (i = 1; i < 32; i++)
  543. if (s->ep_mask & (1u << i))
  544. last = i;
  545. spin_lock_irqsave(&xhci_lock, &flags);
  546. zero_page(input_ctx, 4096);
  547. *(unsigned int *)input_ctx = 0;
  548. *(unsigned int *)(input_ctx + 4) = SLOT_FLAG | ep_flag;
  549. fill_slot_ctx(s->dev_addr, s->port, s->speed, last);
  550. fill_ep_ctx(ep_index, type, mps, interval, ring);
  551. spin_unlock_irqrestore(&xhci_lock, flags);
  552. rc = xhci_cmd(input_ctx, 0,
  553. TRB_TYPE(TRB_CONFIG_EP) | (s->slot_id << 24), 0);
  554. if (rc == 0)
  555. s->ep_mask |= ep_flag;
  556. return rc;
  557. }
  558. /* ---- slots ------------------------------------------------------------- */
  559. /* Create a slot. bsr=1: Address Device with Block Set Address Request,
  560. * leaving the slot in the Default state at device address 0 (new
  561. * enumeration scheme). bsr=0: the command carries target_addr and moves
  562. * Enabled -> Addressed directly (traditional scheme), which is what Linux
  563. * does when the port is addressed before the first descriptor read.
  564. * mps == 0 (or a SuperSpeed port) derives it from the port speed. */
  565. static struct xhci_slot *new_slot_common(unsigned int target_addr,
  566. unsigned int mps, int bsr)
  567. {
  568. struct xhci_slot *s;
  569. unsigned int slot_id, dc, ep0, flags;
  570. int rc;
  571. if (last_port < 0 || last_port >= (int)max_ports)
  572. return 0;
  573. if (port_slot[last_port])
  574. return 0;
  575. slot_id = 0;
  576. rc = xhci_cmd(0, 0, TRB_TYPE(TRB_ENABLE_SLOT), &slot_id);
  577. if (rc != 0 || slot_id == 0 || slot_id > max_slots) {
  578. dbg("xhci: enable slot failed\n");
  579. return 0;
  580. }
  581. dc = mm_alloc_page();
  582. ep0 = mm_alloc_page();
  583. if (!dc || !ep0) {
  584. if (dc)
  585. mm_free_page(dc);
  586. if (ep0)
  587. mm_free_page(ep0);
  588. return 0;
  589. }
  590. zero_page(dc, 4096);
  591. zero_page(ep0, 4096);
  592. s = &slots[slot_id];
  593. s->used = 1;
  594. s->slot_id = slot_id;
  595. s->addr = 0;
  596. s->port = (unsigned int)last_port;
  597. s->speed = port_speed((unsigned int)last_port);
  598. s->dc = dc;
  599. s->ep0 = ep0;
  600. s->ep0_enq = 0;
  601. s->ep0_cycle = 1;
  602. s->ep_mask = 0;
  603. s->dev_addr = 0;
  604. ring_init(ep0, XHCI_EP0_TRBS, &s->ep0_enq, &s->ep0_cycle);
  605. if (!mps || s->speed >= 4)
  606. mps = ep0_mps_for_speed(s->speed);
  607. ram_wr64(dcbaa + slot_id * 8, dc);
  608. spin_lock_irqsave(&xhci_lock, &flags);
  609. zero_page(input_ctx, 4096);
  610. *(unsigned int *)input_ctx = 0;
  611. *(unsigned int *)(input_ctx + 4) = SLOT_FLAG | EP0_FLAG;
  612. fill_slot_ctx(0, s->port, s->speed, 1);
  613. fill_ep0_ctx(mps, ep0, s->ep0_enq, s->ep0_cycle);
  614. spin_unlock_irqrestore(&xhci_lock, flags);
  615. rc = xhci_cmd(input_ctx, 0,
  616. TRB_TYPE(TRB_ADDR_DEV) | (slot_id << 24) |
  617. (bsr ? TRB_BSR : 0), 0);
  618. if (rc != 0) {
  619. dbg(bsr ? "xhci: address default failed\n" :
  620. "xhci: address device failed\n");
  621. ram_wr64(dcbaa + slot_id * 8, 0);
  622. xhci_cmd(0, 0,
  623. TRB_TYPE(TRB_DISABLE_SLOT) | (slot_id << 24), 0);
  624. mm_free_page(dc);
  625. mm_free_page(ep0);
  626. s->used = 0;
  627. return 0;
  628. }
  629. /* The xHC assigns the USB device address (the input slot context
  630. * address is 0); keep it for later Configure Endpoint commands. */
  631. s->dev_addr = ((unsigned int *)s->dc)[3] & 0xFF;
  632. s->addr = bsr ? 0 : target_addr;
  633. addr_slot[s->addr] = slot_id;
  634. port_slot[s->port] = slot_id;
  635. return s;
  636. }
  637. /* New-scheme slot: Enabled -> Default, at device address 0. */
  638. static struct xhci_slot *new_slot(void)
  639. {
  640. return new_slot_common(0, 0, 1);
  641. }
  642. static void free_slot(struct xhci_slot *s)
  643. {
  644. unsigned int i, flags;
  645. if (!s || !s->used)
  646. return;
  647. xhci_cmd(0, 0, TRB_TYPE(TRB_DISABLE_SLOT) | (s->slot_id << 24), 0);
  648. ram_wr64(dcbaa + s->slot_id * 8, 0);
  649. spin_lock_irqsave(&xhci_lock, &flags);
  650. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  651. if (ints[i].slot == s->slot_id) {
  652. ints[i].active = 0;
  653. ints[i].pending = 0;
  654. ints[i].error = 0;
  655. if (ints[i].ring) {
  656. mm_free_page(ints[i].ring);
  657. ints[i].ring = 0;
  658. }
  659. ints[i].slot = 0;
  660. }
  661. }
  662. if (s->dc) {
  663. mm_free_page(s->dc);
  664. s->dc = 0;
  665. }
  666. if (s->ep0) {
  667. mm_free_page(s->ep0);
  668. s->ep0 = 0;
  669. }
  670. if (s->addr < 128)
  671. addr_slot[s->addr] = 0;
  672. if (bulk_in.active && bulk_in.slot == s->slot_id)
  673. bulk_reset(&bulk_in);
  674. if (bulk_out.active && bulk_out.slot == s->slot_id)
  675. bulk_reset(&bulk_out);
  676. if (s->port < XHCI_MAX_PORTS)
  677. port_slot[s->port] = 0;
  678. s->used = 0;
  679. s->addr = 0;
  680. s->ep_mask = 0;
  681. spin_unlock_irqrestore(&xhci_lock, flags);
  682. }
  683. static int set_address(unsigned int new_addr, unsigned int mps)
  684. {
  685. struct xhci_slot *s = slot_by_addr(0);
  686. unsigned int flags;
  687. int rc;
  688. if (!s || new_addr == 0 || new_addr > 127)
  689. return -1;
  690. /* The USB core's first SET_ADDRESS uses its high/full-speed 64-byte
  691. * guess; a SuperSpeed slot's EP0 is always 512 bytes. */
  692. if (s->speed >= 4)
  693. mps = ep0_mps_for_speed(s->speed);
  694. spin_lock_irqsave(&xhci_lock, &flags);
  695. zero_page(input_ctx, 4096);
  696. *(unsigned int *)input_ctx = 0;
  697. *(unsigned int *)(input_ctx + 4) = SLOT_FLAG | EP0_FLAG;
  698. fill_slot_ctx(0, s->port, s->speed, 1);
  699. fill_ep0_ctx(mps, s->ep0, s->ep0_enq, s->ep0_cycle);
  700. spin_unlock_irqrestore(&xhci_lock, flags);
  701. rc = xhci_cmd(input_ctx, 0,
  702. TRB_TYPE(TRB_ADDR_DEV) | (s->slot_id << 24), 0);
  703. if (rc != 0) {
  704. dbg("xhci: set address failed\n");
  705. return -1;
  706. }
  707. spin_lock_irqsave(&xhci_lock, &flags);
  708. s->dev_addr = ((unsigned int *)s->dc)[3] & 0xFF;
  709. addr_slot[0] = 0;
  710. s->addr = new_addr;
  711. addr_slot[new_addr] = s->slot_id;
  712. spin_unlock_irqrestore(&xhci_lock, flags);
  713. return 0;
  714. }
  715. /* ---- control transfers ------------------------------------------------- */
  716. static int control_transfer(unsigned int ctrl, unsigned int addr,
  717. unsigned int mps, int low_speed,
  718. const unsigned char *setup, unsigned char *data,
  719. unsigned int len, int dir_in)
  720. {
  721. struct xhci_slot *s;
  722. unsigned int flags, t0, rc, st;
  723. unsigned int setup_c, data_c, status_c;
  724. unsigned int last_bell;
  725. (void)ctrl;
  726. (void)low_speed;
  727. if (!found)
  728. return -1;
  729. /* xHCI owns SET_ADDRESS: it is an Address Device command, not a
  730. * control request on the wire. If a Default-state slot exists (new
  731. * scheme), move it to Addressed. Otherwise this is the traditional
  732. * scheme: create the slot Enabled and address it in one command,
  733. * without ever using the Default state. */
  734. if (setup[0] == 0x00 && setup[1] == 0x05 && addr == 0) {
  735. unsigned int new_addr = (unsigned int)setup[2] |
  736. ((unsigned int)setup[3] << 8);
  737. s = slot_by_addr(0);
  738. if (s)
  739. return set_address(new_addr, mps);
  740. s = new_slot_common(new_addr, mps, 0);
  741. return s ? 0 : -1;
  742. }
  743. s = slot_by_addr(addr);
  744. if (s && addr == 0 && (int)s->port != last_port) {
  745. /* A failed enumeration on another port left a slot at
  746. * address 0; it must not be reused for this device. */
  747. free_slot(s);
  748. s = 0;
  749. }
  750. if (!s) {
  751. if (addr != 0)
  752. return -1;
  753. s = new_slot();
  754. if (!s)
  755. return -1;
  756. }
  757. spin_lock_irqsave(&xhci_lock, &flags);
  758. if (ctl_busy) {
  759. spin_unlock_irqrestore(&xhci_lock, flags);
  760. dbg("xhci: control busy\n");
  761. return -1;
  762. }
  763. ctl_busy = 1;
  764. ctl_done = 0;
  765. ctl_status = 0;
  766. ctl_actual = 0;
  767. ctl_data_seen = 0;
  768. ctl_slot = s->slot_id;
  769. ctl_req_len = len;
  770. /* Encode the three stages the way EDK2's XhciDxe does: every stage
  771. * is its own TD (no CHAIN) and carries IOC. This Braswell xHC never
  772. * completes a chained multi-TRB control TD: with Linux's chaining
  773. * (CHAIN on setup+data, IOC on status) the data stage hangs and no
  774. * transfer event is ever posted, while the per-stage encoding
  775. * completes. */
  776. setup_c = TRB_TYPE(TRB_SETUP) | TRB_IDT | TRB_IOC;
  777. if (len)
  778. setup_c |= (dir_in ? 3u : 2u) << 16;
  779. ctl_setup_trb = s->ep0 + s->ep0_enq * 16;
  780. ring_queue(s->ep0, XHCI_EP0_TRBS, &s->ep0_enq, &s->ep0_cycle,
  781. (unsigned int)setup[0] | ((unsigned int)setup[1] << 8) |
  782. ((unsigned int)setup[2] << 16) |
  783. ((unsigned int)setup[3] << 24),
  784. (unsigned int)setup[4] | ((unsigned int)setup[5] << 8) |
  785. ((unsigned int)setup[6] << 16) |
  786. ((unsigned int)setup[7] << 24),
  787. 8, setup_c);
  788. ctl_data_trb = 0;
  789. if (len) {
  790. ctl_data_trb = s->ep0 + s->ep0_enq * 16;
  791. data_c = TRB_TYPE(TRB_DATA) | TRB_IOC;
  792. if (dir_in)
  793. data_c |= TRB_DIR_IN | TRB_ISP;
  794. ring_queue(s->ep0, XHCI_EP0_TRBS, &s->ep0_enq,
  795. &s->ep0_cycle, (unsigned int)data, 0, len, data_c);
  796. }
  797. ctl_status_trb = s->ep0 + s->ep0_enq * 16;
  798. status_c = TRB_TYPE(TRB_STATUS) | TRB_IOC;
  799. if (!len || !dir_in)
  800. status_c |= TRB_DIR_IN;
  801. ring_queue(s->ep0, XHCI_EP0_TRBS, &s->ep0_enq, &s->ep0_cycle,
  802. 0, 0, 0, status_c);
  803. __asm__ volatile ("" ::: "memory");
  804. mmio_wr(db_off + 4 * s->slot_id, 1);
  805. spin_unlock_irqrestore(&xhci_lock, flags);
  806. t0 = get_ticks_ms();
  807. last_bell = t0;
  808. while (get_ticks_ms() - t0 < 1000) {
  809. spin_lock_irqsave(&xhci_lock, &flags);
  810. xhci_poll_locked();
  811. rc = ctl_done;
  812. if (!rc && get_ticks_ms() - last_bell >= 100) {
  813. /* Re-ring while the endpoint is still busy; a posted
  814. * doorbell can be lost on some real xHCs. */
  815. mmio_wr(db_off + 4 * s->slot_id, 1);
  816. last_bell = get_ticks_ms();
  817. }
  818. spin_unlock_irqrestore(&xhci_lock, flags);
  819. if (rc)
  820. break;
  821. usb_poll_relax(t0);
  822. __asm__ volatile ("pause");
  823. }
  824. spin_lock_irqsave(&xhci_lock, &flags);
  825. if (!ctl_done) {
  826. ctl_busy = 0;
  827. spin_unlock_irqrestore(&xhci_lock, flags);
  828. dbg("xhci: control timeout\n");
  829. return -1;
  830. }
  831. rc = ctl_actual;
  832. st = ctl_status;
  833. ctl_busy = 0;
  834. spin_unlock_irqrestore(&xhci_lock, flags);
  835. if (st != CC_SUCCESS && st != CC_SHORT_PACKET) {
  836. dbg_hex("xhci: control status ", st);
  837. return -1;
  838. }
  839. return (int)rc;
  840. }
  841. /* ---- HID interrupt endpoint -------------------------------------------- */
  842. static int int_in_start(unsigned int ctrl, unsigned int slot,
  843. unsigned int addr, unsigned int ep,
  844. unsigned int mps, int low_speed)
  845. {
  846. struct xhci_slot *s = slot_by_addr(addr);
  847. struct xhci_int *in;
  848. unsigned int ep_index, ep_id, ring, flags;
  849. int rc;
  850. (void)ctrl;
  851. (void)low_speed;
  852. if (!found || !s || slot >= USB_HCD_INT_SLOTS || ep < 1 || ep > 15)
  853. return -1;
  854. if (mps < 1)
  855. mps = 8;
  856. if (mps > 8)
  857. mps = 8;
  858. if (ints[slot].active)
  859. stop_int(slot);
  860. ep_index = (ep - 1) * 2 + 2; /* EP index 2 for EP1 IN, 4 for EP2 IN */
  861. ep_id = 2 * ep + 1;
  862. ring = mm_alloc_page();
  863. if (!ring)
  864. return -1;
  865. zero_page(ring, 4096);
  866. in = &ints[slot];
  867. in->slot = s->slot_id;
  868. in->ep_id = ep_id;
  869. in->mps = mps;
  870. in->ring = ring;
  871. in->enq = 0;
  872. in->cycle = 1;
  873. in->trb_len = mps;
  874. in->pending = 0;
  875. in->error = 0;
  876. in->active = 0;
  877. ring_init(ring, XHCI_INT_TRBS, &in->enq, &in->cycle);
  878. rc = xhci_config_ep(s, ep_index, EP_INT_IN, mps, 1, ring);
  879. if (rc != 0) {
  880. dbg_hex("xhci: configure int ep failed ", ep_index);
  881. mm_free_page(ring);
  882. in->ring = 0;
  883. return -1;
  884. }
  885. in->active = 1;
  886. spin_lock_irqsave(&xhci_lock, &flags);
  887. ring_queue(ring, XHCI_INT_TRBS, &in->enq, &in->cycle,
  888. (unsigned int)in->buf, 0, in->trb_len,
  889. TRB_TYPE(TRB_NORMAL) | TRB_IOC);
  890. __asm__ volatile ("" ::: "memory");
  891. mmio_wr(db_off + 4 * s->slot_id, ep_id);
  892. spin_unlock_irqrestore(&xhci_lock, flags);
  893. return 0;
  894. }
  895. static int int_in_poll(unsigned int slot, unsigned char *buf, unsigned int len)
  896. {
  897. struct xhci_int *in;
  898. unsigned int flags, n, i;
  899. if (!found || slot >= USB_HCD_INT_SLOTS)
  900. return 0;
  901. in = &ints[slot];
  902. if (!in->active)
  903. return 0;
  904. spin_lock_irqsave(&xhci_lock, &flags);
  905. xhci_poll_locked();
  906. spin_unlock_irqrestore(&xhci_lock, flags);
  907. if (!in->pending)
  908. return 0;
  909. if (in->error) {
  910. in->pending = 0;
  911. in->error = 0;
  912. return -1;
  913. }
  914. n = in->pending_len;
  915. if (n > len)
  916. n = len;
  917. for (i = 0; i < n; i++)
  918. buf[i] = in->buf[i];
  919. in->pending = 0;
  920. spin_lock_irqsave(&xhci_lock, &flags);
  921. if (in->active) {
  922. ring_queue(in->ring, XHCI_INT_TRBS, &in->enq, &in->cycle,
  923. (unsigned int)in->buf, 0, in->trb_len,
  924. TRB_TYPE(TRB_NORMAL) | TRB_IOC);
  925. __asm__ volatile ("" ::: "memory");
  926. mmio_wr(db_off + 4 * in->slot, in->ep_id);
  927. }
  928. spin_unlock_irqrestore(&xhci_lock, flags);
  929. return (int)in->pending_len;
  930. }
  931. static void stop_int(unsigned int slot)
  932. {
  933. struct xhci_int *in;
  934. unsigned int flags;
  935. if (slot >= USB_HCD_INT_SLOTS)
  936. return;
  937. in = &ints[slot];
  938. spin_lock_irqsave(&xhci_lock, &flags);
  939. in->active = 0;
  940. in->pending = 0;
  941. in->error = 0;
  942. spin_unlock_irqrestore(&xhci_lock, flags);
  943. }
  944. /* ---- bulk transfers ---------------------------------------------------- */
  945. static void bulk_reset(struct xhci_bulk *b)
  946. {
  947. if (b->ring) {
  948. mm_free_page(b->ring);
  949. b->ring = 0;
  950. }
  951. b->active = 0;
  952. b->armed = 0;
  953. b->slot = 0;
  954. b->pending = 0;
  955. b->error = 0;
  956. b->actual = 0;
  957. }
  958. static int xhci_bulk_in(unsigned int ctrl, unsigned int addr, unsigned int ep,
  959. unsigned int mps, unsigned char *buf, unsigned int len)
  960. {
  961. struct xhci_slot *s = slot_by_addr(addr);
  962. struct xhci_bulk *b = &bulk_in;
  963. unsigned int ep_index, ep_id, ring, flags, n, err;
  964. int rc;
  965. (void)ctrl;
  966. if (!found || !s || ep < 1 || ep > 15 || !buf || !len)
  967. return -1;
  968. if (mps < 1)
  969. mps = 512;
  970. ep_index = (ep - 1) * 2 + 2;
  971. ep_id = 2 * ep + 1;
  972. if (!b->active || b->slot != s->slot_id || b->ep != ep) {
  973. ring = mm_alloc_page();
  974. if (!ring)
  975. return -1;
  976. zero_page(ring, 4096);
  977. bulk_reset(b);
  978. b->slot = s->slot_id;
  979. b->ep = ep;
  980. b->ep_id = ep_id;
  981. b->mps = mps;
  982. b->ring = ring;
  983. b->enq = 0;
  984. b->cycle = 1;
  985. ring_init(ring, XHCI_BULK_TRBS, &b->enq, &b->cycle);
  986. rc = xhci_config_ep(s, ep_index, EP_BULK_IN, mps, 0, ring);
  987. if (rc != 0) {
  988. bulk_reset(b);
  989. return -1;
  990. }
  991. b->active = 1;
  992. }
  993. b->buf = (unsigned int)buf;
  994. b->len = len;
  995. spin_lock_irqsave(&xhci_lock, &flags);
  996. xhci_poll_locked();
  997. if (b->pending) {
  998. n = b->actual;
  999. err = (unsigned int)b->error;
  1000. b->pending = 0;
  1001. b->error = 0;
  1002. /* Do not re-arm here: the next call may use a different
  1003. * buffer/length (the mass-storage driver alternates CBW,
  1004. * data and CSW buffers). The caller's next usb_bulk_in()
  1005. * arms the new transfer with its own buffer. */
  1006. b->armed = 0;
  1007. spin_unlock_irqrestore(&xhci_lock, flags);
  1008. return err ? -1 : (int)n;
  1009. }
  1010. if (!b->armed) {
  1011. b->trb_len = len;
  1012. ring_queue(b->ring, XHCI_BULK_TRBS, &b->enq, &b->cycle,
  1013. (unsigned int)buf, 0, len,
  1014. TRB_TYPE(TRB_NORMAL) | TRB_IOC);
  1015. b->armed = 1;
  1016. __asm__ volatile ("" ::: "memory");
  1017. mmio_wr(db_off + 4 * b->slot, b->ep_id);
  1018. }
  1019. spin_unlock_irqrestore(&xhci_lock, flags);
  1020. return 0;
  1021. }
  1022. static int xhci_bulk_out(unsigned int ctrl, unsigned int addr, unsigned int ep,
  1023. unsigned int mps, const unsigned char *buf,
  1024. unsigned int len)
  1025. {
  1026. struct xhci_slot *s = slot_by_addr(addr);
  1027. struct xhci_bulk *b = &bulk_out;
  1028. unsigned int ep_index, ep_id, ring, flags, t0, done, status;
  1029. int rc;
  1030. (void)ctrl;
  1031. if (!found || !s || ep < 1 || ep > 15 || !buf || !len)
  1032. return -1;
  1033. if (mps < 1)
  1034. mps = 512;
  1035. ep_index = (ep - 1) * 2 + 1;
  1036. ep_id = 2 * ep;
  1037. if (!b->active || b->slot != s->slot_id || b->ep != ep) {
  1038. ring = mm_alloc_page();
  1039. if (!ring)
  1040. return -1;
  1041. zero_page(ring, 4096);
  1042. bulk_reset(b);
  1043. b->slot = s->slot_id;
  1044. b->ep = ep;
  1045. b->ep_id = ep_id;
  1046. b->mps = mps;
  1047. b->ring = ring;
  1048. b->enq = 0;
  1049. b->cycle = 1;
  1050. ring_init(ring, XHCI_BULK_TRBS, &b->enq, &b->cycle);
  1051. rc = xhci_config_ep(s, ep_index, EP_BULK_OUT, mps, 0, ring);
  1052. if (rc != 0) {
  1053. bulk_reset(b);
  1054. return -1;
  1055. }
  1056. b->active = 1;
  1057. }
  1058. spin_lock_irqsave(&xhci_lock, &flags);
  1059. if (out_busy) {
  1060. spin_unlock_irqrestore(&xhci_lock, flags);
  1061. return -1;
  1062. }
  1063. out_busy = 1;
  1064. out_done = 0;
  1065. out_status = 0;
  1066. out_slot = s->slot_id;
  1067. out_trb = b->ring + b->enq * 16;
  1068. b->trb_len = len;
  1069. ring_queue(b->ring, XHCI_BULK_TRBS, &b->enq, &b->cycle,
  1070. (unsigned int)buf, 0, len, TRB_TYPE(TRB_NORMAL) | TRB_IOC);
  1071. __asm__ volatile ("" ::: "memory");
  1072. mmio_wr(db_off + 4 * s->slot_id, ep_id);
  1073. spin_unlock_irqrestore(&xhci_lock, flags);
  1074. t0 = get_ticks_ms();
  1075. while (get_ticks_ms() - t0 < 1000) {
  1076. spin_lock_irqsave(&xhci_lock, &flags);
  1077. xhci_poll_locked();
  1078. done = out_done;
  1079. spin_unlock_irqrestore(&xhci_lock, flags);
  1080. if (done)
  1081. break;
  1082. usb_poll_relax(t0);
  1083. __asm__ volatile ("pause");
  1084. }
  1085. spin_lock_irqsave(&xhci_lock, &flags);
  1086. done = out_done;
  1087. status = out_status;
  1088. out_busy = 0;
  1089. spin_unlock_irqrestore(&xhci_lock, flags);
  1090. if (!done) {
  1091. dbg("xhci: bulk out timeout\n");
  1092. return -1;
  1093. }
  1094. if (status != CC_SUCCESS) {
  1095. dbg_hex("xhci: bulk out status ", status);
  1096. return -1;
  1097. }
  1098. return 0;
  1099. }
  1100. /* ---- root hub ports ---------------------------------------------------- */
  1101. static int port_reset(unsigned int ctrl, unsigned int port)
  1102. {
  1103. unsigned int ps, t0;
  1104. (void)ctrl;
  1105. if (!found || port >= max_ports ||
  1106. !(port_mask & (1u << port)))
  1107. return -1;
  1108. if (port_slot[port])
  1109. free_slot(&slots[port_slot[port]]);
  1110. ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10);
  1111. if (!(ps & PORTSC_CCS))
  1112. return -1;
  1113. /* Force the port into the Disabled state first: some xHCs ignore
  1114. * PR while the port is already enabled, leaving the device at its
  1115. * firmware-assigned address instead of address 0. */
  1116. if (ps & PORTSC_PED) {
  1117. mmio_wr(op(XHCI_OP_PORTSC) + port * 0x10,
  1118. portsc_neutral(ps) | PORTSC_PED);
  1119. t0 = get_ticks_ms();
  1120. while (get_ticks_ms() - t0 < 100) {
  1121. ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10);
  1122. if (!(ps & PORTSC_PED))
  1123. break;
  1124. usb_poll_relax(t0);
  1125. }
  1126. }
  1127. /* Acknowledge stale change latches first; a leftover PRC would
  1128. * otherwise let the wait below succeed before this reset has even
  1129. * started (the firmware may have left one behind). */
  1130. mmio_wr(op(XHCI_OP_PORTSC) + port * 0x10,
  1131. portsc_neutral(ps) | PORTSC_CSC | PORTSC_PEC |
  1132. PORTSC_PRC | PORTSC_PLC);
  1133. ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10);
  1134. mmio_wr(op(XHCI_OP_PORTSC) + port * 0x10,
  1135. portsc_neutral(ps) | PORTSC_PR);
  1136. t0 = get_ticks_ms();
  1137. while (get_ticks_ms() - t0 < 500) {
  1138. ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10);
  1139. if (ps & PORTSC_PRC)
  1140. break;
  1141. usb_poll_relax(t0);
  1142. }
  1143. if (!(ps & PORTSC_PRC)) {
  1144. dbg_hex("xhci: port reset timeout ", port);
  1145. return -1;
  1146. }
  1147. mmio_wr(op(XHCI_OP_PORTSC) + port * 0x10,
  1148. portsc_neutral(ps) | PORTSC_PRC);
  1149. t0 = get_ticks_ms();
  1150. while (get_ticks_ms() - t0 < 500) {
  1151. ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10);
  1152. if (ps & PORTSC_PED)
  1153. break;
  1154. usb_poll_relax(t0);
  1155. }
  1156. if (!(ps & PORTSC_PED)) {
  1157. dbg_hex("xhci: port not enabled ", port);
  1158. return -1;
  1159. }
  1160. /* Reset recovery: USB2 devices need TRSTRCY (10 ms) before the first
  1161. * control request; the delay is harmless for SuperSpeed. */
  1162. delay_ms(10);
  1163. last_port = (int)port;
  1164. return 0;
  1165. }
  1166. static int port_connected(unsigned int ctrl, unsigned int port)
  1167. {
  1168. unsigned int ps;
  1169. (void)ctrl;
  1170. if (!found || port >= max_ports ||
  1171. !(port_mask & (1u << port)))
  1172. return 0;
  1173. ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10);
  1174. return (ps & PORTSC_CCS) ? 1 : 0;
  1175. }
  1176. static int port_low_speed(unsigned int ctrl, unsigned int port)
  1177. {
  1178. unsigned int ps;
  1179. (void)ctrl;
  1180. if (!found || port >= max_ports)
  1181. return 0;
  1182. ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10);
  1183. return ((ps >> PORTSC_SPEED_SHIFT) & PORTSC_SPEED_MASK) == 2;
  1184. }
  1185. static int port_take_change(unsigned int ctrl, unsigned int port)
  1186. {
  1187. unsigned int ps;
  1188. (void)ctrl;
  1189. if (!found || port >= max_ports)
  1190. return 0;
  1191. ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10);
  1192. if (!(ps & PORTSC_CSC))
  1193. return 0;
  1194. mmio_wr(op(XHCI_OP_PORTSC) + port * 0x10,
  1195. portsc_neutral(ps) | PORTSC_CSC);
  1196. return 1;
  1197. }
  1198. /* ---- controller discovery ---------------------------------------------- */
  1199. /* xHCI USB Legacy Support: take ownership from the firmware before the
  1200. * reset and stop its SMI from firing on port changes once we own the
  1201. * controller (the same idea as the PCI 0xC0 write in uhci.c/ohci.c, but
  1202. * xHCI keeps it in an extended capability).
  1203. *
  1204. * The BIOS-owned semaphore must actually clear before HCRST: while the
  1205. * firmware still owns the controller its SMI handler can interfere with
  1206. * the reset and the command ring. Linux waits for it too. */
  1207. static void legacy_handoff(unsigned int off)
  1208. {
  1209. unsigned int sup = mmio_rd(off);
  1210. unsigned int ctl, t0;
  1211. if ((sup & 0xFF) != 1)
  1212. return;
  1213. if (sup & (1u << 16)) { /* BIOS still owns it */
  1214. mmio_wr(off, sup | (1u << 24));
  1215. t0 = get_ticks_ms();
  1216. while (get_ticks_ms() - t0 < 1000) {
  1217. if (!(mmio_rd(off) & (1u << 16)))
  1218. break;
  1219. }
  1220. }
  1221. ctl = mmio_rd(off + 4);
  1222. ctl &= ~((0x7u << 1) | (0xFFu << 5) | (0x7u << 17));
  1223. ctl |= 0x7u << 29;
  1224. mmio_wr(off + 4, ctl);
  1225. }
  1226. /* Walk the extended capabilities for the legacy handoff. Runs before the
  1227. * controller reset; scan_protocols() parses the port list afterwards. */
  1228. static void ext_caps_handoff(void)
  1229. {
  1230. unsigned int off, guard = 0;
  1231. off = ((hcc_params >> 16) & 0xFFFF) << 2;
  1232. while (off && guard++ < 32) {
  1233. unsigned int cap = mmio_rd(off);
  1234. unsigned int next = (cap >> 8) & 0xFF;
  1235. if ((cap & 0xFF) == 1)
  1236. legacy_handoff(off);
  1237. if (!next)
  1238. break;
  1239. off += next << 2;
  1240. }
  1241. }
  1242. static void scan_protocols(void)
  1243. {
  1244. unsigned int off, guard = 0;
  1245. port_mask = 0;
  1246. off = ((hcc_params >> 16) & 0xFFFF) << 2;
  1247. while (off && guard++ < 32) {
  1248. unsigned int cap = mmio_rd(off);
  1249. unsigned int id = cap & 0xFF;
  1250. unsigned int next = (cap >> 8) & 0xFF;
  1251. if (id == 2) { /* Supported Protocol */
  1252. unsigned int major = (cap >> 24) & 0xFF;
  1253. unsigned int pi = mmio_rd(off + 8);
  1254. unsigned int po = pi & 0xFF;
  1255. unsigned int pc = (pi >> 8) & 0xFF;
  1256. unsigned int i;
  1257. /* The controller may expose separate USB2 and USB3
  1258. * protocol entries for the same physical connectors;
  1259. * include both so a SuperSpeed device is not skipped. */
  1260. if ((major == 2 || major == 3) && po >= 1) {
  1261. for (i = 0; i < pc && po + i <= 32; i++)
  1262. port_mask |= 1u << (po - 1 + i);
  1263. }
  1264. }
  1265. if (!next)
  1266. break;
  1267. off += next << 2;
  1268. }
  1269. if (!port_mask)
  1270. port_mask = (max_ports >= 32) ? 0xFFFFFFFFu :
  1271. ((1u << max_ports) - 1);
  1272. }
  1273. static int xhci_init(void)
  1274. {
  1275. unsigned int bus, dev, func, id, class_rev, bar, bar_hi, cmd;
  1276. unsigned int hcs1, hcs2, t0, i, n, scratch;
  1277. if (found)
  1278. return 1;
  1279. for (bus = 0; bus < 256; bus++) {
  1280. for (dev = 0; dev < 32; dev++) {
  1281. for (func = 0; func < 8; func++) {
  1282. id = pci_cfg_read32(bus, dev, func, 0x00);
  1283. if (id == 0xFFFFFFFFu || id == 0)
  1284. continue;
  1285. class_rev = pci_cfg_read32(bus, dev, func, 0x08);
  1286. if ((class_rev >> 16) != 0x0C03)
  1287. continue;
  1288. if (((class_rev >> 8) & 0xFF) != 0x30)
  1289. continue;
  1290. bar = pci_cfg_read32(bus, dev, func, 0x10);
  1291. if (bar & 1)
  1292. continue;
  1293. bar_hi = 0;
  1294. if ((bar & 0x6) == 0x4)
  1295. bar_hi = pci_cfg_read32(bus, dev,
  1296. func, 0x14);
  1297. bar &= 0xFFFFFFF0u;
  1298. if (!bar || bar_hi)
  1299. continue;
  1300. pci_bus = bus;
  1301. pci_dev = dev;
  1302. pci_func = func;
  1303. goto found_xhci;
  1304. }
  1305. }
  1306. }
  1307. return 0;
  1308. found_xhci:
  1309. cmd = pci_cfg_read32(pci_bus, pci_dev, pci_func, 0x04);
  1310. pci_cfg_write32(pci_bus, pci_dev, pci_func, 0x04, cmd | 0x0006);
  1311. mmio_base = bar;
  1312. paging_map_mmio(mmio_base, 0x10000);
  1313. cap_len = mmio_rd(0) & 0xFF;
  1314. hcs1 = mmio_rd(XHCI_CAP_HCSPARAMS1);
  1315. hcs2 = mmio_rd(XHCI_CAP_HCSPARAMS2);
  1316. hcc_params = mmio_rd(XHCI_CAP_HCCPARAMS1);
  1317. rt_off = mmio_rd(XHCI_CAP_RTSOFF) & ~0x1Fu;
  1318. db_off = mmio_rd(XHCI_CAP_DBOFF) & ~0x3u;
  1319. max_slots = hcs1 & 0xFF;
  1320. if (max_slots > XHCI_MAX_SLOTS)
  1321. max_slots = XHCI_MAX_SLOTS;
  1322. if (!max_slots)
  1323. max_slots = 1;
  1324. max_ports = (hcs1 >> 24) & 0xFF;
  1325. if (max_ports > XHCI_MAX_PORTS)
  1326. max_ports = XHCI_MAX_PORTS;
  1327. if (!max_ports)
  1328. max_ports = 1;
  1329. ctx_size = (hcc_params & 0x0004u) ? 64 : 32;
  1330. dbg("xhci: found\n");
  1331. dbg_hex("xhci: mmio ", mmio_base);
  1332. dbg_dec("xhci: slots ", max_slots);
  1333. dbg_dec("xhci: ports ", max_ports);
  1334. dbg_dec("xhci: cap len ", cap_len);
  1335. dcbaa = mm_alloc_page();
  1336. input_ctx = mm_alloc_page();
  1337. cmd_ring = mm_alloc_page();
  1338. evt_ring = mm_alloc_page();
  1339. erst = mm_alloc_page();
  1340. if (!dcbaa || !input_ctx || !cmd_ring || !evt_ring || !erst) {
  1341. dbg("xhci: out of pages\n");
  1342. return 0;
  1343. }
  1344. zero_page(dcbaa, 4096);
  1345. zero_page(input_ctx, 4096);
  1346. zero_page(cmd_ring, 4096);
  1347. zero_page(evt_ring, 4096);
  1348. zero_page(erst, 4096);
  1349. /* Firmware handoff before the reset: while the BIOS still owns the
  1350. * controller its SMI handler can interfere with HCRST and the
  1351. * command ring. */
  1352. ext_caps_handoff();
  1353. /* Wait for the controller to become ready before touching USBCMD. */
  1354. t0 = get_ticks_ms();
  1355. while (get_ticks_ms() - t0 < 1000) {
  1356. if (!(mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_CNR))
  1357. break;
  1358. }
  1359. /* Reset. The xHC may ignore HCRST while it is running, so halt it
  1360. * and wait for HCH first (xHCI 4.2; Linux does the same). QEMU
  1361. * halts instantly, real silicon does not. */
  1362. mmio_wr(op(XHCI_OP_USBCMD), 0);
  1363. t0 = get_ticks_ms();
  1364. while (get_ticks_ms() - t0 < 100) {
  1365. if (mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_HCH)
  1366. break;
  1367. }
  1368. if (!(mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_HCH))
  1369. dbg("xhci: halt timeout\n");
  1370. mmio_wr(op(XHCI_OP_USBCMD), XHCI_CMD_HCRST);
  1371. /* Intel xHCI (CherryView/Braswell included) needs 1 ms after the
  1372. * HCRST write before any other HC register is accessed; polling
  1373. * USBCMD too early can leave the controller partially reset. */
  1374. delay_ms(2);
  1375. t0 = get_ticks_ms();
  1376. while (get_ticks_ms() - t0 < 1000) {
  1377. if (!(mmio_rd(op(XHCI_OP_USBCMD)) & XHCI_CMD_HCRST))
  1378. break;
  1379. }
  1380. t0 = get_ticks_ms();
  1381. while (get_ticks_ms() - t0 < 1000) {
  1382. if (!(mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_CNR))
  1383. break;
  1384. }
  1385. if (mmio_rd(op(XHCI_OP_USBSTS)) & (XHCI_STS_CNR | XHCI_STS_HCE)) {
  1386. dbg_hex("xhci: reset failed ",
  1387. mmio_rd(op(XHCI_OP_USBSTS)));
  1388. return 0;
  1389. }
  1390. /* After HCRST the controller must be halted before the ring
  1391. * registers are written; otherwise the writes can be dropped and
  1392. * every command times out. */
  1393. if (!(mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_HCH)) {
  1394. mmio_wr(op(XHCI_OP_USBCMD), 0);
  1395. t0 = get_ticks_ms();
  1396. while (get_ticks_ms() - t0 < 100) {
  1397. if (mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_HCH)
  1398. break;
  1399. }
  1400. }
  1401. /* A working HCRST clears DCBAAP/CRCR/CONFIG. If firmware values
  1402. * survive, the reset did not take effect: the ports keep their old
  1403. * device addresses and every control transfer to address 0 fails. */
  1404. if (mmio_rd(op(XHCI_OP_DCBAAP)) != 0 ||
  1405. (mmio_rd(op(XHCI_OP_CRCR)) & ~0xFu) != 0 ||
  1406. (mmio_rd(op(XHCI_OP_CONFIG)) & 0xFFu) != 0)
  1407. dbg("xhci: hcreset not applied\n");
  1408. /* Clear stale status latches from the firmware's session. */
  1409. mmio_wr(op(XHCI_OP_USBSTS), (1u << 2) | (1u << 3) | (1u << 4));
  1410. mmio_wr(op(XHCI_OP_PAGESIZE), 1);
  1411. mmio_wr(op(XHCI_OP_CONFIG), max_slots);
  1412. mmio_wr64(op(XHCI_OP_DCBAAP), dcbaa);
  1413. /* Scratchpad buffers, if the controller asks for any. */
  1414. n = (((hcs2 >> 21) & 0x1F) << 5) | ((hcs2 >> 27) & 0x1F);
  1415. if (n) {
  1416. unsigned int arr;
  1417. if (n > 512)
  1418. n = 512;
  1419. arr = mm_alloc_page();
  1420. if (!arr) {
  1421. dbg("xhci: no scratchpad array\n");
  1422. return 0;
  1423. }
  1424. zero_page(arr, 4096);
  1425. for (i = 0; i < n; i++) {
  1426. scratch = mm_alloc_page();
  1427. if (!scratch) {
  1428. dbg("xhci: no scratchpad page\n");
  1429. return 0;
  1430. }
  1431. zero_page(scratch, 4096);
  1432. ram_wr64(arr + i * 8, scratch);
  1433. }
  1434. ram_wr64(dcbaa, arr);
  1435. dbg_dec("xhci: scratchpads ", n);
  1436. }
  1437. /* Command ring and event ring. */
  1438. ring_init(cmd_ring, XHCI_CMD_TRBS, &cmd_enq, &cmd_cycle);
  1439. mmio_wr64(op(XHCI_OP_CRCR), cmd_ring | 1);
  1440. evt_idx = 0;
  1441. evt_cycle = 1;
  1442. *(volatile unsigned int *)(erst + 0) = evt_ring;
  1443. *(volatile unsigned int *)(erst + 4) = 0;
  1444. *(volatile unsigned int *)(erst + 8) = XHCI_EVT_TRBS;
  1445. *(volatile unsigned int *)(erst + 12) = 0;
  1446. mmio_wr(rt(XHCI_RT_ERSTSZ), 1);
  1447. mmio_wr64(rt(XHCI_RT_ERSTBA), erst);
  1448. mmio_wr64(rt(XHCI_RT_ERDP), evt_ring);
  1449. mmio_wr(rt(XHCI_RT_IMAN), 0);
  1450. mmio_wr(rt(XHCI_RT_IMOD), 0);
  1451. /* Run. */
  1452. mmio_wr(op(XHCI_OP_USBCMD), XHCI_CMD_RS);
  1453. t0 = get_ticks_ms();
  1454. while (get_ticks_ms() - t0 < 1000) {
  1455. if (!(mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_HCH))
  1456. break;
  1457. }
  1458. if (mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_HCH) {
  1459. dbg("xhci: controller did not start\n");
  1460. return 0;
  1461. }
  1462. /* The first command completion (or timeout) tells whether the ring is
  1463. * actually serviced. Don't gate on CRR: QEMU leaves it clear even
  1464. * while commands complete normally. */
  1465. scan_protocols();
  1466. dbg_hex("xhci: ports ", port_mask);
  1467. /* Power every root port. VBus is supposed to come up after HCRST,
  1468. * but some firmware/PPC combinations leave PP clear; the USB3
  1469. * entries may share a switch with a USB2 connector, so write all of
  1470. * them, not just the USB2 range. */
  1471. for (i = 0; i < max_ports; i++) {
  1472. unsigned int ps = mmio_rd(op(XHCI_OP_PORTSC) + i * 0x10);
  1473. if (!(ps & PORTSC_PP))
  1474. mmio_wr(op(XHCI_OP_PORTSC) + i * 0x10,
  1475. portsc_neutral(ps) | PORTSC_PP);
  1476. }
  1477. delay_ms(20);
  1478. found = 1;
  1479. return 1;
  1480. }
  1481. static unsigned int xhci_count(void)
  1482. {
  1483. return found ? 1u : 0u;
  1484. }
  1485. static void xhci_info(unsigned int ctrl, unsigned int *bus,
  1486. unsigned int *dev, unsigned int *func,
  1487. unsigned int *base, int *mmio)
  1488. {
  1489. (void)ctrl;
  1490. *bus = pci_bus;
  1491. *dev = pci_dev;
  1492. *func = pci_func;
  1493. *base = mmio_base;
  1494. *mmio = 1;
  1495. }
  1496. static unsigned int xhci_port_count(unsigned int ctrl)
  1497. {
  1498. (void)ctrl;
  1499. return found ? max_ports : 0;
  1500. }
  1501. const struct usb_hcd_ops usb_xhci_ops = {
  1502. "xHCI",
  1503. xhci_init,
  1504. xhci_count,
  1505. xhci_info,
  1506. control_transfer,
  1507. xhci_port_count,
  1508. port_connected,
  1509. port_low_speed,
  1510. port_reset,
  1511. port_take_change,
  1512. int_in_start,
  1513. int_in_poll,
  1514. stop_int,
  1515. xhci_bulk_out,
  1516. xhci_bulk_in,
  1517. 0,
  1518. };