ehci.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  1. /* EHCI (USB 2.0) host controller driver.
  2. *
  3. * Scope: high-speed devices on the root ports. The EHCI controller is paired
  4. * with UHCI/OHCI companions on most chipsets; low/full-speed devices are left
  5. * to those controllers (the hardware routes them back when a port reset shows
  6. * a non-high-speed device). usb.c enumerates every controller type, so a
  7. * high-speed USB Ethernet dongle on EHCI and a full-speed mouse/keyboard on a
  8. * companion both work.
  9. *
  10. * The schedule is polled (no IRQs), like the UHCI/OHCI drivers. All control,
  11. * bulk and interrupt endpoints live in the asynchronous schedule: a circular
  12. * QH list with the control QH after the reclamation head. Interrupt endpoints
  13. * are retried every microframe rather than through the periodic list; for a
  14. * handful of boot HID devices the extra NAK traffic is negligible and it keeps
  15. * the schedule simple.
  16. *
  17. * Descriptors (frame list, QHs, qTDs, bounce buffers) come from the page
  18. * allocator, so the driver does not grow the kernel BSS. */
  19. #include "ehci.h"
  20. #include "io.h"
  21. #include "mm.h"
  22. #include "paging.h"
  23. #include "pci.h"
  24. #include "serial.h"
  25. #include "timer.h"
  26. #define EHCI_DEBUG 0
  27. /* ---- capability / operational register offsets (bytes) ----------------- */
  28. #define EHCI_CAP_CAPLENGTH 0x00
  29. #define EHCI_CAP_HCSPARAMS 0x04
  30. #define EHCI_CAP_HCCPARAMS 0x08
  31. #define EHCI_OP_USBCMD 0x00
  32. #define EHCI_OP_USBSTS 0x04
  33. #define EHCI_OP_USBINTR 0x08
  34. #define EHCI_OP_FRINDEX 0x0C
  35. #define EHCI_OP_CTRLDSSEGMENT 0x10
  36. #define EHCI_OP_PERIODICLIST 0x14
  37. #define EHCI_OP_ASYNCLIST 0x18
  38. #define EHCI_OP_CONFIGFLAG 0x40
  39. #define EHCI_OP_PORTSC 0x44
  40. /* USBCMD */
  41. #define CMD_RUN 0x00000001u
  42. #define CMD_RESET 0x00000002u
  43. #define CMD_PSE 0x00000010u
  44. #define CMD_ASE 0x00000020u
  45. #define CMD_ITC_8 0x00080000u
  46. /* USBSTS */
  47. #define STS_INT 0x00000001u
  48. #define STS_ERR 0x00000002u
  49. #define STS_PCD 0x00000004u
  50. #define STS_FLR 0x00000008u
  51. #define STS_FATAL 0x00000010u
  52. #define STS_IAA 0x00000020u
  53. #define STS_HALT 0x00001000u
  54. #define STS_RECL 0x00002000u
  55. #define STS_PSS 0x00004000u
  56. #define STS_ASS 0x00008000u
  57. /* PORTSC */
  58. #define PORT_CONNECT 0x00000001u
  59. #define PORT_CSC 0x00000002u
  60. #define PORT_PE 0x00000004u
  61. #define PORT_PEC 0x00000008u
  62. #define PORT_OC 0x00000010u
  63. #define PORT_OCC 0x00000020u
  64. #define PORT_RESUME 0x00000040u
  65. #define PORT_SUSPEND 0x00000080u
  66. #define PORT_RESET 0x00000100u
  67. #define PORT_LS_MASK 0x00000C00u
  68. #define PORT_POWER 0x00001000u
  69. #define PORT_OWNER 0x00002000u
  70. #define PORT_RWC (PORT_CSC | PORT_PEC | PORT_OCC)
  71. #define PORT_USB11(ps) (((ps) & PORT_LS_MASK) == (1u << 10))
  72. /* qTD token bits (EHCI 3.5.3). */
  73. #define QTD_ACTIVE 0x00000080u
  74. #define QTD_HALT 0x00000040u
  75. #define QTD_DBE 0x00000020u
  76. #define QTD_BABBLE 0x00000010u
  77. #define QTD_XACT 0x00000008u
  78. #define QTD_MMF 0x00000004u
  79. #define QTD_PING 0x00000001u
  80. #define QTD_ERRORS (QTD_HALT | QTD_DBE | QTD_BABBLE | \
  81. QTD_XACT | QTD_MMF)
  82. #define QTD_TOGGLE 0x80000000u
  83. #define QTD_PID_OUT 0u
  84. #define QTD_PID_IN 1u
  85. #define QTD_PID_SETUP 2u
  86. #define QTD_CERR_3 (3u << 10)
  87. #define QTD_LEN(tok) (((tok) >> 16) & 0x7FFFu)
  88. /* QH endpoint characteristics (EHCI 3.6). */
  89. #define QH_DEVADDR(a) ((a) & 0x7Fu)
  90. #define QH_ENDPT(e) (((e) & 0x0Fu) << 8)
  91. #define QH_EPS_HIGH (2u << 12)
  92. #define QH_DTC (1u << 14)
  93. #define QH_HEAD (1u << 15)
  94. #define QH_MPS(m) (((m) & 0x7FFu) << 16)
  95. #define QH_CTRL_EP (1u << 27)
  96. #define QH_RL(n) (((n) & 7u) << 28)
  97. /* QH endpoint capabilities. */
  98. #define QH_MULT(n) (((n) & 3u) << 30)
  99. /* Link pointers: bit 0 terminates, bits 2:1 select the structure type. */
  100. #define QH_LIST_END 1u
  101. #define QH_NEXT(a) (((a) & ~0x1Fu) | 0x2u)
  102. #define EHCI_MAX_QH (4 + USB_HCD_INT_SLOTS)
  103. #define EHCI_QH_STRIDE 128
  104. #define EHCI_QTD_STRIDE 64
  105. struct ehci_qh {
  106. volatile unsigned int next;
  107. volatile unsigned int info1;
  108. volatile unsigned int info2;
  109. volatile unsigned int current;
  110. volatile unsigned int qtd_next;
  111. volatile unsigned int alt_next;
  112. volatile unsigned int token;
  113. volatile unsigned int buf[5];
  114. volatile unsigned int buf_hi[5]; /* 64-bit addressing extension */
  115. } __attribute__((aligned(32)));
  116. struct ehci_qtd {
  117. volatile unsigned int next;
  118. volatile unsigned int alt_next;
  119. volatile unsigned int token;
  120. volatile unsigned int buf[5];
  121. volatile unsigned int buf_hi[5]; /* 64-bit addressing extension */
  122. } __attribute__((aligned(32)));
  123. struct ehci_bulk {
  124. struct ehci_qh *qh;
  125. struct ehci_qtd *qtd;
  126. struct ehci_qtd *zqtd; /* zero-length packet, bulk OUT only */
  127. unsigned int addr;
  128. unsigned int ep;
  129. unsigned int mps;
  130. unsigned int buf_len;
  131. unsigned int toggle;
  132. int armed;
  133. };
  134. struct ehci_int {
  135. struct ehci_qh *qh;
  136. struct ehci_qtd *qtd;
  137. unsigned char *buf;
  138. unsigned int mps;
  139. unsigned int buf_len;
  140. unsigned int toggle;
  141. int active;
  142. };
  143. struct ehci_ctrl {
  144. unsigned int base;
  145. unsigned int cap_len;
  146. unsigned int bus, dev, func;
  147. unsigned int n_ports;
  148. int ppc;
  149. int last_port;
  150. unsigned int hcs;
  151. unsigned int hcc;
  152. unsigned int frame_list;
  153. unsigned int desc_page;
  154. unsigned int data_page;
  155. struct ehci_qh *head;
  156. struct ehci_qh *ctl_qh;
  157. struct ehci_qh *bin_qh;
  158. struct ehci_qh *bout_qh;
  159. struct ehci_qh *int_qh[USB_HCD_INT_SLOTS];
  160. struct ehci_qtd *ctl_qtd[3];
  161. struct ehci_qtd *bin_qtd;
  162. struct ehci_qtd *bout_qtd;
  163. struct ehci_qtd *bout_zqtd;
  164. struct ehci_qtd *int_qtd[USB_HCD_INT_SLOTS];
  165. unsigned char *setup;
  166. unsigned char *rx_buf;
  167. unsigned char *int_buf[USB_HCD_INT_SLOTS];
  168. struct ehci_bulk bin;
  169. struct ehci_bulk bout;
  170. struct ehci_int ints[USB_HCD_INT_SLOTS];
  171. int ctl_busy;
  172. int recovering;
  173. unsigned char addr_port[128];
  174. };
  175. static struct ehci_ctrl ctrls[USB_HCD_MAX_CTRL];
  176. static unsigned int n_ctrls;
  177. static struct ehci_int *slot_int[USB_HCD_INT_SLOTS];
  178. static int ehci_start(struct ehci_ctrl *c);
  179. /* ---- debug ------------------------------------------------------------- */
  180. #if EHCI_DEBUG
  181. static void dbg(const char *s)
  182. {
  183. serial_puts(SERIAL_COM1, s);
  184. }
  185. #else
  186. static void dbg(const char *s) { (void)s; }
  187. #endif
  188. static unsigned int rd32(unsigned int addr)
  189. {
  190. return *(volatile unsigned int *)addr;
  191. }
  192. static void wr32(unsigned int addr, unsigned int val)
  193. {
  194. *(volatile unsigned int *)addr = val;
  195. }
  196. static unsigned int op_reg(struct ehci_ctrl *c, unsigned int off)
  197. {
  198. return c->base + c->cap_len + off;
  199. }
  200. static unsigned int port_reg(struct ehci_ctrl *c, unsigned int port)
  201. {
  202. return c->base + c->cap_len + EHCI_OP_PORTSC + port * 4u;
  203. }
  204. static void delay_ms(unsigned int ms)
  205. {
  206. unsigned int t0 = get_ticks_ms();
  207. while (get_ticks_ms() - t0 < ms)
  208. ;
  209. }
  210. static void zero_region(unsigned int addr, unsigned int len)
  211. {
  212. unsigned int *p = (unsigned int *)addr;
  213. while (len >= 4) {
  214. *p++ = 0;
  215. len -= 4;
  216. }
  217. }
  218. static void zero_ctrl(struct ehci_ctrl *c)
  219. {
  220. unsigned char *p = (unsigned char *)c;
  221. unsigned int i;
  222. for (i = 0; i < sizeof(*c); i++)
  223. p[i] = 0;
  224. }
  225. /* ---- descriptor helpers ------------------------------------------------ */
  226. static void qh_init(struct ehci_qh *qh)
  227. {
  228. unsigned int i;
  229. qh->info1 = 0;
  230. qh->info2 = 0;
  231. qh->current = 0;
  232. qh->qtd_next = QH_LIST_END;
  233. qh->alt_next = QH_LIST_END;
  234. qh->token = QTD_HALT;
  235. for (i = 0; i < 5; i++) {
  236. qh->buf[i] = 0;
  237. qh->buf_hi[i] = 0;
  238. }
  239. }
  240. static void qh_configure(struct ehci_qh *qh, unsigned int addr,
  241. unsigned int ep, unsigned int mps, int control)
  242. {
  243. qh->info1 = QH_DEVADDR(addr) | QH_ENDPT(ep) | QH_EPS_HIGH |
  244. QH_MPS(mps) | QH_RL(4);
  245. if (control)
  246. qh->info1 |= QH_CTRL_EP | QH_DTC;
  247. qh->info2 = QH_MULT(1);
  248. }
  249. static void qh_arm(struct ehci_qh *qh, struct ehci_qtd *qtd)
  250. {
  251. __asm__ volatile ("" ::: "memory");
  252. qh->qtd_next = (unsigned int)qtd;
  253. qh->alt_next = QH_LIST_END;
  254. qh->token &= (QTD_TOGGLE | QTD_PING);
  255. __asm__ volatile ("" ::: "memory");
  256. }
  257. static void qh_disarm(struct ehci_qh *qh)
  258. {
  259. qh->qtd_next = QH_LIST_END;
  260. qh->token = (qh->token & (QTD_TOGGLE | QTD_PING)) | QTD_HALT;
  261. __asm__ volatile ("" ::: "memory");
  262. }
  263. /* Fill one qTD. Up to five 4 KB pages are described (all current callers
  264. are well below that); the C_PAGE field starts at zero. */
  265. static void qtd_fill(struct ehci_qtd *qtd, const void *buf, unsigned int len,
  266. unsigned int pid, int toggle)
  267. {
  268. unsigned int addr = (unsigned int)buf;
  269. unsigned int left, chunk, i;
  270. qtd->next = QH_LIST_END;
  271. qtd->alt_next = QH_LIST_END;
  272. for (i = 0; i < 5; i++) {
  273. qtd->buf[i] = 0;
  274. qtd->buf_hi[i] = 0;
  275. }
  276. qtd->token = ((len & 0x7FFFu) << 16) |
  277. ((pid & 3u) << 8) | QTD_CERR_3 | QTD_ACTIVE |
  278. (toggle ? QTD_TOGGLE : 0);
  279. if (!len)
  280. return;
  281. qtd->buf[0] = addr;
  282. left = len;
  283. chunk = 0x1000u - (addr & 0xFFFu);
  284. if (chunk > left)
  285. chunk = left;
  286. left -= chunk;
  287. addr += chunk;
  288. i = 1;
  289. while (left && i < 5) {
  290. qtd->buf[i++] = addr & 0xFFFFF000u;
  291. chunk = (left > 0x1000u) ? 0x1000u : left;
  292. left -= chunk;
  293. addr += chunk;
  294. }
  295. }
  296. static int qtd_error(unsigned int token)
  297. {
  298. return (token & QTD_ERRORS) != 0;
  299. }
  300. /* Wait for the qTD to retire. If early is non-NULL and has halted, stop
  301. waiting immediately (a control data-stage STALL leaves the status qTD
  302. armed forever). */
  303. static int qtd_wait(struct ehci_qtd *qtd, struct ehci_qtd *early,
  304. unsigned int ms)
  305. {
  306. unsigned int t0 = get_ticks_ms();
  307. while (qtd->token & QTD_ACTIVE) {
  308. if (early && !(early->token & QTD_ACTIVE) &&
  309. qtd_error(early->token))
  310. return -1;
  311. if (get_ticks_ms() - t0 >= ms)
  312. return -1;
  313. __asm__ volatile ("pause");
  314. }
  315. return 0;
  316. }
  317. /* ---- bulk endpoints ---------------------------------------------------- */
  318. static void bulk_stop(struct ehci_bulk *b)
  319. {
  320. if (b->qh)
  321. qh_disarm(b->qh);
  322. if (b->qtd)
  323. b->qtd->token = 0;
  324. if (b->zqtd)
  325. b->zqtd->token = 0;
  326. b->armed = 0;
  327. }
  328. static void bulk_reset(struct ehci_bulk *b)
  329. {
  330. bulk_stop(b);
  331. b->addr = 0xFFFFFFFFu;
  332. b->ep = 0;
  333. b->mps = 0;
  334. b->buf_len = 0;
  335. b->toggle = 0;
  336. }
  337. static void bulk_configure(struct ehci_bulk *b, unsigned int addr,
  338. unsigned int ep, unsigned int mps,
  339. unsigned int buf_len)
  340. {
  341. if (b->addr == addr && b->ep == ep && b->mps == mps)
  342. return;
  343. bulk_stop(b);
  344. b->addr = addr;
  345. b->ep = ep;
  346. b->mps = mps;
  347. b->buf_len = buf_len;
  348. b->toggle = 0; /* first packet is DATA0 */
  349. qh_init(b->qh); /* keeps the link pointer out of it */
  350. qh_configure(b->qh, addr, ep, mps, 0);
  351. b->qh->token = 0; /* DATA0, not halted */
  352. b->armed = 0;
  353. }
  354. static void bulk_arm(struct ehci_bulk *b)
  355. {
  356. b->qh->token = (b->qh->token & QTD_PING) |
  357. (b->toggle ? QTD_TOGGLE : 0);
  358. qh_arm(b->qh, b->qtd);
  359. b->armed = 1;
  360. }
  361. static int ehci_bulk_out(unsigned int ctrl, unsigned int addr,
  362. unsigned int ep, unsigned int mps,
  363. const unsigned char *buf, unsigned int len)
  364. {
  365. struct ehci_ctrl *c;
  366. struct ehci_bulk *b;
  367. struct ehci_qtd *qtd, *zqtd, *last;
  368. unsigned int token, ztoken = 0;
  369. int zlp;
  370. if (ctrl >= n_ctrls || !buf || !len || len > 0x7FFFu)
  371. return -1;
  372. c = &ctrls[ctrl];
  373. b = &c->bout;
  374. if (!b->qh || !b->qtd || !b->zqtd)
  375. return -1;
  376. if (mps < 1)
  377. mps = 512;
  378. if (mps > 1024)
  379. mps = 1024;
  380. bulk_configure(b, addr, ep, mps, 0);
  381. bulk_stop(b);
  382. qtd = b->qtd;
  383. zqtd = b->zqtd;
  384. qtd_fill(qtd, buf, len, QTD_PID_OUT, 0);
  385. /* A bulk transfer that ends on a max-packet boundary needs a
  386. zero-length packet to mark the end (USB 2.0 5.8.3). */
  387. zlp = (len % mps) == 0;
  388. if (zlp) {
  389. qtd_fill(zqtd, 0, 0, QTD_PID_OUT, 0);
  390. qtd->next = (unsigned int)zqtd;
  391. last = zqtd;
  392. } else {
  393. last = qtd;
  394. }
  395. bulk_arm(b);
  396. if (qtd_wait(last, qtd, 2000) < 0) {
  397. bulk_stop(b);
  398. return -1;
  399. }
  400. token = qtd->token;
  401. if (zlp)
  402. ztoken = last->token;
  403. bulk_stop(b);
  404. if ((token & QTD_ACTIVE) || qtd_error(token) ||
  405. (zlp && ((ztoken & QTD_ACTIVE) || qtd_error(ztoken))))
  406. return -1;
  407. {
  408. unsigned int packets = (len + mps - 1) / mps;
  409. if (zlp)
  410. packets++;
  411. b->toggle ^= packets & 1u;
  412. }
  413. return 0;
  414. }
  415. /* One page of bounce buffer; the caller's buffer stays stable while the
  416. completed transfer is consumed (see usb_hcd.h). */
  417. #define EHCI_RX_MAX 2048u
  418. static int ehci_bulk_in(unsigned int ctrl, unsigned int addr,
  419. unsigned int ep, unsigned int mps,
  420. unsigned char *buf, unsigned int len)
  421. {
  422. struct ehci_ctrl *c;
  423. struct ehci_bulk *b;
  424. unsigned int token, raw, packets, actual, i;
  425. if (ctrl >= n_ctrls || !buf || !len)
  426. return -1;
  427. c = &ctrls[ctrl];
  428. b = &c->bin;
  429. if (!b->qh || !b->qtd || !c->rx_buf)
  430. return -1;
  431. if (mps < 1)
  432. mps = 512;
  433. if (mps > 1024)
  434. mps = 1024;
  435. bulk_configure(b, addr, ep, mps, EHCI_RX_MAX);
  436. if (!b->armed) {
  437. qtd_fill(b->qtd, c->rx_buf, b->buf_len, QTD_PID_IN, 0);
  438. bulk_arm(b);
  439. return 0;
  440. }
  441. token = b->qtd->token;
  442. if (token & QTD_ACTIVE)
  443. return 0;
  444. b->armed = 0;
  445. if (qtd_error(token)) {
  446. bulk_stop(b);
  447. return -1;
  448. }
  449. raw = QTD_LEN(token) <= b->buf_len ?
  450. b->buf_len - QTD_LEN(token) : 0;
  451. actual = raw;
  452. if (actual > len)
  453. actual = len;
  454. for (i = 0; i < actual; i++)
  455. buf[i] = c->rx_buf[i];
  456. /* A bulk IN transfer ends with a short packet; a zero-length
  457. packet also counts as one successful transaction. */
  458. packets = raw ? (raw + b->mps - 1) / b->mps : 1;
  459. b->toggle ^= packets & 1u;
  460. qh_disarm(b->qh);
  461. qtd_fill(b->qtd, c->rx_buf, b->buf_len, QTD_PID_IN, 0);
  462. bulk_arm(b);
  463. return (int)actual;
  464. }
  465. /* ---- interrupt-IN endpoints ------------------------------------------- */
  466. static void int_arm(struct ehci_int *in)
  467. {
  468. in->qh->token = (in->qh->token & QTD_PING) |
  469. (in->toggle ? QTD_TOGGLE : 0);
  470. qh_arm(in->qh, in->qtd);
  471. }
  472. static void ehci_int_in_stop(unsigned int slot)
  473. {
  474. struct ehci_int *in;
  475. if (slot >= USB_HCD_INT_SLOTS)
  476. return;
  477. in = slot_int[slot];
  478. if (!in)
  479. return;
  480. in->active = 0;
  481. if (in->qh)
  482. qh_disarm(in->qh);
  483. if (in->qtd)
  484. in->qtd->token = 0;
  485. slot_int[slot] = 0;
  486. }
  487. static int ehci_int_in_start(unsigned int ctrl, unsigned int slot,
  488. unsigned int addr, unsigned int ep,
  489. unsigned int mps, int low_speed)
  490. {
  491. struct ehci_ctrl *c;
  492. struct ehci_int *in;
  493. if (ctrl >= n_ctrls || slot >= USB_HCD_INT_SLOTS || low_speed)
  494. return -1;
  495. c = &ctrls[ctrl];
  496. in = &c->ints[slot];
  497. if (!in->qh || !in->qtd || !in->buf)
  498. return -1;
  499. ehci_int_in_stop(slot);
  500. in->mps = mps ? mps : 8;
  501. if (in->mps > 1024)
  502. in->mps = 1024;
  503. in->buf_len = in->mps > 64 ? 64 : in->mps;
  504. qh_init(in->qh);
  505. in->qh->info1 = QH_DEVADDR(addr) | QH_ENDPT(ep) | QH_EPS_HIGH |
  506. QH_MPS(in->mps) | QH_RL(4);
  507. in->qh->info2 = QH_MULT(1);
  508. in->qh->token = 0;
  509. in->toggle = 0; /* first report is DATA0 */
  510. qtd_fill(in->qtd, in->buf, in->buf_len, QTD_PID_IN, 0);
  511. int_arm(in);
  512. in->active = 1;
  513. slot_int[slot] = in;
  514. return 0;
  515. }
  516. static int ehci_int_in_poll(unsigned int slot, unsigned char *buf,
  517. unsigned int len)
  518. {
  519. struct ehci_int *in;
  520. unsigned int token, raw, packets, actual, i;
  521. if (slot >= USB_HCD_INT_SLOTS)
  522. return 0;
  523. in = slot_int[slot];
  524. if (!in || !in->active)
  525. return 0;
  526. token = in->qtd->token;
  527. if (token & QTD_ACTIVE)
  528. return 0;
  529. if (qtd_error(token)) {
  530. qh_disarm(in->qh);
  531. qtd_fill(in->qtd, in->buf, in->buf_len, QTD_PID_IN, 0);
  532. int_arm(in);
  533. return -1;
  534. }
  535. raw = QTD_LEN(token) <= in->buf_len ?
  536. in->buf_len - QTD_LEN(token) : 0;
  537. actual = raw;
  538. if (actual > len)
  539. actual = len;
  540. for (i = 0; i < actual; i++)
  541. buf[i] = in->buf[i];
  542. packets = raw ? (raw + in->mps - 1) / in->mps : 1;
  543. in->toggle ^= packets & 1u;
  544. qh_disarm(in->qh);
  545. qtd_fill(in->qtd, in->buf, in->buf_len, QTD_PID_IN, 0);
  546. int_arm(in);
  547. return (int)actual;
  548. }
  549. /* ---- control transfers ------------------------------------------------- */
  550. static int ehci_control(struct ehci_ctrl *c, unsigned int addr,
  551. unsigned int mps, int low_speed,
  552. const unsigned char *setup, unsigned char *data,
  553. unsigned int len, int dir_in)
  554. {
  555. struct ehci_qh *qh;
  556. struct ehci_qtd *q0, *q1, *q2, *last, *early;
  557. unsigned int i, actual = 0, residual;
  558. (void)mps;
  559. if (!c->ctl_qh || !c->setup || !setup || c->ctl_busy || low_speed)
  560. return -1;
  561. if (len && !data)
  562. return -1;
  563. qh = c->ctl_qh;
  564. q0 = c->ctl_qtd[0];
  565. q1 = c->ctl_qtd[1];
  566. q2 = c->ctl_qtd[2];
  567. c->ctl_busy = 1;
  568. for (i = 0; i < 8; i++)
  569. c->setup[i] = setup[i];
  570. qh_init(qh);
  571. qh_configure(qh, addr, 0, 64, 1);
  572. qh->token = 0; /* control toggles come from the qTDs */
  573. qtd_fill(q0, c->setup, 8, QTD_PID_SETUP, 0);
  574. if (len) {
  575. qtd_fill(q1, data, len,
  576. dir_in ? QTD_PID_IN : QTD_PID_OUT, 1);
  577. qtd_fill(q2, 0, 0,
  578. dir_in ? QTD_PID_OUT : QTD_PID_IN, 1);
  579. q0->next = (unsigned int)q1;
  580. q1->next = (unsigned int)q2;
  581. last = q2;
  582. early = q1;
  583. } else {
  584. qtd_fill(q2, 0, 0, QTD_PID_IN, 1);
  585. q0->next = (unsigned int)q2;
  586. last = q2;
  587. early = q0;
  588. }
  589. q2->next = QH_LIST_END;
  590. qh_arm(qh, q0);
  591. if (qtd_wait(last, early, 2000) < 0) {
  592. qh_disarm(qh);
  593. delay_ms(1);
  594. c->ctl_busy = 0;
  595. return -1;
  596. }
  597. qh_disarm(qh);
  598. if (qtd_error(q0->token) || qtd_error(q2->token) ||
  599. (len && qtd_error(q1->token))) {
  600. c->ctl_busy = 0;
  601. return -1;
  602. }
  603. if (len && dir_in) {
  604. residual = QTD_LEN(q1->token);
  605. actual = residual >= len ? 0 : len - residual;
  606. } else if (len) {
  607. actual = len;
  608. }
  609. /* The device changes address after the status stage. Remember which
  610. root port it came from so a port reset can drop this controller's
  611. bulk state without touching an unrelated port. */
  612. if (addr == 0 && setup[0] == 0x00 && setup[1] == 0x05) {
  613. unsigned int new_addr = (unsigned int)setup[2] |
  614. ((unsigned int)setup[3] << 8);
  615. if (new_addr > 0 && new_addr < 128 && c->last_port >= 0)
  616. c->addr_port[new_addr] = (unsigned char)c->last_port;
  617. }
  618. c->ctl_busy = 0;
  619. return (int)actual;
  620. }
  621. static int ehci_control_transfer(unsigned int ctrl, unsigned int addr,
  622. unsigned int mps, int low_speed,
  623. const unsigned char *setup,
  624. unsigned char *data, unsigned int len,
  625. int dir_in)
  626. {
  627. if (ctrl >= n_ctrls)
  628. return -1;
  629. return ehci_control(&ctrls[ctrl], addr, mps, low_speed, setup, data,
  630. len, dir_in);
  631. }
  632. /* ---- root hub ports ---------------------------------------------------- */
  633. static void port_endpoints_reset(struct ehci_ctrl *c, unsigned int port)
  634. {
  635. unsigned int i;
  636. if (c->bin.addr < 128 && c->addr_port[c->bin.addr] == port)
  637. bulk_reset(&c->bin);
  638. if (c->bout.addr < 128 && c->addr_port[c->bout.addr] == port)
  639. bulk_reset(&c->bout);
  640. for (i = 0; i < 128; i++)
  641. if (c->addr_port[i] == port)
  642. c->addr_port[i] = 0xFF;
  643. }
  644. static unsigned int ehci_port_count(unsigned int ctrl)
  645. {
  646. return ctrl < n_ctrls ? ctrls[ctrl].n_ports : 0;
  647. }
  648. static int ehci_port_connected(unsigned int ctrl, unsigned int port)
  649. {
  650. struct ehci_ctrl *c;
  651. unsigned int ps;
  652. if (ctrl >= n_ctrls || port >= ctrls[ctrl].n_ports)
  653. return 0;
  654. c = &ctrls[ctrl];
  655. ps = rd32(port_reg(c, port));
  656. return (ps & PORT_CONNECT) ? 1 : 0;
  657. }
  658. static int ehci_port_low_speed(unsigned int ctrl, unsigned int port)
  659. {
  660. (void)ctrl;
  661. (void)port;
  662. return 0; /* EHCI only drives high-speed devices */
  663. }
  664. static int ehci_port_take_change(unsigned int ctrl, unsigned int port)
  665. {
  666. struct ehci_ctrl *c;
  667. unsigned int ps;
  668. if (ctrl >= n_ctrls || port >= ctrls[ctrl].n_ports)
  669. return 0;
  670. c = &ctrls[ctrl];
  671. ps = rd32(port_reg(c, port));
  672. if (!(ps & PORT_CSC))
  673. return 0;
  674. wr32(port_reg(c, port),
  675. (ps & ~PORT_RWC) | PORT_CSC | PORT_PEC | PORT_OCC);
  676. return 1;
  677. }
  678. static int ehci_port_reset_once(unsigned int ctrl, unsigned int port)
  679. {
  680. struct ehci_ctrl *c;
  681. unsigned int ps, t0, try;
  682. if (ctrl >= n_ctrls || port >= ctrls[ctrl].n_ports)
  683. return -1;
  684. c = &ctrls[ctrl];
  685. /* A halted controller ignores port commands; restart it first. */
  686. if (rd32(op_reg(c, EHCI_OP_USBSTS)) & STS_HALT) {
  687. serial_puts(SERIAL_COM1, "ehci: controller halted, restarting\n");
  688. if (ehci_start(c) < 0)
  689. return -1;
  690. }
  691. ps = rd32(port_reg(c, port));
  692. if (!(ps & PORT_CONNECT))
  693. return -1;
  694. /* Drop any reset/suspend state the firmware left behind. The port
  695. can be stuck with PR set; a single clear write is not enough on
  696. some PCHs. */
  697. for (try = 0; try < 10 && (ps & PORT_RESET); try++) {
  698. wr32(port_reg(c, port),
  699. (ps & ~(PORT_RWC | PORT_RESET | PORT_PE |
  700. PORT_SUSPEND | PORT_RESUME)) |
  701. PORT_CSC | PORT_PEC | PORT_OCC);
  702. delay_ms(5);
  703. ps = rd32(port_reg(c, port));
  704. }
  705. if ((ps & PORT_RESET) || !(ps & PORT_CONNECT))
  706. return -2;
  707. /* The port may still be owned by the companion controller. A
  708. low/full-speed device belongs there; a high-speed one can be taken
  709. over by clearing PORT_OWNER (the controller may need more than one
  710. write while the port is enabled). */
  711. if (ps & PORT_OWNER) {
  712. if (PORT_USB11(ps))
  713. return -1;
  714. for (try = 0; try < 8 && (ps & PORT_OWNER); try++) {
  715. wr32(port_reg(c, port),
  716. (ps & ~(PORT_RWC | PORT_OWNER | PORT_RESET |
  717. PORT_PE | PORT_SUSPEND | PORT_RESUME)) |
  718. PORT_CSC | PORT_PEC | PORT_OCC);
  719. delay_ms(5);
  720. ps = rd32(port_reg(c, port));
  721. }
  722. if ((ps & PORT_OWNER) || !(ps & PORT_CONNECT))
  723. return -1;
  724. }
  725. port_endpoints_reset(c, port);
  726. c->last_port = (int)port;
  727. /* Clear stale change bits, then assert reset. PED is cleared while
  728. reset is asserted (like Linux's SetPortFeature(RESET)). */
  729. wr32(port_reg(c, port),
  730. (ps & ~(PORT_RWC | PORT_PE | PORT_SUSPEND | PORT_RESUME)) |
  731. PORT_CSC | PORT_PEC | PORT_OCC | PORT_RESET);
  732. delay_ms(50);
  733. /* EHCI expects software to end the reset, but some controllers hold
  734. PR set while the device is still chirping. Give it up to 200 ms to
  735. self-clear before forcing the write. */
  736. t0 = get_ticks_ms();
  737. while (get_ticks_ms() - t0 < 200) {
  738. if (!(rd32(port_reg(c, port)) & PORT_RESET))
  739. break;
  740. delay_ms(1);
  741. }
  742. /* Force completion if the controller still holds PR; retry because
  743. the first write can land while the reset is still in progress. */
  744. for (try = 0; try < 20; try++) {
  745. ps = rd32(port_reg(c, port));
  746. if (!(ps & PORT_RESET))
  747. break;
  748. wr32(port_reg(c, port),
  749. (ps & ~(PORT_RWC | PORT_RESET | PORT_PE |
  750. PORT_SUSPEND | PORT_RESUME)) |
  751. PORT_CSC | PORT_PEC | PORT_OCC);
  752. delay_ms(10);
  753. }
  754. if (ps & PORT_RESET) {
  755. serial_puts(SERIAL_COM1, "ehci: reset stuck ");
  756. serial_putdec(SERIAL_COM1, c->bus);
  757. serial_putc(SERIAL_COM1, ':');
  758. serial_putdec(SERIAL_COM1, c->dev);
  759. serial_putc(SERIAL_COM1, '.');
  760. serial_putdec(SERIAL_COM1, c->func);
  761. serial_puts(SERIAL_COM1, " port ");
  762. serial_putdec(SERIAL_COM1, port + 1);
  763. serial_puts(SERIAL_COM1, " ps ");
  764. serial_puthex(SERIAL_COM1, ps);
  765. serial_putc(SERIAL_COM1, '\n');
  766. return -2;
  767. }
  768. t0 = get_ticks_ms();
  769. while (!(rd32(port_reg(c, port)) & PORT_PE)) {
  770. if (get_ticks_ms() - t0 > 200)
  771. break;
  772. }
  773. ps = rd32(port_reg(c, port));
  774. if (!(ps & PORT_PE)) {
  775. if (ps & PORT_OWNER)
  776. return -1;
  777. if (PORT_USB11(ps)) {
  778. /* Low/full speed: make sure the companion owns it. */
  779. wr32(port_reg(c, port),
  780. (ps & ~(PORT_RWC | PORT_PE)) | PORT_OWNER |
  781. PORT_CSC | PORT_PEC | PORT_OCC);
  782. return -1;
  783. }
  784. serial_puts(SERIAL_COM1, "ehci: reset no PED ");
  785. serial_puthex(SERIAL_COM1, ps);
  786. serial_putc(SERIAL_COM1, '\n');
  787. return -2;
  788. }
  789. if (ps & PORT_OWNER)
  790. return -1;
  791. return 0;
  792. }
  793. /* A stuck HS port reset on some Intel PCHs only completes after the whole
  794. EHCI controller is reset. Retry the port reset once in that case. */
  795. static int ehci_port_reset(unsigned int ctrl, unsigned int port)
  796. {
  797. struct ehci_ctrl *c;
  798. int r;
  799. if (ctrl >= n_ctrls || port >= ctrls[ctrl].n_ports)
  800. return -1;
  801. c = &ctrls[ctrl];
  802. r = ehci_port_reset_once(ctrl, port);
  803. if (r == -2 && !c->recovering) {
  804. c->recovering = 1;
  805. serial_puts(SERIAL_COM1,
  806. "ehci: port reset retry after controller reset\n");
  807. if (ehci_start(c) == 0)
  808. r = ehci_port_reset_once(ctrl, port);
  809. else
  810. r = -1;
  811. c->recovering = 0;
  812. }
  813. return r < 0 ? -1 : 0;
  814. }
  815. /* ---- schedule / controller setup -------------------------------------- */
  816. static int ehci_sched_init(struct ehci_ctrl *c)
  817. {
  818. unsigned char *desc = (unsigned char *)c->desc_page;
  819. unsigned int qtd_base = c->desc_page + EHCI_MAX_QH * EHCI_QH_STRIDE;
  820. struct ehci_qh *list[EHCI_MAX_QH];
  821. unsigned int i;
  822. c->head = (struct ehci_qh *)(desc + 0 * EHCI_QH_STRIDE);
  823. c->ctl_qh = (struct ehci_qh *)(desc + 1 * EHCI_QH_STRIDE);
  824. c->bin_qh = (struct ehci_qh *)(desc + 2 * EHCI_QH_STRIDE);
  825. c->bout_qh = (struct ehci_qh *)(desc + 3 * EHCI_QH_STRIDE);
  826. for (i = 0; i < USB_HCD_INT_SLOTS; i++)
  827. c->int_qh[i] = (struct ehci_qh *)(desc +
  828. (4 + i) * EHCI_QH_STRIDE);
  829. c->ctl_qtd[0] = (struct ehci_qtd *)(qtd_base + 0 * EHCI_QTD_STRIDE);
  830. c->ctl_qtd[1] = (struct ehci_qtd *)(qtd_base + 1 * EHCI_QTD_STRIDE);
  831. c->ctl_qtd[2] = (struct ehci_qtd *)(qtd_base + 2 * EHCI_QTD_STRIDE);
  832. c->bin_qtd = (struct ehci_qtd *)(qtd_base + 3 * EHCI_QTD_STRIDE);
  833. c->bout_qtd = (struct ehci_qtd *)(qtd_base + 4 * EHCI_QTD_STRIDE);
  834. c->bout_zqtd = (struct ehci_qtd *)(qtd_base + 11 * EHCI_QTD_STRIDE);
  835. for (i = 0; i < USB_HCD_INT_SLOTS; i++)
  836. c->int_qtd[i] = (struct ehci_qtd *)(qtd_base +
  837. (5 + i) * EHCI_QTD_STRIDE);
  838. c->rx_buf = (unsigned char *)c->data_page;
  839. c->setup = (unsigned char *)(c->data_page + 2048);
  840. for (i = 0; i < USB_HCD_INT_SLOTS; i++)
  841. c->int_buf[i] = (unsigned char *)(c->data_page + 2112 +
  842. i * 64);
  843. for (i = 0; i < 1024; i++)
  844. wr32(c->frame_list + i * 4, QH_LIST_END);
  845. list[0] = c->head;
  846. list[1] = c->ctl_qh;
  847. list[2] = c->bin_qh;
  848. list[3] = c->bout_qh;
  849. for (i = 0; i < USB_HCD_INT_SLOTS; i++)
  850. list[4 + i] = c->int_qh[i];
  851. for (i = 0; i < EHCI_MAX_QH; i++) {
  852. qh_init(list[i]);
  853. list[i]->next = QH_NEXT((unsigned int)
  854. list[(i + 1) % EHCI_MAX_QH]);
  855. }
  856. c->head->info1 = QH_HEAD;
  857. c->head->token = QTD_HALT;
  858. c->bin.qh = c->bin_qh;
  859. c->bin.qtd = c->bin_qtd;
  860. c->bin.zqtd = 0;
  861. c->bin.addr = 0xFFFFFFFFu;
  862. c->bout.qh = c->bout_qh;
  863. c->bout.qtd = c->bout_qtd;
  864. c->bout.zqtd = c->bout_zqtd;
  865. c->bout.addr = 0xFFFFFFFFu;
  866. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  867. c->ints[i].qh = c->int_qh[i];
  868. c->ints[i].qtd = c->int_qtd[i];
  869. c->ints[i].buf = c->int_buf[i];
  870. c->ints[i].active = 0;
  871. }
  872. for (i = 0; i < 128; i++)
  873. c->addr_port[i] = 0xFF;
  874. c->last_port = -1;
  875. return 0;
  876. }
  877. static void ehci_halt(struct ehci_ctrl *c)
  878. {
  879. unsigned int t0;
  880. wr32(op_reg(c, EHCI_OP_USBINTR), 0);
  881. wr32(op_reg(c, EHCI_OP_USBCMD), 0);
  882. t0 = get_ticks_ms();
  883. while (!(rd32(op_reg(c, EHCI_OP_USBSTS)) & STS_HALT)) {
  884. if (get_ticks_ms() - t0 > 100)
  885. break;
  886. }
  887. }
  888. static int ehci_start(struct ehci_ctrl *c)
  889. {
  890. unsigned int v, t0, i;
  891. /* Stop first, then reset. HCRESET self-clears. */
  892. ehci_halt(c);
  893. wr32(op_reg(c, EHCI_OP_USBCMD), CMD_RESET);
  894. t0 = get_ticks_ms();
  895. while (rd32(op_reg(c, EHCI_OP_USBCMD)) & CMD_RESET) {
  896. if (get_ticks_ms() - t0 > 250) {
  897. dbg("ehci: reset timeout\n");
  898. return -1;
  899. }
  900. }
  901. delay_ms(2);
  902. if (c->hcc & 1u) /* 64-bit addressing capable */
  903. wr32(op_reg(c, EHCI_OP_CTRLDSSEGMENT), 0);
  904. wr32(op_reg(c, EHCI_OP_PERIODICLIST), c->frame_list);
  905. wr32(op_reg(c, EHCI_OP_ASYNCLIST), (unsigned int)c->head);
  906. wr32(op_reg(c, EHCI_OP_USBINTR), 0);
  907. wr32(op_reg(c, EHCI_OP_USBSTS), 0x3Fu);
  908. wr32(op_reg(c, EHCI_OP_USBCMD), CMD_RUN | CMD_ASE | CMD_ITC_8);
  909. __asm__ volatile ("" ::: "memory");
  910. t0 = get_ticks_ms();
  911. while (rd32(op_reg(c, EHCI_OP_USBSTS)) & STS_HALT) {
  912. if (get_ticks_ms() - t0 > 100) {
  913. dbg("ehci: controller did not start\n");
  914. return -1;
  915. }
  916. }
  917. /* Take the root ports over from the companion controllers. */
  918. wr32(op_reg(c, EHCI_OP_CONFIGFLAG), 1);
  919. v = rd32(op_reg(c, EHCI_OP_CONFIGFLAG));
  920. if (!(v & 1u))
  921. serial_puts(SERIAL_COM1, "ehci: CONFIGFLAG did not stick\n");
  922. delay_ms(5);
  923. if (c->ppc) {
  924. for (i = 0; i < c->n_ports; i++) {
  925. v = rd32(port_reg(c, i));
  926. if (!(v & PORT_POWER))
  927. wr32(port_reg(c, i),
  928. (v & ~PORT_RWC) | PORT_POWER);
  929. }
  930. delay_ms(100);
  931. }
  932. t0 = get_ticks_ms();
  933. while (!(rd32(op_reg(c, EHCI_OP_USBSTS)) & STS_ASS)) {
  934. if (get_ticks_ms() - t0 > 100)
  935. break;
  936. }
  937. if (!(rd32(op_reg(c, EHCI_OP_USBSTS)) & STS_ASS))
  938. dbg("ehci: async schedule not active\n");
  939. return 0;
  940. }
  941. /* Best-effort EHCI legacy-support handshake: stop the firmware's SMIs from
  942. firing on port changes now that the kernel owns the controller. */
  943. static void legacy_smi_disable(unsigned int bus, unsigned int dev,
  944. unsigned int func, unsigned int hcc)
  945. {
  946. unsigned int eecp = (hcc >> 8) & 0xFFu;
  947. unsigned int guard = 0;
  948. while (eecp >= 0x40 && guard++ < 16) {
  949. unsigned int cap = pci_cfg_read32(bus, dev, func, eecp);
  950. unsigned int id = cap & 0xFFu;
  951. unsigned int next = (cap >> 8) & 0xFFu;
  952. if (id == 1) { /* EHCI Legacy Support */
  953. pci_cfg_write32(bus, dev, func, eecp,
  954. (cap | (1u << 24)) & ~(1u << 31));
  955. cap = pci_cfg_read32(bus, dev, func, eecp + 4);
  956. pci_cfg_write32(bus, dev, func, eecp + 4,
  957. cap & 0xFFFF0000u);
  958. return;
  959. }
  960. if (!next || next == eecp)
  961. return;
  962. eecp = next;
  963. }
  964. }
  965. static int ehci_setup_one(unsigned int bus, unsigned int dev,
  966. unsigned int func, unsigned int bar)
  967. {
  968. struct ehci_ctrl *c;
  969. unsigned int cap_len, hcs, hcc;
  970. if (n_ctrls >= USB_HCD_MAX_CTRL)
  971. return -1;
  972. c = &ctrls[n_ctrls];
  973. zero_ctrl(c);
  974. c->base = bar;
  975. c->bus = bus;
  976. c->dev = dev;
  977. c->func = func;
  978. paging_map_mmio(bar, 0x1000);
  979. if (rd32(bar) == 0xFFFFFFFFu)
  980. return -1;
  981. cap_len = rd32(bar + EHCI_CAP_CAPLENGTH) & 0xFFu;
  982. if (cap_len < 0x20 || cap_len > 0x100)
  983. return -1;
  984. hcs = rd32(bar + EHCI_CAP_HCSPARAMS);
  985. if (hcs == 0xFFFFFFFFu)
  986. return -1;
  987. c->cap_len = cap_len;
  988. c->n_ports = hcs & 0x0Fu;
  989. c->ppc = (hcs & 0x10u) ? 1 : 0;
  990. c->hcs = hcs;
  991. if (!c->n_ports) {
  992. dbg("ehci: no root ports\n");
  993. return -1;
  994. }
  995. hcc = rd32(bar + EHCI_CAP_HCCPARAMS);
  996. c->hcc = hcc;
  997. legacy_smi_disable(bus, dev, func, hcc);
  998. c->frame_list = mm_alloc_page();
  999. c->desc_page = mm_alloc_page();
  1000. c->data_page = mm_alloc_page();
  1001. if (!c->frame_list || !c->desc_page || !c->data_page) {
  1002. dbg("ehci: out of pages\n");
  1003. goto fail;
  1004. }
  1005. zero_region(c->frame_list, 4096);
  1006. zero_region(c->desc_page, 4096);
  1007. zero_region(c->data_page, 4096);
  1008. ehci_sched_init(c);
  1009. if (ehci_start(c) < 0)
  1010. goto fail;
  1011. serial_puts(SERIAL_COM1, "ehci: controller at ");
  1012. serial_putdec(SERIAL_COM1, bus);
  1013. serial_puts(SERIAL_COM1, ":");
  1014. serial_putdec(SERIAL_COM1, dev);
  1015. serial_puts(SERIAL_COM1, ".");
  1016. serial_putdec(SERIAL_COM1, func);
  1017. serial_puts(SERIAL_COM1, " ports ");
  1018. serial_putdec(SERIAL_COM1, c->n_ports);
  1019. serial_puts(SERIAL_COM1, "\n");
  1020. n_ctrls++;
  1021. return 0;
  1022. fail:
  1023. ehci_halt(c);
  1024. /* Give the shared ports back to the companion controllers. */
  1025. wr32(op_reg(c, EHCI_OP_CONFIGFLAG), 0);
  1026. if (c->frame_list)
  1027. mm_free_page(c->frame_list);
  1028. if (c->desc_page)
  1029. mm_free_page(c->desc_page);
  1030. if (c->data_page)
  1031. mm_free_page(c->data_page);
  1032. zero_ctrl(c);
  1033. return -1;
  1034. }
  1035. static int ehci_init(void)
  1036. {
  1037. unsigned int bus, dev, func;
  1038. if (n_ctrls)
  1039. return (int)n_ctrls;
  1040. for (bus = 0; bus < 256 && n_ctrls < USB_HCD_MAX_CTRL; bus++) {
  1041. for (dev = 0; dev < 32; dev++) {
  1042. for (func = 0; func < 8; func++) {
  1043. unsigned int id = pci_cfg_read32(bus, dev, func,
  1044. 0x00);
  1045. unsigned int class_rev, bar, bar_hi, cmd;
  1046. if (id == 0xFFFFFFFFu || id == 0)
  1047. continue;
  1048. class_rev = pci_cfg_read32(bus, dev, func,
  1049. 0x08);
  1050. if ((class_rev >> 16) != 0x0C03u)
  1051. continue;
  1052. if (((class_rev >> 8) & 0xFFu) != 0x20u)
  1053. continue; /* not EHCI */
  1054. bar = pci_cfg_read32(bus, dev, func, 0x10);
  1055. if (bar & 1u)
  1056. continue; /* register block is MMIO */
  1057. bar_hi = 0;
  1058. if (((bar >> 1) & 3u) == 2u)
  1059. bar_hi = pci_cfg_read32(bus, dev, func,
  1060. 0x14);
  1061. bar &= 0xFFFFFFF0u;
  1062. if (!bar || bar_hi)
  1063. continue;
  1064. cmd = pci_cfg_read32(bus, dev, func, 0x04);
  1065. pci_cfg_write32(bus, dev, func, 0x04,
  1066. cmd | 0x0006u);
  1067. ehci_setup_one(bus, dev, func, bar);
  1068. }
  1069. }
  1070. }
  1071. return (int)n_ctrls;
  1072. }
  1073. static unsigned int ehci_count(void)
  1074. {
  1075. return n_ctrls;
  1076. }
  1077. static void ehci_info(unsigned int ctrl, unsigned int *bus, unsigned int *dev,
  1078. unsigned int *func, unsigned int *base, int *mmio)
  1079. {
  1080. struct ehci_ctrl *c = &ctrls[ctrl];
  1081. *bus = c->bus;
  1082. *dev = c->dev;
  1083. *func = c->func;
  1084. *base = c->base;
  1085. *mmio = 1;
  1086. }
  1087. /* Raw PORTSC for `lsusb`; useful when a port is connected but owned by a
  1088. companion (bit 13) or not powered (bit 12). */
  1089. static void ehci_print_ports(unsigned int ctrl)
  1090. {
  1091. struct ehci_ctrl *c;
  1092. unsigned int i, ps, eecp;
  1093. if (ctrl >= n_ctrls)
  1094. return;
  1095. c = &ctrls[ctrl];
  1096. io_putstr(" CMD 0x");
  1097. io_puthex(rd32(op_reg(c, EHCI_OP_USBCMD)));
  1098. io_putstr(" STS 0x");
  1099. io_puthex(rd32(op_reg(c, EHCI_OP_USBSTS)));
  1100. io_putchar('\n');
  1101. io_putstr(" CF 0x");
  1102. io_puthex(rd32(op_reg(c, EHCI_OP_CONFIGFLAG)));
  1103. io_putstr(" HCC 0x");
  1104. io_puthex(c->hcc);
  1105. io_putstr(" HCS 0x");
  1106. io_puthex(c->hcs);
  1107. io_putchar('\n');
  1108. eecp = (c->hcc >> 8) & 0xFFu;
  1109. if (eecp >= 0x40) {
  1110. unsigned int leg = pci_cfg_read32(c->bus, c->dev, c->func,
  1111. eecp);
  1112. if ((leg & 0xFFu) == 1u) {
  1113. io_putstr(" LEGSUP 0x");
  1114. io_puthex(leg);
  1115. io_putchar('\n');
  1116. }
  1117. }
  1118. for (i = 0; i < c->n_ports; i++) {
  1119. ps = rd32(port_reg(c, i));
  1120. io_putstr(" Port ");
  1121. if (i < 9)
  1122. io_putchar((char)('1' + i));
  1123. else
  1124. io_putchar((char)('A' + i - 9));
  1125. io_putstr(": 0x");
  1126. io_puthex(ps);
  1127. io_putchar('\n');
  1128. }
  1129. }
  1130. const struct usb_hcd_ops usb_ehci_ops = {
  1131. "EHCI",
  1132. ehci_init,
  1133. ehci_count,
  1134. ehci_info,
  1135. ehci_control_transfer,
  1136. ehci_port_count,
  1137. ehci_port_connected,
  1138. ehci_port_low_speed,
  1139. ehci_port_reset,
  1140. ehci_port_take_change,
  1141. ehci_int_in_start,
  1142. ehci_int_in_poll,
  1143. ehci_int_in_stop,
  1144. ehci_bulk_out,
  1145. ehci_bulk_in,
  1146. ehci_print_ports,
  1147. };