uhci.c 13 KB

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