ohci.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /* OHCI (USB 1.1) host controller driver.
  2. Memory-mapped (BAR0) controller with ED/TD lists in the identity-mapped
  3. kernel address space. Polling based (no IRQ): control transfers run on
  4. the control list and are monitored by polling each TD's condition code;
  5. the mouse's interrupt-IN endpoint is a single always-armed TD linked into
  6. every HCCA interrupt-table slot (F = 0 means "every frame"), so the
  7. controller retries NAKs on its own and software only harvests reports.
  8. Up to four controller instances (MCP79 exposes four of them); each PCI
  9. function is one root hub with its own device address space. */
  10. #include "usb.h"
  11. #include "ohci.h"
  12. #include "usb_hcd.h"
  13. #include "pci.h"
  14. #include "paging.h"
  15. #include "mm.h"
  16. #include "serial.h"
  17. #include "timer.h"
  18. /* Register offsets. */
  19. #define R_REVISION 0x00
  20. #define R_CONTROL 0x04
  21. #define R_COMMAND 0x08
  22. #define R_INT_STATUS 0x0C
  23. #define R_INT_ENABLE 0x10
  24. #define R_INT_DISABLE 0x14
  25. #define R_HCCA 0x18
  26. #define R_PERIOD_CUR_ED 0x1C
  27. #define R_CTRL_HEAD_ED 0x20
  28. #define R_CTRL_CUR_ED 0x24
  29. #define R_BULK_HEAD_ED 0x28
  30. #define R_BULK_CUR_ED 0x2C
  31. #define R_DONE_HEAD 0x30
  32. #define R_FM_INTERVAL 0x34
  33. #define R_FM_REMAINING 0x38
  34. #define R_FM_NUMBER 0x3C
  35. #define R_PERIODIC_START 0x40
  36. #define R_LS_THRESHOLD 0x44
  37. #define R_RH_DESC_A 0x48
  38. #define R_RH_DESC_B 0x4C
  39. #define R_RH_STATUS 0x50
  40. #define R_RH_PORT_FIRST 0x54
  41. /* R_CONTROL bits. */
  42. #define CTL_PLE 0x00000004u
  43. #define CTL_CLE 0x00000010u
  44. #define CTL_BLE 0x00000020u
  45. #define CTL_HCFS 0x000000C0u
  46. #define HCFS_RESUME 0x00000040u
  47. #define HCFS_OPER 0x00000080u
  48. #define CTL_IR 0x00000100u /* SMM owns the controller */
  49. /* R_COMMAND bits. */
  50. #define CMD_HCR 0x00000001u
  51. #define CMD_CLF 0x00000002u
  52. #define CMD_OCR 0x00000008u /* request ownership from the SMM */
  53. /* R_INT_STATUS bits. */
  54. #define INTR_WDH 0x00000002u
  55. #define INTR_RHSC 0x00000040u
  56. /* HCCA: DoneHead is the 33rd dword. */
  57. #define HCCA_DONE_HEAD 0x84u
  58. /* R_RH_DESC_A bits. */
  59. #define RHA_PSM 0x00000100u
  60. #define RHA_NPS 0x00000200u
  61. #define RHA_POTPGT 0xFF000000u
  62. #define RHA_NDP 0x000000FFu
  63. /* R_RH_STATUS bits. */
  64. #define RHS_LPSC 0x00010000u
  65. /* R_RH_DESC_B: PortPowerControlMask (which ports are individually
  66. switched when PSM is set). */
  67. #define RHB_PPCM 0xFFFF0000u
  68. /* Root hub port status bits. */
  69. #define PS_CCS 0x00000001u
  70. #define PS_PES 0x00000002u
  71. #define PS_PRS 0x00000010u
  72. #define PS_PPS 0x00000100u /* port power (write 1 to switch power on) */
  73. #define PS_LSDA 0x00000200u
  74. #define PS_CSC 0x00010000u
  75. #define PS_PESC 0x00020000u
  76. #define PS_PRSC 0x00100000u
  77. #define PS_WTC (PS_CSC | PS_PESC | PS_PRSC)
  78. /* Endpoint descriptor (ED) fields. */
  79. #define ED_FA(a) ((a) & 0x7Fu)
  80. #define ED_EN(e) (((e) & 0xFu) << 7)
  81. #define ED_S (1u << 13)
  82. #define ED_MPS(m) (((m) & 0x7FFu) << 16)
  83. #define ED_PTR_MASK 0xFFFFFFF0u
  84. #define ED_H 0x00000001u /* head pointer: halted */
  85. #define ED_C 0x00000002u /* head pointer: data-toggle carry */
  86. /* Transfer descriptor (TD) fields. */
  87. #define TD_R (1u << 18)
  88. #define TD_DP(d) (((d) & 3u) << 19)
  89. #define TD_DP_SETUP 0u
  90. #define TD_DP_OUT 1u
  91. #define TD_DP_IN 2u
  92. /* Data toggle field (bits 24-25): 10b = DATA0, 11b = DATA1. */
  93. #define TD_T_DATA0 0x02000000u
  94. #define TD_T_DATA1 0x03000000u
  95. #define TD_CC_SHIFT 28
  96. #define TD_CC_MASK 0xF0000000u
  97. #define TD_NOTACCESSED 0xFu
  98. /* PCI: class 0x0C (serial), subclass 0x03 (USB), prog.if 0x10 (OHCI). */
  99. #define PCI_USB_CLASS 0x0C03u
  100. #define OHCI_MAX_TD 16
  101. struct ohci_ed {
  102. unsigned int flags;
  103. unsigned int tail;
  104. unsigned int head;
  105. unsigned int next;
  106. } __attribute__((aligned(16)));
  107. struct ohci_td {
  108. unsigned int flags;
  109. unsigned int cbp;
  110. unsigned int next;
  111. unsigned int be;
  112. } __attribute__((aligned(16)));
  113. struct ohci_ctrl {
  114. volatile unsigned int *regs;
  115. unsigned int bus, dev, func, bar;
  116. unsigned int hcca;
  117. unsigned int ports;
  118. unsigned int potpgt;
  119. int nps;
  120. /* Interrupt-IN endpoints: slot 0 = mouse, slot 1 = keyboard. All
  121. slots stay linked into the periodic list; empty EDs are skipped
  122. by the controller. */
  123. struct ohci_ed int_ed[USB_HCD_INT_SLOTS];
  124. struct ohci_td int_td[USB_HCD_INT_SLOTS];
  125. struct ohci_td int_dummy[USB_HCD_INT_SLOTS];
  126. unsigned char int_buf[USB_HCD_INT_SLOTS][8] __attribute__((aligned(16)));
  127. unsigned int int_mps[USB_HCD_INT_SLOTS];
  128. unsigned int int_toggle[USB_HCD_INT_SLOTS];
  129. int int_armed[USB_HCD_INT_SLOTS];
  130. };
  131. static unsigned int n_ctrls;
  132. static int ctl_busy;
  133. /* DMA structures are filled by the CPU and read/written by the controller.
  134. * The SB950's OHCI does not snoop dirty CPU cache lines, so paging_init()
  135. * maps this page uncacheable (see paging.c). One ED/TD set is shared by
  136. * all control transfers (they are serialized by ctl_busy). */
  137. struct ohci_dma {
  138. struct ohci_ed ctrl_ed;
  139. struct ohci_td ctrl_tds[OHCI_MAX_TD];
  140. struct ohci_td ctrl_dummy;
  141. unsigned char setup_buf[8] __attribute__((aligned(16)));
  142. struct ohci_ctrl ctrls[USB_HCD_MAX_CTRL];
  143. };
  144. unsigned char ohci_dma_page[4096] __attribute__((aligned(4096)));
  145. static struct ohci_dma *const dma = (struct ohci_dma *)ohci_dma_page;
  146. typedef char ohci_dma_fits_page[(sizeof(struct ohci_dma) <= 4096) ? 1 : -1];
  147. static void ohci_int_in_stop(unsigned int slot);
  148. static unsigned int rd(struct ohci_ctrl *c, unsigned int ofs)
  149. {
  150. return c->regs[ofs >> 2];
  151. }
  152. static void wr(struct ohci_ctrl *c, unsigned int ofs, unsigned int val)
  153. {
  154. c->regs[ofs >> 2] = val;
  155. }
  156. /* The EDs/TDs live in cacheable RAM while the controller DMAs them. Store
  157. * ordering against the MMIO doorbell is not free: without a full barrier the
  158. * HC can read an ED before the (WB) head-pointer store is visible, see
  159. * HeadP == TailP, skip the transfer and never retire the TD. That was the
  160. * SB950 mouse's intermittent "cursor stops, no error" failure. MFENCE needs
  161. * SSE2, so use the i486-compatible locked no-op. */
  162. static void ohci_barrier(void)
  163. {
  164. __asm__ volatile ("lock; addl $0, 0(%%esp)" ::: "memory");
  165. }
  166. /* The descriptors are uncacheable, but the caller's control data buffer is
  167. * ordinary write-back memory. Flush dirty OUT data before the HC reads it
  168. * and drop stale lines before reading the DMA-written IN data. Control
  169. * transfers are rare (enumeration, SCSI commands), so WBINVD is cheap here;
  170. * the polled HID path never calls it. */
  171. static void ohci_flush(void)
  172. {
  173. __asm__ volatile ("wbinvd" ::: "memory");
  174. }
  175. static void delay_ms(unsigned int ms)
  176. {
  177. usb_delay_ms(ms); /* sleeps inside a sleepable scan */
  178. }
  179. static unsigned int port_reg(unsigned int port)
  180. {
  181. return R_RH_PORT_FIRST + 4u * port;
  182. }
  183. /* Polled driver: retired TDs are harvested from their CC fields, so the
  184. * done list itself is discarded. WDH must still be acknowledged, or the
  185. * HC stops refreshing HCCA.DoneHead and (on this SB950) occasionally
  186. * stops fetching a freshly filled control list. */
  187. static void ohci_ack_done(struct ohci_ctrl *c)
  188. {
  189. if (rd(c, R_INT_STATUS) & (INTR_WDH | INTR_RHSC)) {
  190. if (rd(c, R_INT_STATUS) & INTR_WDH)
  191. ((volatile unsigned int *)c->hcca)[HCCA_DONE_HEAD / 4] = 0;
  192. wr(c, R_INT_STATUS, INTR_WDH | INTR_RHSC);
  193. }
  194. }
  195. /* AMD SB600/SB700/SB800/SB950 SMBus device. Its config 0x64 bit 15 gates
  196. * the southbridge USB SMI: the firmware keeps legacy USB emulation alive
  197. * with it, and with it enabled the SMM can intercept/take back the OHCI
  198. * controllers after the OS has programmed them. Linux/FreeBSD disable it
  199. * once the USB host drivers own the ports. */
  200. static void amd_sb_usb_smi_disable(void)
  201. {
  202. static int done;
  203. unsigned int bus, dev, func;
  204. if (done)
  205. return;
  206. done = 1;
  207. /* The southbridge SMBus function is always on PCI bus 0. Scanning
  208. * only that bus keeps the probe cheap on Intel/QEMU machines, where
  209. * it finds nothing. */
  210. bus = 0;
  211. for (dev = 0; dev < 32; dev++) {
  212. unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
  213. unsigned int htype, nfunc;
  214. if (id == 0xFFFFFFFFu || id == 0)
  215. continue;
  216. htype = (pci_cfg_read32(bus, dev, 0, 0x0C) >> 16) & 0xFFu;
  217. nfunc = (htype & 0x80u) ? 8u : 1u;
  218. for (func = 0; func < nfunc; func++) {
  219. unsigned int idf = pci_cfg_read32(bus, dev, func, 0x00);
  220. unsigned int val;
  221. if (idf == 0xFFFFFFFFu || idf == 0)
  222. continue;
  223. if ((idf & 0xFFFFu) != 0x1002u ||
  224. ((idf >> 16) & 0xFFFFu) != 0x4385u)
  225. continue;
  226. val = pci_cfg_read32(bus, dev, func, 0x64);
  227. if (val & 0x8000u) {
  228. pci_cfg_write32(bus, dev, func, 0x64,
  229. val & ~0x8000u);
  230. serial_puts(SERIAL_COM1,
  231. "ohci: disabled AMD SB USB SMI\n");
  232. }
  233. return;
  234. }
  235. }
  236. }
  237. static int ohci_setup_one(unsigned int bus, unsigned int dev, unsigned int func)
  238. {
  239. struct ohci_ctrl *c;
  240. unsigned int bar, cmd, rha, t0, i;
  241. if (n_ctrls >= USB_HCD_MAX_CTRL)
  242. return -1;
  243. bar = pci_cfg_read32(bus, dev, func, 0x10);
  244. if (bar & 0x1u) {
  245. serial_puts(SERIAL_COM1, "ohci: BAR0 is not MMIO\n");
  246. return -1; /* register block must be MMIO */
  247. }
  248. bar &= 0xFFFFFFF0u;
  249. if (!bar) {
  250. serial_puts(SERIAL_COM1, "ohci: BAR0 is unassigned\n");
  251. return -1;
  252. }
  253. cmd = pci_cfg_read32(bus, dev, func, 0x04);
  254. pci_cfg_write32(bus, dev, func, 0x04, cmd | 0x0006u); /* mem + bus master */
  255. /* Disable legacy SMI emulation (same idea as the UHCI driver). */
  256. pci_cfg_write16(bus, dev, func, 0xC0, 0);
  257. c = &dma->ctrls[n_ctrls];
  258. c->regs = (volatile unsigned int *)bar;
  259. c->bus = bus;
  260. c->dev = dev;
  261. c->func = func;
  262. c->bar = bar;
  263. paging_map_mmio(bar, 0x1000);
  264. if (rd(c, R_REVISION) == 0xFFFFFFFFu || rd(c, R_REVISION) == 0) {
  265. serial_puts(SERIAL_COM1, "ohci: no register response (rev ");
  266. serial_puthex(SERIAL_COM1, rd(c, R_REVISION));
  267. serial_puts(SERIAL_COM1, ")\n");
  268. return -1;
  269. }
  270. /* On AMD southbridges the firmware SMM can still own the controller.
  271. * Request ownership (HcCommandStatus.OCR) and clear HcControl.IR
  272. * before resetting; otherwise the controller ignores our commands
  273. * until the SMM decides to release it. On Intel/QEMU IR is already
  274. * clear, so this is a no-op. */
  275. {
  276. unsigned int ctl = rd(c, R_CONTROL);
  277. if (ctl & CTL_IR) {
  278. serial_puts(SERIAL_COM1,
  279. "ohci: SMM owns controller, requesting change\n");
  280. wr(c, R_COMMAND, CMD_OCR);
  281. wr(c, R_CONTROL, ctl & ~CTL_IR);
  282. t0 = get_ticks_ms();
  283. while (rd(c, R_CONTROL) & CTL_IR) {
  284. if (get_ticks_ms() - t0 > 200)
  285. break;
  286. delay_ms(1);
  287. }
  288. serial_puts(SERIAL_COM1,
  289. (rd(c, R_CONTROL) & CTL_IR) ?
  290. "ohci: SMM did not release\n" :
  291. "ohci: ownership acquired\n");
  292. }
  293. }
  294. c->hcca = mm_alloc_page();
  295. if (!c->hcca) {
  296. serial_puts(SERIAL_COM1, "ohci: no memory for HCCA\n");
  297. return -1;
  298. }
  299. for (i = 0; i < 1024; i++)
  300. ((unsigned int *)c->hcca)[i] = 0;
  301. /* Periodic list: the interrupt EDs are chained and referenced from
  302. every HCCA table slot (F = 0 means "every frame"). Idle queues
  303. are empty (HeadP == TailP == dummy) but carry valid ED fields. */
  304. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  305. c->int_ed[i].flags = ED_FA(0) | ED_EN(0) | ED_MPS(8);
  306. c->int_ed[i].tail = (unsigned int)&c->int_dummy[i];
  307. c->int_ed[i].head = (unsigned int)&c->int_dummy[i];
  308. c->int_ed[i].next = (i + 1 < USB_HCD_INT_SLOTS)
  309. ? (unsigned int)&c->int_ed[i + 1] : 0;
  310. c->int_dummy[i].flags = TD_NOTACCESSED << TD_CC_SHIFT;
  311. c->int_dummy[i].cbp = 0;
  312. c->int_dummy[i].next = 0;
  313. c->int_dummy[i].be = 0;
  314. c->int_armed[i] = 0;
  315. }
  316. for (i = 0; i < 32; i++)
  317. ((unsigned int *)c->hcca)[i] = (unsigned int)&c->int_ed[0];
  318. ohci_barrier();
  319. /* Host controller reset. */
  320. wr(c, R_COMMAND, CMD_HCR);
  321. t0 = get_ticks_ms();
  322. while (rd(c, R_COMMAND) & CMD_HCR) {
  323. if (get_ticks_ms() - t0 > 100)
  324. break;
  325. }
  326. delay_ms(2);
  327. /* Standard frame timing (12 MHz / 1 kHz - 1) and list setup. */
  328. wr(c, R_FM_INTERVAL, (0x2778u << 16) | 0x2EDFu);
  329. wr(c, R_PERIODIC_START, 0x2A2Fu);
  330. wr(c, R_LS_THRESHOLD, 0x628u);
  331. wr(c, R_HCCA, c->hcca);
  332. wr(c, R_INT_DISABLE, 0xFFFFFFFFu);
  333. wr(c, R_INT_STATUS, 0xFFFFFFFFu); /* clear pending */
  334. ((unsigned int *)c->hcca)[HCCA_DONE_HEAD / 4] = 0;
  335. /* Root hub: port count and power. */
  336. rha = rd(c, R_RH_DESC_A);
  337. c->ports = rha & RHA_NDP;
  338. if (c->ports > 15)
  339. c->ports = 15;
  340. if (c->ports == 0) {
  341. serial_puts(SERIAL_COM1, "ohci: no downstream ports\n");
  342. return -1;
  343. }
  344. c->nps = (rha & RHA_NPS) ? 1 : 0;
  345. c->potpgt = (rha & RHA_POTPGT) >> 24;
  346. if (!c->nps) {
  347. /* Power switching is implemented: mark every port as
  348. individually switchable, raise global power and switch on
  349. each port, then let the supply settle (POTPGT in 2 ms
  350. units). */
  351. wr(c, R_RH_DESC_B, RHB_PPCM);
  352. wr(c, R_RH_STATUS, RHS_LPSC);
  353. for (i = 0; i < c->ports; i++)
  354. wr(c, port_reg(i), PS_PPS);
  355. delay_ms(c->potpgt * 2 + 20);
  356. } else {
  357. delay_ms(20);
  358. }
  359. /* Leave reset (spec: via USBRESUME) and enter operational with the
  360. periodic + control lists enabled. */
  361. wr(c, R_CONTROL, HCFS_RESUME);
  362. delay_ms(20);
  363. ohci_barrier();
  364. wr(c, R_CONTROL, HCFS_OPER | CTL_PLE | CTL_CLE);
  365. ohci_barrier();
  366. n_ctrls++;
  367. {
  368. unsigned int conn = 0, pwr = 0, st;
  369. for (i = 0; i < c->ports; i++) {
  370. st = rd(c, port_reg(i));
  371. if (st & PS_CCS)
  372. conn++;
  373. if (st & PS_PPS)
  374. pwr++;
  375. }
  376. serial_puts(SERIAL_COM1, "ohci: controller at ");
  377. serial_putdec(SERIAL_COM1, bus);
  378. serial_puts(SERIAL_COM1, ":");
  379. serial_putdec(SERIAL_COM1, dev);
  380. serial_puts(SERIAL_COM1, ".");
  381. serial_putdec(SERIAL_COM1, func);
  382. serial_puts(SERIAL_COM1, " ports ");
  383. serial_putdec(SERIAL_COM1, c->ports);
  384. serial_puts(SERIAL_COM1, " psm ");
  385. serial_putdec(SERIAL_COM1, (rha & RHA_PSM) ? 1u : 0u);
  386. serial_puts(SERIAL_COM1, " nps ");
  387. serial_putdec(SERIAL_COM1, (unsigned int)c->nps);
  388. serial_puts(SERIAL_COM1, " potpgt ");
  389. serial_putdec(SERIAL_COM1, c->potpgt);
  390. serial_puts(SERIAL_COM1, " conn ");
  391. serial_putdec(SERIAL_COM1, conn);
  392. serial_puts(SERIAL_COM1, " pwr ");
  393. serial_putdec(SERIAL_COM1, pwr);
  394. serial_puts(SERIAL_COM1, "\n");
  395. }
  396. return 0;
  397. }
  398. static int ohci_init(void)
  399. {
  400. unsigned int bus, dev, func;
  401. if (n_ctrls)
  402. return (int)n_ctrls;
  403. amd_sb_usb_smi_disable();
  404. for (bus = 0; bus < 256 && n_ctrls < USB_HCD_MAX_CTRL; bus++) {
  405. for (dev = 0; dev < 32; dev++) {
  406. for (func = 0; func < 8; func++) {
  407. unsigned int id = pci_cfg_read32(bus, dev, func, 0x00);
  408. unsigned int class_rev;
  409. if (id == 0xFFFFFFFFu || id == 0)
  410. continue;
  411. class_rev = pci_cfg_read32(bus, dev, func, 0x08);
  412. if ((class_rev >> 16) != PCI_USB_CLASS)
  413. continue;
  414. if (((class_rev >> 8) & 0xFFu) != 0x10u)
  415. continue; /* not OHCI */
  416. ohci_setup_one(bus, dev, func);
  417. }
  418. }
  419. }
  420. return (int)n_ctrls;
  421. }
  422. static unsigned int ohci_count(void)
  423. {
  424. return n_ctrls;
  425. }
  426. static void ohci_info(unsigned int ctrl, unsigned int *bus, unsigned int *dev,
  427. unsigned int *func, unsigned int *base, int *mmio)
  428. {
  429. struct ohci_ctrl *c = &dma->ctrls[ctrl];
  430. *bus = c->bus;
  431. *dev = c->dev;
  432. *func = c->func;
  433. *base = c->bar;
  434. *mmio = 1;
  435. }
  436. static unsigned int ohci_port_count(unsigned int ctrl)
  437. {
  438. return dma->ctrls[ctrl].ports;
  439. }
  440. static int ohci_port_connected(unsigned int ctrl, unsigned int port)
  441. {
  442. return (rd(&dma->ctrls[ctrl], port_reg(port)) & PS_CCS) ? 1 : 0;
  443. }
  444. static int ohci_port_low_speed(unsigned int ctrl, unsigned int port)
  445. {
  446. return (rd(&dma->ctrls[ctrl], port_reg(port)) & PS_LSDA) ? 1 : 0;
  447. }
  448. static int ohci_port_take_change(unsigned int ctrl, unsigned int port)
  449. {
  450. struct ohci_ctrl *c = &dma->ctrls[ctrl];
  451. unsigned int ofs = port_reg(port);
  452. if (!(rd(c, ofs) & PS_CSC))
  453. return 0;
  454. wr(c, ofs, PS_CSC); /* write-1-to-clear */
  455. return 1;
  456. }
  457. static int ohci_port_reset(unsigned int ctrl, unsigned int port)
  458. {
  459. struct ohci_ctrl *c = &dma->ctrls[ctrl];
  460. unsigned int ofs = port_reg(port);
  461. unsigned int t0, st;
  462. if (!(rd(c, ofs) & PS_CCS))
  463. return -1;
  464. /* Clear stale change bits. */
  465. wr(c, ofs, PS_WTC);
  466. /* Assert reset; the controller clears PRS when the reset finishes
  467. (at least 10 ms for the device to see it). */
  468. wr(c, ofs, PS_PRS);
  469. t0 = get_ticks_ms();
  470. for (;;) {
  471. if (!(rd(c, ofs) & PS_PRS))
  472. break;
  473. if (get_ticks_ms() - t0 > 200)
  474. return -1;
  475. usb_poll_relax(t0);
  476. }
  477. delay_ms(10);
  478. /* Acknowledge reset-complete and make sure the port is enabled. */
  479. wr(c, ofs, PS_PRSC | PS_CSC | PS_PESC);
  480. wr(c, ofs, PS_PES);
  481. st = rd(c, ofs);
  482. if (!(st & PS_CCS) || !(st & PS_PES))
  483. return -1;
  484. return 0;
  485. }
  486. static void td_set(struct ohci_td *td, unsigned int flags, unsigned int cbp,
  487. unsigned int be)
  488. {
  489. td->flags = flags | (TD_NOTACCESSED << TD_CC_SHIFT);
  490. td->cbp = cbp;
  491. td->next = 0;
  492. td->be = be;
  493. }
  494. static int ohci_transfer(struct ohci_ctrl *c, unsigned int addr,
  495. unsigned int mps, int low,
  496. const unsigned char *setup, unsigned char *data,
  497. unsigned int len, int dir_in)
  498. {
  499. unsigned int i, n = 0, n_data = 0, t0;
  500. if (mps == 0 || mps > 64)
  501. mps = 64;
  502. for (i = 0; i < 8; i++)
  503. dma->setup_buf[i] = setup[i];
  504. /* SETUP stage (DATA0). */
  505. td_set(&dma->ctrl_tds[n++], TD_DP(TD_DP_SETUP) | TD_T_DATA0,
  506. (unsigned int)dma->setup_buf, (unsigned int)dma->setup_buf + 7);
  507. /* DATA stage: one TD for the whole buffer, DATA1 (like Linux; the
  508. controller splits it into MPS-sized packets itself). */
  509. if (len > 0) {
  510. td_set(&dma->ctrl_tds[n++],
  511. TD_R | TD_DP(dir_in ? TD_DP_IN : TD_DP_OUT) |
  512. TD_T_DATA1,
  513. (unsigned int)data, (unsigned int)(data + len - 1));
  514. n_data = 1;
  515. }
  516. /* STATUS stage (DATA1, opposite direction, no data). */
  517. td_set(&dma->ctrl_tds[n++],
  518. TD_DP(dir_in ? TD_DP_OUT : TD_DP_IN) | TD_T_DATA1, 0, 0);
  519. /* Chain the TDs and anchor the tail on the dummy TD. */
  520. for (i = 0; i + 1 < n; i++)
  521. dma->ctrl_tds[i].next = (unsigned int)&dma->ctrl_tds[i + 1];
  522. dma->ctrl_tds[n - 1].next = (unsigned int)&dma->ctrl_dummy;
  523. dma->ctrl_dummy.flags = TD_NOTACCESSED << TD_CC_SHIFT;
  524. dma->ctrl_dummy.cbp = 0;
  525. dma->ctrl_dummy.next = 0;
  526. dma->ctrl_dummy.be = 0;
  527. ohci_barrier();
  528. dma->ctrl_ed.flags = ED_FA(addr) | ED_EN(0) | ED_MPS(mps) | (low ? ED_S : 0);
  529. dma->ctrl_ed.tail = (unsigned int)&dma->ctrl_dummy;
  530. dma->ctrl_ed.head = (unsigned int)&dma->ctrl_tds[0];
  531. dma->ctrl_ed.next = 0;
  532. ohci_barrier();
  533. ohci_flush();
  534. wr(c, R_CTRL_HEAD_ED, (unsigned int)&dma->ctrl_ed);
  535. wr(c, R_COMMAND, CMD_CLF);
  536. /* Poll until every TD has left "not accessed". A NAKed TD stays
  537. untouched and is retried by the controller next frame. */
  538. t0 = get_ticks_ms();
  539. for (;;) {
  540. int done = 1, err = 0;
  541. ohci_flush();
  542. ohci_ack_done(c);
  543. for (i = 0; i < n; i++) {
  544. unsigned int cc = (dma->ctrl_tds[i].flags & TD_CC_MASK)
  545. >> TD_CC_SHIFT;
  546. if (cc == TD_NOTACCESSED) {
  547. done = 0;
  548. continue;
  549. }
  550. if (cc != 0) {
  551. err = 1;
  552. serial_puts(SERIAL_COM1, "ohci: xfer err td ");
  553. serial_putdec(SERIAL_COM1, i);
  554. serial_puts(SERIAL_COM1, " cc ");
  555. serial_putdec(SERIAL_COM1, cc);
  556. serial_putc(SERIAL_COM1, '\n');
  557. }
  558. }
  559. if (err) {
  560. serial_puts(SERIAL_COM1, "ohci: xfer err addr ");
  561. serial_putdec(SERIAL_COM1, addr);
  562. serial_puts(SERIAL_COM1, " mps ");
  563. serial_putdec(SERIAL_COM1, mps);
  564. serial_puts(SERIAL_COM1, " low ");
  565. serial_putdec(SERIAL_COM1, (unsigned int)low);
  566. serial_puts(SERIAL_COM1, "\n");
  567. dma->ctrl_ed.head = (unsigned int)&dma->ctrl_dummy;
  568. wr(c, R_CTRL_HEAD_ED, 0);
  569. return -1;
  570. }
  571. if (done)
  572. break;
  573. if (get_ticks_ms() - t0 > 2000) {
  574. serial_puts(SERIAL_COM1, "ohci: xfer timeout addr ");
  575. serial_putdec(SERIAL_COM1, addr);
  576. serial_puts(SERIAL_COM1, " mps ");
  577. serial_putdec(SERIAL_COM1, mps);
  578. serial_puts(SERIAL_COM1, " low ");
  579. serial_putdec(SERIAL_COM1, (unsigned int)low);
  580. for (i = 0; i < n; i++) {
  581. serial_puts(SERIAL_COM1, " td");
  582. serial_putdec(SERIAL_COM1, i);
  583. serial_puts(SERIAL_COM1, " cc ");
  584. serial_putdec(SERIAL_COM1,
  585. (dma->ctrl_tds[i].flags & TD_CC_MASK) >>
  586. TD_CC_SHIFT);
  587. }
  588. /* Controller state: a frozen frame number or a
  589. * cleared CLE/IR says whether the HC is running and
  590. * owns the controller at all. */
  591. serial_puts(SERIAL_COM1, " ctl ");
  592. serial_puthex(SERIAL_COM1, rd(c, R_CONTROL));
  593. serial_puts(SERIAL_COM1, " cmd ");
  594. serial_puthex(SERIAL_COM1, rd(c, R_COMMAND));
  595. serial_puts(SERIAL_COM1, " int ");
  596. serial_puthex(SERIAL_COM1, rd(c, R_INT_STATUS));
  597. serial_puts(SERIAL_COM1, " fm ");
  598. serial_puthex(SERIAL_COM1, rd(c, R_FM_NUMBER));
  599. serial_puts(SERIAL_COM1, " head ");
  600. serial_puthex(SERIAL_COM1, rd(c, R_CTRL_HEAD_ED));
  601. serial_puts(SERIAL_COM1, " ed ");
  602. serial_puthex(SERIAL_COM1, dma->ctrl_ed.flags);
  603. serial_putc(SERIAL_COM1, ' ');
  604. serial_puthex(SERIAL_COM1, dma->ctrl_ed.tail);
  605. serial_putc(SERIAL_COM1, ' ');
  606. serial_puthex(SERIAL_COM1, dma->ctrl_ed.head);
  607. serial_putc(SERIAL_COM1, ' ');
  608. serial_puthex(SERIAL_COM1, dma->ctrl_ed.next);
  609. serial_puts(SERIAL_COM1, " t0 ");
  610. serial_puthex(SERIAL_COM1, dma->ctrl_tds[0].flags);
  611. serial_putc(SERIAL_COM1, ' ');
  612. serial_puthex(SERIAL_COM1, dma->ctrl_tds[0].cbp);
  613. serial_putc(SERIAL_COM1, ' ');
  614. serial_puthex(SERIAL_COM1, dma->ctrl_tds[0].next);
  615. serial_putc(SERIAL_COM1, ' ');
  616. serial_puthex(SERIAL_COM1, dma->ctrl_tds[0].be);
  617. serial_puts(SERIAL_COM1, "\n");
  618. dma->ctrl_ed.head = (unsigned int)&dma->ctrl_dummy;
  619. wr(c, R_CTRL_HEAD_ED, 0);
  620. return -1;
  621. }
  622. usb_poll_relax(t0);
  623. }
  624. dma->ctrl_ed.head = (unsigned int)&dma->ctrl_dummy;
  625. wr(c, R_CTRL_HEAD_ED, 0);
  626. /* Bytes actually received: the TD's CBP advances with the data
  627. (the controller clears it to 0 when the whole buffer arrived). */
  628. if (len > 0 && dir_in && n_data == 1) {
  629. unsigned int cbp = dma->ctrl_tds[1].cbp;
  630. if (cbp == 0)
  631. return (int)len;
  632. return (int)(cbp - (unsigned int)data);
  633. }
  634. return (int)len;
  635. }
  636. static int ohci_control_transfer(unsigned int ctrl, unsigned int addr,
  637. unsigned int mps, int low_speed,
  638. const unsigned char *setup,
  639. unsigned char *data, unsigned int len,
  640. int dir_in)
  641. {
  642. int r;
  643. if (ctrl >= n_ctrls || ctl_busy)
  644. return -1;
  645. ctl_busy = 1;
  646. r = ohci_transfer(&dma->ctrls[ctrl], addr, mps, low_speed, setup, data, len,
  647. dir_in);
  648. ctl_busy = 0;
  649. return r;
  650. }
  651. /* Arm one interrupt TD of one slot: (re)write it active. Only call with
  652. the TD retired. The ED stays linked in the periodic chain. */
  653. static void ohci_int_arm(struct ohci_ctrl *c, unsigned int slot)
  654. {
  655. c->int_td[slot].flags = (TD_NOTACCESSED << TD_CC_SHIFT) | TD_R |
  656. TD_DP(TD_DP_IN) |
  657. (c->int_toggle[slot] ? TD_T_DATA1
  658. : TD_T_DATA0);
  659. c->int_td[slot].cbp = (unsigned int)c->int_buf[slot];
  660. c->int_td[slot].next = (unsigned int)&c->int_dummy[slot];
  661. c->int_td[slot].be = (unsigned int)c->int_buf[slot] +
  662. c->int_mps[slot] - 1;
  663. /* Publish the TD before the ED points at it, and the new head before
  664. * the controller's next pass. */
  665. ohci_barrier();
  666. c->int_ed[slot].head = (unsigned int)&c->int_td[slot] |
  667. (c->int_toggle[slot] ? ED_C : 0);
  668. c->int_ed[slot].tail = (unsigned int)&c->int_dummy[slot];
  669. /* int_ed[slot].next keeps the chain to the other slots. */
  670. ohci_barrier();
  671. c->int_armed[slot] = 1;
  672. }
  673. static int ohci_int_in_start(unsigned int ctrl, unsigned int slot,
  674. unsigned int addr, unsigned int ep,
  675. unsigned int mps, int low_speed)
  676. {
  677. struct ohci_ctrl *c;
  678. if (ctrl >= n_ctrls || slot >= USB_HCD_INT_SLOTS)
  679. return -1;
  680. c = &dma->ctrls[ctrl];
  681. ohci_int_in_stop(slot);
  682. c->int_mps[slot] = mps ? mps : 8;
  683. c->int_ed[slot].flags = ED_FA(addr) | ED_EN(ep) |
  684. ED_MPS(c->int_mps[slot]) |
  685. (low_speed ? ED_S : 0);
  686. c->int_ed[slot].tail = (unsigned int)&c->int_dummy[slot];
  687. c->int_ed[slot].head = (unsigned int)&c->int_dummy[slot];
  688. c->int_toggle[slot] = 0; /* first report is DATA0 */
  689. ohci_int_arm(c, slot);
  690. return 0;
  691. }
  692. static int ohci_int_in_poll(unsigned int slot, unsigned char *buf,
  693. unsigned int len)
  694. {
  695. struct ohci_ctrl *c = 0;
  696. unsigned int i, cc, n;
  697. if (slot >= USB_HCD_INT_SLOTS)
  698. return 0;
  699. for (i = 0; i < n_ctrls; i++) {
  700. if (dma->ctrls[i].int_armed[slot]) {
  701. c = &dma->ctrls[i];
  702. break;
  703. }
  704. }
  705. if (!c)
  706. return 0;
  707. ohci_ack_done(c);
  708. ohci_barrier();
  709. cc = (c->int_td[slot].flags & TD_CC_MASK) >> TD_CC_SHIFT;
  710. if (cc == TD_NOTACCESSED) {
  711. if (c->int_ed[slot].head & ED_H) {
  712. /* Halted by the controller without a CC update:
  713. clear it and stay armed. */
  714. serial_puts(SERIAL_COM1, "ohci: int halt slot ");
  715. serial_putdec(SERIAL_COM1, slot);
  716. serial_puts(SERIAL_COM1, "\n");
  717. ohci_int_arm(c, slot);
  718. return -1;
  719. }
  720. return 0; /* still queued; the controller retries NAKs */
  721. }
  722. if (cc != 0) {
  723. /* Report the error but stay armed (toggle preserved). */
  724. serial_puts(SERIAL_COM1, "ohci: int err slot ");
  725. serial_putdec(SERIAL_COM1, slot);
  726. serial_puts(SERIAL_COM1, " cc ");
  727. serial_putdec(SERIAL_COM1, cc);
  728. serial_puts(SERIAL_COM1, "\n");
  729. ohci_int_arm(c, slot);
  730. return -1;
  731. }
  732. n = c->int_td[slot].cbp
  733. ? (c->int_td[slot].cbp - (unsigned int)c->int_buf[slot])
  734. : c->int_mps[slot];
  735. if (n > len)
  736. n = len;
  737. for (i = 0; i < n; i++)
  738. buf[i] = c->int_buf[slot][i];
  739. c->int_toggle[slot] ^= 1u;
  740. ohci_int_arm(c, slot);
  741. return (int)n;
  742. }
  743. static void ohci_int_in_stop(unsigned int slot)
  744. {
  745. unsigned int i;
  746. if (slot >= USB_HCD_INT_SLOTS)
  747. return;
  748. for (i = 0; i < n_ctrls; i++) {
  749. struct ohci_ctrl *c = &dma->ctrls[i];
  750. if (!c->int_armed[slot])
  751. continue;
  752. c->int_armed[slot] = 0;
  753. c->int_ed[slot].head = (unsigned int)&c->int_dummy[slot];
  754. c->int_ed[slot].tail = (unsigned int)&c->int_dummy[slot];
  755. ohci_barrier();
  756. }
  757. }
  758. const struct usb_hcd_ops usb_ohci_ops = {
  759. "OHCI",
  760. ohci_init,
  761. ohci_count,
  762. ohci_info,
  763. ohci_control_transfer,
  764. ohci_port_count,
  765. ohci_port_connected,
  766. ohci_port_low_speed,
  767. ohci_port_reset,
  768. ohci_port_take_change,
  769. ohci_int_in_start,
  770. ohci_int_in_poll,
  771. ohci_int_in_stop,
  772. 0,
  773. 0,
  774. 0,
  775. };