S3C2440 支援兩種啟動模式:NAND Flash 與 NOR Flash。本篇先來搞定 NOR Flash 的啟動方式。FL2440 板上的 NOR Flash 是 Intel J3 系列、容量 4M bytes,這系列支援標準的 CFI 指令。在較新的 U-boot 版本裡,只要加上巨集定義就能支援 CFI 介面的 NOR Flash,但我們這個版本還沒那麼省事,得動手改。
先把 board/cmi/flash.c 複製到 board/fl2440/flash.c,裡頭就有我們開發板需要的 NOR Flash 讀寫函數,但仍有一處要修:函數 write_buff 必須把小端位元組做 short 型別轉換(低位元組放低位、高位元組放高位),還要處理位址對齊。因為這型 Flash 只支援 16 位元寫入、不支援 8 位元寫入,所以當我們要寫 8 位元時,得先把原資料讀出來、保留不改的部分,再與要寫入的 8 位元組成 16 位元後寫回。修改後的 write_buff 如下:
int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
{
ulong cp, wp;
ushort data;
int i, rc;
if (info->flash_id == FLASH_UNKNOWN) {
return 4;
}
wp = (addr & ~1); /* get lower word aligned address */
/*
* handle unaligned start byte
*/
/* for not to modify the origin data in flash*/
if (addr - wp) {
data = 0;
for(i = 0, cp = wp; i < 1; ++i, ++cp){
data = (data >> 8) | (*(uchar *)cp << 8);
}
for(; i < 2 && cnt > 0; ++i){
data = (data >> 8) | (*src++ <<8);
--cnt;
++cp;
}
for(; cnt == 0 && i < 2; ++i, ++cp){
data = (data >> 8) | (*(uchar *)cp << 8);
}
if ((rc = write_short(info, wp, data)) != 0) {
return (rc);
}
wp += 2;
}
/*
* handle word aligned part
*/
while (cnt >= 2) {
data = *((vu_short *)src) ;
if ((rc = write_short(info, wp, data)) != 0) {
return (rc);
}
src += 2;
wp += 2;
cnt -= 2;
}
if (cnt == 0) {
return (0);
}
/*
* handle unaligned tail bytes
* read the origin high byte data and write again!
* modified by yanghao
*/
data = 0;
for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
data = (data >> 8) | (*src++ << 8);
--cnt;
}
for (; i<2; ++i, ++cp) {
data = (data >> 8) | (*(uchar *)cp << 8);
}
return (write_short(info, wp, data));
}
此外,還要修改 flash.c 中的 NOR Flash 塊大小:
#define FLASH_BLOCK_SIZE 0x00020000 //數據手冊p8說了32Mbit有32個128kBytes block組成
把這兩處改好,NOR Flash 的讀寫就能正確配合這塊 Intel J3 系列 Flash 的 16 位元寫入特性了。