⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nandflash.c

📁 基于S3C2410和SM501的彩屏控制器程序
💻 C
📖 第 1 页 / 共 3 页
字号:
			NAND_ENABLE_CE(nand);  /* set pin low */
			NanD_Command(nand, NAND_CMD_ERASE1);
			NanD_Address(nand, ADDR_PAGE, ofs);
			NanD_Command(nand, NAND_CMD_ERASE2);
			NanD_Command(nand, NAND_CMD_STATUS);

#ifdef NAND_NO_RB
			{ u_char ret_val;
			  do{
				ret_val = READ_NAND(nandptr); /* wait till ready */
			  } while((ret_val & 0x40) != 0x40);
			}
#endif
			if (READ_NAND(nandptr) & 1) {
				//printf ("%s: Error erasing at 0x%lx\n",
					//__FUNCTION__, (long)ofs);
				/* There was an error */
				ret = -1;
				goto out;
			}
			if (clean) {
				int n;	/* return value not used */
				int p, l;

				/* clean marker position and size depend
				 * on the page size, since 256 byte pages
				 * only have 8 bytes of oob data
				 */
				if (nand->page256) {
					p = NAND_JFFS2_OOB8_FSDAPOS;
					l = NAND_JFFS2_OOB8_FSDALEN;
				}
				else {
					p = NAND_JFFS2_OOB16_FSDAPOS;
					l = NAND_JFFS2_OOB16_FSDALEN;
				}

				ret = nand_write_oob(nand, ofs + p, l,(size_t *)&n,
						     (u_char *)&clean_marker);
				/* quit here if write failed */
				if (ret)
					goto out;
			}
		}
		//else
			//UartSendOneByte(0,0x24);
		ofs += nand->erasesize;
		len -= nand->erasesize;
	}

out:
	/* De-select the NAND device */
	NAND_DISABLE_CE(nand);  /* set pin high */
#ifdef CONFIG_OMAP1510
    	archflashwp(0,1);
#endif
	return ret;
}

static __inline int nandcheck(unsigned long potential, unsigned long physadr)
{
	return 0;
}



//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 函数: NAND Flash初始化.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ulong nand_init (void)
{
	struct nand_chip *nand = NULL;
	int i = 0, ChipID = 1;
	ulong physadr;
	
	physadr = CFG_NAND_BASE;
#ifdef CONFIG_MTD_NAND_ECC_JFFS2
	oob_config.ecc_pos[0] = NAND_JFFS2_OOB_ECCPOS0;
	oob_config.ecc_pos[1] = NAND_JFFS2_OOB_ECCPOS1;
	oob_config.ecc_pos[2] = NAND_JFFS2_OOB_ECCPOS2;
	oob_config.ecc_pos[3] = NAND_JFFS2_OOB_ECCPOS3;
	oob_config.ecc_pos[4] = NAND_JFFS2_OOB_ECCPOS4;
	oob_config.ecc_pos[5] = NAND_JFFS2_OOB_ECCPOS5;
	oob_config.eccvalid_pos = 4;
#else
	oob_config.ecc_pos[0] = NAND_NOOB_ECCPOS0;
	oob_config.ecc_pos[1] = NAND_NOOB_ECCPOS1;
	oob_config.ecc_pos[2] = NAND_NOOB_ECCPOS2;
	oob_config.ecc_pos[3] = NAND_NOOB_ECCPOS3;
	oob_config.ecc_pos[4] = NAND_NOOB_ECCPOS4;
	oob_config.ecc_pos[5] = NAND_NOOB_ECCPOS5;
	oob_config.eccvalid_pos = NAND_NOOB_ECCVPOS;
#endif
	oob_config.badblock_pos = 5;
	for (i=0; i<CFG_MAX_NAND_DEVICE; i++)
	{
		if (nand_dev_desc[i].ChipID == NAND_ChipID_UNKNOWN) {
			nand = &nand_dev_desc[i];
			break;
		}
	}

	if (!nand)
		return (0);

	memset((char *)nand, 0, sizeof(struct nand_chip));

	nand->IO_ADDR = physadr;
	nand->cache_page = -1;  // init the cache page
	NanD_ScanChips(nand);

	if (nand->totlen == 0) {
		/* no chips found, clean up and quit */
		memset((char *)nand, 0, sizeof(struct nand_chip));
		nand->ChipID = NAND_ChipID_UNKNOWN;
		return (0);
	}

	nand->ChipID = ChipID;
	if (curr_device == -1)
		curr_device = i;

	if (!nand->data_buf) {
		puts ("Cannot allocate memory for data structures.\n");
		return (0);
	}

	return (nand->totlen);
}//nand_init()


#ifdef CONFIG_MTD_NAND_ECC
/*
 * 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
};


/*
 * 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;
}

/*
 * Calculate 3 byte ECC code for 256 byte block
 */
static void nand_calculate_ecc (const u_char *dat, u_char *ecc_code)
{
	u_char idx, reg1, reg3;
	int j;

	/* Initialize variables */
	reg1 = 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;

		/* All bit XOR = 1 ? */
		if (idx & 0x40) {
			reg3 ^= (u_char) j;
		}
	}

	/* Create non-inverted ECC code from line parity */
	nand_trans_result((reg1 & 0x40) ? ~reg3 : reg3, 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;
}

/*
 * Detect and correct a 1 bit error for 256 byte block
 */
static 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;
}

#endif
//#endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) */

//从dwNandAddr读出dwLen个字节到dwRamAddr
int ReadNandFlash(u32 dwNandAddr,u32 dwRamAddr,u32 dwLen)
{
	int cmd = NANDRW_READ|NANDRW_JFFS2;
  	int total = 0x100000;
	
	return nand_rw(nand_dev_desc , cmd, dwNandAddr, dwLen,(size_t*)&total, (u_char*)dwRamAddr);
} // NandReadData

int WriteNandFlash(u32 dwNandAddr,u32 dwRamAddr,u32 dwLen)
{
	int cmd = NANDRW_WRITE|NANDRW_JFFS2;
  	int total = 0x100000;
	
	return nand_rw(nand_dev_desc , cmd, dwNandAddr, dwLen,(size_t*)&total, (u_char*)dwRamAddr);
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 函数: NandFlash擦除.
//
// [参数表]
//	ofs: 擦除起始地址.
//	len: 擦除长度.
//
// 返回: 0-成功; 非0-不成功.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int NandErase(ulong ofs, ulong len)
{
	int ret;
	struct nand_chip* nand = &nand_dev_desc[curr_device];
	
	ret = nand_erase (nand, ofs, len, 1);
	return ret ;
}//NandErase()


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 函数: 读NandFlash.
// 描述: 从NandFlash地址读出dwLen个字节到内存地址.
//
// [参数表]
//	dwRamAddr: 内存地址.
//	dwNandAddr: NandFlash地址.
//	dwLen: 数据长度.
//
// 返回: 0-成功; 非0-不成功.
//
// 注: NandFlash读要以页对齐, 所以地址和长度要是页的整数倍(一个页512字节).
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int NandReadData(u32 dwRamAddr,u32 dwNandAddr,u32 dwLen)
{
	u32 tmpNandAddr,tmpLen;
	u16	piancha;
 	u32 tmpAddr;
 	
 	if(dwNandAddr%PAGE_BYTES == 0 && dwLen%PAGE_BYTES == 0)//传入参数已经以页对齐
		return ReadNandFlash(dwNandAddr,dwRamAddr,dwLen);
		
	tmpNandAddr = dwNandAddr;//临时变量记录原实际地址
	dwNandAddr = (dwNandAddr/PAGE_BYTES)*PAGE_BYTES;//地址以页对齐
	piancha = tmpNandAddr - dwNandAddr;//地址对齐后与原地址偏差值
	tmpLen = dwLen;//临时变量记录原实际长度
	dwLen += piancha;//长度跟随的偏差
	
	if(dwLen > 0) dwLen = ((dwLen - 1)/PAGE_BYTES + 1)*PAGE_BYTES;//长度以页对齐
	
	//tmpAddr = malloc(dwLen+1);//开辟一个内存区域
	//if(tmpAddr == NULL) return 0;
	tmpAddr = TEMP_LZMA_ADDR;
	
	if(ReadNandFlash(dwNandAddr,tmpAddr,dwLen) == 0) //读Nandflash成功
	{
		memcpy((char *)dwRamAddr,(char *)tmpAddr+piancha,tmpLen);
		//free(tmpAddr);//释放开辟的临时内存区
		return 0;
	}
	//free(tmpAddr);
	return 1;
}//NandReadData()



//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 函数: 写NandFlash.
// 描述: 
//
// [参数表]
//	dwNandAddr: 写入的NandFlash地址.
//	dwRamAddr: 要写入的数据的起始地址(内存地址).
//	dwLen: 写入的长度.
// 返回: 0-成功; 非0-不成功.
//
// 注: NandFlash写要以块对齐, 所以地址和长度必须是块的整数倍(一个块16k字节).
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int NandWriteData(u32 dwNandAddr,u32 dwRamAddr,u32 dwLen)
{
	u32 tmpNandAddr,tmpLen,tmpEndAddr;
	u16	piancha,tmpOff;
 	u32 tmpAddr;
 	char returnValue;
 	
	if(dwNandAddr%BLOCK_BYTES == 0 && dwLen%BLOCK_BYTES == 0)//传入参数已经对齐
		return WriteNandFlash(dwNandAddr,dwRamAddr,dwLen);

	returnValue = 1;
	tmpNandAddr = dwNandAddr;//临时变量记录原地址
	dwNandAddr  = (dwNandAddr/BLOCK_BYTES)*BLOCK_BYTES;//地址以块对齐
	piancha = tmpNandAddr - dwNandAddr;//需要写地址与实际开始地址偏差
	tmpOff = BLOCK_BYTES - piancha;
	tmpLen = dwLen;//临时变量记录原实际长度
	dwLen += piancha;//长度跟随的偏差
	if(dwLen > 0) dwLen = ((dwLen - 1)/BLOCK_BYTES + 1)*BLOCK_BYTES;//长度以块对齐
	
	tmpAddr = TEMP_LZMA_ADDR;
	//tmpAddr = malloc(BLOCK_BYTES+1);//开辟一个内存区域

	//if(tmpAddr == NULL) return 1;//未开辟成功,返回

	if(NandReadData((u32)tmpAddr,dwNandAddr,BLOCK_BYTES) == 0 || piancha == 0) //第一块回写,先把第一块数据读出
	{
		if(dwLen > BLOCK_BYTES) // 跨块写数据
		{
			memcpy((char *)tmpAddr+piancha,(char *)dwRamAddr,tmpOff);//第一块要写的实际数据(后部分)
			if (NandErase(dwNandAddr, BLOCK_BYTES) == 0  && //擦除nandflash
			WriteNandFlash(dwNandAddr,TEMP_LZMA_ADDR,BLOCK_BYTES) == 0)//写第一块
			{
				tmpEndAddr = tmpNandAddr + tmpLen; 					//写结束地址
				tmpNandAddr = (tmpEndAddr/BLOCK_BYTES)*BLOCK_BYTES;//写最后一块的起始地址
				piancha = tmpEndAddr - tmpNandAddr;
				if(NandReadData((u32)tmpAddr,tmpEndAddr,BLOCK_BYTES) == 0)	//读出最后一块数据
				{
					memcpy((char *)tmpAddr,(char *)dwRamAddr + tmpLen - piancha,piancha);//最后一块实际写数据
					if (NandErase(tmpNandAddr, BLOCK_BYTES) == 0  && 
					WriteNandFlash(tmpNandAddr,tmpAddr,BLOCK_BYTES) == 0)	//写最后一块
					{
						dwLen = tmpNandAddr - dwNandAddr - BLOCK_BYTES;//中间块写长度
						if(dwLen > 0)
						{
							if (NandErase(dwNandAddr + BLOCK_BYTES, dwLen) == 0)
								returnValue = WriteNandFlash(dwNandAddr + BLOCK_BYTES,dwRamAddr + tmpOff,dwLen); //写中间的块	
						}
						else
							returnValue = 0;
					}
				}
			}
			
		}
		else//只写一个块
		{
			memcpy((char *)tmpAddr+piancha,(char *)dwRamAddr,tmpLen);			//实际写的数据
			if (NandErase(dwNandAddr , BLOCK_BYTES) == 0)
				returnValue = WriteNandFlash(dwNandAddr,tmpAddr,BLOCK_BYTES);	//写入NandFlash

		}
		
	}
	return returnValue; 
	return 1;
}//NandWriteData()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -