]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/hal/fr30/arch/v2_0/src/hal_misc.c
Initial revision
[karo-tx-redboot.git] / packages / hal / fr30 / arch / v2_0 / src / hal_misc.c
1 //==========================================================================
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 // Copyright (C) 2007 eCosCentric Ltd.
13 //
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.
17 //
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
21 // for more details.
22 //
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.
26 //
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.
33 //
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.
36 //
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 //==========================================================================
42
43 #include <pkgconf/hal.h>
44
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>
51
52 /*------------------------------------------------------------------------*/
53 /* If required, define a variable to store the clock period.              */
54
55 #ifdef CYGHWR_HAL_CLOCK_PERIOD_DEFINED
56
57 CYG_WORD32 cyg_hal_clock_period;
58
59 #endif
60
61
62 /*****************************************************************************
63 hal_default_exception_handler -- First level C exception handler
64
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
67 pointed to by regs.
68
69      We declare this  routine as  weak so  that other  handlers can  easily
70 become the default exception handler.
71
72 INPUT:
73
74      vector: The exception vector number.
75
76      regs: A pointer to the saved state.
77
78 OUTPUT:
79
80 RETURN VALUE:
81
82      None
83
84 *****************************************************************************/
85
86 externC void
87 hal_default_exception_handler(CYG_WORD vector, HAL_SavedRegisters *regs)
88                                                     __attribute__ ((weak));
89
90 void hal_default_exception_handler(CYG_WORD vector, HAL_SavedRegisters *regs)
91 {
92
93 #ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
94     externC void __handle_exception(void);
95     externC HAL_SavedRegisters * _hal_registers;
96
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;
101
102     __handle_exception();
103
104 #elif defined(CYGFUN_HAL_COMMON_KERNEL_SUPPORT) && \
105       defined(CYGPKG_HAL_EXCEPTIONS)
106
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.
111
112     cyg_hal_deliver_exception(vector, (CYG_ADDRWORD)regs);
113
114 #else
115
116     CYG_FAIL("Exception!!!");
117
118 #endif
119
120     return;
121 }
122
123 //---------------------------------------------------------------------------
124 // Default arch ISR
125
126 externC cyg_uint32
127 hal_arch_default_isr(CYG_ADDRWORD vector, CYG_ADDRWORD data)
128 {
129     CYG_FAIL("Spurious Interrupt!!!");
130     return 0;
131 }
132
133 //---------------------------------------------------------------------------
134 // Idle thread action
135
136 void
137 hal_idle_thread_action( cyg_uint32 count )
138 {
139 }
140
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
144
145 #ifndef CYGPKG_HAL_FR30_MON_DEFINED
146 void hal_mon_init(void){
147     int i;
148     /* 0 - 14 exceptions */
149     for(i = 0; i < CYGNUM_HAL_VECTOR_INTRFIRST; i++){
150         hal_vsr_table[i] = (CYG_ADDRESS)__default_exception_vsr;
151     }
152     /* 15 - xx interrupts */
153     for( ; i < CYGNUM_HAL_VECTOR_INTRLAST; i++){
154         hal_vsr_table[i] = (CYG_ADDRESS)__default_interrupt_vsr;
155     }
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;
159     }
160 }
161 #endif
162
163 //---------------------------------------------------------------------------
164 // Determine the index of the ls bit of the supplied mask.
165
166 cyg_uint32
167 hal_lsbit_index(cyg_uint32 mask)
168 {
169     cyg_uint32 n = mask;
170
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
176     };
177
178     n &= ~(n-1UL);
179     n = (n<<16)-n;
180     n = (n<<6)+n;
181     n = (n<<4)+n;
182
183     return tab[n>>26];
184 }
185
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)
189
190 cyg_uint32
191 hal_msbit_index(cyg_uint32 mask)
192 {
193     cyg_uint32 x = mask;
194     cyg_uint32 w;
195
196     // Phase 1: make word with all ones from that one to the right.
197     x |= x >> 16;
198     x |= x >> 8;
199     x |= x >> 4;
200     x |= x >> 2;
201     x |= x >> 1;
202
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);
206     w = w + (w >> 4);
207     w = (w & 0x000F000F) + ((w >> 8) & 0x000F000F);
208     return (cyg_uint32)((w + (w >> 16)) & 0xFF);
209
210 }
211
212 /*------------------------------------------------------------------------*/
213 /* C++ support - run initial constructors                                 */
214
215 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
216 cyg_bool cyg_hal_stop_constructors;
217 #endif
218
219 typedef void (*pfunc) (void);
220 extern pfunc __CTOR_LIST__[];
221 extern pfunc __CTOR_END__[];
222
223 void
224         cyg_hal_invoke_constructors (void)
225 {
226 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
227     static pfunc *p = &__CTOR_END__[-1];
228
229     cyg_hal_stop_constructors = 0;
230     for (; p >= __CTOR_LIST__; p--) {
231         (*p) ();
232         if (cyg_hal_stop_constructors) {
233             p--;
234             break;
235         }
236     }
237 #else
238     pfunc *p;
239     for (p = &__CTOR_END__[-1]; p >= __CTOR_LIST__; p--) {
240         (*p) ();
241     }
242 #endif
243 }
244
245 //---------------------------------------------------------------------------