[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 <rtems.h> |
---|
[c8d9183] | 47 | |
---|
[d4886a06] | 48 | #include <bsp/bootcard.h> |
---|
[5ab278f] | 49 | #include <rtems/bspIo.h> |
---|
[fa379b0] | 50 | |
---|
[ec3007c] | 51 | /* |
---|
[2549b4d] | 52 | * At most a single pointer to the cmdline for those target |
---|
| 53 | * short on memory and not supporting a command line. |
---|
[ec3007c] | 54 | */ |
---|
[2549b4d] | 55 | const char *bsp_boot_cmdline; |
---|
[ec3007c] | 56 | |
---|
[1986152] | 57 | /* |
---|
| 58 | * Are we using a single heap for the RTEMS Workspace and C Program Heap? |
---|
| 59 | */ |
---|
| 60 | extern bool rtems_unified_work_area; |
---|
| 61 | |
---|
[c8d9183] | 62 | /* |
---|
[ec3007c] | 63 | * These are the prototypes and helper routines which are used |
---|
| 64 | * when the BSP lets the framework handle RAM allocation between |
---|
| 65 | * the RTEMS Workspace and C Program Heap. |
---|
[c8d9183] | 66 | */ |
---|
[0de9fdf] | 67 | static rtems_status_code bootcard_bsp_libc_helper( |
---|
[61814f9] | 68 | void *work_area_start, |
---|
[71d0488b] | 69 | intptr_t work_area_size, |
---|
[61814f9] | 70 | void *heap_start, |
---|
[71d0488b] | 71 | intptr_t heap_size |
---|
[0de9fdf] | 72 | ) |
---|
| 73 | { |
---|
[71d0488b] | 74 | intptr_t heap_size_default = 0; |
---|
[0de9fdf] | 75 | |
---|
| 76 | if ( !rtems_unified_work_area && |
---|
| 77 | heap_start == BSP_BOOTCARD_HEAP_USES_WORK_AREA) { |
---|
[96f2d87] | 78 | /* Place the heap immediately following the work area */ |
---|
| 79 | heap_start = work_area_start + rtems_configuration_get_work_space_size(); |
---|
[0de9fdf] | 80 | |
---|
| 81 | /* Ensure proper alignement */ |
---|
| 82 | if ((uintptr_t) heap_start & (CPU_ALIGNMENT - 1)) { |
---|
| 83 | heap_start = (void *) (((uintptr_t) heap_start + CPU_ALIGNMENT) |
---|
| 84 | & ~(CPU_ALIGNMENT - 1)); |
---|
[ec3007c] | 85 | } |
---|
[c8d9183] | 86 | |
---|
[0de9fdf] | 87 | /* |
---|
[96f2d87] | 88 | * For the default heap size use the free space from the end of the |
---|
| 89 | * work space up to the end of the work area as heap. |
---|
[0de9fdf] | 90 | */ |
---|
[f1e8903] | 91 | heap_size_default = work_area_size - |
---|
| 92 | rtems_configuration_get_work_space_size(); |
---|
[0de9fdf] | 93 | |
---|
| 94 | /* Keep it as a multiple of 16 bytes */ |
---|
[71d0488b] | 95 | heap_size_default &= ~((intptr_t) 0xf); |
---|
[cc54cc9] | 96 | |
---|
[0de9fdf] | 97 | /* Use default heap size if requested */ |
---|
| 98 | if (heap_size == BSP_BOOTCARD_HEAP_SIZE_DEFAULT) { |
---|
| 99 | heap_size = heap_size_default; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | /* Check heap size */ |
---|
| 103 | if (heap_size > heap_size_default) { |
---|
| 104 | return RTEMS_INVALID_SIZE; |
---|
| 105 | } |
---|
[ec3007c] | 106 | } |
---|
[0de9fdf] | 107 | |
---|
| 108 | bsp_libc_init(heap_start, heap_size, 0); |
---|
| 109 | |
---|
| 110 | return RTEMS_SUCCESSFUL; |
---|
| 111 | } |
---|
[ec3007c] | 112 | |
---|
| 113 | /* |
---|
| 114 | * This is the initialization framework routine that weaves together |
---|
| 115 | * calls to RTEMS and the BSP in the proper sequence to initialize |
---|
| 116 | * the system while maximizing shared code and keeping BSP code in C |
---|
| 117 | * as much as possible. |
---|
| 118 | */ |
---|
[7492598] | 119 | int boot_card( |
---|
[2549b4d] | 120 | const char *cmdline |
---|
[7492598] | 121 | ) |
---|
[c8d9183] | 122 | { |
---|
[2549b4d] | 123 | rtems_interrupt_level bsp_isr_level; |
---|
| 124 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
| 125 | void *work_area_start = NULL; |
---|
[5ab278f] | 126 | uintptr_t work_area_size = 0; |
---|
[2549b4d] | 127 | void *heap_start = NULL; |
---|
[5ab278f] | 128 | uintptr_t heap_size = 0; |
---|
[4e9893b1] | 129 | |
---|
[a86f3aac] | 130 | /* |
---|
| 131 | * Special case for PowerPC: The interrupt disable mask is stored in SPRG0. |
---|
| 132 | * It must be valid before we can use rtems_interrupt_disable(). |
---|
| 133 | */ |
---|
| 134 | #ifdef PPC_INTERRUPT_DISABLE_MASK_DEFAULT |
---|
[18e6e824] | 135 | ppc_interrupt_set_disable_mask( PPC_INTERRUPT_DISABLE_MASK_DEFAULT ); |
---|
[a86f3aac] | 136 | #endif /* PPC_INTERRUPT_DISABLE_MASK_DEFAULT */ |
---|
| 137 | |
---|
[4e9893b1] | 138 | /* |
---|
| 139 | * Make sure interrupts are disabled. |
---|
| 140 | */ |
---|
| 141 | rtems_interrupt_disable( bsp_isr_level ); |
---|
[67d0f4e] | 142 | |
---|
[2549b4d] | 143 | bsp_boot_cmdline = cmdline; |
---|
[af043e13] | 144 | |
---|
[4e1cc17] | 145 | /* |
---|
| 146 | * Invoke Board Support Package initialization routine written in C. |
---|
| 147 | */ |
---|
| 148 | bsp_start(); |
---|
| 149 | |
---|
[ec3007c] | 150 | /* |
---|
| 151 | * Find out where the block of memory the BSP will use for |
---|
| 152 | * the RTEMS Workspace and the C Program Heap is. |
---|
| 153 | */ |
---|
[5ab278f] | 154 | bsp_get_work_area(&work_area_start, &work_area_size, |
---|
| 155 | &heap_start, &heap_size); |
---|
[0de9fdf] | 156 | |
---|
[5ab278f] | 157 | if ( (uint32_t) work_area_size <= (uint32_t) Configuration.work_space_size ) { |
---|
[dce79aee] | 158 | printk( |
---|
[5ab278f] | 159 | "bootcard: Work space too big for work area! (0x%08lx > 0x%08lx)\n", |
---|
| 160 | (uint32_t)Configuration.work_space_size, |
---|
| 161 | (uint32_t)work_area_size |
---|
[dce79aee] | 162 | ); |
---|
[0de9fdf] | 163 | bsp_cleanup(); |
---|
| 164 | return -1; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | if ( rtems_unified_work_area ) { |
---|
| 168 | Configuration.work_space_start = work_area_start; |
---|
| 169 | Configuration.work_space_size = work_area_size; |
---|
| 170 | } else { |
---|
[96f2d87] | 171 | Configuration.work_space_start = (char *) work_area_start; |
---|
[0de9fdf] | 172 | } |
---|
| 173 | |
---|
| 174 | #if (BSP_DIRTY_MEMORY == 1) |
---|
| 175 | memset( work_area_start, 0xCF, work_area_size ); |
---|
[ec3007c] | 176 | #endif |
---|
| 177 | |
---|
[c8d9183] | 178 | /* |
---|
[3820ff24] | 179 | * Initialize RTEMS data structures |
---|
[c8d9183] | 180 | */ |
---|
[71d0488b] | 181 | rtems_initialize_data_structures(); |
---|
[c8d9183] | 182 | |
---|
[ec3007c] | 183 | /* |
---|
| 184 | * Initialize the C library for those BSPs using the shared |
---|
| 185 | * framework. |
---|
| 186 | */ |
---|
[0de9fdf] | 187 | sc = bootcard_bsp_libc_helper( |
---|
| 188 | work_area_start, |
---|
| 189 | work_area_size, |
---|
| 190 | heap_start, |
---|
| 191 | heap_size |
---|
| 192 | ); |
---|
| 193 | if ( sc != RTEMS_SUCCESSFUL ) { |
---|
| 194 | printk( "bootcard: Cannot initialize C library!\n"); |
---|
| 195 | bsp_cleanup(); |
---|
| 196 | return -1; |
---|
| 197 | } |
---|
[ec3007c] | 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 | */ |
---|
[460cd5b7] | 268 | return 0; |
---|
[c8d9183] | 269 | } |
---|