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
= {
43 { HARDWARE_DEVICE_PATH
, HW_VENDOR_DP
, { sizeof (VENDOR_DEVICE_PATH
), 0 } },
46 { END_DEVICE_PATH_TYPE
, END_ENTIRE_DEVICE_PATH_SUBTYPE
, { sizeof (EFI_DEVICE_PATH_PROTOCOL
), 0} }
51 //Actual page address = Column address + Page address + Block address.
53 GetActualPageAddressInBytes (
58 //BlockAddressStart = Start of the Block address in actual NAND
59 //PageAddressStart = Start of the Page address in actual NAND
60 return ((BlockIndex
<< gNandFlashInfo
->BlockAddressStart
) + (PageIndex
<< gNandFlashInfo
->PageAddressStart
));
68 MmioWrite16(GPMC_NAND_COMMAND_0
, Command
);
76 MmioWrite16(GPMC_NAND_ADDRESS_0
, Address
);
84 //Send READ STATUS command
85 NandSendCommand(READ_STATUS_CMD
);
88 return MmioRead16(GPMC_NAND_DATA_0
);
92 NandSendAddressCycles (
97 NandSendAddress(Address
& 0xff);
101 NandSendAddress(Address
& 0x07);
104 //Page and Block address
105 NandSendAddress(Address
& 0xff);
109 NandSendAddress(Address
& 0xff);
113 NandSendAddress(Address
& 0x01);
121 //Enable Smart-idle mode.
122 MmioWrite32 (GPMC_SYSCONFIG
, SMARTIDLEMODE
);
124 //Set IRQSTATUS and IRQENABLE to the reset value
125 MmioWrite32 (GPMC_IRQSTATUS
, 0x0);
126 MmioWrite32 (GPMC_IRQENABLE
, 0x0);
128 //Disable GPMC timeout control.
129 MmioWrite32 (GPMC_TIMEOUT_CONTROL
, TIMEOUTDISABLE
);
131 //Set WRITEPROTECT bit to enable write access.
132 MmioWrite32 (GPMC_CONFIG
, WRITEPROTECT_HIGH
);
134 //NOTE: Following GPMC_CONFIGi_0 register settings are taken from u-boot memory dump.
135 MmioWrite32 (GPMC_CONFIG1_0
, DEVICETYPE_NAND
| DEVICESIZE_X16
);
136 MmioWrite32 (GPMC_CONFIG2_0
, CSRDOFFTIME
| CSWROFFTIME
);
137 MmioWrite32 (GPMC_CONFIG3_0
, ADVRDOFFTIME
| ADVWROFFTIME
);
138 MmioWrite32 (GPMC_CONFIG4_0
, OEONTIME
| OEOFFTIME
| WEONTIME
| WEOFFTIME
);
139 MmioWrite32 (GPMC_CONFIG5_0
, RDCYCLETIME
| WRCYCLETIME
| RDACCESSTIME
| PAGEBURSTACCESSTIME
);
140 MmioWrite32 (GPMC_CONFIG6_0
, WRACCESSTIME
| WRDATAONADMUXBUS
| CYCLE2CYCLEDELAY
| CYCLE2CYCLESAMECSEN
);
141 MmioWrite32 (GPMC_CONFIG7_0
, MASKADDRESS_128MB
| CSVALID
| BASEADDRESS
);
152 BOOLEAN Found
= FALSE
;
154 //Send READ ID command
155 NandSendCommand(READ_ID_CMD
);
157 //Send one address cycle.
160 //Read 5-bytes to idenfity code programmed into the NAND flash devices.
161 //BYTE 0 = Manufacture ID
163 //Byte 2, 3, 4 = Nand part specific information (Page size, Block size etc)
164 for (Index
= 0; Index
< sizeof(PartInfo
); Index
++) {
165 PartInfo
[Index
] = MmioRead16(GPMC_NAND_DATA_0
);
168 //Check if the ManufactureId and DeviceId are part of the currently supported nand parts.
169 for (Index
= 0; Index
< sizeof(gNandPartInfoTable
)/sizeof(NAND_PART_INFO_TABLE
); Index
++) {
170 if (gNandPartInfoTable
[Index
].ManufactureId
== PartInfo
[0] && gNandPartInfoTable
[Index
].DeviceId
== PartInfo
[1]) {
171 gNandFlashInfo
->BlockAddressStart
= gNandPartInfoTable
[Index
].BlockAddressStart
;
172 gNandFlashInfo
->PageAddressStart
= gNandPartInfoTable
[Index
].PageAddressStart
;
178 if (Found
== FALSE
) {
179 DEBUG ((EFI_D_ERROR
, "Nand part is not currently supported. Manufacture id: %x, Device id: %x\n", PartInfo
[0], PartInfo
[1]));
180 return EFI_NOT_FOUND
;
183 //Populate NAND_FLASH_INFO based on the result of READ ID command.
184 gNandFlashInfo
->ManufactureId
= PartInfo
[0];
185 gNandFlashInfo
->DeviceId
= PartInfo
[1];
186 NandInfo
= PartInfo
[3];
188 if (PAGE_SIZE(NandInfo
) == PAGE_SIZE_2K_VAL
) {
189 gNandFlashInfo
->PageSize
= PAGE_SIZE_2K
;
191 DEBUG ((EFI_D_ERROR
, "Unknown Page size.\n"));
192 return EFI_DEVICE_ERROR
;
195 if (SPARE_AREA_SIZE(NandInfo
) == SPARE_AREA_SIZE_64B_VAL
) {
196 gNandFlashInfo
->SparePageSize
= SPARE_AREA_SIZE_64B
;
198 DEBUG ((EFI_D_ERROR
, "Unknown Spare area size.\n"));
199 return EFI_DEVICE_ERROR
;
202 if (BLOCK_SIZE(NandInfo
) == BLOCK_SIZE_128K_VAL
) {
203 gNandFlashInfo
->BlockSize
= BLOCK_SIZE_128K
;
205 DEBUG ((EFI_D_ERROR
, "Unknown Block size.\n"));
206 return EFI_DEVICE_ERROR
;
209 if (ORGANIZATION(NandInfo
) == ORGANIZATION_X8
) {
210 gNandFlashInfo
->Organization
= 0;
211 } else if (ORGANIZATION(NandInfo
) == ORGANIZATION_X16
) {
212 gNandFlashInfo
->Organization
= 1;
215 //Calculate total number of blocks.
216 gNandFlashInfo
->NumPagesPerBlock
= DivU64x32(gNandFlashInfo
->BlockSize
, gNandFlashInfo
->PageSize
);
226 //Define ECC size 0 and size 1 to 512 bytes
227 MmioWrite32 (GPMC_ECC_SIZE_CONFIG
, (ECCSIZE0_512BYTES
| ECCSIZE1_512BYTES
));
235 //Clear all the ECC result registers and select ECC result register 1
236 MmioWrite32 (GPMC_ECC_CONTROL
, (ECCCLEAR
| ECCPOINTER_REG1
));
238 //Enable ECC engine on CS0
239 MmioWrite32 (GPMC_ECC_CONFIG
, (ECCENABLE
| ECCCS_0
| ECC16B
));
247 //Turn off ECC engine.
248 MmioWrite32 (GPMC_ECC_CONFIG
, ECCDISABLE
);
257 UINTN EccResultRegister
;
260 //Capture 32-bit ECC result for each 512-bytes chunk.
261 //In our case PageSize is 2K so read ECC1-ECC4 result registers and
262 //generate total of 12-bytes of ECC code for the particular page.
264 EccResultRegister
= GPMC_ECC1_RESULT
;
266 for (Index
= 0; Index
< gNum512BytesChunks
; Index
++) {
268 EccResult
= MmioRead32 (EccResultRegister
);
270 //Calculate ECC code from 32-bit ECC result value.
271 //NOTE: Following calculation is not part of TRM. We got this information
272 //from Beagleboard mailing list.
273 gEccCode
[Index
* 3] = EccResult
& 0xFF;
274 gEccCode
[(Index
* 3) + 1] = (EccResult
>> 16) & 0xFF;
275 gEccCode
[(Index
* 3) + 2] = (((EccResult
>> 20) & 0xF0) | ((EccResult
>> 8) & 0x0F));
277 //Point to next ECC result register.
278 EccResultRegister
+= 4;
287 OUT UINT8
*SpareBuffer
292 UINTN NumMainAreaWords
= (gNandFlashInfo
->PageSize
/2);
293 UINTN NumSpareAreaWords
= (gNandFlashInfo
->SparePageSize
/2);
294 UINT16
*MainAreaWordBuffer
= Buffer
;
295 UINT16
*SpareAreaWordBuffer
= (UINT16
*)SpareBuffer
;
296 UINTN Timeout
= MAX_RETRY_COUNT
;
298 //Generate device address in bytes to access specific block and page index
299 Address
= GetActualPageAddressInBytes(BlockIndex
, PageIndex
);
302 NandSendCommand(PAGE_READ_CMD
);
304 //Send 5 Address cycles to access specific device address
305 NandSendAddressCycles(Address
);
307 //Send READ CONFIRM command
308 NandSendCommand(PAGE_READ_CONFIRM_CMD
);
310 //Poll till device is busy.
312 if ((NandReadStatus() & NAND_READY
) == NAND_READY
) {
319 DEBUG ((EFI_D_ERROR
, "Read page timed out.\n"));
323 //Reissue READ command
324 NandSendCommand(PAGE_READ_CMD
);
329 //Read data into the buffer.
330 for (Index
= 0; Index
< NumMainAreaWords
; Index
++) {
331 *MainAreaWordBuffer
++ = MmioRead16(GPMC_NAND_DATA_0
);
334 //Read spare area into the buffer.
335 for (Index
= 0; Index
< NumSpareAreaWords
; Index
++) {
336 *SpareAreaWordBuffer
++ = MmioRead16(GPMC_NAND_DATA_0
);
342 //Turn off ECC engine.
345 //Perform ECC correction.
346 //Need to implement..
356 IN UINT8
*SpareBuffer
360 UINT16
*MainAreaWordBuffer
= Buffer
;
361 UINT16
*SpareAreaWordBuffer
= (UINT16
*)SpareBuffer
;
364 UINTN Timeout
= MAX_RETRY_COUNT
;
366 //Generate device address in bytes to access specific block and page index
367 Address
= GetActualPageAddressInBytes(BlockIndex
, PageIndex
);
369 //Send SERIAL DATA INPUT command
370 NandSendCommand(PROGRAM_PAGE_CMD
);
372 //Send 5 Address cycles to access specific device address
373 NandSendAddressCycles(Address
);
378 //Data input from Buffer
379 for (Index
= 0; Index
< (gNandFlashInfo
->PageSize
/2); Index
++) {
380 MmioWrite16(GPMC_NAND_DATA_0
, *MainAreaWordBuffer
++);
382 //After each write access, device has to wait to accept data.
383 //Currently we may not be programming proper timing parameters to
384 //the GPMC_CONFIGi_0 registers and we would need to figure that out.
385 //Without following delay, page programming fails.
392 //Turn off ECC engine.
395 //Prepare Spare area buffer with ECC codes.
396 SetMem(SpareBuffer
, gNandFlashInfo
->SparePageSize
, 0xFF);
397 CopyMem(&SpareBuffer
[ECC_POSITION
], gEccCode
, gNum512BytesChunks
* 3);
399 //Program spare area with calculated ECC.
400 for (Index
= 0; Index
< (gNandFlashInfo
->SparePageSize
/2); Index
++) {
401 MmioWrite16(GPMC_NAND_DATA_0
, *SpareAreaWordBuffer
++);
404 //Send PROGRAM command
405 NandSendCommand(PROGRAM_PAGE_CONFIRM_CMD
);
407 //Poll till device is busy.
410 NandStatus
= NandReadStatus();
411 if ((NandStatus
& NAND_READY
) == NAND_READY
) {
418 DEBUG ((EFI_D_ERROR
, "Program page timed out.\n"));
422 //Bit0 indicates Pass/Fail status
423 if (NandStatus
& NAND_FAILURE
) {
424 return EFI_DEVICE_ERROR
;
437 UINTN Timeout
= MAX_RETRY_COUNT
;
439 //Generate device address in bytes to access specific block and page index
440 Address
= GetActualPageAddressInBytes(BlockIndex
, 0);
442 //Send ERASE SETUP command
443 NandSendCommand(BLOCK_ERASE_CMD
);
445 //Send 3 address cycles to device to access Page address and Block address
446 Address
>>= 11; //Ignore column addresses
448 NandSendAddress(Address
& 0xff);
451 NandSendAddress(Address
& 0xff);
454 NandSendAddress(Address
& 0xff);
456 //Send ERASE CONFIRM command
457 NandSendCommand(BLOCK_ERASE_CONFIRM_CMD
);
459 //Poll till device is busy.
462 NandStatus
= NandReadStatus();
463 if ((NandStatus
& NAND_READY
) == NAND_READY
) {
471 DEBUG ((EFI_D_ERROR
, "Erase block timed out for Block: %d.\n", BlockIndex
));
475 //Bit0 indicates Pass/Fail status
476 if (NandStatus
& NAND_FAILURE
) {
477 return EFI_DEVICE_ERROR
;
485 IN UINTN StartBlockIndex
,
486 IN UINTN EndBlockIndex
,
488 OUT VOID
*SpareBuffer
493 EFI_STATUS Status
= EFI_SUCCESS
;
495 for (BlockIndex
= StartBlockIndex
; BlockIndex
<= EndBlockIndex
; BlockIndex
++) {
496 //For each block read number of pages
497 for (PageIndex
= 0; PageIndex
< gNandFlashInfo
->NumPagesPerBlock
; PageIndex
++) {
498 Status
= NandReadPage(BlockIndex
, PageIndex
, Buffer
, SpareBuffer
);
499 if (EFI_ERROR(Status
)) {
502 Buffer
= ((UINT8
*)Buffer
+ gNandFlashInfo
->PageSize
);
511 IN UINTN StartBlockIndex
,
512 IN UINTN EndBlockIndex
,
514 OUT VOID
*SpareBuffer
519 EFI_STATUS Status
= EFI_SUCCESS
;
521 for (BlockIndex
= StartBlockIndex
; BlockIndex
<= EndBlockIndex
; BlockIndex
++) {
523 for (PageIndex
= 0; PageIndex
< gNandFlashInfo
->NumPagesPerBlock
; PageIndex
++) {
524 Status
= NandWritePage(BlockIndex
, PageIndex
, Buffer
, SpareBuffer
);
525 if (EFI_ERROR(Status
)) {
528 Buffer
= ((UINT8
*)Buffer
+ gNandFlashInfo
->PageSize
);
538 IN EFI_BLOCK_IO_PROTOCOL
*This
,
539 IN BOOLEAN ExtendedVerification
542 UINTN BusyStall
= 50; // microSeconds
543 UINTN ResetBusyTimeout
= (1000000 / BusyStall
); // 1 Second
545 //Send RESET command to device.
546 NandSendCommand(RESET_CMD
);
548 //Wait for 1ms before we check status register.
551 //Check BIT#5 & BIT#6 in Status register to make sure RESET is done.
552 while ((NandReadStatus() & NAND_RESET_STATUS
) != NAND_RESET_STATUS
) {
554 //In case of extended verification, wait for extended amount of time
555 //to make sure device is reset.
556 if (ExtendedVerification
) {
557 if (ResetBusyTimeout
== 0) {
558 return EFI_DEVICE_ERROR
;
561 gBS
->Stall(BusyStall
);
571 NandFlashReadBlocks (
572 IN EFI_BLOCK_IO_PROTOCOL
*This
,
582 UINT8
*SpareBuffer
= NULL
;
584 if (Buffer
== NULL
) {
585 Status
= EFI_INVALID_PARAMETER
;
589 if (Lba
> LAST_BLOCK
) {
590 Status
= EFI_INVALID_PARAMETER
;
594 if ((BufferSize
% gNandFlashInfo
->BlockSize
) != 0) {
595 Status
= EFI_BAD_BUFFER_SIZE
;
599 NumBlocks
= DivU64x32(BufferSize
, gNandFlashInfo
->BlockSize
);
600 EndBlockIndex
= ((UINTN
)Lba
+ NumBlocks
) - 1;
602 SpareBuffer
= (UINT8
*)AllocatePool(gNandFlashInfo
->SparePageSize
);
603 if (SpareBuffer
== NULL
) {
604 Status
= EFI_OUT_OF_RESOURCES
;
609 Status
= NandReadBlock((UINTN
)Lba
, EndBlockIndex
, Buffer
, SpareBuffer
);
610 if (EFI_ERROR(Status
)) {
611 DEBUG((EFI_D_ERROR
, "Read block fails: %x\n", Status
));
616 if (SpareBuffer
!= NULL
) {
617 FreePool (SpareBuffer
);
625 NandFlashWriteBlocks (
626 IN EFI_BLOCK_IO_PROTOCOL
*This
,
637 UINT8
*SpareBuffer
= NULL
;
639 if (Buffer
== NULL
) {
640 Status
= EFI_INVALID_PARAMETER
;
644 if (Lba
> LAST_BLOCK
) {
645 Status
= EFI_INVALID_PARAMETER
;
649 if ((BufferSize
% gNandFlashInfo
->BlockSize
) != 0) {
650 Status
= EFI_BAD_BUFFER_SIZE
;
654 NumBlocks
= DivU64x32(BufferSize
, gNandFlashInfo
->BlockSize
);
655 EndBlockIndex
= ((UINTN
)Lba
+ NumBlocks
) - 1;
657 SpareBuffer
= (UINT8
*)AllocatePool(gNandFlashInfo
->SparePageSize
);
658 if (SpareBuffer
== NULL
) {
659 Status
= EFI_OUT_OF_RESOURCES
;
664 for (BlockIndex
= (UINTN
)Lba
; BlockIndex
<= EndBlockIndex
; BlockIndex
++) {
665 Status
= NandEraseBlock(BlockIndex
);
666 if (EFI_ERROR(Status
)) {
667 DEBUG((EFI_D_ERROR
, "Erase block failed. Status: %x\n", Status
));
673 Status
= NandWriteBlock((UINTN
)Lba
, EndBlockIndex
, Buffer
, SpareBuffer
);
674 if (EFI_ERROR(Status
)) {
675 DEBUG((EFI_D_ERROR
, "Block write fails: %x\n", Status
));
680 if (SpareBuffer
!= NULL
) {
681 FreePool (SpareBuffer
);
689 NandFlashFlushBlocks (
690 IN EFI_BLOCK_IO_PROTOCOL
*This
698 EFI_BLOCK_IO_MEDIA gNandFlashMedia
= {
699 SIGNATURE_32('n','a','n','d'), // MediaId
700 FALSE
, // RemovableMedia
701 TRUE
, // MediaPresent
702 FALSE
, // LogicalPartition
704 FALSE
, // WriteCaching
711 EFI_BLOCK_IO_PROTOCOL BlockIo
=
713 EFI_BLOCK_IO_INTERFACE_REVISION
, // Revision
714 &gNandFlashMedia
, // *Media
715 NandFlashReset
, // Reset
716 NandFlashReadBlocks
, // ReadBlocks
717 NandFlashWriteBlocks
, // WriteBlocks
718 NandFlashFlushBlocks
// FlushBlocks
722 NandFlashInitialize (
723 IN EFI_HANDLE ImageHandle
,
724 IN EFI_SYSTEM_TABLE
*SystemTable
729 gNandFlashInfo
= (NAND_FLASH_INFO
*)AllocateZeroPool (sizeof(NAND_FLASH_INFO
));
731 //Initialize GPMC module.
735 NandFlashReset(&BlockIo
, FALSE
);
737 //Detect NAND part and populate gNandFlashInfo structure
738 Status
= NandDetectPart ();
739 if (EFI_ERROR(Status
)) {
740 DEBUG((EFI_D_ERROR
, "Nand part id detection failure: Status: %x\n", Status
));
744 //Count total number of 512Bytes chunk based on the page size.
745 if (gNandFlashInfo
->PageSize
== PAGE_SIZE_512B
) {
746 gNum512BytesChunks
= 1;
747 } else if (gNandFlashInfo
->PageSize
== PAGE_SIZE_2K
) {
748 gNum512BytesChunks
= 4;
749 } else if (gNandFlashInfo
->PageSize
== PAGE_SIZE_4K
) {
750 gNum512BytesChunks
= 8;
753 gEccCode
= (UINT8
*)AllocatePool(gNum512BytesChunks
* 3);
754 if (gEccCode
== NULL
) {
755 return EFI_OUT_OF_RESOURCES
;
761 //Patch EFI_BLOCK_IO_MEDIA structure.
762 gNandFlashMedia
.BlockSize
= gNandFlashInfo
->BlockSize
;
763 gNandFlashMedia
.LastBlock
= LAST_BLOCK
;
766 Status
= gBS
->InstallMultipleProtocolInterfaces (
768 &gEfiBlockIoProtocolGuid
, &BlockIo
,
769 &gEfiDevicePathProtocolGuid
, &gDevicePath
,