]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/hal/arm/at91/v2_0/src/at91_misc.c
Initial revision
[karo-tx-redboot.git] / packages / hal / arm / at91 / v2_0 / src / at91_misc.c
1 //==========================================================================
2 //
3 //      at91_misc.c
4 //
5 //      HAL misc board support code for Atmel AT91/EB40
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):    gthomas
44 // Contributors: gthomas, jskov
45 // Date:         2001-07-12
46 // Purpose:      HAL board support
47 // Description:  Implementations of HAL board interfaces
48 //
49 //####DESCRIPTIONEND####
50 //
51 //========================================================================*/
52
53 #include <pkgconf/hal.h>
54
55 #include <cyg/infra/cyg_type.h>         // base types
56 #include <cyg/infra/cyg_trac.h>         // tracing macros
57 #include <cyg/infra/cyg_ass.h>          // assertion macros
58
59 #include <cyg/hal/hal_io.h>             // IO macros
60 #include <cyg/hal/hal_arch.h>           // Register state info
61 #include <cyg/hal/hal_diag.h>
62 #include <cyg/hal/hal_intr.h>           // necessary?
63 #include <cyg/hal/hal_cache.h>
64 #include <cyg/hal/hal_if.h>             // calling interface
65 #include <cyg/hal/hal_misc.h>           // helper functions
66 #ifdef CYGDBG_HAL_DEBUG_GDB_BREAK_SUPPORT
67 #include <cyg/hal/drv_api.h>            // HAL ISR support
68 #endif
69 #include <cyg/hal/plf_io.h>             // platform registers
70
71 static cyg_uint32 _period;
72
73 void hal_clock_initialize(cyg_uint32 period)
74 {
75     CYG_ADDRESS timer = AT91_TC+AT91_TC_TC0;
76
77     CYG_ASSERT(period < 0x10000, "Invalid clock period");
78
79     // Disable counter
80     HAL_WRITE_UINT32(timer+AT91_TC_CCR, AT91_TC_CCR_CLKDIS);
81
82     // Set registers
83     HAL_WRITE_UINT32(timer+AT91_TC_CMR, AT91_TC_CMR_CPCTRG |        // Reset counter on CPC
84                                         AT91_TC_CMR_CLKS_MCK32);    // 1 MHz
85     HAL_WRITE_UINT32(timer+AT91_TC_RC, period);
86
87     // Start timer
88     HAL_WRITE_UINT32(timer+AT91_TC_CCR, AT91_TC_CCR_TRIG | AT91_TC_CCR_CLKEN);
89
90     // Enable timer 0 interrupt    
91     HAL_WRITE_UINT32(timer+AT91_TC_IER, AT91_TC_IER_CPC);
92 }
93
94 void hal_clock_reset(cyg_uint32 vector, cyg_uint32 period)
95 {
96     CYG_ADDRESS timer = AT91_TC+AT91_TC_TC0;
97     cyg_uint32 sr;
98
99     CYG_ASSERT(period < 0x10000, "Invalid clock period");
100
101     HAL_READ_UINT32(timer+AT91_TC_SR, sr);  // Clear interrupt
102     HAL_INTERRUPT_ACKNOWLEDGE(CYGNUM_HAL_INTERRUPT_RTC);
103
104     if (period != _period) {
105         hal_clock_initialize(period);
106     }
107     _period = period;
108
109 }
110
111 void hal_clock_read(cyg_uint32 *pvalue)
112 {
113     CYG_ADDRESS timer = AT91_TC+AT91_TC_TC0;
114     cyg_uint32 val;
115
116     HAL_READ_UINT32(timer+AT91_TC_CV, val);
117     *pvalue = val;
118 }
119
120 // -------------------------------------------------------------------------
121 //
122 // Delay for some number of micro-seconds
123 //   Use timer #2 in 1MHz mode
124 //
125 void hal_delay_us(cyg_int32 usecs)
126 {
127     CYG_ADDRESS timer = AT91_TC+AT91_TC_TC2;
128     cyg_uint32 stat;
129
130     // Disable counter
131     HAL_WRITE_UINT32(timer+AT91_TC_CCR, AT91_TC_CCR_CLKDIS);
132
133     // Set registers
134     HAL_WRITE_UINT32(timer+AT91_TC_CMR, AT91_TC_CMR_CLKS_MCK32);  // 1MHz
135     HAL_WRITE_UINT32(timer+AT91_TC_RA, 0);
136     HAL_WRITE_UINT32(timer+AT91_TC_RC, usecs);
137
138     // Start timer
139     HAL_WRITE_UINT32(timer+AT91_TC_CCR, AT91_TC_CCR_TRIG | AT91_TC_CCR_CLKEN);
140
141     // Wait for the compare
142     do {
143         HAL_READ_UINT32(timer+AT91_TC_SR, stat);
144     } while ((stat & AT91_TC_SR_CPC) == 0);
145 }
146
147 // -------------------------------------------------------------------------
148 // Hardware init
149 void hal_hardware_init(void)
150 {
151     // Set up eCos/ROM interfaces
152     hal_if_init();
153
154     // Reset all interrupts
155     HAL_WRITE_UINT32(AT91_AIC+AT91_AIC_IDCR, 0xFFFFFFFF);  
156
157     // Make sure interrupt controller is happy
158     HAL_WRITE_UINT32(AT91_AIC+AT91_AIC_EOI, 0xFFFFFFFF);  
159 }
160
161 //
162 // This routine is called to respond to a hardware interrupt (IRQ).  It
163 // should interrogate the hardware and return the IRQ vector number.
164
165 int hal_IRQ_handler(void)
166 {
167     cyg_uint32 irq_num;
168     cyg_uint32 ipr, imr;
169
170     HAL_READ_UINT32(AT91_AIC+AT91_AIC_IPR, ipr);
171     HAL_READ_UINT32(AT91_AIC+AT91_AIC_IMR, imr);
172     ipr &= imr;
173     for (irq_num = 0;  irq_num < 19;  irq_num++) {
174         if (ipr & (1 << irq_num)) {
175             break;
176         }
177     }
178
179     return irq_num;
180 }
181
182 //
183 // Interrupt control
184 //
185
186 void hal_interrupt_mask(int vector)
187 {
188     CYG_ASSERT(vector <= CYGNUM_HAL_ISR_MAX &&
189                vector >= CYGNUM_HAL_ISR_MIN , "Invalid vector");
190
191     HAL_WRITE_UINT32(AT91_AIC+AT91_AIC_IDCR, (1<<vector));
192 }
193
194 void hal_interrupt_unmask(int vector)
195 {
196     CYG_ASSERT(vector <= CYGNUM_HAL_ISR_MAX &&
197                vector >= CYGNUM_HAL_ISR_MIN , "Invalid vector");
198
199     HAL_WRITE_UINT32(AT91_AIC+AT91_AIC_IECR, (1<<vector));
200 }
201
202 void hal_interrupt_acknowledge(int vector)
203 {
204     CYG_ASSERT(vector <= CYGNUM_HAL_ISR_MAX &&
205                vector >= CYGNUM_HAL_ISR_MIN , "Invalid vector");
206
207     HAL_WRITE_UINT32(AT91_AIC+AT91_AIC_ICCR, (1<<vector));
208
209     // FIXME - This isn't 100% correct
210     HAL_WRITE_UINT32(AT91_AIC+AT91_AIC_EOI, 0xFFFFFFFF);  
211 }
212
213 void hal_interrupt_configure(int vector, int level, int up)
214 {
215     cyg_uint32 mode;
216
217     CYG_ASSERT(vector <= CYGNUM_HAL_ISR_MAX &&
218                vector >= CYGNUM_HAL_ISR_MIN , "Invalid vector");
219
220     if (level) {
221         if (up) {
222             mode = AT91_AIC_SMR_LEVEL_HI;
223         } else {
224             mode = AT91_AIC_SMR_LEVEL_LOW;
225         }
226     } else {
227         if (up) {
228             mode = AT91_AIC_SMR_EDGE_POS;
229         } else {
230             mode = AT91_AIC_SMR_EDGE_NEG;
231         }
232     }
233     mode |= 7;  // Default priority
234     HAL_WRITE_UINT32(AT91_AIC+(AT91_AIC_SMR0+(vector*4)), mode);
235 }
236
237 void hal_interrupt_set_level(int vector, int level)
238 {
239     cyg_uint32 mode;
240
241     CYG_ASSERT(vector <= CYGNUM_HAL_ISR_MAX &&
242                vector >= CYGNUM_HAL_ISR_MIN , "Invalid vector");
243     CYG_ASSERT(level >= 1 && level <= 7, "Invalid level");
244
245     HAL_READ_UINT32(AT91_AIC+(AT91_AIC_SMR0+(vector*4)), mode);
246     mode = (mode & ~AT91_AIC_SMR_PRIORITY) | level;
247     HAL_WRITE_UINT32(AT91_AIC+(AT91_AIC_SMR0+(vector*4)), mode);
248 }
249
250 void hal_show_IRQ(int vector, int data, int handler)
251 {
252 //    UNDEFINED(__FUNCTION__);  // FIXME
253 }
254
255 // 
256 // Diagnostic LEDs - there are three colored LEDs which can be used
257 // to send a simple diagnostic value (8 bits)
258 //
259
260 void 
261 _at91_led(int val)
262 {
263     int i, to;
264
265     HAL_WRITE_UINT32(AT91_PIO+AT91_PIO_CODR, 0x06);  // DATA+CLOCK LEDs off
266     for (to = 0;  to < 0x200000; to++) ;
267     for (i = 0;  i < 8;  i++) {        
268         HAL_WRITE_UINT32(AT91_PIO+AT91_PIO_SODR, ((val>>(7-i)) & 0x01)<<2);  // DATA LED
269         HAL_WRITE_UINT32(AT91_PIO+AT91_PIO_SODR, 0x02);  // CLOCK LED on
270         for (to = 0;  to < 0x80000; to++) ;
271         HAL_WRITE_UINT32(AT91_PIO+AT91_PIO_CODR, 0x02);  // CLOCK LED off
272         for (to = 0;  to < 0x40000; to++) ;
273         HAL_WRITE_UINT32(AT91_PIO+AT91_PIO_CODR, 0x04);  // DATA LED off
274     }
275 }
276
277 void
278 set_leds(int val)
279 {
280     HAL_WRITE_UINT32(AT91_PIO+AT91_PIO_CODR, 0x16);
281     HAL_WRITE_UINT32(AT91_PIO+AT91_PIO_SODR, val);
282 }
283
284
285 //--------------------------------------------------------------------------
286 // EOF hal_misc.c