/*
 *               Copyright (C) 2005, 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.
 *
 */

#include <sc/sc.h>

/*
 * Boot ROM 
 *
 * The booting of the secure cartridge is done in several stages.  This code
 * is for the first stage of boot (boot0) when power is first applied to the
 * SC.  The goal of boot0 is to load, decrypt, and verify the next stage of
 * the boot process (boot1) from the builtin NAND flash device.
 *
 * The purpose of the assembler portion of the boot process is to swap
 * the location of boot rom and direct access (DA) memory, set the stack
 * pointer, and set memory permissions on the stack.  Once the stack
 * has been setup, the assembler code jumps to C code to perform the rest
 * of the boot process.
 *
 */

     .text
     .code 32
     .align 0
     .global _start

#define BOOT_STACK (320*1024)
#define BOOT_STACK_SIZE 1024
#define CACHE_LINE_SIZE 16
_start:
        ldr     r0, =SYS_CTRL_REG   @ Check to see if boot rom
        ldr     r1, [r0]            @ and DA have been flipped
        ldr     r2, =SYS_CTRL_MEM_FLIP|SYS_CTRL_CFG_EN
        tst     r1, #SYS_CTRL_MEM_FLIP

        bne     flipped
        nop                         @ Cacheline align flipping code
        nop
        nop

        str     r2, [r0]            @ if not flipped, go ahead and flip
        mov     pc, #DA_START       @ then jump back to start

flipped:
        /* Make stack read/writable */
        ldr     r0, =DAX_START+BOOT_STACK
        ldr     r1, =(PT_ACCESS_RW << PT_SC_PROT_SHIFT)
        ldr     r2, =BOOT_STACK_SIZE/CACHE_LINE_SIZE
loop:
        subs    r2, r2, #1
        str     r1, [r0, #-CACHE_LINE_SIZE]!
        bne     loop

        mov     r3, #DA_START       @ Init unused registers
        ldmia   r3, {r4-r11}        @ for verilog simulation

        ldr     sp, =BOOT_STACK     @ Set the stack pointer
        ldr     r0, =bootc
        mov     lr, pc
        bx      r0                  @ and jump to C initialization code

        mov     pc, #0              @ Jump to start of Boot1

    .end

