2 Debug Port Library implementation based on usb3 debug port.
4 Copyright (c) 2014 - 2015, 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
= XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE
;
575 DebugCapabilityContext
->EpOutContext
.AverageTRBLength
= 0x1000;
578 // Initialize EpInContext.
580 DebugCapabilityContext
->EpInContext
.CErr
= 0x3;
581 DebugCapabilityContext
->EpInContext
.EPType
= ED_BULK_IN
;
582 DebugCapabilityContext
->EpInContext
.MaxPacketSize
= XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE
;
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.
713 // Allocate data buffer with max packet size for data read and data poll.
714 // Allocate data buffer for data write.
716 Buffer
= AllocateAlignBuffer (XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE
* 2 + USB3_DEBUG_PORT_WRITE_MAX_PACKET_SIZE
);
717 if (Buffer
== NULL
) {
719 // AllocatePages can not still work now, return fail and do not initialize now.
721 return RETURN_NOT_READY
;
725 // Reset port to get debug device discovered
727 for (Index
= 0; Index
< TotalUsb3Port
; Index
++) {
728 XhcSetR32Bit ((UINTN
)XhciOpRegister
+ XHC_PORTSC_OFFSET
+ Index
* 0x10, BIT4
);
729 MicroSecondDelay (10 * 1000);
733 // Construct the buffer for read, poll and write.
735 Handle
->UrbIn
.Data
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Buffer
;
736 Handle
->Data
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) Buffer
+ XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE
;
737 Handle
->UrbOut
.Data
= Handle
->UrbIn
.Data
+ XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE
* 2;
740 // Initialize event ring
742 ZeroMem (&Handle
->EventRing
, sizeof (EVENT_RING
));
743 Status
= CreateEventRing (Handle
, &Handle
->EventRing
);
744 ASSERT_EFI_ERROR (Status
);
747 // Init IN and OUT endpoint context
749 Status
= CreateDebugCapabilityContext (Handle
);
750 ASSERT_EFI_ERROR (Status
);
753 // Init DCDDI1 and DCDDI2
758 (UINT32
)((XHCI_DEBUG_DEVICE_VENDOR_ID
<< 16) | XHCI_DEBUG_DEVICE_PROTOCOL
)
764 (UINT32
)((XHCI_DEBUG_DEVICE_REVISION
<< 16) | XHCI_DEBUG_DEVICE_PRODUCT_ID
)
768 if ((Handle
->Initialized
== USB3DBG_NOT_ENABLED
) && (!Handle
->ChangePortPower
)) {
770 // If the first time detection is failed, turn port power off and on in order to
771 // reset port status this time, then try to check if debug device is ready again.
773 for (Index
= 0; Index
< TotalUsb3Port
; Index
++) {
774 XhcClearR32Bit ((UINTN
)XhciOpRegister
+ XHC_PORTSC_OFFSET
+ Index
* 0x10, BIT9
);
775 MicroSecondDelay (XHC_DEBUG_PORT_ON_OFF_DELAY
);
776 XhcSetR32Bit ((UINTN
)XhciOpRegister
+ XHC_PORTSC_OFFSET
+ Index
* 0x10, BIT9
);
777 MicroSecondDelay (XHC_DEBUG_PORT_ON_OFF_DELAY
);
778 Handle
->ChangePortPower
= TRUE
;
783 // Set DCE bit and LSE bit to "1" in DCCTRL in first initialization
785 XhcSetDebugRegBit (Handle
, XHC_DC_DCCTRL
, BIT1
|BIT31
);
787 XhcDetectDebugCapabilityReady (Handle
);
789 Status
= RETURN_SUCCESS
;
790 if (!Handle
->Ready
) {
791 Handle
->Initialized
= USB3DBG_NOT_ENABLED
;
792 Status
= RETURN_NOT_READY
;
794 Handle
->Initialized
= USB3DBG_ENABLED
;
801 Read data from debug device and save the data in buffer.
803 Reads NumberOfBytes data bytes from a debug device into the buffer
804 specified by Buffer. The number of bytes actually read is returned.
805 If the return value is less than NumberOfBytes, then the rest operation failed.
806 If NumberOfBytes is zero, then return 0.
808 @param Handle Debug port handle.
809 @param Buffer Pointer to the data buffer to store the data read from the debug device.
810 @param NumberOfBytes Number of bytes which will be read.
811 @param Timeout Timeout value for reading from debug device. It unit is Microsecond.
813 @retval 0 Read data failed, no data is to be read.
814 @retval >0 Actual number of bytes read from debug device.
819 DebugPortReadBuffer (
820 IN DEBUG_PORT_HANDLE Handle
,
822 IN UINTN NumberOfBytes
,
826 USB3_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
827 RETURN_STATUS Status
;
834 UINT64 TimeoutTicker
;
836 EFI_PHYSICAL_ADDRESS XhciMmioBase
;
839 if (NumberOfBytes
== 0 || Buffer
== NULL
) {
848 // If Handle is NULL, it means memory is ready for use.
849 // Use global variable to store handle value.
851 if (Handle
== NULL
) {
852 UsbDebugPortHandle
= &mDebugCommunicationLibUsb3DebugPortHandle
;
854 UsbDebugPortHandle
= (USB3_DEBUG_PORT_HANDLE
*)Handle
;
857 if (UsbDebugPortHandle
->Initialized
== USB3DBG_NO_DBG_CAB
) {
861 XhciMmioBase
= ProgramXhciBaseAddress ();
862 UpdateXhcResource (UsbDebugPortHandle
, XhciMmioBase
);
864 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
865 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
866 if (RETURN_ERROR(Status
)) {
871 Data
= (UINT8
*)(UINTN
)UsbDebugPortHandle
->Data
;
874 // First read data from buffer, then read debug port hw to get received data.
876 if (UsbDebugPortHandle
->DataCount
> 0) {
877 if (NumberOfBytes
<= UsbDebugPortHandle
->DataCount
) {
878 Total
= NumberOfBytes
;
880 Total
= UsbDebugPortHandle
->DataCount
;
883 for (Index
= 0; Index
< Total
; Index
++) {
884 Buffer
[Index
] = Data
[Index
];
887 for (Index
= 0; Index
< UsbDebugPortHandle
->DataCount
- Total
; Index
++) {
888 if (Total
+ Index
>= XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE
) {
891 Data
[Index
] = Data
[Total
+ Index
];
893 UsbDebugPortHandle
->DataCount
= (UINT8
)(UsbDebugPortHandle
->DataCount
- (UINT8
)Total
);
897 // If Timeout is equal to 0, then it means it should always wait until all data required are received.
903 Begin
= GetPerformanceCounter ();
904 TimeoutTicker
= DivU64x32 (
906 UsbDebugPortHandle
->TimerFrequency
,
911 TimerRound
= DivU64x64Remainder (
913 DivU64x32 (UsbDebugPortHandle
->TimerCycle
, 2),
919 // Read remaining data by executing one or more usb debug transfer transactions at usb debug port hw.
921 while (Total
< NumberOfBytes
) {
923 if (TimerRound
== 0) {
924 if (IsTimerTimeout (UsbDebugPortHandle
, Begin
, TimeoutTicker
)) {
926 // If time out occurs.
931 if (IsTimerTimeout (UsbDebugPortHandle
, Begin
, DivU64x32 (UsbDebugPortHandle
->TimerCycle
, 2))) {
936 Remaining
= NumberOfBytes
- Total
;
937 if (Remaining
>= XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE
) {
938 Received
= XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE
;
939 Status
= XhcDataTransfer (UsbDebugPortHandle
, EfiUsbDataIn
, Buffer
+ Total
, &Received
, DATA_TRANSFER_READ_TIMEOUT
);
941 Received
= XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE
;
942 Status
= XhcDataTransfer (UsbDebugPortHandle
, EfiUsbDataIn
, (VOID
*)Data
, &Received
, DATA_TRANSFER_READ_TIMEOUT
);
943 UsbDebugPortHandle
->DataCount
= (UINT8
) Received
;
945 if (Remaining
<= Received
) {
947 // The data received are more than required
949 Length
= (UINT8
)Remaining
;
952 // The data received are less than the remaining bytes
954 Length
= (UINT8
)Received
;
958 // Copy required data from the data buffer to user buffer.
960 for (Index
= 0; Index
< Length
; Index
++) {
961 (Buffer
+ Total
)[Index
] = Data
[Index
];
962 UsbDebugPortHandle
->DataCount
--;
966 // reorder the data buffer to make available data arranged from the beginning of the data buffer.
968 for (Index
= 0; Index
< Received
- Length
; Index
++) {
969 if (Length
+ Index
>= XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE
) {
972 Data
[Index
] = Data
[Length
+ Index
];
975 // fixup the real required length of data.
985 Write data from buffer to debug device.
987 Writes NumberOfBytes data bytes from Buffer to the debug device.
988 The number of bytes actually written to the debug device is returned.
989 If the return value is less than NumberOfBytes, then the write operation failed.
990 If NumberOfBytes is zero, then return 0.
992 @param Handle Debug port handle.
993 @param Buffer Pointer to the data buffer to be written.
994 @param NumberOfBytes Number of bytes to written to the debug device.
996 @retval 0 NumberOfBytes is 0.
997 @retval >0 The number of bytes written to the debug device.
998 If this value is less than NumberOfBytes, then the read operation failed.
1003 DebugPortWriteBuffer (
1004 IN DEBUG_PORT_HANDLE Handle
,
1006 IN UINTN NumberOfBytes
1009 USB3_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
1010 RETURN_STATUS Status
;
1013 EFI_PHYSICAL_ADDRESS XhciMmioBase
;
1016 if (NumberOfBytes
== 0 || Buffer
== NULL
) {
1024 // If Handle is NULL, it means memory is ready for use.
1025 // Use global variable to store handle value.
1027 if (Handle
== NULL
) {
1028 UsbDebugPortHandle
= &mDebugCommunicationLibUsb3DebugPortHandle
;
1030 UsbDebugPortHandle
= (USB3_DEBUG_PORT_HANDLE
*)Handle
;
1033 if (UsbDebugPortHandle
->Initialized
== USB3DBG_NO_DBG_CAB
) {
1038 // MMIO base address is possible to clear, set it if it is cleared. (XhciMemorySpaceClose in PchUsbCommon.c)
1040 XhciMmioBase
= ProgramXhciBaseAddress ();
1042 UpdateXhcResource (UsbDebugPortHandle
, XhciMmioBase
);
1044 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
1045 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
1046 if (RETURN_ERROR(Status
)) {
1052 // When host is trying to send data, write will be blocked.
1053 // Poll to see if there is any data sent by host at first.
1055 DebugPortPollBuffer (Handle
);
1058 while ((Total
< NumberOfBytes
)) {
1059 if (NumberOfBytes
- Total
> USB3_DEBUG_PORT_WRITE_MAX_PACKET_SIZE
) {
1060 Sent
= USB3_DEBUG_PORT_WRITE_MAX_PACKET_SIZE
;
1062 Sent
= (UINT8
)(NumberOfBytes
- Total
);
1064 Status
= XhcDataTransfer (UsbDebugPortHandle
, EfiUsbDataOut
, Buffer
+ Total
, &Sent
, DATA_TRANSFER_WRITE_TIMEOUT
);
1072 Polls a debug device to see if there is any data waiting to be read.
1074 Polls a debug device to see if there is any data waiting to be read.
1075 If there is data waiting to be read from the debug device, then TRUE is returned.
1076 If there is no data waiting to be read from the debug device, then FALSE is returned.
1078 @param Handle Debug port handle.
1080 @retval TRUE Data is waiting to be read from the debug device.
1081 @retval FALSE There is no data waiting to be read from the serial device.
1086 DebugPortPollBuffer (
1087 IN DEBUG_PORT_HANDLE Handle
1090 USB3_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
1092 RETURN_STATUS Status
;
1093 EFI_PHYSICAL_ADDRESS XhciMmioBase
;
1096 // If Handle is NULL, it means memory is ready for use.
1097 // Use global variable to store handle value.
1099 if (Handle
== NULL
) {
1100 UsbDebugPortHandle
= &mDebugCommunicationLibUsb3DebugPortHandle
;
1102 UsbDebugPortHandle
= (USB3_DEBUG_PORT_HANDLE
*)Handle
;
1105 if (UsbDebugPortHandle
->Initialized
== USB3DBG_NO_DBG_CAB
) {
1109 XhciMmioBase
= ProgramXhciBaseAddress ();
1110 UpdateXhcResource (UsbDebugPortHandle
, XhciMmioBase
);
1112 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
1113 Status
= InitializeUsbDebugHardware(UsbDebugPortHandle
);
1114 if (RETURN_ERROR(Status
)) {
1120 // If the data buffer is not empty, then return TRUE directly.
1121 // Otherwise initialize a usb read transaction and read data to internal data buffer.
1123 if (UsbDebugPortHandle
->DataCount
!= 0) {
1128 // Read data as much as we can
1130 Length
= XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE
;
1131 XhcDataTransfer (Handle
, EfiUsbDataIn
, (VOID
*)(UINTN
)UsbDebugPortHandle
->Data
, &Length
, DATA_TRANSFER_POLL_TIMEOUT
);
1133 if (Length
> XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE
) {
1142 // Store data into internal buffer for use later
1144 UsbDebugPortHandle
->DataCount
= (UINT8
) Length
;
1149 Initialize the debug port.
1151 If Function is not NULL, Debug Communication Libary will call this function
1152 by passing in the Context to be the first parameter. If needed, Debug Communication
1153 Library will create one debug port handle to be the second argument passing in
1154 calling the Function, otherwise it will pass NULL to be the second argument of
1157 If Function is NULL, and Context is not NULL, the Debug Communication Library could
1158 a) Return the same handle as passed in (as Context parameter).
1159 b) Ignore the input Context parameter and create new hanlde to be returned.
1161 If parameter Function is NULL and Context is NULL, Debug Communication Library could
1162 created a new handle if needed and return it, otherwise it will return NULL.
1164 @param[in] Context Context needed by callback function; it was optional.
1165 @param[in] Function Continue function called by Debug Communication library;
1168 @return The debug port handle created by Debug Communication Library if Function
1174 DebugPortInitialize (
1176 IN DEBUG_PORT_CONTINUE Function
1179 RETURN_STATUS Status
;
1180 USB3_DEBUG_PORT_HANDLE Handle
;
1181 USB3_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
1182 UINT64 TimerStartValue
;
1183 UINT64 TimerEndValue
;
1186 // Validate the PCD PcdDebugPortHandleBufferSize value
1188 ASSERT (PcdGet16 (PcdDebugPortHandleBufferSize
) == sizeof (USB3_DEBUG_PORT_HANDLE
));
1190 if (Function
== NULL
&& Context
!= NULL
) {
1191 UsbDebugPortHandle
= (USB3_DEBUG_PORT_HANDLE
*)Context
;
1193 ZeroMem(&Handle
, sizeof (USB3_DEBUG_PORT_HANDLE
));
1194 UsbDebugPortHandle
= &Handle
;
1197 UsbDebugPortHandle
->TimerFrequency
= GetPerformanceCounterProperties (
1202 if (TimerEndValue
< TimerStartValue
) {
1203 UsbDebugPortHandle
->TimerCountDown
= TRUE
;
1204 UsbDebugPortHandle
->TimerCycle
= TimerStartValue
- TimerEndValue
;
1206 UsbDebugPortHandle
->TimerCountDown
= FALSE
;
1207 UsbDebugPortHandle
->TimerCycle
= TimerEndValue
- TimerStartValue
;
1210 if (Function
== NULL
&& Context
!= NULL
) {
1211 return (DEBUG_PORT_HANDLE
*) Context
;
1215 // Read 64-bit MMIO base address
1217 UsbDebugPortHandle
->XhciMmioBase
= ProgramXhciBaseAddress ();
1219 Status
= CalculateUsbDebugPortMmioBase (UsbDebugPortHandle
);
1220 if (RETURN_ERROR (Status
)) {
1224 if (NeedReinitializeHardware(&Handle
)) {
1225 Status
= InitializeUsbDebugHardware (&Handle
);
1226 if (RETURN_ERROR(Status
)) {
1233 if (Function
!= NULL
) {
1234 Function (Context
, &Handle
);
1236 CopyMem(&mDebugCommunicationLibUsb3DebugPortHandle
, &Handle
, sizeof (USB3_DEBUG_PORT_HANDLE
));
1239 return (DEBUG_PORT_HANDLE
)(UINTN
)&mDebugCommunicationLibUsb3DebugPortHandle
;