]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/serial/arm/aeb/v2_0/src/aeb_serial.c
Initial revision
[karo-tx-redboot.git] / packages / devs / serial / arm / aeb / v2_0 / src / aeb_serial.c
1 //==========================================================================
2 //
3 //      io/serial/arm/aeb_serial.c
4 //
5 //      ARM AEB-1 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):   gthomas
44 // Contributors:  gthomas
45 // Date:        1999-02-04
46 // Purpose:     AEB-1 Serial I/O module (interrupt driven version)
47 // Description: 
48 //
49 //####DESCRIPTIONEND####
50 //
51 //==========================================================================
52
53 #include <pkgconf/system.h>
54 #include <pkgconf/io_serial.h>
55 #include <pkgconf/io.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_AEB
63
64 #include "aeb_serial.h"
65
66 typedef struct aeb_serial_info {
67     CYG_ADDRWORD   base;
68     CYG_WORD       int_num;
69     cyg_interrupt  serial_interrupt;
70     cyg_handle_t   serial_interrupt_handle;
71 } aeb_serial_info;
72
73 static bool aeb_serial_init(struct cyg_devtab_entry *tab);
74 static bool aeb_serial_putc(serial_channel *chan, unsigned char c);
75 static Cyg_ErrNo aeb_serial_lookup(struct cyg_devtab_entry **tab, 
76                                    struct cyg_devtab_entry *sub_tab,
77                                    const char *name);
78 static unsigned char aeb_serial_getc(serial_channel *chan);
79 static Cyg_ErrNo aeb_serial_set_config(serial_channel *chan, 
80                                        cyg_uint32 key,
81                                        const void *xbuf, cyg_uint32 *len);
82 static void aeb_serial_start_xmit(serial_channel *chan);
83 static void aeb_serial_stop_xmit(serial_channel *chan);
84
85 static cyg_uint32 aeb_serial_ISR(cyg_vector_t vector, cyg_addrword_t data);
86 static void       aeb_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data);
87
88 static SERIAL_FUNS(aeb_serial_funs, 
89                    aeb_serial_putc, 
90                    aeb_serial_getc,
91                    aeb_serial_set_config,
92                    aeb_serial_start_xmit,
93                    aeb_serial_stop_xmit
94     );
95
96 #ifdef CYGPKG_IO_SERIAL_ARM_AEB_SERIAL0
97 static aeb_serial_info aeb_serial_info0 = {0xFFFF0000, 
98                                            CYGNUM_HAL_INTERRUPT_UART0};
99 #if CYGNUM_IO_SERIAL_ARM_AEB_SERIAL0_BUFSIZE > 0
100 static unsigned char aeb_serial_out_buf0[CYGNUM_IO_SERIAL_ARM_AEB_SERIAL0_BUFSIZE];
101 static unsigned char aeb_serial_in_buf0[CYGNUM_IO_SERIAL_ARM_AEB_SERIAL0_BUFSIZE];
102
103 static SERIAL_CHANNEL_USING_INTERRUPTS(aeb_serial_channel0,
104                                        aeb_serial_funs, 
105                                        aeb_serial_info0,
106                                        CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_AEB_SERIAL0_BAUD),
107                                        CYG_SERIAL_STOP_DEFAULT,
108                                        CYG_SERIAL_PARITY_DEFAULT,
109                                        CYG_SERIAL_WORD_LENGTH_DEFAULT,
110                                        CYG_SERIAL_FLAGS_DEFAULT,
111                                        &aeb_serial_out_buf0[0], sizeof(aeb_serial_out_buf0),
112                                        &aeb_serial_in_buf0[0], sizeof(aeb_serial_in_buf0)
113     );
114 #else
115 static SERIAL_CHANNEL(aeb_serial_channel0,
116                       aeb_serial_funs, 
117                       aeb_serial_info0,
118                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_AEB_SERIAL0_BAUD),
119                       CYG_SERIAL_STOP_DEFAULT,
120                       CYG_SERIAL_PARITY_DEFAULT,
121                       CYG_SERIAL_WORD_LENGTH_DEFAULT,
122                       CYG_SERIAL_FLAGS_DEFAULT
123     );
124 #endif
125
126 DEVTAB_ENTRY(aeb_serial_io0, 
127              CYGDAT_IO_SERIAL_ARM_AEB_SERIAL0_NAME,
128              0,                     // Does not depend on a lower level interface
129              &cyg_io_serial_devio, 
130              aeb_serial_init, 
131              aeb_serial_lookup,     // Serial driver may need initializing
132              &aeb_serial_channel0
133     );
134 #endif //  CYGPKG_IO_SERIAL_ARM_AEB_SERIAL0
135
136 #ifdef CYGPKG_IO_SERIAL_ARM_AEB_SERIAL1
137 static aeb_serial_info aeb_serial_info1 = {0xFFFF0400, 
138                                            CYGNUM_HAL_INTERRUPT_UART1};
139 #if CYGNUM_IO_SERIAL_ARM_AEB_SERIAL1_BUFSIZE > 0
140 static unsigned char aeb_serial_out_buf1[CYGNUM_IO_SERIAL_ARM_AEB_SERIAL1_BUFSIZE];
141 static unsigned char aeb_serial_in_buf1[CYGNUM_IO_SERIAL_ARM_AEB_SERIAL1_BUFSIZE];
142
143 static SERIAL_CHANNEL_USING_INTERRUPTS(aeb_serial_channel1,
144                                        aeb_serial_funs, 
145                                        aeb_serial_info1,
146                                        CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_AEB_SERIAL1_BAUD),
147                                        CYG_SERIAL_STOP_DEFAULT,
148                                        CYG_SERIAL_PARITY_DEFAULT,
149                                        CYG_SERIAL_WORD_LENGTH_DEFAULT,
150                                        CYG_SERIAL_FLAGS_DEFAULT,
151                                        &aeb_serial_out_buf1[0], sizeof(aeb_serial_out_buf1),
152                                        &aeb_serial_in_buf1[0], sizeof(aeb_serial_in_buf1)
153     );
154 #else
155 static SERIAL_CHANNEL(aeb_serial_channel1,
156                       aeb_serial_funs, 
157                       aeb_serial_info1,
158                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_AEB_SERIAL1_BAUD),
159                       CYG_SERIAL_STOP_DEFAULT,
160                       CYG_SERIAL_PARITY_DEFAULT,
161                       CYG_SERIAL_WORD_LENGTH_DEFAULT,
162                       CYG_SERIAL_FLAGS_DEFAULT
163     );
164 #endif
165
166 DEVTAB_ENTRY(aeb_serial_io1, 
167              CYGDAT_IO_SERIAL_ARM_AEB_SERIAL1_NAME,
168              0,                     // Does not depend on a lower level interface
169              &cyg_io_serial_devio, 
170              aeb_serial_init, 
171              aeb_serial_lookup,     // Serial driver may need initializing
172              &aeb_serial_channel1
173     );
174 #endif //  CYGPKG_IO_SERIAL_ARM_AEB_SERIAL1
175
176 // Internal function to actually configure the hardware to desired baud rate, etc.
177 static bool
178 aeb_serial_config_port(serial_channel *chan, cyg_serial_info_t *new_config, bool init)
179 {
180     aeb_serial_info *aeb_chan = (aeb_serial_info *)chan->dev_priv;
181     volatile struct serial_port *port = (volatile struct serial_port *)aeb_chan->base;
182     unsigned short baud_divisor = select_baud[new_config->baud];
183     unsigned char _lcr, _ier;
184     if (baud_divisor == 0) return false;
185     _ier = port->REG_IER;
186     port->REG_IER = 0;  // Disable port interrupts while changing hardware
187     _lcr = select_word_length[new_config->word_length - CYGNUM_SERIAL_WORD_LENGTH_5] | 
188         select_stop_bits[new_config->stop] |
189         select_parity[new_config->parity];
190     port->REG_LCR = _lcr;
191     port->REG_LCR |= LCR_DL;
192     port->REG_MDL = baud_divisor >> 8;
193     port->REG_LDL = baud_divisor & 0xFF;
194     port->REG_LCR &= ~LCR_DL;
195     if (init) {
196         port->REG_FCR = 0x07;  // Enable and clear FIFO
197         if (chan->out_cbuf.len != 0) {
198             port->REG_IER = IER_RCV;
199         } else {
200             port->REG_IER = 0;
201         }
202         port->REG_MCR = MCR_INT|MCR_DTR|MCR_RTS;  // Master interrupt enable
203     } else {
204         port->REG_IER = _ier;
205     }
206     if (new_config != &chan->config) {
207         chan->config = *new_config;
208     }
209     return true;
210 }
211
212 // Function to initialize the device.  Called at bootstrap time.
213 static bool 
214 aeb_serial_init(struct cyg_devtab_entry *tab)
215 {
216     serial_channel *chan = (serial_channel *)tab->priv;
217     aeb_serial_info *aeb_chan = (aeb_serial_info *)chan->dev_priv;
218 #ifdef CYGDBG_IO_INIT
219     diag_printf("AEB SERIAL init - dev: %x.%d\n", aeb_chan->base, aeb_chan->int_num);
220 #endif
221     (chan->callbacks->serial_init)(chan);  // Really only required for interrupt driven devices
222     if (chan->out_cbuf.len != 0) {
223         cyg_drv_interrupt_create(aeb_chan->int_num,
224                                  99,                     // Priority - unused
225                                  (cyg_addrword_t)chan,   //  Data item passed to interrupt handler
226                                  aeb_serial_ISR,
227                                  aeb_serial_DSR,
228                                  &aeb_chan->serial_interrupt_handle,
229                                  &aeb_chan->serial_interrupt);
230         cyg_drv_interrupt_attach(aeb_chan->serial_interrupt_handle);
231         cyg_drv_interrupt_unmask(aeb_chan->int_num);
232     }
233     aeb_serial_config_port(chan, &chan->config, true);
234     return true;
235 }
236
237 // This routine is called when the device is "looked" up (i.e. attached)
238 static Cyg_ErrNo 
239 aeb_serial_lookup(struct cyg_devtab_entry **tab, 
240                   struct cyg_devtab_entry *sub_tab,
241                   const char *name)
242 {
243     serial_channel *chan = (serial_channel *)(*tab)->priv;
244     (chan->callbacks->serial_init)(chan);  // Really only required for interrupt driven devices
245     return ENOERR;
246 }
247
248 // Send a character to the device output buffer.
249 // Return 'true' if character is sent to device
250 static bool
251 aeb_serial_putc(serial_channel *chan, unsigned char c)
252 {
253     aeb_serial_info *aeb_chan = (aeb_serial_info *)chan->dev_priv;
254     volatile struct serial_port *port = (volatile struct serial_port *)aeb_chan->base;
255     if (port->REG_LSR & LSR_THE) {
256 // Transmit buffer is empty
257         port->REG_THR = c;
258         return true;
259     } else {
260 // No space
261         return false;
262     }
263 }
264
265 // Fetch a character from the device input buffer, waiting if necessary
266 static unsigned char 
267 aeb_serial_getc(serial_channel *chan)
268 {
269     unsigned char c;
270     aeb_serial_info *aeb_chan = (aeb_serial_info *)chan->dev_priv;
271     volatile struct serial_port *port = (volatile struct serial_port *)aeb_chan->base;
272     while ((port->REG_LSR & LSR_RSR) == 0) ;   // Wait for char
273     c = port->REG_RHR;
274     return c;
275 }
276
277 // Set up the device characteristics; baud rate, etc.
278 static Cyg_ErrNo
279 aeb_serial_set_config(serial_channel *chan, cyg_uint32 key,
280                       const void *xbuf, cyg_uint32 *len)
281 {
282     switch (key) {
283     case CYG_IO_SET_CONFIG_SERIAL_INFO:
284       {
285         cyg_serial_info_t *config = (cyg_serial_info_t *)xbuf;
286         if ( *len < sizeof(cyg_serial_info_t) ) {
287             return -EINVAL;
288         }
289         *len = sizeof(cyg_serial_info_t);
290         if ( true != aeb_serial_config_port(chan, config, false) )
291             return -EINVAL;
292       }
293       break;
294     default:
295         return -EINVAL;
296     }
297     return ENOERR;
298 }
299
300 // Enable the transmitter on the device
301 static void
302 aeb_serial_start_xmit(serial_channel *chan)
303 {
304     aeb_serial_info *aeb_chan = (aeb_serial_info *)chan->dev_priv;
305     volatile struct serial_port *port = (volatile struct serial_port *)aeb_chan->base;
306     port->REG_IER |= IER_XMT;  // Enable xmit interrupt
307 }
308
309 // Disable the transmitter on the device
310 static void 
311 aeb_serial_stop_xmit(serial_channel *chan)
312 {
313     aeb_serial_info *aeb_chan = (aeb_serial_info *)chan->dev_priv;
314     volatile struct serial_port *port = (volatile struct serial_port *)aeb_chan->base;
315     port->REG_IER &= ~IER_XMT;  // Disable xmit interrupt
316 }
317
318 // Serial I/O - low level interrupt handler (ISR)
319 static cyg_uint32 
320 aeb_serial_ISR(cyg_vector_t vector, cyg_addrword_t data)
321 {
322     serial_channel *chan = (serial_channel *)data;
323     aeb_serial_info *aeb_chan = (aeb_serial_info *)chan->dev_priv;
324     cyg_drv_interrupt_mask(aeb_chan->int_num);
325     cyg_drv_interrupt_acknowledge(aeb_chan->int_num);
326     return CYG_ISR_CALL_DSR;  // Cause DSR to be run
327 }
328
329 // Serial I/O - high level interrupt handler (DSR)
330 static void       
331 aeb_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
332 {
333     serial_channel *chan = (serial_channel *)data;
334     aeb_serial_info *aeb_chan = (aeb_serial_info *)chan->dev_priv;
335     volatile struct serial_port *port = (volatile struct serial_port *)aeb_chan->base;
336     unsigned char isr;
337     isr = port->REG_ISR & 0x0E;
338     if (isr == ISR_Tx) {
339         (chan->callbacks->xmt_char)(chan);
340     } else if (isr == ISR_Rx) {
341         (chan->callbacks->rcv_char)(chan, port->REG_RHR);
342     }
343     cyg_drv_interrupt_unmask(aeb_chan->int_num);
344 }
345 #endif