39 IN UINT8 AddressBytesSupported,
40 IN BOOLEAN UseAddress,
47 UINT32 BigEndianAddress;
49 UINT8 SfdpAddressBytes;
51 SfdpAddressBytes = (UINT8)Instance->SfdpBasicFlash->AddressBytes;
54 Instance->SpiTransactionWriteBuffer[0] = Opcode;
58 if (SfdpAddressBytes != 0) {
61 DEBUG ((DEBUG_ERROR,
"%a: Unsupported Address Bytes: 0x%x, SFDP is: 0x%x\n", __func__, AddressBytesSupported, SfdpAddressBytes));
67 }
else if (AddressBytesSupported == SPI_ADDR_4BYTE_ONLY) {
68 if (SfdpAddressBytes != 0) {
70 if ((SfdpAddressBytes != SPI_ADDR_4BYTE_ONLY) && (SfdpAddressBytes != SPI_ADDR_3OR4BYTE)) {
71 DEBUG ((DEBUG_ERROR,
"%a: Unsupported Address Bytes: 0x%x, SFDP is: 0x%x\n", __func__, AddressBytesSupported, SfdpAddressBytes));
77 }
else if (AddressBytesSupported == SPI_ADDR_3OR4BYTE) {
78 if (SfdpAddressBytes != 0) {
80 if (SfdpAddressBytes != SPI_ADDR_3OR4BYTE) {
81 DEBUG ((DEBUG_ERROR,
"%a: Unsupported Address Bytes: 0x%x, SFDP is: 0x%x\n", __func__, AddressBytesSupported, SfdpAddressBytes));
86 if (Instance->Protocol.FlashSize <= SIZE_16MB) {
93 DEBUG ((DEBUG_ERROR,
"%a: Invalid Address Bytes\n", __func__));
98 BigEndianAddress >>= ((
sizeof (UINT32) - AddressSize) * 8);
100 &Instance->SpiTransactionWriteBuffer[Index],
104 Index += AddressSize;
107 if (SfdpAddressBytes == SPI_ADDR_3OR4BYTE) {
116 if (DummyBytes != 0) {
118 &Instance->SpiTransactionWriteBuffer[Index],
126 if (WriteBytes > 0) {
128 &Instance->SpiTransactionWriteBuffer[Index],
155 IN UINT32 LengthInBytes,
156 OUT UINT8 *FlashStatus
160 UINT32 TransactionBufferLength;
166 SPI_FLASH_RDSR_DUMMY,
167 SPI_FLASH_RDSR_ADDR_BYTES,
173 Status = Instance->SpiIo->Transaction (
180 TransactionBufferLength,
181 Instance->SpiTransactionWriteBuffer,
203 UINT32 TransactionBufferLength;
207 Instance->WriteEnableLatchCommand,
208 SPI_FLASH_WREN_DUMMY,
209 SPI_FLASH_WREN_ADDR_BYTES,
215 Status = Instance->SpiIo->Transaction (
222 TransactionBufferLength,
223 Instance->SpiTransactionWriteBuffer,
227 if (EFI_ERROR (Status)) {
228 DEBUG ((DEBUG_ERROR,
"%a: Set WEL fail.\n", __func__));
254 UINT32 AlreadyDelayedInMicroseconds;
260 if (RetryCount == 0) {
265 AlreadyDelayedInMicroseconds = 0;
266 while (AlreadyDelayedInMicroseconds < Timeout) {
268 if (EFI_ERROR (Status)) {
269 DEBUG ((DEBUG_ERROR,
"%a: Read status error\n", __func__));
274 if ((DeviceStatus & SPI_FLASH_SR_WIP) == SPI_FLASH_SR_NOT_WIP) {
279 AlreadyDelayedInMicroseconds +=
FixedPcdGet32 (PcdSpiNorFlashOperationDelayMicroseconds);
283 }
while (RetryCount > 0);
285 DEBUG ((DEBUG_ERROR,
"%a: Timeout error\n", __func__));
286 return EFI_DEVICE_ERROR;
309 UINT32 AlreadyDelayedInMicroseconds;
315 if (RetryCount == 0) {
320 AlreadyDelayedInMicroseconds = 0;
321 while (AlreadyDelayedInMicroseconds < Timeout) {
323 if (EFI_ERROR (Status)) {
324 DEBUG ((DEBUG_ERROR,
"%a: Fail to read WEL.\n", __func__));
329 if ((DeviceStatus & (SPI_FLASH_SR_WIP | SPI_FLASH_SR_WEL)) == SPI_FLASH_SR_WEL) {
334 AlreadyDelayedInMicroseconds +=
FixedPcdGet32 (PcdSpiNorFlashOperationDelayMicroseconds);
338 }
while (RetryCount > 0);
340 DEBUG ((DEBUG_ERROR,
"%a: Timeout error\n", __func__));
341 return EFI_DEVICE_ERROR;
364 UINT32 AlreadyDelayedInMicroseconds;
370 if (RetryCount == 0) {
375 AlreadyDelayedInMicroseconds = 0;
376 while (AlreadyDelayedInMicroseconds < Timeout) {
379 if (EFI_ERROR (Status) ||
380 ((DeviceStatus & (SPI_FLASH_SR_WIP | SPI_FLASH_SR_WEL)) == SPI_FLASH_SR_NOT_WIP))
386 AlreadyDelayedInMicroseconds +=
FixedPcdGet32 (PcdSpiNorFlashOperationDelayMicroseconds);
390 }
while (RetryCount > 0);
392 DEBUG ((DEBUG_ERROR,
"SpiNorFlash:%a: Timeout error\n", __func__));
393 return EFI_DEVICE_ERROR;
422 UINT32 TransactionBufferLength;
424 DEBUG ((DEBUG_INFO,
"%a: Entry\n", __func__));
426 if (Buffer ==
NULL) {
427 return EFI_INVALID_PARAMETER;
430 Instance = SPI_NOR_FLASH_FROM_THIS (This);
435 if (!EFI_ERROR (Status)) {
439 SPI_FLASH_RDID_DUMMY,
440 SPI_FLASH_RDID_ADDR_BYTES,
453 TransactionBufferLength,
454 Instance->SpiTransactionWriteBuffer,
486 IN UINT32 FlashAddress,
487 IN UINT32 LengthInBytes,
494 UINT32 CurrentAddress;
495 UINT8 *CurrentBuffer;
497 UINT32 TransactionBufferLength;
498 UINT32 MaximumTransferBytes;
500 DEBUG ((DEBUG_INFO,
"%a: Entry\n", __func__));
502 Status = EFI_DEVICE_ERROR;
503 if ((Buffer ==
NULL) ||
504 (FlashAddress >= This->FlashSize) ||
505 (LengthInBytes > This->FlashSize - FlashAddress))
507 return EFI_INVALID_PARAMETER;
510 Instance = SPI_NOR_FLASH_FROM_THIS (This);
513 CurrentBuffer = Buffer;
515 for (ByteCounter = 0; ByteCounter < LengthInBytes;) {
516 CurrentAddress = FlashAddress + ByteCounter;
517 CurrentBuffer = Buffer + ByteCounter;
518 Length = LengthInBytes - ByteCounter;
520 if (Length > MaximumTransferBytes) {
521 Length = MaximumTransferBytes;
526 if (EFI_ERROR (Status)) {
533 SPI_FLASH_READ_DUMMY,
534 SPI_FLASH_READ_ADDR_BYTES,
547 TransactionBufferLength,
548 Instance->SpiTransactionWriteBuffer,
553 ByteCounter += Length;
581 IN UINT32 FlashAddress,
582 IN UINT32 LengthInBytes,
589 UINT32 CurrentAddress;
590 UINT8 *CurrentBuffer;
592 UINT32 TransactionBufferLength;
593 UINT32 MaximumTransferBytes;
594 UINT8 FastReadInstruction;
595 UINT8 FastReadWaitStateDummyClocks;
596 UINT8 FastReadModeClock;
598 DEBUG ((DEBUG_INFO,
"%a: Entry, Read address = 0x%08x, Length = 0x%08x\n", __func__, FlashAddress, LengthInBytes));
600 Status = EFI_DEVICE_ERROR;
601 if ((Buffer ==
NULL) ||
602 (FlashAddress >= This->FlashSize) ||
603 (LengthInBytes > This->FlashSize - FlashAddress))
605 return EFI_INVALID_PARAMETER;
608 Instance = SPI_NOR_FLASH_FROM_THIS (This);
614 FastReadInstruction = SPI_FLASH_FAST_READ;
615 FastReadWaitStateDummyClocks = SPI_FLASH_FAST_READ_DUMMY * 8;
616 FastReadModeClock = 0;
629 &FastReadInstruction,
631 &FastReadWaitStateDummyClocks
633 if (!EFI_ERROR (Status)) {
634 DEBUG ((DEBUG_VERBOSE,
" Use below Fast Read mode:\n"));
636 DEBUG ((DEBUG_VERBOSE,
" Use the default Fast Read mode:\n"));
639 DEBUG ((DEBUG_VERBOSE,
" Instruction : 0x%x\n", FastReadInstruction));
640 DEBUG ((DEBUG_VERBOSE,
" Mode Clock : 0x%x\n", FastReadModeClock));
641 DEBUG ((DEBUG_VERBOSE,
" Wait States (Dummy Clocks) in clock: 0x%x\n", FastReadWaitStateDummyClocks));
642 DEBUG ((DEBUG_VERBOSE,
" Supported erase address bytes by device: 0x%02x.\n", Instance->SfdpBasicFlash->AddressBytes));
643 DEBUG ((DEBUG_VERBOSE,
" (00: 3-Byte, 01: 3 or 4-Byte. 10: 4-Byte)\n"));
645 CurrentBuffer = Buffer;
647 for (ByteCounter = 0; ByteCounter < LengthInBytes;) {
648 CurrentAddress = FlashAddress + ByteCounter;
649 CurrentBuffer = Buffer + ByteCounter;
650 Length = LengthInBytes - ByteCounter;
652 if (Length > MaximumTransferBytes) {
653 Length = MaximumTransferBytes;
658 if (EFI_ERROR (Status)) {
665 FastReadWaitStateDummyClocks / 8,
666 (UINT8)Instance->SfdpBasicFlash->AddressBytes,
679 TransactionBufferLength,
680 Instance->SpiTransactionWriteBuffer,
685 ByteCounter += Length;
709 IN UINT32 LengthInBytes,
710 OUT UINT8 *FlashStatus
716 if (LengthInBytes != 1) {
717 return EFI_INVALID_PARAMETER;
720 Instance = SPI_NOR_FLASH_FROM_THIS (This);
746 IN UINT32 LengthInBytes,
747 IN UINT8 *FlashStatus
752 UINT32 TransactionBufferLength;
754 if (LengthInBytes != 1) {
755 return EFI_INVALID_PARAMETER;
758 Instance = SPI_NOR_FLASH_FROM_THIS (This);
764 if (!EFI_ERROR (Status)) {
766 Status =
SetWel (Instance);
767 DEBUG ((DEBUG_ERROR,
"%a: set Write Enable Error.\n", __func__));
774 if (!EFI_ERROR (Status)) {
778 SPI_FLASH_WRSR_DUMMY,
779 SPI_FLASH_WRSR_ADDR_BYTES,
792 TransactionBufferLength,
793 Instance->SpiTransactionWriteBuffer,
828 IN UINT32 FlashAddress,
829 IN UINT32 LengthInBytes,
836 UINT32 CurrentAddress;
838 UINT32 BytesUntilBoundary;
839 UINT8 *CurrentBuffer;
840 UINT32 TransactionBufferLength;
841 UINT32 MaximumTransferBytes;
842 UINT32 SpiFlashPageSize;
844 DEBUG ((DEBUG_INFO,
"%a: Entry: Write address = 0x%08x, Length = 0x%08x\n", __func__, FlashAddress, LengthInBytes));
846 Status = EFI_DEVICE_ERROR;
847 if ((Buffer ==
NULL) ||
848 (LengthInBytes == 0) ||
849 (FlashAddress >= This->FlashSize) ||
850 (LengthInBytes > This->FlashSize - FlashAddress))
852 return EFI_INVALID_PARAMETER;
855 Instance = SPI_NOR_FLASH_FROM_THIS (This);
857 if (Instance->SfdpBasicFlashByteCount >= 11 * 4) {
859 SpiFlashPageSize = 1 << Instance->SfdpBasicFlash->PageSize;
861 SpiFlashPageSize = 256;
864 CurrentBuffer = Buffer;
866 for (ByteCounter = 0; ByteCounter < LengthInBytes;) {
867 CurrentAddress = FlashAddress + ByteCounter;
868 CurrentBuffer = Buffer + ByteCounter;
869 Length = LengthInBytes - ByteCounter;
871 if (Length > MaximumTransferBytes) {
872 Length = MaximumTransferBytes;
876 BytesUntilBoundary = SpiFlashPageSize
877 - (CurrentAddress % SpiFlashPageSize);
878 if ((BytesUntilBoundary != 0) && (Length > BytesUntilBoundary)) {
879 Length = BytesUntilBoundary;
884 if (EFI_ERROR (Status)) {
890 Status =
SetWel (Instance);
892 if (EFI_ERROR (Status)) {
898 if (EFI_ERROR (Status)) {
908 SPI_FLASH_PP_ADDR_BYTES,
921 TransactionBufferLength,
922 Instance->SpiTransactionWriteBuffer,
927 if (EFI_ERROR (Status)) {
934 if (EFI_ERROR (Status)) {
939 if (EFI_ERROR (Status)) {
944 ByteCounter += Length;
973 IN UINT32 FlashAddress,
983 UINT32 TotalEraseLength;
984 UINT32 CurrentAddress;
985 UINT32 TransactionBufferLength;
986 UINT32 BlockCountToErase;
987 UINT32 BlockSizeToErase;
988 UINT8 BlockEraseCommand;
989 UINT32 TypicalEraseTime;
990 UINT64 MaximumEraseTimeout;
993 DEBUG ((DEBUG_INFO,
"%a: Entry: Erase address = 0x%08x, Block count = 0x%x\n", __func__, FlashAddress, BlockCount));
995 Status = EFI_DEVICE_ERROR;
996 Instance = SPI_NOR_FLASH_FROM_THIS (This);
1000 if (EFI_ERROR (Status)) {
1001 DEBUG ((DEBUG_ERROR,
" Failed to get the flash region of this flash address.\n"));
1006 CurrentAddress = FlashAddress;
1007 BlockCountToErase = BlockCount;
1009 TotalEraseLength = BlockCountToErase * FlashRegion->
SectorSize;
1011 DEBUG ((DEBUG_ERROR,
" The blocks to erase exceeds the region boundary.\n"));
1012 return EFI_INVALID_PARAMETER;
1015 DEBUG ((DEBUG_VERBOSE,
" Region starting address: 0x%08x.\n", FlashRegion->
RegionAddress));
1017 DEBUG ((DEBUG_VERBOSE,
" Region sector size : 0x%08x.\n", FlashRegion->
SectorSize));
1018 DEBUG ((DEBUG_VERBOSE,
" Supported erase address bytes by device: 0x%02x.\n", Instance->SfdpBasicFlash->AddressBytes));
1019 DEBUG ((DEBUG_VERBOSE,
" (00: 3-Byte, 01: 3 or 4-Byte. 10: 4-Byte)\n"));
1023 while (ByteCounter < TotalEraseLength) {
1024 CurrentAddress = FlashAddress + ByteCounter;
1027 if (TotalEraseLength == This->FlashSize) {
1028 Opcode = SPI_FLASH_CE;
1029 Dummy = SPI_FLASH_CE_DUMMY;
1030 EraseLength = TotalEraseLength;
1031 DEBUG ((DEBUG_VERBOSE,
" This is the chip erase.\n"));
1040 TotalEraseLength - ByteCounter,
1045 &MaximumEraseTimeout
1047 if (EFI_ERROR (Status)) {
1048 DEBUG ((DEBUG_ERROR,
" Failed to get erase block attribute.\n"));
1052 Opcode = BlockEraseCommand;
1053 Dummy = SPI_FLASH_BE_DUMMY;
1054 EraseLength = BlockCountToErase * BlockSizeToErase;
1057 " Erase command 0x%02x at adddress 0x%08x for length 0x%08x.\n",
1070 if (EFI_ERROR (Status)) {
1076 Status =
SetWel (Instance);
1077 if (EFI_ERROR (Status)) {
1083 if (EFI_ERROR (Status)) {
1093 (UINT8)Instance->SfdpBasicFlash->AddressBytes,
1106 TransactionBufferLength,
1107 Instance->SpiTransactionWriteBuffer,
1112 if (EFI_ERROR (Status)) {
1115 DEBUG ((DEBUG_VERBOSE,
"Erase command sucessfully.\n"));
1124 if (EFI_ERROR (Status)) {
1131 Status =
WaitNotWip (Instance, (UINT32)MaximumEraseTimeout * 1000,
FixedPcdGet32 (PcdSpiNorFlashOperationRetryCount));
1132 if (EFI_ERROR (Status)) {
1137 ByteCounter += EraseLength;
UINTN EFIAPI MicroSecondDelay(IN UINTN MicroSeconds)
UINT32 EFIAPI SwapBytes32(IN UINT32 Value)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI SetMem(OUT VOID *Buffer, IN UINTN Length, IN UINT8 Value)
#define ASSERT_EFI_ERROR(StatusParameter)
#define DEBUG(Expression)
#define FixedPcdGet32(TokenName)
@ SPI_TRANSACTION_WRITE_ONLY
@ SPI_TRANSACTION_WRITE_THEN_READ
EFI_STATUS EFIAPI ReadStatus(IN CONST EFI_SPI_NOR_FLASH_PROTOCOL *This, IN UINT32 LengthInBytes, OUT UINT8 *FlashStatus)
EFI_STATUS EFIAPI InternalReadStatus(IN SPI_NOR_FLASH_INSTANCE *Instance, IN UINT32 LengthInBytes, OUT UINT8 *FlashStatus)
EFI_STATUS WaitNotWip(IN SPI_NOR_FLASH_INSTANCE *SpiNorFlashInstance, IN UINT32 Timeout, IN UINT32 RetryCount)
EFI_STATUS EFIAPI WriteStatus(IN CONST EFI_SPI_NOR_FLASH_PROTOCOL *This, IN UINT32 LengthInBytes, IN UINT8 *FlashStatus)
EFI_STATUS WaitNotWelNotWip(IN SPI_NOR_FLASH_INSTANCE *SpiNorFlashInstance, IN UINT32 Timeout, IN UINT32 RetryCount)
EFI_STATUS EFIAPI LfReadData(IN CONST EFI_SPI_NOR_FLASH_PROTOCOL *This, IN UINT32 FlashAddress, IN UINT32 LengthInBytes, OUT UINT8 *Buffer)
EFI_STATUS EFIAPI GetFlashId(IN CONST EFI_SPI_NOR_FLASH_PROTOCOL *This, OUT UINT8 *Buffer)
EFI_STATUS EFIAPI Erase(IN CONST EFI_SPI_NOR_FLASH_PROTOCOL *This, IN UINT32 FlashAddress, IN UINT32 BlockCount)
EFI_STATUS WaitWelNotWip(IN SPI_NOR_FLASH_INSTANCE *SpiNorFlashInstance, IN UINT32 Timeout, IN UINT32 RetryCount)
EFI_STATUS SetWel(IN SPI_NOR_FLASH_INSTANCE *Instance)
UINT32 FillWriteBuffer(IN SPI_NOR_FLASH_INSTANCE *Instance, IN UINT8 Opcode, IN UINT32 DummyBytes, IN UINT8 AddressBytesSupported, IN BOOLEAN UseAddress, IN UINT32 Address, IN UINT32 WriteBytes, IN UINT8 *WriteBuffer)
EFI_STATUS GetFastReadParameter(IN SPI_NOR_FLASH_INSTANCE *Instance, IN OUT UINT8 *FastReadInstruction, IN OUT UINT8 *FastReadModeBits, IN OUT UINT8 *FastReadDummyClocks)
EFI_STATUS GetEraseBlockAttribute(IN SPI_NOR_FLASH_INSTANCE *Instance, IN SFDP_SECTOR_REGION_RECORD *FlashRegion, IN UINT32 FlashAddress, IN UINT32 RemainingSize, IN OUT UINT32 *BlockSizeToErase, IN OUT UINT32 *BlockCountToErase, OUT UINT8 *BlockEraseCommand, OUT UINT32 *TypicalTime, OUT UINT64 *MaximumTimeout)
EFI_STATUS GetRegionByFlashAddress(IN SPI_NOR_FLASH_INSTANCE *Instance, IN UINT32 FlashAddress, OUT SFDP_SECTOR_REGION_RECORD **FlashRegion)
#define SPI_ADDR_3BYTE_ONLY
UINT32 MaximumTransferBytes
EFI_SPI_IO_PROTOCOL_TRANSACTION Transaction
UINT32 RegionAddress
Region starting address.
UINT32 SectorSize
Sector size in byte (Minimum blcok erase size)
UINT32 RegionTotalSize
Region total size in bytes.
BOOLEAN WriteEnableLatchRequired
Wether Write Enable Latch is supported.