1 /** @file NorFlashDxe.h
3 Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #ifndef __NOR_FLASH_DXE_H__
10 #define __NOR_FLASH_DXE_H__
16 #include <Guid/EventGroup.h>
18 #include <Protocol/BlockIo.h>
19 #include <Protocol/DiskIo.h>
20 #include <Protocol/FirmwareVolumeBlock.h>
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>
28 #define NOR_FLASH_ERASE_RETRY 10
30 // Device access macros
31 // These are necessary because we use 2 x 16bit parts to make up 32bit data
33 #define HIGH_16_BITS 0xFFFF0000
34 #define LOW_16_BITS 0x0000FFFF
35 #define LOW_8_BITS 0x000000FF
37 #define FOLD_32BIT_INTO_16BIT(value) ( ( value >> 16 ) | ( value & LOW_16_BITS ) )
39 #define GET_LOW_BYTE(value) ( value & LOW_8_BITS )
40 #define GET_HIGH_BYTE(value) ( GET_LOW_BYTE( value >> 16 ) )
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) )
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)
59 // Device Commands for Intel StrataFlash(R) Embedded Memory (P30) Family
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
70 #define P30_CFI_ADDR_QUERY_UNIQUE_QRY 0x10
71 #define P30_CFI_ADDR_VENDOR_ID 0x13
74 #define CFI_QRY 0x00595251
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
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
92 #define P30_CMD_BLOCK_ERASE_SETUP 0x0020
93 #define P30_CMD_BLOCK_ERASE_CONFIRM 0x00D0
96 #define P30_CMD_PROGRAM_OR_ERASE_SUSPEND 0x00B0
97 #define P30_CMD_SUSPEND_RESUME 0x00D0
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
105 // PROTECTION Commands
106 #define P30_CMD_PROGRAM_PROTECTION_REGISTER_SETUP 0x00C0
108 // CONFIGURATION Commands
109 #define P30_CMD_READ_CONFIGURATION_REGISTER_SETUP 0x0060
110 #define P30_CMD_READ_CONFIGURATION_REGISTER 0x0003
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)
117 typedef struct _NOR_FLASH_INSTANCE NOR_FLASH_INSTANCE
;
121 VENDOR_DEVICE_PATH Vendor
;
123 EFI_DEVICE_PATH_PROTOCOL End
;
124 } NOR_FLASH_DEVICE_PATH
;
127 struct _NOR_FLASH_INSTANCE
{
131 UINTN DeviceBaseAddress
;
132 UINTN RegionBaseAddress
;
136 EFI_BLOCK_IO_PROTOCOL BlockIoProtocol
;
137 EFI_BLOCK_IO_MEDIA Media
;
138 EFI_DISK_IO_PROTOCOL DiskIoProtocol
;
140 EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL FvbProtocol
;
143 NOR_FLASH_DEVICE_PATH DevicePath
;
147 NorFlashReadCfiData (
148 IN UINTN DeviceBaseAddress
,
150 IN UINT32 NumberOfBytes
,
155 NorFlashWriteBuffer (
156 IN NOR_FLASH_INSTANCE
*Instance
,
157 IN UINTN TargetAddress
,
158 IN UINTN BufferSizeInBytes
,
163 // BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.Reset
167 NorFlashBlockIoReset (
168 IN EFI_BLOCK_IO_PROTOCOL
*This
,
169 IN BOOLEAN ExtendedVerification
173 // BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.ReadBlocks
177 NorFlashBlockIoReadBlocks (
178 IN EFI_BLOCK_IO_PROTOCOL
*This
,
181 IN UINTN BufferSizeInBytes
,
186 // BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.WriteBlocks
190 NorFlashBlockIoWriteBlocks (
191 IN EFI_BLOCK_IO_PROTOCOL
*This
,
194 IN UINTN BufferSizeInBytes
,
199 // BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.FlushBlocks
203 NorFlashBlockIoFlushBlocks (
204 IN EFI_BLOCK_IO_PROTOCOL
*This
208 // DiskIO Protocol function EFI_DISK_IO_PROTOCOL.ReadDisk
212 NorFlashDiskIoReadDisk (
213 IN EFI_DISK_IO_PROTOCOL
*This
,
221 // DiskIO Protocol function EFI_DISK_IO_PROTOCOL.WriteDisk
225 NorFlashDiskIoWriteDisk (
226 IN EFI_DISK_IO_PROTOCOL
*This
,
239 NorFlashFvbInitialize (
240 IN NOR_FLASH_INSTANCE
* Instance
246 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
247 OUT EFI_FVB_ATTRIBUTES_2
*Attributes
253 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
254 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
259 FvbGetPhysicalAddress(
260 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
261 OUT EFI_PHYSICAL_ADDRESS
*Address
267 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
269 OUT UINTN
*BlockSize
,
270 OUT UINTN
*NumberOfBlocks
276 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
279 IN OUT UINTN
*NumBytes
,
286 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
289 IN OUT UINTN
*NumBytes
,
296 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
305 NorFlashUnlockAndEraseSingleBlock (
306 IN NOR_FLASH_INSTANCE
*Instance
,
307 IN UINTN BlockAddress
311 NorFlashWriteSingleBlock (
312 IN NOR_FLASH_INSTANCE
*Instance
,
315 IN OUT UINTN
*NumBytes
,
320 NorFlashWriteBlocks (
321 IN NOR_FLASH_INSTANCE
*Instance
,
323 IN UINTN BufferSizeInBytes
,
329 IN NOR_FLASH_INSTANCE
*Instance
,
331 IN UINTN BufferSizeInBytes
,
337 IN NOR_FLASH_INSTANCE
*Instance
,
340 IN UINTN BufferSizeInBytes
,
346 IN NOR_FLASH_INSTANCE
*Instance
,
349 IN OUT UINTN
*NumBytes
,
355 IN NOR_FLASH_INSTANCE
*Instance
358 #endif /* __NOR_FLASH_DXE_H__ */