2010年(130)
分类: LINUX
2010-01-16 00:19:15
细心的人肯定会发现,这里定义了两个宏来设置NAND 的控制寄存器地址,NFCONF是指向整形的地址,而其他都是指向字符的地址 这与寄存器具体使用情况有关,因为datesheet中只有NFCONF使用了16位,其他都只使用了8位#include <config.h>
#define __REGb(x) (*(volatile unsigned char *)(x))
#define __REGi(x) (*(volatile unsigned int *)(x))
#define NF_BASE 0x4e000000
#define NFCONF __REGi(NF_BASE + 0x0)
#define NFCMD __REGb(NF_BASE + 0x4)
#define NFADDR __REGb(NF_BASE + 0x8)
#define NFDATA __REGb(NF_BASE + 0xc)
#define NFSTAT __REGb(NF_BASE + 0x10)
#define BUSY 1
inline void wait_idle(void) {
int i;
while(!(NFSTAT & BUSY))
for(i=0; i<10; i++); //这段的作用是判断NAND的状态,NFSTAT=0
//表明NAND忙,执行延时子程序。等待NAND空闲
}
#define NAND_SECTOR_SIZE 512
#define NAND_BLOCK_MASK (NAND_SECTOR_SIZE - 1)
/* low level nand read function */
int nand_read_ll(unsigned char *buf, unsigned long start_addr, int size)
{
int i, j;
if ((start_addr & NAND_BLOCK_MASK) || (size & NAND_BLOCK_MASK)) {
return -1; /* invalid alignment */
}
/* chip Enable */
NFCONF &= ~0x800;
for(i=0; i<10; i++); //delay
for(i=start_addr; i < (start_addr + size);) {
/* READ0 */
NFCMD = 0; //为什么写0参考k9f2410芯片手册,读命令
/* Write Address */ //地址操作,在k9f2410中有表格列出,其中A8未用
NFADDR = i & 0xff;
NFADDR = (i >> 9) & 0xff; //左移9位
NFADDR = (i >> 17) & 0xff;
NFADDR = (i >> 25) & 0xff;
wait_idle();
for(j=0; j < NAND_SECTOR_SIZE; j++, i++) {
*buf = (NFDATA & 0xff);
buf++;
}//一次循环读NFDATA寄存器512次,得到512个字节
}
/* chip Disable */
NFCONF |= 0x800; /* chip disable */
return 0;