xhci.c 43 KB

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