xhci.c 48 KB

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