ehci.c 40 KB

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