1 | /* |
---|
2 | * This is the C entry point for ALL RTEMS BSPs. It is invoked |
---|
3 | * from the assembly language initialization file usually called |
---|
4 | * start.S. It provides the framework for the BSP initialization |
---|
5 | * sequence. The basic flow of initialization is: |
---|
6 | * |
---|
7 | * + start.S: basic CPU setup (stack, zero BSS) |
---|
8 | * + boot_card |
---|
9 | * + if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION) |
---|
10 | * - obtain information on BSP memory and allocate RTEMS Workspace |
---|
11 | * + bspstart.c: bsp_start - more advanced initialization |
---|
12 | * + rtems_initialize_data_structures |
---|
13 | * + if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION) |
---|
14 | * - Allocate memory to C Program Heap |
---|
15 | * - initialize C Library and C Program Heap |
---|
16 | * + bsp_pretasking_hook |
---|
17 | * + if defined(RTEMS_DEBUG) |
---|
18 | * - rtems_debug_enable( RTEMS_DEBUG_ALL_MASK ); |
---|
19 | * + rtems_initialize_before_drivers |
---|
20 | * + bsp_predriver_hook |
---|
21 | * + rtems_initialize_device_drivers |
---|
22 | * - all device drivers |
---|
23 | * + bsp_postdriver_hook |
---|
24 | * + rtems_initialize_start_multitasking |
---|
25 | * - 1st task executes C++ global constructors |
---|
26 | * .... appplication runs ... |
---|
27 | * - exit |
---|
28 | * + back to here eventually |
---|
29 | * + bspclean.c: bsp_cleanup |
---|
30 | * |
---|
31 | * This style of initialization ensures that the C++ global |
---|
32 | * constructors are executed after RTEMS is initialized. |
---|
33 | * Thanks to Chris Johns <cjohns@plessey.com.au> for the idea |
---|
34 | * to move C++ global constructors into the first task. |
---|
35 | * |
---|
36 | * COPYRIGHT (c) 1989-2008. |
---|
37 | * On-Line Applications Research Corporation (OAR). |
---|
38 | * |
---|
39 | * The license and distribution terms for this file may be |
---|
40 | * found in the file LICENSE in this distribution or at |
---|
41 | * http://www.rtems.com/license/LICENSE. |
---|
42 | * |
---|
43 | * $Id$ |
---|
44 | */ |
---|
45 | |
---|
46 | #include <stddef.h> |
---|
47 | #include <stdint.h> |
---|
48 | |
---|
49 | #include <rtems.h> |
---|
50 | |
---|
51 | #include <bspopts.h> /* for BSP_BOOTCARD_HANDLES_RAM_ALLOCATION */ |
---|
52 | |
---|
53 | /* |
---|
54 | * Since there is a forward reference |
---|
55 | */ |
---|
56 | char *rtems_progname; |
---|
57 | |
---|
58 | /* |
---|
59 | * Prototypes of external routines |
---|
60 | */ |
---|
61 | extern void bsp_start( void ); |
---|
62 | extern void bsp_cleanup( void ); |
---|
63 | extern void bsp_pretasking_hook(void); |
---|
64 | extern void bsp_libc_init( void *, uint32_t, int ); |
---|
65 | extern void bsp_predriver_hook(void); |
---|
66 | extern void bsp_postdriver_hook(void); |
---|
67 | |
---|
68 | /* |
---|
69 | * These are the prototypes and helper routines which are used |
---|
70 | * when the BSP lets the framework handle RAM allocation between |
---|
71 | * the RTEMS Workspace and C Program Heap. |
---|
72 | */ |
---|
73 | #if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION) |
---|
74 | extern void bsp_get_workarea( void **, size_t *, size_t *); |
---|
75 | |
---|
76 | void bootcard_bsp_libc_helper( |
---|
77 | void *workarea_base, |
---|
78 | size_t workarea_size, |
---|
79 | size_t requested_heap_size |
---|
80 | ) |
---|
81 | { |
---|
82 | uint32_t heap_start; |
---|
83 | uint32_t heap_size; |
---|
84 | |
---|
85 | heap_start = (uint32_t) workarea_base; |
---|
86 | if (heap_start & (CPU_ALIGNMENT-1)) |
---|
87 | heap_start = (heap_start + CPU_ALIGNMENT) & ~(CPU_ALIGNMENT-1); |
---|
88 | |
---|
89 | if ( requested_heap_size == 0 ) { |
---|
90 | heap_size = Configuration.work_space_start - workarea_base; |
---|
91 | heap_size &= 0xfffffff0; /* keep it as a multiple of 16 bytes */ |
---|
92 | } else { |
---|
93 | heap_size = requested_heap_size; |
---|
94 | } |
---|
95 | |
---|
96 | bsp_libc_init((void *) heap_start, heap_size, 0); |
---|
97 | } |
---|
98 | #endif |
---|
99 | |
---|
100 | /* |
---|
101 | * This is the initialization framework routine that weaves together |
---|
102 | * calls to RTEMS and the BSP in the proper sequence to initialize |
---|
103 | * the system while maximizing shared code and keeping BSP code in C |
---|
104 | * as much as possible. |
---|
105 | */ |
---|
106 | int boot_card( |
---|
107 | int argc, |
---|
108 | char **argv, |
---|
109 | char **envp |
---|
110 | ) |
---|
111 | { |
---|
112 | static char *argv_pointer = NULL; |
---|
113 | static char *envp_pointer = NULL; |
---|
114 | char **argv_p = &argv_pointer; |
---|
115 | char **envp_p = &envp_pointer; |
---|
116 | rtems_interrupt_level bsp_isr_level; |
---|
117 | #if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION) |
---|
118 | void *workarea_base; |
---|
119 | size_t workarea_size; |
---|
120 | size_t heap_size; |
---|
121 | #endif |
---|
122 | |
---|
123 | /* |
---|
124 | * Special case for PowerPC: The interrupt disable mask is stored in SPRG0. |
---|
125 | * It must be valid before we can use rtems_interrupt_disable(). |
---|
126 | */ |
---|
127 | #ifdef PPC_INTERRUPT_DISABLE_MASK_DEFAULT |
---|
128 | ppc_interrupt_set_disable_mask( PPC_INTERRUPT_DISABLE_MASK_DEFAULT ); |
---|
129 | #endif /* PPC_INTERRUPT_DISABLE_MASK_DEFAULT */ |
---|
130 | |
---|
131 | /* |
---|
132 | * Make sure interrupts are disabled. |
---|
133 | */ |
---|
134 | rtems_interrupt_disable( bsp_isr_level ); |
---|
135 | |
---|
136 | /* |
---|
137 | * Set things up so we have real pointers for argv and envp. |
---|
138 | * If the BSP has passed us something useful, then pass it on. |
---|
139 | * Somehow we need to eventually make this available to |
---|
140 | * a real main() in user land. :) |
---|
141 | */ |
---|
142 | if ( argv ) argv_p = argv; |
---|
143 | if ( envp ) envp_p = envp; |
---|
144 | |
---|
145 | /* |
---|
146 | * Set the program name in case some application cares. |
---|
147 | */ |
---|
148 | if ((argc > 0) && argv && argv[0]) |
---|
149 | rtems_progname = argv[0]; |
---|
150 | else |
---|
151 | rtems_progname = "RTEMS"; |
---|
152 | |
---|
153 | /* |
---|
154 | * Find out where the block of memory the BSP will use for |
---|
155 | * the RTEMS Workspace and the C Program Heap is. |
---|
156 | */ |
---|
157 | #if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION) |
---|
158 | { |
---|
159 | unsigned char *work_space_start; |
---|
160 | |
---|
161 | bsp_get_workarea( &workarea_base, &workarea_size, &heap_size ); |
---|
162 | |
---|
163 | work_space_start = workarea_base + workarea_size |
---|
164 | - rtems_configuration_get_work_space_size(); |
---|
165 | |
---|
166 | if ( work_space_start <= (unsigned char *)workarea_base ) { |
---|
167 | printk( "bootcard: Not enough RAM!!!\n" ); |
---|
168 | bsp_cleanup(); |
---|
169 | return -1; |
---|
170 | } |
---|
171 | |
---|
172 | Configuration.work_space_start = work_space_start; |
---|
173 | |
---|
174 | #if (BSP_DIRTY_MEMORY == 1) |
---|
175 | memset(workarea_base, 0xCF, workarea_size); |
---|
176 | #endif |
---|
177 | } |
---|
178 | |
---|
179 | #endif |
---|
180 | |
---|
181 | /* |
---|
182 | * Invoke Board Support Package initialization routine written in C. |
---|
183 | */ |
---|
184 | bsp_start(); |
---|
185 | |
---|
186 | /* |
---|
187 | * Initialize RTEMS data structures |
---|
188 | */ |
---|
189 | rtems_initialize_data_structures( &Configuration ); |
---|
190 | |
---|
191 | /* |
---|
192 | * Initialize the C library for those BSPs using the shared |
---|
193 | * framework. |
---|
194 | */ |
---|
195 | #if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION) |
---|
196 | bootcard_bsp_libc_helper( workarea_base, workarea_size, heap_size ); |
---|
197 | #endif |
---|
198 | |
---|
199 | /* |
---|
200 | * All BSP to do any required initialization now that RTEMS |
---|
201 | * data structures are initialized. In older BSPs or those |
---|
202 | * which do not use the shared framework, this is the typical |
---|
203 | * time when the C Library is initialized so malloc() |
---|
204 | * can be called by device drivers. For BSPs using the shared |
---|
205 | * framework, this routine can be empty. |
---|
206 | */ |
---|
207 | bsp_pretasking_hook(); |
---|
208 | |
---|
209 | /* |
---|
210 | * If debug is enabled, then enable all dynamic RTEMS debug |
---|
211 | * capabilities. |
---|
212 | * |
---|
213 | * NOTE: Most debug features are conditionally compiled in |
---|
214 | * or enabled via configure time plugins. |
---|
215 | */ |
---|
216 | #ifdef RTEMS_DEBUG |
---|
217 | rtems_debug_enable( RTEMS_DEBUG_ALL_MASK ); |
---|
218 | #endif |
---|
219 | |
---|
220 | /* |
---|
221 | * Let RTEMS perform initialization it requires before drivers |
---|
222 | * are allowed to be initialized. |
---|
223 | */ |
---|
224 | rtems_initialize_before_drivers(); |
---|
225 | |
---|
226 | /* |
---|
227 | * Execute BSP specific pre-driver hook. Drivers haven't gotten |
---|
228 | * to initialize yet so this is a good chance to initialize |
---|
229 | * buses, spurious interrupt handlers, etc.. |
---|
230 | * |
---|
231 | * NOTE: Many BSPs do not require this handler and use the |
---|
232 | * shared stub. |
---|
233 | */ |
---|
234 | bsp_predriver_hook(); |
---|
235 | |
---|
236 | /* |
---|
237 | * Initialize all device drivers. |
---|
238 | */ |
---|
239 | rtems_initialize_device_drivers(); |
---|
240 | |
---|
241 | /* |
---|
242 | * Invoke the postdriver hook. This normally opens /dev/console |
---|
243 | * for use as stdin, stdout, and stderr. |
---|
244 | */ |
---|
245 | bsp_postdriver_hook(); |
---|
246 | |
---|
247 | /* |
---|
248 | * Complete initialization of RTEMS and switch to the first task. |
---|
249 | * Global C++ constructors will be executed in the context of that task. |
---|
250 | */ |
---|
251 | rtems_initialize_start_multitasking(); |
---|
252 | |
---|
253 | /*************************************************************** |
---|
254 | *************************************************************** |
---|
255 | * APPLICATION RUNS HERE!!! When it shuts down, we return!!! * |
---|
256 | *************************************************************** |
---|
257 | *************************************************************** |
---|
258 | */ |
---|
259 | |
---|
260 | /* |
---|
261 | * Perform any BSP specific shutdown actions which are written in C. |
---|
262 | */ |
---|
263 | bsp_cleanup(); |
---|
264 | |
---|
265 | /* |
---|
266 | * Now return to the start code. |
---|
267 | */ |
---|
268 | |
---|
269 | return 0; |
---|
270 | } |
---|