1 //==========================================================================
5 // HAL miscellaneous functions
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 // Copyright (C) 2007 eCosCentric Ltd.
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 // You should have received a copy of the GNU General Public License along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
37 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38 // at http://sources.redhat.com/ecos/ecos-license/
39 // -------------------------------------------
40 //####ECOSGPLCOPYRIGHTEND####
41 //==========================================================================
43 #include <pkgconf/hal.h>
45 #include <cyg/infra/cyg_type.h>
46 #include <cyg/infra/cyg_trac.h> // tracing macros
47 #include <cyg/infra/cyg_ass.h> // assertion macros
48 #include <cyg/hal/hal_arch.h> // HAL header
49 #include <cyg/hal/hal_intr.h> // VSR/ISR defines
50 #include <cyg/hal/hal_misc.h>
52 /*------------------------------------------------------------------------*/
53 /* If required, define a variable to store the clock period. */
55 #ifdef CYGHWR_HAL_CLOCK_PERIOD_DEFINED
57 CYG_WORD32 cyg_hal_clock_period;
62 /*****************************************************************************
63 hal_default_exception_handler -- First level C exception handler
65 The assembly default VSR handler calls this routine to handle the
66 exception. When this routine returns, the state is restored to the state
69 We declare this routine as weak so that other handlers can easily
70 become the default exception handler.
74 vector: The exception vector number.
76 regs: A pointer to the saved state.
84 *****************************************************************************/
87 hal_default_exception_handler(CYG_WORD vector, HAL_SavedRegisters *regs)
88 __attribute__ ((weak));
90 void hal_default_exception_handler(CYG_WORD vector, HAL_SavedRegisters *regs)
93 #ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
94 externC void __handle_exception(void);
95 externC HAL_SavedRegisters * _hal_registers;
97 // Set the pointer to the registers of the current exception
98 // context. At entry the GDB stub will expand the
99 // HAL_SavedRegisters structure into a (bigger) register array.
100 _hal_registers = regs;
102 __handle_exception();
104 #elif defined(CYGFUN_HAL_COMMON_KERNEL_SUPPORT) && \
105 defined(CYGPKG_HAL_EXCEPTIONS)
107 // We should decode the vector and pass a more appropriate
108 // value as the second argument. For now we simply pass a
109 // pointer to the saved registers. We should also divert
110 // breakpoint and other debug vectors into the debug stubs.
112 cyg_hal_deliver_exception(vector, (CYG_ADDRWORD)regs);
116 CYG_FAIL("Exception!!!");
123 //---------------------------------------------------------------------------
127 hal_arch_default_isr(CYG_ADDRWORD vector, CYG_ADDRWORD data)
129 CYG_FAIL("Spurious Interrupt!!!");
133 //---------------------------------------------------------------------------
134 // Idle thread action
137 hal_idle_thread_action( cyg_uint32 count )
141 //-----------------------------------------------------------------------------
142 // Monitor initialization. This means to init the vsr vector table.
143 // TODO maybe init hal_vsr_table through defining it in assembler code
145 #ifndef CYGPKG_HAL_FR30_MON_DEFINED
146 void hal_mon_init(void){
148 /* 0 - 14 exceptions */
149 for(i = 0; i < CYGNUM_HAL_VECTOR_INTRFIRST; i++){
150 hal_vsr_table[i] = (CYG_ADDRESS)__default_exception_vsr;
152 /* 15 - xx interrupts */
153 for( ; i < CYGNUM_HAL_VECTOR_INTRLAST; i++){
154 hal_vsr_table[i] = (CYG_ADDRESS)__default_interrupt_vsr;
156 /* xx+1 - 255 exceptions (fr30 traps) */
157 for( ; i < CYGNUM_HAL_VSR_MAX; i++){
158 hal_vsr_table[i] = (CYG_ADDRESS)__default_exception_vsr;
163 //---------------------------------------------------------------------------
164 // Determine the index of the ls bit of the supplied mask.
167 hal_lsbit_index(cyg_uint32 mask)
171 static const signed char tab[64] =
172 { -1, 0, 1, 12, 2, 6, 0, 13, 3, 0, 7, 0, 0, 0, 0, 14, 10,
173 4, 0, 0, 8, 0, 0, 25, 0, 0, 0, 0, 0, 21, 27 , 15, 31, 11,
174 5, 0, 0, 0, 0, 0, 9, 0, 0, 24, 0, 0 , 20, 26, 30, 0, 0, 0,
175 0, 23, 0, 19, 29, 0, 22, 18, 28, 17, 16, 0
186 //---------------------------------------------------------------------------
187 // Determine the index of the ms bit of the supplied mask. FIXME: since we
188 // have hardware support for it, use it! (bit search module)
191 hal_msbit_index(cyg_uint32 mask)
196 // Phase 1: make word with all ones from that one to the right.
203 // Phase 2: calculate number of "1" bits in the word.
204 w = (x & 0x55555555) + ((x >> 1) & 0x55555555);
205 w = (w & 0x33333333) + ((w >> 2) & 0x33333333);
207 w = (w & 0x000F000F) + ((w >> 8) & 0x000F000F);
208 return (cyg_uint32)((w + (w >> 16)) & 0xFF);
212 /*------------------------------------------------------------------------*/
213 /* C++ support - run initial constructors */
215 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
216 cyg_bool cyg_hal_stop_constructors;
219 typedef void (*pfunc) (void);
220 extern pfunc __CTOR_LIST__[];
221 extern pfunc __CTOR_END__[];
224 cyg_hal_invoke_constructors (void)
226 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
227 static pfunc *p = &__CTOR_END__[-1];
229 cyg_hal_stop_constructors = 0;
230 for (; p >= __CTOR_LIST__; p--) {
232 if (cyg_hal_stop_constructors) {
239 for (p = &__CTOR_END__[-1]; p >= __CTOR_LIST__; p--) {
245 //---------------------------------------------------------------------------