crtat91x40_ram.S 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. .nolist
  2. .psize 0
  3. /*
  4. * Copyright (C) 2005 by egnite Software GmbH
  5. * Copyright (C) 2011 by egnite GmbH
  6. *
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the copyright holders nor the names of
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  32. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * For additional information see http://www.ethernut.de/
  36. */
  37. /*
  38. * $Id$
  39. */
  40. #include <arch/arm.h>
  41. .list
  42. /*
  43. * This file contains the runtime initialization for AT91X40 based systems.
  44. * It replaces the older crtat91_ram.S, which is now deprecated.
  45. *
  46. * The related linker file is at91x40_ram.ld.
  47. *
  48. * All code and all data is located in internal RAM and may be used with
  49. * a boot loader or debugger, where the remapping must take place. It is
  50. * assumed, that this code is loaded at address 0x00000000.
  51. *
  52. * Tested on AT91R40008 only.
  53. */
  54. /*
  55. * Stack sizes.
  56. *
  57. * Interrupt nesting is currently not supported. Therefore dedicated
  58. * stacks are used for all exceptions.
  59. */
  60. #ifndef INI_STACK_SIZE
  61. #define INI_STACK_SIZE (128 * 4)
  62. #endif
  63. #ifndef IRQ_STACK_SIZE
  64. #define IRQ_STACK_SIZE (128 * 4)
  65. #endif
  66. #ifndef FIQ_STACK_SIZE
  67. #define FIQ_STACK_SIZE (64 * 4)
  68. #endif
  69. #ifndef EXC_STACK_SIZE
  70. #define EXC_STACK_SIZE (32 * 4)
  71. #endif
  72. #define TOTAL_STACK_SIZE (INI_STACK_SIZE + IRQ_STACK_SIZE + FIQ_STACK_SIZE + EXC_STACK_SIZE)
  73. /* We start in ARM mode. */
  74. .arm
  75. /*
  76. * Vector Section: Must be located at address 0.
  77. */
  78. .section .vectors,"ax",%progbits
  79. .global __vectors
  80. __vectors:
  81. /* Reset entry. */
  82. b _start
  83. /* Undefined instruction exception entry. */
  84. ldr pc, [pc, #(5 * 4)]
  85. /* Software interrupt entry. */
  86. ldr pc, [pc, #(5 * 4)]
  87. /* Prefetch abort exception entry. */
  88. ldr pc, [pc, #(5 * 4)]
  89. /* Data abort exception entry. */
  90. ldr pc, [pc, #(5 * 4)]
  91. /*
  92. * Reserved entry, used by some boot loaders to store the
  93. * size of the binary image.
  94. */
  95. .word 0
  96. /*
  97. * On IRQ/FIQ the PC will be loaded from AIC_IVR, which
  98. * provides the address previously set in AIC_SVR().
  99. * The interrupt routine will be called in ARM_MODE_IRQ
  100. * with IRQ disabled and FIQ unchanged.
  101. */
  102. ldr pc, [pc, #-0xF20]
  103. ldr pc, [pc, #-0xF20]
  104. /*
  105. * Exceptions are weakly linked to endless loops.
  106. *
  107. * This may look a bit confusing. The following words contain
  108. * the jump addresses that are loaded at each entry, see above.
  109. * They contain weakly defined addresses pointing to executable
  110. * code. Each of them may be overridden by the operating system
  111. * or the application, if a function with the same name exist.
  112. * By default they simply point to endless loops.
  113. *
  114. * We intentionally use a dedicated loop with a global label
  115. * for each exception. If an unhandled exception occurs, we
  116. * can use a simple debugger (like OpenOCD) to retrieve the
  117. * current program counter and check the linker map to determine
  118. * the type of the exception.
  119. */
  120. .word __undef
  121. .word __swi
  122. .word __prefetch_abort
  123. .word __data_abort
  124. .weak __undef, __swi, __prefetch_abort, __data_abort
  125. .set __undef, __undef_stop
  126. .set __swi, __swi_stop
  127. .set __prefetch_abort, __prefetch_abort_stop
  128. .set __data_abort, __data_abort_stop
  129. /*
  130. * Init Section 0: Exception Dummies.
  131. */
  132. .section .init0,"ax",%progbits
  133. __undef_stop:
  134. b __undef_stop
  135. __swi_stop:
  136. b __swi_stop
  137. __prefetch_abort_stop:
  138. b __prefetch_abort_stop
  139. __data_abort_stop:
  140. b __data_abort_stop
  141. /* Make sure that the literal pool is empty before the section ends. */
  142. .ltorg
  143. /*
  144. * User Init Section 0
  145. *
  146. * Additional code may be added here by placing functions in
  147. * section .init0.user.
  148. */
  149. /*
  150. * Init Section 1: Empty, remapping is done in the loader.
  151. *
  152. * Remapping is done in the loader.
  153. */
  154. .section .init1,"ax",%progbits
  155. /*
  156. * User Init Section 1
  157. *
  158. * Additional code may be added here by placing functions in
  159. * section .init1.user. With code running in RAM this is
  160. * actually the same as .init0.user.
  161. */
  162. .global _start
  163. _start:
  164. /*
  165. * Init Section 2: Set stack pointers and initialize C variables.
  166. */
  167. .section .init2,"ax",%progbits
  168. /*
  169. * Set stack pointers with disabled interrupts.
  170. */
  171. ldr r0, =__stack
  172. msr CPSR_c, #ARM_MODE_SVC | ARM_CPSR_I_BIT | ARM_CPSR_F_BIT
  173. mov sp, r0
  174. sub r0, r0, #INI_STACK_SIZE
  175. msr CPSR_c, #ARM_MODE_FIQ | ARM_CPSR_I_BIT | ARM_CPSR_F_BIT
  176. mov sp, r0
  177. sub r0, r0, #FIQ_STACK_SIZE
  178. msr CPSR_c, #ARM_MODE_IRQ | ARM_CPSR_I_BIT | ARM_CPSR_F_BIT
  179. mov sp, r0
  180. sub r0, r0, #IRQ_STACK_SIZE
  181. msr CPSR_c, #ARM_MODE_ABORT | ARM_CPSR_I_BIT | ARM_CPSR_F_BIT
  182. mov sp, r0
  183. msr CPSR_c, #ARM_MODE_UNDEF | ARM_CPSR_I_BIT | ARM_CPSR_F_BIT
  184. mov sp, r0
  185. /*
  186. * Nut/OS system and application are running in system mode.
  187. * Note, that we re-use the supervisory stack.
  188. */
  189. msr CPSR_c, #ARM_MODE_SYS | ARM_CPSR_I_BIT | ARM_CPSR_F_BIT
  190. ldr r0, =__stack
  191. mov sp, r0
  192. /*
  193. * Clear bss.
  194. */
  195. ldr r1, =__bss_start
  196. ldr r2, =__bss_end
  197. ldr r3, =0
  198. 1: cmp r1, r2
  199. strne r3, [r1], #+4
  200. bne 1b
  201. /* Jump over the literal pool. */
  202. b _init_hw
  203. /* Make sure that the literal pool is empty before the section ends. */
  204. .ltorg
  205. /*
  206. * User Init Section 2
  207. *
  208. * Additional code may be added here by placing functions in
  209. * section .init2.user.
  210. *
  211. * This section may be used for early hardware initialization
  212. * routines written in C.
  213. */
  214. _init_hw:
  215. /*
  216. * Init Section 3: Hardware initialization.
  217. */
  218. .section .init3,"ax",%progbits
  219. /*
  220. * Enable all clocks.
  221. */
  222. mvn r0, #0
  223. ldr r1, =PS_BASE
  224. str r0, [r1, #(PS_PCER - PS_BASE)]
  225. /*
  226. * Initialize the interrupt controller.
  227. */
  228. add r0, pc,#-(8 + . - __aic_table)
  229. ldmia r0, {r1-r4}
  230. str r4, [r1, #AIC_SPU]
  231. mov r0, #8
  232. 1: str r1, [r1, #AIC_EOICR]
  233. subs r0, r0, #1
  234. bhi 1b
  235. str r2, [r1, #AIC_SVR(0)]
  236. add r1, r1, #AIC_SVR(0)
  237. mov r0, #31
  238. 1: str r3, [r1, r0, LSL #2]
  239. subs r0, r0, #1
  240. bhi 1b
  241. b _enter_rtos
  242. __aic_table:
  243. .word AIC_BASE
  244. .word _irq_dummy
  245. .word _irq_dummy
  246. .word _irq_dummy
  247. /* Interrupt dummy. */
  248. .global _irq_dummy
  249. _irq_dummy:
  250. b _irq_dummy
  251. /* Make sure that the literal pool is empty before the section ends. */
  252. .ltorg
  253. /*
  254. * User Init Section 3
  255. *
  256. * Additional code may be added here by placing functions in
  257. * section .init3.user.
  258. *
  259. * This section may be used for additional hardware initialization
  260. * routines written in C. Native interrupt routines may be used.
  261. */
  262. _enter_rtos:
  263. /*
  264. * Init Section 4: Enter system.
  265. */
  266. .section .init4,"ax",%progbits
  267. /* Jump to Nut/OS initialization. */
  268. ldr r0, =NutInit
  269. mov lr, pc
  270. bx r0
  271. /*
  272. * Exit Section 0: Endless loop.
  273. */
  274. .section .exit0,"ax",%progbits
  275. 1: b 1b
  276. /*
  277. * Stack Section.
  278. */
  279. .section .stack, "w", %nobits
  280. .space TOTAL_STACK_SIZE