TianoCore EDK2 master
Loading...
Searching...
No Matches
SpiFlashLib.c
Go to the documentation of this file.
1
8#include "SpiCommon.h"
9
10SPI_INSTANCE *mSpiInstance = NULL;
11
19 VOID
20 )
21{
22 if (mSpiInstance == NULL) {
23 mSpiInstance = AllocatePool (sizeof (SPI_INSTANCE));
24 if (mSpiInstance == NULL) {
25 return NULL;
26 }
27
28 ZeroMem (mSpiInstance, sizeof (SPI_INSTANCE));
29 }
30
31 return mSpiInstance;
32}
33
41EFIAPI
43 VOID
44 )
45{
46 UINT32 ScSpiBar0;
47 UINT8 Comp0Density;
48 SPI_INSTANCE *SpiInstance;
49 EFI_HOB_GUID_TYPE *GuidHob;
50 SPI_FLASH_INFO *SpiFlashInfo;
51
52 //
53 // Find SPI flash hob
54 //
55 GuidHob = GetFirstGuidHob (&gSpiFlashInfoGuid);
56 if (GuidHob == NULL) {
57 ASSERT (FALSE);
58 return EFI_NOT_FOUND;
59 }
60
61 SpiFlashInfo = (SPI_FLASH_INFO *)GET_GUID_HOB_DATA (GuidHob);
62
63 //
64 // Initialize the SPI instance
65 //
66 SpiInstance = GetSpiInstance ();
67 if (SpiInstance == NULL) {
68 return EFI_NOT_FOUND;
69 }
70
71 DEBUG ((DEBUG_INFO, "SpiInstance = %08X\n", SpiInstance));
72
73 SpiInstance->Signature = SC_SPI_PRIVATE_DATA_SIGNATURE;
74 SpiInstance->Handle = NULL;
75
76 //
77 // Check the SPI address
78 //
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))
83 {
84 DEBUG ((DEBUG_ERROR, "SPI FLASH HOB is not expected. need check the hob or enhance SPI flash driver.\n"));
85 }
86
87 SpiInstance->PchSpiBase = (UINT32)(UINTN)SpiFlashInfo->SpiAddress.Address;
88 SpiInstance->Flags = SpiFlashInfo->Flags;
89 DEBUG ((DEBUG_INFO, "PchSpiBase at 0x%x\n", SpiInstance->PchSpiBase));
90
91 ScSpiBar0 = AcquireSpiBar0 (SpiInstance->PchSpiBase);
92 DEBUG ((DEBUG_INFO, "ScSpiBar0 at 0x%08X\n", ScSpiBar0));
93
94 if (ScSpiBar0 == 0) {
95 ASSERT (FALSE);
96 }
97
98 if ((MmioRead32 (ScSpiBar0 + R_SPI_HSFS) & B_SPI_HSFS_FDV) == 0) {
99 DEBUG ((DEBUG_ERROR, "SPI Flash descriptor invalid, cannot use Hardware Sequencing registers!\n"));
100 ASSERT (FALSE);
101 }
102
103 MmioOr32 (SpiInstance->PchSpiBase + PCI_COMMAND_OFFSET, EFI_PCI_COMMAND_MEMORY_SPACE);
104 SpiInstance->RegionPermission = MmioRead16 (ScSpiBar0 + R_SPI_FRAP);
105 SpiInstance->SfdpVscc0Value = MmioRead32 (ScSpiBar0 + R_SPI_LVSCC);
106 SpiInstance->SfdpVscc1Value = MmioRead32 (ScSpiBar0 + R_SPI_UVSCC);
107
108 //
109 // Select to Flash Map 0 Register to get the number of flash Component
110 //
112 ScSpiBar0 + R_SPI_FDOC,
115 );
116
117 //
118 // Copy Zero based Number Of Components
119 //
120 SpiInstance->NumberOfComponents = (UINT8)((MmioRead16 (ScSpiBar0 + R_SPI_FDOD) & B_SPI_FDBAR_NC) >> N_SPI_FDBAR_NC);
121
123 ScSpiBar0 + R_SPI_FDOC,
126 );
127
128 //
129 // Copy Component 0 Density
130 //
131 Comp0Density = (UINT8)MmioRead32 (ScSpiBar0 + R_SPI_FDOD) & B_SPI_FLCOMP_COMP1_MASK;
132 SpiInstance->Component1StartAddr = (UINT32)(SIZE_512KB << Comp0Density);
133
134 //
135 // Select FLASH_MAP1 to get Flash SC Strap Base Address
136 //
138 (ScSpiBar0 + R_SPI_FDOC),
141 );
142
143 SpiInstance->StrapBaseAddress = MmioRead32 (ScSpiBar0 + R_SPI_FDOD) & B_SPI_FDBAR_FPSBA;
144
145 //
146 // Align FPSBA with address bits for the SC Strap portion of flash descriptor
147 //
148 SpiInstance->StrapBaseAddress &= B_SPI_FDBAR_FPSBA;
149
150 return EFI_SUCCESS;
151}
152
167EFIAPI
169 IN FLASH_REGION_TYPE FlashRegionType,
170 IN UINT32 Address,
171 IN UINT32 ByteCount,
172 OUT UINT8 *Buffer
173 )
174{
175 EFI_STATUS Status;
176
177 Status = SendSpiCmd (FlashRegionType, FlashCycleRead, Address, ByteCount, Buffer);
178 return Status;
179}
180
194EFIAPI
196 IN FLASH_REGION_TYPE FlashRegionType,
197 IN UINT32 Address,
198 IN UINT32 ByteCount,
199 IN UINT8 *Buffer
200 )
201{
202 EFI_STATUS Status;
203
204 Status = SendSpiCmd (FlashRegionType, FlashCycleWrite, Address, ByteCount, Buffer);
205 return Status;
206}
207
220EFIAPI
222 IN FLASH_REGION_TYPE FlashRegionType,
223 IN UINT32 Address,
224 IN UINT32 ByteCount
225 )
226{
227 EFI_STATUS Status;
228
229 Status = SendSpiCmd (FlashRegionType, FlashCycleErase, Address, ByteCount, NULL);
230 return Status;
231}
232
246EFIAPI
248 IN UINT8 ComponentNumber,
249 IN UINT32 ByteCount,
250 OUT UINT8 *SfdpData
251 )
252{
253 EFI_STATUS Status;
254 UINT32 Address;
255 SPI_INSTANCE *SpiInstance;
256
257 SpiInstance = GetSpiInstance ();
258 if (SpiInstance == NULL) {
259 return EFI_DEVICE_ERROR;
260 }
261
262 if ((ByteCount > 64) || (ComponentNumber > SpiInstance->NumberOfComponents)) {
263 ASSERT (FALSE);
264 return EFI_INVALID_PARAMETER;
265 }
266
267 Address = 0;
268 if (ComponentNumber == FlashComponent1) {
269 Address = SpiInstance->Component1StartAddr;
270 }
271
272 Status = SendSpiCmd (0, FlashCycleReadSfdp, Address, ByteCount, SfdpData);
273 return Status;
274}
275
289EFIAPI
291 IN UINT8 ComponentNumber,
292 IN UINT32 ByteCount,
293 OUT UINT8 *JedecId
294 )
295{
296 EFI_STATUS Status;
297 UINT32 Address;
298 SPI_INSTANCE *SpiInstance;
299
300 SpiInstance = GetSpiInstance ();
301 if (SpiInstance == NULL) {
302 return EFI_DEVICE_ERROR;
303 }
304
305 if (ComponentNumber > SpiInstance->NumberOfComponents) {
306 ASSERT (FALSE);
307 return EFI_INVALID_PARAMETER;
308 }
309
310 Address = 0;
311 if (ComponentNumber == FlashComponent1) {
312 Address = SpiInstance->Component1StartAddr;
313 }
314
315 Status = SendSpiCmd (0, FlashCycleReadJedecId, Address, ByteCount, JedecId);
316 return Status;
317}
318
330EFIAPI
332 IN UINT32 ByteCount,
333 IN UINT8 *StatusValue
334 )
335{
336 EFI_STATUS Status;
337
338 Status = SendSpiCmd (0, FlashCycleWriteStatus, 0, ByteCount, StatusValue);
339 return Status;
340}
341
353EFIAPI
355 IN UINT32 ByteCount,
356 OUT UINT8 *StatusValue
357 )
358{
359 EFI_STATUS Status;
360
361 Status = SendSpiCmd (0, FlashCycleReadStatus, 0, ByteCount, StatusValue);
362 return Status;
363}
364
378EFIAPI
380 IN UINT32 SoftStrapAddr,
381 IN UINT32 ByteCount,
382 OUT UINT8 *SoftStrapValue
383 )
384{
385 UINT32 StrapFlashAddr;
386 EFI_STATUS Status;
387 SPI_INSTANCE *SpiInstance;
388
389 SpiInstance = GetSpiInstance ();
390 if (SpiInstance == NULL) {
391 return EFI_DEVICE_ERROR;
392 }
393
394 ASSERT (SpiInstance->StrapBaseAddress != 0);
395 //
396 // SC Strap Flash Address = FPSBA + RamAddr
397 //
398 StrapFlashAddr = SpiInstance->StrapBaseAddress + SoftStrapAddr;
399
400 Status = SendSpiCmd (FlashRegionDescriptor, FlashCycleRead, StrapFlashAddr, ByteCount, SoftStrapValue);
401 return Status;
402}
403
420 IN FLASH_REGION_TYPE FlashRegionType,
421 IN FLASH_CYCLE_TYPE FlashCycleType,
422 IN UINT32 Address,
423 IN UINT32 ByteCount,
424 IN OUT UINT8 *Buffer
425 )
426{
427 EFI_STATUS Status;
428 UINT32 Index;
429 UINTN SpiBaseAddress;
430 UINT32 ScSpiBar0;
431 UINT32 LimitAddress;
432 UINT32 HardwareSpiAddr;
433 UINT16 PermissionBit;
434 UINT32 SpiDataCount;
435 UINT32 FlashCycle;
436 UINT8 BiosCtlSave;
437 SPI_INSTANCE *SpiInstance;
438 UINT32 Data32;
439
440 SpiInstance = GetSpiInstance ();
441 if (SpiInstance == NULL) {
442 return EFI_DEVICE_ERROR;
443 }
444
445 Status = EFI_SUCCESS;
446 SpiBaseAddress = SpiInstance->PchSpiBase;
447 ScSpiBar0 = AcquireSpiBar0 (SpiBaseAddress);
448 BiosCtlSave = 0;
449 SpiInstance->RegionPermission = MmioRead16 (ScSpiBar0 + R_SPI_FRAP);
450
451 //
452 // If it's write cycle, disable Prefetching, Caching and disable BIOS Write Protect
453 //
454 if ((FlashCycleType == FlashCycleWrite) || (FlashCycleType == FlashCycleErase)) {
455 Status = DisableBiosWriteProtect (SpiBaseAddress, mSpiInstance->Flags & FLAGS_SPI_DISABLE_SMM_WRITE_PROTECT);
456 if (EFI_ERROR (Status)) {
457 goto SendSpiCmdEnd;
458 }
459
460 BiosCtlSave = SaveAndDisableSpiPrefetchCache (SpiBaseAddress);
461 }
462
463 //
464 // Make sure it's safe to program the command.
465 //
466 if (!WaitForSpiCycleComplete (ScSpiBar0, FALSE)) {
467 Status = EFI_DEVICE_ERROR;
468 goto SendSpiCmdEnd;
469 }
470
471 HardwareSpiAddr = Address;
472 if ((FlashCycleType == FlashCycleRead) ||
473 (FlashCycleType == FlashCycleWrite) ||
474 (FlashCycleType == FlashCycleErase))
475 {
476 switch (FlashRegionType) {
477 case FlashRegionDescriptor:
478 if (FlashCycleType == FlashCycleRead) {
479 PermissionBit = B_SPI_FRAP_BRRA_FLASHD;
480 } else {
481 PermissionBit = B_SPI_FRAP_BRWA_FLASHD;
482 }
483
484 Data32 = MmioRead32 (ScSpiBar0 + R_SPI_FREG0_FLASHD);
485 HardwareSpiAddr += (Data32 & B_SPI_FREG0_BASE_MASK) << N_SPI_FREG0_BASE;
486 LimitAddress = (Data32 & B_SPI_FREG0_LIMIT_MASK) >> N_SPI_FREG0_LIMIT;
487 break;
488
489 case FlashRegionBios:
490 if (FlashCycleType == FlashCycleRead) {
491 PermissionBit = B_SPI_FRAP_BRRA_BIOS;
492 } else {
493 PermissionBit = B_SPI_FRAP_BRWA_BIOS;
494 }
495
496 Data32 = MmioRead32 (ScSpiBar0 + R_SPI_FREG1_BIOS);
497 HardwareSpiAddr += (Data32 & B_SPI_FREG1_BASE_MASK) << N_SPI_FREG1_BASE;
498 LimitAddress = (Data32 & B_SPI_FREG1_LIMIT_MASK) >> N_SPI_FREG1_LIMIT;
499 break;
500
501 case FlashRegionMe:
502 if (FlashCycleType == FlashCycleRead) {
503 PermissionBit = B_SPI_FRAP_BRRA_SEC;
504 } else {
505 PermissionBit = B_SPI_FRAP_BRWA_SEC;
506 }
507
508 Data32 = MmioRead32 (ScSpiBar0 + R_SPI_FREG2_SEC);
509 HardwareSpiAddr += (Data32 & B_SPI_FREG2_BASE_MASK) << N_SPI_FREG2_BASE;
510 LimitAddress = (Data32 & B_SPI_FREG2_LIMIT_MASK) >> N_SPI_FREG2_LIMIT;
511 break;
512
513 case FlashRegionGbE:
514 if (FlashCycleType == FlashCycleRead) {
515 PermissionBit = B_SPI_FRAP_BRRA_GBE;
516 } else {
517 PermissionBit = B_SPI_FRAP_BRWA_GBE;
518 }
519
520 Data32 = MmioRead32 (ScSpiBar0 + R_SPI_FREG3_GBE);
521 HardwareSpiAddr += (Data32 & B_SPI_FREG3_BASE_MASK) << N_SPI_FREG3_BASE;
522 LimitAddress = (Data32 & B_SPI_FREG3_LIMIT_MASK) >> N_SPI_FREG3_LIMIT;
523 break;
524
525 case FlashRegionPlatformData:
526 if (FlashCycleType == FlashCycleRead) {
527 PermissionBit = B_SPI_FRAP_BRRA_PLATFORM;
528 } else {
529 PermissionBit = B_SPI_FRAP_BRWA_PLATFORM;
530 }
531
532 Data32 = MmioRead32 (ScSpiBar0 + R_SPI_FREG4_PLATFORM_DATA);
533 HardwareSpiAddr += (Data32 & B_SPI_FREG4_BASE_MASK) << N_SPI_FREG4_BASE;
534 LimitAddress = (Data32 & B_SPI_FREG4_LIMIT_MASK) >> N_SPI_FREG4_LIMIT;
535 break;
536
537 case FlashRegionAll:
538 //
539 // FlashRegionAll indicates address is relative to flash device
540 // No error checking for this case
541 //
542 LimitAddress = 0;
543 PermissionBit = 0;
544 break;
545
546 default:
547 Status = EFI_UNSUPPORTED;
548 goto SendSpiCmdEnd;
549 }
550
551 if ((LimitAddress != 0) && (Address > LimitAddress)) {
552 Status = EFI_INVALID_PARAMETER;
553 goto SendSpiCmdEnd;
554 }
555
556 //
557 // If the operation is read, but the region attribute is not read allowed, return error.
558 // If the operation is write, but the region attribute is not write allowed, return error.
559 //
560 if ((PermissionBit != 0) && ((SpiInstance->RegionPermission & PermissionBit) == 0)) {
561 Status = EFI_ACCESS_DENIED;
562 goto SendSpiCmdEnd;
563 }
564 }
565
566 //
567 // Check for SC SPI hardware sequencing required commands
568 //
569 FlashCycle = 0;
570 switch (FlashCycleType) {
571 case FlashCycleRead:
572 FlashCycle = (UINT32)(V_SPI_HSFS_CYCLE_READ << N_SPI_HSFS_CYCLE);
573 break;
574
575 case FlashCycleWrite:
576 FlashCycle = (UINT32)(V_SPI_HSFS_CYCLE_WRITE << N_SPI_HSFS_CYCLE);
577 break;
578
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"));
582 ASSERT (FALSE);
583 Status = EFI_INVALID_PARAMETER;
584 goto SendSpiCmdEnd;
585 }
586
587 break;
588
589 case FlashCycleReadSfdp:
590 FlashCycle = (UINT32)(V_SPI_HSFS_CYCLE_READ_SFDP << N_SPI_HSFS_CYCLE);
591 break;
592
593 case FlashCycleReadJedecId:
594 FlashCycle = (UINT32)(V_SPI_HSFS_CYCLE_READ_JEDEC_ID << N_SPI_HSFS_CYCLE);
595 break;
596
597 case FlashCycleWriteStatus:
598 FlashCycle = (UINT32)(V_SPI_HSFS_CYCLE_WRITE_STATUS << N_SPI_HSFS_CYCLE);
599 break;
600
601 case FlashCycleReadStatus:
602 FlashCycle = (UINT32)(V_SPI_HSFS_CYCLE_READ_STATUS << N_SPI_HSFS_CYCLE);
603 break;
604
605 default:
606 //
607 // Unrecognized Operation
608 //
609 ASSERT (FALSE);
610 Status = EFI_INVALID_PARAMETER;
611 goto SendSpiCmdEnd;
612 break;
613 }
614
615 do {
616 SpiDataCount = ByteCount;
617 if ((FlashCycleType == FlashCycleRead) || (FlashCycleType == FlashCycleWrite)) {
618 //
619 // Trim at 256 byte boundary per operation,
620 // - SC SPI controller requires trimming at 4KB boundary
621 // - Some SPI chips require trimming at 256 byte boundary for write operation
622 // - Trimming has limited performance impact as we can read / write at most 64 byte
623 // per operation
624 //
625 if (HardwareSpiAddr + ByteCount > ((HardwareSpiAddr + BIT8) &~(BIT8 - 1))) {
626 SpiDataCount = (((UINT32)(HardwareSpiAddr) + BIT8) &~(BIT8 - 1)) - (UINT32)(HardwareSpiAddr);
627 }
628
629 //
630 // Calculate the number of bytes to shift in/out during the SPI data cycle.
631 // Valid settings for the number of bytes during each data portion of the
632 // SC SPI cycles are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 16, 24, 32, 40, 48, 56, 64
633 //
634 if (SpiDataCount >= 64) {
635 SpiDataCount = 64;
636 } else if ((SpiDataCount &~0x07) != 0) {
637 SpiDataCount = SpiDataCount &~0x07;
638 }
639 }
640
641 if (FlashCycleType == FlashCycleErase) {
642 if (((ByteCount / SIZE_64KB) != 0) &&
643 ((ByteCount % SIZE_64KB) == 0) &&
644 ((HardwareSpiAddr % SIZE_64KB) == 0))
645 {
646 if (HardwareSpiAddr < SpiInstance->Component1StartAddr) {
647 //
648 // Check whether Component0 support 64k Erase
649 //
650 if ((SpiInstance->SfdpVscc0Value & B_SPI_LVSCC_EO_64K) != 0) {
651 SpiDataCount = SIZE_64KB;
652 } else {
653 SpiDataCount = SIZE_4KB;
654 }
655 } else {
656 //
657 // Check whether Component1 support 64k Erase
658 //
659 if ((SpiInstance->SfdpVscc1Value & B_SPI_LVSCC_EO_64K) != 0) {
660 SpiDataCount = SIZE_64KB;
661 } else {
662 SpiDataCount = SIZE_4KB;
663 }
664 }
665 } else {
666 SpiDataCount = SIZE_4KB;
667 }
668
669 if (SpiDataCount == SIZE_4KB) {
670 FlashCycle = (UINT32)(V_SPI_HSFS_CYCLE_4K_ERASE << N_SPI_HSFS_CYCLE);
671 } else {
672 FlashCycle = (UINT32)(V_SPI_HSFS_CYCLE_64K_ERASE << N_SPI_HSFS_CYCLE);
673 }
674 }
675
676 //
677 // If it's write cycle, load data into the SPI data buffer.
678 //
679 if ((FlashCycleType == FlashCycleWrite) || (FlashCycleType == FlashCycleWriteStatus)) {
680 if ((SpiDataCount & 0x07) != 0) {
681 //
682 // Use Byte write if Data Count is 0, 1, 2, 3, 4, 5, 6, 7
683 //
684 for (Index = 0; Index < SpiDataCount; Index++) {
685 MmioWrite8 (ScSpiBar0 + R_SPI_FDATA00 + Index, Buffer[Index]);
686 }
687 } else {
688 //
689 // Use Dword write if Data Count is 8, 16, 24, 32, 40, 48, 56, 64
690 //
691 for (Index = 0; Index < SpiDataCount; Index += sizeof (UINT32)) {
692 MmioWrite32 (ScSpiBar0 + R_SPI_FDATA00 + Index, *(UINT32 *)(Buffer + Index));
693 }
694 }
695 }
696
697 //
698 // Set the Flash Address
699 //
700 MmioWrite32 (ScSpiBar0 + R_SPI_FADDR, (UINT32)(HardwareSpiAddr & B_SPI_FADDR_MASK));
701
702 //
703 // Set Data count, Flash cycle, and Set Go bit to start a cycle
704 //
706 ScSpiBar0 + R_SPI_HSFS,
708 (UINT32)(((SpiDataCount - 1) << N_SPI_HSFS_FDBC) | FlashCycle | B_SPI_HSFS_CYCLE_FGO)
709 );
710
711 //
712 // Wait for command execution complete.
713 //
714 if (!WaitForSpiCycleComplete (ScSpiBar0, TRUE)) {
715 Status = EFI_DEVICE_ERROR;
716 goto SendSpiCmdEnd;
717 }
718
719 //
720 // If it's read cycle, load data into the caller's buffer.
721 //
722 if ((FlashCycleType == FlashCycleRead) ||
723 (FlashCycleType == FlashCycleReadSfdp) ||
724 (FlashCycleType == FlashCycleReadJedecId) ||
725 (FlashCycleType == FlashCycleReadStatus))
726 {
727 if ((SpiDataCount & 0x07) != 0) {
728 //
729 // Use Byte read if Data Count is 0, 1, 2, 3, 4, 5, 6, 7
730 //
731 for (Index = 0; Index < SpiDataCount; Index++) {
732 Buffer[Index] = MmioRead8 (ScSpiBar0 + R_SPI_FDATA00 + Index);
733 }
734 } else {
735 //
736 // Use Dword read if Data Count is 8, 16, 24, 32, 40, 48, 56, 64
737 //
738 for (Index = 0; Index < SpiDataCount; Index += sizeof (UINT32)) {
739 *(UINT32 *)(Buffer + Index) = MmioRead32 (ScSpiBar0 + R_SPI_FDATA00 + Index);
740 }
741 }
742 }
743
744 HardwareSpiAddr += SpiDataCount;
745 Buffer += SpiDataCount;
746 ByteCount -= SpiDataCount;
747 } while (ByteCount > 0);
748
749SendSpiCmdEnd:
753 if ((FlashCycleType == FlashCycleWrite) || (FlashCycleType == FlashCycleErase)) {
754 EnableBiosWriteProtect (SpiBaseAddress, mSpiInstance->Flags & FLAGS_SPI_DISABLE_SMM_WRITE_PROTECT);
755 SetSpiBiosControlRegister (SpiBaseAddress, BiosCtlSave);
756 }
757
758 ReleaseSpiBar0 (SpiBaseAddress);
759
760 return Status;
761}
762
773BOOLEAN
775 IN UINT32 ScSpiBar0,
776 IN BOOLEAN ErrorCheck
777 )
778{
779 UINT64 WaitTicks;
780 UINT64 WaitCount;
781 UINT32 Data32;
782
783 //
784 // Convert the wait period allowed into to tick count
785 //
786 WaitCount = WAIT_TIME / WAIT_PERIOD;
787 //
788 // Wait for the SPI cycle to complete.
789 //
790 for (WaitTicks = 0; WaitTicks < WaitCount; WaitTicks++) {
791 Data32 = MmioRead32 (ScSpiBar0 + R_SPI_HSFS);
792 if ((Data32 & B_SPI_HSFS_SCIP) == 0) {
794 if (((Data32 & B_SPI_HSFS_FCERR) != 0) && ErrorCheck) {
795 return FALSE;
796 } else {
797 return TRUE;
798 }
799 }
800
802 }
803
804 return FALSE;
805}
806
819EFIAPI
821 IN FLASH_REGION_TYPE FlashRegionType,
822 OUT UINT32 *BaseAddress OPTIONAL,
823 OUT UINT32 *RegionSize OPTIONAL
824 )
825{
826 UINT32 ScSpiBar0;
827 UINT32 ReadValue;
828 UINT32 Base;
829 SPI_INSTANCE *SpiInstance;
830
831 if (FlashRegionType >= FlashRegionMax) {
832 return EFI_INVALID_PARAMETER;
833 }
834
835 SpiInstance = GetSpiInstance ();
836 if (SpiInstance == NULL) {
837 return EFI_DEVICE_ERROR;
838 }
839
840 if (FlashRegionType == FlashRegionAll) {
841 if (BaseAddress != NULL) {
842 *BaseAddress = 0;
843 }
844
845 if (RegionSize != NULL) {
846 *RegionSize = SpiInstance->Component1StartAddr;
847 }
848
849 return EFI_SUCCESS;
850 }
851
852 ScSpiBar0 = AcquireSpiBar0 (SpiInstance->PchSpiBase);
853 ReadValue = MmioRead32 (ScSpiBar0 + R_SPI_FREG0_FLASHD + S_SPI_FREGX * (UINT32)FlashRegionType);
854 ReleaseSpiBar0 (SpiInstance->PchSpiBase);
855
856 //
857 // If the region is not used, the Region Base is 7FFFh and Region Limit is 0000h
858 //
859 if (ReadValue == B_SPI_FREGX_BASE_MASK) {
860 return EFI_DEVICE_ERROR;
861 }
862
863 Base = (ReadValue & B_SPI_FREG1_BASE_MASK) << N_SPI_FREG1_BASE;
864 if (BaseAddress != NULL) {
865 *BaseAddress = Base;
866 }
867
868 if (RegionSize != NULL) {
869 *RegionSize = ((((ReadValue & B_SPI_FREGX_LIMIT_MASK) >> N_SPI_FREGX_LIMIT) + 1) <<
871 }
872
873 return EFI_SUCCESS;
874}
UINT64 UINTN
UINTN EFIAPI MicroSecondDelay(IN UINTN MicroSeconds)
VOID *EFIAPI GetFirstGuidHob(IN CONST EFI_GUID *Guid)
Definition: HobLib.c:215
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
UINT32 EFIAPI MmioOr32(IN UINTN Address, IN UINT32 OrData)
Definition: IoHighLevel.c:1785
UINT16 EFIAPI MmioRead16(IN UINTN Address)
Definition: IoLib.c:170
UINT8 EFIAPI MmioRead8(IN UINTN Address)
Definition: IoLib.c:82
UINT8 EFIAPI MmioWrite8(IN UINTN Address, IN UINT8 Value)
Definition: IoLib.c:126
UINT32 EFIAPI MmioRead32(IN UINTN Address)
Definition: IoLib.c:262
UINT32 EFIAPI MmioAndThenOr32(IN UINTN Address, IN UINT32 AndData, IN UINT32 OrData)
Definition: IoHighLevel.c:1845
UINT32 EFIAPI MmioWrite32(IN UINTN Address, IN UINT32 Value)
Definition: IoLib.c:309
#define NULL
Definition: Base.h:319
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define DEBUG(Expression)
Definition: DebugLib.h:434
UINT8 SaveAndDisableSpiPrefetchCache(IN UINTN PchSpiBase)
Definition: PchSpi.c:139
VOID EFIAPI EnableBiosWriteProtect(IN UINTN PchSpiBase, IN UINT8 CpuSmmBwp)
Definition: PchSpi.c:114
EFI_STATUS EFIAPI DisableBiosWriteProtect(IN UINTN PchSpiBase, IN UINT8 CpuSmmBwp)
Definition: PchSpi.c:83
VOID SetSpiBiosControlRegister(IN UINTN PchSpiBase, IN UINT8 BiosCtlValue)
Definition: PchSpi.c:164
UINT32 AcquireSpiBar0(IN UINTN PchSpiBase)
Definition: PchSpi.c:18
VOID ReleaseSpiBar0(IN UINTN PchSpiBase)
Definition: PchSpi.c:32
#define EFI_PCI_COMMAND_MEMORY_SPACE
0x0002
Definition: Pci22.h:592
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
#define B_SPI_FLCOMP_COMP1_MASK
Flash Component 1 Density.
Definition: RegsSpi.h:118
#define N_SPI_FDBAR_NC
< Number Of Components
Definition: RegsSpi.h:108
#define R_SPI_FDATA00
SPI Data 00 (32 bits)
Definition: RegsSpi.h:44
#define B_SPI_FDBAR_FPSBA
Flash Strap Base Address.
Definition: RegsSpi.h:112
#define N_SPI_FREG1_BASE
Bit 14:0 identifies address bits [26:2].
Definition: RegsSpi.h:68
#define B_SPI_HSFS_FCERR
Flash Cycle Error.
Definition: RegsSpi.h:38
#define V_SPI_HSFS_CYCLE_64K_ERASE
Flash Cycle 64K Sector Erase.
Definition: RegsSpi.h:30
#define N_SPI_FREG4_LIMIT
Bit 30:16 identifies address bits [26:12].
Definition: RegsSpi.h:84
#define B_SPI_FRAP_BRRA_FLASHD
Region Read Access for Region0 Flash Descriptor.
Definition: RegsSpi.h:56
#define B_SPI_HSFS_FDV
Flash Descriptor Valid.
Definition: RegsSpi.h:36
#define B_SPI_FADDR_MASK
SPI Flash Address Mask (0~26bit)
Definition: RegsSpi.h:42
#define V_SPI_FDOC_FDSS_COMP
Component.
Definition: RegsSpi.h:97
#define B_SPI_HSFS_SCIP
SPI Cycle in Progress.
Definition: RegsSpi.h:37
#define R_SPI_UVSCC
Vendor Specific Component Capabilities for Component 1 (32 bits)
Definition: RegsSpi.h:105
#define B_SPI_FRAP_BRRA_BIOS
Region Read Access for Region1 BIOS.
Definition: RegsSpi.h:55
#define B_SPI_FRAP_BRWA_BIOS
Region Write Access for Region1 BIOS.
Definition: RegsSpi.h:50
#define B_SPI_FREG0_BASE_MASK
Base, [14:0] here represents base [26:12].
Definition: RegsSpi.h:61
#define B_SPI_FREGX_LIMIT_MASK
Flash Region Limit [30:16] represents [26:12], [11:0] are assumed to be FFFh.
Definition: RegsSpi.h:89
#define B_SPI_FREG2_LIMIT_MASK
Size, [30:16] here represents limit[26:12].
Definition: RegsSpi.h:71
#define V_SPI_HSFS_CYCLE_WRITE_STATUS
Flash Cycle Write Status.
Definition: RegsSpi.h:33
#define B_SPI_LVSCC_EO_64K
< 64k Erase valid (EO_64k_valid)
Definition: RegsSpi.h:103
#define B_SPI_FREG2_BASE_MASK
Base, [14:0] here represents base [26:12].
Definition: RegsSpi.h:73
#define B_SPI_HSFS_CYCLE_MASK
Flash Cycle.
Definition: RegsSpi.h:25
#define V_SPI_HSFS_CYCLE_READ_STATUS
Flash Cycle Read Status.
Definition: RegsSpi.h:34
#define N_SPI_FREG4_BASE
Bit 14:0 identifies address bits [26:2].
Definition: RegsSpi.h:86
#define R_SPI_FREG2_SEC
Flash Region 2 (SEC) (32bits)
Definition: RegsSpi.h:70
#define N_SPI_FREG1_LIMIT
Bit 30:16 identifies address bits [26:12].
Definition: RegsSpi.h:66
#define R_SPI_FREG4_PLATFORM_DATA
Flash Region 4 (Platform Data) (32bits)
Definition: RegsSpi.h:82
#define B_SPI_FRAP_BRRA_SEC
Region Read Access for Region2 SEC.
Definition: RegsSpi.h:54
#define N_SPI_FREG0_LIMIT
Bit 30:16 identifies address bits [26:12].
Definition: RegsSpi.h:60
#define B_SPI_FRAP_BRRA_PLATFORM
Region read access for Region4 PlatformData.
Definition: RegsSpi.h:52
#define N_SPI_FREGX_LIMIT
Region limit bit position.
Definition: RegsSpi.h:90
#define R_SPI_FREG1_BIOS
Flash Region 1 (BIOS) (32bits)
Definition: RegsSpi.h:64
#define B_SPI_FRAP_BRWA_FLASHD
Region Write Access for Region0 Flash Descriptor.
Definition: RegsSpi.h:51
#define B_SPI_FDBAR_NC
Number Of Components.
Definition: RegsSpi.h:109
#define R_SPI_HSFS
SPI Host Interface Registers.
Definition: RegsSpi.h:22
#define B_SPI_FREG3_BASE_MASK
Base, [14:0] here represents base [26:12].
Definition: RegsSpi.h:79
#define S_SPI_FREGX
Size of Flash Region register.
Definition: RegsSpi.h:88
#define R_SPI_FRAP
SPI Flash Regions Access Permissions Register.
Definition: RegsSpi.h:46
#define V_SPI_HSFS_CYCLE_WRITE
Flash Cycle Write.
Definition: RegsSpi.h:28
#define V_SPI_FDOC_FDSS_FSDM
Flash Signature and Descriptor Map.
Definition: RegsSpi.h:96
#define R_SPI_FCBA_FLCOMP
Flash Components Register.
Definition: RegsSpi.h:117
#define B_SPI_FREG4_LIMIT_MASK
Size, [30:16] here represents limit[26:12].
Definition: RegsSpi.h:83
#define B_SPI_FRAP_BRRA_GBE
Region read access for Region3 GbE.
Definition: RegsSpi.h:53
#define V_SPI_HSFS_CYCLE_4K_ERASE
Flash Cycle 4K Block Erase.
Definition: RegsSpi.h:29
#define B_SPI_FDOC_FDSI_MASK
Flash Descriptor Section Index.
Definition: RegsSpi.h:98
#define B_SPI_FREG3_LIMIT_MASK
Size, [30:16] here represents limit[26:12].
Definition: RegsSpi.h:77
#define B_SPI_FRAP_BRWA_SEC
Region Write Access for Region2 SEC.
Definition: RegsSpi.h:49
#define R_SPI_FDBAR_FLASH_MAP1
Flash MAP 1.
Definition: RegsSpi.h:111
#define R_SPI_LVSCC
Vendor Specific Component Capabilities for Component 0 (32 bits)
Definition: RegsSpi.h:102
#define B_SPI_FREG1_BASE_MASK
Base, [14:0] here represents base [26:12].
Definition: RegsSpi.h:67
#define V_SPI_HSFS_CYCLE_READ_SFDP
Flash Cycle Read SFDP.
Definition: RegsSpi.h:31
#define R_SPI_FADDR
SPI Flash Address.
Definition: RegsSpi.h:41
#define V_SPI_HSFS_CYCLE_READ_JEDEC_ID
Flash Cycle Read JEDEC ID.
Definition: RegsSpi.h:32
#define R_SPI_FDBAR_FLASH_MAP0
Flash MAP 0.
Definition: RegsSpi.h:107
#define B_SPI_FREG1_LIMIT_MASK
Size, [30:16] here represents limit[26:12].
Definition: RegsSpi.h:65
#define R_SPI_FDOD
Flash Descriptor Observability Data Register (32 bits)
Definition: RegsSpi.h:100
#define B_SPI_HSFS_CYCLE_FGO
Flash Cycle Go.
Definition: RegsSpi.h:35
#define R_SPI_FREG0_FLASHD
Flash Region 0 (Flash Descriptor) (32bits)
Definition: RegsSpi.h:58
#define R_SPI_FDOC
Flash Descriptor Observability Control Register (32 bits)
Definition: RegsSpi.h:94
#define N_SPI_FREGX_LIMIT_REPR
Region limit bit represents position.
Definition: RegsSpi.h:91
#define B_SPI_HSFS_FDBC_MASK
Flash Data Byte Count ( <= 64), Count = (Value in this field) + 1.
Definition: RegsSpi.h:23
#define B_SPI_FREGX_BASE_MASK
Flash Region Base, [14:0] represents [26:12].
Definition: RegsSpi.h:92
#define B_SPI_FDOC_FDSS_MASK
Flash Descriptor Section Select.
Definition: RegsSpi.h:95
#define V_SPI_HSFS_CYCLE_READ
Flash Cycle Read.
Definition: RegsSpi.h:27
#define B_SPI_FREG4_BASE_MASK
Base, [14:0] here represents base [26:12].
Definition: RegsSpi.h:85
#define B_SPI_FREG0_LIMIT_MASK
Size, [30:16] here represents limit[26:12].
Definition: RegsSpi.h:59
#define N_SPI_FREG0_BASE
Bit 14:0 identifies address bits [26:2].
Definition: RegsSpi.h:62
#define B_SPI_HSFS_FDONE
Flash Cycle Done.
Definition: RegsSpi.h:39
FLASH_CYCLE_TYPE
Definition: SpiCommon.h:37
#define SC_SPI_PRIVATE_DATA_SIGNATURE
Definition: SpiCommon.h:60
#define WAIT_TIME
Wait Time = 6 seconds = 6000000 microseconds.
Definition: SpiCommon.h:31
#define WAIT_PERIOD
Wait Period = 10 microseconds.
Definition: SpiCommon.h:32
BOOLEAN WaitForSpiCycleComplete(IN UINT32 ScSpiBar0, IN BOOLEAN ErrorCheck)
Definition: SpiFlashLib.c:774
EFI_STATUS EFIAPI SpiFlashErase(IN FLASH_REGION_TYPE FlashRegionType, IN UINT32 Address, IN UINT32 ByteCount)
Definition: SpiFlashLib.c:221
SPI_INSTANCE * GetSpiInstance(VOID)
Definition: SpiFlashLib.c:18
EFI_STATUS EFIAPI SpiFlashWriteStatus(IN UINT32 ByteCount, IN UINT8 *StatusValue)
Definition: SpiFlashLib.c:331
EFI_STATUS EFIAPI SpiReadPchSoftStrap(IN UINT32 SoftStrapAddr, IN UINT32 ByteCount, OUT UINT8 *SoftStrapValue)
Definition: SpiFlashLib.c:379
EFI_STATUS EFIAPI SpiConstructor(VOID)
Definition: SpiFlashLib.c:42
EFI_STATUS SendSpiCmd(IN FLASH_REGION_TYPE FlashRegionType, IN FLASH_CYCLE_TYPE FlashCycleType, IN UINT32 Address, IN UINT32 ByteCount, IN OUT UINT8 *Buffer)
Definition: SpiFlashLib.c:419
EFI_STATUS EFIAPI SpiGetRegionAddress(IN FLASH_REGION_TYPE FlashRegionType, OUT UINT32 *BaseAddress OPTIONAL, OUT UINT32 *RegionSize OPTIONAL)
Definition: SpiFlashLib.c:820
EFI_STATUS EFIAPI SpiFlashWrite(IN FLASH_REGION_TYPE FlashRegionType, IN UINT32 Address, IN UINT32 ByteCount, IN UINT8 *Buffer)
Definition: SpiFlashLib.c:195
EFI_STATUS EFIAPI SpiFlashRead(IN FLASH_REGION_TYPE FlashRegionType, IN UINT32 Address, IN UINT32 ByteCount, OUT UINT8 *Buffer)
Definition: SpiFlashLib.c:168
EFI_STATUS EFIAPI SpiFlashReadJedecId(IN UINT8 ComponentNumber, IN UINT32 ByteCount, OUT UINT8 *JedecId)
Definition: SpiFlashLib.c:290
EFI_STATUS EFIAPI SpiFlashReadSfdp(IN UINT8 ComponentNumber, IN UINT32 ByteCount, OUT UINT8 *SfdpData)
Definition: SpiFlashLib.c:247
EFI_STATUS EFIAPI SpiFlashReadStatus(IN UINT32 ByteCount, OUT UINT8 *StatusValue)
Definition: SpiFlashLib.c:354
FLASH_REGION_TYPE
Definition: SpiFlashLib.h:15
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112