1 | /** @file
|
---|
2 | Header file for the SPI flash module.
|
---|
3 |
|
---|
4 | Copyright (c) 2017 - 2021, Intel Corporation. All rights reserved.<BR>
|
---|
5 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
6 |
|
---|
7 | **/
|
---|
8 |
|
---|
9 | #ifndef SPI_COMMON_LIB_H_
|
---|
10 | #define SPI_COMMON_LIB_H_
|
---|
11 |
|
---|
12 | #include <PiDxe.h>
|
---|
13 | #include <Uefi/UefiBaseType.h>
|
---|
14 | #include <IndustryStandard/Pci30.h>
|
---|
15 | #include <Library/IoLib.h>
|
---|
16 | #include <Library/DebugLib.h>
|
---|
17 | #include <Library/BaseMemoryLib.h>
|
---|
18 | #include <Library/SpiFlashLib.h>
|
---|
19 | #include <Library/MemoryAllocationLib.h>
|
---|
20 | #include <Library/BaseLib.h>
|
---|
21 | #include <Library/HobLib.h>
|
---|
22 | #include <Library/TimerLib.h>
|
---|
23 | #include <Guid/SpiFlashInfoGuid.h>
|
---|
24 | #include "RegsSpi.h"
|
---|
25 |
|
---|
26 | ///
|
---|
27 | /// Maximum time allowed while waiting the SPI cycle to complete
|
---|
28 | /// Wait Time = 6 seconds = 6000000 microseconds
|
---|
29 | /// Wait Period = 10 microseconds
|
---|
30 | ///
|
---|
31 | #define WAIT_TIME 6000000 ///< Wait Time = 6 seconds = 6000000 microseconds
|
---|
32 | #define WAIT_PERIOD 10 ///< Wait Period = 10 microseconds
|
---|
33 |
|
---|
34 | ///
|
---|
35 | /// Flash cycle Type
|
---|
36 | ///
|
---|
37 | typedef enum {
|
---|
38 | FlashCycleRead,
|
---|
39 | FlashCycleWrite,
|
---|
40 | FlashCycleErase,
|
---|
41 | FlashCycleReadSfdp,
|
---|
42 | FlashCycleReadJedecId,
|
---|
43 | FlashCycleWriteStatus,
|
---|
44 | FlashCycleReadStatus,
|
---|
45 | FlashCycleMax
|
---|
46 | } FLASH_CYCLE_TYPE;
|
---|
47 |
|
---|
48 | ///
|
---|
49 | /// Flash Component Number
|
---|
50 | ///
|
---|
51 | typedef enum {
|
---|
52 | FlashComponent0,
|
---|
53 | FlashComponent1,
|
---|
54 | FlashComponentMax
|
---|
55 | } FLASH_COMPONENT_NUM;
|
---|
56 |
|
---|
57 | ///
|
---|
58 | /// Private data structure definitions for the driver
|
---|
59 | ///
|
---|
60 | #define SC_SPI_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('P', 'S', 'P', 'I')
|
---|
61 |
|
---|
62 | typedef struct {
|
---|
63 | UINTN Signature;
|
---|
64 | EFI_HANDLE Handle;
|
---|
65 | UINT32 AcpiTmrReg;
|
---|
66 | UINTN PchSpiBase;
|
---|
67 | UINT16 RegionPermission;
|
---|
68 | UINT32 SfdpVscc0Value;
|
---|
69 | UINT32 SfdpVscc1Value;
|
---|
70 | UINT32 StrapBaseAddress;
|
---|
71 | UINT8 NumberOfComponents;
|
---|
72 | UINT16 Flags;
|
---|
73 | UINT32 Component1StartAddr;
|
---|
74 | } SPI_INSTANCE;
|
---|
75 |
|
---|
76 | /**
|
---|
77 | Acquire SPI MMIO BAR
|
---|
78 |
|
---|
79 | @param[in] PchSpiBase PCH SPI PCI Base Address
|
---|
80 |
|
---|
81 | @retval Return SPI BAR Address
|
---|
82 |
|
---|
83 | **/
|
---|
84 | UINT32
|
---|
85 | AcquireSpiBar0 (
|
---|
86 | IN UINTN PchSpiBase
|
---|
87 | );
|
---|
88 |
|
---|
89 | /**
|
---|
90 | Release SPI MMIO BAR. Do nothing.
|
---|
91 |
|
---|
92 | @param[in] PchSpiBase PCH SPI PCI Base Address
|
---|
93 |
|
---|
94 | @retval None
|
---|
95 |
|
---|
96 | **/
|
---|
97 | VOID
|
---|
98 | ReleaseSpiBar0 (
|
---|
99 | IN UINTN PchSpiBase
|
---|
100 | );
|
---|
101 |
|
---|
102 | /**
|
---|
103 | This function is a hook for Spi to disable BIOS Write Protect
|
---|
104 |
|
---|
105 | @param[in] PchSpiBase PCH SPI PCI Base Address
|
---|
106 | @param[in] CpuSmmBwp Need to disable CPU SMM Bios write protection or not
|
---|
107 |
|
---|
108 | @retval EFI_SUCCESS The protocol instance was properly initialized
|
---|
109 | @retval EFI_ACCESS_DENIED The BIOS Region can only be updated in SMM phase
|
---|
110 |
|
---|
111 | **/
|
---|
112 | EFI_STATUS
|
---|
113 | EFIAPI
|
---|
114 | DisableBiosWriteProtect (
|
---|
115 | IN UINTN PchSpiBase,
|
---|
116 | IN UINT8 CpuSmmBwp
|
---|
117 | );
|
---|
118 |
|
---|
119 | /**
|
---|
120 | This function is a hook for Spi to enable BIOS Write Protect
|
---|
121 |
|
---|
122 | @param[in] PchSpiBase PCH SPI PCI Base Address
|
---|
123 | @param[in] CpuSmmBwp Need to disable CPU SMM Bios write protection or not
|
---|
124 |
|
---|
125 | @retval None
|
---|
126 |
|
---|
127 | **/
|
---|
128 | VOID
|
---|
129 | EFIAPI
|
---|
130 | EnableBiosWriteProtect (
|
---|
131 | IN UINTN PchSpiBase,
|
---|
132 | IN UINT8 CpuSmmBwp
|
---|
133 | );
|
---|
134 |
|
---|
135 | /**
|
---|
136 | This function disables SPI Prefetching and caching,
|
---|
137 | and returns previous BIOS Control Register value before disabling.
|
---|
138 |
|
---|
139 | @param[in] PchSpiBase PCH SPI PCI Base Address
|
---|
140 |
|
---|
141 | @retval Previous BIOS Control Register value
|
---|
142 |
|
---|
143 | **/
|
---|
144 | UINT8
|
---|
145 | SaveAndDisableSpiPrefetchCache (
|
---|
146 | IN UINTN PchSpiBase
|
---|
147 | );
|
---|
148 |
|
---|
149 | /**
|
---|
150 | This function updates BIOS Control Register with the given value.
|
---|
151 |
|
---|
152 | @param[in] PchSpiBase PCH SPI PCI Base Address
|
---|
153 | @param[in] BiosCtlValue BIOS Control Register Value to be updated
|
---|
154 |
|
---|
155 | @retval None
|
---|
156 |
|
---|
157 | **/
|
---|
158 | VOID
|
---|
159 | SetSpiBiosControlRegister (
|
---|
160 | IN UINTN PchSpiBase,
|
---|
161 | IN UINT8 BiosCtlValue
|
---|
162 | );
|
---|
163 |
|
---|
164 | /**
|
---|
165 | This function sends the programmed SPI command to the slave device.
|
---|
166 |
|
---|
167 | @param[in] SpiRegionType The SPI Region type for flash cycle which is listed in the Descriptor
|
---|
168 | @param[in] FlashCycleType The Flash SPI cycle type list in HSFC (Hardware Sequencing Flash Control Register) register
|
---|
169 | @param[in] Address The Flash Linear Address must fall within a region for which BIOS has access permissions.
|
---|
170 | @param[in] ByteCount Number of bytes in the data portion of the SPI cycle.
|
---|
171 | @param[in,out] Buffer Pointer to caller-allocated buffer containing the data received or sent during the SPI cycle.
|
---|
172 |
|
---|
173 | @retval EFI_SUCCESS SPI command completes successfully.
|
---|
174 | @retval EFI_DEVICE_ERROR Device error, the command aborts abnormally.
|
---|
175 | @retval EFI_ACCESS_DENIED Some unrecognized command encountered in hardware sequencing mode
|
---|
176 | @retval EFI_INVALID_PARAMETER The parameters specified are not valid.
|
---|
177 | **/
|
---|
178 | EFI_STATUS
|
---|
179 | SendSpiCmd (
|
---|
180 | IN FLASH_REGION_TYPE FlashRegionType,
|
---|
181 | IN FLASH_CYCLE_TYPE FlashCycleType,
|
---|
182 | IN UINT32 Address,
|
---|
183 | IN UINT32 ByteCount,
|
---|
184 | IN OUT UINT8 *Buffer
|
---|
185 | );
|
---|
186 |
|
---|
187 | /**
|
---|
188 | Wait execution cycle to complete on the SPI interface.
|
---|
189 |
|
---|
190 | @param[in] PchSpiBar0 Spi MMIO base address
|
---|
191 | @param[in] ErrorCheck TRUE if the SpiCycle needs to do the error check
|
---|
192 |
|
---|
193 | @retval TRUE SPI cycle completed on the interface.
|
---|
194 | @retval FALSE Time out while waiting the SPI cycle to complete.
|
---|
195 | It's not safe to program the next command on the SPI interface.
|
---|
196 | **/
|
---|
197 | BOOLEAN
|
---|
198 | WaitForSpiCycleComplete (
|
---|
199 | IN UINT32 PchSpiBar0,
|
---|
200 | IN BOOLEAN ErrorCheck
|
---|
201 | );
|
---|
202 |
|
---|
203 | #endif
|
---|