source: rtems/c/src/lib/libbsp/arm/csb336/startup/bspstart.c @ 6ea100c1

4.104.114.95
Last change on this file since 6ea100c1 was 6ea100c1, checked in by Joel Sherrill <joel.sherrill@…>, on 05/12/08 at 18:43:55

2008-05-12 Joel Sherrill <joel.sherrill@…>

  • startup/bspstart.c: Refactored and renamed initialization routines to rtems_initialize_data_structures, rtems_initialize_before_drivers, rtems_initialize_device_drivers, and rtems_initialize_start_multitasking. This opened the sequence up so that bootcard() could provide a more robust and flexible framework which is easier to explain and understand. This also lays the groundwork for sharing the division of available memory between the RTEMS workspace and heap and the C library initialization across all BSPs.
  • Property mode set to 100644
File size: 7.2 KB
Line 
1/*
2 * Cogent CSB336 - MC9328MXL SBC startup code
3 *
4 * Copyright (c) 2004 by Cogent Computer Systems
5 * Written by Jay Monkman <jtm@lopingdog.com>
6 *     
7 * The license and distribution terms for this file may be
8 * found in the file LICENSE in this distribution or at
9 *
10 * http://www.rtems.com/license/LICENSE.
11 *
12 *  $Id$
13 */
14#include <bsp.h>
15#include <rtems/libcsupport.h>
16#include <rtems/bspIo.h>
17#include <rtems/libio.h>
18#include <mc9328mxl.h>
19
20/* Global Variables */
21extern void            *_flash_size;
22extern void            *_flash_base;
23extern void            *_sdram_size;
24extern void            *_sdram_base;
25extern void            *_bss_free_start;
26
27unsigned long           free_mem_start;
28unsigned long           free_mem_end;
29
30extern void rtems_irq_mngt_init(void);
31void bsp_libc_init( void *, uint32_t, int );
32
33/**************************************************************************/
34/*                                                                        */
35/* NAME: bsp_pretasking_hook - Function to setup system before startup    */
36/*                                                                        */
37/* DESCRIPTION:                                                           */
38/*   This function is called before drivers are initialized and used      */
39/*   to setup libc and BSP extensions.                                    */
40/*                                                                        */
41/* RESTRICTIONS/LIMITATIONS:                                              */
42/*   Since this function is setting up libc, it cannot use and libc       */
43/*   functions.                                                           */
44/*                                                                        */
45/**************************************************************************/
46void bsp_pretasking_hook(void)
47{
48    uint32_t heap_start;
49    uint32_t heap_size;
50
51    /*
52     * Set up the heap.
53     */
54    heap_start =  free_mem_start;
55    heap_size = free_mem_end - free_mem_start;
56
57    /* call rtems lib init - malloc stuff */
58    bsp_libc_init((void *)heap_start, heap_size, 0);
59
60#ifdef RTEMS_DEBUG
61
62    rtems_debug_enable(RTEMS_DEBUG_ALL_MASK);
63
64#endif /* RTEMS_DEBUG */
65
66}
67 
68
69/**************************************************************************/
70/*                                                                        */
71/* NAME: bsp_start_default - BSP initialization function                  */
72/*                                                                        */
73/* DESCRIPTION:                                                           */
74/*   This function is called before RTEMS is initialized and used         */
75/*   adjust the kernel's configuration.                                   */
76/*                                                                        */
77/*   This function also configures the CPU's memory protection unit.      */
78/*                                                                        */
79/* RESTRICTIONS/LIMITATIONS:                                              */
80/*   Since RTEMS is not configured, no RTEMS functions can be called.     */
81/*                                                                        */
82/**************************************************************************/
83void mmu_set_cpu_async_mode(void);
84void bsp_start_default( void )
85{
86    int i;
87
88    /* Set the MCU prescaler to divide by 1 */
89    MC9328MXL_PLL_CSCR &= ~MC9328MXL_PLL_CSCR_PRESC;
90
91    /* Enable the MCU PLL */
92    MC9328MXL_PLL_CSCR |= MC9328MXL_PLL_CSCR_MPEN;
93
94    /* Delay to allow time for PLL to get going */
95    for (i = 0; i < 100; i++) {
96        asm volatile ("nop\n");
97    }
98
99    /* Set the CPU to asynchrous clock mode, so it uses its fastest clock */
100    mmu_set_cpu_async_mode();
101
102    /* disable interrupts */
103    MC9328MXL_AITC_INTENABLEL = 0;
104    MC9328MXL_AITC_INTENABLEH = 0;
105
106    /* Set interrupt priority to -1 (allow all priorities) */
107    MC9328MXL_AITC_NIMASK = 0x1f;
108
109    /* Place RTEMS workspace at beginning of free memory. */
110    Configuration.work_space_start = (void *)&_bss_free_start;
111   
112    free_mem_start = ((uint32_t)&_bss_free_start +
113                      rtems_configuration_get_work_space_size());
114   
115    free_mem_end = ((uint32_t)&_sdram_base + (uint32_t)&_sdram_size);
116
117    /*
118     * Init rtems exceptions management
119     */
120    rtems_exception_init_mngt();
121   
122    /*
123     * Init rtems interrupt management
124     */
125    rtems_irq_mngt_init();
126   
127   
128    /*
129     *  The following information is very useful when debugging.
130     */
131#if 0
132    printk( "work_space_size = 0x%x\n",
133            rtems_configuration_get_work_space_size() );
134    printk( "microseconds_per_tick = 0x%x\n",
135            rtems_configuration_get_microseconds_per_tick() );
136    printk( "ticks_per_timeslice = 0x%x\n",
137            rtems_configuration_get_ticks_per_timeslice() );
138    printk( "work_space_start = 0x%x\n",
139            Configuration.work_space_start );
140    printk( "work_space_size = 0x%x\n",
141            rtems_configuration_get_work_space_size() );
142#endif
143} /* bsp_start */
144
145
146
147/* Calcuate the frequency for perclk1 */
148int get_perclk1_freq(void)
149{
150    unsigned int fin;
151    unsigned int fpll;
152    unsigned int pd;
153    unsigned int mfd;
154    unsigned int mfi;
155    unsigned int mfn;
156    uint32_t reg;
157    int perclk1;
158
159    if (MC9328MXL_PLL_CSCR & MC9328MXL_PLL_CSCR_SYSSEL) {
160        /* Use external oscillator */
161        fin = BSP_OSC_FREQ;
162    } else {
163        /* Use scaled xtal freq */
164        fin = BSP_XTAL_FREQ * 512;
165    }
166
167    /* calculate the output of the system PLL */
168    reg = MC9328MXL_PLL_SPCTL0;
169    pd = ((reg & MC9328MXL_PLL_SPCTL_PD_MASK) >>
170          MC9328MXL_PLL_SPCTL_PD_SHIFT);
171    mfd = ((reg & MC9328MXL_PLL_SPCTL_MFD_MASK) >>
172           MC9328MXL_PLL_SPCTL_MFD_SHIFT);
173    mfi = ((reg & MC9328MXL_PLL_SPCTL_MFI_MASK) >>
174           MC9328MXL_PLL_SPCTL_MFI_SHIFT);
175    mfn = ((reg & MC9328MXL_PLL_SPCTL_MFN_MASK) >>
176           MC9328MXL_PLL_SPCTL_MFN_SHIFT);
177
178#if 0
179    printk("fin = %d\n", fin);
180    printk("pd = %d\n", pd);
181    printk("mfd = %d\n", mfd);
182    printk("mfi = %d\n", mfi);
183    printk("mfn = %d\n", mfn);
184    printk("rounded (fin * mfi) / (pd + 1) = %d\n", (fin * mfi) / (pd + 1));
185    printk("rounded (fin * mfn) / ((pd + 1) * (mfd + 1)) = %d\n",
186           ((long long)fin * mfn) / ((pd + 1) * (mfd + 1)));
187#endif
188
189    fpll = 2 * ( ((fin * mfi  + (pd + 1) / 2) / (pd + 1)) +
190                 (((long long)fin * mfn + ((pd + 1) * (mfd + 1)) / 2) /
191                 ((pd + 1) * (mfd + 1))) );
192
193    /* calculate the output of the PERCLK1 divider */
194    reg = MC9328MXL_PLL_PCDR;
195    perclk1 = fpll / (1 + ((reg & MC9328MXL_PLL_PCDR_PCLK1_MASK) >>
196                           MC9328MXL_PLL_PCDR_PCLK1_SHIFT));
197
198    return perclk1;
199}
200
201/*
202 *  By making this a weak alias for bsp_start_default, a brave soul
203 *  can override the actual bsp_start routine used.
204 */
205
206void bsp_start (void) __attribute__ ((weak, alias("bsp_start_default")));
207
208
209/**
210 *  Reset the system.
211 *
212 *  This functions enables the watchdog and waits for it to
213 *  fire, thus resetting the system.
214 */
215/**
216 *  Reset the system.
217 *
218 *  This functions enables the watchdog and waits for it to
219 *  fire, thus resetting the system.
220 */
221void bsp_reset(void)
222{
223    rtems_interrupt_level level;
224
225    _CPU_ISR_Disable(level);
226
227    printk("\n\rI should reset here.\n\r");
228    while(1);
229}
Note: See TracBrowser for help on using the repository browser.