/* This function is used to poll for the BSY bit clear in the Status Register. BSY is clear when the device is not busy. Every command must be sent after device is not busy. */
EFI_STATUS EFIAPI WaitForBSYClear(IN EFI_PCI_IO_PROTOCOL *PciIo, IN EFI_IDE_REGISTERS *IdeRegisters, IN UINT64 Timeout)
/* This function is used to poll for the BSY bit clear in the Status Register. BSY is clear when the device is not busy. Every command must be sent after device is not busy. */
EFI_STATUS EFIAPI WaitForBSYClear(IN EFI_PCI_IO_PROTOCOL *PciIo, IN EFI_IDE_REGISTERS *IdeRegisters, IN UINT64 Timeout)
{
  UINT64   Delay;
  UINT8    StatusRegister;
  BOOLEAN  InfiniteWait;
  ASSERT (PciIo != NULL);
  ASSERT (IdeRegisters != NULL);
  if (Timeout == 0) {
    InfiniteWait = TRUE;
  } else {
    InfiniteWait = FALSE;
  }
  Delay = DivU64x32 (Timeout, 1000) + 1;
  do {
    StatusRegister = IdeReadPortB (PciIo, IdeRegisters->CmdOrStatus);
    if ((StatusRegister & ATA_STSREG_BSY) == 0x00) {
      return EFI_SUCCESS;
    }
    MicroSecondDelay (100);
    Delay--;
  } while (InfiniteWait || (Delay > 0));
  return EFI_TIMEOUT;
}