diff mbox

[U-Boot,v2,17/17] x86: Convert board_init_f_r to a processing loop

Message ID 1325707195-3218-17-git-send-email-graeme.russ@gmail.com
State Awaiting Upstream
Delegated to: Graeme Russ
Headers show

Commit Message

Graeme Russ Jan. 4, 2012, 7:59 p.m. UTC
Create an init function array for board_init_f_r - This finalises the
migration to a purely array based initialisation mechanism

Also tweak a few comments while we are at it so everything is 'correct'

Signed-off-by: Graeme Russ <graeme.russ@gmail.com>
---
Changes for v2:
 - Renamed to a more apt name
 - Fix bug in set_reloc_flag_r
 - Re-instate gd->flags = boot_flags; in board_init_f
 - Added commit message

 arch/x86/include/asm/init_helpers.h |    5 +
 arch/x86/include/asm/relocate.h     |   33 ++++++
 arch/x86/lib/board.c                |  186 +++++++++++++++++------------------
 arch/x86/lib/init_helpers.c         |   75 ++++++++++++++
 arch/x86/lib/relocate.c             |   32 +-----
 5 files changed, 206 insertions(+), 125 deletions(-)
 create mode 100644 arch/x86/include/asm/relocate.h

--
1.7.5.2.317.g391b14

Comments

Simon Glass Jan. 7, 2012, 10:21 p.m. UTC | #1
Hi Graeme,

On Wed, Jan 4, 2012 at 11:59 AM, Graeme Russ <graeme.russ@gmail.com> wrote:
> Create an init function array for board_init_f_r - This finalises the
> migration to a purely array based initialisation mechanism
>
> Also tweak a few comments while we are at it so everything is 'correct'
>
> Signed-off-by: Graeme Russ <graeme.russ@gmail.com>

I have been through this series again and it looks good to me. I don't
suppose an Acked-by from me is much use since you are the x86
maintainer and I admit to no knowledge of the subject, but let me know
if so.

There may be some conflict between your series and the generic board
one that I just sent out yesterday but I have not tried it. But now
that series is opt-in on a board-by-board basis, we might be ok, and
in any case x86 can just opt out until everything works.

Regards,
Simon

> ---
> Changes for v2:
>  - Renamed to a more apt name
>  - Fix bug in set_reloc_flag_r
>  - Re-instate gd->flags = boot_flags; in board_init_f
>  - Added commit message
>
>  arch/x86/include/asm/init_helpers.h |    5 +
>  arch/x86/include/asm/relocate.h     |   33 ++++++
>  arch/x86/lib/board.c                |  186 +++++++++++++++++------------------
>  arch/x86/lib/init_helpers.c         |   75 ++++++++++++++
>  arch/x86/lib/relocate.c             |   32 +-----
>  5 files changed, 206 insertions(+), 125 deletions(-)
>  create mode 100644 arch/x86/include/asm/relocate.h
>
> diff --git a/arch/x86/include/asm/init_helpers.h b/arch/x86/include/asm/init_helpers.h
> index 14ef11a..192f18e 100644
> --- a/arch/x86/include/asm/init_helpers.h
> +++ b/arch/x86/include/asm/init_helpers.h
> @@ -27,7 +27,12 @@
>  int display_banner(void);
>  int display_dram_config(void);
>  int init_baudrate_f(void);
> +int calculate_relocation_address(void);
>
> +int copy_gd_to_ram_f_r(void);
> +int init_cache_f_r(void);
> +
> +int set_reloc_flag_r(void);
>  int mem_malloc_init_r(void);
>  int init_bd_struct_r(void);
>  int flash_init_r(void);
> diff --git a/arch/x86/include/asm/relocate.h b/arch/x86/include/asm/relocate.h
> new file mode 100644
> index 0000000..33129ef
> --- /dev/null
> +++ b/arch/x86/include/asm/relocate.h
> @@ -0,0 +1,33 @@
> +/*
> + * (C) Copyright 2011
> + * Graeme Russ, <graeme.russ@gmail.com>
> + *
> + * 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., 59 Temple Place, Suite 330, Boston,
> + * MA 02111-1307 USA
> + */
> +
> +#ifndef _RELOCATE_H_
> +#define _RELOCATE_H_
> +
> +#include <common.h>
> +
> +int copy_uboot_to_ram(void);
> +int clear_bss(void);
> +int do_elf_reloc_fixups(void);
> +
> +#endif /* !_RELOCATE_H_ */
> diff --git a/arch/x86/lib/board.c b/arch/x86/lib/board.c
> index 89721c7..5f0b62c 100644
> --- a/arch/x86/lib/board.c
> +++ b/arch/x86/lib/board.c
> @@ -35,7 +35,7 @@
>  #include <watchdog.h>
>  #include <stdio_dev.h>
>  #include <asm/u-boot-x86.h>
> -#include <asm/processor.h>
> +#include <asm/relocate.h>
>
>  #include <asm/init_helpers.h>
>  #include <asm/init_wrappers.h>
> @@ -43,31 +43,58 @@
>  /*
>  * Breath some life into the board...
>  *
> - * Initialize an SMC for serial comms, and carry out some hardware
> - * tests.
> + * Getting the board up and running is a three-stage process:
> + *  1) Execute from Flash, SDRAM Uninitialised
> + *     At this point, there is a limited amount of non-SDRAM memory
> + *     (typically the CPU cache, but can also be SRAM or even a buffer of
> + *     of some peripheral). This limited memory is used to hold:
> + *      - The initial copy of the Global Data Structure
> + *      - A temporary stack
> + *      - A temporary x86 Global Descriptor Table
> + *      - The pre-console buffer (if enabled)
>  *
> - * The first part of initialization is running from Flash memory;
> - * its main purpose is to initialize the RAM so that we
> - * can relocate the monitor code to RAM.
> + *     The following is performed during this phase of execution:
> + *      - Core low-level CPU initialisation
> + *      - Console initialisation
> + *      - SDRAM initialisation
> + *
> + *  2) Execute from Flash, SDRAM Initialised
> + *     At this point we copy Global Data from the initial non-SDRAM
> + *     memory and set up the permanent stack in SDRAM. The CPU cache is no
> + *     longer being used as temporary memory, so we can now fully enable
> + *     it.
> + *
> + *     The following is performed during this phase of execution:
> + *      - Create final stack in SDRAM
> + *      - Copy Global Data from temporary memory to SDRAM
> + *      - Enabling of CPU cache(s),
> + *      - Copying of U-Boot code and data from Flash to RAM
> + *      - Clearing of the BSS
> + *      - ELF relocation adjustments
> + *
> + *  3) Execute from SDRAM
> + *     The following is performed during this phase of execution:
> + *      - All remaining initialisation
>  */
>
>  /*
> - * All attempts to come up with a "common" initialization sequence
> - * that works for all boards and architectures failed: some of the
> - * requirements are just _too_ different. To get rid of the resulting
> - * mess of board dependend #ifdef'ed code we now make the whole
> - * initialization sequence configurable to the user.
> - *
> - * The requirements for any new initalization function is simple: it
> - * receives a pointer to the "global data" structure as it's only
> - * argument, and returns an integer return code, where 0 means
> - * "continue" and != 0 means "fatal error, hang the system".
> + * The requirements for any new initalization function is simple: it is
> + * a function with no parameters which returns an integer return code,
> + * where 0 means "continue" and != 0 means "fatal error, hang the system"
>  */
>  typedef int (init_fnc_t) (void);
>
> -static int calculate_relocation_address(void);
> -static int copy_gd_to_ram(void);
> -
> +/*
> + * init_sequence_f is the list of init functions which are run when U-Boot
> + * is executing from Flash with a limited 'C' environment. The following
> + * limitations must be considered when implementing an '_f' function:
> + *  - 'static' variables are read-only
> + *  - Global Data (gd->xxx) is read/write
> + *  - Stack space is limited
> + *
> + * The '_f' sequence must, as a minimum, initialise SDRAM. It _should_
> + * also initialise the console (to provide early debug output)
> + */
>  init_fnc_t *init_sequence_f[] = {
>        cpu_init_f,
>        board_early_init_f,
> @@ -81,7 +108,39 @@ init_fnc_t *init_sequence_f[] = {
>        NULL,
>  };
>
> +/*
> + * init_sequence_f_r is the list of init functions which are run when
> + * U-Boot is executing from Flash with a semi-limited 'C' environment.
> + * The following limitations must be considered when implementing an
> + * '_f_r' function:
> + *  - 'static' variables are read-only
> + *  - Global Data (gd->xxx) is read/write
> + *
> + * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
> + * supported).  It _should_, if possible, copy global data to RAM and
> + * initialise the CPU caches (to speed up the relocation process)
> + */
> +init_fnc_t *init_sequence_f_r[] = {
> +       copy_gd_to_ram_f_r,
> +       init_cache_f_r,
> +       copy_uboot_to_ram,
> +       clear_bss,
> +       do_elf_reloc_fixups,
> +
> +       NULL,
> +};
> +
> +/*
> + * init_sequence_r is the list of init functions which are run when U-Boot
> + * is executing from RAM with a full 'C' environment. There are no longer
> + * any limitations which must be considered when implementing an '_r'
> + * function, (i.e.'static' variables are read/write)
> + *
> + * If not already done, the '_r' sequence must copy global data to RAM and
> + * (should) initialise the CPU caches.
> + */
>  init_fnc_t *init_sequence_r[] = {
> +       set_reloc_flag_r,
>        init_bd_struct_r,
>        mem_malloc_init_r,
>        cpu_init_r,
> @@ -157,43 +216,6 @@ static void do_init_loop(init_fnc_t **init_fnc_ptr)
>        }
>  }
>
> -static int calculate_relocation_address(void)
> -{
> -       ulong text_start = (ulong)&__text_start;
> -       ulong bss_end = (ulong)&__bss_end;
> -       ulong dest_addr;
> -
> -       /*
> -        * NOTE: All destination address are rounded down to 16-byte
> -        *       boundary to satisfy various worst-case alignment
> -        *       requirements
> -        */
> -
> -       /* Global Data is at top of available memory */
> -       dest_addr = gd->ram_size;
> -       dest_addr -= GENERATED_GBL_DATA_SIZE;
> -       dest_addr &= ~15;
> -       gd->new_gd_addr = dest_addr;
> -
> -       /* GDT is below Global Data */
> -       dest_addr -= X86_GDT_SIZE;
> -       dest_addr &= ~15;
> -       gd->gdt_addr = dest_addr;
> -
> -       /* Stack is below GDT */
> -       gd->start_addr_sp = dest_addr;
> -
> -       /* U-Boot is below the stack */
> -       dest_addr -= CONFIG_SYS_STACK_SIZE;
> -       dest_addr -= (bss_end - text_start);
> -       dest_addr &= ~15;
> -       gd->relocaddr = dest_addr;
> -       gd->reloc_off = (dest_addr - text_start);
> -
> -       return 0;
> -}
> -
> -/* Perform all steps necessary to get RAM initialised ready for relocation */
>  void board_init_f(ulong boot_flags)
>  {
>        gd->flags = boot_flags;
> @@ -201,10 +223,9 @@ void board_init_f(ulong boot_flags)
>        do_init_loop(init_sequence_f);
>
>        /*
> -        * SDRAM is now initialised, U-Boot has been copied into SDRAM,
> -        * the BSS has been cleared etc. The final stack can now be setup
> -        * in SDRAM. Code execution will continue (momentarily) in Flash,
> -        * but with the stack in SDRAM and Global Data in temporary memory
> +        * SDRAM and console are now initialised. The final stack can now
> +        * be setup in SDRAM. Code execution will continue in Flash, but
> +        * with the stack in SDRAM and Global Data in temporary memory
>         * (CPU cache)
>         */
>        board_init_f_r_trampoline(gd->start_addr_sp);
> @@ -216,51 +237,22 @@ void board_init_f(ulong boot_flags)
>
>  void board_init_f_r(void)
>  {
> -       if (copy_gd_to_ram() != 0)
> -               hang();
> -
> -       if (init_cache() != 0)
> -               hang();
> -
> -       relocate_code(0, gd, 0);
> -
> -       /* NOTREACHED - relocate_code() does not return */
> -       while (1)
> -               ;
> -}
> -
> -static int copy_gd_to_ram(void)
> -{
> -       gd_t *ram_gd;
> +       do_init_loop(init_sequence_f_r);
>
>        /*
> -        * Global data is still in temporary memory (the CPU cache).
> -        * calculate_relocation_address() has set gd->new_gd_addr to
> -        * where the global data lives in RAM but getting it there
> -        * safely is a bit tricky due to the 'F-Segment Hack' that
> -        * we need to use for x86
> +        * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
> +        * Transfer execution from Flash to RAM by calculating the address
> +        * of the in-RAM copy of board_init_r() and calling it
>         */
> -       ram_gd = (gd_t *)gd->new_gd_addr;
> -       memcpy((void *)ram_gd, gd, sizeof(gd_t));
> +       (board_init_r + gd->reloc_off)(gd, gd->relocaddr);
>
> -       /*
> -        * Reload the Global Descriptor Table so FS points to the
> -        * in-RAM copy of Global Data (calculate_relocation_address()
> -        * has already calculated the in-RAM location of the GDT)
> -        */
> -       ram_gd->gd_addr = (ulong)ram_gd;
> -       init_gd(ram_gd, (u64 *)gd->gdt_addr);
> -
> -       return 0;
> +       /* NOTREACHED - board_init_r() does not return */
> +       while (1)
> +               ;
>  }
>
>  void board_init_r(gd_t *id, ulong dest_addr)
>  {
> -       gd->flags |= GD_FLG_RELOC;
> -
> -       /* compiler optimization barrier needed for GCC >= 3.4 */
> -       __asm__ __volatile__("" : : : "memory");
> -
>        do_init_loop(init_sequence_r);
>
>        /* main_loop() can return to retry autoboot, if so just run it again. */
> diff --git a/arch/x86/lib/init_helpers.c b/arch/x86/lib/init_helpers.c
> index 547b180..9f4dee0 100644
> --- a/arch/x86/lib/init_helpers.c
> +++ b/arch/x86/lib/init_helpers.c
> @@ -29,6 +29,7 @@
>  #include <ide.h>
>  #include <serial.h>
>  #include <status_led.h>
> +#include <asm/processor.h>
>  #include <asm/u-boot-x86.h>
>
>  #include <asm/init_helpers.h>
> @@ -70,6 +71,80 @@ int init_baudrate_f(void)
>        return 0;
>  }
>
> +int calculate_relocation_address(void)
> +{
> +       ulong text_start = (ulong)&__text_start;
> +       ulong bss_end = (ulong)&__bss_end;
> +       ulong dest_addr;
> +
> +       /*
> +        * NOTE: All destination address are rounded down to 16-byte
> +        *       boundary to satisfy various worst-case alignment
> +        *       requirements
> +        */
> +
> +       /* Global Data is at top of available memory */
> +       dest_addr = gd->ram_size;
> +       dest_addr -= GENERATED_GBL_DATA_SIZE;
> +       dest_addr &= ~15;
> +       gd->new_gd_addr = dest_addr;
> +
> +       /* GDT is below Global Data */
> +       dest_addr -= X86_GDT_SIZE;
> +       dest_addr &= ~15;
> +       gd->gdt_addr = dest_addr;
> +
> +       /* Stack is below GDT */
> +       gd->start_addr_sp = dest_addr;
> +
> +       /* U-Boot is below the stack */
> +       dest_addr -= CONFIG_SYS_STACK_SIZE;
> +       dest_addr -= (bss_end - text_start);
> +       dest_addr &= ~15;
> +       gd->relocaddr = dest_addr;
> +       gd->reloc_off = (dest_addr - text_start);
> +
> +       return 0;
> +}
> +
> +int copy_gd_to_ram_f_r(void)
> +{
> +       gd_t *ram_gd;
> +
> +       /*
> +        * Global data is still in temporary memory (the CPU cache).
> +        * calculate_relocation_address() has set gd->new_gd_addr to
> +        * where the global data lives in RAM but getting it there
> +        * safely is a bit tricky due to the 'F-Segment Hack' that
> +        * we need to use for x86
> +        */
> +       ram_gd = (gd_t *)gd->new_gd_addr;
> +       memcpy((void *)ram_gd, gd, sizeof(gd_t));
> +
> +       /*
> +        * Reload the Global Descriptor Table so FS points to the
> +        * in-RAM copy of Global Data (calculate_relocation_address()
> +        * has already calculated the in-RAM location of the GDT)
> +        */
> +       ram_gd->gd_addr = (ulong)ram_gd;
> +       init_gd(ram_gd, (u64 *)gd->gdt_addr);
> +
> +       return 0;
> +}
> +
> +int init_cache_f_r(void)
> +{
> +       /* Initialise the CPU cache(s) */
> +       return init_cache();
> +}
> +
> +int set_reloc_flag_r(void)
> +{
> +       gd->flags = GD_FLG_RELOC;
> +
> +       return 0;
> +}
> +
>  int mem_malloc_init_r(void)
>  {
>        mem_malloc_init(((gd->relocaddr - CONFIG_SYS_MALLOC_LEN)+3)&~3,
> diff --git a/arch/x86/lib/relocate.c b/arch/x86/lib/relocate.c
> index badb5f8..200baab 100644
> --- a/arch/x86/lib/relocate.c
> +++ b/arch/x86/lib/relocate.c
> @@ -34,13 +34,10 @@
>  #include <common.h>
>  #include <malloc.h>
>  #include <asm/u-boot-x86.h>
> +#include <asm/relocate.h>
>  #include <elf.h>
>
> -static int copy_uboot_to_ram(void);
> -static int clear_bss(void);
> -static int do_elf_reloc_fixups(void);
> -
> -static int copy_uboot_to_ram(void)
> +int copy_uboot_to_ram(void)
>  {
>        size_t len = (size_t)&__data_end - (size_t)&__text_start;
>
> @@ -49,7 +46,7 @@ static int copy_uboot_to_ram(void)
>        return 0;
>  }
>
> -static int clear_bss(void)
> +int clear_bss(void)
>  {
>        ulong dst_addr = (ulong)&__bss_start + gd->reloc_off;
>        size_t len = (size_t)&__bss_end - (size_t)&__bss_start;
> @@ -59,7 +56,7 @@ static int clear_bss(void)
>        return 0;
>  }
>
> -static int do_elf_reloc_fixups(void)
> +int do_elf_reloc_fixups(void)
>  {
>        Elf32_Rel *re_src = (Elf32_Rel *)(&__rel_dyn_start);
>        Elf32_Rel *re_end = (Elf32_Rel *)(&__rel_dyn_end);
> @@ -92,24 +89,3 @@ static int do_elf_reloc_fixups(void)
>
>        return 0;
>  }
> -
> -void relocate_code(ulong dummy_1, gd_t *id, ulong dummy_2)
> -{
> -       /*
> -        * Copy U-Boot into RAM, clear the BSS and perform relocation
> -        * adjustments
> -        */
> -       copy_uboot_to_ram();
> -       clear_bss();
> -       do_elf_reloc_fixups();
> -
> -       /*
> -        * Transfer execution from Flash to RAM by calculating the address
> -        * of the in-RAM copy of board_init_r() and calling it
> -        */
> -       (board_init_r + gd->reloc_off)(gd, gd->relocaddr);
> -
> -       /* NOTREACHED - board_init_r() does not return */
> -       while (1)
> -               ;
> -}
> --
> 1.7.5.2.317.g391b14
>
> _______________________________________________
> U-Boot mailing list
> U-Boot@lists.denx.de
> http://lists.denx.de/mailman/listinfo/u-boot
Graeme Russ Jan. 8, 2012, 9:10 a.m. UTC | #2
On 08/01/12 09:21, Simon Glass wrote:
> Hi Graeme,
> 
> On Wed, Jan 4, 2012 at 11:59 AM, Graeme Russ <graeme.russ@gmail.com> wrote:
>> Create an init function array for board_init_f_r - This finalises the
>> migration to a purely array based initialisation mechanism
>>
>> Also tweak a few comments while we are at it so everything is 'correct'
>>
>> Signed-off-by: Graeme Russ <graeme.russ@gmail.com>
> 
> I have been through this series again and it looks good to me. I don't
> suppose an Acked-by from me is much use since you are the x86
> maintainer and I admit to no knowledge of the subject, but let me know
> if so.

Being a maintainer does not mean I'm infallible ;) - A formal Acked-by
would be appreciated

> There may be some conflict between your series and the generic board
> one that I just sent out yesterday but I have not tried it. But now
> that series is opt-in on a board-by-board basis, we might be ok, and
> in any case x86 can just opt out until everything works.

Plus this series is not RFC and pending no objections (particularly from
Wolfgang) I plan to put it in x86/master 'very soon (tm)' and send a pull
request.

Regards,

Graeme
Simon Glass Jan. 8, 2012, 6:01 p.m. UTC | #3
Hi Graeme,

On Sun, Jan 8, 2012 at 1:10 AM, Graeme Russ <graeme.russ@gmail.com> wrote:
> On 08/01/12 09:21, Simon Glass wrote:
>> Hi Graeme,
>>
>> On Wed, Jan 4, 2012 at 11:59 AM, Graeme Russ <graeme.russ@gmail.com> wrote:
>>> Create an init function array for board_init_f_r - This finalises the
>>> migration to a purely array based initialisation mechanism
>>>
>>> Also tweak a few comments while we are at it so everything is 'correct'
>>>
>>> Signed-off-by: Graeme Russ <graeme.russ@gmail.com>
>>
>> I have been through this series again and it looks good to me. I don't
>> suppose an Acked-by from me is much use since you are the x86
>> maintainer and I admit to no knowledge of the subject, but let me know
>> if so.
>
> Being a maintainer does not mean I'm infallible ;) - A formal Acked-by
> would be appreciated

OK will do.

>
>> There may be some conflict between your series and the generic board
>> one that I just sent out yesterday but I have not tried it. But now
>> that series is opt-in on a board-by-board basis, we might be ok, and
>> in any case x86 can just opt out until everything works.
>
> Plus this series is not RFC and pending no objections (particularly from
> Wolfgang) I plan to put it in x86/master 'very soon (tm)' and send a pull
> request.

OK

Regards,
Simon

>
> Regards,
>
> Graeme
>
Simon Glass Jan. 12, 2012, 5:04 a.m. UTC | #4
On Wed, Jan 4, 2012 at 11:59 AM, Graeme Russ <graeme.russ@gmail.com> wrote:
> Create an init function array for board_init_f_r - This finalises the
> migration to a purely array based initialisation mechanism
>
> Also tweak a few comments while we are at it so everything is 'correct'
>
> Signed-off-by: Graeme Russ <graeme.russ@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

> ---
> Changes for v2:
>  - Renamed to a more apt name
>  - Fix bug in set_reloc_flag_r
>  - Re-instate gd->flags = boot_flags; in board_init_f
>  - Added commit message
>
>  arch/x86/include/asm/init_helpers.h |    5 +
>  arch/x86/include/asm/relocate.h     |   33 ++++++
>  arch/x86/lib/board.c                |  186 +++++++++++++++++------------------
>  arch/x86/lib/init_helpers.c         |   75 ++++++++++++++
>  arch/x86/lib/relocate.c             |   32 +-----
>  5 files changed, 206 insertions(+), 125 deletions(-)
>  create mode 100644 arch/x86/include/asm/relocate.h
>
diff mbox

Patch

diff --git a/arch/x86/include/asm/init_helpers.h b/arch/x86/include/asm/init_helpers.h
index 14ef11a..192f18e 100644
--- a/arch/x86/include/asm/init_helpers.h
+++ b/arch/x86/include/asm/init_helpers.h
@@ -27,7 +27,12 @@ 
 int display_banner(void);
 int display_dram_config(void);
 int init_baudrate_f(void);
+int calculate_relocation_address(void);

+int copy_gd_to_ram_f_r(void);
+int init_cache_f_r(void);
+
+int set_reloc_flag_r(void);
 int mem_malloc_init_r(void);
 int init_bd_struct_r(void);
 int flash_init_r(void);
diff --git a/arch/x86/include/asm/relocate.h b/arch/x86/include/asm/relocate.h
new file mode 100644
index 0000000..33129ef
--- /dev/null
+++ b/arch/x86/include/asm/relocate.h
@@ -0,0 +1,33 @@ 
+/*
+ * (C) Copyright 2011
+ * Graeme Russ, <graeme.russ@gmail.com>
+ *
+ * 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., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#ifndef _RELOCATE_H_
+#define _RELOCATE_H_
+
+#include <common.h>
+
+int copy_uboot_to_ram(void);
+int clear_bss(void);
+int do_elf_reloc_fixups(void);
+
+#endif	/* !_RELOCATE_H_ */
diff --git a/arch/x86/lib/board.c b/arch/x86/lib/board.c
index 89721c7..5f0b62c 100644
--- a/arch/x86/lib/board.c
+++ b/arch/x86/lib/board.c
@@ -35,7 +35,7 @@ 
 #include <watchdog.h>
 #include <stdio_dev.h>
 #include <asm/u-boot-x86.h>
-#include <asm/processor.h>
+#include <asm/relocate.h>

 #include <asm/init_helpers.h>
 #include <asm/init_wrappers.h>
@@ -43,31 +43,58 @@ 
 /*
  * Breath some life into the board...
  *
- * Initialize an SMC for serial comms, and carry out some hardware
- * tests.
+ * Getting the board up and running is a three-stage process:
+ *  1) Execute from Flash, SDRAM Uninitialised
+ *     At this point, there is a limited amount of non-SDRAM memory
+ *     (typically the CPU cache, but can also be SRAM or even a buffer of
+ *     of some peripheral). This limited memory is used to hold:
+ *      - The initial copy of the Global Data Structure
+ *      - A temporary stack
+ *      - A temporary x86 Global Descriptor Table
+ *      - The pre-console buffer (if enabled)
  *
- * The first part of initialization is running from Flash memory;
- * its main purpose is to initialize the RAM so that we
- * can relocate the monitor code to RAM.
+ *     The following is performed during this phase of execution:
+ *      - Core low-level CPU initialisation
+ *      - Console initialisation
+ *      - SDRAM initialisation
+ *
+ *  2) Execute from Flash, SDRAM Initialised
+ *     At this point we copy Global Data from the initial non-SDRAM
+ *     memory and set up the permanent stack in SDRAM. The CPU cache is no
+ *     longer being used as temporary memory, so we can now fully enable
+ *     it.
+ *
+ *     The following is performed during this phase of execution:
+ *      - Create final stack in SDRAM
+ *      - Copy Global Data from temporary memory to SDRAM
+ *      - Enabling of CPU cache(s),
+ *      - Copying of U-Boot code and data from Flash to RAM
+ *      - Clearing of the BSS
+ *      - ELF relocation adjustments
+ *
+ *  3) Execute from SDRAM
+ *     The following is performed during this phase of execution:
+ *      - All remaining initialisation
  */

 /*
- * All attempts to come up with a "common" initialization sequence
- * that works for all boards and architectures failed: some of the
- * requirements are just _too_ different. To get rid of the resulting
- * mess of board dependend #ifdef'ed code we now make the whole
- * initialization sequence configurable to the user.
- *
- * The requirements for any new initalization function is simple: it
- * receives a pointer to the "global data" structure as it's only
- * argument, and returns an integer return code, where 0 means
- * "continue" and != 0 means "fatal error, hang the system".
+ * The requirements for any new initalization function is simple: it is
+ * a function with no parameters which returns an integer return code,
+ * where 0 means "continue" and != 0 means "fatal error, hang the system"
  */
 typedef int (init_fnc_t) (void);

-static int calculate_relocation_address(void);
-static int copy_gd_to_ram(void);
-
+/*
+ * init_sequence_f is the list of init functions which are run when U-Boot
+ * is executing from Flash with a limited 'C' environment. The following
+ * limitations must be considered when implementing an '_f' function:
+ *  - 'static' variables are read-only
+ *  - Global Data (gd->xxx) is read/write
+ *  - Stack space is limited
+ *
+ * The '_f' sequence must, as a minimum, initialise SDRAM. It _should_
+ * also initialise the console (to provide early debug output)
+ */
 init_fnc_t *init_sequence_f[] = {
 	cpu_init_f,
 	board_early_init_f,
@@ -81,7 +108,39 @@  init_fnc_t *init_sequence_f[] = {
 	NULL,
 };

+/*
+ * init_sequence_f_r is the list of init functions which are run when
+ * U-Boot is executing from Flash with a semi-limited 'C' environment.
+ * The following limitations must be considered when implementing an
+ * '_f_r' function:
+ *  - 'static' variables are read-only
+ *  - Global Data (gd->xxx) is read/write
+ *
+ * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
+ * supported).  It _should_, if possible, copy global data to RAM and
+ * initialise the CPU caches (to speed up the relocation process)
+ */
+init_fnc_t *init_sequence_f_r[] = {
+	copy_gd_to_ram_f_r,
+	init_cache_f_r,
+	copy_uboot_to_ram,
+	clear_bss,
+	do_elf_reloc_fixups,
+
+	NULL,
+};
+
+/*
+ * init_sequence_r is the list of init functions which are run when U-Boot
+ * is executing from RAM with a full 'C' environment. There are no longer
+ * any limitations which must be considered when implementing an '_r'
+ * function, (i.e.'static' variables are read/write)
+ *
+ * If not already done, the '_r' sequence must copy global data to RAM and
+ * (should) initialise the CPU caches.
+ */
 init_fnc_t *init_sequence_r[] = {
+	set_reloc_flag_r,
 	init_bd_struct_r,
 	mem_malloc_init_r,
 	cpu_init_r,
@@ -157,43 +216,6 @@  static void do_init_loop(init_fnc_t **init_fnc_ptr)
 	}
 }

-static int calculate_relocation_address(void)
-{
-	ulong text_start = (ulong)&__text_start;
-	ulong bss_end = (ulong)&__bss_end;
-	ulong dest_addr;
-
-	/*
-	 * NOTE: All destination address are rounded down to 16-byte
-	 *       boundary to satisfy various worst-case alignment
-	 *       requirements
-	 */
-
-	/* Global Data is at top of available memory */
-	dest_addr = gd->ram_size;
-	dest_addr -= GENERATED_GBL_DATA_SIZE;
-	dest_addr &= ~15;
-	gd->new_gd_addr = dest_addr;
-
-	/* GDT is below Global Data */
-	dest_addr -= X86_GDT_SIZE;
-	dest_addr &= ~15;
-	gd->gdt_addr = dest_addr;
-
-	/* Stack is below GDT */
-	gd->start_addr_sp = dest_addr;
-
-	/* U-Boot is below the stack */
-	dest_addr -= CONFIG_SYS_STACK_SIZE;
-	dest_addr -= (bss_end - text_start);
-	dest_addr &= ~15;
-	gd->relocaddr = dest_addr;
-	gd->reloc_off = (dest_addr - text_start);
-
-	return 0;
-}
-
-/* Perform all steps necessary to get RAM initialised ready for relocation */
 void board_init_f(ulong boot_flags)
 {
 	gd->flags = boot_flags;
@@ -201,10 +223,9 @@  void board_init_f(ulong boot_flags)
 	do_init_loop(init_sequence_f);

 	/*
-	 * SDRAM is now initialised, U-Boot has been copied into SDRAM,
-	 * the BSS has been cleared etc. The final stack can now be setup
-	 * in SDRAM. Code execution will continue (momentarily) in Flash,
-	 * but with the stack in SDRAM and Global Data in temporary memory
+	 * SDRAM and console are now initialised. The final stack can now
+	 * be setup in SDRAM. Code execution will continue in Flash, but
+	 * with the stack in SDRAM and Global Data in temporary memory
 	 * (CPU cache)
 	 */
 	board_init_f_r_trampoline(gd->start_addr_sp);
@@ -216,51 +237,22 @@  void board_init_f(ulong boot_flags)

 void board_init_f_r(void)
 {
-	if (copy_gd_to_ram() != 0)
-		hang();
-
-	if (init_cache() != 0)
-		hang();
-
-	relocate_code(0, gd, 0);
-
-	/* NOTREACHED - relocate_code() does not return */
-	while (1)
-		;
-}
-
-static int copy_gd_to_ram(void)
-{
-	gd_t *ram_gd;
+	do_init_loop(init_sequence_f_r);

 	/*
-	 * Global data is still in temporary memory (the CPU cache).
-	 * calculate_relocation_address() has set gd->new_gd_addr to
-	 * where the global data lives in RAM but getting it there
-	 * safely is a bit tricky due to the 'F-Segment Hack' that
-	 * we need to use for x86
+	 * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
+	 * Transfer execution from Flash to RAM by calculating the address
+	 * of the in-RAM copy of board_init_r() and calling it
 	 */
-	ram_gd = (gd_t *)gd->new_gd_addr;
-	memcpy((void *)ram_gd, gd, sizeof(gd_t));
+	(board_init_r + gd->reloc_off)(gd, gd->relocaddr);

-	/*
-	 * Reload the Global Descriptor Table so FS points to the
-	 * in-RAM copy of Global Data (calculate_relocation_address()
-	 * has already calculated the in-RAM location of the GDT)
-	 */
-	ram_gd->gd_addr = (ulong)ram_gd;
-	init_gd(ram_gd, (u64 *)gd->gdt_addr);
-
-	return 0;
+	/* NOTREACHED - board_init_r() does not return */
+	while (1)
+		;
 }

 void board_init_r(gd_t *id, ulong dest_addr)
 {
-	gd->flags |= GD_FLG_RELOC;
-
-	/* compiler optimization barrier needed for GCC >= 3.4 */
-	__asm__ __volatile__("" : : : "memory");
-
 	do_init_loop(init_sequence_r);

 	/* main_loop() can return to retry autoboot, if so just run it again. */
diff --git a/arch/x86/lib/init_helpers.c b/arch/x86/lib/init_helpers.c
index 547b180..9f4dee0 100644
--- a/arch/x86/lib/init_helpers.c
+++ b/arch/x86/lib/init_helpers.c
@@ -29,6 +29,7 @@ 
 #include <ide.h>
 #include <serial.h>
 #include <status_led.h>
+#include <asm/processor.h>
 #include <asm/u-boot-x86.h>

 #include <asm/init_helpers.h>
@@ -70,6 +71,80 @@  int init_baudrate_f(void)
 	return 0;
 }

+int calculate_relocation_address(void)
+{
+	ulong text_start = (ulong)&__text_start;
+	ulong bss_end = (ulong)&__bss_end;
+	ulong dest_addr;
+
+	/*
+	 * NOTE: All destination address are rounded down to 16-byte
+	 *       boundary to satisfy various worst-case alignment
+	 *       requirements
+	 */
+
+	/* Global Data is at top of available memory */
+	dest_addr = gd->ram_size;
+	dest_addr -= GENERATED_GBL_DATA_SIZE;
+	dest_addr &= ~15;
+	gd->new_gd_addr = dest_addr;
+
+	/* GDT is below Global Data */
+	dest_addr -= X86_GDT_SIZE;
+	dest_addr &= ~15;
+	gd->gdt_addr = dest_addr;
+
+	/* Stack is below GDT */
+	gd->start_addr_sp = dest_addr;
+
+	/* U-Boot is below the stack */
+	dest_addr -= CONFIG_SYS_STACK_SIZE;
+	dest_addr -= (bss_end - text_start);
+	dest_addr &= ~15;
+	gd->relocaddr = dest_addr;
+	gd->reloc_off = (dest_addr - text_start);
+
+	return 0;
+}
+
+int copy_gd_to_ram_f_r(void)
+{
+	gd_t *ram_gd;
+
+	/*
+	 * Global data is still in temporary memory (the CPU cache).
+	 * calculate_relocation_address() has set gd->new_gd_addr to
+	 * where the global data lives in RAM but getting it there
+	 * safely is a bit tricky due to the 'F-Segment Hack' that
+	 * we need to use for x86
+	 */
+	ram_gd = (gd_t *)gd->new_gd_addr;
+	memcpy((void *)ram_gd, gd, sizeof(gd_t));
+
+	/*
+	 * Reload the Global Descriptor Table so FS points to the
+	 * in-RAM copy of Global Data (calculate_relocation_address()
+	 * has already calculated the in-RAM location of the GDT)
+	 */
+	ram_gd->gd_addr = (ulong)ram_gd;
+	init_gd(ram_gd, (u64 *)gd->gdt_addr);
+
+	return 0;
+}
+
+int init_cache_f_r(void)
+{
+	/* Initialise the CPU cache(s) */
+	return init_cache();
+}
+
+int set_reloc_flag_r(void)
+{
+	gd->flags = GD_FLG_RELOC;
+
+	return 0;
+}
+
 int mem_malloc_init_r(void)
 {
 	mem_malloc_init(((gd->relocaddr - CONFIG_SYS_MALLOC_LEN)+3)&~3,
diff --git a/arch/x86/lib/relocate.c b/arch/x86/lib/relocate.c
index badb5f8..200baab 100644
--- a/arch/x86/lib/relocate.c
+++ b/arch/x86/lib/relocate.c
@@ -34,13 +34,10 @@ 
 #include <common.h>
 #include <malloc.h>
 #include <asm/u-boot-x86.h>
+#include <asm/relocate.h>
 #include <elf.h>

-static int copy_uboot_to_ram(void);
-static int clear_bss(void);
-static int do_elf_reloc_fixups(void);
-
-static int copy_uboot_to_ram(void)
+int copy_uboot_to_ram(void)
 {
 	size_t len = (size_t)&__data_end - (size_t)&__text_start;

@@ -49,7 +46,7 @@  static int copy_uboot_to_ram(void)
 	return 0;
 }

-static int clear_bss(void)
+int clear_bss(void)
 {
 	ulong dst_addr = (ulong)&__bss_start + gd->reloc_off;
 	size_t len = (size_t)&__bss_end - (size_t)&__bss_start;
@@ -59,7 +56,7 @@  static int clear_bss(void)
 	return 0;
 }

-static int do_elf_reloc_fixups(void)
+int do_elf_reloc_fixups(void)
 {
 	Elf32_Rel *re_src = (Elf32_Rel *)(&__rel_dyn_start);
 	Elf32_Rel *re_end = (Elf32_Rel *)(&__rel_dyn_end);
@@ -92,24 +89,3 @@  static int do_elf_reloc_fixups(void)

 	return 0;
 }
-
-void relocate_code(ulong dummy_1, gd_t *id, ulong dummy_2)
-{
-	/*
-	 * Copy U-Boot into RAM, clear the BSS and perform relocation
-	 * adjustments
-	 */
-	copy_uboot_to_ram();
-	clear_bss();
-	do_elf_reloc_fixups();
-
-	/*
-	 * Transfer execution from Flash to RAM by calculating the address
-	 * of the in-RAM copy of board_init_r() and calling it
-	 */
-	(board_init_r + gd->reloc_off)(gd, gd->relocaddr);
-
-	/* NOTREACHED - board_init_r() does not return */
-	while (1)
-		;
-}