3 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 NAND_PART_INFO_TABLE gNandPartInfoTable
[1] = {
18 { 0x2C, 0xBA, 17, 11 }
21 NAND_FLASH_INFO
*gNandFlashInfo
= NULL
;
23 UINTN gNum512BytesChunks
= 0;
27 // Device path for SemiHosting. It contains our autogened Caller ID GUID.
33 VENDOR_DEVICE_PATH Guid
;
35 EFI_DEVICE_PATH_PROTOCOL End
;
41 FLASH_DEVICE_PATH gDevicePath
= {
45 { HARDWARE_DEVICE_PATH
, HW_VENDOR_DP
, sizeof (VENDOR_DEVICE_PATH
), 0 },
51 { END_DEVICE_PATH_TYPE
, END_ENTIRE_DEVICE_PATH_SUBTYPE
, sizeof (EFI_DEVICE_PATH_PROTOCOL
), 0}
57 //Actual page address = Column address + Page address + Block address.
59 GetActualPageAddressInBytes (
64 //BlockAddressStart = Start of the Block address in actual NAND
65 //PageAddressStart = Start of the Page address in actual NAND
66 return ((BlockIndex
<< gNandFlashInfo
->BlockAddressStart
) + (PageIndex
<< gNandFlashInfo
->PageAddressStart
));
74 MmioWrite16(GPMC_NAND_COMMAND_0
, Command
);
82 MmioWrite16(GPMC_NAND_ADDRESS_0
, Address
);
90 //Send READ STATUS command
91 NandSendCommand(READ_STATUS_CMD
);
94 return MmioRead16(GPMC_NAND_DATA_0
);
98 NandSendAddressCycles (
103 NandSendAddress(Address
& 0xff);
107 NandSendAddress(Address
& 0x07);
110 //Page and Block address
111 NandSendAddress(Address
& 0xff);
115 NandSendAddress(Address
& 0xff);
119 NandSendAddress(Address
& 0x01);
127 //Enable Smart-idle mode.
128 MmioWrite32 (GPMC_SYSCONFIG
, SMARTIDLEMODE
);
130 //Set IRQSTATUS and IRQENABLE to the reset value
131 MmioWrite32 (GPMC_IRQSTATUS
, 0x0);
132 MmioWrite32 (GPMC_IRQENABLE
, 0x0);
134 //Disable GPMC timeout control.
135 MmioWrite32 (GPMC_TIMEOUT_CONTROL
, TIMEOUTDISABLE
);
137 //Set WRITEPROTECT bit to enable write access.
138 MmioWrite32 (GPMC_CONFIG
, WRITEPROTECT_HIGH
);
140 //NOTE: Following GPMC_CONFIGi_0 register settings are taken from u-boot memory dump.
141 MmioWrite32 (GPMC_CONFIG1_0
, DEVICETYPE_NAND
| DEVICESIZE_X16
);
142 MmioWrite32 (GPMC_CONFIG2_0
, CSRDOFFTIME
| CSWROFFTIME
);
143 MmioWrite32 (GPMC_CONFIG3_0
, ADVRDOFFTIME
| ADVWROFFTIME
);
144 MmioWrite32 (GPMC_CONFIG4_0
, OEONTIME
| OEOFFTIME
| WEONTIME
| WEOFFTIME
);
145 MmioWrite32 (GPMC_CONFIG5_0
, RDCYCLETIME
| WRCYCLETIME
| RDACCESSTIME
| PAGEBURSTACCESSTIME
);
146 MmioWrite32 (GPMC_CONFIG6_0
, WRACCESSTIME
| WRDATAONADMUXBUS
| CYCLE2CYCLEDELAY
| CYCLE2CYCLESAMECSEN
);
147 MmioWrite32 (GPMC_CONFIG7_0
, MASKADDRESS_128MB
| CSVALID
| BASEADDRESS
);
158 BOOLEAN Found
= FALSE
;
160 //Send READ ID command
161 NandSendCommand(READ_ID_CMD
);
163 //Send one address cycle.
166 //Read 5-bytes to idenfity code programmed into the NAND flash devices.
167 //BYTE 0 = Manufacture ID
169 //Byte 2, 3, 4 = Nand part specific information (Page size, Block size etc)
170 for (Index
= 0; Index
< sizeof(PartInfo
); Index
++) {
171 PartInfo
[Index
] = MmioRead16(GPMC_NAND_DATA_0
);
174 //Check if the ManufactureId and DeviceId are part of the currently supported nand parts.
175 for (Index
= 0; Index
< sizeof(gNandPartInfoTable
)/sizeof(NAND_PART_INFO_TABLE
); Index
++) {
176 if (gNandPartInfoTable
[Index
].ManufactureId
== PartInfo
[0] && gNandPartInfoTable
[Index
].DeviceId
== PartInfo
[1]) {
177 gNandFlashInfo
->BlockAddressStart
= gNandPartInfoTable
[Index
].BlockAddressStart
;
178 gNandFlashInfo
->PageAddressStart
= gNandPartInfoTable
[Index
].PageAddressStart
;
184 if (Found
== FALSE
) {
185 DEBUG ((EFI_D_ERROR
, "Nand part is not currently supported. Manufacture id: %x, Device id: %x\n", PartInfo
[0], PartInfo
[1]));
186 return EFI_NOT_FOUND
;
189 //Populate NAND_FLASH_INFO based on the result of READ ID command.
190 gNandFlashInfo
->ManufactureId
= PartInfo
[0];
191 gNandFlashInfo
->DeviceId
= PartInfo
[1];
192 NandInfo
= PartInfo
[3];
194 if (PAGE_SIZE(NandInfo
) == PAGE_SIZE_2K_VAL
) {
195 gNandFlashInfo
->PageSize
= PAGE_SIZE_2K
;
197 DEBUG ((EFI_D_ERROR
, "Unknown Page size.\n"));
198 return EFI_DEVICE_ERROR
;
201 if (SPARE_AREA_SIZE(NandInfo
) == SPARE_AREA_SIZE_64B_VAL
) {
202 gNandFlashInfo
->SparePageSize
= SPARE_AREA_SIZE_64B
;
204 DEBUG ((EFI_D_ERROR
, "Unknown Spare area size.\n"));
205 return EFI_DEVICE_ERROR
;
208 if (BLOCK_SIZE(NandInfo
) == BLOCK_SIZE_128K_VAL
) {
209 gNandFlashInfo
->BlockSize
= BLOCK_SIZE_128K
;
211 DEBUG ((EFI_D_ERROR
, "Unknown Block size.\n"));
212 return EFI_DEVICE_ERROR
;
215 if (ORGANIZATION(NandInfo
) == ORGANIZATION_X8
) {
216 gNandFlashInfo
->Organization
= 0;
217 } else if (ORGANIZATION(NandInfo
) == ORGANIZATION_X16
) {
218 gNandFlashInfo
->Organization
= 1;
221 //Calculate total number of blocks.
222 gNandFlashInfo
->NumPagesPerBlock
= DivU64x32(gNandFlashInfo
->BlockSize
, gNandFlashInfo
->PageSize
);
232 //Define ECC size 0 and size 1 to 512 bytes
233 MmioWrite32 (GPMC_ECC_SIZE_CONFIG
, (ECCSIZE0_512BYTES
| ECCSIZE1_512BYTES
));
241 //Clear all the ECC result registers and select ECC result register 1
242 MmioWrite32 (GPMC_ECC_CONTROL
, (ECCCLEAR
| ECCPOINTER_REG1
));
244 //Enable ECC engine on CS0
245 MmioWrite32 (GPMC_ECC_CONFIG
, (ECCENABLE
| ECCCS_0
| ECC16B
));
253 //Turn off ECC engine.
254 MmioWrite32 (GPMC_ECC_CONFIG
, ECCDISABLE
);
263 UINTN EccResultRegister
;
266 //Capture 32-bit ECC result for each 512-bytes chunk.
267 //In our case PageSize is 2K so read ECC1-ECC4 result registers and
268 //generate total of 12-bytes of ECC code for the particular page.
270 EccResultRegister
= GPMC_ECC1_RESULT
;
272 for (Index
= 0; Index
< gNum512BytesChunks
; Index
++) {
274 EccResult
= MmioRead32 (EccResultRegister
);
276 //Calculate ECC code from 32-bit ECC result value.
277 //NOTE: Following calculation is not part of TRM. We got this information
278 //from Beagleboard mailing list.
279 gEccCode
[Index
* 3] = EccResult
& 0xFF;
280 gEccCode
[(Index
* 3) + 1] = (EccResult
>> 16) & 0xFF;
281 gEccCode
[(Index
* 3) + 2] = (((EccResult
>> 20) & 0xF0) | ((EccResult
>> 8) & 0x0F));
283 //Point to next ECC result register.
284 EccResultRegister
+= 4;
293 OUT UINT8
*SpareBuffer
298 UINTN NumMainAreaWords
= (gNandFlashInfo
->PageSize
/2);
299 UINTN NumSpareAreaWords
= (gNandFlashInfo
->SparePageSize
/2);
300 UINT16
*MainAreaWordBuffer
= Buffer
;
301 UINT16
*SpareAreaWordBuffer
= (UINT16
*)SpareBuffer
;
302 UINTN Timeout
= MAX_RETRY_COUNT
;
304 //Generate device address in bytes to access specific block and page index
305 Address
= GetActualPageAddressInBytes(BlockIndex
, PageIndex
);
308 NandSendCommand(PAGE_READ_CMD
);
310 //Send 5 Address cycles to access specific device address
311 NandSendAddressCycles(Address
);
313 //Send READ CONFIRM command
314 NandSendCommand(PAGE_READ_CONFIRM_CMD
);
316 //Poll till device is busy.
318 if ((NandReadStatus() & NAND_READY
) == NAND_READY
) {
325 DEBUG ((EFI_D_ERROR
, "Read page timed out.\n"));
329 //Reissue READ command
330 NandSendCommand(PAGE_READ_CMD
);
335 //Read data into the buffer.
336 for (Index
= 0; Index
< NumMainAreaWords
; Index
++) {
337 *MainAreaWordBuffer
++ = MmioRead16(GPMC_NAND_DATA_0
);
340 //Read spare area into the buffer.
341 for (Index
= 0; Index
< NumSpareAreaWords
; Index
++) {
342 *SpareAreaWordBuffer
++ = MmioRead16(GPMC_NAND_DATA_0
);
348 //Turn off ECC engine.
351 //Perform ECC correction.
352 //Need to implement..
362 IN UINT8
*SpareBuffer
366 UINT16
*MainAreaWordBuffer
= Buffer
;
367 UINT16
*SpareAreaWordBuffer
= (UINT16
*)SpareBuffer
;
370 UINTN Timeout
= MAX_RETRY_COUNT
;
372 //Generate device address in bytes to access specific block and page index
373 Address
= GetActualPageAddressInBytes(BlockIndex
, PageIndex
);
375 //Send SERIAL DATA INPUT command
376 NandSendCommand(PROGRAM_PAGE_CMD
);
378 //Send 5 Address cycles to access specific device address
379 NandSendAddressCycles(Address
);
384 //Data input from Buffer
385 for (Index
= 0; Index
< (gNandFlashInfo
->PageSize
/2); Index
++) {
386 MmioWrite16(GPMC_NAND_DATA_0
, *MainAreaWordBuffer
++);
388 //After each write access, device has to wait to accept data.
389 //Currently we may not be programming proper timing parameters to
390 //the GPMC_CONFIGi_0 registers and we would need to figure that out.
391 //Without following delay, page programming fails.
398 //Turn off ECC engine.
401 //Prepare Spare area buffer with ECC codes.
402 SetMem(SpareBuffer
, gNandFlashInfo
->SparePageSize
, 0xFF);
403 CopyMem(&SpareBuffer
[ECC_POSITION
], gEccCode
, gNum512BytesChunks
* 3);
405 //Program spare area with calculated ECC.
406 for (Index
= 0; Index
< (gNandFlashInfo
->SparePageSize
/2); Index
++) {
407 MmioWrite16(GPMC_NAND_DATA_0
, *SpareAreaWordBuffer
++);
410 //Send PROGRAM command
411 NandSendCommand(PROGRAM_PAGE_CONFIRM_CMD
);
413 //Poll till device is busy.
415 NandStatus
= NandReadStatus();
416 if ((NandStatus
& NAND_READY
) == NAND_READY
) {
423 DEBUG ((EFI_D_ERROR
, "Program page timed out.\n"));
427 //Bit0 indicates Pass/Fail status
428 if (NandStatus
& NAND_FAILURE
) {
429 return EFI_DEVICE_ERROR
;
442 UINTN Timeout
= MAX_RETRY_COUNT
;
444 //Generate device address in bytes to access specific block and page index
445 Address
= GetActualPageAddressInBytes(BlockIndex
, 0);
447 //Send ERASE SETUP command
448 NandSendCommand(BLOCK_ERASE_CMD
);
450 //Send 3 address cycles to device to access Page address and Block address
451 Address
>>= 11; //Ignore column addresses
453 NandSendAddress(Address
& 0xff);
456 NandSendAddress(Address
& 0xff);
459 NandSendAddress(Address
& 0xff);
461 //Send ERASE CONFIRM command
462 NandSendCommand(BLOCK_ERASE_CONFIRM_CMD
);
464 //Poll till device is busy.
466 NandStatus
= NandReadStatus();
467 if ((NandStatus
& NAND_READY
) == NAND_READY
) {
475 DEBUG ((EFI_D_ERROR
, "Erase block timed out for Block: %d.\n", BlockIndex
));
479 //Bit0 indicates Pass/Fail status
480 if (NandStatus
& NAND_FAILURE
) {
481 return EFI_DEVICE_ERROR
;
489 IN UINTN StartBlockIndex
,
490 IN UINTN EndBlockIndex
,
492 OUT VOID
*SpareBuffer
497 EFI_STATUS Status
= EFI_SUCCESS
;
499 for (BlockIndex
= StartBlockIndex
; BlockIndex
<= EndBlockIndex
; BlockIndex
++) {
500 //For each block read number of pages
501 for (PageIndex
= 0; PageIndex
< gNandFlashInfo
->NumPagesPerBlock
; PageIndex
++) {
502 Status
= NandReadPage(BlockIndex
, PageIndex
, Buffer
, SpareBuffer
);
503 if (EFI_ERROR(Status
)) {
506 Buffer
= ((UINT8
*)Buffer
+ gNandFlashInfo
->PageSize
);
515 IN UINTN StartBlockIndex
,
516 IN UINTN EndBlockIndex
,
518 OUT VOID
*SpareBuffer
523 EFI_STATUS Status
= EFI_SUCCESS
;
525 for (BlockIndex
= StartBlockIndex
; BlockIndex
<= EndBlockIndex
; BlockIndex
++) {
527 for (PageIndex
= 0; PageIndex
< gNandFlashInfo
->NumPagesPerBlock
; PageIndex
++) {
528 Status
= NandWritePage(BlockIndex
, PageIndex
, Buffer
, SpareBuffer
);
529 if (EFI_ERROR(Status
)) {
532 Buffer
= ((UINT8
*)Buffer
+ gNandFlashInfo
->PageSize
);
542 IN EFI_BLOCK_IO_PROTOCOL
*This
,
543 IN BOOLEAN ExtendedVerification
546 UINTN BusyStall
= 50; // microSeconds
547 UINTN ResetBusyTimeout
= (1000000 / BusyStall
); // 1 Second
549 //Send RESET command to device.
550 NandSendCommand(RESET_CMD
);
552 //Wait for 1ms before we check status register.
555 //Check BIT#5 & BIT#6 in Status register to make sure RESET is done.
556 while ((NandReadStatus() & NAND_RESET_STATUS
) != NAND_RESET_STATUS
) {
558 //In case of extended verification, wait for extended amount of time
559 //to make sure device is reset.
560 if (ExtendedVerification
) {
561 if (ResetBusyTimeout
== 0) {
562 return EFI_DEVICE_ERROR
;
565 gBS
->Stall(BusyStall
);
575 NandFlashReadBlocks (
576 IN EFI_BLOCK_IO_PROTOCOL
*This
,
586 UINT8
*SpareBuffer
= NULL
;
588 if (Buffer
== NULL
) {
589 Status
= EFI_INVALID_PARAMETER
;
593 if (Lba
> LAST_BLOCK
) {
594 Status
= EFI_INVALID_PARAMETER
;
598 if ((BufferSize
% gNandFlashInfo
->BlockSize
) != 0) {
599 Status
= EFI_BAD_BUFFER_SIZE
;
603 NumBlocks
= DivU64x32(BufferSize
, gNandFlashInfo
->BlockSize
);
604 EndBlockIndex
= ((UINTN
)Lba
+ NumBlocks
) - 1;
606 SpareBuffer
= (UINT8
*)AllocatePool(gNandFlashInfo
->SparePageSize
);
607 if (SpareBuffer
== NULL
) {
608 Status
= EFI_OUT_OF_RESOURCES
;
613 Status
= NandReadBlock((UINTN
)Lba
, EndBlockIndex
, Buffer
, SpareBuffer
);
614 if (EFI_ERROR(Status
)) {
615 DEBUG((EFI_D_ERROR
, "Read block fails: %x\n", Status
));
620 if (SpareBuffer
!= NULL
) {
621 FreePool (SpareBuffer
);
629 NandFlashWriteBlocks (
630 IN EFI_BLOCK_IO_PROTOCOL
*This
,
641 UINT8
*SpareBuffer
= NULL
;
643 if (Buffer
== NULL
) {
644 Status
= EFI_INVALID_PARAMETER
;
648 if (Lba
> LAST_BLOCK
) {
649 Status
= EFI_INVALID_PARAMETER
;
653 if ((BufferSize
% gNandFlashInfo
->BlockSize
) != 0) {
654 Status
= EFI_BAD_BUFFER_SIZE
;
658 NumBlocks
= DivU64x32(BufferSize
, gNandFlashInfo
->BlockSize
);
659 EndBlockIndex
= ((UINTN
)Lba
+ NumBlocks
) - 1;
661 SpareBuffer
= (UINT8
*)AllocatePool(gNandFlashInfo
->SparePageSize
);
662 if (SpareBuffer
== NULL
) {
663 Status
= EFI_OUT_OF_RESOURCES
;
668 for (BlockIndex
= (UINTN
)Lba
; BlockIndex
<= EndBlockIndex
; BlockIndex
++) {
669 Status
= NandEraseBlock(BlockIndex
);
670 if (EFI_ERROR(Status
)) {
671 DEBUG((EFI_D_ERROR
, "Erase block failed. Status: %x\n", Status
));
677 Status
= NandWriteBlock((UINTN
)Lba
, EndBlockIndex
, Buffer
, SpareBuffer
);
678 if (EFI_ERROR(Status
)) {
679 DEBUG((EFI_D_ERROR
, "Block write fails: %x\n", Status
));
684 if (SpareBuffer
!= NULL
) {
685 FreePool (SpareBuffer
);
693 NandFlashFlushBlocks (
694 IN EFI_BLOCK_IO_PROTOCOL
*This
702 EFI_BLOCK_IO_MEDIA gNandFlashMedia
= {
703 SIGNATURE_32('n','a','n','d'), // MediaId
704 FALSE
, // RemovableMedia
705 TRUE
, // MediaPresent
706 FALSE
, // LogicalPartition
708 FALSE
, // WriteCaching
715 EFI_BLOCK_IO_PROTOCOL BlockIo
=
717 EFI_BLOCK_IO_INTERFACE_REVISION
, // Revision
718 &gNandFlashMedia
, // *Media
719 NandFlashReset
, // Reset
720 NandFlashReadBlocks
, // ReadBlocks
721 NandFlashWriteBlocks
, // WriteBlocks
722 NandFlashFlushBlocks
// FlushBlocks
726 NandFlashInitialize (
727 IN EFI_HANDLE ImageHandle
,
728 IN EFI_SYSTEM_TABLE
*SystemTable
733 gNandFlashInfo
= (NAND_FLASH_INFO
*)AllocateZeroPool (sizeof(NAND_FLASH_INFO
));
735 //Initialize GPMC module.
739 NandFlashReset(&BlockIo
, FALSE
);
741 //Detect NAND part and populate gNandFlashInfo structure
742 Status
= NandDetectPart ();
743 if (EFI_ERROR(Status
)) {
744 DEBUG((EFI_D_ERROR
, "Nand part id detection failure: Status: %x\n", Status
));
748 //Count total number of 512Bytes chunk based on the page size.
749 if (gNandFlashInfo
->PageSize
== PAGE_SIZE_512B
) {
750 gNum512BytesChunks
= 1;
751 } else if (gNandFlashInfo
->PageSize
== PAGE_SIZE_2K
) {
752 gNum512BytesChunks
= 4;
753 } else if (gNandFlashInfo
->PageSize
== PAGE_SIZE_4K
) {
754 gNum512BytesChunks
= 8;
757 gEccCode
= (UINT8
*)AllocatePool(gNum512BytesChunks
* 3);
758 if (gEccCode
== NULL
) {
759 return EFI_OUT_OF_RESOURCES
;
765 //Patch EFI_BLOCK_IO_MEDIA structure.
766 gNandFlashMedia
.BlockSize
= gNandFlashInfo
->BlockSize
;
767 gNandFlashMedia
.LastBlock
= LAST_BLOCK
;
770 Status
= gBS
->InstallMultipleProtocolInterfaces (
772 &gEfiBlockIoProtocolGuid
, &BlockIo
,
773 &gEfiDevicePathProtocolGuid
, &gDevicePath
,