From nobody Wed May 15 07:46:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+99289+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+99289+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1675064265; cv=none; d=zohomail.com; s=zohoarc; b=ed5fRNx8ZEVaCVn8nOi8DlYcqGs2GI1dEqfyQQchxP4KKS8Vot2d7TUMARN01wYgJMJ4kYp0U7J3iLdmtH1rxsNK/ujm5YyMulNcFcn/pq51JPTyOuBRgm9/mar1eK96gyRv+L/gx6yMz+kVhCi1Yq9gfQy0Ur+Ekp8NMj0tuxI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1675064265; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=jfvuBKl/c/pog/JWDaWM9muV2VuOi2w6KuK9Kv7V8OA=; b=c7IIsj5+iqYOuuG4ZcFDIIfRYzxXK3T1cYPlMYWWhHmNwd3rrQ1UuMO08fjk3WIfQfJNdR7mCSwpO4fWnNrVZiVGsSET44UN9/BFx13SZiVN9XkJIuu49zrup5Lsf9n9kQSPjBHbYAzCLm6AFBwjgQfvXQ7n2TSrR77k/gwivVs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+99289+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1675064265983734.524210008472; Sun, 29 Jan 2023 23:37:45 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id 0nY4YY1788612xnIZTugFgmZ; Sun, 29 Jan 2023 23:37:45 -0800 X-Received: from azure-sdnproxy.icoremail.net (azure-sdnproxy.icoremail.net [20.231.56.155]) by mx.groups.io with SMTP id smtpd.web10.35958.1675064260173980105 for ; Sun, 29 Jan 2023 23:37:42 -0800 X-Received: from prodtpl.icoremail.net (unknown [10.12.1.20]) by hzbj-icmmx-6 (Coremail) with SMTP id AQAAfwDHaOwNdNdjy7ySBA--.56144S2; Mon, 30 Jan 2023 15:38:53 +0800 (CST) X-Received: from localhost.localdomain (unknown [117.136.89.78]) by mail (Coremail) with SMTP id AQAAfwBnZB26c9djDDUAAA--.158S3; Mon, 30 Jan 2023 15:37:35 +0800 (CST) From: "Ling Jia" To: devel@edk2.groups.io Cc: Leif Lindholm , Liming Gao , Michael D Kinney , Yiqi Shu , Peng Xie , Ling Jia Subject: [edk2-devel] [PATCH v8 1/4] Platform/Phytium: Solved problems during boot Date: Mon, 30 Jan 2023 15:34:17 +0800 Message-Id: <20230130073420.135519-2-jialing@phytium.com.cn> In-Reply-To: <20230130073420.135519-1-jialing@phytium.com.cn> References: <20230130073420.135519-1-jialing@phytium.com.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAfwBnZB26c9djDDUAAA--.158S3 X-CM-SenderInfo: xmldzxdqj61x51wl3zoofrzhdfq/ X-Coremail-Antispam: 1Uk129KBjvJXoW7KrWxCFW7Jw43WrW5tw1fJFb_yoW8Xr45pF 18Xr4DXa43Ga1j9ws5Jw1kur4Yq3s5K34vqF17C3WfZrnxXFZru3y2yF1rCasFqr4UJw48 u3Wjqw1fuFn8KaUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj DUYxn0WfASr-VFAU7a7-sFnT9fnUUIcSsGvfJ3UbIYCTnIWIevJa73UjIFyTuYvj4RJUUU UUUUU Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,jialing@phytium.com.cn X-Gm-Message-State: jRsdnDciOXnKf7z4hOzFwJfSx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1675064265; bh=rvbJCuw2DrLMfKsgBzIdtq6W06Al19tQUU4oJ1nI8ro=; h=Cc:Date:From:Reply-To:Subject:To; b=OuFVd2Mk8KBBw9BOIcp5W/+RKAi1KbswmovveIFnrATKn8k9+LqrK6UKcFnNhlblFvI zhOPwXhP7aVtPpMtMo6tiOnRHZStIht1WN0uYJBot+1uzkV7N5NsXbb2Mpq4+C87Kopk7 zlEp9YCqrt3u8lFFAc2WAZtP5apLeEWxyNQ= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1675064266426100005 Content-Type: text/plain; charset="utf-8" From: Ling Jia 1.Solved the problem of missing of dependent Library in PciHostBridgeDxe. 2.Solved the problem of missing boot management menu. Signed-off-by: Ling Jia --- Platform/Phytium/DurianPkg/DurianPkg.fdf | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Platform/Phytium/DurianPkg/DurianPkg.fdf b/Platform/Phytium/Du= rianPkg/DurianPkg.fdf index 2d4427e5eaa7..968e93ed80d1 100644 --- a/Platform/Phytium/DurianPkg/DurianPkg.fdf +++ b/Platform/Phytium/DurianPkg/DurianPkg.fdf @@ -1,7 +1,7 @@ ## @file # This package provides common open source Phytium Platform modules. # -# Copyright (C) 2020, Phytium Technology Co, Ltd. All rights reserved. +# Copyright (C) 2020-2023, Phytium Technology Co., Ltd. All rights reserve= d. # # SPDX-License-Identifier:BSD-2-Clause-Patent # @@ -142,6 +142,7 @@ [FV.FvMain] # # PCI Support # + INF ArmPkg/Drivers/ArmPciCpuIo2Dxe/ArmPciCpuIo2Dxe.inf INF MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf INF MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciHostBridgeDxe.inf =20 @@ -204,6 +205,11 @@ [FV.FvMain] INF MdeModulePkg/Universal/BdsDxe/BdsDxe.inf INF MdeModulePkg/Application/UiApp/UiApp.inf =20 + # + # Setup + # + INF MdeModulePkg/Application/BootManagerMenuApp/BootManagerMenuApp.inf + [FV.FVMAIN_COMPACT] FvAlignment =3D 16 ERASE_POLARITY =3D 1 --=20 2.25.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#99289): https://edk2.groups.io/g/devel/message/99289 Mute This Topic: https://groups.io/mt/96623622/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Wed May 15 07:46:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+99286+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+99286+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1675064265; cv=none; d=zohomail.com; s=zohoarc; b=E4WrDZhARPzCJbCU439scC9ozRXfaZxER3NcKNcYVP+7NnniNN3YYfMyo2V8/dK3mAe7L02D6TaxNaAIbhqrsAuszyMEInINJYdFkmznZtYuy0oN07OJRng8BSi+3XvF3RApFGiOTb+Ws2piKEP7mvHX1EVGaSVq5Ez1Dd3cWWE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1675064265; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=6bNz0PBJiKMdxp0+gkROVHJtTPjfDfsTckqdjQwcGWU=; b=EUeATaKU2N77Am5v/EGJgGK9foBhpDc6E6u36oje5CdA6beKbOcIcjxugsTni7V25Pjrho82zEjYUiKhVP//x6i9scKXEBfkNnHYpZz/fTwpAppx2Mj8kHUZFrGKc5TCtM2CK/kDNDPwCu6Q/Z/9VKqXUQY0JZlovD6znfQ4CGk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+99286+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1675064262979649.2003519230295; Sun, 29 Jan 2023 23:37:42 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id rHT5YY1788612xs4QrkR9Z6y; Sun, 29 Jan 2023 23:37:42 -0800 X-Received: from zg8tmtyylji0my4xnjqumte4.icoremail.net (zg8tmtyylji0my4xnjqumte4.icoremail.net [162.243.164.118]) by mx.groups.io with SMTP id smtpd.web11.36104.1675064260846509349 for ; Sun, 29 Jan 2023 23:37:41 -0800 X-Received: from prodtpl.icoremail.net (unknown [10.12.1.20]) by hzbj-icmmx-6 (Coremail) with SMTP id AQAAfwDHae0NdNdjyrySBA--.61831S2; Mon, 30 Jan 2023 15:38:53 +0800 (CST) X-Received: from localhost.localdomain (unknown [117.136.89.78]) by mail (Coremail) with SMTP id AQAAfwBnZB26c9djDDUAAA--.158S4; Mon, 30 Jan 2023 15:37:36 +0800 (CST) From: "Ling Jia" To: devel@edk2.groups.io Cc: Leif Lindholm , Liming Gao , Michael D Kinney , Yiqi Shu , Peng Xie , Ling Jia Subject: [edk2-devel] [PATCH v8 2/4] Silicon/Phytium: Added flash driver support for flash operation Date: Mon, 30 Jan 2023 15:34:18 +0800 Message-Id: <20230130073420.135519-3-jialing@phytium.com.cn> In-Reply-To: <20230130073420.135519-1-jialing@phytium.com.cn> References: <20230130073420.135519-1-jialing@phytium.com.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAfwBnZB26c9djDDUAAA--.158S4 X-CM-SenderInfo: xmldzxdqj61x51wl3zoofrzhdfq/ X-Coremail-Antispam: 1Uk129KBjvAXoWftw1xCFWUAF47ur47Jw48Crg_yoW8Zw4UCo Wxuw1IkrZ7trWIvayqgr9rCw4xXFnavan5tw40vrZxXan2qw43WrWxt3WUXrsxt348K3Zx G3yxXas8JFWaq3Wkn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7ZEXasCq-sGcSsGvf J3UbIjqfuFe4nvWSU8nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2KfnxnUUI43ZEXa7xR_UU UUUUUUU== Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,jialing@phytium.com.cn X-Gm-Message-State: ziNTQEAUxMI50bGipxNB9pySx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1675064262; bh=MKaY0Y2I0yx1oCikCVj22vX6p8b0YFIPbnkJbKXHLzM=; h=Cc:Date:From:Reply-To:Subject:To; b=lOgdhKMzBJ520HqE9+tS/o+vm3n6jyM4uGTqH/10zwK2q9IU2b7FuclCEkiQCgtVwdv kMCrvossi3YWEKZ+ksi2zGV856SwEL3Jf0FrsB5VefimKgriXKAbPjel9XUIEnOPqAlcr sdQi4GvTkdlMrehAtdcwiifv1iKq64zcBP0= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1675064266506100011 Content-Type: text/plain; charset="utf-8" From: Ling Jia The SpiNorFlashDxe provided norflash initialization, read-write, erase and other interfaces. This is a special communication protocol for FT-2000/4 chip. Signed-off-by: Ling Jia --- Silicon/Phytium/PhytiumCommonPkg/PhytiumCommonPkg.dec | = 3 +- Platform/Phytium/DurianPkg/DurianPkg.dsc | = 15 +- Platform/Phytium/DurianPkg/DurianPkg.fdf | = 6 + Silicon/Phytium/FT2000-4Pkg/Drivers/SpiNorFlashDxe/SpiNorFlashDxe.inf | = 51 +++ Silicon/Phytium/FT2000-4Pkg/Drivers/SpiNorFlashDxe/SpiNorFlashDxe.h | = 102 +++++ Silicon/Phytium/PhytiumCommonPkg/Include/Protocol/SpiNorFlashProtocol.h | = 74 ++++ Silicon/Phytium/FT2000-4Pkg/Drivers/SpiNorFlashDxe/SpiNorFlashDxe.c | = 409 ++++++++++++++++++++ 7 files changed, 658 insertions(+), 2 deletions(-) diff --git a/Silicon/Phytium/PhytiumCommonPkg/PhytiumCommonPkg.dec b/Silico= n/Phytium/PhytiumCommonPkg/PhytiumCommonPkg.dec index 8427f32211f4..dc00f4d89716 100644 --- a/Silicon/Phytium/PhytiumCommonPkg/PhytiumCommonPkg.dec +++ b/Silicon/Phytium/PhytiumCommonPkg/PhytiumCommonPkg.dec @@ -1,7 +1,7 @@ ## @file # This package provides common Phytium silicon modules. # -# Copyright (C) 2020, Phytium Technology Co,Ltd. All rights reserved. +# Copyright (C) 2020-2023, Phytium Technology Co., Ltd. All rights reserve= d. # # SPDX-License-Identifier:BSD-2-Clause-Patent # @@ -49,3 +49,4 @@ [PcdsFixedAtBuild.common] =20 [Protocols] gSpiMasterProtocolGuid =3D { 0xdf093560, 0xf955, 0x11ea, { 0x96, 0x42, 0= x43, 0x9d, 0x80, 0xdd, 0x0b, 0x7c}} + gSpiNorFlashProtocolGuid =3D { 0x00b4af42, 0xfbd0, 0x11ea, { 0x80, 0x3a,= 0x27, 0xea, 0x5e, 0x65, 0xe3, 0xf6}} diff --git a/Platform/Phytium/DurianPkg/DurianPkg.dsc b/Platform/Phytium/Du= rianPkg/DurianPkg.dsc index c1519070d616..7a390fbe1383 100644 --- a/Platform/Phytium/DurianPkg/DurianPkg.dsc +++ b/Platform/Phytium/DurianPkg/DurianPkg.dsc @@ -1,7 +1,7 @@ ## @file # This package provides common open source Phytium Platform modules. # -# Copyright (C) 2020, Phytium Technology Co, Ltd. All rights reserved. +# Copyright (C) 2020-2023, Phytium Technology Co., Ltd. All rights reserve= d. # # SPDX-License-Identifier:BSD-2-Clause-Patent # @@ -100,6 +100,14 @@ [PcdsFixedAtBuild.common] # Stack Size gArmPlatformTokenSpaceGuid.PcdCPUCorePrimaryStackSize|0x4000 =20 + # + # SPI Flash Control Register Base Address and Size + # + gPhytiumPlatformTokenSpaceGuid.PcdSpiFlashBase|0x0 + gPhytiumPlatformTokenSpaceGuid.PcdSpiFlashSize|0x1000000 + gPhytiumPlatformTokenSpaceGuid.PcdSpiControllerBase|0x28014000 + gPhytiumPlatformTokenSpaceGuid.PcdSpiControllerSize|0x1000 + gPhytiumPlatformTokenSpaceGuid.PcdRtcBaseAddress|0x2800D000 # # Designware PCI Root Complex # @@ -255,6 +263,11 @@ [Components.common] # Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.inf =20 + # + # NOR Flash driver + # + Silicon/Phytium/FT2000-4Pkg/Drivers/SpiNorFlashDxe/SpiNorFlashDxe.inf + # # Usb Support # diff --git a/Platform/Phytium/DurianPkg/DurianPkg.fdf b/Platform/Phytium/Du= rianPkg/DurianPkg.fdf index 968e93ed80d1..560be98199d2 100644 --- a/Platform/Phytium/DurianPkg/DurianPkg.fdf +++ b/Platform/Phytium/DurianPkg/DurianPkg.fdf @@ -83,6 +83,11 @@ [FV.FvMain] =20 APRIORI DXE { INF MdeModulePkg/Universal/PCD/Dxe/Pcd.inf + INF ArmPkg/Drivers/CpuDxe/CpuDxe.inf + INF ArmPkg/Drivers/ArmGic/ArmGicDxe.inf + INF MdeModulePkg/Core/RuntimeDxe/RuntimeDxe.inf + INF Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.inf + INF Silicon/Phytium/FT2000-4Pkg/Drivers/SpiNorFlashDxe/SpiNorFlashDxe.= inf } =20 INF MdeModulePkg/Core/Dxe/DxeMain.inf @@ -98,6 +103,7 @@ [FV.FvMain] INF EmbeddedPkg/MetronomeDxe/MetronomeDxe.inf =20 INF Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.inf + INF Silicon/Phytium/FT2000-4Pkg/Drivers/SpiNorFlashDxe/SpiNorFlashDxe.inf =20 INF MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleRuntimeDxe.inf INF MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCounterRu= ntimeDxe.inf diff --git a/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiNorFlashDxe/SpiNorFlash= Dxe.inf b/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiNorFlashDxe/SpiNorFlashDxe= .inf new file mode 100755 index 000000000000..e90b03489dd9 --- /dev/null +++ b/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiNorFlashDxe/SpiNorFlashDxe.inf @@ -0,0 +1,51 @@ +## @file +# +# Component file for the Phytium NorFlash Drivers. +# +# Copyright (C) 2023, Phytium Technology Co., Ltd. All rights reserved. +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x0001001B + BASE_NAME =3D SpiNorFlashDxe + FILE_GUID =3D F37EF706-187C-48FD-9102-DDBF86F551BE + MODULE_TYPE =3D DXE_RUNTIME_DRIVER + VERSION_STRING =3D 1.0 + ENTRY_POINT =3D NorFlashPlatformEntryPoint + +[Sources.common] + SpiNorFlashDxe.c + SpiNorFlashDxe.h + +[Packages] + ArmPkg/ArmPkg.dec + MdePkg/MdePkg.dec + Silicon/Phytium/PhytiumCommonPkg/PhytiumCommonPkg.dec + +[LibraryClasses] + BaseLib + DebugLib + IoLib + UefiLib + UefiBootServicesTableLib + UefiRuntimeLib + UefiDriverEntryPoint + UefiRuntimeServicesTableLib + +[FixedPcd] + gPhytiumPlatformTokenSpaceGuid.PcdSpiFlashBase + gPhytiumPlatformTokenSpaceGuid.PcdSpiFlashSize + gPhytiumPlatformTokenSpaceGuid.PcdSpiControllerBase + +[Guids] + gEfiEventVirtualAddressChangeGuid + +[Protocols] + gSpiMasterProtocolGuid + gSpiNorFlashProtocolGuid + + [Depex] + TRUE diff --git a/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiNorFlashDxe/SpiNorFlash= Dxe.h b/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiNorFlashDxe/SpiNorFlashDxe.h new file mode 100755 index 000000000000..4bb2aad4d3c4 --- /dev/null +++ b/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiNorFlashDxe/SpiNorFlashDxe.h @@ -0,0 +1,102 @@ +/** @file + The Header file of Phytium NorFlash Drivers. + + Copyright (C) 2023 Phytium Technology Co., Ltd. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef SPI_NORFLASH_DXE_H_ +#define SPI_NORFLASH_DXE_H_ + +#include +#include +#include +#include +#include +#include + +#include +#include + +// +// Norflash registers +// +#define REG_FLASH_CAP 0x000 +#define REG_RD_CFG 0x004 +#define REG_WR_CFG 0x008 +#define REG_FLUSH_REG 0x00C +#define REG_CMD_PORT 0x010 +#define REG_ADDR_PORT 0x014 +#define REG_HD_PORT 0x018 +#define REG_LD_PORT 0x01C +#define REG_CS_CFG 0x020 +#define REG_WIP_CFG 0x024 +#define REG_WP_REG 0x028 + +#define NORFLASH_SIGNATURE SIGNATURE_32 ('F', 'T', 'S', 'F') +#define SPI_FLASH_BASE FixedPcdGet64 (PcdSpiFlashBase) +#define SPI_FLASH_SIZE FixedPcdGet64 (PcdSpiFlashSize) + +extern EFI_GUID gSpiMasterProtocolGuid; +extern EFI_GUID gSpiNorFlashProtocolGuid; + +// +// Platform Nor Flash Functions +// +EFI_STATUS +EFIAPI +NorFlashPlatformEraseSingleBlock ( + IN UINTN BlockAddress + ); + +EFI_STATUS +EFIAPI +NorFlashPlatformErase ( + IN UINT64 Offset, + IN UINT64 Length + ); + +EFI_STATUS +EFIAPI +NorFlashPlatformRead ( + IN UINTN Address, + IN VOID *Buffer, + OUT UINT32 Length + ); + +EFI_STATUS +EFIAPI +NorFlashPlatformWrite ( + IN UINTN Address, + IN VOID *Buffer, + IN UINT32 Length + ); + +EFI_STATUS +EFIAPI +NorFlashPlatformGetDevices ( + OUT NOR_FLASH_DEVICE_DESCRIPTION *NorFlashDevices + ); + +EFI_STATUS +EFIAPI +NorFlashPlatformInitialization ( + VOID + ); + +EFI_STATUS +EFIAPI +NorFlashPlatformEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ); + +typedef struct { + EFI_NORFLASH_DRV_PROTOCOL FlashProtocol; + UINTN Signature; + EFI_HANDLE Handle; +} NorFlash_Device; + +#endif // SPI_NORFLASH_DXE_H_ diff --git a/Silicon/Phytium/PhytiumCommonPkg/Include/Protocol/SpiNorFlashP= rotocol.h b/Silicon/Phytium/PhytiumCommonPkg/Include/Protocol/SpiNorFlashPr= otocol.h new file mode 100755 index 000000000000..4436af4c6c34 --- /dev/null +++ b/Silicon/Phytium/PhytiumCommonPkg/Include/Protocol/SpiNorFlashProtocol= .h @@ -0,0 +1,74 @@ +/** @file + The Header of Protocol For NorFlash. + + Copyright (C) 2023, Phytium Technology Co., Ltd. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef SPI_NORFLASH_PROTOCOL_H_ +#define SPI_NORFLASH_PROTOCOL_H_ + +typedef struct _EFI_NORFLASH_DRV_PROTOCOL EFI_NORFLASH_DRV_PROTOCOL; +extern EFI_GUID gSpiNorFlashProtocolGuid; + +typedef struct { + UINTN DeviceBaseAddress; // Start address of the Device Base Ad= dress (DBA) + UINTN RegionBaseAddress; // Start address of one single region + UINTN Size; + UINTN BlockSize; + EFI_GUID Guid; +} NOR_FLASH_DEVICE_DESCRIPTION; + +typedef +EFI_STATUS +(EFIAPI *NORFLASH_PLATFORM_ERASE_INTERFACE) ( + IN UINT64 Offset, + IN UINT64 Length + ); + +typedef +EFI_STATUS +(EFIAPI *NORFLASH_PLATFORM_ERASESIGLEBLOCK_INTERFACE) ( + IN UINTN BlockAddress + ); + +typedef +EFI_STATUS +(EFIAPI *NORFLASH_PLATFORM_READ_INTERFACE) ( + IN UINTN Address, + IN VOID *Buffer, + OUT UINT32 Length + ); + +typedef +EFI_STATUS +(EFIAPI *NORFLASH_PLATFORM_WRITE_INTERFACE) ( + IN UINTN Address, + IN VOID *Buffer, + IN UINT32 Length + ); + +typedef +EFI_STATUS +(EFIAPI *NORFLASH_PLATFORM_GETDEVICE_INTERFACE) ( + OUT NOR_FLASH_DEVICE_DESCRIPTION *NorFlashDevices + ); + +typedef +EFI_STATUS +(EFIAPI *NORFLASH_PLATFORM_INIT_INTERFACE) ( + VOID + ); + +struct _EFI_NORFLASH_DRV_PROTOCOL{ + NORFLASH_PLATFORM_INIT_INTERFACE Initialization; + NORFLASH_PLATFORM_GETDEVICE_INTERFACE GetDevices; + NORFLASH_PLATFORM_ERASE_INTERFACE Erase; + NORFLASH_PLATFORM_ERASESIGLEBLOCK_INTERFACE EraseSingleBlock; + NORFLASH_PLATFORM_READ_INTERFACE Read; + NORFLASH_PLATFORM_WRITE_INTERFACE Write; +}; + +#endif // SPI_NORFLASH_PROTOCOL_H_ diff --git a/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiNorFlashDxe/SpiNorFlash= Dxe.c b/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiNorFlashDxe/SpiNorFlashDxe.c new file mode 100755 index 000000000000..47e62c582635 --- /dev/null +++ b/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiNorFlashDxe/SpiNorFlashDxe.c @@ -0,0 +1,409 @@ +/** @file + Phytium NorFlash Drivers. + + Copyright (C) 2023, Phytium Technology Co., Ltd. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "SpiNorFlashDxe.h" + +STATIC EFI_EVENT mSpiNorFlashVirtualAddrChangeEvent; +STATIC UINT8 mCmdWrite; +STATIC UINT8 mCmdErase; +STATIC UINT8 mCmdPp; + +EFI_SPI_DRV_PROTOCOL *mSpiMasterProtocol; +NorFlash_Device *mFlashInstance; + +NOR_FLASH_DEVICE_DESCRIPTION mNorFlashDevices =3D { + SPI_FLASH_BASE, /* Device Base Address */ + SPI_FLASH_BASE, /* Region Base Address */ + SPI_FLASH_SIZE, /* Size */ + SIZE_64KB, /* Block Size */ + {0xE7223039, 0x5836, 0x41E1, { 0xB5, 0x42, 0xD7, 0xEC, 0x73, 0x6C, 0x5= E, 0x59 } } +}; + + +/** + This function writed up to 256 bytes to flash through spi driver. + + @param[in] Address The address of the flash. + @param[in] Buffer The pointer of buffer to be writed. + @param[in] BufferSizeInBytes The bytes to be writed. + + @retval EFI_SUCCESS Write 256 bytes to flash successfully. + @retval EFI_INVALID_PARAMETER Input parameters error. + +**/ +STATIC +EFI_STATUS +NorFlashWrite256 ( + IN UINTN Address, + IN VOID *Buffer, + IN UINT32 BufferSizeInBytes + ) +{ + UINT32 Index; + UINT32 *TempBuffer; + UINT8 WriteSize; + + TempBuffer =3D Buffer; + WriteSize =3D sizeof (UINT32); + + if (BufferSizeInBytes > 256) { + DEBUG ((DEBUG_ERROR, "The max length is 256 bytes.\n")); + return EFI_INVALID_PARAMETER; + } + + if ((BufferSizeInBytes % WriteSize) !=3D 0) { + DEBUG ((DEBUG_ERROR, "The length must four bytes aligned.\n")); + return EFI_INVALID_PARAMETER; + } + + if ((Address % WriteSize) !=3D 0) { + DEBUG ((DEBUG_ERROR, "The address must four bytes aligned.\n")); + return EFI_INVALID_PARAMETER; + } + + mSpiMasterProtocol->SpiSetConfig (mCmdPp, 0x400000, REG_CMD_PORT); + mSpiMasterProtocol->SpiSetConfig (0, 0x1, REG_LD_PORT); + + mSpiMasterProtocol->SpiSetConfig (mCmdWrite, 0x000208, REG_WR_CFG); + + for (Index =3D 0; Index < (BufferSizeInBytes / WriteSize); Index++) { + MmioWrite32 ((Address + (Index * WriteSize)), TempBuffer[Index]); + } + + mSpiMasterProtocol->SpiSetConfig (0, 0x1, REG_FLUSH_REG); + + mSpiMasterProtocol->SpiSetConfig (0, 0x0, REG_WR_CFG); + + return EFI_SUCCESS; +} + +/** + This function erased a sector of flash through spi driver. + + @param[in] BlockAddress The sector address to be erased. + + @retval None. + +**/ +STATIC +VOID +NorFlashPlatformEraseSector ( + IN UINTN BlockAddress + ) +{ + mSpiMasterProtocol->SpiSetConfig (mCmdPp, 0x400000, REG_CMD_PORT); + mSpiMasterProtocol->SpiSetConfig (0, 0x1, REG_LD_PORT); + + mSpiMasterProtocol->SpiSetConfig (mCmdErase, 0x408000, REG_CMD_PORT); + mSpiMasterProtocol->SpiSetConfig (0, BlockAddress, REG_ADDR_PORT); + mSpiMasterProtocol->SpiSetConfig (0, 0x1, REG_LD_PORT); + +} + + +/** + Fixup internal data so that EFI can be call in virtual mode. + Call the passed in Child Notify event and convert any pointers in + lib to virtual mode. + + @param[in] Event The Event that is being processed. + + @param[in] Context Event Context. + + @retval None. + +**/ +VOID +EFIAPI +PlatformNorFlashVirtualNotifyEvent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EfiConvertPointer (0x0, (VOID **)&(mSpiMasterProtocol->SpiGetConfig)); + EfiConvertPointer (0x0, (VOID **)&(mSpiMasterProtocol->SpiSetConfig)); + EfiConvertPointer (0x0, (VOID **)&(mSpiMasterProtocol)); + EfiConvertPointer (0x0, (VOID **)&(mFlashInstance->FlashProtocol.Erase)); + EfiConvertPointer (0x0, (VOID **)&(mFlashInstance->FlashProtocol.Read)); + EfiConvertPointer (0x0, (VOID **)&(mFlashInstance->FlashProtocol.Write)); + EfiConvertPointer (0x0, (VOID **)&(mFlashInstance->FlashProtocol.EraseSi= ngleBlock)); + EfiConvertPointer (0x0, (VOID **)&(mFlashInstance->FlashProtocol)); + EfiConvertPointer (0x0, (VOID **)&(mFlashInstance)); + EfiConvertPointer (0x0, (VOID **)&(mNorFlashDevices)); + + return; +} + + +/** + This function inited the flash platform. + + @param None. + + @retval EFI_SUCCESS Initialize platform successfully. + +**/ +EFI_STATUS +EFIAPI +NorFlashPlatformInitialization ( + VOID + ) +{ + + mCmdWrite =3D 0x2; + mCmdErase =3D 0xD8; + mCmdPp =3D 0x6; + + mSpiMasterProtocol->SpiInit (); + + return EFI_SUCCESS; +} + + +/** + This function geted the flash device information. + + @param[out] NorFlashDevices The pointer to store flash device informa= tion. + @param[out] Count The number of the flash device. + + @retval EFI_SUCCESS Get devices successfully. + +**/ +EFI_STATUS +EFIAPI +NorFlashPlatformGetDevices ( + OUT NOR_FLASH_DEVICE_DESCRIPTION *NorFlashDevices + ) +{ + *NorFlashDevices =3D mNorFlashDevices; + + return EFI_SUCCESS; +} + + +/** + This function readed flash content form the specified area of flash. + + @param[in] Address The address of the flash. + @param[in] Buffer The pointer to the Buffer to be stored. + @param[out] Length The bytes readed form flash. + + @retval EFI_SUCCESS Read data from flash successfully. + +**/ +EFI_STATUS +EFIAPI +NorFlashPlatformRead ( + IN UINTN Address, + IN VOID *Buffer, + OUT UINT32 Length + ) +{ + + CopyMem ((VOID *)Buffer, (VOID *)Address, Length); + + return EFI_SUCCESS; +} + + +/** + This function erased one block flash content. + + @param[in] BlockAddress The BlockAddress to be erased. + + @retval EFI_SUCCESS Erase single block successfully. + +**/ +EFI_STATUS +EFIAPI +NorFlashPlatformEraseSingleBlock ( + IN UINTN BlockAddress + ) +{ + + NorFlashPlatformEraseSector (BlockAddress); + + return EFI_SUCCESS; +} + + +/** + This function erased the flash content of the specified area. + + @param[in] Offset Te offset of the flash. + @param[in] Length Length to be erased. + + @retval EFI_SUCCESS Erase operation successfully. + +**/ +EFI_STATUS +EFIAPI +NorFlashPlatformErase ( + IN UINT64 Offset, + IN UINT64 Length + ) +{ + EFI_STATUS Status; + UINT64 Index; + UINT64 Count; + + Status =3D EFI_SUCCESS; + + if ((Length % SIZE_64KB) =3D=3D 0) { + Count =3D Length / SIZE_64KB; + for (Index =3D 0; Index < Count; Index++) { + NorFlashPlatformEraseSingleBlock (Offset); + Offset +=3D SIZE_64KB; + } + } else { + Status =3D EFI_INVALID_PARAMETER; + } + + return Status; +} + + +/** + This function writed data to flash. + + @param[in] Address The address of the flash. + + @param[in] Buffer The pointer to the Buffer to be writed. + + @param[in] BufferSizeInBytes The size of the Buffer. + + @retval EFI_SUCCESS Write flash successfully. + +**/ +EFI_STATUS +EFIAPI +NorFlashPlatformWrite ( + IN UINTN Address, + IN VOID *Buffer, + IN UINT32 BufferSizeInBytes + ) +{ + UINT32 Index; + UINT32 Remainder; + UINT32 Quotient; + EFI_STATUS Status; + UINTN TmpAddress; + + TmpAddress =3D Address; + Remainder =3D BufferSizeInBytes % 256; + Quotient =3D BufferSizeInBytes / 256; + + if (BufferSizeInBytes <=3D 256) { + Status =3D NorFlashWrite256 (TmpAddress, Buffer, BufferSizeInBytes); + } else { + for (Index =3D 0; Index < Quotient; Index++) { + Status =3D NorFlashWrite256 (TmpAddress, Buffer, 256); + TmpAddress +=3D 256; + Buffer +=3D 256; + } + + if (Remainder !=3D 0) { + Status =3D NorFlashWrite256 (TmpAddress, Buffer, Remainder); + } + } + + if (EFI_ERROR (Status)) { + ASSERT_EFI_ERROR (Status); + } + + return EFI_SUCCESS; + +} + + +/** + This function initialized the flash driver protocol. + + @param[in] NorFlashProtocol A pointer to the norflash protocol struct. + + @retval EFI_SUCCESS Initialized protocol successfully. + +**/ +EFI_STATUS +EFIAPI +NorFlashPlatformInitProtocol ( + IN EFI_NORFLASH_DRV_PROTOCOL *NorFlashProtocol + ) +{ + NorFlashProtocol->Initialization =3D NorFlashPlatformInitialization; + NorFlashProtocol->GetDevices =3D NorFlashPlatformGetDevices; + NorFlashProtocol->Erase =3D NorFlashPlatformErase; + NorFlashProtocol->EraseSingleBlock =3D NorFlashPlatformEraseSingleBlock; + NorFlashProtocol->Read =3D NorFlashPlatformRead; + NorFlashProtocol->Write =3D NorFlashPlatformWrite; + + return EFI_SUCCESS; +} + + +/** + This function is the entrypoint of the norflash driver. + + @param[in] ImageHandle The firmware allocated handle for the EFI imag= e. + + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The entry point is executed successfully. + + @retval other Some error occurs when executing this entry po= int. + +**/ +EFI_STATUS +EFIAPI +NorFlashPlatformEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + Status =3D gBS->LocateProtocol ( + &gSpiMasterProtocolGuid, + NULL, + (VOID **)&mSpiMasterProtocol + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + mFlashInstance =3D AllocateRuntimeZeroPool (sizeof (NorFlash_Device)); + if (mFlashInstance =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + NorFlashPlatformInitProtocol (&mFlashInstance->FlashProtocol); + + mFlashInstance->Signature =3D NORFLASH_SIGNATURE; + + Status =3D gBS->InstallMultipleProtocolInterfaces ( + &(mFlashInstance->Handle), + &gSpiNorFlashProtocolGuid, + &(mFlashInstance->FlashProtocol), + NULL + ); + ASSERT_EFI_ERROR (Status); + + //Register for the virtual address change event + Status =3D gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + PlatformNorFlashVirtualNotifyEvent, + NULL, + &gEfiEventVirtualAddressChangeGuid, + &mSpiNorFlashVirtualAddrChangeEvent + ); + ASSERT_EFI_ERROR (Status); + + return Status; +} --=20 2.25.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#99286): https://edk2.groups.io/g/devel/message/99286 Mute This Topic: https://groups.io/mt/96623619/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Wed May 15 07:46:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+99290+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+99290+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1675064265; cv=none; d=zohomail.com; s=zohoarc; b=WLDRyNN60hv4DjCVfEVID2rEgyGM7MOZXSmm/Zx9sYpyRtWsVsnutX0a6V3ZXIgTzIRUF9b/iFpWZMEOAXMAlLOv0QgJHLZUTT2HvcYqjV+l1DG815z9oa+JpEdztiFW1MCzr6XVpZQ9GDFxkweVS9Dav12wND6mnUprsy8f9I4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1675064265; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=jnkAFhNyI+vZqr16VtLKlsHIXvTE37XQDqIjqENnwGU=; b=Wbcn1lMQe2T7cjnQkhdTHJqIB9zGGINjr8H3hauQCkc9lHyjE+wQ/bWSKxebFVhOMhNZriElm28DafVSWvjXdUpIvCSYbpVagrwRjYFXPc1x5fZRDmV4tpolr1YsKYqer/PO/Dq8Xal0RqxeO3eQzP2Xfgj+fDALmG6u9uY6alQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+99290+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1675064265560748.2568177814777; Sun, 29 Jan 2023 23:37:45 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id LnuhYY1788612xaWWlFRLwKP; Sun, 29 Jan 2023 23:37:45 -0800 X-Received: from azure-sdnproxy.icoremail.net (azure-sdnproxy.icoremail.net [20.231.56.155]) by mx.groups.io with SMTP id smtpd.web10.35959.1675064261168075441 for ; Sun, 29 Jan 2023 23:37:42 -0800 X-Received: from prodtpl.icoremail.net (unknown [10.12.1.20]) by hzbj-icmmx-6 (Coremail) with SMTP id AQAAfwDXeu4PdNdjGr2SBA--.61937S2; Mon, 30 Jan 2023 15:38:55 +0800 (CST) X-Received: from localhost.localdomain (unknown [117.136.89.78]) by mail (Coremail) with SMTP id AQAAfwBnZB26c9djDDUAAA--.158S5; Mon, 30 Jan 2023 15:37:37 +0800 (CST) From: "Ling Jia" To: devel@edk2.groups.io Cc: Leif Lindholm , Liming Gao , Michael D Kinney , Yiqi Shu , Peng Xie , Ling Jia Subject: [edk2-devel] [PATCH v8 3/4] Silicon/Phytium: Added fvb driver for flash Date: Mon, 30 Jan 2023 15:34:19 +0800 Message-Id: <20230130073420.135519-4-jialing@phytium.com.cn> In-Reply-To: <20230130073420.135519-1-jialing@phytium.com.cn> References: <20230130073420.135519-1-jialing@phytium.com.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAfwBnZB26c9djDDUAAA--.158S5 X-CM-SenderInfo: xmldzxdqj61x51wl3zoofrzhdfq/ X-Coremail-Antispam: 1Uk129KBjvAXoWDWF48CF4ftFW8Aw45Ww4UArb_yoW7Jw4UGo WxZr4fAw18trWSqrs8Jryjk3yxtFnaqFsxtrs5Zry2qan5Jw1a9FW0y3WDXws3tw1jkrnx KryfX3s5KFW3trykn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7ZEXasCq-sGcSsGvf J3UbIjqfuFe4nvWSU8nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2KfnxnUUI43ZEXa7xR_UU UUUUUUU== Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,jialing@phytium.com.cn X-Gm-Message-State: RKxqK6dyxqQUgF6w2PkRd0O6x1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1675064265; bh=arkRwJ0MADz372htWeRg1rJEf4naU6bEMwrl7YnRqUU=; h=Cc:Date:From:Reply-To:Subject:To; b=aEehXVwgJ6tbY8Ez0/TloMgGCkCWhIDmWCcWEU6oFCCVIOjFmZLQls+v9yFAJ5E9B2Y 5noSxatTTNcDHLtDRys6ieVQ9y4blduEXurULkYl73ViNm5xv42mAZXh4QfC6URCL+giM 2mIyZO+QTUa/KinScRK6lB35Vcv4l9wfGX4= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1675064266509100013 Content-Type: text/plain; charset="utf-8" From: Ling Jia The FlashFvbDxe provided the fvb protocol, which requested by the flash operators. Signed-off-by: Ling Jia --- Silicon/Phytium/PhytiumCommonPkg/PhytiumCommonPkg.dsc.inc | = 3 +- Platform/Phytium/DurianPkg/DurianPkg.dsc | = 1 + Platform/Phytium/DurianPkg/DurianPkg.fdf | = 2 + Silicon/Phytium/PhytiumCommonPkg/Drivers/FlashFvbDxe/FlashFvbDxe.inf | 6= 2 + Silicon/Phytium/PhytiumCommonPkg/Drivers/FlashFvbDxe/FlashFvbDxe.h | 11= 7 ++ Silicon/Phytium/PhytiumCommonPkg/Drivers/FlashFvbDxe/FlashFvbDxe.c | 145= 0 ++++++++++++++++++++ 6 files changed, 1634 insertions(+), 1 deletion(-) diff --git a/Silicon/Phytium/PhytiumCommonPkg/PhytiumCommonPkg.dsc.inc b/Si= licon/Phytium/PhytiumCommonPkg/PhytiumCommonPkg.dsc.inc index 7166212875ec..5dd5ce0390f3 100644 --- a/Silicon/Phytium/PhytiumCommonPkg/PhytiumCommonPkg.dsc.inc +++ b/Silicon/Phytium/PhytiumCommonPkg/PhytiumCommonPkg.dsc.inc @@ -1,7 +1,7 @@ ## @file # This package provides common open source Phytium silicon modules. # -# Copyright (C) 2020, Phytium Technology Co, Ltd. All rights reserved. +# Copyright (C) 2020-2023, Phytium Technology Co., Ltd. All rights reserve= d. # # SPDX-License-Identifier:BSD-2-Clause-Patent # @@ -23,6 +23,7 @@ [LibraryClasses.common] =20 AcpiLib|EmbeddedPkg/Library/AcpiLib/AcpiLib.inf AuthVariableLib|MdeModulePkg/Library/AuthVariableLibNull/AuthVariableLib= Null.inf + PlatformPKProtectionLib|SecurityPkg/Library/PlatformPKProtectionLibVarPo= licy/PlatformPKProtectionLibVarPolicy.inf =20 BaseLib|MdePkg/Library/BaseLib/BaseLib.inf BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf diff --git a/Platform/Phytium/DurianPkg/DurianPkg.dsc b/Platform/Phytium/Du= rianPkg/DurianPkg.dsc index 7a390fbe1383..b541e407ac8b 100644 --- a/Platform/Phytium/DurianPkg/DurianPkg.dsc +++ b/Platform/Phytium/DurianPkg/DurianPkg.dsc @@ -267,6 +267,7 @@ [Components.common] # NOR Flash driver # Silicon/Phytium/FT2000-4Pkg/Drivers/SpiNorFlashDxe/SpiNorFlashDxe.inf + Silicon/Phytium/PhytiumCommonPkg/Drivers/FlashFvbDxe/FlashFvbDxe.inf =20 # # Usb Support diff --git a/Platform/Phytium/DurianPkg/DurianPkg.fdf b/Platform/Phytium/Du= rianPkg/DurianPkg.fdf index 560be98199d2..70b7e99270c2 100644 --- a/Platform/Phytium/DurianPkg/DurianPkg.fdf +++ b/Platform/Phytium/DurianPkg/DurianPkg.fdf @@ -88,6 +88,7 @@ [FV.FvMain] INF MdeModulePkg/Core/RuntimeDxe/RuntimeDxe.inf INF Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.inf INF Silicon/Phytium/FT2000-4Pkg/Drivers/SpiNorFlashDxe/SpiNorFlashDxe.= inf + INF Silicon/Phytium/PhytiumCommonPkg/Drivers/FlashFvbDxe/FlashFvbDxe.i= nf } =20 INF MdeModulePkg/Core/Dxe/DxeMain.inf @@ -104,6 +105,7 @@ [FV.FvMain] =20 INF Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.inf INF Silicon/Phytium/FT2000-4Pkg/Drivers/SpiNorFlashDxe/SpiNorFlashDxe.inf + INF Silicon/Phytium/PhytiumCommonPkg/Drivers/FlashFvbDxe/FlashFvbDxe.inf =20 INF MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleRuntimeDxe.inf INF MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCounterRu= ntimeDxe.inf diff --git a/Silicon/Phytium/PhytiumCommonPkg/Drivers/FlashFvbDxe/FlashFvbD= xe.inf b/Silicon/Phytium/PhytiumCommonPkg/Drivers/FlashFvbDxe/FlashFvbDxe.i= nf new file mode 100755 index 000000000000..96df8de51115 --- /dev/null +++ b/Silicon/Phytium/PhytiumCommonPkg/Drivers/FlashFvbDxe/FlashFvbDxe.inf @@ -0,0 +1,62 @@ +## @file +# Phytium NorFlash Fvb Drivers. +# +# Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.
+# Copyright (C) 2023, Phytium Technology Co., Ltd. All rights reserved. +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x0001001b + BASE_NAME =3D FlashFvbDxe + FILE_GUID =3D b8923820-3e7c-11eb-b12c-17525e90ecc8 + MODULE_TYPE =3D DXE_RUNTIME_DRIVER + VERSION_STRING =3D 0.1 + ENTRY_POINT =3D FvbEntryPoint + +[Sources] + FlashFvbDxe.c + FlashFvbDxe.h + +[Packages] + EmbeddedPkg/EmbeddedPkg.dec + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + Silicon/Phytium/PhytiumCommonPkg/PhytiumCommonPkg.dec + +[LibraryClasses] + BaseLib + DebugLib + DxeServicesTableLib + HobLib + MemoryAllocationLib + UefiBootServicesTableLib + UefiRuntimeLib + UefiDriverEntryPoint + UefiRuntimeServicesTableLib + +[Guids] + gEfiAuthenticatedVariableGuid + gEfiEventVirtualAddressChangeGuid + gEfiSystemNvDataFvGuid + gEfiVariableGuid + +[Protocols] + gEfiDevicePathProtocolGuid + gEfiFirmwareVolumeBlockProtocolGuid + gSpiNorFlashProtocolGuid + +[FixedPcd] + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize + +[Pcd] + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64 + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64 + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64 + +[Depex] + TRUE diff --git a/Silicon/Phytium/PhytiumCommonPkg/Drivers/FlashFvbDxe/FlashFvbD= xe.h b/Silicon/Phytium/PhytiumCommonPkg/Drivers/FlashFvbDxe/FlashFvbDxe.h new file mode 100755 index 000000000000..216e7bf69944 --- /dev/null +++ b/Silicon/Phytium/PhytiumCommonPkg/Drivers/FlashFvbDxe/FlashFvbDxe.h @@ -0,0 +1,117 @@ +/** @file + Phytium NorFlash Fvb Drivers Header. + + Copyright (C) 2023, Phytium Technology Co., Ltd. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#ifndef FLASH_FVB_DXE_H_ +#define FLASH_FVB_DXE_H_ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#define GET_DATA_OFFSET(BaseAddr, Lba, LbaSize) ((BaseAddr) + (UINTN)((Lba= ) * (LbaSize))) +#define FVB_FLASH_SIGNATURE SIGNATURE_32('S', 'N', '= O', 'R') +#define INSTANCE_FROM_FVB_THIS(a) CR(a, FT_FVB_DEVICE, Fvb= Protocol, FVB_FLASH_SIGNATURE) + +typedef struct _FT_FVB_DEVICE FT_FVB_DEVICE; + +#define NOR_FLASH_ERASE_RETRY 10 + +typedef struct { + VENDOR_DEVICE_PATH Vendor; + EFI_DEVICE_PATH_PROTOCOL End; + } FT_FVB_DEVICE_PATH; + +struct _FT_FVB_DEVICE { + UINT32 Signature; + EFI_HANDLE Handle; + + UINTN DeviceBaseAddress; + UINTN RegionBaseAddress; + UINTN Size; + EFI_LBA StartLba; + EFI_BLOCK_IO_MEDIA Media; + + EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL FvbProtocol; + + FT_FVB_DEVICE_PATH DevicePath; + EFI_NORFLASH_DRV_PROTOCOL *SpiFlashProtocol; + UINTN FvbSize; + VOID* TempBuffer; + }; + +EFI_STATUS +EFIAPI +FvbGetAttributes ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL * This, + OUT EFI_FVB_ATTRIBUTES_2 * Attributes + ); + +EFI_STATUS +EFIAPI +FvbSetAttributes ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL * This, + IN OUT EFI_FVB_ATTRIBUTES_2 * Attributes + ); + +EFI_STATUS +EFIAPI +FvbGetPhysicalAddress ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL * This, + OUT EFI_PHYSICAL_ADDRESS * Address + ); + +EFI_STATUS +EFIAPI +FvbGetBlockSize ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL * This, + IN EFI_LBA Lba, + OUT UINTN * BlockSize, + OUT UINTN * NumberOfBlocks + ); + +EFI_STATUS +EFIAPI +FvbRead ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL * This, + IN EFI_LBA Lba, + IN UINTN Offset, + IN OUT UINTN * NumBytes, + IN OUT UINT8 * Buffer + ); + +EFI_STATUS +EFIAPI +FvbWrite ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL * This, + IN EFI_LBA Lba, + IN UINTN Offset, + IN OUT UINTN * NumBytes, + IN UINT8 * Buffer + ); + +EFI_STATUS +EFIAPI +FvbEraseBlocks ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL * This, + ... + ); + +#endif // FLASH_FVB_DXE_H_ diff --git a/Silicon/Phytium/PhytiumCommonPkg/Drivers/FlashFvbDxe/FlashFvbD= xe.c b/Silicon/Phytium/PhytiumCommonPkg/Drivers/FlashFvbDxe/FlashFvbDxe.c new file mode 100755 index 000000000000..0c6efa5d8368 --- /dev/null +++ b/Silicon/Phytium/PhytiumCommonPkg/Drivers/FlashFvbDxe/FlashFvbDxe.c @@ -0,0 +1,1450 @@ +/** @file + Phytium NorFlash Fvb Drivers. + + Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.
+ Copyright (C) 2023, Phytium Technology Co., Ltd. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "FlashFvbDxe.h" + +STATIC EFI_EVENT mFvbVirtualAddrChangeEvent; +STATIC FT_FVB_DEVICE *mFvbDevice; +UINTN mFlashNvStorageVariableBase; +UINTN mFlashNvStorageFtwWorkingBase; +UINTN mFlashNvStorageFtwSpareBase; +UINT32 mFlashNvStorageVariableSize; +UINT32 mFlashNvStorageFtwWorkingSize; +UINT32 mFlashNvStorageFtwSpareSize; + +STATIC CONST FT_FVB_DEVICE mFvbFlashInstanceTemplate =3D { + FVB_FLASH_SIGNATURE, // Signature + NULL, // Handle ... NEED TO BE FILLED + 0, // DeviceBaseAddress ... NEED TO BE FILLED + 0, // RegionBaseAddress ... NEED TO BE FILLED + 0, // Size ... NEED TO BE FILLED + 0, // StartLba + { + 0, // MediaId ... NEED TO BE FILLED + FALSE, // RemovableMedia + TRUE, // MediaPresent + FALSE, // LogicalPartition + FALSE, // ReadOnly + FALSE, // WriteCaching; + 0, // BlockSize ... NEED TO BE FILLED + 4, // IoAlign + 0, // LastBlock ... NEED TO BE FILLED + 0, // LowestAlignedLba + 1, // LogicalBlocksPerPhysicalBlock + }, //Media; + { + FvbGetAttributes, // GetAttributes + FvbSetAttributes, // SetAttributes + FvbGetPhysicalAddress, // GetPhysicalAddress + FvbGetBlockSize, // GetBlockSize + FvbRead, // Read + FvbWrite, // Write + FvbEraseBlocks, // EraseBlocks + NULL, // ParentHandle + }, // FvbProtoccol; + + { + { + { + HARDWARE_DEVICE_PATH, + HW_VENDOR_DP, + { + (UINT8) sizeof (VENDOR_DEVICE_PATH), + (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8) + } + }, + { + 0x0, 0x0, 0x0, { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 } + }, // GUID ... NEED TO BE FILLED + }, + { + END_DEVICE_PATH_TYPE, + END_ENTIRE_DEVICE_PATH_SUBTYPE, + { + sizeof (EFI_DEVICE_PATH_PROTOCOL), + 0 + } + } + }, // DevicePath + + NULL, // SpiFlashProtocol ... NEED TO BE FILLED + 0, // Fvb Size ... NEED TO BE FILLED + NULL // TempBuffer ... NEED TO BE FILLED +}; + + +/** + Erased a single block of flash. + + @param[in] FlashInstance The poiter of the fvb device sturct. + + @param[in] BlockAddress Physical address of Lba to be erased. + + @retval EFI_SUCCESS The erase single block request successfull= y completed. + +**/ +STATIC +EFI_STATUS +FvbFlashEraseSingleBlock ( + IN FT_FVB_DEVICE *FlashInstance, + IN UINTN BlockAddress + ) +{ + EFI_STATUS Status; + UINTN Index; + EFI_TPL OriginalTPL; + + if ( !EfiAtRuntime ()) { + OriginalTPL =3D gBS->RaiseTPL (TPL_HIGH_LEVEL); + } else { + OriginalTPL =3D TPL_HIGH_LEVEL; + } + + Index =3D 0; + + do { + Status =3D FlashInstance->SpiFlashProtocol->EraseSingleBlock ( BlockAd= dress); + Index++; + } while ((Index < NOR_FLASH_ERASE_RETRY) && (Status =3D=3D EFI_WRITE_PRO= TECTED)); + + if (Index =3D=3D NOR_FLASH_ERASE_RETRY) { + DEBUG (( + DEBUG_ERROR, + "EraseSingleBlock(BlockAddress=3D0x%08x: BlockLocked Error (try to e= rase % d times)\n", + BlockAddress, + Index + )); + } + + if ( !EfiAtRuntime ()) { + gBS->RestoreTPL (OriginalTPL); + } + + return Status; +} + + +/** + Readed the specified number of bytes from the form the block to output b= uffer. + + @param[in] FlashInstance The pointer of FT_FVB_DEVICE instance. + + @param[in] Lba The starting logical block index to wri= te to. + + @param[in] Offset Offset into the block at which to begin= writing. + + @param[in] BufferSizeInBytes The number of bytes to be writed. + + @param[out] Buffer The pointer to a caller-allocated buffe= r that + contains the source for the write. + + @retval EFI_SUCCESS FvbFlashRead() is executed successfully. + +**/ +STATIC +EFI_STATUS +FvbFlashRead ( + IN FT_FVB_DEVICE *FlashInstance, + IN EFI_LBA Lba, + IN UINTN Offset, + IN UINTN BufferSizeInBytes, + OUT VOID *Buffer + ) +{ + UINTN Address; + + // The buffer must be valid + if (Buffer =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + if (BufferSizeInBytes =3D=3D 0) { + return EFI_SUCCESS; + } + + if (((Lba * FlashInstance->Media.BlockSize) + Offset + BufferSizeInBytes= ) > FlashInstance->Size) { + DEBUG ((DEBUG_ERROR, "FvbFlashRead: ERROR - Read will exceed device si= ze.\n")); + return EFI_INVALID_PARAMETER; + } + + Address =3D GET_DATA_OFFSET ( + FlashInstance->RegionBaseAddress, + Lba, + FlashInstance->Media.BlockSize + ) + Offset; + + return FlashInstance->SpiFlashProtocol->Read (Address, Buffer, BufferSiz= eInBytes); +} + + +/** + Write a full or portion of a block. It must not span block boundaries; t= hat is, + Offset + BufferSizeInBytes <=3D FlashInstance->Media.BlockSize. + + @param[in] FlashInstance The pointer of FT_FVB_DEVICE instance. + + @param[in] Lba The starting logical block index to wri= te to. + + @param[in] Offset Offset into the block at which to begin= writing. + + @param[in] BufferSizeInBytes The number of bytes to be writed. + + @param[out] Buffer The pointer to a caller-allocated buffe= r that + contains the source for the write. + + @retval EFI_SUCCESS FvbWriteBlock() is executed successfull= y. + + @retval EFI_BAD_BUFFER_SIZE The write spaned block boundaries. + +**/ +STATIC +EFI_STATUS +FvbWriteBlock ( + IN FT_FVB_DEVICE *FlashInstance, + IN EFI_LBA Lba, + IN UINTN Offset, + IN UINTN BufferSizeInBytes, + IN UINT8 *Buffer + ) +{ + UINTN BlockAddress; + UINTN BlockSize; + UINTN BytesToWrite; + UINTN CurOffset; + BOOLEAN DoErase; + UINT32 Mask; + EFI_STATUS Status; + UINT32 Tmp; + UINT32 TmpBuf; + UINTN WordAddr; + UINT32 WordToWrite; + + // Detect WriteDisabled state + if (FlashInstance->Media.ReadOnly =3D=3D TRUE) { + DEBUG (( + DEBUG_ERROR, + "FvbWriteBlock: ERROR - Can not write:Device is in WriteDisabled sta= te.\n" + )); + // It is in WriteDisabled state, return an error right away + return EFI_ACCESS_DENIED; + } + + // Cache the block size to avoid de-referencing pointers all the time + BlockSize =3D FlashInstance->Media.BlockSize; + + // The write must not span block boundaries. + // We need to check each variable individually because adding two large = values together overflows. + if ((Offset >=3D BlockSize) || + (BufferSizeInBytes > BlockSize) || + ((Offset + BufferSizeInBytes) > BlockSize)) + { + DEBUG (( + DEBUG_ERROR, + "FvbWriteBlock: ERROR - EFI_BAD_BUFFER_SIZE: (Offset =3D0x %x + Buff= erSizeInBytes =3D0x%x) > BlockSize =3D0x%x\n", + Offset, + BufferSizeInBytes, + BlockSize + )); + return EFI_BAD_BUFFER_SIZE; + } + + // We must have some bytes to write + if (BufferSizeInBytes =3D=3D 0) { + DEBUG (( + DEBUG_ERROR, + "FvbWriteBlock: ERROR - EFI_BAD_BUFFER_SIZE: BufferSizeInBytes =3D= =3D 0\n" + )); + return EFI_BAD_BUFFER_SIZE; + } + // Pick 128bytes as a good start for word operations as opposed to erasi= ng the + // block and writing the data regardless if an erase is really needed. + // It looks like most individual NV variable writes are smaller than 128= bytes. + if (BufferSizeInBytes <=3D 128) { + // Check to see if we need to erase before programming the data into N= OR. + // If the destination bits are only changing from 1s to 0s we can just= write. + // After a block is erased all bits in the block is set to 1. + // If any byte requires us to erase we just give up and rewrite all of= it. + DoErase =3D FALSE; + BytesToWrite =3D BufferSizeInBytes; + CurOffset =3D Offset; + + while (BytesToWrite > 0) { + // Read full word from NOR, splice as required. A word is the smalle= st + // unit we can write. + Status =3D FvbFlashRead (FlashInstance, Lba, CurOffset & ~(0x3), siz= eof (Tmp), &Tmp); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + // Physical address of word in NOR to write. + WordAddr =3D (CurOffset & ~(0x3)) + GET_DATA_OFFSET ( + FlashInstance->RegionBaseAddress, + Lba, + BlockSize + ); + // The word of data that is to be written. + TmpBuf =3D *((UINT32 *)(Buffer + (BufferSizeInBytes - BytesToWrite))= ); + + // First do word aligned chunks. + if ((CurOffset & 0x3) =3D=3D 0) { + if (BytesToWrite >=3D 4) { + // Is the destination still in 'erased' state? + if (~Tmp !=3D 0) { + // Check to see if we are only changing bits to zero. + if ((Tmp ^ TmpBuf) & TmpBuf) { + DoErase =3D TRUE; + break; + } + } + + // Write this word to NOR + WordToWrite =3D TmpBuf; + CurOffset +=3D sizeof (TmpBuf); + BytesToWrite -=3D sizeof (TmpBuf); + } else { + // BytesToWrite < 4. Do small writes and left-overs + Mask =3D ~((~0) << (BytesToWrite * 8)); + // Mask out the bytes we want. + TmpBuf &=3D Mask; + // Is the destination still in 'erased' state? + if ((Tmp & Mask) !=3D Mask) { + // Check to see if we are only changing bits to zero. + if ((Tmp ^ TmpBuf) & TmpBuf) { + DoErase =3D TRUE; + break; + } + } + + // Merge old and new data. Write merged word to NOR + WordToWrite =3D (Tmp & ~Mask) | TmpBuf; + CurOffset +=3D BytesToWrite; + BytesToWrite =3D 0; + } + } else { + // Do multiple words, but starting unaligned. + if (BytesToWrite > (4 - (CurOffset & 0x3))) { + Mask =3D ((~0) << ((CurOffset & 0x3) * 8)); + // Mask out the bytes we want. + TmpBuf &=3D Mask; + // Is the destination still in 'erased' state? + if ((Tmp & Mask) !=3D Mask) { + // Check to see if we are only changing bits to zero. + if ((Tmp ^ TmpBuf) & TmpBuf) { + DoErase =3D TRUE; + break; + } + } + + // Merge old and new data. Write merged word to NOR + WordToWrite =3D (Tmp & ~Mask) | TmpBuf; + BytesToWrite -=3D (4 - (CurOffset & 0x3)); + CurOffset +=3D (4 - (CurOffset & 0x3)); + } else { + // Unaligned and fits in one word. + Mask =3D (~((~0) << (BytesToWrite * 8))) << ((CurOffset & 0x3) *= 8); + // Mask out the bytes we want. + TmpBuf =3D (TmpBuf << ((CurOffset & 0x3) * 8)) & Mask; + // Is the destination still in 'erased' state? + if ((Tmp & Mask) !=3D Mask) { + // Check to see if we are only changing bits to zero. + if ((Tmp ^ TmpBuf) & TmpBuf) { + DoErase =3D TRUE; + break; + } + } + + // Merge old and new data. Write merged word to NOR + WordToWrite =3D (Tmp & ~Mask) | TmpBuf; + CurOffset +=3D BytesToWrite; + BytesToWrite =3D 0; + } + } + + // + // Write the word to NOR. + // + Status =3D FlashInstance->SpiFlashProtocol->Write ( + WordAddr, + &WordToWrite, + 4 + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + } + + // Exit if we got here and could write all the data. Otherwise do the + // Erase-Write cycle. + if (!DoErase) { + return EFI_SUCCESS; + } + } + + if (FlashInstance->TempBuffer =3D=3D NULL) { + DEBUG ((DEBUG_ERROR, "FvbWriteBlock: ERROR - Buffer not ready\n")); + return EFI_DEVICE_ERROR; + } + // + // Write the word to NOR. + // + BlockAddress =3D GET_DATA_OFFSET ( + FlashInstance->RegionBaseAddress, + Lba, + FlashInstance->Media.BlockSize + ); + // Read NOR Flash data into shadow buffer + Status =3D FlashInstance->SpiFlashProtocol->Read ( + BlockAddress, + FlashInstance->TempBuffer, + BlockSize + ); + if (EFI_ERROR (Status)) { + // Return one of the pre-approved error statuses + return EFI_DEVICE_ERROR; + } + + // Put the data at the appropriate location inside the buffer area + CopyMem ( + (VOID *) ((UINTN) FlashInstance->TempBuffer + Offset), + Buffer, + BufferSizeInBytes + ); + Status =3D FlashInstance->SpiFlashProtocol->EraseSingleBlock (BlockAddre= ss); + + if (EFI_ERROR (Status)) { + // Return one of the pre-approved error statuses + return EFI_DEVICE_ERROR; + } + + // Write the modified buffer back to the NorFlash + Status =3D FlashInstance->SpiFlashProtocol->Write ( + BlockAddress, + FlashInstance->TempBuffer, + BlockSize + ); + if (EFI_ERROR (Status)) { + // Return one of the pre-approved error statuses + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + + +/** + Writes the specified number of bytes from the input buffer to the block. + + @param[in] FlashInstance The pointer of FT_FVB_DEVICE instance. + + @param[in] Lba The starting logical block index to writ= e to. + + @param[in] Offset Offset into the block at which to begin = writing. + + @param[in] BufferSizeInBytes The number of bytes to be writed. + + @param[in] Buffer The pointer to a caller-allocated buffer= that + contains the source for the write. + + @retval EFI_SUCCESS FvbFlashWrite() is executed successfully. + + @retval EFI_WRITE_PROTECTED Flash state is in the WriteDisabled stat= e. + + @retval EFI_INVALID_PARAMETER The pointer of Buffer is NULL. + +**/ +STATIC +EFI_STATUS +FvbFlashWrite ( + IN FT_FVB_DEVICE *FlashInstance, + IN EFI_LBA Lba, + IN UINTN Offset, + IN UINTN BufferSizeInBytes, + IN VOID *Buffer + ) +{ + EFI_STATUS Status; + UINT32 BlockSize; + UINT32 BlockOffset; + UINTN RemainingBytes; + UINTN WriteSize; + + if (FlashInstance->Media.ReadOnly =3D=3D TRUE) { + return EFI_WRITE_PROTECTED; + } + + if (BufferSizeInBytes =3D=3D 0) { + return EFI_SUCCESS; + } + + if (Buffer =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + Status =3D EFI_SUCCESS; + BlockSize =3D FlashInstance->Media.BlockSize; + BlockOffset =3D Offset; + RemainingBytes =3D BufferSizeInBytes; + + // The write must not span block boundaries. + // We need to check each variable individually because adding + // two large values together overflows. + if (Offset >=3D BlockSize) { + DEBUG (( + DEBUG_ERROR, + "FvbFlashWrite: ERROR - EFI_BAD_BUFFER_SIZE: Offset =3D0x%x > BlockS= ize =3D0x%x\n", + Offset, + BlockSize + )); + return EFI_BAD_BUFFER_SIZE; + } + + // We must have some bytes to read + // Write either all the remaining bytes, or the number of bytes that bri= ng + // us up to a block boundary, whichever is less. + // (DiskOffset | (BlockSize - 1)) + 1) rounds DiskOffset up to the next + // block boundary (even if it is already on one). + WriteSize =3D MIN (RemainingBytes, BlockSize - BlockOffset); + + do { + Status =3D FvbWriteBlock ( + FlashInstance, + Lba, + BlockOffset, + WriteSize, + Buffer + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // Now continue writing either all the remaining bytes or single block= s. + RemainingBytes -=3D WriteSize; + Buffer =3D (UINT8 *) Buffer + WriteSize; + Lba++; + BlockOffset =3D 0; + WriteSize =3D MIN (RemainingBytes, BlockSize); + } while (RemainingBytes); + + return Status; +} + + +/** + Initialises the FV Header and Variable Store Header + to support variable operations. + + @param[in] Ptr Location to initialise the headers. + + @retval EFI_SUCCESS FvbInitFvAndVariableStoreHeaders() + is executed successfully. + +**/ +STATIC +EFI_STATUS +FvbInitFvAndVariableStoreHeaders ( + IN FT_FVB_DEVICE *FlashInstance + ) +{ + EFI_STATUS Status; + VOID * Headers; + UINTN HeadersLength; + UINT32 TempAttributes; + EFI_FIRMWARE_VOLUME_HEADER *FirmwareVolumeHeader; + VARIABLE_STORE_HEADER *VariableStoreHeader; + + HeadersLength =3D sizeof (EFI_FIRMWARE_VOLUME_HEADER) + + sizeof (EFI_FV_BLOCK_MAP_ENTRY) + + sizeof (VARIABLE_STORE_HEADER); + + Headers =3D AllocateZeroPool (HeadersLength); + + // FirmwareVolumeHeader->FvLength is declared to have the Variable area + // AND the FTW working area AND the FTW Spare contiguous. + ASSERT (mFlashNvStorageVariableBase + mFlashNvStorageVariableSize =3D=3D= mFlashNvStorageFtwWorkingBase); + ASSERT (mFlashNvStorageFtwWorkingBase + mFlashNvStorageFtwWorkingSize = =3D=3D mFlashNvStorageFtwSpareBase); + + // Check if the size of the area is at least one block size + ASSERT ((mFlashNvStorageVariableSize > 0) && (mFlashNvStorageVariableSiz= e / FlashInstance->Media.BlockSize > 0)); + ASSERT ((mFlashNvStorageFtwWorkingSize > 0) && (mFlashNvStorageFtwWorkin= gSize / FlashInstance->Media.BlockSize > 0)); + ASSERT ((mFlashNvStorageFtwSpareSize > 0) && (mFlashNvStorageFtwSpareSiz= e / FlashInstance->Media.BlockSize > 0)); + + // Ensure the Variable area Base Addresses are aligned on a block size b= oundaries + ASSERT (mFlashNvStorageVariableBase % FlashInstance->Media.BlockSize =3D= =3D 0); + ASSERT (mFlashNvStorageFtwWorkingBase % FlashInstance->Media.BlockSize = =3D=3D 0); + ASSERT (mFlashNvStorageFtwSpareBase % FlashInstance->Media.BlockSize =3D= =3D 0); + + // + // EFI_FIRMWARE_VOLUME_HEADER + // + FirmwareVolumeHeader =3D (EFI_FIRMWARE_VOLUME_HEADER *)Headers; + CopyGuid (&FirmwareVolumeHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid= ); + FirmwareVolumeHeader->FvLength =3D FlashInstance->FvbSize; + + TempAttributes =3D ( + EFI_FVB2_READ_ENABLED_CAP | // Reads may be enabled + EFI_FVB2_READ_STATUS | // Reads are currently = enabled + EFI_FVB2_STICKY_WRITE | // A block erase is req= uired to + EFI_FVB2_MEMORY_MAPPED | // It is memory mapped + EFI_FVB2_ERASE_POLARITY | // After erasure all bi= ts take this value + EFI_FVB2_WRITE_STATUS | // Writes are currently= enabled + EFI_FVB2_WRITE_ENABLED_CAP // Writes may be enabled + ); + + FirmwareVolumeHeader->Signature =3D EFI_FVH_SIGNATURE; + FirmwareVolumeHeader->Attributes =3D (EFI_FVB_ATTRIBUTES_2) TempAttribut= es; + + FirmwareVolumeHeader->HeaderLength =3D sizeof (EFI_FIRMWARE_VOLUME_HEADE= R) + sizeof (EFI_FV_BLOCK_MAP_ENTRY); + FirmwareVolumeHeader->Revision =3D EFI_FVH_REVISION; + FirmwareVolumeHeader->BlockMap[0].NumBlocks =3D FlashInstance->Media.Las= tBlock + 1; + FirmwareVolumeHeader->BlockMap[0].Length =3D FlashInstance->Media.Blo= ckSize; + FirmwareVolumeHeader->BlockMap[1].NumBlocks =3D 0; + FirmwareVolumeHeader->BlockMap[1].Length =3D 0; + FirmwareVolumeHeader->Checksum =3D CalculateCheckSum16 ( + (UINT16 *)FirmwareVolumeHeader, + FirmwareVolumeHeader->HeaderLength + ); + + // + // VARIABLE_STORE_HEADER + // + VariableStoreHeader =3D (VARIABLE_STORE_HEADER *) ((UINTN)Headers + Firm= wareVolumeHeader->HeaderLength); + CopyGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGui= d); + VariableStoreHeader->Size =3D mFlashNvStorageVariableSize - FirmwareVo= lumeHeader->HeaderLength; + VariableStoreHeader->Format =3D VARIABLE_STORE_FORMATTED; + VariableStoreHeader->State =3D VARIABLE_STORE_HEALTHY; + + // Install the combined super-header in the NorFlash + Status =3D FvbWrite (&FlashInstance->FvbProtocol, 0, 0, &HeadersLength, = Headers); + + FreePool (Headers); + + return Status; +} + + +/** + Check the integrity of firmware volume header. + + @param[in] FwVolHeader A pointer to a firmware volume header + + @retval EFI_SUCCESS The firmware volume is consistent + + @retval EFI_NOT_FOUND The firmware volume has been corrupted. + +**/ +STATIC +EFI_STATUS +FvbValidateFvHeader ( + IN FT_FVB_DEVICE *FlashInstance + ) +{ + UINT16 Checksum; + EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader; + VARIABLE_STORE_HEADER *VariableStoreHeader; + UINTN VariableStoreLength; + UINTN FvLength; + + FwVolHeader =3D (EFI_FIRMWARE_VOLUME_HEADER *)GET_DATA_OFFSET (FlashInst= ance->RegionBaseAddress, + FlashInstance->StartLba, + FlashInstance->Media.Block= Size + ); + FvLength =3D FlashInstance->FvbSize; + + + if ((FwVolHeader->Revision !=3D EFI_FVH_REVISION) || + (FwVolHeader->Signature !=3D EFI_FVH_SIGNATURE) || + (FwVolHeader->FvLength !=3D FvLength)) + { + DEBUG (( + DEBUG_ERROR, + "ValidateFvHeader: No Firmware Volume header present\n" + )); + return EFI_NOT_FOUND; + } + + // Check the Firmware Volume Guid + if ( CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid)= =3D=3D FALSE ) { + DEBUG (( + DEBUG_ERROR, + "ValidateFvHeader: Firmware Volume Guid non-compatible\n" + )); + return EFI_NOT_FOUND; + } + + // Verify the header checksum + Checksum =3D CalculateSum16 ((UINT16 *)FwVolHeader, FwVolHeader->HeaderL= ength); + if (Checksum !=3D 0) { + DEBUG (( + DEBUG_ERROR, + "ValidateFvHeader: FV checksum is invalid (Checksum:0x%X)\n", + Checksum)); + return EFI_NOT_FOUND; + } + + VariableStoreHeader =3D (VARIABLE_STORE_HEADER *) ((UINTN)FwVolHeader + = FwVolHeader->HeaderLength); + + // Check the Variable Store Guid + if ( !CompareGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid) && + !CompareGuid (&VariableStoreHeader->Signature, + &gEfiAuthenticatedVariableGuid)) + { + DEBUG (( + DEBUG_ERROR, + "%a: Variable Store Guid non-compatible\n" + )); + return EFI_NOT_FOUND; + } + + VariableStoreLength =3D mFlashNvStorageVariableSize - FwVolHeader->Heade= rLength; + if (VariableStoreHeader->Size !=3D VariableStoreLength) { + DEBUG (( + DEBUG_ERROR, + "ValidateFvHeader: Variable Store Length does not match\n" + )); + return EFI_NOT_FOUND; + } + + return EFI_SUCCESS; +} + + +/** + The FvbGetAttributes() function retrieves the attributes and + current settings of the block. + + @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL in= stance. + + @param Attributes Pointer to EFI_FVB_ATTRIBUTES_2 in which the attribu= tes and + current settings are returned. + Type EFI_FVB_ATTRIBUTES_2 is defined in + EFI_FIRMWARE_VOLUME_HEADER. + + @retval EFI_SUCCESS The firmware volume attributes were returned. + +**/ +EFI_STATUS +EFIAPI +FvbGetAttributes ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + OUT EFI_FVB_ATTRIBUTES_2 *Attributes + ) +{ + EFI_FVB_ATTRIBUTES_2 FlashFvbAttributes; + CONST FT_FVB_DEVICE *FlashInstance; + + FlashInstance =3D INSTANCE_FROM_FVB_THIS (This); + + FlashFvbAttributes =3D (EFI_FVB_ATTRIBUTES_2) ( + EFI_FVB2_READ_ENABLED_CAP | // Reads may be enabled + EFI_FVB2_READ_STATUS | // Reads are currently enabled + EFI_FVB2_STICKY_WRITE | // A block erase is required to flip bit= s into EFI_FVB2_ERASE_POLARITY + EFI_FVB2_MEMORY_MAPPED | // It is memory mapped + EFI_FVB2_ERASE_POLARITY // After erasure all bits take this valu= e (i.e. '1') + ); + + // Check if it is write protected + if (FlashInstance->Media.ReadOnly !=3D TRUE) { + FlashFvbAttributes =3D FlashFvbAttributes | + EFI_FVB2_WRITE_STATUS | // Writes are curren= tly enabled + EFI_FVB2_WRITE_ENABLED_CAP | // Writes may be ena= bled + EFI_FVB2_ALIGNMENT_4; + } + + *Attributes =3D FlashFvbAttributes; + + return EFI_SUCCESS; +} + + +/** + The FvbSetAttributes() function sets configurable firmware volume attrib= utes + and returns the new settings of the firmware volume. + + @param This EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL inst= ance. + + @param Attributes On input, Attributes is a pointer to + EFI_FVB_ATTRIBUTES_2 that contains the d= esired + firmware volume settings. + On successful return, it contains the new + settings of the firmware volume. + + @retval EFI_SUCCESS The firmware volume attributes were retu= rned. + + @retval EFI_INVALID_PARAMETER The attributes requested are in conflict= with + the capabilities as declared in the firm= ware + volume header. + +**/ +EFI_STATUS +EFIAPI +FvbSetAttributes ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes + ) +{ + return EFI_UNSUPPORTED; +} + + +/** + The FvbGetPhysicalAddress() function retrieves the base address of + a memory-mapped firmware volume. This function should be called + only for memory-mapped firmware volumes. + + @param This EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance. + + @param Address Pointer to a caller-allocated + EFI_PHYSICAL_ADDRESS that, on successful + return from GetPhysicalAddress(), contains the + base address of the firmware volume. + + @retval EFI_SUCCESS The firmware volume base address was returned. + + @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped. + +**/ +EFI_STATUS +EFIAPI +FvbGetPhysicalAddress ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + OUT EFI_PHYSICAL_ADDRESS *Address + ) +{ + ASSERT (Address !=3D NULL); + + *Address =3D mFlashNvStorageVariableBase; + + return EFI_SUCCESS; +} + + +/** + The FvbGetBlockSize() function retrieves the size of the requested + block. It also returns the number of additional blocks with + the identical size. The FvbGetBlockSize() function is used to + retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER). + + + @param This EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL inst= ance. + + @param Lba Indicates the block whose size to return. + + @param BlockSize Pointer to a caller-allocated UINTN in w= hich + the size of the block is returned. + + @param NumberOfBlocks Pointer to a caller-allocated UINTN in + which the number of consecutive blocks, + starting with Lba, is returned. All + blocks in this range have a size of + BlockSize. + + + @retval EFI_SUCCESS The firmware volume base address was ret= urned. + + @retval EFI_INVALID_PARAMETER The requested LBA is out of range. + +**/ +EFI_STATUS +EFIAPI +FvbGetBlockSize ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + IN EFI_LBA Lba, + OUT UINTN *BlockSize, + OUT UINTN *NumberOfBlocks + ) +{ + EFI_STATUS Status; + FT_FVB_DEVICE *FlashInstance; + + FlashInstance =3D INSTANCE_FROM_FVB_THIS (This); + + if (Lba > FlashInstance->Media.LastBlock) { + Status =3D EFI_INVALID_PARAMETER; + } else { + // This is easy because in this platform each NorFlash device has equa= l sized blocks. + *BlockSize =3D (UINTN) FlashInstance->Media.BlockSize; + *NumberOfBlocks =3D (UINTN) (FlashInstance->Media.LastBlock - Lba + 1); + Status =3D EFI_SUCCESS; + } + + return Status; +} + + +/** + Reads the specified number of bytes into a buffer from the specified blo= ck. + + The FvbRead() function reads the requested number of bytes from the + requested block and stores them in the provided buffer. + + @param This EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance. + + @param Lba The starting logical block index from which = to read. + + @param Offset Offset into the block at which to begin read= ing. + + @param NumBytes Pointer to a UINTN. + At entry, *NumBytes contains the total size = of the + buffer. + At exit, *NumBytes contains the total number= of + bytes read. + + @param Buffer Pointer to a caller-allocated buffer that wi= ll be + used to hold the data that is read. + + @retval EFI_SUCCESS The firmware volume was read successfully, a= nd + contents are in Buffer. + + @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary. + On output, NumBytes contains the total numbe= r of + bytes returned in Buffer. + + @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled s= tate. + + @retval EFI_DEVICE_ERROR The block device is not functioning correctl= y and + could not be read. +**/ +EFI_STATUS +EFIAPI +FvbRead ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + IN EFI_LBA Lba, + IN UINTN Offset, + IN OUT UINTN *NumBytes, + IN OUT UINT8 *Buffer + ) +{ + UINTN BlockSize; + FT_FVB_DEVICE *FlashInstance; + EFI_STATUS Status; + + FlashInstance =3D INSTANCE_FROM_FVB_THIS (This); + + // Cache the block size to avoid de-referencing pointers all the time + BlockSize =3D FlashInstance->Media.BlockSize; + + // The read must not span block boundaries. + // We need to check each variable individually because adding two large = values together overflows. + if ((Offset >=3D BlockSize) || + (*NumBytes > BlockSize) || + ((Offset + *NumBytes) > BlockSize)) { + return EFI_BAD_BUFFER_SIZE; + } + + // We must have some bytes to read + if (*NumBytes =3D=3D 0) { + return EFI_BAD_BUFFER_SIZE; + } + + Status =3D FvbFlashRead ( + FlashInstance, + FlashInstance->StartLba + Lba, + Offset, + *NumBytes, + Buffer + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + + +/** + Writes the specified number of bytes from the input buffer to the block. + + The FvbWrite() function writes the specified number of bytes from + the provided buffer to the specified block and offset. + + @param This EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance. + + @param Lba The starting logical block index to write to. + + @param Offset Offset into the block at which to begin writ= ing. + + @param NumBytes The pointer to a UINTN. + At entry, *NumBytes contains the total size = of the + buffer. + At exit, *NumBytes contains the total number= of + bytes actually written. + + @param Buffer The pointer to a caller-allocated buffer that + contains the source for the write. + + @retval EFI_SUCCESS The firmware volume was written successfully. + + @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA bounda= ry. + On output, NumBytes contains the total numbe= r of + bytes actually written. + + @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled = state. + + @retval EFI_DEVICE_ERROR The block device is malfunctioning and could= not be + written. + +**/ +EFI_STATUS +EFIAPI +FvbWrite ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + IN EFI_LBA Lba, + IN UINTN Offset, + IN OUT UINTN *NumBytes, + IN UINT8 *Buffer + ) +{ + FT_FVB_DEVICE *FlashInstance; + + FlashInstance =3D INSTANCE_FROM_FVB_THIS (This); + + return FvbFlashWrite (FlashInstance, + FlashInstance->StartLba + Lba, + Offset, + *NumBytes, + Buffer + ); +} + + +/** + Erases and initialises a firmware volume block. + + The FvbEraseBlocks() function erases one or more blocks as denoted + by the variable argument list. + + @param This EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL + instance. + + @param ... The variable argument list is a list of = tuples. + Each tuple describes a range of LBAs to = erase + and consists of the following: + An EFI_LBA that indicates the starting L= BA + A UINTN that indicates the number of blo= cks + to erase. + + The list is terminated with an + EFI_LBA_LIST_TERMINATOR. + + @retval EFI_SUCCESS The erase request successfully completed. + + @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisab= led + state. + + @retval EFI_DEVICE_ERROR The block device is not functioning corr= ectly + and could not be written. + The firmware device may have been partia= lly + erased. + + @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the va= riable + argument list do not exist in the firmwa= re + volume. + +**/ +EFI_STATUS +EFIAPI +FvbEraseBlocks ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + ... + ) +{ + EFI_STATUS Status; + VA_LIST Args; + UINTN BlockAddress; // Physical address of Lba to erase + EFI_LBA StartingLba; // Lba from which we start erasing + UINTN NumOfLba; // Number of Lba blocks to erase + FT_FVB_DEVICE *Instance; + + Instance =3D INSTANCE_FROM_FVB_THIS (This); + + Status =3D EFI_SUCCESS; + + // Detect WriteDisabled state + if (Instance->Media.ReadOnly =3D=3D TRUE) { + // Firmware volume is in WriteDisabled state + DEBUG (( + DEBUG_ERROR, + "FvbEraseBlocks: ERROR - Device is in WriteDisabled state.\n" + )); + return EFI_ACCESS_DENIED; + } + + // Before erasing, check the entire list of parameters to ensure all spe= cified blocks are valid + + VA_START (Args, This); + do { + // Get the Lba from which we start erasing + StartingLba =3D VA_ARG (Args, EFI_LBA); + + // Have we reached the end of the list? + if (StartingLba =3D=3D EFI_LBA_LIST_TERMINATOR) { + //Exit the while loop + break; + } + + // How many Lba blocks are we requested to erase? + NumOfLba =3D VA_ARG (Args, UINT32); + + // All blocks must be within range + if ((NumOfLba =3D=3D 0) || ((Instance->StartLba + StartingLba + NumOfL= ba - 1) > Instance->Media.LastBlock)) { + VA_END (Args); + DEBUG (( + DEBUG_ERROR, + "FvbEraseBlocks: ERROR - Lba range goes past the last Lba.\n" + )); + Status =3D EFI_INVALID_PARAMETER; + goto EXIT; + } + } while (TRUE); + + VA_END (Args); + + // + // To get here, all must be ok, so start erasing + // + VA_START (Args, This); + do { + // Get the Lba from which we start erasing + StartingLba =3D VA_ARG (Args, EFI_LBA); + + // Have we reached the end of the list? + if (StartingLba =3D=3D EFI_LBA_LIST_TERMINATOR) { + // Exit the while loop + break; + } + + // How many Lba blocks are we requested to erase? + NumOfLba =3D VA_ARG (Args, UINT32); + + // Go through each one and erase it + while (NumOfLba > 0) { + // Get the physical address of Lba to erase + BlockAddress =3D GET_DATA_OFFSET ( + Instance->RegionBaseAddress, + Instance->StartLba + StartingLba, + Instance->Media.BlockSize + ); + + // Erase it + Status =3D FvbFlashEraseSingleBlock (Instance, BlockAddress); + if (EFI_ERROR (Status)) { + VA_END (Args); + Status =3D EFI_DEVICE_ERROR; + goto EXIT; + } + + // Move to the next Lba + StartingLba++; + NumOfLba--; + } + } while (TRUE); + + VA_END (Args); + +EXIT: + return Status; +} + + +/** + This function inited the NorFlash instance. + + @param[in][out] FlashInstance The pointer of FT_FVB_DEVICE instance. + + @retval EFI_SUCCESS PhytNorFlashFvbInitialize() is executed = successfully. + +**/ +STATIC +EFI_STATUS +PhytNorFlashFvbInitialize ( + IN OUT FT_FVB_DEVICE *FlashInstance + ) +{ + EFI_STATUS Status; + UINT32 FvbNumLba; + EFI_BOOT_MODE BootMode; + UINTN RuntimeMmioRegionSize; + + RuntimeMmioRegionSize =3D FlashInstance->Size; + + // Declare the Non-Volatile storage as EFI_MEMORY_RUNTIME + Status =3D gDS->AddMemorySpace ( + EfiGcdMemoryTypeMemoryMappedIo, + FlashInstance->RegionBaseAddress, + RuntimeMmioRegionSize, + EFI_MEMORY_UC | EFI_MEMORY_RUNTIME + ); + ASSERT_EFI_ERROR (Status); + + Status =3D gDS->SetMemorySpaceAttributes ( + FlashInstance->RegionBaseAddress, + RuntimeMmioRegionSize, + EFI_MEMORY_UC | EFI_MEMORY_RUNTIME + ); + ASSERT_EFI_ERROR (Status); + + // Set the index of the first LBA for the FVB + FlashInstance->StartLba =3D (mFlashNvStorageVariableBase - FlashInstance= ->RegionBaseAddress) / FlashInstance->Media.BlockSize; + + BootMode =3D GetBootModeHob (); + if (BootMode =3D=3D BOOT_WITH_DEFAULT_SETTINGS) { + Status =3D EFI_INVALID_PARAMETER; + } else { + // Determine if there is a valid header at the beginning of the NorFla= sh + Status =3D FvbValidateFvHeader (FlashInstance); + } + + // Install the Default FVB header if required + if (EFI_ERROR (Status)) { + // There is no valid header, so time to install one. + DEBUG (( + DEBUG_ERROR, + "NorFlashFvbInitialize: ERROR - The FVB Header is invalid. Installin= g a correct one for this volume.\n" + )); + + // Erase all the NorFlash that is reserved for variable storage + FvbNumLba =3D FlashInstance->FvbSize / FlashInstance->Media.BlockSize; + + Status =3D FvbEraseBlocks ( + &FlashInstance->FvbProtocol, + (EFI_LBA)0, + FvbNumLba, + EFI_LBA_LIST_TERMINATOR + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // Install all appropriate headers + Status =3D FvbInitFvAndVariableStoreHeaders (FlashInstance); + if (EFI_ERROR (Status)) { + return Status; + } + } + + return Status; +} + + +/** + The CreateInstance() function Create Fvb Instance. + + @retval EFI_SUCCESS Create Instance successfully. + + @retval other Create Instance failed. + +**/ +STATIC +EFI_STATUS +CreateInstance ( + VOID + ) +{ + EFI_STATUS Status; + NOR_FLASH_DEVICE_DESCRIPTION *NorFlashDevice; + + mFvbDevice =3D AllocateRuntimeCopyPool (sizeof (mFvbFlashInstanceTemplat= e), &mFvbFlashInstanceTemplate); + if (mFvbDevice =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + + } + // Locate flash protocols + Status =3D gBS->LocateProtocol (&gSpiNorFlashProtocolGuid, + NULL, + (VOID **)&mFvbDevice->SpiFlashProtocol); + if (EFI_ERROR (Status)) { + DEBUG (( + DEBUG_ERROR, + "Cannot locate NorFlash protocol.\n" + )); + return Status; + } + + NorFlashDevice =3D AllocateRuntimePool (sizeof (NOR_FLASH_DEVICE_DESCRIP= TION)); + if (NorFlashDevice =3D=3D NULL) { + DEBUG (( + DEBUG_ERROR, + "Cannot Allocate NorFlashDevice Pool.\n" + )); + return Status; + } + + Status =3D mFvbDevice->SpiFlashProtocol->GetDevices (NorFlashDevice); + if (EFI_ERROR (Status)) { + return Status; + } + + Status =3D mFvbDevice->SpiFlashProtocol->Initialization (); + if (EFI_ERROR (Status)) { + return Status; + } + + mFvbDevice->DeviceBaseAddress =3D NorFlashDevice->DeviceBaseAddress; + mFvbDevice->RegionBaseAddress =3D NorFlashDevice->RegionBaseAddress; + mFvbDevice->Size =3D NorFlashDevice->Size; + + mFvbDevice->Media.MediaId =3D 0; + mFvbDevice->Media.BlockSize =3D NorFlashDevice->BlockSize; + mFvbDevice->Media.LastBlock =3D (mFvbDevice->Size / mFvbDevice->Media.Bl= ockSize) - 1; + mFvbDevice->FvbSize =3D mFlashNvStorageVariableSize + + mFlashNvStorageFtwWorkingSize + + mFlashNvStorageFtwSpareSize; + DEBUG((DEBUG_INFO, "mFvbDevice->fvbSize=3D%x\n", mFvbDevice->FvbSize)); + CopyGuid (&mFvbDevice->DevicePath.Vendor.Guid, &NorFlashDevice->Guid); + + mFvbDevice->TempBuffer =3D AllocateRuntimePool (mFvbDevice->Media.BlockS= ize); + if (mFvbDevice->TempBuffer =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status =3D PhytNorFlashFvbInitialize (mFvbDevice); + if (EFI_ERROR (Status)) { + DEBUG (( + DEBUG_ERROR, + "PhytNorFlashFvbInitialize: Fail to init NorFlash devices\n" + )); + return Status; + } + + Status =3D gBS->InstallMultipleProtocolInterfaces ( + &mFvbDevice->Handle, + &gEfiDevicePathProtocolGuid, + &mFvbDevice->DevicePath, + &gEfiFirmwareVolumeBlockProtocolGuid, + &mFvbDevice->FvbProtocol, + NULL + ); + if (EFI_ERROR (Status)) { + FreePool (mFvbDevice); + FreePool (NorFlashDevice); + return Status; + } + + FreePool (NorFlashDevice); + + return Status; +} + + +/** + Fixup internal data so that EFI can be call in virtual mode. + Call the passed in Child Notify event and convert any pointers + in lib to virtual mode. + + @param[in] Event The Event that is being processed. + + @param[in] Context Event Context. + + @retval None. + +**/ +STATIC +VOID +EFIAPI +FvbVirtualNotifyEvent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + VOID *Pointer; + + EfiConvertPointer (0x0, (VOID **)&mFlashNvStorageVariableBase); + EfiConvertPointer (0x0, (VOID **)&mFlashNvStorageFtwWorkingBase); + EfiConvertPointer (0x0, (VOID **)&mFlashNvStorageFtwSpareBase); + if (!mFvbDevice->DeviceBaseAddress) { + mFvbDevice->DeviceBaseAddress |=3D 0x1; + } + + if (!mFvbDevice->RegionBaseAddress) { + mFvbDevice->RegionBaseAddress |=3D 0x1; + } + EfiConvertPointer (0x0, (VOID **)&mFvbDevice->DeviceBaseAddress); + EfiConvertPointer (0x0, (VOID **)&mFvbDevice->RegionBaseAddress); + + mFvbDevice->DeviceBaseAddress &=3D ~(EFI_PHYSICAL_ADDRESS)0x1; + mFvbDevice->RegionBaseAddress &=3D ~(EFI_PHYSICAL_ADDRESS)0x1; + + // Convert SpiFlashProtocol + EfiConvertPointer (0x0, (VOID **)&(mFvbDevice->SpiFlashProtocol->Erase)); + EfiConvertPointer (0x0, (VOID **)&(mFvbDevice->SpiFlashProtocol->Write)); + EfiConvertPointer (0x0, (VOID **)&(mFvbDevice->SpiFlashProtocol->Read)); + EfiConvertPointer (0x0, (VOID **)&(mFvbDevice->SpiFlashProtocol->GetDevi= ces)); + EfiConvertPointer (0x0, (VOID **)&(mFvbDevice->SpiFlashProtocol)); + + EfiConvertPointer (0x0, (VOID **)&(mFvbDevice->FvbProtocol.EraseBlocks)); + EfiConvertPointer (0x0, (VOID **)&(mFvbDevice->FvbProtocol.GetAttributes= )); + EfiConvertPointer (0x0, (VOID **)&(mFvbDevice->FvbProtocol.GetBlockSize)= ); + + Pointer =3D (VOID *)(UINTN)mFvbDevice->FvbProtocol.GetPhysicalAddress; + EfiConvertPointer (0x0, &Pointer); + mFvbDevice->FvbProtocol.GetPhysicalAddress =3D (EFI_FVB_GET_PHYSICAL_ADD= RESS)(UINTN)Pointer; + EfiConvertPointer (0x0, (VOID **)&(mFvbDevice->FvbProtocol.Read)); + EfiConvertPointer (0x0, (VOID **)&(mFvbDevice->FvbProtocol.SetAttributes= )); + EfiConvertPointer (0x0, (VOID **)&(mFvbDevice->FvbProtocol.Write)); + EfiConvertPointer (0x0, (VOID **)&(mFvbDevice->FvbProtocol)); + + if (mFvbDevice->TempBuffer !=3D NULL) { + EfiConvertPointer (0x0, (VOID **)&(mFvbDevice->TempBuffer)); + } + EfiConvertPointer (0x0, (VOID **)&(mFvbDevice)); + + return; +} + + +/** + This function is the entrypoint of the fvb driver. + + @param[in] ImageHandle The firmware allocated handle for the EFI imag= e. + + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The entry point is executed successfully. + + @retval other Some error occurs when executing this entry po= int. + +**/ +EFI_STATUS +EFIAPI +FvbEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + mFlashNvStorageVariableBase =3D FixedPcdGet64 (PcdFlashNvStorageVariab= leBase64); + mFlashNvStorageFtwWorkingBase =3D FixedPcdGet64 (PcdFlashNvStorageFtwWor= kingBase64); + mFlashNvStorageFtwSpareBase =3D FixedPcdGet64 (PcdFlashNvStorageFtwSpa= reBase64); + mFlashNvStorageVariableSize =3D FixedPcdGet32 (PcdFlashNvStorageVariab= leSize); + mFlashNvStorageFtwWorkingSize =3D FixedPcdGet32 (PcdFlashNvStorageFtwWor= kingSize); + mFlashNvStorageFtwSpareSize =3D FixedPcdGet32 (PcdFlashNvStorageFtwSpa= reSize); + + Status =3D CreateInstance (); + if (EFI_ERROR (Status)) { + DEBUG (( + DEBUG_ERROR, + "CreateInstance: Fail to create instance for NorFlash\n" + )); + } + // Register for the virtual address change event + // + Status =3D gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + FvbVirtualNotifyEvent, + NULL, + &gEfiEventVirtualAddressChangeGuid, + &mFvbVirtualAddrChangeEvent + ); + ASSERT_EFI_ERROR (Status); + + return Status; +} --=20 2.25.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#99290): https://edk2.groups.io/g/devel/message/99290 Mute This Topic: https://groups.io/mt/96623623/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Wed May 15 07:46:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+99288+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+99288+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1675064265; cv=none; d=zohomail.com; s=zohoarc; b=dYOYGW8ahccocc+BN4OlaprTIsINGPKwk0cbVk9sTw2yg5iAevsGCxZL5w0/tdkEu3N7Tq2Z4hgG8UZMqx7b80kQafyae9SDCqLO/zHbzTdjPcZAumWQSgqzAhtDusIj9uaDUV9KkteeIxDf0LFRI9VCvdmwrgv//EsjJjbjkP0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1675064265; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=4mc41wsYFIgRW/i8eoU06j59OjU8Bi0xh580sM1JxEE=; b=aoEZZzVdX5RndjQzfaN1wiU0whRMISqWqpvv8piIB/oes+jl8FSyhheGJjmCWzDU/fFR/yNCMDnFRh3fhjHsSluqDnfB96nP7A19+agr0UlUaJkKQ+HWHrULOOEHuK6CKi++XQY1bGZpnBMKsCFpRfzL07SrkYjf9KD639H0Op0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+99288+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1675064265075972.4896400042636; Sun, 29 Jan 2023 23:37:45 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id vLy5YY1788612x7WnoTqaDTy; Sun, 29 Jan 2023 23:37:44 -0800 X-Received: from azure-sdnproxy.icoremail.net (azure-sdnproxy.icoremail.net [20.228.234.168]) by mx.groups.io with SMTP id smtpd.web10.35960.1675064261634224389 for ; Sun, 29 Jan 2023 23:37:42 -0800 X-Received: from prodtpl.icoremail.net (unknown [10.12.1.20]) by hzbj-icmmx-6 (Coremail) with SMTP id AQAAfwDnie0QdNdjK72SBA--.61759S2; Mon, 30 Jan 2023 15:38:56 +0800 (CST) X-Received: from localhost.localdomain (unknown [117.136.89.78]) by mail (Coremail) with SMTP id AQAAfwBnZB26c9djDDUAAA--.158S6; Mon, 30 Jan 2023 15:37:38 +0800 (CST) From: "Ling Jia" To: devel@edk2.groups.io Cc: Leif Lindholm , Liming Gao , Michael D Kinney , Yiqi Shu , Peng Xie , Ling Jia Subject: [edk2-devel] [PATCH v8 4/4] Silicon/Phytium: Added runtime support to spi master Date: Mon, 30 Jan 2023 15:34:20 +0800 Message-Id: <20230130073420.135519-5-jialing@phytium.com.cn> In-Reply-To: <20230130073420.135519-1-jialing@phytium.com.cn> References: <20230130073420.135519-1-jialing@phytium.com.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAfwBnZB26c9djDDUAAA--.158S6 X-CM-SenderInfo: xmldzxdqj61x51wl3zoofrzhdfq/ X-Coremail-Antispam: 1Uk129KBjvJXoWxKw15Gw4rGw4kGF1DuFy8Zrb_yoWxtF45pa 18trs0qr4UJr43Z3yrZw1kur95A39xKFyDtrZxWa4ruF4agry0ganxZr1rJF97tr9Fy348 WrnYqw47urZ0yw7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj DUYxn0WfASr-VFAU7a7-sFnT9fnUUIcSsGvfJ3UbIYCTnIWIevJa73UjIFyTuYvj4RJUUU UUUUU Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,jialing@phytium.com.cn X-Gm-Message-State: CdrWb4AHG8X6qpqtYplWYKNRx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1675064264; bh=Z0XTc84I/isSQh8IQCzDtYfEItZpnRg+nsvfWq2643Q=; h=Cc:Date:From:Reply-To:Subject:To; b=JIQwcJU/wVA1utEQIjpBOf86uhdvOTFC0L1VdelnMk9cPTxMOJ38oeIfPiOiMNsbnNu e96cYN+50fFLVllHCHv185cDhYZaw5B3cQb9MxHYLPWV70hDObW9flxbnIZwMZhJSJbp4 ZrhS8VcoqOfePqGfm2aRjHBU9H4afwTMa5s= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1675064266445100006 Content-Type: text/plain; charset="utf-8" From: Ling Jia Solved the problem of virtual address translation in runtime access under OS. Signed-off-by: Ling Jia --- Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.inf | 11 ++- Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.h | 4 +- Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.c | 88 +++++++++++++++= ++--- 3 files changed, 86 insertions(+), 17 deletions(-) diff --git a/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.inf b/Silico= n/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.inf index 21d75f268da4..d7ee1999c4ed 100644 --- a/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.inf +++ b/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.inf @@ -1,11 +1,11 @@ -#/** @file +## @file # Phytium Spi Master Drivers. # -# Copyright (C) 2020, Phytium Technology Co, Ltd. All rights reserved.
+# Copyright (C) 2020-2023, Phytium Technology Co., Ltd. All rights reserv= ed.
# # SPDX-License-Identifier: BSD-2-Clause-Patent # -#**/ +# =20 [Defines] INF_VERSION =3D 0x0001001b @@ -27,18 +27,23 @@ [Packages] [LibraryClasses] BaseLib DebugLib + DxeServicesTableLib IoLib UefiLib UefiBootServicesTableLib UefiDriverEntryPoint + UefiRuntimeLib + UefiRuntimeServicesTableLib =20 [Guids] + gEfiEventVirtualAddressChangeGuid =20 [Protocols] gSpiMasterProtocolGuid =20 [FixedPcd] gPhytiumPlatformTokenSpaceGuid.PcdSpiControllerBase + gPhytiumPlatformTokenSpaceGuid.PcdSpiControllerSize =20 [Depex] TRUE diff --git a/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.h b/Silicon/= Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.h index 8795ea238d83..80d1db79ebbc 100644 --- a/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.h +++ b/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.h @@ -1,7 +1,7 @@ /** @file Phytium Spi Drivers Header =20 - Copyright (C) 2020, Phytium Technology Co Ltd. All rights reserved.
+ Copyright (C) 2020-2023, Phytium Technology Co., Ltd. All rights reserve= d.
=20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -10,6 +10,8 @@ #ifndef SPI_DXE_H_ #define SPI_DXE_H_ =20 +#include +#include #include =20 #define SPI_MASTER_SIGNATURE SIGNATURE_32 ('M', 'S', 'P', 'I') diff --git a/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.c b/Silicon/= Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.c index 7602a3e0cd63..6915c95f40b0 100644 --- a/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.c +++ b/Silicon/Phytium/FT2000-4Pkg/Drivers/SpiDxe/SpiDxe.c @@ -1,7 +1,7 @@ /** @file Phytium Spi Master Drivers. =20 - Copyright (C) 2020, Phytium Technology Co Ltd. All rights reserved.
+ Copyright (C) 2020-2023, Phytium Technology Co., Ltd. All rights reserve= d.
=20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -13,8 +13,9 @@ #include #include "SpiDxe.h" =20 -PHYT_SPI_MASTER *pSpiMasterInstance; -static UINTN mSpiControlBase; +STATIC EFI_EVENT mSpiMasterVirtualAddrChangeEvent; +STATIC UINTN mSpiMasterControlBase; +PHYT_SPI_MASTER *mSpiMasterInstance; =20 /** This function inited a spi driver. @@ -66,7 +67,7 @@ SpiMasterSetConfig ( Value =3D Config; } =20 - SpiAddr =3D mSpiControlBase + RegAddr; + SpiAddr =3D mSpiMasterControlBase + RegAddr; MmioWrite32 (SpiAddr, Value); =20 return EFI_SUCCESS; @@ -78,7 +79,7 @@ SpiMasterSetConfig ( =20 @param[in] CmdId The id of command. =20 - @param[out] Config The pointer of the config. + @param[out] Config The pointer to the config. =20 @param[in] RegAddr The address of spi registers. =20 @@ -99,7 +100,7 @@ SpiMasterGetConfig ( SpiAddr =3D 0; Value =3D 0; =20 - SpiAddr =3D mSpiControlBase + RegAddr; + SpiAddr =3D mSpiMasterControlBase + RegAddr; Value =3D MmioRead32 (SpiAddr); =20 if (CmdId !=3D 0) { @@ -157,6 +158,34 @@ SpiMasterInitProtocol ( return EFI_SUCCESS; } =20 +/** + Fixup internal data so that EFI can be call in virtual mode. + Call the passed in Child Notify event and convert any pointers + in lib to virtual mode. + + @param[in] Event The Event that is being processed. + + @param[in] Context Event Context. + + @retval None. + +**/ +STATIC +VOID +EFIAPI +SpiMasterVirtualNotifyEvent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EfiConvertPointer (0x0, (VOID **)&(mSpiMasterControlBase)); + EfiConvertPointer (0x0, (VOID **)&(mSpiMasterInstance->SpiMasterProtocol= .SpiGetConfig)); + EfiConvertPointer (0x0, (VOID **)&(mSpiMasterInstance->SpiMasterProtocol= .SpiSetConfig)); + EfiConvertPointer (0x0, (VOID **)&(mSpiMasterInstance->SpiMasterProtocol= )); + EfiConvertPointer (0x0, (VOID **)&(mSpiMasterInstance)); + + return; +} =20 /** This function is the entrypoint of the spi driver. @@ -178,25 +207,58 @@ SpiMasterDrvEntryPoint ( ) { EFI_STATUS Status; + UINTN SpiMasterControlSize; =20 - pSpiMasterInstance =3D AllocateRuntimeZeroPool (sizeof (PHYT_SPI_MASTER)= ); - if (pSpiMasterInstance =3D=3D NULL) { + mSpiMasterControlBase =3D FixedPcdGet64 (PcdSpiControllerBase); + SpiMasterControlSize =3D FixedPcdGet64 (PcdSpiControllerSize); + + mSpiMasterInstance =3D AllocateRuntimeZeroPool (sizeof (PHYT_SPI_MASTER)= ); + if (mSpiMasterInstance =3D=3D NULL) { return EFI_OUT_OF_RESOURCES; } =20 - mSpiControlBase =3D FixedPcdGet64 (PcdSpiControllerBase); + mSpiMasterControlBase =3D FixedPcdGet64 (PcdSpiControllerBase); =20 - SpiMasterInitProtocol (&pSpiMasterInstance->SpiMasterProtocol); + SpiMasterInitProtocol (&mSpiMasterInstance->SpiMasterProtocol); =20 - pSpiMasterInstance->Signature =3D SPI_MASTER_SIGNATURE; + mSpiMasterInstance->Signature =3D SPI_MASTER_SIGNATURE; =20 Status =3D gBS->InstallMultipleProtocolInterfaces ( - &(pSpiMasterInstance->Handle), + &(mSpiMasterInstance->Handle), &gSpiMasterProtocolGuid, - &(pSpiMasterInstance->SpiMasterProtocol), + &(mSpiMasterInstance->SpiMasterProtocol), NULL ); ASSERT_EFI_ERROR (Status); =20 + // + // Declare the SPI Controller Space as EFI_MEMORY_RUNTIME + // + Status =3D gDS->AddMemorySpace ( + EfiGcdMemoryTypeMemoryMappedIo, + (mSpiMasterControlBase >> EFI_PAGE_SHIFT) << EFI_PAGE_SH= IFT, EFI_PAGES_TO_SIZE(EFI_SIZE_TO_PAGES(SpiMasterControlSize)), + EFI_MEMORY_UC | EFI_MEMORY_RUNTIME + ); + ASSERT_EFI_ERROR (Status); + + Status =3D gDS->SetMemorySpaceAttributes ( + (mSpiMasterControlBase >> EFI_PAGE_SHIFT) << EFI_PAGE_SH= IFT, EFI_PAGES_TO_SIZE(EFI_SIZE_TO_PAGES(SpiMasterControlSize)), + EFI_MEMORY_UC | EFI_MEMORY_RUNTIME + ); + ASSERT_EFI_ERROR (Status); + + // + // Register for the virtual address change event + // + Status =3D gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + SpiMasterVirtualNotifyEvent, + NULL, + &gEfiEventVirtualAddressChangeGuid, + &mSpiMasterVirtualAddrChangeEvent + ); + ASSERT_EFI_ERROR (Status); + return EFI_SUCCESS; } --=20 2.25.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#99288): https://edk2.groups.io/g/devel/message/99288 Mute This Topic: https://groups.io/mt/96623621/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-