ehci.c 34 KB

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