Source Code Analysis of SPL in u-boot

Source: Internet
Author: User
[Author: BoShen & lt; voiceshen @ gmailcom & gt; [u-boot: 201401-rc1] this article uses sama5d3xekSPL implementation as an example. For more information, see: https: githubcomvoiceshenu-boottreesama5d3xek_spl_spi _

[Author: Bo Shen

[U-boot: 2014.01-rc1]

This article will use sama5d3xek SPL implementation as an example, the specific code can be viewed: https://github.com/voiceshen/u-boot/tree/sama5d3xek_spl_spi_nand

U-boot SPL (second program loader) is also a stranger to many people. The following is a brief introduction.

1. ARM SoC startup process:

RomBoot --> SPL --> u-boot --> Linux kernel --> file system --> start application

(RomBoot is fixed in the SoC .)

U-boot implements a new function that can generate a SPL binary file while compiling u-boot.

2. run the SPL code go through

As you can see from the u-boot-spl.lds link file, the startup code is also start. S.

(Reset) (B lowlevel_init: arch/arm/cpu/armv7/lowlevel_init.S) (B _ main) --> (bl board_init_f) --> (board_init_r) --> (Jump_to_image_no_args to start u-boot) the SPL lifecycle ends.

To put it simply, the SPL initializes some hardware, reads u-boot, and finally calls it to u-boot.

3. The following is a detailed analysis of SPL code.

110 reset:111         bl      save_boot_params112         /*113          * disable interrupts (FIQ and IRQ), also set the cpu to SVC32 mode,114          * except if in HYP mode already115          */116         mrs     r0, cpsr117         and     r1, r0, #0x1f           @ mask mode bits118         teq     r1, #0x1a               @ test for HYP mode119         bicne   r0, r0, #0x1f           @ clear all mode bits120         orrne   r0, r0, #0x13           @ set SVC mode121         orr     r0, r0, #0xc0           @ disable FIQ and IRQ122         msr     cpsr,r0123124 /*125  * Setup vector:126  * (OMAP4 spl TEXT_BASE is not 32 byte aligned.127  * Continue to use ROM code vector only in OMAP4 spl)128  */129 #if !(defined(CONFIG_OMAP44XX) && defined(CONFIG_SPL_BUILD))130         /* Set V=0 in CP15 SCTRL register - for VBAR to point to vector */131         mrc     p15, 0, r0, c1, c0, 0   @ Read CP15 SCTRL Register132         bic     r0, #CR_V               @ V = 0133         mcr     p15, 0, r0, c1, c0, 0   @ Write CP15 SCTRL Register134135         /* Set vector address in CP15 VBAR register */136         ldr     r0, =_start137         mcr     p15, 0, r0, c12, c0, 0  @Set VBAR138 #endif139140         /* the mask ROM code should have PLL and others stable */141 #ifndef CONFIG_SKIP_LOWLEVEL_INIT142         bl      cpu_init_cp15143         bl      cpu_init_crit144 #endif145146         bl      _main
111: if save_boot_params is not redefined, use save_boot_params. It does not do anything and returns directly.

116 ~ 138: you can see the annotations.

141: because SPL is mainly used to initialize SoC, CONFIG_SKIP_LOWLEVE_INIT is not defined, that is, row 142,143 can be executed.

142: cpu_init_cpu15, mainly used for invalidate L1 I/D cache and disable MMU. check whether workaround is required.

143: cpu_init_crit jump directly to lowlevel_init

Let's take a look at the implementation of lowlevel_init:

18 ENTRY(lowlevel_init) 19         /* 20          * Setup a temporary stack 21          */      22         ldr     sp, =CONFIG_SYS_INIT_SP_ADDR 23         bic     sp, sp, #7 /* 8-byte alignment for ABI compliance */ 24 #ifdef CONFIG_SPL_BUILD                  25         ldr     r9, =gdata 26 #else 27         sub     sp, #GD_SIZE 28         bic     sp, sp, #7 29         mov     r9, sp 30 #endif 31         /* 32          * Save the old lr(passed in ip) and the current lr to stack 33          */ 34         push    {ip, lr} 35  36         /* 37          * go setup pll, mux, memory 38          */ 39         bl      s_init 40         pop     {ip, pc} 41 ENDPROC(lowlevel_init)
22: assign a value to stack pointer to CONFIG_SYS_INIT_SP_ADDR.

23: make sure that the sp is 8-byte aligned.

25: store the gdata address in the r9 register.

39: Jump to s_init. For the Atmel sama5d3xek board, s_init is defined in: this is not analyzed currently.

Return to start. S and call bl _ main

 58 ENTRY(_main) 59  60 /* 61  * Set up initial C runtime environment and call board_init_f(0). 62  */ 63  64 #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK) 65         ldr     sp, =(CONFIG_SPL_STACK) 66 #else 67         ldr     sp, =(CONFIG_SYS_INIT_SP_ADDR) 68 #endif 69         bic     sp, sp, #7      /* 8-byte alignment for ABI compliance */ 70         sub     sp, #GD_SIZE    /* allocate one GD above SP */ 71         bic     sp, sp, #7      /* 8-byte alignment for ABI compliance */ 72         mov     r9, sp          /* GD is above SP */ 73         mov     r0, #0 74         bl      board_init_f
65: re-assign SP values

69: make sure sp is 8-character aligned

70: The size of a global_data retained.

71: confirm that the updated sp is 8-character aligned.

72: r9 points to global_data

73: r0 value 0

74: Jump to board_init_f to run.

Board_init_f is defined in:

 20 /* 21  * In the context of SPL, board_init_f must ensure that any clocks/etc for 22  * DDR are enabled, ensure that the stack pointer is valid, clear the BSS 23  * and call board_init_f.  We provide this version by default but mark it 24  * as __weak to allow for platforms to do this in their own way if needed. 25  */ 26 void __weak board_init_f(ulong dummy) 27 { 28         /* Clear the BSS. */ 29         memset(__bss_start, 0, __bss_end - __bss_start); 30  31         /* Set global data pointer. */ 32         gd = &gdata; 33  34         board_init_r(NULL, 0); 35 }
26: board_init_f is a weak function that can be redefined.

29: clear BSS segments.

34: Jump to board_init_r

Board_init_r in Definition:

132 void board_init_r(gd_t *dummy1, ulong dummy2)133 {134         u32 boot_device;135         debug(">>spl:board_init_r()\n");136 137 #ifdef CONFIG_SYS_SPL_MALLOC_START138         mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,139                         CONFIG_SYS_SPL_MALLOC_SIZE);140 #endif141 142 #ifndef CONFIG_PPC143         /*144          * timer_init() does not exist on PPC systems. The timer is initialized145          * and enabled (decrementer) in interrupt_init() here.146          */147         timer_init();148 #endif149 150 #ifdef CONFIG_SPL_BOARD_INIT151         spl_board_init();152 #endif

135: output debug information:> spl: board_init_r ();

137 ~ 140: if CONFIG_SYS_SPL_MALLOC_START is defined, the memory malloc pool is initialized. Call malloc to allocate memory in the pool.

142 ~ 148: if CONFIG_PPC is not defined, timer initialization is performed: timer_init ()

150 ~ 150: CONFIG_SPL_BOARD_INIT, call spl_board_init (). This is the definition of the board,

After everything is ready, check the device from which to start. The RAM, MMC, and NAND code is posted here.

154         boot_device = spl_boot_device();155         debug("boot device - %d\n", boot_device);156         switch (boot_device) {157 #ifdef CONFIG_SPL_RAM_DEVICE158         case BOOT_DEVICE_RAM:159                 spl_ram_load_image();160                 break;161 #endif162 #ifdef CONFIG_SPL_MMC_SUPPORT163         case BOOT_DEVICE_MMC1:164         case BOOT_DEVICE_MMC2:165         case BOOT_DEVICE_MMC2_2:166                 spl_mmc_load_image();167                 break;168 #endif169 #ifdef CONFIG_SPL_NAND_SUPPORT170         case BOOT_DEVICE_NAND:171                 spl_nand_load_image();172                 break;173 #endif
154: Get the spl_boot_device, that is, the device from which the spl_boot_device is started.

157 ~ 161: if CONFIG_SPL_RAM_DEVICE is defined, run spl_ram_load_image () to download the image to ram.

162 ~ 168: if CONFIG_SPL_MMC_SUPPORT is defined, execute spl_mmc_load_image () to read the image from mmc/sd to ram.

169 ~ 173: if CONFIG_SPL_NAND_SUPPORT is defined, execute spl_nand_load_image () to read the image from nand flash to ram.

When the image to be started is in RAM, we can start it.

213         switch (spl_image.os) {214         case IH_OS_U_BOOT:215                 debug("Jumping to U-Boot\n");216                 break;217 #ifdef CONFIG_SPL_OS_BOOT218         case IH_OS_LINUX:219                 debug("Jumping to Linux\n");220                 spl_board_prepare_for_linux();221                 jump_to_image_linux((void *)CONFIG_SYS_SPL_ARGS_ADDR);222 #endif223         default:224                 debug("Unsupported OS image.. Jumping nevertheless..\n");225         }226         jump_to_image_no_args(&spl_image);227 }
213: determines the image type.

214: for u-boot, run u-boot directly to 227.

218: if it is Linux, go to 221 to start Linux.

So far, SPL ends its life and control is handed over to u-boot or Linux.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.