在「NorFlash啓動」一文中,咱們把drivers/mtd/nand/s3c2410_nand.c文件複製爲s3c2440_nand.c文件,並把該文件內的全部有關「2410」的地方一概改成「2440」。這麼修改僅僅是可以讓系統編譯成功,並無真正實現NandFlash的讀寫。在這裏,咱們就來介紹如何讓u-boot支持NandFlash的讀寫。函數
因爲s3c2410與s3c2440的NandFlash控制器不同,所以s3c2440_nand.c文件並不能直接應用,須要進行適當的修改,而主要修改的內容就是s3c2440的相關寄存器。spa
首先從新定義要用到的寄存器,把原文中第27行至第37行之間的宏定義去掉,改成下面的形式:debug
#define S3C2440_NFCONT_SECCL (1<<6)blog
#define S3C2440_NFCONT_MECCL (1<<5)ip
#define S3C2440_NFCONT_INITECC (1<<4)get
#define S3C2440_NFCONT_nCE (1<<1)cmd
#define S3C2440_NFCONT_MODE (1<<0)flash
#define S3C2440_NFCONF_TACLS(x) ((x)<<12)it
#define S3C2440_NFCONF_TWRPH0(x) ((x)<<8)io
#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的相關命令進行驗證:
上電之後NAND顯示爲256MiB,說明系統可以正確識別出NandFlash。而後咱們把SDRAM中的數據寫入到NandFlash中,再讀取NandFlash中的這段數據,經過與原SDRAM的數據作比較,能夠看出這兩段數據的內容是一致的,所以咱們可以判定移植的u-boot能夠對NandFlash進行正確的讀和寫操做。