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
721 // Stall 20ms to wait for slave device ready if master device not exists
723 if (!MasterDeviceExist
) {
732 IdeDev
->IoPort
->Head
,
733 (UINT8
) ((1 << 4) | 0xe0)
737 ErrorReg
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
740 // Slave Error register is not 0x01, D1 failed. Return.
742 if (ErrorReg
!= 0x01) {
743 SlaveDeviceExist
= FALSE
;
747 StatusReg
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
750 // Most ATAPI devices don't set DRDY bit, so test with a slow but accurate
751 // "ATAPI TEST UNIT READY" command
753 if (((StatusReg
& DRDY
) == 0) && ((InitStatusReg
& DRDY
) == 0)) {
754 Status
= AtapiTestUnitReady (IdeDev
);
757 // Still fail, Slave doesn't exist.
759 if (EFI_ERROR (Status
)) {
760 SlaveDeviceExist
= FALSE
;
766 // Error reg is 0x01 and DRDY is ready,
767 // or ATAPI test unit ready success,
768 // or init Slave status DRDY is ready
771 SlaveDeviceExist
= TRUE
;
778 This function is used to poll for the DRQ bit clear in the Status
779 Register. DRQ is cleared when the device is finished transferring data.
780 So this function is called after data transfer is finished.
783 pointer pointing to IDE_BLK_IO_DEV data structure, used
784 to record all the information of the IDE device.
786 @param[in] TimeoutInMilliSeconds
787 used to designate the timeout for the DRQ clear.
790 DRQ bit clear within the time out.
793 DRQ bit not clear within the time out.
796 Read Status Register will clear interrupt status.
801 IN IDE_BLK_IO_DEV
*IdeDev
,
802 IN UINTN TimeoutInMilliSeconds
804 // TODO: function comment is missing 'Routine Description:'
805 // TODO: function comment is missing 'Arguments:'
806 // TODO: IdeDev - add argument and description to function comment
807 // TODO: TimeoutInMilliSeconds - add argument and description to function comment
808 // TODO: EFI_ABORTED - add return value to function comment
811 UINT8 StatusRegister
;
814 Delay
= (UINT32
) (((TimeoutInMilliSeconds
* STALL_1_MILLI_SECOND
) / 30) + 1);
817 StatusRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
820 // wait for BSY == 0 and DRQ == 0
822 if ((StatusRegister
& (DRQ
| BSY
)) == 0) {
826 if ((StatusRegister
& (BSY
| ERR
)) == ERR
) {
828 ErrorRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
829 if ((ErrorRegister
& ABRT_ERR
) == ABRT_ERR
) {
851 This function is used to poll for the DRQ bit clear in the Alternate
852 Status Register. DRQ is cleared when the device is finished
853 transferring data. So this function is called after data transfer
857 pointer pointing to IDE_BLK_IO_DEV data structure, used
858 to record all the information of the IDE device.
860 @param[in] TimeoutInMilliSeconds
861 used to designate the timeout for the DRQ clear.
864 DRQ bit clear within the time out.
867 DRQ bit not clear within the time out.
870 Read Alternate Status Register will not clear interrupt status.
875 IN IDE_BLK_IO_DEV
*IdeDev
,
876 IN UINTN TimeoutInMilliSeconds
878 // TODO: function comment is missing 'Routine Description:'
879 // TODO: function comment is missing 'Arguments:'
880 // TODO: IdeDev - add argument and description to function comment
881 // TODO: TimeoutInMilliSeconds - add argument and description to function comment
882 // TODO: EFI_ABORTED - add return value to function comment
888 Delay
= (UINT32
) (((TimeoutInMilliSeconds
* STALL_1_MILLI_SECOND
) / 30) + 1);
891 AltRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.AltStatus
);
894 // wait for BSY == 0 and DRQ == 0
896 if ((AltRegister
& (DRQ
| BSY
)) == 0) {
900 if ((AltRegister
& (BSY
| ERR
)) == ERR
) {
902 ErrorRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
903 if ((ErrorRegister
& ABRT_ERR
) == ABRT_ERR
) {
925 This function is used to poll for the DRQ bit set in the
927 DRQ is set when the device is ready to transfer data. So this function
928 is called after the command is sent to the device and before required
931 @param[in] IDE_BLK_IO_DEV IN *IdeDev
932 pointer pointing to IDE_BLK_IO_DEV data structure,used
933 to record all the information of the IDE device.
935 @param[in] UINTN IN TimeoutInMilliSeconds
936 used to designate the timeout for the DRQ ready.
939 DRQ bit set within the time out.
942 DRQ bit not set within the time out.
945 DRQ bit not set caused by the command abort.
948 Read Status Register will clear interrupt status.
953 IN IDE_BLK_IO_DEV
*IdeDev
,
954 IN UINTN TimeoutInMilliSeconds
956 // TODO: function comment is missing 'Routine Description:'
957 // TODO: function comment is missing 'Arguments:'
958 // TODO: IdeDev - add argument and description to function comment
959 // TODO: TimeoutInMilliSeconds - add argument and description to function comment
962 UINT8 StatusRegister
;
965 Delay
= (UINT32
) (((TimeoutInMilliSeconds
* STALL_1_MILLI_SECOND
) / 30) + 1);
968 // read Status Register will clear interrupt
970 StatusRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
975 if ((StatusRegister
& (BSY
| DRQ
)) == DRQ
) {
979 if ((StatusRegister
& (BSY
| ERR
)) == ERR
) {
981 ErrorRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
982 if ((ErrorRegister
& ABRT_ERR
) == ABRT_ERR
) {
1003 This function is used to poll for the DRQ bit set in the
1004 Alternate Status Register. DRQ is set when the device is ready to
1005 transfer data. So this function is called after the command
1006 is sent to the device and before required data is transferred.
1008 @param[in] IDE_BLK_IO_DEV IN *IdeDev
1009 pointer pointing to IDE_BLK_IO_DEV data structure, used
1010 to record all the information of the IDE device.
1012 @param[in] UINTN IN TimeoutInMilliSeconds
1013 used to designate the timeout for the DRQ ready.
1016 DRQ bit set within the time out.
1019 DRQ bit not set within the time out.
1022 DRQ bit not set caused by the command abort.
1025 Read Alternate Status Register will not clear interrupt status.
1030 IN IDE_BLK_IO_DEV
*IdeDev
,
1031 IN UINTN TimeoutInMilliSeconds
1033 // TODO: function comment is missing 'Routine Description:'
1034 // TODO: function comment is missing 'Arguments:'
1035 // TODO: IdeDev - add argument and description to function comment
1036 // TODO: TimeoutInMilliSeconds - add argument and description to function comment
1040 UINT8 ErrorRegister
;
1042 Delay
= (UINT32
) (((TimeoutInMilliSeconds
* STALL_1_MILLI_SECOND
) / 30) + 1);
1046 // Read Alternate Status Register will not clear interrupt status
1048 AltRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.AltStatus
);
1050 // BSY == 0 , DRQ == 1
1052 if ((AltRegister
& (BSY
| DRQ
)) == DRQ
) {
1056 if ((AltRegister
& (BSY
| ERR
)) == ERR
) {
1058 ErrorRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
1059 if ((ErrorRegister
& ABRT_ERR
) == ABRT_ERR
) {
1080 This function is used to poll for the BSY bit clear in the
1081 Status Register. BSY is clear when the device is not busy.
1082 Every command must be sent after device is not busy.
1084 @param[in] IDE_BLK_IO_DEV IN *IdeDev
1085 pointer pointing to IDE_BLK_IO_DEV data structure, used
1086 to record all the information of the IDE device.
1088 @param[in] UINTN IN TimeoutInMilliSeconds
1089 used to designate the timeout for the DRQ ready.
1092 BSY bit clear within the time out.
1095 BSY bit not clear within the time out.
1098 Read Status Register will clear interrupt status.
1103 IN IDE_BLK_IO_DEV
*IdeDev
,
1104 IN UINTN TimeoutInMilliSeconds
1106 // TODO: function comment is missing 'Routine Description:'
1107 // TODO: function comment is missing 'Arguments:'
1108 // TODO: IdeDev - add argument and description to function comment
1109 // TODO: TimeoutInMilliSeconds - add argument and description to function comment
1112 UINT8 StatusRegister
;
1114 Delay
= (UINT32
) (((TimeoutInMilliSeconds
* STALL_1_MILLI_SECOND
) / 30) + 1);
1117 StatusRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
1118 if ((StatusRegister
& BSY
) == 0x00) {
1141 This function is used to poll for the BSY bit clear in the
1142 Alternate Status Register. BSY is clear when the device is not busy.
1143 Every command must be sent after device is not busy.
1145 @param[in] IDE_BLK_IO_DEV IN *IdeDev
1146 pointer pointing to IDE_BLK_IO_DEV data structure, used
1147 to record all the information of the IDE device.
1149 @param[in] UINTN IN TimeoutInMilliSeconds
1150 used to designate the timeout for the DRQ ready.
1153 BSY bit clear within the time out.
1156 BSY bit not clear within the time out.
1159 Read Alternate Status Register will not clear interrupt status.
1164 IN IDE_BLK_IO_DEV
*IdeDev
,
1165 IN UINTN TimeoutInMilliSeconds
1167 // TODO: function comment is missing 'Routine Description:'
1168 // TODO: function comment is missing 'Arguments:'
1169 // TODO: IdeDev - add argument and description to function comment
1170 // TODO: TimeoutInMilliSeconds - add argument and description to function comment
1175 Delay
= (UINT32
) (((TimeoutInMilliSeconds
* STALL_1_MILLI_SECOND
) / 30) + 1);
1177 AltRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.AltStatus
);
1178 if ((AltRegister
& BSY
) == 0x00) {
1199 This function is used to poll for the DRDY bit set in the
1200 Status Register. DRDY bit is set when the device is ready
1201 to accept command. Most ATA commands must be sent after
1202 DRDY set except the ATAPI Packet Command.
1204 @param[in] IDE_BLK_IO_DEV IN *IdeDev
1205 pointer pointing to IDE_BLK_IO_DEV data structure, used
1206 to record all the information of the IDE device.
1208 @param[in] UINTN IN TimeoutInMilliSeconds
1209 used to designate the timeout for the DRQ ready.
1212 DRDY bit set within the time out.
1215 DRDY bit not set within the time out.
1218 Read Status Register will clear interrupt status.
1223 IN IDE_BLK_IO_DEV
*IdeDev
,
1224 IN UINTN DelayInMilliSeconds
1226 // TODO: function comment is missing 'Routine Description:'
1227 // TODO: function comment is missing 'Arguments:'
1228 // TODO: IdeDev - add argument and description to function comment
1229 // TODO: DelayInMilliSeconds - add argument and description to function comment
1230 // TODO: EFI_ABORTED - add return value to function comment
1233 UINT8 StatusRegister
;
1234 UINT8 ErrorRegister
;
1236 Delay
= (UINT32
) (((DelayInMilliSeconds
* STALL_1_MILLI_SECOND
) / 30) + 1);
1238 StatusRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
1240 // BSY == 0 , DRDY == 1
1242 if ((StatusRegister
& (DRDY
| BSY
)) == DRDY
) {
1246 if ((StatusRegister
& (BSY
| ERR
)) == ERR
) {
1248 ErrorRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
1249 if ((ErrorRegister
& ABRT_ERR
) == ABRT_ERR
) {
1270 This function is used to poll for the DRDY bit set in the
1271 Alternate Status Register. DRDY bit is set when the device is ready
1272 to accept command. Most ATA commands must be sent after
1273 DRDY set except the ATAPI Packet Command.
1275 @param[in] IDE_BLK_IO_DEV IN *IdeDev
1276 pointer pointing to IDE_BLK_IO_DEV data structure, used
1277 to record all the information of the IDE device.
1279 @param[in] UINTN IN TimeoutInMilliSeconds
1280 used to designate the timeout for the DRQ ready.
1283 DRDY bit set within the time out.
1286 DRDY bit not set within the time out.
1289 Read Alternate Status Register will clear interrupt status.
1294 IN IDE_BLK_IO_DEV
*IdeDev
,
1295 IN UINTN DelayInMilliSeconds
1297 // TODO: function comment is missing 'Routine Description:'
1298 // TODO: function comment is missing 'Arguments:'
1299 // TODO: IdeDev - add argument and description to function comment
1300 // TODO: DelayInMilliSeconds - add argument and description to function comment
1301 // TODO: EFI_ABORTED - add return value to function comment
1305 UINT8 ErrorRegister
;
1307 Delay
= (UINT32
) (((DelayInMilliSeconds
* STALL_1_MILLI_SECOND
) / 30) + 1);
1309 AltRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.AltStatus
);
1311 // BSY == 0 , DRDY == 1
1313 if ((AltRegister
& (DRDY
| BSY
)) == DRDY
) {
1317 if ((AltRegister
& (BSY
| ERR
)) == ERR
) {
1319 ErrorRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
1320 if ((ErrorRegister
& ABRT_ERR
) == ABRT_ERR
) {
1341 This function is a helper function used to change the char order in a
1342 string. It is designed specially for the PrintAtaModuleName() function.
1343 After the IDE device is detected, the IDE driver gets the device module
1344 name by sending ATA command called ATA Identify Command or ATAPI
1345 Identify Command to the specified IDE device. The module name returned
1346 is a string of ASCII characters: the first character is bit8--bit15
1347 of the first word, the second character is bit0--bit7 of the first word
1348 and so on. Thus the string can not be print directly before it is
1349 preprocessed by this func to change the order of characters in
1350 each word in the string.
1352 @param[in] CHAR8 IN *Destination
1353 Indicates the destination string.
1355 @param[in] CHAR8 IN *Source
1356 Indicates the source string.
1358 @param[in] UINT8 IN Size
1359 the length of the string
1364 IN CHAR8
*Destination
,
1372 for (Index
= 0; Index
< Size
; Index
+= 2) {
1374 Temp
= Source
[Index
+ 1];
1375 Destination
[Index
+ 1] = Source
[Index
];
1376 Destination
[Index
] = Temp
;
1381 // ReleaseIdeResources
1384 Release resources of an IDE device before stopping it.
1386 @param[in] *IdeBlkIoDevice Standard IDE device private data structure
1390 ReleaseIdeResources (
1391 IN IDE_BLK_IO_DEV
*IdeBlkIoDevice
1394 if (IdeBlkIoDevice
== NULL
) {
1399 // Release all the resourses occupied by the IDE_BLK_IO_DEV
1402 if (IdeBlkIoDevice
->SenseData
!= NULL
) {
1403 gBS
->FreePool (IdeBlkIoDevice
->SenseData
);
1404 IdeBlkIoDevice
->SenseData
= NULL
;
1407 if (IdeBlkIoDevice
->Cache
!= NULL
) {
1408 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1409 IdeBlkIoDevice
->Cache
= NULL
;
1412 if (IdeBlkIoDevice
->pIdData
!= NULL
) {
1413 gBS
->FreePool (IdeBlkIoDevice
->pIdData
);
1414 IdeBlkIoDevice
->pIdData
= NULL
;
1417 if (IdeBlkIoDevice
->pInquiryData
!= NULL
) {
1418 gBS
->FreePool (IdeBlkIoDevice
->pInquiryData
);
1419 IdeBlkIoDevice
->pInquiryData
= NULL
;
1422 if (IdeBlkIoDevice
->ControllerNameTable
!= NULL
) {
1423 FreeUnicodeStringTable (IdeBlkIoDevice
->ControllerNameTable
);
1424 IdeBlkIoDevice
->ControllerNameTable
= NULL
;
1427 if (IdeBlkIoDevice
->IoPort
!= NULL
) {
1428 gBS
->FreePool (IdeBlkIoDevice
->IoPort
);
1431 if (IdeBlkIoDevice
->DevicePath
!= NULL
) {
1432 gBS
->FreePool (IdeBlkIoDevice
->DevicePath
);
1435 gBS
->FreePool (IdeBlkIoDevice
);
1436 IdeBlkIoDevice
= NULL
;
1442 // SetDeviceTransferMode
1445 Set the calculated Best transfer mode to a detected device
1447 @param[in] *IdeDev Standard IDE device private data structure
1448 @param[in] *TransferMode The device transfer mode to be set
1450 @return Set transfer mode Command execute status
1454 SetDeviceTransferMode (
1455 IN IDE_BLK_IO_DEV
*IdeDev
,
1456 IN ATA_TRANSFER_MODE
*TransferMode
1458 // TODO: function comment is missing 'Routine Description:'
1465 DeviceSelect
= (UINT8
) ((IdeDev
->Device
) << 4);
1466 SectorCount
= *((UINT8
*) TransferMode
);
1469 // Send SET FEATURE command (sub command 0x03) to set pio mode.
1471 Status
= AtaNonDataCommandIn (
1486 Send ATA command into device with NON_DATA protocol
1488 @param IdeDev Standard IDE device private data structure
1489 @param AtaCommand The ATA command to be sent
1490 @param Device The value in Device register
1491 @param Feature The value in Feature register
1492 @param SectorCount The value in SectorCount register
1493 @param LbaLow The value in LBA_LOW register
1494 @param LbaMiddle The value in LBA_MIDDLE register
1495 @param LbaHigh The value in LBA_HIGH register
1497 @retval EFI_SUCCESS Reading succeed
1498 @retval EFI_ABORTED Command failed
1499 @retval EFI_DEVICE_ERROR Device status error
1503 AtaNonDataCommandIn (
1504 IN IDE_BLK_IO_DEV
*IdeDev
,
1505 IN UINT8 AtaCommand
,
1508 IN UINT8 SectorCount
,
1515 UINT8 StatusRegister
;
1517 Status
= WaitForBSYClear (IdeDev
, ATATIMEOUT
);
1518 if (EFI_ERROR (Status
)) {
1519 return EFI_DEVICE_ERROR
;
1523 // Select device (bit4), set LBA mode(bit6) (use 0xe0 for compatibility)
1527 IdeDev
->IoPort
->Head
,
1528 (UINT8
) ((IdeDev
->Device
<< 4) | 0xe0)
1532 // ATA commands for ATA device must be issued when DRDY is set
1534 Status
= DRDYReady (IdeDev
, ATATIMEOUT
);
1535 if (EFI_ERROR (Status
)) {
1536 return EFI_DEVICE_ERROR
;
1540 // Pass parameter into device register block
1542 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, Device
);
1543 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, Feature
);
1544 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorCount
, SectorCount
);
1545 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorNumber
, LbaLow
);
1546 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
, LbaMiddle
);
1547 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
, LbaHigh
);
1550 // Send command via Command Register
1552 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, AtaCommand
);
1555 // Wait for command completion
1557 Status
= WaitForBSYClear (IdeDev
, ATATIMEOUT
);
1558 if (EFI_ERROR (Status
)) {
1559 return EFI_DEVICE_ERROR
;
1562 StatusRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
1563 if ((StatusRegister
& ERR
) == ERR
) {
1565 // Failed to execute command, abort operation
1574 Send ATA Ext command into device with NON_DATA protocol
1576 @param IdeDev Standard IDE device private data structure
1577 @param AtaCommand The ATA command to be sent
1578 @param Device The value in Device register
1579 @param Feature The value in Feature register
1580 @param SectorCount The value in SectorCount register
1581 @param LbaAddress The LBA address in 48-bit mode
1583 @retval EFI_SUCCESS Reading succeed
1584 @retval EFI_ABORTED Command failed
1585 @retval EFI_DEVICE_ERROR Device status error
1589 AtaNonDataCommandInExt (
1590 IN IDE_BLK_IO_DEV
*IdeDev
,
1591 IN UINT8 AtaCommand
,
1594 IN UINT16 SectorCount
,
1595 IN EFI_LBA LbaAddress
1599 UINT8 StatusRegister
;
1606 Status
= WaitForBSYClear (IdeDev
, ATATIMEOUT
);
1607 if (EFI_ERROR (Status
)) {
1608 return EFI_DEVICE_ERROR
;
1612 // Select device (bit4), set LBA mode(bit6) (use 0xe0 for compatibility)
1616 IdeDev
->IoPort
->Head
,
1617 (UINT8
) ((IdeDev
->Device
<< 4) | 0xe0)
1621 // ATA commands for ATA device must be issued when DRDY is set
1623 Status
= DRDYReady (IdeDev
, ATATIMEOUT
);
1624 if (EFI_ERROR (Status
)) {
1625 return EFI_DEVICE_ERROR
;
1629 // Pass parameter into device register block
1631 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, Device
);
1634 // Fill the feature register, which is a two-byte FIFO. Need write twice.
1636 Feature8
= (UINT8
) (Feature
>> 8);
1637 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, Feature8
);
1639 Feature8
= (UINT8
) Feature
;
1640 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, Feature8
);
1643 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
1645 SectorCount8
= (UINT8
) (SectorCount
>> 8);
1646 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorCount
, SectorCount8
);
1648 SectorCount8
= (UINT8
) SectorCount
;
1649 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorCount
, SectorCount8
);
1652 // Fill the start LBA registers, which are also two-byte FIFO
1654 LbaLow
= (UINT8
) RShiftU64 (LbaAddress
, 24);
1655 LbaMid
= (UINT8
) RShiftU64 (LbaAddress
, 32);
1656 LbaHigh
= (UINT8
) RShiftU64 (LbaAddress
, 40);
1657 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorNumber
, LbaLow
);
1658 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
, LbaMid
);
1659 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
, LbaHigh
);
1661 LbaLow
= (UINT8
) LbaAddress
;
1662 LbaMid
= (UINT8
) RShiftU64 (LbaAddress
, 8);
1663 LbaHigh
= (UINT8
) RShiftU64 (LbaAddress
, 16);
1664 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->SectorNumber
, LbaLow
);
1665 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
, LbaMid
);
1666 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
, LbaHigh
);
1669 // Send command via Command Register
1671 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, AtaCommand
);
1674 // Wait for command completion
1676 Status
= WaitForBSYClear (IdeDev
, ATATIMEOUT
);
1677 if (EFI_ERROR (Status
)) {
1678 return EFI_DEVICE_ERROR
;
1681 StatusRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
1682 if ((StatusRegister
& ERR
) == ERR
) {
1684 // Failed to execute command, abort operation
1693 // SetDriveParameters
1696 Set drive parameters for devices not support PACKETS command
1698 @param[in] IdeDev Standard IDE device private data structure
1699 @param[in] DriveParameters The device parameters to be set into the disk
1701 @return SetParameters Command execute status
1705 SetDriveParameters (
1706 IN IDE_BLK_IO_DEV
*IdeDev
,
1707 IN ATA_DRIVE_PARMS
*DriveParameters
1714 DeviceSelect
= (UINT8
) ((IdeDev
->Device
) << 4);
1717 // Send Init drive parameters
1719 Status
= AtaPioDataIn (
1723 INIT_DRIVE_PARAM_CMD
,
1724 (UINT8
) (DeviceSelect
+ DriveParameters
->Heads
),
1725 DriveParameters
->Sector
,
1732 // Send Set Multiple parameters
1734 Status
= AtaPioDataIn (
1738 SET_MULTIPLE_MODE_CMD
,
1740 DriveParameters
->MultipleSector
,
1750 TODO: Add function description
1752 @param IdeDev TODO: add argument description
1754 @retval EFI_SUCCESS TODO: Add description for return value
1759 IN IDE_BLK_IO_DEV
*IdeDev
1762 UINT8 DeviceControl
;
1765 // Enable interrupt for DMA operation
1768 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DeviceControl
);