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