]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/hal/arm/at91/v2_0/src/hal_diag.c
RedBoot TX53 Release 2012-02-15
[karo-tx-redboot.git] / packages / hal / arm / at91 / v2_0 / src / hal_diag.c
1 /*=============================================================================
2 //
3 //      hal_diag.c
4 //
5 //      HAL diagnostic output code
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):   jskov
44 // Contributors:jskov, gthomas
45 // Date:        2001-07-12
46 // Purpose:     HAL diagnostic output
47 // Description: Implementations of HAL diagnostic output support.
48 //
49 //####DESCRIPTIONEND####
50 //
51 //===========================================================================*/
52
53 #include <pkgconf/hal.h>
54 #include CYGBLD_HAL_PLATFORM_H
55
56 #include <cyg/infra/cyg_type.h>         // base types
57
58 #include <cyg/hal/hal_arch.h>           // SAVE/RESTORE GP macros
59 #include <cyg/hal/hal_io.h>             // IO macros
60 #include <cyg/hal/hal_if.h>             // interface API
61 #include <cyg/hal/hal_intr.h>           // HAL_ENABLE/MASK/UNMASK_INTERRUPTS
62 #include <cyg/hal/hal_misc.h>           // Helper functions
63 #include <cyg/hal/drv_api.h>            // CYG_ISR_HANDLED
64
65 #include <cyg/hal/plf_io.h>             // USART registers
66
67 //-----------------------------------------------------------------------------
68 typedef struct {
69     cyg_uint8* base;
70     cyg_int32 msec_timeout;
71     int isr_vector;
72 } channel_data_t;
73
74 //-----------------------------------------------------------------------------
75
76 static void
77 cyg_hal_plf_serial_init_channel(void* __ch_data)
78 {
79     cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
80
81     // Reset device
82     HAL_WRITE_UINT32(base+AT91_US_CR, AT91_US_CR_RxRESET | AT91_US_CR_TxRESET);
83
84     // 8-1-no parity.
85     HAL_WRITE_UINT32(base+AT91_US_MR,
86                      AT91_US_MR_CLOCK_MCK | AT91_US_MR_LENGTH_8 |
87                      AT91_US_MR_PARITY_NONE | AT91_US_MR_STOP_1);
88
89     HAL_WRITE_UINT32(base+AT91_US_BRG, AT91_US_BAUD(CYGNUM_HAL_VIRTUAL_VECTOR_CHANNELS_DEFAULT_BAUD));
90
91     // Enable RX and TX
92     HAL_WRITE_UINT32(base+AT91_US_CR, AT91_US_CR_RxENAB | AT91_US_CR_TxENAB);
93 }
94
95 void
96 cyg_hal_plf_serial_putc(void *__ch_data, char c)
97 {
98     cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
99     cyg_uint32 status, ch;
100     CYGARC_HAL_SAVE_GP();
101
102     do {
103         HAL_READ_UINT32(base+AT91_US_CSR, status);
104     } while ((status & AT91_US_CSR_TxRDY) == 0);
105
106     ch = (cyg_uint32)c;
107     HAL_WRITE_UINT32(base+AT91_US_THR, ch);
108
109     CYGARC_HAL_RESTORE_GP();
110 }
111
112 static cyg_bool
113 cyg_hal_plf_serial_getc_nonblock(void* __ch_data, cyg_uint8* ch)
114 {
115     channel_data_t* chan = (channel_data_t*)__ch_data;
116     cyg_uint8* base = chan->base;
117     cyg_uint32 stat;
118     cyg_uint32 c;
119
120     HAL_READ_UINT32(base+AT91_US_CSR, stat);
121     if ((stat & AT91_US_CSR_RxRDY) == 0)
122         return false;
123
124     HAL_READ_UINT32(base+AT91_US_RHR, c);
125     *ch = (cyg_uint8)(c & 0xff);
126
127     return true;
128 }
129
130 cyg_uint8
131 cyg_hal_plf_serial_getc(void* __ch_data)
132 {
133     cyg_uint8 ch;
134     CYGARC_HAL_SAVE_GP();
135
136     while(!cyg_hal_plf_serial_getc_nonblock(__ch_data, &ch));
137
138     CYGARC_HAL_RESTORE_GP();
139     return ch;
140 }
141
142 static void
143 cyg_hal_plf_serial_write(void* __ch_data, const cyg_uint8* __buf, 
144                          cyg_uint32 __len)
145 {
146     CYGARC_HAL_SAVE_GP();
147
148     while(__len-- > 0)
149         cyg_hal_plf_serial_putc(__ch_data, *__buf++);
150
151     CYGARC_HAL_RESTORE_GP();
152 }
153
154 static void
155 cyg_hal_plf_serial_read(void* __ch_data, cyg_uint8* __buf, cyg_uint32 __len)
156 {
157     CYGARC_HAL_SAVE_GP();
158
159     while(__len-- > 0)
160         *__buf++ = cyg_hal_plf_serial_getc(__ch_data);
161
162     CYGARC_HAL_RESTORE_GP();
163 }
164
165 cyg_bool
166 cyg_hal_plf_serial_getc_timeout(void* __ch_data, cyg_uint8* ch)
167 {
168     int delay_count;
169     channel_data_t* chan = (channel_data_t*)__ch_data;
170     cyg_bool res;
171     CYGARC_HAL_SAVE_GP();
172
173     delay_count = chan->msec_timeout * 10; // delay in .1 ms steps
174
175     for(;;) {
176         res = cyg_hal_plf_serial_getc_nonblock(__ch_data, ch);
177         if (res || 0 == delay_count--)
178             break;
179         
180         CYGACC_CALL_IF_DELAY_US(100);
181     }
182
183     CYGARC_HAL_RESTORE_GP();
184     return res;
185 }
186
187 static int
188 cyg_hal_plf_serial_control(void *__ch_data, __comm_control_cmd_t __func, ...)
189 {
190     static int irq_state = 0;
191     channel_data_t* chan = (channel_data_t*)__ch_data;
192     cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
193     int ret = 0;
194     CYGARC_HAL_SAVE_GP();
195
196     switch (__func) {
197     case __COMMCTL_IRQ_ENABLE:
198         irq_state = 1;
199         HAL_INTERRUPT_ACKNOWLEDGE(chan->isr_vector);
200         HAL_INTERRUPT_UNMASK(chan->isr_vector);
201         HAL_WRITE_UINT32(base+AT91_US_IER, AT91_US_IER_RxRDY);
202         break;
203     case __COMMCTL_IRQ_DISABLE:
204         ret = irq_state;
205         irq_state = 0;
206         HAL_INTERRUPT_MASK(chan->isr_vector);
207         HAL_WRITE_UINT32(base+AT91_US_IDR, AT91_US_IER_RxRDY);
208         break;
209     case __COMMCTL_DBG_ISR_VECTOR:
210         ret = chan->isr_vector;
211         break;
212     case __COMMCTL_SET_TIMEOUT:
213     {
214         va_list ap;
215
216         va_start(ap, __func);
217
218         ret = chan->msec_timeout;
219         chan->msec_timeout = va_arg(ap, cyg_uint32);
220
221         va_end(ap);
222     }        
223     default:
224         break;
225     }
226     CYGARC_HAL_RESTORE_GP();
227     return ret;
228 }
229
230 static int
231 cyg_hal_plf_serial_isr(void *__ch_data, int* __ctrlc, 
232                        CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
233 {
234     int res = 0;
235     channel_data_t* chan = (channel_data_t*)__ch_data;
236     cyg_uint32 c;
237     cyg_uint8 ch;
238     cyg_uint32 stat;
239     CYGARC_HAL_SAVE_GP();
240
241     *__ctrlc = 0;
242     HAL_READ_UINT32(chan->base+AT91_US_CSR, stat);
243     if ( (stat & AT91_US_CSR_RxRDY) != 0 ) {
244
245         HAL_READ_UINT32(chan->base+AT91_US_RHR, c);
246         ch = (cyg_uint8)(c & 0xff);
247         if( cyg_hal_is_break( &ch , 1 ) )
248             *__ctrlc = 1;
249
250         res = CYG_ISR_HANDLED;
251     }
252
253     HAL_INTERRUPT_ACKNOWLEDGE(chan->isr_vector);
254
255     CYGARC_HAL_RESTORE_GP();
256     return res;
257 }
258
259 static channel_data_t at91_ser_channels[2] = {
260     { (cyg_uint8*)AT91_USART0, 1000, CYGNUM_HAL_INTERRUPT_USART0},
261     { (cyg_uint8*)AT91_USART1, 1000, CYGNUM_HAL_INTERRUPT_USART1}
262 };
263
264 static void
265 cyg_hal_plf_serial_init(void)
266 {
267     hal_virtual_comm_table_t* comm;
268     int cur;
269
270     cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
271
272     // Init channels
273     cyg_hal_plf_serial_init_channel(&at91_ser_channels[0]);
274 #if CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS > 1
275     cyg_hal_plf_serial_init_channel(&at91_ser_channels[1]);
276 #endif
277
278     // Setup procs in the vector table
279
280     // Set channel 0
281     CYGACC_CALL_IF_SET_CONSOLE_COMM(0);
282     comm = CYGACC_CALL_IF_CONSOLE_PROCS();
283     CYGACC_COMM_IF_CH_DATA_SET(*comm, &at91_ser_channels[0]);
284     CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_serial_write);
285     CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_serial_read);
286     CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_serial_putc);
287     CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_serial_getc);
288     CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_serial_control);
289     CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_serial_isr);
290     CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_serial_getc_timeout);
291
292 #if CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS > 1
293     // Set channel 1
294     CYGACC_CALL_IF_SET_CONSOLE_COMM(1);
295     comm = CYGACC_CALL_IF_CONSOLE_PROCS();
296     CYGACC_COMM_IF_CH_DATA_SET(*comm, &at91_ser_channels[1]);
297     CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_serial_write);
298     CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_serial_read);
299     CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_serial_putc);
300     CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_serial_getc);
301     CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_serial_control);
302     CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_serial_isr);
303     CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_serial_getc_timeout);
304 #endif
305
306     // Restore original console
307     CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
308 }
309
310 void
311 cyg_hal_plf_comms_init(void)
312 {
313     static int initialized = 0;
314
315     if (initialized)
316         return;
317
318     initialized = 1;
319
320     cyg_hal_plf_serial_init();
321 }
322
323 //-----------------------------------------------------------------------------
324 // End of hal_diag.c