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