3 * Copyright (c) 2011-2014, 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 <Library/BaseMemoryLib.h>
16 #include <Library/TimerLib.h>
22 IN MMC_HOST_INSTANCE
*MmcHostInstance
,
26 MmcHostInstance
->State
= State
;
27 return MmcHostInstance
->MmcHost
->NotifyState (MmcHostInstance
->MmcHost
, State
);
33 IN MMC_HOST_INSTANCE
*MmcHostInstance
39 EFI_MMC_HOST_PROTOCOL
*MmcHost
;
42 MmcHost
= MmcHostInstance
->MmcHost
;
45 if (MmcHost
== NULL
) {
46 return EFI_INVALID_PARAMETER
;
48 if (MmcHostInstance
->State
!= MmcHwInitializationState
) {
49 //Get the Status of the card.
50 CmdArg
= MmcHostInstance
->CardInfo
.RCA
<< 16;
51 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD13
, CmdArg
);
52 if (EFI_ERROR (Status
)) {
53 DEBUG ((EFI_D_ERROR
, "MmcGetCardStatus(MMC_CMD13): Error and Status = %r\n", Status
));
58 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_R1
, Response
);
59 PrintResponseR1 (Response
[0]);
68 IN EFI_BLOCK_IO_PROTOCOL
*This
,
69 IN BOOLEAN ExtendedVerification
72 MMC_HOST_INSTANCE
*MmcHostInstance
;
74 MmcHostInstance
= MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This
);
76 if (MmcHostInstance
->MmcHost
== NULL
) {
81 // If a card is not present then clear all media settings
82 if (!MmcHostInstance
->MmcHost
->IsCardPresent (MmcHostInstance
->MmcHost
)) {
83 MmcHostInstance
->BlockIo
.Media
->MediaPresent
= FALSE
;
84 MmcHostInstance
->BlockIo
.Media
->LastBlock
= 0;
85 MmcHostInstance
->BlockIo
.Media
->BlockSize
= 512; // Should be zero but there is a bug in DiskIo
86 MmcHostInstance
->BlockIo
.Media
->ReadOnly
= FALSE
;
88 // Indicate that the driver requires initialization
89 MmcHostInstance
->State
= MmcHwInitializationState
;
94 // Implement me. Either send a CMD0 (could not work for some MMC host) or just turn off/turn
95 // on power and restart Identification mode
101 EFI_MMC_HOST_PROTOCOL
*MmcHost
104 if (!MmcHost
->IsCardPresent (MmcHost
)) {
112 MmcStopTransmission (
113 EFI_MMC_HOST_PROTOCOL
*MmcHost
118 // Command 12 - Stop transmission (ends read or write)
119 // Normally only needed for streaming transfers or after error.
120 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD12
, 0);
121 if (!EFI_ERROR (Status
)) {
122 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_R1b
, Response
);
127 #define MMCI0_BLOCKLEN 512
128 #define MMCI0_TIMEOUT 10000
132 IN EFI_BLOCK_IO_PROTOCOL
*This
,
145 MMC_HOST_INSTANCE
*MmcHostInstance
;
146 EFI_MMC_HOST_PROTOCOL
*MmcHost
;
147 UINTN BytesRemainingToBeTransfered
;
151 MmcHostInstance
= MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This
);
152 ASSERT (MmcHostInstance
!= NULL
);
153 MmcHost
= MmcHostInstance
->MmcHost
;
156 if (This
->Media
->MediaId
!= MediaId
) {
157 return EFI_MEDIA_CHANGED
;
160 if ((MmcHost
== NULL
) || (Buffer
== NULL
)) {
161 return EFI_INVALID_PARAMETER
;
164 // Check if a Card is Present
165 if (!MmcHostInstance
->BlockIo
.Media
->MediaPresent
) {
169 // All blocks must be within the device
170 if ((Lba
+ (BufferSize
/ This
->Media
->BlockSize
)) > (This
->Media
->LastBlock
+ 1)) {
171 return EFI_INVALID_PARAMETER
;
174 if ((Transfer
== MMC_IOBLOCKS_WRITE
) && (This
->Media
->ReadOnly
== TRUE
)) {
175 return EFI_WRITE_PROTECTED
;
178 // Reading 0 Byte is valid
179 if (BufferSize
== 0) {
183 // The buffer size must be an exact multiple of the block size
184 if ((BufferSize
% This
->Media
->BlockSize
) != 0) {
185 return EFI_BAD_BUFFER_SIZE
;
188 // Check the alignment
189 if ((This
->Media
->IoAlign
> 2) && (((UINTN
)Buffer
& (This
->Media
->IoAlign
- 1)) != 0)) {
190 return EFI_INVALID_PARAMETER
;
193 BytesRemainingToBeTransfered
= BufferSize
;
194 while (BytesRemainingToBeTransfered
> 0) {
196 // Check if the Card is in Ready status
197 CmdArg
= MmcHostInstance
->CardInfo
.RCA
<< 16;
200 while( (!(Response
[0] & MMC_R0_READY_FOR_DATA
))
201 && (MMC_R0_CURRENTSTATE (Response
) != MMC_R0_STATE_TRAN
)
203 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD13
, CmdArg
);
204 if (!EFI_ERROR (Status
)) {
205 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_R1
, Response
);
210 DEBUG ((EFI_D_ERROR
, "The Card is busy\n"));
211 return EFI_NOT_READY
;
214 //Set command argument based on the card access mode (Byte mode or Block mode)
215 if (MmcHostInstance
->CardInfo
.OCRData
.AccessMode
& BIT1
) {
218 CmdArg
= Lba
* This
->Media
->BlockSize
;
221 if (Transfer
== MMC_IOBLOCKS_READ
) {
222 // Read a single block
225 // Write a single block
228 Status
= MmcHost
->SendCommand (MmcHost
, Cmd
, CmdArg
);
229 if (EFI_ERROR (Status
)) {
230 DEBUG ((EFI_D_ERROR
, "MmcIoBlocks(MMC_CMD%d): Error %r\n", Cmd
, Status
));
234 if (Transfer
== MMC_IOBLOCKS_READ
) {
235 // Read one block of Data
236 Status
= MmcHost
->ReadBlockData (MmcHost
, Lba
, This
->Media
->BlockSize
, Buffer
);
237 if (EFI_ERROR (Status
)) {
238 DEBUG ((EFI_D_BLKIO
, "MmcIoBlocks(): Error Read Block Data and Status = %r\n", Status
));
239 MmcStopTransmission (MmcHost
);
242 Status
= MmcNotifyState (MmcHostInstance
, MmcProgrammingState
);
243 if (EFI_ERROR (Status
)) {
244 DEBUG ((EFI_D_ERROR
, "MmcIoBlocks() : Error MmcProgrammingState\n"));
248 // Write one block of Data
249 Status
= MmcHost
->WriteBlockData (MmcHost
, Lba
, This
->Media
->BlockSize
, Buffer
);
250 if (EFI_ERROR (Status
)) {
251 DEBUG ((EFI_D_BLKIO
, "MmcIoBlocks(): Error Write Block Data and Status = %r\n", Status
));
252 MmcStopTransmission (MmcHost
);
257 // Command 13 - Read status and wait for programming to complete (return to tran)
258 Timeout
= MMCI0_TIMEOUT
;
259 CmdArg
= MmcHostInstance
->CardInfo
.RCA
<< 16;
261 while( (!(Response
[0] & MMC_R0_READY_FOR_DATA
))
262 && (MMC_R0_CURRENTSTATE (Response
) != MMC_R0_STATE_TRAN
)
264 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD13
, CmdArg
);
265 if (!EFI_ERROR (Status
)) {
266 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_R1
, Response
);
267 if ((Response
[0] & MMC_R0_READY_FOR_DATA
)) {
268 break; // Prevents delay once finished
271 NanoSecondDelay (100);
274 Status
= MmcNotifyState (MmcHostInstance
, MmcTransferState
);
275 if (EFI_ERROR (Status
)) {
276 DEBUG ((EFI_D_ERROR
, "MmcIoBlocks() : Error MmcTransferState\n"));
280 BytesRemainingToBeTransfered
-= This
->Media
->BlockSize
;
282 Buffer
= (UINT8
*)Buffer
+ This
->Media
->BlockSize
;
291 IN EFI_BLOCK_IO_PROTOCOL
*This
,
298 return MmcIoBlocks (This
, MMC_IOBLOCKS_READ
, MediaId
, Lba
, BufferSize
, Buffer
);
304 IN EFI_BLOCK_IO_PROTOCOL
*This
,
311 return MmcIoBlocks (This
, MMC_IOBLOCKS_WRITE
, MediaId
, Lba
, BufferSize
, Buffer
);
317 IN EFI_BLOCK_IO_PROTOCOL
*This