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

📄 romdrv.c

📁 最近在國外網站抓到的作業系統 以Arm為基礎去開發的
💻 C
字号:
/***************************************************************************************\
 *
 * Copyright (c) 2001 National ASIC Center, All Rights Reserved
 *
 * File Name	:	romdrv.c
 * Version		:	1.0
 * Programmer	:	longn_qi
 * 
 * Date of Creation:	2001/11/26
 * Date of Last Modify:	2001/11/27
 *
 * Description:	This is an application file for simulating ROM.
 *
 * Local Function List:
 * void InitROM( )
 * void FlashUnlockBlock( U32 Address )			(empty)
 * void FlashLockBlock( U32 Address )			(empty)
 * void FlashEraseBlock( U32 Address )
 * unsigned char FlashWriteByte( U32 Address, unsigned char Data )
 * U32 FlashWriteBlock( U32 Address, U32 Data, unsigned long len )
 * void CopyFlashProgram( )									(empty)
 * void WriteFlashProgram( U32 address, U32 data, unsigned long len )		(empty)
 * unsigned long FlashReadByte( U32 Address, unsigned char buffer )
 * unsigned long FlashReadBlock( U32 Address, U32 buffer, unsigned long len )
 *
 * Global Variable List:
 * None
 *
 * Note:		Further file description refers to simdrv.txt
 *
 ***************************************************************************************
 *
 * Modification History
 *
 * 2001/11/27		by longn_qi		add functions "FlashReadByte" and "FlashReadBlock".
 * 2001/11/26		by longn_qi		create file
 *
\***************************************************************************************/

/* System or Standard Header */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>//2002

#include "cdevice.h"
//#include <hardware\drball\reg.h>
//#include <hardware\drball\m68328.h>
#include "ppsmtype.h"
#include "ppsmmsg.h"
//#include <ppsm\ppsm.h>
////#include <hardware\drball\reg.h>
#include "m68328.h"
#include "asixsys.h"
#include "flash.h"
#include "lmalloc.h"
#include "Nucleus.h"
#include "pcdisk.h"
#include "fs.h"


U32 		g_nor_blknum;	//gfd Nor flash buffer blk num;
UINT8	g_nor_buf[256*1024];	


UINT8       *nor_rd_pages[NUM_RAMDISK_PAGES];
UINT16          nor_rd_opencount = 0;

typedef void (*	FBP)(U32 Address);    
typedef unsigned char (* FWP)(U32  Address,unsigned char Data);  
typedef U32 (* FWBP)(U32  Address,U32 Data,unsigned long len);
typedef unsigned long (* FRP)(unsigned char buffer, U32  Address);  
typedef unsigned long (* FRBP)(U32 buffer,U32 Address,unsigned long len);

FBP FlashUnlockBlockP = (FBP)FlashUnlockBlock;
FBP FlashLockBlockP = (FBP)FlashLockBlock;
FBP FlashEraseBlockP = (FBP)FlashEraseBlock;
FWP FlashWriteByteP = (FWP)FlashWriteByte;
FWBP FlashWriteBlockWordP = (FWBP)FlashWriteBlockWord;
////FWBP FlashWriteBlockP = (FWBP)FlashWriteBlock;
FWBP FlashWriteBlockP = (FWBP)FlashWriteBlockWord;

void InitROM(void )
{	
	g_nor_blknum = 0;
	drv_flash_read(( unsigned char *)g_nor_buf, (unsigned int *)(g_nor_blknum*512*512 + OLD_START_ADDRESS), FLASH_AREA_SIZE);   
}


void FlashUnlockBlock(U32 Address)
{
	U32	Sr,i;
	R_U32 tempAddr;
	U32	mid;
	U32	IMRTemp;
	U32	counter;
	U32 temp = 0;
/*
	mid = Address;
	mid = mid-(mid& 0x00000003);	
	tempAddr = (R_U32)mid;

	*tempAddr = 0x600060;
	*tempAddr = 0xd000d0;		//Write the control word in

	for(i=0; i<1000; i++);
	
	counter = 0;
	
	*tempAddr = 0x700070;
	mid  = *tempAddr;
	while( mid != 0x800080)
	{	
		*tempAddr = 0x700070;
		mid  = *tempAddr;
		
		counter++;
		if(counter > 10000)
			break;
	}			//Wait for the status register
	
	*tempAddr = 0xff00ff;
*/

	*(R_U32)Address = 0x600060;
	*(R_U32)Address = 0xd000d0;
	
	*(R_U32)Address = 0x700070;
	temp = *(R_U32)Address;
////	while( temp != 0x800080 )
	while( (*(R_U32)Address) != 0x800080 )
	{
		temp = *(R_U32)Address ;
	}	

}

void FlashLockBlock(U32 Address)
{
	U32	Sr,i;
	R_U32	tempAddr;
	U32	mid;
	U32	IMRTemp;
	U32	counter;
	
	mid = Address;
	mid = mid-(mid&0x00000003);	
	tempAddr = (R_U32)mid;
	
	*tempAddr = 0x600060;
	*tempAddr = 0x010001;

	for(i=0; i<100000; i++);
	
	counter = 0;
	
	*tempAddr = 0x700070;
	mid  = *tempAddr;
	while( mid != 0x800080)
	{	
		*tempAddr = 0x700070;
		mid  = *tempAddr;
		
		counter++;
		if(counter > 100000)
			break;
	}

	*tempAddr = 0xff00ff;
}

void FlashEraseBlock(U32 Address)
{
	U32	Sr;
	R_U32	tempAddr;
	U32	mid;
	U32	IMRTemp;
	U32	counter;
	
	mid = Address;
	mid = mid-(mid& 0x00000003) ;
	tempAddr = (R_U32)mid;	
	
	
	*tempAddr = 0x200020;
	*tempAddr = 0xd000d0;
	
	for(Sr=0; Sr<80000; Sr++);
	
	counter = 0;	
	*tempAddr = 0x700070;
	mid  = *tempAddr;
	while( mid != 0x800080)
	{	
		*tempAddr = 0x700070;
		for(Sr=0; Sr<20000; Sr++);
		mid  = *tempAddr;
		counter++;
		if(counter > 10000)
			break;
	}
	
	*tempAddr = 0xff00ff;
}

U8 FlashWriteByte(U32 Address, U8 Data)
{
	U32	IMRTemp,datatemp,k;
	U32	temp,mid;
	R_U32	tempAddr;
	U32	counter;
	U32 tempdata = Data;
	U32 retrytimes = 0;

RETRY_WRITE_BYTE:
	datatemp = *(R_U32)(Address - (Address&0x00000003));
	if( (Address&0x00000003 ) == 0)
	{
		temp = tempdata | (datatemp & 0xffffff00);
	}
	else if( (Address&0x00000003 ) == 0x1)
	{
		temp = (tempdata << 8) | (datatemp & 0xffff00ff);
	}
	else if( (Address &0x00000003 ) == 0x2)
	{
		temp = (tempdata << 16) | (datatemp & 0xff00ffff);
	}
	else if( (Address&0x00000003) == 0x3)
	{
		temp = (tempdata << 24) | (datatemp & 0x00ffffff);
	}
	tempAddr = (R_U32)( Address - (Address&0x00000003));

	counter = 0;
	
	*tempAddr = 0x400040;
	*tempAddr = temp;
	
	for(k=0; k<600; k++);

	
	*tempAddr = 0x700070;
	mid  = *tempAddr;
	while( mid != 0x800080)
	{	
		*tempAddr = 0x700070;
		mid  = *tempAddr;
		
		counter++;
		if(counter > 100000)
		 	break;
	}
		
	*tempAddr = 0x500050;
	*tempAddr = 0xff00ff;

	if(*tempAddr != temp)					//judge
	{
		if(retrytimes++ <5)
		goto	RETRY_WRITE_BYTE;				////gfd
		return(Address);
	}
}

U32 FlashWriteBlockWord(U32  Address, U32 DataPoint, U32 len)               //len--byte numbers;   need modify deeplier
{
	U32	i,mid,rawAddress,k;
	S32	lenX;
	U32	num, r;
	R_U32	tempAddr;
	U32	temp, temp32;
	U32	counter;
	U32 retrytimes=0;
	
	rawAddress = Address;
	if( len==0 )
		return(0);

	if(len < 4)
	{
		for(i = 0; i < len; i++)
		{
			FlashWriteByte(Address,*(P_U8)DataPoint);
			DataPoint += 1;
			Address += 1;
		}
		return( 0);
	}	
		
	if( (Address & 0x00000003) != 0x0)					//judge adress end 
	{
		if(len > 0)
		{
			if( (Address & 0x00000003) == 0x1)
			{
				FlashWriteByte(Address,*(P_U8)DataPoint);
				DataPoint += 1;
				Address += 1;
				FlashWriteByte(Address,*(P_U8)DataPoint);
				DataPoint+= 1;
				Address += 1;
				FlashWriteByte(Address,*(P_U8)DataPoint);
				DataPoint += 1;
				Address += 1;
				
			}
			else if( (Address & 0x00000003) == 0x2)
			{
				FlashWriteByte(Address,*(P_U8)DataPoint);
				DataPoint += 1;
				Address  += 1;
				FlashWriteByte(Address,*(P_U8)DataPoint);
				DataPoint += 1;
				Address += 1;
			}
			else if( (Address & 0x00000003 ) == 0x3)
			{
				FlashWriteByte(Address,*(P_U8)DataPoint);
				DataPoint += 1;
				Address += 1;
			}
			
			len = len -(rawAddress & 0x00000003);			
		}
		}			
	
	lenX = len;
	num = lenX>>2;
	r = lenX & 0x00000003;
	
	for(i = 0; i < num; i++)
	{	
		
RETRY_WRITE:
		tempAddr = (R_U32)(Address);

		if(DataPoint & 0x3)
		{
			temp = *(P_U8)DataPoint;
			DataPoint +=1;
			temp32= *(P_U8)DataPoint;
			temp |= (temp32 <<8);
			DataPoint +=1;
			temp32= *(P_U8)DataPoint;
			temp |= (temp32 <<16);
			DataPoint +=1;
			temp32= *(P_U8)DataPoint;
			temp |= (temp32 <<24);
			DataPoint +=1;
		}
		else temp = *(R_U32)DataPoint;
		
		
		*tempAddr = 0x400040;
		*tempAddr = temp;
		
		for(k=0; k<600; k++);
		
		counter = 0;
		*tempAddr = 0x700070;
		mid  = *tempAddr;
		while( mid != 0x800080)
		{	
			*tempAddr = 0x700070;
			mid  = *tempAddr;
		
			counter++;
			if(counter > 10000000)
			break;
		}
		
		*tempAddr = 0xff00ff;
	
		if(*tempAddr != temp)					//judge
		{
			if(retrytimes++ <5)
			goto	RETRY_WRITE;				////gfd
			return(Address);
		}
		Address += 4 ;
		DataPoint += 4;
	}

	for(i = 0; i < r; i++)					//fill left butes of lenX			
	{
		FlashWriteByte(Address,*(P_U8)DataPoint);
		DataPoint += 1;
		Address += 1;
	}
	
	*tempAddr = 0xff00ff;
	return(0);
	
}



U32 FlashWriteBlock(U32  Address, U32 DataPoint, U32 len)
{
	U32	i;
	S32	lenX;
	U32	IMRTemp, rawAddress;
	R_U32	tempAddr;
	U16	x,y,temp;
	U32	mid;
	P_U8	addr8;
	U8	temp8;
	U32	counter, BufferCount,counter1;	
	
		
	rawAddress = Address;
	lenX = len;
	if( lenX==0 )
		return(0);
		
	if( (Address % 4) != 0x0)					//judge adress end, and fill fore superabundance  bytes. 
	{
		if(lenX > 0)
		{
			if( (Address % 4 ) == 0x1)
			{
				FlashWriteByte(Address,*(P_U8)DataPoint);
				DataPoint++;
				Address++;
				FlashWriteByte(Address,*(P_U8)DataPoint);
				DataPoint++;
				Address++;
				FlashWriteByte(Address,*(P_U8)DataPoint);
				DataPoint++;
				Address++;
			
			}
			else if( (Address % 4 ) == 0x2)
			{
				FlashWriteByte(Address,*(P_U8)DataPoint);
				DataPoint++;
				Address++;
				FlashWriteByte(Address,*(P_U8)DataPoint);
				DataPoint++;
				Address++;
			}
			else if( (Address % 4 ) == 0x3)
			{
				FlashWriteByte(Address,*(P_U8)DataPoint);
				DataPoint++;
				Address++;
			}
			
			lenX = lenX -(rawAddress % 4);		
		}
	}	
	

	counter = 0;
	BufferCount = 0;
	tempAddr = (R_U32)Address;
	while(counter < lenX)
	{
		if( ( (U32)tempAddr + 32) / 0x20000 ==  (U32)tempAddr / 0x20000)
			if( (counter + 32) > lenX )
				BufferCount = (lenX - counter) / 2;
			else
				BufferCount = 16;
		else
			BufferCount = ( ( ( (U32)tempAddr / 0x20000) + 1) * 0x20000 -  (U32)tempAddr) / 2;
		if(BufferCount < 16)
		{
			(*FlashWriteBlockWordP)( (U32)tempAddr, DataPoint, BufferCount * 4);
			tempAddr += BufferCount;
			DataPoint += BufferCount * 4;
			counter += BufferCount * 4;
		}
		else
		{
			counter1 = 0x0;
			
			*tempAddr = 0xe800e8;   			  
			
			//while((*tempAddr & 0x80) == 0);
			
			while( *tempAddr != 0x800080)
			{	
				counter1++;
				if(counter1 > 10000000)
				break;
			}
			
			*tempAddr = 16 - 1; 
			
			counter += BufferCount * 4;
			for(i = 0; i < 16; i++)
			{
				temp = *(R_U32)DataPoint;
				*tempAddr = temp;
				DataPoint += 4;
				tempAddr++;
			}
			*(tempAddr - 1) = 0xd000d0;
			
			
			counter1 = 0x0;
			mid  = *(tempAddr - 1);
			*(tempAddr - 1) = 0x700070;
			while( mid != 0x800080)
			{	
				*(tempAddr - 1) = 0x700070;
				mid  = *(tempAddr - 1);
		
				counter1++;
				if(counter1 > 10000000)
				break;
			}
			
			*(tempAddr - 1) = 0x500050;
			*(tempAddr - 1) = 0xff00ff;
		}
	}
	

	Address = (P_U8)tempAddr;
	if(lenX != len)
	{
		for(i = 0; i < (lenX -lenX); i++)					//fill left butes of lenX			
		{
			FlashWriteByte(Address,*(P_U8)DataPoint);
			DataPoint++;
			Address++;
		}
	}
	return(0);

}


void CopyFlashProgram()
{
	P_U8	copyP,tempP;
	U32	temp1,temp2,len;

	len = (U32)CopyFlashProgram - (U32)FlashUnlockBlock;
	tempP = (P_U8)SysLmalloc(len);
	copyP = (P_U8)FlashUnlockBlock;
	MoveBlock((R_U32)copyP, (R_U32)tempP, len);

	FlashUnlockBlockP=tempP;

	temp1 = (U32)FlashUnlockBlock;
	temp2 = (U32)FlashLockBlock;
	FlashLockBlockP = tempP + temp2 - temp1;

	temp2 = (U32)FlashEraseBlock;
	FlashEraseBlockP = tempP + temp2 - temp1;

	temp2 = (U32)FlashWriteByte;
	FlashWriteByteP = tempP + temp2 - temp1;
	
	temp2 = (U32)FlashWriteBlockWord;
	FlashWriteBlockWordP = tempP + temp2 - temp1;

	temp2 = (U32)FlashWriteBlock;
	FlashWriteBlockP = tempP + temp2 - temp1;
    
}

unsigned long FlashReadBlock( U32 buffer, U32 Address, unsigned long len )
{
	*(U32 *)Address = 0xff00ff;				//gfd read status
	memcpy( (void *)buffer, (void *)Address, len );
	return 0;
}

void drv_flash_write ( unsigned char *data, unsigned int start, unsigned int size )
{
	#ifdef NORFLASH
////	(*FlashUnlockBlockP)(start);
	(*FlashEraseBlockP)(start);
	(*FlashWriteBlockP)( start, data, size );
////	(*FlashLockBlockP)( start );			////gfd 0813
	#else		//NAND FLASH
	nand_write(start, size, data);
	#endif
}

void drv_flash_read ( unsigned char *data, unsigned int start, unsigned int size )
{
	#ifdef NORFLASH
	FlashReadBlock( data, start, size );
	#else		//NAND FLASH
	nand_read(start, size, data);
	#endif
}



UINT8 *norflash_rd_alloc_page(VOID)
{
        return((UINT8 *)0);
}

VOID   norflash_rd_free_page(UINT8 *page)
{

}

INT norflash_rd_open(UINT16 driveno)
{
    return(YES);
}

INT norflash_rd_close(UINT16 driveno)
{
    return(YES);
}

extern INT norflash_rd_io(U16 driveno, U32 page, VOID *buffer, U16 count, INT do_read)
{
UINT8       *p;
UINT32      ltemp,tempblknum;
UINT32      page_number;
UINT32      byte_number;
UINT32		pageoffset = 0;
INT16       i;
UINT8       *pbuffer, *ptempbuffer;
INT			flag;

    /* We don't use drive no. You could have multiple drives if wanted */
    driveno = driveno;
    pbuffer = (UINT8 *)buffer;
    
    flag = do_read;
    flag &= 1;

    while (count)
    {
      	byte_number = 512*(page & 0x1ff);
		tempblknum = (page >> 9);
	
        /* Check. This shouldn't happen */
        //if ( (page_number >= NUM_RAMDISK_PAGES) || !nor_rd_pages[page_number] )
        //    return(NO);
	
        if(tempblknum != g_nor_blknum)	
        {
        	if (flag)
        	{	
        		p = (UINT8 *)(OLD_START_ADDRESS + page * 512);
        		drv_flash_read(( unsigned char *)(pbuffer + (pageoffset++)*512), (unsigned int *)p, 512 );
       	 }
        	else
        	{	
        	
			drv_flash_write (( unsigned char *) g_nor_buf,( unsigned int *) (g_nor_blknum*512*512 + OLD_START_ADDRESS), FLASH_AREA_SIZE );

			drv_flash_read(( unsigned char *)g_nor_buf, (unsigned int *)(tempblknum*512*512 + OLD_START_ADDRESS), FLASH_AREA_SIZE);
			
			for (i = 0; i < 512; i++)
				g_nor_buf[byte_number+i] = *pbuffer++;

			g_nor_blknum = 	tempblknum;
		}
	}
	else
	{
		if (flag)
		{
			for (i = 0; i < 512; i++)
			*pbuffer++ = g_nor_buf[byte_number+i] ;
		}
		else
		{
			for (i = 0; i < 512; i++)
			g_nor_buf[byte_number+i] = *pbuffer++;
		}
	}
	count--;
    page++;
    }
    
    if(do_read&FLUSH)
		drv_flash_write (( unsigned char *) g_nor_buf,( unsigned int *) (g_nor_blknum*512*512 + OLD_START_ADDRESS), FLASH_AREA_SIZE );

    return(YES);

}

INT norflash_rd_raw_open(UINT16 driveno)
{
}
INT norflash_rd_ioctl(UINT16 driveno, UINT16 command, VOID *buffer)
{
}

void norflash_flush(void)
{
	drv_flash_write (( unsigned char *) g_nor_buf,( unsigned int *) (g_nor_blknum*512*512 + OLD_START_ADDRESS), FLASH_AREA_SIZE );
}

int norflash_write_page_format(U32 page, U32 *buffer,  U32 do_read)
{
	int i;
	U8 *tmpbuffer;
	U8 *tmpaddr;

	tmpbuffer = (U8 *)buffer;
	tmpaddr = (U8 *)(page<<9);
	
	for(i = 0; i < 512; i++)							
	{
		FlashWriteByte(tmpaddr,*tmpbuffer);
		tmpbuffer ++;
		tmpaddr ++;
	}

	return YES;
}

⌨️ 快捷键说明

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