1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup mpc55xx |
---|
5 | * |
---|
6 | * @brief Early initialization code. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2008-2012 embedded brains GmbH. All rights reserved. |
---|
11 | * |
---|
12 | * embedded brains GmbH |
---|
13 | * Obere Lagerstr. 30 |
---|
14 | * 82178 Puchheim |
---|
15 | * Germany |
---|
16 | * <rtems@embedded-brains.de> |
---|
17 | * |
---|
18 | * The license and distribution terms for this file may be |
---|
19 | * found in the file LICENSE in this distribution or at |
---|
20 | * http://www.rtems.org/license/LICENSE. |
---|
21 | */ |
---|
22 | |
---|
23 | #include <bsp/mpc55xx-config.h> |
---|
24 | #include <bsp/linker-symbols.h> |
---|
25 | |
---|
26 | /* This function is defined in start.S */ |
---|
27 | BSP_START_TEXT_SECTION void mpc55xx_start_load_section( |
---|
28 | void *dst, |
---|
29 | const void *src, |
---|
30 | size_t n |
---|
31 | ); |
---|
32 | |
---|
33 | static BSP_START_TEXT_SECTION void mpc55xx_start_mmu(void) |
---|
34 | { |
---|
35 | #ifdef MPC55XX_BOOTFLAGS |
---|
36 | /* If the low bit of bootflag 0 is clear don't change the MMU. */ |
---|
37 | bool do_mmu_config = (mpc55xx_bootflag_0 [0] & 1) != 0; |
---|
38 | #else |
---|
39 | bool do_mmu_config = true; |
---|
40 | #endif |
---|
41 | |
---|
42 | if (do_mmu_config) { |
---|
43 | mpc55xx_start_mmu_apply_config( |
---|
44 | &mpc55xx_start_config_mmu [0], |
---|
45 | mpc55xx_start_config_mmu_count [0] |
---|
46 | ); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | static BSP_START_TEXT_SECTION void mpc55xx_start_internal_ram(void) |
---|
51 | { |
---|
52 | #ifdef MPC55XX_NEEDS_LOW_LEVEL_INIT |
---|
53 | /* Initialize internal SRAM to zero (ECC) */ |
---|
54 | bsp_start_zero( |
---|
55 | (char *) bsp_ram_start + MPC55XX_EARLY_STACK_SIZE, |
---|
56 | (size_t) bsp_ram_size - MPC55XX_EARLY_STACK_SIZE |
---|
57 | ); |
---|
58 | #ifdef MPC55XX_HAS_SECOND_INTERNAL_RAM_AREA |
---|
59 | bsp_start_zero(&bsp_ram_1_start [0], (size_t) bsp_ram_1_size); |
---|
60 | #endif |
---|
61 | #else |
---|
62 | bsp_start_zero( |
---|
63 | bsp_section_sbss_begin, |
---|
64 | (size_t) bsp_section_sbss_size |
---|
65 | ); |
---|
66 | bsp_start_zero( |
---|
67 | bsp_section_bss_begin, |
---|
68 | (size_t) bsp_section_bss_size |
---|
69 | ); |
---|
70 | #endif |
---|
71 | } |
---|
72 | |
---|
73 | static BSP_START_TEXT_SECTION void mpc55xx_start_load_nocache_section(void) |
---|
74 | { |
---|
75 | mpc55xx_start_load_section( |
---|
76 | bsp_section_nocache_begin, |
---|
77 | bsp_section_nocache_load_begin, |
---|
78 | (size_t) bsp_section_nocache_size |
---|
79 | ); |
---|
80 | rtems_cache_flush_multiple_data_lines( |
---|
81 | bsp_section_nocache_begin, |
---|
82 | (size_t) bsp_section_nocache_size |
---|
83 | ); |
---|
84 | } |
---|
85 | |
---|
86 | static BSP_START_TEXT_SECTION void mpc55xx_start_mode_change(void) |
---|
87 | { |
---|
88 | #ifdef MPC55XX_NEEDS_LOW_LEVEL_INIT |
---|
89 | #ifdef MPC55XX_HAS_MODE_CONTROL |
---|
90 | uint32_t mctl_key1 = 0x5af0; |
---|
91 | uint32_t mctl_key2 = 0xa50f; |
---|
92 | int i = 0; |
---|
93 | |
---|
94 | /* Clear any pending RGM status */ |
---|
95 | RGM.FES.R = 0xffff; |
---|
96 | RGM.DES.R = 0xffff; |
---|
97 | |
---|
98 | /* Make sure XOSC and PLLs are on in RUN0 state */ |
---|
99 | ME.DRUN_MC.R = 0x001f0074; |
---|
100 | ME.RUN_MC [0].R = 0x001f0074; |
---|
101 | |
---|
102 | /* |
---|
103 | * Make sure all peripherals are active in DRUN and RUN0 state. |
---|
104 | * |
---|
105 | * FIXME: This might be optimized to reduce power consumtion. |
---|
106 | */ |
---|
107 | for (i = 0; i < 8; ++i) { |
---|
108 | ME_RUN_PC_32B_tag run_pc = { .R = ME.RUN_PC [i].R }; |
---|
109 | |
---|
110 | run_pc.B.DRUN = 1; |
---|
111 | run_pc.B.RUN0 = 1; |
---|
112 | |
---|
113 | ME.RUN_PC [i].R = run_pc.R; |
---|
114 | } |
---|
115 | |
---|
116 | /* Switch to RUN0 state */ |
---|
117 | ME.MCTL.R = 0x40000000 | mctl_key1; |
---|
118 | ME.MCTL.R = 0x40000000 | mctl_key2; |
---|
119 | |
---|
120 | while (ME.GS.B.S_MTRANS) { |
---|
121 | /* Wait for mode switch to be completed */ |
---|
122 | } |
---|
123 | #endif |
---|
124 | #endif |
---|
125 | } |
---|
126 | |
---|
127 | static BSP_START_TEXT_SECTION void mpc55xx_start_siu(void) |
---|
128 | { |
---|
129 | size_t i = 0; |
---|
130 | |
---|
131 | for (i = 0; i < mpc55xx_start_config_siu_pcr_count [0]; ++i) { |
---|
132 | const mpc55xx_siu_pcr_config *e = &mpc55xx_start_config_siu_pcr [i]; |
---|
133 | int j = e->index; |
---|
134 | int n = j + e->count; |
---|
135 | uint8_t gpdo = e->output; |
---|
136 | uint16_t pcr = e->pcr.R; |
---|
137 | |
---|
138 | while (j < n) { |
---|
139 | SIU.GPDO [j].R = gpdo; |
---|
140 | SIU.PCR [j].R = pcr; |
---|
141 | ++j; |
---|
142 | } |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | static BSP_START_TEXT_SECTION void mpc55xx_start_ebi_chip_select(void) |
---|
147 | { |
---|
148 | #ifdef MPC55XX_HAS_EBI |
---|
149 | size_t i = 0; |
---|
150 | |
---|
151 | for (i = 0; i < mpc55xx_start_config_ebi_cs_count [0]; ++i) { |
---|
152 | EBI.CS [i] = mpc55xx_start_config_ebi_cs [i]; |
---|
153 | } |
---|
154 | |
---|
155 | for (i = 0; i < mpc55xx_start_config_ebi_cal_cs_count [0]; ++i) { |
---|
156 | EBI.CAL_CS [i] = mpc55xx_start_config_ebi_cal_cs [i]; |
---|
157 | } |
---|
158 | #endif |
---|
159 | } |
---|
160 | |
---|
161 | static BSP_START_TEXT_SECTION void mpc55xx_start_ebi(void) |
---|
162 | { |
---|
163 | #ifdef MPC55XX_HAS_EBI |
---|
164 | size_t i = 0; |
---|
165 | |
---|
166 | for (i = 0; i < mpc55xx_start_config_ebi_count [0]; ++i) { |
---|
167 | SIU.ECCR.B.EBDF = mpc55xx_start_config_ebi [i].siu_eccr_ebdf; |
---|
168 | EBI.MCR.R = mpc55xx_start_config_ebi [i].ebi_mcr.R; |
---|
169 | } |
---|
170 | #endif |
---|
171 | } |
---|
172 | |
---|
173 | #ifdef MPC55XX_NEEDS_LOW_LEVEL_INIT |
---|
174 | static BSP_START_TEXT_SECTION bool |
---|
175 | mpc55xx_start_is_in_internal_ram(const void *addr) |
---|
176 | { |
---|
177 | return (size_t) addr - (size_t) bsp_ram_start < (size_t) bsp_ram_size; |
---|
178 | } |
---|
179 | #endif |
---|
180 | |
---|
181 | static BSP_START_TEXT_SECTION void mpc55xx_start_clear_bss(void) |
---|
182 | { |
---|
183 | #ifdef MPC55XX_NEEDS_LOW_LEVEL_INIT |
---|
184 | if (!mpc55xx_start_is_in_internal_ram(bsp_section_sbss_begin)) { |
---|
185 | bsp_start_zero( |
---|
186 | bsp_section_sbss_begin, |
---|
187 | (size_t) bsp_section_sbss_size |
---|
188 | ); |
---|
189 | } |
---|
190 | |
---|
191 | if (!mpc55xx_start_is_in_internal_ram(bsp_section_bss_begin)) { |
---|
192 | bsp_start_zero( |
---|
193 | bsp_section_bss_begin, |
---|
194 | (size_t) bsp_section_bss_size |
---|
195 | ); |
---|
196 | } |
---|
197 | #endif |
---|
198 | } |
---|
199 | |
---|
200 | BSP_START_TEXT_SECTION void mpc55xx_start_early(void) |
---|
201 | { |
---|
202 | mpc55xx_start_watchdog(); |
---|
203 | mpc55xx_start_clock(); |
---|
204 | mpc55xx_start_flash(); |
---|
205 | #if defined(BSP_DATA_CACHE_ENABLED) || defined(BSP_INSTRUCTION_CACHE_ENABLED) |
---|
206 | mpc55xx_start_cache(); |
---|
207 | #endif |
---|
208 | mpc55xx_start_internal_ram(); |
---|
209 | mpc55xx_start_load_nocache_section(); |
---|
210 | mpc55xx_start_mmu(); |
---|
211 | mpc55xx_start_mode_change(); |
---|
212 | mpc55xx_start_siu(); |
---|
213 | mpc55xx_start_ebi_chip_select(); |
---|
214 | mpc55xx_start_ebi(); |
---|
215 | mpc55xx_start_clear_bss(); |
---|
216 | } |
---|