]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SourceLevelDebugPkg/Library/DebugCommunicationLibUsb3/DebugCommunicationLibUsb3Common.c
SourceLevelDebugPkg DebugUsb3: Fix GCC build failures
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugCommunicationLibUsb3 / DebugCommunicationLibUsb3Common.c
index fe6aec1ca522fc2a205834067f518b188e9f21ed..c577df7dea97e09947f6b562d85e0244a66315bc 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Debug Port Library implementation based on usb3 debug port.\r
 \r
-  Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
 \r
 #include "DebugCommunicationLibUsb3Internal.h"\r
 \r
-//\r
-// The global variable which can be used after memory is ready.\r
-//\r
-USB3_DEBUG_PORT_HANDLE     mDebugCommunicationLibUsb3DebugPortHandle;\r
-\r
 UINT16   mString0Desc[] = {\r
   //  String Descriptor Type + Length\r
   ( USB_DESC_TYPE_STRING << 8 ) + STRING0_DESC_LEN,\r
@@ -85,7 +80,7 @@ XhcClearR32Bit(
   Write the data to the XHCI debug register.\r
 \r
   @param  Handle       Debug port handle.\r
-  @param  Offset       The offset of the runtime register.\r
+  @param  Offset       The offset of the debug register.\r
   @param  Data         The data to write.\r
 \r
 **/\r
@@ -129,16 +124,16 @@ XhcReadDebugReg (
 }\r
 \r
 /**\r
-  Set one bit of the runtime register while keeping other bits.\r
+  Set one bit of the debug register while keeping other bits.\r
 \r
   @param  Handle       Debug port handle.\r
-  @param  Offset       The offset of the runtime register.\r
+  @param  Offset       The offset of the debug register.\r
   @param  Bit          The bit mask of the register to set.\r
 \r
 **/\r
 VOID\r
 XhcSetDebugRegBit (\r
-  IN USB3_DEBUG_PORT_HANDLE *Handle,\r
+  IN USB3_DEBUG_PORT_HANDLE   *Handle,\r
   IN UINT32                   Offset,\r
   IN UINT32                   Bit\r
   )\r
@@ -150,6 +145,28 @@ XhcSetDebugRegBit (
   XhcWriteDebugReg (Handle, Offset, Data);\r
 }\r
 \r
+/**\r
+  Clear one bit of the debug register while keeping other bits.\r
+\r
+  @param  Handle       Debug port handle.\r
+  @param  Offset       The offset of the debug register.\r
+  @param  Bit          The bit mask of the register to clear.\r
+\r
+**/\r
+VOID\r
+XhcClearDebugRegBit (\r
+  IN USB3_DEBUG_PORT_HANDLE   *Handle,\r
+  IN UINT32                   Offset,\r
+  IN UINT32                   Bit\r
+  )\r
+{\r
+  UINT32                  Data;\r
+\r
+  Data  = XhcReadDebugReg (Handle, Offset);\r
+  Data  &= ~Bit;\r
+  XhcWriteDebugReg (Handle, Offset, Data);\r
+}\r
+\r
 /**\r
   Program and eanble XHCI MMIO base address.\r
 \r
@@ -186,58 +203,6 @@ ProgramXhciBaseAddress (
   return XhciMmioBase;\r
 }\r
 \r
-/**\r
-  Check if the timer is timeout.\r
-  \r
-  @param[in] UsbDebugPortHandle  Pointer to USB Debug port handle\r
-  @param[in] Timer               The start timer from the begin.\r
-  @param[in] TimeoutTicker       Ticker number need time out.\r
-\r
-  @return TRUE  Timer time out occurs.\r
-  @retval FALSE Timer does not time out.\r
-\r
-**/\r
-BOOLEAN\r
-IsTimerTimeout (\r
-  IN USB3_DEBUG_PORT_HANDLE  *UsbDebugPortHandle,\r
-  IN UINT64                  Timer,\r
-  IN UINT64                  TimeoutTicker\r
-  )\r
-{\r
-  UINT64  CurrentTimer;\r
-  UINT64  Delta;\r
-\r
-  CurrentTimer = GetPerformanceCounter ();\r
-\r
-  if (UsbDebugPortHandle->TimerCountDown) {\r
-    //\r
-    // The timer counter counts down.  Check for roll over condition.\r
-    //\r
-    if (CurrentTimer < Timer) {\r
-      Delta = Timer - CurrentTimer;\r
-    } else {\r
-      //\r
-      // Handle one roll-over. \r
-      //\r
-      Delta = UsbDebugPortHandle->TimerCycle - (CurrentTimer - Timer);\r
-    }\r
-  } else {\r
-    //\r
-    // The timer counter counts up.  Check for roll over condition.\r
-    //\r
-    if (CurrentTimer > Timer) {\r
-      Delta = CurrentTimer - Timer;\r
-    } else {\r
-      //\r
-      // Handle one roll-over. \r
-      //\r
-      Delta = UsbDebugPortHandle->TimerCycle - (Timer - CurrentTimer);\r
-    }\r
-  }\r
\r
-  return (BOOLEAN) (Delta >= TimeoutTicker);\r
-}\r
-\r
 /**\r
   Update XHC MMIO base address when MMIO base address is changed.\r
 \r
@@ -251,7 +216,7 @@ UpdateXhcResource (
   IN EFI_PHYSICAL_ADDRESS                   XhciMmioBase\r
   )\r
 {\r
-  if ((Handle == NULL) || (Handle->XhciMmioBase == XhciMmioBase)) {\r
+  if (Handle == NULL) {\r
     return;\r
   }\r
 \r
@@ -268,7 +233,7 @@ UpdateXhcResource (
 \r
   @param  Handle             Debug port handle.\r
 \r
-  @retval RETURN_UNSUPPORTED The usb host controller does not supported usb debug port capability.\r
+  @retval RETURN_UNSUPPORTED The usb host controller does not support usb debug port capability.\r
   @retval RETURN_SUCCESS     Get bar and offset successfully.\r
 \r
 **/\r
@@ -288,6 +253,14 @@ CalculateUsbDebugPortMmioBase (
   EFI_PHYSICAL_ADDRESS            CapabilityPointer;\r
   UINT8                           CapLength;\r
 \r
+  if (Handle->Initialized != USB3DBG_UNINITIALIZED) {\r
+    if (Handle->Initialized == USB3DBG_NO_DBG_CAB) {\r
+      return RETURN_UNSUPPORTED;\r
+    } else {\r
+      return RETURN_SUCCESS;\r
+    }\r
+  }\r
+\r
   VendorId = PciRead16 (PcdGet32(PcdUsbXhciPciAddress) + PCI_VENDOR_ID_OFFSET);\r
   DeviceId = PciRead16 (PcdGet32(PcdUsbXhciPciAddress) + PCI_DEVICE_ID_OFFSET);\r
   \r
@@ -340,6 +313,7 @@ CalculateUsbDebugPortMmioBase (
   Handle->DebugCapabilityBase   = CapabilityPointer;\r
   Handle->DebugCapabilityOffset = CapabilityPointer - Handle->XhciMmioBase;\r
   Handle->XhciOpRegister        = Handle->XhciMmioBase + CapLength;\r
+  Handle->DebugSupport = TRUE;\r
   Handle->Initialized = USB3DBG_DBG_CAB;\r
   return RETURN_SUCCESS;\r
 \r
@@ -352,7 +326,7 @@ Done:
   Check if it needs to re-initialize usb debug port hardware.\r
 \r
   During different phases switch, such as SEC to PEI or PEI to DXE or DXE to SMM, we should check\r
-  whether the usb debug port hardware configuration is changed. Such case can be triggerred by\r
+  whether the usb debug port hardware configuration is changed. Such case can be triggered by\r
   Pci bus resource allocation and so on.\r
 \r
   @param  Handle           Debug port handle.\r
@@ -378,6 +352,9 @@ NeedReinitializeHardware(
   Dcctrl = XhcReadDebugReg (Handle, XHC_DC_DCCTRL);\r
   if ((Dcctrl & BIT0) == 0) {\r
     Result = TRUE;\r
+  } else if (!Handle->Ready) {\r
+    Handle->Ready = TRUE;\r
+    Handle->Initialized = USB3DBG_ENABLED;\r
   }\r
 \r
   return Result;\r
@@ -571,7 +548,7 @@ CreateDebugCapabilityContext (
   //\r
   DebugCapabilityContext->EpOutContext.CErr             = 0x3;\r
   DebugCapabilityContext->EpOutContext.EPType           = ED_BULK_OUT;\r
-  DebugCapabilityContext->EpOutContext.MaxPacketSize    = 0x400;\r
+  DebugCapabilityContext->EpOutContext.MaxPacketSize    = XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE;\r
   DebugCapabilityContext->EpOutContext.AverageTRBLength = 0x1000;\r
   \r
   //\r
@@ -579,7 +556,7 @@ CreateDebugCapabilityContext (
   //\r
   DebugCapabilityContext->EpInContext.CErr             = 0x3;\r
   DebugCapabilityContext->EpInContext.EPType           = ED_BULK_IN;\r
-  DebugCapabilityContext->EpInContext.MaxPacketSize    = 0x400;\r
+  DebugCapabilityContext->EpInContext.MaxPacketSize    = XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE;\r
   DebugCapabilityContext->EpInContext.AverageTRBLength = 0x1000;\r
   \r
   //\r
@@ -709,9 +686,11 @@ InitializeUsbDebugHardware (
   }\r
 \r
   //\r
-  // Initialize for PEI phase when AllocatePages can work\r
+  // Initialize for PEI phase when AllocatePages can work.\r
+  // Allocate data buffer with max packet size for data read and data poll.\r
+  // Allocate data buffer for data write.\r
   //\r
-  Buffer = AllocateAlignBuffer (XHC_DEBUG_PORT_DATA_LENGTH);\r
+  Buffer = AllocateAlignBuffer (XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE * 2 + USB3_DEBUG_PORT_WRITE_MAX_PACKET_SIZE);\r
   if (Buffer == NULL) {\r
     //\r
     // AllocatePages can not still work now, return fail and do not initialize now.\r
@@ -728,10 +707,18 @@ InitializeUsbDebugHardware (
   }\r
 \r
   //\r
-  // Construct the buffer for URB in and URB out\r
+  // Clear DCE bit and LSE bit in DCCTRL\r
+  //\r
+  if ((XhcReadDebugReg (Handle, XHC_DC_DCCTRL) & (BIT1|BIT31)) == (BIT1|BIT31)) {\r
+    XhcClearDebugRegBit (Handle, XHC_DC_DCCTRL, BIT1|BIT31);\r
+  }\r
+\r
+  //\r
+  // Construct the buffer for read, poll and write.\r
   //\r
   Handle->UrbIn.Data  = (EFI_PHYSICAL_ADDRESS)(UINTN) Buffer;\r
-  Handle->UrbOut.Data = (EFI_PHYSICAL_ADDRESS)(UINTN) Buffer + XHC_DEBUG_PORT_DATA_LENGTH;\r
+  Handle->Data        = (EFI_PHYSICAL_ADDRESS)(UINTN) Buffer + XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE;\r
+  Handle->UrbOut.Data = Handle->UrbIn.Data + XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE * 2;\r
    \r
   //\r
   // Initialize event ring\r
@@ -794,6 +781,35 @@ Enable:
   return Status;\r
 }\r
 \r
+/**\r
+  Discover and initialize usb debug port.\r
+\r
+  @param Handle                 Debug port handle.\r
+\r
+**/\r
+VOID\r
+DiscoverInitializeUsbDebugPort (\r
+  IN USB3_DEBUG_PORT_HANDLE     *Handle\r
+  )\r
+{\r
+  EFI_STATUS                    Status;\r
+  EFI_PHYSICAL_ADDRESS          XhciMmioBase;\r
+\r
+  //\r
+  // Read 64-bit MMIO base address\r
+  //\r
+  XhciMmioBase = ProgramXhciBaseAddress ();\r
+  Handle->XhciMmioBase = XhciMmioBase;\r
+\r
+  Status = CalculateUsbDebugPortMmioBase (Handle);\r
+  if (!RETURN_ERROR (Status)) {\r
+    UpdateXhcResource (Handle, XhciMmioBase);\r
+    if (NeedReinitializeHardware (Handle)) {\r
+      InitializeUsbDebugHardware (Handle);\r
+    }\r
+  }\r
+}\r
+\r
 /**\r
   Read data from debug device and save the data in buffer.\r
 \r
@@ -805,7 +821,7 @@ Enable:
   @param  Handle           Debug port handle.\r
   @param  Buffer           Pointer to the data buffer to store the data read from the debug device.\r
   @param  NumberOfBytes    Number of bytes which will be read.\r
-  @param  Timeout          Timeout value for reading from debug device. It unit is Microsecond.\r
+  @param  Timeout          Timeout value for reading from debug device. Its unit is Microsecond.\r
 \r
   @retval 0                Read data failed, no data is to be read.\r
   @retval >0               Actual number of bytes read from debug device.\r
@@ -821,158 +837,45 @@ DebugPortReadBuffer (
   )\r
 {\r
   USB3_DEBUG_PORT_HANDLE    *UsbDebugPortHandle;\r
-  RETURN_STATUS             Status;\r
-  UINTN                     Received;\r
-  UINTN                     Total;\r
-  UINTN                     Remaining;\r
   UINT8                     Index;\r
-  UINTN                     Length;\r
-  UINT64                    Begin;\r
-  UINT64                    TimeoutTicker;\r
-  UINT64                    TimerRound;\r
-  EFI_PHYSICAL_ADDRESS      XhciMmioBase;\r
+  UINT8                     *Data;\r
 \r
-  if (NumberOfBytes == 0 || Buffer == NULL) {\r
+  if (NumberOfBytes != 1 || Buffer == NULL || Timeout != 0) {\r
     return 0;\r
   }\r
 \r
-  Received  = 0;\r
-  Total     = 0;\r
-  Remaining = 0;\r
-\r
-  //\r
-  // If Handle is NULL, it means memory is ready for use.\r
-  // Use global variable to store handle value.\r
-  //\r
-  if (Handle == NULL) {\r
-    UsbDebugPortHandle = &mDebugCommunicationLibUsb3DebugPortHandle;\r
-  } else {\r
-    UsbDebugPortHandle = (USB3_DEBUG_PORT_HANDLE *)Handle;\r
-  }\r
-  \r
-  if (UsbDebugPortHandle->Initialized == USB3DBG_NO_DBG_CAB) {\r
+  UsbDebugPortHandle = GetUsb3DebugPortInstance ();\r
+  if (UsbDebugPortHandle == NULL) {\r
     return 0;\r
   }\r
-  \r
-  XhciMmioBase = ProgramXhciBaseAddress ();\r
-  UpdateXhcResource (UsbDebugPortHandle, XhciMmioBase);\r
-  \r
-  if (NeedReinitializeHardware(UsbDebugPortHandle)) {\r
-    Status = InitializeUsbDebugHardware (UsbDebugPortHandle);\r
-    if (RETURN_ERROR(Status)) {\r
-      return 0;\r
-    }\r
-  }\r
 \r
-  //\r
-  // First read data from buffer, then read debug port hw to get received data.\r
-  //\r
-  if (UsbDebugPortHandle->DataCount > 0) {\r
-    if (NumberOfBytes <= UsbDebugPortHandle->DataCount) {\r
-      Total = NumberOfBytes;\r
-    } else {\r
-      Total = UsbDebugPortHandle->DataCount;\r
-    }\r
-\r
-    for (Index = 0; Index < Total; Index++) {\r
-      Buffer[Index] = UsbDebugPortHandle->Data[Index];\r
-    }\r
-\r
-    for (Index = 0; Index < UsbDebugPortHandle->DataCount - Total; Index++) {\r
-      if (Total + Index >= 8) {\r
-        return 0;\r
-      }\r
-      UsbDebugPortHandle->Data[Index] = UsbDebugPortHandle->Data[Total + Index];\r
-    }\r
-    UsbDebugPortHandle->DataCount = (UINT8)(UsbDebugPortHandle->DataCount - (UINT8)Total);\r
+  if (UsbDebugPortHandle->Initialized != USB3DBG_ENABLED) {\r
+    return 0;\r
   }\r
 \r
-  //\r
-  // If Timeout is equal to 0, then it means it should always wait until all data required are received.\r
-  //\r
-  Begin         = 0;\r
-  TimeoutTicker = 0;  \r
-  TimerRound    = 0;\r
-  if (Timeout != 0) {\r
-    Begin = GetPerformanceCounter ();\r
-    TimeoutTicker = DivU64x32 (\r
-                      MultU64x64 (\r
-                        UsbDebugPortHandle->TimerFrequency,\r
-                        Timeout\r
-                        ),\r
-                      1000000u\r
-                      );\r
-    TimerRound = DivU64x64Remainder (\r
-                   TimeoutTicker,\r
-                   DivU64x32 (UsbDebugPortHandle->TimerCycle, 2),\r
-                   &TimeoutTicker\r
-                   );\r
+  if (UsbDebugPortHandle->InNotify) {\r
+    return 0;\r
   }\r
 \r
-  //\r
-  // Read remaining data by executing one or more usb debug transfer transactions at usb debug port hw.\r
-  //\r
-  while (Total < NumberOfBytes) {\r
-    if (Timeout != 0) {\r
-      if (TimerRound == 0) {\r
-        if (IsTimerTimeout (UsbDebugPortHandle, Begin, TimeoutTicker)) {\r
-          //\r
-          // If time out occurs.\r
-          //\r
-          return 0;\r
-        }\r
-      } else {\r
-        if (IsTimerTimeout (UsbDebugPortHandle, Begin, DivU64x32 (UsbDebugPortHandle->TimerCycle, 2))) {\r
-          TimerRound --;\r
-        }\r
-      }\r
-    }\r
-    Remaining = NumberOfBytes - Total;\r
-    if (Remaining >= USB3_DEBUG_PORT_MAX_PACKET_SIZE) {\r
-      Received = USB3_DEBUG_PORT_MAX_PACKET_SIZE;\r
-      Status = XhcDataTransfer (UsbDebugPortHandle, EfiUsbDataIn, Buffer + Total, &Received, DATA_TRANSFER_READ_TIMEOUT);\r
-    } else {\r
-      Received = USB3_DEBUG_PORT_MAX_PACKET_SIZE;\r
-      Status = XhcDataTransfer (UsbDebugPortHandle, EfiUsbDataIn, &UsbDebugPortHandle->Data[0], &Received, DATA_TRANSFER_READ_TIMEOUT);\r
-      UsbDebugPortHandle->DataCount = (UINT8) Received;\r
-\r
-      if (Remaining <= Received) {\r
-        //\r
-        // The data received are more than required\r
-        //\r
-        Length = (UINT8)Remaining;\r
-      } else {\r
-        //\r
-        // The data received are less than the remaining bytes\r
-        //\r
-        Length = (UINT8)Received;\r
-      }\r
+  Data = (UINT8 *)(UINTN)UsbDebugPortHandle->Data;\r
 \r
-      //\r
-      // Copy required data from the data buffer to user buffer.\r
-      //\r
-      for (Index = 0; Index < Length; Index++) {\r
-        (Buffer + Total)[Index] = UsbDebugPortHandle->Data[Index];\r
-        UsbDebugPortHandle->DataCount--;\r
-      }\r
+  //\r
+  // Read data from buffer\r
+  //\r
+  if (UsbDebugPortHandle->DataCount < 1) {\r
+    return 0;\r
+  } else {\r
+    *Buffer = Data[0];\r
 \r
-      //\r
-      // reorder the data buffer to make available data arranged from the beginning of the data buffer.\r
-      //\r
-      for (Index = 0; Index < Received - Length; Index++) {\r
-        if (Length + Index >= 8) {\r
-          return 0;\r
-        }\r
-        UsbDebugPortHandle->Data[Index] = UsbDebugPortHandle->Data[Length + Index];\r
+    for (Index = 0; Index < UsbDebugPortHandle->DataCount - 1; Index++) {\r
+      if ((Index + 1) >= XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE) {\r
+        return 0;\r
       }\r
-      //\r
-      // fixup the real required length of data.\r
-      //\r
-      Received = Length;\r
+      Data[Index] = Data[Index + 1];\r
     }\r
-    Total += Received;\r
+    UsbDebugPortHandle->DataCount = (UINT8)(UsbDebugPortHandle->DataCount - 1);\r
+    return 1;\r
   }\r
-  return Total;\r
 }\r
 \r
 /**\r
@@ -989,7 +892,7 @@ DebugPortReadBuffer (
 \r
   @retval 0                NumberOfBytes is 0.\r
   @retval >0               The number of bytes written to the debug device.\r
-                           If this value is less than NumberOfBytes, then the read operation failed.\r
+                           If this value is less than NumberOfBytes, then the write operation failed.\r
 \r
 **/\r
 UINTN\r
@@ -1001,11 +904,8 @@ DebugPortWriteBuffer (
   )\r
 {\r
   USB3_DEBUG_PORT_HANDLE    *UsbDebugPortHandle;\r
-  RETURN_STATUS             Status;\r
   UINTN                     Sent;\r
   UINTN                     Total;\r
-  EFI_PHYSICAL_ADDRESS      XhciMmioBase;\r
-  UINTN                     Index;\r
 \r
   if (NumberOfBytes == 0 || Buffer == NULL) {\r
     return 0;\r
@@ -1014,48 +914,32 @@ DebugPortWriteBuffer (
   Sent  = 0;\r
   Total = 0;\r
 \r
-  //\r
-  // If Handle is NULL, it means memory is ready for use.\r
-  // Use global variable to store handle value.\r
-  //\r
-  if (Handle == NULL) {\r
-    UsbDebugPortHandle = &mDebugCommunicationLibUsb3DebugPortHandle;\r
-  } else {\r
-    UsbDebugPortHandle = (USB3_DEBUG_PORT_HANDLE *)Handle;\r
-  }\r
-  \r
-  if (UsbDebugPortHandle->Initialized == USB3DBG_NO_DBG_CAB) {\r
+  UsbDebugPortHandle = GetUsb3DebugPortInstance ();\r
+  if (UsbDebugPortHandle == NULL) {\r
     return 0;\r
   }\r
 \r
-  //\r
-  // MMIO base address is possible to clear, set it if it is cleared. (XhciMemorySpaceClose in PchUsbCommon.c)\r
-  //\r
-  XhciMmioBase = ProgramXhciBaseAddress ();\r
-\r
-  UpdateXhcResource (UsbDebugPortHandle, XhciMmioBase);\r
+  if (UsbDebugPortHandle->Initialized != USB3DBG_ENABLED) {\r
+    return 0;\r
+  }\r
 \r
-  if (NeedReinitializeHardware(UsbDebugPortHandle)) {\r
-    Status = InitializeUsbDebugHardware (UsbDebugPortHandle);\r
-    if (RETURN_ERROR(Status)) {\r
-      return 0;\r
-    }\r
+  if (UsbDebugPortHandle->InNotify) {\r
+    return 0;\r
   }\r
 \r
   //\r
   // When host is trying to send data, write will be blocked.\r
   // Poll to see if there is any data sent by host at first.\r
   //\r
-  DebugPortPollBuffer (Handle);\r
+  DebugPortPollBuffer (UsbDebugPortHandle);\r
 \r
-  Index = 0;\r
   while ((Total < NumberOfBytes)) {\r
-    if (NumberOfBytes - Total > USB3_DEBUG_PORT_MAX_PACKET_SIZE) {\r
-      Sent = USB3_DEBUG_PORT_MAX_PACKET_SIZE;\r
+    if (NumberOfBytes - Total > USB3_DEBUG_PORT_WRITE_MAX_PACKET_SIZE) {\r
+      Sent = USB3_DEBUG_PORT_WRITE_MAX_PACKET_SIZE;\r
     } else {\r
       Sent = (UINT8)(NumberOfBytes - Total);\r
     }\r
-    Status = XhcDataTransfer (UsbDebugPortHandle, EfiUsbDataOut, Buffer + Total, &Sent, DATA_TRANSFER_WRITE_TIMEOUT);\r
+    XhcDataTransfer (UsbDebugPortHandle, EfiUsbDataOut, Buffer + Total, &Sent, DATA_TRANSFER_WRITE_TIMEOUT);\r
     Total += Sent;\r
   }\r
   \r
@@ -1072,7 +956,7 @@ DebugPortWriteBuffer (
   @param  Handle           Debug port handle.\r
 \r
   @retval TRUE             Data is waiting to be read from the debug device.\r
-  @retval FALSE            There is no data waiting to be read from the serial device.\r
+  @retval FALSE            There is no data waiting to be read from the debug device.\r
 \r
 **/\r
 BOOLEAN\r
@@ -1083,34 +967,20 @@ DebugPortPollBuffer (
 {\r
   USB3_DEBUG_PORT_HANDLE     *UsbDebugPortHandle;\r
   UINTN                     Length;\r
-  RETURN_STATUS             Status;\r
-  UINT8                     Buffer[XHC_DEBUG_PORT_DATA_LENGTH];\r
-  EFI_PHYSICAL_ADDRESS      XhciMmioBase;\r
 \r
-  //\r
-  // If Handle is NULL, it means memory is ready for use.\r
-  // Use global variable to store handle value.\r
-  //\r
-  if (Handle == NULL) {\r
-    UsbDebugPortHandle = &mDebugCommunicationLibUsb3DebugPortHandle;\r
-  } else {\r
-    UsbDebugPortHandle = (USB3_DEBUG_PORT_HANDLE *)Handle;\r
+  UsbDebugPortHandle = GetUsb3DebugPortInstance ();\r
+  if (UsbDebugPortHandle == NULL) {\r
+    return FALSE;\r
   }\r
 \r
-  if (UsbDebugPortHandle->Initialized == USB3DBG_NO_DBG_CAB) {\r
-    return 0;\r
+  if (UsbDebugPortHandle->Initialized != USB3DBG_ENABLED) {\r
+    return FALSE;\r
   }\r
 \r
-  XhciMmioBase = ProgramXhciBaseAddress ();\r
-  UpdateXhcResource (UsbDebugPortHandle, XhciMmioBase);\r
-  \r
-  if (NeedReinitializeHardware(UsbDebugPortHandle)) {\r
-    Status = InitializeUsbDebugHardware(UsbDebugPortHandle);\r
-    if (RETURN_ERROR(Status)) {\r
-      return FALSE;\r
-    }\r
+  if (UsbDebugPortHandle->InNotify) {\r
+    return FALSE;\r
   }\r
-  \r
+\r
   //\r
   // If the data buffer is not empty, then return TRUE directly.\r
   // Otherwise initialize a usb read transaction and read data to internal data buffer.\r
@@ -1120,12 +990,12 @@ DebugPortPollBuffer (
   }\r
 \r
   //\r
-  // Read most 8-bytes data\r
+  // Read data as much as we can\r
   //\r
-  Length = XHC_DEBUG_PORT_DATA_LENGTH;\r
-  XhcDataTransfer (Handle, EfiUsbDataIn, Buffer, &Length, DATA_TRANSFER_POLL_TIMEOUT);\r
+  Length = XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE;\r
+  XhcDataTransfer (UsbDebugPortHandle, EfiUsbDataIn, (VOID *)(UINTN)UsbDebugPortHandle->Data, &Length, DATA_TRANSFER_POLL_TIMEOUT);\r
 \r
-  if (Length > 8) {\r
+  if (Length > XHCI_DEBUG_DEVICE_MAX_PACKET_SIZE) {\r
     return FALSE;\r
   }\r
 \r
@@ -1136,7 +1006,6 @@ DebugPortPollBuffer (
   //\r
   // Store data into internal buffer for use later\r
   //\r
-  CopyMem (UsbDebugPortHandle->Data, Buffer, Length);\r
   UsbDebugPortHandle->DataCount = (UINT8) Length;\r
   return TRUE;\r
 }\r
@@ -1144,7 +1013,7 @@ DebugPortPollBuffer (
 /**\r
   Initialize the debug port.\r
 \r
-  If Function is not NULL, Debug Communication Libary will call this function\r
+  If Function is not NULL, Debug Communication Library will call this function\r
   by passing in the Context to be the first parameter. If needed, Debug Communication\r
   Library will create one debug port handle to be the second argument passing in\r
   calling the Function, otherwise it will pass NULL to be the second argument of\r
@@ -1152,7 +1021,7 @@ DebugPortPollBuffer (
 \r
   If Function is NULL, and Context is not NULL, the Debug Communication Library could\r
     a) Return the same handle as passed in (as Context parameter).\r
-    b) Ignore the input Context parameter and create new hanlde to be returned.\r
+    b) Ignore the input Context parameter and create new handle to be returned.\r
 \r
   If parameter Function is NULL and Context is NULL, Debug Communication Library could\r
   created a new handle if needed and return it, otherwise it will return NULL.\r
@@ -1172,65 +1041,16 @@ DebugPortInitialize (
   IN DEBUG_PORT_CONTINUE  Function\r
   )\r
 {\r
-  RETURN_STATUS             Status;\r
-  USB3_DEBUG_PORT_HANDLE    Handle;\r
   USB3_DEBUG_PORT_HANDLE    *UsbDebugPortHandle;\r
-  UINT64                    TimerStartValue;\r
-  UINT64                    TimerEndValue;\r
 \r
-  //\r
-  // Validate the PCD PcdDebugPortHandleBufferSize value \r
-  //\r
-  ASSERT (PcdGet16 (PcdDebugPortHandleBufferSize) == sizeof (USB3_DEBUG_PORT_HANDLE));\r
-\r
-  if (Function == NULL && Context != NULL) {\r
-    UsbDebugPortHandle = (USB3_DEBUG_PORT_HANDLE *)Context;\r
-  } else {\r
-    ZeroMem(&Handle, sizeof (USB3_DEBUG_PORT_HANDLE));\r
-    UsbDebugPortHandle = &Handle;\r
-  }\r
-\r
-  UsbDebugPortHandle->TimerFrequency = GetPerformanceCounterProperties (\r
-                                         &TimerStartValue,\r
-                                         &TimerEndValue\r
-                                         );\r
-\r
-  if (TimerEndValue < TimerStartValue) {\r
-    UsbDebugPortHandle->TimerCountDown = TRUE;\r
-    UsbDebugPortHandle->TimerCycle     = TimerStartValue - TimerEndValue;\r
-  } else {\r
-    UsbDebugPortHandle->TimerCountDown = FALSE;\r
-    UsbDebugPortHandle->TimerCycle     = TimerEndValue - TimerStartValue;\r
-  }   \r
-\r
-  if (Function == NULL && Context != NULL) {\r
-    return (DEBUG_PORT_HANDLE *) Context;\r
-  }\r
-  \r
-  //\r
-  // Read 64-bit MMIO base address\r
-  //\r
-  UsbDebugPortHandle->XhciMmioBase = ProgramXhciBaseAddress ();\r
-\r
-  Status = CalculateUsbDebugPortMmioBase (UsbDebugPortHandle);\r
-  if (RETURN_ERROR (Status)) {\r
-    goto Exit;\r
+  UsbDebugPortHandle = GetUsb3DebugPortInstance ();\r
+  if (UsbDebugPortHandle == NULL) {\r
+    return NULL;\r
   }\r
 \r
-  if (NeedReinitializeHardware(&Handle)) {\r
-    Status = InitializeUsbDebugHardware (&Handle);\r
-    if (RETURN_ERROR(Status)) {\r
-      goto Exit;\r
-    }\r
-  }\r
-\r
-Exit:\r
-\r
   if (Function != NULL) {\r
-    Function (Context, &Handle);\r
-  } else {\r
-    CopyMem(&mDebugCommunicationLibUsb3DebugPortHandle, &Handle, sizeof (USB3_DEBUG_PORT_HANDLE));\r
+    Function (Context, UsbDebugPortHandle);\r
   }\r
 \r
-  return (DEBUG_PORT_HANDLE)(UINTN)&mDebugCommunicationLibUsb3DebugPortHandle;\r
+  return (DEBUG_PORT_HANDLE)(UINTN)UsbDebugPortHandle;\r
 }\r