2011-02-02 23:35:30 +01:00
|
|
|
/** @file
|
|
|
|
*
|
2015-05-06 13:15:44 +02:00
|
|
|
* Copyright (c) 2011-2015, ARM Limited. All rights reserved.
|
2011-02-02 23:35:30 +01:00
|
|
|
*
|
2013-08-06 14:09:33 +02:00
|
|
|
* This program and the accompanying materials
|
|
|
|
* are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
* which accompanies this distribution. The full text of the license may be found at
|
|
|
|
* http://opensource.org/licenses/bsd-license.php
|
|
|
|
*
|
|
|
|
* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
2011-02-02 23:35:30 +01:00
|
|
|
*
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include <Library/BaseMemoryLib.h>
|
|
|
|
|
|
|
|
#include "Mmc.h"
|
|
|
|
|
|
|
|
EFI_STATUS
|
|
|
|
MmcNotifyState (
|
2011-06-03 11:10:26 +02:00
|
|
|
IN MMC_HOST_INSTANCE *MmcHostInstance,
|
|
|
|
IN MMC_STATE State
|
|
|
|
)
|
|
|
|
{
|
|
|
|
MmcHostInstance->State = State;
|
2011-09-01 19:08:41 +02:00
|
|
|
return MmcHostInstance->MmcHost->NotifyState (MmcHostInstance->MmcHost, State);
|
2011-02-02 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
2011-03-31 13:16:02 +02:00
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
2013-08-06 14:09:33 +02:00
|
|
|
MmcGetCardStatus (
|
2013-08-30 13:51:51 +02:00
|
|
|
IN MMC_HOST_INSTANCE *MmcHostInstance
|
2011-06-03 11:10:26 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT32 Response[4];
|
|
|
|
UINTN CmdArg;
|
|
|
|
EFI_MMC_HOST_PROTOCOL *MmcHost;
|
|
|
|
|
|
|
|
Status = EFI_SUCCESS;
|
|
|
|
MmcHost = MmcHostInstance->MmcHost;
|
|
|
|
CmdArg = 0;
|
|
|
|
|
|
|
|
if (MmcHost == NULL) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
2013-08-06 14:09:33 +02:00
|
|
|
if (MmcHostInstance->State != MmcHwInitializationState) {
|
2011-06-03 11:10:26 +02:00
|
|
|
//Get the Status of the card.
|
|
|
|
CmdArg = MmcHostInstance->CardInfo.RCA << 16;
|
2011-09-01 19:08:41 +02:00
|
|
|
Status = MmcHost->SendCommand (MmcHost, MMC_CMD13, CmdArg);
|
2013-03-12 01:45:29 +01:00
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
DEBUG ((EFI_D_ERROR, "MmcGetCardStatus(MMC_CMD13): Error and Status = %r\n", Status));
|
2011-06-03 11:10:26 +02:00
|
|
|
return Status;
|
2011-03-31 13:16:02 +02:00
|
|
|
}
|
|
|
|
|
2011-06-03 11:10:26 +02:00
|
|
|
//Read Response
|
2013-03-12 01:45:29 +01:00
|
|
|
MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1, Response);
|
|
|
|
PrintResponseR1 (Response[0]);
|
2011-06-03 11:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
2011-02-02 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
|
|
|
MmcReset (
|
|
|
|
IN EFI_BLOCK_IO_PROTOCOL *This,
|
|
|
|
IN BOOLEAN ExtendedVerification
|
2011-06-03 11:10:26 +02:00
|
|
|
)
|
|
|
|
{
|
2011-08-15 18:04:14 +02:00
|
|
|
MMC_HOST_INSTANCE *MmcHostInstance;
|
|
|
|
|
2013-03-12 01:45:29 +01:00
|
|
|
MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This);
|
2011-08-15 18:04:14 +02:00
|
|
|
|
|
|
|
if (MmcHostInstance->MmcHost == NULL) {
|
|
|
|
// Nothing to do
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a card is not present then clear all media settings
|
2011-09-01 19:08:41 +02:00
|
|
|
if (!MmcHostInstance->MmcHost->IsCardPresent (MmcHostInstance->MmcHost)) {
|
2011-08-15 18:04:14 +02:00
|
|
|
MmcHostInstance->BlockIo.Media->MediaPresent = FALSE;
|
|
|
|
MmcHostInstance->BlockIo.Media->LastBlock = 0;
|
|
|
|
MmcHostInstance->BlockIo.Media->BlockSize = 512; // Should be zero but there is a bug in DiskIo
|
|
|
|
MmcHostInstance->BlockIo.Media->ReadOnly = FALSE;
|
|
|
|
|
|
|
|
// Indicate that the driver requires initialization
|
|
|
|
MmcHostInstance->State = MmcHwInitializationState;
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2011-06-03 11:10:26 +02:00
|
|
|
// Implement me. Either send a CMD0 (could not work for some MMC host) or just turn off/turn
|
|
|
|
// on power and restart Identification mode
|
|
|
|
return EFI_SUCCESS;
|
2011-02-02 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EFI_STATUS
|
|
|
|
MmcDetectCard (
|
|
|
|
EFI_MMC_HOST_PROTOCOL *MmcHost
|
|
|
|
)
|
|
|
|
{
|
2011-09-01 19:08:41 +02:00
|
|
|
if (!MmcHost->IsCardPresent (MmcHost)) {
|
2011-06-03 11:10:26 +02:00
|
|
|
return EFI_NO_MEDIA;
|
|
|
|
} else {
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
2011-02-02 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
2013-08-06 14:10:25 +02:00
|
|
|
EFI_STATUS
|
|
|
|
MmcStopTransmission (
|
|
|
|
EFI_MMC_HOST_PROTOCOL *MmcHost
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT32 Response[4];
|
|
|
|
// Command 12 - Stop transmission (ends read or write)
|
|
|
|
// Normally only needed for streaming transfers or after error.
|
|
|
|
Status = MmcHost->SendCommand (MmcHost, MMC_CMD12, 0);
|
|
|
|
if (!EFI_ERROR (Status)) {
|
|
|
|
MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1b, Response);
|
|
|
|
}
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2011-02-02 23:35:30 +01:00
|
|
|
#define MMCI0_BLOCKLEN 512
|
|
|
|
#define MMCI0_TIMEOUT 10000
|
|
|
|
|
2016-11-23 14:36:24 +01:00
|
|
|
STATIC
|
|
|
|
EFI_STATUS
|
|
|
|
MmcTransferBlock (
|
|
|
|
IN EFI_BLOCK_IO_PROTOCOL *This,
|
|
|
|
IN UINTN Cmd,
|
|
|
|
IN UINTN Transfer,
|
|
|
|
IN UINT32 MediaId,
|
|
|
|
IN EFI_LBA Lba,
|
|
|
|
IN UINTN BufferSize,
|
|
|
|
OUT VOID *Buffer
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINTN CmdArg;
|
|
|
|
INTN Timeout;
|
|
|
|
UINT32 Response[4];
|
|
|
|
MMC_HOST_INSTANCE *MmcHostInstance;
|
|
|
|
EFI_MMC_HOST_PROTOCOL *MmcHost;
|
|
|
|
|
|
|
|
MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This);
|
|
|
|
MmcHost = MmcHostInstance->MmcHost;
|
|
|
|
|
|
|
|
//Set command argument based on the card access mode (Byte mode or Block mode)
|
|
|
|
if ((MmcHostInstance->CardInfo.OCRData.AccessMode & MMC_OCR_ACCESS_MASK) ==
|
|
|
|
MMC_OCR_ACCESS_SECTOR) {
|
|
|
|
CmdArg = Lba;
|
|
|
|
} else {
|
|
|
|
CmdArg = Lba * This->Media->BlockSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = MmcHost->SendCommand (MmcHost, Cmd, CmdArg);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
DEBUG ((EFI_D_ERROR, "%a(MMC_CMD%d): Error %r\n", __func__, Cmd, Status));
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Transfer == MMC_IOBLOCKS_READ) {
|
|
|
|
// Read Data
|
|
|
|
Status = MmcHost->ReadBlockData (MmcHost, Lba, BufferSize, Buffer);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
DEBUG ((EFI_D_BLKIO, "%a(): Error Read Block Data and Status = %r\n", __func__, Status));
|
|
|
|
MmcStopTransmission (MmcHost);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
Status = MmcNotifyState (MmcHostInstance, MmcProgrammingState);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
DEBUG ((EFI_D_ERROR, "%a() : Error MmcProgrammingState\n", __func__));
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Write Data
|
|
|
|
Status = MmcHost->WriteBlockData (MmcHost, Lba, BufferSize, Buffer);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
DEBUG ((EFI_D_BLKIO, "%a(): Error Write Block Data and Status = %r\n", __func__, Status));
|
|
|
|
MmcStopTransmission (MmcHost);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command 13 - Read status and wait for programming to complete (return to tran)
|
|
|
|
Timeout = MMCI0_TIMEOUT;
|
|
|
|
CmdArg = MmcHostInstance->CardInfo.RCA << 16;
|
|
|
|
Response[0] = 0;
|
|
|
|
while(!(Response[0] & MMC_R0_READY_FOR_DATA)
|
|
|
|
&& (MMC_R0_CURRENTSTATE (Response) != MMC_R0_STATE_TRAN)
|
|
|
|
&& Timeout--) {
|
|
|
|
Status = MmcHost->SendCommand (MmcHost, MMC_CMD13, CmdArg);
|
|
|
|
if (!EFI_ERROR (Status)) {
|
|
|
|
MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1, Response);
|
|
|
|
if (Response[0] & MMC_R0_READY_FOR_DATA) {
|
|
|
|
break; // Prevents delay once finished
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BufferSize > This->Media->BlockSize) {
|
|
|
|
Status = MmcHost->SendCommand (MmcHost, MMC_CMD12, 0);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
DEBUG ((EFI_D_BLKIO, "%a(): Error and Status:%r\n", __func__, Status));
|
|
|
|
}
|
2017-08-30 16:20:58 +02:00
|
|
|
MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1b, Response);
|
2016-11-23 14:36:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Status = MmcNotifyState (MmcHostInstance, MmcTransferState);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
DEBUG ((EFI_D_ERROR, "MmcIoBlocks() : Error MmcTransferState\n"));
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2011-06-03 11:10:26 +02:00
|
|
|
EFI_STATUS
|
|
|
|
MmcIoBlocks (
|
2011-02-02 23:35:30 +01:00
|
|
|
IN EFI_BLOCK_IO_PROTOCOL *This,
|
|
|
|
IN UINTN Transfer,
|
|
|
|
IN UINT32 MediaId,
|
|
|
|
IN EFI_LBA Lba,
|
|
|
|
IN UINTN BufferSize,
|
|
|
|
OUT VOID *Buffer
|
2011-06-03 11:10:26 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
UINT32 Response[4];
|
|
|
|
EFI_STATUS Status;
|
2011-08-30 20:02:38 +02:00
|
|
|
UINTN CmdArg;
|
2011-08-15 18:06:53 +02:00
|
|
|
INTN Timeout;
|
2011-06-03 11:10:26 +02:00
|
|
|
UINTN Cmd;
|
|
|
|
MMC_HOST_INSTANCE *MmcHostInstance;
|
|
|
|
EFI_MMC_HOST_PROTOCOL *MmcHost;
|
|
|
|
UINTN BytesRemainingToBeTransfered;
|
2013-08-06 14:09:33 +02:00
|
|
|
UINTN BlockCount;
|
2016-11-23 14:36:24 +01:00
|
|
|
UINTN ConsumeSize;
|
2011-06-03 11:10:26 +02:00
|
|
|
|
2013-08-06 14:09:33 +02:00
|
|
|
BlockCount = 1;
|
2013-03-12 01:45:29 +01:00
|
|
|
MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This);
|
2013-08-06 14:09:33 +02:00
|
|
|
ASSERT (MmcHostInstance != NULL);
|
2011-06-03 11:10:26 +02:00
|
|
|
MmcHost = MmcHostInstance->MmcHost;
|
2013-03-12 01:45:29 +01:00
|
|
|
ASSERT (MmcHost);
|
2011-06-03 11:10:26 +02:00
|
|
|
|
2013-04-14 11:26:28 +02:00
|
|
|
if (This->Media->MediaId != MediaId) {
|
|
|
|
return EFI_MEDIA_CHANGED;
|
|
|
|
}
|
|
|
|
|
2013-08-06 14:09:33 +02:00
|
|
|
if ((MmcHost == NULL) || (Buffer == NULL)) {
|
2011-06-03 11:10:26 +02:00
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a Card is Present
|
2011-08-15 18:04:14 +02:00
|
|
|
if (!MmcHostInstance->BlockIo.Media->MediaPresent) {
|
2011-06-03 11:10:26 +02:00
|
|
|
return EFI_NO_MEDIA;
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:36:24 +01:00
|
|
|
if (MMC_HOST_HAS_ISMULTIBLOCK(MmcHost) && MmcHost->IsMultiBlock(MmcHost)) {
|
|
|
|
BlockCount = (BufferSize + This->Media->BlockSize - 1) / This->Media->BlockSize;
|
|
|
|
}
|
|
|
|
|
2011-08-15 18:06:53 +02:00
|
|
|
// All blocks must be within the device
|
2013-08-06 14:09:33 +02:00
|
|
|
if ((Lba + (BufferSize / This->Media->BlockSize)) > (This->Media->LastBlock + 1)) {
|
2011-08-15 18:06:53 +02:00
|
|
|
return EFI_INVALID_PARAMETER;
|
2011-06-03 11:10:26 +02:00
|
|
|
}
|
|
|
|
|
2013-08-06 14:09:33 +02:00
|
|
|
if ((Transfer == MMC_IOBLOCKS_WRITE) && (This->Media->ReadOnly == TRUE)) {
|
2013-04-14 11:26:28 +02:00
|
|
|
return EFI_WRITE_PROTECTED;
|
2011-08-15 18:06:53 +02:00
|
|
|
}
|
|
|
|
|
2013-04-14 11:26:28 +02:00
|
|
|
// Reading 0 Byte is valid
|
|
|
|
if (BufferSize == 0) {
|
|
|
|
return EFI_SUCCESS;
|
2011-08-15 18:06:53 +02:00
|
|
|
}
|
|
|
|
|
2013-04-14 11:26:28 +02:00
|
|
|
// The buffer size must be an exact multiple of the block size
|
|
|
|
if ((BufferSize % This->Media->BlockSize) != 0) {
|
|
|
|
return EFI_BAD_BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the alignment
|
|
|
|
if ((This->Media->IoAlign > 2) && (((UINTN)Buffer & (This->Media->IoAlign - 1)) != 0)) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
2011-06-03 11:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BytesRemainingToBeTransfered = BufferSize;
|
|
|
|
while (BytesRemainingToBeTransfered > 0) {
|
|
|
|
|
|
|
|
// Check if the Card is in Ready status
|
|
|
|
CmdArg = MmcHostInstance->CardInfo.RCA << 16;
|
|
|
|
Response[0] = 0;
|
|
|
|
Timeout = 20;
|
2012-08-02 12:23:41 +02:00
|
|
|
while( (!(Response[0] & MMC_R0_READY_FOR_DATA))
|
2013-03-12 01:45:29 +01:00
|
|
|
&& (MMC_R0_CURRENTSTATE (Response) != MMC_R0_STATE_TRAN)
|
2012-08-02 12:23:41 +02:00
|
|
|
&& Timeout--) {
|
2011-09-01 19:08:41 +02:00
|
|
|
Status = MmcHost->SendCommand (MmcHost, MMC_CMD13, CmdArg);
|
2013-03-12 01:45:29 +01:00
|
|
|
if (!EFI_ERROR (Status)) {
|
|
|
|
MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1, Response);
|
2011-06-03 11:10:26 +02:00
|
|
|
}
|
2011-02-02 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
2011-06-03 11:10:26 +02:00
|
|
|
if (0 == Timeout) {
|
2013-03-12 01:45:29 +01:00
|
|
|
DEBUG ((EFI_D_ERROR, "The Card is busy\n"));
|
2011-06-03 11:10:26 +02:00
|
|
|
return EFI_NOT_READY;
|
2011-06-03 11:09:18 +02:00
|
|
|
}
|
|
|
|
|
2011-06-03 11:10:26 +02:00
|
|
|
if (Transfer == MMC_IOBLOCKS_READ) {
|
2016-11-23 14:36:24 +01:00
|
|
|
if (BlockCount == 1) {
|
|
|
|
// Read a single block
|
|
|
|
Cmd = MMC_CMD17;
|
|
|
|
} else {
|
|
|
|
// Read multiple blocks
|
|
|
|
Cmd = MMC_CMD18;
|
2011-06-03 11:10:26 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-11-23 14:36:24 +01:00
|
|
|
if (BlockCount == 1) {
|
|
|
|
// Write a single block
|
|
|
|
Cmd = MMC_CMD24;
|
|
|
|
} else {
|
|
|
|
// Write multiple blocks
|
|
|
|
Cmd = MMC_CMD25;
|
2011-06-03 11:10:26 +02:00
|
|
|
}
|
|
|
|
}
|
2011-02-02 23:35:30 +01:00
|
|
|
|
2016-11-23 14:36:24 +01:00
|
|
|
ConsumeSize = BlockCount * This->Media->BlockSize;
|
|
|
|
if (BytesRemainingToBeTransfered < ConsumeSize) {
|
|
|
|
ConsumeSize = BytesRemainingToBeTransfered;
|
2011-06-03 11:10:26 +02:00
|
|
|
}
|
2016-11-23 14:36:24 +01:00
|
|
|
Status = MmcTransferBlock (This, Cmd, Transfer, MediaId, Lba, ConsumeSize, Buffer);
|
2013-03-12 01:45:29 +01:00
|
|
|
if (EFI_ERROR (Status)) {
|
2016-11-23 14:36:24 +01:00
|
|
|
DEBUG ((EFI_D_ERROR, "%a(): Failed to transfer block and Status:%r\n", __func__, Status));
|
2011-02-02 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
2016-11-23 14:36:24 +01:00
|
|
|
BytesRemainingToBeTransfered -= ConsumeSize;
|
|
|
|
if (BytesRemainingToBeTransfered > 0) {
|
|
|
|
Lba += BlockCount;
|
|
|
|
Buffer = (UINT8 *)Buffer + ConsumeSize;
|
|
|
|
}
|
2011-06-03 11:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
2011-02-02 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
|
|
|
MmcReadBlocks (
|
|
|
|
IN EFI_BLOCK_IO_PROTOCOL *This,
|
|
|
|
IN UINT32 MediaId,
|
|
|
|
IN EFI_LBA Lba,
|
|
|
|
IN UINTN BufferSize,
|
|
|
|
OUT VOID *Buffer
|
2011-06-03 11:10:26 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return MmcIoBlocks (This, MMC_IOBLOCKS_READ, MediaId, Lba, BufferSize, Buffer);
|
2011-02-02 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
|
|
|
MmcWriteBlocks (
|
|
|
|
IN EFI_BLOCK_IO_PROTOCOL *This,
|
|
|
|
IN UINT32 MediaId,
|
|
|
|
IN EFI_LBA Lba,
|
|
|
|
IN UINTN BufferSize,
|
|
|
|
IN VOID *Buffer
|
2011-06-03 11:10:26 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return MmcIoBlocks (This, MMC_IOBLOCKS_WRITE, MediaId, Lba, BufferSize, Buffer);
|
2011-02-02 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
|
|
|
MmcFlushBlocks (
|
|
|
|
IN EFI_BLOCK_IO_PROTOCOL *This
|
2011-06-03 11:10:26 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return EFI_SUCCESS;
|
2011-02-02 23:35:30 +01:00
|
|
|
}
|