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
132 IN EFI_BLOCK_IO_PROTOCOL
*This
,
145 MMC_HOST_INSTANCE
*MmcHostInstance
;
146 EFI_MMC_HOST_PROTOCOL
*MmcHost
;
148 MmcHostInstance
= MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This
);
149 MmcHost
= MmcHostInstance
->MmcHost
;
151 //Set command argument based on the card access mode (Byte mode or Block mode)
152 if ((MmcHostInstance
->CardInfo
.OCRData
.AccessMode
& MMC_OCR_ACCESS_MASK
) ==
153 MMC_OCR_ACCESS_SECTOR
) {
156 CmdArg
= Lba
* This
->Media
->BlockSize
;
159 Status
= MmcHost
->SendCommand (MmcHost
, Cmd
, CmdArg
);
160 if (EFI_ERROR (Status
)) {
161 DEBUG ((EFI_D_ERROR
, "%a(MMC_CMD%d): Error %r\n", __func__
, Cmd
, Status
));
165 if (Transfer
== MMC_IOBLOCKS_READ
) {
167 Status
= MmcHost
->ReadBlockData (MmcHost
, Lba
, BufferSize
, Buffer
);
168 if (EFI_ERROR (Status
)) {
169 DEBUG ((EFI_D_BLKIO
, "%a(): Error Read Block Data and Status = %r\n", __func__
, Status
));
170 MmcStopTransmission (MmcHost
);
173 Status
= MmcNotifyState (MmcHostInstance
, MmcProgrammingState
);
174 if (EFI_ERROR (Status
)) {
175 DEBUG ((EFI_D_ERROR
, "%a() : Error MmcProgrammingState\n", __func__
));
180 Status
= MmcHost
->WriteBlockData (MmcHost
, Lba
, BufferSize
, Buffer
);
181 if (EFI_ERROR (Status
)) {
182 DEBUG ((EFI_D_BLKIO
, "%a(): Error Write Block Data and Status = %r\n", __func__
, Status
));
183 MmcStopTransmission (MmcHost
);
188 // Command 13 - Read status and wait for programming to complete (return to tran)
189 Timeout
= MMCI0_TIMEOUT
;
190 CmdArg
= MmcHostInstance
->CardInfo
.RCA
<< 16;
192 while(!(Response
[0] & MMC_R0_READY_FOR_DATA
)
193 && (MMC_R0_CURRENTSTATE (Response
) != MMC_R0_STATE_TRAN
)
195 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD13
, CmdArg
);
196 if (!EFI_ERROR (Status
)) {
197 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_R1
, Response
);
198 if (Response
[0] & MMC_R0_READY_FOR_DATA
) {
199 break; // Prevents delay once finished
204 if (BufferSize
> This
->Media
->BlockSize
) {
205 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD12
, 0);
206 if (EFI_ERROR (Status
)) {
207 DEBUG ((EFI_D_BLKIO
, "%a(): Error and Status:%r\n", __func__
, Status
));
209 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_R1b
, Response
);
212 Status
= MmcNotifyState (MmcHostInstance
, MmcTransferState
);
213 if (EFI_ERROR (Status
)) {
214 DEBUG ((EFI_D_ERROR
, "MmcIoBlocks() : Error MmcTransferState\n"));
222 IN EFI_BLOCK_IO_PROTOCOL
*This
,
235 MMC_HOST_INSTANCE
*MmcHostInstance
;
236 EFI_MMC_HOST_PROTOCOL
*MmcHost
;
237 UINTN BytesRemainingToBeTransfered
;
242 MmcHostInstance
= MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This
);
243 ASSERT (MmcHostInstance
!= NULL
);
244 MmcHost
= MmcHostInstance
->MmcHost
;
247 if (This
->Media
->MediaId
!= MediaId
) {
248 return EFI_MEDIA_CHANGED
;
251 if ((MmcHost
== NULL
) || (Buffer
== NULL
)) {
252 return EFI_INVALID_PARAMETER
;
255 // Check if a Card is Present
256 if (!MmcHostInstance
->BlockIo
.Media
->MediaPresent
) {
260 if (MMC_HOST_HAS_ISMULTIBLOCK(MmcHost
) && MmcHost
->IsMultiBlock(MmcHost
)) {
261 BlockCount
= (BufferSize
+ This
->Media
->BlockSize
- 1) / This
->Media
->BlockSize
;
264 // All blocks must be within the device
265 if ((Lba
+ (BufferSize
/ This
->Media
->BlockSize
)) > (This
->Media
->LastBlock
+ 1)) {
266 return EFI_INVALID_PARAMETER
;
269 if ((Transfer
== MMC_IOBLOCKS_WRITE
) && (This
->Media
->ReadOnly
== TRUE
)) {
270 return EFI_WRITE_PROTECTED
;
273 // Reading 0 Byte is valid
274 if (BufferSize
== 0) {
278 // The buffer size must be an exact multiple of the block size
279 if ((BufferSize
% This
->Media
->BlockSize
) != 0) {
280 return EFI_BAD_BUFFER_SIZE
;
283 // Check the alignment
284 if ((This
->Media
->IoAlign
> 2) && (((UINTN
)Buffer
& (This
->Media
->IoAlign
- 1)) != 0)) {
285 return EFI_INVALID_PARAMETER
;
288 BytesRemainingToBeTransfered
= BufferSize
;
289 while (BytesRemainingToBeTransfered
> 0) {
291 // Check if the Card is in Ready status
292 CmdArg
= MmcHostInstance
->CardInfo
.RCA
<< 16;
295 while( (!(Response
[0] & MMC_R0_READY_FOR_DATA
))
296 && (MMC_R0_CURRENTSTATE (Response
) != MMC_R0_STATE_TRAN
)
298 Status
= MmcHost
->SendCommand (MmcHost
, MMC_CMD13
, CmdArg
);
299 if (!EFI_ERROR (Status
)) {
300 MmcHost
->ReceiveResponse (MmcHost
, MMC_RESPONSE_TYPE_R1
, Response
);
305 DEBUG ((EFI_D_ERROR
, "The Card is busy\n"));
306 return EFI_NOT_READY
;
309 if (Transfer
== MMC_IOBLOCKS_READ
) {
310 if (BlockCount
== 1) {
311 // Read a single block
314 // Read multiple blocks
318 if (BlockCount
== 1) {
319 // Write a single block
322 // Write multiple blocks
327 ConsumeSize
= BlockCount
* This
->Media
->BlockSize
;
328 if (BytesRemainingToBeTransfered
< ConsumeSize
) {
329 ConsumeSize
= BytesRemainingToBeTransfered
;
331 Status
= MmcTransferBlock (This
, Cmd
, Transfer
, MediaId
, Lba
, ConsumeSize
, Buffer
);
332 if (EFI_ERROR (Status
)) {
333 DEBUG ((EFI_D_ERROR
, "%a(): Failed to transfer block and Status:%r\n", __func__
, Status
));
336 BytesRemainingToBeTransfered
-= ConsumeSize
;
337 if (BytesRemainingToBeTransfered
> 0) {
339 Buffer
= (UINT8
*)Buffer
+ ConsumeSize
;
349 IN EFI_BLOCK_IO_PROTOCOL
*This
,
356 return MmcIoBlocks (This
, MMC_IOBLOCKS_READ
, MediaId
, Lba
, BufferSize
, Buffer
);
362 IN EFI_BLOCK_IO_PROTOCOL
*This
,
369 return MmcIoBlocks (This
, MMC_IOBLOCKS_WRITE
, MediaId
, Lba
, BufferSize
, Buffer
);
375 IN EFI_BLOCK_IO_PROTOCOL
*This