uhci.c 13 KB

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