22 if (mSpiInstance ==
NULL) {
24 if (mSpiInstance ==
NULL) {
56 if (GuidHob ==
NULL) {
67 if (SpiInstance ==
NULL) {
71 DEBUG ((DEBUG_INFO,
"SpiInstance = %08X\n", SpiInstance));
74 SpiInstance->Handle =
NULL;
79 if ((SpiFlashInfo->SpiAddress.AddressSpaceId != EFI_ACPI_3_0_PCI_CONFIGURATION_SPACE) ||
80 (SpiFlashInfo->SpiAddress.RegisterBitWidth != 32) ||
81 (SpiFlashInfo->SpiAddress.RegisterBitOffset != 0) ||
82 (SpiFlashInfo->SpiAddress.AccessSize != EFI_ACPI_3_0_DWORD))
84 DEBUG ((DEBUG_ERROR,
"SPI FLASH HOB is not expected. need check the hob or enhance SPI flash driver.\n"));
87 SpiInstance->PchSpiBase = (UINT32)(
UINTN)SpiFlashInfo->SpiAddress.Address;
88 SpiInstance->Flags = SpiFlashInfo->Flags;
89 DEBUG ((DEBUG_INFO,
"PchSpiBase at 0x%x\n", SpiInstance->PchSpiBase));
92 DEBUG ((DEBUG_INFO,
"ScSpiBar0 at 0x%08X\n", ScSpiBar0));
99 DEBUG ((DEBUG_ERROR,
"SPI Flash descriptor invalid, cannot use Hardware Sequencing registers!\n"));
132 SpiInstance->Component1StartAddr = (UINT32)(SIZE_512KB << Comp0Density);
177 Status =
SendSpiCmd (FlashRegionType, FlashCycleRead, Address, ByteCount, Buffer);
204 Status =
SendSpiCmd (FlashRegionType, FlashCycleWrite, Address, ByteCount, Buffer);
229 Status =
SendSpiCmd (FlashRegionType, FlashCycleErase, Address, ByteCount,
NULL);
248 IN UINT8 ComponentNumber,
258 if (SpiInstance ==
NULL) {
259 return EFI_DEVICE_ERROR;
262 if ((ByteCount > 64) || (ComponentNumber > SpiInstance->NumberOfComponents)) {
264 return EFI_INVALID_PARAMETER;
268 if (ComponentNumber == FlashComponent1) {
269 Address = SpiInstance->Component1StartAddr;
272 Status =
SendSpiCmd (0, FlashCycleReadSfdp, Address, ByteCount, SfdpData);
291 IN UINT8 ComponentNumber,
301 if (SpiInstance ==
NULL) {
302 return EFI_DEVICE_ERROR;
305 if (ComponentNumber > SpiInstance->NumberOfComponents) {
307 return EFI_INVALID_PARAMETER;
311 if (ComponentNumber == FlashComponent1) {
312 Address = SpiInstance->Component1StartAddr;
315 Status =
SendSpiCmd (0, FlashCycleReadJedecId, Address, ByteCount, JedecId);
333 IN UINT8 *StatusValue
338 Status =
SendSpiCmd (0, FlashCycleWriteStatus, 0, ByteCount, StatusValue);
356 OUT UINT8 *StatusValue
361 Status =
SendSpiCmd (0, FlashCycleReadStatus, 0, ByteCount, StatusValue);
380 IN UINT32 SoftStrapAddr,
382 OUT UINT8 *SoftStrapValue
385 UINT32 StrapFlashAddr;
390 if (SpiInstance ==
NULL) {
391 return EFI_DEVICE_ERROR;
394 ASSERT (SpiInstance->StrapBaseAddress != 0);
398 StrapFlashAddr = SpiInstance->StrapBaseAddress + SoftStrapAddr;
400 Status =
SendSpiCmd (FlashRegionDescriptor, FlashCycleRead, StrapFlashAddr, ByteCount, SoftStrapValue);
429 UINTN SpiBaseAddress;
432 UINT32 HardwareSpiAddr;
433 UINT16 PermissionBit;
441 if (SpiInstance ==
NULL) {
442 return EFI_DEVICE_ERROR;
446 SpiBaseAddress = SpiInstance->PchSpiBase;
454 if ((FlashCycleType == FlashCycleWrite) || (FlashCycleType == FlashCycleErase)) {
456 if (EFI_ERROR (Status)) {
467 Status = EFI_DEVICE_ERROR;
471 HardwareSpiAddr = Address;
472 if ((FlashCycleType == FlashCycleRead) ||
473 (FlashCycleType == FlashCycleWrite) ||
474 (FlashCycleType == FlashCycleErase))
476 switch (FlashRegionType) {
477 case FlashRegionDescriptor:
478 if (FlashCycleType == FlashCycleRead) {
489 case FlashRegionBios:
490 if (FlashCycleType == FlashCycleRead) {
502 if (FlashCycleType == FlashCycleRead) {
514 if (FlashCycleType == FlashCycleRead) {
517 PermissionBit = B_SPI_FRAP_BRWA_GBE;
520 Data32 =
MmioRead32 (ScSpiBar0 + R_SPI_FREG3_GBE);
525 case FlashRegionPlatformData:
526 if (FlashCycleType == FlashCycleRead) {
529 PermissionBit = B_SPI_FRAP_BRWA_PLATFORM;
547 Status = EFI_UNSUPPORTED;
551 if ((LimitAddress != 0) && (Address > LimitAddress)) {
552 Status = EFI_INVALID_PARAMETER;
560 if ((PermissionBit != 0) && ((SpiInstance->RegionPermission & PermissionBit) == 0)) {
561 Status = EFI_ACCESS_DENIED;
570 switch (FlashCycleType) {
575 case FlashCycleWrite:
579 case FlashCycleErase:
580 if (((ByteCount % SIZE_4KB) != 0) || ((HardwareSpiAddr % SIZE_4KB) != 0)) {
581 DEBUG ((DEBUG_ERROR,
"Erase and erase size must be 4KB aligned. \n"));
583 Status = EFI_INVALID_PARAMETER;
589 case FlashCycleReadSfdp:
593 case FlashCycleReadJedecId:
597 case FlashCycleWriteStatus:
601 case FlashCycleReadStatus:
610 Status = EFI_INVALID_PARAMETER;
616 SpiDataCount = ByteCount;
617 if ((FlashCycleType == FlashCycleRead) || (FlashCycleType == FlashCycleWrite)) {
625 if (HardwareSpiAddr + ByteCount > ((HardwareSpiAddr + BIT8) &~(BIT8 - 1))) {
626 SpiDataCount = (((UINT32)(HardwareSpiAddr) + BIT8) &~(BIT8 - 1)) - (UINT32)(HardwareSpiAddr);
634 if (SpiDataCount >= 64) {
636 }
else if ((SpiDataCount &~0x07) != 0) {
637 SpiDataCount = SpiDataCount &~0x07;
641 if (FlashCycleType == FlashCycleErase) {
642 if (((ByteCount / SIZE_64KB) != 0) &&
643 ((ByteCount % SIZE_64KB) == 0) &&
644 ((HardwareSpiAddr % SIZE_64KB) == 0))
646 if (HardwareSpiAddr < SpiInstance->Component1StartAddr) {
651 SpiDataCount = SIZE_64KB;
653 SpiDataCount = SIZE_4KB;
660 SpiDataCount = SIZE_64KB;
662 SpiDataCount = SIZE_4KB;
666 SpiDataCount = SIZE_4KB;
669 if (SpiDataCount == SIZE_4KB) {
679 if ((FlashCycleType == FlashCycleWrite) || (FlashCycleType == FlashCycleWriteStatus)) {
680 if ((SpiDataCount & 0x07) != 0) {
684 for (Index = 0; Index < SpiDataCount; Index++) {
691 for (Index = 0; Index < SpiDataCount; Index +=
sizeof (UINT32)) {
715 Status = EFI_DEVICE_ERROR;
722 if ((FlashCycleType == FlashCycleRead) ||
723 (FlashCycleType == FlashCycleReadSfdp) ||
724 (FlashCycleType == FlashCycleReadJedecId) ||
725 (FlashCycleType == FlashCycleReadStatus))
727 if ((SpiDataCount & 0x07) != 0) {
731 for (Index = 0; Index < SpiDataCount; Index++) {
738 for (Index = 0; Index < SpiDataCount; Index +=
sizeof (UINT32)) {
744 HardwareSpiAddr += SpiDataCount;
745 Buffer += SpiDataCount;
746 ByteCount -= SpiDataCount;
747 }
while (ByteCount > 0);
753 if ((FlashCycleType == FlashCycleWrite) || (FlashCycleType == FlashCycleErase)) {
776 IN BOOLEAN ErrorCheck
790 for (WaitTicks = 0; WaitTicks < WaitCount; WaitTicks++) {
822 OUT UINT32 *BaseAddress OPTIONAL,
823 OUT UINT32 *RegionSize OPTIONAL
831 if (FlashRegionType >= FlashRegionMax) {
832 return EFI_INVALID_PARAMETER;
836 if (SpiInstance ==
NULL) {
837 return EFI_DEVICE_ERROR;
840 if (FlashRegionType == FlashRegionAll) {
841 if (BaseAddress !=
NULL) {
845 if (RegionSize !=
NULL) {
846 *RegionSize = SpiInstance->Component1StartAddr;
860 return EFI_DEVICE_ERROR;
864 if (BaseAddress !=
NULL) {
868 if (RegionSize !=
NULL) {
UINTN EFIAPI MicroSecondDelay(IN UINTN MicroSeconds)
VOID *EFIAPI GetFirstGuidHob(IN CONST EFI_GUID *Guid)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
UINT32 EFIAPI MmioOr32(IN UINTN Address, IN UINT32 OrData)
UINT16 EFIAPI MmioRead16(IN UINTN Address)
UINT8 EFIAPI MmioRead8(IN UINTN Address)
UINT8 EFIAPI MmioWrite8(IN UINTN Address, IN UINT8 Value)
UINT32 EFIAPI MmioRead32(IN UINTN Address)
UINT32 EFIAPI MmioAndThenOr32(IN UINTN Address, IN UINT32 AndData, IN UINT32 OrData)
UINT32 EFIAPI MmioWrite32(IN UINTN Address, IN UINT32 Value)
#define DEBUG(Expression)
UINT8 SaveAndDisableSpiPrefetchCache(IN UINTN PchSpiBase)
VOID EFIAPI EnableBiosWriteProtect(IN UINTN PchSpiBase, IN UINT8 CpuSmmBwp)
EFI_STATUS EFIAPI DisableBiosWriteProtect(IN UINTN PchSpiBase, IN UINT8 CpuSmmBwp)
VOID SetSpiBiosControlRegister(IN UINTN PchSpiBase, IN UINT8 BiosCtlValue)
UINT32 AcquireSpiBar0(IN UINTN PchSpiBase)
VOID ReleaseSpiBar0(IN UINTN PchSpiBase)
#define EFI_PCI_COMMAND_MEMORY_SPACE
0x0002
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
#define B_SPI_FLCOMP_COMP1_MASK
Flash Component 1 Density.
#define N_SPI_FDBAR_NC
< Number Of Components
#define R_SPI_FDATA00
SPI Data 00 (32 bits)
#define B_SPI_FDBAR_FPSBA
Flash Strap Base Address.
#define N_SPI_FREG1_BASE
Bit 14:0 identifies address bits [26:2].
#define B_SPI_HSFS_FCERR
Flash Cycle Error.
#define V_SPI_HSFS_CYCLE_64K_ERASE
Flash Cycle 64K Sector Erase.
#define N_SPI_FREG4_LIMIT
Bit 30:16 identifies address bits [26:12].
#define B_SPI_FRAP_BRRA_FLASHD
Region Read Access for Region0 Flash Descriptor.
#define B_SPI_HSFS_FDV
Flash Descriptor Valid.
#define B_SPI_FADDR_MASK
SPI Flash Address Mask (0~26bit)
#define V_SPI_FDOC_FDSS_COMP
Component.
#define B_SPI_HSFS_SCIP
SPI Cycle in Progress.
#define R_SPI_UVSCC
Vendor Specific Component Capabilities for Component 1 (32 bits)
#define B_SPI_FRAP_BRRA_BIOS
Region Read Access for Region1 BIOS.
#define B_SPI_FRAP_BRWA_BIOS
Region Write Access for Region1 BIOS.
#define B_SPI_FREG0_BASE_MASK
Base, [14:0] here represents base [26:12].
#define B_SPI_FREGX_LIMIT_MASK
Flash Region Limit [30:16] represents [26:12], [11:0] are assumed to be FFFh.
#define B_SPI_FREG2_LIMIT_MASK
Size, [30:16] here represents limit[26:12].
#define V_SPI_HSFS_CYCLE_WRITE_STATUS
Flash Cycle Write Status.
#define B_SPI_LVSCC_EO_64K
< 64k Erase valid (EO_64k_valid)
#define B_SPI_FREG2_BASE_MASK
Base, [14:0] here represents base [26:12].
#define B_SPI_HSFS_CYCLE_MASK
Flash Cycle.
#define V_SPI_HSFS_CYCLE_READ_STATUS
Flash Cycle Read Status.
#define N_SPI_FREG4_BASE
Bit 14:0 identifies address bits [26:2].
#define R_SPI_FREG2_SEC
Flash Region 2 (SEC) (32bits)
#define N_SPI_FREG1_LIMIT
Bit 30:16 identifies address bits [26:12].
#define R_SPI_FREG4_PLATFORM_DATA
Flash Region 4 (Platform Data) (32bits)
#define B_SPI_FRAP_BRRA_SEC
Region Read Access for Region2 SEC.
#define N_SPI_FREG0_LIMIT
Bit 30:16 identifies address bits [26:12].
#define B_SPI_FRAP_BRRA_PLATFORM
Region read access for Region4 PlatformData.
#define N_SPI_FREGX_LIMIT
Region limit bit position.
#define R_SPI_FREG1_BIOS
Flash Region 1 (BIOS) (32bits)
#define B_SPI_FRAP_BRWA_FLASHD
Region Write Access for Region0 Flash Descriptor.
#define B_SPI_FDBAR_NC
Number Of Components.
#define R_SPI_HSFS
SPI Host Interface Registers.
#define B_SPI_FREG3_BASE_MASK
Base, [14:0] here represents base [26:12].
#define S_SPI_FREGX
Size of Flash Region register.
#define R_SPI_FRAP
SPI Flash Regions Access Permissions Register.
#define V_SPI_HSFS_CYCLE_WRITE
Flash Cycle Write.
#define V_SPI_FDOC_FDSS_FSDM
Flash Signature and Descriptor Map.
#define R_SPI_FCBA_FLCOMP
Flash Components Register.
#define B_SPI_FREG4_LIMIT_MASK
Size, [30:16] here represents limit[26:12].
#define B_SPI_FRAP_BRRA_GBE
Region read access for Region3 GbE.
#define V_SPI_HSFS_CYCLE_4K_ERASE
Flash Cycle 4K Block Erase.
#define B_SPI_FDOC_FDSI_MASK
Flash Descriptor Section Index.
#define B_SPI_FREG3_LIMIT_MASK
Size, [30:16] here represents limit[26:12].
#define B_SPI_FRAP_BRWA_SEC
Region Write Access for Region2 SEC.
#define R_SPI_FDBAR_FLASH_MAP1
Flash MAP 1.
#define R_SPI_LVSCC
Vendor Specific Component Capabilities for Component 0 (32 bits)
#define B_SPI_FREG1_BASE_MASK
Base, [14:0] here represents base [26:12].
#define V_SPI_HSFS_CYCLE_READ_SFDP
Flash Cycle Read SFDP.
#define R_SPI_FADDR
SPI Flash Address.
#define V_SPI_HSFS_CYCLE_READ_JEDEC_ID
Flash Cycle Read JEDEC ID.
#define R_SPI_FDBAR_FLASH_MAP0
Flash MAP 0.
#define B_SPI_FREG1_LIMIT_MASK
Size, [30:16] here represents limit[26:12].
#define R_SPI_FDOD
Flash Descriptor Observability Data Register (32 bits)
#define B_SPI_HSFS_CYCLE_FGO
Flash Cycle Go.
#define R_SPI_FREG0_FLASHD
Flash Region 0 (Flash Descriptor) (32bits)
#define R_SPI_FDOC
Flash Descriptor Observability Control Register (32 bits)
#define N_SPI_FREGX_LIMIT_REPR
Region limit bit represents position.
#define B_SPI_HSFS_FDBC_MASK
Flash Data Byte Count ( <= 64), Count = (Value in this field) + 1.
#define B_SPI_FREGX_BASE_MASK
Flash Region Base, [14:0] represents [26:12].
#define B_SPI_FDOC_FDSS_MASK
Flash Descriptor Section Select.
#define V_SPI_HSFS_CYCLE_READ
Flash Cycle Read.
#define B_SPI_FREG4_BASE_MASK
Base, [14:0] here represents base [26:12].
#define B_SPI_FREG0_LIMIT_MASK
Size, [30:16] here represents limit[26:12].
#define N_SPI_FREG0_BASE
Bit 14:0 identifies address bits [26:2].
#define B_SPI_HSFS_FDONE
Flash Cycle Done.
#define SC_SPI_PRIVATE_DATA_SIGNATURE
#define WAIT_TIME
Wait Time = 6 seconds = 6000000 microseconds.
#define WAIT_PERIOD
Wait Period = 10 microseconds.
BOOLEAN WaitForSpiCycleComplete(IN UINT32 ScSpiBar0, IN BOOLEAN ErrorCheck)
EFI_STATUS EFIAPI SpiFlashErase(IN FLASH_REGION_TYPE FlashRegionType, IN UINT32 Address, IN UINT32 ByteCount)
SPI_INSTANCE * GetSpiInstance(VOID)
EFI_STATUS EFIAPI SpiFlashWriteStatus(IN UINT32 ByteCount, IN UINT8 *StatusValue)
EFI_STATUS EFIAPI SpiReadPchSoftStrap(IN UINT32 SoftStrapAddr, IN UINT32 ByteCount, OUT UINT8 *SoftStrapValue)
EFI_STATUS EFIAPI SpiConstructor(VOID)
EFI_STATUS SendSpiCmd(IN FLASH_REGION_TYPE FlashRegionType, IN FLASH_CYCLE_TYPE FlashCycleType, IN UINT32 Address, IN UINT32 ByteCount, IN OUT UINT8 *Buffer)
EFI_STATUS EFIAPI SpiGetRegionAddress(IN FLASH_REGION_TYPE FlashRegionType, OUT UINT32 *BaseAddress OPTIONAL, OUT UINT32 *RegionSize OPTIONAL)
EFI_STATUS EFIAPI SpiFlashWrite(IN FLASH_REGION_TYPE FlashRegionType, IN UINT32 Address, IN UINT32 ByteCount, IN UINT8 *Buffer)
EFI_STATUS EFIAPI SpiFlashRead(IN FLASH_REGION_TYPE FlashRegionType, IN UINT32 Address, IN UINT32 ByteCount, OUT UINT8 *Buffer)
EFI_STATUS EFIAPI SpiFlashReadJedecId(IN UINT8 ComponentNumber, IN UINT32 ByteCount, OUT UINT8 *JedecId)
EFI_STATUS EFIAPI SpiFlashReadSfdp(IN UINT8 ComponentNumber, IN UINT32 ByteCount, OUT UINT8 *SfdpData)
EFI_STATUS EFIAPI SpiFlashReadStatus(IN UINT32 ByteCount, OUT UINT8 *StatusValue)