You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ehci.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. #ifndef _IPXE_EHCI_H
  2. #define _IPXE_EHCI_H
  3. /** @file
  4. *
  5. * USB Enhanced Host Controller Interface (EHCI) driver
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <ipxe/pci.h>
  10. #include <ipxe/usb.h>
  11. /** Minimum alignment required for data structures
  12. *
  13. * With the exception of the periodic frame list (which is
  14. * page-aligned), data structures used by EHCI generally require
  15. * 32-byte alignment and must not cross a 4kB page boundary. We
  16. * simplify this requirement by aligning each structure on its own
  17. * size, with a minimum of a 32 byte alignment.
  18. */
  19. #define EHCI_MIN_ALIGN 32
  20. /** Maximum transfer size
  21. *
  22. * EHCI allows for transfers of up to 20kB with page-alignment, or
  23. * 16kB with arbitrary alignment.
  24. */
  25. #define EHCI_MTU 16384
  26. /** Page-alignment required for some data structures */
  27. #define EHCI_PAGE_ALIGN 4096
  28. /** EHCI PCI BAR */
  29. #define EHCI_BAR PCI_BASE_ADDRESS_0
  30. /** Capability register length */
  31. #define EHCI_CAP_CAPLENGTH 0x00
  32. /** Host controller interface version number */
  33. #define EHCI_CAP_HCIVERSION 0x02
  34. /** Structural parameters */
  35. #define EHCI_CAP_HCSPARAMS 0x04
  36. /** Number of ports */
  37. #define EHCI_HCSPARAMS_PORTS(params) ( ( (params) >> 0 ) & 0x0f )
  38. /** Capability parameters */
  39. #define EHCI_CAP_HCCPARAMS 0x08
  40. /** 64-bit addressing capability */
  41. #define EHCI_HCCPARAMS_ADDR64(params) ( ( (params) >> 0 ) & 0x1 )
  42. /** Programmable frame list flag */
  43. #define EHCI_HCCPARAMS_FLSIZE(params) ( ( (params) >> 1 ) & 0x1 )
  44. /** EHCI extended capabilities pointer */
  45. #define EHCI_HCCPARAMS_EECP(params) ( ( ( (params) >> 8 ) & 0xff ) )
  46. /** EHCI extended capability ID */
  47. #define EHCI_EECP_ID(eecp) ( ( (eecp) >> 0 ) & 0xff )
  48. /** Next EHCI extended capability pointer */
  49. #define EHCI_EECP_NEXT(eecp) ( ( ( (eecp) >> 8 ) & 0xff ) )
  50. /** USB legacy support extended capability */
  51. #define EHCI_EECP_ID_LEGACY 1
  52. /** USB legacy support BIOS owned semaphore */
  53. #define EHCI_USBLEGSUP_BIOS 0x02
  54. /** USB legacy support BIOS ownership flag */
  55. #define EHCI_USBLEGSUP_BIOS_OWNED 0x01
  56. /** USB legacy support OS owned semaphore */
  57. #define EHCI_USBLEGSUP_OS 0x03
  58. /** USB legacy support OS ownership flag */
  59. #define EHCI_USBLEGSUP_OS_OWNED 0x01
  60. /** USB legacy support control/status */
  61. #define EHCI_USBLEGSUP_CTLSTS 0x04
  62. /** USB command register */
  63. #define EHCI_OP_USBCMD 0x00
  64. /** Run/stop */
  65. #define EHCI_USBCMD_RUN 0x00000001UL
  66. /** Host controller reset */
  67. #define EHCI_USBCMD_HCRST 0x00000002UL
  68. /** Frame list size */
  69. #define EHCI_USBCMD_FLSIZE(flsize) ( (flsize) << 2 )
  70. /** Frame list size mask */
  71. #define EHCI_USBCMD_FLSIZE_MASK EHCI_USBCMD_FLSIZE ( 3 )
  72. /** Default frame list size */
  73. #define EHCI_FLSIZE_DEFAULT 0
  74. /** Smallest allowed frame list size */
  75. #define EHCI_FLSIZE_SMALL 2
  76. /** Number of elements in frame list */
  77. #define EHCI_PERIODIC_FRAMES(flsize) ( 1024 >> (flsize) )
  78. /** Periodic schedule enable */
  79. #define EHCI_USBCMD_PERIODIC 0x00000010UL
  80. /** Asynchronous schedule enable */
  81. #define EHCI_USBCMD_ASYNC 0x00000020UL
  82. /** Asyncchronous schedule advance doorbell */
  83. #define EHCI_USBCMD_ASYNC_ADVANCE 0x000040UL
  84. /** USB status register */
  85. #define EHCI_OP_USBSTS 0x04
  86. /** USB interrupt */
  87. #define EHCI_USBSTS_USBINT 0x00000001UL
  88. /** USB error interrupt */
  89. #define EHCI_USBSTS_USBERRINT 0x00000002UL
  90. /** Port change detect */
  91. #define EHCI_USBSTS_PORT 0x00000004UL
  92. /** Frame list rollover */
  93. #define EHCI_USBSTS_ROLLOVER 0x00000008UL
  94. /** Host system error */
  95. #define EHCI_USBSTS_SYSERR 0x00000010UL
  96. /** Asynchronous schedule advanced */
  97. #define EHCI_USBSTS_ASYNC_ADVANCE 0x00000020UL
  98. /** Periodic schedule enabled */
  99. #define EHCI_USBSTS_PERIODIC 0x00004000UL
  100. /** Asynchronous schedule enabled */
  101. #define EHCI_USBSTS_ASYNC 0x00008000UL
  102. /** Host controller halted */
  103. #define EHCI_USBSTS_HCH 0x00001000UL
  104. /** USB status change mask */
  105. #define EHCI_USBSTS_CHANGE \
  106. ( EHCI_USBSTS_USBINT | EHCI_USBSTS_USBERRINT | \
  107. EHCI_USBSTS_PORT | EHCI_USBSTS_ROLLOVER | \
  108. EHCI_USBSTS_SYSERR | EHCI_USBSTS_ASYNC_ADVANCE )
  109. /** USB interrupt enable register */
  110. #define EHCI_OP_USBINTR 0x08
  111. /** Frame index register */
  112. #define EHCI_OP_FRINDEX 0x0c
  113. /** Control data structure segment register */
  114. #define EHCI_OP_CTRLDSSEGMENT 0x10
  115. /** Periodic frame list base address register */
  116. #define EHCI_OP_PERIODICLISTBASE 0x14
  117. /** Current asynchronous list address register */
  118. #define EHCI_OP_ASYNCLISTADDR 0x18
  119. /** Configure flag register */
  120. #define EHCI_OP_CONFIGFLAG 0x40
  121. /** Configure flag */
  122. #define EHCI_CONFIGFLAG_CF 0x00000001UL
  123. /** Port status and control register */
  124. #define EHCI_OP_PORTSC(port) ( 0x40 + ( (port) << 2 ) )
  125. /** Current connect status */
  126. #define EHCI_PORTSC_CCS 0x00000001UL
  127. /** Connect status change */
  128. #define EHCI_PORTSC_CSC 0x00000002UL
  129. /** Port enabled */
  130. #define EHCI_PORTSC_PED 0x00000004UL
  131. /** Port enabled/disabled change */
  132. #define EHCI_PORTSC_PEC 0x00000008UL
  133. /** Over-current change */
  134. #define EHCI_PORTSC_OCC 0x00000020UL
  135. /** Port reset */
  136. #define EHCI_PORTSC_PR 0x00000100UL
  137. /** Line status */
  138. #define EHCI_PORTSC_LINE_STATUS(portsc) ( ( (portsc) >> 10 ) & 0x3 )
  139. /** Line status: low-speed device */
  140. #define EHCI_PORTSC_LINE_STATUS_LOW 0x1
  141. /** Port power */
  142. #define EHCI_PORTSC_PP 0x00001000UL
  143. /** Port owner */
  144. #define EHCI_PORTSC_OWNER 0x00002000UL
  145. /** Port status change mask */
  146. #define EHCI_PORTSC_CHANGE \
  147. ( EHCI_PORTSC_CSC | EHCI_PORTSC_PEC | EHCI_PORTSC_OCC )
  148. /** List terminator */
  149. #define EHCI_LINK_TERMINATE 0x00000001UL
  150. /** Frame list type */
  151. #define EHCI_LINK_TYPE(type) ( (type) << 1 )
  152. /** Queue head type */
  153. #define EHCI_LINK_TYPE_QH EHCI_LINK_TYPE ( 1 )
  154. /** A periodic frame list entry */
  155. struct ehci_periodic_frame {
  156. /** First queue head */
  157. uint32_t link;
  158. } __attribute__ (( packed ));
  159. /** A transfer descriptor */
  160. struct ehci_transfer_descriptor {
  161. /** Next transfer descriptor */
  162. uint32_t next;
  163. /** Alternate next transfer descriptor */
  164. uint32_t alt;
  165. /** Status */
  166. uint8_t status;
  167. /** Flags */
  168. uint8_t flags;
  169. /** Transfer length */
  170. uint16_t len;
  171. /** Buffer pointers (low 32 bits) */
  172. uint32_t low[5];
  173. /** Extended buffer pointers (high 32 bits) */
  174. uint32_t high[5];
  175. /** Reserved */
  176. uint8_t reserved[12];
  177. } __attribute__ (( packed ));
  178. /** Transaction error */
  179. #define EHCI_STATUS_XACT_ERR 0x08
  180. /** Babble detected */
  181. #define EHCI_STATUS_BABBLE 0x10
  182. /** Data buffer error */
  183. #define EHCI_STATUS_BUFFER 0x20
  184. /** Halted */
  185. #define EHCI_STATUS_HALTED 0x40
  186. /** Active */
  187. #define EHCI_STATUS_ACTIVE 0x80
  188. /** PID code */
  189. #define EHCI_FL_PID(code) ( (code) << 0 )
  190. /** OUT token */
  191. #define EHCI_FL_PID_OUT EHCI_FL_PID ( 0 )
  192. /** IN token */
  193. #define EHCI_FL_PID_IN EHCI_FL_PID ( 1 )
  194. /** SETUP token */
  195. #define EHCI_FL_PID_SETUP EHCI_FL_PID ( 2 )
  196. /** Error counter */
  197. #define EHCI_FL_CERR( count ) ( (count) << 2 )
  198. /** Error counter maximum value */
  199. #define EHCI_FL_CERR_MAX EHCI_FL_CERR ( 3 )
  200. /** Interrupt on completion */
  201. #define EHCI_FL_IOC 0x80
  202. /** Length mask */
  203. #define EHCI_LEN_MASK 0x7fff
  204. /** Data toggle */
  205. #define EHCI_LEN_TOGGLE 0x8000
  206. /** A queue head */
  207. struct ehci_queue_head {
  208. /** Horizontal link pointer */
  209. uint32_t link;
  210. /** Endpoint characteristics */
  211. uint32_t chr;
  212. /** Endpoint capabilities */
  213. uint32_t cap;
  214. /** Current transfer descriptor */
  215. uint32_t current;
  216. /** Transfer descriptor cache */
  217. struct ehci_transfer_descriptor cache;
  218. } __attribute__ (( packed ));
  219. /** Device address */
  220. #define EHCI_CHR_ADDRESS( address ) ( (address) << 0 )
  221. /** Endpoint number */
  222. #define EHCI_CHR_ENDPOINT( address ) ( ( (address) & 0xf ) << 8 )
  223. /** Endpoint speed */
  224. #define EHCI_CHR_EPS( eps ) ( (eps) << 12 )
  225. /** Full-speed endpoint */
  226. #define EHCI_CHR_EPS_FULL EHCI_CHR_EPS ( 0 )
  227. /** Low-speed endpoint */
  228. #define EHCI_CHR_EPS_LOW EHCI_CHR_EPS ( 1 )
  229. /** High-speed endpoint */
  230. #define EHCI_CHR_EPS_HIGH EHCI_CHR_EPS ( 2 )
  231. /** Explicit data toggles */
  232. #define EHCI_CHR_TOGGLE 0x00004000UL
  233. /** Head of reclamation list flag */
  234. #define EHCI_CHR_HEAD 0x00008000UL
  235. /** Maximum packet length */
  236. #define EHCI_CHR_MAX_LEN( len ) ( (len) << 16 )
  237. /** Control endpoint flag */
  238. #define EHCI_CHR_CONTROL 0x08000000UL
  239. /** Interrupt schedule mask */
  240. #define EHCI_CAP_INTR_SCHED( uframe ) ( 1 << ( (uframe) + 0 ) )
  241. /** Split completion schedule mask */
  242. #define EHCI_CAP_SPLIT_SCHED( uframe ) ( 1 << ( (uframe) + 8 ) )
  243. /** Default split completion schedule mask
  244. *
  245. * We schedule all split starts in microframe 0, on the assumption
  246. * that we will never have to deal with more than sixteen actively
  247. * interrupting devices via the same transaction translator. We
  248. * schedule split completions for all remaining microframes after
  249. * microframe 1 (in which the low-speed or full-speed transaction is
  250. * assumed to execute). This is a very crude approximation designed
  251. * to avoid the need for calculating exactly when low-speed and
  252. * full-speed transactions will execute. Since we only ever deal with
  253. * interrupt endpoints (rather than isochronous endpoints), the volume
  254. * of periodic traffic is extremely low, and this approximation should
  255. * remain valid.
  256. */
  257. #define EHCI_CAP_SPLIT_SCHED_DEFAULT \
  258. ( EHCI_CAP_SPLIT_SCHED ( 2 ) | EHCI_CAP_SPLIT_SCHED ( 3 ) | \
  259. EHCI_CAP_SPLIT_SCHED ( 4 ) | EHCI_CAP_SPLIT_SCHED ( 5 ) | \
  260. EHCI_CAP_SPLIT_SCHED ( 6 ) | EHCI_CAP_SPLIT_SCHED ( 7 ) )
  261. /** Transaction translator hub address */
  262. #define EHCI_CAP_TT_HUB( address ) ( (address) << 16 )
  263. /** Transaction translator port number */
  264. #define EHCI_CAP_TT_PORT( port ) ( (port) << 23 )
  265. /** High-bandwidth pipe multiplier */
  266. #define EHCI_CAP_MULT( mult ) ( (mult) << 30 )
  267. /** A transfer descriptor ring */
  268. struct ehci_ring {
  269. /** Producer counter */
  270. unsigned int prod;
  271. /** Consumer counter */
  272. unsigned int cons;
  273. /** Residual untransferred data */
  274. size_t residual;
  275. /** I/O buffers */
  276. struct io_buffer **iobuf;
  277. /** Queue head */
  278. struct ehci_queue_head *head;
  279. /** Transfer descriptors */
  280. struct ehci_transfer_descriptor *desc;
  281. };
  282. /** Number of transfer descriptors in a ring
  283. *
  284. * This is a policy decision.
  285. */
  286. #define EHCI_RING_COUNT 64
  287. /**
  288. * Calculate space used in transfer descriptor ring
  289. *
  290. * @v ring Transfer descriptor ring
  291. * @ret fill Number of entries used
  292. */
  293. static inline __attribute__ (( always_inline )) unsigned int
  294. ehci_ring_fill ( struct ehci_ring *ring ) {
  295. unsigned int fill;
  296. fill = ( ring->prod - ring->cons );
  297. assert ( fill <= EHCI_RING_COUNT );
  298. return fill;
  299. }
  300. /**
  301. * Calculate space remaining in transfer descriptor ring
  302. *
  303. * @v ring Transfer descriptor ring
  304. * @ret remaining Number of entries remaining
  305. */
  306. static inline __attribute__ (( always_inline )) unsigned int
  307. ehci_ring_remaining ( struct ehci_ring *ring ) {
  308. unsigned int fill = ehci_ring_fill ( ring );
  309. return ( EHCI_RING_COUNT - fill );
  310. }
  311. /** Time to delay after enabling power to a port
  312. *
  313. * This is not mandated by EHCI; we use the value given for xHCI.
  314. */
  315. #define EHCI_PORT_POWER_DELAY_MS 20
  316. /** Time to delay after releasing ownership of a port
  317. *
  318. * This is a policy decision.
  319. */
  320. #define EHCI_DISOWN_DELAY_MS 100
  321. /** Maximum time to wait for BIOS to release ownership
  322. *
  323. * This is a policy decision.
  324. */
  325. #define EHCI_USBLEGSUP_MAX_WAIT_MS 100
  326. /** Maximum time to wait for asynchronous schedule to advance
  327. *
  328. * This is a policy decision.
  329. */
  330. #define EHCI_ASYNC_ADVANCE_MAX_WAIT_MS 100
  331. /** Maximum time to wait for host controller to stop
  332. *
  333. * This is a policy decision.
  334. */
  335. #define EHCI_STOP_MAX_WAIT_MS 100
  336. /** Maximum time to wait for reset to complete
  337. *
  338. * This is a policy decision.
  339. */
  340. #define EHCI_RESET_MAX_WAIT_MS 500
  341. /** Maximum time to wait for a port reset to complete
  342. *
  343. * This is a policy decision.
  344. */
  345. #define EHCI_PORT_RESET_MAX_WAIT_MS 500
  346. /** An EHCI transfer */
  347. struct ehci_transfer {
  348. /** Data buffer */
  349. void *data;
  350. /** Length */
  351. size_t len;
  352. /** Flags
  353. *
  354. * This is the bitwise OR of zero or more EHCI_FL_XXX values.
  355. * The low 8 bits are copied to the flags byte within the
  356. * transfer descriptor; the remaining bits hold flags
  357. * meaningful only to our driver code.
  358. */
  359. unsigned int flags;
  360. };
  361. /** Set initial data toggle */
  362. #define EHCI_FL_TOGGLE 0x8000
  363. /** An EHCI device */
  364. struct ehci_device {
  365. /** Registers */
  366. void *regs;
  367. /** Name */
  368. const char *name;
  369. /** Capability registers */
  370. void *cap;
  371. /** Operational registers */
  372. void *op;
  373. /** Number of ports */
  374. unsigned int ports;
  375. /** 64-bit addressing capability */
  376. int addr64;
  377. /** Frame list size */
  378. unsigned int flsize;
  379. /** EHCI extended capabilities offset */
  380. unsigned int eecp;
  381. /** USB legacy support capability (if present and enabled) */
  382. unsigned int legacy;
  383. /** Control data structure segment */
  384. uint32_t ctrldssegment;
  385. /** Asynchronous queue head */
  386. struct ehci_queue_head *head;
  387. /** Periodic frame list */
  388. struct ehci_periodic_frame *frame;
  389. /** List of all endpoints */
  390. struct list_head endpoints;
  391. /** Asynchronous schedule */
  392. struct list_head async;
  393. /** Periodic schedule
  394. *
  395. * Listed in decreasing order of endpoint interval.
  396. */
  397. struct list_head periodic;
  398. /** USB bus */
  399. struct usb_bus *bus;
  400. };
  401. /** An EHCI endpoint */
  402. struct ehci_endpoint {
  403. /** EHCI device */
  404. struct ehci_device *ehci;
  405. /** USB endpoint */
  406. struct usb_endpoint *ep;
  407. /** List of all endpoints */
  408. struct list_head list;
  409. /** Endpoint schedule */
  410. struct list_head schedule;
  411. /** Transfer descriptor ring */
  412. struct ehci_ring ring;
  413. };
  414. extern unsigned int ehci_companion ( struct pci_device *pci );
  415. #endif /* _IPXE_EHCI_H */