/*
 *               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 <arm.h>
#include <asm.h>
#include <mmu.h>
#include <hw.h>
#include <memmap.h>

        .text
        .code 32
        .align 0
        .global _start

_start:
        ldr     pc, iosReset
        ldr     pc, iosUndefined
        ldr     pc, iosSWI
        ldr     pc, iosPrefetchAbort
        ldr     pc, iosDataAbort
        ldr     pc, iosReserved
        ldr     pc, iosIRQ
        ldr     pc, iosFIQ

iosReset:
        .word   iosResetHandler
iosUndefined:
        .word   iosUndefinedHandler
iosSWI:
        .word   iosSWIHandler
iosPrefetchAbort:
        .word   iosAbortHandler
iosDataAbort:
        .word   iosAbortHandler
iosReserved:
        .word   0 /*iosReservedHandler*/
iosIRQ:
        .word   iosIRQHandler
iosFIQ:
        .word   iosFIQHandler

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

iosResetHandler:
        /* Mask all interrupts in ACR */
        ldr     r0, =0
        ldr     r1, =ACR_INTMASK_TRUSTED 
        str     r0, [r1]

        /* Invalidate I and D caches */
        mov     r0, #0
        mcr     p15, 0, r0, c7, c5, 0
        mcr     p15, 0, r0, c7, c6, 0

        /* Disable I, D caches */
        mrc p15, 0, r0, c1, c0, 0
        bic r0, r0, #ENABLE_D_CACHE
        bic 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

        msr     cpsr_c, #ARM_IRQ|ARM_FIQ|ARM_MODE_SVC
        ldr     sp, = dummyStack + 256
        msr     cpsr_c, #ARM_IRQ|ARM_FIQ|ARM_MODE_IRQ
        ldr     sp, = dummyStack + 256
        msr     cpsr_c, #ARM_IRQ|ARM_FIQ|ARM_MODE_FIQ
        ldr     sp, = dummyStack + 256
        msr     cpsr_c, #ARM_IRQ|ARM_FIQ|ARM_MODE_ABT
        ldr     sp, = dummyStack + 256
        msr     cpsr_c, #ARM_IRQ|ARM_FIQ|ARM_MODE_UND
        ldr     sp, = dummyStack + 256
        msr     cpsr_c, #ARM_MODE_SYS           @ Switch to SYS mode
        ldr     sp, =svcStack+0x4000    @ Set SYS stack ptr
                                                @ with intrs enabled.
        mov     lr, pc                         
        ldr     pc, =bootc                      @ Now that stacks are setup
                                                @ do the rest of init in C.
        /* Jump to the OS loader address, as returned by bootc */
        mov     pc, r0

inf:
        b       inf                             @ We should not get here

iosUndefinedHandler:
        b iosUndefinedHandler
iosSWIHandler:
        b   iosSWIHandler
iosAbortHandler:
        b iosAbortHandler
iosIRQHandler:
        b  iosIRQHandler
iosFIQHandler:
        b       iosFIQHandler

    .end

