/** @file
The file ontaining the helper functions implement of the Ide Bus driver
Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "IdeBus.h"
/**
read a one-byte data from a IDE port.
@param PciIo The PCI IO protocol instance
@param Port the IDE Port number
@return the one-byte data read from IDE port
**/
)
{
Data = 0;
//
// perform 1-byte data read from register
//
1,
&Data
);
return Data;
}
/**
Reads multiple words of data from the IDE data port.
Call the IO abstraction once to do the complete read,
not one word at a time
@param PciIo Pointer to the EFI_PCI_IO instance
@param Port IO port to read
@param Count No. of UINT16's to read
@param Buffer Pointer to the data buffer for read
**/
)
{
//
// Prepare an 16-bit alligned working buffer. CpuIo will return failure and
// not perform actual I/O operations if buffer pointer passed in is not at
// natural boundary. The "Buffer" argument is passed in by user and may not
// at 16-bit natural boundary.
//
gBS->AllocatePool (
Size + 1,
(VOID**)&WorkingBuffer
);
//
// Perform UINT16 data read from FIFO
//
);
//
// Copy data to user buffer
//
}
/**
write a 1-byte data to a specific IDE port.
@param PciIo PCI IO protocol instance
@param Port The IDE port to be writen
@param Data The data to write to the port
**/
)
{
//
// perform 1-byte data write to register
//
1,
&Data
);
}
/**
write a 1-word data to a specific IDE port.
@param PciIo PCI IO protocol instance
@param Port The IDE port to be writen
@param Data The data to write to the port
**/
)
{
//
// perform 1-word data write to register
//
1,
&Data
);
}
/**
Write multiple words of data to the IDE data port.
Call the IO abstraction once to do the complete read,
not one word at a time
@param PciIo Pointer to the EFI_PCI_IO instance
@param Port IO port to read
@param Count No. of UINT16's to read
@param Buffer Pointer to the data buffer for read
**/
)
{
//
// Prepare an 16-bit alligned working buffer. CpuIo will return failure and
// not perform actual I/O operations if buffer pointer passed in is not at
// natural boundary. The "Buffer" argument is passed in by user and may not
// at 16-bit natural boundary.
//
gBS->AllocatePool (
Size + 1,
(VOID **) &WorkingBuffer
);
//
// Copy data from user buffer to working buffer
//
//
// perform UINT16 data write to the FIFO
//
(UINT16 *) AlignedBuffer
);
}
/**
Get IDE IO port registers' base addresses by mode. In 'Compatibility' mode,
use fixed addresses. In Native-PCI mode, get base addresses from BARs in
the PCI IDE controller's Configuration Space.
The steps to get IDE IO port registers' base addresses for each channel
as follows:
1. Examine the Programming Interface byte of the Class Code fields in PCI IDE
controller's Configuration Space to determine the operating mode.
2. a) In 'Compatibility' mode, use fixed addresses shown in the Table 1 below.
<pre>
___________________________________________
| | Command Block | Control Block |
| Channel | Registers | Registers |
|___________|_______________|_______________|
| Primary | 1F0h - 1F7h | 3F6h - 3F7h |
|___________|_______________|_______________|
| Secondary | 170h - 177h | 376h - 377h |
|___________|_______________|_______________|
Table 1. Compatibility resource mappings
</pre>
b) In Native-PCI mode, IDE registers are mapped into IO space using the BARs
in IDE controller's PCI Configuration Space, shown in the Table 2 below.
<pre>
___________________________________________________
| | Command Block | Control Block |
| Channel | Registers | Registers |
|___________|___________________|___________________|
| Primary | BAR at offset 0x10| BAR at offset 0x14|
|___________|___________________|___________________|
| Secondary | BAR at offset 0x18| BAR at offset 0x1C|
|___________|___________________|___________________|
Table 2. BARs for Register Mapping
</pre>
@note Refer to Intel ICH4 datasheet, Control Block Offset: 03F4h for
primary, 0374h for secondary. So 2 bytes extra offset should be
added to the base addresses read from BARs.
For more details, please refer to PCI IDE Controller Specification and Intel
ICH4 Datasheet.
@param PciIo Pointer to the EFI_PCI_IO_PROTOCOL instance
@param IdeRegsBaseAddr Pointer to IDE_REGISTERS_BASE_ADDR to
receive IDE IO port registers' base addresses
@retval EFI_UNSUPPORTED return this value when the BARs is not IO type
@retval EFI_SUCCESS Get the Base address successfully
@retval other read the pci configureation data error
**/
)
{
0,
sizeof (PciData),
);
return Status;
}
} else {
//
// The BARs should be of IO type
//
return EFI_UNSUPPORTED;
}
}
} else {
//
// The BARs should be of IO type
//
return EFI_UNSUPPORTED;
}
}
return EFI_SUCCESS;
}
/**
This function is used to requery IDE resources. The IDE controller will
probably switch between native and legacy modes during the EFI->CSM->OS
transfer. We do this everytime before an BlkIo operation to ensure its
succeess.
@param IdeDev The BLK_IO private data which specifies the IDE device
@retval EFI_INVALID_PARAMETER return this value when the channel is invalid
@retval EFI_SUCCESS reassign the IDE IO resource successfully
@retval other get the IDE current base address effor
**/
)
{
return EFI_INVALID_PARAMETER;
}
//
// Requery IDE IO port registers' base addresses in case of the switch of
// native and legacy modes
//
return Status;
}
return EFI_SUCCESS;
}
/**
This function is called by DiscoverIdeDevice(). It is used for detect
whether the IDE device exists in the specified Channel as the specified
Device Number.
There is two IDE channels: one is Primary Channel, the other is
Secondary Channel.(Channel is the logical name for the physical "Cable".)
Different channel has different register group.
On each IDE channel, at most two IDE devices attach,
one is called Device 0 (Master device), the other is called Device 1
(Slave device). The devices on the same channel co-use the same register
group, so before sending out a command for a specified device via command
register, it is a must to select the current device to accept the command
@param IdeDev pointer to IDE_BLK_IO_DEV data structure, used to record all the
information of the IDE device.
@retval EFI_SUCCESS successfully detects device.
@retval other any failure during detection process will return this value.
**/
)
{
//
// Select slave device
//
);
//
// Save the init slave status register
//
//
// Select Master back
//
);
//
// Send ATA Device Execut Diagnostic command.
// This command should work no matter DRDY is ready or not
//
DEBUG((EFI_D_ERROR, "New detecting method: Send Execute Diagnostic Command: WaitForBSYClear: Status: %d\n", Status));
return Status;
}
//
// Read device signature
//
//
// Select Master
//
);
);
);
);
);
if ((SectorCountReg == 0x1) &&
(LBALowReg == 0x1) &&
(LBAMidReg == 0x0) &&
(LBAHighReg == 0x0)) {
} else {
if ((LBAMidReg == 0x14) &&
(LBAHighReg == 0xeb)) {
}
}
//
// For some Hard Drive, it takes some time to get
// the right signature when operating in single slave mode.
// We stall 20ms to work around this.
//
if (!MasterDeviceExist) {
}
//
// Select Slave
//
);
);
);
);
);
);
if ((SectorCountReg == 0x1) &&
(LBALowReg == 0x1) &&
(LBAMidReg == 0x0) &&
(LBAHighReg == 0x0)) {
} else {
if ((LBAMidReg == 0x14) &&
(LBAHighReg == 0xeb)) {
}
}
//
// When single master is plugged, slave device
// will be wrongly detected. Here's the workaround
// for ATA devices by detecting DRY bit in status
// register.
// NOTE: This workaround doesn't apply to ATAPI.
//
if (MasterDeviceExist && SlaveDeviceExist &&
(StatusReg & ATA_STSREG_DRDY) == 0 &&
(InitStatusReg & ATA_STSREG_DRDY) == 0 &&
}
//
// Indicate this channel has been detected
//
return EFI_SUCCESS;
}
/**
Detect if there is disk attached to this port
@param IdeDev The BLK_IO private data which specifies the IDE device.
@retval EFI_NOT_FOUND The device or channel is not found
@retval EFI_SUCCESS The device is found
**/
)
{
//
// If a channel has not been checked, check it now. Then set it to "checked" state
// After this step, all devices in this channel have been checked.
//
if (!ChannelDeviceDetected) {
return EFI_NOT_FOUND;
}
}
//
// Device exists. test if it is an ATA device.
// Prefer the result from DetectIDEController,
// if failed, try another device type to handle
// devices that not follow the spec.
//
if (MasterDeviceType == ATA_DEVICE_TYPE) {
}
}
} else {
}
}
}
}
if (SlaveDeviceType == ATA_DEVICE_TYPE) {
}
}
} else {
}
}
}
}
return EFI_NOT_FOUND;
}
//
// Init Block I/O interface
//
if (!EFI_ERROR (LongPhyStatus)) {
} else {
}
//
// Init Disk Info interface
//
return EFI_SUCCESS;
}
/**
This interface is used to initialize all state data related to the detection of one
channel.
**/
)
{
MasterDeviceType = 0xff;
SlaveDeviceType = 0xff;
}
/**
This function is used to poll for the DRQ bit clear in the Status
Register. DRQ is cleared when the device is finished transferring data.
So this function is called after data transfer is finished.
@param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
to record all the information of the IDE device.
@param TimeoutInMilliSeconds used to designate the timeout for the DRQ clear.
@retval EFI_SUCCESS DRQ bit clear within the time out.
@retval EFI_TIMEOUT DRQ bit not clear within the time out.
@note
Read Status Register will clear interrupt status.
**/
DRQClear (
)
{
do {
//
// wait for BSY == 0 and DRQ == 0
//
break;
}
return EFI_ABORTED;
}
}
//
// Stall for 30 us
//
Delay--;
} while (Delay > 0);
if (Delay == 0) {
return EFI_TIMEOUT;
}
return EFI_SUCCESS;
}
/**
This function is used to poll for the DRQ bit clear in the Alternate
Status Register. DRQ is cleared when the device is finished
transferring data. So this function is called after data transfer
is finished.
@param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
to record all the information of the IDE device.
@param TimeoutInMilliSeconds used to designate the timeout for the DRQ clear.
@retval EFI_SUCCESS DRQ bit clear within the time out.
@retval EFI_TIMEOUT DRQ bit not clear within the time out.
@note Read Alternate Status Register will not clear interrupt status.
**/
)
{
do {
//
// wait for BSY == 0 and DRQ == 0
//
break;
}
return EFI_ABORTED;
}
}
//
// Stall for 30 us
//
Delay--;
} while (Delay > 0);
if (Delay == 0) {
return EFI_TIMEOUT;
}
return EFI_SUCCESS;
}
/**
This function is used to poll for the DRQ bit set in the
Status Register.
DRQ is set when the device is ready to transfer data. So this function
is called after the command is sent to the device and before required
data is transferred.
@param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure,used to
record all the information of the IDE device.
@param TimeoutInMilliSeconds used to designate the timeout for the DRQ ready.
@retval EFI_SUCCESS DRQ bit set within the time out.
@retval EFI_TIMEOUT DRQ bit not set within the time out.
@retval EFI_ABORTED DRQ bit not set caused by the command abort.
@note Read Status Register will clear interrupt status.
**/
DRQReady (
)
{
do {
//
// read Status Register will clear interrupt
//
//
// BSY==0,DRQ==1
//
break;
}
return EFI_ABORTED;
}
}
//
// Stall for 30 us
//
Delay--;
} while (Delay > 0);
if (Delay == 0) {
return EFI_TIMEOUT;
}
return EFI_SUCCESS;
}
/**
This function is used to poll for the DRQ bit set in the Alternate Status Register.
DRQ is set when the device is ready to transfer data. So this function is called after
the command is sent to the device and before required data is transferred.
@param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used to
record all the information of the IDE device.
@param TimeoutInMilliSeconds used to designate the timeout for the DRQ ready.
@retval EFI_SUCCESS DRQ bit set within the time out.
@retval EFI_TIMEOUT DRQ bit not set within the time out.
@retval EFI_ABORTED DRQ bit not set caused by the command abort.
@note Read Alternate Status Register will not clear interrupt status.
**/
)
{
do {
//
// Read Alternate Status Register will not clear interrupt status
//
//
// BSY == 0 , DRQ == 1
//
break;
}
return EFI_ABORTED;
}
}
//
// Stall for 30 us
//
Delay--;
} while (Delay > 0);
if (Delay == 0) {
return EFI_TIMEOUT;
}
return EFI_SUCCESS;
}
/**
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.
@param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
to record all the information of the IDE device.
@param TimeoutInMilliSeconds used to designate the timeout for the DRQ ready.
@retval EFI_SUCCESS BSY bit clear within the time out.
@retval EFI_TIMEOUT BSY bit not clear within the time out.
@note Read Status Register will clear interrupt status.
**/
)
{
do {
break;
}
//
// Stall for 30 us
//
Delay--;
} while (Delay > 0);
if (Delay == 0) {
return EFI_TIMEOUT;
}
return EFI_SUCCESS;
}
/**
This function is used to poll for the BSY bit clear in the Alternate Status Register.
BSY is clear when the device is not busy. Every command must be sent after device is
not busy.
@param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used to record
all the information of the IDE device.
@param TimeoutInMilliSeconds used to designate the timeout for the DRQ ready.
@retval EFI_SUCCESS BSY bit clear within the time out.
@retval EFI_TIMEOUT BSY bit not clear within the time out.
@note Read Alternate Status Register will not clear interrupt status.
**/
)
{
do {
break;
}
Delay--;
} while (Delay > 0);
if (Delay == 0) {
return EFI_TIMEOUT;
}
return EFI_SUCCESS;
}
/**
This function is used to poll for the DRDY bit set in the Status Register. DRDY
bit is set when the device is ready to accept command. Most ATA commands must be
sent after DRDY set except the ATAPI Packet Command.
@param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
to record all the information of the IDE device.
@param DelayInMilliSeconds used to designate the timeout for the DRQ ready.
@retval EFI_SUCCESS DRDY bit set within the time out.
@retval EFI_TIMEOUT DRDY bit not set within the time out.
@note Read Status Register will clear interrupt status.
**/
)
{
do {
//
// BSY == 0 , DRDY == 1
//
break;
}
return EFI_ABORTED;
}
}
Delay--;
} while (Delay > 0);
if (Delay == 0) {
return EFI_TIMEOUT;
}
return EFI_SUCCESS;
}
/**
This function is used to poll for the DRDY bit set in the Alternate Status Register.
DRDY bit is set when the device is ready to accept command. Most ATA commands must
be sent after DRDY set except the ATAPI Packet Command.
@param IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
to record all the information of the IDE device.
@param DelayInMilliSeconds used to designate the timeout for the DRQ ready.
@retval EFI_SUCCESS DRDY bit set within the time out.
@retval EFI_TIMEOUT DRDY bit not set within the time out.
@note Read Alternate Status Register will clear interrupt status.
**/
)
{
do {
//
// BSY == 0 , DRDY == 1
//
break;
}
return EFI_ABORTED;
}
}
Delay--;
} while (Delay > 0);
if (Delay == 0) {
return EFI_TIMEOUT;
}
return EFI_SUCCESS;
}
/**
Release resources of an IDE device before stopping it.
@param IdeBlkIoDevice Standard IDE device private data structure
**/
)
{
if (IdeBlkIoDevice == NULL) {
return ;
}
//
// Release all the resourses occupied by the IDE_BLK_IO_DEV
//
}
}
}
}
}
}
}
}
return ;
}
/**
Set the calculated Best transfer mode to a detected device.
@param IdeDev Standard IDE device private data structure
@param TransferMode The device transfer mode to be set
@return Set transfer mode Command execute status.
**/
)
{
DeviceSelect = 0;
//
// Send SET FEATURE command (sub command 0x03) to set pio mode.
//
0x03,
0,
0,
0
);
return Status;
}
/**
Set drive parameters for devices not support PACKETS command.
@param IdeDev Standard IDE device private data structure
@param DriveParameters The device parameters to be set into the disk
@return SetParameters Command execute status.
**/
)
{
DeviceSelect = 0;
//
// Send Init drive parameters
//
0,
0,
0,
0
);
//
// Send Set Multiple parameters
//
0,
0,
0,
0
);
return Status;
}
/**
Enable Interrupt on IDE controller.
@param IdeDev Standard IDE device private data structure
@retval EFI_SUCCESS Enable Interrupt successfully
**/
)
{
//
// Enable interrupt for DMA operation
//
DeviceControl = 0;
return EFI_SUCCESS;
}