source: rtems/c/src/lib/libbsp/shared/bootcard.c @ 49c8f45

4.104.115
Last change on this file since 49c8f45 was f1e8903, checked in by Joel Sherrill <joel.sherrill@…>, on 08/28/09 at 18:24:10

2009-08-28 Joel Sherrill <joel.sherrill@…>

  • bootcard.c, bsplibc.c, clockdrv_shell.h, console-polled.c: Fix formatting.
  • Property mode set to 100644
File size: 7.8 KB
Line 
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 <rtems.h>
47
48#include <bsp/bootcard.h>
49#include <rtems/bspIo.h>
50
51/*
52 *  At most a single pointer to the cmdline for those target
53 *  short on memory and not supporting a command line.
54 */
55const char *bsp_boot_cmdline;
56
57/*
58 * Are we using a single heap for the RTEMS Workspace and C Program Heap?
59 */
60extern bool rtems_unified_work_area;
61
62/*
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.
66 */
67static rtems_status_code bootcard_bsp_libc_helper(
68  void    *work_area_start,
69  intptr_t work_area_size,
70  void    *heap_start,
71  intptr_t heap_size
72)
73{
74  intptr_t heap_size_default = 0;
75
76  if ( !rtems_unified_work_area &&
77       heap_start == BSP_BOOTCARD_HEAP_USES_WORK_AREA) {
78    /* Place the heap immediately following the work area */
79    heap_start = work_area_start + rtems_configuration_get_work_space_size();
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));
85    }
86
87    /*
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.
90     */
91    heap_size_default = work_area_size -
92        rtems_configuration_get_work_space_size();
93
94    /* Keep it as a multiple of 16 bytes */
95    heap_size_default &= ~((intptr_t) 0xf);
96
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    }
106  }
107
108  bsp_libc_init(heap_start, heap_size, 0);
109
110  return RTEMS_SUCCESSFUL;
111}
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 */
119int boot_card(
120  const char *cmdline
121)
122{
123  rtems_interrupt_level  bsp_isr_level;
124  rtems_status_code      sc = RTEMS_SUCCESSFUL;
125  void                  *work_area_start = NULL;
126  uintptr_t              work_area_size = 0;
127  void                  *heap_start = NULL;
128  uintptr_t              heap_size = 0;
129
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
135    ppc_interrupt_set_disable_mask( PPC_INTERRUPT_DISABLE_MASK_DEFAULT );
136  #endif /* PPC_INTERRUPT_DISABLE_MASK_DEFAULT */
137
138  /*
139   *  Make sure interrupts are disabled.
140   */
141  rtems_interrupt_disable( bsp_isr_level );
142
143  bsp_boot_cmdline = cmdline;
144
145  /*
146   * Invoke Board Support Package initialization routine written in C.
147   */
148  bsp_start();
149
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   */
154  bsp_get_work_area(&work_area_start, &work_area_size,
155                    &heap_start, &heap_size);
156
157  if ( (uint32_t) work_area_size <= (uint32_t) Configuration.work_space_size ) {
158    printk(
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
162    );
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 {
171    Configuration.work_space_start = (char *) work_area_start;
172  }
173
174  #if (BSP_DIRTY_MEMORY == 1)
175    memset( work_area_start, 0xCF,  work_area_size );
176  #endif
177
178  /*
179   *  Initialize RTEMS data structures
180   */
181  rtems_initialize_data_structures();
182
183  /*
184   *  Initialize the C library for those BSPs using the shared
185   *  framework.
186   */
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  }
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  return 0;
269}
Note: See TracBrowser for help on using the repository browser.