]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/hal/arm/arm9/smdk2410/v2_0/src/hal_diag.c
Initial revision
[karo-tx-redboot.git] / packages / hal / arm / arm9 / smdk2410 / 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):    michael anburaj <michaelanburaj@hotmail.com>
44 // Contributors: michael anburaj <michaelanburaj@hotmail.com>
45 // Date:         2003-08-01
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_VARIANT_H           // Variant specific configuration
55 #include CYGBLD_HAL_PLATFORM_H          // Platform specific configuration
56
57 #include <cyg/infra/cyg_type.h>         // base types
58 #include <cyg/infra/cyg_trac.h>         // tracing macros
59 #include <cyg/infra/cyg_ass.h>          // assertion macros
60
61 #include <cyg/hal/hal_arch.h>           // basic machine info
62 #include <cyg/hal/hal_intr.h>           // interrupt macros
63 #include <cyg/hal/hal_io.h>             // IO macros
64 #include <cyg/hal/hal_diag.h>
65 #include <cyg/hal/drv_api.h>
66 #include <cyg/hal/hal_if.h>             // interface API
67 #include <cyg/hal/hal_misc.h>           // Helper functions
68 #include <cyg/hal/s3c2410x.h>           // platform definitions
69
70 //-----------------------------------------------------------------------------
71 typedef struct {
72     cyg_uint32 base;
73     cyg_int32 msec_timeout;
74     int isr_vector;
75 } channel_data_t;
76
77 static channel_data_t smdk_ser_channels[2] = {
78     {(cyg_uint32)ULCON0, 1000, CYGNUM_HAL_INTERRUPT_UART0},
79     {(cyg_uint32)ULCON1, 1000, CYGNUM_HAL_INTERRUPT_UART1}
80 };
81
82
83 #define __READ_UINT32( _register_ ) *((volatile CYG_WORD32 *)(_register_))
84
85
86 //-----------------------------------------------------------------------------
87
88 static void
89 cyg_hal_plf_serial_init_channel(void* __ch_data)
90 {
91     cyg_uint32 base = ((channel_data_t*)__ch_data)->base;
92
93     //UART FIFO control register
94     HAL_WRITE_UINT32(base+OFS_UFCON, (3<<6) | (3<<4) | (1<<2) | (1<<1) | (1<<0));
95
96     //UART modem control register
97     HAL_WRITE_UINT32(base+OFS_UMCON, 0);
98
99     //UART line control register: Normal,No parity,1 stop,8 bits
100     HAL_WRITE_UINT32(base+OFS_ULCON, 0x3);
101
102     //UART control register
103     HAL_WRITE_UINT32(base+OFS_UCON, 0x245);
104
105     //UART baud divider register
106     HAL_WRITE_UINT32(base+OFS_UBRDIV,  (cyg_uint32)((FCLK/4)/16./CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD+0.5) -1);
107 }
108
109 void
110 cyg_hal_plf_serial_putc(void* __ch_data, char c)
111 {
112     cyg_uint32 base = ((channel_data_t*)__ch_data)->base;
113     cyg_uint32 status;
114     CYGARC_HAL_SAVE_GP();
115
116     // Wait for Tx FIFO not full
117     do
118     {
119         HAL_READ_UINT32(base+OFS_UFSTAT, status);
120     }
121     while (status & 0x200) ;
122
123     //UART TX data register
124     HAL_WRITE_UINT8(base+OFS_UTXH, c);
125
126     CYGARC_HAL_RESTORE_GP();
127 }
128
129 static cyg_bool
130 cyg_hal_plf_serial_getc_nonblock(void* __ch_data, cyg_uint8* ch)
131 {
132     cyg_uint32 base = ((channel_data_t*)__ch_data)->base;
133     cyg_uint32 status;
134
135     HAL_READ_UINT32(base+OFS_UFSTAT, status);
136     if (status & 0x0f)
137     {
138         HAL_READ_UINT8(base+OFS_URXH, *ch);
139         return true;
140     }
141
142     return false;
143 }
144
145 cyg_uint8
146 cyg_hal_plf_serial_getc(void* __ch_data)
147 {
148     cyg_uint8 ch;
149     CYGARC_HAL_SAVE_GP();
150
151     while(!cyg_hal_plf_serial_getc_nonblock(__ch_data, &ch));
152
153     CYGARC_HAL_RESTORE_GP();
154     return ch;
155 }
156
157 static void
158 cyg_hal_plf_serial_write(void* __ch_data, const cyg_uint8* __buf, 
159                          cyg_uint32 __len)
160 {
161     CYGARC_HAL_SAVE_GP();
162
163     while(__len-- > 0)
164         cyg_hal_plf_serial_putc(__ch_data, *__buf++);
165
166     CYGARC_HAL_RESTORE_GP();
167 }
168
169 static void
170 cyg_hal_plf_serial_read(void* __ch_data, cyg_uint8* __buf, cyg_uint32 __len)
171 {
172     CYGARC_HAL_SAVE_GP();
173
174     while(__len-- > 0)
175         *__buf++ = cyg_hal_plf_serial_getc(__ch_data);
176
177     CYGARC_HAL_RESTORE_GP();
178 }
179
180 cyg_bool
181 cyg_hal_plf_serial_getc_timeout(void* __ch_data, cyg_uint8* ch)
182 {
183     int delay_count;
184     channel_data_t* chan = (channel_data_t*)__ch_data;
185     cyg_bool res;
186     CYGARC_HAL_SAVE_GP();
187
188     delay_count = chan->msec_timeout * 10; // delay in .1 ms steps
189
190     for(;;) {
191         res = cyg_hal_plf_serial_getc_nonblock(__ch_data, ch);
192         if (res || 0 == delay_count--)
193             break;
194         
195         CYGACC_CALL_IF_DELAY_US(100);
196     }
197
198     CYGARC_HAL_RESTORE_GP();
199     return res;
200 }
201
202 static int
203 cyg_hal_plf_serial_control(void *__ch_data, __comm_control_cmd_t __func, ...)
204 {
205     static int irq_state = 0;
206     channel_data_t* chan = (channel_data_t*)__ch_data;
207     int ret = 0;
208     CYGARC_HAL_SAVE_GP();
209
210     switch (__func) {
211     case __COMMCTL_IRQ_ENABLE:
212         irq_state = 1;
213         HAL_INTERRUPT_UNMASK(chan->isr_vector);
214         break;
215     case __COMMCTL_IRQ_DISABLE:
216         ret = irq_state;
217         irq_state = 0;
218         HAL_INTERRUPT_MASK(chan->isr_vector);
219         break;
220     case __COMMCTL_DBG_ISR_VECTOR:
221         ret = chan->isr_vector;
222         break;
223     case __COMMCTL_SET_TIMEOUT:
224     {
225         va_list ap;
226
227         va_start(ap, __func);
228
229         ret = chan->msec_timeout;
230         chan->msec_timeout = va_arg(ap, cyg_uint32);
231
232         va_end(ap);
233     }        
234     default:
235         break;
236     }
237     CYGARC_HAL_RESTORE_GP();
238     return ret;
239 }
240
241 static int
242 cyg_hal_plf_serial_isr(void *__ch_data, int* __ctrlc, 
243                        CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
244 {
245     int res = 0;
246     channel_data_t* chan = (channel_data_t*)__ch_data;
247     char c;
248     cyg_uint32 lsr;
249     CYGARC_HAL_SAVE_GP();
250
251     cyg_drv_interrupt_acknowledge(chan->isr_vector);
252
253     *__ctrlc = 0;
254     HAL_READ_UINT32(chan->base+OFS_UFSTAT, lsr);
255     if (lsr & 0x0f)
256     {
257         HAL_READ_UINT8(chan->base+OFS_URXH, c);
258         if( cyg_hal_is_break( &c , 1 ) )
259             *__ctrlc = 1;
260
261         res = CYG_ISR_HANDLED;
262     }
263
264     CYGARC_HAL_RESTORE_GP();
265     return res;
266 }
267
268 static void
269 cyg_hal_plf_serial_init(void)
270 {
271     hal_virtual_comm_table_t* comm;
272     int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
273
274     // Disable interrupts.
275     HAL_INTERRUPT_MASK(smdk_ser_channels[0].isr_vector);
276     HAL_INTERRUPT_MASK(smdk_ser_channels[1].isr_vector);
277
278     //Unmask UART0/1 RX interrupt
279     HAL_WRITE_UINT32(INTSUBMSK, __READ_UINT32(INTSUBMSK) & ~(BIT_SUB_RXD0|BIT_SUB_RXD1));
280
281     // Init channels
282     cyg_hal_plf_serial_init_channel(&smdk_ser_channels[0]);
283     cyg_hal_plf_serial_init_channel(&smdk_ser_channels[1]);
284
285     // Setup procs in the vector table
286
287     // Set channel 0
288     CYGACC_CALL_IF_SET_CONSOLE_COMM(0);
289     comm = CYGACC_CALL_IF_CONSOLE_PROCS();
290     CYGACC_COMM_IF_CH_DATA_SET(*comm, &smdk_ser_channels[0]);
291     CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_serial_write);
292     CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_serial_read);
293     CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_serial_putc);
294     CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_serial_getc);
295     CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_serial_control);
296     CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_serial_isr);
297     CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_serial_getc_timeout);
298
299     // Set channel 1
300     CYGACC_CALL_IF_SET_CONSOLE_COMM(1);
301     comm = CYGACC_CALL_IF_CONSOLE_PROCS();
302     CYGACC_COMM_IF_CH_DATA_SET(*comm, &smdk_ser_channels[1]);
303     CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_serial_write);
304     CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_serial_read);
305     CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_serial_putc);
306     CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_serial_getc);
307     CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_serial_control);
308     CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_serial_isr);
309     CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_serial_getc_timeout);
310
311     // Restore original console
312     CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
313 }
314
315 void
316 cyg_hal_plf_comms_init(void)
317 {
318     static int initialized = 0;
319
320     if (initialized)
321         return;
322
323     initialized = 1;
324
325     cyg_hal_plf_serial_init();
326 }
327
328 //-----------------------------------------------------------------------------
329 // End of hal_diag.c