分类: 嵌入式
2010-08-17 16:10:52
In Linux , the upper layer will not access nand driver directly . The upper File System and MTD utility will access MTD layer , and MTD layer will access your nand driver .
FS or MTD util
------------------------------------
MTD Layer
-------------------------------
Nand driver
MTD can support Nand and NOR Flash , For Nand , the source code are placed in drivers\mtd\nand , and the important files are nand_base.c,nand_ecc.c,nand_bbt.c . They are the skeleton of nand driver.
nand_bbt.c is a file for bad block management and nand_ecc.c is used to calculate the ECC and correct the data .
nand_base.c is the most important file , it implements the struct mtd_info . All the upper layers will use the mtd_info variables to do something .
Let's have an insight of the struct mtd_info which is defined in mtd.h.
oobsize: Amount of OOB data per block
writesize: chunk (page) size
erasesize: block size
size: the nand chip size , for only one chip , size is equal to nand->chipsize
The above variables are related to the nand flash type . they are attributes of nand flash. In the function nand_get_flash_type , it emits NAND_CMD_READID command and then read_byte to get the attributes .
oobavail: In the nand spare area , some bytes are used for bad block flags , some bytes are used for SW or HW ECC , and other bytes are reserved for upper layer, for example , yaffs will use these bytes to store FS information . the reserved bytes size is oobavail . It is related to the chip ecc.layout . It will calculate in the function nand_scan_tail .
erase : the nand flash erase function . it will call nand_erase_nand
read : the read function ,nand_read
write: the write function , nand_write
read_oob:nand_read_oob, it will read out-of-band data with or without data
write_oob:nand_write_oob,it will write out-of-band data with or without data
sync:nand_sync,wait for chip ready
block_isbad: check the block is bad or not , if the chip->bbt (bad block table) is not created , it will call chip->block_bad function , otherwise , it will use bbt to confirm the block is bad or not . the default function is nand_block_isbad .
block_markbad:nand_block_markbad, mark the block is bad . If the block is already marked bad , it will do nothing ,otherwise , it will call :chip->block_markbad.
Let's parse the flow :
1) first, nand_scan will be called . nand_scan will call nand_scan_ident , which will set the default chip function and get the flash type , according to the flash type , the other chip attributes can be defined such as writesize , oobsize
2) nand_scan_tail will set the default chip ecc function according to the ECC mode , calculate the oobavail , ecc.steps and then set the mtd function
3) the mtd functions are nand_erase , nand_read , nand_write , nand_read_oob , nand_write_oob , nand_sync , nand_suspend , nand_resume,
nand_block_isbad , nand_block_markbad
4) call the chip scan_bbt function to build up the bad block table
which will call the chip read_oob .
there are several functions need to describle .
nand_get_device:
nand_release_device:
Multi process will access the nand driver , but only one process can access it at the same time . nand_get_device will check the chip state , if it is not ready , the process will add itself into wait queue and shedule . if the chip state is ready now , the process will change the chip state to new_state (so other process can't access the nand ) and return .
nand_release_device change the chip state to READY and then wake up the wait queue .
nand_check_wp: check if the device is write protected .
Now We will parse the mtd nand functions one by one .
nand_erase -> nand_erase_nand
1 nand_get_device to get the device
2 select_chip to enable the CE pin of NAND
3 nand_check_wp to check it is write proected or not
4 enter into the loop
check whether the block is bad , if it is bad , failed and exit
chip->erase_cmd to erase the block
chip_waitfunc to wait the operation finished
according to the status from waitfunc function , if failed ,exit
add the page to erase next block
5 release the device
6 return