]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/hal/calmrisc16/arch/v2_0/src/hal_misc.c
Initial revision
[karo-tx-redboot.git] / packages / hal / calmrisc16 / 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 //
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 #if defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS)
84 externC void* volatile __mem_fault_handler;
85 #endif
86
87 externC cyg_uint32 cyg_hal_exception_handler(HAL_SavedRegisters *regs)
88 {
89     int vec = regs->vector;
90
91     // adjust PC after 'break' insns
92     if (vec == CYGNUM_HAL_VECTOR_IRQ) {
93         if (__read_prog_uint16((void *)(regs->spc_irq - 2)) == HAL_BREAKINST)
94             regs->spc_irq -= 2;
95     }
96
97 #if defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS)
98
99     // If we caught an exception inside the stubs, see if we were expecting it
100     // and if so jump to the saved address
101     if (__mem_fault_handler) {
102         switch (vec) {
103           case CYGNUM_HAL_VECTOR_SWI:
104             regs->a[6] = (CYG_ADDRWORD)__mem_fault_handler;
105             break;
106           case CYGNUM_HAL_VECTOR_FIQ:
107             regs->spc_fiq = (CYG_ADDRWORD)__mem_fault_handler;
108             break;
109           default:
110             regs->spc_irq = (CYG_ADDRWORD)__mem_fault_handler;
111             break;
112         }
113         return 0; // Caught an exception inside stubs        
114     }
115
116     // Set the pointer to the registers of the current exception
117     // context. At entry the GDB stub will expand the
118     // HAL_SavedRegisters structure into a (bigger) register array.
119     _hal_registers = regs;
120     __handle_exception();
121
122 #elif defined(CYGFUN_HAL_COMMON_KERNEL_SUPPORT) && defined(CYGPKG_HAL_EXCEPTIONS)
123
124     // We should decode the vector and pass a more appropriate
125     // value as the second argument. For now we simply pass a
126     // pointer to the saved registers. We should also divert
127     // breakpoint and other debug vectors into the debug stubs.
128     
129     cyg_hal_deliver_exception( vec, (CYG_ADDRWORD)regs );
130
131 #else
132     
133     CYG_FAIL("Exception!!!");
134     
135 #endif    
136     return 0;
137 }
138
139 /*------------------------------------------------------------------------*/
140 /* default ISR                                                            */
141
142 #ifndef CYGSEM_HAL_VIRTUAL_VECTOR_SUPPORT
143 externC cyg_uint32 hal_default_isr(CYG_ADDRWORD vector, CYG_ADDRWORD data)
144 {
145 #if defined(CYGDBG_HAL_CALM32_DEBUG_GDB_CTRLC_SUPPORT) &&      \
146     defined(CYGHWR_HAL_GDB_PORT_VECTOR) &&              \
147     defined(HAL_CTRLC_ISR)
148
149 #ifndef CYGIMP_HAL_COMMON_INTERRUPTS_CHAIN    
150     if( vector == CYGHWR_HAL_GDB_PORT_VECTOR )
151 #endif        
152     {
153         cyg_uint32 result = HAL_CTRLC_ISR( vector, data );
154         if( result != 0 ) return result;
155     }
156     
157 #if defined(CYGSEM_HAL_USE_ROM_MONITOR_CygMon)
158 #if defined(HAL_DIAG_IRQ_CHECK)
159     {
160         cyg_uint32 ret;
161         /* let ROM monitor handle unexpected interrupts */
162         HAL_DIAG_IRQ_CHECK(vector, ret);
163         if (ret<=0)
164             return ret;
165     }
166 #endif // def HAL_DIAG_IRQ_CHECK
167 #endif // def CYGSEM_HAL_USE_ROM_MONITOR_CygMon
168 #endif
169     
170     CYG_TRACE1(true, "Interrupt: %d", vector);
171     CYG_FAIL("Spurious Interrupt!!!");
172     return 0;
173 }
174
175 #else // CYGSEM_HAL_VIRTUAL_VECTOR_SUPPORT
176
177 externC cyg_uint32 hal_arch_default_isr(CYG_ADDRWORD vector, CYG_ADDRWORD data)
178 {
179 #if defined(CYGDBG_HAL_CALM16_DEBUG_GDB_CTRLC_SUPPORT) &&      \
180     defined(CYGHWR_HAL_GDB_PORT_VECTOR) &&              \
181     defined(HAL_CTRLC_ISR)
182
183 #if defined(CYGSEM_HAL_USE_ROM_MONITOR_CygMon)
184 #if defined(HAL_DIAG_IRQ_CHECK)
185     {
186         cyg_uint32 ret;
187         /* let ROM monitor handle unexpected interrupts */
188         HAL_DIAG_IRQ_CHECK(vector, ret);
189         if (ret<=0)
190             return ret;
191     }
192 #endif // def HAL_DIAG_IRQ_CHECK
193 #endif // def CYGSEM_HAL_USE_ROM_MONITOR_CygMon
194 #endif
195
196     return 0;
197 }
198
199 #endif // CYGSEM_HAL_VIRTUAL_VECTOR_SUPPORT
200
201 /*------------------------------------------------------------------------*/
202 /* data copy and bss zero functions                                       */
203
204 typedef void (CYG_SYM_ADDRESS)(void);
205
206 // All these must use this type of address to stop them being given relocations
207 // relative to $gp (i.e. assuming they would be in .sdata)
208 extern CYG_SYM_ADDRESS __ram_data_start;
209 extern CYG_SYM_ADDRESS __ram_data_end;
210 extern CYG_SYM_ADDRESS __rom_data_start;    
211
212 #ifdef CYG_HAL_STARTUP_ROM      
213 void hal_copy_data(void)
214 {
215     short *p = (short *)&__ram_data_start;
216     short *q = (short *)&__rom_data_start;
217     short x;
218     
219     while( p != (short *)&__ram_data_end ) {
220         asm volatile( "ldc %0,@%1\n" : "=r"(x) : "r"(q) );
221         *p++ = x;
222         q++;
223     }
224 }
225 #endif
226
227 extern CYG_SYM_ADDRESS __bss_start;
228 extern CYG_SYM_ADDRESS __bss_end;
229
230 void hal_zero_bss(void)
231 {
232     short *p = (short *)&__bss_start;
233
234     while( p != (short *)&__bss_end )
235         *p++ = 0;   
236 }
237
238 /*------------------------------------------------------------------------*/
239
240 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
241 cyg_bool cyg_hal_stop_constructors;
242 #endif
243
244 typedef void (*pfunc) (void);
245 extern pfunc __CTOR_LIST__[];
246 extern pfunc __CTOR_END__[];
247
248 void
249 cyg_hal_invoke_constructors(void)
250 {
251 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
252     static pfunc *p = &__CTOR_END__[-1];
253     
254     cyg_hal_stop_constructors = 0;
255     for (; p >= __CTOR_LIST__; p--) {
256         (*p) ();
257         if (cyg_hal_stop_constructors) {
258             p--;
259             break;
260         }
261     }
262 #else
263     pfunc *p;
264
265     for (p = &__CTOR_END__[-1]; p >= __CTOR_LIST__; p--)
266         (*p) ();
267 #endif
268     
269 } // cyg_hal_invoke_constructors()
270
271 /*------------------------------------------------------------------------*/
272 /* Determine the index of the ls bit of the supplied mask.                */
273
274 cyg_uint32 hal_lsbit_index(cyg_uint32 mask)
275 {
276     cyg_uint32 n = mask;
277
278     static const signed char tab[64] =
279     { -1, 0, 1, 12, 2, 6, 0, 13, 3, 0, 7, 0, 0, 0, 0, 14, 10,
280       4, 0, 0, 8, 0, 0, 25, 0, 0, 0, 0, 0, 21, 27 , 15, 31, 11,
281       5, 0, 0, 0, 0, 0, 9, 0, 0, 24, 0, 0 , 20, 26, 30, 0, 0, 0,
282       0, 23, 0, 19, 29, 0, 22, 18, 28, 17, 16, 0
283     };
284
285     n &= ~(n-1UL);
286     n = (n<<16)-n;
287     n = (n<<6)+n;
288     n = (n<<4)+n;
289
290     return tab[n>>26];
291 }
292
293 /*------------------------------------------------------------------------*/
294 /* Determine the index of the ms bit of the supplied mask.                */
295
296 cyg_uint32 hal_msbit_index(cyg_uint32 mask)
297 {
298     cyg_uint32 x = mask;    
299     cyg_uint32 w;
300
301     /* Phase 1: make word with all ones from that one to the right */
302     x |= x >> 16;
303     x |= x >> 8;
304     x |= x >> 4;
305     x |= x >> 2;
306     x |= x >> 1;
307
308     /* Phase 2: calculate number of "1" bits in the word        */
309     w = (x & 0x55555555) + ((x >> 1) & 0x55555555);
310     w = (w & 0x33333333) + ((w >> 2) & 0x33333333);
311     w = w + (w >> 4);
312     w = (w & 0x000F000F) + ((w >> 8) & 0x000F000F);
313     return (cyg_uint32)((w + (w >> 16)) & 0xFF) - 1;
314
315 }
316
317 /*------------------------------------------------------------------------*/
318 /* Idle thread action                                                     */
319
320 #include <cyg/infra/diag.h>
321
322 void hal_idle_thread_action( cyg_uint32 count )
323 {
324 }
325
326 /*------------------------------------------------------------------------*/
327 /* End of hal_misc.c                                                      */