分类: 嵌入式
2013-06-06 22:18:27
1、由原理图知接TQ2440的nanflash型号是K9F2G08U0A,2KB页面大页结构,256M。16位数据格式
2、在include/configs/smdk2440.h头文件中定义Nand要用到的宏和寄存器,如下:
vi include/configs/smdk2440.h
/*
* Nand flash register and envionment variables
*/
#define CONFIG_S3C2440_NAND_BOOT 1
#define NAND_CTL_BASE 0x4E000000
#define STACK_BASE 0x33F00000
#define STACK_SIZE 0x8000
#define oNFCONF 0x00
#define oNFCONT 0x04
#define oNFADDR 0x0c //0x4E00000c
#define oNFDATA 0x10 //0x4E000010
#define oNFCMD 0x08 //0x4E000008
#define oNFSTAT 0x20 //0x4E000020
#define oNFECC 0x2c //0x4E00002c
3、修改cpu/arm920t/start.S这个文件,使u-boot从Nand Flash启动,在上一节中提过,u-boot默认是从Nor Flash启动的。修改部分如下:
//注意:在上一篇Nor Flash启动中,我们为了把u-boot用supervivi下载到内存中运行而屏蔽掉这段有关CPU初始化的代码。而现在我们要把u-boot下载到Nand Flash中,从Nand Flash启动,所以现在要恢复这段代码。
#ifndef CONFIG_SKIP_LOWLEVEL_INIT
bl cpu_init_crit
#endif
//注释掉系统从Norflash启动这部分代码
#if 0
#ifndef CONFIG_SKIP_RELOCATE_UBOOT
relocate: /* relocate U-Boot to RAM */
adr r0, _start /* r0 <- current position of code */
ldr r1, _TEXT_BASE /* test if we run from flash or RAM */
cmp r0, r1 /* don't reloc during debug */
beq stack_setup
ldr r2, _armboot_start
ldr r3, _bss_start
sub r2, r3, r2 /* r2 <- size of armboot */
add r2, r0, r2 /* r2 <- source end address */
copy_loop:
ldmia r0!, {r3-r10} /* copy from source address [r0] */
stmia r1!, {r3-r10} /* copy to target address [r1] */
cmp r0, r2 /* until source end addreee [r2] */
ble copy_loop
#endif /* CONFIG_SKIP_RELOCATE_UBOOT */
#endif
//下面添加2440中u-boot从Nand Flash启动-----------------
#ifdef CONFIG_S3C2440_NAND_BOOT
mov r1, #NAND_CTL_BASE //复位Nand Flash
ldr r2, =( (0<<12)|(3<<8)|(0<<4)|(3<<2)|(1<<1) )
str r2, [r1, #oNFCONF] //设置配置寄存器的初始值,参考s3c2440手册
ldr r2, [r1, #oNFCONF]
ldr r2, =( (1<<4)|(1<<1)|(1<<0) )
str r2, [r1, #oNFCONT] //设置控制寄存器
ldr r2, [r1, #oNFCONT]
//ldr r2, =(0x01) //RnB Clear
//str r2, [r1, #oNFSTAT]
//ldr r2, [r1, #oNFSTAT]
mov r2, #0xff //复位command
strb r2, [r1, #oNFCMD]
mov r3, #0 //等待
nand1:
add r3, r3, #0x1
cmp r3, #0xa
blt nand1
nand2:
ldr r2, [r1, #oNFSTAT] //等待就绪
tst r2, #0x4
beq nand2
ldr r2, [r1, #oNFCONT]
orr r2, r2, #0x02 //取消片选
str r2, [r1, #oNFCONT]
//get read to call C functions (for nand_read())
ldr sp, DW_STACK_START //为C代码准备堆栈,DW_STACK_START定义在下面
mov fp, #0
//copy U-Boot to RAM
ldr r0, =TEXT_BASE//传递给C代码的第一个参数:u-boot在RAM中的起始地址
mov r1, #0x0 //传递给C代码的第二个参数:Nand Flash的起始地址
mov r2, #0x30000 //传递给C代码的第三个参数:u-boot的长度大小(192k)
bl nand_read_ll //此处调用C代码中读Nand的函数,现在还没有要自己编写实现
tst r0, #0x0
beq ok_nand_read
bad_nand_read:
loop2: b loop2 //infinite loop
ok_nand_read:
//检查搬移后的数据,如果前4k完全相同,表示搬移成功
mov r0, #0
ldr r1, =TEXT_BASE
mov r2, #0x1000 //4 bytes * 1024 = 4K-bytes
go_next:
ldr r3, [r0], #4
ldr r4, [r1], #4
teq r3, r4
bne notmatch
subs r2, r2, #4
beq stack_setup
bne go_next
notmatch:
loop3:
b loop3 //infinite loop
#endif //CONFIG_S3C2440_NAND_BOOT
_start_armboot: .word start_armboot //在这一句的下面加上DW_STACK_START的定义
.align 2
DW_STACK_START: .word STACK_BASE+STACK_SIZE-4
//以上为2440中u-boot从Nand Flash启动-----------------
4、在board/samsung/smdk2440/目录下新建一个nand_read.c文件,在该文件中来实现上面汇编中要调用的nand_read_ll函数,代码如下:
#include
#define BUSY 1
#define NF_BASE 0x4E000000 //Nand Flash配置寄存器基地址
#define __REGb(x) (*(volatile unsigned char *)(x))
#define __REGi(x) (*(volatile unsigned int *)(x))
#define NFCONF __REGi(NF_BASE + 0x0 ) //通过偏移量还是得到配置寄存器基地址
#define NFCONT __REGi(NF_BASE + 0x4 ) //通过偏移量得到控制寄存器基地址
#define NFCMD __REGb(NF_BASE + 0x8 ) //通过偏移量得到指令寄存器基地址
#define NFADDR __REGb(NF_BASE + 0xC ) //通过偏移量得到地址寄存器基地址
#define NFDATA __REGb(NF_BASE + 0x10) //通过偏移量得到数据寄存器基地址
#define NFSTAT __REGb(NF_BASE + 0x20) //通过偏移量得到状态寄存器基地址
#define NAND_CHIP_ENABLE (NFCONT &= ~(1<<1)) //Nand片选使能
#define NAND_CHIP_DISABLE (NFCONT |= (1<<1)) //取消Nand片选
#define NAND_CLEAR_RB (NFSTAT |= BUSY)
#define NAND_DETECT_RB { while(!(NFSTAT & BUSY));}
#define NAND_SECTOR_SIZE 2048
#define NAND_BLOCK_MASK (NAND_SECTOR_SIZE - 1)
static void s3c2440_write_addr_lp(unsigned int addr)
{
int i;
volatile unsigned char *p = (volatile unsigned char *)&NFADDR;
int col, page;
col = addr & NAND_BLOCK_MASK;
page = addr / NAND_SECTOR_SIZE;
*p = col & 0xff; /* Column Address A0~A7 */
for(i=0; i<10; i++);
*p = (col >> 8) & 0x0f; /* Column Address A8~A11 */
for(i=0; i<10; i++);
*p = page & 0xff; /* Row Address A12~A19 */
for(i=0; i<10; i++);
*p = (page >> 8) & 0xff; /* Row Address A20~A27 */
for(i=0; i<10; i++);
*p = (page >> 16) & 0x01; /* Row Address A28~A29 */
for(i=0; i<10; i++);
}
/* low level nand read function */
int nand_read_ll(unsigned char *buf, unsigned long start_addr, int size)
{
int i, j;
if ((start_addr & NAND_BLOCK_MASK) || (size & NAND_BLOCK_MASK))
{
return -1; //地址或长度不对齐
}
NAND_CHIP_ENABLE; //选中Nand片选
for(i = start_addr; i < (start_addr + size);)
{
//发出READ0指令
//NAND_CLEAR_RB;
NFCMD = 0;
s3c2440_write_addr_lp(i);
//2k page
NFCMD = 0x30;
NAND_DETECT_RB;
for(j=0; j < NAND_SECTOR_SIZE; j++, i++)
{
*buf = NFDATA;
buf++;
}
}
NAND_CHIP_DISABLE; //取消片选信号
return 0;
}
以上紫色部分对应于K9F2G08U0A的数据手册写操作顺序,如下图所示:
整一芯片 = 2048块
一个块(扇区) = 64 页
一页 = 2048 字节 + 64字节
5、然后,在board/samsung/smdk2440/Makefile中添加nand_read.c的编译选项,使他编译到u-boot中,如下:
COBJS := smdk2440.o flash.o nand_read.o
6、还有一个重要的地方要修改,在arch/arm/cpu/arm920t/u-boot.lds中,这个u-boot启动连接脚本文件决定了u-boot运行的入口地址,以及各个段的存储位置,这也是链接定位的作用。添加下面两行代码的主要目的是防止编译器把我们自己添加的用于nandboot的子函数放到4K之后,否则是无法启动的。如下:
.text :
{
arch/arm/cpu/arm920t/start.o (.text)
board/samsung/smdk2440/lowlevel_init.o (.text)
board/samsung/smdk2440/nand_read.o (.text)
*(.text)
}