3 Copyright (c) 2008-2009, Apple Inc. All rights reserved.
5 All rights reserved. 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;
26 // Device path for SemiHosting. It contains our autogened Caller ID GUID.
29 VENDOR_DEVICE_PATH Guid
;
30 EFI_DEVICE_PATH_PROTOCOL End
;
33 FLASH_DEVICE_PATH gDevicePath
= {
35 { HARDWARE_DEVICE_PATH
, HW_VENDOR_DP
, sizeof (VENDOR_DEVICE_PATH
), 0 },
38 { END_DEVICE_PATH_TYPE
, END_ENTIRE_DEVICE_PATH_SUBTYPE
, sizeof (EFI_DEVICE_PATH_PROTOCOL
), 0}
42 //Actual page address = Column address + Page address + Block address.
44 GetActualPageAddressInBytes (
49 //BlockAddressStart = Start of the Block address in actual NAND
50 //PageAddressStart = Start of the Page address in actual NAND
51 return ((BlockIndex
<< gNandFlashInfo
->BlockAddressStart
) + (PageIndex
<< gNandFlashInfo
->PageAddressStart
));
59 MmioWrite16(GPMC_NAND_COMMAND_0
, Command
);
67 MmioWrite16(GPMC_NAND_ADDRESS_0
, Address
);
75 //Send READ STATUS command
76 NandSendCommand(READ_STATUS_CMD
);
79 return MmioRead16(GPMC_NAND_DATA_0
);
83 NandSendAddressCycles (
88 NandSendAddress(Address
& 0xff);
92 NandSendAddress(Address
& 0x07);
95 //Page and Block address
96 NandSendAddress(Address
& 0xff);
100 NandSendAddress(Address
& 0xff);
104 NandSendAddress(Address
& 0x01);
112 //Enable Smart-idle mode.
113 MmioWrite32(GPMC_SYSCONFIG
, SMARTIDLEMODE
);
115 //Set IRQSTATUS and IRQENABLE to the reset value
116 MmioWrite32(GPMC_IRQSTATUS
, 0x0);
117 MmioWrite32(GPMC_IRQENABLE
, 0x0);
119 //Disable GPMC timeout control.
120 MmioWrite32(GPMC_TIMEOUT_CONTROL
, TIMEOUTDISABLE
);
122 //Set WRITEPROTECT bit to enable write access.
123 MmioWrite32(GPMC_CONFIG
, WRITEPROTECT_HIGH
);
125 //NOTE: Following GPMC_CONFIGi_0 register settings are taken from u-boot memory dump.
126 MmioWrite32(GPMC_CONFIG1_0
, DEVICETYPE_NAND
| DEVICESIZE_X16
);
127 MmioWrite32(GPMC_CONFIG2_0
, CSRDOFFTIME
| CSWROFFTIME
);
128 MmioWrite32(GPMC_CONFIG3_0
, ADVRDOFFTIME
| ADVWROFFTIME
);
129 MmioWrite32(GPMC_CONFIG4_0
, OEONTIME
| OEOFFTIME
| WEONTIME
| WEOFFTIME
);
130 MmioWrite32(GPMC_CONFIG5_0
, RDCYCLETIME
| WRCYCLETIME
| RDACCESSTIME
| PAGEBURSTACCESSTIME
);
131 MmioWrite32(GPMC_CONFIG6_0
, WRACCESSTIME
| WRDATAONADMUXBUS
| CYCLE2CYCLEDELAY
| CYCLE2CYCLESAMECSEN
);
132 MmioWrite32(GPMC_CONFIG7_0
, MASKADDRESS_128MB
| CSVALID
| BASEADDRESS
);
143 BOOLEAN Found
= FALSE
;
145 //Send READ ID command
146 NandSendCommand(READ_ID_CMD
);
148 //Send one address cycle.
151 //Read 5-bytes to idenfity code programmed into the NAND flash devices.
152 //BYTE 0 = Manufacture ID
154 //Byte 2, 3, 4 = Nand part specific information (Page size, Block size etc)
155 for (Index
= 0; Index
< sizeof(PartInfo
); Index
++) {
156 PartInfo
[Index
] = MmioRead16(GPMC_NAND_DATA_0
);
159 //Check if the ManufactureId and DeviceId are part of the currently supported nand parts.
160 for (Index
= 0; Index
< sizeof(gNandPartInfoTable
)/sizeof(NAND_PART_INFO_TABLE
); Index
++) {
161 if (gNandPartInfoTable
[Index
].ManufactureId
== PartInfo
[0] && gNandPartInfoTable
[Index
].DeviceId
== PartInfo
[1]) {
162 gNandFlashInfo
->BlockAddressStart
= gNandPartInfoTable
[Index
].BlockAddressStart
;
163 gNandFlashInfo
->PageAddressStart
= gNandPartInfoTable
[Index
].PageAddressStart
;
169 if (Found
== FALSE
) {
170 DEBUG ((EFI_D_ERROR
, "Nand part is not currently supported. Manufacture id: %x, Device id: %x\n", PartInfo
[0], PartInfo
[1]));
171 return EFI_NOT_FOUND
;
174 //Populate NAND_FLASH_INFO based on the result of READ ID command.
175 gNandFlashInfo
->ManufactureId
= PartInfo
[0];
176 gNandFlashInfo
->DeviceId
= PartInfo
[1];
177 NandInfo
= PartInfo
[3];
179 if (PAGE_SIZE(NandInfo
) == PAGE_SIZE_2K_VAL
) {
180 gNandFlashInfo
->PageSize
= PAGE_SIZE_2K
;
182 DEBUG ((EFI_D_ERROR
, "Unknown Page size.\n"));
183 return EFI_DEVICE_ERROR
;
186 if (SPARE_AREA_SIZE(NandInfo
) == SPARE_AREA_SIZE_64B_VAL
) {
187 gNandFlashInfo
->SparePageSize
= SPARE_AREA_SIZE_64B
;
189 DEBUG ((EFI_D_ERROR
, "Unknown Spare area size.\n"));
190 return EFI_DEVICE_ERROR
;
193 if (BLOCK_SIZE(NandInfo
) == BLOCK_SIZE_128K_VAL
) {
194 gNandFlashInfo
->BlockSize
= BLOCK_SIZE_128K
;
196 DEBUG ((EFI_D_ERROR
, "Unknown Block size.\n"));
197 return EFI_DEVICE_ERROR
;
200 if (ORGANIZATION(NandInfo
) == ORGANIZATION_X8
) {
201 gNandFlashInfo
->Organization
= 0;
202 } else if (ORGANIZATION(NandInfo
) == ORGANIZATION_X16
) {
203 gNandFlashInfo
->Organization
= 1;
206 //Calculate total number of blocks.
207 gNandFlashInfo
->NumPagesPerBlock
= DivU64x32(gNandFlashInfo
->BlockSize
, gNandFlashInfo
->PageSize
);
217 //Define ECC size 0 and size 1 to 512 bytes
218 MmioWrite32(GPMC_ECC_SIZE_CONFIG
, (ECCSIZE0_512BYTES
| ECCSIZE1_512BYTES
));
226 //Clear all the ECC result registers and select ECC result register 1
227 MmioWrite32(GPMC_ECC_CONTROL
, (ECCCLEAR
| ECCPOINTER_REG1
));
229 //Enable ECC engine on CS0
230 MmioWrite32(GPMC_ECC_CONFIG
, (ECCENABLE
| ECCCS_0
| ECC16B
));
238 //Turn off ECC engine.
239 MmioWrite32(GPMC_ECC_CONFIG
, ECCDISABLE
);
248 UINTN EccResultRegister
;
251 //Capture 32-bit ECC result for each 512-bytes chunk.
252 //In our case PageSize is 2K so read ECC1-ECC4 result registers and
253 //generate total of 12-bytes of ECC code for the particular page.
255 EccResultRegister
= GPMC_ECC1_RESULT
;
257 for (Index
= 0; Index
< gNum512BytesChunks
; Index
++) {
259 EccResult
= MmioRead32(EccResultRegister
);
261 //Calculate ECC code from 32-bit ECC result value.
262 //NOTE: Following calculation is not part of TRM. We got this information
263 //from Beagleboard mailing list.
264 gEccCode
[Index
* 3] = EccResult
& 0xFF;
265 gEccCode
[(Index
* 3) + 1] = (EccResult
>> 16) & 0xFF;
266 gEccCode
[(Index
* 3) + 2] = (((EccResult
>> 20) & 0xF0) | ((EccResult
>> 8) & 0x0F));
268 //Point to next ECC result register.
269 EccResultRegister
+= 4;
278 OUT UINT8
*SpareBuffer
283 UINTN NumMainAreaWords
= (gNandFlashInfo
->PageSize
/2);
284 UINTN NumSpareAreaWords
= (gNandFlashInfo
->SparePageSize
/2);
285 UINT16
*MainAreaWordBuffer
= Buffer
;
286 UINT16
*SpareAreaWordBuffer
= (UINT16
*)SpareBuffer
;
287 UINTN Timeout
= MAX_RETRY_COUNT
;
289 //Generate device address in bytes to access specific block and page index
290 Address
= GetActualPageAddressInBytes(BlockIndex
, PageIndex
);
293 NandSendCommand(PAGE_READ_CMD
);
295 //Send 5 Address cycles to access specific device address
296 NandSendAddressCycles(Address
);
298 //Send READ CONFIRM command
299 NandSendCommand(PAGE_READ_CONFIRM_CMD
);
301 //Poll till device is busy.
303 if ((NandReadStatus() & NAND_READY
) == NAND_READY
) {
310 DEBUG ((EFI_D_ERROR
, "Read page timed out.\n"));
314 //Reissue READ command
315 NandSendCommand(PAGE_READ_CMD
);
320 //Read data into the buffer.
321 for (Index
= 0; Index
< NumMainAreaWords
; Index
++) {
322 *MainAreaWordBuffer
++ = MmioRead16(GPMC_NAND_DATA_0
);
325 //Read spare area into the buffer.
326 for (Index
= 0; Index
< NumSpareAreaWords
; Index
++) {
327 *SpareAreaWordBuffer
++ = MmioRead16(GPMC_NAND_DATA_0
);
333 //Turn off ECC engine.
336 //Perform ECC correction.
337 //Need to implement..
347 IN UINT8
*SpareBuffer
351 UINT16
*MainAreaWordBuffer
= Buffer
;
352 UINT16
*SpareAreaWordBuffer
= (UINT16
*)SpareBuffer
;
355 UINTN Timeout
= MAX_RETRY_COUNT
;
357 //Generate device address in bytes to access specific block and page index
358 Address
= GetActualPageAddressInBytes(BlockIndex
, PageIndex
);
360 //Send SERIAL DATA INPUT command
361 NandSendCommand(PROGRAM_PAGE_CMD
);
363 //Send 5 Address cycles to access specific device address
364 NandSendAddressCycles(Address
);
369 //Data input from Buffer
370 for (Index
= 0; Index
< (gNandFlashInfo
->PageSize
/2); Index
++) {
371 MmioWrite16(GPMC_NAND_DATA_0
, *MainAreaWordBuffer
++);
373 //After each write access, device has to wait to accept data.
374 //Currently we may not be programming proper timing parameters to
375 //the GPMC_CONFIGi_0 registers and we would need to figure that out.
376 //Without following delay, page programming fails.
383 //Turn off ECC engine.
386 //Prepare Spare area buffer with ECC codes.
387 SetMem(SpareBuffer
, gNandFlashInfo
->SparePageSize
, 0xFF);
388 CopyMem(&SpareBuffer
[ECC_POSITION
], gEccCode
, gNum512BytesChunks
* 3);
390 //Program spare area with calculated ECC.
391 for (Index
= 0; Index
< (gNandFlashInfo
->SparePageSize
/2); Index
++) {
392 MmioWrite16(GPMC_NAND_DATA_0
, *SpareAreaWordBuffer
++);
395 //Send PROGRAM command
396 NandSendCommand(PROGRAM_PAGE_CONFIRM_CMD
);
398 //Poll till device is busy.
400 NandStatus
= NandReadStatus();
401 if ((NandStatus
& NAND_READY
) == NAND_READY
) {
408 DEBUG ((EFI_D_ERROR
, "Program page timed out.\n"));
412 //Bit0 indicates Pass/Fail status
413 if (NandStatus
& NAND_FAILURE
) {
414 return EFI_DEVICE_ERROR
;
427 UINTN Timeout
= MAX_RETRY_COUNT
;
429 //Generate device address in bytes to access specific block and page index
430 Address
= GetActualPageAddressInBytes(BlockIndex
, 0);
432 //Send ERASE SETUP command
433 NandSendCommand(BLOCK_ERASE_CMD
);
435 //Send 3 address cycles to device to access Page address and Block address
436 Address
>>= 11; //Ignore column addresses
438 NandSendAddress(Address
& 0xff);
441 NandSendAddress(Address
& 0xff);
444 NandSendAddress(Address
& 0xff);
446 //Send ERASE CONFIRM command
447 NandSendCommand(BLOCK_ERASE_CONFIRM_CMD
);
449 //Poll till device is busy.
451 NandStatus
= NandReadStatus();
452 if ((NandStatus
& NAND_READY
) == NAND_READY
) {
460 DEBUG ((EFI_D_ERROR
, "Erase block timed out for Block: %d.\n", BlockIndex
));
464 //Bit0 indicates Pass/Fail status
465 if (NandStatus
& NAND_FAILURE
) {
466 return EFI_DEVICE_ERROR
;
474 IN UINTN StartBlockIndex
,
475 IN UINTN EndBlockIndex
,
477 OUT VOID
*SpareBuffer
482 EFI_STATUS Status
= EFI_SUCCESS
;
484 for (BlockIndex
= StartBlockIndex
; BlockIndex
<= EndBlockIndex
; BlockIndex
++) {
485 //For each block read number of pages
486 for (PageIndex
= 0; PageIndex
< gNandFlashInfo
->NumPagesPerBlock
; PageIndex
++) {
487 Status
= NandReadPage(BlockIndex
, PageIndex
, Buffer
, SpareBuffer
);
488 if (EFI_ERROR(Status
)) {
491 Buffer
= ((UINT8
*)Buffer
+ gNandFlashInfo
->PageSize
);
500 IN UINTN StartBlockIndex
,
501 IN UINTN EndBlockIndex
,
503 OUT VOID
*SpareBuffer
508 EFI_STATUS Status
= EFI_SUCCESS
;
510 for (BlockIndex
= StartBlockIndex
; BlockIndex
<= EndBlockIndex
; BlockIndex
++) {
512 for (PageIndex
= 0; PageIndex
< gNandFlashInfo
->NumPagesPerBlock
; PageIndex
++) {
513 Status
= NandWritePage(BlockIndex
, PageIndex
, Buffer
, SpareBuffer
);
514 if (EFI_ERROR(Status
)) {
517 Buffer
= ((UINT8
*)Buffer
+ gNandFlashInfo
->PageSize
);
527 IN EFI_BLOCK_IO_PROTOCOL
*This
,
528 IN BOOLEAN ExtendedVerification
531 UINTN BusyStall
= 50; // microSeconds
532 UINTN ResetBusyTimeout
= (1000000 / BusyStall
); // 1 Second
534 //Send RESET command to device.
535 NandSendCommand(RESET_CMD
);
537 //Wait for 1ms before we check status register.
540 //Check BIT#5 & BIT#6 in Status register to make sure RESET is done.
541 while ((NandReadStatus() & NAND_RESET_STATUS
) != NAND_RESET_STATUS
) {
543 //In case of extended verification, wait for extended amount of time
544 //to make sure device is reset.
545 if (ExtendedVerification
) {
546 if (ResetBusyTimeout
== 0) {
547 return EFI_DEVICE_ERROR
;
550 gBS
->Stall(BusyStall
);
560 NandFlashReadBlocks (
561 IN EFI_BLOCK_IO_PROTOCOL
*This
,
571 UINT8
*SpareBuffer
= NULL
;
573 if (Buffer
== NULL
) {
574 Status
= EFI_INVALID_PARAMETER
;
578 if (Lba
> LAST_BLOCK
) {
579 Status
= EFI_INVALID_PARAMETER
;
583 if ((BufferSize
% gNandFlashInfo
->BlockSize
) != 0) {
584 Status
= EFI_BAD_BUFFER_SIZE
;
588 NumBlocks
= DivU64x32(BufferSize
, gNandFlashInfo
->BlockSize
);
589 EndBlockIndex
= ((UINTN
)Lba
+ NumBlocks
) - 1;
591 SpareBuffer
= (UINT8
*)AllocatePool(gNandFlashInfo
->SparePageSize
);
592 if (SpareBuffer
== NULL
) {
593 Status
= EFI_OUT_OF_RESOURCES
;
598 Status
= NandReadBlock((UINTN
)Lba
, EndBlockIndex
, Buffer
, SpareBuffer
);
599 if (EFI_ERROR(Status
)) {
600 DEBUG((EFI_D_ERROR
, "Read block fails: %x\n", Status
));
605 if (SpareBuffer
!= NULL
) {
606 FreePool (SpareBuffer
);
614 NandFlashWriteBlocks (
615 IN EFI_BLOCK_IO_PROTOCOL
*This
,
626 UINT8
*SpareBuffer
= NULL
;
628 if (Buffer
== NULL
) {
629 Status
= EFI_INVALID_PARAMETER
;
633 if (Lba
> LAST_BLOCK
) {
634 Status
= EFI_INVALID_PARAMETER
;
638 if ((BufferSize
% gNandFlashInfo
->BlockSize
) != 0) {
639 Status
= EFI_BAD_BUFFER_SIZE
;
643 NumBlocks
= DivU64x32(BufferSize
, gNandFlashInfo
->BlockSize
);
644 EndBlockIndex
= ((UINTN
)Lba
+ NumBlocks
) - 1;
646 SpareBuffer
= (UINT8
*)AllocatePool(gNandFlashInfo
->SparePageSize
);
647 if (SpareBuffer
== NULL
) {
648 Status
= EFI_OUT_OF_RESOURCES
;
653 for (BlockIndex
= (UINTN
)Lba
; BlockIndex
<= EndBlockIndex
; BlockIndex
++) {
654 Status
= NandEraseBlock(BlockIndex
);
655 if (EFI_ERROR(Status
)) {
656 DEBUG((EFI_D_ERROR
, "Erase block failed. Status: %x\n", Status
));
662 Status
= NandWriteBlock((UINTN
)Lba
, EndBlockIndex
, Buffer
, SpareBuffer
);
663 if (EFI_ERROR(Status
)) {
664 DEBUG((EFI_D_ERROR
, "Block write fails: %x\n", Status
));
669 if (SpareBuffer
!= NULL
) {
670 FreePool (SpareBuffer
);
678 NandFlashFlushBlocks (
679 IN EFI_BLOCK_IO_PROTOCOL
*This
687 EFI_BLOCK_IO_MEDIA gNandFlashMedia
= {
688 SIGNATURE_32('n','a','n','d'), // MediaId
689 FALSE
, // RemovableMedia
690 TRUE
, // MediaPresent
691 FALSE
, // LogicalPartition
693 FALSE
, // WriteCaching
700 EFI_BLOCK_IO_PROTOCOL BlockIo
=
702 EFI_BLOCK_IO_INTERFACE_REVISION
, // Revision
703 &gNandFlashMedia
, // *Media
704 NandFlashReset
, // Reset
705 NandFlashReadBlocks
, // ReadBlocks
706 NandFlashWriteBlocks
, // WriteBlocks
707 NandFlashFlushBlocks
// FlushBlocks
711 NandFlashInitialize (
712 IN EFI_HANDLE ImageHandle
,
713 IN EFI_SYSTEM_TABLE
*SystemTable
718 gNandFlashInfo
= (NAND_FLASH_INFO
*)AllocateZeroPool (sizeof(NAND_FLASH_INFO
));
720 //Initialize GPMC module.
724 NandFlashReset(&BlockIo
, FALSE
);
726 //Detect NAND part and populate gNandFlashInfo structure
727 Status
= NandDetectPart ();
728 if (EFI_ERROR(Status
)) {
729 DEBUG((EFI_D_ERROR
, "Nand part id detection failure: Status: %x\n", Status
));
733 //Count total number of 512Bytes chunk based on the page size.
734 if (gNandFlashInfo
->PageSize
== PAGE_SIZE_512B
) {
735 gNum512BytesChunks
= 1;
736 } else if (gNandFlashInfo
->PageSize
== PAGE_SIZE_2K
) {
737 gNum512BytesChunks
= 4;
738 } else if (gNandFlashInfo
->PageSize
== PAGE_SIZE_4K
) {
739 gNum512BytesChunks
= 8;
742 gEccCode
= (UINT8
*)AllocatePool(gNum512BytesChunks
* 3);
743 if (gEccCode
== NULL
) {
744 return EFI_OUT_OF_RESOURCES
;
750 //Patch EFI_BLOCK_IO_MEDIA structure.
751 gNandFlashMedia
.BlockSize
= gNandFlashInfo
->BlockSize
;
752 gNandFlashMedia
.LastBlock
= LAST_BLOCK
;
755 Status
= gBS
->InstallMultipleProtocolInterfaces (
757 &gEfiBlockIoProtocolGuid
, &BlockIo
,
758 &gEfiDevicePathProtocolGuid
, &gDevicePath
,