TianoCore EDK2 master
Loading...
Searching...
No Matches
DebugCommunicationLibUsb3Internal.h
Go to the documentation of this file.
1
9#ifndef __USB3_DEBUG_PORT_LIB_INTERNAL__
10#define __USB3_DEBUG_PORT_LIB_INTERNAL__
11
12#include <Uefi.h>
13#include <Base.h>
15#include <Library/IoLib.h>
17#include <Library/PcdLib.h>
18#include <Library/UefiLib.h>
21#include <Library/DebugLib.h>
23#include <Library/BaseLib.h>
24#include <Library/TimerLib.h>
26#include <Library/PciLib.h>
27
28//
29// USB Debug GUID value
30//
31#define USB3_DBG_GUID \
32 { \
33 0xb2a56f4d, 0x9177, 0x4fc8, { 0xa6, 0x77, 0xdd, 0x96, 0x3e, 0xb4, 0xcb, 0x1b } \
34 }
35
36//
37// The state machine of usb debug port
38//
39#define USB3DBG_NO_DBG_CAB 0 // The XHCI host controller does not support debug capability
40#define USB3DBG_DBG_CAB 1 // The XHCI host controller supports debug capability
41#define USB3DBG_ENABLED 2 // The XHCI debug device is enabled
42#define USB3DBG_NOT_ENABLED 4 // The XHCI debug device is not enabled
43#define USB3DBG_UNINITIALIZED 255 // The XHCI debug device is uninitialized
44
45#define USB3_DEBUG_PORT_WRITE_MAX_PACKET_SIZE 0x08
46
47//
48// MaxPacketSize for DbC Endpoint Descriptor IN and OUT
49//
50#define XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE 0x400
51
52#define XHCI_DEBUG_DEVICE_VENDOR_ID 0x0525
53#define XHCI_DEBUG_DEVICE_PRODUCT_ID 0x127A
54#define XHCI_DEBUG_DEVICE_PROTOCOL 0xFF
55#define XHCI_DEBUG_DEVICE_REVISION 0x00
56
57#define XHCI_BASE_ADDRESS_64_BIT_MASK 0xFFFFFFFFFFFF0000ULL
58#define XHCI_BASE_ADDRESS_32_BIT_MASK 0xFFFF0000
59
60#define PCI_CAPABILITY_ID_DEBUG_PORT 0x0A
61#define XHC_HCCPARAMS_OFFSET 0x10
62#define XHC_CAPABILITY_ID_MASK 0xFF
63#define XHC_NEXT_CAPABILITY_MASK 0xFF00
64
65#define XHC_HCSPARAMS1_OFFSET 0x4 // Structural Parameters 1
66#define XHC_USBCMD_OFFSET 0x0 // USB Command Register Offset
67#define XHC_USBSTS_OFFSET 0x4 // USB Status Register Offset
68#define XHC_PORTSC_OFFSET 0x400 // Port Status and Control Register Offset
69
70#define XHC_USBCMD_RUN BIT0 // Run/Stop
71#define XHC_USBCMD_RESET BIT1 // Host Controller Reset
72
73#define XHC_USBSTS_HALT BIT0
74
75//
76// Indicate the timeout when data is transferred in microsecond. 0 means infinite timeout.
77//
78#define DATA_TRANSFER_WRITE_TIMEOUT 0
79#define DATA_TRANSFER_READ_TIMEOUT 50000
80#define DATA_TRANSFER_POLL_TIMEOUT 1000
81#define XHC_DEBUG_PORT_1_MILLISECOND 1000
82//
83// XHCI port power off/on delay
84//
85#define XHC_DEBUG_PORT_ON_OFF_DELAY 100000
86
87//
88// USB debug device string descriptor (header size + unicode string length)
89//
90#define STRING0_DESC_LEN 4
91#define MANU_DESC_LEN 12
92#define PRODUCT_DESC_LEN 40
93#define SERIAL_DESC_LEN 4
94
95//
96// Debug Capability Register Offset
97//
98#define XHC_DC_DCID 0x0
99#define XHC_DC_DCDB 0x4
100#define XHC_DC_DCERSTSZ 0x8
101#define XHC_DC_DCERSTBA 0x10
102#define XHC_DC_DCERDP 0x18
103#define XHC_DC_DCCTRL 0x20
104#define XHC_DC_DCST 0x24
105#define XHC_DC_DCPORTSC 0x28
106#define XHC_DC_DCCP 0x30
107#define XHC_DC_DCDDI1 0x38
108#define XHC_DC_DCDDI2 0x3C
109
110#define TRB_TYPE_LINK 6
111
112#define ERST_NUMBER 0x01
113#define TR_RING_TRB_NUMBER 0x100
114#define EVENT_RING_TRB_NUMBER 0x200
115
116#define ED_BULK_OUT 2
117#define ED_BULK_IN 6
118
119#define XHC_LOW_32BIT(Addr64) ((UINT32)(((UINTN)(Addr64)) & 0xFFFFFFFF))
120#define XHC_HIGH_32BIT(Addr64) ((UINT32)(RShiftU64((UINT64)(UINTN)(Addr64), 32) & 0xFFFFFFFF))
121#define XHC_BIT_IS_SET(Data, Bit) ((BOOLEAN)(((Data) & (Bit)) == (Bit)))
122
123//
124// Endpoint Type (EP Type).
125//
126#define ED_NOT_VALID 0
127#define ED_ISOCH_OUT 1
128#define ED_BULK_OUT 2
129#define ED_INTERRUPT_OUT 3
130#define ED_CONTROL_BIDIR 4
131#define ED_ISOCH_IN 5
132#define ED_BULK_IN 6
133#define ED_INTERRUPT_IN 7
134
135//
136// 6.4.5 TRB Completion Codes
137//
138#define TRB_COMPLETION_INVALID 0
139#define TRB_COMPLETION_SUCCESS 1
140#define TRB_COMPLETION_DATA_BUFFER_ERROR 2
141#define TRB_COMPLETION_BABBLE_ERROR 3
142#define TRB_COMPLETION_USB_TRANSACTION_ERROR 4
143#define TRB_COMPLETION_TRB_ERROR 5
144#define TRB_COMPLETION_STALL_ERROR 6
145#define TRB_COMPLETION_SHORT_PACKET 13
146
147//
148// 6.4.6 TRB Types
149//
150#define TRB_TYPE_NORMAL 1
151#define TRB_TYPE_SETUP_STAGE 2
152#define TRB_TYPE_DATA_STAGE 3
153#define TRB_TYPE_STATUS_STAGE 4
154#define TRB_TYPE_ISOCH 5
155#define TRB_TYPE_LINK 6
156#define TRB_TYPE_EVENT_DATA 7
157#define TRB_TYPE_NO_OP 8
158#define TRB_TYPE_EN_SLOT 9
159#define TRB_TYPE_DIS_SLOT 10
160#define TRB_TYPE_ADDRESS_DEV 11
161#define TRB_TYPE_CON_ENDPOINT 12
162#define TRB_TYPE_EVALU_CONTXT 13
163#define TRB_TYPE_RESET_ENDPOINT 14
164#define TRB_TYPE_STOP_ENDPOINT 15
165#define TRB_TYPE_SET_TR_DEQUE 16
166#define TRB_TYPE_RESET_DEV 17
167#define TRB_TYPE_GET_PORT_BANW 21
168#define TRB_TYPE_FORCE_HEADER 22
169#define TRB_TYPE_NO_OP_COMMAND 23
170#define TRB_TYPE_TRANS_EVENT 32
171#define TRB_TYPE_COMMAND_COMPLT_EVENT 33
172#define TRB_TYPE_PORT_STATUS_CHANGE_EVENT 34
173#define TRB_TYPE_HOST_CONTROLLER_EVENT 37
174#define TRB_TYPE_DEVICE_NOTIFI_EVENT 38
175#define TRB_TYPE_MFINDEX_WRAP_EVENT 39
176
177//
178// Convert millisecond to microsecond.
179//
180#define XHC_1_MILLISECOND (1000)
181#define XHC_POLL_DELAY (1000)
182#define XHC_GENERIC_TIMEOUT (10 * 1000)
183
184#define EFI_USB_SPEED_FULL 0x0000
185#define EFI_USB_SPEED_LOW 0x0001
186#define EFI_USB_SPEED_HIGH 0x0002
187#define EFI_USB_SPEED_SUPER 0x0003
188
189//
190// Transfer types, used in URB to identify the transfer type
191//
192#define XHC_CTRL_TRANSFER 0x01
193#define XHC_BULK_TRANSFER 0x02
194#define XHC_INT_TRANSFER_SYNC 0x04
195#define XHC_INT_TRANSFER_ASYNC 0x08
196#define XHC_INT_ONLY_TRANSFER_ASYNC 0x10
197
198//
199// USB Transfer Results
200//
201#define EFI_USB_NOERROR 0x00
202#define EFI_USB_ERR_NOTEXECUTE 0x01
203#define EFI_USB_ERR_STALL 0x02
204#define EFI_USB_ERR_BUFFER 0x04
205#define EFI_USB_ERR_BABBLE 0x08
206#define EFI_USB_ERR_NAK 0x10
207#define EFI_USB_ERR_CRC 0x20
208#define EFI_USB_ERR_TIMEOUT 0x40
209#define EFI_USB_ERR_BITSTUFF 0x80
210#define EFI_USB_ERR_SYSTEM 0x100
211
212#pragma pack(1)
213
214//
215// 7.6.9 OUT/IN EP Context: 64 bytes
216// 7.6.9.2 When used by the DbC it is always a 64 byte data structure
217//
218typedef struct _ENDPOINT_CONTEXT_64 {
219 UINT32 EPState : 3;
220 UINT32 RsvdZ1 : 5;
221 UINT32 Mult : 2; // set to 0
222 UINT32 MaxPStreams : 5; // set to 0
223 UINT32 LSA : 1; // set to 0
224 UINT32 Interval : 8; // set to 0
225 UINT32 RsvdZ2 : 8;
226
227 UINT32 RsvdZ3 : 1;
228 UINT32 CErr : 2;
229 UINT32 EPType : 3;
230 UINT32 RsvdZ4 : 1;
231 UINT32 HID : 1; // set to 0
232 UINT32 MaxBurstSize : 8;
233 UINT32 MaxPacketSize : 16;
234
235 UINT32 PtrLo;
236
237 UINT32 PtrHi;
238
239 UINT32 AverageTRBLength : 16;
240 UINT32 MaxESITPayload : 16; // set to 0
241
242 UINT32 RsvdZ5; // Reserved
243 UINT32 RsvdZ6;
244 UINT32 RsvdZ7;
245
246 UINT32 RsvdZ8;
247 UINT32 RsvdZ9;
248 UINT32 RsvdZ10;
249 UINT32 RsvdZ11;
250
251 UINT32 RsvdZ12;
252 UINT32 RsvdZ13;
253 UINT32 RsvdZ14;
254 UINT32 RsvdZ15;
256
257//
258// 6.4.1.1 Normal TRB: 16 bytes
259// A Normal TRB is used in several ways; exclusively on Bulk and Interrupt Transfer Rings for normal and
260// Scatter/Gather operations, to define additional data buffers for Scatter/Gather operations on Isoch Transfer
261// Rings, and to define the Data stage information for Control Transfer Rings.
262//
263typedef struct _TRANSFER_TRB_NORMAL {
264 UINT32 TRBPtrLo;
265
266 UINT32 TRBPtrHi;
267
268 UINT32 Length : 17;
269 UINT32 TDSize : 5;
270 UINT32 IntTarget : 10;
271
272 UINT32 CycleBit : 1;
273 UINT32 ENT : 1;
274 UINT32 ISP : 1;
275 UINT32 NS : 1;
276 UINT32 CH : 1;
277 UINT32 IOC : 1;
278 UINT32 IDT : 1;
279 UINT32 RsvdZ1 : 2;
280 UINT32 BEI : 1;
281 UINT32 Type : 6;
282 UINT32 RsvdZ2 : 16;
284
285//
286// 6.4.2.1 Transfer Event TRB: 16 bytes
287// A Transfer Event provides the completion status associated with a Transfer TRB. Refer to section 4.11.3.1
288// for more information on the use and operation of Transfer Events.
289//
290typedef struct _EVT_TRB_TRANSFER {
291 UINT32 TRBPtrLo;
292
293 UINT32 TRBPtrHi;
294
295 UINT32 Length : 24;
296 UINT32 Completecode : 8;
297
298 UINT32 CycleBit : 1;
299 UINT32 RsvdZ1 : 1;
300 UINT32 ED : 1;
301 UINT32 RsvdZ2 : 7;
302 UINT32 Type : 6;
303 UINT32 EndpointId : 5;
304 UINT32 RsvdZ3 : 3;
305 UINT32 SlotId : 8;
307
308//
309// 6.4.4.1 Link TRB: 16 bytes
310// A Link TRB provides support for non-contiguous TRB Rings.
311//
312typedef struct _LINK_TRB {
313 UINT32 PtrLo;
314
315 UINT32 PtrHi;
316
317 UINT32 RsvdZ1 : 22;
318 UINT32 InterTarget : 10;
319
320 UINT32 CycleBit : 1;
321 UINT32 TC : 1;
322 UINT32 RsvdZ2 : 2;
323 UINT32 CH : 1;
324 UINT32 IOC : 1;
325 UINT32 RsvdZ3 : 4;
326 UINT32 Type : 6;
327 UINT32 RsvdZ4 : 16;
328} LINK_TRB;
329
330//
331// TRB Template: 16 bytes
332//
333typedef struct _TRB_TEMPLATE {
334 UINT32 Parameter1;
335
336 UINT32 Parameter2;
337
338 UINT32 Status;
339
340 UINT32 CycleBit : 1;
341 UINT32 RsvdZ1 : 9;
342 UINT32 Type : 6;
343 UINT32 Control : 16;
345
346//
347// Refer to XHCI 6.5 Event Ring Segment Table: 16 bytes
348//
349typedef struct _EVENT_RING_SEG_TABLE_ENTRY {
350 UINT32 PtrLo;
351 UINT32 PtrHi;
352 UINT32 RingTrbSize : 16;
353 UINT32 RsvdZ1 : 16;
354 UINT32 RsvdZ2;
356
357//
358// Size: 40 bytes
359//
360typedef struct _EVENT_RING {
361 EFI_PHYSICAL_ADDRESS ERSTBase;
362 EFI_PHYSICAL_ADDRESS EventRingSeg0;
363 UINT32 TrbNumber;
364 EFI_PHYSICAL_ADDRESS EventRingEnqueue;
365 EFI_PHYSICAL_ADDRESS EventRingDequeue;
366 UINT32 EventRingCCS;
367} EVENT_RING;
368
369// Size: 32 bytes
370typedef struct _TRANSFER_RING {
371 EFI_PHYSICAL_ADDRESS RingSeg0;
372 UINT32 TrbNumber;
373 EFI_PHYSICAL_ADDRESS RingEnqueue;
374 EFI_PHYSICAL_ADDRESS RingDequeue;
375 UINT32 RingPCS;
377
378//
379// Size: 64 bytes
380//
381typedef struct _DBC_INFO_CONTEXT {
382 UINT64 String0DescAddress;
383 UINT64 ManufacturerStrDescAddress;
384 UINT64 ProductStrDescAddress;
385 UINT64 SerialNumberStrDescAddress;
386 UINT64 String0Length : 8;
387 UINT64 ManufacturerStrLength : 8;
388 UINT64 ProductStrLength : 8;
389 UINT64 SerialNumberStrLength : 8;
390 UINT64 RsvdZ1 : 32;
391 UINT64 RsvdZ2;
392 UINT64 RsvdZ3;
393 UINT64 RsvdZ4;
395
396//
397// Debug Capability Context Data Structure: 192 bytes
398//
399typedef struct _XHC_DC_CONTEXT {
400 DBC_INFO_CONTEXT DbcInfoContext;
401 ENDPOINT_CONTEXT_64 EpOutContext;
402 ENDPOINT_CONTEXT_64 EpInContext;
404
405//
406// Size: 16 bytes
407//
408typedef union _TRB {
409 TRB_TEMPLATE TrbTemplate;
410 TRANSFER_TRB_NORMAL TrbNormal;
411} TRB;
412
416typedef enum {
417 EfiUsbDataIn,
418 EfiUsbDataOut,
419 EfiUsbNoData
421
422//
423// URB (Usb Request Block) contains information for all kinds of
424// usb requests.
425//
426typedef struct _URB {
427 //
428 // Transfer data buffer
429 //
431 UINT32 DataLen;
432
433 //
434 // Execute result
435 //
436 UINT32 Result;
437 //
438 // Completed data length
439 //
440 UINT32 Completed;
441 //
442 // Tranfer Ring info
443 //
446 BOOLEAN Finished;
447 EFI_USB_DATA_DIRECTION Direction;
448} URB;
449
451 UINT8 Initialized;
452
453 //
454 // The flag indicates debug capability is supported
455 //
456 BOOLEAN DebugSupport;
457
458 //
459 // The flag indicates debug device is ready
460 //
461 BOOLEAN Ready;
462
463 //
464 // The flag indicates the instance is from HOB
465 //
466 BOOLEAN FromHob;
467
468 //
469 // Prevent notification being interrupted by debug timer
470 //
471 BOOLEAN InNotify;
472
473 //
474 // PciIo protocol event
475 //
476 EFI_PHYSICAL_ADDRESS PciIoEvent;
477
478 //
479 // The flag indicates if USB 3.0 ports has been turn off/on power
480 //
481 BOOLEAN ChangePortPower;
482
483 //
484 // XHCI MMIO Base address
485 //
486 EFI_PHYSICAL_ADDRESS XhciMmioBase;
487
488 //
489 // XHCI OP RegisterBase address
490 //
491 EFI_PHYSICAL_ADDRESS XhciOpRegister;
492
493 //
494 // XHCI Debug Register Base Address
495 //
496 EFI_PHYSICAL_ADDRESS DebugCapabilityBase;
497
498 //
499 // XHCI Debug Capability offset
500 //
501 UINT64 DebugCapabilityOffset;
502
503 //
504 // XHCI Debug Context Address
505 //
506 EFI_PHYSICAL_ADDRESS DebugCapabilityContext;
507
508 //
509 // Transfer Ring
510 //
511 TRANSFER_RING TransferRingOut;
512 TRANSFER_RING TransferRingIn;
513
514 //
515 // EventRing
516 //
517 EVENT_RING EventRing;
518
519 //
520 // URB - Read
521 //
522 URB UrbOut;
523
524 //
525 // URB - Write
526 //
527 URB UrbIn;
528
529 //
530 // The available data length in the following data buffer.
531 //
532 UINT8 DataCount;
533 //
534 // The data buffer address for data read and poll.
535 //
538
539#pragma pack()
540
550UINT32
553 IN UINT32 Offset
554 );
555
564VOID
567 IN UINT32 Offset,
568 IN UINT32 Bit
569 );
570
579VOID
582 IN UINT32 Offset,
583 IN UINT32 Data
584 );
585
596BOOLEAN
599 UINT32 BitMask
600 );
601
608VOID
611 UINT32 BitMask
612 );
613
620VOID
623 IN UINT32 BitMask
624 );
625
634RETURN_STATUS
635EFIAPI
637 VOID
638 );
639
644UINT16
646 VOID
647 );
648
657VOID *
659 IN UINTN BufferSize
660 );
661
670RETURN_STATUS
671EFIAPI
673 VOID
674 );
675
695EFIAPI
698 IN EFI_USB_DATA_DIRECTION Direction,
699 IN OUT VOID *Data,
700 IN OUT UINTN *DataLength,
701 IN UINTN Timeout
702 );
703
713RETURN_STATUS
714EFIAPI
717 );
718
725 VOID
726 );
727
734 VOID
735 );
736
737#endif //__SERIAL_PORT_LIB_USB__
UINT64 UINTN
EFI_STATUS EFIAPI XhcDataTransfer(IN USB3_DEBUG_PORT_HANDLE *Handle, IN EFI_USB_DATA_DIRECTION Direction, IN OUT VOID *Data, IN OUT UINTN *DataLength, IN UINTN Timeout)
UINT16 GetXhciPciCommand(VOID)
VOID XhcSetDebugRegBit(IN USB3_DEBUG_PORT_HANDLE *Handle, IN UINT32 Offset, IN UINT32 Bit)
VOID * AllocateAlignBuffer(IN UINTN BufferSize)
VOID XhcClearR32Bit(IN OUT UINTN Register, IN UINT32 BitMask)
USB3_DEBUG_PORT_HANDLE * GetUsb3DebugPortInstance(VOID)
EFI_PHYSICAL_ADDRESS * GetUsb3DebugPortInstanceAddrPtr(VOID)
UINT32 XhcReadDebugReg(IN USB3_DEBUG_PORT_HANDLE *Handle, IN UINT32 Offset)
BOOLEAN XhcIsBitSet(UINTN Register, UINT32 BitMask)
VOID XhcWriteDebugReg(IN USB3_DEBUG_PORT_HANDLE *Handle, IN UINT32 Offset, IN UINT32 Data)
RETURN_STATUS EFIAPI USB3InitializeReal(VOID)
RETURN_STATUS EFIAPI InitializeUsbDebugHardware(IN USB3_DEBUG_PORT_HANDLE *Handle)
VOID XhcSetR32Bit(UINTN Register, UINT32 BitMask)
RETURN_STATUS EFIAPI USB3Initialize(VOID)
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
EFI_USB_DATA_DIRECTION
Definition: UsbIo.h:44
EFI_STATUS EFIAPI Register(IN EFI_PEI_RSC_HANDLER_CALLBACK Callback)
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
Definition: EhciUrb.h:200