3 MMC/SD transfer specific functions
5 Copyright (c) 2013-2015 Intel Corporation.
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The 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.
17 #include "SDMediaDevice.h"
20 Check card status, print the debug info and check the error
22 @param Status Status got from card status register.
25 @retval EFI_DEVICE_ERROR
33 CARD_STATUS
*CardStatus
;
34 CardStatus
= (CARD_STATUS
*)(&Status
);
36 if (CardStatus
->ADDRESS_OUT_OF_RANGE
) {
37 DEBUG ((EFI_D_ERROR
, "CardStatus: ADDRESS_OUT_OF_RANGE\n"));
40 if (CardStatus
->ADDRESS_MISALIGN
) {
41 DEBUG ((EFI_D_ERROR
, "CardStatus: ADDRESS_MISALIGN\n"));
44 if (CardStatus
->BLOCK_LEN_ERROR
) {
45 DEBUG ((EFI_D_ERROR
, "CardStatus: BLOCK_LEN_ERROR\n"));
48 if (CardStatus
->ERASE_SEQ_ERROR
) {
49 DEBUG ((EFI_D_ERROR
, "CardStatus: ERASE_SEQ_ERROR\n"));
52 if (CardStatus
->ERASE_PARAM
) {
53 DEBUG ((EFI_D_ERROR
, "CardStatus: ERASE_PARAM\n"));
56 if (CardStatus
->WP_VIOLATION
) {
57 DEBUG ((EFI_D_ERROR
, "CardStatus: WP_VIOLATION\n"));
60 if (CardStatus
->CARD_IS_LOCKED
) {
61 DEBUG ((EFI_D_ERROR
, "CardStatus: CARD_IS_LOCKED\n"));
64 if (CardStatus
->LOCK_UNLOCK_FAILED
) {
65 DEBUG ((EFI_D_ERROR
, "CardStatus: LOCK_UNLOCK_FAILED\n"));
68 if (CardStatus
->COM_CRC_ERROR
) {
69 DEBUG ((EFI_D_ERROR
, "CardStatus: COM_CRC_ERROR\n"));
72 if (CardStatus
->ILLEGAL_COMMAND
) {
73 DEBUG ((EFI_D_ERROR
, "CardStatus: ILLEGAL_COMMAND\n"));
76 if (CardStatus
->CARD_ECC_FAILED
) {
77 DEBUG ((EFI_D_ERROR
, "CardStatus: CARD_ECC_FAILED\n"));
80 if (CardStatus
->CC_ERROR
) {
81 DEBUG ((EFI_D_ERROR
, "CardStatus: CC_ERROR\n"));
84 if (CardStatus
->ERROR
) {
85 DEBUG ((EFI_D_ERROR
, "CardStatus: ERROR\n"));
88 if (CardStatus
->UNDERRUN
) {
89 DEBUG ((EFI_D_ERROR
, "CardStatus: UNDERRUN\n"));
92 if (CardStatus
->OVERRUN
) {
93 DEBUG ((EFI_D_ERROR
, "CardStatus: OVERRUN\n"));
96 if (CardStatus
->CID_CSD_OVERWRITE
) {
97 DEBUG ((EFI_D_ERROR
, "CardStatus: CID_CSD_OVERWRITE\n"));
100 if (CardStatus
->WP_ERASE_SKIP
) {
101 DEBUG ((EFI_D_ERROR
, "CardStatus: WP_ERASE_SKIP\n"));
104 if (CardStatus
->ERASE_RESET
) {
105 DEBUG ((EFI_D_ERROR
, "CardStatus: ERASE_RESET\n"));
108 if (CardStatus
->SWITCH_ERROR
) {
109 DEBUG ((EFI_D_ERROR
, "CardStatus: SWITCH_ERROR\n"));
112 if ((Status
& 0xFCFFA080) != 0) {
113 return EFI_DEVICE_ERROR
;
120 Send command by using Host IO protocol
122 @param This A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
123 @param CommandIndex The command index to set the command index field of command register.
124 @param Argument Command argument to set the argument field of command register.
125 @param DataType TRANSFER_TYPE, indicates no data, data in or data out.
126 @param Buffer Contains the data read from / write to the device.
127 @param BufferSize The size of the buffer.
128 @param ResponseType RESPONSE_TYPE.
129 @param TimeOut Time out value in 1 ms unit.
130 @param ResponseData Depending on the ResponseType, such as CSD or card status.
133 @retval EFI_INVALID_PARAMETER
134 @retval EFI_UNSUPPORTED
135 @retval EFI_DEVICE_ERROR
140 IN CARD_DATA
*CardData
,
141 IN UINT16 CommandIndex
,
143 IN TRANSFER_TYPE DataType
,
144 IN UINT8
*Buffer
, OPTIONAL
145 IN UINT32 BufferSize
,
146 IN RESPONSE_TYPE ResponseType
,
148 OUT UINT32
*ResponseData
153 EFI_SD_HOST_IO_PROTOCOL
*SDHostIo
;
154 SDHostIo
= CardData
->SDHostIo
;
155 if (CardData
->CardType
!= MMCCard
&& CardData
->CardType
!= MMCCardHighCap
) {
156 CommandIndex
|= AUTO_CMD12_ENABLE
;
159 Status
= SDHostIo
->SendCommand (
170 if (!EFI_ERROR (Status
)) {
171 if (ResponseType
== ResponseR1
|| ResponseType
== ResponseR1b
) {
172 ASSERT(ResponseData
!= NULL
);
173 Status
= CheckCardStatus (*ResponseData
);
176 SDHostIo
->ResetSDHost (SDHostIo
, Reset_DAT_CMD
);
183 Send the card APP_CMD command with the following command indicated by CommandIndex
185 @param CardData Pointer to CARD_DATA.
186 @param CommandIndex The command index to set the command index field of command register.
187 @param Argument Command argument to set the argument field of command register.
188 @param DataType TRANSFER_TYPE, indicates no data, data in or data out.
189 @param Buffer Contains the data read from / write to the device.
190 @param BufferSize The size of the buffer.
191 @param ResponseType RESPONSE_TYPE.
192 @param TimeOut Time out value in 1 ms unit.
193 @param ResponseData Depending on the ResponseType, such as CSD or card status.
196 @retval EFI_INVALID_PARAMETER
197 @retval EFI_UNSUPPORTED
198 @retval EFI_DEVICE_ERROR
203 IN CARD_DATA
*CardData
,
204 IN UINT16 CommandIndex
,
206 IN TRANSFER_TYPE DataType
,
207 IN UINT8
*Buffer
, OPTIONAL
208 IN UINT32 BufferSize
,
209 IN RESPONSE_TYPE ResponseType
,
211 OUT UINT32
*ResponseData
216 EFI_SD_HOST_IO_PROTOCOL
*SDHostIo
;
219 SDHostIo
= CardData
->SDHostIo
;
220 Status
= EFI_SUCCESS
;
222 for (Index
= 0; Index
< 2; Index
++) {
223 Status
= SDHostIo
->SendCommand (
226 (CardData
->Address
<< 16),
232 (UINT32
*)&(CardData
->CardStatus
)
234 if (!EFI_ERROR (Status
)) {
235 Status
= CheckCardStatus (*(UINT32
*)&(CardData
->CardStatus
));
236 if (CardData
->CardStatus
.SAPP_CMD
!= 1) {
237 Status
= EFI_DEVICE_ERROR
;
239 if (!EFI_ERROR (Status
)) {
243 SDHostIo
->ResetSDHost (SDHostIo
, Reset_Auto
);
247 if (EFI_ERROR (Status
)) {
250 if (CardData
->CardType
!= MMCCard
&& CardData
->CardType
!= MMCCardHighCap
) {
251 CommandIndex
|= AUTO_CMD12_ENABLE
;
254 Status
= SDHostIo
->SendCommand (
265 if (!EFI_ERROR (Status
)) {
266 if (ResponseType
== ResponseR1
|| ResponseType
== ResponseR1b
) {
267 ASSERT(ResponseData
!= NULL
);
268 Status
= CheckCardStatus (*ResponseData
);
271 SDHostIo
->ResetSDHost (SDHostIo
, Reset_Auto
);
279 Send the card FAST_IO command
281 @param CardData Pointer to CARD_DATA.
282 @param RegisterAddress Register Address.
283 @param RegisterData Pointer to register Data.
284 @param Write TRUE for write, FALSE for read.
287 @retval EFI_UNSUPPORTED
288 @retval EFI_INVALID_PARAMETER
289 @retval EFI_DEVICE_ERROR
294 IN CARD_DATA
*CardData
,
295 IN UINT8 RegisterAddress
,
296 IN OUT UINT8
*RegisterData
,
304 Status
= EFI_SUCCESS
;
306 if (RegisterData
== NULL
) {
307 Status
= EFI_INVALID_PARAMETER
;
311 Argument
= (CardData
->Address
<< 16) | (RegisterAddress
<< 8);
313 Argument
|= BIT15
| (*RegisterData
);
316 Status
= SendCommand (
327 if (EFI_ERROR (Status
)) {
331 if ((Data
& BIT15
) == 0) {
332 Status
= EFI_DEVICE_ERROR
;
337 *RegisterData
= (UINT8
)Data
;
345 Send the card GO_INACTIVE_STATE command.
347 @param CardData Pointer to CARD_DATA.
355 IN CARD_DATA
*CardData
361 Status
= SendCommand (
364 (CardData
->Address
<< 16),
378 Get card interested information for CSD rergister
380 @param CardData Pointer to CARD_DATA.
383 @retval EFI_UNSUPPORTED
384 @retval EFI_INVALID_PARAMETER
388 CaculateCardParameter (
389 IN CARD_DATA
*CardData
398 Status
= EFI_SUCCESS
;
400 switch (CardData
->CSDRegister
.TRAN_SPEED
& 0x7) {
402 Frequency
= 100 * 1000;
406 Frequency
= 1 * 1000 * 1000;
410 Frequency
= 10 * 1000 * 1000;
414 Frequency
= 100 * 1000 * 1000;
418 Status
= EFI_INVALID_PARAMETER
;
422 switch ((CardData
->CSDRegister
.TRAN_SPEED
>> 3) & 0xF) {
444 if (CardData
->CardType
== MMCCard
|| CardData
->CardType
== MMCCardHighCap
) {
468 if (CardData
->CardType
== MMCCard
|| CardData
->CardType
== MMCCardHighCap
) {
492 Status
= EFI_INVALID_PARAMETER
;
496 Frequency
= Frequency
* Multiple
/ 10;
497 CardData
->MaxFrequency
= Frequency
;
499 CardData
->BlockLen
= 1 << CardData
->CSDRegister
.READ_BL_LEN
;
501 if (CardData
->CardType
== SDMemoryCard2High
) {
502 ASSERT(CardData
->CSDRegister
.CSD_STRUCTURE
== 1);
503 CsdSDV2
= (CSD_SDV2
*)&CardData
->CSDRegister
;
505 // The SD Spec 2.0 says (CSize + 1) * 512K is the total size, so block numbber is (CSize + 1) * 1K
506 // the K here means 1024 not 1000
508 CardData
->BlockNumber
= DivU64x32 (MultU64x32 (CsdSDV2
->C_SIZE
+ 1, 512 * 1024) , CardData
->BlockLen
);
511 // For MMC card > 2G, the block number will be recaculate later
513 CSize
= CardData
->CSDRegister
.C_SIZELow2
| (CardData
->CSDRegister
.C_SIZEHigh10
<< 2);
514 CardData
->BlockNumber
= MultU64x32 (LShiftU64 (1, CardData
->CSDRegister
.C_SIZE_MULT
+ 2), CSize
+ 1);
518 //For >= 2G card, BlockLen may be 1024, but the transfer size is still 512 bytes
520 if (CardData
->BlockLen
> 512) {
521 CardData
->BlockNumber
= DivU64x32 (MultU64x32 (CardData
->BlockNumber
, CardData
->BlockLen
), 512);
522 CardData
->BlockLen
= 512;
527 "CalculateCardParameter: Card Size: 0x%lx\n", MultU64x32 (CardData
->BlockNumber
, CardData
->BlockLen
)
535 Test the bus width setting for MMC card.It is used only for verification purpose.
537 @param CardData Pointer to CARD_DATA.
538 @param Width 1, 4, 8 bits.
541 @retval EFI_UNSUPPORTED
542 @retval EFI_INVALID_PARAMETER
546 MMCCardBusWidthTest (
547 IN CARD_DATA
*CardData
,
555 ASSERT(CardData
!= NULL
);
574 Status
= EFI_INVALID_PARAMETER
;
578 CopyMem (CardData
->AlignedBuffer
, &Data
, Width
);
579 Status
= SendCommand (
584 CardData
->AlignedBuffer
,
588 (UINT32
*)&(CardData
->CardStatus
)
590 if (EFI_ERROR (Status
)) {
591 DEBUG((EFI_D_ERROR
, "MMCCardBusWidthTest:SendCommand BUSTEST_W 0x%x\n", *(UINT32
*)&(CardData
->CardStatus
)));
595 gBS
->Stall (10 * 1000);
599 Status
= SendCommand (
604 CardData
->AlignedBuffer
,
608 (UINT32
*)&(CardData
->CardStatus
)
610 if (EFI_ERROR (Status
)) {
611 DEBUG((EFI_D_ERROR
, "MMCCardBusWidthTest:SendCommand BUSTEST_R 0x%x\n", *(UINT32
*)&(CardData
->CardStatus
)));
614 CopyMem (&Data
, CardData
->AlignedBuffer
, Width
);
618 Value
= (~(Data
^ 0x80)) & 0xC0;
621 Value
= (~(Data
^ 0x5A)) & 0xFF;
624 Value
= (~(Data
^ 0xAA55)) & 0xFFFF;
629 Status
= EFI_SUCCESS
;
631 Status
= EFI_UNSUPPORTED
;
640 This function can detect these card types:
643 3. SD 2.0 standard card
644 3. SD 2.0 high capacity card
646 @param CardData Pointer to CARD_DATA.
654 IN CARD_DATA
*CardData
658 EFI_SD_HOST_IO_PROTOCOL
*SDHostIo
;
662 BOOLEAN SDCommand8Support
;
665 SDHostIo
= CardData
->SDHostIo
;
670 Status
= SendCommand (
681 if (EFI_ERROR (Status
)) {
682 DEBUG((EFI_D_ERROR
, "GO_IDLE_STATE Fail Status = 0x%x\n", Status
));
687 //No spec requirment, can be adjusted
689 gBS
->Stall (10 * 1000);
693 // Only 2.7V - 3.6V is supported for SD2.0, only SD 2.0 card can pass
694 // MMC and SD1.1 card will fail this command
696 Argument
= (VOLTAGE_27_36
<< 8) | CHECK_PATTERN
;
698 SDCommand8Support
= FALSE
;
700 Status
= SendCommand (
712 if (EFI_ERROR (Status
)) {
713 if (Status
!= EFI_TIMEOUT
) {
714 DEBUG((EFI_D_ERROR
, "SEND_IF_COND Fail, none time out error\n"));
718 if (ResponseData
!= Argument
) {
719 DEBUG((EFI_D_ERROR
, "SEND_IF_COND Fail, respond data does not match send data\n"));
720 Status
= EFI_DEVICE_ERROR
;
723 SDCommand8Support
= TRUE
;
728 if (SDHostIo
->HostCapability
.V30Support
== TRUE
) {
729 Argument
|= BIT17
| BIT18
;
730 } else if (SDHostIo
->HostCapability
.V33Support
== TRUE
) {
731 Argument
|= BIT20
| BIT21
;
734 if (SDCommand8Support
) {
736 //If command SD_SEND_OP_COND sucessed, it should be set.
737 // SD 1.1 card will ignore it
738 // SD 2.0 standard card will repsond with CCS 0, SD high capacity card will respond with CCS 1
739 // CCS is BIT30 of OCR
746 //Only SD card will respond to this command, and spec says the card only checks condition at first ACMD41 command
749 Status
= SendAppCommand (
758 (UINT32
*)&(CardData
->OCRRegister
)
760 if (EFI_ERROR (Status
)) {
761 if ((Status
== EFI_TIMEOUT
) && (!SDCommand8Support
)) {
762 CardData
->CardType
= MMCCard
;
763 Status
= EFI_SUCCESS
;
764 DEBUG((EFI_D_INFO
, "SD_SEND_OP_COND, MMC card was identified\n"));
767 // Not as expected, MMC card should has no response, which means timeout.
768 // SD card should pass this command
770 DEBUG((EFI_D_ERROR
, "SD_SEND_OP_COND Fail, check whether it is neither a MMC card nor a SD card\n"));
775 //Avoid waiting if sucess. Busy bit 0 means not ready
777 if (CardData
->OCRRegister
.Busy
== 1) {
781 gBS
->Stall (50 * 1000);
784 DEBUG((EFI_D_ERROR
, "Card is always in busy state\n"));
785 Status
= EFI_TIMEOUT
;
791 //Check supported voltage
794 if (SDHostIo
->HostCapability
.V30Support
== TRUE
) {
795 if ((CardData
->OCRRegister
.V270_V360
& BIT2
) == BIT2
) {
797 } else if ((CardData
->OCRRegister
.V270_V360
& BIT3
) == BIT3
) {
800 } else if (SDHostIo
->HostCapability
.V33Support
== TRUE
) {
801 if ((CardData
->OCRRegister
.V270_V360
& BIT5
) == BIT5
) {
803 } else if ((CardData
->OCRRegister
.V270_V360
& BIT6
) == BIT6
) {
810 //No matched support voltage
812 PutCardInactive (CardData
);
813 DEBUG((EFI_D_ERROR
, "No matched voltage for this card\n"));
814 Status
= EFI_UNSUPPORTED
;
818 CardData
->CardType
= SDMemoryCard
;
819 if (SDCommand8Support
== TRUE
) {
820 CardData
->CardType
= SDMemoryCard2
;
821 DEBUG((EFI_D_INFO
, "SD_SEND_OP_COND, SD 2.0 or above standard card was identified\n"));
824 if ((CardData
->OCRRegister
.AccessMode
& BIT1
) == BIT1
) {
825 CardData
->CardType
= SDMemoryCard2High
;
826 DEBUG((EFI_D_INFO
, "SD_SEND_OP_COND, SD 2.0 or above high capacity card was identified\n"));
836 MMC card high/low voltage selection function
838 @param CardData Pointer to CARD_DATA.
841 @retval EFI_INVALID_PARAMETER
842 @retval EFI_UNSUPPORTED
843 @retval EFI_BAD_BUFFER_SIZE
847 MMCCardVoltageSelection (
848 IN CARD_DATA
*CardData
852 EFI_SD_HOST_IO_PROTOCOL
*SDHostIo
;
856 Status
= EFI_SUCCESS
;
857 SDHostIo
= CardData
->SDHostIo
;
859 //First try the high voltage, then if supported choose the low voltage
862 for (Retry
= 0; Retry
< 3; Retry
++) {
864 // To bring back the normal MMC card to work
865 // after sending the SD command. Otherwise some
866 // card could not work
868 Status
= SendCommand (
879 if (EFI_ERROR (Status
)) {
880 DEBUG((EFI_D_ERROR
, "GO_IDLE_STATE Fail Status = 0x%x\n", Status
));
884 //CE-ATA device needs long delay
886 gBS
->Stall ((Retry
+ 1) * 50 * 1000);
889 //Get OCR register to check voltage support, first time the OCR is 0
891 Status
= SendCommand (
900 (UINT32
*)&(CardData
->OCRRegister
)
902 if (!EFI_ERROR (Status
)) {
908 DEBUG((EFI_D_ERROR
, "SEND_OP_COND Fail Status = 0x%x\n", Status
));
909 Status
= EFI_DEVICE_ERROR
;
914 //TimeOut Value, 5000 * 100 * 1000 = 5 s
919 Status
= SendCommand (
928 (UINT32
*)&(CardData
->OCRRegister
)
930 if (EFI_ERROR (Status
)) {
931 DEBUG((EFI_D_ERROR
, "SEND_OP_COND Fail Status = 0x%x\n", Status
));
935 gBS
->Stall (1 * 1000);
938 Status
= EFI_TIMEOUT
;
939 DEBUG((EFI_D_ERROR
, "Card is always in busy state\n"));
942 } while (CardData
->OCRRegister
.Busy
!= 1);
944 if (CardData
->OCRRegister
.AccessMode
== 2) // eMMC Card uses Sector Addressing - High Capacity
946 DEBUG((EFI_D_INFO
, "eMMC Card is High Capacity\n"));
947 CardData
->CardType
= MMCCardHighCap
;
956 This function set the bus and device width for MMC card
958 @param CardData Pointer to CARD_DATA.
959 @param Width 1, 4, 8 bits.
962 @retval EFI_UNSUPPORTED
963 @retval EFI_INVALID_PARAMETER
968 IN CARD_DATA
*CardData
,
970 IN BOOLEAN EnableDDRMode
974 EFI_SD_HOST_IO_PROTOCOL
*SDHostIo
;
975 SWITCH_ARGUMENT SwitchArgument
;
978 SDHostIo
= CardData
->SDHostIo
;
996 if (EnableDDRMode
) // Bus width 1 is not supported in ddr mode
997 return EFI_UNSUPPORTED
;
1006 ZeroMem(&SwitchArgument
, sizeof (SWITCH_ARGUMENT
));
1007 SwitchArgument
.CmdSet
= 0;
1008 SwitchArgument
.Value
= Value
;
1009 SwitchArgument
.Index
= (UINT32
)((UINTN
)
1010 (&(CardData
->ExtCSDRegister
.BUS_WIDTH
)) - (UINTN
)(&(CardData
->ExtCSDRegister
)));
1011 SwitchArgument
.Access
= WriteByte_Mode
;
1012 Status
= SendCommand (
1015 *(UINT32
*)&SwitchArgument
,
1021 (UINT32
*)&(CardData
->CardStatus
)
1023 if (!EFI_ERROR (Status
)) {
1024 Status
= SendCommand (
1027 (CardData
->Address
<< 16),
1033 (UINT32
*)&(CardData
->CardStatus
)
1035 if (EFI_ERROR (Status
)) {
1036 DEBUG((EFI_D_ERROR
, "SWITCH %d bits Fail\n", BusWidth
));
1039 DEBUG((EFI_D_ERROR
, "MMCCardSetBusWidth:SWITCH Card Status:0x%x\n", *(UINT32
*)&(CardData
->CardStatus
)));
1040 Status
= SDHostIo
->SetBusWidth (SDHostIo
, BusWidth
);
1041 if (EFI_ERROR (Status
)) {
1042 DEBUG((EFI_D_ERROR
, "SWITCH set %d bits Fail\n", BusWidth
));
1045 gBS
->Stall (5 * 1000);
1049 if (!EnableDDRMode
) { // CMD19 and CMD14 are illegal commands in ddr mode
1050 //if (EFI_ERROR (Status)) {
1051 // DEBUG((EFI_D_ERROR, "MMCCardBusWidthTest: Fail to enable high speed mode\n"));
1055 Status
= MMCCardBusWidthTest (CardData
, BusWidth
);
1056 if (EFI_ERROR (Status
)) {
1057 DEBUG((EFI_D_ERROR
, "MMCCardBusWidthTest %d bit Fail\n", BusWidth
));
1062 CardData
->CurrentBusWidth
= BusWidth
;
1070 MMC/SD card init function
1072 @param CardData Pointer to CARD_DATA.
1080 IN CARD_DATA
*CardData
1084 EFI_SD_HOST_IO_PROTOCOL
*SDHostIo
;
1085 SWITCH_ARGUMENT SwitchArgument
;
1090 BOOLEAN EnableDDRMode
;
1092 ASSERT(CardData
!= NULL
);
1093 SDHostIo
= CardData
->SDHostIo
;
1094 EnableDDRMode
= FALSE
;
1096 CardData
->CardType
= UnknownCard
;
1097 Status
= GetCardType (CardData
);
1098 if (EFI_ERROR (Status
)) {
1101 DEBUG((DEBUG_INFO
, "CardData->CardType 0x%x\n", CardData
->CardType
));
1103 ASSERT (CardData
->CardType
!= UnknownCard
);
1105 //MMC, SD card need host auto stop command support
1107 SDHostIo
->EnableAutoStopCmd (SDHostIo
, TRUE
);
1109 if (CardData
->CardType
== MMCCard
) {
1110 Status
= MMCCardVoltageSelection (CardData
);
1111 if (EFI_ERROR(Status
)) {
1119 Status
= SendCommand (
1128 (UINT32
*)&(CardData
->CIDRegister
)
1130 if (EFI_ERROR (Status
)) {
1131 DEBUG((EFI_D_ERROR
, "ALL_SEND_CID Fail Status = 0x%x\n", Status
));
1134 // Dump out the Card ID data
1135 DEBUG((EFI_D_INFO
, "Product Name: "));
1136 for ( nIndex
=0; nIndex
<6; nIndex
++ ) {
1137 DEBUG((EFI_D_INFO
, "%c", CardData
->CIDRegister
.PNM
[nIndex
]));
1139 DEBUG((EFI_D_INFO
, "\nApplication ID : %d\n", CardData
->CIDRegister
.OID
));
1140 DEBUG((EFI_D_INFO
, "Manufacturer ID: %d\n", CardData
->CIDRegister
.MID
));
1141 DEBUG((EFI_D_INFO
, "Revision ID : %d\n", CardData
->CIDRegister
.PRV
));
1142 DEBUG((EFI_D_INFO
, "Serial Number : %d\n", CardData
->CIDRegister
.PSN
));
1148 if (CardData
->CardType
== MMCCard
|| CardData
->CardType
== MMCCardHighCap
) {
1150 //Hard code the RCA address
1152 CardData
->Address
= 1;
1157 Status
= SendCommand (
1160 (CardData
->Address
<< 16),
1166 (UINT32
*)&(CardData
->CardStatus
)
1168 if (EFI_ERROR (Status
)) {
1169 DEBUG((EFI_D_ERROR
, "SET_RELATIVE_ADDR Fail Status = 0x%x\n", Status
));
1174 Status
= SendCommand (
1185 if (EFI_ERROR (Status
)) {
1186 DEBUG((EFI_D_ERROR
, "SET_RELATIVE_ADDR Fail Status = 0x%x\n", Status
));
1190 CardData
->Address
= (UINT16
)(Data
>> 16);
1191 *(UINT32
*)&CardData
->CardStatus
= Data
& 0x1FFF;
1192 CardData
->CardStatus
.ERROR
= (Data
>> 13) & 0x1;
1193 CardData
->CardStatus
.ILLEGAL_COMMAND
= (Data
>> 14) & 0x1;
1194 CardData
->CardStatus
.COM_CRC_ERROR
= (Data
>> 15) & 0x1;
1195 Status
= CheckCardStatus (*(UINT32
*)&CardData
->CardStatus
);
1196 if (EFI_ERROR (Status
)) {
1197 DEBUG((EFI_D_ERROR
, "SET_RELATIVE_ADDR Fail Status = 0x%x\n", Status
));
1205 Status
= SendCommand (
1208 (CardData
->Address
<< 16),
1214 (UINT32
*)&(CardData
->CSDRegister
)
1216 if (EFI_ERROR (Status
)) {
1217 DEBUG((EFI_D_ERROR
, "SEND_CSD Fail Status = 0x%x\n", Status
));
1221 DEBUG((EFI_D_INFO
, "CardData->CSDRegister.SPEC_VERS = 0x%x\n", CardData
->CSDRegister
.SPEC_VERS
));
1222 DEBUG((EFI_D_INFO
, "CardData->CSDRegister.CSD_STRUCTURE = 0x%x\n", CardData
->CSDRegister
.CSD_STRUCTURE
));
1224 Status
= CaculateCardParameter (CardData
);
1225 if (EFI_ERROR (Status
)) {
1231 // It is platform and hardware specific, need hadrware engineer input
1233 if (CardData
->CSDRegister
.DSR_IMP
== 1) {
1237 Status
= SendCommand (
1240 (DEFAULT_DSR_VALUE
<< 16),
1248 if (EFI_ERROR (Status
)) {
1249 DEBUG((EFI_D_ERROR
, "SET_DSR Fail Status = 0x%x\n", Status
));
1251 // Assume can operate even fail
1256 //Change clock frequency from 400KHz to max supported when not in high speed mode
1258 Status
= SDHostIo
->SetClockFrequency (SDHostIo
, CardData
->MaxFrequency
);
1259 if (EFI_ERROR (Status
)) {
1260 DEBUG((EFI_D_ERROR
, "MMCSDCardInit:Fail to SetClockFrequency \n"));
1265 //Put the card into tran state
1267 Status
= SendCommand (
1269 SELECT_DESELECT_CARD
,
1270 (CardData
->Address
<< 16),
1276 (UINT32
*)&(CardData
->CardStatus
)
1278 if (EFI_ERROR (Status
)) {
1279 DEBUG((EFI_D_ERROR
, "SELECT_DESELECT_CARD Fail Status = 0x%x\n", Status
));
1284 // No spec requirment, can be adjusted
1286 gBS
->Stall (5 * 1000);
1291 Status
= SendCommand (
1294 (CardData
->Address
<< 16),
1300 (UINT32
*)&(CardData
->CardStatus
)
1302 if (EFI_ERROR (Status
)) {
1303 DEBUG((EFI_D_ERROR
, "SELECT_DESELECT_CARD SEND_STATUS Fail Status = 0x%x\n", Status
));
1307 //if the SPEC_VERS indicates a version 4.0 or higher
1308 //The card is a high speed card and support Switch
1309 //and Send_ext_csd command
1310 //otherwise it is an old card
1313 if (CardData
->CardType
== MMCCard
|| CardData
->CardType
== MMCCardHighCap
) {
1315 //Only V4.0 and above supports more than 1 bits and high speed
1317 if (CardData
->CSDRegister
.SPEC_VERS
>= 4) {
1319 //Get ExtCSDRegister
1321 Status
= SendCommand (
1326 CardData
->AlignedBuffer
,
1330 (UINT32
*)&(CardData
->CardStatus
)
1332 if (EFI_ERROR (Status
)) {
1333 DEBUG((EFI_D_ERROR
, "SEND_EXT_CSD Fail Status = 0x%x\n", Status
));
1337 CopyMem (&(CardData
->ExtCSDRegister
), CardData
->AlignedBuffer
, sizeof (EXT_CSD
));
1340 // Recaculate the block number for >2G MMC card
1342 Data
= (CardData
->ExtCSDRegister
.SEC_COUNT
[0]) |
1343 (CardData
->ExtCSDRegister
.SEC_COUNT
[1] << 8) |
1344 (CardData
->ExtCSDRegister
.SEC_COUNT
[2] << 16) |
1345 (CardData
->ExtCSDRegister
.SEC_COUNT
[3] << 24);
1348 CardData
->BlockNumber
= Data
;
1350 DEBUG((DEBUG_INFO
, "CardData->BlockNumber %d\n", Data
));
1351 DEBUG((EFI_D_ERROR
, "CardData->ExtCSDRegister.CARD_TYPE -> %d\n", (UINTN
)CardData
->ExtCSDRegister
.CARD_TYPE
));
1352 if ((CardData
->ExtCSDRegister
.CARD_TYPE
& BIT2
)||
1353 (CardData
->ExtCSDRegister
.CARD_TYPE
& BIT3
)) {
1354 //DEBUG((DEBUG_INFO, "To enable DDR mode\n"));
1355 //EnableDDRMode = TRUE;
1358 // Check current chipset capability and the plugged-in card
1359 // whether supports HighSpeed
1361 if (SDHostIo
->HostCapability
.HighSpeedSupport
) {
1364 //Change card timing to high speed interface timing
1366 ZeroMem(&SwitchArgument
, sizeof (SWITCH_ARGUMENT
));
1367 SwitchArgument
.CmdSet
= 0;
1368 SwitchArgument
.Value
= 1;
1369 SwitchArgument
.Index
= (UINT32
)((UINTN
)
1370 (&(CardData
->ExtCSDRegister
.HS_TIMING
)) - (UINTN
)(&(CardData
->ExtCSDRegister
)));
1371 SwitchArgument
.Access
= WriteByte_Mode
;
1372 Status
= SendCommand (
1375 *(UINT32
*)&SwitchArgument
,
1381 (UINT32
*)&(CardData
->CardStatus
)
1383 if (EFI_ERROR (Status
)) {
1384 DEBUG((EFI_D_ERROR
, "MMCSDCardInit:SWITCH frequency Fail Status = 0x%x\n", Status
));
1387 gBS
->Stall (5 * 1000);
1390 if (!EFI_ERROR (Status
)) {
1391 Status
= SendCommand (
1394 (CardData
->Address
<< 16),
1400 (UINT32
*)&(CardData
->CardStatus
)
1402 if (!EFI_ERROR (Status
)) {
1403 if (EnableDDRMode
) {
1404 DEBUG((EFI_D_ERROR
, "Enable ddr mode on host controller\n"));
1405 SDHostIo
->SetDDRMode (SDHostIo
, TRUE
);
1407 DEBUG((EFI_D_ERROR
, "Enable high speed mode on host controller\n"));
1408 SDHostIo
->SetHighSpeedMode (SDHostIo
, TRUE
);
1411 // Change host clock to support high speed and enable chispet to
1414 if ((CardData
->ExtCSDRegister
.CARD_TYPE
& BIT1
) != 0) {
1415 Status
= SDHostIo
->SetClockFrequency (SDHostIo
, FREQUENCY_MMC_PP_HIGH
);
1416 } else if ((CardData
->ExtCSDRegister
.CARD_TYPE
& BIT0
) != 0) {
1417 Status
= SDHostIo
->SetClockFrequency (SDHostIo
, FREQUENCY_MMC_PP
);
1419 Status
= EFI_UNSUPPORTED
;
1421 if (EFI_ERROR (Status
)) {
1422 DEBUG((EFI_D_ERROR
, "MMCSDCardInit:Fail to SetClockFrequency \n"));
1426 // It seems no need to stall after changing bus freqeuncy.
1427 // It is said that the freqeuncy can be changed at any time. Just appends 8 clocks after command.
1428 // But SetClock alreay has delay.
1438 // Prefer wide bus width for performance
1441 // Set to BusWidth bits mode, only version 4.0 or above support more than 1 bits
1443 if (SDHostIo
->HostCapability
.BusWidth8
== TRUE
) {
1444 Status
= MMCCardSetBusWidth (CardData
, 8, EnableDDRMode
);
1445 if (EFI_ERROR (Status
)) {
1447 // CE-ATA may support 8 bits and 4 bits, but has no software method for detection
1449 Status
= MMCCardSetBusWidth (CardData
, 4, EnableDDRMode
);
1450 if (EFI_ERROR (Status
)) {
1454 } else if (SDHostIo
->HostCapability
.BusWidth4
== TRUE
) {
1455 Status
= MMCCardSetBusWidth (CardData
, 4, EnableDDRMode
);
1456 if (EFI_ERROR (Status
)) {
1463 if (CardData
->CurrentBusWidth
== 8) {
1464 if ((CardData
->ExtCSDRegister
.CARD_TYPE
& BIT1
) != 0) {
1465 PowerValue
= CardData
->ExtCSDRegister
.PWR_CL_52_360
;
1466 PowerValue
= PowerValue
>> 4;
1467 } else if ((CardData
->ExtCSDRegister
.CARD_TYPE
& BIT0
) != 0) {
1468 PowerValue
= CardData
->ExtCSDRegister
.PWR_CL_26_360
;
1469 PowerValue
= PowerValue
>> 4;
1471 } else if (CardData
->CurrentBusWidth
== 4) {
1472 if ((CardData
->ExtCSDRegister
.CARD_TYPE
& BIT1
) != 0) {
1473 PowerValue
= CardData
->ExtCSDRegister
.PWR_CL_52_360
;
1474 PowerValue
= PowerValue
& 0xF;
1475 } else if ((CardData
->ExtCSDRegister
.CARD_TYPE
& BIT0
) != 0) {
1476 PowerValue
= CardData
->ExtCSDRegister
.PWR_CL_26_360
;
1477 PowerValue
= PowerValue
& 0xF;
1481 if (PowerValue
!= 0) {
1483 //Update Power Class
1485 ZeroMem(&SwitchArgument
, sizeof (SWITCH_ARGUMENT
));
1486 SwitchArgument
.CmdSet
= 0;
1487 SwitchArgument
.Value
= PowerValue
;
1488 SwitchArgument
.Index
= (UINT32
)((UINTN
)
1489 (&(CardData
->ExtCSDRegister
.POWER_CLASS
)) - (UINTN
)(&(CardData
->ExtCSDRegister
)));
1490 SwitchArgument
.Access
= WriteByte_Mode
;
1491 Status
= SendCommand (
1494 *(UINT32
*)&SwitchArgument
,
1500 (UINT32
*)&(CardData
->CardStatus
)
1502 if (!EFI_ERROR (Status
)) {
1503 Status
= SendCommand (
1506 (CardData
->Address
<< 16),
1512 (UINT32
*)&(CardData
->CardStatus
)
1514 if (EFI_ERROR (Status
)) {
1515 DEBUG((EFI_D_ERROR
, "SWITCH Power Class Fail Status = 0x%x\n", Status
));
1517 //gBS->Stall (10 * 1000);
1526 DEBUG((EFI_D_ERROR
, "MMC Card version %d only supportes 1 bits at lower transfer speed\n",CardData
->CSDRegister
.SPEC_VERS
));
1530 // Pin 1, at power up this line has a 50KOhm pull up enabled in the card.
1531 // This pull-up should be disconnected by the user, during regular data transfer,
1532 // with SET_CLR_CARD_DETECT (ACMD42) command
1534 Status
= SendAppCommand (
1536 SET_CLR_CARD_DETECT
,
1543 (UINT32
*)&(CardData
->CardStatus
)
1545 if (EFI_ERROR (Status
)) {
1546 DEBUG((EFI_D_ERROR
, "SET_CLR_CARD_DETECT Fail Status = 0x%x\n", Status
));
1552 // Don't rely on SCR and SD status, some cards have unexpected SCR.
1553 // It only sets private section, the other bits are 0
1554 // such as Sandisk Ultra II 4.0G, KinSton mini SD 128M, Toshiba 2.0GB
1555 // Some card even fail this command, KinSton SD 4GB
1557 Status = SendAppCommand (
1562 (UINT8*)&(CardData->SCRRegister),
1566 (UINT32*)&(CardData->CardStatus)
1568 if (EFI_ERROR (Status)) {
1573 // SD memory card at least supports 1 and 4 bits.
1575 // ASSERT ((CardData->SCRRegister.SD_BUS_WIDTH & (BIT0 | BIT2)) == (BIT0 | BIT2));
1579 // Set Bus Width to 4
1581 Status
= SendAppCommand (
1590 (UINT32
*)&(CardData
->CardStatus
)
1592 if (EFI_ERROR (Status
)) {
1593 DEBUG((EFI_D_ERROR
, "SET_BUS_WIDTH 4 bits Fail Status = 0x%x\n", Status
));
1597 Status
= SDHostIo
->SetBusWidth (SDHostIo
, 4);
1598 if (EFI_ERROR (Status
)) {
1601 CardData
->CurrentBusWidth
= 4;
1604 if ((SDHostIo
->HostCapability
.HighSpeedSupport
== FALSE
) ||
1605 ((CardData
->CSDRegister
.CCC
& BIT10
) != BIT10
)) {
1607 // Host must support high speed
1608 // Card must support Switch function
1614 //Mode = 0, group 1, function 1, check operation
1616 Argument
= 0xFFFF01;
1617 ZeroMem (&CardData
->SwitchStatus
, sizeof (SWITCH_STATUS
));
1619 Status
= SendCommand (
1624 CardData
->AlignedBuffer
,
1625 sizeof (SWITCH_STATUS
),
1628 (UINT32
*)&(CardData
->CardStatus
)
1630 if (EFI_ERROR (Status
)) {
1633 CopyMem (&(CardData
->SwitchStatus
), CardData
->AlignedBuffer
, sizeof (SWITCH_STATUS
));
1635 if ((CardData
->SwitchStatus
.DataStructureVersion
== 0x0) ||
1636 ((CardData
->SwitchStatus
.Group1BusyStatus
& BIT1
) != BIT1
)) {
1638 // 1. SD 1.1 card does not suppport busy bit
1644 //Mode = 1, group 1, function 1, BIT31 set means set mode
1646 Argument
= 0xFFFF01 | BIT31
;
1647 ZeroMem (&CardData
->SwitchStatus
, sizeof (SWITCH_STATUS
));
1649 Status
= SendCommand (
1654 CardData
->AlignedBuffer
,
1655 sizeof (SWITCH_STATUS
),
1658 (UINT32
*)&(CardData
->CardStatus
)
1660 if (EFI_ERROR (Status
)) {
1663 CopyMem (&(CardData
->SwitchStatus
), CardData
->AlignedBuffer
, sizeof (SWITCH_STATUS
));
1665 if ((CardData
->SwitchStatus
.DataStructureVersion
== 0x0) ||
1666 ((CardData
->SwitchStatus
.Group1BusyStatus
& BIT1
) != BIT1
)) {
1668 // 1. SD 1.1 card does not suppport busy bit
1673 // 8 clocks, (1/ 25M) * 8 ==> 320 us, so 1ms > 0.32 ms
1680 Status
= SDHostIo
->SetClockFrequency (SDHostIo
, FREQUENCY_SD_PP_HIGH
);
1681 if (EFI_ERROR (Status
)) {
1688 if (!((CardData
->ExtCSDRegister
.CARD_TYPE
& BIT2
) ||
1689 (CardData
->ExtCSDRegister
.CARD_TYPE
& BIT3
))) {
1692 // Set Block Length, to improve compatibility in case of some cards
1694 Status
= SendCommand (
1703 (UINT32
*)&(CardData
->CardStatus
)
1705 if (EFI_ERROR (Status
)) {
1706 DEBUG((EFI_D_ERROR
, "SET_BLOCKLEN Fail Status = 0x%x\n", Status
));
1710 SDHostIo
->SetBlockLength (SDHostIo
, 512);