ohci.c 25 KB

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