From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail05.groups.io (mail05.groups.io [45.79.224.7]) by spool.mail.gandi.net (Postfix) with ESMTPS id 64263740039 for ; Tue, 23 Apr 2024 14:55:59 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=ap0QFhhBL+qJ/qyeFIRWox2S9hLagkyLpGy4k+CKhUs=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Resent-Date:Resent-From:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20240206; t=1713884158; v=1; b=tVDm2jBQtrumXC0uwVpKFoQWwdOKARvbbdqbeRJE3mjXRFmBY+FcoRr41JBdbX0BemP5NQet NavzTSE2C2lWYM+hlNZbctcZz39vwtDkZCtj2x3UYzr1i0TmfktMdcFDgWVcGDx1BZeFAYdqk6W agjz27wZGPJe/4S2oWxe4eWOjm9w8L4wSY5/PdaDJq8adFmYARaqE7BImLeRoS0D/42nNpwaa6O v2FeyoZ097BX7Uye3nu9yXT9q+uFqsBMeDOWXRyHeh13HE2WVLkr784saaCofeujbAIFCys6eAv a/tAIhFr6IGlLwpTSnnzGpRW+9fr+YtPKyNNmLOPlMq3g== X-Received: by 127.0.0.2 with SMTP id SOtSYY7687511xsGypFYG1gt; Tue, 23 Apr 2024 07:55:58 -0700 X-Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by mx.groups.io with SMTP id smtpd.web10.12021.1713851819424996364 for ; Mon, 22 Apr 2024 22:56:59 -0700 X-Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 2306A1480; Mon, 22 Apr 2024 22:57:27 -0700 (PDT) X-Received: from usa.arm.com (a077434.arm.com [10.162.46.143]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 698273F7BD; Mon, 22 Apr 2024 22:56:57 -0700 (PDT) From: "Sahil Kaushal" To: devel@edk2.groups.io Cc: Ard Biesheuvel , =?UTF-8?q?Leif=20Lindholm=20=C2=A0?= , Sami Mujawar , =?UTF-8?q?sahil=20=C2=A0?= Subject: [edk2-devel] [PATCH RESEND edk2-platforms][PATCH V2 04/14] Platform/ARM/NorFlashDxe: Move flash specific functions to NorFlash.c Date: Tue, 23 Apr 2024 11:26:28 +0530 Message-Id: <20240423055638.1271531-5-Sahil.Kaushal@arm.com> In-Reply-To: <20240423055638.1271531-1-Sahil.Kaushal@arm.com> References: <20240423055638.1271531-1-Sahil.Kaushal@arm.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Resent-Date: Tue, 23 Apr 2024 07:55:50 -0700 Resent-From: sahil.kaushal@arm.com Reply-To: devel@edk2.groups.io,sahil.kaushal@arm.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: BMVDReZD8ZcOJ24IJFrS5icNx7686176AA= Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=tVDm2jBQ; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=arm.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 45.79.224.7 as permitted sender) smtp.mailfrom=bounce@groups.io From: sahil Refactoring done in this patch has two major parts: 1. Moving out NorFlashUnlockAndEraseSingleBlock and NorFlashWriteFullBlock functions from NorFlashDxe.c and NorFlashStandaloneMm.c to NorFlash.c files. 2. At the same time, we are adding NorFlashLock and NorFlashUnlock functions which will take care of TPL related operations needed by functions mentioned in point 1. These functions are implemented in NorFlashDxe.c but are just dummy placeholder functions in NorFlashStandaloneMm.c file. Signed-off-by: sahil --- Platform/ARM/Drivers/NorFlashDxe/NorFlash.h | 26 +++ Platform/ARM/Drivers/NorFlashDxe/NorFlashCommon.h | 14 -- Platform/ARM/Drivers/NorFlashDxe/NorFlash.c | 136 ++++++++++++= +- Platform/ARM/Drivers/NorFlashDxe/NorFlashDxe.c | 193 ++++--------= -------- Platform/ARM/Drivers/NorFlashDxe/NorFlashStandaloneMm.c | 151 +++---------= --- 5 files changed, 225 insertions(+), 295 deletions(-) diff --git a/Platform/ARM/Drivers/NorFlashDxe/NorFlash.h b/Platform/ARM/Dri= vers/NorFlashDxe/NorFlash.h index e0ebb1e2fd35..bd5c6a949cf0 100644 --- a/Platform/ARM/Drivers/NorFlashDxe/NorFlash.h +++ b/Platform/ARM/Drivers/NorFlashDxe/NorFlash.h @@ -220,4 +220,30 @@ NorFlashWriteSingleWord ( IN UINT32 WriteData=0D );=0D =0D +EFI_STATUS=0D +NorFlashWriteFullBlock (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN EFI_LBA Lba,=0D + IN UINT32 *DataBuffer,=0D + IN UINT32 BlockSizeInWords=0D + );=0D +=0D +EFI_STATUS=0D +NorFlashUnlockAndEraseSingleBlock (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN UINTN BlockAddress=0D + );=0D +=0D +VOID=0D +EFIAPI=0D +NorFlashLock (=0D + IN EFI_TPL *OriginalTPL=0D + );=0D +=0D +VOID=0D +EFIAPI=0D +NorFlashUnlock (=0D + IN EFI_TPL OriginalTPL=0D + );=0D +=0D #endif /* __NOR_FLASH_H__ */=0D diff --git a/Platform/ARM/Drivers/NorFlashDxe/NorFlashCommon.h b/Platform/A= RM/Drivers/NorFlashDxe/NorFlashCommon.h index e329e0727617..c0a3b5861532 100644 --- a/Platform/ARM/Drivers/NorFlashDxe/NorFlashCommon.h +++ b/Platform/ARM/Drivers/NorFlashDxe/NorFlashCommon.h @@ -31,20 +31,6 @@ //=0D // NorFlashDxe.c=0D //=0D -EFI_STATUS=0D -NorFlashWriteFullBlock (=0D - IN NOR_FLASH_INSTANCE *Instance,=0D - IN EFI_LBA Lba,=0D - IN UINT32 *DataBuffer,=0D - IN UINT32 BlockSizeInWords=0D - );=0D -=0D -EFI_STATUS=0D -NorFlashUnlockAndEraseSingleBlock (=0D - IN NOR_FLASH_INSTANCE *Instance,=0D - IN UINTN BlockAddress=0D - );=0D -=0D EFI_STATUS=0D NorFlashCreateInstance (=0D IN UINTN NorFlashDeviceBase,=0D diff --git a/Platform/ARM/Drivers/NorFlashDxe/NorFlash.c b/Platform/ARM/Dri= vers/NorFlashDxe/NorFlash.c index 4e5a97c83c7b..15000a692b02 100644 --- a/Platform/ARM/Drivers/NorFlashDxe/NorFlash.c +++ b/Platform/ARM/Drivers/NorFlashDxe/NorFlash.c @@ -10,7 +10,6 @@ #include =0D =0D #include "NorFlash.h"=0D -#include "NorFlashCommon.h"=0D =0D //=0D // Global variable declarations=0D @@ -817,3 +816,138 @@ NorFlashReset ( SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);=0D return EFI_SUCCESS;=0D }=0D +=0D +/**=0D + * This function unlock and erase an entire NOR Flash block.=0D +**/=0D +EFI_STATUS=0D +NorFlashUnlockAndEraseSingleBlock (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN UINTN BlockAddress=0D + )=0D +{=0D + EFI_STATUS Status;=0D + UINTN Index;=0D + EFI_TPL OriginalTPL;=0D +=0D + NorFlashLock (&OriginalTPL);=0D +=0D + Index =3D 0;=0D + // The block erase might fail a first time (SW bug ?). Retry it ...=0D + do {=0D + // Unlock the block if we have to=0D + Status =3D NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddres= s);=0D + if (EFI_ERROR (Status)) {=0D + break;=0D + }=0D +=0D + Status =3D NorFlashEraseSingleBlock (Instance, BlockAddress);=0D + Index++;=0D + } while ((Index < NOR_FLASH_ERASE_RETRY) && (Status =3D=3D EFI_WRITE_PRO= TECTED));=0D +=0D + if (Index =3D=3D NOR_FLASH_ERASE_RETRY) {=0D + DEBUG ((DEBUG_ERROR, "EraseSingleBlock(BlockAddress=3D0x%08x: Block Lo= cked Error (try to erase %d times)\n", BlockAddress, Index));=0D + }=0D +=0D + NorFlashUnlock (OriginalTPL);=0D +=0D + return Status;=0D +}=0D +=0D +EFI_STATUS=0D +NorFlashWriteFullBlock (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN EFI_LBA Lba,=0D + IN UINT32 *DataBuffer,=0D + IN UINT32 BlockSizeInWords=0D + )=0D +{=0D + EFI_STATUS Status;=0D + UINTN WordAddress;=0D + UINT32 WordIndex;=0D + UINTN BufferIndex;=0D + UINTN BlockAddress;=0D + UINTN BuffersInBlock;=0D + UINTN RemainingWords;=0D + EFI_TPL OriginalTPL;=0D + UINTN Cnt;=0D +=0D + Status =3D EFI_SUCCESS;=0D +=0D + // Get the physical address of the block=0D + BlockAddress =3D GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba= , BlockSizeInWords * 4);=0D +=0D + // Start writing from the first address at the start of the block=0D + WordAddress =3D BlockAddress;=0D +=0D + NorFlashLock (&OriginalTPL);=0D +=0D + Status =3D NorFlashUnlockAndEraseSingleBlock (Instance, BlockAddress);=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_ERROR, "WriteSingleBlock: ERROR - Failed to Unlock and E= rase the single block at 0x%X\n", BlockAddress));=0D + goto EXIT;=0D + }=0D +=0D + // To speed up the programming operation, NOR Flash is programmed using = the Buffered Programming method.=0D +=0D + // Check that the address starts at a 32-word boundary, i.e. last 7 bits= must be zero=0D + if ((WordAddress & BOUNDARY_OF_32_WORDS) =3D=3D 0x00) {=0D + // First, break the entire block into buffer-sized chunks.=0D + BuffersInBlock =3D (UINTN)(BlockSizeInWords * 4) / P30_MAX_BUFFER_SIZE= _IN_BYTES;=0D +=0D + // Then feed each buffer chunk to the NOR Flash=0D + // If a buffer does not contain any data, don't write it.=0D + for (BufferIndex =3D 0;=0D + BufferIndex < BuffersInBlock;=0D + BufferIndex++, WordAddress +=3D P30_MAX_BUFFER_SIZE_IN_BYTES, Dat= aBuffer +=3D P30_MAX_BUFFER_SIZE_IN_WORDS=0D + )=0D + {=0D + // Check the buffer to see if it contains any data (not set all 1s).= =0D + for (Cnt =3D 0; Cnt < P30_MAX_BUFFER_SIZE_IN_WORDS; Cnt++) {=0D + if (~DataBuffer[Cnt] !=3D 0 ) {=0D + // Some data found, write the buffer.=0D + Status =3D NorFlashWriteBuffer (=0D + Instance,=0D + WordAddress,=0D + P30_MAX_BUFFER_SIZE_IN_BYTES,=0D + DataBuffer=0D + );=0D + if (EFI_ERROR (Status)) {=0D + goto EXIT;=0D + }=0D +=0D + break;=0D + }=0D + }=0D + }=0D +=0D + // Finally, finish off any remaining words that are less than the maxi= mum size of the buffer=0D + RemainingWords =3D BlockSizeInWords % P30_MAX_BUFFER_SIZE_IN_WORDS;=0D +=0D + if (RemainingWords !=3D 0) {=0D + Status =3D NorFlashWriteBuffer (Instance, WordAddress, (RemainingWor= ds * 4), DataBuffer);=0D + if (EFI_ERROR (Status)) {=0D + goto EXIT;=0D + }=0D + }=0D + } else {=0D + // For now, use the single word programming algorithm=0D + // It is unlikely that the NOR Flash will exist in an address which fa= lls within a 32 word boundary range,=0D + // i.e. which ends in the range 0x......01 - 0x......7F.=0D + for (WordIndex =3D 0; WordIndex < BlockSizeInWords; WordIndex++, DataB= uffer++, WordAddress =3D WordAddress + 4) {=0D + Status =3D NorFlashWriteSingleWord (Instance, WordAddress, *DataBuff= er);=0D + if (EFI_ERROR (Status)) {=0D + goto EXIT;=0D + }=0D + }=0D + }=0D +=0D +EXIT:=0D + NorFlashUnlock (OriginalTPL);=0D +=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_ERROR, "NOR FLASH Programming [WriteSingleBlock] failed = at address 0x%08x. Exit Status =3D \"%r\".\n", WordAddress, Status));=0D + }=0D +=0D + return Status;=0D +}=0D diff --git a/Platform/ARM/Drivers/NorFlashDxe/NorFlashDxe.c b/Platform/ARM/= Drivers/NorFlashDxe/NorFlashDxe.c index b1e01169c24e..4bad6e9b2a6b 100644 --- a/Platform/ARM/Drivers/NorFlashDxe/NorFlashDxe.c +++ b/Platform/ARM/Drivers/NorFlashDxe/NorFlashDxe.c @@ -174,161 +174,6 @@ NorFlashCreateInstance ( return Status;=0D }=0D =0D -/**=0D - * This function unlock and erase an entire NOR Flash block.=0D - **/=0D -EFI_STATUS=0D -NorFlashUnlockAndEraseSingleBlock (=0D - IN NOR_FLASH_INSTANCE *Instance,=0D - IN UINTN BlockAddress=0D - )=0D -{=0D - EFI_STATUS Status;=0D - UINTN Index;=0D - EFI_TPL OriginalTPL;=0D -=0D - if (!EfiAtRuntime ()) {=0D - // Raise TPL to TPL_HIGH to stop anyone from interrupting us.=0D - OriginalTPL =3D gBS->RaiseTPL (TPL_HIGH_LEVEL);=0D - } else {=0D - // This initialization is only to prevent the compiler to complain abo= ut the=0D - // use of uninitialized variables=0D - OriginalTPL =3D TPL_HIGH_LEVEL;=0D - }=0D -=0D - Index =3D 0;=0D - // The block erase might fail a first time (SW bug ?). Retry it ...=0D - do {=0D - // Unlock the block if we have to=0D - Status =3D NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddres= s);=0D - if (EFI_ERROR (Status)) {=0D - break;=0D - }=0D -=0D - Status =3D NorFlashEraseSingleBlock (Instance, BlockAddress);=0D - Index++;=0D - } while ((Index < NOR_FLASH_ERASE_RETRY) && (Status =3D=3D EFI_WRITE_PRO= TECTED));=0D -=0D - if (Index =3D=3D NOR_FLASH_ERASE_RETRY) {=0D - DEBUG ((DEBUG_ERROR, "EraseSingleBlock(BlockAddress=3D0x%08x: Block Lo= cked Error (try to erase %d times)\n", BlockAddress, Index));=0D - }=0D -=0D - if (!EfiAtRuntime ()) {=0D - // Interruptions can resume.=0D - gBS->RestoreTPL (OriginalTPL);=0D - }=0D -=0D - return Status;=0D -}=0D -=0D -EFI_STATUS=0D -NorFlashWriteFullBlock (=0D - IN NOR_FLASH_INSTANCE *Instance,=0D - IN EFI_LBA Lba,=0D - IN UINT32 *DataBuffer,=0D - IN UINT32 BlockSizeInWords=0D - )=0D -{=0D - EFI_STATUS Status;=0D - UINTN WordAddress;=0D - UINT32 WordIndex;=0D - UINTN BufferIndex;=0D - UINTN BlockAddress;=0D - UINTN BuffersInBlock;=0D - UINTN RemainingWords;=0D - EFI_TPL OriginalTPL;=0D - UINTN Cnt;=0D -=0D - Status =3D EFI_SUCCESS;=0D -=0D - // Get the physical address of the block=0D - BlockAddress =3D GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba= , BlockSizeInWords * 4);=0D -=0D - // Start writing from the first address at the start of the block=0D - WordAddress =3D BlockAddress;=0D -=0D - if (!EfiAtRuntime ()) {=0D - // Raise TPL to TPL_HIGH to stop anyone from interrupting us.=0D - OriginalTPL =3D gBS->RaiseTPL (TPL_HIGH_LEVEL);=0D - } else {=0D - // This initialization is only to prevent the compiler to complain abo= ut the=0D - // use of uninitialized variables=0D - OriginalTPL =3D TPL_HIGH_LEVEL;=0D - }=0D -=0D - Status =3D NorFlashUnlockAndEraseSingleBlock (Instance, BlockAddress);=0D - if (EFI_ERROR (Status)) {=0D - DEBUG ((DEBUG_ERROR, "WriteSingleBlock: ERROR - Failed to Unlock and E= rase the single block at 0x%X\n", BlockAddress));=0D - goto EXIT;=0D - }=0D -=0D - // To speed up the programming operation, NOR Flash is programmed using = the Buffered Programming method.=0D -=0D - // Check that the address starts at a 32-word boundary, i.e. last 7 bits= must be zero=0D - if ((WordAddress & BOUNDARY_OF_32_WORDS) =3D=3D 0x00) {=0D - // First, break the entire block into buffer-sized chunks.=0D - BuffersInBlock =3D (UINTN)(BlockSizeInWords * 4) / P30_MAX_BUFFER_SIZE= _IN_BYTES;=0D -=0D - // Then feed each buffer chunk to the NOR Flash=0D - // If a buffer does not contain any data, don't write it.=0D - for (BufferIndex =3D 0;=0D - BufferIndex < BuffersInBlock;=0D - BufferIndex++, WordAddress +=3D P30_MAX_BUFFER_SIZE_IN_BYTES, Dat= aBuffer +=3D P30_MAX_BUFFER_SIZE_IN_WORDS=0D - )=0D - {=0D - // Check the buffer to see if it contains any data (not set all 1s).= =0D - for (Cnt =3D 0; Cnt < P30_MAX_BUFFER_SIZE_IN_WORDS; Cnt++) {=0D - if (~DataBuffer[Cnt] !=3D 0 ) {=0D - // Some data found, write the buffer.=0D - Status =3D NorFlashWriteBuffer (=0D - Instance,=0D - WordAddress,=0D - P30_MAX_BUFFER_SIZE_IN_BYTES,=0D - DataBuffer=0D - );=0D - if (EFI_ERROR (Status)) {=0D - goto EXIT;=0D - }=0D -=0D - break;=0D - }=0D - }=0D - }=0D -=0D - // Finally, finish off any remaining words that are less than the maxi= mum size of the buffer=0D - RemainingWords =3D BlockSizeInWords % P30_MAX_BUFFER_SIZE_IN_WORDS;=0D -=0D - if (RemainingWords !=3D 0) {=0D - Status =3D NorFlashWriteBuffer (Instance, WordAddress, (RemainingWor= ds * 4), DataBuffer);=0D - if (EFI_ERROR (Status)) {=0D - goto EXIT;=0D - }=0D - }=0D - } else {=0D - // For now, use the single word programming algorithm=0D - // It is unlikely that the NOR Flash will exist in an address which fa= lls within a 32 word boundary range,=0D - // i.e. which ends in the range 0x......01 - 0x......7F.=0D - for (WordIndex =3D 0; WordIndex < BlockSizeInWords; WordIndex++, DataB= uffer++, WordAddress =3D WordAddress + 4) {=0D - Status =3D NorFlashWriteSingleWord (Instance, WordAddress, *DataBuff= er);=0D - if (EFI_ERROR (Status)) {=0D - goto EXIT;=0D - }=0D - }=0D - }=0D -=0D -EXIT:=0D - if (!EfiAtRuntime ()) {=0D - // Interruptions can resume.=0D - gBS->RestoreTPL (OriginalTPL);=0D - }=0D -=0D - if (EFI_ERROR (Status)) {=0D - DEBUG ((DEBUG_ERROR, "NOR FLASH Programming [WriteSingleBlock] failed = at address 0x%08x. Exit Status =3D \"%r\".\n", WordAddress, Status));=0D - }=0D -=0D - return Status;=0D -}=0D -=0D EFI_STATUS=0D EFIAPI=0D NorFlashInitialise (=0D @@ -549,3 +394,41 @@ NorFlashVirtualNotifyEvent ( =0D return;=0D }=0D +=0D +/**=0D + Lock all pending read/write to Nor flash device=0D +=0D + @param[in] *OriginalTPL Pointer to Nor flash device Original TPL= .=0D +**/=0D +VOID=0D +EFIAPI=0D +NorFlashLock (=0D + IN EFI_TPL *OriginalTPL=0D + )=0D +{=0D + if (!EfiAtRuntime ()) {=0D + // Raise TPL to TPL_HIGH to stop anyone from interrupting us.=0D + *OriginalTPL =3D gBS->RaiseTPL (TPL_HIGH_LEVEL);=0D + } else {=0D + // This initialization is only to prevent the compiler to complain abo= ut the=0D + // use of uninitialized variables=0D + *OriginalTPL =3D TPL_HIGH_LEVEL;=0D + }=0D +}=0D +=0D +/**=0D + Unlock all pending read/write to Nor flash device=0D +=0D + @param[in] OriginalTPL Nor flash device Original TPL.=0D +**/=0D +VOID=0D +EFIAPI=0D +NorFlashUnlock (=0D + IN EFI_TPL OriginalTPL=0D + )=0D +{=0D + if (!EfiAtRuntime ()) {=0D + // Interruptions can resume.=0D + gBS->RestoreTPL (OriginalTPL);=0D + }=0D +}=0D diff --git a/Platform/ARM/Drivers/NorFlashDxe/NorFlashStandaloneMm.c b/Plat= form/ARM/Drivers/NorFlashDxe/NorFlashStandaloneMm.c index f2919265139b..5bff524e5e18 100644 --- a/Platform/ARM/Drivers/NorFlashDxe/NorFlashStandaloneMm.c +++ b/Platform/ARM/Drivers/NorFlashDxe/NorFlashStandaloneMm.c @@ -153,131 +153,6 @@ NorFlashCreateInstance ( return Status;=0D }=0D =0D -/**=0D - * This function unlock and erase an entire NOR Flash block.=0D - **/=0D -EFI_STATUS=0D -NorFlashUnlockAndEraseSingleBlock (=0D - IN NOR_FLASH_INSTANCE *Instance,=0D - IN UINTN BlockAddress=0D - )=0D -{=0D - EFI_STATUS Status;=0D - UINTN Index;=0D -=0D - Index =3D 0;=0D - // The block erase might fail a first time (SW bug ?). Retry it ...=0D - do {=0D - // Unlock the block if we have to=0D - Status =3D NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddres= s);=0D - if (EFI_ERROR (Status)) {=0D - break;=0D - }=0D -=0D - Status =3D NorFlashEraseSingleBlock (Instance, BlockAddress);=0D - Index++;=0D - } while ((Index < NOR_FLASH_ERASE_RETRY) && (Status =3D=3D EFI_WRITE_PRO= TECTED));=0D -=0D - if (Index =3D=3D NOR_FLASH_ERASE_RETRY) {=0D - DEBUG ((DEBUG_ERROR, "EraseSingleBlock(BlockAddress=3D0x%08x: Block Lo= cked Error (try to erase %d times)\n", BlockAddress, Index));=0D - }=0D -=0D - return Status;=0D -}=0D -=0D -EFI_STATUS=0D -NorFlashWriteFullBlock (=0D - IN NOR_FLASH_INSTANCE *Instance,=0D - IN EFI_LBA Lba,=0D - IN UINT32 *DataBuffer,=0D - IN UINT32 BlockSizeInWords=0D - )=0D -{=0D - EFI_STATUS Status;=0D - UINTN WordAddress;=0D - UINT32 WordIndex;=0D - UINTN BufferIndex;=0D - UINTN BlockAddress;=0D - UINTN BuffersInBlock;=0D - UINTN RemainingWords;=0D - UINTN Cnt;=0D -=0D - Status =3D EFI_SUCCESS;=0D -=0D - // Get the physical address of the block=0D - BlockAddress =3D GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba= , BlockSizeInWords * 4);=0D -=0D - // Start writing from the first address at the start of the block=0D - WordAddress =3D BlockAddress;=0D -=0D - Status =3D NorFlashUnlockAndEraseSingleBlock (Instance, BlockAddress);=0D - if (EFI_ERROR (Status)) {=0D - DEBUG ((DEBUG_ERROR, "WriteSingleBlock: ERROR - Failed to Unlock and E= rase the single block at 0x%X\n", BlockAddress));=0D - goto EXIT;=0D - }=0D -=0D - // To speed up the programming operation, NOR Flash is programmed using = the Buffered Programming method.=0D -=0D - // Check that the address starts at a 32-word boundary, i.e. last 7 bits= must be zero=0D - if ((WordAddress & BOUNDARY_OF_32_WORDS) =3D=3D 0x00) {=0D - // First, break the entire block into buffer-sized chunks.=0D - BuffersInBlock =3D (UINTN)(BlockSizeInWords * 4) / P30_MAX_BUFFER_SIZE= _IN_BYTES;=0D -=0D - // Then feed each buffer chunk to the NOR Flash=0D - // If a buffer does not contain any data, don't write it.=0D - for (BufferIndex =3D 0;=0D - BufferIndex < BuffersInBlock;=0D - BufferIndex++, WordAddress +=3D P30_MAX_BUFFER_SIZE_IN_BYTES, Dat= aBuffer +=3D P30_MAX_BUFFER_SIZE_IN_WORDS=0D - )=0D - {=0D - // Check the buffer to see if it contains any data (not set all 1s).= =0D - for (Cnt =3D 0; Cnt < P30_MAX_BUFFER_SIZE_IN_WORDS; Cnt++) {=0D - if (~DataBuffer[Cnt] !=3D 0 ) {=0D - // Some data found, write the buffer.=0D - Status =3D NorFlashWriteBuffer (=0D - Instance,=0D - WordAddress,=0D - P30_MAX_BUFFER_SIZE_IN_BYTES,=0D - DataBuffer=0D - );=0D - if (EFI_ERROR (Status)) {=0D - goto EXIT;=0D - }=0D -=0D - break;=0D - }=0D - }=0D - }=0D -=0D - // Finally, finish off any remaining words that are less than the maxi= mum size of the buffer=0D - RemainingWords =3D BlockSizeInWords % P30_MAX_BUFFER_SIZE_IN_WORDS;=0D -=0D - if (RemainingWords !=3D 0) {=0D - Status =3D NorFlashWriteBuffer (Instance, WordAddress, (RemainingWor= ds * 4), DataBuffer);=0D - if (EFI_ERROR (Status)) {=0D - goto EXIT;=0D - }=0D - }=0D - } else {=0D - // For now, use the single word programming algorithm=0D - // It is unlikely that the NOR Flash will exist in an address which fa= lls within a 32 word boundary range,=0D - // i.e. which ends in the range 0x......01 - 0x......7F.=0D - for (WordIndex =3D 0; WordIndex < BlockSizeInWords; WordIndex++, DataB= uffer++, WordAddress =3D WordAddress + 4) {=0D - Status =3D NorFlashWriteSingleWord (Instance, WordAddress, *DataBuff= er);=0D - if (EFI_ERROR (Status)) {=0D - goto EXIT;=0D - }=0D - }=0D - }=0D -=0D -EXIT:=0D - if (EFI_ERROR (Status)) {=0D - DEBUG ((DEBUG_ERROR, "NOR FLASH Programming [WriteSingleBlock] failed = at address 0x%08x. Exit Status =3D \"%r\".\n", WordAddress, Status));=0D - }=0D -=0D - return Status;=0D -}=0D -=0D EFI_STATUS=0D EFIAPI=0D NorFlashInitialise (=0D @@ -382,3 +257,29 @@ NorFlashFvbInitialize ( =0D return Status;=0D }=0D +=0D +/**=0D + Lock all pending read/write to Nor flash device=0D +=0D + @param[in] OriginalTPL Nor flash device Original TPL.=0D +**/=0D +VOID=0D +EFIAPI=0D +NorFlashLock (=0D + IN EFI_TPL *OriginalTPL=0D + )=0D +{=0D +}=0D +=0D +/**=0D + Unlock all pending read/write to Nor flash device=0D +=0D + @param[in] OriginalTPL Nor flash device Original TPL.=0D +**/=0D +VOID=0D +EFIAPI=0D +NorFlashUnlock (=0D + IN EFI_TPL OriginalTPL=0D + )=0D +{=0D +}=0D --=20 2.25.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#118154): https://edk2.groups.io/g/devel/message/118154 Mute This Topic: https://groups.io/mt/105690939/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-