UINTN BuffersInBlock;\r
UINTN RemainingWords;\r
EFI_TPL OriginalTPL;\r
+ UINTN Cnt;\r
\r
Status = EFI_SUCCESS;\r
\r
BuffersInBlock = (UINTN)(BlockSizeInWords * 4) / P30_MAX_BUFFER_SIZE_IN_BYTES;\r
\r
// Then feed each buffer chunk to the NOR Flash\r
+ // If a buffer does not contain any data, don't write it.\r
for(BufferIndex=0;\r
BufferIndex < BuffersInBlock;\r
BufferIndex++, WordAddress += P30_MAX_BUFFER_SIZE_IN_BYTES, DataBuffer += P30_MAX_BUFFER_SIZE_IN_WORDS\r
) {\r
- Status = NorFlashWriteBuffer (Instance, WordAddress, P30_MAX_BUFFER_SIZE_IN_BYTES, DataBuffer);\r
- if (EFI_ERROR(Status)) {\r
- goto EXIT;\r
+ // Check the buffer to see if it contains any data (not set all 1s).\r
+ for (Cnt = 0; Cnt < P30_MAX_BUFFER_SIZE_IN_WORDS; Cnt++) {\r
+ if (~DataBuffer[Cnt] != 0 ) {\r
+ // Some data found, write the buffer.\r
+ Status = NorFlashWriteBuffer (Instance, WordAddress, P30_MAX_BUFFER_SIZE_IN_BYTES,\r
+ DataBuffer);\r
+ if (EFI_ERROR(Status)) {\r
+ goto EXIT;\r
+ }\r
+ break;\r
+ }\r
}\r
}\r
\r
return EFI_SUCCESS;\r
}\r
\r
+EFI_STATUS\r
+NorFlashRead (\r
+ IN NOR_FLASH_INSTANCE *Instance,\r
+ IN EFI_LBA Lba,\r
+ IN UINTN Offset,\r
+ IN UINTN BufferSizeInBytes,\r
+ OUT VOID *Buffer\r
+ )\r
+{\r
+ UINT32 NumBlocks;\r
+ UINTN StartAddress;\r
+\r
+ // The buffer must be valid\r
+ if (Buffer == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ // Return if we have not any byte to read\r
+ if (BufferSizeInBytes == 0) {\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+ // All blocks must be within the device\r
+ NumBlocks = ((UINT32)BufferSizeInBytes) / Instance->Media.BlockSize ;\r
+\r
+ if ((Lba + NumBlocks) > (Instance->Media.LastBlock + 1)) {\r
+ DEBUG ((EFI_D_ERROR, "NorFlashRead: ERROR - Read will exceed last block\n"));\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (Offset + BufferSizeInBytes >= Instance->Size) {\r
+ DEBUG ((EFI_D_ERROR, "NorFlashRead: ERROR - Read will exceed device size.\n"));\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ // Get the address to start reading from\r
+ StartAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress,\r
+ Lba,\r
+ Instance->Media.BlockSize\r
+ );\r
+\r
+ // Put the device into Read Array mode\r
+ SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);\r
+\r
+ // Readout the data\r
+ CopyMem (Buffer, (UINTN *)(StartAddress + Offset), BufferSizeInBytes);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
EFI_STATUS\r
NorFlashReset (\r
IN NOR_FLASH_INSTANCE *Instance\r
IN OUT UINT8 *Buffer\r
)\r
{\r
- EFI_STATUS Status;\r
EFI_STATUS TempStatus;\r
UINTN BlockSize;\r
NOR_FLASH_INSTANCE *Instance;\r
Instance->Initialize(Instance);\r
}\r
\r
- Status = EFI_SUCCESS;\r
- TempStatus = Status;\r
+ TempStatus = EFI_SUCCESS;\r
\r
// Cache the block size to avoid de-referencing pointers all the time\r
BlockSize = Instance->Media.BlockSize;\r
return EFI_BAD_BUFFER_SIZE;\r
}\r
\r
- // Check we did get some memory\r
- if (Instance->FvbBuffer == NULL) {\r
- DEBUG ((EFI_D_ERROR, "FvbRead: ERROR - Buffer not ready\n"));\r
- return EFI_DEVICE_ERROR;\r
- }\r
-\r
- // Read NOR Flash data into shadow buffer\r
- TempStatus = NorFlashReadBlocks (Instance, Instance->StartLba + Lba, BlockSize, Instance->FvbBuffer);\r
- if (EFI_ERROR (TempStatus)) {\r
- // Return one of the pre-approved error statuses\r
- return EFI_DEVICE_ERROR;\r
+ // Decide if we are doing full block reads or not.\r
+ if (*NumBytes % BlockSize != 0) {\r
+ TempStatus = NorFlashRead (Instance, Instance->StartLba + Lba, Offset, *NumBytes, Buffer);\r
+ if (EFI_ERROR (TempStatus)) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+ } else {\r
+ // Read NOR Flash data into shadow buffer\r
+ TempStatus = NorFlashReadBlocks (Instance, Instance->StartLba + Lba, BlockSize, Buffer);\r
+ if (EFI_ERROR (TempStatus)) {\r
+ // Return one of the pre-approved error statuses\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
}\r
-\r
- // Put the data at the appropriate location inside the buffer area\r
- DEBUG ((DEBUG_BLKIO, "FvbRead: CopyMem( Dst=0x%08x, Src=0x%08x, Size=0x%x ).\n", Buffer, (UINTN)Instance->FvbBuffer + Offset, *NumBytes));\r
-\r
- CopyMem (Buffer, (VOID*)((UINTN)Instance->FvbBuffer + Offset), *NumBytes);\r
-\r
- return Status;\r
+ return EFI_SUCCESS;\r
}\r
\r
/**\r
IN UINT8 *Buffer\r
)\r
{\r
- EFI_STATUS Status;\r
EFI_STATUS TempStatus;\r
+ UINT32 Tmp;\r
+ UINT32 TmpBuf;\r
+ UINT32 WordToWrite;\r
+ UINT32 Mask;\r
+ UINTN DoErase;\r
+ UINTN BytesToWrite;\r
+ UINTN CurOffset;\r
+ UINTN WordAddr;\r
UINTN BlockSize;\r
NOR_FLASH_INSTANCE *Instance;\r
+ UINTN BlockAddress;\r
+ UINTN PrevBlockAddress;\r
+\r
+ PrevBlockAddress = 0;\r
\r
Instance = INSTANCE_FROM_FVB_THIS(This);\r
\r
\r
DEBUG ((DEBUG_BLKIO, "FvbWrite(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Instance->StartLba + Lba, Offset, *NumBytes, Buffer));\r
\r
- Status = EFI_SUCCESS;\r
- TempStatus = Status;\r
-\r
// Detect WriteDisabled state\r
if (Instance->Media.ReadOnly == TRUE) {\r
DEBUG ((EFI_D_ERROR, "FvbWrite: ERROR - Can not write: Device is in WriteDisabled state.\n"));\r
return EFI_BAD_BUFFER_SIZE;\r
}\r
\r
- // Check we did get some memory\r
+ // Pick 128bytes as a good start for word operations as opposed to erasing the\r
+ // block and writing the data regardless if an erase is really needed.\r
+ // It looks like most individual NV variable writes are smaller than 128bytes.\r
+ if (*NumBytes <= 128) {\r
+ // Check to see if we need to erase before programming the data into NOR.\r
+ // If the destination bits are only changing from 1s to 0s we can just write.\r
+ // After a block is erased all bits in the block is set to 1.\r
+ // If any byte requires us to erase we just give up and rewrite all of it.\r
+ DoErase = 0;\r
+ BytesToWrite = *NumBytes;\r
+ CurOffset = Offset;\r
+\r
+ while (BytesToWrite > 0) {\r
+ // Read full word from NOR, splice as required. A word is the smallest\r
+ // unit we can write.\r
+ TempStatus = NorFlashRead (Instance, Instance->StartLba + Lba,\r
+ CurOffset & ~(0x3), sizeof(Tmp), &Tmp);\r
+ if (EFI_ERROR (TempStatus)) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
+ // Physical address of word in NOR to write.\r
+ WordAddr = (CurOffset & ~(0x3)) + GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress,\r
+ Lba, BlockSize);\r
+ // The word of data that is to be written.\r
+ TmpBuf = *((UINT32*)(Buffer + (*NumBytes - BytesToWrite)));\r
+\r
+ // First do word aligned chunks.\r
+ if ((CurOffset & 0x3) == 0) {\r
+ if (BytesToWrite >= 4) {\r
+ // Is the destination still in 'erased' state?\r
+ if (~Tmp != 0) {\r
+ // Check to see if we are only changing bits to zero.\r
+ if ((Tmp ^ TmpBuf) & TmpBuf) {\r
+ DoErase = 1;\r
+ break;\r
+ }\r
+ }\r
+ // Write this word to NOR\r
+ WordToWrite = TmpBuf;\r
+ CurOffset += sizeof(TmpBuf);\r
+ BytesToWrite -= sizeof(TmpBuf);\r
+ } else {\r
+ // BytesToWrite < 4. Do small writes and left-overs\r
+ Mask = ~((~0) << (BytesToWrite * 8));\r
+ // Mask out the bytes we want.\r
+ TmpBuf &= Mask;\r
+ // Is the destination still in 'erased' state?\r
+ if ((Tmp & Mask) != Mask) {\r
+ // Check to see if we are only changing bits to zero.\r
+ if ((Tmp ^ TmpBuf) & TmpBuf) {\r
+ DoErase = 1;\r
+ break;\r
+ }\r
+ }\r
+ // Merge old and new data. Write merged word to NOR\r
+ WordToWrite = (Tmp & ~Mask) | TmpBuf;\r
+ CurOffset += BytesToWrite;\r
+ BytesToWrite = 0;\r
+ }\r
+ } else {\r
+ // Do multiple words, but starting unaligned.\r
+ if (BytesToWrite > (4 - (CurOffset & 0x3))) {\r
+ Mask = ~((~0) << ((CurOffset & 0x3) * 8));\r
+ // Mask out the bytes we want.\r
+ TmpBuf = (TmpBuf << ((CurOffset & 0x3) * 8)) & Mask;\r
+ // Is the destination still in 'erased' state?\r
+ if ((Tmp & Mask) != Mask) {\r
+ // Check to see if we are only changing bits to zero.\r
+ if ((Tmp ^ TmpBuf) & TmpBuf) {\r
+ DoErase = 1;\r
+ break;\r
+ }\r
+ }\r
+ // Merge old and new data. Write merged word to NOR\r
+ WordToWrite = (Tmp & ~Mask) | TmpBuf;\r
+ BytesToWrite -= (4 - (CurOffset & 0x3));\r
+ CurOffset += (4 - (CurOffset & 0x3));\r
+ } else {\r
+ // Unaligned and fits in one word.\r
+ Mask = (~((~0) << (BytesToWrite * 8))) << ((CurOffset & 0x3) * 8);\r
+ // Mask out the bytes we want.\r
+ TmpBuf = (TmpBuf << ((CurOffset & 0x3) * 8)) & Mask;\r
+ // Is the destination still in 'erased' state?\r
+ if ((Tmp & Mask) != Mask) {\r
+ // Check to see if we are only changing bits to zero.\r
+ if ((Tmp ^ TmpBuf) & TmpBuf) {\r
+ DoErase = 1;\r
+ break;\r
+ }\r
+ }\r
+ // Merge old and new data. Write merged word to NOR\r
+ WordToWrite = (Tmp & ~Mask) | TmpBuf;\r
+ CurOffset += BytesToWrite;\r
+ BytesToWrite = 0;\r
+ }\r
+ }\r
+\r
+ //\r
+ // Write the word to NOR.\r
+ //\r
+\r
+ BlockAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba, BlockSize);\r
+ if (BlockAddress != PrevBlockAddress) {\r
+ TempStatus = NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddress);\r
+ if (EFI_ERROR (TempStatus)) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+ PrevBlockAddress = BlockAddress;\r
+ }\r
+ TempStatus = NorFlashWriteSingleWord (Instance, WordAddr, WordToWrite);\r
+ if (EFI_ERROR (TempStatus)) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+ }\r
+ // Exit if we got here and could write all the data. Otherwise do the\r
+ // Erase-Write cycle.\r
+ if (!DoErase) {\r
+ return EFI_SUCCESS;\r
+ }\r
+ }\r
+\r
+ // Check we did get some memory. Buffer is BlockSize.\r
if (Instance->FvbBuffer == NULL) {\r
DEBUG ((EFI_D_ERROR, "FvbWrite: ERROR - Buffer not ready\n"));\r
return EFI_DEVICE_ERROR;\r
return EFI_DEVICE_ERROR;\r
}\r
\r
- return Status;\r
+ return EFI_SUCCESS;\r
}\r
\r
/**\r