ehci.c 34 KB

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