From nobody Fri Apr 26 04:16:50 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1496985996537354.0255635671506; Thu, 8 Jun 2017 22:26:36 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 4723B20945520; Thu, 8 Jun 2017 22:25:24 -0700 (PDT) Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 69AAA21967BC4 for ; Thu, 8 Jun 2017 22:25:22 -0700 (PDT) Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 08 Jun 2017 22:26:31 -0700 Received: from shwdeopenpsi011.ccr.corp.intel.com (HELO SHWDEOPENPSI011.local) ([10.239.9.135]) by fmsmga005.fm.intel.com with SMTP; 08 Jun 2017 22:26:25 -0700 X-Original-To: edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.39,316,1493708400"; d="scan'208";a="112274781" Date: Fri, 9 Jun 2017 13:26:26 +0800 From: lushifex To: edk2-devel@lists.01.org Message-ID: X-Mailer: TortoiseGit MIME-Version: 1.0 Subject: [edk2] [Patch][edk2-platforms/devel-MinnowBoard3-UDK2017] Add eMMC/SD. X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: , david.wei@intel.com Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add Platform eMMC/SD driver. This reverts commit 66d48af2d24645263b8068e261abc58d84cc2b93. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: lushifex --- .../BroxtonPlatformPkg/PlatformDsc/Components.dsc | 11 +- Platform/BroxtonPlatformPkg/PlatformPkg.fdf | 10 +- .../Sdio/Dxe/MMC/MmcHostDxe/ComponentName.c | 162 ++ .../Sdio/Dxe/MMC/MmcHostDxe/MmcHostController.c | 1529 ++++++++++++ .../Sdio/Dxe/MMC/MmcHostDxe/MmcHostDriver.c | 613 +++++ .../Sdio/Dxe/MMC/MmcHostDxe/MmcHostDriver.h | 585 +++++ .../Sdio/Dxe/MMC/MmcHostDxe/MmcHostDxe.inf | 59 + .../Sdio/Dxe/MMC/MmcMediaDeviceDxe/ComponentName.c | 159 ++ .../Sdio/Dxe/MMC/MmcMediaDeviceDxe/MMCSDBlockIo.c | 666 ++++++ .../Sdio/Dxe/MMC/MmcMediaDeviceDxe/MMCSDTransfer.c | 2524 ++++++++++++++++= ++++ .../Dxe/MMC/MmcMediaDeviceDxe/MediaDeviceDriver.c | 605 +++++ .../MMC/MmcMediaDeviceDxe/MmcMediaDeviceDxe.inf | 71 + .../Sdio/Dxe/SD/SdControllerDxe/ComponentName.c | 233 ++ .../Sdio/Dxe/SD/SdControllerDxe/ComponentName.h | 145 ++ .../Sdio/Dxe/SD/SdControllerDxe/SdController.c | 1804 ++++++++++++++ .../Sdio/Dxe/SD/SdControllerDxe/SdController.h | 314 +++ .../Dxe/SD/SdControllerDxe/SdControllerDxe.inf | 59 + .../Sdio/Dxe/SD/SdMediaDeviceDxe/CEATA.c | 638 +++++ .../Sdio/Dxe/SD/SdMediaDeviceDxe/CEATABlockIo.c | 384 +++ .../Sdio/Dxe/SD/SdMediaDeviceDxe/ComponentName.c | 219 ++ .../Sdio/Dxe/SD/SdMediaDeviceDxe/ComponentName.h | 145 ++ .../Sdio/Dxe/SD/SdMediaDeviceDxe/MMCSDBlockIo.c | 547 +++++ .../Sdio/Dxe/SD/SdMediaDeviceDxe/MMCSDTransfer.c | 1661 +++++++++++++ .../Sdio/Dxe/SD/SdMediaDeviceDxe/SdMediaDevice.c | 324 +++ .../Sdio/Dxe/SD/SdMediaDeviceDxe/SdMediaDevice.h | 466 ++++ .../Dxe/SD/SdMediaDeviceDxe/SdMediaDeviceDxe.inf | 64 + 26 files changed, 13989 insertions(+), 8 deletions(-) create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/M= MC/MmcHostDxe/ComponentName.c create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/M= MC/MmcHostDxe/MmcHostController.c create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/M= MC/MmcHostDxe/MmcHostDriver.c create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/M= MC/MmcHostDxe/MmcHostDriver.h create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/M= MC/MmcHostDxe/MmcHostDxe.inf create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/M= MC/MmcMediaDeviceDxe/ComponentName.c create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/M= MC/MmcMediaDeviceDxe/MMCSDBlockIo.c create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/M= MC/MmcMediaDeviceDxe/MMCSDTransfer.c create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/M= MC/MmcMediaDeviceDxe/MediaDeviceDriver.c create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/M= MC/MmcMediaDeviceDxe/MmcMediaDeviceDxe.inf create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/S= D/SdControllerDxe/ComponentName.c create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/S= D/SdControllerDxe/ComponentName.h create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/S= D/SdControllerDxe/SdController.c create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/S= D/SdControllerDxe/SdController.h create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/S= D/SdControllerDxe/SdControllerDxe.inf create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/S= D/SdMediaDeviceDxe/CEATA.c create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/S= D/SdMediaDeviceDxe/CEATABlockIo.c create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/S= D/SdMediaDeviceDxe/ComponentName.c create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/S= D/SdMediaDeviceDxe/ComponentName.h create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/S= D/SdMediaDeviceDxe/MMCSDBlockIo.c create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/S= D/SdMediaDeviceDxe/MMCSDTransfer.c create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/S= D/SdMediaDeviceDxe/SdMediaDevice.c create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/S= D/SdMediaDeviceDxe/SdMediaDevice.h create mode 100644 Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/S= D/SdMediaDeviceDxe/SdMediaDeviceDxe.inf diff --git a/Platform/BroxtonPlatformPkg/PlatformDsc/Components.dsc b/Platf= orm/BroxtonPlatformPkg/PlatformDsc/Components.dsc index 7efcdb4..1322576 100644 --- a/Platform/BroxtonPlatformPkg/PlatformDsc/Components.dsc +++ b/Platform/BroxtonPlatformPkg/PlatformDsc/Components.dsc @@ -386,11 +386,14 @@ $(PLATFORM_PACKAGE_COMMON)/Console/LpssUartSerialDxe/LpssUartSerialDxe.i= nf =20 # - # eMMC/SD Card + # SDIO # - MdeModulePkg/Bus/Pci/SdMmcPciHcDxe/SdMmcPciHcDxe.inf - MdeModulePkg/Bus/Sd/EmmcDxe/EmmcDxe.inf - MdeModulePkg/Bus/Sd/SdDxe/SdDxe.inf + $(PLATFORM_SI_PACKAGE)/SouthCluster/Sdio/Dxe/MMC/MmcHostDxe/MmcHostDxe.i= nf + + $(PLATFORM_SI_PACKAGE)/SouthCluster/Sdio/Dxe/MMC/MmcMediaDeviceDxe/MmcMe= diaDeviceDxe.inf + + $(PLATFORM_SI_PACKAGE)/SouthCluster/Sdio/Dxe/SD/SdControllerDxe/SdContro= llerDxe.inf + $(PLATFORM_SI_PACKAGE)/SouthCluster/Sdio/Dxe/SD/SdMediaDeviceDxe/SdMedia= DeviceDxe.inf =20 =20 !if $(ACPI50_ENABLE) =3D=3D TRUE diff --git a/Platform/BroxtonPlatformPkg/PlatformPkg.fdf b/Platform/Broxton= PlatformPkg/PlatformPkg.fdf index 73174af..ac09ac3 100644 --- a/Platform/BroxtonPlatformPkg/PlatformPkg.fdf +++ b/Platform/BroxtonPlatformPkg/PlatformPkg.fdf @@ -434,14 +434,16 @@ APRIORI DXE { INF MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf =20 # - # eMMC/SD Card + # SDIO # !if $(EMMC_DRIVER_ENABLE) =3D=3D TRUE - INF MdeModulePkg/Bus/Pci/SdMmcPciHcDxe/SdMmcPciHcDxe.inf - INF MdeModulePkg/Bus/Sd/EmmcDxe/EmmcDxe.inf - INF MdeModulePkg/Bus/Sd/SdDxe/SdDxe.inf =20 + INF $(PLATFORM_SI_PACKAGE)/SouthCluster/Sdio/Dxe/MMC/MmcHostDxe/MmcHostD= xe.inf + INF $(PLATFORM_SI_PACKAGE)/SouthCluster/Sdio/Dxe/MMC/MmcMediaDeviceDxe/M= mcMediaDeviceDxe.inf !endif =20 + INF $(PLATFORM_SI_PACKAGE)/SouthCluster/Sdio/Dxe/SD/SdControllerDxe/SdCo= ntrollerDxe.inf + INF $(PLATFORM_SI_PACKAGE)/SouthCluster/Sdio/Dxe/SD/SdMediaDeviceDxe/SdM= ediaDeviceDxe.inf + INF MdeModulePkg/Universal/Acpi/S3SaveStateDxe/S3SaveStateDxe.inf INF IntelFrameworkModulePkg/Universal/Acpi/AcpiS3SaveDxe/AcpiS3SaveDxe.i= nf =20 diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcH= ostDxe/ComponentName.c b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/= Dxe/MMC/MmcHostDxe/ComponentName.c new file mode 100644 index 0000000..f099fa8 --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcHostDxe/= ComponentName.c @@ -0,0 +1,162 @@ +/** @file + Component Name protocol implementation + + Copyright (c) 2012 - 2016, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include "MmcHostDriver.h" + +// +// EFI Component Name Protocol +// +EFI_COMPONENT_NAME_PROTOCOL gMmcHostComponentName =3D { + MmcHostComponentNameGetDriverName, + MmcHostComponentNameGetControllerName, + "eng" +}; + + +static EFI_UNICODE_STRING_TABLE mMmcHostDriverNameTable[] =3D { + { "eng", L"UEFI MMC Host Controller Driver" }, + { NULL , NULL } +}; + + +/** + Retrieves a Unicode string that is the user readable name of the EFI Dri= ver. + + @param[in] This A pointer to the EFI_COMPONENT_NAME_= PROTOCOL instance. + @param[in] Language A pointer to a three character ISO 6= 39-2 language identifier. + This is the language of the driver n= ame that that the caller + is requesting, and it must match one= of the languages specified + in SupportedLanguages. The number o= f languages supported by a + driver is up to the driver writer. + @param[out] DriverName A pointer to the Unicode string to r= eturn. This Unicode string + is the name of the driver specified = by This in the language + specified by Language. + + + @retval EFI_SUCCESS The Unicode string for the Driver sp= ecified by This + and the language specified by Langua= ge was returned + in DriverName. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER DriverName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This does no= t support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +MmcHostComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString ( + Language, + gMmcHostComponentName.SupportedLanguages, + mMmcHostDriverNameTable, + DriverName + ); +} + + +/** + Retrieves a Unicode string that is the user readable name of the control= ler + that is being managed by an EFI Driver. + + @param[in] This A pointer to the EFI_COMPONENT_NAME_= PROTOCOL instance. + @param[in] ControllerHandle The handle of a controller that the = driver specified by + This is managing. This handle speci= fies the controller + whose name is to be returned. + @param[in] ChildHandle The handle of the child controller t= o retrieve the name + of. This is an optional parameter t= hat may be NULL. It + will be NULL for device drivers. It= will also be NULL + for a bus drivers that wish to retri= eve the name of the + bus controller. It will not be NULL= for a bus driver + that wishes to retrieve the name of = a child controller. + @param[in] Language A pointer to a three character ISO 6= 39-2 language + identifier. This is the language of= the controller name + that that the caller is requesting, = and it must match one + of the languages specified in Suppor= tedLanguages. The + number of languages supported by a d= river is up to the + driver writer. + @param[out] ControllerName A pointer to the Unicode string to r= eturn. This Unicode + string is the name of the controller= specified by + ControllerHandle and ChildHandle in = the language + specified by Language from the point= of view of the + driver specified by This. + + + @retval EFI_SUCCESS The Unicode string for the user read= able name in the + language specified by Language for t= he driver + specified by This was returned in Dr= iverName. + @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_= HANDLE. + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is no= t a valid + EFI_HANDLE. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This is not= currently + managing the controller specified by + ControllerHandle and ChildHandle. + @retval EFI_UNSUPPORTED The driver specified by This does no= t support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +MmcHostComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle, OPTIONAL + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo; + MMCHOST_DATA *MmcHostData; + + // + // This is a device driver, so ChildHandle must be NULL. + // + if (ChildHandle !=3D NULL) { + return EFI_UNSUPPORTED; + } + + // + // Get the device context + // + Status =3D gBS->OpenProtocol ( + ControllerHandle, + &gEfiMmcHostIoProtocolGuid, + (VOID **) &MmcHostIo, + gMmcHostDriverBinding.DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + MmcHostData =3D MMCHOST_DATA_FROM_THIS (MmcHostIo); + + return LookupUnicodeString ( + Language, + gMmcHostComponentName.SupportedLanguages, + MmcHostData->ControllerNameTable, + ControllerName + ); +} + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcH= ostDxe/MmcHostController.c b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/S= dio/Dxe/MMC/MmcHostDxe/MmcHostController.c new file mode 100644 index 0000000..f3cd50e --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcHostDxe/= MmcHostController.c @@ -0,0 +1,1529 @@ +/** @file + MMC Host I/O protocol implementation + + Copyright (c) 1999 - 2016, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include "MmcHostDriver.h" + +#define MMC_HOST_DEBUG(a) do { \ + if (MmcHostData->EnableVerboseDebug) { \ + DEBUG (a); \ + } \ + } while (0); + +UINT32 gMmcHostDebugLevel =3D DEBUG_INFO; + +/** + Get Error Reason + + @param[in] CommandIndex The command index to set the command index= field of command register + @param[in] ErrorCode Mmchost specific error code + + @retval EFI_DEVICE_ERROR + @retval EFI_TIMEOUT + @retval EFI_CRC_ERROR + +**/ +STATIC +EFI_STATUS +GetErrorReason ( + IN UINT16 CommandIndex, + IN UINT16 ErrorCode + ) +{ + EFI_STATUS Status; + + Status =3D EFI_DEVICE_ERROR; + DEBUG ((gMmcHostDebugLevel, "[%2d] -- ", CommandIndex)); + + if (ErrorCode & BIT0) { + Status =3D EFI_TIMEOUT; + DEBUG ((gMmcHostDebugLevel, "Command Timeout Error")); + } + + if (ErrorCode & BIT1) { + Status =3D EFI_CRC_ERROR; + DEBUG ((gMmcHostDebugLevel, "Command CRC Error")); + } + + if (ErrorCode & BIT2) { + DEBUG ((gMmcHostDebugLevel, "Command End Bit Error")); + } + + if (ErrorCode & BIT3) { + DEBUG ((gMmcHostDebugLevel, "Command Index Error")); + } + if (ErrorCode & BIT4) { + Status =3D EFI_TIMEOUT; + DEBUG ((gMmcHostDebugLevel, "Data Timeout Error")); + } + + if (ErrorCode & BIT5) { + Status =3D EFI_CRC_ERROR; + DEBUG ((gMmcHostDebugLevel, "Data CRC Error")); + } + + if (ErrorCode & BIT6) { + DEBUG ((gMmcHostDebugLevel, "Data End Bit Error")); + } + + if (ErrorCode & BIT7) { + DEBUG ((gMmcHostDebugLevel, "Current Limit Error")); + } + + if (ErrorCode & BIT8) { + DEBUG ((gMmcHostDebugLevel, "Auto CMD12 Error")); + } + + if (ErrorCode & BIT9) { + DEBUG ((gMmcHostDebugLevel, "ADMA Error")); + } + + DEBUG ((gMmcHostDebugLevel, "\n")); + + return Status; +} + + +/** + Read MMC host register + + @param[in] MmcHost Pointer to EFI_MMC_HOST_IO_PR= OTOCOL + @param[in] Width Signifies the width of the me= mory or I/O operations. + @param[in] Offset Offset of the MMC Card + @param[in, out] Buffer Buffer read from MMC Card + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +STATIC +EFI_STATUS +MmcHostRead ( + IN MMCHOST_DATA *MmcHost, + IN EFI_PCI_IO_PROTOCOL_WIDTH Width, + IN UINTN Offset, + IN OUT VOID *Buffer + ) +{ + return MmcHost->PciIo->Mem.Read ( + MmcHost->PciIo, + Width, + 0, + (UINT64) Offset, + 1, + Buffer + ); +} + + +/** + Read MMC host register + + @param[in] MmcHost Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] Offset Offset of the MMC Card + + @retval Data Data read from MMC Card + +**/ +UINT8 +MmcHostRead8 ( + IN MMCHOST_DATA *MmcHost, + IN UINTN Offset + ) +{ + UINT8 Data; + + MmcHostRead (MmcHost, EfiPciIoWidthUint8, Offset, &Data); + + return Data; +} + + +/** + Read MMC host register + + @param[in] MmcHost Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] Offset Offset of the MMC Card + + @retval Data Data read from MMC Card + +**/ +UINT16 +MmcHostRead16 ( + IN MMCHOST_DATA *MmcHost, + IN UINTN Offset + ) +{ + UINT16 Data; + + MmcHostRead (MmcHost, EfiPciIoWidthUint16, Offset, &Data); + + return Data; +} + + +/** + Read MMC host register + + @param[in] MmcHost Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] Offset Offset of the MMC Card + + @retval Data Data read from MMC Card + +**/ +UINT32 +MmcHostRead32 ( + IN MMCHOST_DATA *MmcHost, + IN UINTN Offset + ) +{ + UINT32 Data; + + MmcHostRead (MmcHost, EfiPciIoWidthUint32, Offset, &Data); + + return Data; +} + + +/** + Dump MMC host registers + + @param[in] MmcHost Pointer to EFI_MMC_HOST_IO_PROTOCOL + + @retval None + +**/ +VOID +DebugPrintMmcHostRegisters ( + IN MMCHOST_DATA *MmcHost + ) +{ +#ifdef VERBOSE_REGISTER_ACCESS_DEBUG + UINTN Loop; + + if (!MmcHost->EnableVerboseDebug) { + return; + } + + for (Loop =3D 0; Loop < 0x40; Loop++) { + DEBUG ((EFI_D_INFO, " %02x", MmcHostRead8 (MmcHost, Loop))); + if ((Loop % 0x10) =3D=3D 0xf) { + DEBUG ((EFI_D_INFO, "\n")); + } + } + +#endif +} + + +/** + Dump MMC host registers before write + + @param[in] MmcHost Pointer to EFI_MMC_HOST_IO_PROTOCOL + + @retval None + +**/ +VOID +DebugPreMmcHostWrite ( + IN MMCHOST_DATA *MmcHost + ) +{ +#ifdef VERBOSE_REGISTER_ACCESS_DEBUG + STATIC UINTN DebugCount =3D 0; + + if (!MmcHost->EnableVerboseDebug) { + return; + } + + DebugCount++; + + if (DebugCount < 0x100) { + DEBUG ((EFI_D_INFO, "MMC HOST Registers before write:\n")); + DebugPrintMmcHostRegisters (MmcHost); + } +#endif +} + + +/** + Dump MMC host registers after write + + @param[in] MmcHost Pointer to EFI_MMC_HOST_IO_PROTOCOL + + @retval None + +**/ +VOID +DebugPostMmcHostWrite ( + IN MMCHOST_DATA *MmcHost + ) +{ +#ifdef VERBOSE_REGISTER_ACCESS_DEBUG + STATIC UINTN DebugCount =3D 0; + + if (!MmcHost->EnableVerboseDebug) { + return; + } + + DebugCount++; + + if (DebugCount < 0x10) { + DEBUG ((EFI_D_INFO, "MMC HOST Registers after write:\n")); + DebugPrintMmcHostRegisters (MmcHost); + } +#endif +} + + +/** + Write MMC host register + + @param[in] MmcHost Pointer to EFI_MMC_HOST_IO_PROTOC= OL + @param[in] Width Signifies the width of the memory= or I/O operations. + @param[in] Offset Offset of the MMC Card + @param[in, out] Buffer Buffer read from MMC Card + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +STATIC +EFI_STATUS +MmcHostWrite ( + IN MMCHOST_DATA *MmcHost, + IN EFI_PCI_IO_PROTOCOL_WIDTH Width, + IN UINTN Offset, + IN OUT VOID *Buffer + ) +{ + EFI_STATUS Status; + + DebugPreMmcHostWrite (MmcHost); + Status =3D MmcHost->PciIo->Mem.Write ( + MmcHost->PciIo, + Width, + 0, + (UINT64) Offset, + 1, + Buffer + ); + + DebugPostMmcHostWrite (MmcHost); + + return Status; +} + + +/** + Write MMC host register + + @param[in] MmcHost Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] Offset Offset of the MMC Card + @param[in] Data Data write to MMC Card + + @retval Data Written to MmcHost + +**/ +UINT8 +MmcHostWrite8 ( + IN MMCHOST_DATA *MmcHost, + IN UINTN Offset, + IN UINT8 Data + ) +{ + MmcHostWrite (MmcHost, EfiPciIoWidthUint8, Offset, &Data); + + return Data; +} + + +/** + Write MMC host register + + @param[in] MmcHost Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] Offset Offset of the MMC Card + @param[in] Data Data write to MMC Card + + @retval Data Data written to MMC Card + +**/ +UINT16 +MmcHostWrite16 ( + IN MMCHOST_DATA *MmcHost, + IN UINTN Offset, + IN UINT16 Data + ) +{ + MmcHostWrite (MmcHost, EfiPciIoWidthUint16, Offset, &Data); + + return Data; +} + + +/** + Write MMC host register + + @param[in] MmcHost Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] Offset Offset of the MMC Card + @param[in] Data Data write to MMC Card + + @retval Data Data written to MMC Card + +**/ +UINT32 +MmcHostWrite32 ( + IN MMCHOST_DATA *MmcHost, + IN UINTN Offset, + IN UINT32 Data + ) +{ + MmcHostWrite (MmcHost, EfiPciIoWidthUint32, Offset, &Data); + + return Data; +} + + +/** + Check Controller Version + + @param[in] MmcHost Pointer to EFI_MMC_HOST_IO_PROTOCOL + + @retval Data Written to MmcHost + +**/ +UINT32 +CheckControllerVersion ( + IN MMCHOST_DATA *MmcHost + ) +{ + UINT16 Data16; + + Data16 =3D MmcHostRead16 (MmcHost, MMIO_CTRLRVER); + DEBUG ((EFI_D_INFO, "CheckControllerVersion: %x \n", Data16 & 0xFF)); + + return (Data16 & 0xFF); +} + + +/** + Power on/off the LED associated with the slot + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] Enable TRUE to set LED on, FALSE to set LED off + + @retval EFI_SUCCESS + +**/ +STATIC +EFI_STATUS +HostLEDEnable ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN BOOLEAN Enable + ) +{ + MMCHOST_DATA *MmcHostData; + UINT8 Data; + + MmcHostData =3D MMCHOST_DATA_FROM_THIS (This); + Data =3D MmcHostRead8 (MmcHostData, MMIO_HOSTCTL); + + if (Enable) { + // + //LED On + // + Data |=3D BIT0; + } else { + // + //LED Off + // + Data &=3D ~BIT0; + } + + MmcHostWrite8 (MmcHostData, MMIO_HOSTCTL, Data); + + return EFI_SUCCESS; +} + + +/** + The main function used to send the command to the card inserted into the= MMC host + slot. + It will assemble the arguments to set the command register and wait for = the command + and transfer completed until timeout. Then it will read the response reg= ister to fill + the ResponseData + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] CommandIndex The command index to set the command= index field of command register + @param[in] Argument Command argument to set the argument= field of command register + @param[in] DataType TRANSFER_TYPE, indicates no data, da= ta in or data out + @param[in] Buffer Contains the data read from / write = to the device + @param[in] BufferSize The size of the buffer + @param[in] ResponseType RESPONSE_TYPE + @param[in] TimeOut Time out value in 1 ms unit + @param[out] ResponseData Depending on the ResponseType, such = as CSD or card status + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +EFIAPI +SendCommand ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN UINT16 CommandIndex, + IN UINT32 Argument, + IN TRANSFER_TYPE DataType, + IN UINT8 *Buffer, OPTIONAL + IN UINT32 BufferSize, + IN RESPONSE_TYPE ResponseType, + IN UINT32 TimeOut, + OUT UINT32 *ResponseData OPTIONAL + ) +{ + EFI_STATUS Status; + MMCHOST_DATA *MmcHostData; + UINT32 ResponseDataCount; + UINT16 Data16; + UINT32 Data32; + UINT64 Data64; + UINT8 Index; + BOOLEAN CommandCompleted; + INT32 Timeout =3D 1000; + + Status =3D EFI_SUCCESS; + ResponseDataCount =3D 1; + MmcHostData =3D MMCHOST_DATA_FROM_THIS (This); + + if (Buffer !=3D NULL && DataType =3D=3D NoData) { + Status =3D EFI_INVALID_PARAMETER; + DEBUG ((gMmcHostDebugLevel, "SendCommand: Invalid parameter -> \n")); + goto Exit; + } + + if (((UINTN) Buffer & (This->HostCapability.BoundarySize - 1)) !=3D (UIN= TN) NULL) { + Status =3D EFI_INVALID_PARAMETER; + DEBUG ((gMmcHostDebugLevel, "SendCommand: Invalid parameter -> \n")); + goto Exit; + } + + DEBUG ((DEBUG_INFO, "SendCommand: CMD%d \n", CommandIndex)); + if (MmcHostData->EnableVerboseDebug) { + DEBUG ((EFI_D_INFO, "00 -10: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x00), MmcHostRead32 (MmcHostData, 0x04), MmcHostRead32 (Mm= cHostData, 0x08), MmcHostRead32 (MmcHostData, 0x0C))); + DEBUG ((EFI_D_INFO, "10 -20: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x10), MmcHostRead32 (MmcHostData, 0x14), MmcHostRead32 (Mm= cHostData, 0x18), MmcHostRead32 (MmcHostData, 0x1C))); + DEBUG ((EFI_D_INFO, "20 -30: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x20), MmcHostRead32 (MmcHostData, 0x24), MmcHostRead32 (Mm= cHostData, 0x28), MmcHostRead32 (MmcHostData, 0x2C))); + DEBUG ((EFI_D_INFO, "30 -40: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x30), MmcHostRead32 (MmcHostData, 0x34), MmcHostRead32 (Mm= cHostData, 0x38), MmcHostRead32 (MmcHostData, 0x3C))); + DEBUG ((EFI_D_INFO, "40 -50: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x40), MmcHostRead32 (MmcHostData, 0x44), MmcHostRead32 (Mm= cHostData, 0x48), MmcHostRead32 (MmcHostData, 0x4C))); + DEBUG ((EFI_D_INFO, "50 -60: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x50), MmcHostRead32 (MmcHostData, 0x54), MmcHostRead32 (Mm= cHostData, 0x58), MmcHostRead32 (MmcHostData, 0x5C))); + } + + // + // Check CMD INHIBIT and DATA INHIBIT before send command + // + do { + Data32 =3D MmcHostRead32 (MmcHostData, MMIO_PSTATE); + if (MmcHostData->EnableVerboseDebug) { + DEBUG ((EFI_D_INFO, "Wait CMD INHIBIT %x\n",Data32 )); + } + } while (Timeout-- > 0 && Data32 & BIT0); + + Timeout =3D 1000; + do { + Data32 =3D MmcHostRead32 (MmcHostData, MMIO_PSTATE); + if (MmcHostData->EnableVerboseDebug) { + DEBUG ((EFI_D_INFO, "Wait DATA INHIBIT %x\n",Data32 )); + } + } while (Timeout-- >0 && Data32 & BIT1); + + // + //Clear status bits + // + MMC_HOST_DEBUG ((EFI_D_INFO, "NINTSTS(0x30) <- 0x%x\n", (UINTN) 0xffff)); + MmcHostWrite16 (MmcHostData, MMIO_NINTSTS, 0xffff); + MMC_HOST_DEBUG ((EFI_D_INFO, "SendCommand: NINTSTS (0x30): 0x%x\n", MmcH= ostRead16 (MmcHostData, MMIO_NINTSTS))); + + MMC_HOST_DEBUG ((EFI_D_INFO, "ERINTSTS <- 0x%x\n", (UINTN) 0xffff)); + MmcHostWrite16 (MmcHostData, MMIO_ERINTSTS, 0xffff); + MMC_HOST_DEBUG ((EFI_D_INFO, "SendCommand: ERINTSTS (0x32): 0x%x\n", Mmc= HostRead16 (MmcHostData, MMIO_ERINTSTS))); + + Data16 =3D MmcHostRead16 (MmcHostData, MMIO_NINTEN); + MmcHostWrite16 (MmcHostData, MMIO_NINTEN, (Data16 | BIT3)); + MMC_HOST_DEBUG ((DEBUG_INFO, "SendCommand: NINTEN (0x34) <- 0x%x\n", Mmc= HostRead16 (MmcHostData, MMIO_NINTEN))); + MMC_HOST_DEBUG ((DEBUG_INFO, "SendCommand: DMAADR (0x00) <- 0x%x\n", (UI= NTN) Buffer)); + MmcHostWrite32 (MmcHostData, MMIO_DMAADR, (UINT32) (UINTN) Buffer); + + if (Buffer !=3D NULL) { + Data16 =3D MmcHostRead16 (MmcHostData, MMIO_BLKSZ); + Data16 &=3D ~(0xFFF); + if (BufferSize <=3D MmcHostData->BlockLength) { + Data16 |=3D BufferSize; + } else { + Data16 |=3D MmcHostData->BlockLength; + } + Data16 |=3D 0x7000; // Set to 512KB for SDMA block size + } else { + Data16 =3D 0; + } + + MMC_HOST_DEBUG ((EFI_D_INFO, "SendCommand: BLKSZ(0x04) <- 0x%x\n", Data1= 6)); + MmcHostWrite16 (MmcHostData, MMIO_BLKSZ, Data16); + + if (Buffer !=3D NULL) { + if (BufferSize <=3D MmcHostData->BlockLength) { + Data16 =3D 1; + } else { + Data16 =3D (UINT16) (BufferSize / MmcHostData->BlockLength); + } + } else { + Data16 =3D 0; + } + + MMC_HOST_DEBUG ((EFI_D_INFO, "SendCommand: BLKCNT(0x06) <- 0x%x\n", Data= 16)); + MmcHostWrite16 (MmcHostData, MMIO_BLKCNT, Data16); + + // + // Argument + // + MMC_HOST_DEBUG ((EFI_D_INFO, "SendCommand: CMDARG (0x08) <- 0x%x\n", Arg= ument)); + MmcHostWrite32 (MmcHostData, MMIO_CMDARG, Argument); + + // + // Transfer Mode + // + Data16 =3D MmcHostRead16 (MmcHostData, MMIO_XFRMODE); + MMC_HOST_DEBUG ((EFI_D_INFO, "SendCommand: XFRMODE (0x0C) mode read 0x%x= \n", Data16)); + + // + // BIT0 DMA Enable + // BIT2 Auto Cmd12 + // + if (DataType =3D=3D InData) { + Data16 |=3D BIT4 | BIT0; + } else if (DataType =3D=3D OutData) { + Data16 &=3D ~BIT4; + Data16 |=3D BIT0; + } else { + Data16 &=3D ~(BIT4 | BIT0); + } + + if (BufferSize <=3D MmcHostData->BlockLength) { + Data16 &=3D ~ (BIT5 | BIT1 | BIT2); + } else { + if (MmcHostData->IsAutoStopCmd && !MmcHostData->IsEmmc) { + Data16 |=3D (BIT5 | BIT1 | BIT2); + } else { + Data16 |=3D (BIT5 | BIT1); + } + } + if (CommandIndex =3D=3D SEND_EXT_CSD) { + Data16 |=3D BIT1; + MMC_HOST_DEBUG ((EFI_D_INFO, "SendCommand:Enable bit 1, XFRMODE <- 0x%= x\n", Data16)); + } + + MMC_HOST_DEBUG ((EFI_D_INFO, "SendCommand: XFRMODE (0x0C) <- 0x%x\n", Da= ta16)); + MmcHostWrite16 (MmcHostData, MMIO_XFRMODE, Data16); + + switch (ResponseType) { + case ResponseNo: + Data16 =3D (CommandIndex << 8); + ResponseDataCount =3D 0; + break; + + case ResponseR1: + case ResponseR5: + case ResponseR6: + case ResponseR7: + Data16 =3D (CommandIndex << 8) | BIT1 | BIT4| BIT3; + ResponseDataCount =3D 1; + break; + + case ResponseR1b: + case ResponseR5b: + Data16 =3D (CommandIndex << 8) | BIT0 | BIT1 | BIT4| BIT3; + ResponseDataCount =3D 1; + break; + + case ResponseR2: + Data16 =3D (CommandIndex << 8) | BIT0 | BIT3; + ResponseDataCount =3D 4; + break; + + case ResponseR3: + case ResponseR4: + Data16 =3D (CommandIndex << 8) | BIT1; + ResponseDataCount =3D 1; + break; + + default: + ASSERT (0); + Status =3D EFI_INVALID_PARAMETER; + goto Exit; + } + + if (DataType !=3D NoData) { + Data16 |=3D BIT5; + } + + HostLEDEnable (This, TRUE); + + MMC_HOST_DEBUG ((EFI_D_INFO, "SendCommand: SDCMD(0x0E) <- 0x%x\n", Data1= 6)); + CommandCompleted =3D FALSE; + MmcHostWrite16 (MmcHostData, MMIO_SDCMD, Data16); + + if (MmcHostData->EnableVerboseDebug) { + DEBUG ((EFI_D_INFO, "After write to Command\n")); + DEBUG ((EFI_D_INFO, "00 -10: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x00), MmcHostRead32 (MmcHostData, 0x04), MmcHostRead32 (Mm= cHostData, 0x08), MmcHostRead32 (MmcHostData, 0x0C))); + DEBUG ((EFI_D_INFO, "10 -20: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x10), MmcHostRead32 (MmcHostData, 0x14), MmcHostRead32 (Mm= cHostData, 0x18), MmcHostRead32 (MmcHostData, 0x1C))); + DEBUG ((EFI_D_INFO, "20 -30: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x20), MmcHostRead32 (MmcHostData, 0x24), MmcHostRead32 (Mm= cHostData, 0x28), MmcHostRead32 (MmcHostData, 0x2C))); + DEBUG ((EFI_D_INFO, "30 -40: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x30), MmcHostRead32 (MmcHostData, 0x34), MmcHostRead32 (Mm= cHostData, 0x38), MmcHostRead32 (MmcHostData, 0x3C))); + DEBUG ((EFI_D_INFO, "40 -50: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x40), MmcHostRead32 (MmcHostData, 0x44), MmcHostRead32 (Mm= cHostData, 0x48), MmcHostRead32 (MmcHostData, 0x4C))); + DEBUG ((EFI_D_INFO, "50 -60: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x50), MmcHostRead32 (MmcHostData, 0x54), MmcHostRead32 (Mm= cHostData, 0x58), MmcHostRead32 (MmcHostData, 0x5C))); + } + + TimeOut *=3D 1000; //ms to us conversion + do { + Data16 =3D MmcHostRead16 (MmcHostData, MMIO_ERINTSTS); + if (MmcHostData->EnableVerboseDebug && (Data16 !=3D 0)) { + DEBUG ((EFI_D_INFO, "SendCommand: ERINTSTS (0x32): 0x%x\n", Data16)); + DEBUG ((EFI_D_INFO, "SendCommand: ERINTEN (0x36): 0x%x\n", MmcHostRe= ad16 (MmcHostData, MMIO_ERINTEN))); + DEBUG ((EFI_D_INFO, "SendCommand: NINTSTS (0x30): 0x%x\n", MmcHostRe= ad16 (MmcHostData, MMIO_NINTSTS))); + DEBUG ((EFI_D_INFO, "SendCommand: NINTEN (0x34): 0x%x\n", MmcHostRea= d16 (MmcHostData, MMIO_NINTEN))); + } + + if ((Data16 & 0x17FF) !=3D 0) { + Status =3D GetErrorReason (CommandIndex, Data16); + DEBUG ((EFI_D_INFO, "00 -10: %08x %08x %08x %08x \n", MmcHostRea= d32 (MmcHostData, 0x00), MmcHostRead32 (MmcHostData, 0x04), MmcHostRead32 (= MmcHostData, 0x08), MmcHostRead32 (MmcHostData, 0x0C))); + DEBUG ((EFI_D_INFO, "10 -20: %08x %08x %08x %08x \n", MmcHostRea= d32 (MmcHostData, 0x10), MmcHostRead32 (MmcHostData, 0x14), MmcHostRead32 (= MmcHostData, 0x18), MmcHostRead32 (MmcHostData, 0x1C))); + DEBUG ((EFI_D_INFO, "20 -30: %08x %08x %08x %08x \n", MmcHostRea= d32 (MmcHostData, 0x20), MmcHostRead32 (MmcHostData, 0x24), MmcHostRead32 (= MmcHostData, 0x28), MmcHostRead32 (MmcHostData, 0x2C))); + DEBUG ((EFI_D_INFO, "30 -40: %08x %08x %08x %08x \n", MmcHostRea= d32 (MmcHostData, 0x30), MmcHostRead32 (MmcHostData, 0x34), MmcHostRead32 (= MmcHostData, 0x38), MmcHostRead32 (MmcHostData, 0x3C))); + DEBUG ((EFI_D_INFO, "40 -50: %08x %08x %08x %08x \n", MmcHostRea= d32 (MmcHostData, 0x40), MmcHostRead32 (MmcHostData, 0x44), MmcHostRead32 (= MmcHostData, 0x48), MmcHostRead32 (MmcHostData, 0x4C))); + DEBUG ((EFI_D_INFO, "50 -60: %08x %08x %08x %08x \n", MmcHostRea= d32 (MmcHostData, 0x50), MmcHostRead32 (MmcHostData, 0x54), MmcHostRead32 (= MmcHostData, 0x58), MmcHostRead32 (MmcHostData, 0x5C))); + goto Exit; + } + + Data16 =3D MmcHostRead16 (MmcHostData, MMIO_NINTSTS) & 0x1ff; + if (MmcHostData->EnableVerboseDebug && (Data16 > 1)) { + DEBUG ((EFI_D_INFO, "SendCommand: NINTSTS (0x30): 0x%x\n", Data16)); + } + + if ((Data16 & BIT0) =3D=3D BIT0) { + // + // Command completed + // + CommandCompleted =3D TRUE; + + MmcHostWrite16 (MmcHostData, MMIO_NINTSTS, BIT0); + + if ((DataType =3D=3D NoData) && (ResponseType !=3D ResponseR1b)) { + break; + } + } + + if (CommandCompleted) { + // + // DMA interrupted + // + if ((Data16 & BIT3) =3D=3D BIT3) { + MmcHostWrite16 (MmcHostData, MMIO_NINTSTS, BIT3); + Data32 =3D MmcHostRead32 (MmcHostData, MMIO_DMAADR); + MMC_HOST_DEBUG ((DEBUG_INFO, "SendCommand: DMAADR (0x00) <- 0x%x\n= ", Data32)); + MmcHostWrite32 (MmcHostData, MMIO_DMAADR, Data32); + } + + // + // Transfer completed + // + if ((Data16 & BIT1) =3D=3D BIT1) { + MmcHostWrite16 (MmcHostData, MMIO_NINTSTS, BIT1); + break; + } + } + + gBS->Stall (1); + TimeOut --; + + } while (TimeOut > 0); + + if (TimeOut =3D=3D 0) { + Status =3D EFI_TIMEOUT; + DEBUG ((gMmcHostDebugLevel, "SendCommand: Time out \n")); + goto Exit; + } + + if (ResponseData !=3D NULL) { + UINT32 *ResDataPtr =3D NULL; + + ResDataPtr =3D ResponseData; + for (Index =3D 0; Index < ResponseDataCount; Index++) { + *ResDataPtr =3D MmcHostRead32 (MmcHostData, MMIO_RESP + Index * 4); + ResDataPtr++; + } + MMC_HOST_DEBUG ((EFI_D_INFO, "Reponse Data 0: RESPONSE (0x10) <- 0x%x\= n", *ResponseData)); + + if (ResponseType =3D=3D ResponseR2) { + // + // Adjustment for R2 response + // + Data32 =3D 1; + for (Index =3D 0; Index < ResponseDataCount; Index++) { + Data64 =3D LShiftU64 (*ResponseData, 8); + *ResponseData =3D (UINT32) ((Data64 & 0xFFFFFFFF) | Data32); + Data32 =3D (UINT32) RShiftU64 (Data64, 32); + ResponseData++; + } + } + } + + if (MmcHostData->EnableVerboseDebug) { + DEBUG ((EFI_D_INFO, "Before Exit Send Command\n")); + DEBUG ((EFI_D_INFO, "00 -10: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x00), MmcHostRead32 (MmcHostData, 0x04), MmcHostRead32 (Mm= cHostData, 0x08), MmcHostRead32 (MmcHostData, 0x0C))); + DEBUG ((EFI_D_INFO, "10 -20: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x10), MmcHostRead32 (MmcHostData, 0x14), MmcHostRead32 (Mm= cHostData, 0x18), MmcHostRead32 (MmcHostData, 0x1C))); + DEBUG ((EFI_D_INFO, "20 -30: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x20), MmcHostRead32 (MmcHostData, 0x24), MmcHostRead32 (Mm= cHostData, 0x28), MmcHostRead32 (MmcHostData, 0x2C))); + DEBUG ((EFI_D_INFO, "30 -40: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x30), MmcHostRead32 (MmcHostData, 0x34), MmcHostRead32 (Mm= cHostData, 0x38), MmcHostRead32 (MmcHostData, 0x3C))); + DEBUG ((EFI_D_INFO, "40 -50: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x40), MmcHostRead32 (MmcHostData, 0x44), MmcHostRead32 (Mm= cHostData, 0x48), MmcHostRead32 (MmcHostData, 0x4C))); + DEBUG ((EFI_D_INFO, "50 -60: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x50), MmcHostRead32 (MmcHostData, 0x54), MmcHostRead32 (Mm= cHostData, 0x58), MmcHostRead32 (MmcHostData, 0x5C))); + } + +Exit: + HostLEDEnable (This, FALSE); + MMC_HOST_DEBUG ((EFI_D_INFO, "SendCommand: Status -> %r\n", Status)); + + return Status; +} + + +/** + Set clock frequency of the host, the actual frequency + may not be the same as MaxFrequencyInKHz. It depends on + the max frequency the host can support, divider, and host + speed mode. + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] MaxFrequency Max frequency in HZ + + @retval EFI_SUCCESS + +**/ +EFI_STATUS +EFIAPI +SetClockFrequency ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN UINT32 MaxFrequency + ) +{ + UINT16 Data16; + UINT32 Frequency; + UINT32 Divider =3D 0; + MMCHOST_DATA *MmcHostData; + UINT32 TimeOutCount; + + MmcHostData =3D MMCHOST_DATA_FROM_THIS (This); + + DEBUG ((gMmcHostDebugLevel, "SetClockFrequency: BaseClockInMHz =3D %d \n= ", MmcHostData->BaseClockInMHz)); + + Frequency =3D (MmcHostData->BaseClockInMHz * 1000 * 1000) / MaxFrequency; + DEBUG ((gMmcHostDebugLevel, "SetClockFrequency: FrequencyInHz =3D %d \n"= , Frequency)); + + if ((MmcHostData->BaseClockInMHz * 1000 * 1000 % MaxFrequency) !=3D 0) { + Frequency +=3D 1; + } + + Divider =3D 1; + while (Frequency > Divider) { + Divider =3D Divider * 2; + } + + if (Divider >=3D 0x400) { + Divider =3D 0x200; + } + DEBUG ((gMmcHostDebugLevel, "SetClockFrequency: before shift: Base Clock= Divider =3D 0x%x \n", Divider)); + Divider =3D Divider >> 1; + DEBUG ((gMmcHostDebugLevel, "SetClockFrequency: before shift: Base Clock= Divider =3D 0x%x \n", Divider)); + + MmcHostData->CurrentClockInKHz =3D (MmcHostData->BaseClockInMHz * 1000); + if (Divider !=3D 0) { + MmcHostData->CurrentClockInKHz =3D MmcHostData->CurrentClockInKHz / (D= ivider * 2); + } + + if (2 =3D=3D CheckControllerVersion (MmcHostData)) { + Data16 =3D (UINT16) ((Divider & 0xFF) << 8 | (((Divider & 0xFF00) >>8)= <<6)); + } else { + Data16 =3D (UINT16) ( Divider << 8); + } + + DEBUG ((gMmcHostDebugLevel, + "SetClockFrequency: base=3D%dMHz, clkctl=3D0x%04x, f=3D%dKHz\n", + MmcHostData->BaseClockInMHz, + Data16, + MmcHostData->CurrentClockInKHz + )); + DEBUG ((gMmcHostDebugLevel, "SetClockFrequency: set MMIO_CLKCTL value = =3D 0x%x \n", Data16)); + MmcHostWrite16 (MmcHostData, MMIO_CLKCTL, Data16); + + gBS->Stall (10); + Data16 |=3D BIT0; + MmcHostWrite16 (MmcHostData, MMIO_CLKCTL, Data16); + + TimeOutCount =3D TIME_OUT_1S; + do { + Data16 =3D MmcHostRead16 (MmcHostData, MMIO_CLKCTL); + gBS->Stall (10); + TimeOutCount --; + if (TimeOutCount =3D=3D 0) { + DEBUG ((gMmcHostDebugLevel, "SetClockFrequency: Timeout\n")); + return EFI_TIMEOUT; + } + } while ((Data16 & BIT1) !=3D BIT1); + + gBS->Stall (10); + Data16 |=3D BIT2; + MmcHostWrite16 (MmcHostData, MMIO_CLKCTL, Data16); + + return EFI_SUCCESS; +} + + +/** + Set bus width of the host + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] BusWidth Bus width in 1, 4, 8 bits + + @retval EFI_SUCCESS + +**/ +EFI_STATUS +EFIAPI +SetBusWidth ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN UINT32 BusWidth + ) +{ + MMCHOST_DATA *MmcHostData; + UINT8 Data; + + MmcHostData =3D MMCHOST_DATA_FROM_THIS (This); + + if ((BusWidth !=3D 1) && (BusWidth !=3D 4) && (BusWidth !=3D 8)) { + DEBUG ((gMmcHostDebugLevel, "SetBusWidth: Invalid parameter \n")); + return EFI_INVALID_PARAMETER; + } + + if ((MmcHostData->MmcHostIo.HostCapability.BusWidth8 =3D=3D FALSE) && (B= usWidth =3D=3D 8)) { + DEBUG ((gMmcHostDebugLevel, "SetBusWidth: Invalid parameter \r\n")); + return EFI_INVALID_PARAMETER; + } + Data =3D MmcHostRead8 (MmcHostData, MMIO_HOSTCTL); + + if (BusWidth =3D=3D 8) { + DEBUG ((gMmcHostDebugLevel, "SetBusWidth: Bus Width is 8-bit ... \r\n"= )); + Data |=3D BIT5; + } else if (BusWidth =3D=3D 4) { + DEBUG ((gMmcHostDebugLevel, "SetBusWidth: Bus Width is 4-bit ... \r\n"= )); + Data &=3D ~BIT5; + Data |=3D BIT1; + } else { + DEBUG ((gMmcHostDebugLevel, "SetBusWidth: Bus Width is 1-bit ... \r\n"= )); + Data &=3D ~BIT5; + Data &=3D ~BIT1; + } + MmcHostWrite8 (MmcHostData, MMIO_HOSTCTL, Data); + DEBUG ((gMmcHostDebugLevel, "SetBusWidth: MMIO_HOSTCTL value: 0x%x \n",= MmcHostRead8 (MmcHostData, MMIO_HOSTCTL))); + + return EFI_SUCCESS; +} + + +/** + Set voltage which could supported by the host. + Support 0(Power off the host), 1.8V, 3.0V, 3.3V + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOC= OL + @param[in] Voltage Units in 0.1 V + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +EFIAPI +SetHostVoltage ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN UINT32 Voltage + ) +{ + MMCHOST_DATA *MmcHostData; + UINT8 Data; + EFI_STATUS Status; + + MmcHostData =3D MMCHOST_DATA_FROM_THIS (This); + Status =3D EFI_SUCCESS; + + Data =3D MmcHostRead8 (MmcHostData, MMIO_PWRCTL); + + if (Voltage =3D=3D 0) { + // + //Power Off the host + // + Data &=3D ~BIT0; + } else if (Voltage <=3D 18 && This->HostCapability.V18Support) { + // + //1.8V + // + Data |=3D (BIT1 | BIT3 | BIT0); + } else if (Voltage > 18 && Voltage <=3D 30 && This->HostCapability.V30S= upport) { + // + //3.0V + // + Data |=3D (BIT2 | BIT3 | BIT0); + } else if (Voltage > 30 && Voltage <=3D 33 && This->HostCapability.V33Su= pport) { + // + //3.3V + // + Data |=3D (BIT1 | BIT2 | BIT3 | BIT0); + } else { + Status =3D EFI_UNSUPPORTED; + goto Exit; + } + + MmcHostWrite8 (MmcHostData, MMIO_PWRCTL, Data); + gBS->Stall (10); + +Exit: + return Status; +} + + +/** + Set Host High Speed + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] HighSpeed True for High Speed Mode set, fals= e for normal mode + + @retval EFI_INVALID_PARAMETER + @retval EFI_SUCCESS + +**/ +EFI_STATUS +EFIAPI +SetHostSpeedMode( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN UINT32 HighSpeed + ) +{ + EFI_STATUS Status; + MMCHOST_DATA *MmcHostData; + UINT8 Data8; + + Status =3D EFI_SUCCESS; + MmcHostData =3D MMCHOST_DATA_FROM_THIS (This); + + if (MmcHostData->IsEmmc) { + Data8 =3D MmcHostRead8 (MmcHostData, MMIO_HOSTCTL); + Data8 &=3D ~(BIT2); + if (HighSpeed) { + Data8 |=3D BIT2; + DEBUG ((gMmcHostDebugLevel, "High Speed mode: Data8=3D0x%x \n", Data= 8)); + } else { + DEBUG ((gMmcHostDebugLevel, "Normal Speed mode: Data8=3D0x%x \n", Da= ta8)); + } + MmcHostWrite8 (MmcHostData, MMIO_HOSTCTL, Data8); + gBS->Stall (10); + DEBUG ((gMmcHostDebugLevel, "MMIO_HOSTCTL value: 0x%x \n", MmcHostRea= d8 (MmcHostData, MMIO_HOSTCTL))); + return EFI_SUCCESS; + } else { + return EFI_INVALID_PARAMETER; + } +} + + +/** + Set Host mode in DDR + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTO= COL + @param[in] SetHostDdrMode True for DDR Mode set, false ret= urns EFI_SUCCESS + + @retval EFI_INVALID_PARAMETER + @retval EFI_SUCCESS + +**/ +EFI_STATUS +EFIAPI +SetHostDdrMode( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN UINT32 DdrMode + ) +{ + EFI_STATUS Status; + MMCHOST_DATA *MmcHostData; + UINT16 ModeSet; + + Status =3D EFI_SUCCESS; + MmcHostData =3D MMCHOST_DATA_FROM_THIS (This); + + if (MmcHostData->IsEmmc) { + ModeSet =3D MmcHostRead16 (MmcHostData, MMIO_HOST_CTL2); + ModeSet &=3D ~(BIT0 | BIT1 | BIT2); + if (DdrMode) { + ModeSet |=3D 0x0004; + ModeSet |=3D BIT3; //1.8v + DEBUG ((gMmcHostDebugLevel, "DDR mode: Data16=3D0x%x \n", ModeSet)); + } else { + if (CheckControllerVersion (MmcHostData) !=3D 2) { + ModeSet =3D 0x0; + } + } + + MmcHostWrite16 (MmcHostData, MMIO_HOST_CTL2, ModeSet); + gBS->Stall (10); + DEBUG ((gMmcHostDebugLevel, "MMIO_HOST_CTL2 value: 0x%x \n", MmcHostR= ead16 (MmcHostData, MMIO_HOST_CTL2))); + return EFI_SUCCESS; + } else { + return EFI_INVALID_PARAMETER; + } +} + + +/** + Set Host SDR Mode + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] DdrMode True for SDR Mode set, false for norm= al mode + + @retval EFI_SUCCESS The function completed successfully + @retval EFI_INVALID_PARAMETER A parameter was incorrect. + +**/ +EFI_STATUS +EFIAPI +SetHostSdrMode( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN UINT32 SdrMode + ) +{ + MMCHOST_DATA *MmcHostData; + UINT16 ModeSet; + + MmcHostData =3D MMCHOST_DATA_FROM_THIS (This); + + if (MmcHostData->IsEmmc) { + ModeSet =3D MmcHostRead16 (MmcHostData, MMIO_HOST_CTL2); + ModeSet &=3D ~(BIT0 | BIT1 | BIT2); + if (SdrMode) { + if (MmcHostData->MmcHostIo.HostCapability.SDR104Support) { + ModeSet |=3D 3; + } else if (MmcHostData->MmcHostIo.HostCapability.SDR50Support) { + ModeSet |=3D 2; + } + ModeSet |=3D 1; + DEBUG ((gMmcHostDebugLevel, "SDR mode: Data16=3D0x%x \n", ModeSet)); + } else { + if (CheckControllerVersion (MmcHostData) !=3D 2) { + ModeSet =3D 0x0; + } + } + + MmcHostWrite16 (MmcHostData, MMIO_HOST_CTL2, ModeSet); + gBS->Stall (10); + DEBUG ((gMmcHostDebugLevel, "MMIO_HOST_CTL2 value: 0x%x \n", MmcHostR= ead16 (MmcHostData, MMIO_HOST_CTL2))); + return EFI_SUCCESS; + } else { + return EFI_INVALID_PARAMETER; + } +} + + +/** + Reset the host + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] ResetType Reset data/cmd all + + @retval EFI_SUCCESS The function completed successfully + @retval EFI_TIMEOUT The timeout time expired. + +**/ +EFI_STATUS +EFIAPI +ResetMmcHost ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN RESET_TYPE ResetType + ) +{ + MMCHOST_DATA *MmcHostData; + UINT8 Data8; + UINT32 Data; + UINT16 ErrStatus; + UINT8 Mask; + UINT32 TimeOutCount; + UINT16 SaveClkCtl; + UINT16 SavePwrCtl =3D 0; + + MmcHostData =3D MMCHOST_DATA_FROM_THIS (This); + MmcHostData->IsEmmc =3D TRUE; + Mask =3D 0; + ErrStatus =3D 0; + + if (ResetType =3D=3D Reset_Auto) { + ErrStatus =3D MmcHostRead16 (MmcHostData, MMIO_ERINTSTS); + if ((ErrStatus & 0xF) !=3D 0) { + // + //Command Line + // + Mask |=3D BIT1; + } + if ((ErrStatus & 0x70) !=3D 0) { + // + //Data Line + // + Mask |=3D BIT2; + } + } + + if (ResetType =3D=3D Reset_DAT || ResetType =3D=3D Reset_DAT_CMD) { + Mask |=3D BIT2; + } + if (ResetType =3D=3D Reset_CMD || ResetType =3D=3D Reset_DAT_CMD) { + Mask |=3D BIT1; + } + if (ResetType =3D=3D Reset_All) { + Mask =3D BIT0; + } + if (ResetType =3D=3D Reset_HW) { + SavePwrCtl =3D MmcHostRead16 (MmcHostData, MMIO_PWRCTL); + DEBUG ((gMmcHostDebugLevel, "Write SavePwrCtl: %x \n", (SavePwrCtl | B= IT4))); + + MmcHostWrite16 (MmcHostData, MMIO_PWRCTL, SavePwrCtl | BIT4); + gBS->Stall (10); + DEBUG ((gMmcHostDebugLevel, "Write SavePwrCtl: %x \n", (SavePwrCtl &(~= BIT4)))); + MmcHostWrite16 (MmcHostData, MMIO_PWRCTL, SavePwrCtl &(~BIT4)); + gBS->Stall (10); + } + + if (Mask =3D=3D 0) { + return EFI_SUCCESS; + } + + SaveClkCtl =3D MmcHostRead16 (MmcHostData, MMIO_CLKCTL); + + MmcHostWrite16 (MmcHostData, MMIO_CLKCTL, 0); + + gBS->Stall (10); + + // + // Reset the MMC host controller + // + MmcHostWrite8 (MmcHostData, MMIO_SWRST, Mask); + + Data =3D 0; + TimeOutCount =3D TIME_OUT_1S; + do { + gBS->Stall (10); + TimeOutCount --; + + Data8 =3D MmcHostRead8 (MmcHostData, MMIO_SWRST); + if ((Data8 & Mask) =3D=3D 0) { + break; + } + } while (TimeOutCount > 0); + + // + // We now restore the MMIO_CLKCTL register which we set to 0 above. + // + MmcHostWrite16 (MmcHostData, MMIO_CLKCTL, SaveClkCtl); + + if (TimeOutCount =3D=3D 0) { + DEBUG ((gMmcHostDebugLevel, "ResetMMCHost: Time out \n")); + return EFI_TIMEOUT; + } + + return EFI_SUCCESS; +} + + +/** + Enable AutoStop Cmd + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] Enable TRUE to enable + + @retval EFI_SUCCESS The function completed successfully + +**/ +EFI_STATUS +EFIAPI +EnableAutoStopCmd ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN BOOLEAN Enable + ) +{ + MMCHOST_DATA *MmcHostData; + + MmcHostData =3D MMCHOST_DATA_FROM_THIS (This); + + MmcHostData->IsAutoStopCmd =3D Enable; + + return EFI_SUCCESS; +} + + +/** + Set the Block length + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] BlockLength card supportes block length + + @retval EFI_SUCCESS + +**/ +EFI_STATUS +EFIAPI +SetBlockLength ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN UINT32 BlockLength + ) +{ + MMCHOST_DATA *MmcHostData; + + MmcHostData =3D MMCHOST_DATA_FROM_THIS (This); + + DEBUG ((gMmcHostDebugLevel, "SetBlockLength: Block length on the host co= ntroller: %d \n", BlockLength)); + MmcHostData->BlockLength =3D BlockLength; + + return EFI_SUCCESS; +} + + +/** + Detect card and init Sd host + Find whether these is a card inserted into the slot. If so + init the host. If not, return EFI_NOT_FOUND. + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +EFIAPI +DetectCardAndInitHost ( + IN EFI_MMC_HOST_IO_PROTOCOL *This + ) +{ + MMCHOST_DATA *MmcHostData; + UINT16 Data16; + UINT32 Data; + EFI_STATUS Status; + UINT8 Voltages[] =3D { 33, 30, 18 }; + UINTN Loop; + + MmcHostData =3D MMCHOST_DATA_FROM_THIS (This); + + Data =3D 0; + Data =3D MmcHostRead32 (MmcHostData, MMIO_PSTATE); + + if ((Data & (BIT16 | BIT17 | BIT18)) !=3D (BIT16 | BIT17 | BIT18)) { + // + // Has no card inserted + // + DEBUG ((gMmcHostDebugLevel, "DetectCardAndInitHost: No card\n")); + Status =3D EFI_NOT_FOUND; + goto Exit; + } + DEBUG ((gMmcHostDebugLevel, "DetectCardAndInitHost: Card present\n")); + + // + //Enable normal status change + // + MmcHostWrite16 (MmcHostData, MMIO_NINTEN, BIT1 | BIT0); + + // + // Enable error status change + // + Data16 =3D MmcHostRead16 (MmcHostData, MMIO_ERINTEN); + Data16 |=3D 0xFFFF; //(BIT0 | BIT1 | BIT2 | BIT3 | BIT4 | BIT5 | BIT6 | = BIT7 | BIT8); + MmcHostWrite16 (MmcHostData, MMIO_ERINTEN, Data16); + + // + // Data transfer Timeout control + // + MmcHostWrite8 (MmcHostData, MMIO_TOCTL, 0x0E); + + // + // Stall 1 milliseconds to increase MMC stability. + // + gBS->Stall (10); + + Status =3D SetClockFrequency (This, FREQUENCY_OD); + if (EFI_ERROR (Status)) { + DEBUG ((gMmcHostDebugLevel, "DetectCardAndInitHost: SetClockFrequency = failed\n")); + goto Exit; + } + DEBUG ((gMmcHostDebugLevel, "DetectCardAndInitHost: SetClockFrequency do= ne\n")); + + Status =3D EFI_NOT_FOUND; + for (Loop =3D 0; Loop < sizeof (Voltages); Loop++) { + DEBUG (( + EFI_D_INFO, + "DetectCardAndInitHost: SetHostVoltage %d.%dV\n", + Voltages[Loop] / 10, + Voltages[Loop] % 10 + )); + Status =3D SetHostVoltage (This, Voltages[Loop]); + if (EFI_ERROR (Status)) { + DEBUG ((gMmcHostDebugLevel, "DetectCardAndInitHost: SetHostVoltage f= ailed\n")); + } else { + DEBUG ((gMmcHostDebugLevel, "DetectCardAndInitHost: SetHostVoltage d= one\n")); + break; + } + } + if (EFI_ERROR (Status)) { + DEBUG ((gMmcHostDebugLevel, "DetectCardAndInitHost: Failed to SetHostV= oltage\n")); + goto Exit; + } + +Exit: + return Status; +} + + +/** + Setup the MMC Host Device + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + + @retval EFI_SUCCESS + +**/ +EFI_STATUS +EFIAPI +SetupDevice ( + IN EFI_MMC_HOST_IO_PROTOCOL *This + ) +{ + UINT32 tempD =3D 0; + INT32 timeOut =3D 1000; + UINT8 temp8 =3D 0; + UINT16 temp16 =3D 0; + MMCHOST_DATA *MmcHostData; + + MmcHostData =3D MMCHOST_DATA_FROM_THIS (This); + + // + // Reset HC and wait for self-clear + // + MmcHostWrite8 (MmcHostData, MMIO_SWRST, 0x7); + gBS->Stall (1000); + timeOut =3D 1000; + do { + temp8 =3D MmcHostRead8 (MmcHostData, MMIO_SWRST); + timeOut--; + } while ((temp8 & (1 << 0)) && (timeOut > 0)); + + DEBUG ((EFI_D_INFO, "Reset HC and wait for self-clear Done\n")); + + // + // Enable all interrupt status bits (NO CARD_INTERRUPT!) + // + MmcHostWrite16 (MmcHostData, MMIO_NINTEN, 0x3); + + // + // Clear all interrupt status bits + // + MmcHostWrite16 (MmcHostData, MMIO_NINTSTS, 0xFFFF); + MmcHostWrite16 (MmcHostData, MMIO_ERINTEN, 0xFFFF); + + if (2 =3D=3D CheckControllerVersion (MmcHostData)) + temp16 =3D (UINT16) (0x1 << 6); + else + temp16 =3D (UINT16) (0x80 << 8); + // + // Set to 400KB, enable internal clock and wait for stability + // + MmcHostWrite32 (MmcHostData, MMIO_CLKCTL, (1<<0) | temp16); + + gBS->Stall (1000); + do { + tempD =3D MmcHostRead32 (MmcHostData, MMIO_CLKCTL); + timeOut--; + } while ((!(tempD & (1 << 1))) && (timeOut > 0)); + gBS->Stall (1000); + + // + // Enable MMC clock + // + tempD |=3D (1 << 2); + MmcHostWrite32 (MmcHostData, MMIO_CLKCTL, tempD); + gBS->Stall (1000); + + temp8 =3D MmcHostRead8 (MmcHostData, MMIO_PWRCTL); + DEBUG ((DEBUG_INFO, "=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D%a, %d, offset=3D0x%x= , PWRCTL =3D 0x%x=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n", __FUN= CTION__, __LINE__, MMIO_PWRCTL, temp8)); + + // + // Apply 1.8V to the bus + // + temp8 =3D (0x5 << (1) ); + MmcHostWrite8 (MmcHostData, MMIO_PWRCTL, temp8); + gBS->Stall (1000); + temp8 =3D MmcHostRead8 (MmcHostData, MMIO_PWRCTL); + DEBUG ((DEBUG_INFO, "=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D%a, %d, set (0x5 << 1= ):offset=3D0x%x, PWRCTL =3D 0x%x=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D\n", __FUNCTION__, __LINE__, MMIO_PWRCTL, temp8)); + + // + // Set 1.8V sigaling Enabled + // + temp16 =3D MmcHostRead16 (MmcHostData, MMIO_HOST_CTL2); + temp16 &=3D ~BIT3; + MmcHostWrite16 (MmcHostData, MMIO_HOST_CTL2, temp16); + DEBUG ((gMmcHostDebugLevel, "Set 1.8 V signaling Enable:0x%x \r\n", MmcH= ostRead16 (MmcHostData, MMIO_HOST_CTL2))); + + // + // Apply power to MMC + // + temp8 =3D MmcHostRead8 (MmcHostData, MMIO_PWRCTL); + DEBUG ((DEBUG_INFO, "=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D%a, %d, read offset= =3D0x%x, PWRCTL =3D 0x%x=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n"= , __FUNCTION__, __LINE__, MMIO_PWRCTL, temp8)); + + temp8 |=3D (1 << (0)); + DEBUG ((DEBUG_INFO, "=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D%a, %d, set 1<<0:offs= et=3D0x%x, PWRCTL =3D 0x%x=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\= n", __FUNCTION__, __LINE__, MMIO_PWRCTL, temp8)); + + MmcHostWrite8 (MmcHostData, MMIO_PWRCTL, temp8); + gBS->Stall (1000); ///Stall (1000); + + if (MmcHostData->EnableVerboseDebug) { + DEBUG ((EFI_D_INFO, "=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D%a, Start. RegMap= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n", __FUNCTION__)); + DEBUG ((DEBUG_INFO, "00 -10: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x00), MmcHostRead32 (MmcHostData, 0x04), MmcHostRead32 (Mm= cHostData, 0x08), MmcHostRead32 (MmcHostData, 0x0C))); + DEBUG ((DEBUG_INFO, "10 -20: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x10), MmcHostRead32 (MmcHostData, 0x14), MmcHostRead32 (Mm= cHostData, 0x18), MmcHostRead32 (MmcHostData, 0x1C))); + DEBUG ((DEBUG_INFO, "20 -30: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x20), MmcHostRead32 (MmcHostData, 0x24), MmcHostRead32 (Mm= cHostData, 0x28), MmcHostRead32 (MmcHostData, 0x2C))); + DEBUG ((DEBUG_INFO, "30 -40: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x30), MmcHostRead32 (MmcHostData, 0x34), MmcHostRead32 (Mm= cHostData, 0x38), MmcHostRead32 (MmcHostData, 0x3C))); + DEBUG ((DEBUG_INFO, "40 -50: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x40), MmcHostRead32 (MmcHostData, 0x44), MmcHostRead32 (Mm= cHostData, 0x48), MmcHostRead32 (MmcHostData, 0x4C))); + DEBUG ((DEBUG_INFO, "50 -60: %08x %08x %08x %08x \n", MmcHostRead3= 2 (MmcHostData, 0x50), MmcHostRead32 (MmcHostData, 0x54), MmcHostRead32 (Mm= cHostData, 0x58), MmcHostRead32 (MmcHostData, 0x5C))); + DEBUG ((DEBUG_INFO, "=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D%a, END. RegMap=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n", __FUNCTION__)); + } + return EFI_SUCCESS; +} + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcH= ostDxe/MmcHostDriver.c b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/= Dxe/MMC/MmcHostDxe/MmcHostDriver.c new file mode 100644 index 0000000..f94dc1d --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcHostDxe/= MmcHostDriver.c @@ -0,0 +1,613 @@ +/** @file + UEFI Driver Entry and Binding support. + + Copyright (c) 1999 - 2016, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include "MmcHostDriver.h" +#include + +// +// MMCSDIOController Driver Global Variables +// +EFI_DRIVER_BINDING_PROTOCOL gMmcHostDriverBinding =3D { + MmcHostDriverBindingSupported, + MmcHostDriverBindingStart, + MmcHostDriverBindingStop, + 0x10, + NULL, + NULL +}; + + +EFI_MMC_HOST_IO_PROTOCOL mMmcHostIo =3D { + EFI_MMC_HOST_IO_PROTOCOL_REVISION_01, + { + 0, ///< HighSpeedSupport + 0, ///< V18Support + 0, ///< V30Support + 0, ///< V33Support + 0, ///< HS400Support + 0, ///< BusWidth4 + 0, ///< BusWidth8 + 0, ///< Reserved1 + 0, + 0, + 0, + 0, + 0, ///< ADMA2Support + 0, ///< DmaMode + 0, ///< ReTune Timer + 0, ///< ReTune Mode + 0, ///< Reserved2 + (512 * 1024) ///OpenProtocol ( + Controller, + &gEfiMmcHostIoProtocolGuid, + (VOID **) &MmcHostIo, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + DEBUG ((DEBUG_INFO, "MmcHost controller already started, Controller: 0= x%016Lx\r\n", (UINT64) (UINTN) Controller)); + Status =3D EFI_ALREADY_STARTED; + return Status; + } + + // + // Test whether there is PCI IO Protocol attached on the controller hand= le. + // + Status =3D gBS->OpenProtocol ( + Controller, + &gEfiPciIoProtocolGuid, + (VOID**) &PciIo, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status =3D PciIo->Pci.Read ( + PciIo, + EfiPciIoWidthUint32, + PCI_VENDOR_ID_OFFSET, + 1, + &VidDid + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + Status =3D PciIo->Pci.Read ( + PciIo, + EfiPciIoWidthUint8, + PCI_CLASSCODE_OFFSET, + sizeof (PCI_CLASSC) / sizeof (UINT8), + &PciClass + ); + if (EFI_ERROR (Status)) { + Status =3D EFI_UNSUPPORTED; + goto Exit; + } + + Status =3D PciIo->Pci.Read ( + PciIo, + EfiPciIoWidthUint32, + PCI_BASE_ADDRESSREG_OFFSET, + 1, + &Bar0 + ); + if (EFI_ERROR (Status)) { + Status =3D EFI_UNSUPPORTED; + goto Exit; + } + + Status =3D PciIo->Pci.Read ( + PciIo, + EfiPciIoWidthUint32, + PCI_BASE_ADDRESSREG_OFFSET + 4, + 1, + &Bar1 + ); + if (EFI_ERROR (Status)) { + Status =3D EFI_UNSUPPORTED; + goto Exit; + } + + Status =3D PciIo->GetLocation ( + PciIo, + &Seg, + &Bus, + &Dev, + &Func + ); + + if (EFI_ERROR (Status)) { + Status =3D EFI_UNSUPPORTED; + goto Exit; + } + + if ((Seg !=3D 0) || (Bus !=3D 0) || (Dev !=3D 28) || (Func !=3D 0)) { + // + // This is not the eMMC controller, bail. + // + Status =3D EFI_UNSUPPORTED; + goto Exit; + } + + if ((PciClass.BaseCode !=3D PCI_CLASS_SYSTEM_PERIPHERAL) || + (PciClass.SubClassCode !=3D PCI_SUBCLASS_SD_HOST_CONTROLLER) || + ((PciClass.PI !=3D PCI_IF_STANDARD_HOST_NO_DMA) && (PciClass.PI !=3D = PCI_IF_STANDARD_HOST_SUPPORT_DMA)) + ) { + Status =3D EFI_UNSUPPORTED; + goto Exit; + } + + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingSupported0: Seg %d, bus:%d, Dev= :%d, Func:%d\n", Seg, Bus, Dev, Func)); + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingSupported1: VidDid %08x\n", Vid= Did)); + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingSupported1: Base Code %x\n", Pc= iClass.BaseCode)); + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingSupported1: SubClassCode %x\n",= PciClass.SubClassCode)); + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingSupported1: PI %x\n", PciClass.= PI)); + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingSupported0: MEMIO Base0 %x\n", = Bar0)); + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingSupported0: MEMIO Base1 %x\n", = Bar1)); + +Exit: + gBS->CloseProtocol ( + Controller, + &gEfiPciIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + return Status; +} + + +/** + Starting the MMC Host Driver + + @param[in] This Protocol instance pointer + @param[in] Controller Handle of device to start + @param[in] RemainingDevicePath Not used + + @retval EFI_SUCCESS This driver start this device + @retval EFI_UNSUPPORTED This driver does not support this de= vice + @retval EFI_DEVICE_ERROR This driver cannot be started due to= device Error + @retval EFI_OUT_OF_RESOURCES This driver cannot allocate resources + +**/ +EFI_STATUS +EFIAPI +MmcHostDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) +{ + EFI_PCI_IO_PROTOCOL *PciIo; + EFI_STATUS Status; + MMCHOST_DATA *MmcHostData; + UINT32 Data; + UINT16 Data16; + UINT32 VidDid; + UINT32 Bar0 =3D 0; + UINT32 Bar1 =3D 0; + UINTN Seg, Bus, Dev, Func; + + MmcHostData =3D NULL; + Data =3D 0; + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: Start\n")); + + // + // Open PCI I/O Protocol and save pointer to open protocol + // in private data area. + // + Status =3D gBS->OpenProtocol ( + Controller, + &gEfiPciIoProtocolGuid, + (VOID **) &PciIo, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Enable the MMC Host Controller MMIO space + // + Status =3D PciIo->Attributes ( + PciIo, + EfiPciIoAttributeOperationEnable, + EFI_PCI_DEVICE_ENABLE, + NULL + ); + if (EFI_ERROR (Status)) { + Status =3D EFI_OUT_OF_RESOURCES; + goto Exit; + } + + + MmcHostData =3D (MMCHOST_DATA *) AllocateZeroPool (sizeof (MMCHOST_DATA)= ); + if (MmcHostData =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + goto Exit; + } + + MmcHostData->Signature =3D MMCHOST_DATA_SIGNATURE; + MmcHostData->PciIo =3D PciIo; + + CopyMem (&MmcHostData->MmcHostIo, &mMmcHostIo, sizeof (EFI_MMC_HOST_IO_P= ROTOCOL)); + + Status =3D PciIo->Pci.Read ( + PciIo, + EfiPciIoWidthUint32, + PCI_VENDOR_ID_OFFSET, + 1, + &VidDid + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: VidDid =3D 0x%08x\n", Vi= dDid)); + + Status =3D PciIo->GetLocation ( + PciIo, + &Seg, + &Bus, + &Dev, + &Func + ); + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: Seg %d, bus:%d,Dev:%d,Fu= nc:%d\n", Seg, Bus, Dev,Func)); + + MmcHostData->PciVid =3D (UINT16) (VidDid & 0xffff); + MmcHostData->PciDid =3D (UINT16) (VidDid >> 16); + + Status =3D PciIo->Pci.Read ( + PciIo, + EfiPciIoWidthUint32, + PCI_BASE_ADDRESSREG_OFFSET, + 1, + &Bar0 + ); + + Status =3D PciIo->Pci.Read ( + PciIo, + EfiPciIoWidthUint32, + PCI_BASE_ADDRESSREG_OFFSET + 4, + 1, + &Bar1 + ); + + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: MEMIO Base0 %x\n", Bar0)= ); + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: MEMIO Base1 %x\n", Bar1)= ); + + + MmcHostData->MmcHostIo.ResetMmcHost (&MmcHostData->MmcHostIo, Reset_All); + MmcHostData->EnableVerboseDebug =3D FALSE; + + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_CTRLRVER, + 1, + &Data16 + ); + + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: MMIO_CTRLRVER =3D 0x%08x= \n", Data16)); + + MmcHostData->ControllerVersion =3D Data16 & 0xFF; + switch (MmcHostData->ControllerVersion) { + case 0: DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: MMC Host Contr= oller Version 1.0\n")); break; + case 1: DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: MMC Host Contr= oller Version 2.0\n")); break; + case 2: DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: MMC Host Contr= oller Version 3.0\n")); break; + default: + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: Unknown MMC Host Con= troller Version, Stopping Driver!!\n")); + goto Exit; + } + + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint32, + 0, + (UINT32) MMIO_CAP, + 1, + &Data + ); + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: Low MMIO_CAP =3D 0x%08x\= n", Data)); + + if ((Data & BIT18) !=3D 0) { + MmcHostData->MmcHostIo.HostCapability.BusWidth8 =3D TRUE; + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: BusWidth8\n")); + } + + MmcHostData->MmcHostIo.HostCapability.BusWidth4 =3D TRUE; + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: BusWidth4\n")); + + if ((Data & BIT19) !=3D 0) { + MmcHostData->MmcHostIo.HostCapability.ADMA2Support =3D TRUE; + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: ADMA2Support\n")); + } + + if ((Data & BIT21) !=3D 0) { + MmcHostData->MmcHostIo.HostCapability.HighSpeedSupport =3D TRUE; + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: HighSpeedSupport\n")); + } + + if ((Data & BIT22) !=3D 0) { + MmcHostData->MmcHostIo.HostCapability.SDMASupport =3D TRUE; + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: SDMASupport\n")); + } + + if ((Data & BIT24) !=3D 0) { + MmcHostData->MmcHostIo.HostCapability.V33Support =3D TRUE; + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: V33Support\n")); + } + + if ((Data & BIT25) !=3D 0) { + MmcHostData->MmcHostIo.HostCapability.V30Support =3D TRUE; + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: V30Support\n")); + } + + if ((Data & BIT26) !=3D 0) { + MmcHostData->MmcHostIo.HostCapability.V18Support =3D TRUE; + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: V18Support\n")); + } + + if (((Data) & (BIT30 | BIT31)) =3D=3D 0) { + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: Removable Card Slot\n"= )); + } else if ((Data & BIT30) && (Data & (~BIT31))) { + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: Embedded Slot for One = Device\n")); + } + MmcHostData->BaseClockInMHz =3D (Data >> 8) & 0xFF; + + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint32, + 0, + (UINT32) (MMIO_CAP + 4), + 1, + &Data + ); + + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: High MMIO_CAP =3D 0x%08x= \n", Data)); + + + + if ((Data & 0x1<<(32-32)) !=3D 0) { + MmcHostData->MmcHostIo.HostCapability.SDR50Support=3D TRUE; + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: SDR50Support\n")); + } + + if ((Data & 0x1<<(33-32)) !=3D 0) { + MmcHostData->MmcHostIo.HostCapability.SDR104Support=3D TRUE; + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: SDR104Support\n")); + } + + if ((Data & 0x1<<(34-32)) !=3D 0) { + MmcHostData->MmcHostIo.HostCapability.DDR50Support=3D TRUE; + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: DDR50Support\n")); + } + + if ((Data & 0x1<<(63-32)) !=3D 0) { + MmcHostData->MmcHostIo.HostCapability.HS400Support =3D TRUE; + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: HS400Support\n")); + } + + if (MmcHostData->ControllerVersion >=3D 2) { + MmcHostData->MmcHostIo.HostCapability.ReTuneMode =3D (Data >> (46-32))= & 0x3; + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: ReTuneMode =3D 0x%08x= \n", MmcHostData->MmcHostIo.HostCapability.ReTuneMode)); + + MmcHostData->MmcHostIo.HostCapability.ReTuneTimer =3D (Data>>(40-32)) = & 0xF; + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: ReTuneTimer =3D 0x%08= x\n", MmcHostData->MmcHostIo.HostCapability.ReTuneTimer)); + } + + + MmcHostData->BlockLength =3D BLOCK_SIZE; + MmcHostData->IsAutoStopCmd =3D TRUE; + + Status =3D gBS->InstallProtocolInterface ( + &Controller, + &gEfiMmcHostIoProtocolGuid, + EFI_NATIVE_INTERFACE, + &MmcHostData->MmcHostIo + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Component name protocol + // + Status =3D AddUnicodeString ( + "eng", + gMmcHostComponentName.SupportedLanguages, + &MmcHostData->ControllerNameTable, + L"MMC Host Controller" + ); + if (EFI_ERROR (Status)) { + Status =3D EFI_OUT_OF_RESOURCES; + gBS->UninstallProtocolInterface ( + Controller, + &gEfiMmcHostIoProtocolGuid, + &MmcHostData->MmcHostIo + ); + } + + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: Host Started\n")); + +Exit: + if (EFI_ERROR (Status)) { + if (MmcHostData !=3D NULL) { + FreePool (MmcHostData); + } + gBS->CloseProtocol ( + Controller, + &gEfiPciIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + } + return Status; +} + + +/** + Stop MMC Host driver on ControllerHandle. Support stoping any child hand= les + created by this driver. + + @param[in] This Protocol instance pointer + @param[in] Controller Handle of device to stop driver on + @param[in] NumberOfChildren Number of Children in the ChildHandleBu= ffer + @param[in] ChildHandleBuffer List of handles for the children we nee= d to stop + + @retval EFI_SUCCESS This driver stop this device + @retval EFI_DEVICE_ERROR This driver cannot be stop due to devic= e Error + +**/ +EFI_STATUS +EFIAPI +MmcHostDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ) +{ + EFI_STATUS Status; + EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo; + MMCHOST_DATA *MmcHostData; + + Status =3D gBS->OpenProtocol ( + Controller, + &gEfiMmcHostIoProtocolGuid, + (VOID **) &MmcHostIo, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + goto _exit_MmcHostDriverBindingStop; + } + + gBS->CloseProtocol ( + Controller, + &gEfiPciIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + + MmcHostData =3D MMCHOST_DATA_FROM_THIS (MmcHostIo); + + // + // Uninstall Block I/O protocol from the device handle + // + Status =3D gBS->UninstallProtocolInterface ( + Controller, + &gEfiMmcHostIoProtocolGuid, + &MmcHostData->MmcHostIo + ); + if (EFI_ERROR (Status)) { + goto _exit_MmcHostDriverBindingStop; + } + + FreeUnicodeStringTable (MmcHostData->ControllerNameTable); + FreePool (MmcHostData); + +_exit_MmcHostDriverBindingStop: + DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStop exited with Status %r\n", = Status)); + + return Status; +} + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcH= ostDxe/MmcHostDriver.h b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/= Dxe/MMC/MmcHostDxe/MmcHostDriver.h new file mode 100644 index 0000000..f724061 --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcHostDxe/= MmcHostDriver.h @@ -0,0 +1,585 @@ +/** @file + Header file for MMC driver. + + Copyright (c) 1999 - 2016, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#ifndef _MMC_HOST_DRIVER_H +#define _MMC_HOST_DRIVER_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// +// Driver Consumed Protocol Prototypes +// +#include +#include + +// +// Driver Produced Protocol Prototypes +// +#include +#include +#include +#include + +extern EFI_COMPONENT_NAME_PROTOCOL gMmcHostComponentName; +extern EFI_DRIVER_BINDING_PROTOCOL gMmcHostDriverBinding; + +#define MMCHOST_DATA_SIGNATURE SIGNATURE_32 ('m', 'c', 'h', 's') + +#define MMCHOST_DATA_FROM_THIS(a) \ + CR (a, MMCHOST_DATA, MmcHostIo, MMCHOST_DATA_SIGNATURE) + +#define BLOCK_SIZE 0x200 +#define TIME_OUT_1S 1000 + +#define INTEL_VENDOR_ID 0x8086 + +#define BUFFER_CTL_REGISTER 0x84 + +#pragma pack(1) +// +// PCI Class Code structure +// +typedef struct { + UINT8 PI; + UINT8 SubClassCode; + UINT8 BaseCode; +} PCI_CLASSC; + +#pragma pack() + +// +// MMCHOST_DATA structure +// +typedef struct { + UINTN Signature; + EFI_MMC_HOST_IO_PROTOCOL MmcHostIo; + EFI_PCI_IO_PROTOCOL *PciIo; + UINT16 PciVid; + UINT16 PciDid; + BOOLEAN IsAutoStopCmd; + BOOLEAN IsEmmc; + BOOLEAN EnableVerboseDebug; + UINT32 BaseClockInMHz; + UINT32 CurrentClockInKHz; + UINT32 BlockLength; + EFI_UNICODE_STRING_TABLE *ControllerNameTable; + UINT32 ControllerVersion; +} MMCHOST_DATA; + +// +// Prototypes +// Driver model protocol interface +// +/** + Entry point for MMC Host EFI drivers. + + @param[in] ImageHandle EFI_HANDLE + @param[in] SystemTable EFI_SYSTEM_TABLE + + @retval EFI_SUCCESS The function completed successfully + @retval EFI_DEVICE_ERROR The function failed to complete + +**/ +EFI_STATUS +EFIAPI +MmcHostDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ); + +/** + Test to see if this MMC Host driver supports ControllerHandle. + Any ControllerHandle that has installed will be supported. + + @param[in] This Protocol instance pointer + @param[in] Controller Handle of device to test + @param[in] RemainingDevicePath Not used + + @retval EFI_SUCCESS This driver supports this device + @retval EFI_UNSUPPORTED This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +MmcHostDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ); + +/** + Starting the MMC Host Driver + + @param[in] This Protocol instance pointer + @param[in] Controller Handle of device to start + @param[in] RemainingDevicePath Not used + + @retval EFI_SUCCESS This driver start this device + @retval EFI_UNSUPPORTED This driver does not support this de= vice + @retval EFI_DEVICE_ERROR This driver cannot be started due to= device Error + @retval EFI_OUT_OF_RESOURCES This driver cannot allocate resources + +**/ +EFI_STATUS +EFIAPI +MmcHostDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ); + +/** + Stop MMC Host driver on ControllerHandle. Support stopping any child han= dles + created by this driver. + + @param[in] This Protocol instance pointer + @param[in] Controller Handle of device to stop driver on + @param[in] NumberOfChildren Number of Children in the ChildHandl= eBuffer + @param[in] ChildHandleBuffer List of handles for the children we = need to stop + + @retval EFI_SUCCESS This driver stop this device + @retval EFI_DEVICE_ERROR This driver cannot be stop due to de= vice Error + +**/ +EFI_STATUS +EFIAPI +MmcHostDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ); + +// +// EFI Component Name Functions +// +/** + Retrieves a Unicode string that is the user readable name of the EFI Dri= ver. + + @param[in] This A pointer to the EFI_COMPONENT_NAME_= PROTOCOL instance. + @param[in] Language A pointer to a three character ISO 6= 39-2 language identifier. + This is the language of the driver n= ame that that the caller + is requesting, and it must match one= of the languages specified + in SupportedLanguages. The number o= f languages supported by a + driver is up to the driver writer. + @param[out] DriverName A pointer to the Unicode string to r= eturn. This Unicode string + is the name of the driver specified = by This in the language + specified by Language. + + + @retval EFI_SUCCESS The Unicode string for the Driver sp= ecified by This + and the language specified by Langua= ge was returned + in DriverName. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER DriverName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This does no= t support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +MmcHostComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + +/** + Retrieves a Unicode string that is the user readable name of the control= ler + that is being managed by an EFI Driver. + + @param[in] This A pointer to the EFI_COMPONENT_NAME_= PROTOCOL instance. + @param[in] ControllerHandle The handle of a controller that the = driver specified by + This is managing. This handle speci= fies the controller + whose name is to be returned. + @param[in] ChildHandle The handle of the child controller t= o retrieve the name + of. This is an optional parameter t= hat may be NULL. It + will be NULL for device drivers. It= will also be NULL + for a bus drivers that wish to retri= eve the name of the + bus controller. It will not be NULL= for a bus driver + that wishes to retrieve the name of = a child controller. + @param[in] Language A pointer to a three character ISO 6= 39-2 language + identifier. This is the language of= the controller name + that that the caller is requesting, = and it must match one + of the languages specified in Suppor= tedLanguages. The + number of languages supported by a d= river is up to the + driver writer. + @param[out] ControllerName A pointer to the Unicode string to r= eturn. This Unicode + string is the name of the controller= specified by + ControllerHandle and ChildHandle in = the language + specified by Language from the point= of view of the + driver specified by This. + + + @retval EFI_SUCCESS The Unicode string for the user read= able name in the + language specified by Language for t= he driver + specified by This was returned in Dr= iverName. + @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_= HANDLE. + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is no= t a valid + EFI_HANDLE. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This is not = currently + managing the controller specified by + ControllerHandle and ChildHandle. + @retval EFI_UNSUPPORTED The driver specified by This does no= t support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +MmcHostComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle, OPTIONAL + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + +/** + The main function used to send the command to the card inserted into the= MMC host + slot. + It will assemble the arguments to set the command register and wait for = the command + and transfer completed until timeout. Then it will read the response reg= ister to fill + the ResponseData + + @param[in] This Pointer to the EFI_COMPONENT_NAME_= PROTOCOL instance. + @param[in] CommandIndex The command index to set the comma= nd index field of command register + @param[in] Argument Command argument to set the argume= nt field of command register + @param[in] DataType TRANSFER_TYPE, indicates no data, = data in or data out + @param[in] Buffer Contains the data read from / writ= e to the device + @param[in] BufferSize The size of the buffer + @param[in] ResponseType RESPONSE_TYPE + @param[in] TimeOut Time out value in 1 ms unit + @param[out] ResponseData Depending on the ResponseType, suc= h as CSD or card status + + @retval EFI_SUCCESS + @retval EFI_INVALID_PARAMETER + @retval EFI_OUT_OF_RESOURCES + @retval EFI_TIMEOUT + @retval EFI_DEVICE_ERROR + +**/ +EFI_STATUS +EFIAPI +SendCommand ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN UINT16 CommandIndex, + IN UINT32 Argument, + IN TRANSFER_TYPE DataType, + IN UINT8 *Buffer, OPTIONAL + IN UINT32 BufferSize, + IN RESPONSE_TYPE ResponseType, + IN UINT32 TimeOut, + OUT UINT32 *ResponseData OPTIONAL + ); + +/** + Set max clock frequency of the host, the actual frequency + may not be the same as MaxFrequencyInKHz. It depends on + the max frequency the host can support, divider, and host + speed mode. + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] MaxFrequency Max frequency in HZ + + @retval EFI_SUCCESS + @retval EFI_TIMEOUT +**/ +EFI_STATUS +EFIAPI +SetClockFrequency ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN UINT32 MaxFrequencyInKHz + ); + +/** + Set bus width of the host + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] BusWidth Bus width in 1, 4, 8 bits + + + @retval EFI_STATUS + @retval EFI_INVALID_PARAMETER + +**/ +EFI_STATUS +EFIAPI +SetBusWidth ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN UINT32 BusWidth + ); + +/** + Set voltage which could supported by the host. + Support 0(Power off the host), 1.8V, 3.0V, 3.3V + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] Voltage Units in 0.1 V + + @retval EFI_SUCCESS + @retval EFI_INVALID_PARAMETER + +**/ +EFI_STATUS +EFIAPI +SetHostVoltage ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN UINT32 Voltage + ); + +/** + Set Host mode in DDR + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] DdrMode True for DDR Mode set, false returns EFI_S= UCCESS + + @retval EFI_SUCCESS + @retval EFI_INVALID_PARAMETER + +**/ +EFI_STATUS +EFIAPI +SetHostDdrMode ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN UINT32 DdrMode + ); + +/** + Set Host SDR Mode + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] DdrMode True for SDR Mode set, false for nor= mal mode + + @retval EFI_SUCCESS The function completed successfully + @retval EFI_INVALID_PARAMETER A parameter was incorrect. + +**/ +EFI_STATUS +EFIAPI +SetHostSdrMode ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN UINT32 SdrMode + ); + +/** + Set Host High Speed + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] HighSpeed True for High Speed Mode set, false = for normal mode + + @retval EFI_SUCCESS The function completed successfully + @retval EFI_INVALID_PARAMETER A parameter was incorrect. + +**/ +EFI_STATUS +EFIAPI +SetHostSpeedMode ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN UINT32 HighSpeed + ); + +/** + Reset the host + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] ResetAll TRUE to reset all + + @retval EFI_SUCCESS The function completed successfully + @retval EFI_TIMEOUT The timeout time expired. + +**/ +EFI_STATUS +EFIAPI +ResetMmcHost ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN RESET_TYPE ResetType + ); + +/** + Reset the host + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] ResetAll TRUE to reset all + + @retval EFI_SUCCESS The function completed successfully + @retval EFI_TIMEOUT The timeout time expired. + +**/ +EFI_STATUS +EFIAPI +EnableAutoStopCmd ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN BOOLEAN Enable + ); + +/** + Find whether these is a card inserted into the slot. If so + init the host. If not, return EFI_NOT_FOUND. + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Fail + +**/ +EFI_STATUS +EFIAPI +DetectCardAndInitHost ( + IN EFI_MMC_HOST_IO_PROTOCOL *This + ); + +/** + Set the Block length + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] BlockLength card supports block length + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Fail + +**/ +EFI_STATUS +EFIAPI +SetBlockLength ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN UINT32 BlockLength + ); + +/** + Setup Device + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Fail + +**/ +EFI_STATUS +EFIAPI +SetupDevice( + IN EFI_MMC_HOST_IO_PROTOCOL *This + ); + +/** + Read MMC host register + + @param[in] MmcHost Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] Offset Offset of the eMMC + + @retval Data Data read from eMMC + +**/ +UINT8 +MmcHostRead8 ( + IN MMCHOST_DATA *MmcHost, + IN UINTN Offset + ); + +/** + Read MMC host register + + @param[in] MmcHost Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] Offset Offset of the eMMC + + @retval Data Data read from eMMC + +**/ +UINT16 +MmcHostRead16 ( + IN MMCHOST_DATA *MmcHost, + IN UINTN Offset + ); + +/** + Read MMC host register + + @param[in] MmcHost Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] Offset Offset of the eMMC + + @retval Data Data read from eMMC + +**/ +UINT32 +MmcHostRead32 ( + IN MMCHOST_DATA *MmcHost, + IN UINTN Offset + ); + +/** + Write MMC host register + + @param[in] MmcHost Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] Offset Offset of the eMMC + @param[in] Data Data write to eMMC + + @retval Data Written to MmcHost + +**/ +UINT8 +MmcHostWrite8 ( + IN MMCHOST_DATA *MmcHost, + IN UINTN Offset, + IN UINT8 Data + ); + +/** + Write MMC host register + + @param[in] MmcHost Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] Offset Offset of the eMMC + @param[in] Data Data write to eMMC + + @retval Data Data written to eMMC + +**/ +UINT16 +MmcHostWrite16 ( + IN MMCHOST_DATA *MmcHost, + IN UINTN Offset, + IN UINT16 Data + ); + +/** + Write MMC host register + + @param[in] MmcHost Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] Offset Offset of the eMMC + @param[in] Data Data write to eMMC + + @retval Data Data written to eMMC + +**/ +UINT32 +MmcHostWrite32 ( + IN MMCHOST_DATA *MmcHost, + IN UINTN Offset, + IN UINT32 Data + ); + +#endif + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcH= ostDxe/MmcHostDxe.inf b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/D= xe/MMC/MmcHostDxe/MmcHostDxe.inf new file mode 100644 index 0000000..0433019 --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcHostDxe/= MmcHostDxe.inf @@ -0,0 +1,59 @@ +## @file +# MMC Host module +# +# Copyright (c) 1999 - 2016, Intel Corporation. All rights reserved.
+# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the B= SD License +# which accompanies this distribution. The full text of the license may b= e 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 IM= PLIED. +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D MmcHost + FILE_GUID =3D D86DB3C5-57FE-44CF-A82A-74C4AD5B1C2E + MODULE_TYPE =3D UEFI_DRIVER + VERSION_STRING =3D 1.0 + ENTRY_POINT =3D MmcHostDriverEntryPoint + +# +# The following information is for reference only and not required by the = build tools. +# +# VALID_ARCHITECTURES =3D IA32 X64 IPF EBC +# + +[Sources.common] + ComponentName.c + MmcHostDriver.h + MmcHostDriver.c + MmcHostController.c + +[Packages] + MdePkg/MdePkg.dec + BroxtonSiPkg/BroxtonSiPkg.dec + +[LibraryClasses] + UefiDriverEntryPoint + DebugLib + UefiBootServicesTableLib + BaseMemoryLib + MemoryAllocationLib + UefiLib + DevicePathLib + IoLib + PcdLib + HobLib + +[Protocols] + gEfiPciIoProtocolGuid ## CONSUMES + gEfiMmcHostIoProtocolGuid ## BY_START + +[Guids] + gEfiBootMediaHobGuid ## UNDEFINED + +[Pcd] diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcM= ediaDeviceDxe/ComponentName.c b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluste= r/Sdio/Dxe/MMC/MmcMediaDeviceDxe/ComponentName.c new file mode 100644 index 0000000..a0b0940 --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcMediaDev= iceDxe/ComponentName.c @@ -0,0 +1,159 @@ +/** @file + Header file for ComponentName. + + Copyright (c) 1999 - 2016, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include + +// +// EFI Component Name Protocol +// +EFI_COMPONENT_NAME_PROTOCOL gMediaDeviceComponentName =3D { + MediaDeviceComponentNameGetDriverName, + MediaDeviceComponentNameGetControllerName, + "eng" +}; + + +static EFI_UNICODE_STRING_TABLE mMediaDeviceDriverNameTable[] =3D { + { "eng", L"UEFI MMC/SD Media Device Driver" }, + { NULL , NULL } +}; + + +/** + Retrieves a Unicode string that is the user readable name of the EFI Dri= ver. + + @param[in] This A pointer to the EFI_COMPONENT_NAME_= PROTOCOL instance. + @param[in] Language A pointer to a three character ISO 6= 39-2 language identifier. + This is the language of the driver n= ame that that the caller + is requesting, and it must match one= of the languages specified + in SupportedLanguages. The number o= f languages supported by a + driver is up to the driver writer. + @param[out] DriverName A pointer to the Unicode string to r= eturn. This Unicode string + is the name of the driver specified = by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver s= pecified by This + and the language specified by Langu= age was returned + in DriverName. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER DriverName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This does n= ot support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +MediaDeviceComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString ( + Language, + gMediaDeviceComponentName.SupportedLanguages, + mMediaDeviceDriverNameTable, + DriverName + ); +} + + +/** + Retrieves a Unicode string that is the user readable name of the control= ler + that is being managed by an EFI Driver. + + @param[in] This A pointer to the EFI_COMPONENT_NAME_= PROTOCOL instance. + @param[in] ControllerHandle The handle of a controller that the = driver specified by + This is managing. This handle speci= fies the controller + whose name is to be returned. + @param[in] ChildHandle The handle of the child controller t= o retrieve the name + of. This is an optional parameter t= hat may be NULL. It + will be NULL for device drivers. It= will also be NULL + for a bus drivers that wish to retri= eve the name of the + bus controller. It will not be NULL= for a bus driver + that wishes to retrieve the name of = a child controller. + @param[in] Language A pointer to a three character ISO 6= 39-2 language + identifier. This is the language of= the controller name + that that the caller is requesting, = and it must match one + of the languages specified in Suppor= tedLanguages. The + number of languages supported by a d= river is up to the + driver writer. + @param[out] ControllerName A pointer to the Unicode string to r= eturn. This Unicode + string is the name of the controller= specified by + ControllerHandle and ChildHandle in = the language + specified by Language from the point= of view of the + driver specified by This. + + @retval EFI_SUCCESS The Unicode string for the user read= able name in the + language specified by Language for t= he driver + specified by This was returned in Dr= iverName. + @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_= HANDLE. + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is no= t a valid + EFI_HANDLE. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This is not = currently + managing the controller specified by + ControllerHandle and ChildHandle. + @retval EFI_UNSUPPORTED The driver specified by This does no= t support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +MediaDeviceComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle, OPTIONAL + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_BLOCK_IO_PROTOCOL *BlockIo; + CARD_DATA *CardData; + + // + // This is a device driver, so ChildHandle must be NULL. + // + if (ChildHandle !=3D NULL) { + return EFI_UNSUPPORTED; + } + + // + // Get the device context + // + Status =3D gBS->OpenProtocol ( + ControllerHandle, + &gEfiBlockIoProtocolGuid, + (VOID **) &BlockIo, + gMediaDeviceDriverBinding.DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + CardData =3D CARD_DATA_FROM_THIS (BlockIo); + return LookupUnicodeString ( + Language, + gMediaDeviceComponentName.SupportedLanguages, + CardData->ControllerNameTable, + ControllerName + ); +} + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcM= ediaDeviceDxe/MMCSDBlockIo.c b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster= /Sdio/Dxe/MMC/MmcMediaDeviceDxe/MMCSDBlockIo.c new file mode 100644 index 0000000..5c91190 --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcMediaDev= iceDxe/MMCSDBlockIo.c @@ -0,0 +1,666 @@ +/** @file + Block I/O protocol for MMC/SD device. + + Copyright (c) 2012 - 2017, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include + + +/** + Implements EFI_BLOCK_IO_PROTOCOL.Reset() function. + + @param[in] This The EFI_BLOCK_IO_PROTOCOL instance. + @param[in] ExtendedVerification Indicates that the driver may perfor= m a more + exhaustive verification operation of= the device during reset. + (This parameter is ingored in this d= river.) + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +EFIAPI +MMCSDBlockReset ( + IN EFI_BLOCK_IO_PROTOCOL *This, + IN BOOLEAN ExtendedVerification + ) +{ + CARD_DATA *CardData; + EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo; + + CardData =3D CARD_DATA_FROM_THIS (This); + MmcHostIo =3D CardData->MmcHostIo; + + DEBUG ((EFI_D_INFO, "MMC SD Block: Resetting host\n")); + + return MmcHostIo->ResetMmcHost (MmcHostIo, Reset_DAT_CMD); +} + + +/** + Implements EFI_BLOCK_IO_PROTOCOL.ReadBlocks() function. + + @param[in] This The EFI_BLOCK_IO_PROTOCOL instance. + @param[in] MediaId The media id that the read request = is for. + @param[in] LBA The starting logical block address = to read from on the device. + @param[in] BufferSiz The size of the Buffer in bytes. Th= is must be a multiple of + the intrinsic block size of the dev= ice. + @param[out] Buffer A pointer to the destination buffer= for the data. The caller + is responsible for either having im= plicit or explicit ownership + of the buffer. + + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +EFIAPI +MMCSDBlockReadBlocks ( + IN EFI_BLOCK_IO_PROTOCOL *This, + IN UINT32 MediaId, + IN EFI_LBA LBA, + IN UINTN BufferSize, + OUT VOID *Buffer + ) +{ + EFI_STATUS Status; + UINT32 Address; + CARD_DATA *CardData; + EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo; + UINT32 RemainingLength; + UINT32 TransferLength; + UINT8 *BufferPointer; + BOOLEAN SectorAddressing; + UINT64 CardSize; + MMC_PARTITION_DATA *Partition; + UINTN TotalBlock; + + Status =3D EFI_SUCCESS; + Partition =3D CARD_PARTITION_DATA_FROM_THIS (This); + CardData =3D Partition->CardData; + MmcHostIo =3D CardData->MmcHostIo; + + // + // Media ID has high priority that need to be verify first + // + if (MediaId !=3D Partition->BlockIoMedia.MediaId) { + return EFI_MEDIA_CHANGED; + } + + Status =3D MmcSelectPartition (Partition); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // BufferSize must be a multiple of the intrinsic block size of the devi= ce. + // + if (ModU64x32 (BufferSize,Partition->BlockIoMedia.BlockSize) !=3D 0) { + return EFI_BAD_BUFFER_SIZE; + } + + CardSize =3D MultU64x32 (CardData->BlockNumber, CardData->BlockLen); + + if ((CardData->CardType =3D=3D SdMemoryCard2High) || (CardSize >=3D SIZE= _2GB)) { + SectorAddressing =3D TRUE; + } else { + SectorAddressing =3D FALSE; + } + + if (SectorAddressing) { + // + // Sector Address + // + Address =3D (UINT32) DivU64x32 (MultU64x32 (LBA, Partition->BlockIoMed= ia.BlockSize), 512); + } else { + // + // Byte Address + // + Address =3D (UINT32) MultU64x32 (LBA, Partition->BlockIoMedia.BlockSi= ze); + } + + TotalBlock =3D (UINTN) DivU64x32 (BufferSize, Partition->BlockIoMedia.Bl= ockSize); + // + // Make sure the range to read is valid. + // + if (LBA + TotalBlock > Partition->BlockIoMedia.LastBlock + 1) { + return EFI_INVALID_PARAMETER; + } + + if (!(Partition->BlockIoMedia.MediaPresent)) { + return EFI_NO_MEDIA; + } + + if (!Buffer) { + Status =3D EFI_INVALID_PARAMETER; + DEBUG ((EFI_D_ERROR, "MMCSDBlockReadBlocks: Invalid parameter \n")); + goto Done; + } + + if ((BufferSize % Partition->BlockIoMedia.BlockSize) !=3D 0) { + Status =3D EFI_BAD_BUFFER_SIZE; + DEBUG ((EFI_D_ERROR, "MMCSDBlockReadBlocks: Bad buffer size \n")); + goto Done; + } + + if (BufferSize =3D=3D 0) { + Status =3D EFI_SUCCESS; + goto Done; + } + + BufferPointer =3D Buffer; + RemainingLength =3D (UINT32) BufferSize; + + while (RemainingLength > 0) { + if ((BufferSize >=3D Partition->BlockIoMedia.BlockSize)) { + if (RemainingLength > MmcHostIo->HostCapability.BoundarySize) { + TransferLength =3D MmcHostIo->HostCapability.BoundarySize; + } else { + TransferLength =3D RemainingLength; + } + + if (CardData->CardType =3D=3D MMCCard) { + if (!(CardData->ExtCSDRegister.CARD_TYPE & (BIT2 | BIT3))) { + Status =3D SendCommand ( + MmcHostIo, + SET_BLOCKLEN, + Partition->BlockIoMedia.BlockSize, + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + if (EFI_ERROR (Status)) { + break; + } + } + + Status =3D SendCommand ( + MmcHostIo, + SET_BLOCK_COUNT, + TransferLength / Partition->BlockIoMedia.BlockSize, + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + if (EFI_ERROR (Status)) { + break; + } + } + + Status =3D SendCommand ( + MmcHostIo, + READ_MULTIPLE_BLOCK, + Address, + InData, + CardData->AlignedBuffer, + TransferLength, + ResponseR1, + TIMEOUT_DATA, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MMCSDBlockReadBlocks: READ_MULTIPLE_BLOCK ->= Fail\n")); + break; + } + } else { + if (RemainingLength > Partition->BlockIoMedia.BlockSize) { + TransferLength =3D Partition->BlockIoMedia.BlockSize; + } else { + TransferLength =3D RemainingLength; + } + + Status =3D SendCommand ( + MmcHostIo, + READ_SINGLE_BLOCK, + Address, + InData, + CardData->AlignedBuffer, + (UINT32) TransferLength, + ResponseR1, + TIMEOUT_DATA, + (UINT32*) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MMCSDBlockReadBlocks: READ_SINGLE_BLOCK -> F= ail\n")); + break; + } + } + + CopyMem (BufferPointer, CardData->AlignedBuffer, TransferLength); + + if (SectorAddressing) { + // + // Sector Address + // + Address +=3D TransferLength / 512; + } else { + // + // Byte Address + // + Address +=3D TransferLength; + } + BufferPointer +=3D TransferLength; + RemainingLength -=3D TransferLength; + } + + if (EFI_ERROR (Status)) { + if ((CardData->CardType =3D=3D SdMemoryCard) || + (CardData->CardType =3D=3D SdMemoryCard2)|| + (CardData->CardType =3D=3D SdMemoryCard2High)) { + SendCommand ( + MmcHostIo, + STOP_TRANSMISSION, + 0, + NoData, + NULL, + 0, + ResponseR1b, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + } else { + SendCommand ( + MmcHostIo, + STOP_TRANSMISSION, + 0, + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + } + } + +Done: + return Status; +} + + +/** + Implements EFI_BLOCK_IO_PROTOCOL.WriteBlocks() function. + + @param[in] This The EFI_BLOCK_IO_PROTOCOL instance. + @param[in] MediaId The media id that the write request = is for. + @param[in] LBA The starting logical block address t= o be written. + The caller is responsible for writin= g to only + legitimate locations. + @param[in] BufferSize + The size of the Buffer in bytes. Thi= s must be a multiple of + the intrinsic block size of the devi= ce. + @param[in] Buffer A pointer to the source buffer for t= he data. The caller + is responsible for either having imp= licit or explicit ownership + of the buffer. + + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +EFIAPI +MMCSDBlockWriteBlocks ( + IN EFI_BLOCK_IO_PROTOCOL *This, + IN UINT32 MediaId, + IN EFI_LBA LBA, + IN UINTN BufferSize, + IN VOID *Buffer + ) +{ + EFI_STATUS Status; + UINT32 Address; + CARD_DATA *CardData; + EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo; + UINT32 RemainingLength; + UINT32 TransferLength; + UINT8 *BufferPointer; + BOOLEAN SectorAddressing; + UINT64 CardSize; + MMC_PARTITION_DATA *Partition; + + Status =3D EFI_SUCCESS; + Partition =3D CARD_PARTITION_DATA_FROM_THIS (This); + CardData =3D Partition->CardData; + MmcHostIo =3D CardData->MmcHostIo; + + Status =3D MmcSelectPartition (Partition); + if (EFI_ERROR (Status)) { + return Status; + } + + CardSize =3D MultU64x32 (CardData->BlockNumber, CardData->BlockLen); + + if ((CardData->CardType =3D=3D SdMemoryCard2High) || (CardSize >=3D SIZE= _2GB)) { + SectorAddressing =3D TRUE; + } else { + SectorAddressing =3D FALSE; + } + + if (SectorAddressing) { + // + // Sector Address + // + Address =3D (UINT32) DivU64x32 (MultU64x32 (LBA, Partition->BlockIoMed= ia.BlockSize), 512); + } else { + // + // Byte Address + // + Address =3D (UINT32) MultU64x32 (LBA, Partition->BlockIoMedia.BlockSiz= e); + } + + if (!Buffer) { + Status =3D EFI_INVALID_PARAMETER; + DEBUG ((EFI_D_ERROR, "MMCSDBlockWriteBlocks: Invalid parameter \n")); + goto Done; + } + + if ((BufferSize % Partition->BlockIoMedia.BlockSize) !=3D 0) { + Status =3D EFI_BAD_BUFFER_SIZE; + DEBUG ((EFI_D_ERROR, "MMCSDBlockWriteBlocks: Bad buffer size \n")); + goto Done; + } + + if (BufferSize =3D=3D 0) { + Status =3D EFI_SUCCESS; + goto Done; + } + + if (This->Media->ReadOnly =3D=3D TRUE) { + Status =3D EFI_WRITE_PROTECTED; + DEBUG ((EFI_D_ERROR, "MMCSDBlockWriteBlocks: Write protected \n")); + goto Done; + } + + BufferPointer =3D Buffer; + RemainingLength =3D (UINT32) BufferSize; + + while (RemainingLength > 0) { + if ((BufferSize >=3D Partition->BlockIoMedia.BlockSize) ) { + if (RemainingLength > MmcHostIo->HostCapability.BoundarySize) { + TransferLength =3D MmcHostIo->HostCapability.BoundarySize; + } else { + TransferLength =3D RemainingLength; + } + + if ((CardData->CardType =3D=3D SdMemoryCard) || + (CardData->CardType =3D=3D SdMemoryCard2)|| + (CardData->CardType =3D=3D SdMemoryCard2High)) { + // + // Write performance improvement + // + if ((TransferLength / Partition->BlockIoMedia.BlockSize) > 64) { + Status =3D SendAppCommand ( + CardData, + SET_WR_BLK_ERASE_COUNT, + (UINT32) (TransferLength / Partition->BlockIoMedia.B= lockSize), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + } + } else if (CardData->CardType =3D=3D MMCCard) { + + if (!(CardData->ExtCSDRegister.CARD_TYPE & (BIT2 | BIT3))) { + Status =3D SendCommand ( + MmcHostIo, + SET_BLOCKLEN, + Partition->BlockIoMedia.BlockSize, + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + break; + } + } + Status =3D SendCommand ( + MmcHostIo, + SET_BLOCK_COUNT, + TransferLength / Partition->BlockIoMedia.BlockSize, + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + if (EFI_ERROR (Status)) { + break; + } + } + + CopyMem (CardData->AlignedBuffer, BufferPointer, TransferLength); + + Status =3D SendCommand ( + MmcHostIo, + WRITE_MULTIPLE_BLOCK, + Address, + OutData, + CardData->AlignedBuffer, + (UINT32) TransferLength, + ResponseR1, + TIMEOUT_DATA, + (UINT32 *) &(CardData->CardStatus) + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MMCSDBlockWriteBlocks: WRITE_MULTIPLE_BLOCK = -> Fail\n")); + break; + } + } else { + if (RemainingLength > Partition->BlockIoMedia.BlockSize) { + TransferLength =3D Partition->BlockIoMedia.BlockSize; + } else { + TransferLength =3D RemainingLength; + } + + CopyMem (CardData->AlignedBuffer, BufferPointer, TransferLength); + + Status =3D SendCommand ( + MmcHostIo, + WRITE_BLOCK, + Address, + OutData, + CardData->AlignedBuffer, + (UINT32) TransferLength, + ResponseR1, + TIMEOUT_DATA, + (UINT32 *) &(CardData->CardStatus) + ); + } + + if (SectorAddressing) { + // + // Sector Address + // + Address +=3D TransferLength / 512; + } else { + // + // Byte Address + // + Address +=3D TransferLength; + } + BufferPointer +=3D TransferLength; + RemainingLength -=3D TransferLength; + } + + if (EFI_ERROR (Status)) { + SendCommand ( + MmcHostIo, + STOP_TRANSMISSION, + 0, + NoData, + NULL, + 0, + ResponseR1b, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + } + +Done: + return Status; +} + + +/** + Implements EFI_BLOCK_IO_PROTOCOL.FlushBlocks() function. + (In this driver, this function just returns EFI_SUCCESS.) + + @param[in] This The EFI_BLOCK_IO_PROTOCOL instance. + + @retval EFI_SUCCESS Success + +**/ +EFI_STATUS +EFIAPI +MMCSDBlockFlushBlocks ( + IN EFI_BLOCK_IO_PROTOCOL *This + ) +{ + return EFI_SUCCESS; +} + + +/** + MMC/SD card BlockIo init function + + @param[in] CardData Pointer to CARD_DATA + + @retval EFI_SUCCESS Success + +**/ +EFI_STATUS +MMCSDBlockIoInit ( + IN CARD_DATA *CardData + ) +{ + UINTN Loop; + MMC_PARTITION_DATA *Partition; + EXT_CSD *ExtCsd; + UINT64 GP_CHUNK_SIZE; + UINT32 GP_SIZE_MULT; + UINT64 GppSize; + UINTN GppIndex=3D0; + + Partition =3D CardData->Partitions; + ExtCsd =3D &CardData->ExtCSDRegister; + + // + // Determine GP partitioning chunk size + // + GP_CHUNK_SIZE =3D 0; + if (((ExtCsd->PARTITIONING_SUPPORT & BIT0) =3D=3D BIT0) && + ((ExtCsd->PARTITION_SETTING_COMPLETED & BIT0) =3D=3D BIT0)) { + GP_CHUNK_SIZE =3D MultU64x32 (ExtCsd->HC_WP_GRP_SIZE, ExtCsd->HC_ERASE= _GRP_SIZE); + GP_CHUNK_SIZE =3D MultU64x32 (GP_CHUNK_SIZE, SIZE_512KB); + } + + for (Loop =3D 0; Loop < MAX_NUMBER_OF_PARTITIONS; Partition++, Loop++) { + // + // BlockIO protocol + // + Partition->BlockIo.Revision =3D EFI_BLOCK_IO_PROTOCOL_REVISION; + Partition->BlockIo.Media =3D &(Partition->BlockIoMedia); + Partition->BlockIo.Reset =3D MMCSDBlockReset; + Partition->BlockIo.ReadBlocks =3D MMCSDBlockReadBlocks ; + Partition->BlockIo.WriteBlocks =3D MMCSDBlockWriteBlocks; + Partition->BlockIo.FlushBlocks =3D MMCSDBlockFlushBlocks; + + Partition->BlockIoMedia.MediaId =3D 0; + Partition->BlockIoMedia.RemovableMedia =3D FALSE; + Partition->BlockIoMedia.MediaPresent =3D TRUE; + Partition->BlockIoMedia.LogicalPartition =3D FALSE; + + // + // Force the User partition to be enabled + // + if (Loop =3D=3D 0) { + Partition->Present =3D TRUE; + } + + if (CardData->CSDRegister.PERM_WRITE_PROTECT || CardData->CSDRegister.= TMP_WRITE_PROTECT) { + Partition->BlockIoMedia.ReadOnly =3D TRUE; + } else { + Partition->BlockIoMedia.ReadOnly =3D FALSE; + } + + Partition->BlockIoMedia.WriteCaching =3D FALSE; + Partition->BlockIoMedia.BlockSize =3D CardData->BlockLen; + Partition->BlockIoMedia.IoAlign =3D 1; + Partition->BlockIoMedia.LastBlock =3D (EFI_LBA) (CardData->Bloc= kNumber - 1); + + // + // Handle GPP partitions + // + GppSize =3D 0; + if ((GP_CHUNK_SIZE !=3D 0) && (Loop >=3D 4)) { + Partition->BlockIoMedia.LastBlock =3D (EFI_LBA) 0; + GppIndex =3D Loop - 4; + GP_SIZE_MULT =3D MmcGetExtCsd24 ( + CardData, + OFFSET_OF (EXT_CSD, GP_SIZE_MULT_1) + (3 * GppIndex) + ); + GppSize =3D MultU64x32 (GP_SIZE_MULT, (UINT32) GP_CHUNK_SIZE); + } + + if (GppSize !=3D 0) { + Partition->BlockIoMedia.LastBlock =3D + DivU64x32 (GppSize, Partition->BlockIoMedia.BlockSize) - 1; + DEBUG ((EFI_D_INFO, + "GPP%d last-block: 0x%lx\n", + GppIndex + 1, + Partition->BlockIoMedia.LastBlock + )); + Partition->Present =3D TRUE; + } + + if (CardData->CardType =3D=3D MMCCard) { + // + // Handle Boot partitions + // + if ((Loop =3D=3D 1) || (Loop =3D=3D 2)) { + Partition->BlockIoMedia.LastBlock =3D 128 * 1024 * ((UINTN) MmcGet= ExtCsd8 (CardData, OFFSET_OF (EXT_CSD, BOOT_SIZE_MULTI))) / 512; + Partition->Present =3D TRUE; + } + } + } + + DEBUG ((EFI_D_INFO, "MMC SD Block I/O: Initialized\n")); + + return EFI_SUCCESS; +} + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcM= ediaDeviceDxe/MMCSDTransfer.c b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluste= r/Sdio/Dxe/MMC/MmcMediaDeviceDxe/MMCSDTransfer.c new file mode 100644 index 0000000..13258de --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcMediaDev= iceDxe/MMCSDTransfer.c @@ -0,0 +1,2524 @@ +/** @file + SMMC transfer specific functions. + + Copyright (c) 1999 - 2017, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include +#include +#include +#include +#include +#include + + +/** + Set Mmc ExtCsd register + + @param[in] PeiCardData Pointer to CARD_DATA + @param[in] Index + @param[in] Value + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +MmcSetExtCsd8 ( + IN CARD_DATA *CardData, + IN UINT8 Index, + IN UINT8 Value + ); + +EFI_EVENT mSetEmmcWpOnEvent =3D NULL; + + +/** + Check card status, print the debug info and check the error + + @param[in] Status Status got from card status register + + @retval EFI_DEVICE_ERROR + @retval EFI_SUCCESS + +**/ +EFI_STATUS +CheckCardStatus ( + IN UINT32 Status + ) +{ + CARD_STATUS *CardStatus; + CardStatus =3D (CARD_STATUS *) (&Status); + + if (CardStatus->ADDRESS_OUT_OF_RANGE) { + DEBUG ((EFI_D_ERROR, "CardStatus: ADDRESS_OUT_OF_RANGE\n")); + } + + if (CardStatus->ADDRESS_MISALIGN) { + DEBUG ((EFI_D_ERROR, "CardStatus: ADDRESS_MISALIGN\n")); + } + + if (CardStatus->BLOCK_LEN_ERROR) { + DEBUG ((EFI_D_ERROR, "CardStatus: BLOCK_LEN_ERROR\n")); + } + + if (CardStatus->ERASE_SEQ_ERROR) { + DEBUG ((EFI_D_ERROR, "CardStatus: ERASE_SEQ_ERROR\n")); + } + + if (CardStatus->ERASE_PARAM) { + DEBUG ((EFI_D_ERROR, "CardStatus: ERASE_PARAM\n")); + } + + if (CardStatus->WP_VIOLATION) { + DEBUG ((EFI_D_ERROR, "CardStatus: WP_VIOLATION\n")); + } + + if (CardStatus->CARD_IS_LOCKED) { + DEBUG ((EFI_D_ERROR, "CardStatus: CARD_IS_LOCKED\n")); + } + + if (CardStatus->LOCK_UNLOCK_FAILED) { + DEBUG ((EFI_D_ERROR, "CardStatus: LOCK_UNLOCK_FAILED\n")); + } + + if (CardStatus->COM_CRC_ERROR) { + DEBUG ((EFI_D_ERROR, "CardStatus: COM_CRC_ERROR\n")); + } + + if (CardStatus->ILLEGAL_COMMAND) { + DEBUG ((EFI_D_ERROR, "CardStatus: ILLEGAL_COMMAND\n")); + } + + if (CardStatus->CARD_ECC_FAILED) { + DEBUG ((EFI_D_ERROR, "CardStatus: CARD_ECC_FAILED\n")); + } + + if (CardStatus->CC_ERROR) { + DEBUG ((EFI_D_ERROR, "CardStatus: CC_ERROR\n")); + } + + if (CardStatus->ERROR) { + DEBUG ((EFI_D_ERROR, "CardStatus: ERROR\n")); + } + + if (CardStatus->UNDERRUN) { + DEBUG ((EFI_D_ERROR, "CardStatus: UNDERRUN\n")); + } + + if (CardStatus->OVERRUN) { + DEBUG ((EFI_D_ERROR, "CardStatus: OVERRUN\n")); + } + + if (CardStatus->CID_CSD_OVERWRITE) { + DEBUG ((EFI_D_ERROR, "CardStatus: CID_CSD_OVERWRITE\n")); + } + + if (CardStatus->WP_ERASE_SKIP) { + DEBUG ((EFI_D_ERROR, "CardStatus: WP_ERASE_SKIP\n")); + } + + if (CardStatus->ERASE_RESET) { + DEBUG ((EFI_D_ERROR, "CardStatus: ERASE_RESET\n")); + } + + if (CardStatus->SWITCH_ERROR) { + DEBUG ((EFI_D_ERROR, "CardStatus: SWITCH_ERROR\n")); + } + + if ((Status & 0xFCFFA080) !=3D 0) { + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + + +/** + Send command by using Host IO protocol + + @param[in] This Pointer to EFI_MMC_HOST_IO_PROTOCOL + @param[in] CommandIndex The command index to set the command= index field of command register + @param[in] Argument Command argument to set the argument= field of command register + @param[in] DataType TRANSFER_TYPE, indicates no data, da= ta in or data out + @param[in] Buffer Contains the data read from / write = to the device + @param[in] BufferSize The size of the buffer + @param[in] ResponseType RESPONSE_TYPE + @param[in] TimeOut Time out value in 1 ms unit + @param[out] ResponseData Depending on the ResponseType, such = as CSD or card status + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_DEVICE_ERROR + @retval EFI_SUCCESS + +**/ +EFI_STATUS +SendCommand ( + IN EFI_MMC_HOST_IO_PROTOCOL *This, + IN UINT16 CommandIndex, + IN UINT32 Argument, + IN TRANSFER_TYPE DataType, + IN UINT8 *Buffer, OPTIONAL + IN UINT32 BufferSize, + IN RESPONSE_TYPE ResponseType, + IN UINT32 TimeOut, + OUT UINT32 *ResponseData + ) +{ + + EFI_STATUS Status; + + Status =3D This->SendCommand ( + This, + CommandIndex, + Argument, + DataType, + Buffer, + BufferSize, + ResponseType, + TimeOut, + ResponseData + ); + if (!EFI_ERROR (Status)) { + if (ResponseType =3D=3D ResponseR1 || ResponseType =3D=3D ResponseR1b)= { + if (ResponseData =3D=3D NULL) { + ASSERT(ResponseData !=3D NULL); + return EFI_OUT_OF_RESOURCES; + } + Status =3D CheckCardStatus (*ResponseData); + } + } else { + This->ResetMmcHost (This, Reset_DAT_CMD); + } + + return Status; +} + + +/** + Send the card APP_CMD command with the following command indicated + by CommandIndex + + @param[in] CardData Pointer to CARD_DATA + @param[in] CommandIndex The command index to set the comman= d index field of command register + @param[in] Argument Command argument to set the argumen= t field of command register + @param[in] DataType TRANSFER_TYPE, indicates no data, d= ata in or data out + @param[in] Buffer Contains the data read from / write= to the device + @param[in] BufferSize The size of the buffer + @param[in] ResponseType RESPONSE_TYPE + @param[in] TimeOut Time out value in 1 ms unit + @param[out] ResponseData Depending on the ResponseType, such= as CSD or card status + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_DEVICE_ERROR + @retval EFI_SUCCESS + +**/ +EFI_STATUS +SendAppCommand ( + IN CARD_DATA *CardData, + IN UINT16 CommandIndex, + IN UINT32 Argument, + IN TRANSFER_TYPE DataType, + IN UINT8 *Buffer, OPTIONAL + IN UINT32 BufferSize, + IN RESPONSE_TYPE ResponseType, + IN UINT32 TimeOut, + OUT UINT32 *ResponseData + ) +{ + + EFI_STATUS Status; + EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo; + UINT8 Index; + + MmcHostIo =3D CardData->MmcHostIo; + Status =3D EFI_SUCCESS; + + for (Index =3D 0; Index < 2; Index++) { + Status =3D MmcHostIo->SendCommand ( + MmcHostIo, + APP_CMD, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + if (!EFI_ERROR (Status)) { + Status =3D CheckCardStatus (*(UINT32 *) &(CardData->CardStatus)); + if (CardData->CardStatus.SAPP_CMD !=3D 1) { + Status =3D EFI_DEVICE_ERROR; + } + if (!EFI_ERROR (Status)) { + break; + } + } else { + MmcHostIo->ResetMmcHost (MmcHostIo, Reset_Auto); + } + } + + if (EFI_ERROR (Status)) { + return Status; + } + + Status =3D MmcHostIo->SendCommand ( + MmcHostIo, + CommandIndex, + Argument, + DataType, + Buffer, + BufferSize, + ResponseType, + TimeOut, + ResponseData + ); + if (!EFI_ERROR (Status)) { + if (ResponseType =3D=3D ResponseR1 || ResponseType =3D=3D ResponseR1b)= { + if (ResponseData =3D=3D NULL) { + ASSERT(ResponseData !=3D NULL); + return EFI_OUT_OF_RESOURCES; + } + Status =3D CheckCardStatus (*ResponseData); + } + } else { + MmcHostIo->ResetMmcHost (MmcHostIo, Reset_Auto); + } + + return Status; +} + + +/** + Dump Mmc Decode OCR register + + @param[in] OCRReg Pointer to OCR + + @retval None + +**/ +EFI_STATUS +MmcDecodeOCR ( + IN OCR *OCRReg + ) +{ + DEBUG ((DEBUG_INFO, "\n=3D=3D=3D=3D=3D=3D=3D=3D=3D=3DDECODE MMC OCR REGI= STER=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n")); + DEBUG ((DEBUG_INFO, " OCR =3D 0x%08X\n", *((UINT32 *) OCRReg))); + DEBUG ((DEBUG_INFO, " CARD_NOT_BUSY =3D 0x%X\n", OCRReg->Busy)); + DEBUG ((DEBUG_INFO, " ACCESS_MODE =3D 0x%X\n", OCRReg->AccessMode= )); + DEBUG ((DEBUG_INFO, " VDD_270_360 =3D 0x%X\n", OCRReg->V270_V360)= ); + DEBUG ((DEBUG_INFO, " VDD_200_260 =3D 0x%X\n", OCRReg->V200_V260)= ); + DEBUG ((DEBUG_INFO, " VDD_170_195 =3D 0x%X\n", OCRReg->V170_V195)= ); + DEBUG ((DEBUG_INFO, "=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D\n")); + return 0; +} + + +/** + Dump Mmc Decode CID value + + @param[in] CIDReg Pointer to CIDReg + + @retval None + +**/ +EFI_STATUS +MmcDecodeCID ( + IN CID *CIDReg + ) +{ + UINT32 i =3D 0; + + DEBUG ((EFI_D_INFO, "\n=3D=3D=3D=3D=3D=3D=3D=3D=3D=3DDECODE MMC CID REGI= STER=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n")); + DEBUG ((EFI_D_INFO, " CID =3D 0x%032X\n", CIDReg)); + DEBUG ((EFI_D_INFO, " MANUFACTURER_ID =3D 0x%X\n", CIDReg->MID)); + DEBUG ((EFI_D_INFO, " CARD_OR_BGA =3D 0x%X\n", (CIDReg->OID & 0x= FF00)>>6)); + DEBUG ((EFI_D_INFO, " OEM_APPLICATION_ID =3D 0x%X\n", (CIDReg->OID>>8) = &0xFF)); + DEBUG ((EFI_D_INFO, " PRODUCT_NAME =3D ")); + + for (i=3D0; i < 6; i++) { + DEBUG ((EFI_D_INFO, "%c", CIDReg->PNM[i])); + } + DEBUG ((EFI_D_INFO, "\n")); + DEBUG ((EFI_D_INFO, " PRODUCT_REVISION =3D 0x%X\n", CIDReg->PRV)); + DEBUG ((EFI_D_INFO, " PRODUCT_SERIAL_NUM =3D 0x%X\n", CIDReg->PSN)); + DEBUG ((EFI_D_INFO, " MANUFACTURE_DATE =3D 0x%X\n", CIDReg->MDT)); + DEBUG ((EFI_D_INFO, "=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D\n")); + return 0; +} + + +/** + Dump Mmc Decode CSD value + + @param[in] CSDReg Pointer to CSDReg + + @retval None + +**/ +EFI_STATUS +MmcDecodeCSD ( + IN CSD *CSDReg + ) +{ + DEBUG ((EFI_D_INFO, "\n=3D=3D=3D=3D=3D=3D=3D=3D=3D=3DDECODE MMC CSD REGI= STER=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n")); + DEBUG ((EFI_D_INFO, "csd_struct : [0x%0x] \n", CSDReg->CSD_STRUCT= URE)); + DEBUG ((EFI_D_INFO, "specs_ver : [0x%0x] \n", CSDReg->SPEC_VERS)= ); + DEBUG ((EFI_D_INFO, "reserve2 : [0x%0x] \n", CSDReg->Reserved2)= ); + DEBUG ((EFI_D_INFO, "taac : [0x%0x] \n", CSDReg->TAAC)); + DEBUG ((EFI_D_INFO, "nsac : [0x%0x] \n", CSDReg->NSAC)); + DEBUG ((EFI_D_INFO, "tran_speed : [0x%0x] \n", CSDReg->TRAN_SPEED= )); + DEBUG ((EFI_D_INFO, "ccc : [0x%0x] \n", CSDReg->CCC)); + DEBUG ((EFI_D_INFO, "read_bl_len : [0x%0x] \n", CSDReg->READ_BL_LE= N)); + DEBUG ((EFI_D_INFO, "read_partial : [0x%0x] \n", CSDReg->READ_BL_PA= RTIAL)); + DEBUG ((EFI_D_INFO, "write_misalign : [0x%0x] \n", CSDReg->WRITE_BLK_= MISALIGN)); + DEBUG ((EFI_D_INFO, "read_misalign : [0x%0x] \n", CSDReg->READ_BLK_M= ISALIGN)); + DEBUG ((EFI_D_INFO, "dsr_imp : [0x%0x] \n", CSDReg->DSR_IMP)); + DEBUG ((EFI_D_INFO, "reserve1 : [0x%0x] \n", CSDReg->Reserved1)= ); + DEBUG ((EFI_D_INFO, "c_size : [0x%0x] \n", CSDReg->C_SIZELow2= | CSDReg->C_SIZEHigh10<<2)); + DEBUG ((EFI_D_INFO, "vdd_r_curr_min : [0x%0x] \n", CSDReg->VDD_R_CURR= _MIN)); + DEBUG ((EFI_D_INFO, "vdd_r_curr_max : [0x%0x] \n", CSDReg->VDD_R_CURR= _MAX)); + DEBUG ((EFI_D_INFO, "vdd_w_curr_min : [0x%0x] \n", CSDReg->VDD_W_CURR= _MIN)); + DEBUG ((EFI_D_INFO, "vdd_w_curr_max : [0x%0x] \n", CSDReg->VDD_W_CURR= _MAX)); + DEBUG ((EFI_D_INFO, "c_size_mult : [0x%0x] \n", CSDReg->C_SIZE_MUL= T)); + DEBUG ((EFI_D_INFO, "erase_grp_size : [0x%0x] \n", CSDReg->ERASE_GRP_= SIZE)); + DEBUG ((EFI_D_INFO, "erase_grp_mult : [0x%0x] \n", CSDReg->ERASE_GRP_= MULT)); + DEBUG ((EFI_D_INFO, "wp_grp_size : [0x%0x] \n", CSDReg->WP_GRP_SIZ= E)); + DEBUG ((EFI_D_INFO, "wp_grp_enable : [0x%0x] \n", CSDReg->WP_GRP_ENA= BLE)); + DEBUG ((EFI_D_INFO, "default_ecc : [0x%0x] \n", CSDReg->DEFAULT_EC= C)); + DEBUG ((EFI_D_INFO, "r2w_factor : [0x%0x] \n", CSDReg->R2W_FACTOR= )); + DEBUG ((EFI_D_INFO, "write_bl_len : [0x%0x] \n", CSDReg->WRITE_BL_L= EN)); + DEBUG ((EFI_D_INFO, "write_partial : [0x%0x] \n", CSDReg->WRITE_BL_P= ARTIAL)); + DEBUG ((EFI_D_INFO, "reserve0 : [0x%0x] \n", CSDReg->Reserved0)= ); + DEBUG ((EFI_D_INFO, "content_prot_app : [0x%0x] \n", CSDReg->CONTENT_PR= OT_APP)); + DEBUG ((EFI_D_INFO, "file_format_grp : [0x%0x] \n", CSDReg->FILE_FORMA= T_GRP)); + DEBUG ((EFI_D_INFO, "copy : [0x%0x] \n", CSDReg->COPY)); + DEBUG ((EFI_D_INFO, "perm_write_protect: [0x%0x] \n", CSDReg->PERM_WRITE= _PROTECT)); + DEBUG ((EFI_D_INFO, "tmp_write_prot : [0x%0x] \n", CSDReg->TMP_WRITE_= PROTECT)); + DEBUG ((EFI_D_INFO, "file_format : [0x%0x] \n", CSDReg->FILE_FORMA= T)); + DEBUG ((EFI_D_INFO, "ecc : [0x%0x] \n", CSDReg->ECC)); + DEBUG ((EFI_D_INFO, "=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D\n")); + + return 0; +} + + +/** + Dump Mmc Decode Ext CSD value + + @param[in] ExtCSDReg Pointer to ExtCSDReg + + @retval None + +**/ +EFI_STATUS +MmcDecodeExtCSD ( + IN EXT_CSD *ExtCSDReg + ) +{ + DEBUG ((EFI_D_INFO, "\n=3D=3D=3D=3D=3D=3D=3D=3D=3D=3DDECODE MMC EXT CSD = REGISTER=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n")); + DEBUG ((EFI_D_INFO, " SUPPORTED_CMD_SETS =3D 0x%X\n", ExtCSDReg->= CMD_SET)); + DEBUG ((EFI_D_INFO, " HPI_FEATURES =3D 0x%X\n", ExtCSDReg->= HPI_FEATURES)); + DEBUG ((EFI_D_INFO, " BKOPS_SUPPORT =3D 0x%X\n", ExtCSDReg->= BKOPS_SUPPORT)); + DEBUG ((EFI_D_INFO, " BKOPS_STATUS =3D 0x%X\n", ExtCSDReg->= BKOPS_STATUS)); + DEBUG ((EFI_D_INFO, " CORRECTLY_PRG_SECTORS_NUM =3D 0x%X%X%X%X\n", ExtCS= DReg->CORRECTLY_PRG_SECTORS_NUM[3], \ + ExtCSDReg->CORRECTLY_P= RG_SECTORS_NUM[2], ExtCSDReg->CORRECTLY_PRG_SECTORS_NUM[1], ExtCSDReg->CORR= ECTLY_PRG_SECTORS_NUM[0])); + DEBUG ((EFI_D_INFO, " INI_TIMEOUT_AP =3D 0x%X\n", ExtCSDReg->= INI_TIMEOUT_AP)); + DEBUG ((EFI_D_INFO, " PWR_CL_DDR_52_195 =3D 0x%X\n", ExtCSDReg->= PWR_CL_DDR_52_195)); + DEBUG ((EFI_D_INFO, " PWR_CL_DDR_52_360 =3D 0x%X\n", ExtCSDReg->= PWR_CL_DDR_52_360)); + DEBUG ((EFI_D_INFO, " MIN_PRF_DDR_W_8_52 =3D 0x%X\n", ExtCSDReg->= MIN_PERF_DDR_W_8_52)); + DEBUG ((EFI_D_INFO, " MIN_PRF_DDR_R_8_52 =3D 0x%X\n", ExtCSDReg->= MIN_PERF_DDR_R_8_52)); + DEBUG ((EFI_D_INFO, " TRIM_MULT =3D 0x%X\n", ExtCSDReg->= TRIM_MULT)); + DEBUG ((EFI_D_INFO, " SEC_FEATURE_SUPP =3D 0x%X\n", ExtCSDReg->= SEC_FEATURE_SUPPORT)); + DEBUG ((EFI_D_INFO, " SEC_ERASE_MULT =3D 0x%X\n", ExtCSDReg->= SEC_ERASE_MULT)); + DEBUG ((EFI_D_INFO, " SEC_TRIM_MULT =3D 0x%X\n", ExtCSDReg->= SEC_TRIM_MULT)); + DEBUG ((EFI_D_INFO, " BOOT_INFO =3D 0x%X\n", ExtCSDReg->= BOOT_INFO)); + DEBUG ((EFI_D_INFO, " BOOT_PART_SIZE =3D 0x%X\n", ExtCSDReg->= BOOT_SIZE_MULTI)); + DEBUG ((EFI_D_INFO, " ACCESS_SIZE =3D 0x%X\n", ExtCSDReg->= ACC_SIZE)); + DEBUG ((EFI_D_INFO, " HI_CAP_ER_GRP_SIZE =3D 0x%X\n", ExtCSDReg->= HC_ERASE_GRP_SIZE)); + DEBUG ((EFI_D_INFO, " HI_CAP_ER_TIMEOUT =3D 0x%X\n", ExtCSDReg->= ERASE_TIMEOUT_MULT)); + DEBUG ((EFI_D_INFO, " REL_WR_SECTOR_CNT =3D 0x%X\n", ExtCSDReg->= REL_WR_SEC_C)); + DEBUG ((EFI_D_INFO, " HI_CAP_WP_GRP_SIZE =3D 0x%X\n", ExtCSDReg->= HC_WP_GRP_SIZE)); + DEBUG ((EFI_D_INFO, " SLEEP_CURRENT_VCC =3D 0x%X\n", ExtCSDReg->= S_C_VCC)); + DEBUG ((EFI_D_INFO, " SLEEP_CURRENT_VCCQ =3D 0x%X\n", ExtCSDReg->= S_C_VCCQ)); + DEBUG ((EFI_D_INFO, " SLP_AWK_TIMEOUT =3D 0x%X\n", ExtCSDReg->= S_A_TIMEOUT)); + DEBUG ((EFI_D_INFO, " SECTOR_COUNT =3D 0x%X\n", *(UINT32*) = ((UINT8 *) &ExtCSDReg->SEC_COUNT))); + DEBUG ((EFI_D_INFO, " MIN_PERF_W_8_52 =3D 0x%X\n", ExtCSDReg->= MIN_PERF_W_8_52)); + DEBUG ((EFI_D_INFO, " MIN_PERF_R_8_52 =3D 0x%X\n", ExtCSDReg->= MIN_PERF_R_8_52)); + DEBUG ((EFI_D_INFO, " MIN_PERF_W_8_26_4_52 =3D 0x%X\n", ExtCSDReg->= MIN_PERF_W_8_26_4_52)); + DEBUG ((EFI_D_INFO, " MIN_PERF_W_8_26_4_52 =3D 0x%X\n", ExtCSDReg->= MIN_PERF_W_8_26_4_52)); + DEBUG ((EFI_D_INFO, " MIN_PERF_W_4_26 =3D 0x%X\n", ExtCSDReg->= MIN_PERF_W_4_26)); + DEBUG ((EFI_D_INFO, " MIN_PERF_R_4_26 =3D 0x%X\n", ExtCSDReg->= MIN_PERF_R_4_26)); + DEBUG ((EFI_D_INFO, " PWR_CLASS_26_360 =3D 0x%X\n", ExtCSDReg->= PWR_CL_26_360)); + DEBUG ((EFI_D_INFO, " PWR_CLASS_52_360 =3D 0x%X\n", ExtCSDReg->= PWR_CL_52_360)); + DEBUG ((EFI_D_INFO, " PWR_CLASS_26_195 =3D 0x%X\n", ExtCSDReg->= PWR_CL_26_195)); + DEBUG ((EFI_D_INFO, " PWR_CLASS_52_195 =3D 0x%X\n", ExtCSDReg->= PWR_CL_52_195)); + DEBUG ((EFI_D_INFO, " PARTITION_SWITCH_TIME =3D 0x%X\n", ExtCSDReg->= PARTITION_SWITCH_TIME)); + DEBUG ((EFI_D_INFO, " OUT_OF_INTERRUPT_TIME =3D 0x%X\n", ExtCSDReg->= OUT_OF_INTERRUPT_TIME)); + DEBUG ((EFI_D_INFO, " CARD_TYPE =3D 0x%X\n", ExtCSDReg->= CARD_TYPE)); + DEBUG ((EFI_D_INFO, " CSD_STRUCTURE =3D 0x%X\n", ExtCSDReg->= CSD_STRUCTURE)); + DEBUG ((EFI_D_INFO, " EXT_CSD_REV =3D 0x%X\n", ExtCSDReg->= EXT_CSD_REV)); + DEBUG ((EFI_D_INFO, " CMD_SET =3D 0x%X\n", ExtCSDReg->= CMD_SET)); + DEBUG ((EFI_D_INFO, " CMD_SET_REV =3D 0x%X\n", ExtCSDReg->= CMD_SET_REV)); + DEBUG ((EFI_D_INFO, " PWR_CLASS =3D 0x%X\n", ExtCSDReg->= POWER_CLASS)); + DEBUG ((EFI_D_INFO, " HI_SPEED_TIMING =3D 0x%X\n", ExtCSDReg->= HS_TIMING)); + DEBUG ((EFI_D_INFO, " BUS_WIDTH_MODE =3D 0x%X\n", ExtCSDReg->= BUS_WIDTH)); + DEBUG ((EFI_D_INFO, " ERASED_MEM_CONTENT =3D 0x%X\n", ExtCSDReg->= ERASED_MEM_CONT)); + DEBUG ((EFI_D_INFO, " PARTITION_CONFIG =3D 0x%X\n", ExtCSDReg->= PARTITION_CONFIG)); + DEBUG ((EFI_D_INFO, " BOOT_CONFIG_PROT =3D 0x%X\n", ExtCSDReg->= BOOT_CONFIG_PROT)); + DEBUG ((EFI_D_INFO, " BOOT_BUS_WIDTH =3D 0x%X\n", ExtCSDReg->= BOOT_BUS_WIDTH)); + DEBUG ((EFI_D_INFO, " HI_DEN_ER_GRP_DEF =3D 0x%X\n", ExtCSDReg->= ERASE_GROUP_DEF)); + DEBUG ((EFI_D_INFO, " BOOT_WP =3D 0x%X\n", ExtCSDReg->= BOOT_WP)); + DEBUG ((EFI_D_INFO, " USER_WP =3D 0x%X\n", ExtCSDReg->= USER_WP)); + DEBUG ((EFI_D_INFO, " FW_CONFIG =3D 0x%X\n", ExtCSDReg->= FW_CONFIG)); + DEBUG ((EFI_D_INFO, " RPMB_SIZE_MULT =3D 0x%X\n", ExtCSDReg->= RPMB_SIZE_MULT)); + DEBUG ((EFI_D_INFO, " RST_N_FUNCTION =3D 0x%X\n", ExtCSDReg->= RST_n_FUNCTION)); + DEBUG ((EFI_D_INFO, " PARTITIONING_SUPP =3D 0x%X\n", ExtCSDReg->= PARTITIONING_SUPPORT)); + DEBUG ((EFI_D_INFO, " MAX_ENH_SIZE_MULT =3D 0x%02X%02X%02X\n", E= xtCSDReg->MAX_ENH_SIZE_MULT[2],ExtCSDReg->MAX_ENH_SIZE_MULT[1], ExtCSDReg->= MAX_ENH_SIZE_MULT[0])); + DEBUG ((EFI_D_INFO, " PART_ATTRIBUTE =3D 0x%X\n", ExtCSDReg->= PARTITIONS_ATTRIBUTES)); + DEBUG ((EFI_D_INFO, " PART_SETTING_COMP =3D 0x%X\n", ExtCSDReg->= PARTITION_SETTING_COMPLETED)); + DEBUG ((EFI_D_INFO, " GP_SIZE_MULT =3D 0x%02X%02X%02X%02X%0= 2X%02X%02X%02X%02X%02X%02X%02X\n", ExtCSDReg->GP_SIZE_MULT_4[2], ExtCSDReg-= >GP_SIZE_MULT_4[1], ExtCSDReg->GP_SIZE_MULT_4[0], + ExtCSDReg->GP_SIZE_MULT_= 3[2], ExtCSDReg->GP_SIZE_MULT_3[1], ExtCSDReg->GP_SIZE_MULT_3[0], + ExtCSDReg->GP_SIZE_MULT_= 2[2], ExtCSDReg->GP_SIZE_MULT_2[1], ExtCSDReg->GP_SIZE_MULT_2[0], + ExtCSDReg->GP_SIZE_MULT_= 1[2], ExtCSDReg->GP_SIZE_MULT_1[1], ExtCSDReg->GP_SIZE_MULT_1[0])); + DEBUG ((EFI_D_INFO, " ENH_SIZE_MULT =3D 0x%02X%02X%02X\n", E= xtCSDReg->ENH_SIZE_MULT[2], ExtCSDReg->ENH_SIZE_MULT[1], ExtCSDReg->ENH_SIZ= E_MULT[0])); + DEBUG ((EFI_D_INFO, " ENH_START_ADDR =3D 0x%02X%02X%02X%02X\n= ", ExtCSDReg->ENH_START_ADDR[3], ExtCSDReg->ENH_START_ADDR[2], ExtCSDReg->E= NH_START_ADDR[1], ExtCSDReg->ENH_START_ADDR[0])); + DEBUG ((EFI_D_INFO, " SEC_BAD_BLK_MGMNT =3D 0x%X\n", ExtCSDReg->= SEC_BAD_BLOCK_MGMNT)); + DEBUG ((EFI_D_INFO, "=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D\n")); + + return 0; +} + + +/** + Send the card FAST_IO command + + @param[in] CardData Pointer to CARD_DATA + @param[in] RegisterAddress Register Address + @param[in, out] RegisterData Pointer to register Data + @param[in] Write TRUE for write, FALSE for read + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_DEVICE_ERROR + @retval EFI_SUCCESS + +**/ +EFI_STATUS +FastIO ( + IN CARD_DATA *CardData, + IN UINT8 RegisterAddress, + IN OUT UINT8 *RegisterData, + IN BOOLEAN Write + ) +{ + EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo; + EFI_STATUS Status; + UINT32 Argument; + UINT32 Data; + + Status =3D EFI_SUCCESS; + MmcHostIo =3D CardData->MmcHostIo; + + if (RegisterData =3D=3D NULL) { + Status =3D EFI_INVALID_PARAMETER; + goto Exit; + } + + Argument =3D (CardData->Address << 16) | (RegisterAddress << 8); + if (Write) { + Argument |=3D BIT15 | (*RegisterData); + } + + Status =3D SendCommand ( + MmcHostIo, + FAST_IO, + Argument, + NoData, + NULL, + 0, + ResponseR4, + TIMEOUT_COMMAND, + &Data + ); + + if (EFI_ERROR (Status)) { + goto Exit; + } + + if ((Data & BIT15) =3D=3D 0) { + Status =3D EFI_DEVICE_ERROR; + goto Exit; + } + + if (!Write) { + *RegisterData =3D (UINT8)Data; + } + +Exit: + return Status; +} + + +/** + Send the card GO_INACTIVE_STATE command + + @param[in] CardData Pointer to CARD_DATA + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +PutCardInactive ( + IN CARD_DATA *CardData + ) +{ + EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo; + EFI_STATUS Status; + + MmcHostIo =3D CardData->MmcHostIo; + + Status =3D SendCommand ( + MmcHostIo, + GO_INACTIVE_STATE, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseNo, + TIMEOUT_COMMAND, + NULL + ); + + gBS->Stall(1000); + + return Status; +} + + +/** + Get card interested information for CSD rergister + + @param[in] CardData Pointer to CARD_DATA + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +CalculateCardParameter ( + IN CARD_DATA *CardData + ) +{ + EFI_STATUS Status; + UINT32 Frequency; + UINT32 Multiple; + UINT32 CSize; + CSD_SDV2 *CsdSDV2; + + Status =3D EFI_SUCCESS; + + switch (CardData->CSDRegister.TRAN_SPEED & 0x7) { + case 0: + Frequency =3D 100 * 1000; + break; + + case 1: + Frequency =3D 1 * 1000 * 1000; + break; + + case 2: + Frequency =3D 10 * 1000 * 1000; + break; + + case 3: + Frequency =3D 100 * 1000 * 1000; + break; + + default: + DEBUG ((EFI_D_ERROR, "Invalid CSD TRAN_SPEED Frequency: 0x%x\n", Car= dData->CSDRegister.TRAN_SPEED & 0x7)); + Status =3D EFI_INVALID_PARAMETER; + goto Exit; + } + + switch ((CardData->CSDRegister.TRAN_SPEED >> 3) & 0xF) { + case 1: + Multiple =3D 10; + break; + + case 2: + Multiple =3D 12; + break; + + case 3: + Multiple =3D 13; + break; + + case 4: + Multiple =3D 15; + break; + + case 5: + Multiple =3D 20; + break; + + case 6: + if (CardData->CardType =3D=3D MMCCard) { + Multiple =3D 26; + } else { + Multiple =3D 25; + } + break; + + case 7: + Multiple =3D 30; + break; + + case 8: + Multiple =3D 35; + break; + + case 9: + Multiple =3D 40; + break; + + case 10: + Multiple =3D 45; + break; + + case 11: + if (CardData->CardType =3D=3D MMCCard) { + Multiple =3D 52; + } else { + Multiple =3D 50; + } + break; + + case 12: + Multiple =3D 55; + break; + + case 13: + Multiple =3D 60; + break; + + case 14: + Multiple =3D 70; + break; + + case 15: + Multiple =3D 80; + break; + + default: + DEBUG ((EFI_D_ERROR, "CalculateCardParameter: Invalid CSD TRAN_SPEED= Multiple: 0x%x\n", CardData->CSDRegister.TRAN_SPEED >> 3)); + Status =3D EFI_INVALID_PARAMETER; + goto Exit; + } + + Frequency =3D Frequency * Multiple / 10; + CardData->MaxFrequency =3D Frequency; + + if ((CardData->ExtCSDRegister.CARD_TYPE & BIT2) || + (CardData->ExtCSDRegister.CARD_TYPE & BIT3)) { + CardData->BlockLen =3D 512; + } else { + CardData->BlockLen =3D 1 << CardData->CSDRegister.READ_BL_LEN; + } + + if (CardData->CardType =3D=3D SdMemoryCard2High) { + ASSERT(CardData->CSDRegister.CSD_STRUCTURE =3D=3D 1); + CsdSDV2 =3D (CSD_SDV2 *) &CardData->CSDRegister; + // + // the K here means 1024 not 1000 + // + CardData->BlockNumber =3D DivU64x32 (MultU64x32 (CsdSDV2->C_SIZE + 1, = 512 * 1024) , CardData->BlockLen); + } else { + // + // For MMC card > 2G, the block number will be recaculate later + // + CSize =3D CardData->CSDRegister.C_SIZELow2 | (CardData->CSDRegister.C_= SIZEHigh10 << 2); + CardData->BlockNumber =3D MultU64x32 (LShiftU64 (1, CardData->CSDRegis= ter.C_SIZE_MULT + 2), CSize + 1); + } + + // + //For >=3D 2G card, BlockLen may be 1024, but the transfer size is still= 512 bytes + // + if (CardData->BlockLen > 512) { + CardData->BlockNumber =3D DivU64x32 (MultU64x32 (CardData->BlockNumber= , CardData->BlockLen), 512); + CardData->BlockLen =3D 512; + } + + DEBUG ((DEBUG_INFO, "CalculateCardParameter: Card Size: 0x%lx\n", MultU6= 4x32 (CardData->BlockNumber, CardData->BlockLen) + )); + +Exit: + return Status; +} + + +/** + Test the bus width setting for MMC card + It is used only for verification purpose + + @param[in] CardData Pointer to CARD_DATA + @param[in] Width 1, 4, 8 bits + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +MMCCardBusWidthTest ( + IN CARD_DATA *CardData, + IN UINT32 Width + ) +{ + EFI_STATUS Status; + EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo; + UINT64 Data; + UINT64 Value; + + if (CardData =3D=3D NULL) { + ASSERT(CardData !=3D NULL); + return EFI_INVALID_PARAMETER; + } + + MmcHostIo =3D CardData->MmcHostIo; + Value =3D 0; + + switch (Width) { + case 1: + Data =3D 0x80; + break; + + case 4: + Data =3D 0x5A; + break; + + case 8: + Data =3D 0xAA55; + break; + + default: + Status =3D EFI_INVALID_PARAMETER; + goto Exit; + } + + CopyMem (CardData->AlignedBuffer, &Data, Width); + Status =3D SendCommand ( + MmcHostIo, + BUSTEST_W, + 0, + OutData, + CardData->AlignedBuffer, + Width, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + goto Exit; + } + + Data =3D 0; + gBS->Stall(10000); + + Status =3D SendCommand ( + MmcHostIo, + BUSTEST_R, + 0, + InData, + CardData->AlignedBuffer, + Width, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + goto Exit; + } + CopyMem (&Data, CardData->AlignedBuffer, Width); + + switch (Width) { + case 1: + Value =3D (~(Data ^ 0x80)) & 0xC0; + break; + case 4: + Value =3D (~(Data ^ 0x5A)) & 0xFF; + break; + case 8: + Value =3D (~(Data ^ 0xAA55)) & 0xFFFF; + break; + } + + if (Value =3D=3D 0) { + Status =3D EFI_SUCCESS; + } else { + Status =3D EFI_UNSUPPORTED; + } + +Exit: + return Status; +} + + +/** + This function can detect these card types + 1. MMC card + 2. SD 1.1 card + 3. SD 2.0 standard card + 3. SD 2.0 high capacity card + + @param[in] CardData Pointer to CARD_DATA + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +GetCardType ( + IN CARD_DATA *CardData + ) +{ + EFI_STATUS Status; + EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo; + UINT32 TimeOut=3D5000; + MmcHostIo =3D CardData->MmcHostIo; + + CardData->CardType =3D MMCCard; + MmcHostIo->SetupDevice (MmcHostIo); + + // + // To bring back the normal MMC card to work + // after sending the SD command. Otherwise some + // card could not work + // + Status =3D SendCommand ( + MmcHostIo, + GO_IDLE_STATE, + 0, + NoData, + NULL, + 0, + ResponseNo, + TIMEOUT_COMMAND, + NULL + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "GO_IDLE_STATE Fail Status =3D 0x%x\n", Status)); + } + + DEBUG (( DEBUG_INFO, "Sending first CMD1.\n")); + + // + // CE-ATA device needs long delay + // + gBS->Stall (1 * 1000); + + // + // Get OCR register to check voltage support, first time the OCR is 0 + // + DEBUG (( DEBUG_INFO, "Sending first CMD1 with 0 .\n")); + Status =3D SendCommand ( + MmcHostIo, + SEND_OP_COND, + 0x0, + NoData, + NULL, + 0, + ResponseR3, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->OCRRegister) + ); + + DEBUG ((DEBUG_INFO, "Check OCR register for busy 0x%x\n", *((UINT32 *) &= CardData->OCRRegister ))); + + gBS->Stall (1 * 1000); + + while (CardData->OCRRegister.Busy !=3D 1) { + CardData->OCRRegister.AccessMode =3D 2; + Status =3D SendCommand ( + MmcHostIo, + SEND_OP_COND, + *(UINT32 *) &(CardData->OCRRegister), + NoData, + NULL, + 0, + ResponseR3, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->OCRRegister) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SEND_OP_COND Fail Status =3D 0x%x\n", Status)); + goto Exit; + } + + gBS->Stall (1 * 1000); + TimeOut--; + if (TimeOut =3D=3D 0) { + DEBUG ((EFI_D_ERROR, "Card is always in busy state\n")); + Status =3D EFI_TIMEOUT; + goto Exit; + } + } + +Exit: + return Status; +} + + +/** + MMC card high/low voltage selection function + + @param[in] CardData Pointer to CARD_DATA + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + @retval EFI_BAD_BUFFER_SIZE + +**/ +EFI_STATUS +MMCCardVoltageSelection ( + IN CARD_DATA *CardData + ) +{ + EFI_STATUS Status; + EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo; + UINT8 Index; + UINT8 Retry; + UINT32 TimeOut; + + MmcHostIo =3D CardData->MmcHostIo; + Status =3D EFI_SUCCESS; + + if (FALSE) { + // + //First try the high voltage, then if supported choose the low voltage + // + for (Index =3D 0; Index < 2; Index++) { + for (Retry =3D 0; Retry < 3; Retry++) { + // + // To bring back the normal MMC card to work + // after sending the SD command. Otherwise some + // card could not work + // + Status =3D SendCommand ( + MmcHostIo, + GO_IDLE_STATE, + 0, + NoData, + NULL, + 0, + ResponseNo, + TIMEOUT_COMMAND, + NULL + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "GO_IDLE_STATE Fail Status =3D 0x%x\n", Sta= tus)); + continue; + } + // + //CE-ATA device needs long delay + // + gBS->Stall ((Retry + 1) * 50 * 1000); + + // + //Get OCR register to check voltage support, first time the OCR is= 0 + // + Status =3D SendCommand ( + MmcHostIo, + SEND_OP_COND, + 0, + NoData, + NULL, + 0, + ResponseR3, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->OCRRegister) + ); + + if (!EFI_ERROR (Status)) { + break; + } + } + + if (Retry =3D=3D 3) { + DEBUG ((EFI_D_ERROR, "SEND_OP_COND Fail Status =3D 0x%x\n", Status= )); + Status =3D EFI_DEVICE_ERROR; + goto Exit; + } + + if (CardData->OCRRegister.V170_V195 =3D=3D 1) { + CardData->DualVoltage =3D TRUE; + } + if (CardData->OCRRegister.V270_V360 !=3D 0x1F && + CardData->OCRRegister.V200_V260 !=3D 0) { + DEBUG ((EFI_D_ERROR, "Incompatiable voltage device\n")); + PutCardInactive (CardData); + Status =3D EFI_INCOMPATIBLE_VERSION; + goto Exit; + } + + if (Index =3D=3D 0) { + // + // Choose the high voltage first + // + CardData->OCRRegister.V170_V195 =3D 0; + } else { + // + // Choose the low voltage + // + CardData->OCRRegister.V170_V195 =3D 1; + CardData->OCRRegister.V270_V360 =3D 0; + } + + // + // Set sector mode + // + CardData->OCRRegister.AccessMode |=3D 2; + + // + // TimeOut Value, 5000 * 100 * 1000 =3D 5 s + // + TimeOut =3D 5000; + + do { + Status =3D SendCommand ( + MmcHostIo, + SEND_OP_COND, + *(UINT32 *) &(CardData->OCRRegister), + NoData, + NULL, + 0, + ResponseR3, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->OCRRegister) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SEND_OP_COND Fail Status =3D 0x%x\n", Stat= us)); + goto Exit; + } + + gBS->Stall (1 * 1000); + TimeOut--; + if (TimeOut =3D=3D 0) { + DEBUG ((EFI_D_ERROR, "Card is always in busy state\n")); + Status =3D EFI_TIMEOUT; + goto Exit; + } + } while (CardData->OCRRegister.Busy !=3D 1); + + if (CardData->DualVoltage =3D=3D TRUE && MmcHostIo->HostCapability.V= 18Support =3D=3D TRUE) { + // + //Power Off the card and then power on into low voltage + // + MmcHostIo->SetHostVoltage (MmcHostIo, 0); + gBS->Stall (1 * 1000); + MmcHostIo->SetHostVoltage (MmcHostIo, 18); + } else { + // + //Not support the low voltage, exit + // + break; + } + } + } + +Exit: + return Status; +} + + +/** + This function set the bus and device width for MMC card + + @param[in] CardData Pointer to CARD_DATA + @param[in] BusWidth 1, 4, 8 bits + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +MMCCardSetBusWidth ( + IN CARD_DATA *CardData, + IN UINT8 BusWidth + ) +{ + EFI_STATUS Status; + EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo; + SWITCH_ARGUMENT SwitchArgument; + UINT8 Value; + + MmcHostIo =3D CardData->MmcHostIo; + Value =3D 0; + switch (BusWidth) { + case 28: //20 in 28 indicates DDR in 8 bit bus + Value =3D 6; + break; + + case 24: //20 in 24 indicates DDR in 4 bit bus + Value =3D 5; + break; + + case 8: + Value =3D 2; + break; + + case 4: + Value =3D 1; + break; + + case 1: + Value =3D 0; + break; + + default: + ASSERT (0); + } + + // + // HS_TIMING must be set to 0x1 before setting BUS_WIDTH for dual data r= ate operation (values 5 or 6) + // + if (Value =3D=3D 5 || Value =3D=3D 6 ) { + ZeroMem(&SwitchArgument, sizeof (SWITCH_ARGUMENT)); + SwitchArgument.CmdSet =3D 0; + SwitchArgument.Value =3D 0x1; + SwitchArgument.Index =3D (UINT32) ((UINTN) + (&(CardData->ExtCSDRegister.HS_TIMING)) - (UINTN) (&(CardData->ExtCSDR= egister))); + SwitchArgument.Access =3D WriteByte_Mode; + Status =3D SendCommand ( + MmcHostIo, + SWITCH, + *(UINT32 *) &SwitchArgument, + NoData, + NULL, + 0, + ResponseR1b, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (!EFI_ERROR (Status)) { + Status =3D SendCommand ( + MmcHostIo, + SEND_STATUS, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + } else { + DEBUG ((DEBUG_ERROR, "SWITCH Fail in HS Timing setting\n")); + } + } + + ZeroMem (&SwitchArgument, sizeof (SWITCH_ARGUMENT)); + SwitchArgument.CmdSet =3D 0; + SwitchArgument.Value =3D Value; + SwitchArgument.Index =3D (UINT32) ((UINTN) + (&(CardData->ExtCSDRegister.BUS_WIDTH)) - (UINTN= ) (&(CardData->ExtCSDRegister))); + SwitchArgument.Access =3D WriteByte_Mode; + Status =3D SendCommand ( + MmcHostIo, + SWITCH, + *(UINT32 *) &SwitchArgument, + NoData, + NULL, + 0, + ResponseR1b, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (!EFI_ERROR (Status)) { + Status =3D SendCommand ( + MmcHostIo, + SEND_STATUS, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SWITCH %d bits Fail\n", BusWidth)); + goto Exit; + } else { + if ((BusWidth =3D=3D 24) || (BusWidth =3D=3D 28)) { + Status =3D MmcHostIo->SetBusWidth (MmcHostIo, BusWidth - 20); + } else { + Status =3D MmcHostIo->SetBusWidth (MmcHostIo, BusWidth); + } + if (EFI_ERROR (Status)) { + goto Exit; + } + + gBS->Stall (5 * 1000); + } + } + if ((BusWidth =3D=3D 24) || (BusWidth =3D=3D 28)) { + goto Exit; + } else { + Status =3D MMCCardBusWidthTest (CardData, BusWidth); + } + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MMCCardBusWidthTest %d bit Fail\n", BusWidth)); + goto Exit; + } + + CardData->CurrentBusWidth =3D BusWidth; + +Exit: + return Status; +} + + +/** + MMC/SD card init function + + @param[in] CardData Pointer to CARD_DATA + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + @retval EFI_BAD_BUFFER_SIZE + +**/ +EFI_STATUS +MMCSDCardInit ( + IN CARD_DATA *CardData + ) +{ + EFI_STATUS Status; + EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo; + SWITCH_ARGUMENT SwitchArgument; + UINT32 Data; + UINT32 Argument; + UINT32 HsTimingValue; + UINT8 PowerValue; + UINT8 DoubleSpeed; + UINTN Offset; + + if (CardData =3D=3D NULL) { + ASSERT (CardData !=3D NULL); + return EFI_INVALID_PARAMETER; + } + MmcHostIo =3D CardData->MmcHostIo; + CardData->CurrentBusWidth =3D 1; + + Status =3D GetCardType (CardData); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "GetCardType -> %r\n", Status)); + goto Exit; + } + + ASSERT (CardData->CardType !=3D UnknownCard); + // + //MMC, SD card need host auto stop command support + // + MmcHostIo->EnableAutoStopCmd (MmcHostIo, TRUE); + + + // + // Get CID Register, but the info is not used currently + // + Status =3D SendCommand ( + MmcHostIo, + ALL_SEND_CID, + 0, + NoData, + NULL, + 0, + ResponseR2, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CIDRegister) + ); + + PcdSet8S (PcdEmmcManufacturerId, CardData->CIDRegister.MID); + PcdSet32S (PcdProductSerialNumber, CardData->CIDRegister.PSN); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "ALL_SEND_CID -> %r\n", Status)); + goto Exit; + } + + // + //SET_RELATIVE_ADDR + // + if (CardData->CardType =3D=3D MMCCard) { + CardData->Address =3D 1; + // + // Set RCA Register + // + Status =3D SendCommand ( + MmcHostIo, + SET_RELATIVE_ADDR, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SET_RELATIVE_ADDR -> %r\n", Status)); + goto Exit; + } + } + + // + // Get CSD Register + // + Status =3D SendCommand ( + MmcHostIo, + SEND_CSD, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR2, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CSDRegister) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SEND_CSD -> %r\n", Status)); + goto Exit; + } + + MmcDecodeCSD(&CardData->CSDRegister); + + Status =3D CalculateCardParameter (CardData); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "CalculateCardParameter -> %r\n", Status)); + goto Exit; + } + + // + // Put the card into tran state + // + Status =3D SendCommand ( + MmcHostIo, + SELECT_DESELECT_CARD, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SELECT_DESELECT_CARD -> %r\n", Status)); + goto Exit; + } + + Status =3D SendCommand ( + MmcHostIo, + SEND_STATUS, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SELECT_DESELECT_CARD SEND_STATUS Fail Status =3D= 0x%x\n", Status)); + goto Exit; + } + + if (CardData->CardType =3D=3D MMCCard) { + // + // Only V4.0 and above supports more than 1 bits and high speed + // + if (CardData->CSDRegister.SPEC_VERS >=3D 4) { + Offset =3D OFFSET_OF (EXT_CSD, ERASE_GROUP_DEF); + Status =3D MmcSetExtCsd8 (CardData, (UINT8) Offset, 0x01); + // + // Get ExtCSDRegister + // + Status =3D SendCommand ( + MmcHostIo, + SEND_EXT_CSD, + 0, + InData, + CardData->AlignedBuffer, + sizeof (EXT_CSD), + ResponseR1, + TIMEOUT_DATA, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SEND_EXT_CSD -> %r\n", Status)); + goto Exit; + } + + CopyMem (&(CardData->ExtCSDRegister), CardData->AlignedBuffer, sizeo= f (EXT_CSD)); + + MmcDecodeExtCSD (&CardData->ExtCSDRegister); + + // + // Recaculate the block number for >2G MMC card + // + Data =3D (CardData->ExtCSDRegister.SEC_COUNT[0]) | + (CardData->ExtCSDRegister.SEC_COUNT[1] << 8) | + (CardData->ExtCSDRegister.SEC_COUNT[2] << 16) | + (CardData->ExtCSDRegister.SEC_COUNT[3] << 24); + + if (Data !=3D 0) { + CardData->BlockNumber =3D Data; + } + DEBUG ((DEBUG_INFO, "CardData->BlockNumber %d\n", Data)); + + // + // Check the DDR setting + // + DoubleSpeed =3D 0 ; + DEBUG ((DEBUG_INFO, "CardData->ExtCSDRegister.CARD_TYPE -> %d\n", (U= INTN) CardData->ExtCSDRegister.CARD_TYPE)); + if (CardData->ExtCSDRegister.CARD_TYPE & BIT0) { + DEBUG ((DEBUG_INFO, "High-Speed e MMC @ 26MHz - at rated device vo= ltage(s) supported\n")); + } + if (CardData->ExtCSDRegister.CARD_TYPE & BIT1) { + DEBUG ((DEBUG_INFO, "High-Speed e MMC @ 52MHz - at rated device vo= ltage(s) supported\n")); + } + if (CardData->ExtCSDRegister.CARD_TYPE & BIT2) { + DEBUG ((DEBUG_INFO, "High-Speed Dual Data Rate e MMC @ 52MHz - 1.8= V or 3V I/O supported\n")); + } + if (CardData->ExtCSDRegister.CARD_TYPE & BIT3) { + DEBUG ((DEBUG_INFO, "High-Speed Dual Data Rate e MMC @ 52MHz - 1.2= V I/O supported\n")); + } + if (CardData->ExtCSDRegister.CARD_TYPE & BIT4) { + DEBUG ((DEBUG_INFO, "HS200 Single Data Rate e MMC @ 200 MHz - 1.8V= I/O supported\n")); + } + if (CardData->ExtCSDRegister.CARD_TYPE & BIT5) { + DEBUG ((DEBUG_INFO, "HS200 Single Data Rate e MMC @ 200 MHz - 1.2V= I/O supported\n")); + } + if (CardData->ExtCSDRegister.CARD_TYPE & BIT6) { + DEBUG ((DEBUG_INFO, "HS400 Dual Data Rate e MMC @ 200MHz - 1.8V I/= O supported\n")); + } + if (CardData->ExtCSDRegister.CARD_TYPE & BIT7) { + DEBUG ((DEBUG_INFO, "HS400 Dual Data Rate e MMC @ 200MHz - 1.2V I/= O supported\n")); + } + + if ((CardData->ExtCSDRegister.CARD_TYPE & BIT2) || (CardData->ExtCSD= Register.CARD_TYPE & BIT3)) { + DEBUG ((DEBUG_INFO, "Card support DDR\n")); + DoubleSpeed =3D 20; //Add 20 for double speed, decoded in MMCCar= dSetBusWidth() + } + + DEBUG ((EFI_D_INFO, "%a(%d): %a() no more FPGA doublespeed workaroun= d needed\n", __FILE__, __LINE__, __FUNCTION__)); + + if (MmcHostIo->HostCapability.HighSpeedSupport) { + + HsTimingValue =3D 1; + + // + // Change to high frequency mode + // + ZeroMem (&SwitchArgument, sizeof (SWITCH_ARGUMENT)); + SwitchArgument.CmdSet =3D 0; + SwitchArgument.Value =3D HsTimingValue; + SwitchArgument.Index =3D (UINT32) ((UINTN) + (&(CardData->ExtCSDRegister.HS_TIMING)) - = (UINTN) (&(CardData->ExtCSDRegister))); + SwitchArgument.Access =3D WriteByte_Mode; + Status =3D SendCommand ( + CardData->MmcHostIo, + SWITCH, + *(UINT32 *) &SwitchArgument, + NoData, + NULL, + 0, + ResponseR1b, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SWITCH frequency -> %r\n", Status)); + } + + if (!EFI_ERROR (Status)) { + Status =3D SendCommand ( + MmcHostIo, + SEND_STATUS, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (!EFI_ERROR (Status)) { + // + // Enable the high speed mode + // + if (DoubleSpeed !=3D 0) { + Status =3D MmcHostIo->SetHostDdrMode (MmcHostIo, TRUE); + DEBUG ((DEBUG_INFO, "Set to DDR50 mode \n", Status)); + } else { + Status =3D MmcHostIo->SetHostSpeedMode (MmcHostIo, 1); + DEBUG ((DEBUG_ERROR, "Set to HS mode \n", Status)); + } + + if (EFI_ERROR (Status)) { + goto Exit; + } + // + // Change host clock + // + if ((CardData->ExtCSDRegister.CARD_TYPE & BIT1) !=3D 0) { + Status =3D MmcHostIo->SetClockFrequency (MmcHostIo, FREQUENC= Y_MMC_PP_HIGH); + } else if ((CardData->ExtCSDRegister.CARD_TYPE & BIT0) !=3D 0)= { + Status =3D MmcHostIo->SetClockFrequency (MmcHostIo, FREQUENC= Y_MMC_PP); + } else { + Status =3D EFI_UNSUPPORTED; + } + + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // It seems no need to stall after changing bus freqeuncy. + // It is said that the freqeuncy can be changed at any time. J= ust appends 8 clocks after command. + // But SetClock alreay has delay. + // + } + } + } + + // + // Prefer wide bus width for performance + // + // + // Set to BusWidth bits mode, only version 4.0 or above support more= than 1 bits + // + if (MmcHostIo->HostCapability.BusWidth8 =3D=3D 1) { + Status =3D MMCCardSetBusWidth (CardData, DoubleSpeed + 8); + if (EFI_ERROR (Status)) { + // + // CE-ATA may support 8 bits and 4 bits, but has no software met= hod for detection + // + Status =3D MMCCardSetBusWidth (CardData, DoubleSpeed + 4); + if (EFI_ERROR (Status)) { + goto Exit; + } + } + } else if (MmcHostIo->HostCapability.BusWidth4 =3D=3D 1) { + Status =3D MMCCardSetBusWidth (CardData, DoubleSpeed + 4); + if (EFI_ERROR (Status)) { + goto Exit; + } + } + + PowerValue =3D 0; + + if (CardData->CurrentBusWidth =3D=3D 8) { + if ((CardData->ExtCSDRegister.CARD_TYPE & BIT1) !=3D 0) { + PowerValue =3D CardData->ExtCSDRegister.PWR_CL_52_360; + PowerValue =3D PowerValue >> 4; + } else if ((CardData->ExtCSDRegister.CARD_TYPE & BIT0) !=3D 0) { + PowerValue =3D CardData->ExtCSDRegister.PWR_CL_26_360; + PowerValue =3D PowerValue >> 4; + } + } else if (CardData->CurrentBusWidth =3D=3D 4) { + if ((CardData->ExtCSDRegister.CARD_TYPE & BIT1) !=3D 0) { + PowerValue =3D CardData->ExtCSDRegister.PWR_CL_52_360; + PowerValue =3D PowerValue & 0xF; + } else if ((CardData->ExtCSDRegister.CARD_TYPE & BIT0) !=3D 0) { + PowerValue =3D CardData->ExtCSDRegister.PWR_CL_26_360; + PowerValue =3D PowerValue & 0xF; + } + } + + if (PowerValue !=3D 0) { + // + // Update Power Class + // + ZeroMem (&SwitchArgument, sizeof (SWITCH_ARGUMENT)); + SwitchArgument.CmdSet =3D 0; + SwitchArgument.Value =3D PowerValue; + SwitchArgument.Index =3D (UINT32) ((UINTN) + (&(CardData->ExtCSDRegister.POWER_CLASS)) = - (UINTN) (&(CardData->ExtCSDRegister))); + SwitchArgument.Access =3D WriteByte_Mode; + Status =3D SendCommand ( + MmcHostIo, + SWITCH, + *(UINT32 *) &SwitchArgument, + NoData, + NULL, + 0, + ResponseR1b, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (!EFI_ERROR (Status)) { + Status =3D SendCommand ( + MmcHostIo, + SEND_STATUS, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SWITCH Power Class -> %r\n", Status)); + } + + } + } + } else { + DEBUG ((EFI_D_ERROR, "MMC Card version %d only supportes 1 bits at l= ower transfer speed\n", CardData->CSDRegister.SPEC_VERS)); + } + + // + // Register for Ready to Boot event to enable Write protection + // + Status =3D gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + SetEmmcWpOnEvent, + CardData, + &gEfiEventReadyToBootGuid, + &mSetEmmcWpOnEvent + ); + + } else { + // + // Pin 1, at power up this line has a 50KOhm pull up enabled in the ca= rd. + // This pull-up should be disconnected by the user, during regular dat= a transfer, + // with SET_CLR_CARD_DETECT (ACMD42) command + // + Status =3D SendAppCommand ( + CardData, + SET_CLR_CARD_DETECT, + 0, + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SET_CLR_CARD_DETECT Fail -> %r\n", Status)); + goto Exit; + } + + // + // Set Bus Width to 4 + // + Status =3D SendAppCommand ( + CardData, + SET_BUS_WIDTH, + SD_BUS_WIDTH_4, + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SET_BUS_WIDTH 4 bits -> %r\n", Status)); + goto Exit; + } + + Status =3D MmcHostIo->SetBusWidth (MmcHostIo, 4); + if (EFI_ERROR (Status)) { + goto Exit; + } + CardData->CurrentBusWidth =3D 4; + + if ((MmcHostIo->HostCapability.HighSpeedSupport =3D=3D FALSE) || + ((CardData->CSDRegister.CCC & BIT10) !=3D BIT10)) { + // + // Host must support high speed + // Card must support Switch function + // + goto Exit; + } + + // + // Mode =3D 0, group 1, function 1, check operation + // + Argument =3D 0xFFFF01; + ZeroMem (&CardData->SwitchStatus, sizeof (SWITCH_STATUS)); + + Status =3D SendCommand ( + MmcHostIo, + SWITCH_FUNC, + Argument, + InData, + CardData->AlignedBuffer, + sizeof (SWITCH_STATUS), + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + goto Exit; + } + CopyMem (&(CardData->SwitchStatus), CardData->AlignedBuffer, sizeof (S= WITCH_STATUS)); + + if ((CardData->SwitchStatus.DataStructureVersion =3D=3D 0x0) || + ((CardData->SwitchStatus.Group1BusyStatus & BIT1) !=3D BIT1)) { + // + // 1. SD 1.1 card does not suppport busy bit + // 2. Ready state + // + // + // + // Mode =3D 1, group 1, function 1, BIT31 set means set mode + // + Argument =3D 0xFFFF01 | BIT31; + ZeroMem (&CardData->SwitchStatus, sizeof (SWITCH_STATUS)); + + Status =3D SendCommand ( + MmcHostIo, + SWITCH_FUNC, + Argument, + InData, + CardData->AlignedBuffer, + sizeof (SWITCH_STATUS), + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + goto Exit; + } + CopyMem (&(CardData->SwitchStatus), CardData->AlignedBuffer, sizeof = (SWITCH_STATUS)); + + if ((CardData->SwitchStatus.DataStructureVersion =3D=3D 0x0) || + ((CardData->SwitchStatus.Group1BusyStatus & BIT1) !=3D BIT1)) { + // + // 1. SD 1.1 card does not suppport busy bit + // 2. Ready state + // + // + // 8 clocks, (1/ 25M) * 8 =3D=3D> 320 us, so 1ms > 0.32 ms + // + gBS->Stall (1000); + } + } + } + if (!((CardData->ExtCSDRegister.CARD_TYPE & BIT2) || + (CardData->ExtCSDRegister.CARD_TYPE & BIT3))) { + // + // Set Block Length, to improve compatibility in case of some cards + // + Status =3D SendCommand ( + MmcHostIo, + SET_BLOCKLEN, + 512, + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SET_BLOCKLEN -> %r\n", Status)); + goto Exit; + } + } + MmcHostIo->SetBlockLength (MmcHostIo, 512); + +Exit: + if (Status) { + + } + + return Status; +} + + +/** + MmcSelect + + @param[in] CardData Pointer to CARD_DATA + @param[in] Select + + @retval EFI_STATUS + +**/ +EFI_STATUS +MmcSelect ( + IN CARD_DATA *CardData, + IN BOOLEAN Select + ) +{ + return SendCommand ( + CardData->MmcHostIo, + SELECT_DESELECT_CARD, + Select ? (CardData->Address << 16) : ~(CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); +} + + +/** + MmcSendSwitch + + @param[in] CardData Pointer to CARD_DATA + @param[in] SwitchArgument Pointer to SWITCH_ARGUMENT + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +MmcSendSwitch ( + IN CARD_DATA *CardData, + IN SWITCH_ARGUMENT *SwitchArgument + ) +{ + EFI_STATUS Status; + EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo; + + MmcHostIo =3D CardData->MmcHostIo; + + Status =3D SendCommand ( + MmcHostIo, + SWITCH, + *(UINT32 *) SwitchArgument, + NoData, + NULL, + 0, + ResponseR1b, + TIMEOUT_DATA, + (UINT32 *) &(CardData->CardStatus) + ); + + if (!EFI_ERROR (Status)) { + Status =3D SendCommand ( + MmcHostIo, + SEND_STATUS, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SWITCH FAILURE\n")); + } + } + + return Status; +} + + +/** + MmcUpdateCardStatus + + @param[in] CardData Pointer to CARD_DATA + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +MmcUpdateCardStatus ( + IN CARD_DATA *CardData + ) +{ + return SendCommand ( + CardData->MmcHostIo, + SEND_STATUS, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); +} + + +/** + MmcMoveToTranState + + @param[in] CardData Pointer to CARD_DATA + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +MmcMoveToTranState ( + IN CARD_DATA *CardData + ) +{ + EFI_STATUS Status; + + Status =3D EFI_SUCCESS; + + if (CardData->CardStatus.CURRENT_STATE !=3D Tran_STATE) { + // + // Put the card into tran state + // + Status =3D MmcSelect (CardData, TRUE); + MmcUpdateCardStatus (CardData); + } + + if (CardData->CardStatus.CURRENT_STATE !=3D Tran_STATE) { + DEBUG ((EFI_D_ERROR, "MmcMoveToTranState: Unable to put card into tran= state\n")); + return EFI_DEVICE_ERROR; + } + + return Status; +} + + +/** + MmcReadExtCsd + + @param[in,out] CardData Pointer to CARD_DATA + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +MmcReadExtCsd ( + IN OUT CARD_DATA *CardData + ) +{ + EFI_STATUS Status; + + Status =3D MmcMoveToTranState (CardData); + if (EFI_ERROR (Status)) { + return Status; + } + + Status =3D SendCommand ( + CardData->MmcHostIo, + SEND_EXT_CSD, + 0, + InData, + CardData->AlignedBuffer, + sizeof (EXT_CSD), + ResponseR1, + TIMEOUT_DATA, + (UINT32 *) &(CardData->CardStatus) + ); + + DEBUG ((EFI_D_INFO, "MmcReadExtCsd: SEND_EXT_CSD -> %r\n", Status)); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + CopyMem (&(CardData->ExtCSDRegister), CardData->AlignedBuffer, sizeof (E= XT_CSD)); + + return Status; +} + + +/** + MmcSetExtCsd8 + + @param[in] CardData Pointer to CARD_DATA + @param[in] Index + @param[in] Value + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +MmcSetExtCsd8 ( + IN CARD_DATA *CardData, + IN UINT8 Index, + IN UINT8 Value + ) +{ + EFI_STATUS Status; + SWITCH_ARGUMENT SwitchArgument; + + Status =3D MmcMoveToTranState (CardData); + if (EFI_ERROR (Status)) { + return Status; + } + + ZeroMem (&SwitchArgument, sizeof (SWITCH_ARGUMENT)); + SwitchArgument.CmdSet =3D 0; + SwitchArgument.Value =3D (UINT8) Value; + SwitchArgument.Index =3D (UINT8) Index; + SwitchArgument.Access =3D WriteByte_Mode; // SetBits_Mode; + + return MmcSendSwitch (CardData, &SwitchArgument); +} + + +/** + MmcSetExtCsd24 + + @param[in] CardData Pointer to CARD_DATA + @param[in] Index + @param[in] Value + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +MmcSetExtCsd24 ( + IN CARD_DATA *CardData, + IN UINT8 Index, + IN UINT32 Value + ) +{ + EFI_STATUS Status; + UINTN Loop; + + ASSERT ((Value & 0xff000000ULL) =3D=3D 0); + + for (Loop =3D 0; Loop < 3; Loop++) { + Status =3D MmcSetExtCsd8 (CardData, Index + (UINT8) Loop, Value & 0xff= ); + if (EFI_ERROR (Status)) { + return Status; + } + Value =3D Value >> 8; + } + + return Status; +} + + +/** + MmcGetExtCsd8 + + @param[in] CardData Pointer to CARD_DATA + @param[in] Offset + + @retval CSD Register value + +**/ +UINT32 +MmcGetExtCsd8 ( + IN CARD_DATA *CardData, + IN UINTN Offset + ) +{ + ASSERT (Offset < sizeof (CardData->ExtCSDRegister)); + return ((UINT8 *) &CardData->ExtCSDRegister)[Offset]; +} + + +/** + MmcGetExtCsd32 + + @param[in] CardData Pointer to CARD_DATA + @param[in] Offset + + @retval CSD Register value + +**/ +UINT32 +MmcGetExtCsd32 ( + IN CARD_DATA *CardData, + IN UINTN Offset + ) +{ + return *(UINT32 *) (((UINT8 *) &CardData->ExtCSDRegister) + Offset); +} + + +/** + MmcGetExtCsd24 + + @param[in] CardData Pointer to CARD_DATA + @param[in] Offset + + @retval CSD Register value + +**/ +UINT32 +MmcGetExtCsd24 ( + IN CARD_DATA *CardData, + IN UINTN Offset + ) +{ + return MmcGetExtCsd32 (CardData, Offset) & 0xffffff; +} + + +/** + MmcGetCurrentPartitionNum + + @param[in] CardData Pointer to CARD_DATA + + @retval Current Partition Num + +**/ +UINTN +MmcGetCurrentPartitionNum ( + IN CARD_DATA *CardData + ) +{ + return MmcGetExtCsd8 ( + CardData, + OFFSET_OF (EXT_CSD, PARTITION_CONFIG) + ) & 0x7; +} + + +/** + SetEmmcWpOnEvent + + @param[in] Event + @param[in] Context + + @retval None + +**/ +VOID +EFIAPI +SetEmmcWpOnEvent( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EFI_STATUS Status; + UINTN Offset; + CARD_DATA *CardData; + UINT8 TempData; + UINTN WriteProtectAddress; + UINTN WriteProtectSize; + UINTN WriteProtectGroupSize; + static BOOLEAN WriteProtectDone =3D FALSE; + HECI_FWS_REGISTER SecFwStatus; + SC_POLICY_HOB *ScPolicy; + EFI_PEI_HOB_POINTERS HobList; + SC_SCS_CONFIG *ScsConfig; + + DEBUG ((EFI_D_INFO, "eMMC Write Protection Config Checkpoint\n")); + + CardData =3D (CARD_DATA *) Context; + // + // Enable the eMMC protection + // + if (FALSE) { + if (!WriteProtectDone) { + Status =3D MmcReadExtCsd (CardData); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_INFO, "Read EXT_CSD failed\n")); + } + DEBUG ((EFI_D_INFO, "\nBOOT_WP 0x%02x", (UINT8) (CardData->ExtCSDReg= ister.BOOT_WP))); + DEBUG ((EFI_D_INFO, "\nBOOT_WP_STATUS 0x%02x", (UINT8) (CardData->Ex= tCSDRegister.BOOT_WP_STATUS))); + DEBUG ((EFI_D_INFO, "\nUSER_WP 0x%02x", (UINT8) (CardData->ExtCSDReg= ister.USER_WP))); + DEBUG ((EFI_D_INFO, "\nRST_n_FUNCTION 0x%02x\n", (UINT8) (CardData->= ExtCSDRegister.RST_n_FUNCTION))); + + if (BxtStepping() >=3D BxtPB0) { //For BXTP-B and above, BIOS to tog= gle rst_n_function from 2'b00 to 2'b01 + if ((CardData->ExtCSDRegister.RST_n_FUNCTION & 0x3) !=3D 1) { + Offset =3D OFFSET_OF (EXT_CSD, RST_n_FUNCTION); + Status =3D MmcSetExtCsd8 (CardData, (UINT8) Offset, 0x01); + if (Status) { + DEBUG ((EFI_D_INFO, "Setting RST_n_FUNCTION failed\n")); + } + } + } + + // Protect and BP + // Update Power on write protection bit in USER_WP and BP_WP EXT_CSD= registers + // + Offset =3D OFFSET_OF (EXT_CSD, ERASE_GROUP_DEF); + Status =3D MmcSetExtCsd8 (CardData, (UINT8) Offset, 0x01); + if (Status) { + DEBUG ((EFI_D_INFO, "Setting ERASE_GROUP_DEF failed\n")); + } + + if (BxtStepping() !=3D BxtPA0) { // program WP only if it's not Bxtp= -A0 + Offset =3D OFFSET_OF (EXT_CSD, BOOT_WP); + TempData =3D (CardData->ExtCSDRegister.BOOT_WP) | B_PWR_WP_EN | B_= PERM_WP_DIS; + Status =3D MmcSetExtCsd8 (CardData, (UINT8) Offset, TempData); + if (Status) { + DEBUG ((EFI_D_INFO, "BP Write protect failed\n")); + } + } + + HobList.Guid =3D GetFirstGuidHob (&gScPolicyHobGuid); + ASSERT (HobList.Guid !=3D NULL); + ScPolicy =3D (SC_POLICY_HOB *) GET_GUID_HOB_DATA (HobList.Guid); + Status =3D GetConfigBlock ((VOID *) ScPolicy, &gScsConfigGuid, (VOID= *) &ScsConfig); + ASSERT_EFI_ERROR (Status); + + SecFwStatus.ul =3D HeciPciRead32 (R_SEC_FW_STS0); + + if (SecFwStatus.r.ManufacturingMode =3D=3D 0) { + DEBUG ((EFI_D_INFO, "SEC F/W is not in Manufacturing mode and is r= eady for production \n")); + } else if (SecFwStatus.r.SeCOperationMode !=3D SEC_OPERATION_MODE_SE= COVR_JMPR) { + DEBUG ((EFI_D_INFO, "Flash Descriptor Override HW strap not set\n"= )); + } + + if (((SecFwStatus.r.ManufacturingMode =3D=3D 0) = // if EOM is set + && (SecFwStatus.r.SeCOperationMode !=3D SEC_OPERATION_MODE_SECOV= R_JMPR)) // Flash Descriptor Override HW strap not set + || (ScsConfig->GppLock =3D=3D 1) = // OR GPP Partition Lock Policy is set + ) { + // + // Protect GPP1 + // + Status =3D MmcSelectPartitionNum (CardData, 4); // Switch to GPP1 = before issuing CMD28 + if (!EFI_ERROR(Status)) { + DEBUG ((EFI_D_INFO, "\nSwitch to GPP Partition Successful\n")); + WriteProtectSize =3D (((UINTN) (CardData->ExtCSDRegister.GP_SI= ZE_MULT_1[2] *( 0x1<<16))) + +((UINTN) (CardData->ExtCSDRegister.= GP_SIZE_MULT_1[1] *( 0x1<<8))) + +((UINTN) (CardData->ExtCSDRegister.= GP_SIZE_MULT_1[0]))) + * ((UINTN) (CardData->ExtCSDRegister= .HC_WP_GRP_SIZE)) + * 512 * 1024 * ((UINTN) (CardData->= ExtCSDRegister.HC_ERASE_GRP_SIZE)); + + DEBUG ((EFI_D_INFO, "WriteProtectSize =3D 0x%x\n", WriteProtect= Size)); + + Offset =3D OFFSET_OF (EXT_CSD, USER_WP); + TempData =3D (CardData->ExtCSDRegister.USER_WP) | US_PWR_WP_EN = | US_PERM_WP_DIS; + Status =3D MmcSetExtCsd8 (CardData, (UINT8) Offset, TempData); + if (Status) { + DEBUG ((EFI_D_INFO, "GPP Write protect failed\n")); + } + if (CardData->ExtCSDRegister.ERASE_GROUP_DEF) { + WriteProtectGroupSize =3D ((UINTN) (CardData->ExtCSDRegister.= HC_WP_GRP_SIZE)) + * 512 * 1024 * ((UINTN) (CardData= ->ExtCSDRegister.HC_ERASE_GRP_SIZE)); + } else { + WriteProtectGroupSize =3D (CardData->CSDRegister.WP_GRP_SIZE = + 1) + * ((UINTN) CardData->CSDRegister.E= RASE_GRP_SIZE + 1) + * ((UINTN) CardData->CSDRegister.E= RASE_GRP_MULT + 1) + * ((UINTN) (CardData->Partitions[4= ].BlockIoMedia.BlockSize)); + } + + DEBUG ((EFI_D_INFO, "WriteProtectGroupSize =3D 0x%x\n", WritePr= otectGroupSize)); + + for (WriteProtectAddress =3D 0; WriteProtectAddress < WriteProt= ectSize; WriteProtectAddress+=3DWriteProtectGroupSize) { + // + // Send Write protect command + // + DEBUG ((EFI_D_INFO, "Send Write protect command Address =3D 0= x%x\n", WriteProtectAddress)); + Status =3D SendCommand ( + CardData->MmcHostIo, + SET_WRITE_PROT, + (UINT32) (WriteProtectAddress / 0x200), + NoData, + NULL, + 0, + ResponseR1b, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (Status) { + DEBUG ((EFI_D_INFO, "GPP1 Write protect failed\n")); + break; + } + } + + } + + } + } + + Status =3D MmcReadExtCsd (CardData); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_INFO, "Read EXT_CSD failed\n")); + } + + DEBUG ((EFI_D_INFO, "\nBOOT_WP 0x%02x", (UINT8) (CardData->ExtCSDRegis= ter.BOOT_WP))); + DEBUG ((EFI_D_INFO, "\nBOOT_WP_STATUS 0x%02x", (UINT8) (CardData->ExtC= SDRegister.BOOT_WP_STATUS))); + DEBUG ((EFI_D_INFO, "\nUSER_WP 0x%02x", (UINT8) (CardData->ExtCSDRegis= ter.USER_WP))); + DEBUG ((EFI_D_INFO, "\nRST_n_FUNCTION 0x%02x\n", (UINT8) (CardData->Ex= tCSDRegister.RST_n_FUNCTION))); + WriteProtectDone =3D TRUE; + } +} + + +/** + MmcSelectPartitionNum + + @param[in] CardData Pointer to CARD_DATA + @param[in] Partition + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +MmcSelectPartitionNum ( + IN CARD_DATA *CardData, + IN UINT8 Partition + ) +{ + EFI_STATUS Status; + UINTN Offset; + UINT8 *ExtByte; + UINTN CurrentPartition; + + if (Partition > 7) { + return EFI_INVALID_PARAMETER; + } + + CurrentPartition =3D MmcGetCurrentPartitionNum (CardData); + if (Partition =3D=3D CurrentPartition) { + return EFI_SUCCESS; + } + MmcMoveToTranState (CardData); + Status =3D MmcReadExtCsd (CardData); + if (EFI_ERROR (Status)) { + return Status; + } + + DEBUG ((EFI_D_INFO, + "MmcSelectPartitionNum: Switch partition: %d =3D> %d\n", + CurrentPartition, + Partition + )); + + Offset =3D OFFSET_OF (EXT_CSD, PARTITION_CONFIG); + Status =3D MmcSetExtCsd8 (CardData, (UINT8) Offset, Partition); + +#if 1 + Status =3D MmcReadExtCsd (CardData); + if (EFI_ERROR (Status)) { + return Status; + } + + CurrentPartition =3D MmcGetCurrentPartitionNum (CardData); + if (Partition !=3D CurrentPartition) { + DEBUG ((EFI_D_INFO, "MmcSelectPartitionNum: Switch partition failed!\n= ")); + return EFI_DEVICE_ERROR; + } + + ExtByte =3D NULL; +#else + if (!EFI_ERROR (Status)) { + ExtByte =3D ((UINT8 *) &CardData->ExtCSDRegister) + Offset; + *ExtByte =3D (UINT8) ((*ExtByte & 0xF7) | Partition); + } +#endif + MmcMoveToTranState (CardData); + + return Status; +} + + +/** + MmcSelectPartitionNum + + @param[in] Partition Pointer to MMC_PARTITION_DATA + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +MmcSelectPartition ( + IN MMC_PARTITION_DATA *Partition + ) +{ + return MmcSelectPartitionNum ( + Partition->CardData, + (UINT8) CARD_DATA_PARTITION_NUM (Partition) + ); +} + + +/** + MmcSetPartition + + @param[in] CardData Pointer to CARD_DATA + @param[in] Value 0: user partition; 1: boot part= ition 1; 2:boot partition 2 + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +MmcSetPartition ( + IN CARD_DATA *CardData, + IN UINT8 Value + ) +{ + EFI_STATUS Status; + UINTN Offset; + UINT32 Data; + + Offset =3D OFFSET_OF (EXT_CSD, PARTITION_CONFIG); + Data =3D MmcGetExtCsd8 (CardData, Offset); + Data &=3D 0xf8; + Data |=3D Value; + Status =3D MmcSetExtCsd8 (CardData, (UINT8) Offset, (UINT8) Data); + + return Status; +} + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcM= ediaDeviceDxe/MediaDeviceDriver.c b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCl= uster/Sdio/Dxe/MMC/MmcMediaDeviceDxe/MediaDeviceDriver.c new file mode 100644 index 0000000..7f5bd11 --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcMediaDev= iceDxe/MediaDeviceDriver.c @@ -0,0 +1,605 @@ +/** @file + UEFI Driver Entry and Binding support. + + Copyright (c) 1999 - 2017, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include +#include + +// +// MMCSDIOController Driver Global Variables +// +EFI_DRIVER_BINDING_PROTOCOL gMediaDeviceDriverBinding =3D { + MediaDeviceDriverBindingSupported, + MediaDeviceDriverBindingStart, + MediaDeviceDriverBindingStop, + 0x10, + NULL, + NULL +}; + +EFI_EMMC_CARD_INFO_PROTOCOL *gEfiEmmcCardInfo =3D NULL; + +/** + Entry point for EFI drivers. + + @param[in] ImageHandle EFI_HANDLE + @param[in] SystemTable EFI_SYSTEM_TABLE + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Fail + +**/ +EFI_STATUS +EFIAPI +MediaDeviceDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + return EfiLibInstallAllDriverProtocols ( + ImageHandle, + SystemTable, + &gMediaDeviceDriverBinding, + ImageHandle, + &gMediaDeviceComponentName, + NULL, + NULL + ); +} + + +/** + Test to see if this driver supports ControllerHandle. Any ControllerHand= le + that has installed will be supported. + + @param[in] This Protocol instance pointer. + @param[in] Controlle Handle of device to test + @param[in] RemainingDevicePath Not used + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_UNSUPPORTED This driver does not support this devi= ce. + +**/ +EFI_STATUS +EFIAPI +MediaDeviceDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) +{ + EFI_STATUS Status; + EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo; + + // + // Test whether there is MMCHostIO Protocol attached on the controller h= andle. + // + Status =3D gBS->OpenProtocol ( + Controller, + &gEfiMmcHostIoProtocolGuid, + (VOID **) &MmcHostIo, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + gBS->CloseProtocol ( + Controller, + &gEfiMmcHostIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + +Exit: + return Status; +} + + +/** + Starting the Media Device Driver + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to test + @param[in] RemainingDevicePath Not used + + @retval EFI_SUCCESS supports this device. + @retval EFI_UNSUPPORTED do not support this device. + @retval EFI_DEVICE_ERROR cannot be started due to device Error + @retval EFI_OUT_OF_RESOURCES cannot allocate resources + +**/ +EFI_STATUS +EFIAPI +MediaDeviceDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) +{ + EFI_STATUS Status; + EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo; + CARD_DATA *CardData; + UINTN Loop; + EFI_EMMC_CARD_INFO_PROTOCOL *EfiEmmcCardInfoRegister; + + DEBUG ((EFI_D_INFO, "%a(%d): %a()\n", __FILE__, __LINE__, __FUNCTION__)); + + EfiEmmcCardInfoRegister =3D NULL; + CardData =3D NULL; + MmcHostIo =3D NULL; + + // + // Alloc memory for EfiEmmcCardInfoRegister variable + // + EfiEmmcCardInfoRegister =3D (EFI_EMMC_CARD_INFO_PROTOCOL *) AllocateZero= Pool (sizeof (EFI_EMMC_CARD_INFO_PROTOCOL)); + if (EfiEmmcCardInfoRegister =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + goto Exit; + } + + // + // Open PCI I/O Protocol and save pointer to open protocol + // in private data area. + // + Status =3D gBS->OpenProtocol ( + Controller, + &gEfiMmcHostIoProtocolGuid, + (VOID **) &MmcHostIo, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MediaDeviceDriverBindingStart: Fail to open gEfi= MmcHostIoProtocolGuid \n")); + goto Exit; + } + + Status =3D MmcHostIo->DetectCardAndInitHost (MmcHostIo); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "MediaDeviceDriverBindingStart: Fail to DetectCar= dAndInitHost %r\n", Status)); + goto Exit; + } + + CardData =3D (CARD_DATA *) AllocateZeroPool (sizeof (CARD_DATA)); + if (CardData =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + DEBUG ((EFI_D_ERROR, "MediaDeviceDriverBindingStart: Fail to AllocateZ= eroPool(CARD_DATA) \n")); + goto Exit; + } + + ASSERT (MmcHostIo->HostCapability.BoundarySize >=3D 4 * 1024); + + CardData->RawBufferPointer =3D (UINT8*) (UINTN) 0xffffffff; + + DEBUG ((EFI_D_INFO, "CardData->RawBufferPointer =3D 0x%x \n",CardData->R= awBufferPointer)); + DEBUG ((EFI_D_INFO, "requesting 0x%x pages \n",EFI_SIZE_TO_PAGES(2 * Mmc= HostIo->HostCapability.BoundarySize))); + + // + // Allocated the buffer under 4G + // + Status =3D gBS->AllocatePages ( + AllocateMaxAddress, + EfiBootServicesData, + EFI_SIZE_TO_PAGES (2 * MmcHostIo->HostCapability.Boundar= ySize), + (EFI_PHYSICAL_ADDRESS *) (&(CardData->RawBufferPointer)) + ); + DEBUG ((EFI_D_INFO, "CardData->RawBufferPointer =3D 0x%x \n",CardData->R= awBufferPointer)); + if (!EFI_ERROR (Status)) { + CardData->RawBufferPointer =3D ZeroMem (CardData->RawBufferPointer, EF= I_SIZE_TO_PAGES (2 * MmcHostIo->HostCapability.BoundarySize)); + } else { + DEBUG ((DEBUG_ERROR, "MediaDeviceDriverBindingStart: Fail to AllocateZ= eroPool(2*x) \n")); + Status =3D EFI_OUT_OF_RESOURCES; + goto Exit; + } + CardData->AlignedBuffer =3D CardData->RawBufferPointer - ((UINTN) (CardD= ata->RawBufferPointer) & (MmcHostIo->HostCapability.BoundarySize - 1)) + Mm= cHostIo->HostCapability.BoundarySize; + + CardData->Signature =3D CARD_DATA_SIGNATURE; + CardData->MmcHostIo =3D MmcHostIo; + CardData->Handle =3D Controller; + for (Loop =3D 0; Loop < MAX_NUMBER_OF_PARTITIONS; Loop++) { + CardData->Partitions[Loop].Signature =3D CARD_PARTITION_SIGNATURE; + } + Status =3D MMCSDCardInit (CardData); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MediaDeviceDriverBindingStart: Fail to MMCSDCard= Init \n")); + goto Exit; + } + + DEBUG ((DEBUG_INFO, "MediaDeviceDriverBindingStart: MMC SD card\n")); + Status =3D MMCSDBlockIoInit (CardData); + + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "MediaDeviceDriverBindingStart: Card BlockIo init= failed\n")); + goto Exit; + } + + + Status =3D MediaDeviceDriverInstallBlockIo (This, CardData); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "MediaDeviceDriverBindingStart: Fail to install g= EfiBlockIoProtocolGuid \n")); + goto Exit; + } + + // + // Component name protocol + // + Status =3D AddUnicodeString ( + "eng", + gMediaDeviceComponentName.SupportedLanguages, + &CardData->ControllerNameTable, + L"MMC/SD Media Device" + ); + if (EFI_ERROR (Status)) { + Status =3D EFI_OUT_OF_RESOURCES; + for (Loop =3D 0; Loop < MAX_NUMBER_OF_PARTITIONS; Loop++) { + if (!CardData->Partitions[Loop].Present) { + continue; + } + gBS->UninstallMultipleProtocolInterfaces ( + CardData->Partitions[Loop].Handle, + &gEfiBlockIoProtocolGuid, + &CardData->Partitions[Loop].BlockIo, + &gEfiDevicePathProtocolGuid, + CardData->Partitions[Loop].DevPath, + NULL + ); + } + goto Exit; + } + if (EfiEmmcCardInfoRegister !=3D NULL) { + + // + // assign to protocol + // + EfiEmmcCardInfoRegister->CardData =3D CardData; + + Status =3D gBS->InstallMultipleProtocolInterfaces ( + &Controller, + &gEfiEmmcCardInfoProtocolGuid, + EfiEmmcCardInfoRegister, + NULL + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "MediaDeviceDriverBindingStart: Install eMMC Ca= rd info protocol failed\n")); + for (Loop =3D 0; Loop < MAX_NUMBER_OF_PARTITIONS; Loop++) { + if (!CardData->Partitions[Loop].Present) { + continue; + } + gBS->UninstallMultipleProtocolInterfaces ( + CardData->Partitions[Loop].Handle, + &gEfiBlockIoProtocolGuid, + &CardData->Partitions[Loop].BlockIo, + &gEfiDevicePathProtocolGuid, + CardData->Partitions[Loop].DevPath, + NULL + ); + } + + goto Exit; + } + + gEfiEmmcCardInfo =3D EfiEmmcCardInfoRegister; + } + + DEBUG ((DEBUG_INFO, "MediaDeviceDriverBindingStart: Started\n")); + +Exit: + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MediaDeviceDriverBindingStart: End with failure\= n")); + if (CardData !=3D NULL && MmcHostIo !=3D NULL) { + if (CardData->RawBufferPointer !=3D NULL) { + gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) CardData->RawBuffer= Pointer, EFI_SIZE_TO_PAGES (MmcHostIo->HostCapability.BoundarySize * 2)); + } + FreePool (CardData); + } + gBS->CloseProtocol ( + Controller, + &gEfiMmcHostIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + } + return Status; +} + + +/** + Stop this driver on ControllerHandle. Support stoping any child handles + created by this driver. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to stop driver on + @param[in] NumberOfChildren Number of Children in the ChildHandle= Buffer + @param[in] ChildHandleBuffer List of handles for the children we n= eed to stop. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Fail + +**/ +EFI_STATUS +EFIAPI +MediaDeviceDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ) +{ + EFI_STATUS Status; + CARD_DATA *CardData; + BOOLEAN AllChildrenStopped; + UINTN Index; + UINTN Pages =3D 0; + + Status =3D EFI_SUCCESS; + CardData =3D gEfiEmmcCardInfo->CardData; + + if (NumberOfChildren =3D=3D 0) { + Status =3D gBS->UninstallMultipleProtocolInterfaces ( + Controller, + &gEfiEmmcCardInfoProtocolGuid, + gEfiEmmcCardInfo, + NULL + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MediaDeviceDriverBindingStop: UNINSTALL gEfiEm= mcCardInfoProtocolGuid FAILURE\n")); + return Status; + } + + FreeUnicodeStringTable (CardData->ControllerNameTable); + + Pages =3D (2 * (CardData->MmcHostIo->HostCapability.BoundarySize)); + if (CardData->RawBufferPointer !=3D NULL) { + FreePages (CardData->RawBufferPointer, EFI_SIZE_TO_PAGES(Pages)); + } + + Status =3D gBS->CloseProtocol ( + Controller, + &gEfiMmcHostIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + + if (MediaDeviceDriverAllPartitionNotPresent(CardData)) { + FreePool (CardData); + FreePool (gEfiEmmcCardInfo); + gEfiEmmcCardInfo =3D NULL; + } + + return Status; + } + + AllChildrenStopped =3D TRUE; + for (Index =3D 0; Index < NumberOfChildren; Index++) { + Status =3D MediaDeviceDriverUninstallBlockIo(This, CardData, ChildHand= leBuffer[Index]); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MediaDeviceDriverBindingStop: UNINSTALL block = io FAILURE\n")); + AllChildrenStopped =3D FALSE; + break; + } + } + + return Status; +} + + +BOOLEAN +MediaDeviceDriverAllPartitionNotPresent ( + IN CARD_DATA *CardData + ) +{ + BOOLEAN AllPartitionNotPresent; + UINTN Loop; + MMC_PARTITION_DATA *Partition; + + Partition =3D CardData->Partitions; + + AllPartitionNotPresent =3D TRUE; + + for (Loop =3D 0; Loop < MAX_NUMBER_OF_PARTITIONS; Partition++, Loop++) { + if (Partition->Present) { + AllPartitionNotPresent =3D FALSE; + break; + } + } + + return AllPartitionNotPresent; +} + + +STATIC +struct { + DEVICE_LOGICAL_UNIT_DEVICE_PATH LogicalUnit; + EFI_DEVICE_PATH_PROTOCOL End; +} EmmcDevPathTemplate =3D { + { + { + MESSAGING_DEVICE_PATH, + MSG_DEVICE_LOGICAL_UNIT_DP, + { + sizeof (DEVICE_LOGICAL_UNIT_DEVICE_PATH), + 0 + }, + }, + 0 + }, + { + END_DEVICE_PATH_TYPE, + END_ENTIRE_DEVICE_PATH_SUBTYPE, + { + END_DEVICE_PATH_LENGTH, + 0 + } + } +}; + + +/** + MediaDeviceDriverInstallBlockIo + + @param[in] This Pointer to the EFI_DRIVER_BINDING_PR= OTOCOL. + @param[in] CardData Pointer to CARD_DATA + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +MediaDeviceDriverInstallBlockIo ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN CARD_DATA *CardData + ) +{ + EFI_STATUS Status; + UINT8 Loop; + MMC_PARTITION_DATA *Partition; + EFI_DEVICE_PATH_PROTOCOL *MainPath; + EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo =3D NULL; + + Partition =3D CardData->Partitions; + + Status =3D gBS->HandleProtocol ( + CardData->Handle, + &gEfiDevicePathProtocolGuid, + (VOID **) &MainPath + ); + if (EFI_ERROR (Status)) { + return Status; + } + + for (Loop =3D 0; Loop < MAX_NUMBER_OF_PARTITIONS; Partition++, Loop++) { + if (!Partition->Present) { + continue; + } + + DEBUG ((EFI_D_INFO, "MediaDeviceDriverInstallBlockIo: Installing Block= I/O for partition %d\n", Loop)); + + Partition->Handle =3D NULL; + Partition->CardData =3D CardData; + + EmmcDevPathTemplate.LogicalUnit.Lun =3D Loop; + Partition->DevPath =3D + AppendDevicePath ( + MainPath, + (EFI_DEVICE_PATH_PROTOCOL *) &EmmcDevPathTemplate + ); + if (Partition->DevPath =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + break; + } + + Status =3D gBS->InstallProtocolInterface ( + &(Partition->Handle), + &gEfiDevicePathProtocolGuid, + EFI_NATIVE_INTERFACE, + Partition->DevPath + ); + if (EFI_ERROR (Status)) { + break; + } + + Status =3D gBS->InstallProtocolInterface ( + &(Partition->Handle), + &gEfiBlockIoProtocolGuid, + EFI_NATIVE_INTERFACE, + &Partition->BlockIo + ); + + // + // Open parent controller by child + // + Status =3D gBS->OpenProtocol ( + CardData->Handle, + &gEfiMmcHostIoProtocolGuid, + (VOID **) &MmcHostIo, + This->DriverBindingHandle, + Partition->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + } + + return Status; +} + + +/** + MediaDeviceDriverUninstallBlockIo + + @param[in] This Pointer to the EFI_DRIVER_BINDING_P= ROTOCOL. + @param[in] CardData Pointer to CARD_DATA + @param[in] Handle Handle of Partition + + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_SUCCESS + +**/ +EFI_STATUS +MediaDeviceDriverUninstallBlockIo ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN CARD_DATA *CardData, + IN EFI_HANDLE Handle + ) +{ + EFI_STATUS Status; + UINTN Loop; + MMC_PARTITION_DATA *Partition; + + Partition =3D CardData->Partitions; + Status =3D EFI_SUCCESS; + + for (Loop =3D 0; Loop < MAX_NUMBER_OF_PARTITIONS; Partition++, Loop++) { + if (!Partition->Present || Partition->Handle !=3D Handle) { + continue; + } + + // + // Close MmcHostIoProtocol by child + // + Status =3D gBS->CloseProtocol ( + CardData->Handle, + &gEfiMmcHostIoProtocolGuid, + This->DriverBindingHandle, + Partition->Handle + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "CloseProtocol gEfiMmcHostIoProtocolGuid FAILUR= E \n")); + return Status; + } + + Status =3D gBS->UninstallMultipleProtocolInterfaces ( + Partition->Handle, + &gEfiBlockIoProtocolGuid, + &Partition->BlockIo, + &gEfiDevicePathProtocolGuid, + Partition->DevPath, + NULL + ); + Partition->Present =3D FALSE; + DEBUG ((EFI_D_ERROR, "MediaDeviceDriverBindingStop gEfiBlockIoProtocol= Guid removed. %x\n", Status)); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MediaDeviceDriverUninstallBlockIo UNISTALL FAI= LURE \n")); + } + return Status; + } + + return EFI_INVALID_PARAMETER; +} + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcM= ediaDeviceDxe/MmcMediaDeviceDxe.inf b/Silicon/BroxtonSoC/BroxtonSiPkg/South= Cluster/Sdio/Dxe/MMC/MmcMediaDeviceDxe/MmcMediaDeviceDxe.inf new file mode 100644 index 0000000..500d1de --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/MMC/MmcMediaDev= iceDxe/MmcMediaDeviceDxe.inf @@ -0,0 +1,71 @@ +## @file +# SD Host module +# +# Copyright (c) 1999 - 2016, Intel Corporation. All rights reserved.
+# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the B= SD License +# which accompanies this distribution. The full text of the license may b= e 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 IM= PLIED. +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D MmcMediaDevice + FILE_GUID =3D 1CFD8F87-355A-4954-859F-DDC5D8876D44 + MODULE_TYPE =3D UEFI_DRIVER + VERSION_STRING =3D 1.0 + ENTRY_POINT =3D MediaDeviceDriverEntryPoint + +# +# The following information is for reference only and not required by the = build tools. +# +# VALID_ARCHITECTURES =3D IA32 X64 IPF EBC +# + +[Sources.common] + ComponentName.c + MediaDeviceDriver.c + MMCSDBlockIo.c + MMCSDTransfer.c + +[Packages] + MdePkg/MdePkg.dec + BroxtonSiPkg/BroxtonSiPkg.dec + BroxtonSiPkg/BroxtonSiPrivate.dec + +[LibraryClasses] + UefiDriverEntryPoint + DebugLib + UefiBootServicesTableLib + BaseMemoryLib + MemoryAllocationLib + UefiLib + DevicePathLib + IoLib + PcdLib + HobLib + PciLib + SteppingLib + ConfigBlockLib + +[Guids] + gEfiEventReadyToBootGuid ## UNDEFINED + gScPolicyHobGuid + gEfiUnbootablePartitionGuid ## PRODUCE ## GUID + gScsConfigGuid + +[Protocols] + gEfiDevicePathProtocolGuid ## BY_START + gEfiMmcHostIoProtocolGuid ## CONSUMES + gEfiBlockIoProtocolGuid ## BY_START + gEfiEmmcCardInfoProtocolGuid ## BY_START + gEfiEmmcBootPartitionProtocolGuid ## BY_START + +[Pcd] + gEfiBxtTokenSpaceGuid.PcdEmmcManufacturerId ## PRODUCES + gEfiBxtTokenSpaceGuid.PcdProductSerialNumber ## PRODUCES diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdCon= trollerDxe/ComponentName.c b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/S= dio/Dxe/SD/SdControllerDxe/ComponentName.c new file mode 100644 index 0000000..0bc9861 --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdController= Dxe/ComponentName.c @@ -0,0 +1,233 @@ +/** @file + UEFI Component Name(2) protocol implementation for SD controller driver. + + Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include "SdController.h" + +// +// EFI Component Name Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gSdControllerNa= me =3D { + SdControllerGetDriverName, + SdControllerGetControllerName, + "eng" +}; + + +// +// EFI Component Name 2 Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gSdControllerNa= me2 =3D { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) SdControllerGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) SdControllerGetControllerName, + "en" +}; + + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mSdControllerDriver= NameTable[] =3D { + { "eng;en", L"EFI SD Host Controller Driver" }, + { NULL, NULL } +}; + + +// +// EFI Component Name Functions +// +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form o= f a + Unicode string. If the driver specified by This has a user readable name= in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver speci= fied + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2= _PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII= string + array indicating the language. This = is the + language of the driver name that the= caller is + requesting, and it must match one of= the + languages specified in SupportedLang= uages. The + number of languages supported by a d= river is up + to the driver writer. Language is sp= ecified + in RFC 3066 or ISO 639-2 language co= de format. + + @param[out] DriverName A pointer to the Unicode string to r= eturn. + This Unicode string is the name of t= he + driver specified by This in the lang= uage + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver sp= ecified by + This and the language specified by L= anguage was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does no= t support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +SdControllerGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mSdControllerDriverNameTable, + DriverName, + (BOOLEAN)(This =3D=3D &gSdControllerName) + ); +} + + +/** + Retrieves a Unicode string that is the user readable name of the control= ler + that is being managed by a driver. + + This function retrieves the user readable name of the controller specifi= ed by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specif= ied by + Language, then a pointer to the controller name is returned in Controlle= rName, + and EFI_SUCCESS is returned. If the driver specified by This is not cur= rently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does = not + support the language specified by Language, then EFI_UNSUPPORTED is retu= rned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_P= ROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the dr= iver + specified by This is managing. This h= andle + specifies the controller whose name is= to be + returned. + + @param[in] ChildHandle The handle of the child controller to = retrieve + the name of. This is an optional para= meter that + may be NULL. It will be NULL for devi= ce + drivers. It will also be NULL for a b= us drivers + that wish to retrieve the name of the = bus + controller. It will not be NULL for a= bus + driver that wishes to retrieve the nam= e of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII s= tring + array indicating the language. This i= s the + language of the driver name that the c= aller is + requesting, and it must match one of t= he + languages specified in SupportedLangua= ges. The + number of languages supported by a dri= ver is up + to the driver writer. Language is spec= ified in + RFC 3066 or ISO 639-2 language code fo= rmat. + + @param[out] ControllerName A pointer to the Unicode string to ret= urn. + This Unicode string is the name of the + controller specified by ControllerHand= le and + ChildHandle in the language specified = by + Language from the point of view of the= driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readab= le name in + the language specified by Language for= the + driver specified by This was returned = in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HA= NDLE. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not = a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not cu= rrently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not = support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +SdControllerGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle O= PTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_SD_HOST_IO_PROTOCOL *SdHostIo; + SDHOST_DATA *SdHostData; + + // + // This is a device driver, so ChildHandle must be NULL. + // + if (ChildHandle !=3D NULL) { + return EFI_UNSUPPORTED; + } + + // + // Make sure this driver is currently managing ControllerHandle + // + Status =3D EfiTestManagedDevice ( + ControllerHandle, + gSdControllerDriverBinding.DriverBindingHandle, + &gEfiPciIoProtocolGuid + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Get the device context + // + Status =3D gBS->OpenProtocol ( + ControllerHandle, + &gEfiSdHostIoProtocolGuid, + (VOID **) &SdHostIo, + gSdControllerDriverBinding.DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + SdHostData =3D SDHOST_DATA_FROM_THIS (SdHostIo); + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + SdHostData->ControllerNameTable, + ControllerName, + (BOOLEAN)(This =3D=3D &gSdControllerName) + ); + +} + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdCon= trollerDxe/ComponentName.h b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/S= dio/Dxe/SD/SdControllerDxe/ComponentName.h new file mode 100644 index 0000000..89d8a27 --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdController= Dxe/ComponentName.h @@ -0,0 +1,145 @@ +/** @file + This file contains the declarations for component name routines. + + Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#ifndef _COMPONENT_NAME_H_ +#define _COMPONENT_NAME_H_ + +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form o= f a + Unicode string. If the driver specified by This has a user readable name= in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver speci= fied + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_P= ROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII s= tring + array indicating the language. This is= the + language of the driver name that the c= aller is + requesting, and it must match one of t= he + languages specified in SupportedLangua= ges. The + number of languages supported by a dri= ver is up + to the driver writer. Language is spec= ified + in RFC 3066 or ISO 639-2 language code= format. + + @param[out] DriverName A pointer to the Unicode string to ret= urn. + This Unicode string is the name of the + driver specified by This in the langua= ge + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver spec= ified by + This and the language specified by Lan= guage was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not = support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +SdControllerGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + +/** + Retrieves a Unicode string that is the user readable name of the control= ler + that is being managed by a driver. + + This function retrieves the user readable name of the controller specifi= ed by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specif= ied by + Language, then a pointer to the controller name is returned in Controlle= rName, + and EFI_SUCCESS is returned. If the driver specified by This is not cur= rently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does = not + support the language specified by Language, then EFI_UNSUPPORTED is retu= rned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME= 2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instanc= e. + + @param[in] ControllerHandle The handle of a controller that the= driver + specified by This is managing. Thi= s handle + specifies the controller whose name= is to be + returned. + + @param[in] ChildHandle The handle of the child controller = to retrieve + the name of. This is an optional p= arameter that + may be NULL. It will be NULL for d= evice + drivers. It will also be NULL for = a bus drivers + that wish to retrieve the name of t= he bus + controller. It will not be NULL fo= r a bus + driver that wishes to retrieve the = name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCI= I string + array indicating the language. Thi= s is the + language of the driver name that th= e caller is + requesting, and it must match one o= f the + languages specified in SupportedLan= guages. The + number of languages supported by a = driver is up + to the driver writer. Language is s= pecified in + RFC 3066 or ISO 639-2 language code= format. + + @param[out] ControllerName A pointer to the Unicode string to = return. + This Unicode string is the name of = the + controller specified by ControllerH= andle and + ChildHandle in the language specifi= ed by + Language from the point of view of = the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user rea= dable name in + the language specified by Language = for the + driver specified by This was return= ed in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI= _HANDLE. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is n= ot a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not= currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does n= ot support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +SdControllerGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + +#endif + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdCon= trollerDxe/SdController.c b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sd= io/Dxe/SD/SdControllerDxe/SdController.c new file mode 100644 index 0000000..f481196 --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdController= Dxe/SdController.c @@ -0,0 +1,1804 @@ +/** @file + The SD host controller driver model and HC protocol routines. + + Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include "SdController.h" + +EFI_DRIVER_BINDING_PROTOCOL gSdControllerDriverBinding =3D { + SdControllerSupported, + SdControllerStart, + SdControllerStop, + 0x20, + NULL, + NULL +}; + + +EFI_SD_HOST_IO_PROTOCOL mSdHostIo =3D { + EFI_SD_HOST_IO_PROTOCOL_REVISION_01, + { + 0, // HighSpeedSupport + 0, // V18Support + 0, // V30Support + 0, // V33Support + 0, // Reserved0 + 0, // BusWidth4 + 0, // BusWidth8 + 0, // Reserved1 + 0, // Reserved1 + (512 * 1024) // BoundarySize + }, + + SendCommand, + SetClockFrequency, + SetBusWidth, + SetHostVoltage, + ResetSdHost, + EnableAutoStopCmd, + DetectCardAndInitHost, + SetBlockLength, + SetHighSpeedMode, + SetDDRMode +}; + + +/** + Find sdclk_freq_sel and upr_sdclk_freq_sel bits + for Clock Control Register (CLK_CTL)Offset 2Ch when using 8bit or 10bit + divided clock mode. + + @param[in] BaseClockFreg Base Clock Frequency in Hz For SD Cloc= k in the + Capabilities register. + @param[in] TargetFreq Target Frequency in Hz to reach. + @param[in] Is8BitMode True if 8-bit Divided Clock Mode else = 10bit mode. + @param[out] Bits sdclk_freq_sel and upr_sdclk_freq_sel = bits for + TargetFreq. + + @retval EFI_SUCCESS Bits setup. + @retval EFI_UNSUPPORTED Cannot divide base clock to reach targ= et clock. + +**/ +EFI_STATUS +DividedClockModeBits ( + IN CONST UINTN BaseClockFreg, + IN CONST UINTN TargetFreq, + IN CONST BOOLEAN Is8BitMode, + OUT UINT16 *Bits + ) +{ + UINTN N; + UINTN CurrFreq; + + *Bits =3D 0; + CurrFreq =3D BaseClockFreg; + N =3D 0; + // + // N =3D=3D 0 same for 8bit & 10bit mode i.e. BaseClockFreg of controlle= r. + // + if (TargetFreq < CurrFreq) { + if (Is8BitMode) { + N =3D 1; + do { + // + // N values for 8bit mode when N > 0. + // Bit[15:8] SDCLK Frequency Select at offset 2Ch + // 80h - base clock divided by 256 + // 40h - base clock divided by 128 + // 20h - base clock divided by 64 + // 10h - base clock divided by 32 + // 08h - base clock divided by 16 + // 04h - base clock divided by 8 + // 02h - base clock divided by 4 + // 01h - base clock divided by 2 + // + CurrFreq =3D BaseClockFreg / (2 * N); + if (TargetFreq >=3D CurrFreq) { + break; + } + N *=3D 2; + if (N > V_MMIO_CLKCTL_MAX_8BIT_FREQ_SEL) { + return EFI_UNSUPPORTED; + } + } while (TRUE); + } else { + N =3D 1; + CurrFreq =3D BaseClockFreg / (2 * N); + // + // (try N =3D 0 or 1 first since don't want divide by 0). + // + if (TargetFreq < CurrFreq) { + // + // If still no match then calculate it for 10bit. + // N values for 10bit mode. + // N 1/2N Divided Clock (Duty 50%). + // from Spec "The length of divider is extended to 10 bits and all + // divider values shall be supported. + // + N =3D (BaseClockFreg / TargetFreq) / 2; + + // + // Can only be N or N+1; + // + CurrFreq =3D BaseClockFreg / (2 * N); + if (TargetFreq < CurrFreq) { + N++; + CurrFreq =3D BaseClockFreg / (2 * N); + } + + if (N > V_MMIO_CLKCTL_MAX_10BIT_FREQ_SEL) { + return EFI_UNSUPPORTED; + } + + // + // Set upper bits of SDCLK Frequency Select (bits 7:6 of reg 0x2c). + // + *Bits |=3D ((UINT16) ((N >> 2) & B_MMIO_CLKCTL_UPR_SDCLK_FREQ_SEL_= MASK)); + } + } + } + + // + // Set lower bits of SDCLK Frequency Select (bits 15:8 of reg 0x2c). + // + *Bits |=3D ((UINT16) ((UINT8) N) << 8); + DEBUG ( + (EFI_D_INFO, + "SDIO:DividedClockModeBits: %dbit mode Want %dHz Got %dHz bits =3D %04= x\r\n", + (Is8BitMode) ? 8 : 10, + TargetFreq, + CurrFreq, + (UINTN) *Bits + )); + + return EFI_SUCCESS; +} + + +/** + Print type of error and command index + + @param[in] CommandIndex Command index to set the command index = field of command register. + @param[in] ErrorCode Error interrupt status read from host c= ontroller + + @retval EFI_DEVICE_ERROR + @retval EFI_TIMEOUT + @retval EFI_CRC_ERROR + +**/ +EFI_STATUS +GetErrorReason ( + IN UINT16 CommandIndex, + IN UINT16 ErrorCode + ) +{ + EFI_STATUS Status; + + Status =3D EFI_DEVICE_ERROR; + + DEBUG ((EFI_D_ERROR, "[%2d] -- ", CommandIndex)); + + if (ErrorCode & BIT0) { + Status =3D EFI_TIMEOUT; + DEBUG ((EFI_D_ERROR, "Command Timeout Erro")); + } + + if (ErrorCode & BIT1) { + Status =3D EFI_CRC_ERROR; + DEBUG ((EFI_D_ERROR, "Command CRC Error")); + } + + if (ErrorCode & BIT2) { + DEBUG ((EFI_D_ERROR, "Command End Bit Error")); + } + + if (ErrorCode & BIT3) { + DEBUG ((EFI_D_ERROR, "Command Index Error")); + } + if (ErrorCode & BIT4) { + Status =3D EFI_TIMEOUT; + DEBUG ((EFI_D_ERROR, "Data Timeout Error")); + } + + if (ErrorCode & BIT5) { + Status =3D EFI_CRC_ERROR; + DEBUG ((EFI_D_ERROR, "Data CRC Error")); + } + + if (ErrorCode & BIT6) { + DEBUG ((EFI_D_ERROR, "Data End Bit Error")); + } + + if (ErrorCode & BIT7) { + DEBUG ((EFI_D_ERROR, "Current Limit Error")); + } + + if (ErrorCode & BIT8) { + DEBUG ((EFI_D_ERROR, "Auto CMD12 Error")); + } + + if (ErrorCode & BIT9) { + DEBUG ((EFI_D_ERROR, "ADMA Error")); + } + + DEBUG ((EFI_D_ERROR, "\n")); + + return Status; +} + + +/** + Enable/Disable High Speed transfer mode + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTO= COL instance. + @param[in] Enable TRUE to Enable, FALSE to Disable + + @retval EFI_SUCCESS + +**/ +EFI_STATUS +EFIAPI +SetHighSpeedMode ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN BOOLEAN Enable + ) +{ + UINT32 Data; + SDHOST_DATA *SdHostData; + EFI_PCI_IO_PROTOCOL *PciIo; + + SdHostData =3D SDHOST_DATA_FROM_THIS (This); + PciIo =3D SdHostData->PciIo; + + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint8, + 0, + (UINT64) MMIO_HOSTCTL, + 1, + &Data + ); + + if (Enable) { + DEBUG ((EFI_D_INFO, "Enable High Speed transfer mode ... \r\n")); + Data |=3D BIT2; + } else { + Data &=3D ~BIT2; + } + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint8, + 0, + (UINT64) MMIO_HOSTCTL, + 1, + &Data + ); + + return EFI_SUCCESS; +} +EFI_STATUS +EFIAPI +SetDDRMode ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN BOOLEAN Enable + ) +{ + UINT16 Data; + SDHOST_DATA *SdHostData; + EFI_PCI_IO_PROTOCOL *PciIo; + + SdHostData =3D SDHOST_DATA_FROM_THIS (This); + PciIo =3D SdHostData->PciIo; + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_HOSTCTL2, + 1, + &Data + ); + + Data &=3D 0xFFF0; + if (Enable) { + Data |=3D 0x0004; // Enable DDR50 by default, later should enable othe= r mode like HS200/400 + Data |=3D BIT3; // Enable 1.8V Signaling + } + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_HOSTCTL2, + 1, + &Data + ); + + return EFI_SUCCESS; +} + + +/** + Power on/off the LED associated with the slot + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTO= COL instance. + @param[in] Enable TRUE to set LED on, FALSE to set LED = off + + @retval EFI_SUCCESS + +**/ +EFI_STATUS +HostLEDEnable ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN BOOLEAN Enable + ) +{ + SDHOST_DATA *SdHostData; + EFI_PCI_IO_PROTOCOL *PciIo; + UINT32 Data; + + SdHostData =3D SDHOST_DATA_FROM_THIS (This); + PciIo =3D SdHostData->PciIo; + + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint8, + 0, + (UINT64) MMIO_HOSTCTL, + 1, + &Data + ); + + if (Enable) { + // + // LED On + // + Data |=3D BIT0; + } else { + // + // LED Off + // + Data &=3D ~BIT0; + } + + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint8, + 0, + (UINT64) MMIO_HOSTCTL, + 1, + &Data + ); + + return EFI_SUCCESS; +} + + +/** + The main function used to send the command to the card inserted into the= SD host slot. + It will assemble the arguments to set the command register and wait for = the command + and transfer completed until timeout. Then it will read the response reg= ister to fill + the ResponseData. + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTO= COL instance. + @param[in] CommandIndex The command index to set the command = index field of command register. + @param[in] Argument Command argument to set the argument = field of command register. + @param[in] DataType TRANSFER_TYPE, indicates no data, dat= a in or data out. + @param[in] Buffer Contains the data read from / write t= o the device. + @param[in] BufferSize The size of the buffer. + @param[in] ResponseType RESPONSE_TYPE. + @param[in] TimeOut Time out value in 1 ms unit. + @param[out] ResponseData Depending on the ResponseType, such a= s CSD or card status. + + @retval EFI_SUCCESS + @retval EFI_INVALID_PARAMETER + @retval EFI_OUT_OF_RESOURCES + @retval EFI_TIMEOUT + @retval EFI_DEVICE_ERROR + +**/ +EFI_STATUS +EFIAPI +SendCommand ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN UINT16 CommandIndex, + IN UINT32 Argument, + IN TRANSFER_TYPE DataType, + IN UINT8 *Buffer, OPTIONAL + IN UINT32 BufferSize, + IN RESPONSE_TYPE ResponseType, + IN UINT32 TimeOut, + OUT UINT32 *ResponseData OPTIONAL + ) +{ + EFI_STATUS Status; + SDHOST_DATA *SdHostData; + EFI_PCI_IO_PROTOCOL *PciIo; + UINT32 ResponseDataCount; + UINT32 Data; + UINT64 Data64; + UINT8 Index; + INTN TimeOut2; + BOOLEAN AutoCMD12Enable =3D FALSE; + + Status =3D EFI_SUCCESS; + ResponseDataCount =3D 1; + SdHostData =3D SDHOST_DATA_FROM_THIS (This); + PciIo =3D SdHostData->PciIo; + AutoCMD12Enable =3D (CommandIndex & AUTO_CMD12_ENABLE) ? TRUE : FA= LSE; + CommandIndex =3D CommandIndex & CMD_INDEX_MASK; + + if (Buffer !=3D NULL && DataType =3D=3D NoData) { + Status =3D EFI_INVALID_PARAMETER; + DEBUG ((EFI_D_ERROR, "SendCommand: invalid parameter \r\n")); + goto Exit; + } + + if (((UINTN) Buffer & (This->HostCapability.BoundarySize - 1)) !=3D (UIN= TN) NULL) { + Status =3D EFI_INVALID_PARAMETER; + DEBUG ((EFI_D_ERROR, "SendCommand: invalid parameter \r\n")); + goto Exit; + } + + DEBUG ((EFI_D_INFO, "SendCommand: Command Index =3D %d \r\n", CommandInd= ex)); + + TimeOut2 =3D 1000; // 10 ms + do { + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint32, + 0, + (UINT64) MMIO_PSTATE, + 1, + &Data + ); + + gBS->Stall (10); + } while ((TimeOut2-- > 0) && (Data &BIT0)); + TimeOut2 =3D 1000; // 10 ms + do { + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint32, + 0, + (UINT64) MMIO_PSTATE, + 1, + &Data + ); + + gBS->Stall (10); + } while ((TimeOut2-- > 0) && (Data & BIT1)); + + // + // Clear status bits + // + Data =3D 0xFFFF; + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_NINTSTS, + 1, + &Data + ); + + Data =3D 0xFFFF; + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_ERINTSTS, + 1, + &Data + ); + + + if (Buffer !=3D NULL) { + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint32, + 0, + (UINT64) MMIO_DMAADR, + 1, + &Buffer + ); + + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_BLKSZ, + 1, + &Data + ); + + Data &=3D ~(0xFFF); + if (BufferSize <=3D SdHostData->BlockLength) { + Data |=3D (BufferSize | 0x7000); + } else { + Data |=3D (SdHostData->BlockLength | 0x7000); + } + + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_BLKSZ, + 1, + &Data + ); + + if (BufferSize <=3D SdHostData->BlockLength) { + Data =3D 1; + } else { + Data =3D BufferSize / SdHostData->BlockLength; + } + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_BLKCNT, + 1, + &Data + ); + + } else { + Data =3D 0; + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_BLKSZ, + 1, + &Data + ); + + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_BLKCNT, + 1, + &Data + ); + } + + // + // Argument + // + Data =3D Argument; + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint32, + 0, + (UINT64) MMIO_CMDARG, + 1, + &Data + ); + + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_XFRMODE, + 1, + &Data + ); + + DEBUG ((EFI_D_INFO, "Transfer mode read =3D 0x%x \r\n", (Data & 0xFFFF)= )); + + // + // BIT0 - DMA Enable + // BIT2 - Auto Cmd12 + // + if (DataType =3D=3D InData) { + Data |=3D BIT4 | BIT0; + } else if (DataType =3D=3D OutData){ + Data &=3D ~BIT4; + Data |=3D BIT0; + } else { + Data &=3D ~(BIT4 | BIT0); + } + + if (BufferSize <=3D SdHostData->BlockLength) { + Data &=3D ~(BIT5 | BIT1 | BIT2); + Data |=3D BIT1; // Enable block count always + } else { + if (SdHostData->IsAutoStopCmd && AutoCMD12Enable) { + Data |=3D (BIT5 | BIT1 | BIT2); + } else { + Data |=3D (BIT5 | BIT1); + } + } + + DEBUG ((EFI_D_INFO, "Transfer mode write =3D 0x%x \r\n", (Data & 0xffff)= )); + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_XFRMODE, + 1, + &Data + ); + + // + //Command + // + //ResponseTypeSelect IndexCheck CRCCheck ResponseType + // 00 0 0 NoResponse + // 01 0 1 R2 + // 10 0 0 R3, R4 + // 10 1 1 R1, R5, R6, R7 + // 11 1 1 R1b, R5b + // + switch (ResponseType) { + case ResponseNo: + Data =3D (CommandIndex << 8); + ResponseDataCount =3D 0; + break; + + case ResponseR1: + case ResponseR5: + case ResponseR6: + case ResponseR7: + Data =3D (CommandIndex << 8) | BIT1 | BIT4| BIT3; + ResponseDataCount =3D 1; + break; + + case ResponseR1b: + case ResponseR5b: + Data =3D (CommandIndex << 8) | BIT0 | BIT1 | BIT4| BIT3; + ResponseDataCount =3D 1; + break; + + case ResponseR2: + Data =3D (CommandIndex << 8) | BIT0 | BIT3; + ResponseDataCount =3D 4; + break; + + case ResponseR3: + case ResponseR4: + Data =3D (CommandIndex << 8) | BIT1; + ResponseDataCount =3D 1; + break; + + default: + ASSERT (0); + Status =3D EFI_INVALID_PARAMETER; + DEBUG ((EFI_D_ERROR, "SendCommand: invalid parameter \r\n")); + goto Exit; + } + + if (DataType !=3D NoData) { + Data |=3D BIT5; + } + + HostLEDEnable (This, TRUE); + + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_SDCMD, + 1, + &Data + ); + + Data =3D 0; + do { + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_ERINTSTS, + 1, + &Data + ); + + if ((Data & 0x07FF) !=3D 0) { + Status =3D GetErrorReason (CommandIndex, (UINT16) Data); + DEBUG ((EFI_D_ERROR, "SendCommand: Error happens \r\n")); + goto Exit; + } + + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_NINTSTS, + 1, + &Data + ); + + if ((Data & BIT0) =3D=3D BIT0) { + // + //Command completed, can read response + // + if (DataType =3D=3D NoData) { + break; + } else { + // + // Transfer completed + // + if ((Data & BIT1) =3D=3D BIT1) { + break; + } + } + } + + gBS->Stall (1 * 1000); + + TimeOut --; + + } while (TimeOut > 0); + + if (TimeOut =3D=3D 0) { + Status =3D EFI_TIMEOUT; + DEBUG ((EFI_D_ERROR, "SendCommand: Time out \r\n")); + goto Exit; + } + + if (ResponseData !=3D NULL) { + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint32, + 0, + (UINT64) MMIO_RESP, + ResponseDataCount, + ResponseData + ); + + if (ResponseType =3D=3D ResponseR2) { + // + // Adjustment for R2 response + // + Data =3D 1; + for (Index =3D 0; Index < ResponseDataCount; Index++) { + Data64 =3D LShiftU64 (*ResponseData, 8); + *ResponseData =3D (UINT32) ((Data64 & 0xFFFFFFFF) | Data); + Data =3D (UINT32) RShiftU64 (Data64, 32); + ResponseData++; + } + } + } + +Exit: + HostLEDEnable (This, FALSE); + return Status; +} + + +/** + Set max clock frequency of the host, the actual frequency may not be the= same as MaxFrequency. + It depends on the max frequency the host can support, divider, and host = speed mode. + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTOC= OL instance. + @param[in] MaxFrequency Max frequency in HZ. + + @retval EFI_SUCCESS + @retval EFI_TIMEOUT + +**/ +EFI_STATUS +EFIAPI +SetClockFrequency ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN UINT32 MaxFrequency + ) +{ + UINT32 Data; + UINT16 FreqSelBits; + EFI_STATUS Status; + SDHOST_DATA *SdHostData; + EFI_PCI_IO_PROTOCOL *PciIo; + UINT32 TimeOutCount; + UINT32 Revision; + + SdHostData =3D SDHOST_DATA_FROM_THIS (This); + PciIo =3D SdHostData->PciIo; + Data =3D 0; + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_CLKCTL, + 1, + &Data + ); + + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint8, + 0, + (UINT64) MMIO_CTRLRVER, + 1, + &Revision + ); + + Revision &=3D 0x000000FF; + + Status =3D DividedClockModeBits ( + SdHostData->BaseClockInMHz * 1000 * 1000, + MaxFrequency, + (Revision < SDHCI_SPEC_300), + &FreqSelBits + ); + + if (EFI_ERROR (Status)) { + // + // Cannot reach MaxFrequency with SdHostData->BaseClockInMHz. + // + ASSERT_EFI_ERROR (Status); + return Status; + } + + Data =3D 0; + + // + // Enable internal clock and Stop Clock Enable + // + Data =3D BIT0; + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_CLKCTL, + 1, + &Data + ); + + TimeOutCount =3D TIME_OUT_1S; + do { + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_CLKCTL, + 1, + &Data + ); + + gBS->Stall (1 * 1000); + TimeOutCount --; + if (TimeOutCount =3D=3D 0) { + DEBUG ((EFI_D_ERROR, "SetClockFrequency: Time out \r\n")); + return EFI_TIMEOUT; + } + } while ((Data & BIT1) !=3D BIT1); + + DEBUG ((EFI_D_INFO, "Base Clock In MHz: %d\r\n", SdHostData->BaseClockIn= MHz)); + + Data =3D (BIT0 | ((UINT32) FreqSelBits)); + DEBUG ((EFI_D_INFO, "Data write to MMIO_CLKCTL: 0x%04x \r\n", Data)); + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_CLKCTL, + 1, + &Data + ); + + TimeOutCount =3D TIME_OUT_1S; + do { + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_CLKCTL, + 1, + &Data + ); + + gBS->Stall (1 * 1000); + TimeOutCount --; + if (TimeOutCount =3D=3D 0) { + DEBUG ((EFI_D_ERROR, "SetClockFrequency: Time out \r\n")); + return EFI_TIMEOUT; + } + } while ((Data & BIT1) !=3D BIT1); + gBS->Stall (20 * 1000); + Data |=3D BIT2; + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_CLKCTL, + 1, + &Data + ); + + return EFI_SUCCESS; +} + + +/** + Set bus width of the host controller + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTO= COL instance. + @param[in] BusWidth Bus width in 1, 4, 8 bits. + + @retval EFI_SUCCESS + @retval EFI_INVALID_PARAMETER + +**/ +EFI_STATUS +EFIAPI +SetBusWidth ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN UINT32 BusWidth + ) +{ + SDHOST_DATA *SdHostData; + EFI_PCI_IO_PROTOCOL *PciIo; + UINT8 Data; + + SdHostData =3D SDHOST_DATA_FROM_THIS (This); + + if ((BusWidth !=3D 1) && (BusWidth !=3D 4) && (BusWidth !=3D 8)) { + DEBUG ((EFI_D_ERROR, "SetBusWidth: Invalid parameter \r\n")); + return EFI_INVALID_PARAMETER; + } + + if ((SdHostData->SdHostIo.HostCapability.BusWidth8 =3D=3D FALSE) && (Bus= Width =3D=3D 8)) { + DEBUG ((EFI_D_ERROR, "SetBusWidth: Invalid parameter \r\n")); + return EFI_INVALID_PARAMETER; + } + + PciIo =3D SdHostData->PciIo; + + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint8, + 0, + (UINT64) MMIO_HOSTCTL, + 1, + &Data + ); + // + // BIT5 8-bit MMC Support (MMC8): + // If set, IOH supports 8-bit MMC. When cleared, IOH does not support th= is feature + // + if (BusWidth =3D=3D 8) { + DEBUG ((EFI_D_INFO, "Bus Width is 8-bit ... \r\n")); + Data |=3D BIT5; + } else if (BusWidth =3D=3D 4) { + DEBUG ((EFI_D_INFO, "Bus Width is 4-bit ... \r\n")); + Data &=3D ~BIT5; + Data |=3D BIT1; + } else { + DEBUG ((EFI_D_INFO, "Bus Width is 1-bit ... \r\n")); + Data &=3D ~BIT5; + Data &=3D ~BIT1; + } + + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint8, + 0, + (UINT64) MMIO_HOSTCTL, + 1, + &Data + ); + + return EFI_SUCCESS; +} + + +/** + Set voltage which could supported by the host controller. + Support 0(Power off the host), 1.8V, 3.0V, 3.3V + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROT= OCOL instance. + @param[in] Voltage Units in 0.1 V. + + @retval EFI_SUCCESS + @retval EFI_INVALID_PARAMETER + +**/ +EFI_STATUS +EFIAPI +SetHostVoltage ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN UINT32 Voltage + ) +{ + SDHOST_DATA *SdHostData; + EFI_PCI_IO_PROTOCOL *PciIo; + UINT8 Data; + EFI_STATUS Status; + + SdHostData =3D SDHOST_DATA_FROM_THIS (This); + PciIo =3D SdHostData->PciIo; + Status =3D EFI_SUCCESS; + + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint8, + 0, + (UINT64) MMIO_PWRCTL, + 1, + &Data + ); + + if (Voltage =3D=3D 0) { + // + // Power Off the host + // + Data &=3D ~BIT0; + } else if (Voltage <=3D 18 && This->HostCapability.V18Support) { + // + // 1.8V + // + Data |=3D (BIT1 | BIT3 | BIT0); + } else if (Voltage > 18 && Voltage <=3D 30 && This->HostCapability.V30S= upport) { + // + // 3.0V + // + Data |=3D (BIT2 | BIT3 | BIT0); + } else if (Voltage > 30 && Voltage <=3D 33 && This->HostCapability.V33Su= pport) { + // + // 3.3V + // + Data |=3D (BIT1 | BIT2 | BIT3 | BIT0); + } else { + Status =3D EFI_UNSUPPORTED; + goto Exit; + } + + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint8, + 0, + (UINT64) MMIO_PWRCTL, + 1, + &Data + ); + + gBS->Stall (10 * 1000); + +Exit: + return Status; +} + + +/** + Reset the host controller. + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTO= COL instance. + @param[in] ResetAll TRUE to reset all. + + @retval EFI_SUCCESS + @retval EFI_TIMEOUT + +**/ +EFI_STATUS +EFIAPI +ResetSdHost ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN RESET_TYPE ResetType + ) +{ + SDHOST_DATA *SdHostData; + EFI_PCI_IO_PROTOCOL *PciIo; + UINT32 Data; + UINT16 ErrStatus; + UINT32 Mask; + UINT32 TimeOutCount; + UINT16 SaveClkCtl; + UINT16 ZeroClkCtl; + + SdHostData =3D SDHOST_DATA_FROM_THIS (This); + PciIo =3D SdHostData->PciIo; + Mask =3D 0; + ErrStatus =3D 0; + + if (ResetType =3D=3D Reset_Auto) { + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_ERINTSTS, + 1, + &ErrStatus + ); + + if ((ErrStatus & 0xF) !=3D 0) { + // + // Command Line + // + Mask |=3D BIT1; + } + if ((ErrStatus & 0x70) !=3D 0) { + // + // Data Line + // + Mask |=3D BIT2; + } + } + + if (ResetType =3D=3D Reset_DAT || ResetType =3D=3D Reset_DAT_CMD) { + Mask |=3D BIT2; + } + if (ResetType =3D=3D Reset_CMD || ResetType =3D=3D Reset_DAT_CMD) { + Mask |=3D BIT1; + } + if (ResetType =3D=3D Reset_All) { + Mask =3D BIT0; + } + + if (Mask =3D=3D 0) { + return EFI_SUCCESS; + } + + // + // To improve SD stability, we zero the MMIO_CLKCTL register and + // stall for 50 microseconds before reseting the controller. We + // restore the register setting following the reset operation. + // + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_CLKCTL, + 1, + &SaveClkCtl + ); + + ZeroClkCtl =3D (UINT16) 0; + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_CLKCTL, + 1, + &ZeroClkCtl + ); + + gBS->Stall (50); + + // + // Reset the SD host controller + // + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint8, + 0, + (UINT64) MMIO_SWRST, + 1, + &Mask + ); + + Data =3D 0; + TimeOutCount =3D TIME_OUT_1S; + do { + + gBS->Stall (1 * 1000); + + TimeOutCount --; + + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint8, + 0, + (UINT64) MMIO_SWRST, + 1, + &Data + ); + + if ((Data & Mask) =3D=3D 0) { + break; + } + } while (TimeOutCount > 0); + + // + // We now restore the MMIO_CLKCTL register which we set to 0 above. + // + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_CLKCTL, + 1, + &SaveClkCtl + ); + + if (TimeOutCount =3D=3D 0) { + DEBUG ((EFI_D_ERROR, "ResetSdHost: Time out \r\n")); + return EFI_TIMEOUT; + } + + return EFI_SUCCESS; +} + + +/** + Enable auto stop on the host controller. + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTO= COL instance. + @param[in] Enable TRUE to enable, FALSE to disable. + + @retval EFI_SUCCESS + @retval EFI_TIMEOUT + +**/ +EFI_STATUS +EFIAPI +EnableAutoStopCmd ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN BOOLEAN Enable + ) +{ + SDHOST_DATA *SdHostData; + + SdHostData =3D SDHOST_DATA_FROM_THIS (This); + + SdHostData->IsAutoStopCmd =3D Enable; + + return EFI_SUCCESS; +} + + +/** + Set the Block length on the host controller. + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTO= COL instance. + @param[in] BlockLength card supportes block length. + + @retval EFI_SUCCESS + @retval EFI_TIMEOUT + +**/ +EFI_STATUS +EFIAPI +SetBlockLength ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN UINT32 BlockLength + ) +{ + SDHOST_DATA *SdHostData; + + SdHostData =3D SDHOST_DATA_FROM_THIS (This); + + DEBUG ((EFI_D_INFO, "Block length on the host controller: %d \r\n", Bloc= kLength)); + SdHostData->BlockLength =3D BlockLength; + + return EFI_SUCCESS; +} + + +/** + Find whether these is a card inserted into the slot. If so init the host. + If not, return EFI_NOT_FOUND. + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTO= COL instance. + + @retval EFI_SUCCESS + @retval EFI_NOT_FOUND + +**/ +EFI_STATUS +EFIAPI +DetectCardAndInitHost ( + IN EFI_SD_HOST_IO_PROTOCOL *This + ) +{ + SDHOST_DATA *SdHostData; + EFI_PCI_IO_PROTOCOL *PciIo; + UINT32 Data; + EFI_STATUS Status; + UINT8 Voltages[] =3D { 33, 30, 18 }; + UINTN Loop; + + SdHostData =3D SDHOST_DATA_FROM_THIS (This); + PciIo =3D SdHostData->PciIo; + Status =3D EFI_NOT_FOUND; + + Data =3D 0; + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint32, + 0, + (UINT64) MMIO_PSTATE, + 1, + &Data + ); + + if ((Data & (BIT16 | BIT17 | BIT18)) !=3D (BIT16 | BIT17 | BIT18)) { + // + // Has no card inserted + // + DEBUG ((EFI_D_INFO, "DetectCardAndInitHost: No Cards \r\n")); + Status =3D EFI_NOT_FOUND; + goto Exit; + } + DEBUG ((EFI_D_INFO, "DetectCardAndInitHost: Find Cards \r\n")); + + Status =3D EFI_NOT_FOUND; + for (Loop =3D 0; Loop < sizeof (Voltages); Loop++) { + DEBUG (( + EFI_D_INFO, + "DetectCardAndInitHost: SetHostVoltage %d.%dV \r\n", + Voltages[Loop] / 10, + Voltages[Loop] % 10 + )); + Status =3D SetHostVoltage (This, Voltages[Loop]); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_INFO, "DetectCardAndInitHost set voltages: [failed]\n"= )); + } else { + DEBUG ((EFI_D_INFO, "DetectCardAndInitHost set voltages: [success]\n= ")); + break; + } + } + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "DetectCardAndInitHost: Fail to set voltage \r\n"= )); + goto Exit; + } + + Status =3D SetClockFrequency (This, FREQUENCY_OD); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "DetectCardAndInitHost: Fail to set frequency \r\= n")); + goto Exit; + } + SetBusWidth (This, 1); + + // + // Enable normal status change + // + Data =3D (BIT0 | BIT1); + + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_NINTEN, + 1, + &Data + ); + + // + // Enable error status change + // + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_ERINTEN, + 1, + &Data + ); + + Data |=3D (BIT0 | BIT1 | BIT2 | BIT3 | BIT4 | BIT5 | BIT6 | BIT7 | BIT8); + + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_ERINTEN, + 1, + &Data + ); + + // + // Data transfer Timeout control + // + Data =3D 0x0E; + + PciIo->Mem.Write ( + PciIo, + EfiPciIoWidthUint8, + 0, + (UINT64) MMIO_TOCTL, + 1, + &Data + ); + // + // Set Default Bus width as 1 bit + // + +Exit: + return Status; + +} + + +/** + Entry point for EFI drivers. + + @param[in] ImageHandle EFI_HANDLE. + @param[in] SystemTable EFI_SYSTEM_TABLE. + + @retval EFI_SUCCESS Driver is successfully loaded. + @retval Others Failed. + +**/ +EFI_STATUS +EFIAPI +InitializeSdController ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + return EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gSdControllerDriverBinding, + ImageHandle, + &gSdControllerName, + &gSdControllerName2 + ); +} + + +/** + Test to see if this driver supports ControllerHandle. Any + ControllerHandle that has SdHostIoProtocol installed will be supported. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to test. + @param[in] RemainingDevicePath Not used. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_UNSUPPORTED This driver does not support this devi= ce. + +**/ +EFI_STATUS +EFIAPI +SdControllerSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) +{ + EFI_STATUS OpenStatus; + EFI_STATUS Status; + EFI_PCI_IO_PROTOCOL *PciIo; + PCI_CLASSC PciClass; + EFI_SD_HOST_IO_PROTOCOL *SdHostIo; + UINTN Seg, Bus, Dev, Func; + + Status =3D gBS->OpenProtocol ( + Controller, + &gEfiSdHostIoProtocolGuid, + (VOID **) &SdHostIo, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (!EFI_ERROR (Status)) { + DEBUG (( DEBUG_INFO, "SdHost controller is already started\n")); + return EFI_ALREADY_STARTED; + } + + // + // Test whether there is PCI IO Protocol attached on the controller hand= le. + // + OpenStatus =3D gBS->OpenProtocol ( + Controller, + &gEfiPciIoProtocolGuid, + (VOID **) &PciIo, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + + if (EFI_ERROR (OpenStatus)) { + return OpenStatus; + } + + Status =3D PciIo->Pci.Read ( + PciIo, + EfiPciIoWidthUint8, + PCI_CLASSCODE_OFFSET, + sizeof (PCI_CLASSC) / sizeof (UINT8), + &PciClass + ); + + if (EFI_ERROR (Status)) { + Status =3D EFI_UNSUPPORTED; + goto ON_EXIT; + } + + // + // Test whether the controller belongs to SD type + // + if ((PciClass.BaseCode !=3D PCI_CLASS_SYSTEM_PERIPHERAL) || + (PciClass.SubClassCode !=3D PCI_SUBCLASS_SD_HOST_CONTROLLER) || + ((PciClass.PI !=3D PCI_IF_STANDARD_HOST_NO_DMA) && (PciClass.PI !=3D= PCI_IF_STANDARD_HOST_SUPPORT_DMA)) + ) { + + Status =3D EFI_UNSUPPORTED; + goto ON_EXIT; + } + + Status =3D PciIo->GetLocation ( + PciIo, + &Seg, + &Bus, + &Dev, + &Func + ); + + if (EFI_ERROR (Status)) { + Status =3D EFI_UNSUPPORTED; + goto ON_EXIT; + } + + if ((Seg !=3D 0) || (Bus !=3D 0) || (Dev !=3D 27) || (Func !=3D 0)) { + // + // This is not the SD controller, bail. + // + Status =3D EFI_UNSUPPORTED; + goto ON_EXIT; + } + + DEBUG ((EFI_D_INFO, "%a(#%d) - Seg %d, bus:%d, Dev:%d, Func:%d\n", __FUN= CTION__, __LINE__, Seg, Bus, Dev, Func)); + +ON_EXIT: + gBS->CloseProtocol ( + Controller, + &gEfiPciIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + + return Status; +} + + +/** + Starting the SD Host Controller Driver. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to test. + @param[in] RemainingDevicePath Not used. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_UNSUPPORTED This driver does not support this devi= ce. + @retval EFI_DEVICE_ERROR This driver cannot be started due to d= evice Error. + EFI_OUT_OF_RESOURCES- Failed due to re= source shortage. + +**/ +EFI_STATUS +EFIAPI +SdControllerStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) +{ + EFI_STATUS Status; + EFI_PCI_IO_PROTOCOL *PciIo; + SDHOST_DATA *SdHostData; + UINT32 Data; + + SdHostData =3D NULL; + Data =3D 0; + + // + // Open PCI I/O Protocol and save pointer to open protocol + // in private data area. + // + Status =3D gBS->OpenProtocol ( + Controller, + &gEfiPciIoProtocolGuid, + (VOID **) &PciIo, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Enable the SD Host Controller MMIO space + // + Status =3D PciIo->Attributes ( + PciIo, + EfiPciIoAttributeOperationEnable, + EFI_PCI_DEVICE_ENABLE, + NULL + ); + + if (EFI_ERROR (Status)) { + Status =3D EFI_OUT_OF_RESOURCES; + goto Exit; + } + + SdHostData =3D (SDHOST_DATA *) AllocateZeroPool (sizeof (SDHOST_DATA)); + if (SdHostData =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + goto Exit; + } + + SdHostData->Signature =3D SDHOST_DATA_SIGNATURE; + SdHostData->PciIo =3D PciIo; + + CopyMem (&SdHostData->SdHostIo, &mSdHostIo, sizeof (EFI_SD_HOST_IO_PROTO= COL)); + + ResetSdHost (&SdHostData->SdHostIo, Reset_All); + + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint16, + 0, + (UINT64) MMIO_CTRLRVER, + 1, + &Data + ); + + SdHostData->SdHostIo.HostCapability.HostVersion =3D Data & 0xFF; + DEBUG ((EFI_D_INFO, "SdHostDriverBindingStart: HostVersion 0x%x \r\n", S= dHostData->SdHostIo.HostCapability.HostVersion)); + + PciIo->Mem.Read ( + PciIo, + EfiPciIoWidthUint32, + 0, + (UINT64) MMIO_CAP, + 1, + &Data + ); + + DEBUG ((EFI_D_INFO, "SdHostDriverBindingStart: MMIO_CAP 0x%x \r\n", Data= )); + if ((Data & BIT18) !=3D 0) { + SdHostData->SdHostIo.HostCapability.BusWidth8 =3D TRUE; + } + + if ((Data & BIT21) !=3D 0) { + SdHostData->SdHostIo.HostCapability.HighSpeedSupport =3D TRUE; + } + + if ((Data & BIT24) !=3D 0) { + SdHostData->SdHostIo.HostCapability.V33Support =3D TRUE; + } + + if ((Data & BIT25) !=3D 0) { + SdHostData->SdHostIo.HostCapability.V30Support =3D TRUE; + } + + if ((Data & BIT26) !=3D 0) { + SdHostData->SdHostIo.HostCapability.V18Support =3D TRUE; + } + + SdHostData->SdHostIo.HostCapability.BusWidth4 =3D TRUE; + + if (SdHostData->SdHostIo.HostCapability.HostVersion < SDHCI_SPEC_300) { + SdHostData->BaseClockInMHz =3D (Data >> 8) & 0x3F; + } else { + SdHostData->BaseClockInMHz =3D (Data >> 8) & 0xFF; + + } + + SdHostData->BlockLength =3D 512 << ((Data >> 16) & 0x03); + DEBUG ((EFI_D_INFO, "SdHostDriverBindingStart: BlockLength 0x%x \r\n", S= dHostData->BlockLength)); + SdHostData->IsAutoStopCmd =3D TRUE; + + Status =3D gBS->InstallProtocolInterface ( + &Controller, + &gEfiSdHostIoProtocolGuid, + EFI_NATIVE_INTERFACE, + &SdHostData->SdHostIo + ); + + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Install the component name protocol + // + SdHostData->ControllerNameTable =3D NULL; + + AddUnicodeString2 ( + "eng", + gSdControllerName.SupportedLanguages, + &SdHostData->ControllerNameTable, + L"SD Host Controller", + TRUE + ); + + AddUnicodeString2 ( + "en", + gSdControllerName2.SupportedLanguages, + &SdHostData->ControllerNameTable, + L"SD Host Controller", + FALSE + ); + +Exit: + if (EFI_ERROR (Status)) { + if (SdHostData !=3D NULL) { + FreePool (SdHostData); + } + } + + return Status; +} + + +/** + Stop this driver on ControllerHandle. Support stoping any child handles + created by this driver. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to stop driver on. + @param[in] NumberOfChildren Number of Children in the ChildHandleB= uffer. + @param[in] ChildHandleBuffer List of handles for the children we ne= ed to stop. + + @retval EFI_SUCCESS + @retval others + +**/ +EFI_STATUS +EFIAPI +SdControllerStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ) +{ + EFI_STATUS Status; + EFI_SD_HOST_IO_PROTOCOL *SdHostIo; + SDHOST_DATA *SdHostData; + + Status =3D gBS->OpenProtocol ( + Controller, + &gEfiSdHostIoProtocolGuid, + (VOID **) &SdHostIo, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + // + // Test whether the Controller handler passed in is a valid + // Usb controller handle that should be supported, if not, + // return the error status directly + // + if (EFI_ERROR (Status)) { + return Status; + } + + SetHostVoltage (SdHostIo, 0); + + SdHostData =3D SDHOST_DATA_FROM_THIS(SdHostIo); + + // + // Uninstall Block I/O protocol from the device handle + // + Status =3D gBS->UninstallProtocolInterface ( + Controller, + &gEfiSdHostIoProtocolGuid, + SdHostIo + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + FreeUnicodeStringTable (SdHostData->ControllerNameTable); + + FreePool (SdHostData); + + gBS->CloseProtocol ( + Controller, + &gEfiPciIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + + return EFI_SUCCESS; +} + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdCon= trollerDxe/SdController.h b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sd= io/Dxe/SD/SdControllerDxe/SdController.h new file mode 100644 index 0000000..a702565 --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdController= Dxe/SdController.h @@ -0,0 +1,314 @@ +/** @file + The definition for SD host controller driver model and HC protocol routi= nes. + + Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMPLI= ED. + +**/ + +#ifndef _SD_CONTROLLER_H_ +#define _SD_CONTROLLER_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "ComponentName.h" +#include "SdHostIo.h" + +extern EFI_DRIVER_BINDING_PROTOCOL gSdControllerDriverBinding; +extern EFI_COMPONENT_NAME_PROTOCOL gSdControllerName; +extern EFI_COMPONENT_NAME2_PROTOCOL gSdControllerName2; + +#define SDHOST_DATA_SIGNATURE SIGNATURE_32 ('s', 'd', 'h', 's') + +#define BLOCK_SIZE 0x200 +#define TIME_OUT_1S 1000 + +#pragma pack(1) + +// +// PCI Class Code structure +// +typedef struct { + UINT8 PI; + UINT8 SubClassCode; + UINT8 BaseCode; +} PCI_CLASSC; + +#pragma pack() + +typedef struct { + UINTN Signature; + EFI_SD_HOST_IO_PROTOCOL SdHostIo; + EFI_PCI_IO_PROTOCOL *PciIo; + BOOLEAN IsAutoStopCmd; + UINT32 BaseClockInMHz; + UINT32 CurrentClockInKHz; + UINT32 BlockLength; + EFI_UNICODE_STRING_TABLE *ControllerNameTable; +} SDHOST_DATA; + +#define SDHOST_DATA_FROM_THIS(a) \ + CR(a, SDHOST_DATA, SdHostIo, SDHOST_DATA_SIGNATURE) + +/** + Test to see if this driver supports ControllerHandle. Any + ControllerHandle that has SdHostIoProtocol installed will be supported. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to test. + @param[in] RemainingDevicePath Not used. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_UNSUPPORTED This driver does not support this devic= e. + +**/ +EFI_STATUS +EFIAPI +SdControllerSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ); + +/** + Starting the SD Host Controller Driver. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to test. + @param[in] RemainingDevicePath Not used. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_UNSUPPORTED This driver does not support this devic= e. + @retval EFI_DEVICE_ERROR This driver cannot be started due to de= vice Error. + EFI_OUT_OF_RESOURCES- Failed due to res= ource shortage. + +**/ +EFI_STATUS +EFIAPI +SdControllerStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ); + +/** + Stop this driver on ControllerHandle. Support stoping any child handles + created by this driver. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to stop driver on. + @param[in] NumberOfChildren Number of Children in the ChildHandleBu= ffer. + @param[in] ChildHandleBuffer List of handles for the children we nee= d to stop. + + @retval EFI_SUCCESS + @retval others + +**/ +EFI_STATUS +EFIAPI +SdControllerStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ); + +/** + The main function used to send the command to the card inserted into the= SD host slot. + It will assemble the arguments to set the command register and wait for = the command + and transfer completed until timeout. Then it will read the response reg= ister to fill + the ResponseData. + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTOC= OL instance. + @param[in] CommandIndex The command index to set the command i= ndex field of command register. + @param[in] Argument Command argument to set the argument f= ield of command register. + @param[in] DataType TRANSFER_TYPE, indicates no data, data= in or data out. + @param[in] Buffer Contains the data read from / write to= the device. + @param[in] BufferSize The size of the buffer. + @param[in] ResponseType RESPONSE_TYPE. + @param[in] TimeOut Time out value in 1 ms unit. + @param[out] ResponseData Depending on the ResponseType, such as= CSD or card status. + + @retval EFI_SUCCESS + @retval EFI_INVALID_PARAMETER + @retval EFI_OUT_OF_RESOURCES + @retval EFI_TIMEOUT + @retval EFI_DEVICE_ERROR + +**/ +EFI_STATUS +EFIAPI +SendCommand ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN UINT16 CommandIndex, + IN UINT32 Argument, + IN TRANSFER_TYPE DataType, + IN UINT8 *Buffer, OPTIONAL + IN UINT32 BufferSize, + IN RESPONSE_TYPE ResponseType, + IN UINT32 TimeOut, + OUT UINT32 *ResponseData OPTIONAL + ); + +/** + Set max clock frequency of the host, the actual frequency may not be the= same as MaxFrequency. + It depends on the max frequency the host can support, divider, and host = speed mode. + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTOC= OL instance. + @param[in] MaxFrequency Max frequency in HZ. + + @retval EFI_SUCCESS + @retval EFI_TIMEOUT + +**/ +EFI_STATUS +EFIAPI +SetClockFrequency ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN UINT32 MaxFrequencyInKHz + ); + +/** + Set bus width of the host controller + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTOC= OL instance. + @param[in] BusWidth Bus width in 1, 4, 8 bits. + + @retval EFI_SUCCESS + @retval EFI_INVALID_PARAMETER + +**/ +EFI_STATUS +EFIAPI +SetBusWidth ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN UINT32 BusWidth + ); + +/** + Set voltage which could supported by the host controller. + Support 0(Power off the host), 1.8V, 3.0V, 3.3V + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTOC= OL instance. + @param[in] Voltage Units in 0.1 V. + + @retval EFI_SUCCESS + @retval EFI_INVALID_PARAMETER + +**/ +EFI_STATUS +EFIAPI +SetHostVoltage ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN UINT32 Voltage + ); + +/** + Reset the host controller. + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTOC= OL instance. + @param[in] ResetAll TRUE to reset all. + + @retval EFI_SUCCESS + @retval EFI_TIMEOUT + +**/ +EFI_STATUS +EFIAPI +ResetSdHost ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN RESET_TYPE ResetType + ); + +/** + Enable auto stop on the host controller. + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTOC= OL instance. + @param[in] Enable TRUE to enable, FALSE to disable. + + @retval EFI_SUCCESS + @retval EFI_TIMEOUT + +**/ +EFI_STATUS +EFIAPI +EnableAutoStopCmd ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN BOOLEAN Enable + ); + +/** + Find whether these is a card inserted into the slot. If so init the host. + If not, return EFI_NOT_FOUND. + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTOC= OL instance. + + @retval EFI_SUCCESS + @retval EFI_NOT_FOUND + +**/ +EFI_STATUS +EFIAPI +DetectCardAndInitHost ( + IN EFI_SD_HOST_IO_PROTOCOL *This + ); + +/** + Set the Block length on the host controller. + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTOC= OL instance. + @param[in] BlockLength card supportes block length. + + @retval EFI_SUCCESS + @retval EFI_TIMEOUT + +**/ +EFI_STATUS +EFIAPI +SetBlockLength ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN UINT32 BlockLength + ); + +/** + Enable/Disable High Speed transfer mode + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTOCOL= instance. + @param[in] Enable TRUE to Enable, FALSE to Disable + + @retval EFI_SUCCESS + +**/ +EFI_STATUS +EFIAPI +SetHighSpeedMode ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN BOOLEAN Enable + ); + +EFI_STATUS +EFIAPI +SetDDRMode ( + IN EFI_SD_HOST_IO_PROTOCOL *This, + IN BOOLEAN Enable + ); + +#endif + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdCon= trollerDxe/SdControllerDxe.inf b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthClust= er/Sdio/Dxe/SD/SdControllerDxe/SdControllerDxe.inf new file mode 100644 index 0000000..499c9b7 --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdController= Dxe/SdControllerDxe.inf @@ -0,0 +1,59 @@ +## @file +# Component Description File For SdControllerDxe Module. +# +# Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the B= SD License +# which accompanies this distribution. The full text of the license may b= e 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 IM= PLIED. +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D SdController + FILE_GUID =3D 90A330BD-6F89-4900-933A-C25EB4356348 + MODULE_TYPE =3D UEFI_DRIVER + VERSION_STRING =3D 1.0 + ENTRY_POINT =3D InitializeSdController + +# +# The following information is for reference only and not required by the = build tools. +# +# VALID_ARCHITECTURES =3D IA32 X64 IPF EBC +# +# DRIVER_BINDING =3D gSdControllerDriverBinding +# COMPONENT_NAME =3D gSdControllerName +# COMPONENT_NAME2 =3D gSdControllerName2 +# + +[Sources] + SdController.c + SdController.h + ComponentName.c + ComponentName.h + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + BroxtonSiPkg/BroxtonSiPkg.dec + +[LibraryClasses] + MemoryAllocationLib + BaseLib + UefiLib + UefiBootServicesTableLib + UefiDriverEntryPoint + BaseMemoryLib + DebugLib + PcdLib + +[Protocols] + gEfiPciIoProtocolGuid ## TO_START + gEfiSdHostIoProtocolGuid ## BY_START + +[FeaturePcd] diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdMed= iaDeviceDxe/CEATA.c b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe= /SD/SdMediaDeviceDxe/CEATA.c new file mode 100644 index 0000000..cb7a1bb --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdMediaDevic= eDxe/CEATA.c @@ -0,0 +1,638 @@ +/** @file + CEATA specific functions implementation + + Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include "SdMediaDevice.h" + +/** + Send RW_MULTIPLE_REGISTER command + + @param[in] CardData Pointer to CARD_DATA. + @param[in] Address Register address. + @param[in] ByteCount Buffer size. + @param[in] Write TRUE means write, FALSE means rea= d. + @param[in] Buffer Buffer pointer. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +ReadWriteMultipleRegister ( + IN CARD_DATA *CardData, + IN UINT16 Address, + IN UINT8 ByteCount, + IN BOOLEAN Write, + IN UINT8 *Buffer + ) +{ + EFI_STATUS Status; + UINT32 Argument; + + Status =3D EFI_SUCCESS; + + if ((Address % 4 !=3D 0) || (ByteCount % 4 !=3D 0)) { + Status =3D EFI_INVALID_PARAMETER; + goto Exit; + } + + Argument =3D (Address << 16) | ByteCount; + if (Write) { + Argument |=3D BIT31; + } + + if (Write) { + CopyMem (CardData->AlignedBuffer, Buffer, ByteCount); + + Status =3D SendCommand ( + CardData, + RW_MULTIPLE_REGISTER, + Argument, + OutData, + CardData->AlignedBuffer, + ByteCount, + ResponseR1b, + TIMEOUT_DATA, + (UINT32 *) &(CardData->CardStatus) + ); + + } else { + Status =3D SendCommand ( + CardData, + RW_MULTIPLE_REGISTER, + Argument, + InData, + CardData->AlignedBuffer, + ByteCount, + ResponseR1, + TIMEOUT_DATA, + (UINT32 *) &(CardData->CardStatus) + ); + + if (!EFI_ERROR (Status)) { + CopyMem (Buffer, CardData->AlignedBuffer, ByteCount); + } + } +Exit: + return Status; +} + + +/** + Send ReadWriteMultipleBlock command with RW_MULTIPLE_REGISTER command + + @param[in] CardData Pointer to CARD_DATA. + @param[in] DataUnitCount Buffer size in 512 bytes unit. + @param[in] Write TRUE means write, FALSE means rea= d. + @param[in] Buffer Buffer pointer. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +ReadWriteMultipleBlock ( + IN CARD_DATA *CardData, + IN UINT16 DataUnitCount, + IN BOOLEAN Write, + IN UINT8 *Buffer + ) +{ + EFI_STATUS Status; + EFI_SD_HOST_IO_PROTOCOL *SdHostIo; + UINT32 TransferLength; + + Status =3D EFI_SUCCESS; + SdHostIo =3D CardData->SdHostIo; + + TransferLength =3D DataUnitCount * DATA_UNIT_SIZE; + if (TransferLength > SdHostIo->HostCapability.BoundarySize) { + return EFI_INVALID_PARAMETER; + } + + if (Write) { + CopyMem (CardData->AlignedBuffer, Buffer, TransferLength); + + Status =3D SendCommand ( + CardData, + RW_MULTIPLE_BLOCK, + (DataUnitCount | BIT31), + OutData, + CardData->AlignedBuffer, + TransferLength, + ResponseR1b, + TIMEOUT_DATA, + (UINT32 *) &(CardData->CardStatus) + ); + + } else { + Status =3D SendCommand ( + CardData, + RW_MULTIPLE_BLOCK, + DataUnitCount, + InData, + CardData->AlignedBuffer, + TransferLength, + ResponseR1, + TIMEOUT_DATA, + (UINT32 *) &(CardData->CardStatus) + ); + + if (!EFI_ERROR (Status)) { + CopyMem (Buffer, CardData->AlignedBuffer, TransferLength); + } + } + + return Status; +} + + +/** + Send software reset + + @param[in] CardData Pointer to CARD_DATA. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +SoftwareReset ( + IN CARD_DATA *CardData + ) +{ + EFI_STATUS Status; + UINT8 Data; + UINT32 TimeOut; + + Data =3D BIT2; + + Status =3D FastIO (CardData, Reg_Control, &Data, TRUE); + if (EFI_ERROR (Status)) { + goto Exit; + } + + TimeOut =3D 5 * 1000; + + do { + gBS->Stall (1 * 1000); + Status =3D FastIO (CardData, Reg_Control, &Data, FALSE); + if (EFI_ERROR (Status)) { + goto Exit; + } + + if ((Data & BIT2) =3D=3D BIT2) { + break; + } + + TimeOut--; + } while (TimeOut > 0); + + if (TimeOut =3D=3D 0) { + Status =3D EFI_TIMEOUT; + goto Exit; + } + + Data &=3D ~BIT2; + Status =3D FastIO (CardData, Reg_Control, &Data, TRUE); + + TimeOut =3D 5 * 1000; + + do { + gBS->Stall (1 * 1000); + Status =3D FastIO (CardData, Reg_Control, &Data, FALSE); + if (EFI_ERROR (Status)) { + goto Exit; + } + + if ((Data & BIT2) !=3D BIT2) { + break; + } + + TimeOut--; + } while (TimeOut > 0); + + if (TimeOut =3D=3D 0) { + Status =3D EFI_TIMEOUT; + goto Exit; + } + +Exit: + return Status; +} + + +/** + SendATACommand specificed in Taskfile + + @param[in] CardData Pointer to CARD_DATA. + @param[in] TaskFile Pointer to TASK_FILE. + @param[in] Write TRUE means write, FALSE means rea= d. + @param[in] Buffer If NULL, means no data transfer, = neither read nor write. + @param[in] SectorCount Buffer size in 512 bytes unit. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +SendATACommand ( + IN CARD_DATA *CardData, + IN TASK_FILE *TaskFile, + IN BOOLEAN Write, + IN UINT8 *Buffer, + IN UINT16 SectorCount + ) +{ + EFI_STATUS Status; + UINT8 Data; + UINT32 TimeOut; + + // + // Write register + // + Status =3D ReadWriteMultipleRegister ( + CardData, + 0, + sizeof (TASK_FILE), + TRUE, + (UINT8 *) TaskFile + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "ReadWriteMultipleRegister 0x%x\n", Status)); + goto Exit; + } + + TimeOut =3D 5000; + do { + gBS->Stall (1 * 1000); + Data =3D 0; + Status =3D FastIO ( + CardData, + Reg_Command_Status, + &Data, + FALSE + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + if (((Data & BIT7) =3D=3D 0) && ((Data & BIT6) =3D=3D BIT6)) { + break; + } + + TimeOut --; + } while (TimeOut > 0); + + if (TimeOut =3D=3D 0) { + DEBUG ((EFI_D_ERROR, "ReadWriteMultipleRegister FastIO EFI_TIMEOUT 0x%= x\n", Data)); + Status =3D EFI_TIMEOUT; + goto Exit; + } + + if (Buffer !=3D NULL) { + Status =3D ReadWriteMultipleBlock ( + CardData, + SectorCount, + Write, + (UINT8 *) Buffer + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "ReadWriteMultipleBlock EFI_TIMEOUT 0x%x\n", St= atus)); + goto Exit; + } + + TimeOut =3D 5 * 1000; + do { + gBS->Stall (1 * 1000); + Data =3D 0; + Status =3D FastIO ( + CardData, + Reg_Command_Status, + &Data, + FALSE + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + if (((Data & BIT7) =3D=3D 0) && ((Data & BIT3) =3D=3D 0)) { + break; + } + + TimeOut --; + } while (TimeOut > 0); + if (TimeOut =3D=3D 0) { + DEBUG ((EFI_D_ERROR, "ReadWriteMultipleBlock FastIO EFI_TIMEOUT 0x%x= \n", Data)); + Status =3D EFI_TIMEOUT; + goto Exit; + } + + if (((Data & BIT6) =3D=3D BIT6) && (Data & BIT0) =3D=3D 0) { + Status =3D EFI_SUCCESS; + } else { + Status =3D EFI_DEVICE_ERROR; + } + } + +Exit: + if (EFI_ERROR (Status)) { + SoftwareReset (CardData); + } + + return Status; +} + + +/** + IDENTIFY_DEVICE command + + @param[in] CardData Pointer to CARD_DATA. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +IndentifyDevice ( + IN CARD_DATA *CardData + ) +{ + EFI_STATUS Status; + + ZeroMem (&CardData->TaskFile, sizeof (TASK_FILE)); + + // + // The host only supports nIEN =3D 0 + // + CardData->TaskFile.Command_Status =3D IDENTIFY_DEVICE; + Status =3D SendATACommand ( + CardData, + &CardData->TaskFile, + FALSE, + (UINT8 *) &(CardData->IndentifyDeviceData), + 1 + ); + + return Status; +} + + +/** + FLUSH_CACHE_EXT command + + @param[in] CardData Pointer to CARD_DATA. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +FlushCache ( + IN CARD_DATA *CardData + ) +{ + + return EFI_SUCCESS; +} + + +/** + STANDBY_IMMEDIATE command + + @param[in] CardData Pointer to CARD_DATA. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +StandByImmediate ( + IN CARD_DATA *CardData + ) +{ + EFI_STATUS Status; + + ZeroMem (&CardData->TaskFile, sizeof (TASK_FILE)); + + // + // The host only supports nIEN =3D 0 + // + CardData->TaskFile.Command_Status =3D STANDBY_IMMEDIATE; + Status =3D SendATACommand ( + CardData, + &CardData->TaskFile, + FALSE, + NULL, + 0 + ); + + return Status; +} + + +/** + READ_DMA_EXT command + + @param[in] CardData Pointer to CARD_DATA. + @param[in] LBA The starting logical block addres= s to read from on the device. + @param[in] Buffer A pointer to the destination buff= er for the data. The caller + is responsible for either having = implicit or explicit ownership + of the buffer. + @param[in] SectorCount Size in 512 bytes unit. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +ReadDMAExt ( + IN CARD_DATA *CardData, + IN EFI_LBA LBA, + IN UINT8 *Buffer, + IN UINT16 SectorCount + ) +{ + EFI_STATUS Status; + + ZeroMem (&CardData->TaskFile, sizeof (TASK_FILE)); + + // + // The host only supports nIEN =3D 0 + // + CardData->TaskFile.Command_Status =3D READ_DMA_EXT; + + CardData->TaskFile.SectorCount =3D (UINT8) SectorCount; + CardData->TaskFile.SectorCount_Exp =3D (UINT8) (SectorCount >> 8); + + CardData->TaskFile.LBALow =3D (UINT8) LBA; + CardData->TaskFile.LBAMid =3D (UINT8) RShiftU64 (LBA, 8); + CardData->TaskFile.LBAHigh =3D (UINT8) RShiftU64 (LBA, 16); + + CardData->TaskFile.LBALow_Exp =3D (UINT8) RShiftU64 (LBA, 24); + CardData->TaskFile.LBAMid_Exp =3D (UINT8) RShiftU64 (LBA, 32); + CardData->TaskFile.LBAHigh_Exp =3D (UINT8) RShiftU64 (LBA, 40); + + Status =3D SendATACommand ( + CardData, + &CardData->TaskFile, + FALSE, + Buffer, + SectorCount + ); + + return Status; +} + + +/** + WRITE_DMA_EXT command + + @param[in] CardData Pointer to CARD_DATA. + @param[in] LBA The starting logical block addres= s to read from on the device. + @param[in] Buffer A pointer to the destination buff= er for the data. The caller + is responsible for either having = implicit or explicit ownership + of the buffer. + @param[in] SectorCount Size in 512 bytes unit. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +WriteDMAExt ( + IN CARD_DATA *CardData, + IN EFI_LBA LBA, + IN UINT8 *Buffer, + IN UINT16 SectorCount + ) +{ + EFI_STATUS Status; + + ZeroMem (&CardData->TaskFile, sizeof (TASK_FILE)); + + // + // The host only supports nIEN =3D 0 + // + CardData->TaskFile.Command_Status =3D WRITE_DMA_EXT; + + CardData->TaskFile.SectorCount =3D (UINT8) SectorCount; + CardData->TaskFile.SectorCount_Exp =3D (UINT8) (SectorCount >> 8); + + CardData->TaskFile.LBALow =3D (UINT8) LBA; + CardData->TaskFile.LBAMid =3D (UINT8) RShiftU64 (LBA, 8); + CardData->TaskFile.LBAHigh =3D (UINT8) RShiftU64 (LBA, 16); + + CardData->TaskFile.LBALow_Exp =3D (UINT8) RShiftU64 (LBA, 24); + CardData->TaskFile.LBAMid_Exp =3D (UINT8) RShiftU64 (LBA, 32); + CardData->TaskFile.LBAHigh_Exp =3D (UINT8) RShiftU64 (LBA, 40); + + Status =3D SendATACommand ( + CardData, + &CardData->TaskFile, + TRUE, + Buffer, + SectorCount + ); + + return Status; + +} + + +/** + Judge whether it is CE-ATA device or not. + + @param[in] CardData Pointer to CARD_DATA. + + @retval TRUE + @retval FALSE + +**/ +BOOLEAN +IsCEATADevice ( + IN CARD_DATA *CardData + ) +{ + EFI_STATUS Status; + + Status =3D ReadWriteMultipleRegister ( + CardData, + 0, + sizeof (TASK_FILE), + FALSE, + (UINT8 *) &CardData->TaskFile + ); + + if (EFI_ERROR (Status)) { + // + // To bring back the normal MMC card to work + // + CardData->SdHostIo->ResetSdHost (CardData->SdHostIo, Reset_DAT_CMD); + return FALSE; + } + + if (CardData->TaskFile.LBAMid =3D=3D CE_ATA_SIG_CE && + CardData->TaskFile.LBAHigh =3D=3D CE_ATA_SIG_AA + ) { + // + // Disable Auto CMD for CE-ATA + // + CardData->SdHostIo->EnableAutoStopCmd (CardData->SdHostIo, FALSE); + return TRUE; + } + + return FALSE; +} + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdMed= iaDeviceDxe/CEATABlockIo.c b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/S= dio/Dxe/SD/SdMediaDeviceDxe/CEATABlockIo.c new file mode 100644 index 0000000..55fd085 --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdMediaDevic= eDxe/CEATABlockIo.c @@ -0,0 +1,384 @@ +/** @file + Block I/O protocol for CE-ATA device + + Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include "SdMediaDevice.h" + + +/** + Implements EFI_BLOCK_IO_PROTOCOL.Reset() function. + + @param[in] This The EFI_BLOCK_IO_PROTOCOL instance. + @param[in] ExtendedVerification Indicates that the driver may perform= a more exhaustive. + verification operation of the device = during reset. + (This parameter is ingored in this dr= iver.) + + @retval EFI_SUCCESS Success + +**/ +EFI_STATUS +EFIAPI +CEATABlockReset ( + IN EFI_BLOCK_IO_PROTOCOL *This, + IN BOOLEAN ExtendedVerification + ) +{ + EFI_STATUS Status; + CARD_DATA *CardData; + EFI_SD_HOST_IO_PROTOCOL *SdHostIo; + + CardData =3D CARD_DATA_FROM_THIS (This); + SdHostIo =3D CardData->SdHostIo; + + if (!ExtendedVerification) { + Status =3D SoftwareReset (CardData); + } else { + Status =3D SdHostIo->ResetSdHost (SdHostIo, Reset_DAT_CMD); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "CEATABlockReset: Fail to ResetSDHost\n" )); + return Status; + } + Status =3D MMCSDCardInit (CardData); + } + + return Status; +} + + +/** + Implements EFI_BLOCK_IO_PROTOCOL.ReadBlocks() function. + + @param[in] This The EFI_BLOCK_IO_PROTOCOL instanc= e. + @param[in] MediaId The media id that the write reque= st is for. + @param[in] LBA The starting logical block addres= s to read from on the device. + The caller is responsible for wri= ting to only legitimate locations. + @param[in] BufferSize The size of the Buffer in bytes. = This must be a multiple of the + intrinsic block size of the devic= e. + @param[out] Buffer A pointer to the destination buff= er for the data. The caller + is responsible for either having = implicit or explicit ownership + of the buffer. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +EFIAPI +CEATABlockReadBlocks ( + IN EFI_BLOCK_IO_PROTOCOL *This, + IN UINT32 MediaId, + IN EFI_LBA LBA, + IN UINTN BufferSize, + OUT VOID *Buffer + ) +{ + EFI_STATUS Status; + CARD_DATA *CardData; + UINT32 TransferSize; + UINT8 *pBuf; + UINT32 Index; + UINT64 Address; + UINT32 Remainder; + UINT64 CEATALBA; + UINT32 BoundarySize; + + Status =3D EFI_SUCCESS; + CardData =3D CARD_DATA_FROM_THIS (This); + pBuf =3D Buffer; + Index =3D 0; + Address =3D MultU64x32 (LBA, CardData->BlockIoMedia.BlockSize); + BoundarySize =3D CardData->SdHostIo->HostCapability.BoundarySize; + + if (!Buffer) { + Status =3D EFI_INVALID_PARAMETER; + DEBUG ((EFI_D_ERROR, "CEATABlockReadBlocks:Invalid parameter\n" )); + goto Exit; + } + + if (MediaId !=3D CardData->BlockIoMedia.MediaId) { + Status =3D EFI_MEDIA_CHANGED; + DEBUG ((EFI_D_ERROR, "CEATABlockReadBlocks:Media changed\n" )); + goto Exit; + } + + if ((BufferSize % CardData->BlockIoMedia.BlockSize) !=3D 0) { + Status =3D EFI_BAD_BUFFER_SIZE; + DEBUG ((EFI_D_ERROR, "CEATABlockReadBlocks:Bad buffer size\n" )); + goto Exit; + } + + if (BufferSize =3D=3D 0) { + Status =3D EFI_SUCCESS; + goto Exit; + } + + if ((Address + BufferSize) > MultU64x32 (CardData->BlockIoMedia.LastBloc= k + 1, CardData->BlockIoMedia.BlockSize)) { + Status =3D EFI_INVALID_PARAMETER; + DEBUG ((EFI_D_ERROR, "CEATABlockReadBlocks:Invalid parameter\n" )); + goto Exit; + } + + do { + if (BufferSize < BoundarySize) { + TransferSize =3D (UINT32) BufferSize; + } else { + TransferSize =3D BoundarySize; + } + + Address +=3D Index * TransferSize; + CEATALBA =3D DivU64x32Remainder (Address, DATA_UNIT_SIZE, &Remainder); + ASSERT(Remainder =3D=3D 0); + + Status =3D ReadDMAExt ( + CardData, + CEATALBA, + pBuf, + (UINT16) (TransferSize / DATA_UNIT_SIZE) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Read Failed at 0x%x, Index %d, Size 0x%x\n", A= ddress, Index, TransferSize)); + This->Reset (This, TRUE); + goto Exit; + } + + BufferSize -=3D TransferSize; + pBuf +=3D TransferSize; + Index ++; + } while (BufferSize !=3D 0); + +Exit: + return Status; +} + + +/** + Implements EFI_BLOCK_IO_PROTOCOL.WriteBlocks() function. + + @param[in] This The EFI_BLOCK_IO_PROTOCOL instanc= e. + @param[in] MediaId The media id that the write reque= st is for. + @param[in] LBA The starting logical block addres= s to read from on the device. + The caller is responsible for wri= ting to only legitimate locations. + @param[in] BufferSize The size of the Buffer in bytes. = This must be a multiple of the + intrinsic block size of the devic= e. + @param[in] Buffer A pointer to the destination buff= er for the data. The caller + is responsible for either having = implicit or explicit ownership + of the buffer. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +EFIAPI +CEATABlockWriteBlocks ( + IN EFI_BLOCK_IO_PROTOCOL *This, + IN UINT32 MediaId, + IN EFI_LBA LBA, + IN UINTN BufferSize, + IN VOID *Buffer + ) +{ + EFI_STATUS Status; + CARD_DATA *CardData; + UINT32 TransferSize; + UINT8 *pBuf; + UINT32 Index; + UINT64 Address; + UINT32 Remainder; + UINT64 CEATALBA; + UINT32 BoundarySize; + + Status =3D EFI_SUCCESS; + CardData =3D CARD_DATA_FROM_THIS (This); + pBuf =3D Buffer; + Index =3D 0; + Address =3D MultU64x32 (LBA, CardData->BlockIoMedia.BlockSize); + BoundarySize =3D CardData->SdHostIo->HostCapability.BoundarySize; + + if (!Buffer) { + Status =3D EFI_INVALID_PARAMETER; + goto Exit; + } + + if (MediaId !=3D CardData->BlockIoMedia.MediaId) { + Status =3D EFI_MEDIA_CHANGED; + goto Exit; + } + + if ((BufferSize % CardData->BlockIoMedia.BlockSize) !=3D 0) { + Status =3D EFI_BAD_BUFFER_SIZE; + goto Exit; + } + + if (BufferSize =3D=3D 0) { + Status =3D EFI_SUCCESS; + goto Exit; + } + + if (CardData->BlockIoMedia.ReadOnly) { + Status =3D EFI_WRITE_PROTECTED; + goto Exit; + } + + if ((Address + BufferSize) > MultU64x32 (CardData->BlockIoMedia.LastBloc= k + 1, CardData->BlockIoMedia.BlockSize)) { + Status =3D EFI_INVALID_PARAMETER; + goto Exit; + } + + CardData->NeedFlush =3D TRUE; + + do { + if (BufferSize < BoundarySize) { + TransferSize =3D (UINT32) BufferSize; + } else { + TransferSize =3D BoundarySize; + } + + Address +=3D Index * TransferSize; + CEATALBA =3D DivU64x32Remainder (Address, DATA_UNIT_SIZE, &Remainder); + ASSERT (Remainder =3D=3D 0); + + Status =3D WriteDMAExt ( + CardData, + CEATALBA, + pBuf, + (UINT16) (TransferSize / DATA_UNIT_SIZE) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Write Failed at 0x%x, Index %d, Size 0x%x\n", = Address, Index, TransferSize)); + This->Reset (This, TRUE); + goto Exit; + } + + BufferSize -=3D TransferSize; + pBuf +=3D TransferSize; + Index ++; + } while (BufferSize !=3D 0); + +Exit: + return Status; +} + + +/** + Implements EFI_BLOCK_IO_PROTOCOL.FlushBlocks() function. + (In this driver, this function just returns EFI_SUCCESS.) + + @param[in] This The EFI_BLOCK_IO_PROTOCOL instance. + + @retval EFI_SUCCESS + @retval Others + +**/ +EFI_STATUS +EFIAPI +CEATABlockFlushBlocks ( + IN EFI_BLOCK_IO_PROTOCOL *This + ) +{ + + CARD_DATA *CardData; + + CardData =3D CARD_DATA_FROM_THIS (This); + + if (CardData->NeedFlush) { + CardData->NeedFlush =3D FALSE; + FlushCache (CardData); + } + + return EFI_SUCCESS; +} + + +/** + CEATA card BlockIo init function. + + @param[in] CardData Pointer to CARD_DATA. + + @retval EFI_SUCCESS + @retval Others + +**/ +EFI_STATUS +CEATABlockIoInit ( + IN CARD_DATA *CardData + ) +{ + EFI_STATUS Status; + UINT64 MaxSize; + UINT32 Remainder; + + // + // BlockIO protocol + // + CardData->BlockIo.Revision =3D EFI_BLOCK_IO_PROTOCOL_REVISION; + CardData->BlockIo.Media =3D &(CardData->BlockIoMedia); + CardData->BlockIo.Reset =3D CEATABlockReset; + CardData->BlockIo.ReadBlocks =3D CEATABlockReadBlocks ; + CardData->BlockIo.WriteBlocks =3D CEATABlockWriteBlocks; + CardData->BlockIo.FlushBlocks =3D CEATABlockFlushBlocks; + + CardData->BlockIoMedia.MediaId =3D 0; + CardData->BlockIoMedia.RemovableMedia =3D FALSE; + CardData->BlockIoMedia.MediaPresent =3D TRUE; + CardData->BlockIoMedia.LogicalPartition =3D FALSE; + + if (CardData->CSDRegister.PERM_WRITE_PROTECT | CardData->CSDRegister.TMP= _WRITE_PROTECT) { + CardData->BlockIoMedia.ReadOnly =3D TRUE; + } else { + CardData->BlockIoMedia.ReadOnly =3D FALSE; + } + + CardData->BlockIoMedia.WriteCaching =3D FALSE; + CardData->BlockIoMedia.IoAlign =3D 1; + + Status =3D IndentifyDevice (CardData); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + //Some device does not support this feature + // + if (CardData->IndentifyDeviceData.MaxWritesPerAddress =3D=3D 0) { + CardData->BlockIoMedia.ReadOnly =3D TRUE; + } + + CardData->BlockIoMedia.BlockSize =3D (1 << CardData->IndentifyDev= iceData.Sectorsize); + ASSERT (CardData->BlockIoMedia.BlockSize >=3D 12); + + + MaxSize =3D *(UINT64 *) (CardData->IndentifyDeviceData.MaximumLBA); + MaxSize =3D MultU64x32 (MaxSize, 512); + + Remainder =3D 0; + CardData->BlockNumber =3D DivU64x32Remainder (MaxSize, CardData->BlockIo= Media.BlockSize, &Remainder); + ASSERT (Remainder =3D=3D 0); + + CardData->BlockIoMedia.LastBlock =3D (EFI_LBA) (CardData->BlockNumber - = 1); + +Exit: + return Status; +} + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdMed= iaDeviceDxe/ComponentName.c b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/= Sdio/Dxe/SD/SdMediaDeviceDxe/ComponentName.c new file mode 100644 index 0000000..15fdaf6 --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdMediaDevic= eDxe/ComponentName.c @@ -0,0 +1,219 @@ +/** @file + UEFI Component Name(2) protocol implementation for SD media device drive= r. + + Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include "SdMediaDevice.h" + +// +// EFI Component Name Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gSdMediaDeviceN= ame =3D { + SdMediaDeviceGetDriverName, + SdMediaDeviceGetControllerName, + "eng" +}; + +// +// EFI Component Name 2 Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gSdMediaDeviceN= ame2 =3D { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) SdMediaDeviceGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) SdMediaDeviceGetControllerName, + "en" +}; + + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mSdMediaDeviceDrive= rNameTable[] =3D { + { "eng;en", L"UEFI MMC/SD Media Device Driver" }, + { NULL, NULL } +}; + + +// +// EFI Component Name Functions +// +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form o= f a + Unicode string. If the driver specified by This has a user readable name= in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver speci= fied + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2= _PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII= string + array indicating the language. This = is the + language of the driver name that the= caller is + requesting, and it must match one of= the + languages specified in SupportedLang= uages. The + number of languages supported by a d= river is up + to the driver writer. Language is sp= ecified + in RFC 3066 or ISO 639-2 language co= de format. + + @param[out] DriverName A pointer to the Unicode string to r= eturn. + This Unicode string is the name of t= he + driver specified by This in the lang= uage + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver sp= ecified by + This and the language specified by L= anguage was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does no= t support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +SdMediaDeviceGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mSdMediaDeviceDriverNameTable, + DriverName, + (BOOLEAN) (This =3D=3D &gSdMediaDeviceName) + ); +} + + +/** + Retrieves a Unicode string that is the user readable name of the control= ler + that is being managed by a driver. + + This function retrieves the user readable name of the controller specifi= ed by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specif= ied by + Language, then a pointer to the controller name is returned in Controlle= rName, + and EFI_SUCCESS is returned. If the driver specified by This is not cur= rently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does = not + support the language specified by Language, then EFI_UNSUPPORTED is retu= rned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_P= ROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the dr= iver + specified by This is managing. This h= andle + specifies the controller whose name is= to be + returned. + + @param[in] ChildHandle The handle of the child controller to = retrieve + the name of. This is an optional para= meter that + may be NULL. It will be NULL for devi= ce + drivers. It will also be NULL for a b= us drivers + that wish to retrieve the name of the = bus + controller. It will not be NULL for a= bus + driver that wishes to retrieve the nam= e of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII s= tring + array indicating the language. This i= s the + language of the driver name that the c= aller is + requesting, and it must match one of t= he + languages specified in SupportedLangua= ges. The + number of languages supported by a dri= ver is up + to the driver writer. Language is spec= ified in + RFC 3066 or ISO 639-2 language code fo= rmat. + + @param[out] ControllerName A pointer to the Unicode string to ret= urn. + This Unicode string is the name of the + controller specified by ControllerHand= le and + ChildHandle in the language specified = by + Language from the point of view of the= driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readab= le name in + the language specified by Language for= the + driver specified by This was returned = in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HA= NDLE. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not = a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not cu= rrently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not = support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +SdMediaDeviceGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle O= PTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_BLOCK_IO_PROTOCOL *BlockIo; + CARD_DATA *CardData; + + // + // This is a device driver, so ChildHandle must be NULL. + // + if (ChildHandle !=3D NULL) { + return EFI_UNSUPPORTED; + } + + // + // Get the device context + // + Status =3D gBS->OpenProtocol ( + ControllerHandle, + &gEfiBlockIoProtocolGuid, + (VOID **) &BlockIo, + gSdMediaDeviceDriverBinding.DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + CardData =3D CARD_DATA_FROM_THIS (BlockIo); + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + CardData->ControllerNameTable, + ControllerName, + (BOOLEAN) (This =3D=3D &gSdMediaDeviceName) + ); + +} + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdMed= iaDeviceDxe/ComponentName.h b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/= Sdio/Dxe/SD/SdMediaDeviceDxe/ComponentName.h new file mode 100644 index 0000000..3dea441 --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdMediaDevic= eDxe/ComponentName.h @@ -0,0 +1,145 @@ +/** @file + This file contains the declarations for component name routines. + + Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#ifndef _COMPONENT_NAME_H_ +#define _COMPONENT_NAME_H_ + +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form o= f a + Unicode string. If the driver specified by This has a user readable name= in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver speci= fied + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME= 2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instanc= e. + + @param[in] Language A pointer to a Null-terminated ASCI= I string + array indicating the language. This= is the + language of the driver name that th= e caller is + requesting, and it must match one o= f the + languages specified in SupportedLan= guages. The + number of languages supported by a = driver is up + to the driver writer. Language is s= pecified + in RFC 3066 or ISO 639-2 language c= ode format. + + @param[out] DriverName A pointer to the Unicode string to = return. + This Unicode string is the name of = the + driver specified by This in the lan= guage + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver s= pecified by + This and the language specified by = Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does n= ot support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +SdMediaDeviceGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + +/** + Retrieves a Unicode string that is the user readable name of the control= ler + that is being managed by a driver. + + This function retrieves the user readable name of the controller specifi= ed by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specif= ied by + Language, then a pointer to the controller name is returned in Controlle= rName, + and EFI_SUCCESS is returned. If the driver specified by This is not cur= rently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does = not + support the language specified by Language, then EFI_UNSUPPORTED is retu= rned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME= 2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instanc= e. + + @param[in] ControllerHandle The handle of a controller that the= driver + specified by This is managing. Thi= s handle + specifies the controller whose name= is to be + returned. + + @param[in] ChildHandle The handle of the child controller = to retrieve + the name of. This is an optional p= arameter that + may be NULL. It will be NULL for d= evice + drivers. It will also be NULL for = a bus drivers + that wish to retrieve the name of t= he bus + controller. It will not be NULL fo= r a bus + driver that wishes to retrieve the = name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCI= I string + array indicating the language. Thi= s is the + language of the driver name that th= e caller is + requesting, and it must match one o= f the + languages specified in SupportedLan= guages. The + number of languages supported by a = driver is up + to the driver writer. Language is s= pecified in + RFC 3066 or ISO 639-2 language code= format. + + @param[out] ControllerName A pointer to the Unicode string to = return. + This Unicode string is the name of = the + controller specified by ControllerH= andle and + ChildHandle in the language specifi= ed by + Language from the point of view of = the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user rea= dable name in + the language specified by Language = for the + driver specified by This was return= ed in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI= _HANDLE. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is n= ot a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not= currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does n= ot support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +SdMediaDeviceGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle O= PTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + +#endif + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdMed= iaDeviceDxe/MMCSDBlockIo.c b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/S= dio/Dxe/SD/SdMediaDeviceDxe/MMCSDBlockIo.c new file mode 100644 index 0000000..6180a25 --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdMediaDevic= eDxe/MMCSDBlockIo.c @@ -0,0 +1,547 @@ +/** @file + Block I/O protocol for MMC/SD device. + + Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include "SdMediaDevice.h" + +/** + Implements EFI_BLOCK_IO_PROTOCOL.Reset() function. + + @param[in] This The EFI_BLOCK_IO_PROTOCOL instance. + @param[in] ExtendedVerification Indicates that the driver may perfor= m a more exhaustive. + verification operation of the device= during reset. + (This parameter is ignored in this d= river.) + + @retval EFI_SUCCESS Success + +**/ +EFI_STATUS +EFIAPI +MMCSDBlockReset ( + IN EFI_BLOCK_IO_PROTOCOL *This, + IN BOOLEAN ExtendedVerification + ) +{ + CARD_DATA *CardData; + EFI_SD_HOST_IO_PROTOCOL *SdHostIo; + + CardData =3D CARD_DATA_FROM_THIS (This); + SdHostIo =3D CardData->SdHostIo; + + return SdHostIo->ResetSdHost (SdHostIo, Reset_DAT_CMD); + } + + +/** + Implements EFI_BLOCK_IO_PROTOCOL.ReadBlocks() function. + + @param[in] This The EFI_BLOCK_IO_PROTOCOL instanc= e. + @param[in] MediaId The media id that the write reque= st is for. + @param[in] LBA The starting logical block addres= s to read from on the device. + The caller is responsible for wri= ting to only legitimate locations. + @param[in] BufferSize The size of the Buffer in bytes. = This must be a multiple of the + intrinsic block size of the devic= e. + @param[out] Buffer A pointer to the destination buff= er for the data. The caller + is responsible for either having = implicit or explicit ownership + of the buffer. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +EFIAPI +MMCSDBlockReadBlocks ( + IN EFI_BLOCK_IO_PROTOCOL *This, + IN UINT32 MediaId, + IN EFI_LBA LBA, + IN UINTN BufferSize, + OUT VOID *Buffer + ) +{ + EFI_STATUS Status; + UINT32 Address; + CARD_DATA *CardData; + EFI_SD_HOST_IO_PROTOCOL *SdHostIo; + UINT32 RemainingLength; + UINT32 TransferLength; + UINT8 *BufferPointer; + BOOLEAN SectorAddressing; + UINTN TotalBlock; + + DEBUG ((EFI_D_INFO, "Read(LBA=3D%08lx, Buffer=3D%08x, Size=3D%08x)\n", L= BA, Buffer, BufferSize)); + Status =3D EFI_SUCCESS; + CardData =3D CARD_DATA_FROM_THIS (This); + SdHostIo =3D CardData->SdHostIo; + + if (MediaId !=3D CardData->BlockIoMedia.MediaId) { + return EFI_MEDIA_CHANGED; + } + + if (ModU64x32 (BufferSize, CardData->BlockIoMedia.BlockSize) !=3D 0) { + return EFI_BAD_BUFFER_SIZE; + } + if ((CardData->CardType =3D=3D SdMemoryCard2High) || (CardData->CardType= =3D=3D MMCCardHighCap)) { + SectorAddressing =3D TRUE; + } else { + SectorAddressing =3D FALSE; + } + if (SectorAddressing) { + // + //Block Address + // + Address =3D (UINT32) DivU64x32 (MultU64x32 (LBA, CardData->BlockIoMedi= a.BlockSize), 512); + } else { + // + //Byte Address + // + Address =3D (UINT32) MultU64x32 (LBA, CardData->BlockIoMedia.BlockSiz= e); + } + TotalBlock =3D (UINTN) DivU64x32 (BufferSize, CardData->BlockIoMedia.Blo= ckSize); + if (LBA + TotalBlock > CardData->BlockIoMedia.LastBlock + 1) { + return EFI_INVALID_PARAMETER; + } + + if (!Buffer) { + Status =3D EFI_INVALID_PARAMETER; + DEBUG ((EFI_D_ERROR, "MMCSDBlockReadBlocks:Invalid parameter \r\n")); + goto Done; + } + + if ((BufferSize % CardData->BlockIoMedia.BlockSize) !=3D 0) { + Status =3D EFI_BAD_BUFFER_SIZE; + DEBUG ((EFI_D_ERROR, "MMCSDBlockReadBlocks: Bad buffer size \r\n")); + goto Done; + } + + if (BufferSize =3D=3D 0) { + Status =3D EFI_SUCCESS; + goto Done; + } + + BufferPointer =3D Buffer; + RemainingLength =3D (UINT32) BufferSize; + + while (RemainingLength > 0) { + if ((BufferSize > CardData->BlockIoMedia.BlockSize)) { + if (RemainingLength > SdHostIo->HostCapability.BoundarySize) { + TransferLength =3D SdHostIo->HostCapability.BoundarySize; + } else { + TransferLength =3D RemainingLength; + } + + if (CardData->CardType =3D=3D MMCCard || CardData->CardType =3D=3D M= MCCardHighCap) { + if (!(CardData->ExtCSDRegister.CARD_TYPE & (BIT2 | BIT3))) { + Status =3D SendCommand ( + CardData, + SET_BLOCKLEN, + CardData->BlockIoMedia.BlockSize, + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + break; + } + } + Status =3D SendCommand ( + CardData, + SET_BLOCK_COUNT, + TransferLength / CardData->BlockIoMedia.BlockSize, + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + break; + } + } + Status =3D SendCommand ( + CardData, + READ_MULTIPLE_BLOCK, + Address, + InData, + CardData->AlignedBuffer, + TransferLength, + ResponseR1, + TIMEOUT_DATA, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MMCSDBlockReadBlocks: READ_MULTIPLE_BLOCK ->= Fail\n")); + break; + } + } else { + if (RemainingLength > CardData->BlockIoMedia.BlockSize) { + TransferLength =3D CardData->BlockIoMedia.BlockSize; + } else { + TransferLength =3D RemainingLength; + } + + Status =3D SendCommand ( + CardData, + READ_SINGLE_BLOCK, + Address, + InData, + CardData->AlignedBuffer, + (UINT32) TransferLength, + ResponseR1, + TIMEOUT_DATA, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MMCSDBlockReadBlocks: READ_SINGLE_BLOCK -> F= ail\n")); + break; + } + } + CopyMem (BufferPointer, CardData->AlignedBuffer, TransferLength); + if (SectorAddressing) { + // + //Block Address + // + Address +=3D TransferLength / 512; + } else { + // + //Byte Address + // + Address +=3D TransferLength; + } + BufferPointer +=3D TransferLength; + RemainingLength -=3D TransferLength; + } + + if (EFI_ERROR (Status)) { + if ((CardData->CardType =3D=3D SdMemoryCard) || + (CardData->CardType =3D=3D SdMemoryCard2)|| + (CardData->CardType =3D=3D SdMemoryCard2High)) { + SendCommand ( + CardData, + STOP_TRANSMISSION, + 0, + NoData, + NULL, + 0, + ResponseR1b, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + } else { + SendCommand ( + CardData, + STOP_TRANSMISSION, + 0, + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + } + + } + +Done: + DEBUG ((EFI_D_INFO, "MMCSDBlockReadBlocks: Status =3D %r\n", Status)); + return Status; +} + + +/** + Implements EFI_BLOCK_IO_PROTOCOL.WriteBlocks() function. + + @param[in] This The EFI_BLOCK_IO_PROTOCOL instanc= e. + @param[in] MediaId The media id that the write reque= st is for. + @param[in] LBA The starting logical block addres= s to read from on the device. + The caller is responsible for wri= ting to only legitimate locations. + @param[in] BufferSize The size of the Buffer in bytes. = This must be a multiple of the + intrinsic block size of the devic= e. + @param[in] Buffer A pointer to the destination buff= er for the data. The caller + is responsible for either having = implicit or explicit ownership + of the buffer. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +EFIAPI +MMCSDBlockWriteBlocks ( + IN EFI_BLOCK_IO_PROTOCOL *This, + IN UINT32 MediaId, + IN EFI_LBA LBA, + IN UINTN BufferSize, + IN VOID *Buffer + ) +{ + EFI_STATUS Status; + UINT32 Address; + CARD_DATA *CardData; + EFI_SD_HOST_IO_PROTOCOL *SdHostIo; + UINT32 RemainingLength; + UINT32 TransferLength; + UINT8 *BufferPointer; + BOOLEAN SectorAddressing; + + DEBUG ((EFI_D_INFO, "Write(LBA=3D%08lx, Buffer=3D%08x, Size=3D%08x)\n", = LBA, Buffer, BufferSize)); + Status =3D EFI_SUCCESS; + CardData =3D CARD_DATA_FROM_THIS (This); + SdHostIo =3D CardData->SdHostIo; + if ((CardData->CardType =3D=3D SdMemoryCard2High) || (CardData->CardType= =3D=3D MMCCardHighCap)) { + SectorAddressing =3D TRUE; + } else { + SectorAddressing =3D FALSE; + } + if (SectorAddressing) { + // + //Block Address + // + Address =3D (UINT32) DivU64x32 (MultU64x32 (LBA, CardData->BlockIoMedi= a.BlockSize), 512); + } else { + // + //Byte Address + // + Address =3D (UINT32) MultU64x32 (LBA, CardData->BlockIoMedia.BlockSize= ); + } + + if (!Buffer) { + Status =3D EFI_INVALID_PARAMETER; + DEBUG ((EFI_D_ERROR, "MMCSDBlockWriteBlocks: Invalid parameter \r\n")); + goto Done; + } + + if ((BufferSize % CardData->BlockIoMedia.BlockSize) !=3D 0) { + Status =3D EFI_BAD_BUFFER_SIZE; + DEBUG ((EFI_D_ERROR, "MMCSDBlockWriteBlocks: Bad buffer size \r\n")); + goto Done; + } + + if (BufferSize =3D=3D 0) { + Status =3D EFI_SUCCESS; + goto Done; + } + + if (This->Media->ReadOnly =3D=3D TRUE) { + Status =3D EFI_WRITE_PROTECTED; + DEBUG ((EFI_D_ERROR, "MMCSDBlockWriteBlocks: Write protected \r\n")); + goto Done; + } + + BufferPointer =3D Buffer; + RemainingLength =3D (UINT32) BufferSize; + + while (RemainingLength > 0) { + if ((BufferSize > CardData->BlockIoMedia.BlockSize) ) { + if (RemainingLength > SdHostIo->HostCapability.BoundarySize) { + TransferLength =3D SdHostIo->HostCapability.BoundarySize; + } else { + TransferLength =3D RemainingLength; + } + + if (CardData->CardType =3D=3D MMCCard || CardData->CardType =3D=3D M= MCCardHighCap) { + + if (!(CardData->ExtCSDRegister.CARD_TYPE & (BIT2 | BIT3))) { + Status =3D SendCommand ( + CardData, + SET_BLOCKLEN, + CardData->BlockIoMedia.BlockSize, + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + break; + } + } + Status =3D SendCommand ( + CardData, + SET_BLOCK_COUNT, + TransferLength / CardData->BlockIoMedia.BlockSize, + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + break; + } + } + + CopyMem (CardData->AlignedBuffer, BufferPointer, TransferLength); + + Status =3D SendCommand ( + CardData, + WRITE_MULTIPLE_BLOCK, + Address, + OutData, + CardData->AlignedBuffer, + (UINT32) TransferLength, + ResponseR1, + TIMEOUT_DATA, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MMCSDBlockWriteBlocks: WRITE_MULTIPLE_BLOCK = -> Fail\n")); + break; + } + } else { + if (RemainingLength > CardData->BlockIoMedia.BlockSize) { + TransferLength =3D CardData->BlockIoMedia.BlockSize; + } else { + TransferLength =3D RemainingLength; + } + + CopyMem (CardData->AlignedBuffer, BufferPointer, TransferLength); + + Status =3D SendCommand ( + CardData, + WRITE_BLOCK, + Address, + OutData, + CardData->AlignedBuffer, + (UINT32) TransferLength, + ResponseR1, + TIMEOUT_DATA, + (UINT32 *) &(CardData->CardStatus) + ); + + } + + if (SectorAddressing) { + // + //Block Address + // + Address +=3D TransferLength / 512; + } else { + // + //Byte Address + // + Address +=3D TransferLength; + } + BufferPointer +=3D TransferLength; + RemainingLength -=3D TransferLength; + + } + + if (EFI_ERROR (Status)) { + SendCommand ( + CardData, + STOP_TRANSMISSION, + 0, + NoData, + NULL, + 0, + ResponseR1b, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + } + +Done: + return EFI_SUCCESS; +} + + +/** + Implements EFI_BLOCK_IO_PROTOCOL.FlushBlocks() function. + (In this driver, this function just returns EFI_SUCCESS.) + + @param[in] This The EFI_BLOCK_IO_PROTOCOL instance. + + @retval EFI_SUCCESS + @retval Others + +**/ +EFI_STATUS +EFIAPI +MMCSDBlockFlushBlocks ( + IN EFI_BLOCK_IO_PROTOCOL *This + ) +{ + return EFI_SUCCESS; +} + + +/** + MMC/SD card BlockIo init function. + + @param[in] CardData Pointer to CARD_DATA. + + @retval EFI_SUCCESS + @retval Others + +**/ +EFI_STATUS +MMCSDBlockIoInit ( + IN CARD_DATA *CardData + ) +{ + + // + // BlockIO protocol + // + CardData->BlockIo.Revision =3D EFI_BLOCK_IO_PROTOCOL_REVISION; + CardData->BlockIo.Media =3D &(CardData->BlockIoMedia); + CardData->BlockIo.Reset =3D MMCSDBlockReset; + CardData->BlockIo.ReadBlocks =3D MMCSDBlockReadBlocks ; + CardData->BlockIo.WriteBlocks =3D MMCSDBlockWriteBlocks; + CardData->BlockIo.FlushBlocks =3D MMCSDBlockFlushBlocks; + + CardData->BlockIoMedia.MediaId =3D 0; + CardData->BlockIoMedia.RemovableMedia =3D FALSE; + CardData->BlockIoMedia.MediaPresent =3D TRUE; + CardData->BlockIoMedia.LogicalPartition =3D FALSE; + + if (CardData->CSDRegister.PERM_WRITE_PROTECT || CardData->CSDRegister.TM= P_WRITE_PROTECT) { + CardData->BlockIoMedia.ReadOnly =3D TRUE; + } else { + CardData->BlockIoMedia.ReadOnly =3D FALSE; + } + + CardData->BlockIoMedia.WriteCaching =3D FALSE; + CardData->BlockIoMedia.BlockSize =3D CardData->BlockLen; + CardData->BlockIoMedia.IoAlign =3D 1; + CardData->BlockIoMedia.LastBlock =3D (EFI_LBA) (CardData->BlockNu= mber - 1); + + return EFI_SUCCESS; +} + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdMed= iaDeviceDxe/MMCSDTransfer.c b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/= Sdio/Dxe/SD/SdMediaDeviceDxe/MMCSDTransfer.c new file mode 100644 index 0000000..e0304de --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdMediaDevic= eDxe/MMCSDTransfer.c @@ -0,0 +1,1661 @@ +/** @file + MMC/SD transfer specific functions + + Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include "SdMediaDevice.h" + + +/** + Check card status, print the debug info and check the error + + @param[in] Status Status got from card status register. + + @retval EFI_SUCCESS + @retval EFI_DEVICE_ERROR + +**/ +EFI_STATUS +CheckCardStatus ( + IN UINT32 Status + ) +{ + CARD_STATUS *CardStatus; + CardStatus =3D (CARD_STATUS *) (&Status); + + if (CardStatus->ADDRESS_OUT_OF_RANGE) { + DEBUG ((EFI_D_ERROR, "CardStatus: ADDRESS_OUT_OF_RANGE\n")); + } + + if (CardStatus->ADDRESS_MISALIGN) { + DEBUG ((EFI_D_ERROR, "CardStatus: ADDRESS_MISALIGN\n")); + } + + if (CardStatus->BLOCK_LEN_ERROR) { + DEBUG ((EFI_D_ERROR, "CardStatus: BLOCK_LEN_ERROR\n")); + } + + if (CardStatus->ERASE_SEQ_ERROR) { + DEBUG ((EFI_D_ERROR, "CardStatus: ERASE_SEQ_ERROR\n")); + } + + if (CardStatus->ERASE_PARAM) { + DEBUG ((EFI_D_ERROR, "CardStatus: ERASE_PARAM\n")); + } + + if (CardStatus->WP_VIOLATION) { + DEBUG ((EFI_D_ERROR, "CardStatus: WP_VIOLATION\n")); + } + + if (CardStatus->CARD_IS_LOCKED) { + DEBUG ((EFI_D_ERROR, "CardStatus: CARD_IS_LOCKED\n")); + } + + if (CardStatus->LOCK_UNLOCK_FAILED) { + DEBUG ((EFI_D_ERROR, "CardStatus: LOCK_UNLOCK_FAILED\n")); + } + + if (CardStatus->COM_CRC_ERROR) { + DEBUG ((EFI_D_ERROR, "CardStatus: COM_CRC_ERROR\n")); + } + + if (CardStatus->ILLEGAL_COMMAND) { + DEBUG ((EFI_D_ERROR, "CardStatus: ILLEGAL_COMMAND\n")); + } + + if (CardStatus->CARD_ECC_FAILED) { + DEBUG ((EFI_D_ERROR, "CardStatus: CARD_ECC_FAILED\n")); + } + + if (CardStatus->CC_ERROR) { + DEBUG ((EFI_D_ERROR, "CardStatus: CC_ERROR\n")); + } + + if (CardStatus->ERROR) { + DEBUG ((EFI_D_ERROR, "CardStatus: ERROR\n")); + } + + if (CardStatus->UNDERRUN) { + DEBUG ((EFI_D_ERROR, "CardStatus: UNDERRUN\n")); + } + + if (CardStatus->OVERRUN) { + DEBUG ((EFI_D_ERROR, "CardStatus: OVERRUN\n")); + } + + if (CardStatus->CID_CSD_OVERWRITE) { + DEBUG ((EFI_D_ERROR, "CardStatus: CID_CSD_OVERWRITE\n")); + } + + if (CardStatus->WP_ERASE_SKIP) { + DEBUG ((EFI_D_ERROR, "CardStatus: WP_ERASE_SKIP\n")); + } + + if (CardStatus->ERASE_RESET) { + DEBUG ((EFI_D_ERROR, "CardStatus: ERASE_RESET\n")); + } + + if (CardStatus->SWITCH_ERROR) { + DEBUG ((EFI_D_ERROR, "CardStatus: SWITCH_ERROR\n")); + } + + if ((Status & 0xFCFFA080) !=3D 0) { + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + + +/** + Send command by using Host IO protocol + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTOC= OL instance. + @param[in] CommandIndex The command index to set the command i= ndex field of command register. + @param[in] Argument Command argument to set the argument f= ield of command register. + @param[in] DataType TRANSFER_TYPE, indicates no data, data= in or data out. + @param[in] Buffer Contains the data read from / write to= the device. + @param[in] BufferSize The size of the buffer. + @param[in] ResponseType RESPONSE_TYPE. + @param[in] TimeOut Time out value in 1 ms unit. + @param[out] ResponseData Depending on the ResponseType, such as= CSD or card status. + + @retval EFI_SUCCESS + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_DEVICE_ERROR + +**/ +EFI_STATUS +SendCommand ( + IN CARD_DATA *CardData, + IN UINT16 CommandIndex, + IN UINT32 Argument, + IN TRANSFER_TYPE DataType, + IN UINT8 *Buffer, OPTIONAL + IN UINT32 BufferSize, + IN RESPONSE_TYPE ResponseType, + IN UINT32 TimeOut, + OUT UINT32 *ResponseData + ) +{ + + EFI_STATUS Status; + EFI_SD_HOST_IO_PROTOCOL *SdHostIo; + + SdHostIo =3D CardData->SdHostIo; + if (CardData->CardType !=3D MMCCard && CardData->CardType !=3D MMCCardHi= ghCap) { + CommandIndex |=3D AUTO_CMD12_ENABLE; + } + Status =3D SdHostIo->SendCommand ( + SdHostIo, + CommandIndex, + Argument, + DataType, + Buffer, + BufferSize, + ResponseType, + TimeOut, + ResponseData + ); + + if (!EFI_ERROR (Status)) { + if (ResponseType =3D=3D ResponseR1 || ResponseType =3D=3D ResponseR1b)= { + ASSERT (ResponseData !=3D NULL); + Status =3D CheckCardStatus (*ResponseData); + } + } else { + SdHostIo->ResetSdHost (SdHostIo, Reset_DAT_CMD); + } + + return Status; +} + + +/** + Send the card APP_CMD command with the following command indicated by Co= mmandIndex + + @param[in] CardData Pointer to CARD_DATA. + @param[in] CommandIndex The command index to set the command i= ndex field of command register. + @param[in] Argument Command argument to set the argument f= ield of command register. + @param[in] DataType TRANSFER_TYPE, indicates no data, data= in or data out. + @param[in] Buffer Contains the data read from / write to= the device. + @param[in] BufferSize The size of the buffer. + @param[in] ResponseType RESPONSE_TYPE. + @param[in] TimeOut Time out value in 1 ms unit. + @param[out] ResponseData Depending on the ResponseType, such as= CSD or card status. + + @retval EFI_SUCCESS + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_DEVICE_ERROR + +**/ +EFI_STATUS +SendAppCommand ( + IN CARD_DATA *CardData, + IN UINT16 CommandIndex, + IN UINT32 Argument, + IN TRANSFER_TYPE DataType, + IN UINT8 *Buffer, OPTIONAL + IN UINT32 BufferSize, + IN RESPONSE_TYPE ResponseType, + IN UINT32 TimeOut, + OUT UINT32 *ResponseData + ) +{ + + EFI_STATUS Status; + EFI_SD_HOST_IO_PROTOCOL *SdHostIo; + UINT8 Index; + + SdHostIo =3D CardData->SdHostIo; + Status =3D EFI_SUCCESS; + + for (Index =3D 0; Index < 2; Index++) { + Status =3D SdHostIo->SendCommand ( + SdHostIo, + APP_CMD, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (!EFI_ERROR (Status)) { + Status =3D CheckCardStatus (*(UINT32 *) &(CardData->CardStatus)); + if (CardData->CardStatus.SAPP_CMD !=3D 1) { + Status =3D EFI_DEVICE_ERROR; + } + if (!EFI_ERROR (Status)) { + break; + } + } else { + SdHostIo->ResetSdHost (SdHostIo, Reset_Auto); + } + } + + if (EFI_ERROR (Status)) { + return Status; + } + if (CardData->CardType !=3D MMCCard && CardData->CardType !=3D MMCCardHi= ghCap) { + CommandIndex |=3D AUTO_CMD12_ENABLE; + } + + Status =3D SdHostIo->SendCommand ( + SdHostIo, + CommandIndex, + Argument, + DataType, + Buffer, + BufferSize, + ResponseType, + TimeOut, + ResponseData + ); + + if (!EFI_ERROR (Status)) { + if (ResponseType =3D=3D ResponseR1 || ResponseType =3D=3D ResponseR1b)= { + ASSERT (ResponseData !=3D NULL); + Status =3D CheckCardStatus (*ResponseData); + } + } else { + SdHostIo->ResetSdHost (SdHostIo, Reset_Auto); + } + + return Status; +} + + +/** + Send the card FAST_IO command + + @param[in] CardData Pointer to CARD_DATA. + @param[in] RegisterAddress Register Address. + @param[in, out] RegisterData Pointer to register Data. + @param[in] Write TRUE for write, FALSE for read. + + @retval EFI_SUCCESS + @retval EFI_UNSUPPORTED + @retval EFI_INVALID_PARAMETER + @retval EFI_DEVICE_ERROR + +**/ +EFI_STATUS +FastIO ( + IN CARD_DATA *CardData, + IN UINT8 RegisterAddress, + IN OUT UINT8 *RegisterData, + IN BOOLEAN Write + ) +{ + EFI_STATUS Status; + UINT32 Argument; + UINT32 Data; + + Status =3D EFI_SUCCESS; + + if (RegisterData =3D=3D NULL) { + Status =3D EFI_INVALID_PARAMETER; + goto Exit; + } + + Argument =3D (CardData->Address << 16) | (RegisterAddress << 8); + if (Write) { + Argument |=3D BIT15 | (*RegisterData); + } + + Status =3D SendCommand ( + CardData, + FAST_IO, + Argument, + NoData, + NULL, + 0, + ResponseR4, + TIMEOUT_COMMAND, + &Data + ); + + if (EFI_ERROR (Status)) { + goto Exit; + } + + if ((Data & BIT15) =3D=3D 0) { + Status =3D EFI_DEVICE_ERROR; + goto Exit; + } + + if (!Write) { + *RegisterData =3D (UINT8) Data; + } + +Exit: + return Status; +} + + +/** + Send the card GO_INACTIVE_STATE command. + + @param[in] CardData Pointer to CARD_DATA. + + @retval EFI_SUCCESS + @retval others + +**/ +EFI_STATUS +PutCardInactive ( + IN CARD_DATA *CardData + ) +{ + EFI_STATUS Status; + + + Status =3D SendCommand ( + CardData, + GO_INACTIVE_STATE, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseNo, + TIMEOUT_COMMAND, + NULL + ); + + return Status; + +} + + +/** + Get card interested information for CSD rergister + + @param[in] CardData Pointer to CARD_DATA. + + @retval EFI_SUCCESS + @retval EFI_UNSUPPORTED + @retval EFI_INVALID_PARAMETER + +**/ +EFI_STATUS +CaculateCardParameter ( + IN CARD_DATA *CardData + ) +{ + EFI_STATUS Status; + UINT32 Frequency; + UINT32 Multiple; + UINT32 CSize; + CSD_SDV2 *CsdSDV2; + + Status =3D EFI_SUCCESS; + + switch (CardData->CSDRegister.TRAN_SPEED & 0x7) { + case 0: + Frequency =3D 100 * 1000; + break; + + case 1: + Frequency =3D 1 * 1000 * 1000; + break; + + case 2: + Frequency =3D 10 * 1000 * 1000; + break; + + case 3: + Frequency =3D 100 * 1000 * 1000; + break; + + default: + Status =3D EFI_INVALID_PARAMETER; + goto Exit; + } + + switch ((CardData->CSDRegister.TRAN_SPEED >> 3) & 0xF) { + case 1: + Multiple =3D 10; + break; + + case 2: + Multiple =3D 12; + break; + + case 3: + Multiple =3D 13; + break; + + case 4: + Multiple =3D 15; + break; + + case 5: + Multiple =3D 20; + break; + + case 6: + if (CardData->CardType =3D=3D MMCCard || CardData->CardType =3D=3D = MMCCardHighCap) { + Multiple =3D 26; + } else { + Multiple =3D 25; + } + break; + + case 7: + Multiple =3D 30; + break; + + case 8: + Multiple =3D 35; + break; + + case 9: + Multiple =3D 40; + break; + + case 10: + Multiple =3D 45; + break; + + case 11: + if (CardData->CardType =3D=3D MMCCard || CardData->CardType =3D=3D = MMCCardHighCap) { + Multiple =3D 52; + } else { + Multiple =3D 50; + } + break; + + case 12: + Multiple =3D 55; + break; + + case 13: + Multiple =3D 60; + break; + + case 14: + Multiple =3D 70; + break; + + case 15: + Multiple =3D 80; + break; + + default: + Status =3D EFI_INVALID_PARAMETER; + goto Exit; + } + + Frequency =3D Frequency * Multiple / 10; + CardData->MaxFrequency =3D Frequency; + + CardData->BlockLen =3D 1 << CardData->CSDRegister.READ_BL_LEN; + + if (CardData->CardType =3D=3D SdMemoryCard2High) { + ASSERT (CardData->CSDRegister.CSD_STRUCTURE =3D=3D 1); + CsdSDV2 =3D (CSD_SDV2 *) &CardData->CSDRegister; + + // + // the K here means 1024 not 1000 + // + CardData->BlockNumber =3D DivU64x32 (MultU64x32 (CsdSDV2->C_SIZE + 1, = 512 * 1024) , CardData->BlockLen); + } else { + // + // For MMC card > 2G, the block number will be recaculate later + // + CSize =3D CardData->CSDRegister.C_SIZELow2 | (CardData->CSDRegister.C_= SIZEHigh10 << 2); + CardData->BlockNumber =3D MultU64x32 (LShiftU64 (1, CardData->CSDRegis= ter.C_SIZE_MULT + 2), CSize + 1); + } + + // + //For >=3D 2G card, BlockLen may be 1024, but the transfer size is still= 512 bytes + // + if (CardData->BlockLen > 512) { + CardData->BlockNumber =3D DivU64x32 (MultU64x32 (CardData->BlockNumber= , CardData->BlockLen), 512); + CardData->BlockLen =3D 512; + } + + DEBUG (( + EFI_D_INFO, + "CalculateCardParameter: Card Size: 0x%lx\n", MultU64x32 (CardData->Bl= ockNumber, CardData->BlockLen) + )); + +Exit: + return Status; +} + + +/** + Test the bus width setting for MMC card.It is used only for verification= purpose. + + @param[in] CardData Pointer to CARD_DATA. + @param[in] Width 1, 4, 8 bits. + + @retval EFI_SUCCESS + @retval EFI_UNSUPPORTED + @retval EFI_INVALID_PARAMETER + +**/ +EFI_STATUS +MMCCardBusWidthTest ( + IN CARD_DATA *CardData, + IN UINT32 Width + ) +{ + EFI_STATUS Status; + UINT64 Data; + UINT64 Value; + + ASSERT(CardData !=3D NULL); + + Value =3D 0; + + switch (Width) { + case 1: + Data =3D 0x80; + break; + + case 4: + Data =3D 0x5A; + break; + + case 8: + Data =3D 0xAA55; + break; + + default: + Status =3D EFI_INVALID_PARAMETER; + goto Exit; + } + + CopyMem (CardData->AlignedBuffer, &Data, Width); + Status =3D SendCommand ( + CardData, + BUSTEST_W, + 0, + OutData, + CardData->AlignedBuffer, + Width, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MMCCardBusWidthTest:SendCommand BUSTEST_W 0x%x\n= ", *(UINT32*)&(CardData->CardStatus))); + goto Exit; + } + + gBS->Stall (10 * 1000); + + Data =3D 0; + + Status =3D SendCommand ( + CardData, + BUSTEST_R, + 0, + InData, + CardData->AlignedBuffer, + Width, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MMCCardBusWidthTest:SendCommand BUSTEST_R 0x%x\n= ", *(UINT32 *) &(CardData->CardStatus))); + goto Exit; + } + CopyMem (&Data, CardData->AlignedBuffer, Width); + + switch (Width) { + case 1: + Value =3D (~(Data ^ 0x80)) & 0xC0; + break; + + case 4: + Value =3D (~(Data ^ 0x5A)) & 0xFF; + break; + + case 8: + Value =3D (~(Data ^ 0xAA55)) & 0xFFFF; + break; + } + + if (Value =3D=3D 0) { + Status =3D EFI_SUCCESS; + } else { + Status =3D EFI_UNSUPPORTED; + } + +Exit: + return Status; +} + + +/** + This function can detect these card types: + 1. MMC card + 2. SD 1.1 card + 3. SD 2.0 standard card + 3. SD 2.0 high capacity card + + @param[in] CardData Pointer to CARD_DATA. + + @retval EFI_SUCCESS + @retval others + +**/ +EFI_STATUS +GetCardType ( + IN CARD_DATA *CardData + ) +{ + EFI_STATUS Status; + EFI_SD_HOST_IO_PROTOCOL *SdHostIo; + UINT32 Argument; + UINT32 ResponseData; + UINT32 Count; + BOOLEAN SdCommand8Support; + + SdHostIo =3D CardData->SdHostIo; + + // + // Reset the card + // + Status =3D SendCommand ( + CardData, + GO_IDLE_STATE, + 0, + NoData, + NULL, + 0, + ResponseNo, + TIMEOUT_COMMAND, + NULL + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "GO_IDLE_STATE Fail Status =3D 0x%x\n", Status)); + goto Exit; + } + gBS->Stall (10 * 1000); + + // + // Only 2.7V - 3.6V is supported for SD2.0, only SD 2.0 card can pass + // MMC and SD1.1 card will fail this command + // + Argument =3D (VOLTAGE_27_36 << 8) | CHECK_PATTERN; + ResponseData =3D 0; + SdCommand8Support =3D FALSE; + + Status =3D SendCommand ( + CardData, + SEND_IF_COND, + Argument, + NoData, + NULL, + 0, + ResponseR7, + TIMEOUT_COMMAND, + &ResponseData + ); + + if (EFI_ERROR (Status)) { + if (Status !=3D EFI_TIMEOUT) { + DEBUG ((EFI_D_ERROR, "SEND_IF_COND Fail, none time out error\n")); + goto Exit; + } + } else { + if (ResponseData !=3D Argument) { + DEBUG ((EFI_D_ERROR, "SEND_IF_COND Fail, respond data does not matc= h send data\n")); + Status =3D EFI_DEVICE_ERROR; + goto Exit; + } + SdCommand8Support =3D TRUE; + } + + Argument =3D 0; + + if (SdHostIo->HostCapability.V30Support =3D=3D TRUE) { + Argument |=3D BIT17 | BIT18; + } else if (SdHostIo->HostCapability.V33Support =3D=3D TRUE) { + Argument |=3D BIT20 | BIT21; + } + + if (SdCommand8Support) { + Argument |=3D BIT30; + } + + Count =3D 20; + do { + Status =3D SendAppCommand ( + CardData, + SD_SEND_OP_COND, + Argument, + NoData, + NULL, + 0, + ResponseR3, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->OCRRegister) + ); + + if (EFI_ERROR (Status)) { + if ((Status =3D=3D EFI_TIMEOUT) && (!SdCommand8Support)) { + CardData->CardType =3D MMCCard; + Status =3D EFI_SUCCESS; + DEBUG ((EFI_D_INFO, "SD_SEND_OP_COND, MMC card was identified\n")); + } else { + DEBUG ((EFI_D_ERROR, "SD_SEND_OP_COND Fail, check whether it is ne= ither a MMC card nor a SD card\n")); + } + goto Exit; + } + // + // Avoid waiting if sucess. Busy bit 0 means not ready + // + if (CardData->OCRRegister.Busy =3D=3D 1) { + break; + } + + gBS->Stall (50 * 1000); + Count--; + if (Count =3D=3D 0) { + DEBUG ((EFI_D_ERROR, "Card is always in busy state\n")); + Status =3D EFI_TIMEOUT; + goto Exit; + } + } while (1); + + // + // Check supported voltage + // + Argument =3D 0; + if (SdHostIo->HostCapability.V30Support =3D=3D TRUE) { + if ((CardData->OCRRegister.V270_V360 & BIT2) =3D=3D BIT2) { + Argument |=3D BIT17; + } else if ((CardData->OCRRegister.V270_V360 & BIT3) =3D=3D BIT3) { + Argument |=3D BIT18; + } + } else if (SdHostIo->HostCapability.V33Support =3D=3D TRUE) { + if ((CardData->OCRRegister.V270_V360 & BIT5) =3D=3D BIT5) { + Argument |=3D BIT20; + } else if ((CardData->OCRRegister.V270_V360 & BIT6) =3D=3D BIT6) { + Argument |=3D BIT21; + } + } + + if (Argument =3D=3D 0) { + // + // No matched support voltage + // + PutCardInactive (CardData); + DEBUG ((EFI_D_ERROR, "No matched voltage for this card\n")); + Status =3D EFI_UNSUPPORTED; + goto Exit; + } + + CardData->CardType =3D SdMemoryCard; + if (SdCommand8Support =3D=3D TRUE) { + CardData->CardType =3D SdMemoryCard2; + DEBUG ((EFI_D_INFO, "SD_SEND_OP_COND, SD 2.0 or above standard card was= identified\n")); + } + + if ((CardData->OCRRegister.AccessMode & BIT1) =3D=3D BIT1) { + CardData->CardType =3D SdMemoryCard2High; + DEBUG ((EFI_D_INFO, "SD_SEND_OP_COND, SD 2.0 or above high capacity ca= rd was identified\n")); + } + +Exit: + return Status; +} + + +/** + MMC card high/low voltage selection function + + @param[in] CardData Pointer to CARD_DATA. + + @retval EFI_SUCCESS + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_BAD_BUFFER_SIZE + +**/ +EFI_STATUS +MMCCardVoltageSelection ( + IN CARD_DATA *CardData + ) +{ + EFI_STATUS Status; + UINT8 Retry; + UINT32 TimeOut; + + Status =3D EFI_SUCCESS; + + // + // First try the high voltage, then if supported choose the low voltage + // + for (Retry =3D 0; Retry < 3; Retry++) { + // + // To bring back the normal MMC card to work + // after sending the SD command. Otherwise some + // card could not work + + Status =3D SendCommand ( + CardData, + GO_IDLE_STATE, + 0, + NoData, + NULL, + 0, + ResponseNo, + TIMEOUT_COMMAND, + NULL + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "GO_IDLE_STATE Fail Status =3D 0x%x\n", Status)= ); + continue; + } + // + //CE-ATA device needs long delay + // + gBS->Stall ((Retry + 1) * 50 * 1000); + + // + // Get OCR register to check voltage support, first time the OCR is 0 + // + Status =3D SendCommand ( + CardData, + SEND_OP_COND, + 0, + NoData, + NULL, + 0, + ResponseR3, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->OCRRegister) + ); + + if (!EFI_ERROR (Status)) { + break; + } + } + + if (Retry =3D=3D 3) { + DEBUG ((EFI_D_ERROR, "SEND_OP_COND Fail Status =3D 0x%x\n", Status)); + Status =3D EFI_DEVICE_ERROR; + goto Exit; + } + + // + // TimeOut Value, 5000 * 100 * 1000 =3D 5 s + // + TimeOut =3D 5000; + + do { + Status =3D SendCommand ( + CardData, + SEND_OP_COND, + 0x40300000, + NoData, + NULL, + 0, + ResponseR3, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->OCRRegister) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SEND_OP_COND Fail Status =3D 0x%x\n", Status)); + goto Exit; + } + + gBS->Stall (1 * 1000); + TimeOut--; + if (TimeOut =3D=3D 0) { + Status =3D EFI_TIMEOUT; + DEBUG ((EFI_D_ERROR, "Card is always in busy state\n")); + goto Exit; + } + } while (CardData->OCRRegister.Busy !=3D 1); + + if (CardData->OCRRegister.AccessMode =3D=3D 2) { + DEBUG ((EFI_D_INFO, "eMMC Card is High Capacity\n")); + CardData->CardType =3D MMCCardHighCap; + } + +Exit: + return Status; +} + + +/** + This function set the bus and device width for MMC card. + + @param[in] CardData Pointer to CARD_DATA. + @param[in] BusWidth 1, 4, 8 bits. + @param[in] EnableDDRMode Enable DDR Mode. + + @retval EFI_SUCCESS + @retval EFI_UNSUPPORTED + @retval EFI_INVALID_PARAMETER + +**/ +EFI_STATUS +MMCCardSetBusWidth ( + IN CARD_DATA *CardData, + IN UINT8 BusWidth, + IN BOOLEAN EnableDDRMode + ) +{ + EFI_STATUS Status; + EFI_SD_HOST_IO_PROTOCOL *SdHostIo; + SWITCH_ARGUMENT SwitchArgument; + UINT8 Value; + + SdHostIo =3D CardData->SdHostIo; + Value =3D 0; + switch (BusWidth) { + case 8: + if (EnableDDRMode) + Value =3D 6; + else + Value =3D 2; + break; + + case 4: + if (EnableDDRMode) + Value =3D 5; + else + Value =3D 1; + break; + + case 1: + if (EnableDDRMode) // Bus width 1 is not supported in ddr mode + return EFI_UNSUPPORTED; + Value =3D 0; + break; + + default: + ASSERT (0); + } + + ZeroMem (&SwitchArgument, sizeof (SWITCH_ARGUMENT)); + SwitchArgument.CmdSet =3D 0; + SwitchArgument.Value =3D Value; + SwitchArgument.Index =3D (UINT32) ((UINTN) + (&(CardData->ExtCSDRegister.BUS_WIDTH)) - (UINTN) (&(CardData->ExtCSDReg= ister))); + SwitchArgument.Access =3D WriteByte_Mode; + Status =3D SendCommand ( + CardData, + SWITCH, + *(UINT32 *) &SwitchArgument, + NoData, + NULL, + 0, + ResponseR1b, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (!EFI_ERROR (Status)) { + Status =3D SendCommand ( + CardData, + SEND_STATUS, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SWITCH %d bits Fail\n", BusWidth)); + goto Exit; + } else { + DEBUG ((EFI_D_ERROR, "MMCCardSetBusWidth:SWITCH Card Status:0x%x\n",= *(UINT32 *) &(CardData->CardStatus))); + Status =3D SdHostIo->SetBusWidth (SdHostIo, BusWidth); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SWITCH set %d bits Fail\n", BusWidth)); + goto Exit; + } + gBS->Stall (5 * 1000); + } + } + + if (!EnableDDRMode) { // CMD19 and CMD14 are illegal commands in ddr= mode + + Status =3D MMCCardBusWidthTest (CardData, BusWidth); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MMCCardBusWidthTest %d bit Fail\n", BusWidth)); + goto Exit; + } + } + + CardData->CurrentBusWidth =3D BusWidth; + +Exit: + return Status; +} + + +/** + MMC/SD card init function + + @param[in] CardData Pointer to CARD_DATA. + + @retval EFI_SUCCESS + @retval others + +**/ +EFI_STATUS +MMCSDCardInit ( + IN CARD_DATA *CardData + ) +{ + EFI_STATUS Status; + EFI_SD_HOST_IO_PROTOCOL *SdHostIo; + SWITCH_ARGUMENT SwitchArgument; + UINT32 Data; + UINT32 Argument; + UINT32 nIndex; + UINT8 PowerValue; + BOOLEAN EnableDDRMode; + + ASSERT (CardData !=3D NULL); + SdHostIo =3D CardData->SdHostIo; + EnableDDRMode =3D FALSE; + + CardData->CardType =3D UnknownCard; + Status =3D GetCardType (CardData); + if (EFI_ERROR (Status)) { + goto Exit; + } + DEBUG ((DEBUG_INFO, "CardData->CardType 0x%x\n", CardData->CardType)); + + ASSERT (CardData->CardType !=3D UnknownCard); + + // + // MMC, SD card need host auto stop command support + // + SdHostIo->EnableAutoStopCmd (SdHostIo, TRUE); + + if (CardData->CardType =3D=3D MMCCard) { + Status =3D MMCCardVoltageSelection (CardData); + if (EFI_ERROR (Status)) { + goto Exit; + } + } + + // + // Get CID Register + // + Status =3D SendCommand ( + CardData, + ALL_SEND_CID, + 0, + NoData, + NULL, + 0, + ResponseR2, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CIDRegister) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "ALL_SEND_CID Fail Status =3D 0x%x\n", Status)); + goto Exit; + } else { + // + // Dump out the Card ID data + // + DEBUG ((EFI_D_INFO, "Product Name: ")); + for (nIndex=3D0; nIndex<6; nIndex++ ) { + DEBUG ((EFI_D_INFO, "%c", CardData->CIDRegister.PNM[nIndex])); + } + DEBUG ((EFI_D_INFO, "\nApplication ID : %d\n", CardData->CIDRegister.O= ID)); + DEBUG ((EFI_D_INFO, "Manufacturer ID : %d\n", CardData->CIDRegister.M= ID)); + DEBUG ((EFI_D_INFO, "Revision ID : %d\n", CardData->CIDRegister.P= RV)); + DEBUG ((EFI_D_INFO, "Serial Number : %d\n", CardData->CIDRegister.P= SN)); + } + + // + // SET_RELATIVE_ADDR + // + if (CardData->CardType =3D=3D MMCCard || CardData->CardType =3D=3D MMCC= ardHighCap) { + // + // Hard code the RCA address + // + CardData->Address =3D 1; + + // + // Set RCA Register + // + Status =3D SendCommand ( + CardData, + SET_RELATIVE_ADDR, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SET_RELATIVE_ADDR Fail Status =3D 0x%x\n", Sta= tus)); + goto Exit; + } + } else { + Data =3D 0; + Status =3D SendCommand ( + CardData, + SET_RELATIVE_ADDR, + 0, + NoData, + NULL, + 0, + ResponseR6, + TIMEOUT_COMMAND, + &Data + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SET_RELATIVE_ADDR Fail Status =3D 0x%x\n", Sta= tus)); + goto Exit; + } + + CardData->Address =3D (UINT16) (Data >> 16); + *(UINT32 *) &CardData->CardStatus =3D Data & 0x1FFF; + CardData->CardStatus.ERROR =3D (Data >> 13) & 0x1; + CardData->CardStatus.ILLEGAL_COMMAND =3D (Data >> 14) & 0x1; + CardData->CardStatus.COM_CRC_ERROR =3D (Data >> 15) & 0x1; + Status =3D CheckCardStatus (*(UINT32 *) &CardData->CardStatus); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SET_RELATIVE_ADDR Fail Status =3D 0x%x\n", Sta= tus)); + goto Exit; + } + } + + // + // Get CSD Register + // + Status =3D SendCommand ( + CardData, + SEND_CSD, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR2, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CSDRegister) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SEND_CSD Fail Status =3D 0x%x\n", Status)); + goto Exit; + } + + DEBUG ((EFI_D_INFO, "CardData->CSDRegister.SPEC_VERS =3D 0x%x\n", CardDa= ta->CSDRegister.SPEC_VERS)); + DEBUG ((EFI_D_INFO, "CardData->CSDRegister.CSD_STRUCTURE =3D 0x%x\n", Ca= rdData->CSDRegister.CSD_STRUCTURE)); + + Status =3D CaculateCardParameter (CardData); + if (EFI_ERROR (Status)) { + goto Exit; + } + + + // + // It is platform and hardware specific, need hadrware engineer input + // + if (CardData->CSDRegister.DSR_IMP =3D=3D 1) { + // + // Default is 0x404 + // + Status =3D SendCommand ( + CardData, + SET_DSR, + (DEFAULT_DSR_VALUE << 16), + NoData, + NULL, + 0, + ResponseNo, + TIMEOUT_COMMAND, + NULL + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SET_DSR Fail Status =3D 0x%x\n", Status)); + // + // Assume can operate even fail + // + } + } + // + // Change clock frequency from 400KHz to max supported when not in high = speed mode + // + Status =3D SdHostIo->SetClockFrequency (SdHostIo, CardData->MaxFrequency= ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MMCSDCardInit:Fail to SetClockFrequency \n")); + goto Exit; + } + + // + // Put the card into tran state + // + Status =3D SendCommand ( + CardData, + SELECT_DESELECT_CARD, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SELECT_DESELECT_CARD Fail Status =3D 0x%x\n", St= atus)); + goto Exit; + } + gBS->Stall (5 * 1000); + // + // No need to do so + // + // + Status =3D SendCommand ( + CardData, + SEND_STATUS, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SELECT_DESELECT_CARD SEND_STATUS Fail Status =3D= 0x%x\n", Status)); + goto Exit; + } + if (CardData->CardType =3D=3D MMCCard || CardData->CardType =3D=3D MMCC= ardHighCap) { + // + // Only V4.0 and above supports more than 1 bits and high speed + // + if (CardData->CSDRegister.SPEC_VERS >=3D 4) { + // + // Get ExtCSDRegister + // + Status =3D SendCommand ( + CardData, + SEND_EXT_CSD, + 0x0, + InData, + CardData->AlignedBuffer, + sizeof (EXT_CSD), + ResponseR1, + TIMEOUT_DATA, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SEND_EXT_CSD Fail Status =3D 0x%x\n", Status= )); + goto Exit; + } + + CopyMem (&(CardData->ExtCSDRegister), CardData->AlignedBuffer, sizeo= f(EXT_CSD)); + + // + // Recaculate the block number for >2G MMC card + // + Data =3D (CardData->ExtCSDRegister.SEC_COUNT[0]) | + (CardData->ExtCSDRegister.SEC_COUNT[1] << 8) | + (CardData->ExtCSDRegister.SEC_COUNT[2] << 16) | + (CardData->ExtCSDRegister.SEC_COUNT[3] << 24); + + if (Data !=3D 0) { + CardData->BlockNumber =3D Data; + } + DEBUG ((DEBUG_INFO, "CardData->BlockNumber %d\n", Data)); + DEBUG ((EFI_D_ERROR, "CardData->ExtCSDRegister.CARD_TYPE -> %d\n", (= UINTN)CardData->ExtCSDRegister.CARD_TYPE)); + if ((CardData->ExtCSDRegister.CARD_TYPE & BIT2)|| + (CardData->ExtCSDRegister.CARD_TYPE & BIT3)) { + + } + // + // Check current chipset capability and the plugged-in card + // whether supports HighSpeed + // + if (SdHostIo->HostCapability.HighSpeedSupport) { + + // + // Change card timing to high speed interface timing + // + ZeroMem (&SwitchArgument, sizeof (SWITCH_ARGUMENT)); + SwitchArgument.CmdSet =3D 0; + SwitchArgument.Value =3D 1; + SwitchArgument.Index =3D (UINT32) ((UINTN) + (&(CardData->ExtCSDRegister.HS_TIMING)) - (UINTN) (&(CardData->Ext= CSDRegister))); + SwitchArgument.Access =3D WriteByte_Mode; + Status =3D SendCommand ( + CardData, + SWITCH, + *(UINT32 *) &SwitchArgument, + NoData, + NULL, + 0, + ResponseR1b, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MMCSDCardInit:SWITCH frequency Fail Status= =3D 0x%x\n", Status)); + } + + gBS->Stall (5 * 1000); + + if (!EFI_ERROR (Status)) { + Status =3D SendCommand ( + CardData, + SEND_STATUS, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (!EFI_ERROR (Status)) { + if (EnableDDRMode) { + DEBUG ((EFI_D_ERROR, "Enable ddr mode on host controller\n")= ); + SdHostIo->SetDDRMode (SdHostIo, TRUE); + } else { + DEBUG ((EFI_D_ERROR, "Enable high speed mode on host control= ler\n")); + SdHostIo->SetHighSpeedMode (SdHostIo, TRUE); + } + // + // Change host clock to support high speed and enable chispet = to + // support speed + // + if ((CardData->ExtCSDRegister.CARD_TYPE & BIT1) !=3D 0) { + Status =3D SdHostIo->SetClockFrequency (SdHostIo, FREQUENCY_= MMC_PP_HIGH); + } else if ((CardData->ExtCSDRegister.CARD_TYPE & BIT0) !=3D 0)= { + Status =3D SdHostIo->SetClockFrequency (SdHostIo, FREQUENCY_= MMC_PP); + } else { + Status =3D EFI_UNSUPPORTED; + } + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MMCSDCardInit:Fail to SetClockFrequenc= y \n")); + goto Exit; + } + // + // It seems no need to stall after changing bus freqeuncy. + // It is said that the freqeuncy can be changed at any time. J= ust appends 8 clocks after command. + // But SetClock alreay has delay. + // + } + } + + } + + // + // Prefer wide bus width for performance + // + // + // Set to BusWidth bits mode, only version 4.0 or above support more= than 1 bits + // + if (SdHostIo->HostCapability.BusWidth8 =3D=3D TRUE) { + Status =3D MMCCardSetBusWidth (CardData, 8, EnableDDRMode); + if (EFI_ERROR (Status)) { + // + // CE-ATA may support 8 bits and 4 bits, but has no software m= ethod for detection + // + Status =3D MMCCardSetBusWidth (CardData, 4, EnableDDRMode); + if (EFI_ERROR (Status)) { + goto Exit; + } + } + } else if (SdHostIo->HostCapability.BusWidth4 =3D=3D TRUE) { + Status =3D MMCCardSetBusWidth (CardData, 4, EnableDDRMode); + if (EFI_ERROR (Status)) { + goto Exit; + } + } + + PowerValue =3D 0; + + if (CardData->CurrentBusWidth =3D=3D 8) { + if ((CardData->ExtCSDRegister.CARD_TYPE & BIT1) !=3D 0) { + PowerValue =3D CardData->ExtCSDRegister.PWR_CL_52_360; + PowerValue =3D PowerValue >> 4; + } else if ((CardData->ExtCSDRegister.CARD_TYPE & BIT0) !=3D 0) { + PowerValue =3D CardData->ExtCSDRegister.PWR_CL_26_360; + PowerValue =3D PowerValue >> 4; + } + } else if (CardData->CurrentBusWidth =3D=3D 4) { + if ((CardData->ExtCSDRegister.CARD_TYPE & BIT1) !=3D 0) { + PowerValue =3D CardData->ExtCSDRegister.PWR_CL_52_360; + PowerValue =3D PowerValue & 0xF; + } else if ((CardData->ExtCSDRegister.CARD_TYPE & BIT0) !=3D 0) { + PowerValue =3D CardData->ExtCSDRegister.PWR_CL_26_360; + PowerValue =3D PowerValue & 0xF; + } + } + + if (PowerValue !=3D 0) { + // + // Update Power Class + // + ZeroMem (&SwitchArgument, sizeof (SWITCH_ARGUMENT)); + SwitchArgument.CmdSet =3D 0; + SwitchArgument.Value =3D PowerValue; + SwitchArgument.Index =3D (UINT32) ((UINTN) + (&(CardData->ExtCSDRegister.POWER_CLASS)) - (UINTN) (&(CardData->E= xtCSDRegister))); + SwitchArgument.Access =3D WriteByte_Mode; + Status =3D SendCommand ( + CardData, + SWITCH, + *(UINT32 *) &SwitchArgument, + NoData, + NULL, + 0, + ResponseR1b, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (!EFI_ERROR (Status)) { + Status =3D SendCommand ( + CardData, + SEND_STATUS, + (CardData->Address << 16), + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SWITCH Power Class Fail Status =3D 0x%x= \n", Status)); + } + } + } + + } else { + DEBUG ((EFI_D_ERROR, "MMC Card version %d only supportes 1 bits at l= ower transfer speed\n",CardData->CSDRegister.SPEC_VERS)); + } + } else { + // + // Pin 1, at power up this line has a 50KOhm pull up enabled in the = card. + // This pull-up should be disconnected by the user, during regular d= ata transfer, + // with SET_CLR_CARD_DETECT (ACMD42) command + // + Status =3D SendAppCommand ( + CardData, + SET_CLR_CARD_DETECT, + 0, + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SET_CLR_CARD_DETECT Fail Status =3D 0x%x\n",= Status)); + goto Exit; + } + + // + // Set Bus Width to 4 + // + Status =3D SendAppCommand ( + CardData, + SET_BUS_WIDTH, + SD_BUS_WIDTH_4, + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SET_BUS_WIDTH 4 bits Fail Status =3D 0x%x\n"= , Status)); + goto Exit; + } + + Status =3D SdHostIo->SetBusWidth (SdHostIo, 4); + if (EFI_ERROR (Status)) { + goto Exit; + } + CardData->CurrentBusWidth =3D 4; + + if ((SdHostIo->HostCapability.HighSpeedSupport =3D=3D FALSE) || + ((CardData->CSDRegister.CCC & BIT10) !=3D BIT10)) { + // + // Host must support high speed + // Card must support Switch function + // + goto Exit; + } + + // + // Mode =3D 0, group 1, function 1, check operation + // + Argument =3D 0xFFFF01; + ZeroMem (&CardData->SwitchStatus, sizeof (SWITCH_STATUS)); + + Status =3D SendCommand ( + CardData, + SWITCH_FUNC, + Argument, + InData, + CardData->AlignedBuffer, + sizeof (SWITCH_STATUS), + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + goto Exit; + } + CopyMem (&(CardData->SwitchStatus), CardData->AlignedBuffer, sizeof = (SWITCH_STATUS)); + + if ((CardData->SwitchStatus.DataStructureVersion =3D=3D 0x0) || + ((CardData->SwitchStatus.Group1BusyStatus & BIT1) !=3D BIT1)) { + Argument =3D 0xFFFF01 | BIT31; + ZeroMem (&CardData->SwitchStatus, sizeof (SWITCH_STATUS)); + + Status =3D SendCommand ( + CardData, + SWITCH_FUNC, + Argument, + InData, + CardData->AlignedBuffer, + sizeof (SWITCH_STATUS), + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + goto Exit; + } + CopyMem (&(CardData->SwitchStatus), CardData->AlignedBuffer, size= of (SWITCH_STATUS)); + + if ((CardData->SwitchStatus.DataStructureVersion =3D=3D 0x0) || + ((CardData->SwitchStatus.Group1BusyStatus & BIT1) !=3D BIT1)) { + gBS->Stall (1000); + + // + // Change host clock + // + Status =3D SdHostIo->SetClockFrequency (SdHostIo, FREQUENCY_SD_P= P_HIGH); + if (EFI_ERROR (Status)) { + goto Exit; + } + + } + } + } + if (!((CardData->ExtCSDRegister.CARD_TYPE & BIT2) || + (CardData->ExtCSDRegister.CARD_TYPE & BIT3))) { + + // + // Set Block Length, to improve compatibility in case of some cards + // + Status =3D SendCommand ( + CardData, + SET_BLOCKLEN, + 512, + NoData, + NULL, + 0, + ResponseR1, + TIMEOUT_COMMAND, + (UINT32 *) &(CardData->CardStatus) + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SET_BLOCKLEN Fail Status =3D 0x%x\n", Status)); + goto Exit; + } + } + SdHostIo->SetBlockLength (SdHostIo, 512); + +Exit: + return Status; +} + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdMed= iaDeviceDxe/SdMediaDevice.c b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/= Sdio/Dxe/SD/SdMediaDeviceDxe/SdMediaDevice.c new file mode 100644 index 0000000..ad0eb2c --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdMediaDevic= eDxe/SdMediaDevice.c @@ -0,0 +1,324 @@ +/** @file + The definition for SD media device driver model and blkio protocol routi= nes. + + Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include "SdMediaDevice.h" + +EFI_DRIVER_BINDING_PROTOCOL gSdMediaDeviceDriverBinding =3D { + SdMediaDeviceSupported, + SdMediaDeviceStart, + SdMediaDeviceStop, + 0x20, + NULL, + NULL +}; + + +/** + Entry point for EFI drivers. + + @param[in] ImageHandle EFI_HANDLE. + @param[in] SystemTable EFI_SYSTEM_TABLE. + + @retval EFI_SUCCESS Driver is successfully loaded. + @retval Others Failed. + +**/ +EFI_STATUS +EFIAPI +InitializeSdMediaDevice ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + return EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gSdMediaDeviceDriverBinding, + ImageHandle, + &gSdMediaDeviceName, + &gSdMediaDeviceName2 + ); +} + + +/** + Test to see if this driver supports ControllerHandle. Any + ControllerHandle that has BlockIoProtocol installed will be supported. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to test. + @param[in] RemainingDevicePath Not used. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_UNSUPPORTED This driver does not support this devic= e. + +**/ +EFI_STATUS +EFIAPI +SdMediaDeviceSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) +{ + EFI_STATUS Status; + EFI_SD_HOST_IO_PROTOCOL *SdHostIo; + + // + // Test whether there is PCI IO Protocol attached on the controller hand= le. + // + Status =3D gBS->OpenProtocol ( + Controller, + &gEfiSdHostIoProtocolGuid, + (VOID **) &SdHostIo, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + + if (EFI_ERROR (Status)) { + goto Exit; + } + + gBS->CloseProtocol ( + Controller, + &gEfiSdHostIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + +Exit: + return Status; +} + + +/** + Starting the SD Media Device Driver. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to test. + @param[in] RemainingDevicePath Not used. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_UNSUPPORTED This driver does not support this devic= e. + @retval EFI_DEVICE_ERROR This driver cannot be started due to de= vice Error. + EFI_OUT_OF_RESOURCES- Failed due to res= ource shortage. + +**/ +EFI_STATUS +EFIAPI +SdMediaDeviceStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) +{ + EFI_STATUS Status; + EFI_SD_HOST_IO_PROTOCOL *SdHostIo; + CARD_DATA *CardData; + + CardData =3D NULL; + + // + // Open PCI I/O Protocol and save pointer to open protocol + // in private data area. + // + Status =3D gBS->OpenProtocol ( + Controller, + &gEfiSdHostIoProtocolGuid, + (VOID **) &SdHostIo, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SDMediaDeviceStart: Fail to open gEfiSdHostIoPro= tocolGuid \r\n")); + goto Exit; + } + + Status =3D SdHostIo->DetectCardAndInitHost (SdHostIo); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_INFO, "SdMediaDeviceStart: Fail to DetectCardAndInitHost= \r\n")); + goto Exit; + } + + CardData =3D (CARD_DATA *) AllocateZeroPool (sizeof (CARD_DATA)); + if (CardData =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + DEBUG ((EFI_D_ERROR, "SdMediaDeviceStart: Fail to AllocateZeroPool(CAR= D_DATA) \r\n")); + goto Exit; + } + + ASSERT (SdHostIo->HostCapability.BoundarySize >=3D 4 * 1024); + CardData->RawBufferPointer =3D (UINT8 *) ((UINTN) DMA_MEMORY_TOP); + Status =3D gBS->AllocatePages ( + AllocateMaxAddress, + EfiBootServicesData, + EFI_SIZE_TO_PAGES (2 * SdHostIo->HostCapability.Boundary= Size), + (EFI_PHYSICAL_ADDRESS *) (&CardData->RawBufferPointer) + ); + + if (CardData->RawBufferPointer =3D=3D NULL) { + DEBUG ((EFI_D_ERROR, "SdMediaDeviceStart: Fail to AllocateZeroPool(2*x= ) \r\n")); + Status =3D EFI_OUT_OF_RESOURCES; + goto Exit; + } + CardData->AlignedBuffer =3D CardData->RawBufferPointer - ((UINTN) (CardD= ata->RawBufferPointer) & (SdHostIo->HostCapability.BoundarySize - 1)) + SdH= ostIo->HostCapability.BoundarySize; + + CardData->Signature =3D CARD_DATA_SIGNATURE; + CardData->SdHostIo =3D SdHostIo; + + Status =3D MMCSDCardInit (CardData); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SdMediaDeviceStart: Fail to MMCSDCardInit \r\n")= ); + goto Exit; + } + DEBUG ((EFI_D_INFO, "SdMediaDeviceStart: MMCSDCardInit SuccessFul\n")); + + if (CardData->CardType =3D=3D CEATACard) { + Status =3D CEATABlockIoInit (CardData); + } else { + Status =3D MMCSDBlockIoInit (CardData); + } + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SdMediaDeviceStart: Fail to BlockIoInit \r\n")); + goto Exit; + } + DEBUG ((EFI_D_INFO, "SdMediaDeviceStart: BlockIo is successfully install= ed\n")); + + Status =3D gBS->InstallProtocolInterface ( + &Controller, + &gEfiBlockIoProtocolGuid, + EFI_NATIVE_INTERFACE, + &CardData->BlockIo + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "SdMediaDeviceStart: Fail to install gEfiBlockIoP= rotocolGuid \r\n")); + goto Exit; + } + + // + // Install the component name protocol + // + CardData->ControllerNameTable =3D NULL; + + AddUnicodeString2 ( + "eng", + gSdMediaDeviceName.SupportedLanguages, + &CardData->ControllerNameTable, + L"MMC/SD Media Device", + TRUE + ); + + AddUnicodeString2 ( + "en", + gSdMediaDeviceName2.SupportedLanguages, + &CardData->ControllerNameTable, + L"MMC/Sd Media Device", + FALSE + ); + +Exit: + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_INFO, "SdMediaDeviceStart: End with failure\r\n")); + if (CardData !=3D NULL) { + if (CardData->RawBufferPointer !=3D NULL) { + gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) CardData->RawBuffer= Pointer, EFI_SIZE_TO_PAGES (2 * SdHostIo->HostCapability.BoundarySize)); + } + FreePool (CardData); + } + } + + return Status; +} + + +/** + Stop this driver on ControllerHandle. Support stoping any child handles + created by this driver. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to stop driver on. + @param[in] NumberOfChildren Number of Children in the ChildHandleBu= ffer. + @param[in] ChildHandleBuffer List of handles for the children we nee= d to stop. + + @retval EFI_SUCCESS + @retval others + +**/ +EFI_STATUS +EFIAPI +SdMediaDeviceStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ) +{ + EFI_STATUS Status; + CARD_DATA *CardData; + EFI_BLOCK_IO_PROTOCOL *BlockIo; + + // + // First find BlockIo Protocol + // + Status =3D gBS->OpenProtocol ( + Controller, + &gEfiBlockIoProtocolGuid, + (VOID **) &BlockIo, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + CardData =3D CARD_DATA_FROM_THIS (BlockIo); + + // + // Uninstall Block I/O protocol from the device handle + // + Status =3D gBS->UninstallProtocolInterface ( + Controller, + &gEfiBlockIoProtocolGuid, + BlockIo + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + if (CardData !=3D NULL) { + if (CardData->RawBufferPointer !=3D NULL) { + gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) CardData->RawBufferPo= inter, EFI_SIZE_TO_PAGES (2 * CardData->SdHostIo->HostCapability.BoundarySi= ze)); + } + FreeUnicodeStringTable (CardData->ControllerNameTable); + FreePool (CardData); + } + + gBS->CloseProtocol ( + Controller, + &gEfiSdHostIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + + return EFI_SUCCESS; +} + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdMed= iaDeviceDxe/SdMediaDevice.h b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/= Sdio/Dxe/SD/SdMediaDeviceDxe/SdMediaDevice.h new file mode 100644 index 0000000..fe3c4fa --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdMediaDevic= eDxe/SdMediaDevice.h @@ -0,0 +1,466 @@ +/** @file + The definition for SD media device driver model and blkio protocol routi= nes. + + Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#ifndef _SD_MEDIA_DEVICE_H_ +#define _SD_MEDIA_DEVICE_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "ComponentName.h" +#include "SdHostIo.h" + +extern EFI_DRIVER_BINDING_PROTOCOL gSdMediaDeviceDriverBinding; +extern EFI_COMPONENT_NAME_PROTOCOL gSdMediaDeviceName; +extern EFI_COMPONENT_NAME2_PROTOCOL gSdMediaDeviceName2; + +// +// Define the region of memory used for DMA memory +// +#define DMA_MEMORY_TOP 0x0000000001FFFFFFULL +#define CARD_DATA_SIGNATURE SIGNATURE_32 ('c', 'a', 'r', 'd') + +// +// Command timeout will be max 100 ms +// +#define TIMEOUT_COMMAND 100 +#define TIMEOUT_DATA 5000 + +typedef enum{ + UnknownCard =3D 0, + MMCCard, // MMC card + MMCCardHighCap, // MMC Card High Capacity + CEATACard, // CE-ATA device + SdMemoryCard, // SD 1.1 card + SdMemoryCard2, // SD 2.0 or above standard card + SdMemoryCard2High // SD 2.0 or above high capacity card +} CARD_TYPE; + +typedef struct { + // + // BlockIO + // + UINTN Signature; + EFI_BLOCK_IO_PROTOCOL BlockIo; + EFI_BLOCK_IO_MEDIA BlockIoMedia; + + EFI_SD_HOST_IO_PROTOCOL *SdHostIo; + EFI_UNICODE_STRING_TABLE *ControllerNameTable; + CARD_TYPE CardType; + + UINT8 CurrentBusWidth; + BOOLEAN DualVoltage; + BOOLEAN NeedFlush; + UINT8 Reserved[3]; + + UINT16 Address; + UINT32 BlockLen; + UINT32 MaxFrequency; + UINT64 BlockNumber; + + // + // Common used + // + CARD_STATUS CardStatus; + OCR OCRRegister; + CID CIDRegister; + CSD CSDRegister; + EXT_CSD ExtCSDRegister; + UINT8 *RawBufferPointer; + UINT8 *AlignedBuffer; + + // + // CE-ATA specific + // + TASK_FILE TaskFile; + IDENTIFY_DEVICE_DATA IndentifyDeviceData; + + // + //SD specific + // + SCR SCRRegister; + SD_STATUS_REG SdSattus; + SWITCH_STATUS SwitchStatus; +} CARD_DATA; + +#define CARD_DATA_FROM_THIS(a) \ + CR(a, CARD_DATA, BlockIo, CARD_DATA_SIGNATURE) + +/** + Test to see if this driver supports ControllerHandle. Any + ControllerHandle that has BlockIoProtocol installed will be supported. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to test. + @param[in] RemainingDevicePath Not used. + + @return EFI_SUCCESS This driver supports this device. + @return EFI_UNSUPPORTED This driver does not support this devic= e. + +**/ +EFI_STATUS +EFIAPI +SdMediaDeviceSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ); + +/** + Starting the SD Media Device Driver. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to test. + @param[in] RemainingDevicePath Not used. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_UNSUPPORTED This driver does not support this devic= e. + @retval EFI_DEVICE_ERROR This driver cannot be started due to de= vice Error. + EFI_OUT_OF_RESOURCES- Failed due to res= ource shortage. + +**/ +EFI_STATUS +EFIAPI +SdMediaDeviceStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ); + +/** + Stop this driver on ControllerHandle. Support stoping any child handles + created by this driver. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to stop driver on. + @param[in] NumberOfChildren Number of Children in the ChildHandleBu= ffer. + @param[in] ChildHandleBuffer List of handles for the children we nee= d to stop. + + @return EFI_SUCCESS + @return others + +**/ +EFI_STATUS +EFIAPI +SdMediaDeviceStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ); + +/** + MMC/SD card init function + + @param[in] CardData Pointer to CARD_DATA. + + @return EFI_SUCCESS + @return others + +**/ +EFI_STATUS +MMCSDCardInit ( + IN CARD_DATA *CardData + ); + +/** + Send command by using Host IO protocol + + @param[in] This A pointer to the EFI_SD_HOST_IO_PROTOC= OL instance. + @param[in] CommandIndex The command index to set the command i= ndex field of command register. + @param[in] Argument Command argument to set the argument f= ield of command register. + @param[in] DataType TRANSFER_TYPE, indicates no data, data= in or data out. + @param[in] Buffer Contains the data read from / write to= the device. + @param[in] BufferSize The size of the buffer. + @param[in] ResponseType RESPONSE_TYPE. + @param[in] TimeOut Time out value in 1 ms unit. + @param[out] ResponseData Depending on the ResponseType, such as= CSD or card status. + + @retval EFI_SUCCESS + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_DEVICE_ERROR + +**/ +EFI_STATUS +SendCommand ( + IN CARD_DATA *CardData, + IN UINT16 CommandIndex, + IN UINT32 Argument, + IN TRANSFER_TYPE DataType, + IN UINT8 *Buffer, OPTIONAL + IN UINT32 BufferSize, + IN RESPONSE_TYPE ResponseType, + IN UINT32 TimeOut, + OUT UINT32 *ResponseData + ); + +/** + Send the card APP_CMD command with the following command indicated by Co= mmandIndex + + @param[in] CardData Pointer to CARD_DATA. + @param[in] CommandIndex The command index to set the command i= ndex field of command register. + @param[in] Argument Command argument to set the argument f= ield of command register. + @param[in] DataType TRANSFER_TYPE, indicates no data, data= in or data out. + @param[in] Buffer Contains the data read from / write to= the device. + @param[in] BufferSize The size of the buffer. + @param[in] ResponseType RESPONSE_TYPE. + @param[in] TimeOut Time out value in 1 ms unit. + @param[out] ResponseData Depending on the ResponseType, such as= CSD or card status. + + @retval EFI_SUCCESS + @retval EFI_INVALID_PARAMETER + @retval EFI_UNSUPPORTED + @retval EFI_DEVICE_ERROR + +**/ +EFI_STATUS +SendAppCommand ( + IN CARD_DATA *CardData, + IN UINT16 CommandIndex, + IN UINT32 Argument, + IN TRANSFER_TYPE DataType, + IN UINT8 *Buffer, OPTIONAL + IN UINT32 BufferSize, + IN RESPONSE_TYPE ResponseType, + IN UINT32 TimeOut, + OUT UINT32 *ResponseData + ); + +/** + Send the card FAST_IO command + + @param[in] CardData Pointer to CARD_DATA. + @param[in] RegisterAddress Register Address. + @param[in, out] RegisterData Pointer to register Data. + @param[in] Write TRUE for write, FALSE for read. + + @retval EFI_SUCCESS + @retval EFI_UNSUPPORTED + @retval EFI_INVALID_PARAMETER + @retval EFI_DEVICE_ERROR + +**/ +EFI_STATUS +FastIO ( + IN CARD_DATA *CardData, + IN UINT8 RegisterAddress, + IN OUT UINT8 *RegisterData, + IN BOOLEAN Write + ); + +/** + Judge whether it is CE-ATA device or not. + + @param[in] CardData Pointer to CARD_DATA. + + @retval TRUE + @retval FALSE + +**/ +BOOLEAN +IsCEATADevice ( + IN CARD_DATA *CardData + ); + +/** + Send software reset + + @param[in] CardData Pointer to CARD_DATA. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +SoftwareReset ( + IN CARD_DATA *CardData + ); + +/** + SendATACommand specificed in Taskfile + + @param[in] CardData Pointer to CARD_DATA. + @param[in] TaskFile Pointer to TASK_FILE. + @param[in] Write TRUE means write, FALSE means read. + @param[in] Buffer If NULL, means no data transfer, n= either read nor write. + @param[in] SectorCount Buffer size in 512 bytes unit. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +SendATACommand ( + IN CARD_DATA *CardData, + IN TASK_FILE *TaskFile, + IN BOOLEAN Write, + IN UINT8 *Buffer, + IN UINT16 SectorCount + ); + +/** + IDENTIFY_DEVICE command + + @param[in] CardData Pointer to CARD_DATA. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +IndentifyDevice ( + IN CARD_DATA *CardData + ); + +/** + FLUSH_CACHE_EXT command + + @param[in] CardData Pointer to CARD_DATA. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +FlushCache ( + IN CARD_DATA *CardData + ); + +/** + STANDBY_IMMEDIATE command + + @param[in] CardData Pointer to CARD_DATA. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +StandByImmediate ( + IN CARD_DATA *CardData + ); + +/** + READ_DMA_EXT command + + @param[in] CardData Pointer to CARD_DATA. + @param[in] LBA The starting logical block addres= s to read from on the device. + @param[in] Buffer A pointer to the destination buff= er for the data. The caller + is responsible for either having = implicit or explicit ownership + of the buffer. + @param[in] SectorCount Size in 512 bytes unit. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +ReadDMAExt ( + IN CARD_DATA *CardData, + IN EFI_LBA LBA, + IN UINT8 *Buffer, + IN UINT16 SectorCount + ); + +/** + WRITE_DMA_EXT command + + @param[in] CardData Pointer to CARD_DATA. + @param[in] LBA The starting logical block addres= s to read from on the device. + @param[in] Buffer A pointer to the destination buff= er for the data. The caller + is responsible for either having = implicit or explicit ownership + of the buffer. + @param[in] SectorCount Size in 512 bytes unit. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + @retval EFI_INVALID_PARAMETER Parameter is error + @retval EFI_NO_MEDIA No media + @retval EFI_MEDIA_CHANGED Media Change + @retval EFI_BAD_BUFFER_SIZE Buffer size is bad + +**/ +EFI_STATUS +WriteDMAExt ( + IN CARD_DATA *CardData, + IN EFI_LBA LBA, + IN UINT8 *Buffer, + IN UINT16 SectorCount + ); + +/** + CEATA card BlockIo init function. + + @param[in] CardData Pointer to CARD_DATA. + + @retval EFI_SUCCESS + @retval Others + +**/ +EFI_STATUS +CEATABlockIoInit ( + IN CARD_DATA *CardData + ); + +/** + MMC/SD card BlockIo init function. + + @param[in] CardData Pointer to CARD_DATA. + + @retval EFI_SUCCESS + @retval Others + +**/ +EFI_STATUS +MMCSDBlockIoInit ( + IN CARD_DATA *CardData + ); + +#endif + diff --git a/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdMed= iaDeviceDxe/SdMediaDeviceDxe.inf b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthClu= ster/Sdio/Dxe/SD/SdMediaDeviceDxe/SdMediaDeviceDxe.inf new file mode 100644 index 0000000..e352b1d --- /dev/null +++ b/Silicon/BroxtonSoC/BroxtonSiPkg/SouthCluster/Sdio/Dxe/SD/SdMediaDevic= eDxe/SdMediaDeviceDxe.inf @@ -0,0 +1,64 @@ +## @file +# Component Description File For SdMediaDeviceDxe Module. +# +# Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the B= SD License +# which accompanies this distribution. The full text of the license may b= e 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 IM= PLIED. +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D SdMediaDevice + FILE_GUID =3D E076205A-6EDC-4F68-A535-93C5D790B1DA + MODULE_TYPE =3D UEFI_DRIVER + VERSION_STRING =3D 1.0 + ENTRY_POINT =3D InitializeSdMediaDevice + +# +# The following information is for reference only and not required by the= build tools. +# +# VALID_ARCHITECTURES =3D IA32 X64 IPF EBC +# +# DRIVER_BINDING =3D gSdMediaDeviceDriverBinding +# COMPONENT_NAME =3D gSdMediaDeviceName +# COMPONENT_NAME2 =3D gSdMediaDeviceName2 +# + +[Sources] + SdMediaDevice.c + SdMediaDevice.h + MMCSDTransfer.c + CEATA.c + CEATABlockIo.c + MMCSDBlockIo.c + ComponentName.c + ComponentName.h + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + BroxtonSiPkg/BroxtonSiPkg.dec + +[LibraryClasses] + MemoryAllocationLib + BaseLib + UefiLib + UefiBootServicesTableLib + UefiDriverEntryPoint + BaseMemoryLib + DebugLib + PcdLib + +[Protocols] + gEfiPciIoProtocolGuid ## TO_START + gEfiSdHostIoProtocolGuid ## TO_START + gEfiBlockIoProtocolGuid ## BY_START + +[Pcd.common] --=20 2.7.0.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel