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 = ioremap(bar, 0x1000, IOREMAP_MMIO);
  259. if (!c->regs)
  260. return -1;
  261. c->bus = bus;
  262. c->dev = dev;
  263. c->func = func;
  264. c->bar = bar;
  265. if (rd(c, R_REVISION) == 0xFFFFFFFFu || rd(c, R_REVISION) == 0) {
  266. serial_puts(SERIAL_COM1, "ohci: no register response (rev ");
  267. serial_puthex(SERIAL_COM1, rd(c, R_REVISION));
  268. serial_puts(SERIAL_COM1, ")\n");
  269. return -1;
  270. }
  271. /* On AMD southbridges the firmware SMM can still own the controller.
  272. * Request ownership (HcCommandStatus.OCR) and clear HcControl.IR
  273. * before resetting; otherwise the controller ignores our commands
  274. * until the SMM decides to release it. On Intel/QEMU IR is already
  275. * clear, so this is a no-op. */
  276. {
  277. unsigned int ctl = rd(c, R_CONTROL);
  278. if (ctl & CTL_IR) {
  279. serial_puts(SERIAL_COM1,
  280. "ohci: SMM owns controller, requesting change\n");
  281. wr(c, R_COMMAND, CMD_OCR);
  282. wr(c, R_CONTROL, ctl & ~CTL_IR);
  283. t0 = get_ticks_ms();
  284. while (rd(c, R_CONTROL) & CTL_IR) {
  285. if (get_ticks_ms() - t0 > 200)
  286. break;
  287. delay_ms(1);
  288. }
  289. serial_puts(SERIAL_COM1,
  290. (rd(c, R_CONTROL) & CTL_IR) ?
  291. "ohci: SMM did not release\n" :
  292. "ohci: ownership acquired\n");
  293. }
  294. }
  295. c->hcca = mm_alloc_page();
  296. if (!c->hcca) {
  297. serial_puts(SERIAL_COM1, "ohci: no memory for HCCA\n");
  298. return -1;
  299. }
  300. for (i = 0; i < 1024; i++)
  301. ((unsigned int *)c->hcca)[i] = 0;
  302. /* Periodic list: the interrupt EDs are chained and referenced from
  303. every HCCA table slot (F = 0 means "every frame"). Idle queues
  304. are empty (HeadP == TailP == dummy) but carry valid ED fields. */
  305. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  306. c->int_ed[i].flags = ED_FA(0) | ED_EN(0) | ED_MPS(8);
  307. c->int_ed[i].tail = (unsigned int)&c->int_dummy[i];
  308. c->int_ed[i].head = (unsigned int)&c->int_dummy[i];
  309. c->int_ed[i].next = (i + 1 < USB_HCD_INT_SLOTS)
  310. ? (unsigned int)&c->int_ed[i + 1] : 0;
  311. c->int_dummy[i].flags = TD_NOTACCESSED << TD_CC_SHIFT;
  312. c->int_dummy[i].cbp = 0;
  313. c->int_dummy[i].next = 0;
  314. c->int_dummy[i].be = 0;
  315. c->int_armed[i] = 0;
  316. }
  317. for (i = 0; i < 32; i++)
  318. ((unsigned int *)c->hcca)[i] = (unsigned int)&c->int_ed[0];
  319. ohci_barrier();
  320. /* Host controller reset. */
  321. wr(c, R_COMMAND, CMD_HCR);
  322. t0 = get_ticks_ms();
  323. while (rd(c, R_COMMAND) & CMD_HCR) {
  324. if (get_ticks_ms() - t0 > 100)
  325. break;
  326. }
  327. delay_ms(2);
  328. /* Standard frame timing (12 MHz / 1 kHz - 1) and list setup. */
  329. wr(c, R_FM_INTERVAL, (0x2778u << 16) | 0x2EDFu);
  330. wr(c, R_PERIODIC_START, 0x2A2Fu);
  331. wr(c, R_LS_THRESHOLD, 0x628u);
  332. wr(c, R_HCCA, c->hcca);
  333. wr(c, R_INT_DISABLE, 0xFFFFFFFFu);
  334. wr(c, R_INT_STATUS, 0xFFFFFFFFu); /* clear pending */
  335. ((unsigned int *)c->hcca)[HCCA_DONE_HEAD / 4] = 0;
  336. /* Root hub: port count and power. */
  337. rha = rd(c, R_RH_DESC_A);
  338. c->ports = rha & RHA_NDP;
  339. if (c->ports > 15)
  340. c->ports = 15;
  341. if (c->ports == 0) {
  342. serial_puts(SERIAL_COM1, "ohci: no downstream ports\n");
  343. return -1;
  344. }
  345. c->nps = (rha & RHA_NPS) ? 1 : 0;
  346. c->potpgt = (rha & RHA_POTPGT) >> 24;
  347. if (!c->nps) {
  348. /* Power switching is implemented: mark every port as
  349. individually switchable, raise global power and switch on
  350. each port, then let the supply settle (POTPGT in 2 ms
  351. units). */
  352. wr(c, R_RH_DESC_B, RHB_PPCM);
  353. wr(c, R_RH_STATUS, RHS_LPSC);
  354. for (i = 0; i < c->ports; i++)
  355. wr(c, port_reg(i), PS_PPS);
  356. delay_ms(c->potpgt * 2 + 20);
  357. } else {
  358. delay_ms(20);
  359. }
  360. /* Leave reset (spec: via USBRESUME) and enter operational with the
  361. periodic + control lists enabled. */
  362. wr(c, R_CONTROL, HCFS_RESUME);
  363. delay_ms(20);
  364. ohci_barrier();
  365. wr(c, R_CONTROL, HCFS_OPER | CTL_PLE | CTL_CLE);
  366. ohci_barrier();
  367. n_ctrls++;
  368. {
  369. unsigned int conn = 0, pwr = 0, st;
  370. for (i = 0; i < c->ports; i++) {
  371. st = rd(c, port_reg(i));
  372. if (st & PS_CCS)
  373. conn++;
  374. if (st & PS_PPS)
  375. pwr++;
  376. }
  377. serial_puts(SERIAL_COM1, "ohci: controller at ");
  378. serial_putdec(SERIAL_COM1, bus);
  379. serial_puts(SERIAL_COM1, ":");
  380. serial_putdec(SERIAL_COM1, dev);
  381. serial_puts(SERIAL_COM1, ".");
  382. serial_putdec(SERIAL_COM1, func);
  383. serial_puts(SERIAL_COM1, " ports ");
  384. serial_putdec(SERIAL_COM1, c->ports);
  385. serial_puts(SERIAL_COM1, " psm ");
  386. serial_putdec(SERIAL_COM1, (rha & RHA_PSM) ? 1u : 0u);
  387. serial_puts(SERIAL_COM1, " nps ");
  388. serial_putdec(SERIAL_COM1, (unsigned int)c->nps);
  389. serial_puts(SERIAL_COM1, " potpgt ");
  390. serial_putdec(SERIAL_COM1, c->potpgt);
  391. serial_puts(SERIAL_COM1, " conn ");
  392. serial_putdec(SERIAL_COM1, conn);
  393. serial_puts(SERIAL_COM1, " pwr ");
  394. serial_putdec(SERIAL_COM1, pwr);
  395. serial_puts(SERIAL_COM1, "\n");
  396. }
  397. return 0;
  398. }
  399. static int ohci_init(void)
  400. {
  401. unsigned int bus, dev, func;
  402. if (n_ctrls)
  403. return (int)n_ctrls;
  404. amd_sb_usb_smi_disable();
  405. for (bus = 0; bus < 256 && n_ctrls < USB_HCD_MAX_CTRL; bus++) {
  406. for (dev = 0; dev < 32; dev++) {
  407. for (func = 0; func < 8; func++) {
  408. unsigned int id = pci_cfg_read32(bus, dev, func, 0x00);
  409. unsigned int class_rev;
  410. if (id == 0xFFFFFFFFu || id == 0)
  411. continue;
  412. class_rev = pci_cfg_read32(bus, dev, func, 0x08);
  413. if ((class_rev >> 16) != PCI_USB_CLASS)
  414. continue;
  415. if (((class_rev >> 8) & 0xFFu) != 0x10u)
  416. continue; /* not OHCI */
  417. ohci_setup_one(bus, dev, func);
  418. }
  419. }
  420. }
  421. return (int)n_ctrls;
  422. }
  423. static unsigned int ohci_count(void)
  424. {
  425. return n_ctrls;
  426. }
  427. static void ohci_info(unsigned int ctrl, unsigned int *bus, unsigned int *dev,
  428. unsigned int *func, unsigned int *base, int *mmio)
  429. {
  430. struct ohci_ctrl *c = &dma->ctrls[ctrl];
  431. *bus = c->bus;
  432. *dev = c->dev;
  433. *func = c->func;
  434. *base = c->bar;
  435. *mmio = 1;
  436. }
  437. static unsigned int ohci_port_count(unsigned int ctrl)
  438. {
  439. return dma->ctrls[ctrl].ports;
  440. }
  441. static int ohci_port_connected(unsigned int ctrl, unsigned int port)
  442. {
  443. return (rd(&dma->ctrls[ctrl], port_reg(port)) & PS_CCS) ? 1 : 0;
  444. }
  445. static int ohci_port_low_speed(unsigned int ctrl, unsigned int port)
  446. {
  447. return (rd(&dma->ctrls[ctrl], port_reg(port)) & PS_LSDA) ? 1 : 0;
  448. }
  449. static int ohci_port_take_change(unsigned int ctrl, unsigned int port)
  450. {
  451. struct ohci_ctrl *c = &dma->ctrls[ctrl];
  452. unsigned int ofs = port_reg(port);
  453. if (!(rd(c, ofs) & PS_CSC))
  454. return 0;
  455. wr(c, ofs, PS_CSC); /* write-1-to-clear */
  456. return 1;
  457. }
  458. static int ohci_port_reset(unsigned int ctrl, unsigned int port)
  459. {
  460. struct ohci_ctrl *c = &dma->ctrls[ctrl];
  461. unsigned int ofs = port_reg(port);
  462. unsigned int t0, st;
  463. if (!(rd(c, ofs) & PS_CCS))
  464. return -1;
  465. /* Clear stale change bits. */
  466. wr(c, ofs, PS_WTC);
  467. /* Assert reset; the controller clears PRS when the reset finishes
  468. (at least 10 ms for the device to see it). */
  469. wr(c, ofs, PS_PRS);
  470. t0 = get_ticks_ms();
  471. for (;;) {
  472. if (!(rd(c, ofs) & PS_PRS))
  473. break;
  474. if (get_ticks_ms() - t0 > 200)
  475. return -1;
  476. usb_poll_relax(t0);
  477. }
  478. delay_ms(10);
  479. /* Acknowledge reset-complete and make sure the port is enabled. */
  480. wr(c, ofs, PS_PRSC | PS_CSC | PS_PESC);
  481. wr(c, ofs, PS_PES);
  482. st = rd(c, ofs);
  483. if (!(st & PS_CCS) || !(st & PS_PES))
  484. return -1;
  485. return 0;
  486. }
  487. static void td_set(struct ohci_td *td, unsigned int flags, unsigned int cbp,
  488. unsigned int be)
  489. {
  490. td->flags = flags | (TD_NOTACCESSED << TD_CC_SHIFT);
  491. td->cbp = cbp;
  492. td->next = 0;
  493. td->be = be;
  494. }
  495. static int ohci_transfer(struct ohci_ctrl *c, unsigned int addr,
  496. unsigned int mps, int low,
  497. const unsigned char *setup, unsigned char *data,
  498. unsigned int len, int dir_in)
  499. {
  500. unsigned int i, n = 0, n_data = 0, t0;
  501. if (mps == 0 || mps > 64)
  502. mps = 64;
  503. for (i = 0; i < 8; i++)
  504. dma->setup_buf[i] = setup[i];
  505. /* SETUP stage (DATA0). */
  506. td_set(&dma->ctrl_tds[n++], TD_DP(TD_DP_SETUP) | TD_T_DATA0,
  507. (unsigned int)dma->setup_buf, (unsigned int)dma->setup_buf + 7);
  508. /* DATA stage: one TD for the whole buffer, DATA1 (like Linux; the
  509. controller splits it into MPS-sized packets itself). */
  510. if (len > 0) {
  511. td_set(&dma->ctrl_tds[n++],
  512. TD_R | TD_DP(dir_in ? TD_DP_IN : TD_DP_OUT) |
  513. TD_T_DATA1,
  514. (unsigned int)data, (unsigned int)(data + len - 1));
  515. n_data = 1;
  516. }
  517. /* STATUS stage (DATA1, opposite direction, no data). */
  518. td_set(&dma->ctrl_tds[n++],
  519. TD_DP(dir_in ? TD_DP_OUT : TD_DP_IN) | TD_T_DATA1, 0, 0);
  520. /* Chain the TDs and anchor the tail on the dummy TD. */
  521. for (i = 0; i + 1 < n; i++)
  522. dma->ctrl_tds[i].next = (unsigned int)&dma->ctrl_tds[i + 1];
  523. dma->ctrl_tds[n - 1].next = (unsigned int)&dma->ctrl_dummy;
  524. dma->ctrl_dummy.flags = TD_NOTACCESSED << TD_CC_SHIFT;
  525. dma->ctrl_dummy.cbp = 0;
  526. dma->ctrl_dummy.next = 0;
  527. dma->ctrl_dummy.be = 0;
  528. ohci_barrier();
  529. dma->ctrl_ed.flags = ED_FA(addr) | ED_EN(0) | ED_MPS(mps) | (low ? ED_S : 0);
  530. dma->ctrl_ed.tail = (unsigned int)&dma->ctrl_dummy;
  531. dma->ctrl_ed.head = (unsigned int)&dma->ctrl_tds[0];
  532. dma->ctrl_ed.next = 0;
  533. ohci_barrier();
  534. ohci_flush();
  535. wr(c, R_CTRL_HEAD_ED, (unsigned int)&dma->ctrl_ed);
  536. wr(c, R_COMMAND, CMD_CLF);
  537. /* Poll until every TD has left "not accessed". A NAKed TD stays
  538. untouched and is retried by the controller next frame. */
  539. t0 = get_ticks_ms();
  540. for (;;) {
  541. int done = 1, err = 0;
  542. ohci_flush();
  543. ohci_ack_done(c);
  544. for (i = 0; i < n; i++) {
  545. unsigned int cc = (dma->ctrl_tds[i].flags & TD_CC_MASK)
  546. >> TD_CC_SHIFT;
  547. if (cc == TD_NOTACCESSED) {
  548. done = 0;
  549. continue;
  550. }
  551. if (cc != 0) {
  552. err = 1;
  553. serial_puts(SERIAL_COM1, "ohci: xfer err td ");
  554. serial_putdec(SERIAL_COM1, i);
  555. serial_puts(SERIAL_COM1, " cc ");
  556. serial_putdec(SERIAL_COM1, cc);
  557. serial_putc(SERIAL_COM1, '\n');
  558. }
  559. }
  560. if (err) {
  561. serial_puts(SERIAL_COM1, "ohci: xfer err addr ");
  562. serial_putdec(SERIAL_COM1, addr);
  563. serial_puts(SERIAL_COM1, " mps ");
  564. serial_putdec(SERIAL_COM1, mps);
  565. serial_puts(SERIAL_COM1, " low ");
  566. serial_putdec(SERIAL_COM1, (unsigned int)low);
  567. serial_puts(SERIAL_COM1, "\n");
  568. dma->ctrl_ed.head = (unsigned int)&dma->ctrl_dummy;
  569. wr(c, R_CTRL_HEAD_ED, 0);
  570. return -1;
  571. }
  572. if (done)
  573. break;
  574. if (get_ticks_ms() - t0 > 2000) {
  575. serial_puts(SERIAL_COM1, "ohci: xfer timeout addr ");
  576. serial_putdec(SERIAL_COM1, addr);
  577. serial_puts(SERIAL_COM1, " mps ");
  578. serial_putdec(SERIAL_COM1, mps);
  579. serial_puts(SERIAL_COM1, " low ");
  580. serial_putdec(SERIAL_COM1, (unsigned int)low);
  581. for (i = 0; i < n; i++) {
  582. serial_puts(SERIAL_COM1, " td");
  583. serial_putdec(SERIAL_COM1, i);
  584. serial_puts(SERIAL_COM1, " cc ");
  585. serial_putdec(SERIAL_COM1,
  586. (dma->ctrl_tds[i].flags & TD_CC_MASK) >>
  587. TD_CC_SHIFT);
  588. }
  589. /* Controller state: a frozen frame number or a
  590. * cleared CLE/IR says whether the HC is running and
  591. * owns the controller at all. */
  592. serial_puts(SERIAL_COM1, " ctl ");
  593. serial_puthex(SERIAL_COM1, rd(c, R_CONTROL));
  594. serial_puts(SERIAL_COM1, " cmd ");
  595. serial_puthex(SERIAL_COM1, rd(c, R_COMMAND));
  596. serial_puts(SERIAL_COM1, " int ");
  597. serial_puthex(SERIAL_COM1, rd(c, R_INT_STATUS));
  598. serial_puts(SERIAL_COM1, " fm ");
  599. serial_puthex(SERIAL_COM1, rd(c, R_FM_NUMBER));
  600. serial_puts(SERIAL_COM1, " head ");
  601. serial_puthex(SERIAL_COM1, rd(c, R_CTRL_HEAD_ED));
  602. serial_puts(SERIAL_COM1, " ed ");
  603. serial_puthex(SERIAL_COM1, dma->ctrl_ed.flags);
  604. serial_putc(SERIAL_COM1, ' ');
  605. serial_puthex(SERIAL_COM1, dma->ctrl_ed.tail);
  606. serial_putc(SERIAL_COM1, ' ');
  607. serial_puthex(SERIAL_COM1, dma->ctrl_ed.head);
  608. serial_putc(SERIAL_COM1, ' ');
  609. serial_puthex(SERIAL_COM1, dma->ctrl_ed.next);
  610. serial_puts(SERIAL_COM1, " t0 ");
  611. serial_puthex(SERIAL_COM1, dma->ctrl_tds[0].flags);
  612. serial_putc(SERIAL_COM1, ' ');
  613. serial_puthex(SERIAL_COM1, dma->ctrl_tds[0].cbp);
  614. serial_putc(SERIAL_COM1, ' ');
  615. serial_puthex(SERIAL_COM1, dma->ctrl_tds[0].next);
  616. serial_putc(SERIAL_COM1, ' ');
  617. serial_puthex(SERIAL_COM1, dma->ctrl_tds[0].be);
  618. serial_puts(SERIAL_COM1, "\n");
  619. dma->ctrl_ed.head = (unsigned int)&dma->ctrl_dummy;
  620. wr(c, R_CTRL_HEAD_ED, 0);
  621. return -1;
  622. }
  623. usb_poll_relax(t0);
  624. }
  625. dma->ctrl_ed.head = (unsigned int)&dma->ctrl_dummy;
  626. wr(c, R_CTRL_HEAD_ED, 0);
  627. /* Bytes actually received: the TD's CBP advances with the data
  628. (the controller clears it to 0 when the whole buffer arrived). */
  629. if (len > 0 && dir_in && n_data == 1) {
  630. unsigned int cbp = dma->ctrl_tds[1].cbp;
  631. if (cbp == 0)
  632. return (int)len;
  633. return (int)(cbp - (unsigned int)data);
  634. }
  635. return (int)len;
  636. }
  637. static int ohci_control_transfer(unsigned int ctrl, unsigned int addr,
  638. unsigned int mps, int low_speed,
  639. const unsigned char *setup,
  640. unsigned char *data, unsigned int len,
  641. int dir_in)
  642. {
  643. int r;
  644. if (ctrl >= n_ctrls || ctl_busy)
  645. return -1;
  646. ctl_busy = 1;
  647. r = ohci_transfer(&dma->ctrls[ctrl], addr, mps, low_speed, setup, data, len,
  648. dir_in);
  649. ctl_busy = 0;
  650. return r;
  651. }
  652. /* Arm one interrupt TD of one slot: (re)write it active. Only call with
  653. the TD retired. The ED stays linked in the periodic chain. */
  654. static void ohci_int_arm(struct ohci_ctrl *c, unsigned int slot)
  655. {
  656. c->int_td[slot].flags = (TD_NOTACCESSED << TD_CC_SHIFT) | TD_R |
  657. TD_DP(TD_DP_IN) |
  658. (c->int_toggle[slot] ? TD_T_DATA1
  659. : TD_T_DATA0);
  660. c->int_td[slot].cbp = (unsigned int)c->int_buf[slot];
  661. c->int_td[slot].next = (unsigned int)&c->int_dummy[slot];
  662. c->int_td[slot].be = (unsigned int)c->int_buf[slot] +
  663. c->int_mps[slot] - 1;
  664. /* Publish the TD before the ED points at it, and the new head before
  665. * the controller's next pass. */
  666. ohci_barrier();
  667. c->int_ed[slot].head = (unsigned int)&c->int_td[slot] |
  668. (c->int_toggle[slot] ? ED_C : 0);
  669. c->int_ed[slot].tail = (unsigned int)&c->int_dummy[slot];
  670. /* int_ed[slot].next keeps the chain to the other slots. */
  671. ohci_barrier();
  672. c->int_armed[slot] = 1;
  673. }
  674. static int ohci_int_in_start(unsigned int ctrl, unsigned int slot,
  675. unsigned int addr, unsigned int ep,
  676. unsigned int mps, int low_speed)
  677. {
  678. struct ohci_ctrl *c;
  679. if (ctrl >= n_ctrls || slot >= USB_HCD_INT_SLOTS)
  680. return -1;
  681. c = &dma->ctrls[ctrl];
  682. ohci_int_in_stop(slot);
  683. c->int_mps[slot] = mps ? mps : 8;
  684. c->int_ed[slot].flags = ED_FA(addr) | ED_EN(ep) |
  685. ED_MPS(c->int_mps[slot]) |
  686. (low_speed ? ED_S : 0);
  687. c->int_ed[slot].tail = (unsigned int)&c->int_dummy[slot];
  688. c->int_ed[slot].head = (unsigned int)&c->int_dummy[slot];
  689. c->int_toggle[slot] = 0; /* first report is DATA0 */
  690. ohci_int_arm(c, slot);
  691. return 0;
  692. }
  693. static int ohci_int_in_poll(unsigned int slot, unsigned char *buf,
  694. unsigned int len)
  695. {
  696. struct ohci_ctrl *c = 0;
  697. unsigned int i, cc, n;
  698. if (slot >= USB_HCD_INT_SLOTS)
  699. return 0;
  700. for (i = 0; i < n_ctrls; i++) {
  701. if (dma->ctrls[i].int_armed[slot]) {
  702. c = &dma->ctrls[i];
  703. break;
  704. }
  705. }
  706. if (!c)
  707. return 0;
  708. ohci_ack_done(c);
  709. ohci_barrier();
  710. cc = (c->int_td[slot].flags & TD_CC_MASK) >> TD_CC_SHIFT;
  711. if (cc == TD_NOTACCESSED) {
  712. if (c->int_ed[slot].head & ED_H) {
  713. /* Halted by the controller without a CC update:
  714. clear it and stay armed. */
  715. serial_puts(SERIAL_COM1, "ohci: int halt slot ");
  716. serial_putdec(SERIAL_COM1, slot);
  717. serial_puts(SERIAL_COM1, "\n");
  718. ohci_int_arm(c, slot);
  719. return -1;
  720. }
  721. return 0; /* still queued; the controller retries NAKs */
  722. }
  723. if (cc != 0) {
  724. /* Report the error but stay armed (toggle preserved). */
  725. serial_puts(SERIAL_COM1, "ohci: int err slot ");
  726. serial_putdec(SERIAL_COM1, slot);
  727. serial_puts(SERIAL_COM1, " cc ");
  728. serial_putdec(SERIAL_COM1, cc);
  729. serial_puts(SERIAL_COM1, "\n");
  730. ohci_int_arm(c, slot);
  731. return -1;
  732. }
  733. n = c->int_td[slot].cbp
  734. ? (c->int_td[slot].cbp - (unsigned int)c->int_buf[slot])
  735. : c->int_mps[slot];
  736. if (n > len)
  737. n = len;
  738. for (i = 0; i < n; i++)
  739. buf[i] = c->int_buf[slot][i];
  740. c->int_toggle[slot] ^= 1u;
  741. ohci_int_arm(c, slot);
  742. return (int)n;
  743. }
  744. static void ohci_int_in_stop(unsigned int slot)
  745. {
  746. unsigned int i;
  747. if (slot >= USB_HCD_INT_SLOTS)
  748. return;
  749. for (i = 0; i < n_ctrls; i++) {
  750. struct ohci_ctrl *c = &dma->ctrls[i];
  751. if (!c->int_armed[slot])
  752. continue;
  753. c->int_armed[slot] = 0;
  754. c->int_ed[slot].head = (unsigned int)&c->int_dummy[slot];
  755. c->int_ed[slot].tail = (unsigned int)&c->int_dummy[slot];
  756. ohci_barrier();
  757. }
  758. }
  759. const struct usb_hcd_ops usb_ohci_ops = {
  760. "OHCI",
  761. ohci_init,
  762. ohci_count,
  763. ohci_info,
  764. ohci_control_transfer,
  765. ohci_port_count,
  766. ohci_port_connected,
  767. ohci_port_low_speed,
  768. ohci_port_reset,
  769. ohci_port_take_change,
  770. ohci_int_in_start,
  771. ohci_int_in_poll,
  772. ohci_int_in_stop,
  773. 0,
  774. 0,
  775. 0,
  776. };