1 |
27 |
unneback |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// hal_misc.c
|
4 |
|
|
//
|
5 |
|
|
// HAL miscellaneous functions
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
16 |
|
|
//
|
17 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
18 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
20 |
|
|
// for more details.
|
21 |
|
|
//
|
22 |
|
|
// You should have received a copy of the GNU General Public License along
|
23 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
24 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
25 |
|
|
//
|
26 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
27 |
|
|
// or inline functions from this file, or you compile this file and link it
|
28 |
|
|
// with other works to produce a work based on this file, this file does not
|
29 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
30 |
|
|
// License. However the source code for this file must still be made available
|
31 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
32 |
|
|
//
|
33 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
34 |
|
|
// this file might be covered by the GNU General Public License.
|
35 |
|
|
//
|
36 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
37 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
38 |
|
|
// -------------------------------------------
|
39 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
40 |
|
|
//==========================================================================
|
41 |
|
|
//#####DESCRIPTIONBEGIN####
|
42 |
|
|
//
|
43 |
|
|
// Author(s): nickg
|
44 |
|
|
// Contributors: nickg, jlarmour
|
45 |
|
|
// Date: 1999-01-21
|
46 |
|
|
// Purpose: HAL miscellaneous functions
|
47 |
|
|
// Description: This file contains miscellaneous functions provided by the
|
48 |
|
|
// HAL.
|
49 |
|
|
//
|
50 |
|
|
//####DESCRIPTIONEND####
|
51 |
|
|
//
|
52 |
|
|
//========================================================================*/
|
53 |
|
|
|
54 |
|
|
#include <pkgconf/hal.h>
|
55 |
|
|
|
56 |
|
|
#include <cyg/infra/cyg_type.h> // Base types
|
57 |
|
|
#include <cyg/infra/cyg_trac.h> // tracing macros
|
58 |
|
|
#include <cyg/infra/cyg_ass.h> // assertion macros
|
59 |
|
|
|
60 |
|
|
#define CYGARC_HAL_COMMON_EXPORT_CPU_MACROS
|
61 |
|
|
#include <cyg/hal/hal_arch.h> // architectural definitions
|
62 |
|
|
|
63 |
|
|
#include <cyg/hal/hal_intr.h> // Interrupt handling
|
64 |
|
|
|
65 |
|
|
#include <cyg/hal/hal_cache.h> // Cache handling
|
66 |
|
|
|
67 |
|
|
/*------------------------------------------------------------------------*/
|
68 |
|
|
/* If required, define a variable to store the clock period. */
|
69 |
|
|
|
70 |
|
|
#ifdef CYGHWR_HAL_CLOCK_PERIOD_DEFINED
|
71 |
|
|
|
72 |
|
|
CYG_WORD32 cyg_hal_clock_period;
|
73 |
|
|
|
74 |
|
|
#endif
|
75 |
|
|
|
76 |
|
|
/*------------------------------------------------------------------------*/
|
77 |
|
|
/* First level C exception handler. */
|
78 |
|
|
|
79 |
|
|
externC void __handle_exception (void);
|
80 |
|
|
|
81 |
|
|
externC HAL_SavedRegisters *_hal_registers;
|
82 |
|
|
|
83 |
|
|
externC void* volatile __mem_fault_handler;
|
84 |
|
|
|
85 |
|
|
externC cyg_uint32 cyg_hal_exception_handler(HAL_SavedRegisters *regs)
|
86 |
|
|
{
|
87 |
|
|
int vec = regs->vector;
|
88 |
|
|
|
89 |
|
|
#if defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS)
|
90 |
|
|
|
91 |
|
|
// If we caught an exception inside the stubs, see if we were expecting it
|
92 |
|
|
// and if so jump to the saved address
|
93 |
|
|
if (__mem_fault_handler) {
|
94 |
|
|
switch (vec) {
|
95 |
|
|
case CYGNUM_HAL_VECTOR_SWI:
|
96 |
|
|
regs->spc_swi = (CYG_ADDRWORD)__mem_fault_handler;
|
97 |
|
|
break;
|
98 |
|
|
case CYGNUM_HAL_VECTOR_FIQ:
|
99 |
|
|
regs->spc_fiq = (CYG_ADDRWORD)__mem_fault_handler;
|
100 |
|
|
break;
|
101 |
|
|
case CYGNUM_HAL_VECTOR_IRQ:
|
102 |
|
|
regs->spc_irq = (CYG_ADDRWORD)__mem_fault_handler;
|
103 |
|
|
break;
|
104 |
|
|
default:
|
105 |
|
|
regs->spc_expt = (CYG_ADDRWORD)__mem_fault_handler;
|
106 |
|
|
break;
|
107 |
|
|
}
|
108 |
|
|
return 0; // Caught an exception inside stubs
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
// Set the pointer to the registers of the current exception
|
112 |
|
|
// context. At entry the GDB stub will expand the
|
113 |
|
|
// HAL_SavedRegisters structure into a (bigger) register array.
|
114 |
|
|
_hal_registers = regs;
|
115 |
|
|
__handle_exception();
|
116 |
|
|
|
117 |
|
|
#elif defined(CYGFUN_HAL_COMMON_KERNEL_SUPPORT) && defined(CYGPKG_HAL_EXCEPTIONS)
|
118 |
|
|
|
119 |
|
|
// We should decode the vector and pass a more appropriate
|
120 |
|
|
// value as the second argument. For now we simply pass a
|
121 |
|
|
// pointer to the saved registers. We should also divert
|
122 |
|
|
// breakpoint and other debug vectors into the debug stubs.
|
123 |
|
|
|
124 |
|
|
cyg_hal_deliver_exception( vec, (CYG_ADDRWORD)regs );
|
125 |
|
|
|
126 |
|
|
#else
|
127 |
|
|
|
128 |
|
|
CYG_FAIL("Exception!!!");
|
129 |
|
|
|
130 |
|
|
#endif
|
131 |
|
|
return 0;
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
/*------------------------------------------------------------------------*/
|
135 |
|
|
/* default ISR */
|
136 |
|
|
|
137 |
|
|
#ifndef CYGSEM_HAL_VIRTUAL_VECTOR_SUPPORT
|
138 |
|
|
externC cyg_uint32 hal_default_isr(CYG_ADDRWORD vector, CYG_ADDRWORD data)
|
139 |
|
|
{
|
140 |
|
|
#if defined(CYGDBG_HAL_CALM32_DEBUG_GDB_CTRLC_SUPPORT) && \
|
141 |
|
|
defined(CYGHWR_HAL_GDB_PORT_VECTOR) && \
|
142 |
|
|
defined(HAL_CTRLC_ISR)
|
143 |
|
|
|
144 |
|
|
#ifndef CYGIMP_HAL_COMMON_INTERRUPTS_CHAIN
|
145 |
|
|
if( vector == CYGHWR_HAL_GDB_PORT_VECTOR )
|
146 |
|
|
#endif
|
147 |
|
|
{
|
148 |
|
|
cyg_uint32 result = HAL_CTRLC_ISR( vector, data );
|
149 |
|
|
if( result != 0 ) return result;
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
#if defined(CYGSEM_HAL_USE_ROM_MONITOR_CygMon)
|
153 |
|
|
#if defined(HAL_DIAG_IRQ_CHECK)
|
154 |
|
|
{
|
155 |
|
|
cyg_uint32 ret;
|
156 |
|
|
/* let ROM monitor handle unexpected interrupts */
|
157 |
|
|
HAL_DIAG_IRQ_CHECK(vector, ret);
|
158 |
|
|
if (ret<=0)
|
159 |
|
|
return ret;
|
160 |
|
|
}
|
161 |
|
|
#endif // def HAL_DIAG_IRQ_CHECK
|
162 |
|
|
#endif // def CYGSEM_HAL_USE_ROM_MONITOR_CygMon
|
163 |
|
|
#endif
|
164 |
|
|
|
165 |
|
|
CYG_TRACE1(true, "Interrupt: %d", vector);
|
166 |
|
|
CYG_FAIL("Spurious Interrupt!!!");
|
167 |
|
|
return 0;
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
#else // CYGSEM_HAL_VIRTUAL_VECTOR_SUPPORT
|
171 |
|
|
|
172 |
|
|
externC cyg_uint32 hal_arch_default_isr(CYG_ADDRWORD vector, CYG_ADDRWORD data)
|
173 |
|
|
{
|
174 |
|
|
#if defined(CYGDBG_HAL_CALM32_DEBUG_GDB_CTRLC_SUPPORT) && \
|
175 |
|
|
defined(CYGHWR_HAL_GDB_PORT_VECTOR) && \
|
176 |
|
|
defined(HAL_CTRLC_ISR)
|
177 |
|
|
|
178 |
|
|
#if defined(CYGSEM_HAL_USE_ROM_MONITOR_CygMon)
|
179 |
|
|
#if defined(HAL_DIAG_IRQ_CHECK)
|
180 |
|
|
{
|
181 |
|
|
cyg_uint32 ret;
|
182 |
|
|
/* let ROM monitor handle unexpected interrupts */
|
183 |
|
|
HAL_DIAG_IRQ_CHECK(vector, ret);
|
184 |
|
|
if (ret<=0)
|
185 |
|
|
return ret;
|
186 |
|
|
}
|
187 |
|
|
#endif // def HAL_DIAG_IRQ_CHECK
|
188 |
|
|
#endif // def CYGSEM_HAL_USE_ROM_MONITOR_CygMon
|
189 |
|
|
#endif
|
190 |
|
|
|
191 |
|
|
return 0;
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
#endif // CYGSEM_HAL_VIRTUAL_VECTOR_SUPPORT
|
195 |
|
|
|
196 |
|
|
/*------------------------------------------------------------------------*/
|
197 |
|
|
/* data copy and bss zero functions */
|
198 |
|
|
|
199 |
|
|
typedef void (CYG_SYM_ADDRESS)(void);
|
200 |
|
|
|
201 |
|
|
// All these must use this type of address to stop them being given relocations
|
202 |
|
|
// relative to $gp (i.e. assuming they would be in .sdata)
|
203 |
|
|
extern CYG_SYM_ADDRESS __ram_data_start;
|
204 |
|
|
extern CYG_SYM_ADDRESS __ram_data_end;
|
205 |
|
|
extern CYG_SYM_ADDRESS __rom_data_start;
|
206 |
|
|
|
207 |
|
|
#ifdef CYG_HAL_STARTUP_ROM
|
208 |
|
|
void hal_copy_data(void)
|
209 |
|
|
{
|
210 |
|
|
short *p = (short *)&__ram_data_start;
|
211 |
|
|
short *q = (short *)&__rom_data_start;
|
212 |
|
|
short x;
|
213 |
|
|
|
214 |
|
|
while( p != (short *)&__ram_data_end ) {
|
215 |
|
|
asm volatile( "ldch %0,@%1\n" : "=r"(x) : "r"(q) );
|
216 |
|
|
*p++ = x;
|
217 |
|
|
q++;
|
218 |
|
|
}
|
219 |
|
|
}
|
220 |
|
|
#endif
|
221 |
|
|
|
222 |
|
|
extern CYG_SYM_ADDRESS __bss_start;
|
223 |
|
|
extern CYG_SYM_ADDRESS __bss_end;
|
224 |
|
|
|
225 |
|
|
void hal_zero_bss(void)
|
226 |
|
|
{
|
227 |
|
|
short *p = (short *)&__bss_start;
|
228 |
|
|
|
229 |
|
|
while( p != (short *)&__bss_end )
|
230 |
|
|
*p++ = 0;
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
/*------------------------------------------------------------------------*/
|
234 |
|
|
|
235 |
|
|
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
|
236 |
|
|
cyg_bool cyg_hal_stop_constructors;
|
237 |
|
|
#endif
|
238 |
|
|
|
239 |
|
|
typedef void (*pfunc) (void);
|
240 |
|
|
extern pfunc __CTOR_LIST__[];
|
241 |
|
|
extern pfunc __CTOR_END__[];
|
242 |
|
|
|
243 |
|
|
void
|
244 |
|
|
cyg_hal_invoke_constructors(void)
|
245 |
|
|
{
|
246 |
|
|
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
|
247 |
|
|
static pfunc *p = &__CTOR_END__[-1];
|
248 |
|
|
|
249 |
|
|
cyg_hal_stop_constructors = 0;
|
250 |
|
|
for (; p >= __CTOR_LIST__; p--) {
|
251 |
|
|
(*p) ();
|
252 |
|
|
if (cyg_hal_stop_constructors) {
|
253 |
|
|
p--;
|
254 |
|
|
break;
|
255 |
|
|
}
|
256 |
|
|
}
|
257 |
|
|
#else
|
258 |
|
|
pfunc *p;
|
259 |
|
|
|
260 |
|
|
for (p = &__CTOR_END__[-1]; p >= __CTOR_LIST__; p--)
|
261 |
|
|
(*p) ();
|
262 |
|
|
#endif
|
263 |
|
|
|
264 |
|
|
} // cyg_hal_invoke_constructors()
|
265 |
|
|
|
266 |
|
|
/*------------------------------------------------------------------------*/
|
267 |
|
|
/* Determine the index of the ls bit of the supplied mask. */
|
268 |
|
|
|
269 |
|
|
cyg_uint32 hal_lsbit_index(cyg_uint32 mask)
|
270 |
|
|
{
|
271 |
|
|
cyg_uint32 n = mask;
|
272 |
|
|
|
273 |
|
|
static const signed char tab[64] =
|
274 |
|
|
{ -1, 0, 1, 12, 2, 6, 0, 13, 3, 0, 7, 0, 0, 0, 0, 14, 10,
|
275 |
|
|
4, 0, 0, 8, 0, 0, 25, 0, 0, 0, 0, 0, 21, 27 , 15, 31, 11,
|
276 |
|
|
5, 0, 0, 0, 0, 0, 9, 0, 0, 24, 0, 0 , 20, 26, 30, 0, 0, 0,
|
277 |
|
|
0, 23, 0, 19, 29, 0, 22, 18, 28, 17, 16, 0
|
278 |
|
|
};
|
279 |
|
|
|
280 |
|
|
n &= ~(n-1UL);
|
281 |
|
|
n = (n<<16)-n;
|
282 |
|
|
n = (n<<6)+n;
|
283 |
|
|
n = (n<<4)+n;
|
284 |
|
|
|
285 |
|
|
return tab[n>>26];
|
286 |
|
|
}
|
287 |
|
|
|
288 |
|
|
/*------------------------------------------------------------------------*/
|
289 |
|
|
/* Determine the index of the ms bit of the supplied mask. */
|
290 |
|
|
|
291 |
|
|
cyg_uint32 hal_msbit_index(cyg_uint32 mask)
|
292 |
|
|
{
|
293 |
|
|
cyg_uint32 x = mask;
|
294 |
|
|
cyg_uint32 w;
|
295 |
|
|
|
296 |
|
|
/* Phase 1: make word with all ones from that one to the right */
|
297 |
|
|
x |= x >> 16;
|
298 |
|
|
x |= x >> 8;
|
299 |
|
|
x |= x >> 4;
|
300 |
|
|
x |= x >> 2;
|
301 |
|
|
x |= x >> 1;
|
302 |
|
|
|
303 |
|
|
/* Phase 2: calculate number of "1" bits in the word */
|
304 |
|
|
w = (x & 0x55555555) + ((x >> 1) & 0x55555555);
|
305 |
|
|
w = (w & 0x33333333) + ((w >> 2) & 0x33333333);
|
306 |
|
|
w = w + (w >> 4);
|
307 |
|
|
w = (w & 0x000F000F) + ((w >> 8) & 0x000F000F);
|
308 |
|
|
return (cyg_uint32)((w + (w >> 16)) & 0xFF) - 1;
|
309 |
|
|
|
310 |
|
|
}
|
311 |
|
|
|
312 |
|
|
/*------------------------------------------------------------------------*/
|
313 |
|
|
/* Idle thread action */
|
314 |
|
|
|
315 |
|
|
#include <cyg/infra/diag.h>
|
316 |
|
|
|
317 |
|
|
void hal_idle_thread_action( cyg_uint32 count )
|
318 |
|
|
{
|
319 |
|
|
}
|
320 |
|
|
|
321 |
|
|
/*------------------------------------------------------------------------*/
|
322 |
|
|
/* End of hal_misc.c */
|