ehci.c 40 KB

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