crtzero.S 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. .nolist
  2. .psize 0
  3. /*
  4. * Copyright (C) 2005-2006 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_rom.S, which is now deprecated.
  45. *
  46. * The related linker file is at91x40_rom.ld.
  47. *
  48. * Code is running in Flash memory, which also contains all constant data.
  49. * Internal RAM is used for variables and may be used for functions,
  50. * which require to run at maximum speed.
  51. *
  52. * Tested on AT91R40008 only.
  53. *
  54. */
  55. /*
  56. * Stack sizes.
  57. *
  58. * Interrupt nesting is currently not supported. Therefore dedicated
  59. * stacks are used for all exceptions.
  60. */
  61. #ifndef INI_STACK_SIZE
  62. #define INI_STACK_SIZE (128 * 4)
  63. #endif
  64. #ifndef IRQ_STACK_SIZE
  65. #define IRQ_STACK_SIZE (128 * 4)
  66. #endif
  67. #ifndef FIQ_STACK_SIZE
  68. #define FIQ_STACK_SIZE (64 * 4)
  69. #endif
  70. #ifndef EXC_STACK_SIZE
  71. #define EXC_STACK_SIZE (32 * 4)
  72. #endif
  73. #define TOTAL_STACK_SIZE (INI_STACK_SIZE + IRQ_STACK_SIZE + FIQ_STACK_SIZE + EXC_STACK_SIZE)
  74. /* We start in ARM mode. */
  75. .arm
  76. /*
  77. * Vector Section: Must be copied to RAM during runtime.
  78. */
  79. .section .vectors,"ax",%progbits
  80. .global __vectors
  81. __vectors:
  82. /* Reset entry. */
  83. b _start
  84. /* Undefined instruction exception entry. */
  85. ldr pc, [pc, #(5 * 4)]
  86. /* Software interrupt entry. */
  87. ldr pc, [pc, #(5 * 4)]
  88. /* Prefetch abort exception entry. */
  89. ldr pc, [pc, #(5 * 4)]
  90. /* Data abort exception entry. */
  91. ldr pc, [pc, #(5 * 4)]
  92. /*
  93. * Reserved entry, used by some boot loaders to store the
  94. * size of the binary image.
  95. */
  96. .word 0
  97. /*
  98. * On IRQ/FIQ the PC will be loaded from AIC_IVR, which
  99. * provides the address previously set in AIC_SVR().
  100. * The interrupt routine will be called in ARM_MODE_IRQ
  101. * with IRQ disabled and FIQ unchanged.
  102. */
  103. ldr pc, [pc, #-0xF20]
  104. ldr pc, [pc, #-0xF20]
  105. /*
  106. * Exceptions are weakly linked to endless loops.
  107. *
  108. * This may look a bit confusing. The following words contain
  109. * the jump addresses that are loaded at each entry, see above.
  110. * They contain weakly defined addresses pointing to executable
  111. * code. Each of them may be overridden by the operating system
  112. * or the application, if a function with the same name exist.
  113. * By default they simply point to endless loops.
  114. *
  115. * We intentionally use a dedicated loop with a global label
  116. * for each exception. If an unhandled exception occurs, we
  117. * can use a simple debugger (like OpenOCD) to retrieve the
  118. * current program counter and check the linker map to determine
  119. * the type of the exception.
  120. */
  121. .word __undef
  122. .word __swi
  123. .word __prefetch_abort
  124. .word __data_abort
  125. .weak __undef, __swi, __prefetch_abort, __data_abort
  126. .set __undef, __undef_stop
  127. .set __swi, __swi_stop
  128. .set __prefetch_abort, __prefetch_abort_stop
  129. .set __data_abort, __data_abort_stop
  130. /*
  131. * Init Section 0: Exception Dummies.
  132. */
  133. .section .init0,"ax",%progbits
  134. __undef_stop:
  135. b __undef_stop
  136. __swi_stop:
  137. b __swi_stop
  138. __prefetch_abort_stop:
  139. b __prefetch_abort_stop
  140. __data_abort_stop:
  141. b __data_abort_stop
  142. /* Make sure that the literal pool is empty before the section ends. */
  143. .ltorg
  144. /*
  145. * User Init Section 0
  146. *
  147. * Additional code may be added here by placing functions in
  148. * section .init0.user. Only relocatable code is allowed.
  149. *
  150. * Memory test routines are good candidates.
  151. */
  152. .global _start
  153. _start:
  154. /*
  155. * Init Section 1: Remapping.
  156. */
  157. .section .init1,"ax",%progbits
  158. /*
  159. * Disable interrupts and switch to supervisor mode. This is
  160. * actually the same state as after hardware reset. We explicitly
  161. * set it here for debugging. It may not be possible to halt the
  162. * CPU at address zero immediately after reset. In this case we
  163. * can use the debugger to reset the PC to zero or to this address
  164. * and resume.
  165. */
  166. msr CPSR_c, #ARM_MODE_SVC | ARM_CPSR_I_BIT | ARM_CPSR_F_BIT
  167. /*
  168. * Move vectors from Flash to RAM.
  169. */
  170. mov r0, #0x00000000
  171. mov r1, #0x00300000
  172. ldmia r0!, {r2-r7}
  173. stmia r1!, {r2-r7}
  174. ldmia r0!, {r2-r7}
  175. stmia r1!, {r2-r7}
  176. b _init_rt
  177. /* Make sure that the literal pool is empty before the section ends. */
  178. .ltorg
  179. /*
  180. * User Init Section 1
  181. *
  182. * Additional code may be added here by placing functions in
  183. * section .init1.user. CPU is running in relocated Flash.
  184. */
  185. _init_rt:
  186. /*
  187. * Init Section 2: Set stack pointers and initialize C variables.
  188. */
  189. .section .init2,"ax",%progbits
  190. /*
  191. * Set stack pointer.
  192. * Nut/OS system and application are running in system mode.
  193. * Note, that we re-use the supervisory stack.
  194. */
  195. msr CPSR_c, #ARM_MODE_SYS | ARM_CPSR_I_BIT | ARM_CPSR_F_BIT
  196. ldr r0, =__stack
  197. mov sp, r0
  198. /*
  199. * Clear bss.
  200. */
  201. ldr r1, =__bss_start
  202. ldr r2, =__bss_end
  203. ldr r3, =0
  204. 1: cmp r1, r2
  205. strne r3, [r1], #+4
  206. bne 1b
  207. /*
  208. * Copy data and RAM functions from Flash to RAM.
  209. */
  210. ldr r0, =__data_load_start
  211. ldr r1, =__data_start
  212. ldr r2, =__data_end
  213. subs r2, r2, r1
  214. beq _init_hw
  215. 1: ldr r3, [r0], #4
  216. str r3, [r1], #4
  217. subs r2, r2, #4
  218. bne 1b
  219. /* Jump over the literal pool. */
  220. b _init_hw
  221. /* Make sure that the literal pool is empty before the section ends. */
  222. .ltorg
  223. /*
  224. * User Init Section 2
  225. *
  226. * Additional code may be added here by placing functions in
  227. * section .init2.user.
  228. *
  229. * This section may be used for early hardware initialization
  230. * routines written in C.
  231. */
  232. _init_hw:
  233. /*
  234. * Init Section 3: Hardware initialization.
  235. */
  236. .section .init3,"ax",%progbits
  237. /*
  238. * Enable all clocks.
  239. */
  240. b _enter_rtos
  241. _irq_dummy:
  242. b _irq_dummy
  243. /* Make sure that the literal pool is empty before the section ends. */
  244. .ltorg
  245. /*
  246. * User Init Section 3
  247. *
  248. * Additional code may be added here by placing functions in
  249. * section .init3.user.
  250. *
  251. * This section may be used for additional hardware initialization
  252. * routines written in C. Native interrupt routines may be used.
  253. */
  254. _enter_rtos:
  255. /*
  256. * Init Section 4: Enter system.
  257. */
  258. .section .init4,"ax",%progbits
  259. /* Jump to Nut/OS initialization. */
  260. ldr r0, =NutInit
  261. mov lr, pc
  262. bx r0
  263. /*
  264. * Exit Section 0: Endless loop.
  265. */
  266. .section .exit0,"ax",%progbits
  267. 1: b 1b
  268. /*
  269. * Stack Section.
  270. */
  271. .section .stack, "w", %nobits
  272. .space TOTAL_STACK_SIZE