3 * Copyright (c) 2011-2013, 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.
15 #include <Protocol/MmcHost.h>
16 #include <Library/DebugLib.h>
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/TimerLib.h>
25 #define MAX_RETRY_COUNT 1000
26 #define CMD_RETRY_COUNT 20
30 IN MMC_HOST_INSTANCE
*MmcHostInstance
,
34 MmcHostInstance
->State
= State
;
35 return MmcHostInstance
->MmcHost
->NotifyState (MmcHostInstance
->MmcHost
, State
);
52 // The MMC register bits [23:8] indicate the working range of the card
53 for (Loop
= 8; Loop
< 24; Loop
++) {
54 if (Ocr
& (1 << Loop
)) {
55 if (MinV
> Volts
) MinV
= Volts
;
56 if (MaxV
< Volts
) MaxV
= Volts
+ 1;
61 DEBUG ((EFI_D_ERROR
, "- PrintOCR Ocr (0x%X)\n",Ocr
));
62 DEBUG ((EFI_D_ERROR
, "\t- Card operating voltage: %d.%d to %d.%d\n", MinV
/10, MinV
% 10, MaxV
/10, MaxV
% 10));
63 if (((Ocr
>> 29) & 3) == 0) {
64 DEBUG ((EFI_D_ERROR
, "\t- AccessMode: Byte Mode\n"));
66 DEBUG ((EFI_D_ERROR
, "\t- AccessMode: Block Mode (0x%X)\n", ((Ocr
>> 29) & 3)));
69 if (Ocr
& MMC_OCR_POWERUP
) {
70 DEBUG ((EFI_D_ERROR
, "\t- PowerUp\n"));
72 DEBUG ((EFI_D_ERROR
, "\t- Voltage Not Supported\n"));
80 DEBUG ((EFI_D_ERROR
, "- PrintCID\n"));
81 DEBUG ((EFI_D_ERROR
, "\t- Manufacturing date: %d/%d\n", (Cid
[0] >> 8) & 0xF, (Cid
[0] >> 12) & 0xFF));
82 DEBUG ((EFI_D_ERROR
, "\t- Product serial number: 0x%X%X\n", Cid
[1] & 0xFFFFFF, (Cid
[0] >> 24) & 0xFF));
83 DEBUG ((EFI_D_ERROR
, "\t- Product revision: %d\n", Cid
[1] >> 24));
84 //DEBUG ((EFI_D_ERROR, "\t- Product name: %s\n", (char*)(Cid + 2)));
85 DEBUG ((EFI_D_ERROR
, "\t- OEM ID: %c%c\n", (Cid
[3] >> 8) & 0xFF, (Cid
[3] >> 16) & 0xFF));
88 #if !defined(MDEPKG_NDEBUG)
89 CONST CHAR8
* mStrUnit
[] = { "100kbit/s", "1Mbit/s", "10Mbit/s", "100MBit/s",
90 "Unknown", "Unknown", "Unknown", "Unknown" };
91 CONST CHAR8
* mStrValue
[] = { "1.0", "1.2", "1.3", "1.5", "2.0", "2.5", "3.0", "3.5", "4.0", "4.5", "5.0",
92 "Unknown", "Unknown", "Unknown", "Unknown" };
102 if (((Csd
[2] >> 30) & 0x3) == 0) {
103 DEBUG ((EFI_D_ERROR
, "- PrintCSD Version 1.01-1.10/Version 2.00/Standard Capacity\n"));
104 } else if (((Csd
[2] >> 30) & 0x3) == 1) {
105 DEBUG ((EFI_D_ERROR
, "- PrintCSD Version 2.00/High Capacity\n"));
107 DEBUG ((EFI_D_ERROR
, "- PrintCSD Version Higher than v3.3\n"));
110 DEBUG ((EFI_D_ERROR
, "\t- Supported card command class: 0x%X\n", MMC_CSD_GET_CCC(Csd
)));
111 DEBUG ((EFI_D_ERROR
, "\t- Speed: %a %a\n",mStrValue
[(MMC_CSD_GET_TRANSPEED(Csd
) >> 3) & 0xF],mStrUnit
[MMC_CSD_GET_TRANSPEED(Csd
) & 7]));
112 DEBUG ((EFI_D_ERROR
, "\t- Maximum Read Data Block: %d\n",2 << (MMC_CSD_GET_READBLLEN(Csd
)-1)));
113 DEBUG ((EFI_D_ERROR
, "\t- Maximum Write Data Block: %d\n",2 << (MMC_CSD_GET_WRITEBLLEN(Csd
)-1)));
115 if (!MMC_CSD_GET_FILEFORMATGRP(Csd
)) {
116 Value
= MMC_CSD_GET_FILEFORMAT (Csd
);
117 if (Value
== 0) DEBUG ((EFI_D_ERROR
, "\t- Format (0): Hard disk-like file system with partition table\n"));
118 else if (Value
== 1) DEBUG ((EFI_D_ERROR
, "\t- Format (1): DOS FAT (floppy-like) with boot sector only (no partition table)\n"));
119 else if (Value
== 2) DEBUG ((EFI_D_ERROR
, "\t- Format (2): Universal File Format\n"));
120 else DEBUG ((EFI_D_ERROR
, "\t- Format (3): Others/Unknown\n"));
122 DEBUG ((EFI_D_ERROR
, "\t- Format: Reserved\n"));
131 DEBUG ((EFI_D_ERROR
, "- PrintRCA: 0x%X\n", Rca
));
132 DEBUG ((EFI_D_ERROR
, "\t- Status: 0x%X\n", Rca
& 0xFFFF));
133 DEBUG ((EFI_D_ERROR
, "\t- RCA: 0x%X\n", (Rca
>> 16) & 0xFFFF));
141 DEBUG ((EFI_D_INFO
, "Response: 0x%X\n", Response
));
142 if (Response
& MMC_R0_READY_FOR_DATA
) {
143 DEBUG ((EFI_D_INFO
, "\t- READY_FOR_DATA\n"));
146 if (((Response
>> 9) & 0xF) == 0) DEBUG ((EFI_D_INFO
, "\t- State: Idle\n"));
147 else if (((Response
>> 9) & 0xF) == 1) DEBUG ((EFI_D_INFO
, "\t- State: Ready\n"));
148 else if (((Response
>> 9) & 0xF) == 2) DEBUG ((EFI_D_INFO
, "\t- State: Ident\n"));
149 else if (((Response
>> 9) & 0xF) == 3) DEBUG ((EFI_D_INFO
, "\t- State: StandBy\n"));
150 else if (((Response
>> 9) & 0xF) == 4) DEBUG ((EFI_D_INFO
, "\t- State: Tran\n"));
151 else if (((Response
>> 9) & 0xF) == 5) DEBUG ((EFI_D_INFO
, "\t- State: Data\n"));
152 else if (((Response
>> 9) & 0xF) == 6) DEBUG ((EFI_D_INFO
, "\t- State: Rcv\n"));
153 else if (((Response
>> 9) & 0xF) == 7) DEBUG ((EFI_D_INFO
, "\t- State: Prg\n"));
154 else if (((Response
>> 9) & 0xF) == 8) DEBUG ((EFI_D_INFO
, "\t- State: Dis\n"));
155 else DEBUG ((EFI_D_INFO
, "\t- State: Reserved\n"));
161 IN MMC_HOST_INSTANCE
*MmcHostInstance
167 EFI_MMC_HOST_PROTOCOL
*MmcHost
;
169 Status
= EFI_SUCCESS
;
170 MmcHost
= MmcHostInstance
->MmcHost
;
173 if (MmcHost
== NULL
) {
174 return EFI_INVALID_PARAMETER
;
176 if(MmcHostInstance
->State
!= MmcHwInitializationState
){
177 //Get the Status of the card.
178 CmdArg
= MmcHostInstance
->CardInfo
.RCA
<< 16;
179 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD13
, CmdArg
);
180 if (EFI_ERROR (Status
)) {
181 DEBUG ((EFI_D_ERROR
, "MmcGetCardStatus(MMC_CMD13): Error and Status = %r\n", Status
));
186 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_R1
, Response
);
187 PrintResponseR1 (Response
[0]);
195 MmcIdentificationMode (
196 IN MMC_HOST_INSTANCE
*MmcHostInstance
204 EFI_MMC_HOST_PROTOCOL
*MmcHost
;
206 MmcHost
= MmcHostInstance
->MmcHost
;
210 if (MmcHost
== NULL
) {
211 return EFI_INVALID_PARAMETER
;
214 // We can get into this function if we restart the identification mode
215 if (MmcHostInstance
->State
== MmcHwInitializationState
) {
216 // Initialize the MMC Host HW
217 Status
= MmcNotifyState (MmcHostInstance
, MmcHwInitializationState
);
218 if (EFI_ERROR (Status
)) {
219 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode() : Error MmcHwInitializationState\n"));
224 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD0
, 0);
225 if (EFI_ERROR (Status
)) {
226 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode(MMC_CMD0): Error\n"));
230 Status
= MmcNotifyState (MmcHostInstance
, MmcIdleState
);
231 if (EFI_ERROR (Status
)) {
232 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode() : Error MmcIdleState\n"));
236 // Are we using SDIO ?
237 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD5
, 0);
238 if (Status
== EFI_SUCCESS
) {
239 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode(MMC_CMD5): Error - SDIO not supported.\n"));
240 return EFI_UNSUPPORTED
;
243 // Check which kind of card we are using. Ver2.00 or later SD Memory Card (PL180 is SD v1.1)
244 CmdArg
= (0x0UL
<< 12 | BIT8
| 0xCEUL
<< 0);
245 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD8
, CmdArg
);
246 if (Status
== EFI_SUCCESS
) {
247 DEBUG ((EFI_D_ERROR
, "Card is SD2.0 => Supports high capacity\n"));
249 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_R7
, Response
);
250 PrintResponseR1 (Response
[0]);
251 //check if it is valid response
252 if(Response
[0] != CmdArg
){
253 DEBUG ((EFI_D_ERROR
, "The Card is not usable\n"));
254 return EFI_UNSUPPORTED
;
257 DEBUG ((EFI_D_ERROR
, "Not a SD2.0 Card\n"));
260 // We need to wait for the MMC or SD card is ready => (gCardInfo.OCRData.PowerUp == 1)
261 Timeout
= MAX_RETRY_COUNT
;
262 while (Timeout
> 0) {
263 // SD Card or MMC Card ? CMD55 indicates to the card that the next command is an application specific command
264 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD55
, 0);
265 if (Status
== EFI_SUCCESS
) {
266 DEBUG ((EFI_D_INFO
, "Card should be SD\n"));
268 MmcHostInstance
->CardInfo
.CardType
= SD_CARD_2
;
270 MmcHostInstance
->CardInfo
.CardType
= SD_CARD
;
273 // Note: The first time CmdArg will be zero
274 CmdArg
= ((UINTN
*) &(MmcHostInstance
->CardInfo
.OCRData
))[0];
278 Status
= MmcHost
->SendCommand (MmcHost
, MMC_ACMD41
, CmdArg
);
279 if (!EFI_ERROR (Status
)) {
280 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_OCR
, Response
);
281 ((UINT32
*) &(MmcHostInstance
->CardInfo
.OCRData
))[0] = Response
[0];
284 DEBUG ((EFI_D_INFO
, "Card should be MMC\n"));
285 MmcHostInstance
->CardInfo
.CardType
= MMC_CARD
;
287 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD1
, 0x800000);
288 if (!EFI_ERROR (Status
)) {
289 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_OCR
, Response
);
290 ((UINT32
*) &(MmcHostInstance
->CardInfo
.OCRData
))[0] = Response
[0];
294 if (!EFI_ERROR (Status
)) {
295 if (!MmcHostInstance
->CardInfo
.OCRData
.PowerUp
) {
296 MicroSecondDelay (1);
299 if ((MmcHostInstance
->CardInfo
.CardType
== SD_CARD_2
) && (MmcHostInstance
->CardInfo
.OCRData
.AccessMode
& BIT1
)) {
300 MmcHostInstance
->CardInfo
.CardType
= SD_CARD_2_HIGH
;
301 DEBUG ((EFI_D_ERROR
, "High capacity card.\n"));
303 break; // The MMC/SD card is ready. Continue the Identification Mode
306 MicroSecondDelay (1);
312 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode(): No Card\n"));
315 PrintOCR (Response
[0]);
318 Status
= MmcNotifyState (MmcHostInstance
, MmcReadyState
);
319 if (EFI_ERROR (Status
)) {
320 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode() : Error MmcReadyState\n"));
324 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD2
, 0);
325 if (EFI_ERROR (Status
)) {
326 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode(MMC_CMD2): Error\n"));
329 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_CID
, Response
);
332 Status
= MmcNotifyState (MmcHostInstance
, MmcIdentificationState
);
333 if (EFI_ERROR (Status
)) {
334 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode() : Error MmcIdentificationState\n"));
339 // Note, SD specifications say that "if the command execution causes a state change, it
340 // will be visible to the host in the response to the next command"
341 // The status returned for this CMD3 will be 2 - identification
344 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD3
, CmdArg
);
345 if (EFI_ERROR (Status
)) {
346 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode(MMC_CMD3): Error\n"));
350 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_RCA
, Response
);
351 PrintRCA (Response
[0]);
353 // For MMC card, RCA is assigned by CMD3 while CMD3 dumps the RCA for SD card
354 if (MmcHostInstance
->CardInfo
.CardType
!= MMC_CARD
) {
355 MmcHostInstance
->CardInfo
.RCA
= Response
[0] >> 16;
357 MmcHostInstance
->CardInfo
.RCA
= CmdArg
;
360 Status
= MmcNotifyState (MmcHostInstance
, MmcStandByState
);
361 if (EFI_ERROR (Status
)) {
362 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode() : Error MmcStandByState\n"));
369 EFI_STATUS
InitializeMmcDevice (
370 IN MMC_HOST_INSTANCE
*MmcHostInstance
375 UINTN CardSize
, NumBlocks
, BlockSize
, CmdArg
;
376 EFI_MMC_HOST_PROTOCOL
*MmcHost
;
377 UINTN BlockCount
= 1;
379 MmcHost
= MmcHostInstance
->MmcHost
;
381 MmcIdentificationMode (MmcHostInstance
);
383 //Send a command to get Card specific data
384 CmdArg
= MmcHostInstance
->CardInfo
.RCA
<< 16;
385 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD9
, CmdArg
);
386 if (EFI_ERROR (Status
)) {
387 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode(MMC_CMD9): Error, Status=%r\n", Status
));
391 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_CSD
, Response
);
394 if (MmcHostInstance
->CardInfo
.CardType
== SD_CARD_2_HIGH
) {
395 CardSize
= HC_MMC_CSD_GET_DEVICESIZE (Response
);
396 NumBlocks
= ((CardSize
+ 1) * 1024);
397 BlockSize
= 1 << MMC_CSD_GET_READBLLEN(Response
);
399 CardSize
= MMC_CSD_GET_DEVICESIZE (Response
);
400 NumBlocks
= (CardSize
+ 1) * (1 << (MMC_CSD_GET_DEVICESIZEMULT (Response
) + 2));
401 BlockSize
= 1 << MMC_CSD_GET_READBLLEN(Response
);
404 //For >=2G card, BlockSize may be 1K, but the transfer size is 512 bytes.
405 if (BlockSize
> 512) {
406 NumBlocks
= MultU64x32 (NumBlocks
, BlockSize
/512);
410 MmcHostInstance
->BlockIo
.Media
->LastBlock
= (NumBlocks
- 1);
411 MmcHostInstance
->BlockIo
.Media
->BlockSize
= BlockSize
;
412 MmcHostInstance
->BlockIo
.Media
->ReadOnly
= MmcHost
->IsReadOnly (MmcHost
);
413 MmcHostInstance
->BlockIo
.Media
->MediaPresent
= TRUE
;
414 MmcHostInstance
->BlockIo
.Media
->MediaId
++;
416 CmdArg
= MmcHostInstance
->CardInfo
.RCA
<< 16;
417 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD7
, CmdArg
);
418 if (EFI_ERROR (Status
)) {
419 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode(MMC_CMD7): Error and Status = %r\n", Status
));
423 Status
= MmcNotifyState (MmcHostInstance
, MmcTransferState
);
424 if (EFI_ERROR (Status
)) {
425 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode() : Error MmcTransferState\n"));
430 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD16
, MmcHostInstance
->BlockIo
.Media
->BlockSize
);
431 if (EFI_ERROR (Status
)) {
432 DEBUG ((EFI_D_ERROR
, "MmcIdentificationMode(MMC_CMD16): Error MmcHostInstance->BlockIo.Media->BlockSize: %d and Error = %r\n", MmcHostInstance
->BlockIo
.Media
->BlockSize
, Status
));
436 // Block Count (not used). Could return an error for SD card
437 if (MmcHostInstance
->CardInfo
.CardType
== MMC_CARD
) {
438 MmcHost
->SendCommand (MmcHost
, MMC_CMD23
, BlockCount
);
447 IN EFI_BLOCK_IO_PROTOCOL
*This
,
448 IN BOOLEAN ExtendedVerification
451 MMC_HOST_INSTANCE
*MmcHostInstance
;
453 MmcHostInstance
= MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This
);
455 if (MmcHostInstance
->MmcHost
== NULL
) {
460 // If a card is not present then clear all media settings
461 if (!MmcHostInstance
->MmcHost
->IsCardPresent (MmcHostInstance
->MmcHost
)) {
462 MmcHostInstance
->BlockIo
.Media
->MediaPresent
= FALSE
;
463 MmcHostInstance
->BlockIo
.Media
->LastBlock
= 0;
464 MmcHostInstance
->BlockIo
.Media
->BlockSize
= 512; // Should be zero but there is a bug in DiskIo
465 MmcHostInstance
->BlockIo
.Media
->ReadOnly
= FALSE
;
467 // Indicate that the driver requires initialization
468 MmcHostInstance
->State
= MmcHwInitializationState
;
473 // Implement me. Either send a CMD0 (could not work for some MMC host) or just turn off/turn
474 // on power and restart Identification mode
480 EFI_MMC_HOST_PROTOCOL
*MmcHost
483 if (!MmcHost
->IsCardPresent (MmcHost
)) {
490 #define MMCI0_BLOCKLEN 512
491 #define MMCI0_TIMEOUT 10000
495 IN EFI_BLOCK_IO_PROTOCOL
*This
,
508 MMC_HOST_INSTANCE
*MmcHostInstance
;
509 EFI_MMC_HOST_PROTOCOL
*MmcHost
;
510 UINTN BytesRemainingToBeTransfered
;
511 UINTN BlockCount
= 1;
513 MmcHostInstance
= MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This
);
514 ASSERT (MmcHostInstance
!= 0);
515 MmcHost
= MmcHostInstance
->MmcHost
;
518 if ((MmcHost
== 0)|| (Buffer
== NULL
)) {
519 return EFI_INVALID_PARAMETER
;
522 // Check if a Card is Present
523 if (!MmcHostInstance
->BlockIo
.Media
->MediaPresent
) {
527 // All blocks must be within the device
528 if ((Lba
+ (BufferSize
/ This
->Media
->BlockSize
)) > (This
->Media
->LastBlock
+ 1)){
529 return EFI_INVALID_PARAMETER
;
532 // The buffer size must not be zero and it must be an exact multiple of the block size
533 if ((BufferSize
== 0) || ((BufferSize
% This
->Media
->BlockSize
) != 0)) {
534 return EFI_BAD_BUFFER_SIZE
;
537 if (This
->Media
->MediaId
!= MediaId
) {
538 return EFI_MEDIA_CHANGED
;
541 if((Transfer
== MMC_IOBLOCKS_WRITE
) && (This
->Media
->ReadOnly
== TRUE
)) {
542 return EFI_WRITE_PROTECTED
;
545 BytesRemainingToBeTransfered
= BufferSize
;
546 while (BytesRemainingToBeTransfered
> 0) {
548 // Check if the Card is in Ready status
549 CmdArg
= MmcHostInstance
->CardInfo
.RCA
<< 16;
552 while( (!(Response
[0] & MMC_R0_READY_FOR_DATA
))
553 && (MMC_R0_CURRENTSTATE (Response
) != MMC_R0_STATE_TRAN
)
555 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD13
, CmdArg
);
556 if (!EFI_ERROR (Status
)) {
557 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_R1
, Response
);
562 DEBUG ((EFI_D_ERROR
, "The Card is busy\n"));
563 return EFI_NOT_READY
;
566 //Set command argument based on the card access mode (Byte mode or Block mode)
567 if (MmcHostInstance
->CardInfo
.OCRData
.AccessMode
& BIT1
) {
570 CmdArg
= Lba
* This
->Media
->BlockSize
;
573 if (Transfer
== MMC_IOBLOCKS_READ
) {
575 // Read a single block
578 //TODO: Should we support read stream (MMC_CMD11)
582 // Write a single block
585 //TODO: Should we support write stream (MMC_CMD20)
588 Status
= MmcHost
->SendCommand (MmcHost
, Cmd
, CmdArg
);
589 if (EFI_ERROR (Status
)) {
590 DEBUG ((EFI_D_ERROR
, "MmcIoBlocks(MMC_CMD%d): Error %r\n", Cmd
, Status
));
594 if (Transfer
== MMC_IOBLOCKS_READ
) {
596 // Read one block of Data
597 Status
= MmcHost
->ReadBlockData (MmcHost
, Lba
, This
->Media
->BlockSize
, Buffer
);
598 if (EFI_ERROR (Status
)) {
599 DEBUG ((EFI_D_BLKIO
, "MmcIoBlocks(): Error Read Block Data and Status = %r\n", Status
));
603 //TODO: Read a stream
606 Status
= MmcNotifyState (MmcHostInstance
, MmcProgrammingState
);
607 if (EFI_ERROR (Status
)) {
608 DEBUG ((EFI_D_ERROR
, "MmcIoBlocks() : Error MmcProgrammingState\n"));
613 // Write one block of Data
614 Status
= MmcHost
->WriteBlockData (MmcHost
, Lba
, This
->Media
->BlockSize
, Buffer
);
615 if (EFI_ERROR (Status
)) {
616 DEBUG ((EFI_D_BLKIO
, "MmcIoBlocks(): Error Write Block Data and Status = %r\n", Status
));
620 //TODO: Write a stream
625 // Command 12 - Stop transmission (ends read)
626 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD12
, 0);
627 if (!EFI_ERROR (Status
)) {
628 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_R1b
, Response
);
631 // Command 13 - Read status and wait for programming to complete (return to tran)
632 Timeout
= MMCI0_TIMEOUT
;
633 CmdArg
= MmcHostInstance
->CardInfo
.RCA
<< 16;
635 while( (!(Response
[0] & MMC_R0_READY_FOR_DATA
))
636 && (MMC_R0_CURRENTSTATE (Response
) != MMC_R0_STATE_TRAN
)
638 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD13
, CmdArg
);
639 if (!EFI_ERROR (Status
)) {
640 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_R1
, Response
);
641 if ((Response
[0] & MMC_R0_READY_FOR_DATA
)) {
642 break; // Prevents delay once finished
645 NanoSecondDelay (100);
648 Status
= MmcNotifyState (MmcHostInstance
, MmcTransferState
);
649 if (EFI_ERROR (Status
)) {
650 DEBUG ((EFI_D_ERROR
, "MmcIoBlocks() : Error MmcTransferState\n"));
654 BytesRemainingToBeTransfered
-= This
->Media
->BlockSize
;
656 Buffer
= (UINT8
*)Buffer
+ This
->Media
->BlockSize
;
665 IN EFI_BLOCK_IO_PROTOCOL
*This
,
672 return MmcIoBlocks (This
, MMC_IOBLOCKS_READ
, MediaId
, Lba
, BufferSize
, Buffer
);
678 IN EFI_BLOCK_IO_PROTOCOL
*This
,
685 return MmcIoBlocks (This
, MMC_IOBLOCKS_WRITE
, MediaId
, Lba
, BufferSize
, Buffer
);
691 IN EFI_BLOCK_IO_PROTOCOL
*This