/*
 *               Copyright (C) 2006, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and are protected by Federal copyright law. They may not be disclosed
 *  to third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */

/*
 * Boot1
 *
 * The booting of NNGC is done in several stages. The code in this
 * file is for the second stage of boot (boot1) after boot0 is
 * completed. The goal of boot1 is to load, decrypt, and verify
 * the image of boot2, which is a mini-OS read from the flash
 * device. See the comments in boot1.c for an explanation of how
 * that process is carried out.
 *
 * The purpose of the assembler portion of the boot process, which
 * is all in this source file is to set up enoug machine state to
 * be able to run C code. The  rest of the boot process is conducted
 * in C. See the other source files in this directory.
 */


#include <arm.h>
#include <asm.h>
#include <mmu.h>
#include <addr_map.h>
#include <iop.h>


        .text
        .code 32
        .align 0
        .global _start

/*
 * These constants can be changed to change the memory layout.
 * Don't use any hard constants in the source code below.
 *
 * Notice that these addresses are in SRAM.   What we are doing
 * here is preparing the way for SRAM-resident code, before we
 * jump to it.
 */

    .equ    SramRoot, 0x0d400000
    .equ    SramTop, 0x0d40effc
    .equ    SramStack, SramTop

_start:
    /* Invalidate both the I-cache and D-cache */
    mov     r0, #0
    mcr     p15, 0, r0, c7, c5, 0
    mcr     p15, 0, r0, c7, c6, 0

    /* Enable the I-cache */
    mrc     p15, 0, r0, c1, c0, 0
    orr     r0, r0, #ENABLE_I_CACHE
    mcr     p15, 0, r0, c1, c0, 0

    /* Zero the bss */
    ldr     r0, =__bss_start
    ldr     r1, =__bss_end
    ldr     r2, =0
    ldr     r3, =4
    bl      word_set

    /* Initialise the core */
	ldr	    r1, =0
	ldr	    r4, =0
	ldr	    r11, =0
	ldr	    fp, =0
	ldr	    lr, =0

    /* Set up the initial stack in SRAM */
    ldr     sp, =SramStack

    /* Call the C routine */
	bl      bootc
    mov     r1, r0

    /* Disable the I-cache */
    mrc     p15, 0, r0, c1, c0, 0
    bic     r0, r0, #ENABLE_I_CACHE
    mcr     p15, 0, r0, c1, c0, 0

    /* Jump to the OS loader address, as returned by bootc */
    mov     pc, r1

word_set:
    cmp     r0, r1
    bcs     1f
    str     r2, [r0], r3
    b       word_set
1:
    bx      lr

    .end
