/************************************************************************/
/* Copyright(c) ?, ? */
/* Created By ztg */
/* File: efDlg.cpp */
/* Ver: 1.0.1 */
/* Project: */
/* Description: */
/************************************************************************/
#include "stdafx.h"
#include "ef.h"
#include "efDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
volatile NANDreg *s2440NAND = (NANDreg *)NAND_BASE;
volatile CLKPWRreg *s2440CLKPWR = (CLKPWRreg *)CLKPWR_BASE;
//#define NFChipEn() (s2440NAND->rNFCONT &= ~(1<<1))
#define EnNandFlash() (s2440NAND->rNFCONT |= 1)
#define DsNandFlash() (s2440NAND->rNFCONT &= ~1)
#define NFChipEn() (s2440NAND->rNFCONT &= ~(1<<1))
#define NFChipDs() (s2440NAND->rNFCONT |= (1<<1))
#define InitEcc() (s2440NAND->rNFCONT |= (1<<4))
#define MEccUnlock() (s2440NAND->rNFCONT &= ~(1<<5))
#define MEccLock() (s2440NAND->rNFCONT |= (1<<5))
#define SEccUnlock() (s2440NAND->rNFCONT &= ~(1<<6))
#define SEccLock() (s2440NAND->rNFCONT |= (1<<6))
//#define WrNFDat8(dat) (s2440NAND->rNFDATA = (dat))//rNFDATA8
#define WrNFDat8(dat) (s2440NAND->rNFDATA8 = (dat))
//#define WrNFDat32(dat) (s2440NAND->rNFDATA = (dat))
//#define RdNFDat8() (s2440NAND->rNFDATA) //byte access//rNFDATA8
#define RdNFDat8() (s2440NAND->rNFDATA8) //byte access
//#define RdNFDat32() (s2440NAND->rNFDATA) //word access
#define WrNFCmd(cmd) (s2440NAND->rNFCMMD = (cmd))
#define WrNFAddr(addr) (s2440NAND->rNFADDR = (addr))
#define WrNFDat(dat) WrNFDat8(dat)
#define RdNFDat() RdNFDat8() //for 8 bit nand flash, use byte access
#define RdNFMEcc() (s2440NAND->rNFMECC0) //for 8 bit nand flash, only use NFMECC0
#define RdNFSEcc() (s2440NAND->rNFSECC) //for 8 bit nand flash, only use low 16 bits
#define RdNFStat() (s2440NAND->rNFSTAT)
#define NFIsBusy() (!(s2440NAND->rNFSTAT&1))
#define NFIsReady() (s2440NAND->rNFSTAT&1)
#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;
// CefDlg 对话框
CefDlg::CefDlg(CWnd* pParent /*=NULL*/)
: CDialog(CefDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CefDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CefDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
ON_WM_SIZE()
#endif
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_UpData, &CefDlg::OnBnClickedUpdata)
ON_BN_CLICKED(IDC_BUTTON1, &CefDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_UpDateWINCE, &CefDlg::OnBnClickedUpdatewince)
ON_BN_CLICKED(IDOK, &CefDlg::OnBnClickedOk)
ON_BN_CLICKED(IDC_AUTO, &CefDlg::OnBnClickedAuto)
END_MESSAGE_MAP()
// CefDlg 消息处理程序
CProgressCtrl myCtrl;
BOOL CefDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
this->SetWindowPos(&CWnd::wndBottom, 0, 0, 315, 190,SWP_SHOWWINDOW);
this->SetWindowText(_T("UpData"));
CenterWindow(GetDesktopWindow()); // center to the hpc screen
volatile NANDreg *s2440NAND = (NANDreg *)NAND_BASE;
volatile CLKPWRreg *s2440CLKPWR = (CLKPWRreg *)CLKPWR_BASE;
s2440NAND = (volatile NANDreg *)VirtualAlloc(0, sizeof(NANDreg), MEM_RESERVE, PAGE_NOACCESS);
s2440CLKPWR = (volatile CLKPWRreg *)VirtualAlloc(0, sizeof(CLKPWRreg), MEM_RESERVE, PAGE_NOACCESS);
InitNandFlash(1);
//VirtualFree((PVOID) s2440NAND, 0, MEM_RELEASE);
//VirtualFree((PVOID) s2440CLKPWR, 0, MEM_RELEASE);
myCtrl.Create(WS_CHILD|WS_VISIBLE,CRect(10,90,303,110),this,1);
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CefDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
DRA::RelayoutDialog(
AfxGetInstanceHandle(),
this->m_hWnd,
DRA::GetDisplayMode() != DRA::Portrait ?
MAKEINTRESOURCE(IDD_EF_DIALOG_WIDE) :
MAKEINTRESOURCE(IDD_EF_DIALOG));
}
#endif
int pri=1;
void CefDlg::OnBnClickedButton1()//update bootloader
{
// TODO: 在此添加控件通知处理程序代码
WrFileToNF(0);
s2440NAND->rNFCONT &= ~1; //disable nand flash interface
AfxMessageBox(L"UpDate BootLoader Ok");
myCtrl.SetPos(0);
}
void CefDlg::OnBnClickedUpdatewince()//update wince
{
// TODO: 在此添加控件通知处理程序代码
WrFileToNF(1);
s2440NAND->rNFCONT &= ~1; //disable nand flash interface
AfxMessageBox(L"UpDate Wince Ok");
myCtrl.SetPos(0);
}
void CefDlg::OnBnClickedUpdata()//update logo
{
WrFileToNF(2);
s2440NAND->rNFCONT &= ~1; //disable nand flash interface
AfxMessageBox(L"UpDate Logo Ok");
myCtrl.SetPos(0);
}
/************************************************************/
static int have_nandflash;
void InitNandFlash(int info)
{
U32 i;
//RETAILMSG(1, (_T("SALCD2: DisplayInit: can't open '%s'\r\n"), gszBaseInstance));
//RETAILMSG(1, (_T("NandFlash Init+\r\n")));
InitNandCfg();
i = ReadChipId();//Read chip id = ec76
//if(info)
//RETAILMSG(1, (_T("Read chip id = %x\n"), i));
if((i==0x9873)||(i==0xec75))
NandAddr = 0;
else if(i==0xec76)
NandAddr = 1;
else {
if(info)
//puts("Chip id error!!!\n");
have_nandflash = 0;
return;
}
have_nandflash = 1;
//if(info)
//RETAILMSG(1, (_T("Nand flash status = %x\n"), ReadStatus()));
}
static void InitNandCfg(void)
{
s2440CLKPWR->rCLKCON |= (1<<4);
s2440NAND->rNFCONF = (0<<12)|(6<<8)|(0<<4)|(0<<0);
s2440NAND->rNFCONT = (0<<13)|(0<<12)|(0<<10)|(0<<9)|(0<<8)|(1<<6)|(1<<5)|(1<<4)|(1<<1)|(1<<0);
s2440NAND->rNFSTAT = 0;
}
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;
}
//0xAEC00000
U32 downloadAddress=0xAE200000, downloadFileSize=0x0;//(B0000000-AE00000=2000000~~~32M)
static U32 StartPage1;
static U32 BlockCnt;
static int NandSelPart(int update)
{
// U16 i, max_sel;
struct Partition *ptr = NandPart;
if(0==update)
{
//printf("Write to nand flash part_3: offset 0x%-8x, size 0x%-8x [%s]\n", ptr->offset, ptr->size, ptr->name);
StartPage1 = NandPart[0].offset>>9;
BlockCnt = NandPart[0].size>>14;
return 0;
}
if(1==update)
{
ptr+=1;
//printf("Write to nand flash part_3: offset 0x%-8x, size 0x%-8x [%s]\n", ptr->offset, ptr->size, ptr->name);
StartPage1 = NandPart[1].offset>>9;
BlockCnt = NandPart[1].size>>14;
return 1;
}
if(2==update)
{
ptr+=3;
//printf("Write to nand flash part_3: offset 0x%-8x, size 0x%-8x [%s]\n", ptr->offset, ptr->size, ptr->name);
StartPage1 = NandPart[3].offset>>9;
BlockCnt = NandPart[3].size>>14;
return 3;
}
return -1;
}