/* Wait the operation register's bit as specified by Bit to become set (or clear). */
EFI_STATUS XhcWaitOpRegBit(IN USB_XHCI_INSTANCE *Xhc, IN UINT32 Offset, IN UINT32 Bit, IN BOOLEAN WaitToSet, IN UINT32 Timeout)
/* Wait the operation register's bit as specified by Bit to become set (or clear). */
EFI_STATUS XhcWaitOpRegBit(IN USB_XHCI_INSTANCE *Xhc, IN UINT32 Offset, IN UINT32 Bit, IN BOOLEAN WaitToSet, IN UINT32 Timeout)
{
  UINT64  TimeoutTicks;
  UINT64  ElapsedTicks;
  UINT64  TicksDelta;
  UINT64  CurrentTick;
  if (Timeout == 0) {
    return EFI_TIMEOUT;
  }
  TimeoutTicks = XhcConvertTimeToTicks (XHC_MICROSECOND_TO_NANOSECOND (Timeout * XHC_1_MILLISECOND));
  ElapsedTicks = 0;
  CurrentTick  = GetPerformanceCounter ();
  do {
    if (XHC_REG_BIT_IS_SET (Xhc, Offset, Bit) == WaitToSet) {
      return EFI_SUCCESS;
    }
    gBS->Stall (XHC_1_MICROSECOND);
    TicksDelta = XhcGetElapsedTicks (&CurrentTick);
    if (TicksDelta == 0) {
      TicksDelta = XhcConvertTimeToTicks (XHC_MICROSECOND_TO_NANOSECOND (XHC_1_MICROSECOND));
    }
    ElapsedTicks += TicksDelta;
  } while (ElapsedTicks < TimeoutTicks);
  return EFI_TIMEOUT;
}