s3c6410_uboot中的代碼重定位(nand->sdram)

來源:互聯網
上載者:User

標籤:style   blog   color   io   strong   資料   for   art   

本文僅探討s3c6410從nand flash啟動u-boot時的代碼重定位過程

參考:

1)《USER‘S MANUAL-S3C6410X》第二章 MEMORY MAP 第八章 NAND FLASH CONTROLLER

2)u-boot源碼:

u-boot-x.x.x/board/samsumg/smdk6410/lowlevel_init.S

u-boot-x.x.x/cpu/s3c64xx/start.S

u-boot-x.x.x/cpu/s3c64xx/nand_cp.c

代碼重定位過程簡述

由於在nand flash中無法運行代碼,所以當開發板從nand flash啟動時,我們需要將儲存在外設nand flash中的u-boot代碼搬運到sdram中運行,如何完成這個搬運工作呢?這需要藉助一個跳板,即“stepping stone”,它是s3c6410的一塊內建sram,開發板上電時,nand flash控制器自動將nand flash的前8K的內容拷貝到sram中並執行,這一小段啟動代碼除了初始化硬體外,最重要的一個工作就是將nand flash中的所有u-boot代碼拷貝(即重定位)到sdram的指定地址上去,然後跳轉到sdram中執行。

重定位代碼解析:

1)nand介面初始化

u-boot啟動時,首先執行相應硬體平台的start.S,start.S中調用lowlevel_init對時鐘,uart,nand,mmu等底層硬體作初始化。

start.S:

...bl    lowlevel_init    /* go setup pll,mux,memory */...

lowlevel_init.S:

...
/* * Nand Interface Init for SMDK6400 */nand_asm_init: ldr r0, =ELFIN_NAND_BASE ldr r1, [r0, #NFCONF_OFFSET] orr r1, r1, #0x70 orr r1, r1, #0x7700 str r1, [r0, #NFCONF_OFFSET] ldr r1, [r0, #NFCONT_OFFSET] orr r1, r1, #0x03 str r1, [r0, #NFCONT_OFFSET] mov pc, lr
...

2)代碼重定位

從nand flash啟動時,重定位代碼如下:

start.S:

/* when we already run in ram, we don‘t need to relocate U-Boot.     * and actually, memory controller must be configured before U-Boot     * is running in ram.     */    ldr    r0, =0xff000fff    bic    r1, pc, r0        /* r0 <- current base addr of code */    ldr    r2, _TEXT_BASE        /* r1 <- original base addr in ram */    bic    r2, r2, r0        /* r0 <- current base addr of code */    cmp     r1, r2                  /* compare r0, r1                  */    beq     after_copy        /* r0 == r1 then skip flash copy   */#ifdef CONFIG_BOOT_NAND    mov    r0, #0x1000    bl    copy_from_nand#endif

r1存放當前代碼啟動並執行起始地址,r2存放u-boot即將在sdram中啟動並執行地址,如果兩個地址相等,說明此時u-boot已經在sdram中運行了,無需再執行從nand拷貝資料到sdram的動作;否則,此時u-boot還在它的臨時住所sram中執行,此地不可久留,需要執行copy_from_nand將u-boot代碼完完整整地拷貝到sdram中去,然後跳轉到sdram中去執行剩下的代碼。

/* * copy U-Boot to SDRAM and jump to ram (from NAND or OneNAND) * r0: size to be compared * Load 1‘st 2blocks to RAM because U-boot‘s size is larger than 1block(128k) size */    .globl copy_from_nandcopy_from_nand:    mov    r10, lr        /* save return address */    mov    r9, r0    /* get ready to call C functions */    ldr    sp, _TEXT_PHY_BASE    /* setup temp stack pointer */    sub    sp, sp, #12    mov    fp, #0            /* no previous frame, so fp=0 */    mov    r9, #0x1000    bl    copy_uboot_to_ram3:    tst     r0, #0x0    bne    copy_failed    ldr    r0, =0x0c000000    ldr    r1, _TEXT_PHY_BASE1:    ldr    r3, [r0], #4    ldr    r4, [r1], #4    teq    r3, r4    bne    compare_failed    /* not matched */    subs    r9, r9, #4    bne    1b4:    mov    lr, r10        /* all is OK */    mov    pc, lrcopy_failed:    nop            /* copy from nand failed */    b    copy_failedcompare_failed:    nop            /* compare failed */    b    compare_failed

真正執行拷貝動作的是copy_uboot_to_ram函數,它定義在u-boot-x.x.x/cpu/s3c64xx/nand_cp.c中,

int copy_uboot_to_ram (void){    int large_block = 0;    int i;    vu_char id;            NAND_ENABLE_CE();        NFCMD_REG = NAND_CMD_READID;        NFADDR_REG =  0x00;    /* wait for a while */        for (i=0; i<200; i++);    id = NFDATA8_REG;    id = NFDATA8_REG;    if (id > 0x80)        large_block = 1;    /* read NAND Block.     * 128KB ->240KB because of U-Boot size increase. by scsuh     * So, read 0x3c000 bytes not 0x20000(128KB).     */    return nandll_read_blocks(CFG_PHY_UBOOT_BASE, 0x3c000, large_block);}

nand flash支援兩種頁大小,512B和2KB,large_block = 0時,頁大小為512位元組,large_block = 1時,頁大小為2K位元組。nandll_read_blocks拷貝nand flash從第0頁開始的0x3c00(240K)大小的資料到sdram的CFG_PHY_UBOOT_BASE地址處。

/* * Read data from NAND. */static int nandll_read_blocks (ulong dst_addr, ulong size, int large_block){        uchar *buf = (uchar *)dst_addr;        int i;    uint page_shift = 9;    if (large_block)        page_shift = 11;        /* Read pages */        for (i = 0; i < (0x3c000>>page_shift); i++, buf+=(1<<page_shift)) {                nandll_read_page(buf, i, large_block);        }        return 0;}

首先根據large_block判斷nand flash一個頁的大小,從而計算需要拷貝的頁的數量,即需要拷貝(0x3c000>>page_shift)個頁,nandll_read_page每次只拷貝一個頁的資料。

/* * address format *              17 16         9 8            0 * -------------------------------------------- * | block(12bit) | page(5bit) | offset(9bit) | * -------------------------------------------- */static int nandll_read_page (uchar *buf, ulong addr, int large_block){        int i;    int page_size = 512;    if (large_block)        page_size = 2048;        NAND_ENABLE_CE();        NFCMD_REG = NAND_CMD_READ0;        /* Write Address */        NFADDR_REG = 0;    if (large_block)            NFADDR_REG = 0;    NFADDR_REG = (addr) & 0xff;    NFADDR_REG = (addr >> 8) & 0xff;    NFADDR_REG = (addr >> 16) & 0xff;    if (large_block)        NFCMD_REG = NAND_CMD_READSTART;        NF_TRANSRnB();    /* for compatibility(2460). u32 cannot be used. by scsuh */    for(i=0; i < page_size; i++) {                *buf++ = NFDATA8_REG;        }        NAND_DISABLE_CE();        return 0;}

從nand flash中讀取資料的流程為片選(NAND_ENABLE_CE)->發讀命令(NFCMD_REG)->發地址(NFADDR_REG)->發讀命令(NFCMD_REG)->等待資料可讀(NF_TRANSRnB)->讀資料(NFDATA8_REG)。由於每次從NFDATA8_REG中只可讀取1個位元組的資料,所以拷貝一頁需要讀取512或2048次。

 

當執行完copy_uboot_to_ram返回到start.S時,nand flash中的代碼重定位便完成了,此後程式跳轉到sdram中執行,stepping stone的職責就此結束。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.