/*
This is the default Startup for STR91x devices for the GNU toolchain

It has been designed by ST Microelectronics and modified by Raisonance.

You can use it, modify it, distribute it freely but without any waranty.

*/



.extern main


;/* the following are useful for initializing the .data section */
.extern _sidata ;/* start address for the initialization values of the .data section. defined in linker script */
.extern _sdata ;/* start address for the .data section. defined in linker script */
.extern _edata ;/* end address for the .data section. defined in linker script */

;/* the following are useful for initializing the .bss section */
.extern _sbss ;/* start address for the .bss section. defined in linker script */
.extern _ebss ;/* end address for the .bss section. defined in linker script */

;/* Standard definitions of Mode bits and Interrupt (I & F) flags in PSRs */
.set  Mode_USR, 0x10            ;/* User Mode */
.set  Mode_FIQ, 0x11            ;/* FIQ Mode */
.set  Mode_IRQ, 0x12            ;/* IRQ Mode */
.set  Mode_SVC, 0x13            ;/* Supervisor Mode */
.set  Mode_ABT, 0x17            ;/* Abort Mode */
.set  Mode_UNDEF, 0x1B          ;/* Undefined Mode */
.set  Mode_SYS, 0x1F            ;/* System Mode */

.extern VIC0VECT
.extern VIC1VECT


.equ  I_Bit, 0x80               ;/* when I bit is set, IRQ is disabled */
.equ  F_Bit, 0x40               ;/* when F bit is set, FIQ is disabled */

/*; --- System memory locations */

;/* init value for the stack pointer. defined in linker script */
.extern _estack

;/* Stack Sizes. The default values are in the linker script, but they can be overriden. */
.extern _UND_Stack_Init
.extern _SVC_Stack_Init
.extern _ABT_Stack_Init
.extern _FIQ_Stack_Init
.extern _IRQ_Stack_Init
.extern _USR_Stack_Init

.extern _UND_Stack_Size
.extern _SVC_Stack_Size
.extern _ABT_Stack_Size
.extern _FIQ_Stack_Size
.extern _IRQ_Stack_Size
.extern _USR_Stack_Size

SVC_Stack           =     _SVC_Stack_Init /*_estack*/           /*; 256 byte SVC stack at*/
                                              /*; top of memory */
                                              
IRQ_Stack           =     _IRQ_Stack_Init /*SVC_Stack - 256*/     /*; followed by IRQ stack */
USR_Stack           =     _USR_Stack_Init /*IRQ_Stack-1024*/    /*; followed by USR stack */
FIQ_Stack           =     _FIQ_Stack_Init /*USR_Stack-1024*/    /*; followed by FIQ stack*/
ABT_Stack           =     _ABT_Stack_Init /*FIQ_Stack-256*/     /*; followed by ABT stack */
UNDEF_Stack         =     _UND_Stack_Init /*ABT_Stack-256*/     /*; followed by UNDEF stack */

/*; STR9X register specific definition*/

.equ SCRO_AHB_UNB,     0x5C002034

/***************************************************************************************/

        
       
/*;*******************************************************************************
;* Macro Name     : SaveContext
;* Description    : This macro used to save the context before entering
;                   an exception handler.
;* Input          : The range of registers to store.
;* Output         : none
;*******************************************************************************/
  
	.macro Savecontext $r0,$r12
	STMFD sp!,{r0-r12,lr}
	MRS    r1,spsr
	STMFD  sp!,{r1}
	.endm
/*;*******************************************************************************
;* Macro Name     : RestoreContext
;* Description    : This macro used to restore the context to return from
;                   an exception handler and continue the program execution.
;* Input          : The range of registers to restore.
;* Output         : none
;*******************************************************************************/
	.macro RestoreContext $r0,$r12  
	LDMFD   sp!,{r1}           /*; Restore the saved spsr_mode into r1.*/
	MSR     spsr_cxsf,r1      /*; Restore spsr_mode.*/
	LDMFD   sp!,{r0-r12,pc}^   /*; Return to the instruction following...*/
	.endm                      /*; ...the exception interrupt.*/
                 

.globl start
.globl _start
.globl _startup

.section .flashtext,"ax",%progbits
start:
_start:
_startup:

	ldr     PC, =Reset_Handler
	ldr     PC, =UndefinedHandler
	ldr     PC, =SWIHandler
	ldr     PC, =PrefetchAbortHandler
	ldr     PC, =DataAbortHandler
	nop                          /*; Reserved vector*/
	ldr     PC, =IRQHandler
	ldr     PC, =FIQHandler

.text

/*;*******************************************************************************
;* Function Name  : FIQHandler
;* Description    : This function is called when FIQ
;                   exception is entered.
;* Input          : none
;* Output         : none
;******************************************************************************* */

FIQHandler:
	SUB    lr,lr,#4       /*; Update the link register.*/
	SaveContext r0,r7   /* ; Save the workspace plus the current*/
                         /*; return address lr_ fiq and spsr_fiq.*/
	ldr PC,=FIQ_Handler   /*; Branch to FIQ_Handler.*/
	RestoreContext r0,r7  /*; Restore the context and return to the...*/
                         /*; ...program execution.*/


/*;*******************************************************************************
;* Function Name  : UndefinedHandler
;* Description    : This function called when undefined instruction
;                   exception is entered.
;* Input          : none
;* Output         : none
;*******************************************************************************/

UndefinedHandler:
	SaveContext r0,r12      /*; Save the workspace plus the current*/
	ldr PC,=Undefined_Handler /*; Branch to Undefined_Handler.*/
	RestoreContext r0,r12 /*; Return to the instruction following..*/
                           /*; ...the undefined instruction.*/

/*;*******************************************************************************
;* Function Name  : SWIHandler
;* Description    : This function called when SWI instruction executed.
;* Input          : none
;* Output         : none
;*******************************************************************************/

SWIHandler:
	SaveContext r0,r12     /*; Save the workspace plus the current*/
                          /*; return address lr_ svc and spsr_svc.*/
	ldr PC,=SWI_Handler  /*; Branch to SWI_Handler.*/
	RestoreContext r0,r12 /*; Return to the instruction following...*/
                         /*; ...the SWI instruction.*/

/*;*******************************************************************************
;* Function Name  : IRQHandler
;* Description    : This function called when IRQ exception is entered.
;* Input          : none
;* Output         : none
;*******************************************************************************/
 

IRQHandler:
	SUB    lr,lr,#4        /*Update the link register*/
	SaveContext r0,r12     /*Save the workspace plus the current*/
                          /*return address lr_irq and spsr_irq*/
   LDR    r0, = VIC0VECT
   LDR    r0, [r0]        /*Read the routine address*/
   LDR    r1, = VIC1VECT
   LDR    r1, [r1]
   /*Padding between the acknowledge and re-enable of interrupts*/
   /*For more details, please refer to the following URL*/
   /*http://www.arm.com/support/faqip/3682.html*/
   NOP
   NOP
   MSR    cpsr_c,#0x1F            /*Switch to SYS mode and enable IRQ*/
   STMFD  sp!,{lr}                /*Save the link register.*/
   LDR    lr, = IRQ_ReturnAddress /*Read the return address.*/
   BX r0 /*MOV    pc, r0*/                  /*Branch to the IRQ handler.*/
IRQ_ReturnAddress:
   LDMFD  sp!,{lr}                /*Restore the link register.*/
   MSR    cpsr_c,#0xD2 | I_Bit    /*Switch to IRQ mode and disable IRQ*/
   LDR    r0, = VIC0VECT      	 /*Write to the VectorAddress to clear the*/
   STR    r0, [r0]                /*respective interrupt in the internal interrupt*/
   LDR    r1, = VIC1VECT    	    /*Write to the VectorAddressDaisy to clear the*/
   STR    r1,[r1]                 /*respective interrupt in the internal interrupt*/
   RestoreContext r0,r12          /*Restore the context and return to the...*/
                                  /*...program execution.*/
                        
                                                       
/*;*******************************************************************************
;* Function Name  : PrefetchAbortHandler
;* Description    : This function called when Prefetch Abort
;                   exception is entered.
;* Input          : none
;* Output         : none
;*******************************************************************************/

PrefetchAbortHandler:
	SUB    lr,lr,#4      /* ; Update the link register.*/
   SaveContext r0,r12     /*; Save the workspace plus the current*/
                          /*; return address lr_abt and spsr_abt.*/
   ldr PC,=Prefetch_Handler /*; Branch to Prefetch_Handler. */
   RestoreContext r0,r12 /*; Return to the instruction following that... */
                         /*; ...has generated the prefetch abort exception.*/

/*;*******************************************************************************
;* Function Name  : DataAbortHandler
;* Description    : This function is called when Data Abort
;                   exception is entered.
;* Input          : none
;* Output         : none
;********************************************************************************/

DataAbortHandler:
   SUB    lr,lr,#8       /*; Update the link register.*/
   SaveContext r0,r12   /*; Save the workspace plus the current*/
                         /*; return address lr_ abt and spsr_abt.*/
   ldr PC,=Abort_Handler  /*; Branch to Abort_Handler.*/
   RestoreContext r0,r12 /*; Return to the instruction following that...*/
                         /*; ...has generated the data abort exception.*/

/*;*******************************************************************************
;* Macro Name     : IRQ_to_SYS
;* Description    : This macro used to switch form IRQ mode to SYS mode
;* Input          : none.
;* Output         : none
;*******************************************************************************/
   .macro IRQ_to_SYS
   MSR    cpsr_c,#0x1F
   STMFD  sp!,{lr}
   .endm
/*;*******************************************************************************
;* Macro Name     : SYS_to_IRQ
;* Description    : This macro used to switch from SYS mode to IRQ mode
;                   then to return to IRQHnadler routine.
;* Input          : none.
;* Output         : none.
;*******************************************************************************/
	.macro SYS_to_IRQ
   LDMFD  sp!,{lr}      /*; Restore the link register. */
   MSR    cpsr_c,#0xD2  /*; Switch to IRQ mode.*/
   MOV    pc,lr         /*; Return to IRQHandler routine to clear the*/
                        /*; pending bit.*/
   .endm


/***********************************************************************************************/
Reset_Handler:
	ldr    PC, =NextInst

NextInst:

	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop

/*; --- ARM9 core configuration*/

/* configure flash */
   ldr R0, =0x54000000
   ldr R7, =_bb_size /*boot bank size configuration 0x4 */
   str R7, [R0]

   ldr R0, =0x54000004
   ldr R1, =_nbb_size /*non-boot bank size configuration 0x3 */
   str R1, [R0]

	/*
	don't change the boot bank address because this code is probably executing from it.
	and it is happy staying at address zero anyways.
	*/
   /*ldr R0, =0x5400000C*/
   /*ldr R1, =0x0*/ /*boot bank address configuration */
   /*str R1, [R0]*/

   ldr R0, =0x54000010
   ldr R1, =_nbb_addr /*non-boot bank address configuration 0x20000 */
   str R1, [R0]

   ldr R0, =0x54000018
   ldr R1, =0x18   /*enable both banks*/
	str R1, [R0]

/* configure ram */
Enable_SRAM_96K:
   ldr R0, =SCRO_AHB_UNB
   ldr R1, =0x0196  /*TODO: the value written there should depend on the derivative...*/
   str R1, [R0]
   
   MRC P15, 0, r0, c1, c0, 0

  
/*; Initialize Stack pointer for various CPU Modes.*/
   MSR     CPSR_c, #Mode_ABT|F_Bit|I_Bit
   LDR     SP, =ABT_Stack

   MSR     CPSR_c, #Mode_UNDEF|F_Bit|I_Bit
   LDR     SP, =UNDEF_Stack

   MSR     CPSR_c, #Mode_SVC|F_Bit|I_Bit
   LDR     SP, =_estack /*RAM_Limit*/
      
   msr    CPSR_c, #Mode_FIQ        /*; Change to FIQ mode*/
   ldr     SP, =FIQ_Stack           /*; Initialize FIQ stack pointer */

	msr     CPSR_c, #Mode_IRQ        /*; Change to IRQ mode  */
   ldr     SP, =IRQ_Stack           /*; Initialize IRQ stack pointer*/
   MSR     CPSR_c, #Mode_USR        /*; Change to User mode, Enable IRQ and FIQ */
   ldr     SP, =USR_Stack           /*; Initialize USR stack pointer*/

   msr     CPSR_c, #Mode_SYS       /* ; Change to System mode*/

	;/* copy the initial values for .data section from FLASH to RAM */
	ldr	R1, =_sidata
	ldr	R2, =_sdata
	ldr	R3, =_edata
_reset_inidata_loop:
	cmp	R2, R3
	ldrlO	R0, [R1], #4
	strlO	R0, [R2], #4
	blO	_reset_inidata_loop

	;/* Clear the .bss section */
	mov   r0,#0						;/* get a zero */
	ldr   r1,=_sbss				;/* point to bss start */
	ldr   r2,=_ebss				;/* point to bss end */
_reset_inibss_loop:
	cmp   r1,r2						;/* check if some data remains to clear */
	strlo r0,[r1],#4				;/* clear 4 bytes */
	blo   _reset_inibss_loop	;/* loop until done */


        
        
/************************************************************************************************/


/*; --- Now enter the C code */
             ldr    PC, =main
             
        
/*
END
;*************
****** (C) COPYRIGHT 2006 STMicroelectronics *****
****** (C) COPYRIGHT 2006 RAISONANCE *****

END OF FILE****/


