2 Floppy Peim to support Recovery function from Floppy device.
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions
8 of the BSD License which accompanies this distribution. The
9 full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include "FloppyPeim.h"
21 PEI_DMA_TABLE mRegisterTable
[] = {
23 // DMA2: Clear Byte Ptr, Enable
26 R_8237_DMA_CBPR_CH4_7
,
30 R_8237_DMA_COMMAND_CH4_7
,
34 // DMA1: Clear Byte Ptr, Enable
37 R_8237_DMA_CBPR_CH0_3
,
41 R_8237_DMA_COMMAND_CH0_3
,
45 // Configure Channel 4 for Cascade Mode
46 // Clear DMA Request and enable DREQ
49 R_8237_DMA_CHMODE_CH4_7
,
50 V_8237_DMA_CHMODE_CASCADE
| 0
57 R_8237_DMA_WRSMSK_CH4_7
,
61 // Configure DMA1 (Channels 0-3) for Single Mode
62 // Clear DMA Request and enable DREQ
65 R_8237_DMA_CHMODE_CH0_3
,
66 V_8237_DMA_CHMODE_SINGLE
| 0
73 R_8237_DMA_WRSMSK_CH0_3
,
77 R_8237_DMA_CHMODE_CH0_3
,
78 V_8237_DMA_CHMODE_SINGLE
| 1
85 R_8237_DMA_WRSMSK_CH0_3
,
89 R_8237_DMA_CHMODE_CH0_3
,
90 V_8237_DMA_CHMODE_SINGLE
| 2
97 R_8237_DMA_WRSMSK_CH0_3
,
101 R_8237_DMA_CHMODE_CH0_3
,
102 V_8237_DMA_CHMODE_SINGLE
| 3
105 R_8237_DMA_STA_CH0_3
,
109 R_8237_DMA_WRSMSK_CH0_3
,
113 // Configure DMA2 (Channels 5-7) for Single Mode
114 // Clear DMA Request and enable DREQ
117 R_8237_DMA_CHMODE_CH4_7
,
118 V_8237_DMA_CHMODE_SINGLE
| 1
121 R_8237_DMA_STA_CH4_7
,
125 R_8237_DMA_WRSMSK_CH4_7
,
129 R_8237_DMA_CHMODE_CH4_7
,
130 V_8237_DMA_CHMODE_SINGLE
| 2
133 R_8237_DMA_STA_CH4_7
,
137 R_8237_DMA_WRSMSK_CH4_7
,
141 R_8237_DMA_CHMODE_CH4_7
,
142 V_8237_DMA_CHMODE_SINGLE
| 3
145 R_8237_DMA_STA_CH4_7
,
149 R_8237_DMA_WRSMSK_CH4_7
,
155 // Table of diskette parameters of various diskette types
157 DISKET_PARA_TABLE DiskPara
[9] = {
260 // Byte per sector corresponding to various device types.
262 UINTN BytePerSector
[6] = { 0, 256, 512, 1024, 2048, 4096 };
264 FDC_BLK_IO_DEV mBlockIoDevTemplate
= {
265 FDC_BLK_IO_DEV_SIGNATURE
,
267 FdcGetNumberOfBlockDevices
,
268 FdcGetBlockDeviceMediaInfo
,
272 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
273 &gEfiPeiVirtualBlockIoPpiGuid
,
281 Wait and check if bits for DIO and RQM of FDC Main Status Register
282 indicates FDC is ready for read or write.
284 Before writing to FDC or reading from FDC, the Host must examine
285 the bit7(RQM) and bit6(DIO) of the Main Status Register.
287 Command bytes can not be written to Data Register unless RQM is 1 and DIO is 0.
288 Result bytes can not be read from Data Register unless RQM is 1 and DIO is 1.
290 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
291 @param DataIn Indicates data input or output.
294 @param TimeoutInMseconds Timeout value to wait.
296 @retval EFI_SUCCESS FDC is ready.
297 @retval EFI_NOT_READY FDC is not ready within the specified time period.
302 IN FDC_BLK_IO_DEV
*FdcBlkIoDev
,
304 IN UINTN TimeoutInMseconds
308 UINT8 StatusRegister
;
312 // Check bit6 of Main Status Register.
319 Delay
= ((TimeoutInMseconds
* STALL_1_MSECOND
) / FDC_CHECK_INTERVAL
) + 1;
321 StatusRegister
= IoRead8 ((UINT16
) (PcdGet16 (PcdFdcBaseAddress
) + FDC_REGISTER_MSR
));
322 if ((StatusRegister
& MSR_RQM
) == MSR_RQM
&& (StatusRegister
& MSR_DIO
) == BitInOut
) {
329 MicroSecondDelay (FDC_SHORT_DELAY
);
330 } while (--Delay
> 0);
334 // FDC is not ready within the specified time period
336 return EFI_NOT_READY
;
343 Read a byte from FDC data register.
345 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
346 @param Pointer Pointer to buffer to hold data read from FDC.
348 @retval EFI_SUCCESS Byte successfully read.
349 @retval EFI_DEVICE_ERROR FDC is not ready.
354 IN FDC_BLK_IO_DEV
*FdcBlkIoDev
,
361 // Wait for 1ms and detect the FDC is ready to be read
363 if (FdcDRQReady (FdcBlkIoDev
, TRUE
, 1) != EFI_SUCCESS
) {
367 return EFI_DEVICE_ERROR
;
370 Data
= IoRead8 ((UINT16
) (PcdGet16 (PcdFdcBaseAddress
) + FDC_REGISTER_DTR
));
371 MicroSecondDelay (FDC_SHORT_DELAY
);
378 Write a byte to FDC data register.
380 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
381 @param Pointer Pointer to data to write.
383 @retval EFI_SUCCESS Byte successfully written.
384 @retval EFI_DEVICE_ERROR FDC is not ready.
389 IN FDC_BLK_IO_DEV
*FdcBlkIoDev
,
396 // Wait for 1ms and detect the FDC is ready to be written
398 if (FdcDRQReady (FdcBlkIoDev
, FALSE
, 1) != EFI_SUCCESS
) {
402 return EFI_DEVICE_ERROR
;
406 IoWrite8 ((UINT16
) (PcdGet16 (PcdFdcBaseAddress
) + FDC_REGISTER_DTR
), Data
);
407 MicroSecondDelay (FDC_SHORT_DELAY
);
413 Get Sts0 and Pcn status from FDC
415 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV
416 @param Sts0 Value of Sts0
417 @param Pcn Value of Pcn
419 @retval EFI_SUCCESS Successfully retrieved status value of Sts0 and Pcn.
420 @retval EFI_DEVICE_ERROR Fail to send SENSE_INT_STATUS_CMD.
421 @retval EFI_DEVICE_ERROR Fail to read Sts0.
422 @retval EFI_DEVICE_ERROR Fail to read Pcn.
427 IN FDC_BLK_IO_DEV
*FdcBlkIoDev
,
434 Command
= SENSE_INT_STATUS_CMD
;
436 if (DataOutByte (FdcBlkIoDev
, &Command
) != EFI_SUCCESS
) {
437 return EFI_DEVICE_ERROR
;
440 if (DataInByte (FdcBlkIoDev
, Sts0
) != EFI_SUCCESS
) {
441 return EFI_DEVICE_ERROR
;
444 if (DataInByte (FdcBlkIoDev
, Pcn
) != EFI_SUCCESS
) {
445 return EFI_DEVICE_ERROR
;
452 Issue Specify command.
454 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
456 @retval EFI_SUCCESS Specify command successfully issued.
457 @retval EFI_DEVICE_ERROR FDC device has errors.
462 IN FDC_BLK_IO_DEV
*FdcBlkIoDev
465 FDC_SPECIFY_CMD Command
;
469 ZeroMem (&Command
, sizeof (FDC_SPECIFY_CMD
));
470 Command
.CommandCode
= SPECIFY_CMD
;
474 Command
.SrtHut
= 0xdf;
479 Command
.HltNd
= 0x02;
481 Pointer
= (UINT8
*) (&Command
);
482 for (Index
= 0; Index
< sizeof (FDC_SPECIFY_CMD
); Index
++) {
483 if (DataOutByte (FdcBlkIoDev
, Pointer
++) != EFI_SUCCESS
) {
484 return EFI_DEVICE_ERROR
;
492 Wait until busy bit is cleared.
494 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
495 @param DevPos Position of FDC (Driver A or B)
496 @param TimeoutInMseconds Timeout value to wait.
498 @retval EFI_SUCCESS Busy bit has been cleared before timeout.
499 @retval EFI_TIMEOUT Time goes out before busy bit is cleared.
504 IN FDC_BLK_IO_DEV
*FdcBlkIoDev
,
506 IN UINTN TimeoutInMseconds
510 UINT8 StatusRegister
;
514 // How to determine drive and command are busy or not: by the bits of Main Status Register
515 // bit0: Drive 0 busy (drive A)
516 // bit1: Drive 1 busy (drive B)
517 // bit4: Command busy
519 // set mask: for drive A set bit0 & bit4; for drive B set bit1 & bit4
521 Mask
= (UINT8
) ((DevPos
== 0 ? MSR_DAB
: MSR_DBB
) | MSR_CB
);
523 Delay
= ((TimeoutInMseconds
* STALL_1_MSECOND
) / FDC_CHECK_INTERVAL
) + 1;
526 StatusRegister
= IoRead8 ((UINT16
) (PcdGet16 (PcdFdcBaseAddress
) + FDC_REGISTER_MSR
));
528 if ((StatusRegister
& Mask
) == 0x00) {
535 MicroSecondDelay (FDC_SHORT_DELAY
);
536 } while (--Delay
> 0);
548 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV
549 @param DevPos Index of FDC device.
551 @retval EFI_SUCCESS FDC device successfully reset.
552 @retval EFI_DEVICE_ERROR Fail to reset FDC device.
557 IN FDC_BLK_IO_DEV
*FdcBlkIoDev
,
567 // Reset specified Floppy Logic Drive according to Fdd -> Disk
568 // Set Digital Output Register(DOR) to do reset work
569 // bit0 & bit1 of DOR : Drive Select
571 // bit3 : DMA and Int bit
572 // Reset : A "0" written to bit2 resets the FDC, this reset will remain active until
573 // a "1" is written to this bit.
575 // use bit0 & bit1 to select the logic drive
579 Data
= (UINT8
) (Data
| (SELECT_DRV
& DevPos
));
580 IoWrite8 ((UINT16
) (PcdGet16 (PcdFdcBaseAddress
) + FDC_REGISTER_DOR
), Data
);
583 // Wait some time, at least 120us.
585 MicroSecondDelay (FDC_RESET_DELAY
);
589 // write "1" to bit3 : enable DMA
592 IoWrite8 ((UINT16
) (PcdGet16 (PcdFdcBaseAddress
) + FDC_REGISTER_DOR
), Data
);
594 MicroSecondDelay (FDC_RESET_DELAY
);
597 // Wait until specified floppy logic drive is not busy
599 if (FdcWaitForBSYClear (FdcBlkIoDev
, DevPos
, 1) != EFI_SUCCESS
) {
600 return EFI_DEVICE_ERROR
;
603 // Set the Transfer Data Rate
605 IoWrite8 ((UINT16
) (PcdGet16 (PcdFdcBaseAddress
) + FDC_REGISTER_CCR
), 0x0);
607 MicroSecondDelay (FDC_MEDIUM_DELAY
);
610 // Issue Sense interrupt command for each drive (totally 4 drives)
612 for (Index
= 0; Index
< 4; Index
++) {
613 if (SenseIntStatus (FdcBlkIoDev
, &Sts0
, &Pcn
) != EFI_SUCCESS
) {
614 return EFI_DEVICE_ERROR
;
618 // Issue Specify command
620 if (Specify (FdcBlkIoDev
) != EFI_SUCCESS
) {
621 return EFI_DEVICE_ERROR
;
628 Turn on the motor of floppy drive.
630 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
631 @param Info Information of floppy device.
633 @retval EFI_SUCCESS Motor is successfully turned on.
634 @retval EFI_SUCCESS Motor is already on.
635 @retval EFI_DEVICE_ERROR Busy bit of FDC cannot be cleared.
640 IN FDC_BLK_IO_DEV
*FdcBlkIoDev
,
641 IN OUT PEI_FLOPPY_DEVICE_INFO
*Info
648 // Control of the floppy drive motors is a big pain. If motor is off, you have to turn it
649 // on first. But you can not leave the motor on all the time, since that would wear out the
650 // disk. On the other hand, if you turn the motor off after each operation, the system performance
651 // will be awful. The compromise used in this driver is to leave the motor on for 2 seconds after
652 // each operation. If a new operation is started in that interval(2s), the motor need not be
653 // turned on again. If no new operation is started, a timer goes off and the motor is turned off.
655 DevPos
= Info
->DevPos
;
658 // If the Motor is already on, just return EFI_SUCCESS.
664 // The drive's motor is off, so need turn it on.
665 // First check if command and drive are busy or not.
667 if (FdcWaitForBSYClear (FdcBlkIoDev
, DevPos
, 1) != EFI_SUCCESS
) {
668 return EFI_DEVICE_ERROR
;
671 // for drive A: 1CH, drive B: 2DH
674 Data
= (UINT8
) (Data
| (SELECT_DRV
& DevPos
));
676 Data
|= DRVA_MOTOR_ON
;
678 Data
|= DRVB_MOTOR_ON
;
681 Info
->MotorOn
= FALSE
;
684 // Turn on the motor and wait for some time to ensure it takes effect.
686 IoWrite8 ((UINT16
) (PcdGet16 (PcdFdcBaseAddress
) + FDC_REGISTER_DOR
), Data
);
687 MicroSecondDelay (FDC_LONG_DELAY
);
689 Info
->MotorOn
= TRUE
;
695 Turn off the motor of floppy drive.
697 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
698 @param Info Information of floppy device.
703 IN FDC_BLK_IO_DEV
*FdcBlkIoDev
,
704 IN OUT PEI_FLOPPY_DEVICE_INFO
*Info
710 DevPos
= Info
->DevPos
;
712 if (!Info
->MotorOn
) {
716 // The motor is on, so need motor off
719 Data
= (UINT8
) (Data
| (SELECT_DRV
& DevPos
));
721 IoWrite8 ((UINT16
) (PcdGet16 (PcdFdcBaseAddress
) + FDC_REGISTER_DOR
), Data
);
722 MicroSecondDelay (FDC_SHORT_DELAY
);
724 Info
->MotorOn
= FALSE
;
728 Recalibrate the FDC device.
730 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
731 @param Info Information of floppy device.
733 @retval EFI_SUCCESS FDC successfully recalibrated.
734 @retval EFI_DEVICE_ERROR Fail to send RECALIBRATE_CMD.
735 @retval EFI_DEVICE_ERROR Fail to get status value of Sts0 and Pcn.
736 @retval EFI_DEVICE_ERROR Fail to recalibrate FDC device.
741 IN FDC_BLK_IO_DEV
*FdcBlkIoDev
,
742 IN OUT PEI_FLOPPY_DEVICE_INFO
*Info
745 FDC_COMMAND_PACKET2 Command
;
753 DevPos
= Info
->DevPos
;
756 // We would try twice.
760 ZeroMem (&Command
, sizeof (FDC_COMMAND_PACKET2
));
761 Command
.CommandCode
= RECALIBRATE_CMD
;
766 Command
.DiskHeadSel
= 0;
768 Command
.DiskHeadSel
= 1;
771 Pointer
= (UINT8
*) (&Command
);
772 for (Index
= 0; Index
< sizeof (FDC_COMMAND_PACKET2
); Index
++) {
773 if (DataOutByte (FdcBlkIoDev
, Pointer
++) != EFI_SUCCESS
) {
774 return EFI_DEVICE_ERROR
;
778 MicroSecondDelay (FDC_RECALIBRATE_DELAY
);
780 if (SenseIntStatus (FdcBlkIoDev
, &Sts0
, &Pcn
) != EFI_SUCCESS
) {
781 return EFI_DEVICE_ERROR
;
784 if ((Sts0
& 0xf0) == BIT5
&& Pcn
== 0) {
786 // Recalibration is successful.
789 Info
->NeedRecalibrate
= FALSE
;
794 // Recalibration is not successful. Try again.
795 // If trial is used out, return EFI_DEVICE_ERROR.
799 return EFI_DEVICE_ERROR
;
808 Seek for the cylinder according to given LBA.
810 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
811 @param Info Information of floppy device.
812 @param Lba LBA for which to seek for cylinder.
814 @retval EFI_SUCCESS Successfully moved to the destination cylinder.
815 @retval EFI_SUCCESS Destination cylinder is just the present cylinder.
816 @retval EFI_DEVICE_ERROR Fail to move to the destination cylinder.
821 IN FDC_BLK_IO_DEV
*FdcBlkIoDev
,
822 IN OUT PEI_FLOPPY_DEVICE_INFO
*Info
,
826 FDC_SEEK_CMD Command
;
827 DISKET_PARA_TABLE
*Para
;
838 DevPos
= Info
->DevPos
;
839 if (Info
->NeedRecalibrate
) {
840 if (Recalibrate (FdcBlkIoDev
, Info
) != EFI_SUCCESS
) {
841 return EFI_DEVICE_ERROR
;
844 // Recalibrate Success
846 Info
->NeedRecalibrate
= FALSE
;
850 // Get the base of disk parameter information corresponding to its type.
852 Para
= (DISKET_PARA_TABLE
*) ((UINT8
*) DiskPara
+ sizeof (DISKET_PARA_TABLE
) * Info
->Type
);
853 EndOfTrack
= Para
->EndOfTrack
;
855 // Calculate cylinder based on Lba and EOT
857 Cylinder
= (UINT8
) ((UINTN
) Lba
/ EndOfTrack
/ 2);
860 // If the dest cylinder is the present cylinder, unnecessary to do the seek operation
862 if (Info
->Pcn
== Cylinder
) {
867 // Calculate the head : 0 or 1
869 Head
= (UINT8
) ((UINTN
) Lba
/ EndOfTrack
% 2);
871 ZeroMem (&Command
, sizeof (FDC_SEEK_CMD
));
872 Command
.CommandCode
= SEEK_CMD
;
874 Command
.DiskHeadSel
= 0;
876 Command
.DiskHeadSel
= 1;
880 // Send command to move to destination cylinder.
882 Command
.DiskHeadSel
= (UINT8
) (Command
.DiskHeadSel
| (Head
<< 2));
883 Command
.NewCylinder
= Cylinder
;
885 Pointer
= (UINT8
*) (&Command
);
886 for (Index
= 0; Index
< sizeof (FDC_SEEK_CMD
); Index
++) {
887 if (DataOutByte (FdcBlkIoDev
, Pointer
++) != EFI_SUCCESS
) {
888 return EFI_DEVICE_ERROR
;
892 MicroSecondDelay (FDC_SHORT_DELAY
);
895 // Calculate waiting time, which is proportional to the gap between destination
896 // cylinder and present cylinder.
898 if (Info
->Pcn
> Cylinder
) {
899 Gap
= (UINT8
) (Info
->Pcn
- Cylinder
);
901 Gap
= (UINT8
) (Cylinder
- Info
->Pcn
);
904 MicroSecondDelay ((Gap
+ 1) * FDC_LONG_DELAY
);
907 // Confirm if the new cylinder is the destination and status is correct.
909 if (SenseIntStatus (FdcBlkIoDev
, &Sts0
, &Pcn
) != EFI_SUCCESS
) {
910 return EFI_DEVICE_ERROR
;
913 if ((Sts0
& 0xf0) == BIT5
) {
914 Info
->Pcn
= Command
.NewCylinder
;
915 Info
->NeedRecalibrate
= FALSE
;
918 Info
->NeedRecalibrate
= TRUE
;
919 return EFI_DEVICE_ERROR
;
924 Check if diskette is changed.
926 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV
927 @param Info Information of floppy device.
929 @retval EFI_SUCCESS Diskette is not changed.
930 @retval EFI_MEDIA_CHANGED Diskette is changed.
931 @retval EFI_NO_MEDIA No diskette.
932 @retval EFI_DEVICE_ERROR Fail to do the seek or recalibrate operation.
937 IN FDC_BLK_IO_DEV
*FdcBlkIoDev
,
938 IN OUT PEI_FLOPPY_DEVICE_INFO
*Info
947 Data
= IoRead8 ((UINT16
) (PcdGet16 (PcdFdcBaseAddress
) + FDC_REGISTER_DIR
));
949 MicroSecondDelay (FDC_SHORT_DELAY
);
951 if ((Data
& DIR_DCL
) == DIR_DCL
) {
952 if (Info
->Pcn
!= 0) {
953 Status
= Recalibrate (FdcBlkIoDev
, Info
);
955 Status
= Seek (FdcBlkIoDev
, Info
, 0x30);
958 if (Status
!= EFI_SUCCESS
) {
960 // Fail to do the seek or recalibrate operation
962 return EFI_DEVICE_ERROR
;
965 Data
= IoRead8 ((UINT16
) (PcdGet16 (PcdFdcBaseAddress
) + FDC_REGISTER_DIR
));
967 MicroSecondDelay (FDC_SHORT_DELAY
);
969 if ((Data
& DIR_DCL
) == DIR_DCL
) {
973 return EFI_MEDIA_CHANGED
;
980 Detects if FDC device exists.
982 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV
983 @param Info Information of floppy device.
984 @param MediaInfo Information of floppy media.
986 @retval TRUE FDC device exists and is working properly.
987 @retval FALSE FDC device does not exist or cannot work properly.
992 IN FDC_BLK_IO_DEV
*FdcBlkIoDev
,
993 IN OUT PEI_FLOPPY_DEVICE_INFO
*Info
,
994 OUT EFI_PEI_BLOCK_IO_MEDIA
*MediaInfo
998 DISKET_PARA_TABLE
*Para
;
1000 Status
= MotorOn (FdcBlkIoDev
, Info
);
1001 if (Status
!= EFI_SUCCESS
) {
1005 Status
= Recalibrate (FdcBlkIoDev
, Info
);
1007 if (Status
!= EFI_SUCCESS
) {
1008 MotorOff (FdcBlkIoDev
, Info
);
1012 // Set Media Parameter
1014 MediaInfo
->DeviceType
= LegacyFloppy
;
1015 MediaInfo
->MediaPresent
= TRUE
;
1020 Status
= DisketChanged (FdcBlkIoDev
, Info
);
1024 // No diskette in floppy.
1026 MediaInfo
->MediaPresent
= FALSE
;
1029 case EFI_MEDIA_CHANGED
:
1032 // Diskette exists in floppy.
1040 MotorOff (FdcBlkIoDev
, Info
);
1044 MotorOff (FdcBlkIoDev
, Info
);
1047 // Get the base of disk parameter information corresponding to its type.
1049 Para
= (DISKET_PARA_TABLE
*) ((UINT8
*) DiskPara
+ sizeof (DISKET_PARA_TABLE
) * Info
->Type
);
1051 MediaInfo
->BlockSize
= BytePerSector
[Para
->Number
];
1052 MediaInfo
->LastBlock
= Para
->EndOfTrack
* 2 * (Para
->MaxTrackNum
+ 1) - 1;
1058 Enumerate floppy device
1060 @param FdcBlkIoDev Instance of floppy device controller
1062 @return Number of FDC devices.
1067 IN FDC_BLK_IO_DEV
*FdcBlkIoDev
1072 EFI_PEI_BLOCK_IO_MEDIA MediaInfo
;
1078 // DevPos=0 means Drive A, 1 means Drive B.
1080 for (DevPos
= 0; DevPos
< 2; DevPos
++) {
1082 // Detecting device presence
1084 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, EFI_PERIPHERAL_REMOVABLE_MEDIA
+ EFI_P_PC_PRESENCE_DETECT
);
1089 Status
= FdcReset (FdcBlkIoDev
, DevPos
);
1091 if (EFI_ERROR (Status
)) {
1095 FdcBlkIoDev
->DeviceInfo
[DevPos
].DevPos
= DevPos
;
1096 FdcBlkIoDev
->DeviceInfo
[DevPos
].Pcn
= 0;
1097 FdcBlkIoDev
->DeviceInfo
[DevPos
].MotorOn
= FALSE
;
1098 FdcBlkIoDev
->DeviceInfo
[DevPos
].NeedRecalibrate
= TRUE
;
1099 FdcBlkIoDev
->DeviceInfo
[DevPos
].Type
= FdcType1440K1440K
;
1102 // Discover FDC device
1104 if (DiscoverFdcDevice (FdcBlkIoDev
, &(FdcBlkIoDev
->DeviceInfo
[DevPos
]), &MediaInfo
)) {
1105 FdcBlkIoDev
->DeviceInfo
[DevNo
].DevPos
= DevPos
;
1107 FdcBlkIoDev
->DeviceInfo
[DevNo
].Pcn
= FdcBlkIoDev
->DeviceInfo
[DevPos
].Pcn
;
1108 FdcBlkIoDev
->DeviceInfo
[DevNo
].MotorOn
= FdcBlkIoDev
->DeviceInfo
[DevPos
].MotorOn
;
1109 FdcBlkIoDev
->DeviceInfo
[DevNo
].NeedRecalibrate
= FdcBlkIoDev
->DeviceInfo
[DevPos
].NeedRecalibrate
;
1110 FdcBlkIoDev
->DeviceInfo
[DevNo
].Type
= FdcBlkIoDev
->DeviceInfo
[DevPos
].Type
;
1113 &(FdcBlkIoDev
->DeviceInfo
[DevNo
].MediaInfo
),
1115 sizeof (EFI_PEI_BLOCK_IO_MEDIA
)
1121 // Assume controller error
1123 REPORT_STATUS_CODE (
1124 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
1125 EFI_PERIPHERAL_REMOVABLE_MEDIA
+ EFI_P_EC_CONTROLLER_ERROR
1130 FdcBlkIoDev
->DeviceCount
= DevNo
;
1135 Checks result reflected by FDC_RESULT_PACKET.
1137 @param Result FDC_RESULT_PACKET read from FDC after certain operation.
1138 @param Info Information of floppy device.
1140 @retval EFI_SUCCESS Result is healthy.
1141 @retval EFI_DEVICE_ERROR Result is not healthy.
1146 IN FDC_RESULT_PACKET
*Result
,
1147 OUT PEI_FLOPPY_DEVICE_INFO
*Info
1150 if ((Result
->Status0
& STS0_IC
) != IC_NT
) {
1151 if ((Result
->Status0
& STS0_SE
) == BIT5
) {
1155 Info
->NeedRecalibrate
= TRUE
;
1158 Info
->NeedRecalibrate
= TRUE
;
1159 return EFI_DEVICE_ERROR
;
1162 // Check Status Register1
1164 if ((Result
->Status1
& (STS1_EN
| STS1_DE
| STS1_OR
| STS1_ND
| STS1_NW
| STS1_MA
)) != 0) {
1165 Info
->NeedRecalibrate
= TRUE
;
1166 return EFI_DEVICE_ERROR
;
1169 // Check Status Register2
1171 if ((Result
->Status2
& (STS2_CM
| STS2_DD
| STS2_WC
| STS2_BC
| STS2_MD
)) != 0) {
1172 Info
->NeedRecalibrate
= TRUE
;
1173 return EFI_DEVICE_ERROR
;
1180 Fill parameters for command packet.
1182 @param Info Information of floppy device.
1183 @param Lba Logical block address.
1184 @param Command Command for which for fill parameters.
1189 IN PEI_FLOPPY_DEVICE_INFO
*Info
,
1191 OUT FDC_COMMAND_PACKET1
*Command
1194 DISKET_PARA_TABLE
*Para
;
1198 DevPos
= Info
->DevPos
;
1201 // Get the base of disk parameter information corresponding to its type.
1203 Para
= (DISKET_PARA_TABLE
*) ((UINT8
*) DiskPara
+ sizeof (DISKET_PARA_TABLE
) * Info
->Type
);
1205 EndOfTrack
= Para
->EndOfTrack
;
1208 Command
->DiskHeadSel
= 0;
1210 Command
->DiskHeadSel
= 1;
1213 Command
->Cylinder
= (UINT8
) ((UINTN
) Lba
/ EndOfTrack
/ 2);
1214 Command
->Head
= (UINT8
) ((UINTN
) Lba
/ EndOfTrack
% 2);
1215 Command
->Sector
= (UINT8
) ((UINT8
) ((UINTN
) Lba
% EndOfTrack
) + 1);
1216 Command
->DiskHeadSel
= (UINT8
) (Command
->DiskHeadSel
| (Command
->Head
<< 2));
1217 Command
->Number
= Para
->Number
;
1218 Command
->EndOfTrack
= Para
->EndOfTrack
;
1219 Command
->GapLength
= Para
->GapLength
;
1220 Command
->DataLength
= Para
->DataLength
;
1224 Setup specifed FDC device.
1226 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
1227 @param DevPos Index of FDC device.
1229 @retval EFI_SUCCESS FDC device successfully set up.
1230 @retval EFI_DEVICE_ERROR FDC device has errors.
1235 IN FDC_BLK_IO_DEV
*FdcBlkIoDev
,
1241 IoWrite8 ((UINT16
) (PcdGet16 (PcdFdcBaseAddress
) + FDC_REGISTER_CCR
), 0x0);
1243 MicroSecondDelay (FDC_MEDIUM_DELAY
);
1245 Status
= Specify (FdcBlkIoDev
);
1250 Setup DMA channels to read data.
1252 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
1253 @param Buffer Memory buffer for DMA transfer.
1254 @param BlockSize the number of the bytes in one block.
1255 @param NumberOfBlocks Number of blocks to read.
1260 IN FDC_BLK_IO_DEV
*FdcBlkIoDev
,
1263 IN UINTN NumberOfBlocks
1270 // Mask DMA channel 2;
1272 IoWrite8 (R_8237_DMA_WRSMSK_CH0_3
, B_8237_DMA_WRSMSK_CMS
| 2);
1275 // Clear first/last flip flop
1277 IoWrite8 (R_8237_DMA_CBPR_CH0_3
, B_8237_DMA_WRSMSK_CMS
| 2);
1282 IoWrite8 (R_8237_DMA_CHMODE_CH0_3
, V_8237_DMA_CHMODE_SINGLE
| V_8237_DMA_CHMODE_IO2MEM
| 2);
1285 // Set base address and page register
1287 Data
= (UINT8
) (UINTN
) Buffer
;
1288 IoWrite8 (R_8237_DMA_BASE_CA_CH2
, Data
);
1289 Data
= (UINT8
) ((UINTN
) Buffer
>> 8);
1290 IoWrite8 (R_8237_DMA_BASE_CA_CH2
, Data
);
1292 Data
= (UINT8
) ((UINTN
) Buffer
>> 16);
1293 IoWrite8 (R_8237_DMA_MEM_LP_CH2
, Data
);
1296 // Set count register
1298 Count
= BlockSize
* NumberOfBlocks
- 1;
1299 Data
= (UINT8
) (Count
& 0xff);
1300 IoWrite8 (R_8237_DMA_BASE_CC_CH2
, Data
);
1301 Data
= (UINT8
) (Count
>> 8);
1302 IoWrite8 (R_8237_DMA_BASE_CC_CH2
, Data
);
1305 // Clear channel 2 mask
1307 IoWrite8 (R_8237_DMA_WRSMSK_CH0_3
, 0x02);
1312 According to the block range specified by Lba and NumberOfBlocks, calculate
1313 the number of blocks in the same sector, which can be transferred in a batch.
1315 @param Info Information of floppy device.
1316 @param Lba Start address of block range.
1317 @param NumberOfBlocks Number of blocks of the range.
1319 @return Number of blocks in the same sector.
1323 GetTransferBlockCount (
1324 IN PEI_FLOPPY_DEVICE_INFO
*Info
,
1326 IN UINTN NumberOfBlocks
1329 DISKET_PARA_TABLE
*Para
;
1332 UINT8 SectorsInTrack
;
1335 // Get the base of disk parameter information corresponding to its type.
1337 Para
= (DISKET_PARA_TABLE
*) ((UINT8
*) DiskPara
+ sizeof (DISKET_PARA_TABLE
) * Info
->Type
);
1339 EndOfTrack
= Para
->EndOfTrack
;
1340 Head
= (UINT8
) ((UINTN
) Lba
/ EndOfTrack
% 2);
1342 SectorsInTrack
= (UINT8
) (EndOfTrack
* (2 - Head
) - (UINT8
) ((UINTN
) Lba
% EndOfTrack
));
1343 if (SectorsInTrack
< NumberOfBlocks
) {
1345 // Not all the block range locates in the same sector
1347 return SectorsInTrack
;
1350 // All the block range is in the same sector.
1352 return NumberOfBlocks
;
1357 Read data sector from FDC device.
1359 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
1360 @param Info Information of floppy device.
1361 @param Buffer Buffer to setup for DMA.
1362 @param Lba The start address to read.
1363 @param NumberOfBlocks Number of blocks to read.
1365 @retval EFI_SUCCESS Data successfully read out.
1366 @retval EFI_DEVICE_ERROR FDC device has errors.
1367 @retval EFI_TIMEOUT Command does not take effect in time.
1372 IN FDC_BLK_IO_DEV
*FdcBlkIoDev
,
1373 IN OUT PEI_FLOPPY_DEVICE_INFO
*Info
,
1376 IN UINTN NumberOfBlocks
1380 FDC_COMMAND_PACKET1 Command
;
1381 FDC_RESULT_PACKET Result
;
1386 Status
= Seek (FdcBlkIoDev
, Info
, Lba
);
1387 if (Status
!= EFI_SUCCESS
) {
1388 return EFI_DEVICE_ERROR
;
1394 SetDMA (FdcBlkIoDev
, Buffer
, Info
->MediaInfo
.BlockSize
, NumberOfBlocks
);
1397 // Allocate Read command packet
1399 ZeroMem (&Command
, sizeof (FDC_COMMAND_PACKET1
));
1400 Command
.CommandCode
= READ_DATA_CMD
| CMD_MT
| CMD_MFM
| CMD_SK
;
1403 // Fill parameters for command.
1405 FillPara (Info
, Lba
, &Command
);
1408 // Write command bytes to FDC
1410 Pointer
= (UINT8
*) (&Command
);
1411 for (Index
= 0; Index
< sizeof (FDC_COMMAND_PACKET1
); Index
++) {
1412 if (DataOutByte (FdcBlkIoDev
, Pointer
++) != EFI_SUCCESS
) {
1413 return EFI_DEVICE_ERROR
;
1418 // Wait for some time until command takes effect.
1420 Times
= (STALL_1_SECOND
/ FDC_CHECK_INTERVAL
) + 1;
1422 if ((IoRead8 ((UINT16
) (PcdGet16 (PcdFdcBaseAddress
) + FDC_REGISTER_MSR
)) & 0xc0) == 0xc0) {
1426 MicroSecondDelay (FDC_SHORT_DELAY
);
1427 } while (--Times
> 0);
1431 // Command fails to take effect in time, return EFI_TIMEOUT.
1437 // Read result bytes from FDC
1439 Pointer
= (UINT8
*) (&Result
);
1440 for (Index
= 0; Index
< sizeof (FDC_RESULT_PACKET
); Index
++) {
1441 if (DataInByte (FdcBlkIoDev
, Pointer
++) != EFI_SUCCESS
) {
1442 return EFI_DEVICE_ERROR
;
1446 return CheckResult (&Result
, Info
);
1450 Gets the count of block I/O devices that one specific block driver detects.
1452 This function is used for getting the count of block I/O devices that one
1453 specific block driver detects. To the PEI ATAPI driver, it returns the number
1454 of all the detected ATAPI devices it detects during the enumeration process.
1455 To the PEI legacy floppy driver, it returns the number of all the legacy
1456 devices it finds during its enumeration process. If no device is detected,
1457 then the function will return zero.
1459 @param[in] PeiServices General-purpose services that are available
1461 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI
1463 @param[out] NumberBlockDevices The number of block I/O devices discovered.
1465 @retval EFI_SUCCESS Operation performed successfully.
1470 FdcGetNumberOfBlockDevices (
1471 IN EFI_PEI_SERVICES
**PeiServices
,
1472 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
1473 OUT UINTN
*NumberBlockDevices
1476 FDC_BLK_IO_DEV
*FdcBlkIoDev
;
1480 FdcBlkIoDev
= PEI_RECOVERY_FDC_FROM_BLKIO_THIS (This
);
1482 *NumberBlockDevices
= FdcBlkIoDev
->DeviceCount
;
1488 Gets a block device's media information.
1490 This function will provide the caller with the specified block device's media
1491 information. If the media changes, calling this function will update the media
1492 information accordingly.
1494 @param[in] PeiServices General-purpose services that are available to every
1496 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
1497 @param[in] DeviceIndex Specifies the block device to which the function wants
1498 to talk. Because the driver that implements Block I/O
1499 PPIs will manage multiple block devices, the PPIs that
1500 want to talk to a single device must specify the
1501 device index that was assigned during the enumeration
1502 process. This index is a number from one to
1504 @param[out] MediaInfo The media information of the specified block media.
1505 The caller is responsible for the ownership of this
1508 @retval EFI_SUCCESS Media information about the specified block device
1509 was obtained successfully.
1510 @retval EFI_DEVICE_ERROR Cannot get the media information due to a hardware
1512 @retval Others Other failure occurs.
1517 FdcGetBlockDeviceMediaInfo (
1518 IN EFI_PEI_SERVICES
**PeiServices
,
1519 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
1520 IN UINTN DeviceIndex
,
1521 OUT EFI_PEI_BLOCK_IO_MEDIA
*MediaInfo
1525 FDC_BLK_IO_DEV
*FdcBlkIoDev
;
1531 if (This
== NULL
|| MediaInfo
== NULL
) {
1532 return EFI_INVALID_PARAMETER
;
1535 FdcBlkIoDev
= PEI_RECOVERY_FDC_FROM_BLKIO_THIS (This
);
1537 DeviceCount
= FdcBlkIoDev
->DeviceCount
;
1540 // DeviceIndex is a value from 1 to NumberBlockDevices.
1542 if ((DeviceIndex
< 1) || (DeviceIndex
> DeviceCount
) || (DeviceIndex
> 2)) {
1543 return EFI_INVALID_PARAMETER
;
1546 Index
= DeviceIndex
- 1;
1548 // Probe media and retrieve latest media information
1550 Healthy
= DiscoverFdcDevice (
1552 &FdcBlkIoDev
->DeviceInfo
[Index
],
1557 return EFI_DEVICE_ERROR
;
1561 &(FdcBlkIoDev
->DeviceInfo
[Index
].MediaInfo
),
1563 sizeof (EFI_PEI_BLOCK_IO_MEDIA
)
1570 Reads the requested number of blocks from the specified block device.
1572 The function reads the requested number of blocks from the device. All the
1573 blocks are read, or an error is returned. If there is no media in the device,
1574 the function returns EFI_NO_MEDIA.
1576 @param[in] PeiServices General-purpose services that are available to
1578 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
1579 @param[in] DeviceIndex Specifies the block device to which the function wants
1580 to talk. Because the driver that implements Block I/O
1581 PPIs will manage multiple block devices, the PPIs that
1582 want to talk to a single device must specify the device
1583 index that was assigned during the enumeration process.
1584 This index is a number from one to NumberBlockDevices.
1585 @param[in] StartLBA The starting logical block address (LBA) to read from
1587 @param[in] BufferSize The size of the Buffer in bytes. This number must be
1588 a multiple of the intrinsic block size of the device.
1589 @param[out] Buffer A pointer to the destination buffer for the data.
1590 The caller is responsible for the ownership of the
1593 @retval EFI_SUCCESS The data was read correctly from the device.
1594 @retval EFI_DEVICE_ERROR The device reported an error while attempting
1595 to perform the read operation.
1596 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not
1597 valid, or the buffer is not properly aligned.
1598 @retval EFI_NO_MEDIA There is no media in the device.
1599 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of
1600 the intrinsic block size of the device.
1606 IN EFI_PEI_SERVICES
**PeiServices
,
1607 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
1608 IN UINTN DeviceIndex
,
1609 IN EFI_PEI_LBA StartLBA
,
1610 IN UINTN BufferSize
,
1614 EFI_PEI_BLOCK_IO_MEDIA MediaInfo
;
1617 UINTN NumberOfBlocks
;
1619 FDC_BLK_IO_DEV
*FdcBlkIoDev
;
1625 return EFI_INVALID_PARAMETER
;
1628 FdcBlkIoDev
= PEI_RECOVERY_FDC_FROM_BLKIO_THIS (This
);
1630 if (Buffer
== NULL
) {
1631 return EFI_INVALID_PARAMETER
;
1634 Status
= FdcGetBlockDeviceMediaInfo (PeiServices
, This
, DeviceIndex
, &MediaInfo
);
1635 if (Status
!= EFI_SUCCESS
) {
1636 return EFI_DEVICE_ERROR
;
1639 if (!MediaInfo
.MediaPresent
) {
1640 return EFI_NO_MEDIA
;
1643 BlockSize
= MediaInfo
.BlockSize
;
1646 // If BufferSize cannot be divided by block size of FDC device,
1647 // return EFI_BAD_BUFFER_SIZE.
1649 if (BufferSize
% BlockSize
!= 0) {
1650 return EFI_BAD_BUFFER_SIZE
;
1653 NumberOfBlocks
= BufferSize
/ BlockSize
;
1655 if ((StartLBA
+ NumberOfBlocks
- 1) > FdcBlkIoDev
->DeviceInfo
[DeviceIndex
- 1].MediaInfo
.LastBlock
) {
1656 return EFI_INVALID_PARAMETER
;
1659 MemPage
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1660 if ((MemPage
== NULL
) || ((UINTN
) MemPage
>= ISA_MAX_MEMORY_ADDRESS
)) {
1662 // If fail to allocate memory under ISA_MAX_MEMORY_ADDRESS, designate the address space for DMA
1664 MemPage
= (VOID
*) ((UINTN
) (UINT32
) 0x0f00000);
1666 Status
= MotorOn (FdcBlkIoDev
, &(FdcBlkIoDev
->DeviceInfo
[DeviceIndex
- 1]));
1667 if (Status
!= EFI_SUCCESS
) {
1668 return EFI_DEVICE_ERROR
;
1671 Status
= Setup (FdcBlkIoDev
, FdcBlkIoDev
->DeviceInfo
[DeviceIndex
- 1].DevPos
);
1672 if (Status
!= EFI_SUCCESS
) {
1673 MotorOff (FdcBlkIoDev
, &(FdcBlkIoDev
->DeviceInfo
[DeviceIndex
- 1]));
1674 return EFI_DEVICE_ERROR
;
1677 // Read data in batches.
1678 // Blocks in the same cylinder are read out in a batch.
1680 while ((Count
= GetTransferBlockCount (
1681 &(FdcBlkIoDev
->DeviceInfo
[DeviceIndex
- 1]),
1684 )) != 0 && Status
== EFI_SUCCESS
) {
1685 Status
= ReadDataSector (
1687 &(FdcBlkIoDev
->DeviceInfo
[DeviceIndex
- 1]),
1692 CopyMem (Buffer
, MemPage
, BlockSize
* Count
);
1694 NumberOfBlocks
-= Count
;
1695 Buffer
= (VOID
*) ((UINTN
) Buffer
+ Count
* BlockSize
);
1698 MotorOff (FdcBlkIoDev
, &(FdcBlkIoDev
->DeviceInfo
[DeviceIndex
- 1]));
1705 FdcReset (FdcBlkIoDev
, FdcBlkIoDev
->DeviceInfo
[DeviceIndex
- 1].DevPos
);
1706 return EFI_DEVICE_ERROR
;
1711 Initializes the floppy disk controller and installs FDC Block I/O PPI.
1713 @param FileHandle Handle of the file being invoked.
1714 @param PeiServices Describes the list of possible PEI Services.
1716 @retval EFI_SUCCESS Successfully initialized FDC and installed PPI.
1717 @retval EFI_NOT_FOUND Cannot find FDC device.
1718 @retval EFI_OUT_OF_RESOURCES Have no enough memory to create instance or descriptors.
1719 @retval Other Fail to install FDC Block I/O PPI.
1725 IN EFI_PEI_FILE_HANDLE FileHandle
,
1726 IN CONST EFI_PEI_SERVICES
**PeiServices
1730 FDC_BLK_IO_DEV
*FdcBlkIoDev
;
1734 Status
= PeiServicesRegisterForShadow (FileHandle
);
1735 if (!EFI_ERROR (Status
)) {
1740 // Allocate memory for instance of FDC_BLK_IO_DEV and copy initial value
1741 // from template to it.
1743 FdcBlkIoDev
= AllocatePages (EFI_SIZE_TO_PAGES(sizeof (FDC_BLK_IO_DEV
)));
1744 if (FdcBlkIoDev
== NULL
) {
1745 return EFI_OUT_OF_RESOURCES
;
1747 CopyMem (FdcBlkIoDev
, &mBlockIoDevTemplate
, sizeof (mBlockIoDevTemplate
));
1750 // Initialize DMA controller to enable all channels.
1752 for (Index
= 0; Index
< sizeof (mRegisterTable
) / sizeof (PEI_DMA_TABLE
); Index
++) {
1753 IoWrite8 (mRegisterTable
[Index
].Register
, mRegisterTable
[Index
].Value
);
1755 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, EFI_PERIPHERAL_REMOVABLE_MEDIA
+ EFI_P_PC_INIT
);
1758 // Enumerate FDC devices.
1760 DeviceCount
= FdcEnumeration (FdcBlkIoDev
);
1761 if (DeviceCount
== 0) {
1762 return EFI_NOT_FOUND
;
1765 FdcBlkIoDev
->PpiDescriptor
.Ppi
= &FdcBlkIoDev
->FdcBlkIo
;
1767 return PeiServicesInstallPpi (&FdcBlkIoDev
->PpiDescriptor
);