]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/serial/arm/cma230/v2_0/src/cma230_serial.c
Initial revision
[karo-tx-redboot.git] / packages / devs / serial / arm / cma230 / v2_0 / src / cma230_serial.c
1 //==========================================================================
2 //
3 //      io/serial/arm/cma230_serial.c
4 //
5 //      Cogent CMA230 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:     CMA230 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/hal/hal_cma230.h>
61 #include <cyg/infra/diag.h>
62
63 #ifdef CYGPKG_IO_SERIAL_ARM_CMA230
64
65 #include "cma230_serial.h"
66
67 typedef struct cma230_serial_info {
68     CYG_ADDRWORD   base;
69     CYG_WORD       int_num;
70     cyg_interrupt  serial_interrupt;
71     cyg_handle_t   serial_interrupt_handle;
72 } cma230_serial_info;
73
74 static bool cma230_serial_init(struct cyg_devtab_entry *tab);
75 static bool cma230_serial_putc(serial_channel *chan, unsigned char c);
76 static Cyg_ErrNo cma230_serial_lookup(struct cyg_devtab_entry **tab, 
77                                    struct cyg_devtab_entry *sub_tab,
78                                    const char *name);
79 static unsigned char cma230_serial_getc(serial_channel *chan);
80 static Cyg_ErrNo cma230_serial_set_config(serial_channel *chan, cyg_uint32 key,
81                                           const void *xbuf, cyg_uint32 *len);
82 static void cma230_serial_start_xmit(serial_channel *chan);
83 static void cma230_serial_stop_xmit(serial_channel *chan);
84
85 static cyg_uint32 cma230_serial_ISR(cyg_vector_t vector, cyg_addrword_t data);
86 static void       cma230_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data);
87
88 static SERIAL_FUNS(cma230_serial_funs, 
89                    cma230_serial_putc, 
90                    cma230_serial_getc,
91                    cma230_serial_set_config,
92                    cma230_serial_start_xmit,
93                    cma230_serial_stop_xmit
94     );
95
96 #ifdef CYGPKG_IO_SERIAL_ARM_CMA230_SERIAL_A
97 static cma230_serial_info cma230_serial_info0 = {CMA101_DUARTA, 
98                                                  CYGNUM_HAL_INTERRUPT_SERIAL_A};
99 #if CYGNUM_IO_SERIAL_ARM_CMA230_SERIAL_A_BUFSIZE > 0
100 static unsigned char cma230_serial_out_buf0[CYGNUM_IO_SERIAL_ARM_CMA230_SERIAL_A_BUFSIZE];
101 static unsigned char cma230_serial_in_buf0[CYGNUM_IO_SERIAL_ARM_CMA230_SERIAL_A_BUFSIZE];
102
103 static SERIAL_CHANNEL_USING_INTERRUPTS(cma230_serial_channel0,
104                                        cma230_serial_funs, 
105                                        cma230_serial_info0,
106                                        CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_CMA230_SERIAL_A_BAUD),
107                                        CYG_SERIAL_STOP_DEFAULT,
108                                        CYG_SERIAL_PARITY_DEFAULT,
109                                        CYG_SERIAL_WORD_LENGTH_DEFAULT,
110                                        CYG_SERIAL_FLAGS_DEFAULT,
111                                        &cma230_serial_out_buf0[0], sizeof(cma230_serial_out_buf0),
112                                        &cma230_serial_in_buf0[0], sizeof(cma230_serial_in_buf0)
113     );
114 #else
115 static SERIAL_CHANNEL(cma230_serial_channel0,
116                       cma230_serial_funs, 
117                       cma230_serial_info0,
118                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_CMA230_SERIAL_A_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(cma230_serial_io0, 
127              CYGDAT_IO_SERIAL_ARM_CMA230_SERIAL_A_NAME,
128              0,                     // Does not depend on a lower level interface
129              &cyg_io_serial_devio, 
130              cma230_serial_init, 
131              cma230_serial_lookup,     // Serial driver may need initializing
132              &cma230_serial_channel0
133     );
134 #endif //  CYGPKG_IO_SERIAL_ARM_CMA230_SERIAL_A
135
136 #ifdef CYGPKG_IO_SERIAL_ARM_CMA230_SERIAL_B
137 static cma230_serial_info cma230_serial_info1 = {CMA101_DUARTB,
138                                                  CYGNUM_HAL_INTERRUPT_SERIAL_B};
139 #if CYGNUM_IO_SERIAL_ARM_CMA230_SERIAL_B_BUFSIZE > 0
140 static unsigned char cma230_serial_out_buf1[CYGNUM_IO_SERIAL_ARM_CMA230_SERIAL_B_BUFSIZE];
141 static unsigned char cma230_serial_in_buf1[CYGNUM_IO_SERIAL_ARM_CMA230_SERIAL_B_BUFSIZE];
142
143 static SERIAL_CHANNEL_USING_INTERRUPTS(cma230_serial_channel1,
144                                        cma230_serial_funs, 
145                                        cma230_serial_info1,
146                                        CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_CMA230_SERIAL_B_BAUD),
147                                        CYG_SERIAL_STOP_DEFAULT,
148                                        CYG_SERIAL_PARITY_DEFAULT,
149                                        CYG_SERIAL_WORD_LENGTH_DEFAULT,
150                                        CYG_SERIAL_FLAGS_DEFAULT,
151                                        &cma230_serial_out_buf1[0], sizeof(cma230_serial_out_buf1),
152                                        &cma230_serial_in_buf1[0], sizeof(cma230_serial_in_buf1)
153     );
154 #else
155 static SERIAL_CHANNEL(cma230_serial_channel1,
156                       cma230_serial_funs, 
157                       cma230_serial_info1,
158                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_CMA230_SERIAL_B_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(cma230_serial_io1, 
167              CYGDAT_IO_SERIAL_ARM_CMA230_SERIAL_B_NAME,
168              0,                     // Does not depend on a lower level interface
169              &cyg_io_serial_devio, 
170              cma230_serial_init, 
171              cma230_serial_lookup,     // Serial driver may need initializing
172              &cma230_serial_channel1
173     );
174 #endif //  CYGPKG_IO_SERIAL_ARM_CMA230_SERIAL_B
175
176 // Internal function to actually configure the hardware to desired baud rate, etc.
177 static bool
178 cma230_serial_config_port(serial_channel *chan, cyg_serial_info_t *new_config, bool init)
179 {
180     cma230_serial_info *cma230_chan = (cma230_serial_info *)chan->dev_priv;
181     volatile struct serial_port *port = (volatile struct serial_port *)cma230_chan->base;
182     unsigned short baud_divisor = select_baud[new_config->baud];
183     unsigned char _lcr, _ier;
184     if (baud_divisor == 0) return false;  // Invalid configuration
185     _ier = port->ier;
186     port->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->lcr = _lcr;
191     port->lcr |= LCR_DL;
192     port->mdl = baud_divisor >> 8;
193     port->ldl = baud_divisor & 0xFF;
194     port->lcr &= ~LCR_DL;
195     if (init) {
196         port->fcr = 0x07;  // Enable and clear FIFO
197         if (chan->out_cbuf.len != 0) {
198             port->ier = IER_RCV;
199         } else {
200             port->ier = 0;
201         }
202         port->mcr = MCR_INT|MCR_DTR|MCR_RTS;  // Master interrupt enable
203     } else {
204         port->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 cma230_serial_init(struct cyg_devtab_entry *tab)
215 {
216     serial_channel *chan = (serial_channel *)tab->priv;
217     cma230_serial_info *cma230_chan = (cma230_serial_info *)chan->dev_priv;
218 #ifdef CYGDBG_IO_INIT
219     diag_printf("CMA230 SERIAL init - dev: %x.%d\n", cma230_chan->base, cma230_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(cma230_chan->int_num,
224                                  99,                     // Priority - what goes here?
225                                  (cyg_addrword_t)chan,   //  Data item passed to interrupt handler
226                                  cma230_serial_ISR,
227                                  cma230_serial_DSR,
228                                  &cma230_chan->serial_interrupt_handle,
229                                  &cma230_chan->serial_interrupt);
230         cyg_drv_interrupt_attach(cma230_chan->serial_interrupt_handle);
231         cyg_drv_interrupt_unmask(cma230_chan->int_num);
232     }
233     cma230_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 cma230_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 cma230_serial_putc(serial_channel *chan, unsigned char c)
252 {
253     cma230_serial_info *cma230_chan = (cma230_serial_info *)chan->dev_priv;
254     volatile struct serial_port *port = (volatile struct serial_port *)cma230_chan->base;
255     if (port->lsr & LSR_THE) {
256 // Transmit buffer is empty
257         port->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 cma230_serial_getc(serial_channel *chan)
268 {
269     unsigned char c;
270     cma230_serial_info *cma230_chan = (cma230_serial_info *)chan->dev_priv;
271     volatile struct serial_port *port = (volatile struct serial_port *)cma230_chan->base;
272     while ((port->lsr & LSR_RSR) == 0) ;   // Wait for char
273     c = port->rhr;
274     return c;
275 }
276
277 // Set up the device characteristics; baud rate, etc.
278 static Cyg_ErrNo
279 cma230_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 != cma230_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 cma230_serial_start_xmit(serial_channel *chan)
303 {
304     cma230_serial_info *cma230_chan = (cma230_serial_info *)chan->dev_priv;
305     volatile struct serial_port *port = (volatile struct serial_port *)cma230_chan->base;
306     port->ier |= IER_XMT;  // Enable xmit interrupt
307 }
308
309 // Disable the transmitter on the device
310 static void 
311 cma230_serial_stop_xmit(serial_channel *chan)
312 {
313     cma230_serial_info *cma230_chan = (cma230_serial_info *)chan->dev_priv;
314     volatile struct serial_port *port = (volatile struct serial_port *)cma230_chan->base;
315     port->ier &= ~IER_XMT;  // Disable xmit interrupt
316 }
317
318 // Serial I/O - low level interrupt handler (ISR)
319 static cyg_uint32 
320 cma230_serial_ISR(cyg_vector_t vector, cyg_addrword_t data)
321 {
322     serial_channel *chan = (serial_channel *)data;
323     cma230_serial_info *cma230_chan = (cma230_serial_info *)chan->dev_priv;
324     cyg_drv_interrupt_mask(cma230_chan->int_num);
325     cyg_drv_interrupt_acknowledge(cma230_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 cma230_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
332 {
333     serial_channel *chan = (serial_channel *)data;
334     cma230_serial_info *cma230_chan = (cma230_serial_info *)chan->dev_priv;
335     volatile struct serial_port *port = (volatile struct serial_port *)cma230_chan->base;
336     unsigned char isr;
337     isr = port->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->rhr);
342     }
343     cyg_drv_interrupt_unmask(cma230_chan->int_num);
344 }
345 #endif