From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from zg8tmja5ljk3lje4mi4ymjia.icoremail.net (zg8tmja5ljk3lje4mi4ymjia.icoremail.net [209.97.182.222]) by mx.groups.io with SMTP id smtpd.web09.6573.1615546933917414041 for ; Fri, 12 Mar 2021 03:02:14 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: phytium.com.cn, ip: 209.97.182.222, mailfrom: jialing@phytium.com.cn) Received: from localhost.localdomain (unknown [223.104.131.160]) by c1app4 (Coremail) with SMTP id BAINCgC3mwEVSktgMY01BQ--.34795S19; Fri, 12 Mar 2021 19:02:11 +0800 (CST) From: Ling Jia To: devel@edk2.groups.io Cc: Leif Lindholm , Peng Xie , Yiqi Shu , Ling Jia Subject: [PATCH v3 42/46] Silicon/Phytium: Added Spi driver support to FT2000/4 Date: Fri, 12 Mar 2021 19:01:37 +0800 Message-Id: <20210312110141.75749-18-jialing@phytium.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210312110141.75749-1-jialing@phytium.com.cn> References: <20210312110141.75749-1-jialing@phytium.com.cn> MIME-Version: 1.0 X-CM-TRANSID: BAINCgC3mwEVSktgMY01BQ--.34795S19 X-Coremail-Antispam: 1UD129KBjvJXoW3CF15XrW3GrWUJFy5ArWDArb_yoWkJF18pF 4xtrsIgr48Gr4aqw4rX340grs5A3s09a4DKrsxWFy5ZFs8WFy8Xa1qyry5JFyqqr4qyFW8 WFsYqw1Uur1qyw7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUm014x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26r4UJVWxJr1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl 6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6x IIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_ Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7MxkF7I0En4kS14v26r 126r1DMxkIecxEwVAFwVW8KwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8 JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1V AFwI0_JF0_Jw1lIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVW5JVW7JwCI42IY6xII jxv20xvEc7CjxVAFwI0_Gr1j6F4UJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcV C2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8Jr0_Cr1UYxBIdaVFxhVj vjDU0xZFpf9x0JUk8nOUUUUU= X-Originating-IP: [223.104.131.160] X-CM-SenderInfo: xmldzxdqj61x51wl3zoofrzhdfq/ Content-Transfer-Encoding: quoted-printable The SpiDxe is to provide Spi bus read-write interfaces. v3: Optimized the codes to conform to specifications. Signed-off-by: Ling Jia Reviewed-by: Leif Lindholm --- Silicon/Phytium/PhytiumCommonPkg/PhytiumCommonPkg.dec | 9 + Platform/Phytium/DurianPkg/DurianPkg.dsc | 5 + Platform/Phytium/DurianPkg/DurianPkg.fdf | 2 + Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.inf | 44 +++++ Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.h | 64 ++++= +++ Silicon/Phytium/PhytiumCommonPkg/Include/Protocol/SpiProtocol.h | 51 +++++ Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.c | 198 ++++= ++++++++++++++++ 7 files changed, 373 insertions(+) diff --git a/Silicon/Phytium/PhytiumCommonPkg/PhytiumCommonPkg.dec b/Silico= n/Phytium/PhytiumCommonPkg/PhytiumCommonPkg.dec index 48f430c88de6..69842b89e021 100644 --- a/Silicon/Phytium/PhytiumCommonPkg/PhytiumCommonPkg.dec +++ b/Silicon/Phytium/PhytiumCommonPkg/PhytiumCommonPkg.dec @@ -38,4 +38,13 @@ [PcdsFixedAtBuild.common] gPhytiumPlatformTokenSpaceGuid.PcdPciConfigBase|0x0|UINT64|0x00000002=0D gPhytiumPlatformTokenSpaceGuid.PcdPciConfigSize|0x0|UINT64|0x00000003=0D =0D + #=0D + # SPI Flash Controller Register Base Address and Size=0D + #=0D + gPhytiumPlatformTokenSpaceGuid.PcdSpiFlashBase|0x0|UINT64|0x00000004=0D + gPhytiumPlatformTokenSpaceGuid.PcdSpiFlashSize|0x0|UINT64|0x00000005=0D + gPhytiumPlatformTokenSpaceGuid.PcdSpiControllerBase|0x0|UINT64|0x0000000= 6=0D + gPhytiumPlatformTokenSpaceGuid.PcdSpiControllerSize|0x0|UINT64|0x0000000= 7=0D +=0D [Protocols]=0D + gSpiMasterProtocolGuid =3D { 0xdf093560, 0xf955, 0x11ea, { 0x96, 0x42, 0= x43, 0x9d, 0x80, 0xdd, 0x0b, 0x7c}}=0D diff --git a/Platform/Phytium/DurianPkg/DurianPkg.dsc b/Platform/Phytium/Du= rianPkg/DurianPkg.dsc index 3a9bc2289cf3..68698d613f96 100644 --- a/Platform/Phytium/DurianPkg/DurianPkg.dsc +++ b/Platform/Phytium/DurianPkg/DurianPkg.dsc @@ -244,6 +244,11 @@ [Components.common] #=0D ArmPkg/Drivers/GenericWatchdogDxe/GenericWatchdogDxe.inf=0D =0D + #=0D + # Spi driver=0D + #=0D + Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.inf=0D +=0D #=0D # Usb Support=0D #=0D diff --git a/Platform/Phytium/DurianPkg/DurianPkg.fdf b/Platform/Phytium/Du= rianPkg/DurianPkg.fdf index a443d0f3a4ac..1cf1927484db 100644 --- a/Platform/Phytium/DurianPkg/DurianPkg.fdf +++ b/Platform/Phytium/DurianPkg/DurianPkg.fdf @@ -95,6 +95,8 @@ [FV.FvMain] INF MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf=0D INF EmbeddedPkg/MetronomeDxe/MetronomeDxe.inf=0D =0D + INF Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.inf=0D +=0D INF MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleRuntimeDxe.inf=0D INF MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCounterRu= ntimeDxe.inf=0D =0D diff --git a/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.inf b/Silico= n/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.inf new file mode 100644 index 000000000000..21d75f268da4 --- /dev/null +++ b/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.inf @@ -0,0 +1,44 @@ +#/** @file=0D +# Phytium Spi Master Drivers.=0D +#=0D +# Copyright (C) 2020, Phytium Technology Co, Ltd. All rights reserved.=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +#**/=0D +=0D +[Defines]=0D + INF_VERSION =3D 0x0001001b=0D + BASE_NAME =3D SpiDxe=0D + FILE_GUID =3D 2ba95e5c-f7f5-11ea-bf18-67fdc5787495= =0D + MODULE_TYPE =3D DXE_RUNTIME_DRIVER=0D + VERSION_STRING =3D 1.0=0D + ENTRY_POINT =3D SpiMasterDrvEntryPoint=0D +=0D +[Sources.common]=0D + SpiDxe.c=0D + SpiDxe.h=0D +=0D +[Packages]=0D + ArmPkg/ArmPkg.dec=0D + MdePkg/MdePkg.dec=0D + Silicon/Phytium/PhytiumCommonPkg/PhytiumCommonPkg.dec=0D +=0D +[LibraryClasses]=0D + BaseLib=0D + DebugLib=0D + IoLib=0D + UefiLib=0D + UefiBootServicesTableLib=0D + UefiDriverEntryPoint=0D +=0D +[Guids]=0D +=0D +[Protocols]=0D + gSpiMasterProtocolGuid=0D +=0D +[FixedPcd]=0D + gPhytiumPlatformTokenSpaceGuid.PcdSpiControllerBase=0D +=0D +[Depex]=0D + TRUE=0D diff --git a/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.h b/Silicon/= Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.h new file mode 100644 index 000000000000..fbadd01921ff --- /dev/null +++ b/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.h @@ -0,0 +1,64 @@ +/** @file=0D + Phytium Spi Drivers Header=0D +=0D + Copyright (C) 2020, Phytium Technology Co Ltd. All rights reserved.
= =0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#ifndef SPI_DXE_H_=0D +#define SPI_DXE_H_=0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +#define SPI_MASTER_SIGNATURE SIGNATURE_32 ('M', 'S', 'P', 'I')=0D +#define REG_MODE_REG 0x02C=0D +=0D +EFI_STATUS=0D +EFIAPI=0D +SpiMasterGetConfig (=0D + IN UINT8 CmdId,=0D + OUT UINT32 *Config,=0D + IN UINTN RegAddr=0D + );=0D +=0D +EFI_STATUS=0D +EFIAPI=0D +SpiMasterSetConfig (=0D + IN UINT8 CmdId,=0D + IN UINT32 Config,=0D + IN UINTN RegAddr=0D + );=0D +=0D +EFI_STATUS=0D +EFIAPI=0D +SpiMasterSetMode (=0D + IN UINT32 Config=0D + );=0D +=0D +EFI_STATUS=0D +EFIAPI=0D +SpiMasterInit (=0D + VOID=0D + );=0D +=0D +typedef struct {=0D + EFI_SPI_DRV_PROTOCOL SpiMasterProtocol;=0D + UINTN Signature;=0D + EFI_HANDLE Handle;=0D +} PHYT_SPI_MASTER;=0D +=0D +EFI_STATUS=0D +EFIAPI=0D +SpiMasterDrvEntryPoint (=0D + IN EFI_HANDLE ImageHandle,=0D + IN EFI_SYSTEM_TABLE *SystemTable=0D + );=0D +=0D +#endif // SPI_DXE_H_=0D diff --git a/Silicon/Phytium/PhytiumCommonPkg/Include/Protocol/SpiProtocol.= h b/Silicon/Phytium/PhytiumCommonPkg/Include/Protocol/SpiProtocol.h new file mode 100644 index 000000000000..3ed64d1a5dd7 --- /dev/null +++ b/Silicon/Phytium/PhytiumCommonPkg/Include/Protocol/SpiProtocol.h @@ -0,0 +1,51 @@ +/** @file=0D + The Header of Protocol For SPI.=0D +=0D + Copyright (C) 2020, Phytium Technology Co Ltd. All rights reserved.
= =0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#ifndef SPI_H_=0D +#define SPI_H_=0D +=0D +extern EFI_GUID gSpiMasterProtocolGuid;=0D +typedef struct _EFI_SPI_DRV_PROTOCOL EFI_SPI_DRV_PROTOCOL;=0D +=0D +typedef=0D +EFI_STATUS=0D +(EFIAPI *SPI_DRV_INIT_INTERFACE) (=0D + VOID=0D + );=0D +=0D +typedef=0D +EFI_STATUS=0D +(EFIAPI *SPI_DRV_SET_CONFIG_INTERFACE)(=0D + IN UINT8 CmdId,=0D + IN UINT32 Config,=0D + IN UINTN RegAddr=0D + );=0D +=0D +typedef=0D +EFI_STATUS=0D +(EFIAPI *SPI_DRV_GET_CONFIG_INTERFACE)(=0D + IN UINT8 CmdId,=0D + OUT UINT32 *Config,=0D + IN UINTN RegAddr=0D + );=0D +=0D +typedef=0D +EFI_STATUS=0D +(EFIAPI *SPI_DRV_CONFIG_MODE_INTERFACE)(=0D + IN UINT32 Config=0D + );=0D +=0D +struct _EFI_SPI_DRV_PROTOCOL{=0D + SPI_DRV_INIT_INTERFACE SpiInit;=0D + SPI_DRV_SET_CONFIG_INTERFACE SpiSetConfig;=0D + SPI_DRV_GET_CONFIG_INTERFACE SpiGetConfig;=0D + SPI_DRV_CONFIG_MODE_INTERFACE SpiSetMode;=0D +};=0D +=0D +#endif // SPI_H_=0D diff --git a/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.c b/Silicon/= Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.c new file mode 100644 index 000000000000..885bbd63619c --- /dev/null +++ b/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.c @@ -0,0 +1,198 @@ +/** @file=0D + Phytium Spi Master Drivers.=0D +=0D + Copyright (C) 2020, Phytium Technology Co Ltd. All rights reserved.
= =0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include "SpiDxe.h"=0D +=0D +PHYT_SPI_MASTER *pSpiMasterInstance;=0D +static UINTN mSpiControlBase;=0D +=0D +/**=0D + This function inited a spi driver.=0D +=0D + @param None.=0D +=0D + @retval None.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +SpiMasterInit (=0D + VOID=0D + )=0D +{=0D + return EFI_SUCCESS;=0D +}=0D +=0D +=0D +/**=0D + This function seted config to spi registers.=0D +=0D + @param[in] CmdId The id of command.=0D +=0D + @param[in] Config The value to be seted.=0D +=0D + @param[in] RegAddr The address of spi registers.=0D +=0D + @retval EFI_SUCCESS SpiMasterSetConfig() is executed successfully.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +SpiMasterSetConfig (=0D + IN UINT8 CmdId,=0D + IN UINT32 Config,=0D + IN UINTN RegAddr=0D + )=0D +{=0D + UINTN SpiAddr;=0D + UINT32 Value;=0D +=0D + SpiAddr =3D 0;=0D + Value =3D 0;=0D +=0D + if (CmdId !=3D 0) {=0D + Value =3D (CmdId << 24) | (Config & 0xffffff);=0D + } else {=0D + Value =3D Config;=0D + }=0D +=0D + SpiAddr =3D mSpiControlBase + RegAddr;=0D + MmioWrite32 (SpiAddr, Value);=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +=0D +/**=0D + This function geted config from spi registers.=0D +=0D + @param[in] CmdId The id of command.=0D +=0D + @param[out] Config The pointer of the config.=0D +=0D + @param[in] RegAddr The address of spi registers.=0D +=0D + @retval EFI_SUCCESS SpiMasterGetConfig() is executed successfully.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +SpiMasterGetConfig (=0D + IN UINT8 CmdId,=0D + OUT UINT32 *Config,=0D + IN UINTN RegAddr=0D + )=0D +{=0D + UINTN SpiAddr;=0D + UINT32 Value;=0D +=0D + SpiAddr =3D 0;=0D + Value =3D 0;=0D +=0D + SpiAddr =3D mSpiControlBase + RegAddr;=0D + Value =3D MmioRead32 (SpiAddr);=0D +=0D + if (CmdId !=3D 0) {=0D + *Config =3D Value & 0xffffff;=0D + } else {=0D + *Config =3D Value;=0D + }=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +=0D +/**=0D + This function seted spi mode.=0D +=0D + @param[in] Config The value to seted.=0D +=0D + @retval EFI_SUCCESS SpiMasterSetMode() is executed successfully.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +SpiMasterSetMode (=0D + IN UINT32 Config=0D + )=0D +{=0D +=0D + SpiMasterSetConfig (0, Config, REG_MODE_REG);=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +=0D +/**=0D + This function inited the spi driver protocol.=0D +=0D + @param[in] SpiMasterProtocol A pointer to the master protocol struct.= =0D +=0D + @retval EFI_SUCCESS SpiMasterInitProtocol() is executed succ= essfully.=0D +=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +EFIAPI=0D +SpiMasterInitProtocol (=0D + IN EFI_SPI_DRV_PROTOCOL *SpiMasterProtocol=0D + )=0D +{=0D +=0D + SpiMasterProtocol->SpiInit =3D SpiMasterInit;=0D + SpiMasterProtocol->SpiSetConfig =3D SpiMasterSetConfig;=0D + SpiMasterProtocol->SpiGetConfig =3D SpiMasterGetConfig;=0D + SpiMasterProtocol->SpiSetMode =3D SpiMasterSetMode;=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +=0D +/**=0D + This function is the entrypoint of the spi driver.=0D +=0D + @param[in] ImageHandle The firmware allocated handle for the EFI imag= e.=0D +=0D + @param[in] SystemTable A pointer to the EFI System Table.=0D +=0D + @retval EFI_SUCCESS The entry point is executed successfully.=0D +=0D + @retval other Some error occurs when executing this entry po= int.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +SpiMasterDrvEntryPoint (=0D + IN EFI_HANDLE ImageHandle,=0D + IN EFI_SYSTEM_TABLE *SystemTable=0D + )=0D +{=0D + EFI_STATUS Status;=0D +=0D + pSpiMasterInstance =3D AllocateRuntimeZeroPool (sizeof (PHYT_SPI_MASTER)= );=0D + if (pSpiMasterInstance =3D=3D NULL) {=0D + return EFI_OUT_OF_RESOURCES;=0D + }=0D +=0D + mSpiControlBase =3D FixedPcdGet64 (PcdSpiControllerBase);=0D +=0D + SpiMasterInitProtocol (&pSpiMasterInstance->SpiMasterProtocol);=0D +=0D + pSpiMasterInstance->Signature =3D SPI_MASTER_SIGNATURE;=0D +=0D + Status =3D gBS->InstallMultipleProtocolInterfaces (=0D + &(pSpiMasterInstance->Handle),=0D + &gSpiMasterProtocolGuid,=0D + &(pSpiMasterInstance->SpiMasterProtocol),=0D + NULL=0D + );=0D + ASSERT_EFI_ERROR (Status);=0D +=0D + return EFI_SUCCESS;=0D +}=0D --=20 2.25.1