uhci.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* UHCI (USB 1.1) host controller driver.
  2. PCI discovery, register access, frame-list / queue-head / transfer-
  3. descriptor schedule, and root-hub port control. Polling based (no IRQ);
  4. the PIC slave (and thus the UHCI IRQ) is masked, so we poll TD status. */
  5. #include "pci.h"
  6. #include "timer.h"
  7. #include "uhci.h"
  8. /* Register offsets from the I/O base. */
  9. #define USBCMD 0x00
  10. #define USBSTS 0x02
  11. #define USBINTR 0x04
  12. #define USBFLBASEADD 0x08
  13. #define USBPORTSC1 0x10
  14. #define USBPORTSC2 0x12
  15. /* USBCMD bits. */
  16. #define USBCMD_RS 0x0001
  17. #define USBCMD_HCRESET 0x0002
  18. #define USBCMD_GRESET 0x0004
  19. #define USBCMD_CF 0x0040
  20. #define USBCMD_MAXP 0x0080
  21. /* USBSTS bits. */
  22. #define USBSTS_HCH 0x0020
  23. /* PORTSC bits. */
  24. #define USBPORTSC_CCS 0x0001
  25. #define USBPORTSC_CSC 0x0002
  26. #define USBPORTSC_PE 0x0004
  27. #define USBPORTSC_PEC 0x0008
  28. #define USBPORTSC_LSDA 0x0100
  29. #define USBPORTSC_PR 0x0200
  30. /* TD status bits. */
  31. #define TD_ACTIVE 0x00800000u
  32. #define TD_LS 0x04000000u
  33. #define TD_CERR_3 0x18000000u
  34. #define TD_ERR_MASK 0x007E0000u
  35. #define TD_ACTLEN 0x000007FFu
  36. /* TD token PIDs (UHCI-encoded). */
  37. #define TD_PID_SETUP 0x2Du
  38. #define TD_PID_IN 0x69u
  39. #define TD_PID_OUT 0xE1u
  40. /* PCI: base class 0x0C (serial), subclass 0x03 (USB). */
  41. #define PCI_USB_CLASS 0x0C03u
  42. struct uhci_td {
  43. unsigned int link;
  44. volatile unsigned int status;
  45. unsigned int token;
  46. unsigned int buffer;
  47. } __attribute__((aligned(16)));
  48. struct uhci_qh {
  49. unsigned int link;
  50. unsigned int element;
  51. } __attribute__((aligned(16)));
  52. #define UHCI_NUM_TD 16
  53. /* Frame list lives in low memory (below the kernel at 0x7E00) to avoid
  54. bloating the kernel's BSS past the 0x30000 linker budget. It must be
  55. 4 KB aligned; 0x3000 is free after boot (see AGENTS.md memory map). */
  56. #define UHCI_FRAME_LIST_ADDR 0x3000u
  57. static struct uhci_td tds[UHCI_NUM_TD];
  58. static struct uhci_qh qh;
  59. static unsigned char setup_buf[8];
  60. static unsigned int io_base;
  61. static unsigned int pci_bus, pci_dev, pci_func;
  62. static int found;
  63. static unsigned short reg_readw(unsigned int reg)
  64. {
  65. return port_inw((unsigned short)(io_base + reg));
  66. }
  67. static void reg_writew(unsigned int reg, unsigned short val)
  68. {
  69. port_outw((unsigned short)(io_base + reg), val);
  70. }
  71. static void reg_writel(unsigned int reg, unsigned int val)
  72. {
  73. port_outl((unsigned short)(io_base + reg), val);
  74. }
  75. static void delay_ms(unsigned int ms)
  76. {
  77. unsigned int t0 = get_ticks_ms();
  78. while (get_ticks_ms() - t0 < ms)
  79. ;
  80. }
  81. static void td_set(struct uhci_td *td, unsigned int link, unsigned int status,
  82. unsigned int token, unsigned int buffer)
  83. {
  84. td->link = link;
  85. td->status = status;
  86. td->token = token;
  87. td->buffer = buffer;
  88. }
  89. /* Stop the controller (clear RS) and wait for it to actually halt. */
  90. static void uhci_halt(void)
  91. {
  92. unsigned int t0;
  93. reg_writew(USBCMD, USBCMD_CF | USBCMD_MAXP);
  94. t0 = get_ticks_ms();
  95. while (!(reg_readw(USBSTS) & USBSTS_HCH)) {
  96. if (get_ticks_ms() - t0 > 100)
  97. break;
  98. }
  99. }
  100. static unsigned int td_token(unsigned int pid, unsigned int addr,
  101. unsigned int ep, unsigned int toggle,
  102. unsigned int maxlen)
  103. {
  104. unsigned int explen = (maxlen == 0) ? 0x7FFu
  105. : ((maxlen - 1) & 0x7FFu);
  106. return (pid & 0xFFu) |
  107. ((addr & 0x7Fu) << 8) |
  108. ((ep & 0xFu) << 15) |
  109. ((toggle & 1u) << 19) |
  110. (explen << 21);
  111. }
  112. static void reset_controller(void)
  113. {
  114. unsigned int *fl = (unsigned int *)UHCI_FRAME_LIST_ADDR;
  115. unsigned int i;
  116. reg_writew(USBCMD, USBCMD_GRESET);
  117. delay_ms(50);
  118. reg_writew(USBCMD, 0);
  119. delay_ms(1);
  120. reg_writew(USBCMD, USBCMD_HCRESET);
  121. delay_ms(50);
  122. reg_writew(USBCMD, 0);
  123. delay_ms(1);
  124. for (i = 0; i < 1024; i++)
  125. fl[i] = (unsigned int)&qh | 0x2u;
  126. qh.link = 0x1u;
  127. qh.element = 0x1u;
  128. reg_writel(USBFLBASEADD, UHCI_FRAME_LIST_ADDR);
  129. reg_writew(USBINTR, 0);
  130. reg_writew(USBSTS, 0x3Fu);
  131. reg_writew(USBCMD, USBCMD_CF | USBCMD_MAXP);
  132. }
  133. int uhci_init(void)
  134. {
  135. unsigned int bus, dev;
  136. found = 0;
  137. io_base = 0;
  138. for (bus = 0; bus < 256 && !found; bus++) {
  139. for (dev = 0; dev < 32; dev++) {
  140. unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
  141. unsigned int class_rev, cmd, bar;
  142. if (id == 0xFFFFFFFFu || id == 0)
  143. continue;
  144. class_rev = pci_cfg_read32(bus, dev, 0, 0x08);
  145. if ((class_rev >> 16) != PCI_USB_CLASS)
  146. continue;
  147. if (((class_rev >> 8) & 0xFFu) != 0x00u)
  148. continue;
  149. /* Enable I/O space + bus master. */
  150. cmd = pci_cfg_read32(bus, dev, 0, 0x04);
  151. pci_cfg_write32(bus, dev, 0, 0x04, cmd | 0x0005u);
  152. /* Disable legacy SMI emulation. */
  153. pci_cfg_write16(bus, dev, 0, 0xC0, 0);
  154. bar = pci_cfg_read32(bus, dev, 0, 0x20);
  155. if (!(bar & 0x1u))
  156. continue;
  157. io_base = bar & 0xFFFFFFFCu;
  158. pci_bus = bus;
  159. pci_dev = dev;
  160. pci_func = 0;
  161. found = 1;
  162. break;
  163. }
  164. }
  165. if (!found)
  166. return -1;
  167. reset_controller();
  168. return 0;
  169. }
  170. void uhci_pci_info(unsigned int *bus, unsigned int *dev, unsigned int *func,
  171. unsigned int *base)
  172. {
  173. *bus = pci_bus;
  174. *dev = pci_dev;
  175. *func = pci_func;
  176. *base = io_base;
  177. }
  178. unsigned int uhci_root_port_count(void)
  179. {
  180. return 2;
  181. }
  182. int uhci_root_port_connected(unsigned int port)
  183. {
  184. unsigned int reg = (port == 0) ? USBPORTSC1 : USBPORTSC2;
  185. return (reg_readw(reg) & USBPORTSC_CCS) ? 1 : 0;
  186. }
  187. int uhci_root_port_low_speed(unsigned int port)
  188. {
  189. unsigned int reg = (port == 0) ? USBPORTSC1 : USBPORTSC2;
  190. return (reg_readw(reg) & USBPORTSC_LSDA) ? 1 : 0;
  191. }
  192. int uhci_root_port_reset(unsigned int port)
  193. {
  194. unsigned int reg = (port == 0) ? USBPORTSC1 : USBPORTSC2;
  195. unsigned int st;
  196. /* Assert reset (this also resets the attached device). */
  197. st = reg_readw(reg);
  198. reg_writew(reg, (unsigned short)((st & ~USBPORTSC_PE) | USBPORTSC_PR));
  199. delay_ms(50);
  200. /* Deassert reset and clear the connect/enable change bits. */
  201. st = reg_readw(reg);
  202. reg_writew(reg, (unsigned short)((st & ~USBPORTSC_PR) |
  203. USBPORTSC_CSC | USBPORTSC_PEC));
  204. delay_ms(20);
  205. /* Enable the port: QEMU (and some hardware) require software to
  206. set PE explicitly after a reset. */
  207. st = reg_readw(reg);
  208. reg_writew(reg, (unsigned short)(st | USBPORTSC_PE));
  209. delay_ms(10);
  210. st = reg_readw(reg);
  211. if (!(st & USBPORTSC_PE))
  212. return -1;
  213. return 0;
  214. }
  215. int uhci_control_transfer(unsigned int dev_addr, unsigned int mps, int low_speed,
  216. const unsigned char *setup, unsigned char *data,
  217. unsigned int len, int dir_in)
  218. {
  219. unsigned int i, ntd = 0, n_data = 0;
  220. unsigned int base = TD_ACTIVE | TD_CERR_3 | (low_speed ? TD_LS : 0);
  221. unsigned int t0;
  222. if (!found)
  223. return -1;
  224. if (mps == 0 || mps > 64)
  225. mps = 64;
  226. for (i = 0; i < 8; i++)
  227. setup_buf[i] = setup[i];
  228. /* SETUP stage (8 bytes, DATA0). */
  229. td_set(&tds[ntd++], 0, base, td_token(TD_PID_SETUP, dev_addr, 0, 0, 8),
  230. (unsigned int)setup_buf);
  231. /* DATA stage (split into max-packet-sized TDs, DATA1/DATA0/...). */
  232. if (len > 0) {
  233. unsigned int n = (len + mps - 1) / mps;
  234. unsigned int offset = 0;
  235. unsigned int toggle = 1;
  236. for (i = 0; i < n; i++) {
  237. unsigned int chunk = len - offset;
  238. if (chunk > mps)
  239. chunk = mps;
  240. td_set(&tds[ntd++], 0, base,
  241. td_token(dir_in ? TD_PID_IN : TD_PID_OUT,
  242. dev_addr, 0, toggle, chunk),
  243. (unsigned int)(data + offset));
  244. offset += chunk;
  245. toggle ^= 1u;
  246. }
  247. n_data = n;
  248. }
  249. /* STATUS stage (0 bytes, DATA1). */
  250. td_set(&tds[ntd++], 0, base,
  251. td_token(dir_in ? TD_PID_OUT : TD_PID_IN, dev_addr, 0, 1, 0), 0);
  252. /* Chain the TDs. */
  253. for (i = 0; i + 1 < ntd; i++)
  254. tds[i].link = (unsigned int)&tds[i + 1];
  255. tds[ntd - 1].link = 0x1u;
  256. qh.element = (unsigned int)&tds[0];
  257. /* Run and poll until all TDs complete. */
  258. reg_writew(USBCMD, USBCMD_RS | USBCMD_CF | USBCMD_MAXP);
  259. t0 = get_ticks_ms();
  260. for (;;) {
  261. int done = 1;
  262. for (i = 0; i < ntd; i++) {
  263. unsigned int st = tds[i].status;
  264. if (st & TD_ACTIVE) {
  265. done = 0;
  266. continue;
  267. }
  268. if (st & TD_ERR_MASK) {
  269. uhci_halt();
  270. return -1;
  271. }
  272. }
  273. if (done)
  274. break;
  275. if (get_ticks_ms() - t0 > 2000) {
  276. uhci_halt();
  277. return -1;
  278. }
  279. }
  280. uhci_halt();
  281. {
  282. unsigned int total = 0;
  283. for (i = 0; i < n_data; i++) {
  284. unsigned int st = tds[1 + i].status;
  285. total += ((st & TD_ACTLEN) + 1) & TD_ACTLEN;
  286. }
  287. return (int)total;
  288. }
  289. }