u-boot-2016.03 在mini2440移植之nandflash读写

时间:2021-10-30 17:12:21

在“NorFlash启动”一文中,我们把drivers/mtd/nand/s3c2410_nand.c文件复制为s3c2440_nand.c文件,并把该文件内的所有有关“2410”的地方一律改为“2440”。这么修改仅仅是能够让系统编译成功,并没有真正实现NandFlash的读写。在这里,我们就来介绍如何让u-boot支持NandFlash的读写。

 

由于s3c2410与s3c2440的NandFlash控制器不一样,因此s3c2440_nand.c文件并不能直接应用,需要进行适当的修改,而主要修改的内容就是s3c2440的相关寄存器。

 

首先重新定义要用到的寄存器,把原文中第27行至第37行之间的宏定义去掉,改为下面的形式:

#define S3C2440_NFCONT_SECCL       (1<<6)

#define S3C2440_NFCONT_MECCL       (1<<5)

#define S3C2440_NFCONT_INITECC     (1<<4)

#define S3C2440_NFCONT_nCE         (1<<1)

#define S3C2440_NFCONT_MODE        (1<<0)

#define S3C2440_NFCONF_TACLS(x)    ((x)<<12)

#define S3C2440_NFCONF_TWRPH0(x)   ((x)<<8)

#define S3C2440_NFCONF_TWRPH1(x)   ((x)<<4)

 

#define S3C2440_ADDR_NALE        0x08

#define S3C2440_ADDR_NCLE       0x0C

 

然后就是修改s3c24x0_hwcontrol函数和board_nand_init函数,其他函数不变。

 

board_nand_init函数主要是用于对NandFlash的初始化,对它修改的内容是对寄存器NFCONF和寄存器NFCONT的修改,如下所示为修改后的board_nand_init函数,其中红色标注的地方为修改的地方:

int board_nand_init(struct nand_chip *nand)
{
        u_int32_t cfg;
        u_int8_t tacls, twrph0, twrph1;
        struct s3c24x0_clock_power *clk_power = s3c24x0_get_base_clock_power();
        struct s3c24x0_nand *nand_reg = s3c24x0_get_base_nand();

        debug("board_nand_init()\n");

        writel(readl(&clk_power->clkcon) | (1 << 4), &clk_power->clkcon);

        /* initialize hardware */
#if defined(CONFIG_S3C24XX_CUSTOM_NAND_TIMING)
        tacls  = CONFIG_S3C24XX_TACLS;
        twrph0 = CONFIG_S3C24XX_TWRPH0;
        twrph1 =  CONFIG_S3C24XX_TWRPH1;
#else
        tacls = 2;
        twrph0 = 3;
        twrph1 = 1;
#endif

         cfg = S3C2440_NFCONF_TACLS(tacls - 1);
        cfg |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
        cfg |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
        writel(cfg, &nand_reg->nfconf);

        cfg = S3C2440_NFCONT_SECCL;
        cfg |= S3C2440_NFCONT_MECCL;
        cfg |= S3C2440_NFCONT_MODE;
        writel(cfg,&nand_reg->nfcont);

 /* initialize nand_chip data structure */
        nand->IO_ADDR_R = (void *)&nand_reg->nfdata;
        nand->IO_ADDR_W = (void *)&nand_reg->nfdata;

        nand->select_chip = NULL;

        /* read_buf and write_buf are default */
        /* read_byte and write_byte are default */
#ifdef CONFIG_NAND_SPL
        nand->read_buf = nand_read_buf;
#endif

        /* hwcontrol always must be implemented */
        nand->cmd_ctrl = s3c24x0_hwcontrol;

        nand->dev_ready = s3c24x0_dev_ready;

#ifdef CONFIG_S3C2410_NAND_HWECC
        nand->ecc.hwctl = s3c24x0_nand_enable_hwecc;
        nand->ecc.calculate = s3c24x0_nand_calculate_ecc;
        nand->ecc.correct = s3c24x0_nand_correct_data;
        nand->ecc.mode = NAND_ECC_HW;
        nand->ecc.size = CONFIG_SYS_NAND_ECCSIZE;
        nand->ecc.bytes = CONFIG_SYS_NAND_ECCBYTES;
        nand->ecc.strength = 1;
#else
        nand->ecc.mode = NAND_ECC_SOFT;
#endif

#ifdef CONFIG_S3C2410_NAND_BBT
        nand->bbt_options |= NAND_BBT_USE_FLASH;
#endif

        debug("end of nand_init\n");
        return 0;
}                                                                                                                       

最后修改s3c24x0_hwcontrol函数,该函数是用于对NandFlash的写命令和写地址操作:

static void s3c24x0_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
{
        struct nand_chip *chip = mtd->priv;
        struct s3c24x0_nand *nand = s3c24x0_get_base_nand();

        debug("hwcontrol(): 0x%02x 0x%02x\n", cmd, ctrl);

        if (ctrl & NAND_CTRL_CHANGE) {
                ulong IO_ADDR_W = (ulong)nand;

                if (!(ctrl & NAND_CLE))
                        IO_ADDR_W |= S3C2440_ADDR_NCLE;
                if (!(ctrl & NAND_ALE))
                        IO_ADDR_W |= S3C2440_ADDR_NALE;
                 if(cmd ==NAND_CMD_NONE)

                     IO_ADDR_W = &nand->nfdata;

                chip->IO_ADDR_W = (void *)IO_ADDR_W;

                if (ctrl & NAND_NCE)
                        writel(readl(&nand->nfcont) & ~S3C2440_NFCONT_nCE,
                               &nand->nfcont);
                else
                        writel(readl(&nand->nfcont) | S3C2440_NFCONT_nCE,
                               &nand->nfcont);
        }

        if (cmd != NAND_CMD_NONE)

       writeb(cmd, chip->IO_ADDR_W);
}
       在这个函数中,除了修改寄存器的值以及设置写命令和写地址的IO端口外,我们还增加了if(cmd == NAND_CMD_NONE)判断语句。如果不加这个判断语句,向NandFlash内写数据是写不进去的,尽管系统不会提示任何错误,并显示“OK”,但其实数据是没有被写入的,因此一定要加上这条语句。这是因为在写完命令和地址后,一定还要把IO端口的地址重新设置为寄存器NFDATA。

 

需要说明的是,由于系统没有定义CONFIG_S3C2410_NAND_HWECC,因此我们暂时先不对s3c24x0_nand_enable_hwecc函数、s3c24x0_nand_calculate_ecc函数和s3c24x0_nand_correct_data函数进行修改。

我们把编译好的u-boot.bin文件烧写到norflash中,利用NandFlash的相关命令进行验证:

u-boot-2016.03 在mini2440移植之nandflash读写

 

u-boot-2016.03 在mini2440移植之nandflash读写

上电以后NAND显示为256MiB,说明系统能够正确识别出NandFlash。然后我们把SDRAM中的数据写入到NandFlash中,再读取NandFlash中的这段数据,通过与原SDRAM的数据做比较,可以看出这两段数据的内容是一致的,因此我们能够断定移植的u-boot可以对NandFlash进行正确的读和写操作。