s3c2440 nand flash 拷貝實(shí)驗(yàn)
首先明確一下我們的編程步驟。
(1)、加電在nand_flash加載boot.s中4K以內(nèi)的程序。這4k將自動拷貝到SRAM(片內(nèi)RAM)執(zhí)行。
(2)、我們需要用這4k的程序?qū)崿F(xiàn)nand-flash中4K以后的程序的拷貝(當(dāng)然,拷貝到SDRAM基址為0x30000000處)繼續(xù)執(zhí)行(main.o部分的程序)。對于SDRAM的初始化和Watchdog的禁用已經(jīng)在前一個實(shí)驗(yàn)中使用到了,這里就不再詳細(xì)敘述。主要來看一下nand-flash的初始化和使用。
查閱一下s3c2440的空間布局。查看手冊圖Figure 5-1. S3C2440A Memory Map after Reset一目了然。
有8個banks— Total 8 memory banksSix memory banks for ROM, SRAM, etc.Remaining two memory banks for ROM, SRAM, SDRAM, etc .
每個bank擁有128M空間。當(dāng)訪問bankx時,對應(yīng)的地址范圍是128*n 到 128*(1+n)tq2440使用了64M的nand flash和64M的SDROMNAND Flash不對應(yīng)任何bank,他是通過幾組寄存器來訪問的;上電以后,nand flash開始的4k數(shù)據(jù)被自動的復(fù)制到芯片內(nèi)部一個稱為steppingstone的RAM上。steppingstore的映射地址為0,上面的4k完成初始化工作;SDRAM則使用bank6,起始位置為0x30000000
該實(shí)驗(yàn)中我們將使用SDRAM的bank6實(shí)驗(yàn)?zāi)康模?/p>
(1)、mem controller的原理和工作過程
(2)、bank的使用
(3)、nand flash的讀寫控制
(4)、啟動代碼流程分析
在實(shí)際編程中,uboot和vivi都是絕佳的參考源碼,我這里參考的是vivi的代碼。
vivi已經(jīng)上傳到新浪共享:http://ishare.iask.sina.com.cn/f/11353581.html
datasheet上關(guān)于啟動原理的介紹:
Bank0:The data bus of BANK0 (nGCS0) should be configured with a width as one of 16-bit and 32-bit ones. Because theBANK0 works as the booting ROM bank (map to 0x0000_0000), the bus width of BANK0 should be determinedbefore the first ROM access, which will depend on the logic level of OM[1:0] at Reset.
下面小結(jié)一下對nand flash控制器的操作過程.
s3c2440對nandflash讀寫操作寄存器配置的流程:
s3c2440對nandflash讀寫操作寄存器配置的流程:
1.初始化
(1)NFCONT= (1<<0) //enable NAND flash controller
(2)NFCONT|= (1<<0)//chip disable
2.復(fù)位
(1)NFCONT&= ~(1<<1) //chip enable
(2)NFCMD= 0xff; //reset command
(3)while(!(NFSTAT& BUSY))等待NAND flashmemory ready to operate
3.讀函數(shù)
(1)NFCONT&= ~(1<<1)//chip enable
(2)NFSTAT|= (1<<2) //NAND_CLEAR_RB ,RnBtransition is detected
(3)NFCMD= 0; //READ0,讀上半葉
(4)//Write Address
NFADDR= i & 0xff;
NFADDR= (i >> 9) & 0xff;
NFADDR= (i >> 17) & 0xff;
NFADDR= (i >> 25) & 0xff;
(5)while(!(NFSTAT&(1<<0)) ); //NAND_DETECT_RB,等待NANDflash memory ready to operate
(6)*buf= (NFDATA & 0xff); //讀數(shù)據(jù)線
(7)NFCONT|= (1<<1)//chip disable
用到的nand flash初始化讀操作源碼:
1 /*在第一次實(shí)用NAND Flash前,復(fù)位一下NAND Flash */
2 void nand_flash_reset()
3 {
4 NAND_CHIP_ENABLE;
5 NFCMD = 0xff; //reset command
6 wait_idle();
7 }
8
9 /*初始化NAND Flash */
10 void nand_flash_init()
11 {
12 //vivi init
13 int i = 0;
14 NFCONF = ( (7<<12)|(7<<8)|(7<<4)|(0<<0) );
15 NFCONT = ( (1<<4)|(0<<1)|(1<<0) );// Active low CE Control
16 NFSTAT = (0x6);//RnB Clear
17 NFCMD = 0xff; //reset command
18 for(i = 0; i < 10; i++)
19 ;
20 wait_idle();
21 /*
22 //----------------------------------------------------------------
23 // following is the copy module
24 //----------------------------------------------------------------
25 NFCONT |= 0x2;//@ Flash Memory Chip Disable
26 //----------------------------------------------------------------
27 @ Flash Memory Chip Disable
28 @ get read to call C functions (for nand_read())
29 @ copy vivi to RAM
30 ldr r0, =VIVI_RAM_BASE
31 mov r1, #0x0
32 mov r2, #0x20000
33 bl nand_read_ll
34 //---------------------------------------------------------------
35 */
36 /*
37 NFCONT = (1<<0);
38 NAND_CHIP_DISABLE;
39 nand_flash_reset();
40 */
41 }
42
43 #define BUSY 1
44 inline void wait_idle(void)
45 {
46 while(!(NFSTAT & BUSY));
47 NFSTAT |= BUSY;
48 }
49
50 #define NAND_SECTOR_SIZE 512
51 #define NAND_BLOCK_MASK (NAND_SECTOR_SIZE - 1)
52
53 /* low level nand read function */
54 int nand_flash_read(unsigned char *buf, unsigned long start_addr, int size)
55 {
56 int i, j;
57
58 if ((start_addr & NAND_BLOCK_MASK) || (size & NAND_BLOCK_MASK)) {
59 return -1; /* invalid alignment */
60 }
61
62 NAND_CHIP_ENABLE;
63
64 for(i=start_addr; i < (start_addr + size);) {
65 /*debug*/
66 (*(volatile unsigned long *)0x56000010) = 0x00015400;
67 (*(volatile unsigned long *)0x56000014) = 0x00000000;
68 /*debug*/
69 /* READ0 */
70 NAND_CLEAR_RB;
71 NFCMD = 0;
72
73 /* Write Address */
74 NFADDR = i & 0xff;
75 NFADDR = (i >> 9) & 0xff;
76 NFADDR = (i >> 17) & 0xff;
77 NFADDR = (i >> 25) & 0xff;
78
79 NAND_DETECT_RB;
80
81 for(j=0; j < NAND_SECTOR_SIZE; j++, i++) {
82 *buf = (NFDATA & 0xff);
83 buf++;
84 }
85 /*debug*/
86 if(i >= 512)
87 {
88 for(j = 0; j < 2048; j++)
89 ;
90 (*(volatile unsigned long *)0x56000014) &= (1 << 5) & (1 << 6);
91 for(j = 0; j < 2048; j++)
92 ;
93 }
94 /*debug*/
95 }
96 NAND_CHIP_DISABLE;
97 return 0;
98 }
在sram執(zhí)行的啟動匯編代碼:
1 @----------------------------------------------------
2 @ boot.s
3 @ yeven @2010.20.28
4 @----------------------------------------------------
5 .text
6 .global _start
7 _start:
8 ldr sp,=4096
9 bl disable_wd @關(guān)閉看門狗
10 bl memsetup @初始化SDRAM
11 bl nand_flash_init @初始化nand-flash
12
13 @下面調(diào)用 nand_flash_read,它需要三個參數(shù):目標(biāo)地址,源地址,數(shù)據(jù)長度
14 ldr r0,=0x30000000 @SDRAM新的起始位置
15 mov r1,#4096 @main.o在nand-flash中的偏移,即數(shù)據(jù)起始位置
16 mov r2,#1024 @復(fù)制長度
17 bl nand_flash_read @調(diào)用vivi代碼中的拷貝函數(shù)
18
19
20 bl led_on_s
21 ldr pc, = set_sp @設(shè)置堆棧,進(jìn)入main.o執(zhí)行
22 set_sp:
23 ldr sp,=0x34000000 @設(shè)置堆棧棧頂指針
24 ldr lr,=halt_loop @設(shè)置主函數(shù)返回地址
25 ldr pc,=main @執(zhí)行主函數(shù)
26
27 halt_loop:
28 b halt_loop
29
30 led_on_s:
31 ldr r0,=0x56000010
32 mov r1,#0x00000400
33 str r1,[r0]
34 ldr r0,=0x56000014
35 mov r1,#0x00000000
36 str r1,[r0]
主函數(shù)執(zhí)行代碼,這一段將在sdram-0x30000000執(zhí)行.他只是不停的閃燈:
1 /*
2 * mem-con.c yeven @2010.10.27
3 * learn to use the sdram,control the memory and memory map
4 * the main program locate at boot.s
5 * we just light the four leds to test the result.
6 */
7
8 //Register for the led
9 #define GPBCON (*(volatile unsigned long *)0x56000010)
10 #define GPBDAT (*(volatile unsigned long *)0x56000014)
11
12 //led-data register value(GPB5-GPB8)
13 #define LED0_ON (1 << (5*2))
14 #define LED1_ON (1 << (6*2))
15 #define LED2_ON (1 << (7*2))
16 #define LED3_ON (1 << (8*2))
17 #define GPB_ON(n) (~(1 << n))
18 #define GPB_OFF(n) (1 << n)
19
20 void delayms(unsigned int n)
21 {
22 int i = 0;
23 for(i = 0; i < 10240*n; i++)
24 ;
25 }
26
27 int main()
28 {
29 GPBCON |= (LED0_ON | LED1_ON | LED2_ON | LED3_ON); //led0-4
30 wh