亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? nand.c

?? NAND Flash Boot Please select function : 0 : USB download file(通過USB下載文件) 1 : Uart download fi
?? C
?? 第 1 頁 / 共 2 頁
字號:
#include "def.h"
#include "2410addr.h"
#include "2410lib.h"


#define	BLK_IDXL	8
#define	BLK_IDXH	9
#define	FMT_TAG		15

char format_tags[] = "Formatted For NAND FLASH Driver";	//must be less than 32
/***********************************************************/
//nand ecc utils
typedef	unsigned char u_char;
static u_char eccpos[6] = {0, 1, 2, 3, 6, 7};

/*
 * Pre-calculated 256-way 1 byte column parity
 */
static const u_char nand_ecc_precalc_table[] = {
	0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00,
	0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
	0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
	0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
	0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
	0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
	0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
	0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
	0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
	0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
	0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
	0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
	0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
	0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
	0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
	0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00
};


/**
 * nand_trans_result - [GENERIC] create non-inverted ECC
 * @reg2:	line parity reg 2
 * @reg3:	line parity reg 3
 * @ecc_code:	ecc 
 *
 * Creates non-inverted ECC code from line parity
 */
static void nand_trans_result(u_char reg2, u_char reg3,
	u_char *ecc_code)
{
	u_char a, b, i, tmp1, tmp2;
	
	/* Initialize variables */
	a = b = 0x80;
	tmp1 = tmp2 = 0;
	
	/* Calculate first ECC byte */
	for (i = 0; i < 4; i++) {
		if (reg3 & a)		/* LP15,13,11,9 --> ecc_code[0] */
			tmp1 |= b;
		b >>= 1;
		if (reg2 & a)		/* LP14,12,10,8 --> ecc_code[0] */
			tmp1 |= b;
		b >>= 1;
		a >>= 1;
	}
	
	/* Calculate second ECC byte */
	b = 0x80;
	for (i = 0; i < 4; i++) {
		if (reg3 & a)		/* LP7,5,3,1 --> ecc_code[1] */
			tmp2 |= b;
		b >>= 1;
		if (reg2 & a)		/* LP6,4,2,0 --> ecc_code[1] */
			tmp2 |= b;
		b >>= 1;
		a >>= 1;
	}
	
	/* Store two of the ECC bytes */
	ecc_code[0] = tmp1;
	ecc_code[1] = tmp2;
}

/**
 * nand_calculate_ecc - [NAND Interface] Calculate 3 byte ECC code for 256 byte block
 * @dat:	raw data
 * @ecc_code:	buffer for ECC
 */
int nand_calculate_ecc(const u_char *dat, u_char *ecc_code)
{
	u_char idx, reg1, reg2, reg3;
	int j;
	
	/* Initialize variables */
	reg1 = reg2 = reg3 = 0;
	ecc_code[0] = ecc_code[1] = ecc_code[2] = 0;
	
	/* Build up column parity */ 
	for(j = 0; j < 256; j++) {
		
		/* Get CP0 - CP5 from table */
		idx = nand_ecc_precalc_table[dat[j]];
		reg1 ^= (idx & 0x3f);
		
		/* All bit XOR = 1 ? */
		if (idx & 0x40) {
			reg3 ^= (u_char) j;
			reg2 ^= ~((u_char) j);
		}
	}
	
	/* Create non-inverted ECC code from line parity */
	nand_trans_result(reg2, reg3, ecc_code);
	
	/* Calculate final ECC code */
	ecc_code[0] = ~ecc_code[0];
	ecc_code[1] = ~ecc_code[1];
	ecc_code[2] = ((~reg1) << 2) | 0x03;
	return 0;
}

/**
 * nand_correct_data - [NAND Interface] Detect and correct bit error(s)
 * @dat:	raw data read from the chip
 * @read_ecc:	ECC from the chip
 * @calc_ecc:	the ECC calculated from raw data
 *
 * Detect and correct a 1 bit error for 256 byte block
 */
int nand_correct_data(u_char *dat, u_char *read_ecc, u_char *calc_ecc)
{
	u_char a, b, c, d1, d2, d3, add, bit, i;
	
	/* Do error detection */ 
	d1 = calc_ecc[0] ^ read_ecc[0];
	d2 = calc_ecc[1] ^ read_ecc[1];
	d3 = calc_ecc[2] ^ read_ecc[2];
	
	if ((d1 | d2 | d3) == 0) {
		/* No errors */
		return 0;
	}
	else {
		a = (d1 ^ (d1 >> 1)) & 0x55;
		b = (d2 ^ (d2 >> 1)) & 0x55;
		c = (d3 ^ (d3 >> 1)) & 0x54;
		
		/* Found and will correct single bit error in the data */
		if ((a == 0x55) && (b == 0x55) && (c == 0x54)) {
			c = 0x80;
			add = 0;
			a = 0x80;
			for (i=0; i<4; i++) {
				if (d1 & c)
					add |= a;
				c >>= 2;
				a >>= 1;
			}
			c = 0x80;
			for (i=0; i<4; i++) {
				if (d2 & c)
					add |= a;
				c >>= 2;
				a >>= 1;
			}
			bit = 0;
			b = 0x04;
			c = 0x80;
			for (i=0; i<3; i++) {
				if (d3 & c)
					bit |= b;
				c >>= 2;
				b >>= 1;
			}
			b = 0x01;
			a = dat[add];
			a ^= (b << bit);
			dat[add] = a;
			return 1;
		}
		else {
			i = 0;
			while (d1) {
				if (d1 & 0x01)
					++i;
				d1 >>= 1;
			}
			while (d2) {
				if (d2 & 0x01)
					++i;
				d2 >>= 1;
			}
			while (d3) {
				if (d3 & 0x01)
					++i;
				d3 >>= 1;
			}
			if (i == 1) {
				/* ECC Code Error Correction */
				read_ecc[0] = calc_ecc[0];
				read_ecc[1] = calc_ecc[1];
				read_ecc[2] = calc_ecc[2];
				return 2;
			}
			else {
				/* Uncorrectable Error */
				return -1;
			}
		}
	}
	
	/* Should never happen */
	return -1;
}


#define	EnNandFlash()	(rNFCONF |= 0x8000)
#define	DsNandFlash()	(rNFCONF &= ~0x8000)
#define	InitEcc()		(rNFCONF |= 0x1000)
#define	NoEcc()			(rNFCONF &= ~0x1000)
#define	NFChipEn()		(rNFCONF &= ~0x800)
#define	NFChipDs()		(rNFCONF |= 0x800)

#define	WrNFCmd(cmd)	(rNFCMD = (cmd))
#define	WrNFAddr(addr)	(rNFADDR = (addr))
#define	WrNFDat(dat)	(rNFDATA = (dat))
#define	RdNFDat()		(rNFDATA)
#define	RdNFStat()		(rNFSTAT)
#define	NFIsBusy()		(!(rNFSTAT&1))
#define	NFIsReady()		(rNFSTAT&1)

//#define	WIAT_BUSY_HARD	1
//#define	ER_BAD_BLK_TEST
//#define	WR_BAD_BLK_TEST

#define	READCMD0	0
#define	READCMD1	1
#define	READCMD2	0x50
#define	ERASECMD0	0x60
#define	ERASECMD1	0xd0
#define	PROGCMD0	0x80
#define	PROGCMD1	0x10
#define	QUERYCMD	0x70
#define	RdIDCMD		0x90

static U16 NandAddr;


static void InitNandCfg(void)
{
	//enable nand flash control, initilize ecc, chip disable,
	rNFCONF = (1<<15)|(1<<12)|(1<<11)|(7<<8)|(7<<4)|(7);	
}

#ifdef	WIAT_BUSY_HARD
#define	WaitNFBusy()	while(NFIsBusy())
#else
static U32 WaitNFBusy(void)	// R/B 未接好?
{
	U8 stat;
	
	WrNFCmd(QUERYCMD);
	do {
		stat = RdNFDat();
		//printf("%x\n", stat);
	}while(!(stat&0x40));
	WrNFCmd(READCMD0);
	return stat&1;
}
#endif

static U32 ReadChipId(void)
{
	U32 id;
	
	NFChipEn();	
	WrNFCmd(RdIDCMD);
	WrNFAddr(0);
	while(NFIsBusy());	
	id  = RdNFDat()<<8;
	id |= RdNFDat();		
	NFChipDs();		
	
	return id;
}

static U16 ReadStatus(void)
{
	U16 stat;
	
	NFChipEn();	
	WrNFCmd(QUERYCMD);		
	stat = RdNFDat();	
	NFChipDs();
	
	return stat;
}

static U32 EraseBlock(U32 addr)
{
	U8 stat;

	addr &= ~0x1f;
		
	NFChipEn();	
	WrNFCmd(ERASECMD0);		
	WrNFAddr(addr);
	WrNFAddr(addr>>8);
	if(NandAddr)
		WrNFAddr(addr>>16);
	WrNFCmd(ERASECMD1);		
	stat = WaitNFBusy();
	NFChipDs();
	
#ifdef	ER_BAD_BLK_TEST
	if(!((addr+0xe0)&0xff)) stat = 1;	//just for test bad block
#endif
	
	//printf("Erase block 0x%x %s\n", addr, stat?"fail":"ok");
	putch('.');
	return stat;
}

//addr = page address
static void ReadPage(U32 addr, U8 *buf)
{
	U16 i;
	
	NFChipEn();
	WrNFCmd(READCMD0);
	WrNFAddr(0);
	WrNFAddr(addr);
	WrNFAddr(addr>>8);
	if(NandAddr)
		WrNFAddr(addr>>16);
	InitEcc();
	WaitNFBusy();
	for(i=0; i<512; i++)
		buf[i] = RdNFDat();
	NFChipDs();
}

static U32 WritePage(U32 addr, U8 *buf, U16 blk_idx)
{
	U16 i;
	U8 stat;
	U8 ecc_code[3];
	U8 oob_info[16];
		
	for(i=0; i<sizeof(oob_info); i++)
		oob_info[i] = 0xff;
	
	nand_calculate_ecc(buf, ecc_code);
	oob_info[eccpos[0]] = ecc_code[0];
	oob_info[eccpos[1]] = ecc_code[1];
	oob_info[eccpos[2]] = ecc_code[2];
	nand_calculate_ecc(buf+256, ecc_code);
	oob_info[eccpos[3]] = ecc_code[0];
	oob_info[eccpos[4]] = ecc_code[1];
	oob_info[eccpos[5]] = ecc_code[2];
	oob_info[BLK_IDXL]  = blk_idx;
	oob_info[BLK_IDXH]  = blk_idx>>8;
	oob_info[FMT_TAG]   = format_tags[addr&0x1f];
	
	NFChipEn();
	WrNFCmd(PROGCMD0);
	WrNFAddr(0);
	WrNFAddr(addr);
	WrNFAddr(addr>>8);
	if(NandAddr)
		WrNFAddr(addr>>16);
//	InitEcc();	
	for(i=0; i<512; i++)
		WrNFDat(buf[i]);
		
	if(!addr) {
		WrNFDat('b');
		WrNFDat('o');
		WrNFDat('o');
		WrNFDat('t');		
	} else {		
		for(i=0; i<sizeof(oob_info); i++)
			WrNFDat(oob_info[i]);
	}
/*	tmp[0] = rNFECC0;
    tmp[1] = rNFECC1;
    tmp[2] = rNFECC2;
    	
	WrNFDat(tmp[0]);
	WrNFDat(tmp[1]);
	WrNFDat(tmp[2]);
*/		
	WrNFCmd(PROGCMD1);
	stat = WaitNFBusy();
	NFChipDs();
	
#ifdef	WR_BAD_BLK_TEST
	if((addr&0xff)==0x17) stat = 1;	//just for test bad block
#endif
		
	if(stat)
		printf("Write nand flash 0x%x fail\n", addr);
	else {	
		U8 RdDat[512];
		
		ReadPage(addr, RdDat);		
		for(i=0; i<512; i++)
			if(RdDat[i]!=buf[i]) {
				printf("Check data at page 0x%x, offset 0x%x fail\n", addr, i);
				stat = 1;
				break;
			}
	}
		
	return stat;	
}

static void MarkBadBlk(U32 addr)
{
	addr &= ~0x1f;
	
	NFChipEn();
	
	WrNFCmd(READCMD2);	//point to area c
	
	WrNFCmd(PROGCMD0);
	WrNFAddr(4);		//mark offset 4,5,6,7
	WrNFAddr(addr);
	WrNFAddr(addr>>8);
	if(NandAddr)
		WrNFAddr(addr>>16);
	WrNFDat(0);			//mark with 0
	WrNFDat(0);
	WrNFDat(0);			//mark with 0
	WrNFDat(0);
	WrNFCmd(PROGCMD1);
	WaitNFBusy();		//needn't check return status
	
	WrNFCmd(READCMD0);	//point to area a
		
	NFChipDs();
}

static int CheckBadBlk(U32 addr)
{
	U8 dat;
	
	addr &= ~0x1f;

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
韩国三级中文字幕hd久久精品| 91啦中文在线观看| 欧美一区二区久久久| 日本成人在线网站| 成人性视频免费网站| 国产亚洲成av人在线观看导航| 日韩黄色在线观看| 国产精品福利av| 国产iv一区二区三区| 国产欧美精品一区二区色综合 | 91在线看国产| 亚洲精品自拍动漫在线| 在线一区二区三区四区五区 | 国产精品伦一区二区三级视频| 国产成人丝袜美腿| 亚洲欧洲成人自拍| 欧美精品在欧美一区二区少妇| 麻豆精品久久精品色综合| 久久久一区二区| 色综合久久久久综合| 亚洲成人激情综合网| 久久中文字幕电影| 色狠狠一区二区| 九九视频精品免费| 亚洲色图欧美在线| 日韩久久久精品| 91麻豆国产自产在线观看| 日韩精品一级中文字幕精品视频免费观看 | 日本一区中文字幕| 欧美国产日韩一二三区| 在线观看不卡视频| 极品少妇xxxx精品少妇| 亚洲人精品午夜| 日韩免费电影网站| 欧美无砖砖区免费| 国产精品一品二品| 天天色综合天天| 国产精品麻豆99久久久久久| 日韩一卡二卡三卡国产欧美| 97久久精品人人澡人人爽| 麻豆精品一区二区综合av| 亚洲三级在线免费观看| 337p粉嫩大胆噜噜噜噜噜91av| 91久久精品一区二区三区| 国产在线观看一区二区| 午夜精品在线视频一区| 国产精品沙发午睡系列990531| 日韩欧美第一区| 欧美日韩在线电影| 91欧美一区二区| 国产超碰在线一区| 国内精品伊人久久久久影院对白| 亚洲午夜av在线| 亚洲人成网站影音先锋播放| 久久久精品人体av艺术| 日韩美女一区二区三区四区| 精品视频色一区| 91免费观看视频| 成人18精品视频| 国产成人免费av在线| 精品一区二区在线免费观看| 亚洲1区2区3区视频| 亚洲精品写真福利| 成人欧美一区二区三区| 中文字幕欧美日韩一区| 久久久影院官网| 国产色一区二区| 久久久五月婷婷| 欧美成人aa大片| 欧美大片国产精品| 日韩美女在线视频| 精品国产99国产精品| 日韩欧美一级二级三级| 欧美一区二区三区婷婷月色| 欧美人xxxx| 欧美一区二区三区不卡| 欧美一区在线视频| 日韩一区二区免费在线电影| 欧美一区日本一区韩国一区| 在线播放视频一区| 88在线观看91蜜桃国自产| 欧美日韩精品福利| 91精品国产欧美一区二区18| 欧美日韩国产另类一区| 制服丝袜av成人在线看| 91精品一区二区三区在线观看| 欧美区在线观看| 日韩欧美中文字幕公布| 亚洲精品一区二区三区99| 久久九九影视网| 国产精品私人影院| 夜夜亚洲天天久久| 日韩经典一区二区| 国产一区二区三区香蕉| 国产91精品免费| 色先锋aa成人| 91麻豆精品久久久久蜜臀| 精品国产乱码久久久久久蜜臀| 欧美不卡一二三| 国产色爱av资源综合区| 亚洲同性同志一二三专区| 亚洲综合一区在线| 秋霞午夜鲁丝一区二区老狼| 久久超碰97中文字幕| 欧美综合在线视频| 欧美精品国产精品| 久久精品在这里| 亚洲激情五月婷婷| 老司机免费视频一区二区| 粉嫩欧美一区二区三区高清影视| 一本久久综合亚洲鲁鲁五月天| 欧美老肥妇做.爰bbww| 久久久噜噜噜久噜久久综合| 亚洲女与黑人做爰| 看电视剧不卡顿的网站| 99久久精品免费看国产| 欧美老肥妇做.爰bbww| 国产欧美综合在线观看第十页| 亚洲一卡二卡三卡四卡| 国产最新精品免费| 91九色02白丝porn| 久久久久久久久免费| 洋洋av久久久久久久一区| 极品尤物av久久免费看| 欧美性xxxxx极品少妇| 国产亚洲综合在线| 亚洲成人福利片| 不卡视频在线看| 日韩精品最新网址| 亚洲国产综合色| 成人黄色一级视频| 精品精品国产高清a毛片牛牛| 亚洲日本va午夜在线影院| 久久成人综合网| 欧美精品第1页| 亚洲久本草在线中文字幕| 国产一区不卡视频| 欧美一区二区三区四区五区| 亚洲精品视频免费观看| 国产精品 日产精品 欧美精品| 538prom精品视频线放| 国产精品不卡在线| 国产福利91精品一区二区三区| 5月丁香婷婷综合| 一区二区三区欧美视频| 菠萝蜜视频在线观看一区| 精品99一区二区| 美女视频一区二区三区| 欧美日韩久久一区| 亚洲亚洲人成综合网络| 91麻豆精品视频| 亚洲日韩欧美一区二区在线| 成人小视频免费观看| 久久久久久一二三区| 美腿丝袜亚洲三区| 在线综合视频播放| 日韩高清不卡一区二区| 欧美男人的天堂一二区| 亚洲第一主播视频| 欧美日韩在线播| 亚洲午夜羞羞片| 欧美日韩成人综合在线一区二区| 亚洲激情欧美激情| 91麻豆视频网站| 一区二区三区在线播| 色噜噜狠狠成人中文综合| 一区二区三区鲁丝不卡| 91成人免费网站| 亚洲国产一区在线观看| 欧美性大战久久久久久久| 亚洲成人中文在线| 91麻豆精品久久久久蜜臀| 美女视频一区在线观看| 久久综合av免费| 不卡在线视频中文字幕| 亚洲欧美另类小说| 欧美在线一区二区| 日本午夜精品视频在线观看| 欧美精品三级在线观看| 奇米精品一区二区三区四区 | 国产伦精品一区二区三区免费 | 午夜精品久久久久久久久| 欧美日韩视频在线一区二区| 亚洲成av人片在www色猫咪| 欧美精品粉嫩高潮一区二区| 国产精品视频免费| 色综合久久综合中文综合网| 亚洲一区影音先锋| 欧美精品在线观看一区二区| 久久精品99国产精品| 亚洲国产成人一区二区三区| 91一区二区三区在线播放| 亚洲高清视频的网址| 日韩视频免费直播| 成人综合在线观看| 亚洲永久精品大片| 欧美精品一区男女天堂| 成人av在线一区二区三区| 亚洲国产一区二区三区 | 国产精品一区二区你懂的|