ehci.c 35 KB

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