2 Copyright (c) 2006, Intel Corporation
3 All rights reserved. This program and the accompanying materials
4 are licensed and made available under the terms and conditions of the BSD License
5 which accompanies this distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 BOOLEAN SlaveDeviceExist
= FALSE
;
16 BOOLEAN MasterDeviceExist
= FALSE
;
19 TODO: Add function description
21 @param PciIo TODO: add argument description
22 @param Port TODO: add argument description
24 TODO: add return values
29 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
37 // perform 1-byte data read from register
42 EFI_PCI_IO_PASS_THROUGH_BAR
,
51 Reads multiple words of data from the IDE data port.
52 Call the IO abstraction once to do the complete read,
53 not one word at a time
55 @param PciIo Pointer to the EFI_PCI_IO instance
56 @param Port IO port to read
57 @param Count No. of UINT16's to read
58 @param Buffer Pointer to the data buffer for read
62 IDEReadPortWMultiple (
63 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
69 UINT16
*AlignedBuffer
;
70 UINT16
*WorkingBuffer
;
74 // Prepare an 16-bit alligned working buffer. CpuIo will return failure and
75 // not perform actual I/O operations if buffer pointer passed in is not at
76 // natural boundary. The "Buffer" argument is passed in by user and may not
77 // at 16-bit natural boundary.
79 Size
= sizeof (UINT16
) * Count
;
84 (VOID
**)&WorkingBuffer
87 AlignedBuffer
= (UINT16
*) ((UINTN
)(((UINTN
) WorkingBuffer
+ 0x1) & (~0x1)));
90 // Perform UINT16 data read from FIFO
94 EfiPciIoWidthFifoUint16
,
95 EFI_PCI_IO_PASS_THROUGH_BAR
,
98 (UINT16
*)AlignedBuffer
102 // Copy data to user buffer
104 CopyMem (Buffer
, (UINT16
*)AlignedBuffer
, Size
);
105 gBS
->FreePool (WorkingBuffer
);
109 TODO: Add function description
111 @param PciIo TODO: add argument description
112 @param Port TODO: add argument description
113 @param Data TODO: add argument description
115 TODO: add return values
120 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
126 // perform 1-byte data write to register
131 EFI_PCI_IO_PASS_THROUGH_BAR
,
140 TODO: Add function description
142 @param PciIo TODO: add argument description
143 @param Port TODO: add argument description
144 @param Data TODO: add argument description
146 TODO: add return values
151 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
157 // perform 1-word data write to register
162 EFI_PCI_IO_PASS_THROUGH_BAR
,
170 Write multiple words of data to the IDE data port.
171 Call the IO abstraction once to do the complete read,
172 not one word at a time
174 @param PciIo Pointer to the EFI_PCI_IO instance
175 @param Port IO port to read
176 @param Count No. of UINT16's to read
177 @param Buffer Pointer to the data buffer for read
181 IDEWritePortWMultiple (
182 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
188 UINT16
*AlignedBuffer
;
189 UINT32
*WorkingBuffer
;
193 // Prepare an 16-bit alligned working buffer. CpuIo will return failure and
194 // not perform actual I/O operations if buffer pointer passed in is not at
195 // natural boundary. The "Buffer" argument is passed in by user and may not
196 // at 16-bit natural boundary.
198 Size
= sizeof (UINT16
) * Count
;
203 (VOID
**) &WorkingBuffer
206 AlignedBuffer
= (UINT16
*) ((UINTN
)(((UINTN
) WorkingBuffer
+ 0x1) & (~0x1)));
209 // Copy data from user buffer to working buffer
211 CopyMem ((UINT16
*) AlignedBuffer
, Buffer
, Size
);
214 // perform UINT16 data write to the FIFO
218 EfiPciIoWidthFifoUint16
,
219 EFI_PCI_IO_PASS_THROUGH_BAR
,
222 (UINT16
*) AlignedBuffer
225 gBS
->FreePool (WorkingBuffer
);
229 TODO: Add function description
231 @param IdeDev TODO: add argument description
233 TODO: add return values
238 IN IDE_BLK_IO_DEV
*IdeDev
242 // check whether all registers return 0xff,
243 // if so, deem the channel is disabled.
247 if (IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Data
) != 0xff) {
251 if (IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
) != 0xff) {
255 if (IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorCount
) != 0xff) {
259 if (IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorNumber
) != 0xff) {
263 if (IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
) != 0xff) {
267 if (IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
) != 0xff) {
271 if (IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
) != 0xff) {
275 if (IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
) != 0xff) {
279 if (IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.AltStatus
) != 0xff) {
293 // GetIdeRegistersBaseAddr
296 Get IDE IO port registers' base addresses by mode. In 'Compatibility' mode,
297 use fixed addresses. In Native-PCI mode, get base addresses from BARs in
298 the PCI IDE controller's Configuration Space.
300 The steps to get IDE IO port registers' base addresses for each channel
303 1. Examine the Programming Interface byte of the Class Code fields in PCI IDE
304 controller's Configuration Space to determine the operating mode.
306 2. a) In 'Compatibility' mode, use fixed addresses shown in the Table 1 below.
308 ___________________________________________
309 | | Command Block | Control Block |
310 | Channel | Registers | Registers |
311 |___________|_______________|_______________|
312 | Primary | 1F0h - 1F7h | 3F6h - 3F7h |
313 |___________|_______________|_______________|
314 | Secondary | 170h - 177h | 376h - 377h |
315 |___________|_______________|_______________|
317 Table 1. Compatibility resource mappings
320 b) In Native-PCI mode, IDE registers are mapped into IO space using the BARs
321 in IDE controller's PCI Configuration Space, shown in the Table 2 below.
323 ___________________________________________________
324 | | Command Block | Control Block |
325 | Channel | Registers | Registers |
326 |___________|___________________|___________________|
327 | Primary | BAR at offset 0x10| BAR at offset 0x14|
328 |___________|___________________|___________________|
329 | Secondary | BAR at offset 0x18| BAR at offset 0x1C|
330 |___________|___________________|___________________|
332 Table 2. BARs for Register Mapping
334 @note Refer to Intel ICH4 datasheet, Control Block Offset: 03F4h for
335 primary, 0374h for secondary. So 2 bytes extra offset should be
336 added to the base addresses read from BARs.
338 For more details, please refer to PCI IDE Controller Specification and Intel
341 @param PciIo Pointer to the EFI_PCI_IO_PROTOCOL instance
342 @param IdeRegsBaseAddr Pointer to IDE_REGISTERS_BASE_ADDR to
343 receive IDE IO port registers' base addresses
347 GetIdeRegistersBaseAddr (
348 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
349 OUT IDE_REGISTERS_BASE_ADDR
*IdeRegsBaseAddr
351 // TODO: EFI_UNSUPPORTED - add return value to function comment
352 // TODO: EFI_UNSUPPORTED - add return value to function comment
353 // TODO: EFI_SUCCESS - add return value to function comment
358 Status
= PciIo
->Pci
.Read (
366 if (EFI_ERROR (Status
)) {
370 if ((PciData
.Hdr
.ClassCode
[0] & IDE_PRIMARY_OPERATING_MODE
) == 0) {
371 IdeRegsBaseAddr
[IdePrimary
].CommandBlockBaseAddr
= 0x1f0;
372 IdeRegsBaseAddr
[IdePrimary
].ControlBlockBaseAddr
= 0x3f6;
373 IdeRegsBaseAddr
[IdePrimary
].BusMasterBaseAddr
=
374 (UINT16
)((PciData
.Device
.Bar
[4] & 0x0000fff0));
377 // The BARs should be of IO type
379 if ((PciData
.Device
.Bar
[0] & bit0
) == 0 ||
380 (PciData
.Device
.Bar
[1] & bit0
) == 0) {
381 return EFI_UNSUPPORTED
;
384 IdeRegsBaseAddr
[IdePrimary
].CommandBlockBaseAddr
=
385 (UINT16
) (PciData
.Device
.Bar
[0] & 0x0000fff8);
386 IdeRegsBaseAddr
[IdePrimary
].ControlBlockBaseAddr
=
387 (UINT16
) ((PciData
.Device
.Bar
[1] & 0x0000fffc) + 2);
388 IdeRegsBaseAddr
[IdePrimary
].BusMasterBaseAddr
=
389 (UINT16
) ((PciData
.Device
.Bar
[4] & 0x0000fff0));
392 if ((PciData
.Hdr
.ClassCode
[0] & IDE_SECONDARY_OPERATING_MODE
) == 0) {
393 IdeRegsBaseAddr
[IdeSecondary
].CommandBlockBaseAddr
= 0x170;
394 IdeRegsBaseAddr
[IdeSecondary
].ControlBlockBaseAddr
= 0x376;
395 IdeRegsBaseAddr
[IdeSecondary
].BusMasterBaseAddr
=
396 (UINT16
) ((PciData
.Device
.Bar
[4] & 0x0000fff0));
399 // The BARs should be of IO type
401 if ((PciData
.Device
.Bar
[2] & bit0
) == 0 ||
402 (PciData
.Device
.Bar
[3] & bit0
) == 0) {
403 return EFI_UNSUPPORTED
;
406 IdeRegsBaseAddr
[IdeSecondary
].CommandBlockBaseAddr
=
407 (UINT16
) (PciData
.Device
.Bar
[2] & 0x0000fff8);
408 IdeRegsBaseAddr
[IdeSecondary
].ControlBlockBaseAddr
=
409 (UINT16
) ((PciData
.Device
.Bar
[3] & 0x0000fffc) + 2);
410 IdeRegsBaseAddr
[IdeSecondary
].BusMasterBaseAddr
=
411 (UINT16
) ((PciData
.Device
.Bar
[4] & 0x0000fff0));
418 This function is used to requery IDE resources. The IDE controller will
419 probably switch between native and legacy modes during the EFI->CSM->OS
420 transfer. We do this everytime before an BlkIo operation to ensure its
423 @param IdeDev The BLK_IO private data which specifies the IDE device
427 ReassignIdeResources (
428 IN IDE_BLK_IO_DEV
*IdeDev
430 // TODO: EFI_SUCCESS - add return value to function comment
433 IDE_REGISTERS_BASE_ADDR IdeRegsBaseAddr
[IdeMaxChannel
];
434 UINT16 CommandBlockBaseAddr
;
435 UINT16 ControlBlockBaseAddr
;
438 // Requery IDE IO port registers' base addresses in case of the switch of
439 // native and legacy modes
441 Status
= GetIdeRegistersBaseAddr (IdeDev
->PciIo
, IdeRegsBaseAddr
);
442 if (EFI_ERROR (Status
)) {
446 ZeroMem (IdeDev
->IoPort
, sizeof (IDE_BASE_REGISTERS
));
447 CommandBlockBaseAddr
= IdeRegsBaseAddr
[IdeDev
->Channel
].CommandBlockBaseAddr
;
448 ControlBlockBaseAddr
= IdeRegsBaseAddr
[IdeDev
->Channel
].ControlBlockBaseAddr
;
450 IdeDev
->IoPort
->Data
= CommandBlockBaseAddr
;
451 (*(UINT16
*) &IdeDev
->IoPort
->Reg1
) = (UINT16
) (CommandBlockBaseAddr
+ 0x01);
452 IdeDev
->IoPort
->SectorCount
= (UINT16
) (CommandBlockBaseAddr
+ 0x02);
453 IdeDev
->IoPort
->SectorNumber
= (UINT16
) (CommandBlockBaseAddr
+ 0x03);
454 IdeDev
->IoPort
->CylinderLsb
= (UINT16
) (CommandBlockBaseAddr
+ 0x04);
455 IdeDev
->IoPort
->CylinderMsb
= (UINT16
) (CommandBlockBaseAddr
+ 0x05);
456 IdeDev
->IoPort
->Head
= (UINT16
) (CommandBlockBaseAddr
+ 0x06);
458 (*(UINT16
*) &IdeDev
->IoPort
->Reg
) = (UINT16
) (CommandBlockBaseAddr
+ 0x07);
459 (*(UINT16
*) &IdeDev
->IoPort
->Alt
) = ControlBlockBaseAddr
;
460 IdeDev
->IoPort
->DriveAddress
= (UINT16
) (ControlBlockBaseAddr
+ 0x01);
461 IdeDev
->IoPort
->MasterSlave
= (UINT16
) ((IdeDev
->Device
== IdeMaster
) ? 1 : 0);
463 IdeDev
->IoPort
->BusMasterBaseAddr
= IdeRegsBaseAddr
[IdeDev
->Channel
].BusMasterBaseAddr
;
468 Read SATA registers to detect SATA disks
470 @param IdeDev The BLK_IO private data which specifies the IDE device
475 IDE_BLK_IO_DEV
*IdeDev
477 // TODO: EFI_NOT_FOUND - add return value to function comment
478 // TODO: EFI_SUCCESS - add return value to function comment
479 // TODO: EFI_NOT_FOUND - add return value to function comment
486 IdeDev
->IoPort
->Head
,
487 (UINT8
) ((IdeDev
->Device
<< 4) | 0xe0)
491 // Wait 31 seconds for BSY clear. BSY should be in clear state if there exists
492 // a device (initial state). Normally, BSY is also in clear state if there is
495 Status
= WaitForBSYClear (IdeDev
, 31000);
496 if (EFI_ERROR (Status
)) {
497 return EFI_NOT_FOUND
;
501 // select device, read error register
505 IdeDev
->IoPort
->Head
,
506 (UINT8
) ((IdeDev
->Device
<< 4) | 0xe0)
508 Status
= DRDYReady (IdeDev
, 200);
510 ErrorRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
511 if ((ErrorRegister
== 0x01) || (ErrorRegister
== 0x81)) {
514 return EFI_NOT_FOUND
;
522 Detect if there is disk connected to this port
524 @param IdeDev The BLK_IO private data which specifies the IDE device
529 IN IDE_BLK_IO_DEV
*IdeDev
531 // TODO: EFI_NOT_FOUND - add return value to function comment
532 // TODO: EFI_NOT_FOUND - add return value to function comment
533 // TODO: EFI_SUCCESS - add return value to function comment
540 // This extra detection is for SATA disks
542 Status
= CheckPowerMode (IdeDev
);
543 if (Status
== EFI_SUCCESS
) {
548 // If a channel has not been checked, check it now. Then set it to "checked" state
549 // After this step, all devices in this channel have been checked.
551 Status
= DetectIDEController (IdeDev
);
553 if ((EFI_ERROR (Status
)) && !SataFlag
) {
554 return EFI_NOT_FOUND
;
558 // Device exists. test if it is an ATA device
560 Status
= ATAIdentify (IdeDev
);
561 if (EFI_ERROR (Status
)) {
563 // if not ATA device, test if it is an ATAPI device
565 Status
= ATAPIIdentify (IdeDev
);
566 if (EFI_ERROR (Status
)) {
568 // if not ATAPI device either, return error.
570 return EFI_NOT_FOUND
;
575 // Init Block I/O interface
577 IdeDev
->BlkIo
.Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION
;
578 IdeDev
->BlkIo
.Reset
= IDEBlkIoReset
;
579 IdeDev
->BlkIo
.ReadBlocks
= IDEBlkIoReadBlocks
;
580 IdeDev
->BlkIo
.WriteBlocks
= IDEBlkIoWriteBlocks
;
581 IdeDev
->BlkIo
.FlushBlocks
= IDEBlkIoFlushBlocks
;
583 IdeDev
->BlkMedia
.LogicalPartition
= FALSE
;
584 IdeDev
->BlkMedia
.WriteCaching
= FALSE
;
587 // Init Disk Info interface
589 gBS
->CopyMem (&IdeDev
->DiskInfo
.Interface
, &gEfiDiskInfoIdeInterfaceGuid
, sizeof (EFI_GUID
));
590 IdeDev
->DiskInfo
.Inquiry
= IDEDiskInfoInquiry
;
591 IdeDev
->DiskInfo
.Identify
= IDEDiskInfoIdentify
;
592 IdeDev
->DiskInfo
.SenseData
= IDEDiskInfoSenseData
;
593 IdeDev
->DiskInfo
.WhichIde
= IDEDiskInfoWhichIde
;
599 This function is called by DiscoverIdeDevice(). It is used for detect
600 whether the IDE device exists in the specified Channel as the specified
603 There is two IDE channels: one is Primary Channel, the other is
604 Secondary Channel.(Channel is the logical name for the physical "Cable".)
605 Different channel has different register group.
607 On each IDE channel, at most two IDE devices attach,
608 one is called Device 0 (Master device), the other is called Device 1
609 (Slave device). The devices on the same channel co-use the same register
610 group, so before sending out a command for a specified device via command
611 register, it is a must to select the current device to accept the command
612 by set the device number in the Head/Device Register.
615 pointer pointing to IDE_BLK_IO_DEV data structure, used
616 to record all the information of the IDE device.
619 successfully detects device.
622 any failure during detection process will return this
626 TODO: EFI_SUCCESS - add return value to function comment
627 TODO: EFI_NOT_FOUND - add return value to function comment
631 DetectIDEController (
632 IN IDE_BLK_IO_DEV
*IdeDev
639 EFI_STATUS DeviceStatus
;
642 // Slave device has been detected with master device.
644 if ((IdeDev
->Device
) == 1) {
645 if (SlaveDeviceExist
) {
647 // If master not exists but slave exists, slave have to wait a while
649 if (!MasterDeviceExist
) {
651 // if single slave can't be detected, add delay 4s here.
653 gBS
->Stall (4000000);
658 return EFI_NOT_FOUND
;
663 // Select slave device
667 IdeDev
->IoPort
->Head
,
668 (UINT8
) ((1 << 4) | 0xe0)
673 // Save the init slave status register
675 InitStatusReg
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
678 // Select master back
682 IdeDev
->IoPort
->Head
,
683 (UINT8
) ((0 << 4) | 0xe0)
687 // Send ATA Device Execut Diagnostic command.
688 // This command should work no matter DRDY is ready or not
690 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, 0x90);
692 Status
= WaitForBSYClear (IdeDev
, 3500);
694 ErrorReg
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
697 // Master Error register is 0x01. D0 passed, D1 passed or not present.
698 // Master Error register is 0x81. D0 passed, D1 failed. Return.
699 // Master Error register is other value. D0 failed, D1 passed or not present..
701 if (ErrorReg
== 0x01) {
702 MasterDeviceExist
= TRUE
;
703 DeviceStatus
= EFI_SUCCESS
;
704 } else if (ErrorReg
== 0x81) {
706 MasterDeviceExist
= TRUE
;
707 DeviceStatus
= EFI_SUCCESS
;
708 SlaveDeviceExist
= FALSE
;
712 MasterDeviceExist
= FALSE
;
713 DeviceStatus
= EFI_NOT_FOUND
;
717 // Master Error register is not 0x81, Go on check Slave
725 IdeDev
->IoPort
->Head
,
726 (UINT8
) ((1 << 4) | 0xe0)
730 ErrorReg
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
733 // Slave Error register is not 0x01, D1 failed. Return.
735 if (ErrorReg
!= 0x01) {
736 SlaveDeviceExist
= FALSE
;
740 StatusReg
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
743 // Most ATAPI devices don't set DRDY bit, so test with a slow but accurate
744 // "ATAPI TEST UNIT READY" command
746 if (((StatusReg
& DRDY
) == 0) && ((InitStatusReg
& DRDY
) == 0)) {
747 Status
= AtapiTestUnitReady (IdeDev
);
750 // Still fail, Slave doesn't exist.
752 if (EFI_ERROR (Status
)) {
753 SlaveDeviceExist
= FALSE
;
759 // Error reg is 0x01 and DRDY is ready,
760 // or ATAPI test unit ready success,
761 // or init Slave status DRDY is ready
764 SlaveDeviceExist
= TRUE
;
771 This function is used to poll for the DRQ bit clear in the Status
772 Register. DRQ is cleared when the device is finished transferring data.
773 So this function is called after data transfer is finished.
776 pointer pointing to IDE_BLK_IO_DEV data structure, used
777 to record all the information of the IDE device.
779 @param[in] TimeoutInMilliSeconds
780 used to designate the timeout for the DRQ clear.
783 DRQ bit clear within the time out.
786 DRQ bit not clear within the time out.
789 Read Status Register will clear interrupt status.
794 IN IDE_BLK_IO_DEV
*IdeDev
,
795 IN UINTN TimeoutInMilliSeconds
797 // TODO: function comment is missing 'Routine Description:'
798 // TODO: function comment is missing 'Arguments:'
799 // TODO: IdeDev - add argument and description to function comment
800 // TODO: TimeoutInMilliSeconds - add argument and description to function comment
801 // TODO: EFI_ABORTED - add return value to function comment
804 UINT8 StatusRegister
;
807 Delay
= (UINT32
) (((TimeoutInMilliSeconds
* STALL_1_MILLI_SECOND
) / 30) + 1);
810 StatusRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
813 // wait for BSY == 0 and DRQ == 0
815 if ((StatusRegister
& (DRQ
| BSY
)) == 0) {
819 if ((StatusRegister
& (BSY
| ERR
)) == ERR
) {
821 ErrorRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
822 if ((ErrorRegister
& ABRT_ERR
) == ABRT_ERR
) {
844 This function is used to poll for the DRQ bit clear in the Alternate
845 Status Register. DRQ is cleared when the device is finished
846 transferring data. So this function is called after data transfer
850 pointer pointing to IDE_BLK_IO_DEV data structure, used
851 to record all the information of the IDE device.
853 @param[in] TimeoutInMilliSeconds
854 used to designate the timeout for the DRQ clear.
857 DRQ bit clear within the time out.
860 DRQ bit not clear within the time out.
863 Read Alternate Status Register will not clear interrupt status.
868 IN IDE_BLK_IO_DEV
*IdeDev
,
869 IN UINTN TimeoutInMilliSeconds
871 // TODO: function comment is missing 'Routine Description:'
872 // TODO: function comment is missing 'Arguments:'
873 // TODO: IdeDev - add argument and description to function comment
874 // TODO: TimeoutInMilliSeconds - add argument and description to function comment
875 // TODO: EFI_ABORTED - add return value to function comment
881 Delay
= (UINT32
) (((TimeoutInMilliSeconds
* STALL_1_MILLI_SECOND
) / 30) + 1);
884 AltRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.AltStatus
);
887 // wait for BSY == 0 and DRQ == 0
889 if ((AltRegister
& (DRQ
| BSY
)) == 0) {
893 if ((AltRegister
& (BSY
| ERR
)) == ERR
) {
895 ErrorRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
896 if ((ErrorRegister
& ABRT_ERR
) == ABRT_ERR
) {
918 This function is used to poll for the DRQ bit set in the
920 DRQ is set when the device is ready to transfer data. So this function
921 is called after the command is sent to the device and before required
924 @param[in] IDE_BLK_IO_DEV IN *IdeDev
925 pointer pointing to IDE_BLK_IO_DEV data structure,used
926 to record all the information of the IDE device.
928 @param[in] UINTN IN TimeoutInMilliSeconds
929 used to designate the timeout for the DRQ ready.
932 DRQ bit set within the time out.
935 DRQ bit not set within the time out.
938 DRQ bit not set caused by the command abort.
941 Read Status Register will clear interrupt status.
946 IN IDE_BLK_IO_DEV
*IdeDev
,
947 IN UINTN TimeoutInMilliSeconds
949 // TODO: function comment is missing 'Routine Description:'
950 // TODO: function comment is missing 'Arguments:'
951 // TODO: IdeDev - add argument and description to function comment
952 // TODO: TimeoutInMilliSeconds - add argument and description to function comment
955 UINT8 StatusRegister
;
958 Delay
= (UINT32
) (((TimeoutInMilliSeconds
* STALL_1_MILLI_SECOND
) / 30) + 1);
961 // read Status Register will clear interrupt
963 StatusRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
968 if ((StatusRegister
& (BSY
| DRQ
)) == DRQ
) {
972 if ((StatusRegister
& (BSY
| ERR
)) == ERR
) {
974 ErrorRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
975 if ((ErrorRegister
& ABRT_ERR
) == ABRT_ERR
) {
996 This function is used to poll for the DRQ bit set in the
997 Alternate Status Register. DRQ is set when the device is ready to
998 transfer data. So this function is called after the command
999 is sent to the device and before required data is transferred.
1001 @param[in] IDE_BLK_IO_DEV IN *IdeDev
1002 pointer pointing to IDE_BLK_IO_DEV data structure, used
1003 to record all the information of the IDE device.
1005 @param[in] UINTN IN TimeoutInMilliSeconds
1006 used to designate the timeout for the DRQ ready.
1009 DRQ bit set within the time out.
1012 DRQ bit not set within the time out.
1015 DRQ bit not set caused by the command abort.
1018 Read Alternate Status Register will not clear interrupt status.
1023 IN IDE_BLK_IO_DEV
*IdeDev
,
1024 IN UINTN TimeoutInMilliSeconds
1026 // TODO: function comment is missing 'Routine Description:'
1027 // TODO: function comment is missing 'Arguments:'
1028 // TODO: IdeDev - add argument and description to function comment
1029 // TODO: TimeoutInMilliSeconds - add argument and description to function comment
1033 UINT8 ErrorRegister
;
1035 Delay
= (UINT32
) (((TimeoutInMilliSeconds
* STALL_1_MILLI_SECOND
) / 30) + 1);
1039 // Read Alternate Status Register will not clear interrupt status
1041 AltRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.AltStatus
);
1043 // BSY == 0 , DRQ == 1
1045 if ((AltRegister
& (BSY
| DRQ
)) == DRQ
) {
1049 if ((AltRegister
& (BSY
| ERR
)) == ERR
) {
1051 ErrorRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
1052 if ((ErrorRegister
& ABRT_ERR
) == ABRT_ERR
) {
1073 This function is used to poll for the BSY bit clear in the
1074 Status Register. BSY is clear when the device is not busy.
1075 Every command must be sent after device is not busy.
1077 @param[in] IDE_BLK_IO_DEV IN *IdeDev
1078 pointer pointing to IDE_BLK_IO_DEV data structure, used
1079 to record all the information of the IDE device.
1081 @param[in] UINTN IN TimeoutInMilliSeconds
1082 used to designate the timeout for the DRQ ready.
1085 BSY bit clear within the time out.
1088 BSY bit not clear within the time out.
1091 Read Status Register will clear interrupt status.
1096 IN IDE_BLK_IO_DEV
*IdeDev
,
1097 IN UINTN TimeoutInMilliSeconds
1099 // TODO: function comment is missing 'Routine Description:'
1100 // TODO: function comment is missing 'Arguments:'
1101 // TODO: IdeDev - add argument and description to function comment
1102 // TODO: TimeoutInMilliSeconds - add argument and description to function comment
1105 UINT8 StatusRegister
;
1107 Delay
= (UINT32
) (((TimeoutInMilliSeconds
* STALL_1_MILLI_SECOND
) / 30) + 1);
1110 StatusRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
1111 if ((StatusRegister
& BSY
) == 0x00) {
1134 This function is used to poll for the BSY bit clear in the
1135 Alternate Status Register. BSY is clear when the device is not busy.
1136 Every command must be sent after device is not busy.
1138 @param[in] IDE_BLK_IO_DEV IN *IdeDev
1139 pointer pointing to IDE_BLK_IO_DEV data structure, used
1140 to record all the information of the IDE device.
1142 @param[in] UINTN IN TimeoutInMilliSeconds
1143 used to designate the timeout for the DRQ ready.
1146 BSY bit clear within the time out.
1149 BSY bit not clear within the time out.
1152 Read Alternate Status Register will not clear interrupt status.
1157 IN IDE_BLK_IO_DEV
*IdeDev
,
1158 IN UINTN TimeoutInMilliSeconds
1160 // TODO: function comment is missing 'Routine Description:'
1161 // TODO: function comment is missing 'Arguments:'
1162 // TODO: IdeDev - add argument and description to function comment
1163 // TODO: TimeoutInMilliSeconds - add argument and description to function comment
1168 Delay
= (UINT32
) (((TimeoutInMilliSeconds
* STALL_1_MILLI_SECOND
) / 30) + 1);
1170 AltRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.AltStatus
);
1171 if ((AltRegister
& BSY
) == 0x00) {
1192 This function is used to poll for the DRDY bit set in the
1193 Status Register. DRDY bit is set when the device is ready
1194 to accept command. Most ATA commands must be sent after
1195 DRDY set except the ATAPI Packet Command.
1197 @param[in] IDE_BLK_IO_DEV IN *IdeDev
1198 pointer pointing to IDE_BLK_IO_DEV data structure, used
1199 to record all the information of the IDE device.
1201 @param[in] UINTN IN TimeoutInMilliSeconds
1202 used to designate the timeout for the DRQ ready.
1205 DRDY bit set within the time out.
1208 DRDY bit not set within the time out.
1211 Read Status Register will clear interrupt status.
1216 IN IDE_BLK_IO_DEV
*IdeDev
,
1217 IN UINTN DelayInMilliSeconds
1219 // TODO: function comment is missing 'Routine Description:'
1220 // TODO: function comment is missing 'Arguments:'
1221 // TODO: IdeDev - add argument and description to function comment
1222 // TODO: DelayInMilliSeconds - add argument and description to function comment
1223 // TODO: EFI_ABORTED - add return value to function comment
1226 UINT8 StatusRegister
;
1227 UINT8 ErrorRegister
;
1229 Delay
= (UINT32
) (((DelayInMilliSeconds
* STALL_1_MILLI_SECOND
) / 30) + 1);
1231 StatusRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
1233 // BSY == 0 , DRDY == 1
1235 if ((StatusRegister
& (DRDY
| BSY
)) == DRDY
) {
1239 if ((StatusRegister
& (BSY
| ERR
)) == ERR
) {
1241 ErrorRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
1242 if ((ErrorRegister
& ABRT_ERR
) == ABRT_ERR
) {
1263 This function is used to poll for the DRDY bit set in the
1264 Alternate Status Register. DRDY bit is set when the device is ready
1265 to accept command. Most ATA commands must be sent after
1266 DRDY set except the ATAPI Packet Command.
1268 @param[in] IDE_BLK_IO_DEV IN *IdeDev
1269 pointer pointing to IDE_BLK_IO_DEV data structure, used
1270 to record all the information of the IDE device.
1272 @param[in] UINTN IN TimeoutInMilliSeconds
1273 used to designate the timeout for the DRQ ready.
1276 DRDY bit set within the time out.
1279 DRDY bit not set within the time out.
1282 Read Alternate Status Register will clear interrupt status.
1287 IN IDE_BLK_IO_DEV
*IdeDev
,
1288 IN UINTN DelayInMilliSeconds
1290 // TODO: function comment is missing 'Routine Description:'
1291 // TODO: function comment is missing 'Arguments:'
1292 // TODO: IdeDev - add argument and description to function comment
1293 // TODO: DelayInMilliSeconds - add argument and description to function comment
1294 // TODO: EFI_ABORTED - add return value to function comment
1298 UINT8 ErrorRegister
;
1300 Delay
= (UINT32
) (((DelayInMilliSeconds
* STALL_1_MILLI_SECOND
) / 30) + 1);
1302 AltRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.AltStatus
);
1304 // BSY == 0 , DRDY == 1
1306 if ((AltRegister
& (DRDY
| BSY
)) == DRDY
) {
1310 if ((AltRegister
& (BSY
| ERR
)) == ERR
) {
1312 ErrorRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
1313 if ((ErrorRegister
& ABRT_ERR
) == ABRT_ERR
) {
1334 This function is a helper function used to change the char order in a
1335 string. It is designed specially for the PrintAtaModuleName() function.
1336 After the IDE device is detected, the IDE driver gets the device module
1337 name by sending ATA command called ATA Identify Command or ATAPI
1338 Identify Command to the specified IDE device. The module name returned
1339 is a string of ASCII characters: the first character is bit8--bit15
1340 of the first word, the second character is bit0--bit7 of the first word
1341 and so on. Thus the string can not be print directly before it is
1342 preprocessed by this func to change the order of characters in
1343 each word in the string.
1345 @param[in] CHAR8 IN *Destination
1346 Indicates the destination string.
1348 @param[in] CHAR8 IN *Source
1349 Indicates the source string.
1351 @param[in] UINT8 IN Size
1352 the length of the string
1357 IN CHAR8
*Destination
,
1365 for (Index
= 0; Index
< Size
; Index
+= 2) {
1367 Temp
= Source
[Index
+ 1];
1368 Destination
[Index
+ 1] = Source
[Index
];
1369 Destination
[Index
] = Temp
;
1374 // ReleaseIdeResources
1377 Release resources of an IDE device before stopping it.
1379 @param[in] *IdeBlkIoDevice Standard IDE device private data structure
1383 ReleaseIdeResources (
1384 IN IDE_BLK_IO_DEV
*IdeBlkIoDevice
1387 if (IdeBlkIoDevice
== NULL
) {
1392 // Release all the resourses occupied by the IDE_BLK_IO_DEV
1395 if (IdeBlkIoDevice
->SenseData
!= NULL
) {
1396 gBS
->FreePool (IdeBlkIoDevice
->SenseData
);
1397 IdeBlkIoDevice
->SenseData
= NULL
;
1400 if (IdeBlkIoDevice
->Cache
!= NULL
) {
1401 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1402 IdeBlkIoDevice
->Cache
= NULL
;
1405 if (IdeBlkIoDevice
->pIdData
!= NULL
) {
1406 gBS
->FreePool (IdeBlkIoDevice
->pIdData
);
1407 IdeBlkIoDevice
->pIdData
= NULL
;
1410 if (IdeBlkIoDevice
->pInquiryData
!= NULL
) {
1411 gBS
->FreePool (IdeBlkIoDevice
->pInquiryData
);
1412 IdeBlkIoDevice
->pInquiryData
= NULL
;
1415 if (IdeBlkIoDevice
->ControllerNameTable
!= NULL
) {
1416 FreeUnicodeStringTable (IdeBlkIoDevice
->ControllerNameTable
);
1417 IdeBlkIoDevice
->ControllerNameTable
= NULL
;
1420 if (IdeBlkIoDevice
->IoPort
!= NULL
) {
1421 gBS
->FreePool (IdeBlkIoDevice
->IoPort
);
1424 if (IdeBlkIoDevice
->DevicePath
!= NULL
) {
1425 gBS
->FreePool (IdeBlkIoDevice
->DevicePath
);
1428 gBS
->FreePool (IdeBlkIoDevice
);
1429 IdeBlkIoDevice
= NULL
;
1435 // SetDeviceTransferMode
1438 Set the calculated Best transfer mode to a detected device
1440 @param[in] *IdeDev Standard IDE device private data structure
1441 @param[in] *TransferMode The device transfer mode to be set
1443 @return Set transfer mode Command execute status
1447 SetDeviceTransferMode (
1448 IN IDE_BLK_IO_DEV
*IdeDev
,
1449 IN ATA_TRANSFER_MODE
*TransferMode
1451 // TODO: function comment is missing 'Routine Description:'
1458 DeviceSelect
= (UINT8
) ((IdeDev
->Device
) << 4);
1459 SectorCount
= *((UINT8
*) TransferMode
);
1462 // Send SET FEATURE command (sub command 0x03) to set pio mode.
1464 Status
= AtaNonDataCommandIn (
1479 Send ATA command into device with NON_DATA protocol
1481 @param IdeDev Standard IDE device private data structure
1482 @param AtaCommand The ATA command to be sent
1483 @param Device The value in Device register
1484 @param Feature The value in Feature register
1485 @param SectorCount The value in SectorCount register
1486 @param LbaLow The value in LBA_LOW register
1487 @param LbaMiddle The value in LBA_MIDDLE register
1488 @param LbaHigh The value in LBA_HIGH register
1490 @retval EFI_SUCCESS Reading succeed
1491 @retval EFI_ABORTED Command failed
1492 @retval EFI_DEVICE_ERROR Device status error
1496 AtaNonDataCommandIn (
1497 IN IDE_BLK_IO_DEV
*IdeDev
,
1498 IN UINT8 AtaCommand
,
1501 IN UINT8 SectorCount
,
1508 UINT8 StatusRegister
;
1510 Status
= WaitForBSYClear (IdeDev
, ATATIMEOUT
);
1511 if (EFI_ERROR (Status
)) {
1512 return EFI_DEVICE_ERROR
;
1516 // Select device (bit4), set LBA mode(bit6) (use 0xe0 for compatibility)
1520 IdeDev
->IoPort
->Head
,
1521 (UINT8
) ((IdeDev
->Device
<< 4) | 0xe0)
1525 // ATA commands for ATA device must be issued when DRDY is set
1527 Status
= DRDYReady (IdeDev
, ATATIMEOUT
);
1528 if (EFI_ERROR (Status
)) {
1529 return EFI_DEVICE_ERROR
;
1533 // Pass parameter into device register block
1535 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, Device
);
1536 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, Feature
);
1537 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorCount
, SectorCount
);
1538 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorNumber
, LbaLow
);
1539 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
, LbaMiddle
);
1540 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
, LbaHigh
);
1543 // Send command via Command Register
1545 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, AtaCommand
);
1548 // Wait for command completion
1550 Status
= WaitForBSYClear (IdeDev
, ATATIMEOUT
);
1551 if (EFI_ERROR (Status
)) {
1552 return EFI_DEVICE_ERROR
;
1555 StatusRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
1556 if ((StatusRegister
& ERR
) == ERR
) {
1558 // Failed to execute command, abort operation
1567 Send ATA Ext command into device with NON_DATA protocol
1569 @param IdeDev Standard IDE device private data structure
1570 @param AtaCommand The ATA command to be sent
1571 @param Device The value in Device register
1572 @param Feature The value in Feature register
1573 @param SectorCount The value in SectorCount register
1574 @param LbaAddress The LBA address in 48-bit mode
1576 @retval EFI_SUCCESS Reading succeed
1577 @retval EFI_ABORTED Command failed
1578 @retval EFI_DEVICE_ERROR Device status error
1582 AtaNonDataCommandInExt (
1583 IN IDE_BLK_IO_DEV
*IdeDev
,
1584 IN UINT8 AtaCommand
,
1587 IN UINT16 SectorCount
,
1588 IN EFI_LBA LbaAddress
1592 UINT8 StatusRegister
;
1599 Status
= WaitForBSYClear (IdeDev
, ATATIMEOUT
);
1600 if (EFI_ERROR (Status
)) {
1601 return EFI_DEVICE_ERROR
;
1605 // Select device (bit4), set LBA mode(bit6) (use 0xe0 for compatibility)
1609 IdeDev
->IoPort
->Head
,
1610 (UINT8
) ((IdeDev
->Device
<< 4) | 0xe0)
1614 // ATA commands for ATA device must be issued when DRDY is set
1616 Status
= DRDYReady (IdeDev
, ATATIMEOUT
);
1617 if (EFI_ERROR (Status
)) {
1618 return EFI_DEVICE_ERROR
;
1622 // Pass parameter into device register block
1624 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, Device
);
1627 // Fill the feature register, which is a two-byte FIFO. Need write twice.
1629 Feature8
= (UINT8
) (Feature
>> 8);
1630 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, Feature8
);
1632 Feature8
= (UINT8
) Feature
;
1633 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, Feature8
);
1636 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
1638 SectorCount8
= (UINT8
) (SectorCount
>> 8);
1639 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorCount
, SectorCount8
);
1641 SectorCount8
= (UINT8
) SectorCount
;
1642 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorCount
, SectorCount8
);
1645 // Fill the start LBA registers, which are also two-byte FIFO
1647 LbaLow
= (UINT8
) RShiftU64 (LbaAddress
, 24);
1648 LbaMid
= (UINT8
) RShiftU64 (LbaAddress
, 32);
1649 LbaHigh
= (UINT8
) RShiftU64 (LbaAddress
, 40);
1650 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorNumber
, LbaLow
);
1651 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
, LbaMid
);
1652 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
, LbaHigh
);
1654 LbaLow
= (UINT8
) LbaAddress
;
1655 LbaMid
= (UINT8
) RShiftU64 (LbaAddress
, 8);
1656 LbaHigh
= (UINT8
) RShiftU64 (LbaAddress
, 16);
1657 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorNumber
, LbaLow
);
1658 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
, LbaMid
);
1659 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
, LbaHigh
);
1662 // Send command via Command Register
1664 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, AtaCommand
);
1667 // Wait for command completion
1669 Status
= WaitForBSYClear (IdeDev
, ATATIMEOUT
);
1670 if (EFI_ERROR (Status
)) {
1671 return EFI_DEVICE_ERROR
;
1674 StatusRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
1675 if ((StatusRegister
& ERR
) == ERR
) {
1677 // Failed to execute command, abort operation
1686 // SetDriveParameters
1689 Set drive parameters for devices not support PACKETS command
1691 @param[in] IdeDev Standard IDE device private data structure
1692 @param[in] DriveParameters The device parameters to be set into the disk
1694 @return SetParameters Command execute status
1698 SetDriveParameters (
1699 IN IDE_BLK_IO_DEV
*IdeDev
,
1700 IN ATA_DRIVE_PARMS
*DriveParameters
1707 DeviceSelect
= (UINT8
) ((IdeDev
->Device
) << 4);
1710 // Send Init drive parameters
1712 Status
= AtaPioDataIn (
1716 INIT_DRIVE_PARAM_CMD
,
1717 (UINT8
) (DeviceSelect
+ DriveParameters
->Heads
),
1718 DriveParameters
->Sector
,
1725 // Send Set Multiple parameters
1727 Status
= AtaPioDataIn (
1731 SET_MULTIPLE_MODE_CMD
,
1733 DriveParameters
->MultipleSector
,
1743 TODO: Add function description
1745 @param IdeDev TODO: add argument description
1747 @retval EFI_SUCCESS TODO: Add description for return value
1752 IN IDE_BLK_IO_DEV
*IdeDev
1755 UINT8 DeviceControl
;
1758 // Enable interrupt for DMA operation
1761 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);