uhci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /* UHCI (USB 1.1) host controller driver.
  2. PCI discovery, register access, frame-list / queue-head / transfer-
  3. descriptor schedule, root-hub port control, polling control transfers and
  4. one interrupt-IN endpoint (the mouse). Polling based (no IRQ); the PIC
  5. slave (and thus the UHCI IRQ) is masked, so we poll TD status.
  6. Only the first controller instance is driven (the frame list lives at a
  7. fixed low-memory address); among several PCI functions we pick the one
  8. whose root port has a device attached. */
  9. #include "pci.h"
  10. #include "serial.h"
  11. #include "timer.h"
  12. #include "uhci.h"
  13. #include "usb_hcd.h"
  14. /* Register offsets from the I/O base. */
  15. #define USBCMD 0x00
  16. #define USBSTS 0x02
  17. #define USBINTR 0x04
  18. #define USBFLBASEADD 0x08
  19. #define USBPORTSC1 0x10
  20. #define USBPORTSC2 0x12
  21. /* USBCMD bits. */
  22. #define USBCMD_RS 0x0001
  23. #define USBCMD_HCRESET 0x0002
  24. #define USBCMD_GRESET 0x0004
  25. #define USBCMD_CF 0x0040
  26. #define USBCMD_MAXP 0x0080
  27. /* USBSTS bits. */
  28. #define USBSTS_HCH 0x0020
  29. /* PORTSC bits. */
  30. #define USBPORTSC_CCS 0x0001
  31. #define USBPORTSC_CSC 0x0002
  32. #define USBPORTSC_PE 0x0004
  33. #define USBPORTSC_PEC 0x0008
  34. #define USBPORTSC_LSDA 0x0100
  35. #define USBPORTSC_PR 0x0200
  36. /* TD status bits. */
  37. #define TD_ACTIVE 0x00800000u
  38. #define TD_LS 0x04000000u
  39. #define TD_CERR_3 0x18000000u
  40. #define TD_NAK 0x00080000u
  41. #define TD_ERR_MASK 0x007E0000u
  42. #define TD_ERR_NO_NAK (TD_ERR_MASK & ~TD_NAK)
  43. #define TD_ACTLEN 0x000007FFu
  44. /* TD token PIDs (UHCI-encoded). */
  45. #define TD_PID_SETUP 0x2Du
  46. #define TD_PID_IN 0x69u
  47. #define TD_PID_OUT 0xE1u
  48. /* PCI: base class 0x0C (serial), subclass 0x03 (USB), prog.if 0x00 (UHCI). */
  49. #define PCI_USB_CLASS 0x0C03u
  50. struct uhci_td {
  51. unsigned int link;
  52. volatile unsigned int status;
  53. unsigned int token;
  54. unsigned int buffer;
  55. } __attribute__((aligned(16)));
  56. struct uhci_qh {
  57. unsigned int link;
  58. unsigned int element;
  59. } __attribute__((aligned(16)));
  60. #define UHCI_NUM_TD 16
  61. /* Frame list lives in low memory (below the kernel at 0x7E00) to avoid
  62. bloating the kernel's BSS past its link-time budget (see kernel.ld). It
  63. must be 4 KB aligned; 0x3000 is free after boot (see AGENTS.md memory
  64. map). */
  65. #define UHCI_FRAME_LIST_ADDR 0x3000u
  66. static struct uhci_td tds[UHCI_NUM_TD];
  67. static struct uhci_qh qh;
  68. static unsigned char setup_buf[8];
  69. /* Interrupt-IN endpoints (slot 0 = mouse, slot 1 = keyboard): one TD and
  70. one scheduled QH each, both serviced every frame. */
  71. struct uhci_int_ep {
  72. struct uhci_td td;
  73. struct uhci_qh qh;
  74. unsigned char buf[8] __attribute__((aligned(16)));
  75. unsigned int addr, ep, mps;
  76. unsigned int toggle;
  77. int low;
  78. int armed;
  79. };
  80. static struct uhci_int_ep int_eps[USB_HCD_INT_SLOTS];
  81. static unsigned int io_base;
  82. static unsigned int pci_bus, pci_dev, pci_func;
  83. static int found;
  84. static int ctl_busy;
  85. static unsigned short reg_readw(unsigned int reg)
  86. {
  87. return port_inw((unsigned short)(io_base + reg));
  88. }
  89. static void reg_writew(unsigned int reg, unsigned short val)
  90. {
  91. port_outw((unsigned short)(io_base + reg), val);
  92. }
  93. static void reg_writel(unsigned int reg, unsigned int val)
  94. {
  95. port_outl((unsigned short)(io_base + reg), val);
  96. }
  97. static void delay_ms(unsigned int ms)
  98. {
  99. unsigned int t0 = get_ticks_ms();
  100. while (get_ticks_ms() - t0 < ms)
  101. ;
  102. }
  103. static void td_set(struct uhci_td *td, unsigned int link, unsigned int status,
  104. unsigned int token, unsigned int buffer)
  105. {
  106. td->link = link;
  107. td->status = status;
  108. td->token = token;
  109. td->buffer = buffer;
  110. }
  111. static unsigned int td_token(unsigned int pid, unsigned int addr,
  112. unsigned int ep, unsigned int toggle,
  113. unsigned int maxlen)
  114. {
  115. unsigned int explen = (maxlen == 0) ? 0x7FFu
  116. : ((maxlen - 1) & 0x7FFu);
  117. return (pid & 0xFFu) |
  118. ((addr & 0x7Fu) << 8) |
  119. ((ep & 0xFu) << 15) |
  120. ((toggle & 1u) << 19) |
  121. (explen << 21);
  122. }
  123. /* Bring the controller up: global + host reset, empty schedule (control QH
  124. followed by the interrupt QH), run state. */
  125. static void reset_controller(void)
  126. {
  127. unsigned int *fl = (unsigned int *)UHCI_FRAME_LIST_ADDR;
  128. unsigned int i;
  129. reg_writew(USBCMD, USBCMD_GRESET);
  130. delay_ms(50);
  131. reg_writew(USBCMD, 0);
  132. delay_ms(1);
  133. reg_writew(USBCMD, USBCMD_HCRESET);
  134. delay_ms(50);
  135. reg_writew(USBCMD, 0);
  136. delay_ms(1);
  137. for (i = 0; i < 1024; i++)
  138. fl[i] = (unsigned int)&qh | 0x2u;
  139. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  140. int_eps[i].qh.element = 0x1u;
  141. int_eps[i].qh.link = (i + 1 < USB_HCD_INT_SLOTS)
  142. ? ((unsigned int)&int_eps[i + 1].qh | 0x2u) : 0x1u;
  143. int_eps[i].td.status = 0;
  144. int_eps[i].armed = 0;
  145. }
  146. qh.link = (unsigned int)&int_eps[0].qh | 0x2u;
  147. qh.element = 0x1u;
  148. reg_writel(USBFLBASEADD, UHCI_FRAME_LIST_ADDR);
  149. reg_writew(USBINTR, 0);
  150. reg_writew(USBSTS, 0x3Fu);
  151. reg_writew(USBCMD, USBCMD_RS | USBCMD_CF | USBCMD_MAXP);
  152. }
  153. static int uhci_init(void)
  154. {
  155. unsigned int bus, dev, func;
  156. unsigned int first_io = 0, first_bus = 0, first_dev = 0, first_func = 0;
  157. int have_first = 0;
  158. if (found)
  159. return 1;
  160. for (bus = 0; bus < 256 && !found; bus++) {
  161. for (dev = 0; dev < 32; dev++) {
  162. for (func = 0; func < 8; func++) {
  163. unsigned int id = pci_cfg_read32(bus, dev, func, 0x00);
  164. unsigned int class_rev, cmd, bar, io, st;
  165. if (id == 0xFFFFFFFFu || id == 0)
  166. continue;
  167. class_rev = pci_cfg_read32(bus, dev, func, 0x08);
  168. if ((class_rev >> 16) != PCI_USB_CLASS)
  169. continue;
  170. if (((class_rev >> 8) & 0xFFu) != 0x00u)
  171. continue; /* not UHCI */
  172. /* Enable I/O space + bus master. */
  173. cmd = pci_cfg_read32(bus, dev, func, 0x04);
  174. pci_cfg_write32(bus, dev, func, 0x04, cmd | 0x0005u);
  175. /* Disable legacy SMI emulation. */
  176. pci_cfg_write16(bus, dev, func, 0xC0, 0);
  177. /* The I/O BAR is BAR4 on UHCI controllers. */
  178. bar = pci_cfg_read32(bus, dev, func, 0x20);
  179. if (!(bar & 0x1u))
  180. continue;
  181. io = bar & 0xFFFFFFFCu;
  182. if (!io)
  183. continue; /* BAR not assigned */
  184. /* Prefer a controller with something plugged in. */
  185. st = (unsigned int)port_inw((unsigned short)(io + USBPORTSC1)) |
  186. (unsigned int)port_inw((unsigned short)(io + USBPORTSC2));
  187. if (st & USBPORTSC_CCS) {
  188. io_base = io;
  189. pci_bus = bus;
  190. pci_dev = dev;
  191. pci_func = func;
  192. found = 1;
  193. break;
  194. }
  195. if (!have_first) {
  196. have_first = 1;
  197. first_io = io;
  198. first_bus = bus;
  199. first_dev = dev;
  200. first_func = func;
  201. }
  202. }
  203. if (found)
  204. break;
  205. }
  206. }
  207. if (!found && have_first) {
  208. io_base = first_io;
  209. pci_bus = first_bus;
  210. pci_dev = first_dev;
  211. pci_func = first_func;
  212. found = 1;
  213. }
  214. if (!found)
  215. return 0;
  216. reset_controller();
  217. serial_puts(SERIAL_COM1, "uhci: controller at ");
  218. serial_putdec(SERIAL_COM1, pci_bus);
  219. serial_puts(SERIAL_COM1, ":");
  220. serial_putdec(SERIAL_COM1, pci_dev);
  221. serial_puts(SERIAL_COM1, ".");
  222. serial_putdec(SERIAL_COM1, pci_func);
  223. serial_puts(SERIAL_COM1, ", 2 ports\n");
  224. return 1;
  225. }
  226. static unsigned int uhci_count(void)
  227. {
  228. return found ? 1u : 0u;
  229. }
  230. static void uhci_info(unsigned int ctrl, unsigned int *bus, unsigned int *dev,
  231. unsigned int *func, unsigned int *base, int *mmio)
  232. {
  233. (void)ctrl;
  234. *bus = pci_bus;
  235. *dev = pci_dev;
  236. *func = pci_func;
  237. *base = io_base;
  238. *mmio = 0;
  239. }
  240. static unsigned int uhci_port_count(unsigned int ctrl)
  241. {
  242. (void)ctrl;
  243. return 2;
  244. }
  245. static int uhci_port_connected(unsigned int ctrl, unsigned int port)
  246. {
  247. unsigned int reg = (port == 0) ? USBPORTSC1 : USBPORTSC2;
  248. (void)ctrl;
  249. return (reg_readw(reg) & USBPORTSC_CCS) ? 1 : 0;
  250. }
  251. static int uhci_port_low_speed(unsigned int ctrl, unsigned int port)
  252. {
  253. unsigned int reg = (port == 0) ? USBPORTSC1 : USBPORTSC2;
  254. (void)ctrl;
  255. return (reg_readw(reg) & USBPORTSC_LSDA) ? 1 : 0;
  256. }
  257. static int uhci_port_take_change(unsigned int ctrl, unsigned int port)
  258. {
  259. unsigned int reg = (port == 0) ? USBPORTSC1 : USBPORTSC2;
  260. (void)ctrl;
  261. if (!(reg_readw(reg) & USBPORTSC_CSC))
  262. return 0;
  263. reg_writew(reg, USBPORTSC_CSC); /* write-1-to-clear */
  264. return 1;
  265. }
  266. static int uhci_port_reset(unsigned int ctrl, unsigned int port)
  267. {
  268. unsigned int reg = (port == 0) ? USBPORTSC1 : USBPORTSC2;
  269. unsigned int st;
  270. (void)ctrl;
  271. /* Assert reset (this also resets the attached device). */
  272. st = reg_readw(reg);
  273. reg_writew(reg, (unsigned short)((st & ~USBPORTSC_PE) | USBPORTSC_PR));
  274. delay_ms(50);
  275. /* Deassert reset and clear the connect/enable change bits. */
  276. st = reg_readw(reg);
  277. reg_writew(reg, (unsigned short)((st & ~USBPORTSC_PR) |
  278. USBPORTSC_CSC | USBPORTSC_PEC));
  279. delay_ms(20);
  280. /* Enable the port: QEMU (and some hardware) require software to
  281. set PE explicitly after a reset. */
  282. st = reg_readw(reg);
  283. reg_writew(reg, (unsigned short)(st | USBPORTSC_PE));
  284. delay_ms(10);
  285. st = reg_readw(reg);
  286. if (!(st & USBPORTSC_PE))
  287. return -1;
  288. return 0;
  289. }
  290. static int control_transfer(unsigned int dev_addr, unsigned int mps,
  291. int low_speed, const unsigned char *setup,
  292. unsigned char *data, unsigned int len, int dir_in)
  293. {
  294. unsigned int i, ntd = 0, n_data = 0;
  295. unsigned int base = TD_ACTIVE | TD_CERR_3 | (low_speed ? TD_LS : 0);
  296. unsigned int t0;
  297. if (!found)
  298. return -1;
  299. if (mps == 0 || mps > 64)
  300. mps = 64;
  301. for (i = 0; i < 8; i++)
  302. setup_buf[i] = setup[i];
  303. /* SETUP stage (8 bytes, DATA0). */
  304. td_set(&tds[ntd++], 0, base, td_token(TD_PID_SETUP, dev_addr, 0, 0, 8),
  305. (unsigned int)setup_buf);
  306. /* DATA stage (split into max-packet-sized TDs, DATA1/DATA0/...). */
  307. if (len > 0) {
  308. unsigned int n = (len + mps - 1) / mps;
  309. unsigned int offset = 0;
  310. unsigned int toggle = 1;
  311. for (i = 0; i < n; i++) {
  312. unsigned int chunk = len - offset;
  313. if (chunk > mps)
  314. chunk = mps;
  315. td_set(&tds[ntd++], 0, base,
  316. td_token(dir_in ? TD_PID_IN : TD_PID_OUT,
  317. dev_addr, 0, toggle, chunk),
  318. (unsigned int)(data + offset));
  319. offset += chunk;
  320. toggle ^= 1u;
  321. }
  322. n_data = n;
  323. }
  324. /* STATUS stage (0 bytes, DATA1). */
  325. td_set(&tds[ntd++], 0, base,
  326. td_token(dir_in ? TD_PID_OUT : TD_PID_IN, dev_addr, 0, 1, 0), 0);
  327. /* Chain the TDs. */
  328. for (i = 0; i + 1 < ntd; i++)
  329. tds[i].link = (unsigned int)&tds[i + 1];
  330. tds[ntd - 1].link = 0x1u;
  331. qh.element = (unsigned int)&tds[0];
  332. /* Run and poll until all TDs complete. */
  333. reg_writew(USBCMD, USBCMD_RS | USBCMD_CF | USBCMD_MAXP);
  334. t0 = get_ticks_ms();
  335. for (;;) {
  336. int done = 1;
  337. for (i = 0; i < ntd; i++) {
  338. unsigned int st = tds[i].status;
  339. if (st & TD_ACTIVE) {
  340. done = 0;
  341. continue;
  342. }
  343. if (st & TD_ERR_MASK) {
  344. qh.element = 0x1u;
  345. return -1;
  346. }
  347. }
  348. if (done)
  349. break;
  350. if (get_ticks_ms() - t0 > 2000) {
  351. qh.element = 0x1u;
  352. return -1;
  353. }
  354. }
  355. qh.element = 0x1u;
  356. {
  357. unsigned int total = 0;
  358. for (i = 0; i < n_data; i++) {
  359. unsigned int st = tds[1 + i].status;
  360. total += ((st & TD_ACTLEN) + 1) & TD_ACTLEN;
  361. }
  362. return (int)total;
  363. }
  364. }
  365. static int uhci_control_transfer(unsigned int ctrl, unsigned int addr,
  366. unsigned int mps, int low_speed,
  367. const unsigned char *setup,
  368. unsigned char *data, unsigned int len,
  369. int dir_in)
  370. {
  371. int r;
  372. (void)ctrl;
  373. if (ctl_busy)
  374. return -1;
  375. ctl_busy = 1;
  376. r = control_transfer(addr, mps, low_speed, setup, data, len, dir_in);
  377. ctl_busy = 0;
  378. return r;
  379. }
  380. /* Queue the armed interrupt TD of one slot. Only call when it is retired. */
  381. static void int_rearm(struct uhci_int_ep *ep)
  382. {
  383. ep->td.link = 0x1u;
  384. ep->td.buffer = (unsigned int)ep->buf;
  385. ep->td.token = td_token(TD_PID_IN, ep->addr, ep->ep, ep->toggle, ep->mps);
  386. ep->td.status = TD_ACTIVE | TD_CERR_3 | (ep->low ? TD_LS : 0);
  387. ep->qh.element = (unsigned int)&ep->td;
  388. }
  389. static int uhci_int_in_start(unsigned int ctrl, unsigned int slot,
  390. unsigned int addr, unsigned int ep_num,
  391. unsigned int mps, int low_speed)
  392. {
  393. struct uhci_int_ep *ep;
  394. (void)ctrl;
  395. if (!found || slot >= USB_HCD_INT_SLOTS)
  396. return -1;
  397. ep = &int_eps[slot];
  398. /* Drop any old TD from the schedule, then write the new one. */
  399. ep->armed = 0;
  400. ep->qh.element = 0x1u;
  401. delay_ms(2);
  402. ep->addr = addr;
  403. ep->ep = ep_num;
  404. ep->mps = mps ? mps : 8;
  405. ep->low = low_speed;
  406. ep->toggle = 0; /* first report is DATA0 */
  407. int_rearm(ep);
  408. ep->armed = 1;
  409. return 0;
  410. }
  411. static int uhci_int_in_poll(unsigned int slot, unsigned char *buf,
  412. unsigned int len)
  413. {
  414. struct uhci_int_ep *ep;
  415. unsigned int st, n, i;
  416. if (!found || slot >= USB_HCD_INT_SLOTS)
  417. return 0;
  418. ep = &int_eps[slot];
  419. if (!ep->armed)
  420. return 0;
  421. st = ep->td.status;
  422. if (st & TD_ACTIVE)
  423. return 0;
  424. if (st & TD_ERR_NO_NAK) {
  425. /* Report the error but stay armed (toggle preserved). */
  426. int_rearm(ep);
  427. return -1;
  428. }
  429. if (st & TD_NAK) {
  430. /* Some controllers retire a NAKed TD; just retry. */
  431. int_rearm(ep);
  432. return 0;
  433. }
  434. n = ((st & TD_ACTLEN) + 1) & TD_ACTLEN;
  435. if (n > len)
  436. n = len;
  437. for (i = 0; i < n; i++)
  438. buf[i] = ep->buf[i];
  439. ep->toggle ^= 1u;
  440. int_rearm(ep);
  441. return (int)n;
  442. }
  443. static void uhci_int_in_stop(unsigned int slot)
  444. {
  445. struct uhci_int_ep *ep;
  446. if (slot >= USB_HCD_INT_SLOTS)
  447. return;
  448. ep = &int_eps[slot];
  449. ep->armed = 0;
  450. if (found)
  451. ep->qh.element = 0x1u;
  452. }
  453. const struct usb_hcd_ops usb_uhci_ops = {
  454. "UHCI",
  455. uhci_init,
  456. uhci_count,
  457. uhci_info,
  458. uhci_control_transfer,
  459. uhci_port_count,
  460. uhci_port_connected,
  461. uhci_port_low_speed,
  462. uhci_port_reset,
  463. uhci_port_take_change,
  464. uhci_int_in_start,
  465. uhci_int_in_poll,
  466. uhci_int_in_stop,
  467. 0,
  468. 0,
  469. 0,
  470. };