summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/drivers/usb/ehci.h
blob: 42e282e925df71cd1eb6868c3bed2e143147491b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#ifndef _IPXE_EHCI_H
#define _IPXE_EHCI_H

/** @file
 *
 * USB Enhanced Host Controller Interface (EHCI) driver
 *
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <ipxe/pci.h>
#include <ipxe/usb.h>

/** Minimum alignment required for data structures
 *
 * With the exception of the periodic frame list (which is
 * page-aligned), data structures used by EHCI generally require
 * 32-byte alignment and must not cross a 4kB page boundary.  We
 * simplify this requirement by aligning each structure on its own
 * size, with a minimum of a 32 byte alignment.
 */
#define EHCI_MIN_ALIGN 32

/** Maximum transfer size
 *
 * EHCI allows for transfers of up to 20kB with page-alignment, or
 * 16kB with arbitrary alignment.
 */
#define EHCI_MTU 16384

/** Page-alignment required for some data structures */
#define EHCI_PAGE_ALIGN 4096

/** EHCI PCI BAR */
#define EHCI_BAR PCI_BASE_ADDRESS_0

/** Capability register length */
#define EHCI_CAP_CAPLENGTH 0x00

/** Host controller interface version number */
#define EHCI_CAP_HCIVERSION 0x02

/** Structural parameters */
#define EHCI_CAP_HCSPARAMS 0x04

/** Number of ports */
#define EHCI_HCSPARAMS_PORTS(params) ( ( (params) >> 0 ) & 0x0f )

/** Capability parameters */
#define EHCI_CAP_HCCPARAMS 0x08

/** 64-bit addressing capability */
#define EHCI_HCCPARAMS_ADDR64(params) ( ( (params) >> 0 ) & 0x1 )

/** Programmable frame list flag */
#define EHCI_HCCPARAMS_FLSIZE(params) ( ( (params) >> 1 ) & 0x1 )

/** EHCI extended capabilities pointer */
#define EHCI_HCCPARAMS_EECP(params) ( ( ( (params) >> 8 ) & 0xff ) )

/** EHCI extended capability ID */
#define EHCI_EECP_ID(eecp) ( ( (eecp) >> 0 ) & 0xff )

/** Next EHCI extended capability pointer */
#define EHCI_EECP_NEXT(eecp) ( ( ( (eecp) >> 8 ) & 0xff ) )

/** USB legacy support extended capability */
#define EHCI_EECP_ID_LEGACY 1

/** USB legacy support BIOS owned semaphore */
#define EHCI_USBLEGSUP_BIOS 0x02

/** USB legacy support BIOS ownership flag */
#define EHCI_USBLEGSUP_BIOS_OWNED 0x01

/** USB legacy support OS owned semaphore */
#define EHCI_USBLEGSUP_OS 0x03

/** USB legacy support OS ownership flag */
#define EHCI_USBLEGSUP_OS_OWNED 0x01

/** USB legacy support control/status */
#define EHCI_USBLEGSUP_CTLSTS 0x04

/** USB command register */
#define EHCI_OP_USBCMD 0x00

/** Run/stop */
#define EHCI_USBCMD_RUN 0x00000001UL

/** Host controller reset */
#define EHCI_USBCMD_HCRST 0x00000002UL

/** Frame list size */
#define EHCI_USBCMD_FLSIZE(flsize) ( (flsize) << 2 )

/** Frame list size mask */
#define EHCI_USBCMD_FLSIZE_MASK EHCI_USBCMD_FLSIZE ( 3 )

/** Default frame list size */
#define EHCI_FLSIZE_DEFAULT 0

/** Smallest allowed frame list size */
#define EHCI_FLSIZE_SMALL 2

/** Number of elements in frame list */
#define EHCI_PERIODIC_FRAMES(flsize) ( 1024 >> (flsize) )

/** Periodic schedule enable */
#define EHCI_USBCMD_PERIODIC 0x00000010UL

/** Asynchronous schedule enable */
#define EHCI_USBCMD_ASYNC 0x00000020UL

/** Asyncchronous schedule advance doorbell */
#define EHCI_USBCMD_ASYNC_ADVANCE 0x000040UL

/** USB status register */
#define EHCI_OP_USBSTS 0x04

/** USB interrupt */
#define EHCI_USBSTS_USBINT 0x00000001UL

/** USB error interrupt */
#define EHCI_USBSTS_USBERRINT 0x00000002UL

/** Port change detect */
#define EHCI_USBSTS_PORT 0x00000004UL

/** Frame list rollover */
#define EHCI_USBSTS_ROLLOVER 0x00000008UL

/** Host system error */
#define EHCI_USBSTS_SYSERR 0x00000010UL

/** Asynchronous schedule advanced */
#define EHCI_USBSTS_ASYNC_ADVANCE 0x00000020UL

/** Periodic schedule enabled */
#define EHCI_USBSTS_PERIODIC 0x00004000UL

/** Asynchronous schedule enabled */
#define EHCI_USBSTS_ASYNC 0x00008000UL

/** Host controller halted */
#define EHCI_USBSTS_HCH 0x00001000UL

/** USB status change mask */
#define EHCI_USBSTS_CHANGE						\
	( EHCI_USBSTS_USBINT | EHCI_USBSTS_USBERRINT |			\
	  EHCI_USBSTS_PORT | EHCI_USBSTS_ROLLOVER |			\
	  EHCI_USBSTS_SYSERR | EHCI_USBSTS_ASYNC_ADVANCE )

/** USB interrupt enable register */
#define EHCI_OP_USBINTR 0x08

/** Frame index register */
#define EHCI_OP_FRINDEX 0x0c

/** Control data structure segment register */
#define EHCI_OP_CTRLDSSEGMENT 0x10

/** Periodic frame list base address register */
#define EHCI_OP_PERIODICLISTBASE 0x14

/** Current asynchronous list address register */
#define EHCI_OP_ASYNCLISTADDR 0x18

/** Configure flag register */
#define EHCI_OP_CONFIGFLAG 0x40

/** Configure flag */
#define EHCI_CONFIGFLAG_CF 0x00000001UL

/** Port status and control register */
#define EHCI_OP_PORTSC(port) ( 0x40 + ( (port) << 2 ) )

/** Current connect status */
#define EHCI_PORTSC_CCS 0x00000001UL

/** Connect status change */
#define EHCI_PORTSC_CSC 0x00000002UL

/** Port enabled */
#define EHCI_PORTSC_PED 0x00000004UL

/** Port enabled/disabled change */
#define EHCI_PORTSC_PEC 0x00000008UL

/** Over-current change */
#define EHCI_PORTSC_OCC 0x00000020UL

/** Port reset */
#define EHCI_PORTSC_PR 0x00000100UL

/** Line status */
#define EHCI_PORTSC_LINE_STATUS(portsc) ( ( (portsc) >> 10 ) & 0x3 )

/** Line status: low-speed device */
#define EHCI_PORTSC_LINE_STATUS_LOW 0x1

/** Port power */
#define EHCI_PORTSC_PP 0x00001000UL

/** Port owner */
#define EHCI_PORTSC_OWNER 0x00002000UL

/** Port status change mask */
#define EHCI_PORTSC_CHANGE \
	( EHCI_PORTSC_CSC | EHCI_PORTSC_PEC | EHCI_PORTSC_OCC )

/** List terminator */
#define EHCI_LINK_TERMINATE 0x00000001UL

/** Frame list type */
#define EHCI_LINK_TYPE(type) ( (type) << 1 )

/** Queue head type */
#define EHCI_LINK_TYPE_QH EHCI_LINK_TYPE ( 1 )

/** A periodic frame list entry */
struct ehci_periodic_frame {
	/** First queue head */
	uint32_t link;
} __attribute__ (( packed ));

/** A transfer descriptor */
struct ehci_transfer_descriptor {
	/** Next transfer descriptor */
	uint32_t next;
	/** Alternate next transfer descriptor */
	uint32_t alt;
	/** Status */
	uint8_t status;
	/** Flags */
	uint8_t flags;
	/** Transfer length */
	uint16_t len;
	/** Buffer pointers (low 32 bits) */
	uint32_t low[5];
	/** Extended buffer pointers (high 32 bits) */
	uint32_t high[5];
	/** Reserved */
	uint8_t reserved[12];
} __attribute__ (( packed ));

/** Transaction error */
#define EHCI_STATUS_XACT_ERR 0x08

/** Babble detected */
#define EHCI_STATUS_BABBLE 0x10

/** Data buffer error */
#define EHCI_STATUS_BUFFER 0x20

/** Halted */
#define EHCI_STATUS_HALTED 0x40

/** Active */
#define EHCI_STATUS_ACTIVE 0x80

/** PID code */
#define EHCI_FL_PID(code) ( (code) << 0 )

/** OUT token */
#define EHCI_FL_PID_OUT EHCI_FL_PID ( 0 )

/** IN token */
#define EHCI_FL_PID_IN EHCI_FL_PID ( 1 )

/** SETUP token */
#define EHCI_FL_PID_SETUP EHCI_FL_PID ( 2 )

/** Error counter */
#define EHCI_FL_CERR( count ) ( (count) << 2 )

/** Error counter maximum value */
#define EHCI_FL_CERR_MAX EHCI_FL_CERR ( 3 )

/** Interrupt on completion */
#define EHCI_FL_IOC 0x80

/** Length mask */
#define EHCI_LEN_MASK 0x7fff

/** Data toggle */
#define EHCI_LEN_TOGGLE 0x8000

/** A queue head */
struct ehci_queue_head {
	/** Horizontal link pointer */
	uint32_t link;
	/** Endpoint characteristics */
	uint32_t chr;
	/** Endpoint capabilities */
	uint32_t cap;
	/** Current transfer descriptor */
	uint32_t current;
	/** Transfer descriptor cache */
	struct ehci_transfer_descriptor cache;
} __attribute__ (( packed ));

/** Device address */
#define EHCI_CHR_ADDRESS( address ) ( (address) << 0 )

/** Endpoint number */
#define EHCI_CHR_ENDPOINT( address ) ( ( (address) & 0xf ) << 8 )

/** Endpoint speed */
#define EHCI_CHR_EPS( eps ) ( (eps) << 12 )

/** Full-speed endpoint */
#define EHCI_CHR_EPS_FULL EHCI_CHR_EPS ( 0 )

/** Low-speed endpoint */
#define EHCI_CHR_EPS_LOW EHCI_CHR_EPS ( 1 )

/** High-speed endpoint */
#define EHCI_CHR_EPS_HIGH EHCI_CHR_EPS ( 2 )

/** Explicit data toggles */
#define EHCI_CHR_TOGGLE 0x00004000UL

/** Head of reclamation list flag */
#define EHCI_CHR_HEAD 0x00008000UL

/** Maximum packet length */
#define EHCI_CHR_MAX_LEN( len ) ( (len) << 16 )

/** Control endpoint flag */
#define EHCI_CHR_CONTROL 0x08000000UL

/** Interrupt schedule mask */
#define EHCI_CAP_INTR_SCHED( uframe ) ( 1 << ( (uframe) + 0 ) )

/** Split completion schedule mask */
#define EHCI_CAP_SPLIT_SCHED( uframe ) ( 1 << ( (uframe) + 8 ) )

/** Default split completion schedule mask
 *
 * We schedule all split starts in microframe 0, on the assumption
 * that we will never have to deal with more than sixteen actively
 * interrupting devices via the same transaction translator.  We
 * schedule split completions for all remaining microframes after
 * microframe 1 (in which the low-speed or full-speed transaction is
 * assumed to execute).  This is a very crude approximation designed
 * to avoid the need for calculating exactly when low-speed and
 * full-speed transactions will execute.  Since we only ever deal with
 * interrupt endpoints (rather than isochronous endpoints), the volume
 * of periodic traffic is extremely low, and this approximation should
 * remain valid.
 */
#define EHCI_CAP_SPLIT_SCHED_DEFAULT					\
	( EHCI_CAP_SPLIT_SCHED ( 2 ) | EHCI_CAP_SPLIT_SCHED ( 3 ) |	\
	  EHCI_CAP_SPLIT_SCHED ( 4 ) | EHCI_CAP_SPLIT_SCHED ( 5 ) |	\
	  EHCI_CAP_SPLIT_SCHED ( 6 ) | EHCI_CAP_SPLIT_SCHED ( 7 ) )

/** Transaction translator hub address */
#define EHCI_CAP_TT_HUB( address ) ( (address) << 16 )

/** Transaction translator port number */
#define EHCI_CAP_TT_PORT( port ) ( (port) << 23 )

/** High-bandwidth pipe multiplier */
#define EHCI_CAP_MULT( mult ) ( (mult) << 30 )

/** A transfer descriptor ring */
struct ehci_ring {
	/** Producer counter */
	unsigned int prod;
	/** Consumer counter */
	unsigned int cons;

	/** Residual untransferred data */
	size_t residual;

	/** I/O buffers */
	struct io_buffer **iobuf;

	/** Queue head */
	struct ehci_queue_head *head;
	/** Transfer descriptors */
	struct ehci_transfer_descriptor *desc;
};

/** Number of transfer descriptors in a ring
 *
 * This is a policy decision.
 */
#define EHCI_RING_COUNT 64

/**
 * Calculate space used in transfer descriptor ring
 *
 * @v ring		Transfer descriptor ring
 * @ret fill		Number of entries used
 */
static inline __attribute__ (( always_inline )) unsigned int
ehci_ring_fill ( struct ehci_ring *ring ) {
	unsigned int fill;

	fill = ( ring->prod - ring->cons );
	assert ( fill <= EHCI_RING_COUNT );
	return fill;
}

/**
 * Calculate space remaining in transfer descriptor ring
 *
 * @v ring		Transfer descriptor ring
 * @ret remaining	Number of entries remaining
 */
static inline __attribute__ (( always_inline )) unsigned int
ehci_ring_remaining ( struct ehci_ring *ring ) {
	unsigned int fill = ehci_ring_fill ( ring );

	return ( EHCI_RING_COUNT - fill );
}

/** Time to delay after enabling power to a port
 *
 * This is not mandated by EHCI; we use the value given for xHCI.
 */
#define EHCI_PORT_POWER_DELAY_MS 20

/** Time to delay after releasing ownership of a port
 *
 * This is a policy decision.
 */
#define EHCI_DISOWN_DELAY_MS 100

/** Maximum time to wait for BIOS to release ownership
 *
 * This is a policy decision.
 */
#define EHCI_USBLEGSUP_MAX_WAIT_MS 100

/** Maximum time to wait for asynchronous schedule to advance
 *
 * This is a policy decision.
 */
#define EHCI_ASYNC_ADVANCE_MAX_WAIT_MS 100

/** Maximum time to wait for host controller to stop
 *
 * This is a policy decision.
 */
#define EHCI_STOP_MAX_WAIT_MS 100

/** Maximum time to wait for reset to complete
 *
 * This is a policy decision.
 */
#define EHCI_RESET_MAX_WAIT_MS 500

/** Maximum time to wait for a port reset to complete
 *
 * This is a policy decision.
 */
#define EHCI_PORT_RESET_MAX_WAIT_MS 500

/** An EHCI transfer */
struct ehci_transfer {
	/** Data buffer */
	void *data;
	/** Length */
	size_t len;
	/** Flags
	 *
	 * This is the bitwise OR of zero or more EHCI_FL_XXX values.
	 * The low 8 bits are copied to the flags byte within the
	 * transfer descriptor; the remaining bits hold flags
	 * meaningful only to our driver code.
	 */
	unsigned int flags;
};

/** Set initial data toggle */
#define EHCI_FL_TOGGLE 0x8000

/** An EHCI device */
struct ehci_device {
	/** Registers */
	void *regs;
	/** Name */
	const char *name;

	/** Capability registers */
	void *cap;
	/** Operational registers */
	void *op;

	/** Number of ports */
	unsigned int ports;
	/** 64-bit addressing capability */
	int addr64;
	/** Frame list size */
	unsigned int flsize;
	/** EHCI extended capabilities offset */
	unsigned int eecp;

	/** USB legacy support capability (if present and enabled) */
	unsigned int legacy;

	/** Control data structure segment */
	uint32_t ctrldssegment;
	/** Asynchronous queue head */
	struct ehci_queue_head *head;
	/** Periodic frame list */
	struct ehci_periodic_frame *frame;

	/** List of all endpoints */
	struct list_head endpoints;
	/** Asynchronous schedule */
	struct list_head async;
	/** Periodic schedule
	 *
	 * Listed in decreasing order of endpoint interval.
	 */
	struct list_head periodic;

	/** USB bus */
	struct usb_bus *bus;
};

/** An EHCI endpoint */
struct ehci_endpoint {
	/** EHCI device */
	struct ehci_device *ehci;
	/** USB endpoint */
	struct usb_endpoint *ep;
	/** List of all endpoints */
	struct list_head list;
	/** Endpoint schedule */
	struct list_head schedule;

	/** Transfer descriptor ring */
	struct ehci_ring ring;
};

extern unsigned int ehci_companion ( struct pci_device *pci );

#endif /* _IPXE_EHCI_H */