ehci.c 34 KB

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