2 Debug Port Library implementation based on usb3 debug port.
4 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "DebugCommunicationLibUsb3Internal.h"
18 // The global variable which can be used after memory is ready.
20 USB3_DEBUG_PORT_HANDLE mDebugCommunicationLibUsb3DebugPortHandle
;
22 UINT16 mString0Desc
[] = {
23 // String Descriptor Type + Length
24 ( USB_DESC_TYPE_STRING
<< 8 ) + STRING0_DESC_LEN
,
28 UINT16 mManufacturerStrDesc
[] = {
29 // String Descriptor Type + Length
30 ( USB_DESC_TYPE_STRING
<< 8 ) + MANU_DESC_LEN
,
31 'I', 'n', 't', 'e', 'l'
34 UINT16 mProductStrDesc
[] = {
35 // String Descriptor Type + Length
36 ( USB_DESC_TYPE_STRING
<< 8 ) + PRODUCT_DESC_LEN
,
37 'U', 'S', 'B', ' ', '3', '.', '0', ' ', 'D', 'e', 'b', 'u', 'g', ' ', 'C', 'a', 'b', 'l', 'e'
40 UINT16 mSerialNumberStrDesc
[] = {
41 // String Descriptor Type + Length
42 ( USB_DESC_TYPE_STRING
<< 8 ) + SERIAL_DESC_LEN
,
47 Sets bits as per the enabled bit positions in the mask.
49 @param[in, out] Register UINTN register
50 @param[in] BitMask 32-bit mask
54 IN OUT UINTN Register
,
60 RegisterValue
= MmioRead32 (Register
);
61 RegisterValue
|= (UINT32
)(BitMask
);
62 MmioWrite32 (Register
, RegisterValue
);
66 Clears bits as per the enabled bit positions in the mask.
68 @param[in, out] Register UINTN register
69 @param[in] BitMask 32-bit mask
73 IN OUT UINTN Register
,
79 RegisterValue
= MmioRead32 (Register
);
80 RegisterValue
&= ~BitMask
;
81 MmioWrite32 (Register
, RegisterValue
);
85 Write the data to the XHCI debug register.
87 @param Handle Debug port handle.
88 @param Offset The offset of the runtime register.
89 @param Data The data to write.
94 IN USB3_DEBUG_PORT_HANDLE
*Handle
,
99 EFI_PHYSICAL_ADDRESS DebugCapabilityBase
;
101 DebugCapabilityBase
= Handle
->DebugCapabilityBase
;
102 MmioWrite32 ((UINTN
)(DebugCapabilityBase
+ Offset
), Data
);
108 Read XHCI debug register.
110 @param Handle Debug port handle.
111 @param Offset The offset of the runtime register.
113 @return The register content read
118 IN USB3_DEBUG_PORT_HANDLE
*Handle
,
123 EFI_PHYSICAL_ADDRESS DebugCapabilityBase
;
125 DebugCapabilityBase
= Handle
->DebugCapabilityBase
;
126 Data
= MmioRead32 ((UINTN
)(DebugCapabilityBase
+ Offset
));
132 Set one bit of the runtime register while keeping other bits.
134 @param Handle Debug port handle.
135 @param Offset The offset of the runtime register.
136 @param Bit The bit mask of the register to set.
141 IN USB3_DEBUG_PORT_HANDLE
*Handle
,
148 Data
= XhcReadDebugReg (Handle
, Offset
);
150 XhcWriteDebugReg (Handle
, Offset
, Data
);
154 Program and eanble XHCI MMIO base address.
156 @return XHCI MMIO base address.
160 ProgramXhciBaseAddress (
167 EFI_PHYSICAL_ADDRESS XhciMmioBase
;
169 Low
= PciRead32 (PcdGet32(PcdUsbXhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
170 High
= PciRead32 (PcdGet32(PcdUsbXhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ 4);
171 XhciMmioBase
= (EFI_PHYSICAL_ADDRESS
) (LShiftU64 ((UINT64
) High
, 32) | Low
);
172 XhciMmioBase
&= XHCI_BASE_ADDRESS_64_BIT_MASK
;
174 if ((XhciMmioBase
== 0) || (XhciMmioBase
== XHCI_BASE_ADDRESS_64_BIT_MASK
)) {
175 XhciMmioBase
= PcdGet64(PcdUsbXhciMemorySpaceBase
);
176 PciWrite32(PcdGet32(PcdUsbXhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
, XhciMmioBase
& 0xFFFFFFFF);
177 PciWrite32(PcdGet32(PcdUsbXhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ 4, (RShiftU64 (XhciMmioBase
, 32) & 0xFFFFFFFF));
180 PciCmd
= PciRead16 (PcdGet32(PcdUsbXhciPciAddress
) + PCI_COMMAND_OFFSET
);
181 if (((PciCmd
& EFI_PCI_COMMAND_MEMORY_SPACE
) == 0) || ((PciCmd
& EFI_PCI_COMMAND_BUS_MASTER
) == 0)) {
182 PciCmd
|= EFI_PCI_COMMAND_MEMORY_SPACE
| EFI_PCI_COMMAND_BUS_MASTER
;
183 PciWrite16(PcdGet32(PcdUsbXhciPciAddress
) + PCI_COMMAND_OFFSET
, PciCmd
);
190 Check if the timer is timeout.
192 @param[in] UsbDebugPortHandle Pointer to USB Debug port handle
193 @param[in] Timer The start timer from the begin.
194 @param[in] TimeoutTicker Ticker number need time out.
196 @return TRUE Timer time out occurs.
197 @retval FALSE Timer does not time out.
202 IN USB3_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
,
204 IN UINT64 TimeoutTicker
210 CurrentTimer
= GetPerformanceCounter ();
212 if (UsbDebugPortHandle
->TimerCountDown
) {
214 // The timer counter counts down. Check for roll over condition.
216 if (CurrentTimer
< Timer
) {
217 Delta
= Timer
- CurrentTimer
;
220 // Handle one roll-over.
222 Delta
= UsbDebugPortHandle
->TimerCycle
- (CurrentTimer
- Timer
);
226 // The timer counter counts up. Check for roll over condition.
228 if (CurrentTimer
> Timer
) {
229 Delta
= CurrentTimer
- Timer
;
232 // Handle one roll-over.
234 Delta
= UsbDebugPortHandle
->TimerCycle
- (Timer
- CurrentTimer
);
238 return (BOOLEAN
) (Delta
>= TimeoutTicker
);
242 Update XHC MMIO base address when MMIO base address is changed.
244 @param Handle Debug port handle.
245 @param XhciMmioBase XHCI MMIO base address.
250 IN OUT USB3_DEBUG_PORT_HANDLE
*Handle
,
251 IN EFI_PHYSICAL_ADDRESS XhciMmioBase
254 if ((Handle
== NULL
) || (Handle
->XhciMmioBase
== XhciMmioBase
)) {
259 // Need fix Handle data according to new XHCI MMIO base address.
261 Handle
->XhciMmioBase
= XhciMmioBase
;
262 Handle
->DebugCapabilityBase
= XhciMmioBase
+ Handle
->DebugCapabilityOffset
;
263 Handle
->XhciOpRegister
= XhciMmioBase
+ MmioRead8 ((UINTN
)XhciMmioBase
);
267 Calculate the usb debug port bar address.
269 @param Handle Debug port handle.
271 @retval RETURN_UNSUPPORTED The usb host controller does not supported usb debug port capability.
272 @retval RETURN_SUCCESS Get bar and offset successfully.
277 CalculateUsbDebugPortMmioBase (
278 USB3_DEBUG_PORT_HANDLE
*Handle
288 EFI_PHYSICAL_ADDRESS CapabilityPointer
;
291 VendorId
= PciRead16 (PcdGet32(PcdUsbXhciPciAddress
) + PCI_VENDOR_ID_OFFSET
);
292 DeviceId
= PciRead16 (PcdGet32(PcdUsbXhciPciAddress
) + PCI_DEVICE_ID_OFFSET
);
294 if ((VendorId
== 0xFFFF) || (DeviceId
== 0xFFFF)) {
298 ProgInterface
= PciRead8 (PcdGet32(PcdUsbXhciPciAddress
) + PCI_CLASSCODE_OFFSET
);
299 SubClassCode
= PciRead8 (PcdGet32(PcdUsbXhciPciAddress
) + PCI_CLASSCODE_OFFSET
+ 1);
300 BaseCode
= PciRead8 (PcdGet32(PcdUsbXhciPciAddress
) + PCI_CLASSCODE_OFFSET
+ 2);
302 if ((ProgInterface
!= PCI_IF_XHCI
) || (SubClassCode
!= PCI_CLASS_SERIAL_USB
) || (BaseCode
!= PCI_CLASS_SERIAL
)) {
306 CapLength
= MmioRead8 ((UINTN
) Handle
->XhciMmioBase
);
309 // Get capability pointer from HCCPARAMS at offset 0x10
311 CapabilityPointer
= Handle
->XhciMmioBase
+ (MmioRead32 ((UINTN
)(Handle
->XhciMmioBase
+ XHC_HCCPARAMS_OFFSET
)) >> 16) * 4;
314 // Search XHCI debug capability
317 Capability
= MmioRead32 ((UINTN
)CapabilityPointer
);
319 if ((Capability
& XHC_CAPABILITY_ID_MASK
) == PCI_CAPABILITY_ID_DEBUG_PORT
) {
323 if ((((Capability
& XHC_NEXT_CAPABILITY_MASK
) >> 8) & XHC_CAPABILITY_ID_MASK
) == 0) {
325 // Reach the end of capability list, quit
329 CapabilityPointer
+= ((Capability
& XHC_NEXT_CAPABILITY_MASK
) >> 8) * 4;
330 Capability
= MmioRead32 ((UINTN
)CapabilityPointer
);
338 // USB3 debug capability is supported.
340 Handle
->DebugCapabilityBase
= CapabilityPointer
;
341 Handle
->DebugCapabilityOffset
= CapabilityPointer
- Handle
->XhciMmioBase
;
342 Handle
->XhciOpRegister
= Handle
->XhciMmioBase
+ CapLength
;
343 Handle
->Initialized
= USB3DBG_DBG_CAB
;
344 return RETURN_SUCCESS
;
347 Handle
->Initialized
= USB3DBG_NO_DBG_CAB
;
348 return RETURN_UNSUPPORTED
;
352 Check if it needs to re-initialize usb debug port hardware.
354 During different phases switch, such as SEC to PEI or PEI to DXE or DXE to SMM, we should check
355 whether the usb debug port hardware configuration is changed. Such case can be triggerred by
356 Pci bus resource allocation and so on.
358 @param Handle Debug port handle.
360 @retval TRUE The usb debug port hardware configuration is changed.
361 @retval FALSE The usb debug port hardware configuration is not changed.
366 NeedReinitializeHardware(
367 IN USB3_DEBUG_PORT_HANDLE
*Handle
371 volatile UINT32 Dcctrl
;
376 // If DCE bit, it means USB3 debug is not enabled.
378 Dcctrl
= XhcReadDebugReg (Handle
, XHC_DC_DCCTRL
);
379 if ((Dcctrl
& BIT0
) == 0) {
387 Create XHCI event ring.
389 @param Handle Debug port handle.
390 @param EventRing The created event ring.
395 IN USB3_DEBUG_PORT_HANDLE
*Handle
,
396 OUT EVENT_RING
*EventRing
400 EVENT_RING_SEG_TABLE_ENTRY
*ERSTBase
;
402 ASSERT (EventRing
!= NULL
);
405 // Allocate Event Ring
407 Buf
= AllocateAlignBuffer (sizeof (TRB_TEMPLATE
) * EVENT_RING_TRB_NUMBER
);
408 ASSERT (Buf
!= NULL
);
409 ASSERT (((UINTN
) Buf
& 0x3F) == 0);
410 ZeroMem (Buf
, sizeof (TRB_TEMPLATE
) * EVENT_RING_TRB_NUMBER
);
412 EventRing
->EventRingSeg0
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Buf
;
413 EventRing
->TrbNumber
= EVENT_RING_TRB_NUMBER
;
414 EventRing
->EventRingDequeue
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) EventRing
->EventRingSeg0
;
415 EventRing
->EventRingEnqueue
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) EventRing
->EventRingSeg0
;
418 // Software maintains an Event Ring Consumer Cycle State (CCS) bit, initializing it to '1'
419 // and toggling it every time the Event Ring Dequeue Pointer wraps back to the beginning of the Event Ring.
421 EventRing
->EventRingCCS
= 1;
424 // Allocate Event Ring Segment Table Entry 0 in Event Ring Segment Table
426 Buf
= AllocateAlignBuffer (sizeof (EVENT_RING_SEG_TABLE_ENTRY
) * ERST_NUMBER
);
427 ASSERT (Buf
!= NULL
);
428 ASSERT (((UINTN
) Buf
& 0x3F) == 0);
429 ZeroMem (Buf
, sizeof (EVENT_RING_SEG_TABLE_ENTRY
) * ERST_NUMBER
);
431 ERSTBase
= (EVENT_RING_SEG_TABLE_ENTRY
*) Buf
;
432 EventRing
->ERSTBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) ERSTBase
;
435 // Fill Event Segment address
437 ERSTBase
->PtrLo
= XHC_LOW_32BIT (EventRing
->EventRingSeg0
);
438 ERSTBase
->PtrHi
= XHC_HIGH_32BIT (EventRing
->EventRingSeg0
);
439 ERSTBase
->RingTrbSize
= EVENT_RING_TRB_NUMBER
;
442 // Program the Interrupter Event Ring Dequeue Pointer (DCERDP) register (7.6.4.1)
447 XHC_LOW_32BIT((UINT64
)(UINTN
)EventRing
->EventRingDequeue
)
453 XHC_HIGH_32BIT((UINT64
)(UINTN
)EventRing
->EventRingDequeue
)
457 // Program the Debug Capability Event Ring Segment Table Base Address (DCERSTBA) register(7.6.4.1)
462 XHC_LOW_32BIT((UINT64
)(UINTN
)ERSTBase
)
468 XHC_HIGH_32BIT((UINT64
)(UINTN
)ERSTBase
)
472 // Program the Debug Capability Event Ring Segment Table Size (DCERSTSZ) register(7.6.4.1)
483 Create XHCI transfer ring.
485 @param Handle Debug port handle.
486 @param TrbNum The number of TRB in the ring.
487 @param TransferRing The created transfer ring.
492 IN USB3_DEBUG_PORT_HANDLE
*Handle
,
494 OUT TRANSFER_RING
*TransferRing
500 Buf
= AllocateAlignBuffer (sizeof (TRB_TEMPLATE
) * TrbNum
);
501 ASSERT (Buf
!= NULL
);
502 ASSERT (((UINTN
) Buf
& 0xF) == 0);
503 ZeroMem (Buf
, sizeof (TRB_TEMPLATE
) * TrbNum
);
505 TransferRing
->RingSeg0
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Buf
;
506 TransferRing
->TrbNumber
= TrbNum
;
507 TransferRing
->RingEnqueue
= TransferRing
->RingSeg0
;
508 TransferRing
->RingDequeue
= TransferRing
->RingSeg0
;
509 TransferRing
->RingPCS
= 1;
511 // 4.9.2 Transfer Ring Management
512 // To form a ring (or circular queue) a Link TRB may be inserted at the end of a ring to
513 // point to the first TRB in the ring.
515 EndTrb
= (LINK_TRB
*) ((UINTN
)Buf
+ sizeof (TRB_TEMPLATE
) * (TrbNum
- 1));
516 EndTrb
->Type
= TRB_TYPE_LINK
;
517 EndTrb
->PtrLo
= XHC_LOW_32BIT (Buf
);
518 EndTrb
->PtrHi
= XHC_HIGH_32BIT (Buf
);
520 // Toggle Cycle (TC). When set to '1', the xHC shall toggle its interpretation of the Cycle bit.
524 // Set Cycle bit as other TRB PCS init value
526 EndTrb
->CycleBit
= 0;
530 Create debug capability context for XHC debug device.
532 @param Handle Debug port handle.
534 @retval EFI_SUCCESS The bit successfully changed by host controller.
535 @retval EFI_TIMEOUT The time out occurred.
539 CreateDebugCapabilityContext (
540 IN USB3_DEBUG_PORT_HANDLE
*Handle
544 XHC_DC_CONTEXT
*DebugCapabilityContext
;
546 UINT8
*ManufacturerStrDesc
;
547 UINT8
*ProductStrDesc
;
548 UINT8
*SerialNumberStrDesc
;
551 // Allocate debug device context
553 Buf
= AllocateAlignBuffer (sizeof (XHC_DC_CONTEXT
));
554 ASSERT (Buf
!= NULL
);
555 ASSERT (((UINTN
) Buf
& 0xF) == 0);
556 ZeroMem (Buf
, sizeof (XHC_DC_CONTEXT
));
558 DebugCapabilityContext
= (XHC_DC_CONTEXT
*)(UINTN
) Buf
;
559 Handle
->DebugCapabilityContext
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) DebugCapabilityContext
;
562 // Initialize DbcInfoContext.
564 DebugCapabilityContext
->DbcInfoContext
.String0Length
= STRING0_DESC_LEN
;
565 DebugCapabilityContext
->DbcInfoContext
.ManufacturerStrLength
= MANU_DESC_LEN
;
566 DebugCapabilityContext
->DbcInfoContext
.ProductStrLength
= PRODUCT_DESC_LEN
;
567 DebugCapabilityContext
->DbcInfoContext
.SerialNumberStrLength
= SERIAL_DESC_LEN
;
570 // Initialize EpOutContext.
572 DebugCapabilityContext
->EpOutContext
.CErr
= 0x3;
573 DebugCapabilityContext
->EpOutContext
.EPType
= ED_BULK_OUT
;
574 DebugCapabilityContext
->EpOutContext
.MaxPacketSize
= 0x400;
575 DebugCapabilityContext
->EpOutContext
.AverageTRBLength
= 0x1000;
578 // Initialize EpInContext.
580 DebugCapabilityContext
->EpInContext
.CErr
= 0x3;
581 DebugCapabilityContext
->EpInContext
.EPType
= ED_BULK_IN
;
582 DebugCapabilityContext
->EpInContext
.MaxPacketSize
= 0x400;
583 DebugCapabilityContext
->EpInContext
.AverageTRBLength
= 0x1000;
586 // Update string descriptor address
588 String0Desc
= (UINT8
*) AllocateAlignBuffer (STRING0_DESC_LEN
+ MANU_DESC_LEN
+ PRODUCT_DESC_LEN
+ SERIAL_DESC_LEN
);
589 ASSERT (String0Desc
!= NULL
);
590 ZeroMem (String0Desc
, STRING0_DESC_LEN
+ MANU_DESC_LEN
+ PRODUCT_DESC_LEN
+ SERIAL_DESC_LEN
);
591 CopyMem (String0Desc
, mString0Desc
, STRING0_DESC_LEN
);
592 DebugCapabilityContext
->DbcInfoContext
.String0DescAddress
= (UINT64
)(UINTN
)String0Desc
;
594 ManufacturerStrDesc
= String0Desc
+ STRING0_DESC_LEN
;
595 CopyMem (ManufacturerStrDesc
, mManufacturerStrDesc
, MANU_DESC_LEN
);
596 DebugCapabilityContext
->DbcInfoContext
.ManufacturerStrDescAddress
= (UINT64
)(UINTN
)ManufacturerStrDesc
;
598 ProductStrDesc
= ManufacturerStrDesc
+ MANU_DESC_LEN
;
599 CopyMem (ProductStrDesc
, mProductStrDesc
, PRODUCT_DESC_LEN
);
600 DebugCapabilityContext
->DbcInfoContext
.ProductStrDescAddress
= (UINT64
)(UINTN
)ProductStrDesc
;
602 SerialNumberStrDesc
= ProductStrDesc
+ PRODUCT_DESC_LEN
;
603 CopyMem (SerialNumberStrDesc
, mSerialNumberStrDesc
, SERIAL_DESC_LEN
);
604 DebugCapabilityContext
->DbcInfoContext
.SerialNumberStrDescAddress
= (UINT64
)(UINTN
)SerialNumberStrDesc
;
607 // Allocate and initialize the Transfer Ring for the Input Endpoint Context.
609 ZeroMem (&Handle
->TransferRingIn
, sizeof (TRANSFER_RING
));
610 CreateTransferRing (Handle
, TR_RING_TRB_NUMBER
, &Handle
->TransferRingIn
);
611 DebugCapabilityContext
->EpInContext
.PtrLo
= XHC_LOW_32BIT (Handle
->TransferRingIn
.RingSeg0
) | BIT0
;
612 DebugCapabilityContext
->EpInContext
.PtrHi
= XHC_HIGH_32BIT (Handle
->TransferRingIn
.RingSeg0
);
615 // Allocate and initialize the Transfer Ring for the Output Endpoint Context.
617 ZeroMem (&Handle
->TransferRingOut
, sizeof (TRANSFER_RING
));
618 CreateTransferRing (Handle
, TR_RING_TRB_NUMBER
, &Handle
->TransferRingOut
);
619 DebugCapabilityContext
->EpOutContext
.PtrLo
= XHC_LOW_32BIT (Handle
->TransferRingOut
.RingSeg0
) | BIT0
;
620 DebugCapabilityContext
->EpOutContext
.PtrHi
= XHC_HIGH_32BIT (Handle
->TransferRingOut
.RingSeg0
);
623 // Program the Debug Capability Context Pointer (DCCP) register(7.6.8.7)
628 XHC_LOW_32BIT((UINT64
)(UINTN
)DebugCapabilityContext
)
633 XHC_HIGH_32BIT((UINT64
)(UINTN
)DebugCapabilityContext
)
639 Check if debug device is running.
641 @param Handle Debug port handle.
645 XhcDetectDebugCapabilityReady (
646 IN USB3_DEBUG_PORT_HANDLE
*Handle
650 volatile UINT32 Dcctrl
;
653 if (Handle
->Initialized
== USB3DBG_DBG_CAB
) {
655 // As detection is slow in seconds, wait for longer timeout for the first time.
656 // If first initialization is failed, we will try to enable debug device in the
657 // Poll function invoked by timer.
659 TimeOut
= DivU64x32 (PcdGet64 (PcdUsbXhciDebugDetectTimeout
), XHC_POLL_DELAY
) + 1;
664 // Check if debug device is in configured state
666 Dcctrl
= XhcReadDebugReg (Handle
, XHC_DC_DCCTRL
);
667 if ((Dcctrl
& BIT0
) != 0) {
669 // Set the flag to indicate debug device is in configured state
671 Handle
->Ready
= TRUE
;
674 MicroSecondDelay (XHC_POLL_DELAY
);
676 } while (TimeOut
!= 0);
680 Initialize usb debug port hardware.
682 @param Handle Debug port handle.
684 @retval TRUE The usb debug port hardware configuration is changed.
685 @retval FALSE The usb debug port hardware configuration is not changed.
690 InitializeUsbDebugHardware (
691 IN USB3_DEBUG_PORT_HANDLE
*Handle
694 RETURN_STATUS Status
;
698 EFI_PHYSICAL_ADDRESS XhciOpRegister
;
700 XhciOpRegister
= Handle
->XhciOpRegister
;
701 TotalUsb3Port
= MmioRead32 (((UINTN
) Handle
->XhciMmioBase
+ XHC_HCSPARAMS1_OFFSET
)) >> 24;
703 if (Handle
->Initialized
== USB3DBG_NOT_ENABLED
) {
705 // If XHCI supports debug capability, hardware resource has been allocated,
706 // but it has not been enabled, try to enable again.
712 // Initialize for PEI phase when AllocatePages can work
714 Buffer
= AllocateAlignBuffer (XHC_DEBUG_PORT_DATA_LENGTH
);
715 if (Buffer
== NULL
) {
717 // AllocatePages can not still work now, return fail and do not initialize now.
719 return RETURN_NOT_READY
;
723 // Reset port to get debug device discovered
725 for (Index
= 0; Index
< TotalUsb3Port
; Index
++) {
726 XhcSetR32Bit ((UINTN
)XhciOpRegister
+ XHC_PORTSC_OFFSET
+ Index
* 0x10, BIT4
);
727 MicroSecondDelay (10 * 1000);
731 // Construct the buffer for URB in and URB out
733 Handle
->UrbIn
.Data
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Buffer
;
734 Handle
->UrbOut
.Data
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Buffer
+ XHC_DEBUG_PORT_DATA_LENGTH
;
737 // Initialize event ring
739 ZeroMem (&Handle
->EventRing
, sizeof (EVENT_RING
));
740 Status
= CreateEventRing (Handle
, &Handle
->EventRing
);
741 ASSERT_EFI_ERROR (Status
);
744 // Init IN and OUT endpoint context
746 Status
= CreateDebugCapabilityContext (Handle
);
747 ASSERT_EFI_ERROR (Status
);
750 // Init DCDDI1 and DCDDI2
755 (UINT32
)((XHCI_DEBUG_DEVICE_VENDOR_ID
<< 16) | XHCI_DEBUG_DEVICE_PROTOCOL
)
761 (UINT32
)((XHCI_DEBUG_DEVICE_REVISION
<< 16) | XHCI_DEBUG_DEVICE_PRODUCT_ID
)
765 if ((Handle
->Initialized
== USB3DBG_NOT_ENABLED
) && (!Handle
->ChangePortPower
)) {
767 // If the first time detection is failed, turn port power off and on in order to
768 // reset port status this time, then try to check if debug device is ready again.
770 for (Index
= 0; Index
< TotalUsb3Port
; Index
++) {
771 XhcClearR32Bit ((UINTN
)XhciOpRegister
+ XHC_PORTSC_OFFSET
+ Index
* 0x10, BIT9
);
772 MicroSecondDelay (XHC_DEBUG_PORT_ON_OFF_DELAY
);
773 XhcSetR32Bit ((UINTN
)XhciOpRegister
+ XHC_PORTSC_OFFSET
+ Index
* 0x10, BIT9
);
774 MicroSecondDelay (XHC_DEBUG_PORT_ON_OFF_DELAY
);
775 Handle
->ChangePortPower
= TRUE
;
780 // Set DCE bit and LSE bit to "1" in DCCTRL in first initialization
782 XhcSetDebugRegBit (Handle
, XHC_DC_DCCTRL
, BIT1
|BIT31
);
784 XhcDetectDebugCapabilityReady (Handle
);
786 Status
= RETURN_SUCCESS
;
787 if (!Handle
->Ready
) {
788 Handle
->Initialized
= USB3DBG_NOT_ENABLED
;
789 Status
= RETURN_NOT_READY
;
791 Handle
->Initialized
= USB3DBG_ENABLED
;
798 Read data from debug device and save the data in buffer.
800 Reads NumberOfBytes data bytes from a debug device into the buffer
801 specified by Buffer. The number of bytes actually read is returned.
802 If the return value is less than NumberOfBytes, then the rest operation failed.
803 If NumberOfBytes is zero, then return 0.
805 @param Handle Debug port handle.
806 @param Buffer Pointer to the data buffer to store the data read from the debug device.
807 @param NumberOfBytes Number of bytes which will be read.
808 @param Timeout Timeout value for reading from debug device. It unit is Microsecond.
810 @retval 0 Read data failed, no data is to be read.
811 @retval >0 Actual number of bytes read from debug device.
816 DebugPortReadBuffer (
817 IN DEBUG_PORT_HANDLE Handle
,
819 IN UINTN NumberOfBytes
,
823 USB3_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
824 RETURN_STATUS Status
;
831 UINT64 TimeoutTicker
;
833 EFI_PHYSICAL_ADDRESS XhciMmioBase
;
835 if (NumberOfBytes
== 0 || Buffer
== NULL
) {
844 // If Handle is NULL, it means memory is ready for use.
845 // Use global variable to store handle value.
847 if (Handle
== NULL
) {
848 UsbDebugPortHandle
= &mDebugCommunicationLibUsb3DebugPortHandle
;
850 UsbDebugPortHandle
= (USB3_DEBUG_PORT_HANDLE
*)Handle
;
853 if (UsbDebugPortHandle
->Initialized
== USB3DBG_NO_DBG_CAB
) {
857 XhciMmioBase
= ProgramXhciBaseAddress ();
858 UpdateXhcResource (UsbDebugPortHandle
, XhciMmioBase
);
860 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
861 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
862 if (RETURN_ERROR(Status
)) {
868 // First read data from buffer, then read debug port hw to get received data.
870 if (UsbDebugPortHandle
->DataCount
> 0) {
871 if (NumberOfBytes
<= UsbDebugPortHandle
->DataCount
) {
872 Total
= NumberOfBytes
;
874 Total
= UsbDebugPortHandle
->DataCount
;
877 for (Index
= 0; Index
< Total
; Index
++) {
878 Buffer
[Index
] = UsbDebugPortHandle
->Data
[Index
];
881 for (Index
= 0; Index
< UsbDebugPortHandle
->DataCount
- Total
; Index
++) {
882 if (Total
+ Index
>= 8) {
885 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortHandle
->Data
[Total
+ Index
];
887 UsbDebugPortHandle
->DataCount
= (UINT8
)(UsbDebugPortHandle
->DataCount
- (UINT8
)Total
);
891 // If Timeout is equal to 0, then it means it should always wait until all data required are received.
897 Begin
= GetPerformanceCounter ();
898 TimeoutTicker
= DivU64x32 (
900 UsbDebugPortHandle
->TimerFrequency
,
905 TimerRound
= DivU64x64Remainder (
907 DivU64x32 (UsbDebugPortHandle
->TimerCycle
, 2),
913 // Read remaining data by executing one or more usb debug transfer transactions at usb debug port hw.
915 while (Total
< NumberOfBytes
) {
917 if (TimerRound
== 0) {
918 if (IsTimerTimeout (UsbDebugPortHandle
, Begin
, TimeoutTicker
)) {
920 // If time out occurs.
925 if (IsTimerTimeout (UsbDebugPortHandle
, Begin
, DivU64x32 (UsbDebugPortHandle
->TimerCycle
, 2))) {
930 Remaining
= NumberOfBytes
- Total
;
931 if (Remaining
>= USB3_DEBUG_PORT_MAX_PACKET_SIZE
) {
932 Received
= USB3_DEBUG_PORT_MAX_PACKET_SIZE
;
933 Status
= XhcDataTransfer (UsbDebugPortHandle
, EfiUsbDataIn
, Buffer
+ Total
, &Received
, DATA_TRANSFER_READ_TIMEOUT
);
935 Received
= USB3_DEBUG_PORT_MAX_PACKET_SIZE
;
936 Status
= XhcDataTransfer (UsbDebugPortHandle
, EfiUsbDataIn
, &UsbDebugPortHandle
->Data
[0], &Received
, DATA_TRANSFER_READ_TIMEOUT
);
937 UsbDebugPortHandle
->DataCount
= (UINT8
) Received
;
939 if (Remaining
<= Received
) {
941 // The data received are more than required
943 Length
= (UINT8
)Remaining
;
946 // The data received are less than the remaining bytes
948 Length
= (UINT8
)Received
;
952 // Copy required data from the data buffer to user buffer.
954 for (Index
= 0; Index
< Length
; Index
++) {
955 (Buffer
+ Total
)[Index
] = UsbDebugPortHandle
->Data
[Index
];
956 UsbDebugPortHandle
->DataCount
--;
960 // reorder the data buffer to make available data arranged from the beginning of the data buffer.
962 for (Index
= 0; Index
< Received
- Length
; Index
++) {
963 if (Length
+ Index
>= 8) {
966 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortHandle
->Data
[Length
+ Index
];
969 // fixup the real required length of data.
979 Write data from buffer to debug device.
981 Writes NumberOfBytes data bytes from Buffer to the debug device.
982 The number of bytes actually written to the debug device is returned.
983 If the return value is less than NumberOfBytes, then the write operation failed.
984 If NumberOfBytes is zero, then return 0.
986 @param Handle Debug port handle.
987 @param Buffer Pointer to the data buffer to be written.
988 @param NumberOfBytes Number of bytes to written to the debug device.
990 @retval 0 NumberOfBytes is 0.
991 @retval >0 The number of bytes written to the debug device.
992 If this value is less than NumberOfBytes, then the read operation failed.
997 DebugPortWriteBuffer (
998 IN DEBUG_PORT_HANDLE Handle
,
1000 IN UINTN NumberOfBytes
1003 USB3_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
1004 RETURN_STATUS Status
;
1007 EFI_PHYSICAL_ADDRESS XhciMmioBase
;
1010 if (NumberOfBytes
== 0 || Buffer
== NULL
) {
1018 // If Handle is NULL, it means memory is ready for use.
1019 // Use global variable to store handle value.
1021 if (Handle
== NULL
) {
1022 UsbDebugPortHandle
= &mDebugCommunicationLibUsb3DebugPortHandle
;
1024 UsbDebugPortHandle
= (USB3_DEBUG_PORT_HANDLE
*)Handle
;
1027 if (UsbDebugPortHandle
->Initialized
== USB3DBG_NO_DBG_CAB
) {
1032 // MMIO base address is possible to clear, set it if it is cleared. (XhciMemorySpaceClose in PchUsbCommon.c)
1034 XhciMmioBase
= ProgramXhciBaseAddress ();
1036 UpdateXhcResource (UsbDebugPortHandle
, XhciMmioBase
);
1038 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
1039 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
1040 if (RETURN_ERROR(Status
)) {
1046 // When host is trying to send data, write will be blocked.
1047 // Poll to see if there is any data sent by host at first.
1049 DebugPortPollBuffer (Handle
);
1052 while ((Total
< NumberOfBytes
)) {
1053 if (NumberOfBytes
- Total
> USB3_DEBUG_PORT_MAX_PACKET_SIZE
) {
1054 Sent
= USB3_DEBUG_PORT_MAX_PACKET_SIZE
;
1056 Sent
= (UINT8
)(NumberOfBytes
- Total
);
1058 Status
= XhcDataTransfer (UsbDebugPortHandle
, EfiUsbDataOut
, Buffer
+ Total
, &Sent
, DATA_TRANSFER_WRITE_TIMEOUT
);
1066 Polls a debug device to see if there is any data waiting to be read.
1068 Polls a debug device to see if there is any data waiting to be read.
1069 If there is data waiting to be read from the debug device, then TRUE is returned.
1070 If there is no data waiting to be read from the debug device, then FALSE is returned.
1072 @param Handle Debug port handle.
1074 @retval TRUE Data is waiting to be read from the debug device.
1075 @retval FALSE There is no data waiting to be read from the serial device.
1080 DebugPortPollBuffer (
1081 IN DEBUG_PORT_HANDLE Handle
1084 USB3_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
1086 RETURN_STATUS Status
;
1087 UINT8 Buffer
[XHC_DEBUG_PORT_DATA_LENGTH
];
1088 EFI_PHYSICAL_ADDRESS XhciMmioBase
;
1091 // If Handle is NULL, it means memory is ready for use.
1092 // Use global variable to store handle value.
1094 if (Handle
== NULL
) {
1095 UsbDebugPortHandle
= &mDebugCommunicationLibUsb3DebugPortHandle
;
1097 UsbDebugPortHandle
= (USB3_DEBUG_PORT_HANDLE
*)Handle
;
1100 if (UsbDebugPortHandle
->Initialized
== USB3DBG_NO_DBG_CAB
) {
1104 XhciMmioBase
= ProgramXhciBaseAddress ();
1105 UpdateXhcResource (UsbDebugPortHandle
, XhciMmioBase
);
1107 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
1108 Status
= InitializeUsbDebugHardware(UsbDebugPortHandle
);
1109 if (RETURN_ERROR(Status
)) {
1115 // If the data buffer is not empty, then return TRUE directly.
1116 // Otherwise initialize a usb read transaction and read data to internal data buffer.
1118 if (UsbDebugPortHandle
->DataCount
!= 0) {
1123 // Read most 8-bytes data
1125 Length
= XHC_DEBUG_PORT_DATA_LENGTH
;
1126 XhcDataTransfer (Handle
, EfiUsbDataIn
, Buffer
, &Length
, DATA_TRANSFER_POLL_TIMEOUT
);
1137 // Store data into internal buffer for use later
1139 CopyMem (UsbDebugPortHandle
->Data
, Buffer
, Length
);
1140 UsbDebugPortHandle
->DataCount
= (UINT8
) Length
;
1145 Initialize the debug port.
1147 If Function is not NULL, Debug Communication Libary will call this function
1148 by passing in the Context to be the first parameter. If needed, Debug Communication
1149 Library will create one debug port handle to be the second argument passing in
1150 calling the Function, otherwise it will pass NULL to be the second argument of
1153 If Function is NULL, and Context is not NULL, the Debug Communication Library could
1154 a) Return the same handle as passed in (as Context parameter).
1155 b) Ignore the input Context parameter and create new hanlde to be returned.
1157 If parameter Function is NULL and Context is NULL, Debug Communication Library could
1158 created a new handle if needed and return it, otherwise it will return NULL.
1160 @param[in] Context Context needed by callback function; it was optional.
1161 @param[in] Function Continue function called by Debug Communication library;
1164 @return The debug port handle created by Debug Communication Library if Function
1170 DebugPortInitialize (
1172 IN DEBUG_PORT_CONTINUE Function
1175 RETURN_STATUS Status
;
1176 USB3_DEBUG_PORT_HANDLE Handle
;
1177 USB3_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
1178 UINT64 TimerStartValue
;
1179 UINT64 TimerEndValue
;
1182 // Validate the PCD PcdDebugPortHandleBufferSize value
1184 ASSERT (PcdGet16 (PcdDebugPortHandleBufferSize
) == sizeof (USB3_DEBUG_PORT_HANDLE
));
1186 if (Function
== NULL
&& Context
!= NULL
) {
1187 UsbDebugPortHandle
= (USB3_DEBUG_PORT_HANDLE
*)Context
;
1189 ZeroMem(&Handle
, sizeof (USB3_DEBUG_PORT_HANDLE
));
1190 UsbDebugPortHandle
= &Handle
;
1193 UsbDebugPortHandle
->TimerFrequency
= GetPerformanceCounterProperties (
1198 if (TimerEndValue
< TimerStartValue
) {
1199 UsbDebugPortHandle
->TimerCountDown
= TRUE
;
1200 UsbDebugPortHandle
->TimerCycle
= TimerStartValue
- TimerEndValue
;
1202 UsbDebugPortHandle
->TimerCountDown
= FALSE
;
1203 UsbDebugPortHandle
->TimerCycle
= TimerEndValue
- TimerStartValue
;
1206 if (Function
== NULL
&& Context
!= NULL
) {
1207 return (DEBUG_PORT_HANDLE
*) Context
;
1211 // Read 64-bit MMIO base address
1213 UsbDebugPortHandle
->XhciMmioBase
= ProgramXhciBaseAddress ();
1215 Status
= CalculateUsbDebugPortMmioBase (UsbDebugPortHandle
);
1216 if (RETURN_ERROR (Status
)) {
1220 if (NeedReinitializeHardware(&Handle
)) {
1221 Status
= InitializeUsbDebugHardware (&Handle
);
1222 if (RETURN_ERROR(Status
)) {
1229 if (Function
!= NULL
) {
1230 Function (Context
, &Handle
);
1232 CopyMem(&mDebugCommunicationLibUsb3DebugPortHandle
, &Handle
, sizeof (USB3_DEBUG_PORT_HANDLE
));
1235 return (DEBUG_PORT_HANDLE
)(UINTN
)&mDebugCommunicationLibUsb3DebugPortHandle
;