3 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 NAND_PART_INFO_TABLE gNandPartInfoTable
[1] = {
12 { 0x2C, 0xBA, 17, 11 }
15 NAND_FLASH_INFO
*gNandFlashInfo
= NULL
;
17 UINTN gNum512BytesChunks
= 0;
21 // Device path for SemiHosting. It contains our autogened Caller ID GUID.
27 VENDOR_DEVICE_PATH Guid
;
29 EFI_DEVICE_PATH_PROTOCOL End
;
35 FLASH_DEVICE_PATH gDevicePath
= {
37 { HARDWARE_DEVICE_PATH
, HW_VENDOR_DP
, { sizeof (VENDOR_DEVICE_PATH
), 0 } },
40 { END_DEVICE_PATH_TYPE
, END_ENTIRE_DEVICE_PATH_SUBTYPE
, { sizeof (EFI_DEVICE_PATH_PROTOCOL
), 0} }
45 //Actual page address = Column address + Page address + Block address.
47 GetActualPageAddressInBytes (
52 //BlockAddressStart = Start of the Block address in actual NAND
53 //PageAddressStart = Start of the Page address in actual NAND
54 return ((BlockIndex
<< gNandFlashInfo
->BlockAddressStart
) + (PageIndex
<< gNandFlashInfo
->PageAddressStart
));
62 MmioWrite16(GPMC_NAND_COMMAND_0
, Command
);
70 MmioWrite16(GPMC_NAND_ADDRESS_0
, Address
);
78 //Send READ STATUS command
79 NandSendCommand(READ_STATUS_CMD
);
82 return MmioRead16(GPMC_NAND_DATA_0
);
86 NandSendAddressCycles (
91 NandSendAddress(Address
& 0xff);
95 NandSendAddress(Address
& 0x07);
98 //Page and Block address
99 NandSendAddress(Address
& 0xff);
103 NandSendAddress(Address
& 0xff);
107 NandSendAddress(Address
& 0x01);
115 //Enable Smart-idle mode.
116 MmioWrite32 (GPMC_SYSCONFIG
, SMARTIDLEMODE
);
118 //Set IRQSTATUS and IRQENABLE to the reset value
119 MmioWrite32 (GPMC_IRQSTATUS
, 0x0);
120 MmioWrite32 (GPMC_IRQENABLE
, 0x0);
122 //Disable GPMC timeout control.
123 MmioWrite32 (GPMC_TIMEOUT_CONTROL
, TIMEOUTDISABLE
);
125 //Set WRITEPROTECT bit to enable write access.
126 MmioWrite32 (GPMC_CONFIG
, WRITEPROTECT_HIGH
);
128 //NOTE: Following GPMC_CONFIGi_0 register settings are taken from u-boot memory dump.
129 MmioWrite32 (GPMC_CONFIG1_0
, DEVICETYPE_NAND
| DEVICESIZE_X16
);
130 MmioWrite32 (GPMC_CONFIG2_0
, CSRDOFFTIME
| CSWROFFTIME
);
131 MmioWrite32 (GPMC_CONFIG3_0
, ADVRDOFFTIME
| ADVWROFFTIME
);
132 MmioWrite32 (GPMC_CONFIG4_0
, OEONTIME
| OEOFFTIME
| WEONTIME
| WEOFFTIME
);
133 MmioWrite32 (GPMC_CONFIG5_0
, RDCYCLETIME
| WRCYCLETIME
| RDACCESSTIME
| PAGEBURSTACCESSTIME
);
134 MmioWrite32 (GPMC_CONFIG6_0
, WRACCESSTIME
| WRDATAONADMUXBUS
| CYCLE2CYCLEDELAY
| CYCLE2CYCLESAMECSEN
);
135 MmioWrite32 (GPMC_CONFIG7_0
, MASKADDRESS_128MB
| CSVALID
| BASEADDRESS
);
146 BOOLEAN Found
= FALSE
;
148 //Send READ ID command
149 NandSendCommand(READ_ID_CMD
);
151 //Send one address cycle.
154 //Read 5-bytes to idenfity code programmed into the NAND flash devices.
155 //BYTE 0 = Manufacture ID
157 //Byte 2, 3, 4 = Nand part specific information (Page size, Block size etc)
158 for (Index
= 0; Index
< sizeof(PartInfo
); Index
++) {
159 PartInfo
[Index
] = MmioRead16(GPMC_NAND_DATA_0
);
162 //Check if the ManufactureId and DeviceId are part of the currently supported nand parts.
163 for (Index
= 0; Index
< sizeof(gNandPartInfoTable
)/sizeof(NAND_PART_INFO_TABLE
); Index
++) {
164 if (gNandPartInfoTable
[Index
].ManufactureId
== PartInfo
[0] && gNandPartInfoTable
[Index
].DeviceId
== PartInfo
[1]) {
165 gNandFlashInfo
->BlockAddressStart
= gNandPartInfoTable
[Index
].BlockAddressStart
;
166 gNandFlashInfo
->PageAddressStart
= gNandPartInfoTable
[Index
].PageAddressStart
;
172 if (Found
== FALSE
) {
173 DEBUG ((EFI_D_ERROR
, "Nand part is not currently supported. Manufacture id: %x, Device id: %x\n", PartInfo
[0], PartInfo
[1]));
174 return EFI_NOT_FOUND
;
177 //Populate NAND_FLASH_INFO based on the result of READ ID command.
178 gNandFlashInfo
->ManufactureId
= PartInfo
[0];
179 gNandFlashInfo
->DeviceId
= PartInfo
[1];
180 NandInfo
= PartInfo
[3];
182 if (PAGE_SIZE(NandInfo
) == PAGE_SIZE_2K_VAL
) {
183 gNandFlashInfo
->PageSize
= PAGE_SIZE_2K
;
185 DEBUG ((EFI_D_ERROR
, "Unknown Page size.\n"));
186 return EFI_DEVICE_ERROR
;
189 if (SPARE_AREA_SIZE(NandInfo
) == SPARE_AREA_SIZE_64B_VAL
) {
190 gNandFlashInfo
->SparePageSize
= SPARE_AREA_SIZE_64B
;
192 DEBUG ((EFI_D_ERROR
, "Unknown Spare area size.\n"));
193 return EFI_DEVICE_ERROR
;
196 if (BLOCK_SIZE(NandInfo
) == BLOCK_SIZE_128K_VAL
) {
197 gNandFlashInfo
->BlockSize
= BLOCK_SIZE_128K
;
199 DEBUG ((EFI_D_ERROR
, "Unknown Block size.\n"));
200 return EFI_DEVICE_ERROR
;
203 if (ORGANIZATION(NandInfo
) == ORGANIZATION_X8
) {
204 gNandFlashInfo
->Organization
= 0;
205 } else if (ORGANIZATION(NandInfo
) == ORGANIZATION_X16
) {
206 gNandFlashInfo
->Organization
= 1;
209 //Calculate total number of blocks.
210 gNandFlashInfo
->NumPagesPerBlock
= DivU64x32(gNandFlashInfo
->BlockSize
, gNandFlashInfo
->PageSize
);
220 //Define ECC size 0 and size 1 to 512 bytes
221 MmioWrite32 (GPMC_ECC_SIZE_CONFIG
, (ECCSIZE0_512BYTES
| ECCSIZE1_512BYTES
));
229 //Clear all the ECC result registers and select ECC result register 1
230 MmioWrite32 (GPMC_ECC_CONTROL
, (ECCCLEAR
| ECCPOINTER_REG1
));
232 //Enable ECC engine on CS0
233 MmioWrite32 (GPMC_ECC_CONFIG
, (ECCENABLE
| ECCCS_0
| ECC16B
));
241 //Turn off ECC engine.
242 MmioWrite32 (GPMC_ECC_CONFIG
, ECCDISABLE
);
251 UINTN EccResultRegister
;
254 //Capture 32-bit ECC result for each 512-bytes chunk.
255 //In our case PageSize is 2K so read ECC1-ECC4 result registers and
256 //generate total of 12-bytes of ECC code for the particular page.
258 EccResultRegister
= GPMC_ECC1_RESULT
;
260 for (Index
= 0; Index
< gNum512BytesChunks
; Index
++) {
262 EccResult
= MmioRead32 (EccResultRegister
);
264 //Calculate ECC code from 32-bit ECC result value.
265 //NOTE: Following calculation is not part of TRM. We got this information
266 //from Beagleboard mailing list.
267 gEccCode
[Index
* 3] = EccResult
& 0xFF;
268 gEccCode
[(Index
* 3) + 1] = (EccResult
>> 16) & 0xFF;
269 gEccCode
[(Index
* 3) + 2] = (((EccResult
>> 20) & 0xF0) | ((EccResult
>> 8) & 0x0F));
271 //Point to next ECC result register.
272 EccResultRegister
+= 4;
281 OUT UINT8
*SpareBuffer
286 UINTN NumMainAreaWords
= (gNandFlashInfo
->PageSize
/2);
287 UINTN NumSpareAreaWords
= (gNandFlashInfo
->SparePageSize
/2);
288 UINT16
*MainAreaWordBuffer
= Buffer
;
289 UINT16
*SpareAreaWordBuffer
= (UINT16
*)SpareBuffer
;
290 UINTN Timeout
= MAX_RETRY_COUNT
;
292 //Generate device address in bytes to access specific block and page index
293 Address
= GetActualPageAddressInBytes(BlockIndex
, PageIndex
);
296 NandSendCommand(PAGE_READ_CMD
);
298 //Send 5 Address cycles to access specific device address
299 NandSendAddressCycles(Address
);
301 //Send READ CONFIRM command
302 NandSendCommand(PAGE_READ_CONFIRM_CMD
);
304 //Poll till device is busy.
306 if ((NandReadStatus() & NAND_READY
) == NAND_READY
) {
313 DEBUG ((EFI_D_ERROR
, "Read page timed out.\n"));
317 //Reissue READ command
318 NandSendCommand(PAGE_READ_CMD
);
323 //Read data into the buffer.
324 for (Index
= 0; Index
< NumMainAreaWords
; Index
++) {
325 *MainAreaWordBuffer
++ = MmioRead16(GPMC_NAND_DATA_0
);
328 //Read spare area into the buffer.
329 for (Index
= 0; Index
< NumSpareAreaWords
; Index
++) {
330 *SpareAreaWordBuffer
++ = MmioRead16(GPMC_NAND_DATA_0
);
336 //Turn off ECC engine.
339 //Perform ECC correction.
340 //Need to implement..
350 IN UINT8
*SpareBuffer
354 UINT16
*MainAreaWordBuffer
= Buffer
;
355 UINT16
*SpareAreaWordBuffer
= (UINT16
*)SpareBuffer
;
358 UINTN Timeout
= MAX_RETRY_COUNT
;
360 //Generate device address in bytes to access specific block and page index
361 Address
= GetActualPageAddressInBytes(BlockIndex
, PageIndex
);
363 //Send SERIAL DATA INPUT command
364 NandSendCommand(PROGRAM_PAGE_CMD
);
366 //Send 5 Address cycles to access specific device address
367 NandSendAddressCycles(Address
);
372 //Data input from Buffer
373 for (Index
= 0; Index
< (gNandFlashInfo
->PageSize
/2); Index
++) {
374 MmioWrite16(GPMC_NAND_DATA_0
, *MainAreaWordBuffer
++);
376 //After each write access, device has to wait to accept data.
377 //Currently we may not be programming proper timing parameters to
378 //the GPMC_CONFIGi_0 registers and we would need to figure that out.
379 //Without following delay, page programming fails.
386 //Turn off ECC engine.
389 //Prepare Spare area buffer with ECC codes.
390 SetMem(SpareBuffer
, gNandFlashInfo
->SparePageSize
, 0xFF);
391 CopyMem(&SpareBuffer
[ECC_POSITION
], gEccCode
, gNum512BytesChunks
* 3);
393 //Program spare area with calculated ECC.
394 for (Index
= 0; Index
< (gNandFlashInfo
->SparePageSize
/2); Index
++) {
395 MmioWrite16(GPMC_NAND_DATA_0
, *SpareAreaWordBuffer
++);
398 //Send PROGRAM command
399 NandSendCommand(PROGRAM_PAGE_CONFIRM_CMD
);
401 //Poll till device is busy.
404 NandStatus
= NandReadStatus();
405 if ((NandStatus
& NAND_READY
) == NAND_READY
) {
412 DEBUG ((EFI_D_ERROR
, "Program page timed out.\n"));
416 //Bit0 indicates Pass/Fail status
417 if (NandStatus
& NAND_FAILURE
) {
418 return EFI_DEVICE_ERROR
;
431 UINTN Timeout
= MAX_RETRY_COUNT
;
433 //Generate device address in bytes to access specific block and page index
434 Address
= GetActualPageAddressInBytes(BlockIndex
, 0);
436 //Send ERASE SETUP command
437 NandSendCommand(BLOCK_ERASE_CMD
);
439 //Send 3 address cycles to device to access Page address and Block address
440 Address
>>= 11; //Ignore column addresses
442 NandSendAddress(Address
& 0xff);
445 NandSendAddress(Address
& 0xff);
448 NandSendAddress(Address
& 0xff);
450 //Send ERASE CONFIRM command
451 NandSendCommand(BLOCK_ERASE_CONFIRM_CMD
);
453 //Poll till device is busy.
456 NandStatus
= NandReadStatus();
457 if ((NandStatus
& NAND_READY
) == NAND_READY
) {
465 DEBUG ((EFI_D_ERROR
, "Erase block timed out for Block: %d.\n", BlockIndex
));
469 //Bit0 indicates Pass/Fail status
470 if (NandStatus
& NAND_FAILURE
) {
471 return EFI_DEVICE_ERROR
;
479 IN UINTN StartBlockIndex
,
480 IN UINTN EndBlockIndex
,
482 OUT VOID
*SpareBuffer
487 EFI_STATUS Status
= EFI_SUCCESS
;
489 for (BlockIndex
= StartBlockIndex
; BlockIndex
<= EndBlockIndex
; BlockIndex
++) {
490 //For each block read number of pages
491 for (PageIndex
= 0; PageIndex
< gNandFlashInfo
->NumPagesPerBlock
; PageIndex
++) {
492 Status
= NandReadPage(BlockIndex
, PageIndex
, Buffer
, SpareBuffer
);
493 if (EFI_ERROR(Status
)) {
496 Buffer
= ((UINT8
*)Buffer
+ gNandFlashInfo
->PageSize
);
505 IN UINTN StartBlockIndex
,
506 IN UINTN EndBlockIndex
,
508 OUT VOID
*SpareBuffer
513 EFI_STATUS Status
= EFI_SUCCESS
;
515 for (BlockIndex
= StartBlockIndex
; BlockIndex
<= EndBlockIndex
; BlockIndex
++) {
517 for (PageIndex
= 0; PageIndex
< gNandFlashInfo
->NumPagesPerBlock
; PageIndex
++) {
518 Status
= NandWritePage(BlockIndex
, PageIndex
, Buffer
, SpareBuffer
);
519 if (EFI_ERROR(Status
)) {
522 Buffer
= ((UINT8
*)Buffer
+ gNandFlashInfo
->PageSize
);
532 IN EFI_BLOCK_IO_PROTOCOL
*This
,
533 IN BOOLEAN ExtendedVerification
536 UINTN BusyStall
= 50; // microSeconds
537 UINTN ResetBusyTimeout
= (1000000 / BusyStall
); // 1 Second
539 //Send RESET command to device.
540 NandSendCommand(RESET_CMD
);
542 //Wait for 1ms before we check status register.
545 //Check BIT#5 & BIT#6 in Status register to make sure RESET is done.
546 while ((NandReadStatus() & NAND_RESET_STATUS
) != NAND_RESET_STATUS
) {
548 //In case of extended verification, wait for extended amount of time
549 //to make sure device is reset.
550 if (ExtendedVerification
) {
551 if (ResetBusyTimeout
== 0) {
552 return EFI_DEVICE_ERROR
;
555 gBS
->Stall(BusyStall
);
565 NandFlashReadBlocks (
566 IN EFI_BLOCK_IO_PROTOCOL
*This
,
576 UINT8
*SpareBuffer
= NULL
;
578 if (Buffer
== NULL
) {
579 Status
= EFI_INVALID_PARAMETER
;
583 if (Lba
> LAST_BLOCK
) {
584 Status
= EFI_INVALID_PARAMETER
;
588 if ((BufferSize
% gNandFlashInfo
->BlockSize
) != 0) {
589 Status
= EFI_BAD_BUFFER_SIZE
;
593 NumBlocks
= DivU64x32(BufferSize
, gNandFlashInfo
->BlockSize
);
594 EndBlockIndex
= ((UINTN
)Lba
+ NumBlocks
) - 1;
596 SpareBuffer
= (UINT8
*)AllocatePool(gNandFlashInfo
->SparePageSize
);
597 if (SpareBuffer
== NULL
) {
598 Status
= EFI_OUT_OF_RESOURCES
;
603 Status
= NandReadBlock((UINTN
)Lba
, EndBlockIndex
, Buffer
, SpareBuffer
);
604 if (EFI_ERROR(Status
)) {
605 DEBUG((EFI_D_ERROR
, "Read block fails: %x\n", Status
));
610 if (SpareBuffer
!= NULL
) {
611 FreePool (SpareBuffer
);
619 NandFlashWriteBlocks (
620 IN EFI_BLOCK_IO_PROTOCOL
*This
,
631 UINT8
*SpareBuffer
= NULL
;
633 if (Buffer
== NULL
) {
634 Status
= EFI_INVALID_PARAMETER
;
638 if (Lba
> LAST_BLOCK
) {
639 Status
= EFI_INVALID_PARAMETER
;
643 if ((BufferSize
% gNandFlashInfo
->BlockSize
) != 0) {
644 Status
= EFI_BAD_BUFFER_SIZE
;
648 NumBlocks
= DivU64x32(BufferSize
, gNandFlashInfo
->BlockSize
);
649 EndBlockIndex
= ((UINTN
)Lba
+ NumBlocks
) - 1;
651 SpareBuffer
= (UINT8
*)AllocatePool(gNandFlashInfo
->SparePageSize
);
652 if (SpareBuffer
== NULL
) {
653 Status
= EFI_OUT_OF_RESOURCES
;
658 for (BlockIndex
= (UINTN
)Lba
; BlockIndex
<= EndBlockIndex
; BlockIndex
++) {
659 Status
= NandEraseBlock(BlockIndex
);
660 if (EFI_ERROR(Status
)) {
661 DEBUG((EFI_D_ERROR
, "Erase block failed. Status: %x\n", Status
));
667 Status
= NandWriteBlock((UINTN
)Lba
, EndBlockIndex
, Buffer
, SpareBuffer
);
668 if (EFI_ERROR(Status
)) {
669 DEBUG((EFI_D_ERROR
, "Block write fails: %x\n", Status
));
674 if (SpareBuffer
!= NULL
) {
675 FreePool (SpareBuffer
);
683 NandFlashFlushBlocks (
684 IN EFI_BLOCK_IO_PROTOCOL
*This
692 EFI_BLOCK_IO_MEDIA gNandFlashMedia
= {
693 SIGNATURE_32('n','a','n','d'), // MediaId
694 FALSE
, // RemovableMedia
695 TRUE
, // MediaPresent
696 FALSE
, // LogicalPartition
698 FALSE
, // WriteCaching
705 EFI_BLOCK_IO_PROTOCOL BlockIo
=
707 EFI_BLOCK_IO_INTERFACE_REVISION
, // Revision
708 &gNandFlashMedia
, // *Media
709 NandFlashReset
, // Reset
710 NandFlashReadBlocks
, // ReadBlocks
711 NandFlashWriteBlocks
, // WriteBlocks
712 NandFlashFlushBlocks
// FlushBlocks
716 NandFlashInitialize (
717 IN EFI_HANDLE ImageHandle
,
718 IN EFI_SYSTEM_TABLE
*SystemTable
723 gNandFlashInfo
= (NAND_FLASH_INFO
*)AllocateZeroPool (sizeof(NAND_FLASH_INFO
));
725 //Initialize GPMC module.
729 NandFlashReset(&BlockIo
, FALSE
);
731 //Detect NAND part and populate gNandFlashInfo structure
732 Status
= NandDetectPart ();
733 if (EFI_ERROR(Status
)) {
734 DEBUG((EFI_D_ERROR
, "Nand part id detection failure: Status: %x\n", Status
));
738 //Count total number of 512Bytes chunk based on the page size.
739 if (gNandFlashInfo
->PageSize
== PAGE_SIZE_512B
) {
740 gNum512BytesChunks
= 1;
741 } else if (gNandFlashInfo
->PageSize
== PAGE_SIZE_2K
) {
742 gNum512BytesChunks
= 4;
743 } else if (gNandFlashInfo
->PageSize
== PAGE_SIZE_4K
) {
744 gNum512BytesChunks
= 8;
747 gEccCode
= (UINT8
*)AllocatePool(gNum512BytesChunks
* 3);
748 if (gEccCode
== NULL
) {
749 return EFI_OUT_OF_RESOURCES
;
755 //Patch EFI_BLOCK_IO_MEDIA structure.
756 gNandFlashMedia
.BlockSize
= gNandFlashInfo
->BlockSize
;
757 gNandFlashMedia
.LastBlock
= LAST_BLOCK
;
760 Status
= gBS
->InstallMultipleProtocolInterfaces (
762 &gEfiBlockIoProtocolGuid
, &BlockIo
,
763 &gEfiDevicePathProtocolGuid
, &gDevicePath
,