[c8d9183] | 1 | /* |
---|
[66d973ff] | 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 |
---|
[ec3007c] | 9 | * + if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION) |
---|
| 10 | * - obtain information on BSP memory and allocate RTEMS Workspace |
---|
[66d973ff] | 11 | * + bspstart.c: bsp_start - more advanced initialization |
---|
[3820ff24] | 12 | * + rtems_initialize_data_structures |
---|
[ec3007c] | 13 | * + if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION) |
---|
| 14 | * - Allocate memory to C Program Heap |
---|
| 15 | * - initialize C Library and C Program Heap |
---|
[3820ff24] | 16 | * + bsp_pretasking_hook |
---|
[ec3007c] | 17 | * + if defined(RTEMS_DEBUG) |
---|
| 18 | * - rtems_debug_enable( RTEMS_DEBUG_ALL_MASK ); |
---|
[3820ff24] | 19 | * + rtems_initialize_before_drivers |
---|
| 20 | * + bsp_predriver_hook |
---|
| 21 | * + rtems_initialize_device_drivers |
---|
[ec3007c] | 22 | * - all device drivers |
---|
[3820ff24] | 23 | * + bsp_postdriver_hook |
---|
| 24 | * + rtems_initialize_start_multitasking |
---|
[ec3007c] | 25 | * - 1st task executes C++ global constructors |
---|
[3820ff24] | 26 | * .... appplication runs ... |
---|
[ec3007c] | 27 | * - exit |
---|
[66d973ff] | 28 | * + back to here eventually |
---|
| 29 | * + bspclean.c: bsp_cleanup |
---|
[c8d9183] | 30 | * |
---|
[af043e13] | 31 | * This style of initialization ensures that the C++ global |
---|
[c8d9183] | 32 | * constructors are executed after RTEMS is initialized. |
---|
[af043e13] | 33 | * Thanks to Chris Johns <cjohns@plessey.com.au> for the idea |
---|
| 34 | * to move C++ global constructors into the first task. |
---|
[c8d9183] | 35 | * |
---|
[ec3007c] | 36 | * COPYRIGHT (c) 1989-2008. |
---|
[c8d9183] | 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 |
---|
[7050ec70] | 41 | * http://www.rtems.com/license/LICENSE. |
---|
[c8d9183] | 42 | * |
---|
| 43 | * $Id$ |
---|
| 44 | */ |
---|
| 45 | |
---|
[a86f3aac] | 46 | #include <stddef.h> |
---|
| 47 | #include <stdint.h> |
---|
| 48 | |
---|
| 49 | #include <rtems.h> |
---|
[c8d9183] | 50 | |
---|
[fa379b0] | 51 | #include <bspopts.h> /* for BSP_BOOTCARD_HANDLES_RAM_ALLOCATION */ |
---|
| 52 | |
---|
[ec3007c] | 53 | /* |
---|
| 54 | * Since there is a forward reference |
---|
| 55 | */ |
---|
| 56 | char *rtems_progname; |
---|
| 57 | |
---|
| 58 | /* |
---|
| 59 | * Prototypes of external routines |
---|
| 60 | */ |
---|
[c8d9183] | 61 | extern void bsp_start( void ); |
---|
| 62 | extern void bsp_cleanup( void ); |
---|
[3820ff24] | 63 | extern void bsp_pretasking_hook(void); |
---|
[ec3007c] | 64 | extern void bsp_libc_init( void *, uint32_t, int ); |
---|
[3820ff24] | 65 | extern void bsp_predriver_hook(void); |
---|
| 66 | extern void bsp_postdriver_hook(void); |
---|
[c8d9183] | 67 | |
---|
| 68 | /* |
---|
[ec3007c] | 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. |
---|
[c8d9183] | 72 | */ |
---|
[ec3007c] | 73 | #if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION) |
---|
| 74 | extern void bsp_get_workarea( void **, size_t *, size_t *); |
---|
[c8d9183] | 75 | |
---|
[ec3007c] | 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 | } |
---|
[c8d9183] | 95 | |
---|
[ec3007c] | 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 | */ |
---|
[7492598] | 106 | int boot_card( |
---|
| 107 | int argc, |
---|
| 108 | char **argv, |
---|
| 109 | char **envp |
---|
| 110 | ) |
---|
[c8d9183] | 111 | { |
---|
[67d0f4e] | 112 | static char *argv_pointer = NULL; |
---|
| 113 | static char *envp_pointer = NULL; |
---|
| 114 | char **argv_p = &argv_pointer; |
---|
| 115 | char **envp_p = &envp_pointer; |
---|
[4e9893b1] | 116 | rtems_interrupt_level bsp_isr_level; |
---|
[ec3007c] | 117 | #if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION) |
---|
| 118 | void *workarea_base; |
---|
| 119 | size_t workarea_size; |
---|
| 120 | size_t heap_size; |
---|
| 121 | #endif |
---|
[4e9893b1] | 122 | |
---|
[a86f3aac] | 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 |
---|
[18e6e824] | 128 | ppc_interrupt_set_disable_mask( PPC_INTERRUPT_DISABLE_MASK_DEFAULT ); |
---|
[a86f3aac] | 129 | #endif /* PPC_INTERRUPT_DISABLE_MASK_DEFAULT */ |
---|
| 130 | |
---|
[4e9893b1] | 131 | /* |
---|
| 132 | * Make sure interrupts are disabled. |
---|
| 133 | */ |
---|
| 134 | rtems_interrupt_disable( bsp_isr_level ); |
---|
[67d0f4e] | 135 | |
---|
| 136 | /* |
---|
[7492598] | 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 |
---|
[af536c96] | 140 | * a real main() in user land. :) |
---|
[67d0f4e] | 141 | */ |
---|
[3820ff24] | 142 | if ( argv ) argv_p = argv; |
---|
| 143 | if ( envp ) envp_p = envp; |
---|
[67d0f4e] | 144 | |
---|
[af043e13] | 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 | |
---|
[ec3007c] | 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 | |
---|
[c8d9183] | 181 | /* |
---|
[9f59157] | 182 | * Invoke Board Support Package initialization routine written in C. |
---|
[c8d9183] | 183 | */ |
---|
| 184 | bsp_start(); |
---|
| 185 | |
---|
| 186 | /* |
---|
[3820ff24] | 187 | * Initialize RTEMS data structures |
---|
[c8d9183] | 188 | */ |
---|
[3820ff24] | 189 | rtems_initialize_data_structures( &Configuration ); |
---|
[c8d9183] | 190 | |
---|
[ec3007c] | 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 | |
---|
[3820ff24] | 199 | /* |
---|
| 200 | * All BSP to do any required initialization now that RTEMS |
---|
[ec3007c] | 201 | * data structures are initialized. In older BSPs or those |
---|
| 202 | * which do not use the shared framework, this is the typical |
---|
[3820ff24] | 203 | * time when the C Library is initialized so malloc() |
---|
[ec3007c] | 204 | * can be called by device drivers. For BSPs using the shared |
---|
| 205 | * framework, this routine can be empty. |
---|
[3820ff24] | 206 | */ |
---|
| 207 | bsp_pretasking_hook(); |
---|
| 208 | |
---|
[ec3007c] | 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 |
---|
[3820ff24] | 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(); |
---|
[c8d9183] | 246 | |
---|
[66d973ff] | 247 | /* |
---|
[af043e13] | 248 | * Complete initialization of RTEMS and switch to the first task. |
---|
| 249 | * Global C++ constructors will be executed in the context of that task. |
---|
[66d973ff] | 250 | */ |
---|
[3820ff24] | 251 | rtems_initialize_start_multitasking(); |
---|
[c8d9183] | 252 | |
---|
[4c3d3cdb] | 253 | /*************************************************************** |
---|
| 254 | *************************************************************** |
---|
| 255 | * APPLICATION RUNS HERE!!! When it shuts down, we return!!! * |
---|
| 256 | *************************************************************** |
---|
| 257 | *************************************************************** |
---|
| 258 | */ |
---|
| 259 | |
---|
[c8d9183] | 260 | /* |
---|
[af043e13] | 261 | * Perform any BSP specific shutdown actions which are written in C. |
---|
[c8d9183] | 262 | */ |
---|
[6128a4a] | 263 | bsp_cleanup(); |
---|
[c8d9183] | 264 | |
---|
| 265 | /* |
---|
| 266 | * Now return to the start code. |
---|
| 267 | */ |
---|
| 268 | |
---|
[460cd5b7] | 269 | return 0; |
---|
[c8d9183] | 270 | } |
---|