1 /** @file NorFlashDxe.c
3 Copyright (c) 2010, ARM Ltd. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 #include <Library/UefiLib.h>
15 #include <Library/DebugLib.h>
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/MemoryAllocationLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
20 #include "NorFlashDxe.h"
24 // Global variable declarations
27 #define NOR_FLASH_LAST_DEVICE 4
29 NOR_FLASH_DESCRIPTION mNorFlashDescription
[NOR_FLASH_LAST_DEVICE
] = {
35 {0xE7223039, 0x5836, 0x41E1, 0xB5, 0x42, 0xD7, 0xEC, 0x73, 0x6C, 0x5E, 0x59}
37 { // BootMon non-volatile storage
38 ARM_VE_SMB_NOR0_BASE
+ SIZE_256KB
* 255,
42 {0x02118005, 0x9DA7, 0x443A, 0x92, 0xD5, 0x78, 0x1F, 0x02, 0x2A, 0xED, 0xBB}
49 {0x1F15DA3C, 0x37FF, 0x4070, 0xB4, 0x71, 0xBB, 0x4A, 0xF1, 0x2A, 0x72, 0x4A}
51 { // UEFI Variable Services non-volatile storage
52 ARM_VE_SMB_NOR1_BASE
+ SIZE_256KB
* 255,
53 SIZE_64KB
* 3, //FIXME: Set 3 blocks because I did not succeed to copy 4 blocks into the ARM Versastile Express NOR Falsh in the last NOR Flash. It should be 4 blocks
56 {0xCC2CBF29, 0x1498, 0x4CDD, 0x81, 0x71, 0xF8, 0xB6, 0xB4, 0x1D, 0x09, 0x09}
60 NOR_FLASH_INSTANCE
*mNorFlashInstances
[ NOR_FLASH_LAST_DEVICE
];
62 NOR_FLASH_INSTANCE mNorFlashInstanceTemplate
= {
63 NOR_FLASH_SIGNATURE
, // Signature
64 NULL
, // Handle ... NEED TO BE FILLED
69 0, // BaseAddress ... NEED TO BE FILLED
70 0, // Size ... NEED TO BE FILLED
73 EFI_BLOCK_IO_PROTOCOL_REVISION2
, // Revision
74 NULL
, // Media ... NEED TO BE FILLED
75 NorFlashBlockIoReset
, // Reset;
76 NorFlashBlockIoReadBlocks
, // ReadBlocks
77 NorFlashBlockIoWriteBlocks
, // WriteBlocks
78 NorFlashBlockIoFlushBlocks
// FlushBlocks
82 0, // MediaId ... NEED TO BE FILLED
83 FALSE
, // RemovableMedia
85 FALSE
, // LogicalPartition
87 FALSE
, // WriteCaching;
88 0, // BlockSize ... NEED TO BE FILLED
90 0, // LastBlock ... NEED TO BE FILLED
91 0, // LowestAlignedLba
92 1, // LogicalBlocksPerPhysicalBlock
95 FALSE
, // SupportFvb ... NEED TO BE FILLED
97 FvbGetAttributes
, // GetAttributes
98 FvbSetAttributes
, // SetAttributes
99 FvbGetPhysicalAddress
, // GetPhysicalAddress
100 FvbGetBlockSize
, // GetBlockSize
103 FvbEraseBlocks
, // EraseBlocks
110 HARDWARE_DEVICE_PATH
,
112 (UINT8
)( sizeof(VENDOR_DEVICE_PATH
) ),
113 (UINT8
)((sizeof(VENDOR_DEVICE_PATH
)) >> 8),
115 { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }, // GUID ... NEED TO BE FILLED
118 END_DEVICE_PATH_TYPE
,
119 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
120 sizeof (EFI_DEVICE_PATH_PROTOCOL
),
126 EFI_STATUS
NorFlashCreateInstance(
127 IN UINTN NorFlashBase
,
128 IN UINTN NorFlashSize
,
131 IN BOOLEAN SupportFvb
,
132 IN CONST GUID
*NorFlashGuid
,
133 OUT NOR_FLASH_INSTANCE
** NorFlashInstance
136 NOR_FLASH_INSTANCE
* Instance
;
138 ASSERT(NorFlashInstance
!= NULL
);
140 Instance
= AllocateCopyPool (sizeof(NOR_FLASH_INSTANCE
),&mNorFlashInstanceTemplate
);
141 if (Instance
== NULL
) {
142 return EFI_OUT_OF_RESOURCES
;
145 Instance
->BaseAddress
= NorFlashBase
;
146 Instance
->Size
= NorFlashSize
;
148 Instance
->BlockIoProtocol
.Media
= &Instance
->Media
;
149 Instance
->Media
.MediaId
= MediaId
;
150 Instance
->Media
.BlockSize
= BlockSize
;
151 Instance
->Media
.LastBlock
= (NorFlashSize
/ BlockSize
)-1;
153 CopyGuid (&Instance
->DevicePath
.Vendor
.Guid
,NorFlashGuid
);
156 Instance
->SupportFvb
= TRUE
;
157 Instance
->Initialize
= NorFlashFvbInitialize
;
159 Status
= gBS
->InstallMultipleProtocolInterfaces (
161 &gEfiDevicePathProtocolGuid
, &Instance
->DevicePath
,
162 //&gEfiBlockIoProtocolGuid, &Instance->BlockIoProtocol,
163 &gEfiFirmwareVolumeBlockProtocolGuid
, &Instance
->FvbProtocol
,
166 if (EFI_ERROR(Status
)) {
171 Instance
->Initialize
= NorFlashBlkIoInitialize
;
173 Status
= gBS
->InstallMultipleProtocolInterfaces (
175 &gEfiDevicePathProtocolGuid
, &Instance
->DevicePath
,
176 &gEfiBlockIoProtocolGuid
, &Instance
->BlockIoProtocol
,
179 if (EFI_ERROR(Status
)) {
185 *NorFlashInstance
= Instance
;
190 NorFlashReadCfiData (
191 IN UINTN BaseAddress
,
193 IN UINT32 NumberOfBytes
,
198 volatile UINTN
*ReadAddress
;
202 UINT32 CombinedData
= 0;
203 EFI_STATUS Status
= EFI_SUCCESS
;
206 if( NumberOfBytes
> 4 ) {
207 // Using 32 bit variable so can only read 4 bytes
208 return EFI_INVALID_PARAMETER
;
211 // First combine the base address with the offset address
212 // to create an absolute read address.
213 // However, because we are in little endian, read from the last address down to the first
214 ReadAddress
= CREATE_NOR_ADDRESS( BaseAddress
, CFI_Offset
) + NumberOfBytes
- 1;
216 // Although each read returns 32 bits, because of the NOR Flash structure,
217 // each 16 bits (16 MSB and 16 LSB) come from two different chips.
218 // When in CFI mode, each chip read returns valid data in only the 8 LSBits;
219 // the 8 MSBits are invalid and can be ignored.
220 // Therefore, each read address returns one byte from each chip.
222 // Also note: As we are in little endian notation and we are reading
223 // bytes from incremental addresses, we should assemble them in little endian order.
224 for( CurrentByte
=0; CurrentByte
<NumberOfBytes
; CurrentByte
++ ) {
226 // Read the bytes from the two chips
227 ReadData
= *ReadAddress
;
229 // Check the data validity:
230 // The 'Dual Data' function means that
231 // each chip should return identical data.
232 // If that is not the case then we have a problem.
233 Byte1
= GET_LOW_BYTE ( ReadData
);
234 Byte2
= GET_HIGH_BYTE( ReadData
);
236 if( Byte1
!= Byte2
) {
237 // The two bytes should have been identical
238 return EFI_DEVICE_ERROR
;
241 // Each successive iteration of the 'for' loop reads a lower address.
242 // As we read lower addresses and as we use little endian,
243 // we read lower significance bytes. So combine them in the correct order.
244 CombinedData
= (CombinedData
<< 8) | Byte1
;
246 // Decrement down to the next address
251 *Data
= CombinedData
;
257 NorFlashReadStatusRegister(
261 volatile UINT32
*pStatusRegister
;
262 UINT32 StatusRegister
;
264 EFI_STATUS Status
= EFI_SUCCESS
;
266 // Prepare the read address
267 pStatusRegister
= (UINT32
*) SR_Address
;
270 // Prepare to read the status register
271 SEND_NOR_COMMAND( SR_Address
, 0, P30_CMD_READ_STATUS_REGISTER
);
272 // Snapshot the status register
273 StatusRegister
= *pStatusRegister
;
275 // The chip is busy while the WRITE bit is not asserted
276 while ( (StatusRegister
& P30_SR_BIT_WRITE
) != P30_SR_BIT_WRITE
);
279 // Perform a full status check:
280 // Mask the relevant bits of Status Register.
281 // Everything should be zero, if not, we have a problem
283 // Prepare the Error Mask by setting bits 5, 4, 3, 1
284 ErrorMask
= P30_SR_BIT_ERASE
| P30_SR_BIT_PROGRAM
| P30_SR_BIT_VPP
| P30_SR_BIT_BLOCK_LOCKED
;
286 if ( (StatusRegister
& ErrorMask
) != 0 ) {
287 if ( (StatusRegister
& P30_SR_BIT_VPP
) != 0 ) {
288 DEBUG((EFI_D_ERROR
,"NorFlashReadStatusRegister: VPP Range Error\n"));
289 } else if ( (StatusRegister
& (P30_SR_BIT_ERASE
| P30_SR_BIT_PROGRAM
) ) != 0 ) {
290 DEBUG((EFI_D_ERROR
,"NorFlashReadStatusRegister: Command Sequence Error\n"));
291 } else if ( (StatusRegister
& P30_SR_BIT_PROGRAM
) != 0 ) {
292 DEBUG((EFI_D_ERROR
,"NorFlashReadStatusRegister: Program Error\n"));
293 } else if ( (StatusRegister
& P30_SR_BIT_BLOCK_LOCKED
) != 0 ) {
294 DEBUG((EFI_D_ERROR
,"NorFlashReadStatusRegister: Device Protect Error\n"));
296 DEBUG((EFI_D_ERROR
,"NorFlashReadStatusRegister: Error (0x%X)\n",Status
));
299 // If an error is detected we must clear the Status Register
300 SEND_NOR_COMMAND( SR_Address
, 0, P30_CMD_CLEAR_STATUS_REGISTER
);
301 Status
= EFI_DEVICE_ERROR
;
304 SEND_NOR_COMMAND( SR_Address
, 0, P30_CMD_READ_ARRAY
);
311 NorFlashBlockIsLocked(
312 IN UINTN BlockAddress
315 volatile UINT32
*pReadData
;
317 BOOLEAN BlockIsLocked
= TRUE
;
319 // Prepare the read address
320 pReadData
= (UINT32
*) CREATE_NOR_ADDRESS( BlockAddress
, 2 );
322 // Send command for reading device id
323 SEND_NOR_COMMAND( BlockAddress
, 2, P30_CMD_READ_DEVICE_ID
);
325 // Read block lock status
326 LockStatus
= *pReadData
;
328 // Decode block lock status
329 LockStatus
= FOLD_32BIT_INTO_16BIT(LockStatus
);
331 if( (LockStatus
& 0x2) != 0 ) {
332 DEBUG((EFI_D_ERROR
, "UnlockSingleBlock: WARNING: Block LOCKED DOWN\n"));
335 if( (LockStatus
& 0x1) == 0 ) {
336 // This means the block is unlocked
337 DEBUG((DEBUG_BLKIO
, "UnlockSingleBlock: Block 0x%08x unlocked\n", BlockAddress
));
338 BlockIsLocked
= FALSE
;
341 return BlockIsLocked
;
346 NorFlashUnlockSingleBlock(
347 IN UINTN BlockAddress
350 EFI_STATUS Status
= EFI_SUCCESS
;
352 // Raise the Task Priority Level to TPL_NOTIFY to serialise all its operations
353 // and to protect shared data structures.
355 //while( NorFlashBlockIsLocked( BlockAddress ) )
357 // Request a lock setup
358 SEND_NOR_COMMAND( BlockAddress
, 0, P30_CMD_LOCK_BLOCK_SETUP
);
361 SEND_NOR_COMMAND( BlockAddress
, 0, P30_CMD_UNLOCK_BLOCK
);
364 // Put device back into Read Array mode
365 SEND_NOR_COMMAND( BlockAddress
, 0, P30_CMD_READ_ARRAY
);
367 DEBUG((DEBUG_BLKIO
, "UnlockSingleBlock: BlockAddress=0x%08x, Exit Status = \"%r\".\n", BlockAddress
, Status
));
374 NorFlashUnlockSingleBlockIfNecessary(
375 IN UINTN BlockAddress
378 EFI_STATUS Status
= EFI_SUCCESS
;
380 if ( NorFlashBlockIsLocked( BlockAddress
) == TRUE
) {
381 Status
= NorFlashUnlockSingleBlock( BlockAddress
);
389 * The following function presumes that the block has already been unlocked.
392 NorFlashEraseSingleBlock(
393 IN UINTN BlockAddress
396 EFI_STATUS Status
= EFI_SUCCESS
;
398 // Request a block erase and then confirm it
399 SEND_NOR_COMMAND( BlockAddress
, 0, P30_CMD_BLOCK_ERASE_SETUP
);
400 SEND_NOR_COMMAND( BlockAddress
, 0, P30_CMD_BLOCK_ERASE_CONFIRM
);
401 // Wait until the status register gives us the all clear
402 Status
= NorFlashReadStatusRegister( BlockAddress
);
404 if (EFI_ERROR(Status
)) {
405 DEBUG((DEBUG_BLKIO
, "EraseSingleBlock(BlockAddress=0x%08x) = '%r'\n", BlockAddress
, Status
));
411 * The following function presumes that the block has already been unlocked.
414 NorFlashUnlockAndEraseSingleBlock(
415 IN UINTN BlockAddress
420 // Unlock the block if we have to
421 Status
= NorFlashUnlockSingleBlockIfNecessary( BlockAddress
);
422 if (!EFI_ERROR(Status
)) {
423 Status
= NorFlashEraseSingleBlock( BlockAddress
);
431 NorFlashWriteSingleWord (
432 IN UINTN WordAddress
,
437 volatile UINT32
*Data
;
439 // Prepare the read address
440 Data
= (UINT32
*)WordAddress
;
442 // Request a write single word command
443 SEND_NOR_COMMAND( WordAddress
, 0, P30_CMD_WORD_PROGRAM_SETUP
);
445 // Store the word into NOR Flash;
448 // Wait for the write to complete and then check for any errors; i.e. check the Status Register
449 Status
= NorFlashReadStatusRegister( WordAddress
);
455 * Writes data to the NOR Flash using the Buffered Programming method.
457 * The maximum size of the on-chip buffer is 32-words, because of hardware restrictions.
458 * Therefore this function will only handle buffers up to 32 words or 128 bytes.
459 * To deal with larger buffers, call this function again.
461 * This function presumes that both the TargetAddress and the TargetAddress+BufferSize
462 * exist entirely within the NOR Flash. Therefore these conditions will not be checked here.
464 * In buffered programming, if the target address not at the beginning of a 32-bit word boundary,
465 * then programming time is doubled and power consumption is increased.
466 * Therefore, it is a requirement to align buffer writes to 32-bit word boundaries.
467 * i.e. the last 4 bits of the target start address must be zero: 0x......00
470 NorFlashWriteBuffer (
471 IN UINTN TargetAddress
,
472 IN UINTN BufferSizeInBytes
,
477 UINTN BufferSizeInWords
;
479 volatile UINT32
*Data
;
480 UINTN WaitForBuffer
= MAX_BUFFERED_PROG_ITERATIONS
;
481 BOOLEAN BufferAvailable
= FALSE
;
484 // Check that the target address does not cross a 32-word boundary.
485 if ( (TargetAddress
& BOUNDARY_OF_32_WORDS
) != 0 ) {
486 return EFI_INVALID_PARAMETER
;
489 // Check there are some data to program
490 if ( BufferSizeInBytes
== 0 ) {
491 return EFI_BUFFER_TOO_SMALL
;
494 // Check that the buffer size does not exceed the maximum hardware buffer size on chip.
495 if ( BufferSizeInBytes
> P30_MAX_BUFFER_SIZE_IN_BYTES
) {
496 return EFI_BAD_BUFFER_SIZE
;
499 // Check that the buffer size is a multiple of 32-bit words
500 if ( (BufferSizeInBytes
% 4) != 0 ) {
501 return EFI_BAD_BUFFER_SIZE
;
504 // Pre-programming conditions checked, now start the algorithm.
506 // Prepare the data destination address
507 Data
= (UINT32
*)TargetAddress
;
509 // Check the availability of the buffer
511 // Issue the Buffered Program Setup command
512 SEND_NOR_COMMAND( TargetAddress
, 0, P30_CMD_BUFFERED_PROGRAM_SETUP
);
514 // Read back the status register bit#7 from the same address
515 if ( ((*Data
) & P30_SR_BIT_WRITE
) == P30_SR_BIT_WRITE
) {
516 BufferAvailable
= TRUE
;
519 // Update the loop counter
522 } while (( WaitForBuffer
> 0 ) && ( BufferAvailable
== FALSE
));
524 // The buffer was not available for writing
525 if ( WaitForBuffer
== 0 ) {
526 return EFI_DEVICE_ERROR
;
529 // From now on we work in 32-bit words
530 BufferSizeInWords
= BufferSizeInBytes
/ (UINTN
)4;
532 // Write the word count, which is (buffer_size_in_words - 1),
533 // because word count 0 means one word.
534 SEND_NOR_COMMAND( TargetAddress
, 0, (BufferSizeInWords
- 1) );
536 // Write the data to the NOR Flash, advancing each address by 4 bytes
537 for( Count
=0; Count
<BufferSizeInWords
; Count
++, Data
++, Buffer
++ ) {
541 // Issue the Buffered Program Confirm command, to start the programming operation
542 SEND_NOR_COMMAND( TargetAddress
, 0, P30_CMD_BUFFERED_PROGRAM_CONFIRM
);
544 // Wait for the write to complete and then check for any errors; i.e. check the Status Register
545 Status
= NorFlashReadStatusRegister( TargetAddress
);
551 NorFlashWriteSingleBlock (
552 IN UINTN DeviceBaseAddress
,
554 IN UINT32
*DataBuffer
,
555 IN UINT32 BlockSizeInWords
558 EFI_STATUS Status
= EFI_SUCCESS
;
563 UINTN BuffersInBlock
;
564 UINTN RemainingWords
;
566 // Get the physical address of the block
567 BlockAddress
= GET_NOR_BLOCK_ADDRESS(DeviceBaseAddress
, Lba
, BlockSizeInWords
* 4);
569 Status
= NorFlashUnlockAndEraseSingleBlock( BlockAddress
);
570 if (EFI_ERROR(Status
)) {
571 DEBUG((EFI_D_ERROR
, "WriteSingleBlock: ERROR - Failed to Unlock and Erase the single block at 0x%X\n", BlockAddress
));
575 // To speed up the programming operation, NOR Flash is programmed using the Buffered Programming method.
577 // Start writing from the first address at the start of the block
578 WordAddress
= BlockAddress
;
580 // Check that the address starts at a 32-word boundary, i.e. last 7 bits must be zero
581 if ( (WordAddress
& BOUNDARY_OF_32_WORDS
) == 0x00 ) {
583 // First, break the entire block into buffer-sized chunks.
584 BuffersInBlock
= (UINTN
)BlockSizeInWords
/ P30_MAX_BUFFER_SIZE_IN_BYTES
;
586 // Then feed each buffer chunk to the NOR Flash
588 BufferIndex
< BuffersInBlock
;
589 BufferIndex
++, WordAddress
+= P30_MAX_BUFFER_SIZE_IN_BYTES
, DataBuffer
+= P30_MAX_BUFFER_SIZE_IN_WORDS
591 Status
= NorFlashWriteBuffer ( WordAddress
, P30_MAX_BUFFER_SIZE_IN_BYTES
, DataBuffer
);
592 if (EFI_ERROR(Status
)) {
597 // Finally, finish off any remaining words that are less than the maximum size of the buffer
598 RemainingWords
= BlockSizeInWords
% P30_MAX_BUFFER_SIZE_IN_WORDS
;
600 if( RemainingWords
!= 0) {
601 Status
= NorFlashWriteBuffer ( WordAddress
, (RemainingWords
* 4), DataBuffer
);
602 if (EFI_ERROR(Status
)) {
608 // For now, use the single word programming algorithm
609 // It is unlikely that the NOR Flash will exist in an address which falls within a 32 word boundary range,
610 // i.e. which ends in the range 0x......01 - 0x......7F.
611 for( WordIndex
=0; WordIndex
<BlockSizeInWords
; WordIndex
++, DataBuffer
++, WordAddress
= WordAddress
+ 4 ) {
612 Status
= NorFlashWriteSingleWord( WordAddress
, *DataBuffer
);
613 if (EFI_ERROR(Status
)) {
620 if (EFI_ERROR(Status
)) {
621 DEBUG((EFI_D_ERROR
, "NOR FLASH Programming [WriteSingleBlock] failed at address 0x%08x. Exit Status = \"%r\".\n", WordAddress
, Status
));
628 NorFlashWriteBlocks (
629 IN NOR_FLASH_INSTANCE
*Instance
,
631 IN UINTN BufferSizeInBytes
,
635 UINT32
*pWriteBuffer
;
636 EFI_STATUS Status
= EFI_SUCCESS
;
637 EFI_LBA CurrentBlock
;
638 UINT32 BlockSizeInWords
;
641 volatile UINT32
*VersatileExpress_SYS_FLASH
;
643 // The buffer must be valid
644 if (Buffer
== NULL
) {
645 return EFI_INVALID_PARAMETER
;
648 if( Instance
->Media
.ReadOnly
== TRUE
) {
649 return EFI_WRITE_PROTECTED
;
652 // We must have some bytes to read
653 DEBUG((DEBUG_BLKIO
, "NorFlashWriteBlocks: BufferSizeInBytes=0x%x\n", BufferSizeInBytes
));
654 if( BufferSizeInBytes
== 0 ) {
655 return EFI_BAD_BUFFER_SIZE
;
658 // The size of the buffer must be a multiple of the block size
659 DEBUG((DEBUG_BLKIO
, "NorFlashWriteBlocks: BlockSize in bytes =0x%x\n", Instance
->Media
.BlockSize
));
660 if ((BufferSizeInBytes
% Instance
->Media
.BlockSize
) != 0) {
661 return EFI_BAD_BUFFER_SIZE
;
664 // All blocks must be within the device
665 NumBlocks
= ((UINT32
)BufferSizeInBytes
) / Instance
->Media
.BlockSize
;
667 DEBUG((DEBUG_BLKIO
, "NorFlashWriteBlocks: NumBlocks=%d, LastBlock=%ld, Lba=%ld.\n", NumBlocks
, Instance
->Media
.LastBlock
, Lba
));
669 if ( ( Lba
+ NumBlocks
) > ( Instance
->Media
.LastBlock
+ 1 ) ) {
670 DEBUG((EFI_D_ERROR
, "NorFlashWriteBlocks: ERROR - Write will exceed last block.\n"));
671 return EFI_INVALID_PARAMETER
;
674 // Everything seems ok so far, so now we need to disable the platform-specific
675 // flash write protection for Versatile Express
676 VersatileExpress_SYS_FLASH
= (UINT32
*)VE_REGISTER_SYS_FLASH_ADDR
;
677 if( (*VersatileExpress_SYS_FLASH
& 0x1) == 0 ) {
678 // Writing to NOR FLASH is disabled, so enable it
679 *VersatileExpress_SYS_FLASH
= 0x1;
680 DEBUG((DEBUG_BLKIO
, "NorFlashWriteBlocks: informational - Had to enable HSYS_FLASH flag.\n" ));
683 BlockSizeInWords
= Instance
->Media
.BlockSize
/ 4;
685 // Because the target *Buffer is a pointer to VOID, we must put all the data into a pointer
686 // to a proper data type, so use *ReadBuffer
687 pWriteBuffer
= (UINT32
*)Buffer
;
690 for( BlockCount
=0; BlockCount
<NumBlocks
; BlockCount
++, CurrentBlock
++, pWriteBuffer
= pWriteBuffer
+ BlockSizeInWords
) {
692 DEBUG((DEBUG_BLKIO
, "NorFlashWriteBlocks: Writing block #%d\n", (UINTN
)CurrentBlock
));
694 Status
= NorFlashWriteSingleBlock( Instance
->BaseAddress
, CurrentBlock
, pWriteBuffer
, BlockSizeInWords
);
696 if (EFI_ERROR(Status
)) {
702 DEBUG((DEBUG_BLKIO
, "NorFlashWriteBlocks: Exit Status = \"%r\".\n", Status
));
709 IN NOR_FLASH_INSTANCE
*Instance
,
711 IN UINTN BufferSizeInBytes
,
718 // The buffer must be valid
719 if (Buffer
== NULL
) {
720 return EFI_INVALID_PARAMETER
;
723 // We must have some bytes to read
724 DEBUG((DEBUG_BLKIO
, "NorFlashReadBlocks: BufferSize=0x%x bytes.\n", BufferSizeInBytes
));
725 if( BufferSizeInBytes
== 0 ) {
726 return EFI_BAD_BUFFER_SIZE
;
729 // The size of the buffer must be a multiple of the block size
730 DEBUG((DEBUG_BLKIO
, "NorFlashReadBlocks: BlockSize=0x%x bytes.\n", Instance
->Media
.BlockSize
));
731 if ((BufferSizeInBytes
% Instance
->Media
.BlockSize
) != 0) {
732 return EFI_BAD_BUFFER_SIZE
;
735 // All blocks must be within the device
736 NumBlocks
= ((UINT32
)BufferSizeInBytes
) / Instance
->Media
.BlockSize
;
738 DEBUG((DEBUG_BLKIO
, "NorFlashReadBlocks: NumBlocks=%d, LastBlock=%ld, Lba=%ld\n", NumBlocks
, Instance
->Media
.LastBlock
, Lba
));
740 if ( ( Lba
+ NumBlocks
) > (Instance
->Media
.LastBlock
+ 1) ) {
741 DEBUG((EFI_D_ERROR
, "NorFlashReadBlocks: ERROR - Read will exceed last block\n"));
742 return EFI_INVALID_PARAMETER
;
745 // Get the address to start reading from
746 StartAddress
= GET_NOR_BLOCK_ADDRESS( Instance
->BaseAddress
,
748 Instance
->Media
.BlockSize
751 // Put the device into Read Array mode
752 SEND_NOR_COMMAND( Instance
->BaseAddress
, 0, P30_CMD_READ_ARRAY
);
755 CopyMem(Buffer
, (UINTN
*)StartAddress
, BufferSizeInBytes
);
763 IN NOR_FLASH_INSTANCE
*Instance
766 DEBUG((DEBUG_BLKIO
, "NorFlashReset(BaseAddress=0x%08x)\n", Instance
->BaseAddress
));
768 // As there is no specific RESET to perform, ensure that the devices is in the default Read Array mode
769 SEND_NOR_COMMAND( Instance
->BaseAddress
, 0, P30_CMD_READ_ARRAY
);
779 IN EFI_HANDLE ImageHandle
,
780 IN EFI_SYSTEM_TABLE
*SystemTable
783 EFI_STATUS Status
= EFI_SUCCESS
;
786 for (Index
= 0; Index
< NOR_FLASH_LAST_DEVICE
; Index
++) {
787 Status
= NorFlashCreateInstance(
788 mNorFlashDescription
[Index
].BaseAddress
,
789 mNorFlashDescription
[Index
].Size
,
791 mNorFlashDescription
[Index
].BlockSize
,
792 mNorFlashDescription
[Index
].SupportFvb
,
793 &mNorFlashDescription
[Index
].Guid
,
794 &mNorFlashInstances
[Index
]
796 if (EFI_ERROR(Status
)) {
797 DEBUG((EFI_D_ERROR
,"NorFlashInitialise: Fail to create instance for NorFlash[%d]\n",Index
));