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

📄 nand.c

📁 TI DM6446 EVM 串口下载程序 使用环境:windows dos环境或者linux 使用前需安装mono
💻 C
📖 第 1 页 / 共 2 页
字号:
			    gNandInfo.ECCMask = 0x0FFF0FFF;
			    		
			return E_PASS;
		}
		i++;
	}
	// No match was found for the device ID
	return E_FAIL;
}

// Routine to read a page from NAND
Uint32 NAND_ReadPage(Uint32 block, Uint32 page, Uint8 *dest) {
	Uint32 eccValue[4];
	Uint32 spareValue[4],tempSpareValue;
	Uint8 numReads,i;
	
	//Setup numReads
	numReads = (gNandInfo.bytesPerPage >> 9);
	if (numReads == 0) numReads++;

    // Write read command
    flash_write_cmd((PNAND_INFO)&gNandInfo,NAND_LO_PAGE);
	
	// Write address bytes
	flash_write_addr_cycles((PNAND_INFO)&gNandInfo, block, page);

    // Additional confirm command for big_block devices
	if(gNandInfo.bigBlock)	
		flash_write_cmd((PNAND_INFO)&gNandInfo, NAND_READ_30H);

	// Wait for data to be available
	if(NAND_WaitForRdy(NAND_TIMEOUT) != E_PASS)
		return E_FAIL;

	// Starting the ECC in the NANDFCR register for CS2(bit no.8)
	NAND_ECCReadAndRestart((PNAND_INFO)&gNandInfo);

    // Read the page data
    for (i=0; i<numReads; i++)
    {
        // Actually read bytes
	    flash_read_bytes((PNAND_INFO)&gNandInfo, (void*)(dest), gNandInfo.bytesPerPage);	    
	    // Get the ECC Value
	    eccValue[i] = NAND_ECCReadAndRestart((PNAND_INFO)&gNandInfo);
	    //Increment pointer
	    dest += gNandInfo.bytesPerPage;
	}
		
	// Read the stored ECC value(s)
	for (i=0; i<numReads; i++)
	{
	    if (gNandInfo.bytesPerPage == 256)
            flash_read_bytes((PNAND_INFO)&gNandInfo, (void*)(spareValue), 8);
        else
            flash_read_bytes((PNAND_INFO)&gNandInfo, (void*)(spareValue),16);

        if (gNandInfo.bigBlock)
        {
            flash_swap_data((PNAND_INFO)&gNandInfo, (Uint32*)(spareValue+2));
            tempSpareValue = spareValue[2];
        }
        else
        {
            flash_swap_data((PNAND_INFO)&gNandInfo, (Uint32*)(spareValue));
            tempSpareValue = spareValue[0];
        }

	    // Verify ECC values
		if(eccValue[i] != tempSpareValue)
        {
            UARTSendData((Uint8 *)"NAND ECC failure!\r\n", FALSE);
            return E_FAIL;
        }
    }
    
    // Read remainder of spare bytes
	//flash_read_bytes( (PNAND_INFO)&gNandInfo, (void*)(dest), (gNandInfo.spareBytesPerPage - (4*numReads)) );

    // Return status check result
	return NAND_WaitForStatus(NAND_TIMEOUT);
	
}

// Generic routine to write a page of data to NAND
Uint32 NAND_WritePage(Uint32 block, Uint32 page, Uint8 *src) {
	Uint32 eccValue[4];
	Uint32 tempSpareValue[4];
	Uint8 numWrites,i;
	
	//Setup numReads
	numWrites = (gNandInfo.bytesPerPage >> 9);
	if (numWrites == 0) numWrites++;

	// Write program command
	flash_write_cmd((PNAND_INFO)&gNandInfo, NAND_PGRM_START);
	
	// Write address bytes
	flash_write_addr_cycles((PNAND_INFO)&gNandInfo,block,page);
	
	// Starting the ECC in the NANDFCR register for CS2(bit no.8)
	NAND_ECCReadAndRestart((PNAND_INFO)&gNandInfo);
	
	// Write data
	for (i=0; i<numWrites; i++)
	{
	    // Write data to page
	    flash_write_bytes((PNAND_INFO)&gNandInfo, (void*) src, gNandInfo.bytesPerPage);
	    
	    // Read the ECC value
	    eccValue[i] = NAND_ECCReadAndRestart((PNAND_INFO)&gNandInfo);
	    
	    // Increment the pointer
	    src += gNandInfo.bytesPerPage;
	}
	
	// Write spare bytes
	for (i=0; i<numWrites; i++)
	{
        flash_swap_data((PNAND_INFO)&gNandInfo, &(eccValue[i]));
        
        // Place the ECC values where the ROM read routine expects them
        if (gNandInfo.bigBlock)
        {
            tempSpareValue[0] = 0xFFFFFFFF;
            tempSpareValue[1] = 0xFFFFFFFF;
            tempSpareValue[2] = eccValue[i];
            tempSpareValue[3] = 0xFFFFFFFF;
        }
        else
        {
            tempSpareValue[0] = eccValue[i];
            tempSpareValue[1] = 0xFFFFFFFF;
            tempSpareValue[2] = 0xFFFFFFFF;
            tempSpareValue[3] = 0xFFFFFFFF;
        }
        if (gNandInfo.bytesPerPage == 256)
            flash_write_bytes((PNAND_INFO)&gNandInfo, (void*)(tempSpareValue), 8);
        else
            flash_write_bytes((PNAND_INFO)&gNandInfo, (void*)(tempSpareValue),16);
    }
    			
    // Write program end command
    flash_write_cmd((PNAND_INFO)&gNandInfo, NAND_PGRM_END);
	
	// Wait for the device to be ready
	if (NAND_WaitForRdy(NAND_TIMEOUT) != E_PASS)
		return E_FAIL;

    // Return status check result	
	return NAND_WaitForStatus(NAND_TIMEOUT);
}

// Verify data written by reading and comparing byte for byte
Uint32 NAND_VerifyPage(Uint32 block, Uint32 page, Uint8* src, Uint8* dest)
{
    Uint32 i;

    if (NAND_ReadPage(block, page, dest) != E_PASS)
        return E_FAIL;
    
    for (i=0; i< gNandInfo.bytesPerPage; i++)
    {
        // Check for data read errors
        if (src[i] != dest[i])
        {
            UARTSendData("Data mismatch! Verification failed.", FALSE);
            return E_FAIL;
        }
    }
	return E_PASS;
}

// NAND Flash erase block function
Uint32 NAND_EraseBlocks(Uint32 startBlkNum, Uint32 blkCnt)
{	
	Uint32 i;
		
	// Do bounds checking
	if ( (startBlkNum + blkCnt - 1) >= gNandInfo.numBlocks )
		return E_FAIL;
	
	// Output info about what we are doing
	UARTSendData((Uint8 *)"Erasing blocks 0x", FALSE);
	UARTSendInt(startBlkNum);
	UARTSendData((Uint8 *)" through 0x", FALSE);
	UARTSendInt(startBlkNum + blkCnt - 1);
	UARTSendData((Uint8 *)".\r\n", FALSE);

	for (i = 0; i < blkCnt; i++)
	{
		// Start erase command
		flash_write_cmd((PNAND_INFO)&gNandInfo, NAND_BERASEC1);

		// Write the row addr bytes only
		flash_write_row_addr_bytes((PNAND_INFO)&gNandInfo, (startBlkNum+i), 0);
		
		// Confirm erase command
		flash_write_cmd((PNAND_INFO)&gNandInfo, NAND_BERASEC2);

		// Wait for the device to be ready
		if (NAND_WaitForRdy(NAND_TIMEOUT) != E_PASS)
			return E_FAIL;

		// verify the op succeeded by reading status from flash
        if (NAND_WaitForStatus(NAND_TIMEOUT) != E_PASS)
			return E_FAIL;
	}

	return E_PASS;
}

// NAND Flash unprotect command
Uint32 NAND_UnProtectBlocks(Uint32 startBlkNum, Uint32 blkCnt)
{
	Uint32 endBlkNum;
	endBlkNum = startBlkNum + blkCnt - 1;

	// Do bounds checking
	if (endBlkNum >= gNandInfo.numBlocks)
		return E_FAIL;

	// Output info about what we are doing
	UARTSendData((Uint8 *)"Unprotecting blocks 0x", FALSE);
	UARTSendInt(startBlkNum);
	UARTSendData((Uint8 *)" through 0x", FALSE);
	UARTSendInt(endBlkNum);
	UARTSendData((Uint8 *)".\n", FALSE);

	flash_write_cmd((PNAND_INFO)&gNandInfo, NAND_UNLOCK_START);
	flash_write_row_addr_bytes((PNAND_INFO)&gNandInfo, startBlkNum, 0);
	
	flash_write_cmd((PNAND_INFO)&gNandInfo, NAND_UNLOCK_END);
	flash_write_row_addr_bytes((PNAND_INFO)&gNandInfo, endBlkNum, 0);
		
	return E_PASS;
}

// NAND Flash protect command
void NAND_ProtectBlocks(void)
{
	UARTSendData((Uint8 *)"Protecting the entire NAND flash.\n", FALSE);
	flash_write_cmd((PNAND_INFO)&gNandInfo, NAND_LOCK);
}


// Generic function to write a UBL or Application header and the associated data
Uint32 NAND_WriteHeaderAndData(NAND_BOOT *nandBoot, Uint8 *srcBuf) {
	Uint32     endBlockNum;
	Uint32     *ptr;
	Uint32     blockNum;
	Uint32     count;
	Uint32     countMask;
	Uint32     numBlks;
	
	// Get total number of blocks needed
	numBlks = 0;
	while ( (numBlks * gNandInfo.pagesPerBlock)  < (nandBoot->numPage + 1) )
	{
		numBlks++;
	}
	UARTSendData((Uint8 *)"Number of blocks needed for header and data: 0x", FALSE);
	UARTSendInt(numBlks);
	UARTSendData((Uint8 *)"\r\n", FALSE);

	// Check whether writing UBL or APP (based on destination block)
	blockNum = nandBoot->block;
	if (blockNum == START_UBL_BLOCK_NUM)
	{
		endBlockNum = END_UBL_BLOCK_NUM;
	}
	else if (blockNum == START_APP_BLOCK_NUM)
	{
		endBlockNum = END_APP_BLOCK_NUM;
	}
	else
	{
		return E_FAIL; /* Block number is out of range */
	}

NAND_WRITE_RETRY:
	if (blockNum > endBlockNum)
	{
		return E_FAIL;
	}
	UARTSendData((Uint8 *)"Attempting to start in block number 0x", FALSE);
	UARTSendInt(blockNum);
	UARTSendData((Uint8 *)".\n", FALSE);

	// Unprotect all needed blocks of the Flash 
	if (NAND_UnProtectBlocks(blockNum,numBlks) != E_PASS)
	{
		blockNum++;
		UARTSendData((Uint8 *)"Unprotect failed\n", FALSE);
		goto NAND_WRITE_RETRY;
	}

	// Erase the block where the header goes and the data starts
	if (NAND_EraseBlocks(blockNum,numBlks) != E_PASS)
	{
		blockNum++;
		UARTSendData((Uint8 *)"Erase failed\n", FALSE);
		goto NAND_WRITE_RETRY;
	}
		
	// Setup header to be written
	ptr = (Uint32 *) gNandTx;
	ptr[0] = nandBoot->magicNum;
	ptr[1] = nandBoot->entryPoint;
	ptr[2] = nandBoot->numPage;
	ptr[3] = blockNum;	//always start data in current block
	ptr[4] = 1;			//always start data in page 1 (this header goes in page 0)
	ptr[5] = nandBoot->ldAddress;

	// Write the header to page 0 of the current blockNum
	UARTSendData((Uint8 *)"Writing header...\n", FALSE);
	if (NAND_WritePage(blockNum, 0, gNandTx) != E_PASS)
		return E_FAIL;
		
	waitloop(200);

	// Verify the page just written
	if (NAND_VerifyPage(blockNum, 0, gNandTx, gNandRx) != E_PASS)
		return E_FAIL;

	count = 1; 

	// The following assumes power of 2 page_cnt -  *should* always be valid 
	countMask = (Uint32)gNandInfo.pagesPerBlock - 1;
	UARTSendData((Uint8 *)"Writing data...\n", FALSE);
	do {
		// Write the UBL or APP data on a per page basis
		if (NAND_WritePage(blockNum, (count & countMask), srcBuf) != E_PASS)
			return E_FAIL;

		waitloop(200);
		
		// Verify the page just written
		if (NAND_VerifyPage(blockNum, (count & countMask), srcBuf, gNandRx) != E_PASS)
		    return E_FAIL;
		
		count++;
		srcBuf += gNandInfo.bytesPerPage;
		if (!(count & countMask))
		{
			blockNum++;
		}
	} while (count <= nandBoot->numPage);

	NAND_ProtectBlocks();

	return E_PASS;
}

#endif

⌨️ 快捷键说明

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