本文分析的是在BF533处理器上,以BFIN_BOOT_SPI_MASTER方式启动过程。供交流学习用,引用请注明出处。
uboot版本u-boot-2010.06-2010R1-RC2.tar.bz2来自http://blackfin.uclinux.org/gf/project/u-boot/。
有关BF533上启动文件的格式请参考EE240文档,启动过程中使用的是u-boot.ldr文件,内存初始化部分在/arch/blackfin/cpu/initcode.c中执行,
下面介绍的是进入_start后执行的过程。
/arch/blackfin/cpu/start.s 文件中的
/*
* U-boot - start.S Startup file for Blackfin u-boot
*
* Copyright (c) 2005-2008 Analog Devices Inc.
*
* This file is based on head.S
* Copyright (c) 2003 Metrowerks/Motorola
* Copyright (C) 1998 D. Jeff Dionne <jeff@ryeham.ee.ryerson.ca>,
* Kenneth Albanowski <kjahds@kjahds.com>,
* The Silver Hammer Group, Ltd.
* (c) 1995, Dionne & Associates
* (c) 1995, DKG Display Tech.
*
* See file CREDITS for list of people who contributed to this
* project.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include <config.h>
#include <asm/blackfin.h>
#include <asm/mach-common/bits/core.h>
#include <asm/mach-common/bits/dma.h>
#include <asm/mach-common/bits/pll.h>
#include "serial.h"
/* It may seem odd that we make calls to functions even though we haven't
* relocated ourselves yet out of {flash,ram,wherever}. This is OK because
* the "call" instruction in the Blackfin architecture is actually PC
* relative. So we can call functions all we want and not worry about them
* not being relocated yet.
*/
.text
ENTRY(_start)
/* Set our initial stack to L1 scratch space */
sp.l = LO(L1_SRAM_SCRATCH_END - 20);
sp.h = HI(L1_SRAM_SCRATCH_END - 20);
#ifdef CONFIG_HW_WATCHDOG
# ifndef CONFIG_HW_WATCHDOG_TIMEOUT_START
# define CONFIG_HW_WATCHDOG_TIMEOUT_START 5000
# endif
/* Program the watchdog with an initial timeout of ~5 seconds.
* That should be long enough to bootstrap ourselves up and
* then the common u-boot code can take over.
*/
P0.L = LO(WDOG_CNT);
P0.H = HI(WDOG_CNT);
R0.L = 0;
R0.H = HI(MSEC_TO_SCLK(CONFIG_HW_WATCHDOG_TIMEOUT_START));
[P0] = R0;
/* fire up the watchdog - R0.L above needs to be 0x0000 */
W[P0 + (WDOG_CTL - WDOG_CNT)] = R0;
#endif
/* Turn on the serial for debugging the init process */
serial_early_init
serial_early_set_baud
serial_early_puts("Init Registers");
/* Disable self-nested interrupts and enable CYCLES for udelay() */
R0 = CCEN | 0x30;
SYSCFG = R0;
/* Zero out registers required by Blackfin ABI.
* http://docs.blackfin.uclinux.org/doku.php?id=application_binary_interface
*/
r1 = 0 (x);
/* Disable circular buffers */
l0 = r1;
l1 = r1;
l2 = r1;
l3 = r1;
/* Disable hardware loops in case we were started by 'go' */
lc0 = r1;
lc1 = r1;
/* Save RETX so we can pass it while booting Linux */
r7 = RETX;
#if CONFIG_MEM_SIZE
/* Figure out where we are currently executing so that we can decide
* how to best reprogram and relocate things. We'll pass below:
* R4: load address of _start
* R5: current (not load) address of _start
*/
serial_early_puts("Find ourselves");
call _get_pc;
.Loffset:
r1.l = .Loffset;
r1.h = .Loffset;
r4.l = _start;
r4.h = _start;
r3 = r1 - r4;
r5 = r0 - r3;
/* Inform upper layers if we had to do the relocation ourselves.
* This allows us to detect whether we were loaded by 'go 0x1000'
* or by the bootrom from an LDR. "R6" is "loaded_from_ldr".
*/
r6 = 1 (x);
cc = r4 == r5;
if cc jump .Lnorelocate; //not return forever
r6 = 0 (x);
/* In bypass mode, we don't have an LDR with an init block
* so we need to explicitly call it ourselves. This will
* reprogram our clocks, memory, and setup our async banks.
*/
serial_early_puts("Program Clocks");
/* if we're executing >=0x20000000, then we dont need to dma */
r3 = 0x0;
r3.h = 0x2000;
cc = r5 < r3 (iu);
if cc jump .Ldma_and_reprogram;
#else
r6 = 1 (x); /* fake loaded_from_ldr = 1 */
#endif
r0 = 0 (x); /* set bootstruct to NULL */
call _initcode;
jump .Lprogrammed;
/* we're sitting in external memory, so dma into L1 and reprogram */
.Ldma_and_reprogram:
r0.l = LO(L1_INST_SRAM);
r0.h = HI(L1_INST_SRAM);
r1.l = __initcode_lma;
r1.h = __initcode_lma;
r2.l = __initcode_len;
r2.h = __initcode_len;
r1 = r1 - r4; /* convert r1 from load address of initcode ... */
r1 = r1 + r5; /* ... to current (not load) address of initcode */
p3 = r0;
call _dma_memcpy_nocache;
r0 = 0 (x); /* set bootstruct to NULL */
call (p3);
/* Since we reprogrammed SCLK, we need to update the serial divisor */
.Lprogrammed:
serial_early_set_baud
#if CONFIG_MEM_SIZE
/* Relocate from wherever we are (FLASH/RAM/etc...) to the hardcoded
* monitor location in the end of RAM. We know that memcpy() only
* uses registers, so it is safe to call here. Note that this only
* copies to external memory ... we do not start executing out of
* it yet (see "lower to 15" below).
*/
serial_early_puts("Relocate");
r0 = r4;
r1 = r5;
r2.l = LO(CONFIG_SYS_MONITOR_LEN);
r2.h = HI(CONFIG_SYS_MONITOR_LEN);
call _memcpy_ASM;
#endif
/* Initialize BSS section ... we know that memset() does not
* use the BSS, so it is safe to call here. The bootrom LDR
* takes care of clearing things for us.
*/
serial_early_puts("Zero BSS");
r0.l = __bss_vma;
r0.h = __bss_vma;
r1 = 0 (x);
r2.l = __bss_len;
r2.h = __bss_len;
call _memset;
.Lnorelocate:
/* Setup the actual stack in external memory */
sp.h = HI(CONFIG_STACKBASE);
sp.l = LO(CONFIG_STACKBASE);
fp = sp;
/* Now lower ourselves from the highest interrupt level to
* the lowest. We do this by masking all interrupts but 15,
* setting the 15 handler to ".Lenable_nested", raising the 15
* interrupt, and then returning from the highest interrupt
* level to the dummy "jump" until the interrupt controller
* services the pending 15 interrupt. If executing out of
* flash, these steps also changes the code flow from flash
* to external memory.
*/
serial_early_puts("Lower to 15");
r0 = r7;
r1 = r6;
p0.l = LO(EVT15);
p0.h = HI(EVT15);
p1.l = .Lenable_nested;
p1.h = .Lenable_nested;
[p0] = p1;
r7 = EVT_IVG15 (z);
sti r7;
raise 15;
p4.l = .LWAIT_HERE;
p4.h = .LWAIT_HERE;
reti = p4;
rti;
/* Enable nested interrupts before continuing with cpu init */
.Lenable_nested:
cli r7;
[--sp] = reti;
jump.l _cpu_init_f;
.LWAIT_HERE:
jump .LWAIT_HERE;
ENDPROC(_start)
LENTRY(_get_pc)
r0 = rets;
#if ANOMALY_05000371
NOP;
NOP;
NOP;
#endif
rts;
ENDPROC(_get_pc)
从_start处开始解释
⑴ 在启动的第一阶段,先在L1_SRAM_SCRATCH_END上安装堆栈,以便进行函数调用操作。
⑵ 调用serial_early_init, serial_early_set_baud。然后打印信息,调试时使用 serial_early_puts("Init Registers")。
⑶ 配置系统配置寄存器SYSCFG = 0x32,配置该值的作用参考bf533_hwr_Rev3.4.pdf第164页有关该寄存器的描述。
⑷ 对 L0 ~ L3,LC0、LC1清零处理,r7 = RETX并将异常返回地址寄存器的值赋给R7。
⑸ 调用_get_pc子程序,子程序的作用r0 = rets将子程序返回地址寄存器的值赋给R0,然后调用RTS指令从子程序中返回。
⑹ 上两步的作用是为了下面计算代码在何处执行。
.Loffset:
r1.l = .Loffset;
r1.h = .Loffset;
r4.l = _start;
r4.h = _start;
r3 = r1 - r4;
r5 = r0 - r3;
⑺ 然后就跳转到Lnorelocate处开始执行。
r6 = 1 (x);
cc = r4 == r5;
if cc jump .Lnorelocate;
⑻ 到这里,blackfin开发人员使用了一个技巧,他们使用中断进行u-boot第二阶段的任务,先屏蔽除EVT15外的所有中断,然后使能EVT15中断,接着
使用raise指令强制产生EVT15中断,然后在中断处理中进行u-boot第二阶段的任务。
sp.h = HI(CONFIG_STACKBASE);
sp.l = LO(CONFIG_STACKBASE);
fp = sp;
serial_early_puts("Lower to 15");
r0 = r7;
r1 = r6;
p0.l = LO(EVT15);
p0.h = HI(EVT15);
p1.l = .Lenable_nested;
p1.h = .Lenable_nested;
[p0] = p1;
r7 = EVT_IVG15 (z);
sti r7;
raise 15;
p4.l = .LWAIT_HERE;
p4.h = .LWAIT_HERE;
reti = p4;
rti;
.Lenable_nested:
cli r7;
[--sp] = reti;
jump.l _cpu_init_f;
.LWAIT_HERE:
jump .LWAIT_HERE;
⑼ 该条语句jump.l _cpu_init_f将执行/arch/blackfin/cpu/cpu.c文件中的 void cpu_init_f(ulong bootflag, ulong loaded_from_ldr)
void cpu_init_f(ulong bootflag, ulong loaded_from_ldr)
{
#ifndef CONFIG_BFIN_BOOTROM_USES_EVT1
/* Build a NOP slide over the LDR jump block. Whee! */
char nops[0xC];
serial_early_puts("NOP Slide\n");
memset(nops, 0x00, sizeof(nops));
memcpy((void *)L1_INST_SRAM, nops, sizeof(nops));
#endif
if (!loaded_from_ldr) {
/* Relocate sections into L1 if the LDR didn't do it -- don't
* check length because the linker script does the size
* checking at build time.
*/
serial_early_puts("L1 Relocate\n");
extern char _stext_l1[], _text_l1_lma[], _text_l1_len[];
memcpy(&_stext_l1, &_text_l1_lma, (unsigned long)_text_l1_len);
extern char _sdata_l1[], _data_l1_lma[], _data_l1_len[];
memcpy(&_sdata_l1, &_data_l1_lma, (unsigned long)_data_l1_len);
}
#if defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
/* The BF537 bootrom will reset the EBIU_AMGCTL register on us
* after it has finished loading the LDR. So configure it again.
*/
else
bfin_write_EBIU_AMGCTL(CONFIG_EBIU_AMGCTL_VAL);
#endif
/* Save RETX so we can pass it while booting Linux */
bfin_poweron_retx = bootflag;
#ifdef CONFIG_DEBUG_DUMP
/* Turn on hardware trace buffer */
bfin_write_TBUFCTL(TBUFPWR | TBUFEN);
#endif
#ifndef CONFIG_PANIC_HANG
/* Reset upon a double exception rather than just hanging.
* Do not do bfin_read on SWRST as that will reset status bits.
*/
bfin_write_SWRST(DOUBLE_FAULT);
#endif
serial_early_puts("Board init flash\n");
board_init_f(bootflag);
}
仅执行红色部分代码,主要是调用board_init_f(bootflag),该函数开始进行板级设备初始化。
⑽ 该函数board_init_f(bootflag)位于文件/arch/blackfin/lib/board.c中,
void board_init_f(ulong bootflag)
{
ulong addr;
bd_t *bd;
char buf[32];
#ifdef CONFIG_BOARD_EARLY_INIT_F
serial_early_puts("Board early init flash\n");
board_early_init_f();
#endif
serial_early_puts("Init CPLB tables\n");
init_cplbtables();
serial_early_puts("Exceptions setup\n");
exception_init();
#ifndef CONFIG_ICACHE_OFF
serial_early_puts("Turn on ICACHE\n");
icache_enable();
#endif
#ifndef CONFIG_DCACHE_OFF
serial_early_puts("Turn on DCACHE\n");
dcache_enable();
#endif
#ifdef CONFIG_WATCHDOG
serial_early_puts("Setting up external watchdog\n");
watchdog_init();
#endif
#ifdef DEBUG
if (CONFIG_SYS_GBL_DATA_SIZE < sizeof(*gd))
hang();
#endif
serial_early_puts("Init global data\n");
gd = (gd_t *) (CONFIG_SYS_GBL_DATA_ADDR);
memset((void *)gd, 0, CONFIG_SYS_GBL_DATA_SIZE);
/* Board data initialization */
addr = (CONFIG_SYS_GBL_DATA_ADDR + sizeof(gd_t));
/* Align to 4 byte boundary */
addr &= ~(4 - 1);
bd = (bd_t *) addr;
gd->bd = bd;
memset((void *)bd, 0, sizeof(bd_t));
bd->bi_r_version = version_string;
bd->bi_cpu = MK_STR(CONFIG_BFIN_CPU);
bd->bi_board_name = BFIN_BOARD_NAME;
bd->bi_vco = get_vco();
bd->bi_cclk = get_cclk();
bd->bi_sclk = get_sclk();
bd->bi_memstart = CONFIG_SYS_SDRAM_BASE;
bd->bi_memsize = CONFIG_SYS_MAX_RAM_SIZE;
/* Initialize */
serial_early_puts("IRQ init\n");
irq_init();
serial_early_puts("Environment init\n");
env_init();
serial_early_puts("Baudrate init\n");
init_baudrate();
serial_early_puts("Serial init\n");
serial_init();
serial_early_puts("Console init flash\n");
console_init_f();
serial_early_puts("End of early debugging\n");
display_banner();
checkboard();
timer_init();
printf("Clock: VCO: %s MHz, ", strmhz(buf, get_vco()));
printf("Core: %s MHz, ", strmhz(buf, get_cclk()));
printf("System: %s MHz\n", strmhz(buf, get_sclk()));
printf("RAM: ");
print_size(bd->bi_memsize, "\n");
#if defined(CONFIG_POST)
post_init_f();
post_bootmode_init();
post_run(NULL, POST_ROM | post_bootmode_get(0));
#endif
board_init_r((gd_t *) gd, 0x20000010);
}
⑾ 执行过程中主要执行board_init_r((gd_t *) gd, 0x20000010)。这个函数完成了系统相关的设置.
void board_init_r(gd_t * id, ulong dest_addr)
{
char *s;
bd_t *bd;
gd = id;
gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
bd = gd->bd;
#if defined(CONFIG_POST)
post_output_backlog();
post_reloc();
#endif
/* initialize malloc() area */
mem_malloc_init(CONFIG_SYS_MALLOC_BASE, CONFIG_SYS_MALLOC_LEN);
#if !defined(CONFIG_SYS_NO_FLASH)
/* Initialize the flash and protect u-boot by default */
extern flash_info_t flash_info[];
puts("Flash: ");
ulong size = flash_init();
print_size(size, "\n");
flash_protect(FLAG_PROTECT_SET, CONFIG_SYS_FLASH_BASE,
CONFIG_SYS_FLASH_BASE + CONFIG_SYS_MONITOR_LEN - 1,
&flash_info[0]);
bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
bd->bi_flashsize = size;
bd->bi_flashoffset = 0;
#else
bd->bi_flashstart = 0;
bd->bi_flashsize = 0;
bd->bi_flashoffset = 0;
#endif
#ifdef CONFIG_CMD_NAND
puts("NAND: ");
nand_init(); /* go init the NAND */
#endif
#ifdef CONFIG_GENERIC_MMC
puts("MMC: ");
mmc_initialize(bd);
#endif
/* relocate environment function pointers etc. */
env_relocate();
/* Initialize stdio devices */
stdio_init();
jumptable_init();
/* Initialize the console (after the relocation and devices init) */
console_init_r();
#ifdef CONFIG_CMD_KGDB
puts("KGDB: ");
kgdb_init();
#endif
#ifdef CONFIG_STATUS_LED
status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
status_led_set(STATUS_LED_CRASH, STATUS_LED_OFF);
#endif
/* Initialize from environment */
if ((s = getenv("loadaddr")) != NULL)
load_addr = simple_strtoul(s, NULL, 16);
#if defined(CONFIG_MISC_INIT_R)
/* miscellaneous platform dependent initialisations */
misc_init_r();
#endif
board_net_init_r(bd);
display_global_data();
#if defined(CONFIG_POST)
if (post_flag)
post_run(NULL, POST_RAM | post_bootmode_get(0));
#endif
if (bfin_os_log_check()) {
puts("\nLog buffer from operating system:\n");
bfin_os_log_dump();
puts("\n");
}
/* main_loop() can return to retry autoboot, if so just run it again. */
for (;;)
main_loop();
}