xhci.c 48 KB

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