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