3 MMC/SD transfer specific functions
5 Copyright (c) 2013-2015 Intel Corporation.
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "SDMediaDevice.h"
14 Check card status, print the debug info and check the error
16 @param Status Status got from card status register.
19 @retval EFI_DEVICE_ERROR
27 CARD_STATUS
*CardStatus
;
28 CardStatus
= (CARD_STATUS
*)(&Status
);
30 if (CardStatus
->ADDRESS_OUT_OF_RANGE
) {
31 DEBUG ((EFI_D_ERROR
, "CardStatus: ADDRESS_OUT_OF_RANGE\n"));
34 if (CardStatus
->ADDRESS_MISALIGN
) {
35 DEBUG ((EFI_D_ERROR
, "CardStatus: ADDRESS_MISALIGN\n"));
38 if (CardStatus
->BLOCK_LEN_ERROR
) {
39 DEBUG ((EFI_D_ERROR
, "CardStatus: BLOCK_LEN_ERROR\n"));
42 if (CardStatus
->ERASE_SEQ_ERROR
) {
43 DEBUG ((EFI_D_ERROR
, "CardStatus: ERASE_SEQ_ERROR\n"));
46 if (CardStatus
->ERASE_PARAM
) {
47 DEBUG ((EFI_D_ERROR
, "CardStatus: ERASE_PARAM\n"));
50 if (CardStatus
->WP_VIOLATION
) {
51 DEBUG ((EFI_D_ERROR
, "CardStatus: WP_VIOLATION\n"));
54 if (CardStatus
->CARD_IS_LOCKED
) {
55 DEBUG ((EFI_D_ERROR
, "CardStatus: CARD_IS_LOCKED\n"));
58 if (CardStatus
->LOCK_UNLOCK_FAILED
) {
59 DEBUG ((EFI_D_ERROR
, "CardStatus: LOCK_UNLOCK_FAILED\n"));
62 if (CardStatus
->COM_CRC_ERROR
) {
63 DEBUG ((EFI_D_ERROR
, "CardStatus: COM_CRC_ERROR\n"));
66 if (CardStatus
->ILLEGAL_COMMAND
) {
67 DEBUG ((EFI_D_ERROR
, "CardStatus: ILLEGAL_COMMAND\n"));
70 if (CardStatus
->CARD_ECC_FAILED
) {
71 DEBUG ((EFI_D_ERROR
, "CardStatus: CARD_ECC_FAILED\n"));
74 if (CardStatus
->CC_ERROR
) {
75 DEBUG ((EFI_D_ERROR
, "CardStatus: CC_ERROR\n"));
78 if (CardStatus
->ERROR
) {
79 DEBUG ((EFI_D_ERROR
, "CardStatus: ERROR\n"));
82 if (CardStatus
->UNDERRUN
) {
83 DEBUG ((EFI_D_ERROR
, "CardStatus: UNDERRUN\n"));
86 if (CardStatus
->OVERRUN
) {
87 DEBUG ((EFI_D_ERROR
, "CardStatus: OVERRUN\n"));
90 if (CardStatus
->CID_CSD_OVERWRITE
) {
91 DEBUG ((EFI_D_ERROR
, "CardStatus: CID_CSD_OVERWRITE\n"));
94 if (CardStatus
->WP_ERASE_SKIP
) {
95 DEBUG ((EFI_D_ERROR
, "CardStatus: WP_ERASE_SKIP\n"));
98 if (CardStatus
->ERASE_RESET
) {
99 DEBUG ((EFI_D_ERROR
, "CardStatus: ERASE_RESET\n"));
102 if (CardStatus
->SWITCH_ERROR
) {
103 DEBUG ((EFI_D_ERROR
, "CardStatus: SWITCH_ERROR\n"));
106 if ((Status
& 0xFCFFA080) != 0) {
107 return EFI_DEVICE_ERROR
;
114 Send command by using Host IO protocol
116 @param This A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
117 @param CommandIndex The command index to set the command index field of command register.
118 @param Argument Command argument to set the argument field of command register.
119 @param DataType TRANSFER_TYPE, indicates no data, data in or data out.
120 @param Buffer Contains the data read from / write to the device.
121 @param BufferSize The size of the buffer.
122 @param ResponseType RESPONSE_TYPE.
123 @param TimeOut Time out value in 1 ms unit.
124 @param ResponseData Depending on the ResponseType, such as CSD or card status.
127 @retval EFI_INVALID_PARAMETER
128 @retval EFI_UNSUPPORTED
129 @retval EFI_DEVICE_ERROR
134 IN CARD_DATA
*CardData
,
135 IN UINT16 CommandIndex
,
137 IN TRANSFER_TYPE DataType
,
138 IN UINT8
*Buffer
, OPTIONAL
139 IN UINT32 BufferSize
,
140 IN RESPONSE_TYPE ResponseType
,
142 OUT UINT32
*ResponseData
147 EFI_SD_HOST_IO_PROTOCOL
*SDHostIo
;
148 SDHostIo
= CardData
->SDHostIo
;
149 if (CardData
->CardType
!= MMCCard
&& CardData
->CardType
!= MMCCardHighCap
) {
150 CommandIndex
|= AUTO_CMD12_ENABLE
;
153 Status
= SDHostIo
->SendCommand (
164 if (!EFI_ERROR (Status
)) {
165 if (ResponseType
== ResponseR1
|| ResponseType
== ResponseR1b
) {
166 ASSERT(ResponseData
!= NULL
);
167 Status
= CheckCardStatus (*ResponseData
);
170 SDHostIo
->ResetSDHost (SDHostIo
, Reset_DAT_CMD
);
177 Send the card APP_CMD command with the following command indicated by CommandIndex
179 @param CardData Pointer to CARD_DATA.
180 @param CommandIndex The command index to set the command index field of command register.
181 @param Argument Command argument to set the argument field of command register.
182 @param DataType TRANSFER_TYPE, indicates no data, data in or data out.
183 @param Buffer Contains the data read from / write to the device.
184 @param BufferSize The size of the buffer.
185 @param ResponseType RESPONSE_TYPE.
186 @param TimeOut Time out value in 1 ms unit.
187 @param ResponseData Depending on the ResponseType, such as CSD or card status.
190 @retval EFI_INVALID_PARAMETER
191 @retval EFI_UNSUPPORTED
192 @retval EFI_DEVICE_ERROR
197 IN CARD_DATA
*CardData
,
198 IN UINT16 CommandIndex
,
200 IN TRANSFER_TYPE DataType
,
201 IN UINT8
*Buffer
, OPTIONAL
202 IN UINT32 BufferSize
,
203 IN RESPONSE_TYPE ResponseType
,
205 OUT UINT32
*ResponseData
210 EFI_SD_HOST_IO_PROTOCOL
*SDHostIo
;
213 SDHostIo
= CardData
->SDHostIo
;
214 Status
= EFI_SUCCESS
;
216 for (Index
= 0; Index
< 2; Index
++) {
217 Status
= SDHostIo
->SendCommand (
220 (CardData
->Address
<< 16),
226 (UINT32
*)&(CardData
->CardStatus
)
228 if (!EFI_ERROR (Status
)) {
229 Status
= CheckCardStatus (*(UINT32
*)&(CardData
->CardStatus
));
230 if (CardData
->CardStatus
.SAPP_CMD
!= 1) {
231 Status
= EFI_DEVICE_ERROR
;
233 if (!EFI_ERROR (Status
)) {
237 SDHostIo
->ResetSDHost (SDHostIo
, Reset_Auto
);
241 if (EFI_ERROR (Status
)) {
244 if (CardData
->CardType
!= MMCCard
&& CardData
->CardType
!= MMCCardHighCap
) {
245 CommandIndex
|= AUTO_CMD12_ENABLE
;
248 Status
= SDHostIo
->SendCommand (
259 if (!EFI_ERROR (Status
)) {
260 if (ResponseType
== ResponseR1
|| ResponseType
== ResponseR1b
) {
261 ASSERT(ResponseData
!= NULL
);
262 Status
= CheckCardStatus (*ResponseData
);
265 SDHostIo
->ResetSDHost (SDHostIo
, Reset_Auto
);
273 Send the card FAST_IO command
275 @param CardData Pointer to CARD_DATA.
276 @param RegisterAddress Register Address.
277 @param RegisterData Pointer to register Data.
278 @param Write TRUE for write, FALSE for read.
281 @retval EFI_UNSUPPORTED
282 @retval EFI_INVALID_PARAMETER
283 @retval EFI_DEVICE_ERROR
288 IN CARD_DATA
*CardData
,
289 IN UINT8 RegisterAddress
,
290 IN OUT UINT8
*RegisterData
,
298 Status
= EFI_SUCCESS
;
300 if (RegisterData
== NULL
) {
301 Status
= EFI_INVALID_PARAMETER
;
305 Argument
= (CardData
->Address
<< 16) | (RegisterAddress
<< 8);
307 Argument
|= BIT15
| (*RegisterData
);
310 Status
= SendCommand (
321 if (EFI_ERROR (Status
)) {
325 if ((Data
& BIT15
) == 0) {
326 Status
= EFI_DEVICE_ERROR
;
331 *RegisterData
= (UINT8
)Data
;
339 Send the card GO_INACTIVE_STATE command.
341 @param CardData Pointer to CARD_DATA.
349 IN CARD_DATA
*CardData
355 Status
= SendCommand (
358 (CardData
->Address
<< 16),
372 Get card interested information for CSD rergister
374 @param CardData Pointer to CARD_DATA.
377 @retval EFI_UNSUPPORTED
378 @retval EFI_INVALID_PARAMETER
382 CaculateCardParameter (
383 IN CARD_DATA
*CardData
392 Status
= EFI_SUCCESS
;
394 switch (CardData
->CSDRegister
.TRAN_SPEED
& 0x7) {
396 Frequency
= 100 * 1000;
400 Frequency
= 1 * 1000 * 1000;
404 Frequency
= 10 * 1000 * 1000;
408 Frequency
= 100 * 1000 * 1000;
412 Status
= EFI_INVALID_PARAMETER
;
416 switch ((CardData
->CSDRegister
.TRAN_SPEED
>> 3) & 0xF) {
438 if (CardData
->CardType
== MMCCard
|| CardData
->CardType
== MMCCardHighCap
) {
462 if (CardData
->CardType
== MMCCard
|| CardData
->CardType
== MMCCardHighCap
) {
486 Status
= EFI_INVALID_PARAMETER
;
490 Frequency
= Frequency
* Multiple
/ 10;
491 CardData
->MaxFrequency
= Frequency
;
493 CardData
->BlockLen
= 1 << CardData
->CSDRegister
.READ_BL_LEN
;
495 if (CardData
->CardType
== SDMemoryCard2High
) {
496 ASSERT(CardData
->CSDRegister
.CSD_STRUCTURE
== 1);
497 CsdSDV2
= (CSD_SDV2
*)&CardData
->CSDRegister
;
499 // The SD Spec 2.0 says (CSize + 1) * 512K is the total size, so block numbber is (CSize + 1) * 1K
500 // the K here means 1024 not 1000
502 CardData
->BlockNumber
= DivU64x32 (MultU64x32 (CsdSDV2
->C_SIZE
+ 1, 512 * 1024) , CardData
->BlockLen
);
505 // For MMC card > 2G, the block number will be recaculate later
507 CSize
= CardData
->CSDRegister
.C_SIZELow2
| (CardData
->CSDRegister
.C_SIZEHigh10
<< 2);
508 CardData
->BlockNumber
= MultU64x32 (LShiftU64 (1, CardData
->CSDRegister
.C_SIZE_MULT
+ 2), CSize
+ 1);
512 //For >= 2G card, BlockLen may be 1024, but the transfer size is still 512 bytes
514 if (CardData
->BlockLen
> 512) {
515 CardData
->BlockNumber
= DivU64x32 (MultU64x32 (CardData
->BlockNumber
, CardData
->BlockLen
), 512);
516 CardData
->BlockLen
= 512;
521 "CalculateCardParameter: Card Size: 0x%lx\n", MultU64x32 (CardData
->BlockNumber
, CardData
->BlockLen
)
529 Test the bus width setting for MMC card.It is used only for verification purpose.
531 @param CardData Pointer to CARD_DATA.
532 @param Width 1, 4, 8 bits.
535 @retval EFI_UNSUPPORTED
536 @retval EFI_INVALID_PARAMETER
540 MMCCardBusWidthTest (
541 IN CARD_DATA
*CardData
,
549 ASSERT(CardData
!= NULL
);
568 Status
= EFI_INVALID_PARAMETER
;
572 CopyMem (CardData
->AlignedBuffer
, &Data
, Width
);
573 Status
= SendCommand (
578 CardData
->AlignedBuffer
,
582 (UINT32
*)&(CardData
->CardStatus
)
584 if (EFI_ERROR (Status
)) {
585 DEBUG((EFI_D_ERROR
, "MMCCardBusWidthTest:SendCommand BUSTEST_W 0x%x\n", *(UINT32
*)&(CardData
->CardStatus
)));
589 gBS
->Stall (10 * 1000);
593 Status
= SendCommand (
598 CardData
->AlignedBuffer
,
602 (UINT32
*)&(CardData
->CardStatus
)
604 if (EFI_ERROR (Status
)) {
605 DEBUG((EFI_D_ERROR
, "MMCCardBusWidthTest:SendCommand BUSTEST_R 0x%x\n", *(UINT32
*)&(CardData
->CardStatus
)));
608 CopyMem (&Data
, CardData
->AlignedBuffer
, Width
);
612 Value
= (~(Data
^ 0x80)) & 0xC0;
615 Value
= (~(Data
^ 0x5A)) & 0xFF;
618 Value
= (~(Data
^ 0xAA55)) & 0xFFFF;
623 Status
= EFI_SUCCESS
;
625 Status
= EFI_UNSUPPORTED
;
634 This function can detect these card types:
637 3. SD 2.0 standard card
638 3. SD 2.0 high capacity card
640 @param CardData Pointer to CARD_DATA.
648 IN CARD_DATA
*CardData
652 EFI_SD_HOST_IO_PROTOCOL
*SDHostIo
;
656 BOOLEAN SDCommand8Support
;
659 SDHostIo
= CardData
->SDHostIo
;
664 Status
= SendCommand (
675 if (EFI_ERROR (Status
)) {
676 DEBUG((EFI_D_ERROR
, "GO_IDLE_STATE Fail Status = 0x%x\n", Status
));
681 //No spec requirment, can be adjusted
683 gBS
->Stall (10 * 1000);
687 // Only 2.7V - 3.6V is supported for SD2.0, only SD 2.0 card can pass
688 // MMC and SD1.1 card will fail this command
690 Argument
= (VOLTAGE_27_36
<< 8) | CHECK_PATTERN
;
692 SDCommand8Support
= FALSE
;
694 Status
= SendCommand (
706 if (EFI_ERROR (Status
)) {
707 if (Status
!= EFI_TIMEOUT
) {
708 DEBUG((EFI_D_ERROR
, "SEND_IF_COND Fail, none time out error\n"));
712 if (ResponseData
!= Argument
) {
713 DEBUG((EFI_D_ERROR
, "SEND_IF_COND Fail, respond data does not match send data\n"));
714 Status
= EFI_DEVICE_ERROR
;
717 SDCommand8Support
= TRUE
;
722 if (SDHostIo
->HostCapability
.V30Support
== TRUE
) {
723 Argument
|= BIT17
| BIT18
;
724 } else if (SDHostIo
->HostCapability
.V33Support
== TRUE
) {
725 Argument
|= BIT20
| BIT21
;
728 if (SDCommand8Support
) {
730 //If command SD_SEND_OP_COND sucessed, it should be set.
731 // SD 1.1 card will ignore it
732 // SD 2.0 standard card will repsond with CCS 0, SD high capacity card will respond with CCS 1
733 // CCS is BIT30 of OCR
740 //Only SD card will respond to this command, and spec says the card only checks condition at first ACMD41 command
743 Status
= SendAppCommand (
752 (UINT32
*)&(CardData
->OCRRegister
)
754 if (EFI_ERROR (Status
)) {
755 if ((Status
== EFI_TIMEOUT
) && (!SDCommand8Support
)) {
756 CardData
->CardType
= MMCCard
;
757 Status
= EFI_SUCCESS
;
758 DEBUG((EFI_D_INFO
, "SD_SEND_OP_COND, MMC card was identified\n"));
761 // Not as expected, MMC card should has no response, which means timeout.
762 // SD card should pass this command
764 DEBUG((EFI_D_ERROR
, "SD_SEND_OP_COND Fail, check whether it is neither a MMC card nor a SD card\n"));
769 //Avoid waiting if sucess. Busy bit 0 means not ready
771 if (CardData
->OCRRegister
.Busy
== 1) {
775 gBS
->Stall (50 * 1000);
778 DEBUG((EFI_D_ERROR
, "Card is always in busy state\n"));
779 Status
= EFI_TIMEOUT
;
785 //Check supported voltage
788 if (SDHostIo
->HostCapability
.V30Support
== TRUE
) {
789 if ((CardData
->OCRRegister
.V270_V360
& BIT2
) == BIT2
) {
791 } else if ((CardData
->OCRRegister
.V270_V360
& BIT3
) == BIT3
) {
794 } else if (SDHostIo
->HostCapability
.V33Support
== TRUE
) {
795 if ((CardData
->OCRRegister
.V270_V360
& BIT5
) == BIT5
) {
797 } else if ((CardData
->OCRRegister
.V270_V360
& BIT6
) == BIT6
) {
804 //No matched support voltage
806 PutCardInactive (CardData
);
807 DEBUG((EFI_D_ERROR
, "No matched voltage for this card\n"));
808 Status
= EFI_UNSUPPORTED
;
812 CardData
->CardType
= SDMemoryCard
;
813 if (SDCommand8Support
== TRUE
) {
814 CardData
->CardType
= SDMemoryCard2
;
815 DEBUG((EFI_D_INFO
, "SD_SEND_OP_COND, SD 2.0 or above standard card was identified\n"));
818 if ((CardData
->OCRRegister
.AccessMode
& BIT1
) == BIT1
) {
819 CardData
->CardType
= SDMemoryCard2High
;
820 DEBUG((EFI_D_INFO
, "SD_SEND_OP_COND, SD 2.0 or above high capacity card was identified\n"));
830 MMC card high/low voltage selection function
832 @param CardData Pointer to CARD_DATA.
835 @retval EFI_INVALID_PARAMETER
836 @retval EFI_UNSUPPORTED
837 @retval EFI_BAD_BUFFER_SIZE
841 MMCCardVoltageSelection (
842 IN CARD_DATA
*CardData
849 Status
= EFI_SUCCESS
;
851 //First try the high voltage, then if supported choose the low voltage
854 for (Retry
= 0; Retry
< 3; Retry
++) {
856 // To bring back the normal MMC card to work
857 // after sending the SD command. Otherwise some
858 // card could not work
860 Status
= SendCommand (
871 if (EFI_ERROR (Status
)) {
872 DEBUG((EFI_D_ERROR
, "GO_IDLE_STATE Fail Status = 0x%x\n", Status
));
876 //CE-ATA device needs long delay
878 gBS
->Stall ((Retry
+ 1) * 50 * 1000);
881 //Get OCR register to check voltage support, first time the OCR is 0
883 Status
= SendCommand (
892 (UINT32
*)&(CardData
->OCRRegister
)
894 if (!EFI_ERROR (Status
)) {
900 DEBUG((EFI_D_ERROR
, "SEND_OP_COND Fail Status = 0x%x\n", Status
));
901 Status
= EFI_DEVICE_ERROR
;
906 //TimeOut Value, 5000 * 100 * 1000 = 5 s
911 Status
= SendCommand (
920 (UINT32
*)&(CardData
->OCRRegister
)
922 if (EFI_ERROR (Status
)) {
923 DEBUG((EFI_D_ERROR
, "SEND_OP_COND Fail Status = 0x%x\n", Status
));
927 gBS
->Stall (1 * 1000);
930 Status
= EFI_TIMEOUT
;
931 DEBUG((EFI_D_ERROR
, "Card is always in busy state\n"));
934 } while (CardData
->OCRRegister
.Busy
!= 1);
936 if (CardData
->OCRRegister
.AccessMode
== 2) // eMMC Card uses Sector Addressing - High Capacity
938 DEBUG((EFI_D_INFO
, "eMMC Card is High Capacity\n"));
939 CardData
->CardType
= MMCCardHighCap
;
948 This function set the bus and device width for MMC card
950 @param CardData Pointer to CARD_DATA.
951 @param Width 1, 4, 8 bits.
954 @retval EFI_UNSUPPORTED
955 @retval EFI_INVALID_PARAMETER
960 IN CARD_DATA
*CardData
,
962 IN BOOLEAN EnableDDRMode
966 EFI_SD_HOST_IO_PROTOCOL
*SDHostIo
;
967 SWITCH_ARGUMENT SwitchArgument
;
970 SDHostIo
= CardData
->SDHostIo
;
988 if (EnableDDRMode
) // Bus width 1 is not supported in ddr mode
989 return EFI_UNSUPPORTED
;
998 ZeroMem(&SwitchArgument
, sizeof (SWITCH_ARGUMENT
));
999 SwitchArgument
.CmdSet
= 0;
1000 SwitchArgument
.Value
= Value
;
1001 SwitchArgument
.Index
= (UINT32
)((UINTN
)
1002 (&(CardData
->ExtCSDRegister
.BUS_WIDTH
)) - (UINTN
)(&(CardData
->ExtCSDRegister
)));
1003 SwitchArgument
.Access
= WriteByte_Mode
;
1004 Status
= SendCommand (
1007 *(UINT32
*)&SwitchArgument
,
1013 (UINT32
*)&(CardData
->CardStatus
)
1015 if (!EFI_ERROR (Status
)) {
1016 Status
= SendCommand (
1019 (CardData
->Address
<< 16),
1025 (UINT32
*)&(CardData
->CardStatus
)
1027 if (EFI_ERROR (Status
)) {
1028 DEBUG((EFI_D_ERROR
, "SWITCH %d bits Fail\n", BusWidth
));
1031 DEBUG((EFI_D_ERROR
, "MMCCardSetBusWidth:SWITCH Card Status:0x%x\n", *(UINT32
*)&(CardData
->CardStatus
)));
1032 Status
= SDHostIo
->SetBusWidth (SDHostIo
, BusWidth
);
1033 if (EFI_ERROR (Status
)) {
1034 DEBUG((EFI_D_ERROR
, "SWITCH set %d bits Fail\n", BusWidth
));
1037 gBS
->Stall (5 * 1000);
1041 if (!EnableDDRMode
) { // CMD19 and CMD14 are illegal commands in ddr mode
1042 //if (EFI_ERROR (Status)) {
1043 // DEBUG((EFI_D_ERROR, "MMCCardBusWidthTest: Fail to enable high speed mode\n"));
1047 Status
= MMCCardBusWidthTest (CardData
, BusWidth
);
1048 if (EFI_ERROR (Status
)) {
1049 DEBUG((EFI_D_ERROR
, "MMCCardBusWidthTest %d bit Fail\n", BusWidth
));
1054 CardData
->CurrentBusWidth
= BusWidth
;
1062 MMC/SD card init function
1064 @param CardData Pointer to CARD_DATA.
1072 IN CARD_DATA
*CardData
1076 EFI_SD_HOST_IO_PROTOCOL
*SDHostIo
;
1077 SWITCH_ARGUMENT SwitchArgument
;
1082 BOOLEAN EnableDDRMode
;
1084 ASSERT(CardData
!= NULL
);
1085 SDHostIo
= CardData
->SDHostIo
;
1086 EnableDDRMode
= FALSE
;
1088 CardData
->CardType
= UnknownCard
;
1089 Status
= GetCardType (CardData
);
1090 if (EFI_ERROR (Status
)) {
1093 DEBUG((DEBUG_INFO
, "CardData->CardType 0x%x\n", CardData
->CardType
));
1095 ASSERT (CardData
->CardType
!= UnknownCard
);
1097 //MMC, SD card need host auto stop command support
1099 SDHostIo
->EnableAutoStopCmd (SDHostIo
, TRUE
);
1101 if (CardData
->CardType
== MMCCard
) {
1102 Status
= MMCCardVoltageSelection (CardData
);
1103 if (EFI_ERROR(Status
)) {
1111 Status
= SendCommand (
1120 (UINT32
*)&(CardData
->CIDRegister
)
1122 if (EFI_ERROR (Status
)) {
1123 DEBUG((EFI_D_ERROR
, "ALL_SEND_CID Fail Status = 0x%x\n", Status
));
1126 // Dump out the Card ID data
1127 DEBUG((EFI_D_INFO
, "Product Name: "));
1128 for ( nIndex
=0; nIndex
<6; nIndex
++ ) {
1129 DEBUG((EFI_D_INFO
, "%c", CardData
->CIDRegister
.PNM
[nIndex
]));
1131 DEBUG((EFI_D_INFO
, "\nApplication ID : %d\n", CardData
->CIDRegister
.OID
));
1132 DEBUG((EFI_D_INFO
, "Manufacturer ID: %d\n", CardData
->CIDRegister
.MID
));
1133 DEBUG((EFI_D_INFO
, "Revision ID : %d\n", CardData
->CIDRegister
.PRV
));
1134 DEBUG((EFI_D_INFO
, "Serial Number : %d\n", CardData
->CIDRegister
.PSN
));
1140 if (CardData
->CardType
== MMCCard
|| CardData
->CardType
== MMCCardHighCap
) {
1142 //Hard code the RCA address
1144 CardData
->Address
= 1;
1149 Status
= SendCommand (
1152 (CardData
->Address
<< 16),
1158 (UINT32
*)&(CardData
->CardStatus
)
1160 if (EFI_ERROR (Status
)) {
1161 DEBUG((EFI_D_ERROR
, "SET_RELATIVE_ADDR Fail Status = 0x%x\n", Status
));
1166 Status
= SendCommand (
1177 if (EFI_ERROR (Status
)) {
1178 DEBUG((EFI_D_ERROR
, "SET_RELATIVE_ADDR Fail Status = 0x%x\n", Status
));
1182 CardData
->Address
= (UINT16
)(Data
>> 16);
1183 *(UINT32
*)&CardData
->CardStatus
= Data
& 0x1FFF;
1184 CardData
->CardStatus
.ERROR
= (Data
>> 13) & 0x1;
1185 CardData
->CardStatus
.ILLEGAL_COMMAND
= (Data
>> 14) & 0x1;
1186 CardData
->CardStatus
.COM_CRC_ERROR
= (Data
>> 15) & 0x1;
1187 Status
= CheckCardStatus (*(UINT32
*)&CardData
->CardStatus
);
1188 if (EFI_ERROR (Status
)) {
1189 DEBUG((EFI_D_ERROR
, "SET_RELATIVE_ADDR Fail Status = 0x%x\n", Status
));
1197 Status
= SendCommand (
1200 (CardData
->Address
<< 16),
1206 (UINT32
*)&(CardData
->CSDRegister
)
1208 if (EFI_ERROR (Status
)) {
1209 DEBUG((EFI_D_ERROR
, "SEND_CSD Fail Status = 0x%x\n", Status
));
1213 DEBUG((EFI_D_INFO
, "CardData->CSDRegister.SPEC_VERS = 0x%x\n", CardData
->CSDRegister
.SPEC_VERS
));
1214 DEBUG((EFI_D_INFO
, "CardData->CSDRegister.CSD_STRUCTURE = 0x%x\n", CardData
->CSDRegister
.CSD_STRUCTURE
));
1216 Status
= CaculateCardParameter (CardData
);
1217 if (EFI_ERROR (Status
)) {
1223 // It is platform and hardware specific, need hadrware engineer input
1225 if (CardData
->CSDRegister
.DSR_IMP
== 1) {
1229 Status
= SendCommand (
1232 (DEFAULT_DSR_VALUE
<< 16),
1240 if (EFI_ERROR (Status
)) {
1241 DEBUG((EFI_D_ERROR
, "SET_DSR Fail Status = 0x%x\n", Status
));
1243 // Assume can operate even fail
1248 //Change clock frequency from 400KHz to max supported when not in high speed mode
1250 Status
= SDHostIo
->SetClockFrequency (SDHostIo
, CardData
->MaxFrequency
);
1251 if (EFI_ERROR (Status
)) {
1252 DEBUG((EFI_D_ERROR
, "MMCSDCardInit:Fail to SetClockFrequency \n"));
1257 //Put the card into tran state
1259 Status
= SendCommand (
1261 SELECT_DESELECT_CARD
,
1262 (CardData
->Address
<< 16),
1268 (UINT32
*)&(CardData
->CardStatus
)
1270 if (EFI_ERROR (Status
)) {
1271 DEBUG((EFI_D_ERROR
, "SELECT_DESELECT_CARD Fail Status = 0x%x\n", Status
));
1276 // No spec requirment, can be adjusted
1278 gBS
->Stall (5 * 1000);
1283 Status
= SendCommand (
1286 (CardData
->Address
<< 16),
1292 (UINT32
*)&(CardData
->CardStatus
)
1294 if (EFI_ERROR (Status
)) {
1295 DEBUG((EFI_D_ERROR
, "SELECT_DESELECT_CARD SEND_STATUS Fail Status = 0x%x\n", Status
));
1299 //if the SPEC_VERS indicates a version 4.0 or higher
1300 //The card is a high speed card and support Switch
1301 //and Send_ext_csd command
1302 //otherwise it is an old card
1305 if (CardData
->CardType
== MMCCard
|| CardData
->CardType
== MMCCardHighCap
) {
1307 //Only V4.0 and above supports more than 1 bits and high speed
1309 if (CardData
->CSDRegister
.SPEC_VERS
>= 4) {
1311 //Get ExtCSDRegister
1313 Status
= SendCommand (
1318 CardData
->AlignedBuffer
,
1322 (UINT32
*)&(CardData
->CardStatus
)
1324 if (EFI_ERROR (Status
)) {
1325 DEBUG((EFI_D_ERROR
, "SEND_EXT_CSD Fail Status = 0x%x\n", Status
));
1329 CopyMem (&(CardData
->ExtCSDRegister
), CardData
->AlignedBuffer
, sizeof (EXT_CSD
));
1332 // Recaculate the block number for >2G MMC card
1334 Data
= (CardData
->ExtCSDRegister
.SEC_COUNT
[0]) |
1335 (CardData
->ExtCSDRegister
.SEC_COUNT
[1] << 8) |
1336 (CardData
->ExtCSDRegister
.SEC_COUNT
[2] << 16) |
1337 (CardData
->ExtCSDRegister
.SEC_COUNT
[3] << 24);
1340 CardData
->BlockNumber
= Data
;
1342 DEBUG((DEBUG_INFO
, "CardData->BlockNumber %d\n", Data
));
1343 DEBUG((EFI_D_ERROR
, "CardData->ExtCSDRegister.CARD_TYPE -> %d\n", (UINTN
)CardData
->ExtCSDRegister
.CARD_TYPE
));
1344 if ((CardData
->ExtCSDRegister
.CARD_TYPE
& BIT2
)||
1345 (CardData
->ExtCSDRegister
.CARD_TYPE
& BIT3
)) {
1346 //DEBUG((DEBUG_INFO, "To enable DDR mode\n"));
1347 //EnableDDRMode = TRUE;
1350 // Check current chipset capability and the plugged-in card
1351 // whether supports HighSpeed
1353 if (SDHostIo
->HostCapability
.HighSpeedSupport
) {
1356 //Change card timing to high speed interface timing
1358 ZeroMem(&SwitchArgument
, sizeof (SWITCH_ARGUMENT
));
1359 SwitchArgument
.CmdSet
= 0;
1360 SwitchArgument
.Value
= 1;
1361 SwitchArgument
.Index
= (UINT32
)((UINTN
)
1362 (&(CardData
->ExtCSDRegister
.HS_TIMING
)) - (UINTN
)(&(CardData
->ExtCSDRegister
)));
1363 SwitchArgument
.Access
= WriteByte_Mode
;
1364 Status
= SendCommand (
1367 *(UINT32
*)&SwitchArgument
,
1373 (UINT32
*)&(CardData
->CardStatus
)
1375 if (EFI_ERROR (Status
)) {
1376 DEBUG((EFI_D_ERROR
, "MMCSDCardInit:SWITCH frequency Fail Status = 0x%x\n", Status
));
1379 gBS
->Stall (5 * 1000);
1382 if (!EFI_ERROR (Status
)) {
1383 Status
= SendCommand (
1386 (CardData
->Address
<< 16),
1392 (UINT32
*)&(CardData
->CardStatus
)
1394 if (!EFI_ERROR (Status
)) {
1395 if (EnableDDRMode
) {
1396 DEBUG((EFI_D_ERROR
, "Enable ddr mode on host controller\n"));
1397 SDHostIo
->SetDDRMode (SDHostIo
, TRUE
);
1399 DEBUG((EFI_D_ERROR
, "Enable high speed mode on host controller\n"));
1400 SDHostIo
->SetHighSpeedMode (SDHostIo
, TRUE
);
1403 // Change host clock to support high speed and enable chispet to
1406 if ((CardData
->ExtCSDRegister
.CARD_TYPE
& BIT1
) != 0) {
1407 Status
= SDHostIo
->SetClockFrequency (SDHostIo
, FREQUENCY_MMC_PP_HIGH
);
1408 } else if ((CardData
->ExtCSDRegister
.CARD_TYPE
& BIT0
) != 0) {
1409 Status
= SDHostIo
->SetClockFrequency (SDHostIo
, FREQUENCY_MMC_PP
);
1411 Status
= EFI_UNSUPPORTED
;
1413 if (EFI_ERROR (Status
)) {
1414 DEBUG((EFI_D_ERROR
, "MMCSDCardInit:Fail to SetClockFrequency \n"));
1418 // It seems no need to stall after changing bus freqeuncy.
1419 // It is said that the freqeuncy can be changed at any time. Just appends 8 clocks after command.
1420 // But SetClock alreay has delay.
1430 // Prefer wide bus width for performance
1433 // Set to BusWidth bits mode, only version 4.0 or above support more than 1 bits
1435 if (SDHostIo
->HostCapability
.BusWidth8
== TRUE
) {
1436 Status
= MMCCardSetBusWidth (CardData
, 8, EnableDDRMode
);
1437 if (EFI_ERROR (Status
)) {
1439 // CE-ATA may support 8 bits and 4 bits, but has no software method for detection
1441 Status
= MMCCardSetBusWidth (CardData
, 4, EnableDDRMode
);
1442 if (EFI_ERROR (Status
)) {
1446 } else if (SDHostIo
->HostCapability
.BusWidth4
== TRUE
) {
1447 Status
= MMCCardSetBusWidth (CardData
, 4, EnableDDRMode
);
1448 if (EFI_ERROR (Status
)) {
1455 if (CardData
->CurrentBusWidth
== 8) {
1456 if ((CardData
->ExtCSDRegister
.CARD_TYPE
& BIT1
) != 0) {
1457 PowerValue
= CardData
->ExtCSDRegister
.PWR_CL_52_360
;
1458 PowerValue
= PowerValue
>> 4;
1459 } else if ((CardData
->ExtCSDRegister
.CARD_TYPE
& BIT0
) != 0) {
1460 PowerValue
= CardData
->ExtCSDRegister
.PWR_CL_26_360
;
1461 PowerValue
= PowerValue
>> 4;
1463 } else if (CardData
->CurrentBusWidth
== 4) {
1464 if ((CardData
->ExtCSDRegister
.CARD_TYPE
& BIT1
) != 0) {
1465 PowerValue
= CardData
->ExtCSDRegister
.PWR_CL_52_360
;
1466 PowerValue
= PowerValue
& 0xF;
1467 } else if ((CardData
->ExtCSDRegister
.CARD_TYPE
& BIT0
) != 0) {
1468 PowerValue
= CardData
->ExtCSDRegister
.PWR_CL_26_360
;
1469 PowerValue
= PowerValue
& 0xF;
1473 if (PowerValue
!= 0) {
1475 //Update Power Class
1477 ZeroMem(&SwitchArgument
, sizeof (SWITCH_ARGUMENT
));
1478 SwitchArgument
.CmdSet
= 0;
1479 SwitchArgument
.Value
= PowerValue
;
1480 SwitchArgument
.Index
= (UINT32
)((UINTN
)
1481 (&(CardData
->ExtCSDRegister
.POWER_CLASS
)) - (UINTN
)(&(CardData
->ExtCSDRegister
)));
1482 SwitchArgument
.Access
= WriteByte_Mode
;
1483 Status
= SendCommand (
1486 *(UINT32
*)&SwitchArgument
,
1492 (UINT32
*)&(CardData
->CardStatus
)
1494 if (!EFI_ERROR (Status
)) {
1495 Status
= SendCommand (
1498 (CardData
->Address
<< 16),
1504 (UINT32
*)&(CardData
->CardStatus
)
1506 if (EFI_ERROR (Status
)) {
1507 DEBUG((EFI_D_ERROR
, "SWITCH Power Class Fail Status = 0x%x\n", Status
));
1509 //gBS->Stall (10 * 1000);
1518 DEBUG((EFI_D_ERROR
, "MMC Card version %d only supportes 1 bits at lower transfer speed\n",CardData
->CSDRegister
.SPEC_VERS
));
1522 // Pin 1, at power up this line has a 50KOhm pull up enabled in the card.
1523 // This pull-up should be disconnected by the user, during regular data transfer,
1524 // with SET_CLR_CARD_DETECT (ACMD42) command
1526 Status
= SendAppCommand (
1528 SET_CLR_CARD_DETECT
,
1535 (UINT32
*)&(CardData
->CardStatus
)
1537 if (EFI_ERROR (Status
)) {
1538 DEBUG((EFI_D_ERROR
, "SET_CLR_CARD_DETECT Fail Status = 0x%x\n", Status
));
1544 // Don't rely on SCR and SD status, some cards have unexpected SCR.
1545 // It only sets private section, the other bits are 0
1546 // such as Sandisk Ultra II 4.0G, KinSton mini SD 128M, Toshiba 2.0GB
1547 // Some card even fail this command, KinSton SD 4GB
1549 Status = SendAppCommand (
1554 (UINT8*)&(CardData->SCRRegister),
1558 (UINT32*)&(CardData->CardStatus)
1560 if (EFI_ERROR (Status)) {
1565 // SD memory card at least supports 1 and 4 bits.
1567 // ASSERT ((CardData->SCRRegister.SD_BUS_WIDTH & (BIT0 | BIT2)) == (BIT0 | BIT2));
1571 // Set Bus Width to 4
1573 Status
= SendAppCommand (
1582 (UINT32
*)&(CardData
->CardStatus
)
1584 if (EFI_ERROR (Status
)) {
1585 DEBUG((EFI_D_ERROR
, "SET_BUS_WIDTH 4 bits Fail Status = 0x%x\n", Status
));
1589 Status
= SDHostIo
->SetBusWidth (SDHostIo
, 4);
1590 if (EFI_ERROR (Status
)) {
1593 CardData
->CurrentBusWidth
= 4;
1596 if ((SDHostIo
->HostCapability
.HighSpeedSupport
== FALSE
) ||
1597 ((CardData
->CSDRegister
.CCC
& BIT10
) != BIT10
)) {
1599 // Host must support high speed
1600 // Card must support Switch function
1606 //Mode = 0, group 1, function 1, check operation
1608 Argument
= 0xFFFF01;
1609 ZeroMem (&CardData
->SwitchStatus
, sizeof (SWITCH_STATUS
));
1611 Status
= SendCommand (
1616 CardData
->AlignedBuffer
,
1617 sizeof (SWITCH_STATUS
),
1620 (UINT32
*)&(CardData
->CardStatus
)
1622 if (EFI_ERROR (Status
)) {
1625 CopyMem (&(CardData
->SwitchStatus
), CardData
->AlignedBuffer
, sizeof (SWITCH_STATUS
));
1627 if ((CardData
->SwitchStatus
.DataStructureVersion
== 0x0) ||
1628 ((CardData
->SwitchStatus
.Group1BusyStatus
& BIT1
) != BIT1
)) {
1630 // 1. SD 1.1 card does not suppport busy bit
1636 //Mode = 1, group 1, function 1, BIT31 set means set mode
1638 Argument
= 0xFFFF01 | BIT31
;
1639 ZeroMem (&CardData
->SwitchStatus
, sizeof (SWITCH_STATUS
));
1641 Status
= SendCommand (
1646 CardData
->AlignedBuffer
,
1647 sizeof (SWITCH_STATUS
),
1650 (UINT32
*)&(CardData
->CardStatus
)
1652 if (EFI_ERROR (Status
)) {
1655 CopyMem (&(CardData
->SwitchStatus
), CardData
->AlignedBuffer
, sizeof (SWITCH_STATUS
));
1657 if ((CardData
->SwitchStatus
.DataStructureVersion
== 0x0) ||
1658 ((CardData
->SwitchStatus
.Group1BusyStatus
& BIT1
) != BIT1
)) {
1660 // 1. SD 1.1 card does not suppport busy bit
1665 // 8 clocks, (1/ 25M) * 8 ==> 320 us, so 1ms > 0.32 ms
1672 Status
= SDHostIo
->SetClockFrequency (SDHostIo
, FREQUENCY_SD_PP_HIGH
);
1673 if (EFI_ERROR (Status
)) {
1680 if (!((CardData
->ExtCSDRegister
.CARD_TYPE
& BIT2
) ||
1681 (CardData
->ExtCSDRegister
.CARD_TYPE
& BIT3
))) {
1684 // Set Block Length, to improve compatibility in case of some cards
1686 Status
= SendCommand (
1695 (UINT32
*)&(CardData
->CardStatus
)
1697 if (EFI_ERROR (Status
)) {
1698 DEBUG((EFI_D_ERROR
, "SET_BLOCKLEN Fail Status = 0x%x\n", Status
));
1702 SDHostIo
->SetBlockLength (SDHostIo
, 512);