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