Initial cut of the open ath9k htc firmware.
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / build / magpie_1_1 / sboot / athos / src / xtos / crt0-app.S
1 // crt0-app.S
2 // Applications downloaded in RAM using a debug monitor (eg. XMON, RedBoot)
3 // start here at _app_reset.  Such applications don't have any vectors:
4 // all exceptions are handled by the debug monitor.
5 // Thus, this file essentially plays the role of the reset vector
6 // to setup a few things before jumping to _start (in crt1*.S).
7
8 // Copyright (c) 2005-2010 Tensilica Inc.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included
19 // in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 #include <xtensa/cacheattrasm.h>
30 #include <xtensa/xt2000.h>
31 #include <xtensa/coreasm.h>
32
33
34 //  Assumptions on entry to _app_reset:
35 //      - debug monitor handles all exceptions, has setup all vectors
36 //      - interrupt related state is initialized
37 //        (interrupts disabled or enabled for debug monitor's purposes)
38 //      - debug option related state is initialized (for debug monitor)
39 //      - any MMU related state is initialized (all handled by debug monitor)
40 //      - caches are initialized (cache attributes not necessarily correct)
41 //      - entire application is loaded (no unpacking needed here)
42
43 //  Assumptions on exit from _app_reset, ie. when jumping to _start:
44 //      - low (level-one) and medium priority interrupts are disabled
45 //      - C calling context not initialized:
46 //        - PS not fully initialized (eg. PS.WOE not set per ABI)
47 //        - SP not initialized
48 //      - the following are initialized:
49 //        - LITBASE, WindowBase, WindowStart, LCOUNT, CPENABLE, FP's FCR and FSR,
50 //          cache attributes
51
52 /**************************************************************************/
53
54         .text
55         .global _app_reset
56 _app_reset:
57         /* _app_reset may be required to be located at the beginning of the text
58            segment.  However, the constant pool for _app_reset must be placed
59            before the code.  Jump over the constant pool to solve this. */
60         j       .LpastInitialConstants
61
62         .literal_position       // tells the assembler/linker to place literals here
63
64 .LpastInitialConstants:
65         // Keep a0 zero.  It is used to initialize a few things.
66         // It is also the return address, where zero indicates
67         // that the frame used by _start is the bottommost frame.
68         //
69         movi    a0, 0           // keep this register zero.
70
71 #if XCHAL_HAVE_LOOPS
72         wsr     a0, LCOUNT              // loop count = 0
73 #endif /* XCHAL_HAVE_LOOPS */
74
75         // Interrupts might be enabled, make sure at least medium and low priority
76         // interrupts are disabled until WindowBase, WindowStart, SP, and the stack
77         // are all properly setup (which will happen outside this file, after the
78         // _start label).  We leave loops enabled on new exception architecture.
79 #if XCHAL_HAVE_EXCEPTIONS
80         movi    a2, XCHAL_EXCM_LEVEL
81         wsr     a2, PS          // set PS.INTLEVEL=EXCM_LEVEL, PS.WOE=0, PS.EXCM=0
82         rsync
83 #endif
84
85         // DO THIS FIRST: initialize the window start and base
86         // before, so that windows don't move under us.
87 #if XCHAL_HAVE_WINDOWED
88         // We do this even if we are assembling for the
89         // call0 abi, but it's not really needed.
90         movi    a2, 1
91         wsr     a2, WINDOWSTART         // window start = 1
92         wsr     a0, WINDOWBASE          // window base = 0
93         rsync
94
95         // NOTE:  a0 may no longer be zero here, because
96         // we wrote to WindowBase.  So clear it again.
97         movi    a0, 0
98 #endif
99
100         // Now, BEFORE we do any L32R (or MOVI with non-immediate
101         // range which results in an L32R), ensure LITBASE is set
102         // correctly.  This is necessary for RAM applications loaded
103         // using a target-side debug monitor -- such applications
104         // don't have a reset vector and start execution at _start.
105         // (This part is unnecessary if running from a reset vector.)
106         // The target-side debug monitor might have set LITBASE to
107         // anything at all, so we cannot rely on its value here.
108 #if XCHAL_HAVE_ABSOLUTE_LITERALS
109         wsr     a0, LITBASE             // force PC-relative L32R
110         rsync
111 # if XSHAL_USE_ABSOLUTE_LITERALS
112         .begin  no-absolute-literals            // use PC-rel L32R to load
113         movi    a2, _lit4_start + 0x40001       // address of absolute literals
114         .end    no-absolute-literals            // (see handlers/ResetVector.S
115         wsr     a2, LITBASE                     //  for explanation)
116         rsync
117 # endif
118 #endif
119
120
121         /*
122          *  Enable the caches correctly for this board.
123          *  We use the "safe" sequence.
124          *
125          *  NOTE:  We don't *initialize* the caches here, because the
126          *  target debugger agent (debug monitor) has initialized them for us.
127          */
128         movi    a2, XTBOARD_CACHEATTR_DEFAULT
129         cacheattr_set                           /* set CACHEATTR from a2 (clobbers a3-a8) */
130
131
132
133         // Coprocessor option initialization
134 #if XCHAL_HAVE_CP
135         //movi  a2, XCHAL_CP_MASK       // enable existing CPs
136         //  To allow creating new coprocessors using TC that are not known
137         //  at GUI build time without having to explicitly enable them,
138         //  all CPENABLE bits must be set, even though they may not always
139         //  correspond to a coprocessor.
140         movi    a2, 0xFF        // enable *all* bits, to allow dynamic TIE
141         wsr     a2, CPENABLE
142 #endif
143
144         // Floating point coprocessor option initialization
145 #if XCHAL_HAVE_FP
146 # define FCR    232     /* floating-point control register (user register number) */
147 # define FSR    233     /* floating-point status register (user register number) */
148         rsync           /* wait for WSR to CPENABLE to complete before accessing FP coproc state */
149         wur     a0, FCR /* clear FCR (default rounding mode, round-nearest) */
150         wur     a0, FSR /* clear FSR */
151 #endif
152
153
154         /*  NOTE:  Future releases may clear BSS here rather than in the CRT1.  */
155
156
157         /*
158          *  Now jump to the application.  This is typically the
159          *  C run-time initialization ("CRT") which in turn calls main():
160          */
161         movi    a4, _start
162         jx      a4              // jump to _start
163
164         .size   _app_reset, . - _app_reset
165