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.

uhci.h 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #ifndef _IPXE_UHCI_H
  2. #define _IPXE_UHCI_H
  3. /** @file
  4. *
  5. * USB Universal Host Controller Interface (UHCI) driver
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <assert.h>
  10. #include <ipxe/pci.h>
  11. #include <ipxe/usb.h>
  12. /** Minimum alignment required for data structures
  13. *
  14. * With the exception of the frame list (which is page-aligned), data
  15. * structures used by UHCI generally require 16-byte alignment.
  16. */
  17. #define UHCI_ALIGN 16
  18. /** Number of ports */
  19. #define UHCI_PORTS 2
  20. /** Maximum transfer size */
  21. #define UHCI_MTU 1280
  22. /** I/O BAR size */
  23. #define UHCI_BAR_SIZE 0x14
  24. /** USB command register */
  25. #define UHCI_USBCMD 0x00
  26. /** Max packet is 64 bytes */
  27. #define UHCI_USBCMD_MAX64 0x0080
  28. /** Host controller reset */
  29. #define UHCI_USBCMD_HCRESET 0x0002
  30. /** Run/stop */
  31. #define UHCI_USBCMD_RUN 0x0001
  32. /** USB status register */
  33. #define UHCI_USBSTS 0x02
  34. /** Host controller halted */
  35. #define UHCI_USBSTS_HCHALTED 0x0020
  36. /** USB interrupt */
  37. #define UHCI_USBSTS_USBINT 0x0001
  38. /** Frame list base address register */
  39. #define UHCI_FLBASEADD 0x08
  40. /** Port status and control register */
  41. #define UHCI_PORTSC(port) ( 0x0e + ( (port) << 1 ) )
  42. /** Port reset */
  43. #define UHCI_PORTSC_PR 0x0200
  44. /** Low-speed device attached */
  45. #define UHCI_PORTSC_LS 0x0100
  46. /** Port enabled/disabled change */
  47. #define UHCI_PORTSC_PEC 0x0008
  48. /** Port enabled */
  49. #define UHCI_PORTSC_PED 0x0004
  50. /** Connect status change */
  51. #define UHCI_PORTSC_CSC 0x0002
  52. /** Current connect status */
  53. #define UHCI_PORTSC_CCS 0x0001
  54. /** Port status change mask */
  55. #define UHCI_PORTSC_CHANGE ( UHCI_PORTSC_CSC | UHCI_PORTSC_PEC )
  56. /** Depth-first processing */
  57. #define UHCI_LINK_DEPTH_FIRST 0x00000004UL
  58. /** Queue head type */
  59. #define UHCI_LINK_TYPE_QH 0x00000002UL
  60. /** List terminator */
  61. #define UHCI_LINK_TERMINATE 0x00000001UL
  62. /** Number of frames in frame list */
  63. #define UHCI_FRAMES 1024
  64. /** A frame list */
  65. struct uhci_frame_list {
  66. /** Link pointer */
  67. uint32_t link[UHCI_FRAMES];
  68. } __attribute__ (( packed ));
  69. /** A transfer descriptor */
  70. struct uhci_transfer_descriptor {
  71. /** Link pointer */
  72. uint32_t link;
  73. /** Actual length */
  74. uint16_t actual;
  75. /** Status */
  76. uint8_t status;
  77. /** Flags */
  78. uint8_t flags;
  79. /** Control */
  80. uint32_t control;
  81. /** Buffer pointer */
  82. uint32_t data;
  83. } __attribute__ (( packed ));
  84. /** Length mask */
  85. #define UHCI_LEN_MASK 0x7ff
  86. /** Actual length */
  87. #define UHCI_ACTUAL_LEN( actual ) ( ( (actual) + 1 ) & UHCI_LEN_MASK )
  88. /** Active */
  89. #define UHCI_STATUS_ACTIVE 0x80
  90. /** Stalled */
  91. #define UHCI_STATUS_STALLED 0x40
  92. /** Data buffer error */
  93. #define UHCI_STATUS_BUFFER 0x20
  94. /** Babble detected */
  95. #define UHCI_STATUS_BABBLE 0x10
  96. /** NAK received */
  97. #define UHCI_STATUS_NAK 0x08
  98. /** CRC/timeout error */
  99. #define UHCI_STATUS_CRC_TIMEOUT 0x04
  100. /** Bitstuff error */
  101. #define UHCI_STATUS_BITSTUFF 0x02
  102. /** Short packet detect */
  103. #define UHCI_FL_SPD 0x20
  104. /** Error counter */
  105. #define UHCI_FL_CERR( count ) ( (count) << 3 )
  106. /** Error counter maximum value */
  107. #define UHCI_FL_CERR_MAX UHCI_FL_CERR ( 3 )
  108. /** Low speed device */
  109. #define UHCI_FL_LS 0x04
  110. /** Interrupt on completion */
  111. #define UHCI_FL_IOC 0x01
  112. /** Packet ID */
  113. #define UHCI_CONTROL_PID( pid ) ( (pid) << 0 )
  114. /** Packet ID mask */
  115. #define UHCI_CONTROL_PID_MASK UHCI_CONTROL_PID ( 0xff )
  116. /** Device address */
  117. #define UHCI_CONTROL_DEVICE( address ) ( (address) << 8 )
  118. /** Endpoint address */
  119. #define UHCI_CONTROL_ENDPOINT( address ) ( (address) << 15 )
  120. /** Data toggle */
  121. #define UHCI_CONTROL_TOGGLE ( 1 << 19 )
  122. /** Data length */
  123. #define UHCI_CONTROL_LEN( len ) ( ( ( (len) - 1 ) & UHCI_LEN_MASK ) << 21 )
  124. /** Check for data packet
  125. *
  126. * This check is based on the fact that only USB_PID_SETUP has bit 2
  127. * set.
  128. */
  129. #define UHCI_DATA_PACKET( control ) ( ! ( control & 0x04 ) )
  130. /** Check for short packet */
  131. #define UHCI_SHORT_PACKET( control, actual ) \
  132. ( ( ( (control) >> 21 ) ^ (actual) ) & UHCI_LEN_MASK )
  133. /** USB legacy support register (in PCI configuration space) */
  134. #define UHCI_USBLEGSUP 0xc0
  135. /** USB legacy support default value */
  136. #define UHCI_USBLEGSUP_DEFAULT 0x2000
  137. /** A queue head */
  138. struct uhci_queue_head {
  139. /** Horizontal link pointer */
  140. uint32_t link;
  141. /** Current transfer descriptor */
  142. uint32_t current;
  143. } __attribute__ (( packed ));
  144. /** A single UHCI transfer
  145. *
  146. * UHCI hardware is extremely simple, and requires software to build
  147. * the entire packet schedule (including manually handling all of the
  148. * data toggles). The hardware requires at least 16 bytes of transfer
  149. * descriptors per 64 bytes of transmitted/received data. We allocate
  150. * the transfer descriptors at the time that the transfer is enqueued,
  151. * to avoid the need to allocate unreasonably large blocks when the
  152. * endpoint is opened.
  153. */
  154. struct uhci_transfer {
  155. /** Producer counter */
  156. unsigned int prod;
  157. /** Consumer counter */
  158. unsigned int cons;
  159. /** Completed data length */
  160. size_t len;
  161. /** Transfer descriptors */
  162. struct uhci_transfer_descriptor *desc;
  163. /** I/O buffer */
  164. struct io_buffer *iobuf;
  165. };
  166. /** Number of transfer descriptors in a ring
  167. *
  168. * This is a policy decision.
  169. */
  170. #define UHCI_RING_COUNT 16
  171. /** A transfer ring */
  172. struct uhci_ring {
  173. /** Producer counter */
  174. unsigned int prod;
  175. /** Consumer counter */
  176. unsigned int cons;
  177. /** Maximum packet length */
  178. size_t mtu;
  179. /** Base flags
  180. *
  181. * This incorporates the CERR and LS bits
  182. */
  183. uint8_t flags;
  184. /** Base control word
  185. *
  186. * This incorporates the device address, the endpoint address,
  187. * and the data toggle for the next descriptor to be enqueued.
  188. */
  189. uint32_t control;
  190. /** Transfers */
  191. struct uhci_transfer *xfer[UHCI_RING_COUNT];
  192. /** End of transfer ring (if non-empty) */
  193. struct uhci_transfer *end;
  194. /** Queue head */
  195. struct uhci_queue_head *head;
  196. };
  197. /**
  198. * Calculate space used in transfer ring
  199. *
  200. * @v ring Transfer ring
  201. * @ret fill Number of entries used
  202. */
  203. static inline __attribute__ (( always_inline )) unsigned int
  204. uhci_ring_fill ( struct uhci_ring *ring ) {
  205. unsigned int fill;
  206. fill = ( ring->prod - ring->cons );
  207. assert ( fill <= UHCI_RING_COUNT );
  208. return fill;
  209. }
  210. /**
  211. * Calculate space remaining in transfer ring
  212. *
  213. * @v ring Transfer ring
  214. * @ret remaining Number of entries remaining
  215. */
  216. static inline __attribute__ (( always_inline )) unsigned int
  217. uhci_ring_remaining ( struct uhci_ring *ring ) {
  218. unsigned int fill = uhci_ring_fill ( ring );
  219. return ( UHCI_RING_COUNT - fill );
  220. }
  221. /** Maximum time to wait for host controller to stop
  222. *
  223. * This is a policy decision.
  224. */
  225. #define UHCI_STOP_MAX_WAIT_MS 100
  226. /** Maximum time to wait for reset to complete
  227. *
  228. * This is a policy decision.
  229. */
  230. #define UHCI_RESET_MAX_WAIT_MS 500
  231. /** Maximum time to wait for a port to be enabled
  232. *
  233. * This is a policy decision.
  234. */
  235. #define UHCI_PORT_ENABLE_MAX_WAIT_MS 500
  236. /** A UHCI device */
  237. struct uhci_device {
  238. /** Registers */
  239. unsigned long regs;
  240. /** Name */
  241. const char *name;
  242. /** EHCI companion controller bus:dev.fn address (if any) */
  243. unsigned int companion;
  244. /** Asynchronous queue head */
  245. struct uhci_queue_head *head;
  246. /** Frame list */
  247. struct uhci_frame_list *frame;
  248. /** List of all endpoints */
  249. struct list_head endpoints;
  250. /** Asynchronous schedule */
  251. struct list_head async;
  252. /** Periodic schedule
  253. *
  254. * Listed in decreasing order of endpoint interval.
  255. */
  256. struct list_head periodic;
  257. /** USB bus */
  258. struct usb_bus *bus;
  259. };
  260. /** A UHCI endpoint */
  261. struct uhci_endpoint {
  262. /** UHCI device */
  263. struct uhci_device *uhci;
  264. /** USB endpoint */
  265. struct usb_endpoint *ep;
  266. /** List of all endpoints */
  267. struct list_head list;
  268. /** Endpoint schedule */
  269. struct list_head schedule;
  270. /** Transfer ring */
  271. struct uhci_ring ring;
  272. };
  273. #endif /* _IPXE_UHCI_H */