]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/serial/arm/integrator/v2_0/src/integrator_serial_with_ints.c
Initial revision
[karo-tx-redboot.git] / packages / devs / serial / arm / integrator / v2_0 / src / integrator_serial_with_ints.c
1 //==========================================================================
2 //
3 //      io/serial/arm/integrator_serial_with_ints.c
4 //
5 //      ARM INTEGRATOR Serial I/O Interface Module (interrupt driven)
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):    David A Rusling
44 // Contributors: Philippe Robin
45 // Date:         November 7, 2000
46 // Purpose:      INTEGRATOR Serial I/O module (interrupt driven)
47 // Description: 
48 //
49 //####DESCRIPTIONEND####
50 //
51 //==========================================================================
52
53 #include <pkgconf/system.h>
54 #include <pkgconf/io.h>
55 #include <pkgconf/io_serial.h>
56 #include <cyg/io/io.h>
57 #include <cyg/hal/hal_intr.h>
58 #include <cyg/io/devtab.h>
59 #include <cyg/io/serial.h>
60 #include <cyg/infra/diag.h>
61
62 #ifdef CYGPKG_IO_SERIAL_ARM_INTEGRATOR
63 #include "integrator_serial.h"
64
65 typedef struct integrator_serial_info {
66     CYG_ADDRWORD   base;
67     CYG_WORD       int_num;
68     cyg_interrupt  serial_interrupt;
69     cyg_handle_t   serial_interrupt_handle;
70 } integrator_serial_info;
71
72 static bool integrator_serial_init(struct cyg_devtab_entry *tab);
73 static bool integrator_serial_putc(serial_channel *chan, unsigned char c);
74 static Cyg_ErrNo integrator_serial_lookup(struct cyg_devtab_entry **tab, 
75                                    struct cyg_devtab_entry *sub_tab,
76                                    const char *name);
77 static unsigned char integrator_serial_getc(serial_channel *chan);
78 static Cyg_ErrNo integrator_serial_set_config(serial_channel *chan, cyg_uint32 key,
79                                               const void *xbuf, cyg_uint32 *len);
80 static void integrator_serial_start_xmit(serial_channel *chan);
81 static void integrator_serial_stop_xmit(serial_channel *chan);
82
83 static cyg_uint32 integrator_serial_ISR(cyg_vector_t vector, cyg_addrword_t data);
84 static void       integrator_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data);
85
86 static SERIAL_FUNS(integrator_serial_funs, 
87                    integrator_serial_putc, 
88                    integrator_serial_getc,
89                    integrator_serial_set_config,
90                    integrator_serial_start_xmit,
91                    integrator_serial_stop_xmit
92     );
93
94 #ifdef CYGPKG_IO_SERIAL_ARM_INTEGRATOR_SERIAL0
95 #define INTEGRATOR_UART0_BASE           0x16000000       /*  UART 0 */
96 static integrator_serial_info integrator_serial_info0 = {INTEGRATOR_UART0_BASE, CYGNUM_HAL_INTERRUPT_UARTINT0};
97 #if CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL0_BUFSIZE > 0
98 static unsigned char integrator_serial_out_buf0[CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL0_BUFSIZE];
99 static unsigned char integrator_serial_in_buf0[CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL0_BUFSIZE];
100
101 static SERIAL_CHANNEL_USING_INTERRUPTS(integrator_serial_channel0,
102                                        integrator_serial_funs, 
103                                        integrator_serial_info0,
104                                        CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL0_BAUD),
105                                        CYG_SERIAL_STOP_DEFAULT,
106                                        CYG_SERIAL_PARITY_DEFAULT,
107                                        CYG_SERIAL_WORD_LENGTH_DEFAULT,
108                                        CYG_SERIAL_FLAGS_DEFAULT,
109                                        &integrator_serial_out_buf0[0], sizeof(integrator_serial_out_buf0),
110                                        &integrator_serial_in_buf0[0], sizeof(integrator_serial_in_buf0)
111     );
112 #else
113 static SERIAL_CHANNEL(integrator_serial_channel0,
114                       integrator_serial_funs, 
115                       integrator_serial_info0,
116                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL0_BAUD),
117                       CYG_SERIAL_STOP_DEFAULT,
118                       CYG_SERIAL_PARITY_DEFAULT,
119                       CYG_SERIAL_WORD_LENGTH_DEFAULT,
120                       CYG_SERIAL_FLAGS_DEFAULT
121     );
122 #endif
123
124 DEVTAB_ENTRY(integrator_serial_io0, 
125              CYGDAT_IO_SERIAL_ARM_INTEGRATOR_SERIAL0_NAME,
126              0,                     // Does not depend on a lower level interface
127              &cyg_io_serial_devio, 
128              integrator_serial_init, 
129              integrator_serial_lookup,     // Serial driver may need initializing
130              &integrator_serial_channel0
131     );
132 #endif //  CYGPKG_IO_SERIAL_ARM_INTEGRATOR_SERIAL0
133
134 #ifdef CYGPKG_IO_SERIAL_ARM_INTEGRATOR_SERIAL1
135 #define INTEGRATOR_UART1_BASE           0x17000000       /*  UART 1 */
136 static integrator_serial_info integrator_serial_info1 = {INTEGRATOR_UART1_BASE, CYGNUM_HAL_INTERRUPT_UARTINT1};
137 #if CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL1_BUFSIZE > 0
138 static unsigned char integrator_serial_out_buf1[CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL1_BUFSIZE];
139 static unsigned char integrator_serial_in_buf1[CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL1_BUFSIZE];
140
141 static SERIAL_CHANNEL_USING_INTERRUPTS(integrator_serial_channel1,
142                                        integrator_serial_funs, 
143                                        integrator_serial_info1,
144                                        CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL1_BAUD),
145                                        CYG_SERIAL_STOP_DEFAULT,
146                                        CYG_SERIAL_PARITY_DEFAULT,
147                                        CYG_SERIAL_WORD_LENGTH_DEFAULT,
148                                        CYG_SERIAL_FLAGS_DEFAULT,
149                                        &integrator_serial_out_buf1[0], sizeof(integrator_serial_out_buf1),
150                                        &integrator_serial_in_buf1[0], sizeof(integrator_serial_in_buf1)
151     );
152 #else
153 static SERIAL_CHANNEL(integrator_serial_channel1,
154                       integrator_serial_funs, 
155                       integrator_serial_info1,
156                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL1_BAUD),
157                       CYG_SERIAL_STOP_DEFAULT,
158                       CYG_SERIAL_PARITY_DEFAULT,
159                       CYG_SERIAL_WORD_LENGTH_DEFAULT,
160                       CYG_SERIAL_FLAGS_DEFAULT
161     );
162 #endif
163
164 DEVTAB_ENTRY(integrator_serial_io1, 
165              CYGDAT_IO_SERIAL_ARM_INTEGRATOR_SERIAL1_NAME,
166              0,                     // Does not depend on a lower level interface
167              &cyg_io_serial_devio, 
168              integrator_serial_init, 
169              integrator_serial_lookup,     // Serial driver may need initializing
170              &integrator_serial_channel1
171     );
172 #endif //  CYGPKG_IO_SERIAL_ARM_INTEGRATOR_SERIAL1
173
174 #define GET_INTERRUPT_STATUS(p)           IO_READ((p) + AMBA_UARTIIR)
175 #define GET_STATUS(p)                     (IO_READ((p) + AMBA_UARTFR))
176 #define GET_CHAR(p)                       (IO_READ((p) + AMBA_UARTDR))
177 #define PUT_CHAR(p, c)                    (IO_WRITE(((p) + AMBA_UARTDR), (c)))
178 #define IO_READ(p)                        ((*(volatile unsigned int *)(p)) & 0xFF)
179 #define IO_WRITE(p, c)                    (*(unsigned int *)(p) = (c))
180 #define RX_DATA(s)                        (((s) & AMBA_UARTFR_RXFE) == 0)
181 #define TX_READY(s)                       (((s) & AMBA_UARTFR_TXFF) == 0)
182 #define TX_EMPTY(p)                       ((GET_STATUS(p) & AMBA_UARTFR_TMSK) == 0)
183
184 // debugging help
185 static int chars_rx = 0 ;
186 static int chars_tx = 0 ;
187
188 // Internal function to actually configure the hardware to desired baud rate, etc.
189 static bool
190 integrator_serial_config_port(serial_channel *chan, cyg_serial_info_t *new_config, bool init)
191 {
192     integrator_serial_info *integrator_chan = (integrator_serial_info *)chan->dev_priv;
193     unsigned int port = (unsigned int)integrator_chan->base;
194     unsigned short baud_divisor = select_baud[new_config->baud];
195     unsigned char _lcr ;
196
197     // don't do this baud rate...
198     if (baud_divisor == 0) return false;  // Invalid configuration
199
200     // first, disable everything 
201     IO_WRITE(port + AMBA_UARTCR, 0x0);
202
203     // Set baud rate 
204     IO_WRITE(port + AMBA_UARTLCR_M, ((baud_divisor & 0xf00) >> 8));
205     IO_WRITE(port + AMBA_UARTLCR_L, (baud_divisor & 0xff));
206     
207     // ----------v----------v----------v----------v---------- 
208     // NOTE: MUST BE WRITTEN LAST (AFTER UARTLCR_M & UARTLCR_L) 
209     // ----------^----------^----------^----------^----------
210     _lcr = 
211       select_word_length[new_config->word_length - CYGNUM_SERIAL_WORD_LENGTH_5] | 
212       select_stop_bits[new_config->stop] |
213       select_parity[new_config->parity] | AMBA_UARTLCR_H_FEN ;
214     IO_WRITE(port + AMBA_UARTLCR_H, _lcr);
215     
216     // finally, enable the uart 
217     IO_WRITE(port + AMBA_UARTCR, (AMBA_UARTCR_RIE | AMBA_UARTCR_RTIE | AMBA_UARTCR_UARTEN));
218
219     // save the configuration
220     if (new_config != &chan->config) {
221         chan->config = *new_config;
222     }
223
224     // success
225     return true;
226 }
227
228 // Function to initialize the device.  Called at bootstrap time.
229 static bool 
230 integrator_serial_init(struct cyg_devtab_entry *tab)
231 {
232     serial_channel *chan = (serial_channel *)tab->priv;
233     integrator_serial_info *integrator_chan = (integrator_serial_info *)chan->dev_priv;
234 #ifdef CYGDBG_IO_INIT
235     diag_printf("INTEGRATOR SERIAL init - dev: %x.%d\n", integrator_chan->base, integrator_chan->int_num);
236 #endif
237     (chan->callbacks->serial_init)(chan);  // Really only required for interrupt driven devices
238     if (chan->out_cbuf.len != 0) {
239         cyg_drv_interrupt_create(integrator_chan->int_num,
240                                  99,                     // Priority - what goes here?
241                                  (cyg_addrword_t)chan,   //  Data item passed to interrupt handler
242                                  integrator_serial_ISR,
243                                  integrator_serial_DSR,
244                                  &integrator_chan->serial_interrupt_handle,
245                                  &integrator_chan->serial_interrupt);
246         cyg_drv_interrupt_attach(integrator_chan->serial_interrupt_handle);
247         cyg_drv_interrupt_unmask(integrator_chan->int_num);
248     }
249     integrator_serial_config_port(chan, &chan->config, true);
250     return true;
251 }
252
253 // This routine is called when the device is "looked" up (i.e. attached)
254 static Cyg_ErrNo 
255 integrator_serial_lookup(struct cyg_devtab_entry **tab, 
256                   struct cyg_devtab_entry *sub_tab,
257                   const char *name)
258 {
259     serial_channel *chan = (serial_channel *)(*tab)->priv;
260     (chan->callbacks->serial_init)(chan);  // Really only required for interrupt driven devices
261     return ENOERR;
262 }
263
264 // Send a character to the device output buffer.
265 // Return 'true' if character is sent to device
266 static bool
267 integrator_serial_putc(serial_channel *chan, unsigned char c)
268 {
269     integrator_serial_info *integrator_chan = (integrator_serial_info *)chan->dev_priv;
270     unsigned int status = GET_STATUS(integrator_chan->base) ;
271
272     if (TX_READY(status)) {
273 // Transmit buffer is empty
274         PUT_CHAR(integrator_chan->base, c) ;
275         chars_tx++ ;
276         return true;
277     } else {
278 // No space
279         return false;
280     }
281 }
282
283 // Fetch a character from the device input buffer, waiting if necessary
284 static unsigned char 
285 integrator_serial_getc(serial_channel *chan)
286 {
287     unsigned char c;
288     integrator_serial_info *integrator_chan = (integrator_serial_info *)chan->dev_priv;
289     unsigned int status ;
290
291     do {
292         status = GET_STATUS(integrator_chan->base) ;
293     } while (!RX_DATA(status)) ;                   // Wait for char
294
295     chars_rx++ ;
296
297     // get it 
298     c = GET_CHAR(integrator_chan->base) ;
299     return c;
300 }
301
302 // Set up the device characteristics; baud rate, etc.
303 static Cyg_ErrNo
304 integrator_serial_set_config(serial_channel *chan, cyg_uint32 key, const void *xbuf,
305                       cyg_uint32 *len)
306 {
307     integrator_serial_info *integrator_chan = (integrator_serial_info *)chan->dev_priv;
308
309     switch (key) {
310     case CYG_IO_SET_CONFIG_SERIAL_INFO:
311       {
312         cyg_serial_info_t *config = (cyg_serial_info_t *)xbuf;
313         if ( *len < sizeof(cyg_serial_info_t) ) {
314             return -EINVAL;
315         }
316         *len = sizeof(cyg_serial_info_t);
317         if ( true != integrator_serial_config_port(chan, config, false) )
318             return -EINVAL;
319       }
320       break;
321 #ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_HW
322 #ifdef FIXME
323     case CYG_IO_SET_CONFIG_SERIAL_HW_RX_FLOW_THROTTLE:
324       {
325           volatile struct serial_port *port = (volatile struct serial_port *)integrator_chan->base;
326           cyg_uint32 *f = (cyg_uint32 *)xbuf;
327           unsigned char mask=0;
328           if ( *len < *f )
329               return -EINVAL;
330           
331           if ( chan->config.flags & CYGNUM_SERIAL_FLOW_RTSCTS_RX )
332               mask = MCR_RTS;
333           if ( chan->config.flags & CYGNUM_SERIAL_FLOW_DSRDTR_RX )
334               mask |= MCR_DTR;
335           if (*f) // we should throttle
336               port->REG_mcr &= ~mask;
337           else // we should no longer throttle
338               port->REG_mcr |= mask;
339       }
340       break;
341     case CYG_IO_SET_CONFIG_SERIAL_HW_FLOW_CONFIG:
342         // Nothing to do because we do support both RTSCTS and DSRDTR flow
343         // control.
344         // Other targets would clear any unsupported flags here.
345         // We just return ENOERR.
346       break;
347 #else
348 #error "Flow control for Integrator not integrated!"
349 #endif
350 #endif
351     default:
352         return -EINVAL;
353     }
354     return ENOERR;
355 }
356
357 // Enable the transmitter on the device
358 static void
359 integrator_serial_start_xmit(serial_channel *chan)
360 {
361     integrator_serial_info *integrator_chan = (integrator_serial_info *)chan->dev_priv;
362
363     IO_WRITE(integrator_chan->base + AMBA_UARTCR, 
364              IO_READ(integrator_chan->base + AMBA_UARTCR) | AMBA_UARTCR_TIE);
365 }
366
367 // Disable the transmitter on the device
368 static void 
369 integrator_serial_stop_xmit(serial_channel *chan)
370 {
371     integrator_serial_info *integrator_chan = (integrator_serial_info *)chan->dev_priv;
372
373     IO_WRITE(integrator_chan->base + AMBA_UARTCR, 
374              IO_READ(integrator_chan->base + AMBA_UARTCR) & ~AMBA_UARTCR_TIE);
375 }
376
377 // Serial I/O - low level interrupt handler (ISR)
378 static cyg_uint32 
379 integrator_serial_ISR(cyg_vector_t vector, cyg_addrword_t data)
380 {
381     serial_channel *chan = (serial_channel *)data;
382     integrator_serial_info *integrator_chan = (integrator_serial_info *)chan->dev_priv;
383
384     cyg_drv_interrupt_mask(integrator_chan->int_num);
385     cyg_drv_interrupt_acknowledge(integrator_chan->int_num);
386     return CYG_ISR_CALL_DSR;  // Cause DSR to be run
387 }
388
389 // Serial I/O - high level interrupt handler (DSR)
390 static void       
391 integrator_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
392 {
393     serial_channel *chan = (serial_channel *)data;
394     integrator_serial_info *integrator_chan = (integrator_serial_info *)chan->dev_priv;
395     volatile unsigned char isr = GET_INTERRUPT_STATUS(integrator_chan->base) ;
396
397     while ((isr & (AMBA_UARTIIR_RTIS | AMBA_UARTIIR_TIS | AMBA_UARTIIR_RIS)) != 0) {
398         if (isr & AMBA_UARTIIR_TIS) {
399             (chan->callbacks->xmt_char)(chan);
400         } else if (isr & AMBA_UARTIIR_RTIS) {
401             chars_rx++ ;
402             (chan->callbacks->rcv_char)(chan, GET_CHAR(integrator_chan->base));
403         } else if (isr & AMBA_UARTIIR_RIS) {
404             chars_rx++ ;
405             (chan->callbacks->rcv_char)(chan, GET_CHAR(integrator_chan->base));
406         }
407         isr = GET_INTERRUPT_STATUS(integrator_chan->base) ;
408     }
409     cyg_drv_interrupt_unmask(integrator_chan->int_num);
410 }
411 #endif