📄 ide.c
字号:
/*++
Copyright (c) 2004 - 2006, Intel Corporation
All rights reserved. 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
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
Module Name:
ide.c
Abstract:
Revision History
--*/
#include "idebus.h"
BOOLEAN ChannelDeviceDetected = FALSE;
BOOLEAN SlaveDeviceExist = FALSE;
UINT8 SlaveDeviceType = INVALID_DEVICE_TYPE;
BOOLEAN MasterDeviceExist = FALSE;
UINT8 MasterDeviceType = INVALID_DEVICE_TYPE;
UINT8
IDEReadPortB (
IN EFI_PCI_IO_PROTOCOL *PciIo,
IN UINT16 Port
)
/*++
Routine Description:
TODO: Add function description
Arguments:
PciIo - TODO: add argument description
Port - TODO: add argument description
Returns:
TODO: add return values
--*/
{
UINT8 Data;
Data = 0;
//
// perform 1-byte data read from register
//
PciIo->Io.Read (
PciIo,
EfiPciIoWidthUint8,
EFI_PCI_IO_PASS_THROUGH_BAR,
(UINT64) Port,
1,
&Data
);
return Data;
}
VOID
IDEReadPortWMultiple (
IN EFI_PCI_IO_PROTOCOL *PciIo,
IN UINT16 Port,
IN UINTN Count,
IN VOID *Buffer
)
/*++
Routine Description:
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
Arguments:
PciIo - Pointer to the EFI_PCI_IO instance
Port - IO port to read
Count - No. of UINT16's to read
Buffer - Pointer to the data buffer for read
++*/
// TODO: function comment should end with '--*/'
// TODO: function comment is missing 'Returns:'
{
UINT16 *AlignedBuffer;
UINT16 *WorkingBuffer;
UINTN Size;
//
// 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.
//
Size = sizeof (UINT16) * Count;
gBS->AllocatePool (
EfiBootServicesData,
Size + 1,
(VOID**)&WorkingBuffer
);
AlignedBuffer = (UINT16 *) ((UINTN)(((UINTN) WorkingBuffer + 0x1) & (~0x1)));
//
// Perform UINT16 data read from FIFO
//
PciIo->Io.Read (
PciIo,
EfiPciIoWidthFifoUint16,
EFI_PCI_IO_PASS_THROUGH_BAR,
(UINT64) Port,
Count,
(UINT16*)AlignedBuffer
);
//
// Copy data to user buffer
//
EfiCopyMem (Buffer, (UINT16*)AlignedBuffer, Size);
gBS->FreePool (WorkingBuffer);
}
VOID
IDEWritePortB (
IN EFI_PCI_IO_PROTOCOL *PciIo,
IN UINT16 Port,
IN UINT8 Data
)
/*++
Routine Description:
TODO: Add function description
Arguments:
PciIo - TODO: add argument description
Port - TODO: add argument description
Data - TODO: add argument description
Returns:
TODO: add return values
--*/
{
//
// perform 1-byte data write to register
//
PciIo->Io.Write (
PciIo,
EfiPciIoWidthUint8,
EFI_PCI_IO_PASS_THROUGH_BAR,
(UINT64) Port,
1,
&Data
);
}
VOID
IDEWritePortW (
IN EFI_PCI_IO_PROTOCOL *PciIo,
IN UINT16 Port,
IN UINT16 Data
)
/*++
Routine Description:
TODO: Add function description
Arguments:
PciIo - TODO: add argument description
Port - TODO: add argument description
Data - TODO: add argument description
Returns:
TODO: add return values
--*/
{
//
// perform 1-word data write to register
//
PciIo->Io.Write (
PciIo,
EfiPciIoWidthUint16,
EFI_PCI_IO_PASS_THROUGH_BAR,
(UINT64) Port,
1,
&Data
);
}
VOID
IDEWritePortWMultiple (
IN EFI_PCI_IO_PROTOCOL *PciIo,
IN UINT16 Port,
IN UINTN Count,
IN VOID *Buffer
)
/*++
Routine Description:
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
Arguments:
PciIo - Pointer to the EFI_PCI_IO instance
Port - IO port to read
Count - No. of UINT16's to read
Buffer - Pointer to the data buffer for read
++*/
// TODO: function comment should end with '--*/'
// TODO: function comment is missing 'Returns:'
{
UINT16 *AlignedBuffer;
UINT32 *WorkingBuffer;
UINTN Size;
//
// 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.
//
Size = sizeof (UINT16) * Count;
gBS->AllocatePool (
EfiBootServicesData,
Size + 1,
(VOID **) &WorkingBuffer
);
AlignedBuffer = (UINT16 *) ((UINTN)(((UINTN) WorkingBuffer + 0x1) & (~0x1)));
//
// Copy data from user buffer to working buffer
//
EfiCopyMem ((UINT16 *) AlignedBuffer, Buffer, Size);
//
// perform UINT16 data write to the FIFO
//
PciIo->Io.Write (
PciIo,
EfiPciIoWidthFifoUint16,
EFI_PCI_IO_PASS_THROUGH_BAR,
(UINT64) Port,
Count,
(UINT16 *) AlignedBuffer
);
gBS->FreePool (WorkingBuffer);
}
BOOLEAN
BadIdeDeviceCheck (
IN IDE_BLK_IO_DEV *IdeDev
)
/*++
Routine Description:
TODO: Add function description
Arguments:
IdeDev - TODO: add argument description
Returns:
TODO: add return values
--*/
{
//
// check whether all registers return 0xff,
// if so, deem the channel is disabled.
//
#ifdef EFI_DEBUG
if (IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Data) != 0xff) {
return FALSE;
}
if (IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature) != 0xff) {
return FALSE;
}
if (IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount) != 0xff) {
return FALSE;
}
if (IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber) != 0xff) {
return FALSE;
}
if (IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb) != 0xff) {
return FALSE;
}
if (IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb) != 0xff) {
return FALSE;
}
if (IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Head) != 0xff) {
return FALSE;
}
if (IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command) != 0xff) {
return FALSE;
}
if (IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.AltStatus) != 0xff) {
return FALSE;
}
return TRUE;
#else
return FALSE;
#endif
}
//
// GetIdeRegistersBaseAddr
//
EFI_STATUS
GetIdeRegistersBaseAddr (
IN EFI_PCI_IO_PROTOCOL *PciIo,
OUT IDE_REGISTERS_BASE_ADDR *IdeRegsBaseAddr
)
/*++
Routine Description:
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.
___________________________________________
| | Command Block | Control Block |
| Channel | Registers | Registers |
|___________|_______________|_______________|
| Primary | 1F0h - 1F7h | 3F6h - 3F7h |
|___________|_______________|_______________|
| Secondary | 170h - 177h | 376h - 377h |
|___________|_______________|_______________|
Table 1. Compatibility resource mappings
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.
___________________________________________________
| | 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
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.
Arguments:
PciIo - Pointer to the EFI_PCI_IO_PROTOCOL instance
IdeRegsBaseAddr - Pointer to IDE_REGISTERS_BASE_ADDR to
receive IDE IO port registers' base addresses
Returns:
--*/
// TODO: EFI_UNSUPPORTED - add return value to function comment
// TODO: EFI_UNSUPPORTED - add return value to function comment
// TODO: EFI_SUCCESS - add return value to function comment
{
EFI_STATUS Status;
PCI_TYPE00 PciData;
Status = PciIo->Pci.Read (
PciIo,
EfiPciIoWidthUint8,
0,
sizeof (PciData),
&PciData
);
if (EFI_ERROR (Status)) {
return Status;
}
if ((PciData.Hdr.ClassCode[0] & IDE_PRIMARY_OPERATING_MODE) == 0) {
IdeRegsBaseAddr[IdePrimary].CommandBlockBaseAddr = 0x1f0;
IdeRegsBaseAddr[IdePrimary].ControlBlockBaseAddr = 0x3f6;
IdeRegsBaseAddr[IdePrimary].BusMasterBaseAddr =
(UINT16)((PciData.Device.Bar[4] & 0x0000fff0));
} else {
//
// The BARs should be of IO type
//
if ((PciData.Device.Bar[0] & bit0) == 0 ||
(PciData.Device.Bar[1] & bit0) == 0) {
return EFI_UNSUPPORTED;
}
IdeRegsBaseAddr[IdePrimary].CommandBlockBaseAddr =
(UINT16) (PciData.Device.Bar[0] & 0x0000fff8);
IdeRegsBaseAddr[IdePrimary].ControlBlockBaseAddr =
(UINT16) ((PciData.Device.Bar[1] & 0x0000fffc) + 2);
IdeRegsBaseAddr[IdePrimary].BusMasterBaseAddr =
(UINT16) ((PciData.Device.Bar[4] & 0x0000fff0));
}
if ((PciData.Hdr.ClassCode[0] & IDE_SECONDARY_OPERATING_MODE) == 0) {
IdeRegsBaseAddr[IdeSecondary].CommandBlockBaseAddr = 0x170;
IdeRegsBaseAddr[IdeSecondary].ControlBlockBaseAddr = 0x376;
IdeRegsBaseAddr[IdeSecondary].BusMasterBaseAddr =
(UINT16) ((PciData.Device.Bar[4] & 0x0000fff0));
} else {
//
// The BARs should be of IO type
//
if ((PciData.Device.Bar[2] & bit0) == 0 ||
(PciData.Device.Bar[3] & bit0) == 0) {
return EFI_UNSUPPORTED;
}
IdeRegsBaseAddr[IdeSecondary].CommandBlockBaseAddr =
(UINT16) (PciData.Device.Bar[2] & 0x0000fff8);
IdeRegsBaseAddr[IdeSecondary].ControlBlockBaseAddr =
(UINT16) ((PciData.Device.Bar[3] & 0x0000fffc) + 2);
IdeRegsBaseAddr[IdeSecondary].BusMasterBaseAddr =
(UINT16) ((PciData.Device.Bar[4] & 0x0000fff0));
}
return EFI_SUCCESS;
}
EFI_STATUS
ReassignIdeResources (
IN IDE_BLK_IO_DEV *IdeDev
)
/*++
Routine Description:
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.
Arguments:
IdeDev - The BLK_IO private data which specifies the IDE device
++*/
// TODO: function comment should end with '--*/'
// TODO: function comment is missing 'Returns:'
// TODO: EFI_SUCCESS - add return value to function comment
{
EFI_STATUS Status;
IDE_REGISTERS_BASE_ADDR IdeRegsBaseAddr[IdeMaxChannel];
UINT16 CommandBlockBaseAddr;
UINT16 ControlBlockBaseAddr;
//
// Requery IDE IO port registers' base addresses in case of the switch of
// native and legacy modes
//
Status = GetIdeRegistersBaseAddr (IdeDev->PciIo, IdeRegsBaseAddr);
if (EFI_ERROR (Status)) {
return Status;
}
EfiZeroMem (IdeDev->IoPort, sizeof (IDE_BASE_REGISTERS));
CommandBlockBaseAddr = IdeRegsBaseAddr[IdeDev->Channel].CommandBlockBaseAddr;
ControlBlockBaseAddr = IdeRegsBaseAddr[IdeDev->Channel].ControlBlockBaseAddr;
IdeDev->IoPort->Data = CommandBlockBaseAddr;
(*(UINT16 *) &IdeDev->IoPort->Reg1) = (UINT16) (CommandBlockBaseAddr + 0x01);
IdeDev->IoPort->SectorCount = (UINT16) (CommandBlockBaseAddr + 0x02);
IdeDev->IoPort->SectorNumber = (UINT16) (CommandBlockBaseAddr + 0x03);
IdeDev->IoPort->CylinderLsb = (UINT16) (CommandBlockBaseAddr + 0x04);
IdeDev->IoPort->CylinderMsb = (UINT16) (CommandBlockBaseAddr + 0x05);
IdeDev->IoPort->Head = (UINT16) (CommandBlockBaseAddr + 0x06);
(*(UINT16 *) &IdeDev->IoPort->Reg) = (UINT16) (CommandBlockBaseAddr + 0x07);
(*(UINT16 *) &IdeDev->IoPort->Alt) = ControlBlockBaseAddr;
IdeDev->IoPort->DriveAddress = (UINT16) (ControlBlockBaseAddr + 0x01);
IdeDev->IoPort->MasterSlave = (UINT16) ((IdeDev->Device == IdeMaster) ? 1 : 0);
IdeDev->IoPort->BusMasterBaseAddr = IdeRegsBaseAddr[IdeDev->Channel].BusMasterBaseAddr;
return EFI_SUCCESS;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -