]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtNorFlashDxe/VirtNorFlash.h
OvmfPkg/VirtNorFlashDxe: drop block I/O protocol implementation
[mirror_edk2.git] / OvmfPkg / VirtNorFlashDxe / VirtNorFlash.h
1 /** @file NorFlash.h
2
3 Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.<BR>
4
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #ifndef __VIRT_NOR_FLASH__
10 #define __VIRT_NOR_FLASH__
11
12 #include <Base.h>
13 #include <PiDxe.h>
14
15 #include <Guid/EventGroup.h>
16
17 #include <Protocol/FirmwareVolumeBlock.h>
18
19 #include <Library/DebugLib.h>
20 #include <Library/IoLib.h>
21 #include <Library/UefiLib.h>
22 #include <Library/UefiRuntimeLib.h>
23 #include <Library/VirtNorFlashPlatformLib.h>
24
25 #define NOR_FLASH_ERASE_RETRY 10
26
27 // Device access macros
28 // These are necessary because we use 2 x 16bit parts to make up 32bit data
29
30 #define HIGH_16_BITS 0xFFFF0000
31 #define LOW_16_BITS 0x0000FFFF
32 #define LOW_8_BITS 0x000000FF
33
34 #define FOLD_32BIT_INTO_16BIT(value) ( ( value >> 16 ) | ( value & LOW_16_BITS ) )
35
36 #define GET_LOW_BYTE(value) ( value & LOW_8_BITS )
37 #define GET_HIGH_BYTE(value) ( GET_LOW_BYTE( value >> 16 ) )
38
39 // Each command must be sent simultaneously to both chips,
40 // i.e. at the lower 16 bits AND at the higher 16 bits
41 #define CREATE_NOR_ADDRESS(BaseAddr, OffsetAddr) ((BaseAddr) + ((OffsetAddr) << 2))
42 #define CREATE_DUAL_CMD(Cmd) ( ( Cmd << 16) | ( Cmd & LOW_16_BITS) )
43 #define SEND_NOR_COMMAND(BaseAddr, Offset, Cmd) MmioWrite32 (CREATE_NOR_ADDRESS(BaseAddr,Offset), CREATE_DUAL_CMD(Cmd))
44 #define GET_NOR_BLOCK_ADDRESS(BaseAddr, Lba, LbaSize) ( BaseAddr + (UINTN)((Lba) * LbaSize) )
45
46 // Status Register Bits
47 #define P30_SR_BIT_WRITE (BIT7 << 16 | BIT7)
48 #define P30_SR_BIT_ERASE_SUSPEND (BIT6 << 16 | BIT6)
49 #define P30_SR_BIT_ERASE (BIT5 << 16 | BIT5)
50 #define P30_SR_BIT_PROGRAM (BIT4 << 16 | BIT4)
51 #define P30_SR_BIT_VPP (BIT3 << 16 | BIT3)
52 #define P30_SR_BIT_PROGRAM_SUSPEND (BIT2 << 16 | BIT2)
53 #define P30_SR_BIT_BLOCK_LOCKED (BIT1 << 16 | BIT1)
54 #define P30_SR_BIT_BEFP (BIT0 << 16 | BIT0)
55
56 // Device Commands for Intel StrataFlash(R) Embedded Memory (P30) Family
57
58 // On chip buffer size for buffered programming operations
59 // There are 2 chips, each chip can buffer up to 32 (16-bit)words, and each word is 2 bytes.
60 // Therefore the total size of the buffer is 2 x 32 x 2 = 128 bytes
61 #define P30_MAX_BUFFER_SIZE_IN_BYTES ((UINTN)128)
62 #define P30_MAX_BUFFER_SIZE_IN_WORDS (P30_MAX_BUFFER_SIZE_IN_BYTES/((UINTN)4))
63 #define MAX_BUFFERED_PROG_ITERATIONS 10000000
64 #define BOUNDARY_OF_32_WORDS 0x7F
65
66 // CFI Addresses
67 #define P30_CFI_ADDR_QUERY_UNIQUE_QRY 0x10
68 #define P30_CFI_ADDR_VENDOR_ID 0x13
69
70 // CFI Data
71 #define CFI_QRY 0x00595251
72
73 // READ Commands
74 #define P30_CMD_READ_DEVICE_ID 0x0090
75 #define P30_CMD_READ_STATUS_REGISTER 0x0070
76 #define P30_CMD_CLEAR_STATUS_REGISTER 0x0050
77 #define P30_CMD_READ_ARRAY 0x00FF
78 #define P30_CMD_READ_CFI_QUERY 0x0098
79
80 // WRITE Commands
81 #define P30_CMD_WORD_PROGRAM_SETUP 0x0040
82 #define P30_CMD_ALTERNATE_WORD_PROGRAM_SETUP 0x0010
83 #define P30_CMD_BUFFERED_PROGRAM_SETUP 0x00E8
84 #define P30_CMD_BUFFERED_PROGRAM_CONFIRM 0x00D0
85 #define P30_CMD_BEFP_SETUP 0x0080
86 #define P30_CMD_BEFP_CONFIRM 0x00D0
87
88 // ERASE Commands
89 #define P30_CMD_BLOCK_ERASE_SETUP 0x0020
90 #define P30_CMD_BLOCK_ERASE_CONFIRM 0x00D0
91
92 // SUSPEND Commands
93 #define P30_CMD_PROGRAM_OR_ERASE_SUSPEND 0x00B0
94 #define P30_CMD_SUSPEND_RESUME 0x00D0
95
96 // BLOCK LOCKING / UNLOCKING Commands
97 #define P30_CMD_LOCK_BLOCK_SETUP 0x0060
98 #define P30_CMD_LOCK_BLOCK 0x0001
99 #define P30_CMD_UNLOCK_BLOCK 0x00D0
100 #define P30_CMD_LOCK_DOWN_BLOCK 0x002F
101
102 // PROTECTION Commands
103 #define P30_CMD_PROGRAM_PROTECTION_REGISTER_SETUP 0x00C0
104
105 // CONFIGURATION Commands
106 #define P30_CMD_READ_CONFIGURATION_REGISTER_SETUP 0x0060
107 #define P30_CMD_READ_CONFIGURATION_REGISTER 0x0003
108
109 #define NOR_FLASH_SIGNATURE SIGNATURE_32('n', 'o', 'r', '0')
110 #define INSTANCE_FROM_FVB_THIS(a) CR(a, NOR_FLASH_INSTANCE, FvbProtocol, NOR_FLASH_SIGNATURE)
111
112 typedef struct _NOR_FLASH_INSTANCE NOR_FLASH_INSTANCE;
113
114 #pragma pack (1)
115 typedef struct {
116 VENDOR_DEVICE_PATH Vendor;
117 UINT8 Index;
118 EFI_DEVICE_PATH_PROTOCOL End;
119 } NOR_FLASH_DEVICE_PATH;
120 #pragma pack ()
121
122 struct _NOR_FLASH_INSTANCE {
123 UINT32 Signature;
124 EFI_HANDLE Handle;
125
126 UINTN DeviceBaseAddress;
127 UINTN RegionBaseAddress;
128 UINTN Size;
129 EFI_LBA StartLba;
130 EFI_LBA LastBlock;
131 UINT32 BlockSize;
132
133 EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL FvbProtocol;
134 VOID *ShadowBuffer;
135
136 NOR_FLASH_DEVICE_PATH DevicePath;
137 };
138
139 EFI_STATUS
140 NorFlashReadCfiData (
141 IN UINTN DeviceBaseAddress,
142 IN UINTN CFI_Offset,
143 IN UINT32 NumberOfBytes,
144 OUT UINT32 *Data
145 );
146
147 EFI_STATUS
148 NorFlashWriteBuffer (
149 IN NOR_FLASH_INSTANCE *Instance,
150 IN UINTN TargetAddress,
151 IN UINTN BufferSizeInBytes,
152 IN UINT32 *Buffer
153 );
154
155 //
156 // NorFlashFvbDxe.c
157 //
158
159 EFI_STATUS
160 EFIAPI
161 FvbGetAttributes (
162 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
163 OUT EFI_FVB_ATTRIBUTES_2 *Attributes
164 );
165
166 EFI_STATUS
167 EFIAPI
168 FvbSetAttributes (
169 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
170 IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
171 );
172
173 EFI_STATUS
174 EFIAPI
175 FvbGetPhysicalAddress (
176 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
177 OUT EFI_PHYSICAL_ADDRESS *Address
178 );
179
180 EFI_STATUS
181 EFIAPI
182 FvbGetBlockSize (
183 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
184 IN EFI_LBA Lba,
185 OUT UINTN *BlockSize,
186 OUT UINTN *NumberOfBlocks
187 );
188
189 EFI_STATUS
190 EFIAPI
191 FvbRead (
192 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
193 IN EFI_LBA Lba,
194 IN UINTN Offset,
195 IN OUT UINTN *NumBytes,
196 IN OUT UINT8 *Buffer
197 );
198
199 EFI_STATUS
200 EFIAPI
201 FvbWrite (
202 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
203 IN EFI_LBA Lba,
204 IN UINTN Offset,
205 IN OUT UINTN *NumBytes,
206 IN UINT8 *Buffer
207 );
208
209 EFI_STATUS
210 EFIAPI
211 FvbEraseBlocks (
212 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
213 ...
214 );
215
216 EFI_STATUS
217 ValidateFvHeader (
218 IN NOR_FLASH_INSTANCE *Instance
219 );
220
221 EFI_STATUS
222 InitializeFvAndVariableStoreHeaders (
223 IN NOR_FLASH_INSTANCE *Instance
224 );
225
226 VOID
227 EFIAPI
228 FvbVirtualNotifyEvent (
229 IN EFI_EVENT Event,
230 IN VOID *Context
231 );
232
233 //
234 // NorFlashDxe.c
235 //
236
237 EFI_STATUS
238 NorFlashWriteFullBlock (
239 IN NOR_FLASH_INSTANCE *Instance,
240 IN EFI_LBA Lba,
241 IN UINT32 *DataBuffer,
242 IN UINT32 BlockSizeInWords
243 );
244
245 EFI_STATUS
246 NorFlashUnlockAndEraseSingleBlock (
247 IN NOR_FLASH_INSTANCE *Instance,
248 IN UINTN BlockAddress
249 );
250
251 EFI_STATUS
252 NorFlashCreateInstance (
253 IN UINTN NorFlashDeviceBase,
254 IN UINTN NorFlashRegionBase,
255 IN UINTN NorFlashSize,
256 IN UINT32 Index,
257 IN UINT32 BlockSize,
258 IN BOOLEAN SupportFvb,
259 OUT NOR_FLASH_INSTANCE **NorFlashInstance
260 );
261
262 EFI_STATUS
263 EFIAPI
264 NorFlashFvbInitialize (
265 IN NOR_FLASH_INSTANCE *Instance
266 );
267
268 //
269 // NorFlash.c
270 //
271 EFI_STATUS
272 NorFlashWriteSingleBlock (
273 IN NOR_FLASH_INSTANCE *Instance,
274 IN EFI_LBA Lba,
275 IN UINTN Offset,
276 IN OUT UINTN *NumBytes,
277 IN UINT8 *Buffer
278 );
279
280 EFI_STATUS
281 NorFlashWriteBlocks (
282 IN NOR_FLASH_INSTANCE *Instance,
283 IN EFI_LBA Lba,
284 IN UINTN BufferSizeInBytes,
285 IN VOID *Buffer
286 );
287
288 EFI_STATUS
289 NorFlashReadBlocks (
290 IN NOR_FLASH_INSTANCE *Instance,
291 IN EFI_LBA Lba,
292 IN UINTN BufferSizeInBytes,
293 OUT VOID *Buffer
294 );
295
296 EFI_STATUS
297 NorFlashRead (
298 IN NOR_FLASH_INSTANCE *Instance,
299 IN EFI_LBA Lba,
300 IN UINTN Offset,
301 IN UINTN BufferSizeInBytes,
302 OUT VOID *Buffer
303 );
304
305 EFI_STATUS
306 NorFlashWrite (
307 IN NOR_FLASH_INSTANCE *Instance,
308 IN EFI_LBA Lba,
309 IN UINTN Offset,
310 IN OUT UINTN *NumBytes,
311 IN UINT8 *Buffer
312 );
313
314 EFI_STATUS
315 NorFlashReset (
316 IN NOR_FLASH_INSTANCE *Instance
317 );
318
319 EFI_STATUS
320 NorFlashEraseSingleBlock (
321 IN NOR_FLASH_INSTANCE *Instance,
322 IN UINTN BlockAddress
323 );
324
325 EFI_STATUS
326 NorFlashUnlockSingleBlockIfNecessary (
327 IN NOR_FLASH_INSTANCE *Instance,
328 IN UINTN BlockAddress
329 );
330
331 EFI_STATUS
332 NorFlashWriteSingleWord (
333 IN NOR_FLASH_INSTANCE *Instance,
334 IN UINTN WordAddress,
335 IN UINT32 WriteData
336 );
337
338 VOID
339 EFIAPI
340 NorFlashVirtualNotifyEvent (
341 IN EFI_EVENT Event,
342 IN VOID *Context
343 );
344
345 #endif /* __VIRT_NOR_FLASH__ */