xhci.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  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. if (!err && b->active) {
  997. b->trb_len = len;
  998. ring_queue(b->ring, XHCI_BULK_TRBS, &b->enq,
  999. &b->cycle, (unsigned int)buf, 0, len,
  1000. TRB_TYPE(TRB_NORMAL) | TRB_IOC);
  1001. b->armed = 1;
  1002. __asm__ volatile ("" ::: "memory");
  1003. mmio_wr(db_off + 4 * b->slot, b->ep_id);
  1004. }
  1005. spin_unlock_irqrestore(&xhci_lock, flags);
  1006. return err ? -1 : (int)n;
  1007. }
  1008. if (!b->armed) {
  1009. b->trb_len = len;
  1010. ring_queue(b->ring, XHCI_BULK_TRBS, &b->enq, &b->cycle,
  1011. (unsigned int)buf, 0, len,
  1012. TRB_TYPE(TRB_NORMAL) | TRB_IOC);
  1013. b->armed = 1;
  1014. __asm__ volatile ("" ::: "memory");
  1015. mmio_wr(db_off + 4 * b->slot, b->ep_id);
  1016. }
  1017. spin_unlock_irqrestore(&xhci_lock, flags);
  1018. return 0;
  1019. }
  1020. static int xhci_bulk_out(unsigned int ctrl, unsigned int addr, unsigned int ep,
  1021. unsigned int mps, const unsigned char *buf,
  1022. unsigned int len)
  1023. {
  1024. struct xhci_slot *s = slot_by_addr(addr);
  1025. struct xhci_bulk *b = &bulk_out;
  1026. unsigned int ep_index, ep_id, ring, flags, t0, done, status;
  1027. int rc;
  1028. (void)ctrl;
  1029. if (!found || !s || ep < 1 || ep > 15 || !buf || !len)
  1030. return -1;
  1031. if (mps < 1)
  1032. mps = 512;
  1033. ep_index = (ep - 1) * 2 + 1;
  1034. ep_id = 2 * ep;
  1035. if (!b->active || b->slot != s->slot_id || b->ep != ep) {
  1036. ring = mm_alloc_page();
  1037. if (!ring)
  1038. return -1;
  1039. zero_page(ring, 4096);
  1040. bulk_reset(b);
  1041. b->slot = s->slot_id;
  1042. b->ep = ep;
  1043. b->ep_id = ep_id;
  1044. b->mps = mps;
  1045. b->ring = ring;
  1046. b->enq = 0;
  1047. b->cycle = 1;
  1048. ring_init(ring, XHCI_BULK_TRBS, &b->enq, &b->cycle);
  1049. rc = xhci_config_ep(s, ep_index, EP_BULK_OUT, mps, 0, ring);
  1050. if (rc != 0) {
  1051. bulk_reset(b);
  1052. return -1;
  1053. }
  1054. b->active = 1;
  1055. }
  1056. spin_lock_irqsave(&xhci_lock, &flags);
  1057. if (out_busy) {
  1058. spin_unlock_irqrestore(&xhci_lock, flags);
  1059. return -1;
  1060. }
  1061. out_busy = 1;
  1062. out_done = 0;
  1063. out_status = 0;
  1064. out_slot = s->slot_id;
  1065. out_trb = b->ring + b->enq * 16;
  1066. b->trb_len = len;
  1067. ring_queue(b->ring, XHCI_BULK_TRBS, &b->enq, &b->cycle,
  1068. (unsigned int)buf, 0, len, TRB_TYPE(TRB_NORMAL) | TRB_IOC);
  1069. __asm__ volatile ("" ::: "memory");
  1070. mmio_wr(db_off + 4 * s->slot_id, ep_id);
  1071. spin_unlock_irqrestore(&xhci_lock, flags);
  1072. t0 = get_ticks_ms();
  1073. while (get_ticks_ms() - t0 < 1000) {
  1074. spin_lock_irqsave(&xhci_lock, &flags);
  1075. xhci_poll_locked();
  1076. done = out_done;
  1077. spin_unlock_irqrestore(&xhci_lock, flags);
  1078. if (done)
  1079. break;
  1080. __asm__ volatile ("pause");
  1081. }
  1082. spin_lock_irqsave(&xhci_lock, &flags);
  1083. done = out_done;
  1084. status = out_status;
  1085. out_busy = 0;
  1086. spin_unlock_irqrestore(&xhci_lock, flags);
  1087. if (!done) {
  1088. dbg("xhci: bulk out timeout\n");
  1089. return -1;
  1090. }
  1091. if (status != CC_SUCCESS) {
  1092. dbg_hex("xhci: bulk out status ", status);
  1093. return -1;
  1094. }
  1095. return 0;
  1096. }
  1097. /* ---- root hub ports ---------------------------------------------------- */
  1098. static int port_reset(unsigned int ctrl, unsigned int port)
  1099. {
  1100. unsigned int ps, t0;
  1101. (void)ctrl;
  1102. if (!found || port >= max_ports ||
  1103. !(usb2_mask & (1u << port)))
  1104. return -1;
  1105. if (port_slot[port])
  1106. free_slot(&slots[port_slot[port]]);
  1107. ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10);
  1108. if (!(ps & PORTSC_CCS))
  1109. return -1;
  1110. /* Force the port into the Disabled state first: some xHCs ignore
  1111. * PR while the port is already enabled, leaving the device at its
  1112. * firmware-assigned address instead of address 0. */
  1113. if (ps & PORTSC_PED) {
  1114. mmio_wr(op(XHCI_OP_PORTSC) + port * 0x10,
  1115. portsc_neutral(ps) | PORTSC_PED);
  1116. t0 = get_ticks_ms();
  1117. while (get_ticks_ms() - t0 < 100) {
  1118. ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10);
  1119. if (!(ps & PORTSC_PED))
  1120. break;
  1121. }
  1122. }
  1123. /* Acknowledge stale change latches first; a leftover PRC would
  1124. * otherwise let the wait below succeed before this reset has even
  1125. * started (the firmware may have left one behind). */
  1126. mmio_wr(op(XHCI_OP_PORTSC) + port * 0x10,
  1127. portsc_neutral(ps) | PORTSC_CSC | PORTSC_PEC |
  1128. PORTSC_PRC | PORTSC_PLC);
  1129. ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10);
  1130. mmio_wr(op(XHCI_OP_PORTSC) + port * 0x10,
  1131. portsc_neutral(ps) | PORTSC_PR);
  1132. t0 = get_ticks_ms();
  1133. while (get_ticks_ms() - t0 < 500) {
  1134. ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10);
  1135. if (ps & PORTSC_PRC)
  1136. break;
  1137. }
  1138. if (!(ps & PORTSC_PRC)) {
  1139. dbg_hex("xhci: port reset timeout ", port);
  1140. return -1;
  1141. }
  1142. mmio_wr(op(XHCI_OP_PORTSC) + port * 0x10,
  1143. portsc_neutral(ps) | PORTSC_PRC);
  1144. t0 = get_ticks_ms();
  1145. while (get_ticks_ms() - t0 < 500) {
  1146. ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10);
  1147. if (ps & PORTSC_PED)
  1148. break;
  1149. }
  1150. if (!(ps & PORTSC_PED)) {
  1151. dbg_hex("xhci: port not enabled ", port);
  1152. return -1;
  1153. }
  1154. /* USB2 reset recovery: the device needs TRSTRCY (10 ms) before it
  1155. * answers the first control request. QEMU replies immediately,
  1156. * real devices do not. */
  1157. delay_ms(10);
  1158. last_port = (int)port;
  1159. return 0;
  1160. }
  1161. static int port_connected(unsigned int ctrl, unsigned int port)
  1162. {
  1163. unsigned int ps;
  1164. (void)ctrl;
  1165. if (!found || port >= max_ports ||
  1166. !(usb2_mask & (1u << port)))
  1167. return 0;
  1168. ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10);
  1169. return (ps & PORTSC_CCS) ? 1 : 0;
  1170. }
  1171. static int port_low_speed(unsigned int ctrl, unsigned int port)
  1172. {
  1173. unsigned int ps;
  1174. (void)ctrl;
  1175. if (!found || port >= max_ports)
  1176. return 0;
  1177. ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10);
  1178. return ((ps >> PORTSC_SPEED_SHIFT) & PORTSC_SPEED_MASK) == 2;
  1179. }
  1180. static int port_take_change(unsigned int ctrl, unsigned int port)
  1181. {
  1182. unsigned int ps;
  1183. (void)ctrl;
  1184. if (!found || port >= max_ports)
  1185. return 0;
  1186. ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10);
  1187. if (!(ps & PORTSC_CSC))
  1188. return 0;
  1189. mmio_wr(op(XHCI_OP_PORTSC) + port * 0x10,
  1190. portsc_neutral(ps) | PORTSC_CSC);
  1191. return 1;
  1192. }
  1193. /* ---- controller discovery ---------------------------------------------- */
  1194. /* xHCI USB Legacy Support: take ownership from the firmware before the
  1195. * reset and stop its SMI from firing on port changes once we own the
  1196. * controller (the same idea as the PCI 0xC0 write in uhci.c/ohci.c, but
  1197. * xHCI keeps it in an extended capability).
  1198. *
  1199. * The BIOS-owned semaphore must actually clear before HCRST: while the
  1200. * firmware still owns the controller its SMI handler can interfere with
  1201. * the reset and the command ring. Linux waits for it too. */
  1202. static void legacy_handoff(unsigned int off)
  1203. {
  1204. unsigned int sup = mmio_rd(off);
  1205. unsigned int ctl, t0;
  1206. if ((sup & 0xFF) != 1)
  1207. return;
  1208. if (sup & (1u << 16)) { /* BIOS still owns it */
  1209. mmio_wr(off, sup | (1u << 24));
  1210. t0 = get_ticks_ms();
  1211. while (get_ticks_ms() - t0 < 1000) {
  1212. if (!(mmio_rd(off) & (1u << 16)))
  1213. break;
  1214. }
  1215. }
  1216. ctl = mmio_rd(off + 4);
  1217. ctl &= ~((0x7u << 1) | (0xFFu << 5) | (0x7u << 17));
  1218. ctl |= 0x7u << 29;
  1219. mmio_wr(off + 4, ctl);
  1220. }
  1221. /* Walk the extended capabilities for the legacy handoff. Runs before the
  1222. * controller reset; scan_protocols() parses the port list afterwards. */
  1223. static void ext_caps_handoff(void)
  1224. {
  1225. unsigned int off, guard = 0;
  1226. off = ((hcc_params >> 16) & 0xFFFF) << 2;
  1227. while (off && guard++ < 32) {
  1228. unsigned int cap = mmio_rd(off);
  1229. unsigned int next = (cap >> 8) & 0xFF;
  1230. if ((cap & 0xFF) == 1)
  1231. legacy_handoff(off);
  1232. if (!next)
  1233. break;
  1234. off += next << 2;
  1235. }
  1236. }
  1237. static void scan_protocols(void)
  1238. {
  1239. unsigned int off, guard = 0;
  1240. usb2_mask = 0;
  1241. off = ((hcc_params >> 16) & 0xFFFF) << 2;
  1242. while (off && guard++ < 32) {
  1243. unsigned int cap = mmio_rd(off);
  1244. unsigned int id = cap & 0xFF;
  1245. unsigned int next = (cap >> 8) & 0xFF;
  1246. if (id == 2) { /* Supported Protocol */
  1247. unsigned int major = (cap >> 24) & 0xFF;
  1248. unsigned int pi = mmio_rd(off + 8);
  1249. unsigned int po = pi & 0xFF;
  1250. unsigned int pc = (pi >> 8) & 0xFF;
  1251. unsigned int i;
  1252. if (major <= 2 && po >= 1) {
  1253. for (i = 0; i < pc && po + i <= 32; i++)
  1254. usb2_mask |= 1u << (po - 1 + i);
  1255. }
  1256. }
  1257. if (!next)
  1258. break;
  1259. off += next << 2;
  1260. }
  1261. if (!usb2_mask)
  1262. usb2_mask = (max_ports >= 32) ? 0xFFFFFFFFu :
  1263. ((1u << max_ports) - 1);
  1264. }
  1265. static int xhci_init(void)
  1266. {
  1267. unsigned int bus, dev, func, id, class_rev, bar, bar_hi, cmd;
  1268. unsigned int hcs1, hcs2, t0, i, n, scratch;
  1269. if (found)
  1270. return 1;
  1271. for (bus = 0; bus < 256; bus++) {
  1272. for (dev = 0; dev < 32; dev++) {
  1273. for (func = 0; func < 8; func++) {
  1274. id = pci_cfg_read32(bus, dev, func, 0x00);
  1275. if (id == 0xFFFFFFFFu || id == 0)
  1276. continue;
  1277. class_rev = pci_cfg_read32(bus, dev, func, 0x08);
  1278. if ((class_rev >> 16) != 0x0C03)
  1279. continue;
  1280. if (((class_rev >> 8) & 0xFF) != 0x30)
  1281. continue;
  1282. bar = pci_cfg_read32(bus, dev, func, 0x10);
  1283. if (bar & 1)
  1284. continue;
  1285. bar_hi = 0;
  1286. if ((bar & 0x6) == 0x4)
  1287. bar_hi = pci_cfg_read32(bus, dev,
  1288. func, 0x14);
  1289. bar &= 0xFFFFFFF0u;
  1290. if (!bar || bar_hi)
  1291. continue;
  1292. pci_bus = bus;
  1293. pci_dev = dev;
  1294. pci_func = func;
  1295. goto found_xhci;
  1296. }
  1297. }
  1298. }
  1299. return 0;
  1300. found_xhci:
  1301. cmd = pci_cfg_read32(pci_bus, pci_dev, pci_func, 0x04);
  1302. pci_cfg_write32(pci_bus, pci_dev, pci_func, 0x04, cmd | 0x0006);
  1303. mmio_base = bar;
  1304. paging_map_mmio(mmio_base, 0x10000);
  1305. cap_len = mmio_rd(0) & 0xFF;
  1306. hcs1 = mmio_rd(XHCI_CAP_HCSPARAMS1);
  1307. hcs2 = mmio_rd(XHCI_CAP_HCSPARAMS2);
  1308. hcc_params = mmio_rd(XHCI_CAP_HCCPARAMS1);
  1309. rt_off = mmio_rd(XHCI_CAP_RTSOFF) & ~0x1Fu;
  1310. db_off = mmio_rd(XHCI_CAP_DBOFF) & ~0x3u;
  1311. max_slots = hcs1 & 0xFF;
  1312. if (max_slots > XHCI_MAX_SLOTS)
  1313. max_slots = XHCI_MAX_SLOTS;
  1314. if (!max_slots)
  1315. max_slots = 1;
  1316. max_ports = (hcs1 >> 24) & 0xFF;
  1317. if (max_ports > XHCI_MAX_PORTS)
  1318. max_ports = XHCI_MAX_PORTS;
  1319. if (!max_ports)
  1320. max_ports = 1;
  1321. ctx_size = (hcc_params & 0x0004u) ? 64 : 32;
  1322. dbg("xhci: found\n");
  1323. dbg_hex("xhci: mmio ", mmio_base);
  1324. dbg_dec("xhci: slots ", max_slots);
  1325. dbg_dec("xhci: ports ", max_ports);
  1326. dbg_dec("xhci: cap len ", cap_len);
  1327. dcbaa = mm_alloc_page();
  1328. input_ctx = mm_alloc_page();
  1329. cmd_ring = mm_alloc_page();
  1330. evt_ring = mm_alloc_page();
  1331. erst = mm_alloc_page();
  1332. if (!dcbaa || !input_ctx || !cmd_ring || !evt_ring || !erst) {
  1333. dbg("xhci: out of pages\n");
  1334. return 0;
  1335. }
  1336. zero_page(dcbaa, 4096);
  1337. zero_page(input_ctx, 4096);
  1338. zero_page(cmd_ring, 4096);
  1339. zero_page(evt_ring, 4096);
  1340. zero_page(erst, 4096);
  1341. /* Firmware handoff before the reset: while the BIOS still owns the
  1342. * controller its SMI handler can interfere with HCRST and the
  1343. * command ring. */
  1344. ext_caps_handoff();
  1345. /* Wait for the controller to become ready before touching USBCMD. */
  1346. t0 = get_ticks_ms();
  1347. while (get_ticks_ms() - t0 < 1000) {
  1348. if (!(mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_CNR))
  1349. break;
  1350. }
  1351. /* Reset. The xHC may ignore HCRST while it is running, so halt it
  1352. * and wait for HCH first (xHCI 4.2; Linux does the same). QEMU
  1353. * halts instantly, real silicon does not. */
  1354. mmio_wr(op(XHCI_OP_USBCMD), 0);
  1355. t0 = get_ticks_ms();
  1356. while (get_ticks_ms() - t0 < 100) {
  1357. if (mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_HCH)
  1358. break;
  1359. }
  1360. if (!(mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_HCH))
  1361. dbg("xhci: halt timeout\n");
  1362. mmio_wr(op(XHCI_OP_USBCMD), XHCI_CMD_HCRST);
  1363. /* Intel xHCI (CherryView/Braswell included) needs 1 ms after the
  1364. * HCRST write before any other HC register is accessed; polling
  1365. * USBCMD too early can leave the controller partially reset. */
  1366. delay_ms(2);
  1367. t0 = get_ticks_ms();
  1368. while (get_ticks_ms() - t0 < 1000) {
  1369. if (!(mmio_rd(op(XHCI_OP_USBCMD)) & XHCI_CMD_HCRST))
  1370. break;
  1371. }
  1372. t0 = get_ticks_ms();
  1373. while (get_ticks_ms() - t0 < 1000) {
  1374. if (!(mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_CNR))
  1375. break;
  1376. }
  1377. if (mmio_rd(op(XHCI_OP_USBSTS)) & (XHCI_STS_CNR | XHCI_STS_HCE)) {
  1378. dbg_hex("xhci: reset failed ",
  1379. mmio_rd(op(XHCI_OP_USBSTS)));
  1380. return 0;
  1381. }
  1382. /* After HCRST the controller must be halted before the ring
  1383. * registers are written; otherwise the writes can be dropped and
  1384. * every command times out. */
  1385. if (!(mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_HCH)) {
  1386. mmio_wr(op(XHCI_OP_USBCMD), 0);
  1387. t0 = get_ticks_ms();
  1388. while (get_ticks_ms() - t0 < 100) {
  1389. if (mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_HCH)
  1390. break;
  1391. }
  1392. }
  1393. /* A working HCRST clears DCBAAP/CRCR/CONFIG. If firmware values
  1394. * survive, the reset did not take effect: the ports keep their old
  1395. * device addresses and every control transfer to address 0 fails. */
  1396. if (mmio_rd(op(XHCI_OP_DCBAAP)) != 0 ||
  1397. (mmio_rd(op(XHCI_OP_CRCR)) & ~0xFu) != 0 ||
  1398. (mmio_rd(op(XHCI_OP_CONFIG)) & 0xFFu) != 0)
  1399. dbg("xhci: hcreset not applied\n");
  1400. /* Clear stale status latches from the firmware's session. */
  1401. mmio_wr(op(XHCI_OP_USBSTS), (1u << 2) | (1u << 3) | (1u << 4));
  1402. mmio_wr(op(XHCI_OP_PAGESIZE), 1);
  1403. mmio_wr(op(XHCI_OP_CONFIG), max_slots);
  1404. mmio_wr64(op(XHCI_OP_DCBAAP), dcbaa);
  1405. /* Scratchpad buffers, if the controller asks for any. */
  1406. n = (((hcs2 >> 21) & 0x1F) << 5) | ((hcs2 >> 27) & 0x1F);
  1407. if (n) {
  1408. unsigned int arr;
  1409. if (n > 512)
  1410. n = 512;
  1411. arr = mm_alloc_page();
  1412. if (!arr) {
  1413. dbg("xhci: no scratchpad array\n");
  1414. return 0;
  1415. }
  1416. zero_page(arr, 4096);
  1417. for (i = 0; i < n; i++) {
  1418. scratch = mm_alloc_page();
  1419. if (!scratch) {
  1420. dbg("xhci: no scratchpad page\n");
  1421. return 0;
  1422. }
  1423. zero_page(scratch, 4096);
  1424. ram_wr64(arr + i * 8, scratch);
  1425. }
  1426. ram_wr64(dcbaa, arr);
  1427. dbg_dec("xhci: scratchpads ", n);
  1428. }
  1429. /* Command ring and event ring. */
  1430. ring_init(cmd_ring, XHCI_CMD_TRBS, &cmd_enq, &cmd_cycle);
  1431. mmio_wr64(op(XHCI_OP_CRCR), cmd_ring | 1);
  1432. evt_idx = 0;
  1433. evt_cycle = 1;
  1434. *(volatile unsigned int *)(erst + 0) = evt_ring;
  1435. *(volatile unsigned int *)(erst + 4) = 0;
  1436. *(volatile unsigned int *)(erst + 8) = XHCI_EVT_TRBS;
  1437. *(volatile unsigned int *)(erst + 12) = 0;
  1438. mmio_wr(rt(XHCI_RT_ERSTSZ), 1);
  1439. mmio_wr64(rt(XHCI_RT_ERSTBA), erst);
  1440. mmio_wr64(rt(XHCI_RT_ERDP), evt_ring);
  1441. mmio_wr(rt(XHCI_RT_IMAN), 0);
  1442. mmio_wr(rt(XHCI_RT_IMOD), 0);
  1443. /* Run. */
  1444. mmio_wr(op(XHCI_OP_USBCMD), XHCI_CMD_RS);
  1445. t0 = get_ticks_ms();
  1446. while (get_ticks_ms() - t0 < 1000) {
  1447. if (!(mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_HCH))
  1448. break;
  1449. }
  1450. if (mmio_rd(op(XHCI_OP_USBSTS)) & XHCI_STS_HCH) {
  1451. dbg("xhci: controller did not start\n");
  1452. return 0;
  1453. }
  1454. /* The first command completion (or timeout) tells whether the ring is
  1455. * actually serviced. Don't gate on CRR: QEMU leaves it clear even
  1456. * while commands complete normally. */
  1457. scan_protocols();
  1458. dbg_hex("xhci: usb2 ports ", usb2_mask);
  1459. /* Power every root port. VBus is supposed to come up after HCRST,
  1460. * but some firmware/PPC combinations leave PP clear; the USB3
  1461. * entries may share a switch with a USB2 connector, so write all of
  1462. * them, not just the USB2 range. */
  1463. for (i = 0; i < max_ports; i++) {
  1464. unsigned int ps = mmio_rd(op(XHCI_OP_PORTSC) + i * 0x10);
  1465. if (!(ps & PORTSC_PP))
  1466. mmio_wr(op(XHCI_OP_PORTSC) + i * 0x10,
  1467. portsc_neutral(ps) | PORTSC_PP);
  1468. }
  1469. delay_ms(20);
  1470. found = 1;
  1471. return 1;
  1472. }
  1473. static unsigned int xhci_count(void)
  1474. {
  1475. return found ? 1u : 0u;
  1476. }
  1477. static void xhci_info(unsigned int ctrl, unsigned int *bus,
  1478. unsigned int *dev, unsigned int *func,
  1479. unsigned int *base, int *mmio)
  1480. {
  1481. (void)ctrl;
  1482. *bus = pci_bus;
  1483. *dev = pci_dev;
  1484. *func = pci_func;
  1485. *base = mmio_base;
  1486. *mmio = 1;
  1487. }
  1488. static unsigned int xhci_port_count(unsigned int ctrl)
  1489. {
  1490. (void)ctrl;
  1491. return found ? max_ports : 0;
  1492. }
  1493. const struct usb_hcd_ops usb_xhci_ops = {
  1494. "xHCI",
  1495. xhci_init,
  1496. xhci_count,
  1497. xhci_info,
  1498. control_transfer,
  1499. xhci_port_count,
  1500. port_connected,
  1501. port_low_speed,
  1502. port_reset,
  1503. port_take_change,
  1504. int_in_start,
  1505. int_in_poll,
  1506. stop_int,
  1507. xhci_bulk_out,
  1508. xhci_bulk_in,
  1509. 0,
  1510. };