6
* USB Universal Host Controller Interface (UHCI) driver
10
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
16
/** Minimum alignment required for data structures
18
* With the exception of the frame list (which is page-aligned), data
19
* structures used by UHCI generally require 16-byte alignment.
23
/** Number of ports */
26
/** Maximum transfer size */
30
#define UHCI_BAR_SIZE 0x14
32
/** USB command register */
33
#define UHCI_USBCMD 0x00
35
/** Max packet is 64 bytes */
36
#define UHCI_USBCMD_MAX64 0x0080
38
/** Host controller reset */
39
#define UHCI_USBCMD_HCRESET 0x0002
42
#define UHCI_USBCMD_RUN 0x0001
44
/** USB status register */
45
#define UHCI_USBSTS 0x02
47
/** Host controller halted */
48
#define UHCI_USBSTS_HCHALTED 0x0020
51
#define UHCI_USBSTS_USBINT 0x0001
53
/** Frame list base address register */
54
#define UHCI_FLBASEADD 0x08
56
/** Port status and control register */
57
#define UHCI_PORTSC(port) ( 0x0e + ( (port) << 1 ) )
60
#define UHCI_PORTSC_PR 0x0200
62
/** Low-speed device attached */
63
#define UHCI_PORTSC_LS 0x0100
65
/** Port enabled/disabled change */
66
#define UHCI_PORTSC_PEC 0x0008
69
#define UHCI_PORTSC_PED 0x0004
71
/** Connect status change */
72
#define UHCI_PORTSC_CSC 0x0002
74
/** Current connect status */
75
#define UHCI_PORTSC_CCS 0x0001
77
/** Port status change mask */
78
#define UHCI_PORTSC_CHANGE ( UHCI_PORTSC_CSC | UHCI_PORTSC_PEC )
80
/** Depth-first processing */
81
#define UHCI_LINK_DEPTH_FIRST 0x00000004UL
83
/** Queue head type */
84
#define UHCI_LINK_TYPE_QH 0x00000002UL
86
/** List terminator */
87
#define UHCI_LINK_TERMINATE 0x00000001UL
89
/** Number of frames in frame list */
90
#define UHCI_FRAMES 1024
93
struct uhci_frame_list {
95
uint32_t link[UHCI_FRAMES];
96
} __attribute__ (( packed ));
98
/** A transfer descriptor */
99
struct uhci_transfer_descriptor {
110
/** Buffer pointer */
112
} __attribute__ (( packed ));
115
#define UHCI_LEN_MASK 0x7ff
118
#define UHCI_ACTUAL_LEN( actual ) ( ( (actual) + 1 ) & UHCI_LEN_MASK )
121
#define UHCI_STATUS_ACTIVE 0x80
124
#define UHCI_STATUS_STALLED 0x40
126
/** Data buffer error */
127
#define UHCI_STATUS_BUFFER 0x20
129
/** Babble detected */
130
#define UHCI_STATUS_BABBLE 0x10
133
#define UHCI_STATUS_NAK 0x08
135
/** CRC/timeout error */
136
#define UHCI_STATUS_CRC_TIMEOUT 0x04
138
/** Bitstuff error */
139
#define UHCI_STATUS_BITSTUFF 0x02
141
/** Short packet detect */
142
#define UHCI_FL_SPD 0x20
145
#define UHCI_FL_CERR( count ) ( (count) << 3 )
147
/** Error counter maximum value */
148
#define UHCI_FL_CERR_MAX UHCI_FL_CERR ( 3 )
150
/** Low speed device */
151
#define UHCI_FL_LS 0x04
153
/** Interrupt on completion */
154
#define UHCI_FL_IOC 0x01
157
#define UHCI_CONTROL_PID( pid ) ( (pid) << 0 )
159
/** Packet ID mask */
160
#define UHCI_CONTROL_PID_MASK UHCI_CONTROL_PID ( 0xff )
162
/** Device address */
163
#define UHCI_CONTROL_DEVICE( address ) ( (address) << 8 )
165
/** Endpoint address */
166
#define UHCI_CONTROL_ENDPOINT( address ) ( (address) << 15 )
169
#define UHCI_CONTROL_TOGGLE ( 1 << 19 )
172
#define UHCI_CONTROL_LEN( len ) ( ( ( (len) - 1 ) & UHCI_LEN_MASK ) << 21 )
174
/** Check for data packet
176
* This check is based on the fact that only USB_PID_SETUP has bit 2
179
#define UHCI_DATA_PACKET( control ) ( ! ( control & 0x04 ) )
181
/** Check for short packet */
182
#define UHCI_SHORT_PACKET( control, actual ) \
183
( ( ( (control) >> 21 ) ^ (actual) ) & UHCI_LEN_MASK )
185
/** USB legacy support register (in PCI configuration space) */
186
#define UHCI_USBLEGSUP 0xc0
188
/** USB legacy support default value */
189
#define UHCI_USBLEGSUP_DEFAULT 0x2000
192
struct uhci_queue_head {
193
/** Horizontal link pointer */
195
/** Current transfer descriptor */
197
} __attribute__ (( packed ));
199
/** A single UHCI transfer
201
* UHCI hardware is extremely simple, and requires software to build
202
* the entire packet schedule (including manually handling all of the
203
* data toggles). The hardware requires at least 16 bytes of transfer
204
* descriptors per 64 bytes of transmitted/received data. We allocate
205
* the transfer descriptors at the time that the transfer is enqueued,
206
* to avoid the need to allocate unreasonably large blocks when the
207
* endpoint is opened.
209
struct uhci_transfer {
210
/** Producer counter */
212
/** Consumer counter */
214
/** Completed data length */
217
/** Transfer descriptors */
218
struct uhci_transfer_descriptor *desc;
221
struct io_buffer *iobuf;
224
/** Number of transfer descriptors in a ring
226
* This is a policy decision.
228
#define UHCI_RING_COUNT 16
230
/** A transfer ring */
232
/** Producer counter */
234
/** Consumer counter */
237
/** Maximum packet length */
241
* This incorporates the CERR and LS bits
244
/** Base control word
246
* This incorporates the device address, the endpoint address,
247
* and the data toggle for the next descriptor to be enqueued.
252
struct uhci_transfer *xfer[UHCI_RING_COUNT];
253
/** End of transfer ring (if non-empty) */
254
struct uhci_transfer *end;
257
struct uhci_queue_head *head;
261
* Calculate space used in transfer ring
263
* @v ring Transfer ring
264
* @ret fill Number of entries used
266
static inline __attribute__ (( always_inline )) unsigned int
267
uhci_ring_fill ( struct uhci_ring *ring ) {
270
fill = ( ring->prod - ring->cons );
271
assert ( fill <= UHCI_RING_COUNT );
276
* Calculate space remaining in transfer ring
278
* @v ring Transfer ring
279
* @ret remaining Number of entries remaining
281
static inline __attribute__ (( always_inline )) unsigned int
282
uhci_ring_remaining ( struct uhci_ring *ring ) {
283
unsigned int fill = uhci_ring_fill ( ring );
285
return ( UHCI_RING_COUNT - fill );
288
/** Maximum time to wait for host controller to stop
290
* This is a policy decision.
292
#define UHCI_STOP_MAX_WAIT_MS 100
294
/** Maximum time to wait for reset to complete
296
* This is a policy decision.
298
#define UHCI_RESET_MAX_WAIT_MS 500
300
/** Maximum time to wait for a port to be enabled
302
* This is a policy decision.
304
#define UHCI_PORT_ENABLE_MAX_WAIT_MS 500
313
/** EHCI companion controller bus:dev.fn address (if any) */
314
unsigned int companion;
316
/** Asynchronous queue head */
317
struct uhci_queue_head *head;
319
struct uhci_frame_list *frame;
321
/** List of all endpoints */
322
struct list_head endpoints;
323
/** Asynchronous schedule */
324
struct list_head async;
325
/** Periodic schedule
327
* Listed in decreasing order of endpoint interval.
329
struct list_head periodic;
335
/** A UHCI endpoint */
336
struct uhci_endpoint {
338
struct uhci_device *uhci;
340
struct usb_endpoint *ep;
341
/** List of all endpoints */
342
struct list_head list;
343
/** Endpoint schedule */
344
struct list_head schedule;
347
struct uhci_ring ring;
350
#endif /* _IPXE_UHCI_H */