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

/*
 * Store the contents of register r2 into a memory
 * block starting at r0 and ending at r1.
 * r3 contains the access stride.
 */
word_set:
        cmp r0, r1
        bcs 1f
        str r2, [r0], r3
        b word_set
1:
        bx lr

iosResetHandler:
        /* Invalidate the L1 I and D caches */
        ldr     r0, =4096               @ Size of L1 caches
        ldr     r1, =L1_II_START        @ L1 I Cache op
        ldr     r2, =L1_DI_START        @ L1 D Cache op
        ldr     r3, =1
L1Loop: str     r3, [r1], #16
        str     r3, [r2], #16
        subs    r0, r0, #16
        bne     L1Loop

        /* Mark text segment as read/execute */
        ldr     r0, =(DAX_START+__text_start)
        ldr     r1, =(DAX_START+__text_end)
        ldr     r2, =(PT_ACCESS_EXEC << PT_SC_PROT_SHIFT)
        ldr     r3, =16
        bl      word_set

        /* Mark rodata segment as read-only */
        ldr     r0, =(DAX_START+__rodata_start)
        ldr     r1, =(DAX_START+__rodata_end)
        ldr     r2, =(PT_ACCESS_READ << PT_SC_PROT_SHIFT)
        bl      word_set

        /* Mark data and bss segments as read/write */
        ldr     r0, =(DAX_START+__data_start)
        ldr     r1, =(DAX_START+__data_end)
        ldr     r2, =(PT_ACCESS_RW << PT_SC_PROT_SHIFT)
        bl      word_set
        ldr     r0, =(DAX_START+__bss_start)
        ldr     r1, =(DAX_START+__bss_end)
        ldr     r2, =(PT_ACCESS_RW << PT_SC_PROT_SHIFT)
        bl      word_set

        /* Mark rest of DA as read/write */
        ldr     r0, =(DAX_START+__bss_end+15)
        and     r0, r0, #~15
        ldr     r1, =(DAX_START+192*1024)
        ldr     r2, =(PT_ACCESS_RW << PT_SC_PROT_SHIFT)
        bl      word_set

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

        ldr     sp, =svcStack+SVC_STACK_SIZE    @ Set SVC stack ptr
        msr     cpsr_c, #ARM_IRQ|ARM_FIQ|ARM_MODE_IRQ
        ldr     sp, =irqStack+IRQ_STACK_SIZE    @ Set IRQ stack ptr
        msr     cpsr_c, #ARM_IRQ|ARM_FIQ|ARM_MODE_FIQ
        ldr     sp, =fiqStack+FIQ_STACK_SIZE    @ Set FIQ stack ptr
        msr     cpsr_c, #ARM_IRQ|ARM_FIQ|ARM_MODE_ABT
        ldr     sp, =abtStack+ABT_STACK_SIZE    @ Set ABT stack ptr
        msr     cpsr_c, #ARM_IRQ|ARM_FIQ|ARM_MODE_UND
        ldr     sp, =undStack+UND_STACK_SIZE    @ Set UND stack ptr
        msr     cpsr_c, #ARM_MODE_SYS           @ Switch to SYS mode
        ldr     sp, =svcStack+SVC_STACK_SIZE    @ Set SYS stack ptr
                                                @ with intrs enabled.
        bl      initc                           @ Now that stacks are setup
                                                @ do the rest of init in C.
inf:
        b       inf                             @ We should not get here

iosFIQHandler:
        b       iosFIQHandler

    .end

