3 * Copyright (c) 2011-2015, ARM Limited. All rights reserved.
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD License
7 * which accompanies this distribution. The full text of the license may be found at
8 * http://opensource.org/licenses/bsd-license.php
10 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 #define MAX_RETRY_COUNT 1000
23 #define CMD_RETRY_COUNT 20
24 #define RCA_SHIFT_OFFSET 16
25 #define EMMC_CARD_SIZE 512
26 #define EMMC_ECSD_SIZE_OFFSET 53
28 UINT32 mEmmcRcaCount
= 0;
33 EmmcIdentificationMode (
34 IN MMC_HOST_INSTANCE
*MmcHostInstance
,
35 IN OCR_RESPONSE Response
38 EFI_MMC_HOST_PROTOCOL
*Host
;
39 EFI_BLOCK_IO_MEDIA
*Media
;
44 Host
= MmcHostInstance
->MmcHost
;
45 Media
= MmcHostInstance
->BlockIo
.Media
;
47 // Fetch card identity register
48 Status
= Host
->SendCommand (Host
, MMC_CMD2
, 0);
49 if (EFI_ERROR (Status
)) {
50 DEBUG ((EFI_D_ERROR
, "EmmcIdentificationMode(): Failed to send CMD2, Status=%r.\n", Status
));
54 Status
= Host
->ReceiveResponse (Host
, MMC_RESPONSE_TYPE_R2
, (UINT32
*)&(MmcHostInstance
->CardInfo
.CIDData
));
55 if (EFI_ERROR (Status
)) {
56 DEBUG ((EFI_D_ERROR
, "EmmcIdentificationMode(): CID retrieval error, Status=%r.\n", Status
));
60 // Assign a relative address value to the card
61 MmcHostInstance
->CardInfo
.RCA
= ++mEmmcRcaCount
; // TODO: might need a more sophisticated way of doing this
62 RCA
= MmcHostInstance
->CardInfo
.RCA
<< RCA_SHIFT_OFFSET
;
63 Status
= Host
->SendCommand (Host
, MMC_CMD3
, RCA
);
64 if (EFI_ERROR (Status
)) {
65 DEBUG ((EFI_D_ERROR
, "EmmcIdentificationMode(): RCA set error, Status=%r.\n", Status
));
69 // Fetch card specific data
70 Status
= Host
->SendCommand (Host
, MMC_CMD9
, RCA
);
71 if (EFI_ERROR (Status
)) {
72 DEBUG ((EFI_D_ERROR
, "EmmcIdentificationMode(): Failed to send CMD9, Status=%r.\n", Status
));
76 Status
= Host
->ReceiveResponse (Host
, MMC_RESPONSE_TYPE_R2
, (UINT32
*)&(MmcHostInstance
->CardInfo
.CSDData
));
77 if (EFI_ERROR (Status
)) {
78 DEBUG ((EFI_D_ERROR
, "EmmcIdentificationMode(): CSD retrieval error, Status=%r.\n", Status
));
83 Status
= Host
->SendCommand (Host
, MMC_CMD7
, RCA
);
84 if (EFI_ERROR (Status
)) {
85 DEBUG ((EFI_D_ERROR
, "EmmcIdentificationMode(): Card selection error, Status=%r.\n", Status
));
89 Status
= Host
->SendCommand (Host
, MMC_CMD8
, RCA
);
90 if (EFI_ERROR (Status
)) {
91 DEBUG ((EFI_D_ERROR
, "EmmcIdentificationMode(): ECSD fetch error, Status=%r.\n", Status
));
94 Status
= Host
->ReadBlockData (Host
, 0, 512, ECSD
);
95 if (EFI_ERROR (Status
)) {
96 DEBUG ((EFI_D_ERROR
, "EmmcIdentificationMode(): ECSD read error, Status=%r.\n", Status
));
101 Media
->BlockSize
= EMMC_CARD_SIZE
; // 512-byte support is mandatory for eMMC cards
102 Media
->MediaId
= MmcHostInstance
->CardInfo
.CIDData
.PSN
;
103 Media
->ReadOnly
= MmcHostInstance
->CardInfo
.CSDData
.PERM_WRITE_PROTECT
;
104 Media
->LogicalBlocksPerPhysicalBlock
= 1;
106 // Compute last block using bits [215:212] of the ECSD
107 Media
->LastBlock
= ECSD
[EMMC_ECSD_SIZE_OFFSET
] - 1; // eMMC isn't supposed to report this for
108 // Cards <2GB in size, but the model does.
111 MmcHostInstance
->CardInfo
.CardType
= EMMC_CARD
;
117 InitializeSdMmcDevice (
118 IN MMC_HOST_INSTANCE
*MmcHostInstance
127 EFI_MMC_HOST_PROTOCOL
*MmcHost
;
129 MmcHost
= MmcHostInstance
->MmcHost
;
131 // Send a command to get Card specific data
132 CmdArg
= MmcHostInstance
->CardInfo
.RCA
<< 16;
133 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD9
, CmdArg
);
134 if (EFI_ERROR (Status
)) {
135 DEBUG((EFI_D_ERROR
, "InitializeSdMmcDevice(MMC_CMD9): Error, Status=%r\n", Status
));
140 Status
= MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_CSD
, Response
);
141 if (EFI_ERROR (Status
)) {
142 DEBUG((EFI_D_ERROR
, "InitializeSdMmcDevice(): Failed to receive CSD, Status=%r\n", Status
));
147 if (MmcHostInstance
->CardInfo
.CardType
== SD_CARD_2_HIGH
) {
148 CardSize
= HC_MMC_CSD_GET_DEVICESIZE (Response
);
149 NumBlocks
= ((CardSize
+ 1) * 1024);
150 BlockSize
= 1 << MMC_CSD_GET_READBLLEN (Response
);
152 CardSize
= MMC_CSD_GET_DEVICESIZE (Response
);
153 NumBlocks
= (CardSize
+ 1) * (1 << (MMC_CSD_GET_DEVICESIZEMULT (Response
) + 2));
154 BlockSize
= 1 << MMC_CSD_GET_READBLLEN (Response
);
157 // For >=2G card, BlockSize may be 1K, but the transfer size is 512 bytes.
158 if (BlockSize
> 512) {
159 NumBlocks
= MultU64x32 (NumBlocks
, BlockSize
/ 512);
163 MmcHostInstance
->BlockIo
.Media
->LastBlock
= (NumBlocks
- 1);
164 MmcHostInstance
->BlockIo
.Media
->BlockSize
= BlockSize
;
165 MmcHostInstance
->BlockIo
.Media
->ReadOnly
= MmcHost
->IsReadOnly (MmcHost
);
166 MmcHostInstance
->BlockIo
.Media
->MediaPresent
= TRUE
;
167 MmcHostInstance
->BlockIo
.Media
->MediaId
++;
169 CmdArg
= MmcHostInstance
->CardInfo
.RCA
<< 16;
170 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD7
, CmdArg
);
171 if (EFI_ERROR (Status
)) {
172 DEBUG((EFI_D_ERROR
, "InitializeSdMmcDevice(MMC_CMD7): Error and Status = %r\n", Status
));
182 MmcIdentificationMode (
183 IN MMC_HOST_INSTANCE
*MmcHostInstance
191 EFI_MMC_HOST_PROTOCOL
*MmcHost
;
192 OCR_RESPONSE OcrResponse
;
194 MmcHost
= MmcHostInstance
->MmcHost
;
198 if (MmcHost
== NULL
) {
199 return EFI_INVALID_PARAMETER
;
202 // We can get into this function if we restart the identification mode
203 if (MmcHostInstance
->State
== MmcHwInitializationState
) {
204 // Initialize the MMC Host HW
205 Status
= MmcNotifyState (MmcHostInstance
, MmcHwInitializationState
);
206 if (EFI_ERROR (Status
)) {
207 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode() : Error MmcHwInitializationState, Status=%r.\n", Status
));
212 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD0
, 0);
213 if (EFI_ERROR (Status
)) {
214 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode(MMC_CMD0): Error, Status=%r.\n", Status
));
217 Status
= MmcNotifyState (MmcHostInstance
, MmcIdleState
);
218 if (EFI_ERROR (Status
)) {
219 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode() : Error MmcIdleState, Status=%r.\n", Status
));
223 // Send CMD1 to get OCR (MMC)
224 // This command only valid for MMC and eMMC
225 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD1
, EMMC_CMD1_CAPACITY_GREATER_THAN_2GB
);
226 if (Status
== EFI_SUCCESS
) {
227 Status
= MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_OCR
, (UINT32
*)&OcrResponse
);
228 if (EFI_ERROR (Status
)) {
229 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode() : Failed to receive OCR, Status=%r.\n", Status
));
233 if (!OcrResponse
.Ocr
.PowerUp
) {
234 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode(MMC_CMD1): Card initialisation failure, Status=%r.\n", Status
));
235 return EFI_DEVICE_ERROR
;
237 OcrResponse
.Ocr
.PowerUp
= 0;
238 if (OcrResponse
.Raw
== EMMC_CMD1_CAPACITY_GREATER_THAN_2GB
) {
239 MmcHostInstance
->CardInfo
.OCRData
.AccessMode
= BIT1
;
242 MmcHostInstance
->CardInfo
.OCRData
.AccessMode
= 0x0;
244 // Check whether MMC or eMMC
245 if (OcrResponse
.Raw
== EMMC_CMD1_CAPACITY_GREATER_THAN_2GB
||
246 OcrResponse
.Raw
== EMMC_CMD1_CAPACITY_LESS_THAN_2GB
) {
247 return EmmcIdentificationMode (MmcHostInstance
, OcrResponse
);
251 // Are we using SDIO ?
252 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD5
, 0);
253 if (Status
== EFI_SUCCESS
) {
254 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode(MMC_CMD5): Error - SDIO not supported, Status=%r.\n", Status
));
255 return EFI_UNSUPPORTED
;
258 // Check which kind of card we are using. Ver2.00 or later SD Memory Card (PL180 is SD v1.1)
259 CmdArg
= (0x0UL
<< 12 | BIT8
| 0xCEUL
<< 0);
260 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD8
, CmdArg
);
261 if (Status
== EFI_SUCCESS
) {
262 DEBUG ((EFI_D_ERROR
, "Card is SD2.0 => Supports high capacity\n"));
264 Status
= MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_R7
, Response
);
265 if (EFI_ERROR (Status
)) {
266 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode() : Failed to receive response to CMD8, Status=%r.\n", Status
));
269 PrintResponseR1 (Response
[0]);
270 // Check if it is valid response
271 if (Response
[0] != CmdArg
) {
272 DEBUG ((EFI_D_ERROR
, "The Card is not usable\n"));
273 return EFI_UNSUPPORTED
;
276 DEBUG ((EFI_D_ERROR
, "Not a SD2.0 Card\n"));
279 // We need to wait for the MMC or SD card is ready => (gCardInfo.OCRData.PowerUp == 1)
280 Timeout
= MAX_RETRY_COUNT
;
281 while (Timeout
> 0) {
282 // SD Card or MMC Card ? CMD55 indicates to the card that the next command is an application specific command
283 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD55
, 0);
284 if (Status
== EFI_SUCCESS
) {
285 DEBUG ((EFI_D_INFO
, "Card should be SD\n"));
287 MmcHostInstance
->CardInfo
.CardType
= SD_CARD_2
;
289 MmcHostInstance
->CardInfo
.CardType
= SD_CARD
;
292 // Note: The first time CmdArg will be zero
293 CmdArg
= ((UINTN
*) &(MmcHostInstance
->CardInfo
.OCRData
))[0];
297 Status
= MmcHost
->SendCommand (MmcHost
, MMC_ACMD41
, CmdArg
);
298 if (!EFI_ERROR (Status
)) {
299 Status
= MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_OCR
, Response
);
300 if (EFI_ERROR (Status
)) {
301 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode() : Failed to receive OCR, Status=%r.\n", Status
));
304 ((UINT32
*) &(MmcHostInstance
->CardInfo
.OCRData
))[0] = Response
[0];
307 DEBUG ((EFI_D_INFO
, "Card should be MMC\n"));
308 MmcHostInstance
->CardInfo
.CardType
= MMC_CARD
;
310 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD1
, 0x800000);
311 if (!EFI_ERROR (Status
)) {
312 Status
= MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_OCR
, Response
);
313 if (EFI_ERROR (Status
)) {
314 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode() : Failed to receive OCR, Status=%r.\n", Status
));
317 ((UINT32
*) &(MmcHostInstance
->CardInfo
.OCRData
))[0] = Response
[0];
321 if (!EFI_ERROR (Status
)) {
322 if (!MmcHostInstance
->CardInfo
.OCRData
.PowerUp
) {
326 if ((MmcHostInstance
->CardInfo
.CardType
== SD_CARD_2
) && (MmcHostInstance
->CardInfo
.OCRData
.AccessMode
& BIT1
)) {
327 MmcHostInstance
->CardInfo
.CardType
= SD_CARD_2_HIGH
;
328 DEBUG ((EFI_D_ERROR
, "High capacity card.\n"));
330 break; // The MMC/SD card is ready. Continue the Identification Mode
339 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode(): No Card\n"));
342 PrintOCR (Response
[0]);
345 Status
= MmcNotifyState (MmcHostInstance
, MmcReadyState
);
346 if (EFI_ERROR (Status
)) {
347 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode() : Error MmcReadyState\n"));
351 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD2
, 0);
352 if (EFI_ERROR (Status
)) {
353 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode(MMC_CMD2): Error\n"));
356 Status
= MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_CID
, Response
);
357 if (EFI_ERROR (Status
)) {
358 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode() : Failed to receive CID, Status=%r.\n", Status
));
364 Status
= MmcHost
->NotifyState (MmcHost
, MmcIdentificationState
);
365 if (EFI_ERROR (Status
)) {
366 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode() : Error MmcIdentificationState\n"));
371 // Note, SD specifications say that "if the command execution causes a state change, it
372 // will be visible to the host in the response to the next command"
373 // The status returned for this CMD3 will be 2 - identification
376 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD3
, CmdArg
);
377 if (EFI_ERROR (Status
)) {
378 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode(MMC_CMD3): Error\n"));
382 Status
= MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_RCA
, Response
);
383 if (EFI_ERROR (Status
)) {
384 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode() : Failed to receive RCA, Status=%r.\n", Status
));
387 PrintRCA (Response
[0]);
389 // For MMC card, RCA is assigned by CMD3 while CMD3 dumps the RCA for SD card
390 if (MmcHostInstance
->CardInfo
.CardType
!= MMC_CARD
) {
391 MmcHostInstance
->CardInfo
.RCA
= Response
[0] >> 16;
393 MmcHostInstance
->CardInfo
.RCA
= CmdArg
;
395 Status
= MmcNotifyState (MmcHostInstance
, MmcStandByState
);
396 if (EFI_ERROR (Status
)) {
397 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode() : Error MmcStandByState\n"));
405 InitializeMmcDevice (
406 IN MMC_HOST_INSTANCE
*MmcHostInstance
410 EFI_MMC_HOST_PROTOCOL
*MmcHost
;
414 MmcHost
= MmcHostInstance
->MmcHost
;
416 Status
= MmcIdentificationMode (MmcHostInstance
);
417 if (EFI_ERROR (Status
)) {
418 DEBUG((EFI_D_ERROR
, "InitializeMmcDevice(): Error in Identification Mode, Status=%r\n", Status
));
422 Status
= MmcNotifyState (MmcHostInstance
, MmcTransferState
);
423 if (EFI_ERROR (Status
)) {
424 DEBUG((EFI_D_ERROR
, "InitializeMmcDevice(): Error MmcTransferState, Status=%r\n", Status
));
428 if (MmcHostInstance
->CardInfo
.CardType
!= EMMC_CARD
) {
429 Status
= InitializeSdMmcDevice (MmcHostInstance
);
430 if (EFI_ERROR (Status
)) {
436 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD16
, MmcHostInstance
->BlockIo
.Media
->BlockSize
);
437 if (EFI_ERROR (Status
)) {
438 DEBUG((EFI_D_ERROR
, "InitializeMmcDevice(MMC_CMD16): Error MmcHostInstance->BlockIo.Media->BlockSize: %d and Error = %r\n",
439 MmcHostInstance
->BlockIo
.Media
->BlockSize
, Status
));
443 // Block Count (not used). Could return an error for SD card
444 if (MmcHostInstance
->CardInfo
.CardType
== MMC_CARD
) {
445 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD23
, BlockCount
);
446 if (EFI_ERROR (Status
)) {
447 DEBUG((EFI_D_ERROR
, "InitializeMmcDevice(MMC_CMD23): Error, Status=%r\n", Status
));