diff --git a/EmbeddedPkg/Universal/MmcDxe/Mmc.c b/EmbeddedPkg/Universal/MmcDxe/Mmc.c index 9c67867aa3..468bb1dae6 100644 --- a/EmbeddedPkg/Universal/MmcDxe/Mmc.c +++ b/EmbeddedPkg/Universal/MmcDxe/Mmc.c @@ -52,7 +52,7 @@ InitializeMmcHostPool ( VOID ) { - InitializeListHead (&mMmcHostPool); + InitializeListHead (&mMmcHostPool); } /** @@ -63,7 +63,7 @@ InsertMmcHost ( IN MMC_HOST_INSTANCE *MmcHostInstance ) { - InsertTailList (&mMmcHostPool, &(MmcHostInstance->Link)); + InsertTailList (&mMmcHostPool, &(MmcHostInstance->Link)); } /* @@ -74,101 +74,101 @@ RemoveMmcHost ( IN MMC_HOST_INSTANCE *MmcHostInstance ) { - RemoveEntryList (&(MmcHostInstance->Link)); + RemoveEntryList (&(MmcHostInstance->Link)); } -MMC_HOST_INSTANCE* CreateMmcHostInstance( +MMC_HOST_INSTANCE* CreateMmcHostInstance ( IN EFI_MMC_HOST_PROTOCOL* MmcHost ) { - EFI_STATUS Status; - MMC_HOST_INSTANCE* MmcHostInstance; - EFI_DEVICE_PATH_PROTOCOL *NewDevicePathNode; - EFI_DEVICE_PATH_PROTOCOL *DevicePath; + EFI_STATUS Status; + MMC_HOST_INSTANCE* MmcHostInstance; + EFI_DEVICE_PATH_PROTOCOL *NewDevicePathNode; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; - MmcHostInstance = AllocateZeroPool (sizeof (MMC_HOST_INSTANCE)); - if (MmcHostInstance == NULL) { - return NULL; - } + MmcHostInstance = AllocateZeroPool (sizeof (MMC_HOST_INSTANCE)); + if (MmcHostInstance == NULL) { + return NULL; + } - MmcHostInstance->Signature = MMC_HOST_INSTANCE_SIGNATURE; + MmcHostInstance->Signature = MMC_HOST_INSTANCE_SIGNATURE; - MmcHostInstance->State = MmcHwInitializationState; - - MmcHostInstance->BlockIo.Media = AllocateCopyPool (sizeof(EFI_BLOCK_IO_MEDIA), &mMmcMediaTemplate); - if (MmcHostInstance->BlockIo.Media == NULL) { - goto FREE_INSTANCE; - } + MmcHostInstance->State = MmcHwInitializationState; - MmcHostInstance->BlockIo.Revision = EFI_BLOCK_IO_INTERFACE_REVISION; - MmcHostInstance->BlockIo.Reset = MmcReset; - MmcHostInstance->BlockIo.ReadBlocks = MmcReadBlocks; - MmcHostInstance->BlockIo.WriteBlocks = MmcWriteBlocks; - MmcHostInstance->BlockIo.FlushBlocks = MmcFlushBlocks; + MmcHostInstance->BlockIo.Media = AllocateCopyPool (sizeof(EFI_BLOCK_IO_MEDIA), &mMmcMediaTemplate); + if (MmcHostInstance->BlockIo.Media == NULL) { + goto FREE_INSTANCE; + } - MmcHostInstance->MmcHost = MmcHost; + MmcHostInstance->BlockIo.Revision = EFI_BLOCK_IO_INTERFACE_REVISION; + MmcHostInstance->BlockIo.Reset = MmcReset; + MmcHostInstance->BlockIo.ReadBlocks = MmcReadBlocks; + MmcHostInstance->BlockIo.WriteBlocks = MmcWriteBlocks; + MmcHostInstance->BlockIo.FlushBlocks = MmcFlushBlocks; - // Create DevicePath for the new MMC Host - Status = MmcHost->BuildDevicePath(&NewDevicePathNode); - if (EFI_ERROR (Status)) { - goto FREE_MEDIA; - } + MmcHostInstance->MmcHost = MmcHost; - DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) AllocatePool (END_DEVICE_PATH_LENGTH); - if (DevicePath == NULL) { - goto FREE_MEDIA; - } - - SetDevicePathEndNode (DevicePath); - MmcHostInstance->DevicePath = AppendDevicePathNode (DevicePath, NewDevicePathNode); + // Create DevicePath for the new MMC Host + Status = MmcHost->BuildDevicePath(&NewDevicePathNode); + if (EFI_ERROR (Status)) { + goto FREE_MEDIA; + } - // Publish BlockIO protocol interface - Status = gBS->InstallMultipleProtocolInterfaces ( - &MmcHostInstance->MmcHandle, - &gEfiBlockIoProtocolGuid,&(MmcHostInstance->BlockIo), - &gEfiDevicePathProtocolGuid,MmcHostInstance->DevicePath, - NULL - ); - if (EFI_ERROR(Status)) { - goto FREE_DEVICE_PATH; - } + DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) AllocatePool (END_DEVICE_PATH_LENGTH); + if (DevicePath == NULL) { + goto FREE_MEDIA; + } - return MmcHostInstance; + SetDevicePathEndNode (DevicePath); + MmcHostInstance->DevicePath = AppendDevicePathNode (DevicePath, NewDevicePathNode); + + // Publish BlockIO protocol interface + Status = gBS->InstallMultipleProtocolInterfaces ( + &MmcHostInstance->MmcHandle, + &gEfiBlockIoProtocolGuid,&(MmcHostInstance->BlockIo), + &gEfiDevicePathProtocolGuid,MmcHostInstance->DevicePath, + NULL + ); + if (EFI_ERROR(Status)) { + goto FREE_DEVICE_PATH; + } + + return MmcHostInstance; FREE_DEVICE_PATH: - FreePool(DevicePath); + FreePool(DevicePath); FREE_MEDIA: - FreePool(MmcHostInstance->BlockIo.Media); + FreePool(MmcHostInstance->BlockIo.Media); FREE_INSTANCE: - FreePool(MmcHostInstance); + FreePool(MmcHostInstance); - return NULL; + return NULL; } -EFI_STATUS DestroyMmcHostInstance( +EFI_STATUS DestroyMmcHostInstance ( IN MMC_HOST_INSTANCE* MmcHostInstance ) { - EFI_STATUS Status; + EFI_STATUS Status; - // Uninstall Protocol Interfaces - Status = gBS->UninstallMultipleProtocolInterfaces( + // Uninstall Protocol Interfaces + Status = gBS->UninstallMultipleProtocolInterfaces ( MmcHostInstance->MmcHandle, - &gEfiBlockIoProtocolGuid,&(MmcHostInstance->BlockIo), + &gEfiBlockIoProtocolGuid,&(MmcHostInstance->BlockIo), &gEfiDevicePathProtocolGuid,MmcHostInstance->DevicePath, - NULL - ); - ASSERT_EFI_ERROR (Status); - - // Free Memory allocated for the instance - if (MmcHostInstance->BlockIo.Media) { - FreePool(MmcHostInstance->BlockIo.Media); - } - FreePool (MmcHostInstance); + NULL + ); + ASSERT_EFI_ERROR (Status); - return Status; + // Free Memory allocated for the instance + if (MmcHostInstance->BlockIo.Media) { + FreePool(MmcHostInstance->BlockIo.Media); + } + FreePool (MmcHostInstance); + + return Status; } /** @@ -182,62 +182,62 @@ MmcDriverBindingSupported ( IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath ) { - EFI_STATUS Status; - //EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath; - EFI_MMC_HOST_PROTOCOL *MmcHost; - EFI_DEV_PATH_PTR Node; + EFI_STATUS Status; + //EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath; + EFI_MMC_HOST_PROTOCOL *MmcHost; + EFI_DEV_PATH_PTR Node; + // + // Check RemainingDevicePath validation + // + if (RemainingDevicePath != NULL) { // - // Check RemainingDevicePath validation + // Check if RemainingDevicePath is the End of Device Path Node, + // if yes, go on checking other conditions // - if (RemainingDevicePath != NULL) { - // - // Check if RemainingDevicePath is the End of Device Path Node, - // if yes, go on checking other conditions - // - if (!IsDevicePathEnd (RemainingDevicePath)) { - // - // If RemainingDevicePath isn't the End of Device Path Node, - // check its validation - // - Node.DevPath = RemainingDevicePath; - if (Node.DevPath->Type != HARDWARE_DEVICE_PATH || - Node.DevPath->SubType != HW_VENDOR_DP || - DevicePathNodeLength(Node.DevPath) != sizeof(VENDOR_DEVICE_PATH)) { - return EFI_UNSUPPORTED; - } - } + if (!IsDevicePathEnd (RemainingDevicePath)) { + // + // If RemainingDevicePath isn't the End of Device Path Node, + // check its validation + // + Node.DevPath = RemainingDevicePath; + if (Node.DevPath->Type != HARDWARE_DEVICE_PATH || + Node.DevPath->SubType != HW_VENDOR_DP || + DevicePathNodeLength(Node.DevPath) != sizeof(VENDOR_DEVICE_PATH)) { + return EFI_UNSUPPORTED; + } } + } - // - // Check if Mmc Host protocol is installed by platform - // - Status = gBS->OpenProtocol ( - Controller, - &gEfiMmcHostProtocolGuid, - (VOID **) &MmcHost, - This->DriverBindingHandle, - Controller, - EFI_OPEN_PROTOCOL_BY_DRIVER - ); - if (Status == EFI_ALREADY_STARTED) { - return EFI_SUCCESS; - } - if (EFI_ERROR (Status)) { - return Status; - } - - // - // Close the Mmc Host used to perform the supported test - // - gBS->CloseProtocol ( - Controller, - &gEfiMmcHostProtocolGuid, - This->DriverBindingHandle, - Controller - ); - + // + // Check if Mmc Host protocol is installed by platform + // + Status = gBS->OpenProtocol ( + Controller, + &gEfiMmcHostProtocolGuid, + (VOID **) &MmcHost, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (Status == EFI_ALREADY_STARTED) { return EFI_SUCCESS; + } + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Close the Mmc Host used to perform the supported test + // + gBS->CloseProtocol ( + Controller, + &gEfiMmcHostProtocolGuid, + This->DriverBindingHandle, + Controller + ); + + return EFI_SUCCESS; } /** @@ -251,48 +251,48 @@ MmcDriverBindingStart ( IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath ) { - EFI_STATUS Status; - MMC_HOST_INSTANCE *MmcHostInstance; - EFI_MMC_HOST_PROTOCOL *MmcHost; + EFI_STATUS Status; + MMC_HOST_INSTANCE *MmcHostInstance; + EFI_MMC_HOST_PROTOCOL *MmcHost; + // + // Check RemainingDevicePath validation + // + if (RemainingDevicePath != NULL) { // - // Check RemainingDevicePath validation + // Check if RemainingDevicePath is the End of Device Path Node, + // if yes, return EFI_SUCCESS // - if (RemainingDevicePath != NULL) { - // - // Check if RemainingDevicePath is the End of Device Path Node, - // if yes, return EFI_SUCCESS - // - if (IsDevicePathEnd (RemainingDevicePath)) { - return EFI_SUCCESS; - } + if (IsDevicePathEnd (RemainingDevicePath)) { + return EFI_SUCCESS; } + } - // - // Get the Mmc Host protocol - // - Status = gBS->OpenProtocol ( - Controller, - &gEfiMmcHostProtocolGuid, - (VOID **) &MmcHost, - This->DriverBindingHandle, - Controller, - EFI_OPEN_PROTOCOL_BY_DRIVER - ); - if (EFI_ERROR (Status)) { - if (Status == EFI_ALREADY_STARTED) { - return EFI_SUCCESS; - } - return Status; + // + // Get the Mmc Host protocol + // + Status = gBS->OpenProtocol ( + Controller, + &gEfiMmcHostProtocolGuid, + (VOID **) &MmcHost, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + if (Status == EFI_ALREADY_STARTED) { + return EFI_SUCCESS; } + return Status; + } - MmcHostInstance = CreateMmcHostInstance(MmcHost); - if (MmcHostInstance != NULL) { - // Add the handle to the pool - InsertMmcHost (MmcHostInstance); - } + MmcHostInstance = CreateMmcHostInstance(MmcHost); + if (MmcHostInstance != NULL) { + // Add the handle to the pool + InsertMmcHost (MmcHostInstance); + } - return EFI_SUCCESS; + return EFI_SUCCESS; } /** @@ -307,33 +307,33 @@ MmcDriverBindingStop ( IN EFI_HANDLE *ChildHandleBuffer ) { - EFI_STATUS Status = EFI_SUCCESS; - LIST_ENTRY *CurrentLink; - MMC_HOST_INSTANCE *MmcHostInstance; + EFI_STATUS Status = EFI_SUCCESS; + LIST_ENTRY *CurrentLink; + MMC_HOST_INSTANCE *MmcHostInstance; - MMC_TRACE("MmcDriverBindingStop()"); + MMC_TRACE("MmcDriverBindingStop()"); - // For each MMC instance - CurrentLink = mMmcHostPool.ForwardLink; - while (CurrentLink != NULL && CurrentLink != &mMmcHostPool && (Status == EFI_SUCCESS)) { - MmcHostInstance = MMC_HOST_INSTANCE_FROM_LINK(CurrentLink); - ASSERT(MmcHostInstance != NULL); + // For each MMC instance + CurrentLink = mMmcHostPool.ForwardLink; + while (CurrentLink != NULL && CurrentLink != &mMmcHostPool && (Status == EFI_SUCCESS)) { + MmcHostInstance = MMC_HOST_INSTANCE_FROM_LINK(CurrentLink); + ASSERT(MmcHostInstance != NULL); - // Close gEfiMmcHostProtocolGuid - Status = gBS->CloseProtocol ( - Controller, - &gEfiMmcHostProtocolGuid,(VOID **) &MmcHostInstance->MmcHost, - This->DriverBindingHandle - ); + // Close gEfiMmcHostProtocolGuid + Status = gBS->CloseProtocol ( + Controller, + &gEfiMmcHostProtocolGuid,(VOID **) &MmcHostInstance->MmcHost, + This->DriverBindingHandle + ); - // Remove MMC Host Instance from the pool - RemoveMmcHost (MmcHostInstance); + // Remove MMC Host Instance from the pool + RemoveMmcHost (MmcHostInstance); - // Destroy MmcHostInstance - DestroyMmcHostInstance (MmcHostInstance); - } + // Destroy MmcHostInstance + DestroyMmcHostInstance (MmcHostInstance); + } - return Status; + return Status; } EFI_DRIVER_BINDING_PROTOCOL gMmcDriverBinding = { @@ -355,33 +355,33 @@ MmcDxeInitialize ( IN EFI_SYSTEM_TABLE *SystemTable ) { - EFI_STATUS Status; + EFI_STATUS Status; - // - // Initializes MMC Host pool - // - InitializeMmcHostPool (); + // + // Initializes MMC Host pool + // + InitializeMmcHostPool (); - // - // Install driver model protocol(s). - // - Status = EfiLibInstallDriverBindingComponentName2 ( - ImageHandle, - SystemTable, - &gMmcDriverBinding, - ImageHandle, - &gMmcComponentName, - &gMmcComponentName2 - ); - ASSERT_EFI_ERROR (Status); + // + // Install driver model protocol(s). + // + Status = EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gMmcDriverBinding, + ImageHandle, + &gMmcComponentName, + &gMmcComponentName2 + ); + ASSERT_EFI_ERROR (Status); - // Install driver diagnostics - Status = gBS->InstallMultipleProtocolInterfaces ( - &ImageHandle, - &gEfiDriverDiagnostics2ProtocolGuid,&gMmcDriverDiagnostics2, - NULL - ); - ASSERT_EFI_ERROR (Status); + // Install driver diagnostics + Status = gBS->InstallMultipleProtocolInterfaces ( + &ImageHandle, + &gEfiDriverDiagnostics2ProtocolGuid,&gMmcDriverDiagnostics2, + NULL + ); + ASSERT_EFI_ERROR (Status); - return Status; + return Status; } diff --git a/EmbeddedPkg/Universal/MmcDxe/MmcBlockIo.c b/EmbeddedPkg/Universal/MmcDxe/MmcBlockIo.c index a77c8f088b..138e87e7a8 100644 --- a/EmbeddedPkg/Universal/MmcDxe/MmcBlockIo.c +++ b/EmbeddedPkg/Universal/MmcDxe/MmcBlockIo.c @@ -27,311 +27,337 @@ EFI_STATUS MmcNotifyState ( - MMC_HOST_INSTANCE *MmcHostInstance, - MMC_STATE State - ) { - MmcHostInstance->State = State; - return MmcHostInstance->MmcHost->NotifyState(State); + IN MMC_HOST_INSTANCE *MmcHostInstance, + IN MMC_STATE State + ) +{ + MmcHostInstance->State = State; + return MmcHostInstance->MmcHost->NotifyState(State); } -VOID PrintOCR(UINT32 ocr) { - UINTN minv, maxv, volts; - UINTN loop; +VOID +PrintOCR ( + IN UINT32 Ocr + ) +{ + UINTN minv, maxv, volts; + UINTN loop; - minv = 36; // 3.6 - maxv = 20; // 2.0 - volts = 20; // 2.0 + minv = 36; // 3.6 + maxv = 20; // 2.0 + volts = 20; // 2.0 - // The MMC register bits [23:8] indicate the working range of the card - for (loop = 8; loop < 24; loop++) { - if (ocr & (1 << loop)) { - if (minv > volts) minv = volts; - if (maxv < volts) maxv = volts + 1; - } - volts = volts + 1; + // The MMC register bits [23:8] indicate the working range of the card + for (loop = 8; loop < 24; loop++) { + if (Ocr & (1 << loop)) { + if (minv > volts) minv = volts; + if (maxv < volts) maxv = volts + 1; } + volts = volts + 1; + } - DEBUG((EFI_D_ERROR, "- PrintOCR ocr (0x%X)\n",ocr)); - DEBUG((EFI_D_ERROR, "\t- Card operating voltage: %d.%d to %d.%d\n", minv/10, minv % 10, maxv/10, maxv % 10)); - if (((ocr >> 29) & 3) == 0) - DEBUG((EFI_D_ERROR, "\t- AccessMode: Byte Mode\n")); - else - DEBUG((EFI_D_ERROR, "\t- AccessMode: Block Mode (0x%X)\n",((ocr >> 29) & 3))); + DEBUG((EFI_D_ERROR, "- PrintOCR Ocr (0x%X)\n",Ocr)); + DEBUG((EFI_D_ERROR, "\t- Card operating voltage: %d.%d to %d.%d\n", minv/10, minv % 10, maxv/10, maxv % 10)); + if (((Ocr >> 29) & 3) == 0) { + DEBUG((EFI_D_ERROR, "\t- AccessMode: Byte Mode\n")); + } else { + DEBUG((EFI_D_ERROR, "\t- AccessMode: Block Mode (0x%X)\n",((Ocr >> 29) & 3))); + } - if (ocr & MMC_OCR_POWERUP) - DEBUG((EFI_D_ERROR, "\t- PowerUp\n")); - else - DEBUG((EFI_D_ERROR, "\t- Voltage Not Supported\n")); + if (Ocr & MMC_OCR_POWERUP) { + DEBUG((EFI_D_ERROR, "\t- PowerUp\n")); + } else { + DEBUG((EFI_D_ERROR, "\t- Voltage Not Supported\n")); + } } -VOID PrintCID(UINT32* cid) { - DEBUG((EFI_D_ERROR, "- PrintCID\n")); - DEBUG((EFI_D_ERROR, "\t- Manufacturing date: %d/%d\n",(cid[0] >> 8) & 0xF,(cid[0] >> 12) & 0xFF)); - DEBUG((EFI_D_ERROR, "\t- Product serial number: 0x%X%X\n",cid[1] & 0xFFFFFF,(cid[0] >> 24) & 0xFF)); - DEBUG((EFI_D_ERROR, "\t- Product revision: %d\n",cid[1] >> 24)); - //DEBUG((EFI_D_ERROR, "\t- Product name: %s\n",(char*)(cid + 2))); - DEBUG((EFI_D_ERROR, "\t- OEM ID: %c%c\n",(cid[3] >> 8) & 0xFF,(cid[3] >> 16) & 0xFF)); +VOID PrintCID ( + IN UINT32* Cid + ) +{ + DEBUG((EFI_D_ERROR, "- PrintCID\n")); + DEBUG((EFI_D_ERROR, "\t- Manufacturing date: %d/%d\n",(Cid[0] >> 8) & 0xF,(Cid[0] >> 12) & 0xFF)); + DEBUG((EFI_D_ERROR, "\t- Product serial number: 0x%X%X\n",Cid[1] & 0xFFFFFF,(Cid[0] >> 24) & 0xFF)); + DEBUG((EFI_D_ERROR, "\t- Product revision: %d\n",Cid[1] >> 24)); + //DEBUG((EFI_D_ERROR, "\t- Product name: %s\n",(char*)(Cid + 2))); + DEBUG((EFI_D_ERROR, "\t- OEM ID: %c%c\n",(Cid[3] >> 8) & 0xFF,(Cid[3] >> 16) & 0xFF)); } -VOID PrintCSD(UINT32* csd) { - UINTN val32; - CONST CHAR8* str_unit[] = { "100kbit/s","1Mbit/s","10Mbit/s","100MBit/s","Unkbown","Unkbown","Unkbown","Unkbown" }; - CONST CHAR8* str_value[] = { "1.0","1.2","1.3","1.5","2.0","2.5","3.0","3.5","4.0","4.5","5.0","Unknown","Unknown","Unknown","Unknown" }; +VOID +PrintCSD ( + IN UINT32* Csd + ) +{ + UINTN Value; + CONST CHAR8* str_unit[] = { "100kbit/s","1Mbit/s","10Mbit/s","100MBit/s","Unkbown","Unkbown","Unkbown","Unkbown" }; + CONST CHAR8* str_value[] = { "1.0","1.2","1.3","1.5","2.0","2.5","3.0","3.5","4.0","4.5","5.0","Unknown","Unknown","Unknown","Unknown" }; - if (((csd[2] >> 30) & 0x3) == 0) - DEBUG((EFI_D_ERROR, "- PrintCSD Version 1.01-1.10/Version 2.00/Standard Capacity\n")); - else if (((csd[2] >> 30) & 0x3) == 1) - DEBUG((EFI_D_ERROR, "- PrintCSD Version 2.00/High Capacity\n")); - else - DEBUG((EFI_D_ERROR, "- PrintCSD Version Higher than v3.3\n")); + if (((Csd[2] >> 30) & 0x3) == 0) { + DEBUG((EFI_D_ERROR, "- PrintCSD Version 1.01-1.10/Version 2.00/Standard Capacity\n")); + } else if (((Csd[2] >> 30) & 0x3) == 1) { + DEBUG((EFI_D_ERROR, "- PrintCSD Version 2.00/High Capacity\n")); + } else { + DEBUG((EFI_D_ERROR, "- PrintCSD Version Higher than v3.3\n")); + } - DEBUG((EFI_D_ERROR, "\t- Supported card command class: 0x%X\n",MMC_CSD_GET_CCC(csd))); - DEBUG((EFI_D_ERROR, "\t- Speed: %a %a\n",str_value[(MMC_CSD_GET_TRANSPEED(csd) >> 3) & 0xF],str_unit[MMC_CSD_GET_TRANSPEED(csd) & 7])); - DEBUG((EFI_D_ERROR, "\t- Maximum Read Data Block: %d\n",2 << (MMC_CSD_GET_READBLLEN(csd)-1))); - DEBUG((EFI_D_ERROR, "\t- Maximum Write Data Block: %d\n",2 << (MMC_CSD_GET_WRITEBLLEN(csd)-1))); - - if (!MMC_CSD_GET_FILEFORMATGRP(csd)) { - val32 = MMC_CSD_GET_FILEFORMAT(csd); - if (val32 == 0) DEBUG((EFI_D_ERROR, "\t- Format(0): Hard disk-like file system with partition table\n")); - else if (val32 == 1) DEBUG((EFI_D_ERROR, "\t- Format(1): DOS FAT (floppy-like) with boot sector only (no partition table)\n")); - else if (val32 == 2) DEBUG((EFI_D_ERROR, "\t- Format(2): Universal File Format\n")); - else DEBUG((EFI_D_ERROR, "\t- Format(3): Others/Unknown\n")); - } else { - DEBUG((EFI_D_ERROR, "\t- Format: Reserved\n")); - } + DEBUG((EFI_D_ERROR, "\t- Supported card command class: 0x%X\n",MMC_CSD_GET_CCC(Csd))); + DEBUG((EFI_D_ERROR, "\t- Speed: %a %a\n",str_value[(MMC_CSD_GET_TRANSPEED(Csd) >> 3) & 0xF],str_unit[MMC_CSD_GET_TRANSPEED(Csd) & 7])); + DEBUG((EFI_D_ERROR, "\t- Maximum Read Data Block: %d\n",2 << (MMC_CSD_GET_READBLLEN(Csd)-1))); + DEBUG((EFI_D_ERROR, "\t- Maximum Write Data Block: %d\n",2 << (MMC_CSD_GET_WRITEBLLEN(Csd)-1))); + + if (!MMC_CSD_GET_FILEFORMATGRP(Csd)) { + Value = MMC_CSD_GET_FILEFORMAT(Csd); + if (Value == 0) DEBUG((EFI_D_ERROR, "\t- Format(0): Hard disk-like file system with partition table\n")); + else if (Value == 1) DEBUG((EFI_D_ERROR, "\t- Format(1): DOS FAT (floppy-like) with boot sector only (no partition table)\n")); + else if (Value == 2) DEBUG((EFI_D_ERROR, "\t- Format(2): Universal File Format\n")); + else DEBUG((EFI_D_ERROR, "\t- Format(3): Others/Unknown\n")); + } else { + DEBUG((EFI_D_ERROR, "\t- Format: Reserved\n")); + } } -VOID PrintRCA(UINT32 rca) { - DEBUG((EFI_D_ERROR, "- PrintRCA: 0x%X\n",rca)); - DEBUG((EFI_D_ERROR, "\t- Status: 0x%X\n",rca & 0xFFFF)); - DEBUG((EFI_D_ERROR, "\t- RCA: 0x%X\n",(rca >> 16) & 0xFFFF)); +VOID +PrintRCA ( + IN UINT32 Rca + ) +{ + DEBUG((EFI_D_ERROR, "- PrintRCA: 0x%X\n",Rca)); + DEBUG((EFI_D_ERROR, "\t- Status: 0x%X\n",Rca & 0xFFFF)); + DEBUG((EFI_D_ERROR, "\t- RCA: 0x%X\n",(Rca >> 16) & 0xFFFF)); } -VOID PrintResponseR1(UINT32 response) { - DEBUG((EFI_D_INFO, "Response: 0x%X\n",response)); - if (response & (1 << 8)) DEBUG((EFI_D_INFO, "\t- READY_FOR_DATA\n")); +VOID +PrintResponseR1 ( + IN UINT32 Response + ) +{ + DEBUG((EFI_D_INFO, "Response: 0x%X\n",Response)); + if (Response & (1 << 8)) DEBUG((EFI_D_INFO, "\t- READY_FOR_DATA\n")); - if (((response >> 9) & 0xF) == 0) DEBUG((EFI_D_INFO, "\t- State: Idle\n")); - else if (((response >> 9) & 0xF) == 1) DEBUG((EFI_D_INFO, "\t- State: Ready\n")); - else if (((response >> 9) & 0xF) == 2) DEBUG((EFI_D_INFO, "\t- State: Ident\n")); - else if (((response >> 9) & 0xF) == 3) DEBUG((EFI_D_INFO, "\t- State: StandBy\n")); - else if (((response >> 9) & 0xF) == 4) DEBUG((EFI_D_INFO, "\t- State: Tran\n")); - else if (((response >> 9) & 0xF) == 5) DEBUG((EFI_D_INFO, "\t- State: Data\n")); - else if (((response >> 9) & 0xF) == 6) DEBUG((EFI_D_INFO, "\t- State: Rcv\n")); - else if (((response >> 9) & 0xF) == 7) DEBUG((EFI_D_INFO, "\t- State: Prg\n")); - else if (((response >> 9) & 0xF) == 8) DEBUG((EFI_D_INFO, "\t- State: Dis\n")); - else DEBUG((EFI_D_INFO, "\t- State: Reserved\n")); + if (((Response >> 9) & 0xF) == 0) DEBUG((EFI_D_INFO, "\t- State: Idle\n")); + else if (((Response >> 9) & 0xF) == 1) DEBUG((EFI_D_INFO, "\t- State: Ready\n")); + else if (((Response >> 9) & 0xF) == 2) DEBUG((EFI_D_INFO, "\t- State: Ident\n")); + else if (((Response >> 9) & 0xF) == 3) DEBUG((EFI_D_INFO, "\t- State: StandBy\n")); + else if (((Response >> 9) & 0xF) == 4) DEBUG((EFI_D_INFO, "\t- State: Tran\n")); + else if (((Response >> 9) & 0xF) == 5) DEBUG((EFI_D_INFO, "\t- State: Data\n")); + else if (((Response >> 9) & 0xF) == 6) DEBUG((EFI_D_INFO, "\t- State: Rcv\n")); + else if (((Response >> 9) & 0xF) == 7) DEBUG((EFI_D_INFO, "\t- State: Prg\n")); + else if (((Response >> 9) & 0xF) == 8) DEBUG((EFI_D_INFO, "\t- State: Dis\n")); + else DEBUG((EFI_D_INFO, "\t- State: Reserved\n")); } EFI_STATUS EFIAPI MmcGetCardStatus( - MMC_HOST_INSTANCE *MmcHostInstance - ){ - EFI_STATUS Status=EFI_SUCCESS; - UINT32 Response[4]; - UINTN CmdArg; - EFI_MMC_HOST_PROTOCOL *MmcHost; + IN MMC_HOST_INSTANCE *MmcHostInstance + ) +{ + EFI_STATUS Status; + UINT32 Response[4]; + UINTN CmdArg; + EFI_MMC_HOST_PROTOCOL *MmcHost; - MmcHost = MmcHostInstance->MmcHost; - CmdArg = 0; + 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(MMC_CMD13, CmdArg); - if (EFI_ERROR(Status)) { - DEBUG((EFI_D_ERROR, "MmcGetCardStatus(MMC_CMD13): Error and Status = %r\n", Status)); - ASSERT(0); - return Status; - } - - //Read Response - MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_R1,Response); - PrintResponseR1(Response[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(MMC_CMD13, CmdArg); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_ERROR, "MmcGetCardStatus(MMC_CMD13): Error and Status = %r\n", Status)); + ASSERT(0); + return Status; } - return Status; + //Read Response + MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_R1,Response); + PrintResponseR1(Response[0]); + } + + return Status; } EFI_STATUS EFIAPI MmcIdentificationMode ( - MMC_HOST_INSTANCE *MmcHostInstance - ) { - EFI_STATUS Status; - UINT32 Response[4]; - UINTN Timeout; - UINTN CmdArg; - BOOLEAN bHCS; - EFI_MMC_HOST_PROTOCOL *MmcHost; - UINTN CmdRetryCnt; - - MmcHost = MmcHostInstance->MmcHost; - CmdArg = 0; - bHCS = FALSE; + IN MMC_HOST_INSTANCE *MmcHostInstance + ) +{ + EFI_STATUS Status; + UINT32 Response[4]; + UINTN Timeout; + UINTN CmdArg; + BOOLEAN IsHCS; + EFI_MMC_HOST_PROTOCOL *MmcHost; + UINTN CmdRetryCnt; - if (MmcHost == NULL) { - return EFI_INVALID_PARAMETER; - } + MmcHost = MmcHostInstance->MmcHost; + CmdArg = 0; + IsHCS = FALSE; - // 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\n")); - return Status; - } - } else { - //Note: Could even be used in all cases. But it looks this command could put the state machine into inactive for some cards - Status = MmcHost->SendCommand(MMC_CMD0, 0); - if (EFI_ERROR(Status)) { - DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD0): Error\n")); - return Status; - } - } + if (MmcHost == NULL) { + return EFI_INVALID_PARAMETER; + } - Status = MmcNotifyState (MmcHostInstance, MmcIdleState); + // 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 MmcIdleState\n")); - return Status; + DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcHwInitializationState\n")); + return Status; } - - // Are we using SDIO ? - Status = MmcHost->SendCommand(MMC_CMD5, 0); - if (Status == EFI_SUCCESS) { - DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD5): Error - SDIO not supported.\n")); - return EFI_UNSUPPORTED; + } else { + //Note: Could even be used in all cases. But it looks this command could put the state machine into inactive for some cards + Status = MmcHost->SendCommand(MMC_CMD0, 0); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD0): Error\n")); + return Status; } + } - // 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(MMC_CMD8, CmdArg); + Status = MmcNotifyState (MmcHostInstance, MmcIdleState); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcIdleState\n")); + return Status; + } + + // Are we using SDIO ? + Status = MmcHost->SendCommand(MMC_CMD5, 0); + if (Status == EFI_SUCCESS) { + DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD5): Error - SDIO not supported.\n")); + 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(MMC_CMD8, CmdArg); + if (Status == EFI_SUCCESS) { + DEBUG ((EFI_D_ERROR, "Card is SD2.0 => Supports high capacity\n")); + IsHCS = TRUE; + MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_R7,Response); + 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.Busy == 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(MMC_CMD55, 0); if (Status == EFI_SUCCESS) { - DEBUG ((EFI_D_ERROR, "Card is SD2.0 => Supports high capacity\n")); - bHCS = TRUE; - MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_R7,Response); - 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; + 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(MMC_ACMD41, CmdArg); + if (!EFI_ERROR(Status)) { + MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_OCR,Response); + ((UINT32 *) &(MmcHostInstance->CardInfo.OCRData))[0] = Response[0]; } } else { - DEBUG ((EFI_D_ERROR, "Not a SD2.0 Card\n")); + DEBUG ((EFI_D_INFO, "Card should be MMC\n")); + MmcHostInstance->CardInfo.CardType = MMC_CARD; + + Status = MmcHost->SendCommand(MMC_CMD1, 0x800000); + if (!EFI_ERROR(Status)) { + MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_OCR,Response); + ((UINT32 *) &(MmcHostInstance->CardInfo.OCRData))[0] = Response[0]; + } } - // We need to wait for the MMC or SD card is ready => (gCardInfo.OCRData.Busy == 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(MMC_CMD55, 0); - if (Status == EFI_SUCCESS) { - DEBUG ((EFI_D_INFO, "Card should be SD\n")); - if (bHCS) { - 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 (bHCS) { - CmdArg |= BIT30; - } - Status = MmcHost->SendCommand(MMC_ACMD41, CmdArg); - if (!EFI_ERROR(Status)) { - MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_OCR,Response); - ((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(MMC_CMD1, 0x800000); - if (!EFI_ERROR(Status)) { - MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_OCR,Response); - ((UINT32 *) &(MmcHostInstance->CardInfo.OCRData))[0] = Response[0]; - } - } - - if (!EFI_ERROR(Status)) { - if (MmcHostInstance->CardInfo.OCRData.Busy == 0) { - MicroSecondDelay(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 { + if (!EFI_ERROR(Status)) { + if (MmcHostInstance->CardInfo.OCRData.Busy == 0) { MicroSecondDelay(1); Timeout--; - } - } - - if (Timeout == 0) { - DEBUG((EFI_D_ERROR, "MmcIdentificationMode(): No Card\n")); - ASSERT(0); - return EFI_NO_MEDIA; + } 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 { - PrintOCR(Response[0]); + MicroSecondDelay(1); + Timeout--; } + } - Status = MmcNotifyState (MmcHostInstance, MmcReadyState); + if (Timeout == 0) { + DEBUG((EFI_D_ERROR, "MmcIdentificationMode(): No Card\n")); + ASSERT(0); + 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(MMC_CMD2, 0); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD2): Error\n")); + ASSERT(0); + return Status; + } + MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_CID,Response); + PrintCID(Response); + + Status = MmcNotifyState (MmcHostInstance, MmcIdentificationState); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcIdentificationState\n")); + return Status; + } + + CmdArg = 0; + CmdRetryCnt = CMD_RETRY_COUNT; + //Keep sending CMD 3 until card enters to Standby mode and Card status is ready + while((MMC_R0_CURRENTSTATE(Response) != MMC_R0_STATE_STDBY) && CmdRetryCnt--) { + Status = MmcHost->SendCommand(MMC_CMD3, CmdArg); if (EFI_ERROR(Status)) { - DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcReadyState\n")); + DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD3): Error\n")); return Status; } + MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_RCA,Response); + PrintRCA(Response[0]); + } - Status = MmcHost->SendCommand(MMC_CMD2, 0); - if (EFI_ERROR(Status)) { - DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD2): Error\n")); - ASSERT(0); - return Status; - } - MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_CID,Response); - PrintCID(Response); + // 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 { + MmcHostInstance->CardInfo.RCA = CmdArg; + } - Status = MmcNotifyState (MmcHostInstance, MmcIdentificationState); - if (EFI_ERROR(Status)) { - DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcIdentificationState\n")); - return Status; - } + Status = MmcNotifyState (MmcHostInstance, MmcStandByState); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcStandByState\n")); + return Status; + } - CmdArg = 0; - CmdRetryCnt = CMD_RETRY_COUNT; - //Keep sending CMD 3 until card enters to Standby mode and Card status is ready - while((MMC_R0_CURRENTSTATE(Response) != MMC_R0_STATE_STDBY) && CmdRetryCnt-- ){ - Status = MmcHost->SendCommand(MMC_CMD3, CmdArg); - if (EFI_ERROR(Status)) { - DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD3): Error\n")); - return Status; - } - MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_RCA,Response); - 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 { - MmcHostInstance->CardInfo.RCA = CmdArg; - } - - Status = MmcNotifyState (MmcHostInstance, MmcStandByState); - if (EFI_ERROR(Status)) { - DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcStandByState\n")); - return Status; - } - - return EFI_SUCCESS; + return EFI_SUCCESS; } EFI_STATUS @@ -339,10 +365,11 @@ EFIAPI MmcReset ( IN EFI_BLOCK_IO_PROTOCOL *This, IN BOOLEAN ExtendedVerification - ) { - // 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; + ) +{ + // 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; } EFI_STATUS @@ -350,244 +377,246 @@ MmcDetectCard ( EFI_MMC_HOST_PROTOCOL *MmcHost ) { - if (!MmcHost->IsCardPresent()) { - return EFI_NO_MEDIA; - } else { - return EFI_SUCCESS; - } + if (!MmcHost->IsCardPresent()) { + return EFI_NO_MEDIA; + } else { + return EFI_SUCCESS; + } } #define MMCI0_BLOCKLEN 512 #define MMCI0_TIMEOUT 10000 -EFI_STATUS MmcIoBlocks ( +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 CardSize, NumBlocks, BlockSize, CmdArg; - UINTN Timeout; - UINTN Cmd; - MMC_HOST_INSTANCE *MmcHostInstance; - EFI_MMC_HOST_PROTOCOL *MmcHost; - UINTN BytesRemainingToBeTransfered; - UINTN BlockCount = 1; + ) +{ + UINT32 Response[4]; + EFI_STATUS Status; + UINTN CardSize, NumBlocks, BlockSize, CmdArg; + UINTN Timeout; + UINTN Cmd; + MMC_HOST_INSTANCE *MmcHostInstance; + EFI_MMC_HOST_PROTOCOL *MmcHost; + UINTN BytesRemainingToBeTransfered; + UINTN BlockCount = 1; - MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS(This); - ASSERT(MmcHostInstance != 0); - MmcHost = MmcHostInstance->MmcHost; - ASSERT(MmcHost); + MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS(This); + ASSERT(MmcHostInstance != 0); + MmcHost = MmcHostInstance->MmcHost; + ASSERT(MmcHost); - if (MmcHost == 0) { - return EFI_INVALID_PARAMETER; + if (MmcHost == 0) { + return EFI_INVALID_PARAMETER; + } + + // Check if a Card is Present + if (!MmcHost->IsCardPresent()) { + 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; + return EFI_NO_MEDIA; + } + + // If the driver has not been initialized yet then go into Iddentification Mode + if (MmcHostInstance->State == MmcHwInitializationState) { + MmcIdentificationMode (MmcHostInstance); + + //Send a command to get Card specific data + CmdArg = MmcHostInstance->CardInfo.RCA << 16; + Status = MmcHost->SendCommand(MMC_CMD9, CmdArg); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD9): Error, Status=%r\n", Status)); + ASSERT(0); + return Status; } + //Read Response + MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_CSD,Response); + PrintCSD(Response); - // Check if a Card is Present - if (!MmcHost->IsCardPresent()) { - 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; - return EFI_NO_MEDIA; - } - - // If the driver has not been initialized yet then go into Iddentification Mode - if (MmcHostInstance->State == MmcHwInitializationState) { - MmcIdentificationMode (MmcHostInstance); - - //Send a command to get Card specific data - CmdArg = MmcHostInstance->CardInfo.RCA << 16; - Status = MmcHost->SendCommand(MMC_CMD9, CmdArg); - if (EFI_ERROR(Status)) { - DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD9): Error, Status=%r\n", Status)); - ASSERT(0); - return Status; - } - //Read Response - MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_CSD,Response); - PrintCSD(Response); - - if (MmcHostInstance->CardInfo.CardType == SD_CARD_2_HIGH) { - ASSERT(0); //TODO: Implementation needed - CardSize = 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(); - MmcHostInstance->BlockIo.Media->MediaPresent = TRUE; - MmcHostInstance->BlockIo.Media->MediaId++; - - CmdArg = MmcHostInstance->CardInfo.RCA << 16; - Status = MmcHost->SendCommand(MMC_CMD7, CmdArg); - if (EFI_ERROR(Status)) { - DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD7): Error and Status = %r\n", Status)); - ASSERT(0); - return Status; - } - - Status = MmcNotifyState (MmcHostInstance, MmcTransferState); - if (EFI_ERROR(Status)) { - DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcTransferState\n")); - return Status; - } + if (MmcHostInstance->CardInfo.CardType == SD_CARD_2_HIGH) { + ASSERT(0); //TODO: Implementation needed + CardSize = MMC_CSD_GET_DEVICESIZE(Response); + NumBlocks = ((CardSize + 1) * 1024);; + BlockSize = 1 << MMC_CSD_GET_READBLLEN(Response); } else { - // Maybe test if the card has changed to update gMmcMedia information - if (MmcHostInstance->State == MmcTransferState) { - //DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : MmcTransferState\n")); - } else if (MmcHostInstance->State == MmcStandByState) { - DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : MmcStandByState\n")); - } else { - ASSERT(0); - } + CardSize = MMC_CSD_GET_DEVICESIZE(Response); + NumBlocks = (CardSize + 1) * (1 << (MMC_CSD_GET_DEVICESIZEMULT(Response) + 2)); + BlockSize = 1 << MMC_CSD_GET_READBLLEN(Response); } - if (Lba > This->Media->LastBlock) { - ASSERT(0); - return EFI_INVALID_PARAMETER; - } - - if ((BufferSize % This->Media->BlockSize) != 0) { - ASSERT(0); - return EFI_BAD_BUFFER_SIZE; + //For >=2G card, BlockSize may be 1K, but the transfer size is 512 bytes. + if (BlockSize > 512) { + NumBlocks = MultU64x32(NumBlocks, BlockSize/512); + BlockSize = 512; } - BytesRemainingToBeTransfered = BufferSize; - while (BytesRemainingToBeTransfered > 0) { + MmcHostInstance->BlockIo.Media->LastBlock = (NumBlocks - 1); + MmcHostInstance->BlockIo.Media->BlockSize = BlockSize; + MmcHostInstance->BlockIo.Media->ReadOnly = MmcHost->IsReadOnly(); + MmcHostInstance->BlockIo.Media->MediaPresent = TRUE; + MmcHostInstance->BlockIo.Media->MediaId++; - //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(MMC_CMD13, CmdArg); - if (!EFI_ERROR(Status)){ - MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_R1,Response); - } - } - - if (0 == Timeout) { - DEBUG((EFI_D_ERROR, "The Card is busy\n")); - return EFI_NOT_READY; + CmdArg = MmcHostInstance->CardInfo.RCA << 16; + Status = MmcHost->SendCommand(MMC_CMD7, CmdArg); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD7): Error and Status = %r\n", Status)); + ASSERT(0); + return Status; } - // Set Block Length - Status = MmcHost->SendCommand(MMC_CMD16, This->Media->BlockSize); - if (EFI_ERROR(Status)) { - DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD16): Error This->Media->BlockSize:%d and Error = %r\n",This->Media->BlockSize, Status)); - return Status; - } + Status = MmcNotifyState (MmcHostInstance, MmcTransferState); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcTransferState\n")); + return Status; + } + } else { + // Maybe test if the card has changed to update gMmcMedia information + if (MmcHostInstance->State == MmcTransferState) { + //DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : MmcTransferState\n")); + } else if (MmcHostInstance->State == MmcStandByState) { + DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : MmcStandByState\n")); + } else { + ASSERT(0); + } + } - // Block Count (not used). Could return an error for SD card - MmcHost->SendCommand(MMC_CMD23, BlockCount); + if (Lba > This->Media->LastBlock) { + ASSERT(0); + return EFI_INVALID_PARAMETER; + } - //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 ((BufferSize % This->Media->BlockSize) != 0) { + ASSERT(0); + return EFI_BAD_BUFFER_SIZE; + } - if (Transfer == MMC_IOBLOCKS_READ) { + 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(MMC_CMD13, CmdArg); + if (!EFI_ERROR(Status)) { + MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_R1,Response); + } + } + + if (0 == Timeout) { + DEBUG((EFI_D_ERROR, "The Card is busy\n")); + return EFI_NOT_READY; + } + + // Set Block Length + Status = MmcHost->SendCommand(MMC_CMD16, This->Media->BlockSize); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD16): Error This->Media->BlockSize:%d and Error = %r\n",This->Media->BlockSize, Status)); + return Status; + } + + // Block Count (not used). Could return an error for SD card + MmcHost->SendCommand(MMC_CMD23, BlockCount); + + //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) { #ifndef USE_STREAM - // Read a single block - Cmd = MMC_CMD17; + // Read a single block + Cmd = MMC_CMD17; #else - //TODO: Should we support read stream (MMC_CMD11) + //TODO: Should we support read stream (MMC_CMD11) #endif - } else { + } else { #ifndef USE_STREAM - // Write a single block - Cmd = MMC_CMD24; + // Write a single block + Cmd = MMC_CMD24; #else - //TODO: Should we support write stream (MMC_CMD20) + //TODO: Should we support write stream (MMC_CMD20) #endif - } - Status = MmcHost->SendCommand(Cmd, CmdArg); - if (EFI_ERROR(Status)) { - DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD%d): Error %r\n",Cmd, Status)); - return Status; - } - - if (Transfer == MMC_IOBLOCKS_READ) { -#ifndef USE_STREAM - // Read one block of Data - Status = MmcHost->ReadBlockData(Lba,This->Media->BlockSize,Buffer); - if (EFI_ERROR(Status)) { - DEBUG((EFI_D_BLKIO, "MmcIdentificationMode(): Error Read Block Data and Status = %r\n", Status)); - return Status; - } -#else - //TODO: Read a steam - ASSERT(0); -#endif - Status = MmcNotifyState (MmcHostInstance, MmcProgrammingState); - if (EFI_ERROR(Status)) { - DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcProgrammingState\n")); - return Status; - } - } else { -#ifndef USE_STREAM - // Write one block of Data - Status = MmcHost->WriteBlockData(Lba,This->Media->BlockSize,Buffer); - if (EFI_ERROR(Status)) { - DEBUG((EFI_D_BLKIO, "MmcIdentificationMode(): Error Write Block Data and Status = %r\n", Status)); - return Status; - } -#else - //TODO: Write a steam - ASSERT(0); -#endif - } - - // Command 12 - Stop transmission (ends read) - Status = MmcHost->SendCommand(MMC_CMD12, 0); - if (!EFI_ERROR(Status)) { - MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_R1b,Response); - } - - // 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(MMC_CMD13, CmdArg); - if (!EFI_ERROR(Status)) { - MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_R1,Response); - } - NanoSecondDelay(100); - Timeout--; - } - - Status = MmcNotifyState (MmcHostInstance, MmcTransferState); - if (EFI_ERROR(Status)) { - DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcTransferState\n")); - return Status; - } - - BytesRemainingToBeTransfered -= This->Media->BlockSize; - Lba += BlockCount; - Buffer = (UINT8 *)Buffer + This->Media->BlockSize; + } + Status = MmcHost->SendCommand(Cmd, CmdArg); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD%d): Error %r\n",Cmd, Status)); + return Status; } - return EFI_SUCCESS; + if (Transfer == MMC_IOBLOCKS_READ) { +#ifndef USE_STREAM + // Read one block of Data + Status = MmcHost->ReadBlockData(Lba,This->Media->BlockSize,Buffer); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_BLKIO, "MmcIdentificationMode(): Error Read Block Data and Status = %r\n", Status)); + return Status; + } +#else + //TODO: Read a steam + ASSERT(0); +#endif + Status = MmcNotifyState (MmcHostInstance, MmcProgrammingState); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcProgrammingState\n")); + return Status; + } + } else { +#ifndef USE_STREAM + // Write one block of Data + Status = MmcHost->WriteBlockData(Lba,This->Media->BlockSize,Buffer); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_BLKIO, "MmcIdentificationMode(): Error Write Block Data and Status = %r\n", Status)); + return Status; + } +#else + //TODO: Write a steam + ASSERT(0); +#endif + } + + // Command 12 - Stop transmission (ends read) + Status = MmcHost->SendCommand(MMC_CMD12, 0); + if (!EFI_ERROR(Status)) { + MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_R1b,Response); + } + + // 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(MMC_CMD13, CmdArg); + if (!EFI_ERROR(Status)) { + MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_R1,Response); + } + NanoSecondDelay(100); + Timeout--; + } + + Status = MmcNotifyState (MmcHostInstance, MmcTransferState); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcTransferState\n")); + return Status; + } + + BytesRemainingToBeTransfered -= This->Media->BlockSize; + Lba += BlockCount; + Buffer = (UINT8 *)Buffer + This->Media->BlockSize; + } + + return EFI_SUCCESS; } EFI_STATUS @@ -598,8 +627,9 @@ MmcReadBlocks ( IN EFI_LBA Lba, IN UINTN BufferSize, OUT VOID *Buffer - ) { - return MmcIoBlocks (This, MMC_IOBLOCKS_READ, MediaId, Lba, BufferSize, Buffer); + ) +{ + return MmcIoBlocks (This, MMC_IOBLOCKS_READ, MediaId, Lba, BufferSize, Buffer); } EFI_STATUS @@ -610,15 +640,17 @@ MmcWriteBlocks ( IN EFI_LBA Lba, IN UINTN BufferSize, IN VOID *Buffer - ) { - return MmcIoBlocks (This, MMC_IOBLOCKS_WRITE, MediaId, Lba, BufferSize, Buffer); + ) +{ + return MmcIoBlocks (This, MMC_IOBLOCKS_WRITE, MediaId, Lba, BufferSize, Buffer); } EFI_STATUS EFIAPI MmcFlushBlocks ( IN EFI_BLOCK_IO_PROTOCOL *This - ) { - return EFI_SUCCESS; + ) +{ + return EFI_SUCCESS; }