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.
15 #include <Library/BaseMemoryLib.h>
21 IN MMC_HOST_INSTANCE
*MmcHostInstance
,
25 MmcHostInstance
->State
= State
;
26 return MmcHostInstance
->MmcHost
->NotifyState (MmcHostInstance
->MmcHost
, State
);
32 IN MMC_HOST_INSTANCE
*MmcHostInstance
38 EFI_MMC_HOST_PROTOCOL
*MmcHost
;
41 MmcHost
= MmcHostInstance
->MmcHost
;
44 if (MmcHost
== NULL
) {
45 return EFI_INVALID_PARAMETER
;
47 if (MmcHostInstance
->State
!= MmcHwInitializationState
) {
48 //Get the Status of the card.
49 CmdArg
= MmcHostInstance
->CardInfo
.RCA
<< 16;
50 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD13
, CmdArg
);
51 if (EFI_ERROR (Status
)) {
52 DEBUG ((EFI_D_ERROR
, "MmcGetCardStatus(MMC_CMD13): Error and Status = %r\n", Status
));
57 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_R1
, Response
);
58 PrintResponseR1 (Response
[0]);
67 IN EFI_BLOCK_IO_PROTOCOL
*This
,
68 IN BOOLEAN ExtendedVerification
71 MMC_HOST_INSTANCE
*MmcHostInstance
;
73 MmcHostInstance
= MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This
);
75 if (MmcHostInstance
->MmcHost
== NULL
) {
80 // If a card is not present then clear all media settings
81 if (!MmcHostInstance
->MmcHost
->IsCardPresent (MmcHostInstance
->MmcHost
)) {
82 MmcHostInstance
->BlockIo
.Media
->MediaPresent
= FALSE
;
83 MmcHostInstance
->BlockIo
.Media
->LastBlock
= 0;
84 MmcHostInstance
->BlockIo
.Media
->BlockSize
= 512; // Should be zero but there is a bug in DiskIo
85 MmcHostInstance
->BlockIo
.Media
->ReadOnly
= FALSE
;
87 // Indicate that the driver requires initialization
88 MmcHostInstance
->State
= MmcHwInitializationState
;
93 // Implement me. Either send a CMD0 (could not work for some MMC host) or just turn off/turn
94 // on power and restart Identification mode
100 EFI_MMC_HOST_PROTOCOL
*MmcHost
103 if (!MmcHost
->IsCardPresent (MmcHost
)) {
111 MmcStopTransmission (
112 EFI_MMC_HOST_PROTOCOL
*MmcHost
117 // Command 12 - Stop transmission (ends read or write)
118 // Normally only needed for streaming transfers or after error.
119 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD12
, 0);
120 if (!EFI_ERROR (Status
)) {
121 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_R1b
, Response
);
126 #define MMCI0_BLOCKLEN 512
127 #define MMCI0_TIMEOUT 10000
131 IN EFI_BLOCK_IO_PROTOCOL
*This
,
144 MMC_HOST_INSTANCE
*MmcHostInstance
;
145 EFI_MMC_HOST_PROTOCOL
*MmcHost
;
146 UINTN BytesRemainingToBeTransfered
;
150 MmcHostInstance
= MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This
);
151 ASSERT (MmcHostInstance
!= NULL
);
152 MmcHost
= MmcHostInstance
->MmcHost
;
155 if (This
->Media
->MediaId
!= MediaId
) {
156 return EFI_MEDIA_CHANGED
;
159 if ((MmcHost
== NULL
) || (Buffer
== NULL
)) {
160 return EFI_INVALID_PARAMETER
;
163 // Check if a Card is Present
164 if (!MmcHostInstance
->BlockIo
.Media
->MediaPresent
) {
168 // All blocks must be within the device
169 if ((Lba
+ (BufferSize
/ This
->Media
->BlockSize
)) > (This
->Media
->LastBlock
+ 1)) {
170 return EFI_INVALID_PARAMETER
;
173 if ((Transfer
== MMC_IOBLOCKS_WRITE
) && (This
->Media
->ReadOnly
== TRUE
)) {
174 return EFI_WRITE_PROTECTED
;
177 // Reading 0 Byte is valid
178 if (BufferSize
== 0) {
182 // The buffer size must be an exact multiple of the block size
183 if ((BufferSize
% This
->Media
->BlockSize
) != 0) {
184 return EFI_BAD_BUFFER_SIZE
;
187 // Check the alignment
188 if ((This
->Media
->IoAlign
> 2) && (((UINTN
)Buffer
& (This
->Media
->IoAlign
- 1)) != 0)) {
189 return EFI_INVALID_PARAMETER
;
192 BytesRemainingToBeTransfered
= BufferSize
;
193 while (BytesRemainingToBeTransfered
> 0) {
195 // Check if the Card is in Ready status
196 CmdArg
= MmcHostInstance
->CardInfo
.RCA
<< 16;
199 while( (!(Response
[0] & MMC_R0_READY_FOR_DATA
))
200 && (MMC_R0_CURRENTSTATE (Response
) != MMC_R0_STATE_TRAN
)
202 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD13
, CmdArg
);
203 if (!EFI_ERROR (Status
)) {
204 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_R1
, Response
);
209 DEBUG ((EFI_D_ERROR
, "The Card is busy\n"));
210 return EFI_NOT_READY
;
213 //Set command argument based on the card access mode (Byte mode or Block mode)
214 if (MmcHostInstance
->CardInfo
.OCRData
.AccessMode
& BIT1
) {
217 CmdArg
= Lba
* This
->Media
->BlockSize
;
220 if (Transfer
== MMC_IOBLOCKS_READ
) {
221 // Read a single block
224 // Write a single block
227 Status
= MmcHost
->SendCommand (MmcHost
, Cmd
, CmdArg
);
228 if (EFI_ERROR (Status
)) {
229 DEBUG ((EFI_D_ERROR
, "MmcIoBlocks(MMC_CMD%d): Error %r\n", Cmd
, Status
));
233 if (Transfer
== MMC_IOBLOCKS_READ
) {
234 // Read one block of Data
235 Status
= MmcHost
->ReadBlockData (MmcHost
, Lba
, This
->Media
->BlockSize
, Buffer
);
236 if (EFI_ERROR (Status
)) {
237 DEBUG ((EFI_D_BLKIO
, "MmcIoBlocks(): Error Read Block Data and Status = %r\n", Status
));
238 MmcStopTransmission (MmcHost
);
241 Status
= MmcNotifyState (MmcHostInstance
, MmcProgrammingState
);
242 if (EFI_ERROR (Status
)) {
243 DEBUG ((EFI_D_ERROR
, "MmcIoBlocks() : Error MmcProgrammingState\n"));
247 // Write one block of Data
248 Status
= MmcHost
->WriteBlockData (MmcHost
, Lba
, This
->Media
->BlockSize
, Buffer
);
249 if (EFI_ERROR (Status
)) {
250 DEBUG ((EFI_D_BLKIO
, "MmcIoBlocks(): Error Write Block Data and Status = %r\n", Status
));
251 MmcStopTransmission (MmcHost
);
256 // Command 13 - Read status and wait for programming to complete (return to tran)
257 Timeout
= MMCI0_TIMEOUT
;
258 CmdArg
= MmcHostInstance
->CardInfo
.RCA
<< 16;
260 while( (!(Response
[0] & MMC_R0_READY_FOR_DATA
))
261 && (MMC_R0_CURRENTSTATE (Response
) != MMC_R0_STATE_TRAN
)
263 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD13
, CmdArg
);
264 if (!EFI_ERROR (Status
)) {
265 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_R1
, Response
);
266 if ((Response
[0] & MMC_R0_READY_FOR_DATA
)) {
267 break; // Prevents delay once finished
273 Status
= MmcNotifyState (MmcHostInstance
, MmcTransferState
);
274 if (EFI_ERROR (Status
)) {
275 DEBUG ((EFI_D_ERROR
, "MmcIoBlocks() : Error MmcTransferState\n"));
279 BytesRemainingToBeTransfered
-= This
->Media
->BlockSize
;
281 Buffer
= (UINT8
*)Buffer
+ This
->Media
->BlockSize
;
290 IN EFI_BLOCK_IO_PROTOCOL
*This
,
297 return MmcIoBlocks (This
, MMC_IOBLOCKS_READ
, MediaId
, Lba
, BufferSize
, Buffer
);
303 IN EFI_BLOCK_IO_PROTOCOL
*This
,
310 return MmcIoBlocks (This
, MMC_IOBLOCKS_WRITE
, MediaId
, Lba
, BufferSize
, Buffer
);
316 IN EFI_BLOCK_IO_PROTOCOL
*This