Chinaunix首页 | 论坛 | 博客
  • 博客访问: 265430
  • 博文数量: 49
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 540
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-08 10:33
文章分类

全部博文(49)

文章存档

2010年(2)

2009年(30)

2008年(17)

我的朋友

分类: LINUX

2009-07-27 16:55:44

    声明:以下移植是参照网上的一篇基于优龙FS2410开发板U-Boot-1.1.6的移植,这篇文章确实写得很好,佩服。原文地址如下:http://blog.chinaunix.net/u2/74310/showart_1091899.html
    本文实现U-Boot的写操作,具体步骤如下:
    1. 加入 NAND 闪存芯片型号
     在/include/linux/mtd/ nand_ids.h 中对如下结构体赋值进行修改:
       static struct nand_flash_dev nand_flash_ids[]= {
       ......
        {"Samsung K9F1208U0M",    NAND_MFR_SAMSUNG, 0x76, 26, 0, 3, 0x4000, 0},  
       ......
        }
      这样对于该款 NAND 闪存芯片的操作才能正确执行。
     2.  编写 NAND 闪存初始化函数
     /drivers/nand_legacy/nand_legacy.c 中加入 nand_init()函数。
 

#if (CONFIG_COMMANDS & CFG_CMD_NAND)
#include <s3c2410.h>

typedef enum {
    NFCE_LOW,
    NFCE_HIGH
} NFCE_STATE;


static inline void NF_Conf(u16 conf)
{
    S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
    nand->NFCONF = conf;
}

static inline void NF_Cmd(u8 cmd)
{
    S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
    nand->NFCMD = cmd;
}


static inline void NF_CmdW(u8 cmd)
{
    NF_Cmd(cmd);
    udelay(1);
}

static inline void NF_Addr(u8 addr)
{
    S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
    nand->NFADDR = addr;
}

static inline void NF_SetCE(NFCE_STATE s)
{
    S3C2410_NAND * const nand = S3C2410_GetBase_NAND();


    switch (s) {
        case NFCE_LOW:
            nand->NFCONF &= ~(1<<11);
            break;

        case NFCE_HIGH:
            nand->NFCONF |= (1<<11);
            break;
    }
}

static inline void NF_WaitRB(void)
{
    S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
    while (!(nand->NFSTAT & (1<<0)));
}

static inline void NF_Write(u8 data)
{
    S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
    nand->NFDATA = data;
}

static inline u8 NF_Read(void)
{
    S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
    return(nand->NFDATA);
}

static inline void NF_Init_ECC(void)
{
    S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
    nand->NFCONF |= (1<<12);
}

static inline u32 NF_Read_ECC(void)
{
    S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
    return(nand->NFECC);
}

extern ulong
nand_probe(ulong physadr);


static inline void NF_Reset(void)
{
    int i;

    NF_SetCE(NFCE_LOW);
    NF_Cmd(0xFF); /* reset command */
    for(i = 0; i < 10; i++); /* tWB = 100ns. */
    NF_WaitRB(); /* wait 200~500us; */
    NF_SetCE(NFCE_HIGH);
}


static inline void NF_Init(void)
{
#if 0 /* a little bit too optimistic */
#define TACLS 0
#define TWRPH0 3
#define TWRPH1 0
#else
#define TACLS 0
#define TWRPH0 4
#define TWRPH1 2
#endif

    NF_Conf((1<<15)|(0<<14)|(0<<13)|(1<<12)|(1<<11)|(TACLS<<8)|(TWRPH0<<4)|(TWRPH1<<0));
    /*nand->NFCONF = (1<<15)|(1<<14)|(1<<13)|(1<<12)|(1<<11)|(TACLS<<8)|(TWRPH0<<4)|(TWRPH1<<0); */
    /* 1 1 1 1, 1 xxx, r xxx, r xxx */
    /* En 512B 4step ECCR nFCE=H tACLS tWRPH0 tWRPH1 */

    NF_Reset();
}

void
nand_init(void)
{
    S3C2410_NAND * const nand = S3C2410_GetBase_NAND();

    NF_Init();
#ifdef DEBUG
    printf("NAND flash probing at 0x%.8lX\n", (ulong)nand);
#endif
    printf ("%4lu MB\n", nand_probe((ulong)nand) >> 20);
}

#endif /* CONFIG_COMMANDS & CFG_CMD_NAND */

   可以看到 nand_init()调用 NF_Init()函数,使能 nand flash 控制器和 nand flash;调用 NF_Reset()函数置位,NF_WaitRB()查询 nand flash 的状态,最后在调用 nand_probe((ulong)nand)函数探测 nand flash.

3. 修改include/configs/GEC2410.h,在上次修改的基础上加上如下代码,定义 NAND 闪存命令层的底
接口函数等:

#define CFG_NAND_LEGACY        1
//#define NFCE_LOW        0
//#define NFCE_HIGH       1
#define CFG_ENV_IS_IN_NAND    1
#define CFG_NAND_BASE        0x4E000000
#define CMD_SAVEENV
#define CFG_ENV_SIZE            0x10000 /* Total Size of Environment Sector */
#define CFG_ENV_OFFSET      0x20000 /*
环境变量在NAND FLASH0x20000*/
#define CFG_MONITOR_BASE PHYS_SDRAM_1


/*-----------------------------------------------------------------------
 * NAND flash settings
 */
#if (CONFIG_COMMANDS & CFG_CMD_NAND)

#define CFG_NAND_LEGACY
#define CFG_MAX_NAND_DEVICE    1    /* Max number of NAND devices        */
#define SECTORSIZE 512

#define ADDR_COLUMN 1
#define ADDR_PAGE 2
#define ADDR_COLUMN_PAGE 3

#define NAND_ChipID_UNKNOWN     0x00
#define NAND_MAX_FLOORS 1
#define NAND_MAX_CHIPS 1

#define NAND_WAIT_READY(nand)    NF_WaitRB()

#define NAND_DISABLE_CE(nand)    NF_SetCE(NFCE_HIGH)
#define NAND_ENABLE_CE(nand)    NF_SetCE(NFCE_LOW)


#define WRITE_NAND_COMMAND(d, adr)    NF_Cmd(d)
#define WRITE_NAND_COMMANDW(d, adr)    NF_CmdW(d)
#define WRITE_NAND_ADDRESS(d, adr)    NF_Addr(d)
#define WRITE_NAND(d, adr)        NF_Write(d)
#define READ_NAND(adr)            NF_Read()
/* the following functions are NOP's because S3C24X0 handles this in hardware */
#define NAND_CTL_CLRALE(nandptr)
#define NAND_CTL_SETALE(nandptr)
#define NAND_CTL_CLRCLE(nandptr)
#define NAND_CTL_SETCLE(nandptr)

#define CONFIG_MTD_NAND_VERIFY_WRITE    1
#define CONFIG_MTD_NAND_ECC_JFFS2    1

#endif    /* CONFIG_COMMANDS & CFG_CMD_NAND */

4.
在GEC2410.h中打开命令:
/***********************************************************
 * Command definition
 ***********************************************************/
#define CONFIG_COMMANDS \
            (CONFIG_CMD_DFL     | \
            CFG_CMD_CACHE     | \
            CFG_CMD_ENV         | \
            CFG_CMD_NET         | \
            CFG_CMD_PING     | \
            CFG_CMD_NAND     | \         /*
打开 nand flash 命令 */
            /*CFG_CMD_EEPROM |*/ \
            /*CFG_CMD_I2C     |*/ \
            /*CFG_CMD_USB     |*/ \
            CFG_CMD_REGINFO  | \
            CFG_CMD_DATE     | \
            CFG_CMD_ELF)

    好了,make一下,看看结果,很不幸运,/env_nand.c206 undefined reference to 'nand_info'等等问题,原来nand flash 真正的擦除和读写函数使用的是 drivers/nand_legacy/ 目录下面的读写、擦除函数
int nand_legacy_erase(struct nand_chip* nand, size_t ofs,size_t len, int clean);
int nand_legacy_rw(struct nand_chip* nand, int cmd,size_t start, size_t len,size_t * retlen, u_char * buf);

5. 修改saveenv中对nand的读写函数为nand_legacy的读写函数,修改common/env_nand.c如下:



 

#include <common.h>

#if defined(CFG_ENV_IS_IN_NAND) /* Environment is in Nand Flash */

#include <command.h>
#include <environment.h>
#include <linux/stddef.h>
#include <malloc.h>
#include <nand.h>

#if ((CONFIG_COMMANDS&(CFG_CMD_ENV|CFG_CMD_NAND)) == (CFG_CMD_ENV|CFG_CMD_NAND))
#define CMD_SAVEENV
#elif defined(CFG_ENV_OFFSET_REDUND)
#error Cannot use CFG_ENV_OFFSET_REDUND without CFG_CMD_ENV & CFG_CMD_NAND
#endif

#if defined(CFG_ENV_SIZE_REDUND) && (CFG_ENV_SIZE_REDUND != CFG_ENV_SIZE)
#error CFG_ENV_SIZE_REDUND should be the same as CFG_ENV_SIZE
#endif

#ifdef CONFIG_INFERNO
#error CONFIG_INFERNO not supported yet
#endif

/* My Add*/
 
int nand_legacy_erase(struct nand_chip* nand,
 
 size_t ofs, size_t len, int clean);

int nand_legacy_rw (struct nand_chip* nand, int cmd,
        size_t start, size_t len,
        size_t * retlen, u_char * buf);

/* My Add*/
 
extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];

/* info for NAND chips, defined in drivers/nand/nand.c */
//extern nand_info_t nand_info[];


nand_info_t nand_info[CFG_MAX_NAND_DEVICE];

/* references to names in env_common.c */
extern uchar default_environment[];
extern int default_environment_size;
......
......
#else /* ! CFG_ENV_OFFSET_REDUND */
int saveenv(void) /* 2008-6-26 by weij */
{
    ulong total;
    int ret = 0;

    puts ("Erasing Nand...");
    //if (nand_erase(&nand_info[0], CFG_ENV_OFFSET, CFG_ENV_SIZE))

    if (nand_legacy_erase(nand_dev_desc+0, CFG_ENV_OFFSET, CFG_ENV_SIZE, 0))
        return 1;

    puts ("Writing to Nand... ");
    total = CFG_ENV_SIZE;
    //ret = nand_write(&nand_info[0], CFG_ENV_OFFSET, &total, (u_char*)env_ptr);

    nand_legacy_rw(nand_dev_desc+0, 0x00 | 0x02, CFG_ENV_OFFSET, CFG_ENV_SIZE, &total, (u_char*)env_ptr);

    if (ret || total != CFG_ENV_SIZE)
        return 1;

    puts ("done\n");
    return ret;
}
#endif /* CFG_ENV_OFFSET_REDUND */
......
......
/*
 * The legacy NAND code saved the environment in the first NAND device i.e.,
 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
 */

void env_relocate_spec (void)
{
#if !defined(ENV_IS_EMBEDDED)
    ulong total;
    int ret;

    total = CFG_ENV_SIZE;
    //ret = nand_read(&nand_info[0], CFG_ENV_OFFSET, &total, (u_char*)env_ptr);


    ret=nand_legacy_rw(nand_dev_desc+0, 0x01 | 0x02, CFG_ENV_OFFSET, CFG_ENV_SIZE, &total, (u_char*)env_ptr);

      if (ret || total != CFG_ENV_SIZE)
        return use_default();

    if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
        return use_default();
#endif /* ! ENV_IS_EMBEDDED */
}
#endif /* CFG_ENV_OFFSET_REDUND */
......
......

修改完之后,make一下,生成U-Boot.bin文件,烧写到NAND FLASH,用setenv 设置环境变量,之后再保存saveenv.用TFTP命令,下载内核,就可以出现

TFTP from server 192.168.0.50; our IP address is 192.168.0.100

Filename 'uImage'.

Load address: 0x30007fc0

Loading: #################################################################

         #################################################################

         #################################################################

         #################################################################

         ####################################################

done

下一步就是编译内核、引导内核啦!

阅读(1296) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~