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

📄 2410loader.c

📁 S3C2410的bootloader
💻 C
字号:
/************************************************
 * NAME    : 2410loader.C			*
 * DESC    : 					*
 * History : 2002.02.25 ver 0.0			*
************************************************/

#include <stdlib.h>
#include <string.h>
#include "option.h"
#include "def.h"
#include "2410addr.h"
#include "2410slib.h"
#include "2410addr.h"
#include "2410lib.h"
#include "k9s1208.h"
#include "mmu.h"

#include "usbmain.h"
#include "usbout.h"
#include "usblib.h"
#include "2410usb.h"


#define	DOWN_NANDBOOT	1	//download bootloader to nand flash
#define	DOWN_NORBOOT	0	//download bootloader to nor flash
#define	DOWN_OS_TYPE	0	//download OS Image to (1:SDRAM  2:Nand Flash)



#define DOWNLOAD_ADDRESS _RAM_STARTADDRESS
#define LED_ON	0xa
#define LED_OFF	0x0
#define OS_IMAGE_SIZE 830

void Port_Init(void);
void Led_Display(int);
void Delay(int);

extern int NF_MarkBadBlock(U32 block);
extern int NF_EraseBlock(U32 block);
extern int NF_WritePage(U32 block,U32 page,U8 *buffer,U8 *spareBuf);
extern U16 NF_CheckId(void);
extern void NF_Init(void);
extern int AM29F800_CheckId(void);

extern int isUsbdSetConfiguration;
extern unsigned char *downPt;

void Download_Nand(void);
void Download_Nor(void);
void Download_OS(void);
void Led_Flush(void);
void Init_CheckFlash(void);
char *hex2char(int val);

U8	badblock[64];
U8	badnum;

union uNand
{
  U16	a;
  U8	b[2];
};

void * function[][2]=
{      
	(void *)Download_Nand,		"Download Bootloader To Nand Flash\n",
	(void *)Download_Nor,		"Download Bootloader to Nor Flash\n",
	(void *)Download_OS,		"Download OS To Nand Flash\n", 
	0,0
};



void Main(void)
{
	U16	id, m_id, d_id;
	U32	i;

	memset(badblock, 0, 64);
	MMU_Init();
	ChangeClockDivider(1,1);		// 1:2:4
	ChangeMPllValue(0x5c,0x4,0x0);		//Fin=12MHz FCLK=200MHz
	//ChangeMPllValue(82, 1, 1);			//Fin=12MHz FCLK=180MHz	

	Port_Init();
	
	rGPHCON = rGPHCON&~(0xf<<18)|(0x5<<18);   
	//To enhance the USB signal quality.
	//CLKOUT 0,1=OUTPUT to reduce the power consumption.	

	Isr_Init();
	//if(*pMagicNum!=0x12345678)
    	//	consoleNum=0;
    	//else
    	//	consoleNum=1;




	NF_Init();
	Uart_Init(0,115200);
	Uart_Select(0);




	rMISCCR=rMISCCR&~(1<<3); // USBD is selected instead of USBH1 
	rMISCCR=rMISCCR&~(1<<13); // USB port 1 is enabled.

//
//  USBD should be initialized first of all.
//
	isUsbdSetConfiguration=0;

	UsbdMain(); 



	Uart_SendString("S3C2410 Bootloader Tool start..\n");
	Uart_SendString("Led Flush.\n");
	Led_Flush();

	Uart_SendString("Check Nand Flash ID.\n");
	id=NF_CheckId();
	m_id=(id>>8)&0xff;
	d_id=id&0xff;
	Uart_Printf("Manufacture ID is 0x%x, Device ID is 0x%x.\n", m_id, d_id);
	if(id==0xec75)
	{
		Uart_Printf("The Nand Flash is K9F5608UXX.\n");
		Uart_Printf("Start to check nand flash badblock.\n");
		Init_CheckFlash();
	}
	else
	{
		Uart_Printf("The Nand Flash is incorrect.\n");
	}

	AM29F800_CheckId();
	while(1)
	{
		i = 0;

		//Uart_Printf("\n\nSMDK2410 Board Program Ver 1.2(20030613) FCLK = %d Hz\n\n", FCLK);
		while(1)
		{	//display menu
			Uart_Printf("%2d:%s",i,function[i][1]);
			i++;
			if((int)(function[i][0])==0)
			{
				Uart_Printf("\n");
				break;
			}
			if((i%4)==0)
				Uart_Printf("\n");
		}
        
		Uart_Printf("\nSelect the function to test : ");
		i = Uart_GetIntNum();
		Uart_Printf("\n");

		//GPG4 Output Port [9:8] 01      -> LCD power On
		rGPGCON = (rGPGCON & 0xfffffcff) | (1<<8);
		rGPGDAT = (rGPGDAT & 0xffef) | (1<<4);        
        
		if(i>=0 && (i<(sizeof(function)/8)) ) 
			( (void (*)(void)) (function[i][0]) )();            
	}
}

void Init_CheckFlash()
{
	U32		i, j, mark;
	U8		data[512];
	union uNand	ublock;

	i=j=0;
	for(i=0; i<8; i++)
		NF_ReadPage(0, i, data);
	NF_ReadPage(0, 8, data);
	NF_EraseBlock(0);

	if((data[0]==0x55)&&(data[1]==0xaa)&&(data[2]==0x55)&&(data[3]=0xaa))
	{
		Uart_Printf("There are %d bad blocks.\n", data[4]);
		if(data[4]!=0x00)
		{
			Uart_Printf("Bad block as follow:\n");
			for(i=0; i<data[4]; i++)
			{
				badblock[i*2]=data[5+i*2];
				badblock[i*2+1]=data[5+i*2+1];

				ublock.b[1]=data[5+i*2];
				ublock.b[0]=data[5+i*2+1];
				if(i%8==0)
					Uart_Printf("\n");
				Uart_Printf("Block%d ", ublock.a);
			}
		}
	}
	else
	{
		data[0]=data[2]=0x55;
		data[1]=data[3]=0xaa;
		for(i=0; i<2048; i++)
		{
			mark=NF_IsBadBlock(i);
			if(mark==1)
			{
				ublock.a=i;
				badblock[j*2]=ublock.b[1];
				badblock[j*2+1]=ublock.b[0];
				data[5+j*2]=ublock.b[1];
				data[5+j*2+1]=ublock.b[0];
				
				j++;
			}
		}
		data[4]=j;
		if(j>32)
		{
			Uart_Printf("There are too many bad block to be used.\n");
			return;
		}
		badnum=j;
		mark=NF_WritePage(0, 8, data, NULL);		
		if(mark!=1)
			Uart_Printf("The first block is bad, suggest you change the nand flash.\n");
	}
}

void Led_Flush(void)
{
	rGPFDAT|=0xf0;
	Delay(1000);
	rGPFDAT&=0x0f;
	Delay(1000);
	rGPFDAT|=0xf0;
	Delay(1000);
	rGPFDAT&=0x0f;
	Delay(1000);
	rGPFDAT|=0xf0;
	Delay(1000);
	rGPFDAT&=0x0f;
}


char *hex2char(int val)
{
	static char str[2];
	str[1]='\0';	
	if(val<=9)str[0]='0'+val;
	else str[0]=('a'+val-10);
	return str;
}

⌨️ 快捷键说明

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