]> git.proxmox.com Git - mirror_edk2.git/blame - UefiPayloadPkg/Library/SpiFlashLib/PchSpi.c
UefiPayloadPkg: Add SpiFlashLib
[mirror_edk2.git] / UefiPayloadPkg / Library / SpiFlashLib / PchSpi.c
CommitLineData
1d66480a
GD
1/** @file\r
2\r
3 Copyright (c) 2017-2021, Intel Corporation. All rights reserved.<BR>\r
4 SPDX-License-Identifier: BSD-2-Clause-Patent\r
5\r
6**/\r
7#include "SpiCommon.h"\r
8\r
9/**\r
10 Acquire SPI MMIO BAR.\r
11\r
12 @param[in] PchSpiBase PCH SPI PCI Base Address\r
13\r
14 @retval Return SPI BAR Address\r
15\r
16**/\r
17UINT32\r
18AcquireSpiBar0 (\r
19 IN UINTN PchSpiBase\r
20 )\r
21{\r
22 return MmioRead32 (PchSpiBase + R_SPI_BASE) & ~(B_SPI_BAR0_MASK);\r
23}\r
24\r
25/**\r
26 Release SPI MMIO BAR. Do nothing.\r
27\r
28 @param[in] PchSpiBase PCH SPI PCI Base Address\r
29\r
30**/\r
31VOID\r
32ReleaseSpiBar0 (\r
33 IN UINTN PchSpiBase\r
34 )\r
35{\r
36}\r
37\r
38\r
39\r
40/**\r
41 This function is to enable/disable BIOS Write Protect in SMM phase.\r
42\r
43 @param[in] EnableSmmSts Flag to Enable/disable Bios write protect\r
44\r
45**/\r
46VOID\r
47CpuSmmDisableBiosWriteProtect (\r
48 IN BOOLEAN EnableSmmSts\r
49 )\r
50{\r
51 UINT32 Data32;\r
52\r
53 if(EnableSmmSts){\r
54 //\r
55 // Disable BIOS Write Protect in SMM phase.\r
56 //\r
57 Data32 = MmioRead32 ((UINTN) (0xFED30880)) | (UINT32) (BIT0);\r
58 AsmWriteMsr32 (0x000001FE, Data32);\r
59 } else {\r
60 //\r
61 // Enable BIOS Write Protect in SMM phase\r
62 //\r
63 Data32 = MmioRead32 ((UINTN) (0xFED30880)) & (UINT32) (~BIT0);\r
64 AsmWriteMsr32 (0x000001FE, Data32);\r
65 }\r
66\r
67 //\r
68 // Read FED30880h back to ensure the setting went through.\r
69 //\r
70 Data32 = MmioRead32 (0xFED30880);\r
71}\r
72\r
73\r
74/**\r
75 This function is a hook for Spi to disable BIOS Write Protect.\r
76\r
77 @param[in] PchSpiBase PCH SPI PCI Base Address\r
78 @param[in] CpuSmmBwp Need to disable CPU SMM Bios write protection or not\r
79\r
80 @retval EFI_SUCCESS The protocol instance was properly initialized\r
81 @retval EFI_ACCESS_DENIED The BIOS Region can only be updated in SMM phase\r
82\r
83**/\r
84EFI_STATUS\r
85EFIAPI\r
86DisableBiosWriteProtect (\r
87 IN UINTN PchSpiBase,\r
88 IN UINT8 CpuSmmBwp\r
89 )\r
90{\r
91\r
92 //\r
93 // Write clear BC_SYNC_SS prior to change WPD from 0 to 1.\r
94 //\r
95 MmioOr8 (PchSpiBase + R_SPI_BCR + 1, (B_SPI_BCR_SYNC_SS >> 8));\r
96\r
97 //\r
98 // Enable the access to the BIOS space for both read and write cycles\r
99 //\r
100 MmioOr8 (PchSpiBase + R_SPI_BCR, B_SPI_BCR_BIOSWE);\r
101\r
102 if (CpuSmmBwp != 0) {\r
103 CpuSmmDisableBiosWriteProtect (TRUE);\r
104 }\r
105\r
106 return EFI_SUCCESS;\r
107}\r
108\r
109/**\r
110 This function is a hook for Spi to enable BIOS Write Protect.\r
111\r
112 @param[in] PchSpiBase PCH SPI PCI Base Address\r
113 @param[in] CpuSmmBwp Need to disable CPU SMM Bios write protection or not\r
114\r
115**/\r
116VOID\r
117EFIAPI\r
118EnableBiosWriteProtect (\r
119 IN UINTN PchSpiBase,\r
120 IN UINT8 CpuSmmBwp\r
121 )\r
122{\r
123\r
124 //\r
125 // Disable the access to the BIOS space for write cycles\r
126 //\r
127 MmioAnd8 (PchSpiBase + R_SPI_BCR, (UINT8) (~B_SPI_BCR_BIOSWE));\r
128\r
129 if (CpuSmmBwp != 0) {\r
130 CpuSmmDisableBiosWriteProtect (FALSE);\r
131 }\r
132}\r
133\r
134/**\r
135 This function disables SPI Prefetching and caching,\r
136 and returns previous BIOS Control Register value before disabling.\r
137\r
138 @param[in] PchSpiBase PCH SPI PCI Base Address\r
139\r
140 @retval Previous BIOS Control Register value\r
141\r
142**/\r
143UINT8\r
144SaveAndDisableSpiPrefetchCache (\r
145 IN UINTN PchSpiBase\r
146 )\r
147{\r
148 UINT8 BiosCtlSave;\r
149\r
150 BiosCtlSave = MmioRead8 (PchSpiBase + R_SPI_BCR) & B_SPI_BCR_SRC;\r
151\r
152 MmioAndThenOr32 (PchSpiBase + R_SPI_BCR, \\r
153 (UINT32) (~B_SPI_BCR_SRC), \\r
154 (UINT32) (V_SPI_BCR_SRC_PREF_DIS_CACHE_DIS << B_SPI_BCR_SRC));\r
155\r
156 return BiosCtlSave;\r
157}\r
158\r
159/**\r
160 This function updates BIOS Control Register with the given value.\r
161\r
162 @param[in] PchSpiBase PCH SPI PCI Base Address\r
163 @param[in] BiosCtlValue BIOS Control Register Value to be updated\r
164\r
165**/\r
166VOID\r
167SetSpiBiosControlRegister (\r
168 IN UINTN PchSpiBase,\r
169 IN UINT8 BiosCtlValue\r
170 )\r
171{\r
172 MmioAndThenOr8 (PchSpiBase + R_SPI_BCR, (UINT8) ~B_SPI_BCR_SRC, BiosCtlValue);\r
173}\r