STATIC EFI_STATUS EFIAPI EmmcSetEXTCSD ( IN MMC_HOST_INSTANCE *MmcHostInstance, UINT32 ExtCmdIndex, UINT32 Value ) { EFI_MMC_HOST_PROTOCOL *Host; EMMC_DEVICE_STATE State; EFI_STATUS Status; UINT32 Argument; Host = MmcHostInstance->MmcHost; Argument = EMMC_CMD6_ARG_ACCESS(3) | EMMC_CMD6_ARG_INDEX(ExtCmdIndex) | EMMC_CMD6_ARG_VALUE(Value) | EMMC_CMD6_ARG_CMD_SET(1); Status = Host->SendCommand (Host, MMC_CMD6, Argument); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "EmmcSetEXTCSD(): Failed to send CMD6, Status=%r.\n", Status)); return Status; } // Make sure device exiting prog mode do { Status = EmmcGetDeviceState (MmcHostInstance, &State); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "EmmcSetEXTCSD(): Failed to get device state, Status=%r.\n", Status)); return Status; } } while (State == EMMC_PRG_STATE); return EFI_SUCCESS; }
STATIC EFI_STATUS EFIAPI EmmcGetDeviceState ( IN MMC_HOST_INSTANCE *MmcHostInstance, OUT EMMC_DEVICE_STATE *State ) { EFI_MMC_HOST_PROTOCOL *Host; EFI_STATUS Status; UINT32 Data, RCA; if (State == NULL) { return EFI_INVALID_PARAMETER; } Host = MmcHostInstance->MmcHost; RCA = MmcHostInstance->CardInfo.RCA << RCA_SHIFT_OFFSET; Status = Host->SendCommand (Host, MMC_CMD13, RCA); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "EmmcGetDeviceState(): Failed to get card status, Status=%r.\n", Status)); return Status; } Status = Host->ReceiveResponse (Host, MMC_RESPONSE_TYPE_R1, &Data); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "EmmcGetDeviceState(): Failed to get response of CMD13, Status=%r.\n", Status)); return Status; } if (Data & EMMC_SWITCH_ERROR) { DEBUG ((EFI_D_ERROR, "EmmcGetDeviceState(): Failed to switch expected mode, Status=%r.\n", Status)); return EFI_DEVICE_ERROR; } *State = DEVICE_STATE(Data); return EFI_SUCCESS; }
EFI_STATUS EFIAPI MmcGetCardStatus ( IN MMC_HOST_INSTANCE *MmcHostInstance ) { 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; } if (MmcHostInstance->State != MmcHwInitializationState) { //Get the Status of the card. CmdArg = MmcHostInstance->CardInfo.RCA << 16; Status = MmcHost->SendCommand (MmcHost, MMC_CMD13, CmdArg); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "MmcGetCardStatus(MMC_CMD13): Error and Status = %r\n", Status)); return Status; } //Read Response MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1, Response); PrintResponseR1 (Response[0]); } return Status; }
STATIC EFI_STATUS EFIAPI MmcIdentificationMode ( IN MMC_HOST_INSTANCE *MmcHostInstance ) { EFI_STATUS Status; UINT32 Response[4]; UINTN Timeout; UINTN CmdArg; BOOLEAN IsHCS; EFI_MMC_HOST_PROTOCOL *MmcHost; OCR_RESPONSE OcrResponse; MmcHost = MmcHostInstance->MmcHost; CmdArg = 0; IsHCS = FALSE; if (MmcHost == NULL) { return EFI_INVALID_PARAMETER; } // We can get into this function if we restart the identification mode if (MmcHostInstance->State == MmcHwInitializationState) { // Initialize the MMC Host HW Status = MmcNotifyState (MmcHostInstance, MmcHwInitializationState); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcHwInitializationState, Status=%r.\n", Status)); return Status; } } Status = MmcHost->SendCommand (MmcHost, MMC_CMD0, 0); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD0): Error, Status=%r.\n", Status)); return Status; } Status = MmcNotifyState (MmcHostInstance, MmcIdleState); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcIdleState, Status=%r.\n", Status)); return Status; } // Send CMD1 to get OCR (MMC) // This command only valid for MMC and eMMC Timeout = MAX_RETRY_COUNT; do { Status = MmcHost->SendCommand (MmcHost, MMC_CMD1, EMMC_CMD1_CAPACITY_GREATER_THAN_2GB); if (EFI_ERROR (Status)) break; Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_OCR, (UINT32 *)&OcrResponse); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive OCR, Status=%r.\n", Status)); return Status; } Timeout--; } while (!OcrResponse.Ocr.PowerUp && (Timeout > 0)); if (Status == EFI_SUCCESS) { if (!OcrResponse.Ocr.PowerUp) { DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD1): Card initialisation failure, Status=%r.\n", Status)); return EFI_DEVICE_ERROR; } OcrResponse.Ocr.PowerUp = 0; if (OcrResponse.Raw == EMMC_CMD1_CAPACITY_GREATER_THAN_2GB) { MmcHostInstance->CardInfo.OCRData.AccessMode = BIT1; } else { MmcHostInstance->CardInfo.OCRData.AccessMode = 0x0; } // Check whether MMC or eMMC if (OcrResponse.Raw == EMMC_CMD1_CAPACITY_GREATER_THAN_2GB || OcrResponse.Raw == EMMC_CMD1_CAPACITY_LESS_THAN_2GB) { return EmmcIdentificationMode (MmcHostInstance, OcrResponse); } } // Are we using SDIO ? Status = MmcHost->SendCommand (MmcHost, MMC_CMD5, 0); if (Status == EFI_SUCCESS) { DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD5): Error - SDIO not supported, Status=%r.\n", Status)); return EFI_UNSUPPORTED; } // Check which kind of card we are using. Ver2.00 or later SD Memory Card (PL180 is SD v1.1) CmdArg = (0x0UL << 12 | BIT8 | 0xCEUL << 0); Status = MmcHost->SendCommand (MmcHost, MMC_CMD8, CmdArg); if (Status == EFI_SUCCESS) { DEBUG ((EFI_D_ERROR, "Card is SD2.0 => Supports high capacity\n")); IsHCS = TRUE; Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R7, Response); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive response to CMD8, Status=%r.\n", Status)); return Status; } PrintResponseR1 (Response[0]); // Check if it is valid response if (Response[0] != CmdArg) { DEBUG ((EFI_D_ERROR, "The Card is not usable\n")); return EFI_UNSUPPORTED; } } else { DEBUG ((EFI_D_ERROR, "Not a SD2.0 Card\n")); } // We need to wait for the MMC or SD card is ready => (gCardInfo.OCRData.PowerUp == 1) Timeout = MAX_RETRY_COUNT; while (Timeout > 0) { // SD Card or MMC Card ? CMD55 indicates to the card that the next command is an application specific command Status = MmcHost->SendCommand (MmcHost, MMC_CMD55, 0); if (Status == EFI_SUCCESS) { DEBUG ((EFI_D_INFO, "Card should be SD\n")); if (IsHCS) { MmcHostInstance->CardInfo.CardType = SD_CARD_2; } else { MmcHostInstance->CardInfo.CardType = SD_CARD; } // Note: The first time CmdArg will be zero CmdArg = ((UINTN *) &(MmcHostInstance->CardInfo.OCRData))[0]; if (IsHCS) { CmdArg |= BIT30; } Status = MmcHost->SendCommand (MmcHost, MMC_ACMD41, CmdArg); if (!EFI_ERROR (Status)) { Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_OCR, Response); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive OCR, Status=%r.\n", Status)); return Status; } ((UINT32 *) &(MmcHostInstance->CardInfo.OCRData))[0] = Response[0]; } } else { DEBUG ((EFI_D_INFO, "Card should be MMC\n")); MmcHostInstance->CardInfo.CardType = MMC_CARD; Status = MmcHost->SendCommand (MmcHost, MMC_CMD1, 0x800000); if (!EFI_ERROR (Status)) { Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_OCR, Response); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive OCR, Status=%r.\n", Status)); return Status; } ((UINT32 *) &(MmcHostInstance->CardInfo.OCRData))[0] = Response[0]; } } if (!EFI_ERROR (Status)) { if (!MmcHostInstance->CardInfo.OCRData.PowerUp) { gBS->Stall (1); Timeout--; } else { if ((MmcHostInstance->CardInfo.CardType == SD_CARD_2) && (MmcHostInstance->CardInfo.OCRData.AccessMode & BIT1)) { MmcHostInstance->CardInfo.CardType = SD_CARD_2_HIGH; DEBUG ((EFI_D_ERROR, "High capacity card.\n")); } break; // The MMC/SD card is ready. Continue the Identification Mode } } else { gBS->Stall (1); Timeout--; } } if (Timeout == 0) { DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(): No Card\n")); return EFI_NO_MEDIA; } else { PrintOCR (Response[0]); } Status = MmcNotifyState (MmcHostInstance, MmcReadyState); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcReadyState\n")); return Status; } Status = MmcHost->SendCommand (MmcHost, MMC_CMD2, 0); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD2): Error\n")); return Status; } Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_CID, Response); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive CID, Status=%r.\n", Status)); return Status; } PrintCID (Response); Status = MmcHost->NotifyState (MmcHost, MmcIdentificationState); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcIdentificationState\n")); return Status; } // // Note, SD specifications say that "if the command execution causes a state change, it // will be visible to the host in the response to the next command" // The status returned for this CMD3 will be 2 - identification // CmdArg = 1; Status = MmcHost->SendCommand (MmcHost, MMC_CMD3, CmdArg); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD3): Error\n")); return Status; } Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_RCA, Response); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive RCA, Status=%r.\n", Status)); return Status; } PrintRCA (Response[0]); // For MMC card, RCA is assigned by CMD3 while CMD3 dumps the RCA for SD card if (MmcHostInstance->CardInfo.CardType != MMC_CARD) { MmcHostInstance->CardInfo.RCA = Response[0] >> 16; } else {
STATIC EFI_STATUS InitializeSdMmcDevice ( IN MMC_HOST_INSTANCE *MmcHostInstance ) { UINT32 CmdArg; UINT32 Response[4]; UINT32 Buffer[128]; UINTN BlockSize; UINTN CardSize; UINTN NumBlocks; BOOLEAN CccSwitch; SCR Scr; EFI_STATUS Status; EFI_MMC_HOST_PROTOCOL *MmcHost; MmcHost = MmcHostInstance->MmcHost; // Send a command to get Card specific data CmdArg = MmcHostInstance->CardInfo.RCA << 16; Status = MmcHost->SendCommand (MmcHost, MMC_CMD9, CmdArg); if (EFI_ERROR (Status)) { DEBUG((EFI_D_ERROR, "InitializeSdMmcDevice(MMC_CMD9): Error, Status=%r\n", Status)); return Status; } // Read Response Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_CSD, Response); if (EFI_ERROR (Status)) { DEBUG((EFI_D_ERROR, "InitializeSdMmcDevice(): Failed to receive CSD, Status=%r\n", Status)); return Status; } PrintCSD (Response); if (MMC_CSD_GET_CCC(Response) & SD_CCC_SWITCH) { CccSwitch = TRUE; } else { CccSwitch = FALSE; } if (MmcHostInstance->CardInfo.CardType == SD_CARD_2_HIGH) { CardSize = HC_MMC_CSD_GET_DEVICESIZE (Response); NumBlocks = ((CardSize + 1) * 1024); BlockSize = 1 << MMC_CSD_GET_READBLLEN (Response); } else { CardSize = MMC_CSD_GET_DEVICESIZE (Response); NumBlocks = (CardSize + 1) * (1 << (MMC_CSD_GET_DEVICESIZEMULT (Response) + 2)); BlockSize = 1 << MMC_CSD_GET_READBLLEN (Response); } // For >=2G card, BlockSize may be 1K, but the transfer size is 512 bytes. if (BlockSize > 512) { NumBlocks = MultU64x32 (NumBlocks, BlockSize / 512); BlockSize = 512; } MmcHostInstance->BlockIo.Media->LastBlock = (NumBlocks - 1); MmcHostInstance->BlockIo.Media->BlockSize = BlockSize; MmcHostInstance->BlockIo.Media->ReadOnly = MmcHost->IsReadOnly (MmcHost); MmcHostInstance->BlockIo.Media->MediaPresent = TRUE; MmcHostInstance->BlockIo.Media->MediaId++; CmdArg = MmcHostInstance->CardInfo.RCA << 16; Status = MmcHost->SendCommand (MmcHost, MMC_CMD7, CmdArg); if (EFI_ERROR (Status)) { DEBUG((EFI_D_ERROR, "InitializeSdMmcDevice(MMC_CMD7): Error and Status = %r\n", Status)); return Status; } Status = MmcHost->SendCommand (MmcHost, MMC_CMD55, CmdArg); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "%a(MMC_CMD55): Error and Status = %r\n", Status)); return Status; } Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1, Response); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "%a(MMC_CMD55): Error and Status = %r\n", Status)); return Status; } if ((Response[0] & MMC_STATUS_APP_CMD) == 0) { return EFI_SUCCESS; } /* SCR */ Status = MmcHost->SendCommand (MmcHost, MMC_ACMD51, 0); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "%a(MMC_ACMD51): Error and Status = %r\n", __func__, Status)); return Status; } else { Status = MmcHost->ReadBlockData (MmcHost, 0, 8, Buffer); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "%a(MMC_ACMD51): ReadBlockData Error and Status = %r\n", __func__, Status)); return Status; } CopyMem (&Scr, Buffer, 8); if (Scr.SD_SPEC == 2) { if (Scr.SD_SPEC3 == 1) { if (Scr.SD_SPEC4 == 1) { DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 4.xx\n")); } else { DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 3.0x\n")); } } else { if (Scr.SD_SPEC4 == 0) { DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 2.0\n")); } else { DEBUG ((EFI_D_ERROR, "Found invalid SD Card\n")); } } } else { if ((Scr.SD_SPEC3 == 0) && (Scr.SD_SPEC4 == 0)) { if (Scr.SD_SPEC == 1) { DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 1.10\n")); } else { DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 1.0\n")); } } else { DEBUG ((EFI_D_ERROR, "Found invalid SD Card\n")); } } } if (CccSwitch) { /* SD Switch, Mode:1, Group:0, Value:1 */ CmdArg = 1 << 31 | 0x00FFFFFF; CmdArg &= ~(0xF << (0 * 4)); CmdArg |= 1 << (0 * 4); Status = MmcHost->SendCommand (MmcHost, MMC_CMD6, CmdArg); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "%a(MMC_CMD6): Error and Status = %r\n", Status)); return Status; } else { Status = MmcHost->ReadBlockData (MmcHost, 0, 64, Buffer); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "%a(MMC_CMD6): ReadBlockData Error and Status = %r\n", Status)); return Status; } } } if (Scr.SD_BUS_WIDTHS & SD_BUS_WIDTH_4BIT) { CmdArg = MmcHostInstance->CardInfo.RCA << 16; Status = MmcHost->SendCommand (MmcHost, MMC_CMD55, CmdArg); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "%a(MMC_CMD55): Error and Status = %r\n", Status)); return Status; } /* Width: 4 */ Status = MmcHost->SendCommand (MmcHost, MMC_CMD6, 2); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "%a(MMC_CMD6): Error and Status = %r\n", Status)); return Status; } } if (MMC_HOST_HAS_SETIOS(MmcHost)) { Status = MmcHost->SetIos (MmcHost, 26 * 1000 * 1000, 4, EMMCBACKWARD); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "%a(SetIos): Error and Status = %r\n", Status)); return Status; } } return EFI_SUCCESS; }
STATIC EFI_STATUS EFIAPI EmmcIdentificationMode ( IN MMC_HOST_INSTANCE *MmcHostInstance, IN OCR_RESPONSE Response ) { EFI_MMC_HOST_PROTOCOL *Host; EFI_BLOCK_IO_MEDIA *Media; EFI_STATUS Status; EMMC_DEVICE_STATE State; UINT32 RCA; Host = MmcHostInstance->MmcHost; Media = MmcHostInstance->BlockIo.Media; // Fetch card identity register Status = Host->SendCommand (Host, MMC_CMD2, 0); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): Failed to send CMD2, Status=%r.\n", Status)); return Status; } Status = Host->ReceiveResponse (Host, MMC_RESPONSE_TYPE_R2, (UINT32 *)&(MmcHostInstance->CardInfo.CIDData)); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): CID retrieval error, Status=%r.\n", Status)); return Status; } // Assign a relative address value to the card MmcHostInstance->CardInfo.RCA = ++mEmmcRcaCount; // TODO: might need a more sophisticated way of doing this RCA = MmcHostInstance->CardInfo.RCA << RCA_SHIFT_OFFSET; Status = Host->SendCommand (Host, MMC_CMD3, RCA); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): RCA set error, Status=%r.\n", Status)); return Status; } // Fetch card specific data Status = Host->SendCommand (Host, MMC_CMD9, RCA); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): Failed to send CMD9, Status=%r.\n", Status)); return Status; } Status = Host->ReceiveResponse (Host, MMC_RESPONSE_TYPE_R2, (UINT32 *)&(MmcHostInstance->CardInfo.CSDData)); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): CSD retrieval error, Status=%r.\n", Status)); return Status; } // Select the card Status = Host->SendCommand (Host, MMC_CMD7, RCA); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): Card selection error, Status=%r.\n", Status)); } if (MMC_HOST_HAS_SETIOS(Host)) { // Set 1-bit bus width Status = Host->SetIos (Host, 0, 1, EMMCBACKWARD); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): Set 1-bit bus width error, Status=%r.\n", Status)); return Status; } // Set 1-bit bus width for EXTCSD Status = EmmcSetEXTCSD (MmcHostInstance, EXTCSD_BUS_WIDTH, EMMC_BUS_WIDTH_1BIT); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): Set extcsd bus width error, Status=%r.\n", Status)); return Status; } } // Fetch ECSD MmcHostInstance->CardInfo.ECSDData = AllocatePages (EFI_SIZE_TO_PAGES (sizeof (ECSD))); if (MmcHostInstance->CardInfo.ECSDData == NULL) { return EFI_OUT_OF_RESOURCES; } Status = Host->SendCommand (Host, MMC_CMD8, 0); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): ECSD fetch error, Status=%r.\n", Status)); } Status = Host->ReadBlockData (Host, 0, 512, (UINT32 *)MmcHostInstance->CardInfo.ECSDData); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): ECSD read error, Status=%r.\n", Status)); goto FreePageExit; } // Make sure device exiting data mode do { Status = EmmcGetDeviceState (MmcHostInstance, &State); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): Failed to get device state, Status=%r.\n", Status)); goto FreePageExit; } } while (State == EMMC_DATA_STATE); // Set up media Media->BlockSize = EMMC_CARD_SIZE; // 512-byte support is mandatory for eMMC cards Media->MediaId = MmcHostInstance->CardInfo.CIDData.PSN; Media->ReadOnly = MmcHostInstance->CardInfo.CSDData.PERM_WRITE_PROTECT; Media->LogicalBlocksPerPhysicalBlock = 1; Media->IoAlign = 4; // Compute last block using bits [215:212] of the ECSD Media->LastBlock = MmcHostInstance->CardInfo.ECSDData->SECTOR_COUNT - 1; // eMMC isn't supposed to report this for // Cards <2GB in size, but the model does. // Setup card type MmcHostInstance->CardInfo.CardType = EMMC_CARD; return EFI_SUCCESS; FreePageExit: FreePages (MmcHostInstance->CardInfo.ECSDData, EFI_SIZE_TO_PAGES (sizeof (ECSD))); return Status; }
EFI_STATUS MmcIoBlocks ( IN EFI_BLOCK_IO_PROTOCOL *This, IN UINTN Transfer, IN UINT32 MediaId, IN EFI_LBA Lba, IN UINTN BufferSize, OUT VOID *Buffer ) { UINT32 Response[4]; EFI_STATUS Status; UINTN CmdArg; INTN Timeout; UINTN Cmd; MMC_HOST_INSTANCE *MmcHostInstance; EFI_MMC_HOST_PROTOCOL *MmcHost; UINTN BytesRemainingToBeTransfered; UINTN BlockCount; BlockCount = 1; MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This); ASSERT (MmcHostInstance != NULL); MmcHost = MmcHostInstance->MmcHost; ASSERT (MmcHost); if (This->Media->MediaId != MediaId) { return EFI_MEDIA_CHANGED; } if ((MmcHost == NULL) || (Buffer == NULL)) { return EFI_INVALID_PARAMETER; } // Check if a Card is Present if (!MmcHostInstance->BlockIo.Media->MediaPresent) { return EFI_NO_MEDIA; } // All blocks must be within the device if ((Lba + (BufferSize / This->Media->BlockSize)) > (This->Media->LastBlock + 1)) { return EFI_INVALID_PARAMETER; } if ((Transfer == MMC_IOBLOCKS_WRITE) && (This->Media->ReadOnly == TRUE)) { return EFI_WRITE_PROTECTED; } // Reading 0 Byte is valid if (BufferSize == 0) { return EFI_SUCCESS; } // 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; } BytesRemainingToBeTransfered = BufferSize; while (BytesRemainingToBeTransfered > 0) { // Check if the Card is in Ready status CmdArg = MmcHostInstance->CardInfo.RCA << 16; Response[0] = 0; Timeout = 20; 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 (0 == Timeout) { DEBUG ((EFI_D_ERROR, "The Card is busy\n")); return EFI_NOT_READY; } //Set command argument based on the card access mode (Byte mode or Block mode) if (MmcHostInstance->CardInfo.OCRData.AccessMode & BIT1) { CmdArg = Lba; } else { CmdArg = Lba * This->Media->BlockSize; } if (Transfer == MMC_IOBLOCKS_READ) { // Read a single block Cmd = MMC_CMD17; } else { // Write a single block Cmd = MMC_CMD24; } Status = MmcHost->SendCommand (MmcHost, Cmd, CmdArg); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "MmcIoBlocks(MMC_CMD%d): Error %r\n", Cmd, Status)); return Status; } if (Transfer == MMC_IOBLOCKS_READ) { // Read one block of Data Status = MmcHost->ReadBlockData (MmcHost, Lba, This->Media->BlockSize, Buffer); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_BLKIO, "MmcIoBlocks(): Error Read Block Data and Status = %r\n", Status)); MmcStopTransmission (MmcHost); return Status; } Status = MmcNotifyState (MmcHostInstance, MmcProgrammingState); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "MmcIoBlocks() : Error MmcProgrammingState\n")); return Status; } } else { // Write one block of Data Status = MmcHost->WriteBlockData (MmcHost, Lba, This->Media->BlockSize, Buffer); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_BLKIO, "MmcIoBlocks(): Error Write Block Data and Status = %r\n", 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 } } gBS->Stall (1); } Status = MmcNotifyState (MmcHostInstance, MmcTransferState); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "MmcIoBlocks() : Error MmcTransferState\n")); return Status; } BytesRemainingToBeTransfered -= This->Media->BlockSize; Lba += BlockCount; Buffer = (UINT8 *)Buffer + This->Media->BlockSize; } return EFI_SUCCESS; }
EFI_STATUS MmcIoBlocks ( IN EFI_BLOCK_IO_PROTOCOL *This, IN UINTN Transfer, IN UINT32 MediaId, IN EFI_LBA Lba, IN UINTN BufferSize, OUT VOID *Buffer ) { UINT32 Response[4]; EFI_STATUS Status; UINTN CmdArg; INTN Timeout; UINTN Cmd; MMC_HOST_INSTANCE *MmcHostInstance; EFI_MMC_HOST_PROTOCOL *MmcHost; UINTN BytesRemainingToBeTransfered; UINTN BlockCount; UINTN ConsumeSize; BlockCount = 1; MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This); ASSERT (MmcHostInstance != NULL); MmcHost = MmcHostInstance->MmcHost; ASSERT (MmcHost); if (This->Media->MediaId != MediaId) { return EFI_MEDIA_CHANGED; } if ((MmcHost == NULL) || (Buffer == NULL)) { return EFI_INVALID_PARAMETER; } // Check if a Card is Present if (!MmcHostInstance->BlockIo.Media->MediaPresent) { return EFI_NO_MEDIA; } if (MMC_HOST_HAS_ISMULTIBLOCK(MmcHost) && MmcHost->IsMultiBlock(MmcHost)) { BlockCount = (BufferSize + This->Media->BlockSize - 1) / This->Media->BlockSize; } // All blocks must be within the device if ((Lba + (BufferSize / This->Media->BlockSize)) > (This->Media->LastBlock + 1)) { return EFI_INVALID_PARAMETER; } if ((Transfer == MMC_IOBLOCKS_WRITE) && (This->Media->ReadOnly == TRUE)) { return EFI_WRITE_PROTECTED; } // Reading 0 Byte is valid if (BufferSize == 0) { return EFI_SUCCESS; } // 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; } BytesRemainingToBeTransfered = BufferSize; while (BytesRemainingToBeTransfered > 0) { // Check if the Card is in Ready status CmdArg = MmcHostInstance->CardInfo.RCA << 16; Response[0] = 0; Timeout = 20; 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 (0 == Timeout) { DEBUG ((EFI_D_ERROR, "The Card is busy\n")); return EFI_NOT_READY; } if (Transfer == MMC_IOBLOCKS_READ) { if (BlockCount == 1) { // Read a single block Cmd = MMC_CMD17; } else { // Read multiple blocks Cmd = MMC_CMD18; } } else { if (BlockCount == 1) { // Write a single block Cmd = MMC_CMD24; } else { // Write multiple blocks Cmd = MMC_CMD25; } } ConsumeSize = BlockCount * This->Media->BlockSize; if (BytesRemainingToBeTransfered < ConsumeSize) { ConsumeSize = BytesRemainingToBeTransfered; } Status = MmcTransferBlock (This, Cmd, Transfer, MediaId, Lba, ConsumeSize, Buffer); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "%a(): Failed to transfer block and Status:%r\n", __func__, Status)); } BytesRemainingToBeTransfered -= ConsumeSize; if (BytesRemainingToBeTransfered > 0) { Lba += BlockCount; Buffer = (UINT8 *)Buffer + ConsumeSize; } } return EFI_SUCCESS; }
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; if (MmcHostInstance->CardInfo.CardType != EMMC_CARD) { //Set command argument based on the card capacity //if 0 : SDSC card //if 1 : SDXC/SDHC if (MmcHostInstance->CardInfo.OCRData.AccessMode & SD_CARD_CAPACITY) { CmdArg = Lba; } else { CmdArg = Lba * This->Media->BlockSize; } } else { //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)); } MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1b, Response); } Status = MmcNotifyState (MmcHostInstance, MmcTransferState); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "MmcIoBlocks() : Error MmcTransferState\n")); return Status; } return Status; }