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 "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. /* Control transfers are built from SETUP + one TD per max-packet chunk +
  61. * STATUS in this fixed array. 32 entries cover a full 2 KB descriptor read
  62. * at 64-byte packets (and the RNDIS 1 KB control/config buffers). */
  63. #define UHCI_NUM_TD 32
  64. /* Frame list lives in low memory (below the kernel at 0x7E00) to avoid
  65. bloating the kernel's BSS past its link-time budget (see kernel.ld). It
  66. must be 4 KB aligned; 0x3000 is free after boot (see AGENTS.md memory
  67. map). */
  68. #define UHCI_FRAME_LIST_ADDR 0x3000u
  69. static struct uhci_td tds[UHCI_NUM_TD];
  70. static struct uhci_qh qh;
  71. static unsigned char setup_buf[8];
  72. /* Interrupt-IN endpoints (slot 0 = mouse, slot 1 = keyboard): one TD and
  73. one scheduled QH each, both serviced every frame. */
  74. struct uhci_int_ep {
  75. struct uhci_td td;
  76. struct uhci_qh qh;
  77. unsigned char buf[8] __attribute__((aligned(16)));
  78. unsigned int addr, ep, mps;
  79. unsigned int toggle;
  80. int low;
  81. int armed;
  82. };
  83. static struct uhci_int_ep int_eps[USB_HCD_INT_SLOTS];
  84. static unsigned int io_base;
  85. static unsigned int pci_bus, pci_dev, pci_func;
  86. static int found;
  87. static int ctl_busy;
  88. static unsigned short reg_readw(unsigned int reg)
  89. {
  90. return port_inw((unsigned short)(io_base + reg));
  91. }
  92. static void reg_writew(unsigned int reg, unsigned short val)
  93. {
  94. port_outw((unsigned short)(io_base + reg), val);
  95. }
  96. static void reg_writel(unsigned int reg, unsigned int val)
  97. {
  98. port_outl((unsigned short)(io_base + reg), val);
  99. }
  100. static void delay_ms(unsigned int ms)
  101. {
  102. unsigned int t0 = get_ticks_ms();
  103. while (get_ticks_ms() - t0 < ms)
  104. ;
  105. }
  106. static void td_set(struct uhci_td *td, unsigned int link, unsigned int status,
  107. unsigned int token, unsigned int buffer)
  108. {
  109. td->link = link;
  110. td->status = status;
  111. td->token = token;
  112. td->buffer = buffer;
  113. }
  114. static unsigned int td_token(unsigned int pid, unsigned int addr,
  115. unsigned int ep, unsigned int toggle,
  116. unsigned int maxlen)
  117. {
  118. unsigned int explen = (maxlen == 0) ? 0x7FFu
  119. : ((maxlen - 1) & 0x7FFu);
  120. return (pid & 0xFFu) |
  121. ((addr & 0x7Fu) << 8) |
  122. ((ep & 0xFu) << 15) |
  123. ((toggle & 1u) << 19) |
  124. (explen << 21);
  125. }
  126. /* Bring the controller up: global + host reset, empty schedule (control QH
  127. followed by the interrupt QH), run state. */
  128. static void reset_controller(void)
  129. {
  130. unsigned int *fl = (unsigned int *)UHCI_FRAME_LIST_ADDR;
  131. unsigned int i;
  132. reg_writew(USBCMD, USBCMD_GRESET);
  133. delay_ms(50);
  134. reg_writew(USBCMD, 0);
  135. delay_ms(1);
  136. reg_writew(USBCMD, USBCMD_HCRESET);
  137. delay_ms(50);
  138. reg_writew(USBCMD, 0);
  139. delay_ms(1);
  140. for (i = 0; i < 1024; i++)
  141. fl[i] = (unsigned int)&qh | 0x2u;
  142. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  143. int_eps[i].qh.element = 0x1u;
  144. int_eps[i].qh.link = (i + 1 < USB_HCD_INT_SLOTS)
  145. ? ((unsigned int)&int_eps[i + 1].qh | 0x2u) : 0x1u;
  146. int_eps[i].td.status = 0;
  147. int_eps[i].armed = 0;
  148. }
  149. qh.link = (unsigned int)&int_eps[0].qh | 0x2u;
  150. qh.element = 0x1u;
  151. reg_writel(USBFLBASEADD, UHCI_FRAME_LIST_ADDR);
  152. reg_writew(USBINTR, 0);
  153. reg_writew(USBSTS, 0x3Fu);
  154. reg_writew(USBCMD, USBCMD_RS | USBCMD_CF | USBCMD_MAXP);
  155. }
  156. static int uhci_init(void)
  157. {
  158. unsigned int bus, dev, func;
  159. unsigned int first_io = 0, first_bus = 0, first_dev = 0, first_func = 0;
  160. int have_first = 0;
  161. if (found)
  162. return 1;
  163. for (bus = 0; bus < 256 && !found; bus++) {
  164. for (dev = 0; dev < 32; dev++) {
  165. for (func = 0; func < 8; func++) {
  166. unsigned int id = pci_cfg_read32(bus, dev, func, 0x00);
  167. unsigned int class_rev, cmd, bar, io, st;
  168. if (id == 0xFFFFFFFFu || id == 0)
  169. continue;
  170. class_rev = pci_cfg_read32(bus, dev, func, 0x08);
  171. if ((class_rev >> 16) != PCI_USB_CLASS)
  172. continue;
  173. if (((class_rev >> 8) & 0xFFu) != 0x00u)
  174. continue; /* not UHCI */
  175. /* Enable I/O space + bus master. */
  176. cmd = pci_cfg_read32(bus, dev, func, 0x04);
  177. pci_cfg_write32(bus, dev, func, 0x04, cmd | 0x0005u);
  178. /* Disable legacy SMI emulation. */
  179. pci_cfg_write16(bus, dev, func, 0xC0, 0);
  180. /* The I/O BAR is BAR4 on UHCI controllers. */
  181. bar = pci_cfg_read32(bus, dev, func, 0x20);
  182. if (!(bar & 0x1u))
  183. continue;
  184. io = bar & 0xFFFFFFFCu;
  185. if (!io)
  186. continue; /* BAR not assigned */
  187. /* Prefer a controller with something plugged in. */
  188. st = (unsigned int)port_inw((unsigned short)(io + USBPORTSC1)) |
  189. (unsigned int)port_inw((unsigned short)(io + USBPORTSC2));
  190. if (st & USBPORTSC_CCS) {
  191. io_base = io;
  192. pci_bus = bus;
  193. pci_dev = dev;
  194. pci_func = func;
  195. found = 1;
  196. break;
  197. }
  198. if (!have_first) {
  199. have_first = 1;
  200. first_io = io;
  201. first_bus = bus;
  202. first_dev = dev;
  203. first_func = func;
  204. }
  205. }
  206. if (found)
  207. break;
  208. }
  209. }
  210. if (!found && have_first) {
  211. io_base = first_io;
  212. pci_bus = first_bus;
  213. pci_dev = first_dev;
  214. pci_func = first_func;
  215. found = 1;
  216. }
  217. if (!found)
  218. return 0;
  219. reset_controller();
  220. serial_puts(SERIAL_COM1, "uhci: controller at ");
  221. serial_putdec(SERIAL_COM1, pci_bus);
  222. serial_puts(SERIAL_COM1, ":");
  223. serial_putdec(SERIAL_COM1, pci_dev);
  224. serial_puts(SERIAL_COM1, ".");
  225. serial_putdec(SERIAL_COM1, pci_func);
  226. serial_puts(SERIAL_COM1, ", 2 ports\n");
  227. return 1;
  228. }
  229. static unsigned int uhci_count(void)
  230. {
  231. return found ? 1u : 0u;
  232. }
  233. static void uhci_info(unsigned int ctrl, unsigned int *bus, unsigned int *dev,
  234. unsigned int *func, unsigned int *base, int *mmio)
  235. {
  236. (void)ctrl;
  237. *bus = pci_bus;
  238. *dev = pci_dev;
  239. *func = pci_func;
  240. *base = io_base;
  241. *mmio = 0;
  242. }
  243. static unsigned int uhci_port_count(unsigned int ctrl)
  244. {
  245. (void)ctrl;
  246. return 2;
  247. }
  248. static int uhci_port_connected(unsigned int ctrl, unsigned int port)
  249. {
  250. unsigned int reg = (port == 0) ? USBPORTSC1 : USBPORTSC2;
  251. (void)ctrl;
  252. return (reg_readw(reg) & USBPORTSC_CCS) ? 1 : 0;
  253. }
  254. static int uhci_port_low_speed(unsigned int ctrl, unsigned int port)
  255. {
  256. unsigned int reg = (port == 0) ? USBPORTSC1 : USBPORTSC2;
  257. (void)ctrl;
  258. return (reg_readw(reg) & USBPORTSC_LSDA) ? 1 : 0;
  259. }
  260. static int uhci_port_take_change(unsigned int ctrl, unsigned int port)
  261. {
  262. unsigned int reg = (port == 0) ? USBPORTSC1 : USBPORTSC2;
  263. (void)ctrl;
  264. if (!(reg_readw(reg) & USBPORTSC_CSC))
  265. return 0;
  266. reg_writew(reg, USBPORTSC_CSC); /* write-1-to-clear */
  267. return 1;
  268. }
  269. static int uhci_port_reset(unsigned int ctrl, unsigned int port)
  270. {
  271. unsigned int reg = (port == 0) ? USBPORTSC1 : USBPORTSC2;
  272. unsigned int st;
  273. (void)ctrl;
  274. /* Assert reset (this also resets the attached device). */
  275. st = reg_readw(reg);
  276. reg_writew(reg, (unsigned short)((st & ~USBPORTSC_PE) | USBPORTSC_PR));
  277. delay_ms(50);
  278. /* Deassert reset and clear the connect/enable change bits. */
  279. st = reg_readw(reg);
  280. reg_writew(reg, (unsigned short)((st & ~USBPORTSC_PR) |
  281. USBPORTSC_CSC | USBPORTSC_PEC));
  282. delay_ms(20);
  283. /* Enable the port: QEMU (and some hardware) require software to
  284. set PE explicitly after a reset. */
  285. st = reg_readw(reg);
  286. reg_writew(reg, (unsigned short)(st | USBPORTSC_PE));
  287. delay_ms(10);
  288. st = reg_readw(reg);
  289. if (!(st & USBPORTSC_PE))
  290. return -1;
  291. return 0;
  292. }
  293. static int control_transfer(unsigned int dev_addr, unsigned int mps,
  294. int low_speed, const unsigned char *setup,
  295. unsigned char *data, unsigned int len, int dir_in)
  296. {
  297. unsigned int i, ntd = 0, n_data = 0;
  298. unsigned int base = TD_ACTIVE | TD_CERR_3 | (low_speed ? TD_LS : 0);
  299. unsigned int t0;
  300. if (!found)
  301. return -1;
  302. if (mps == 0 || mps > 64)
  303. mps = 64;
  304. if (len > (UHCI_NUM_TD - 2) * mps)
  305. return -1;
  306. for (i = 0; i < 8; i++)
  307. setup_buf[i] = setup[i];
  308. /* SETUP stage (8 bytes, DATA0). */
  309. td_set(&tds[ntd++], 0, base, td_token(TD_PID_SETUP, dev_addr, 0, 0, 8),
  310. (unsigned int)setup_buf);
  311. /* DATA stage (split into max-packet-sized TDs, DATA1/DATA0/...). */
  312. if (len > 0) {
  313. unsigned int n = (len + mps - 1) / mps;
  314. unsigned int offset = 0;
  315. unsigned int toggle = 1;
  316. for (i = 0; i < n; i++) {
  317. unsigned int chunk = len - offset;
  318. if (chunk > mps)
  319. chunk = mps;
  320. td_set(&tds[ntd++], 0, base,
  321. td_token(dir_in ? TD_PID_IN : TD_PID_OUT,
  322. dev_addr, 0, toggle, chunk),
  323. (unsigned int)(data + offset));
  324. offset += chunk;
  325. toggle ^= 1u;
  326. }
  327. n_data = n;
  328. }
  329. /* STATUS stage (0 bytes, DATA1). */
  330. td_set(&tds[ntd++], 0, base,
  331. td_token(dir_in ? TD_PID_OUT : TD_PID_IN, dev_addr, 0, 1, 0), 0);
  332. /* Chain the TDs. */
  333. for (i = 0; i + 1 < ntd; i++)
  334. tds[i].link = (unsigned int)&tds[i + 1];
  335. tds[ntd - 1].link = 0x1u;
  336. qh.element = (unsigned int)&tds[0];
  337. /* Run and poll until all TDs complete. */
  338. reg_writew(USBCMD, USBCMD_RS | USBCMD_CF | USBCMD_MAXP);
  339. t0 = get_ticks_ms();
  340. for (;;) {
  341. int done = 1;
  342. for (i = 0; i < ntd; i++) {
  343. unsigned int st = tds[i].status;
  344. if (st & TD_ACTIVE) {
  345. done = 0;
  346. continue;
  347. }
  348. if (st & TD_ERR_MASK) {
  349. qh.element = 0x1u;
  350. return -1;
  351. }
  352. }
  353. if (done)
  354. break;
  355. if (get_ticks_ms() - t0 > 2000) {
  356. qh.element = 0x1u;
  357. return -1;
  358. }
  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. };