TX2440 ARM开发板Uboot移植(三、添加Nand Flash的有关操作支持)

在上一节中我们说过,通常在嵌入式bootloader中,有两种方式来引导启动内核:从Nor Flash启动和从Nand Flash启动,但不管是从Nor启动或者从Nand启动,进入第二阶段以后,两者的执行流程是相同的。当u-boot的start.S运行到“_start_armboot: .word start_armboot”时,就会调用lib_arm/board.c中的start_armboot函数,至此u-boot正式进入第二阶段。此时注意:以前较早的u-boot版本进入第二阶段后,对Nand Flash的支持有新旧两套代码,新代码在drivers/nand目录下,旧代码在drivers/nand_legacy目录下,CFG_NAND_LEGACY宏决定了使用哪套代码,如果定义了该宏就使用旧代码,否则使用新代码。但是现在的u-boot版本对Nand的初始化、读写实现是基于最近的Linux内核的MTD架构,删除了以前传统的执行方法,使移植没有以前那样复杂了,实现Nand的操作和基本命令都直接在drivers/mtd/nand目录下(在doc/README.nand中讲得很清楚)。下面我们结合代码来分析一下u-boot在第二阶段的执行流程:

1.lib_arm/board.c文件中的start_armboot函数调用了drivers/mtd/nand/nand.c文件中的nand_init函数,如下:
  #if defined(CONFIG_CMD_NAND)      
//可以看到CONFIG_CMD_NAND宏决定了Nand的初始化
     
 puts ("NAND: ");
      nand_init();
  #endif
2.nand_init调用了同文件下的nand_init_chip函数;
3.nand_init_chip函数调用cpu/arm920t/s3c24x0/nand.c文件下的board_nand_init函数,然后再调用drivers/mtd/nand/nand_base.c函数中的nand_scan函数;
4.nand_scan函数调用了同文件下的nand_scan_ident函数等。

因为2440和2410对nand控制器的操作有很大的不同,所以s3c24x0/nand.c下对nandflash操作的函数就是我们做移植需要实现的部分了,他与具体的Nand Flash硬件密切相关。

1、修改 cpu\arm920t\s3c24x0\nand.c 文件内容为:

#include <common.h>
#if defined(CONFIG_CMD_NAND) && !defined(CFG_NAND_LEGACY)
#include <nand.h>
#if defined(CONFIG_S3C2410)
#include <s3c2410.h>
#define S3C2410_NFSTAT_READY    (1<<0)
#define S3C2410_NFCONF_nFCE     (1<<11)
/* select chip, for s3c2410 */
static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip)
{
    S3C2410_NAND * const s3c2410nand = S3C2410_GetBase_NAND();
    if (chip == -1) {
        s3c2410nand->NFCONF |= S3C2410_NFCONF_nFCE;
    } else {
        s3c2410nand->NFCONF &= ~S3C2410_NFCONF_nFCE;
    }
}

static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd)
{
    S3C2410_NAND * const s3c2410nand = S3C2410_GetBase_NAND();
    struct nand_chip *chip = mtd->priv;

    switch (cmd) {
    case NAND_CTL_SETNCE:
    s3c2410nand->NFCONF &= ~S3C2410_NFCONF_nFCE;
    case NAND_CTL_CLRNCE:
    s3c2410nand->NFCONF |= S3C2410_NFCONF_nFCE;
        printf("%s: called for NCE\n", __FUNCTION__);
        break;
    case NAND_CTL_SETCLE:
        chip->IO_ADDR_W = (void *)&s3c2410nand->NFCMD;
        break;
    case NAND_CTL_SETALE:
        chip->IO_ADDR_W = (void *)&s3c2410nand->NFADDR;
        break;
        /* NAND_CTL_CLRCLE: */
        /* NAND_CTL_CLRALE: */
    default:
        chip->IO_ADDR_W = (void *)&s3c2410nand->NFDATA;
        break;
    }
}

/* s3c2410_nand_devready()
 *
 * returns 0 if the nand is busy, 1 if it is ready
 */
static int s3c2410_nand_devready(struct mtd_info *mtd)
{
    S3C2410_NAND * const s3c2410nand = S3C2410_GetBase_NAND();
    return (s3c2410nand->NFSTAT & S3C2410_NFSTAT_READY);
}
#elif defined(CONFIG_S3C2440)
#include <s3c2440.h>
#define S3C2440_NFSTAT_READY    (1<<0)
#define S3C2440_NFCONT_nFCE     (1<<1)
/* select chip, for s3c2440 */
static void s3c2440_nand_select_chip(struct mtd_info *mtd, int chip)
{
    S3C2440_NAND * const s3c2440nand = S3C2440_GetBase_NAND();
    if (chip == -1) {
        s3c2440nand->NFCONT |= S3C2440_NFCONT_nFCE;
    } else {
        s3c2440nand->NFCONT &= ~S3C2440_NFCONT_nFCE;
    }
}

/* command and control functions */
static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd)
{
    S3C2440_NAND * const s3c2440nand = S3C2440_GetBase_NAND();
    struct nand_chip *chip = mtd->priv;

    switch (cmd) {
    case NAND_CTL_SETNCE:
    s3c2440nand->NFCONT &= ~S3C2440_NFCONT_nFCE;
    case NAND_CTL_CLRNCE:
    s3c2440nand->NFCONT |= S3C2440_NFCONT_nFCE;
        printf("%s: called for NCE\n", __FUNCTION__);
        break;
    case NAND_CTL_SETCLE:
        chip->IO_ADDR_W = (void *)&s3c2440nand->NFCMD;
        break;
    case NAND_CTL_SETALE:
        chip->IO_ADDR_W = (void *)&s3c2440nand->NFADDR;
        break;
        /* NAND_CTL_CLRCLE: */
        /* NAND_CTL_CLRALE: */
    default:
        chip->IO_ADDR_W = (void *)&s3c2440nand->NFDATA;
        break;
    }
}

/* s3c2440_nand_devready()
 *
 * returns 0 if the nand is busy, 1 if it is ready
 */
static int s3c2440_nand_devready(struct mtd_info *mtd)
{
    S3C2440_NAND * const s3c2440nand = S3C2440_GetBase_NAND();
    return (s3c2440nand->NFSTAT & S3C2440_NFSTAT_READY);
}
#endif
/*
 * Called by drivers/nand/nand.c, initialize the interface of nand flash
 */
int board_nand_init(struct nand_chip *chip)
{
#define TACLS   0
#define TWRPH0  4
#define TWRPH1  2
#if defined(CONFIG_S3C2410)
    S3C2410_NAND * const s3c2410nand = S3C2410_GetBase_NAND();
  /* Enable NAND flash controller, Initialize ECC, enable chip select, Set flash memory timing */
  s3c2410nand->NFCONF = (1<<15)|(1<<12)|(1<<11)|(TACLS<<8)|(TWRPH0<<4)|(TWRPH1<<0); 
    chip->IO_ADDR_R    = (void *)&s3c2410nand->NFDATA;
    chip->IO_ADDR_W    = (void *)&s3c2410nand->NFDATA;
    chip->hwcontrol    = s3c2410_nand_hwcontrol;
    chip->dev_ready    = s3c2410_nand_devready;
    chip->select_chip  = s3c2410_nand_select_chip;
#elif defined(CONFIG_S3C2440)
    S3C2440_NAND * const s3c2440nand = S3C2440_GetBase_NAND();
    s3c2440nand->NFCONF = (TACLS<<12)|(TWRPH0<<8)|(TWRPH1<<4);
    /* Initialize ECC, enable chip select, NAND flash controller enable */
    s3c2440nand->NFCONT = (1<<4)|(0<<1)|(1<<0);
    chip->IO_ADDR_R    = (void *)&s3c2440nand->NFDATA;
    chip->IO_ADDR_W    = (void *)&s3c2440nand->NFDATA;
    chip->hwcontrol    = s3c2440_nand_hwcontrol;
    chip->dev_ready    = s3c2440_nand_devready;
    chip->select_chip  = s3c2440_nand_select_chip;
#endif
    chip->options      = 0;
    chip->eccmode       = NAND_ECC_SOFT;
  return 0;
}
#endif

2、修改文件 include/s3c2440.h ,将 S3C2410_GetBase_NAND 修改为  S3C2440_GetBase_NAND ;仿照S3C2410_GetBase_NAND函数(96行)定义2440的函数:

static inline S3C2440_NAND * const S3C2440_GetBase_NAND(void)
{
    return (S3C2440_NAND * const)S3C2440_NAND_BASE;
}
重新编译u-boot并下载到Nand Flash中,把开发板调到Nand档从Nand启动。现在u-boot已经对我们开发板上256M的Nand Flash完全支持了。Nand相关的基本命令也都可以正常使用了。

3、将环境变量存储到Nand Flash中

从上面的启动信息看,有一个警告信息“*** Warning - bad CRC or NAND, using default environment”,这是因为没有将u-boot的环境变量保存nand中的缘故。u-boot在默认的情况下把环境变量都是保存到Nor Flash中的,所以要修改代码,让他保存到Nand中。修改 include/configs/smdk2440.h 文件:

/* 配置环境变量存放设置 */
#define CFG_ENV_ADDR  (CFG_FLASH_BASE + 0x100000) /* 使用NorFlash时,环境变量存放的开始地址 */
//#define  CFG_ENV_IS_IN_FLASH 1                    /* 环境变量保存的位置在NorFlash  */
#define CFG_ENV_IS_IN_NAND 1                        /*  环境变量保存的位置在NandFlash  */
#define CFG_ENV_OFFSET 0x60000                   /* 使用NandFlash时,环境变量存放的偏移地址 */
#define CFG_ENV_SIZE  0x20000               /*Total Size of Environment Sector;一般设定为NandFlash的一个Sector的大小*/

4、最后编译u-boot,生成u-boot.bin文件。将u-boot.bin下载到开发板的Nand Flash中,再把开发板调到Nand启动档,保存环境变量后重启开发板,那条警告信息现在没有了。

你可能感兴趣的:(嵌入式,移植,u-boot,ARM9,fl2440)