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