]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/serial/mips/atlas/v2_0/src/atlas_serial.c
Initial revision
[karo-tx-redboot.git] / packages / devs / serial / mips / atlas / v2_0 / src / atlas_serial.c
1 //==========================================================================
2 //
3 //      atlas_serial.c
4 //
5 //      Serial device driver for ATLAS on-chip serial devices
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):   dmoseley, based on POWERPC driver by jskov
44 // Contributors: gthomas, jskov, dmoseley
45 // Date:        2000-06-23
46 // Purpose:     ATLAS serial device driver
47 // Description: ATLAS serial device driver
48 //
49 // To Do:
50 //   Put in magic to effectively use the FIFOs. Transmitter FIFO fill is a
51 //   problem, and setting receiver FIFO interrupts to happen only after
52 //   n chars may conflict with hal diag.
53 //
54 //####DESCRIPTIONEND####
55 //
56 //==========================================================================
57
58 #include <pkgconf/io_serial.h>
59 #include <pkgconf/io.h>
60
61 #include <cyg/io/io.h>
62 #include <cyg/hal/hal_intr.h>
63 #include <cyg/io/devtab.h>
64 #include <cyg/infra/diag.h>
65 #include <cyg/io/serial.h>
66
67 #ifdef CYGPKG_IO_SERIAL_MIPS_ATLAS
68
69 #include "atlas_serial.h"
70
71 typedef struct atlas_serial_info {
72     CYG_ADDRWORD   base;
73     CYG_WORD       int_num;
74     cyg_interrupt  serial_interrupt;
75     cyg_handle_t   serial_interrupt_handle;
76     cyg_uint8      iir;
77 } atlas_serial_info;
78
79 static bool atlas_serial_init(struct cyg_devtab_entry *tab);
80 static bool atlas_serial_putc(serial_channel *chan, unsigned char c);
81 static Cyg_ErrNo atlas_serial_lookup(struct cyg_devtab_entry **tab, 
82                                    struct cyg_devtab_entry *sub_tab,
83                                    const char *name);
84 static unsigned char atlas_serial_getc(serial_channel *chan);
85 static Cyg_ErrNo atlas_serial_set_config(serial_channel *chan, cyg_uint32 key,
86                                          const void *xbuf, cyg_uint32 *len);
87 static void atlas_serial_start_xmit(serial_channel *chan);
88 static void atlas_serial_stop_xmit(serial_channel *chan);
89
90 static cyg_uint32 atlas_serial_ISR(cyg_vector_t vector, cyg_addrword_t data);
91 static void       atlas_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data);
92
93 static SERIAL_FUNS(atlas_serial_funs, 
94                    atlas_serial_putc, 
95                    atlas_serial_getc,
96                    atlas_serial_set_config,
97                    atlas_serial_start_xmit,
98                    atlas_serial_stop_xmit
99     );
100
101 static atlas_serial_info atlas_serial_info0 ={ATLAS_SER_16550_BASE_A,  
102                                               CYGNUM_HAL_INTERRUPT_SER};
103
104 #if CYGNUM_IO_SERIAL_MIPS_ATLAS_SERIAL_A_BUFSIZE > 0
105 static unsigned char atlas_serial_out_buf0[CYGNUM_IO_SERIAL_MIPS_ATLAS_SERIAL_A_BUFSIZE];
106 static unsigned char atlas_serial_in_buf0[CYGNUM_IO_SERIAL_MIPS_ATLAS_SERIAL_A_BUFSIZE];
107
108 static SERIAL_CHANNEL_USING_INTERRUPTS(atlas_serial_channel0,
109                                        atlas_serial_funs, 
110                                        atlas_serial_info0,
111                                        CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_MIPS_ATLAS_SERIAL_A_BAUD),
112                                        CYG_SERIAL_STOP_DEFAULT,
113                                        CYG_SERIAL_PARITY_DEFAULT,
114                                        CYG_SERIAL_WORD_LENGTH_DEFAULT,
115                                        CYG_SERIAL_FLAGS_DEFAULT,
116                                        &atlas_serial_out_buf0[0], 
117                                        sizeof(atlas_serial_out_buf0),
118                                        &atlas_serial_in_buf0[0], 
119                                        sizeof(atlas_serial_in_buf0)
120     );
121 #else
122 static SERIAL_CHANNEL(atlas_serial_channel0,
123                       atlas_serial_funs, 
124                       atlas_serial_info0,
125                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_MIPS_ATLAS_SERIAL_A_BAUD),
126                       CYG_SERIAL_STOP_DEFAULT,
127                       CYG_SERIAL_PARITY_DEFAULT,
128                       CYG_SERIAL_WORD_LENGTH_DEFAULT,
129                       CYG_SERIAL_FLAGS_DEFAULT
130     );
131 #endif
132
133 DEVTAB_ENTRY(atlas_serial_io0, 
134              CYGDAT_IO_SERIAL_MIPS_ATLAS_SERIAL_A_NAME,
135              0,                 // Does not depend on a lower level interface
136              &cyg_io_serial_devio, 
137              atlas_serial_init, 
138              atlas_serial_lookup,     // Serial driver may need initializing
139              &atlas_serial_channel0
140     );
141
142
143
144 // Internal function to actually configure the hardware to desired baud rate, etc.
145 static bool
146 atlas_serial_config_port(serial_channel *chan, cyg_serial_info_t *new_config, bool init)
147 {
148     atlas_serial_info *atlas_chan = (atlas_serial_info *)chan->dev_priv;
149     cyg_addrword_t port = atlas_chan->base;
150     cyg_uint16 baud_divisor = select_baud[new_config->baud];
151     cyg_uint8 _lcr, _ier;
152
153     if (baud_divisor == 0)
154         return false;    // Invalid baud rate selected
155
156     //
157     // We may need to increase the timeout before causing a break reset.
158     // According to the Atlas Users Manual (Document MD00005) The BRKRES
159     // register will need to be programmed with a value larger that 0xA (the default)
160     // if we are going to use a baud rate lower than 2400.
161     //
162     if (new_config->baud <= CYGNUM_SERIAL_BAUD_2400)
163     {
164         // For now, just disable the break reset entirely.
165         HAL_WRITE_UINT32(HAL_ATLAS_BRKRES, 0);
166     } else {
167         // Put the break reset state back to the default
168         HAL_WRITE_UINT32(HAL_ATLAS_BRKRES, HAL_ATLAS_BRKRES_DEFAULT_VALUE);
169     }
170
171     // Disable port interrupts while changing hardware
172     HAL_READ_UINT8(port+SER_16550_IER, _ier);
173     HAL_WRITE_UINT8(port+SER_16550_IER, 0);
174
175     // Set databits, stopbits and parity.
176     _lcr = select_word_length[(new_config->word_length -
177                                CYGNUM_SERIAL_WORD_LENGTH_5)] | 
178         select_stop_bits[new_config->stop] |
179         select_parity[new_config->parity];
180     HAL_WRITE_UINT8(port+SER_16550_LCR, _lcr);
181
182     // Set baud rate.
183     _lcr |= LCR_DL;
184     HAL_WRITE_UINT8(port+SER_16550_LCR, _lcr);
185     HAL_WRITE_UINT8(port+SER_16550_DLM, baud_divisor >> 8);
186     HAL_WRITE_UINT8(port+SER_16550_DLL, baud_divisor & 0xff);
187     _lcr &= ~LCR_DL;
188     HAL_WRITE_UINT8(port+SER_16550_LCR, _lcr);
189
190     if (init) {
191         // Enable and clear FIFO
192         HAL_WRITE_UINT8(port+SER_16550_FCR,
193                         (FCR_ENABLE | FCR_CLEAR_RCVR | FCR_CLEAR_XMIT));
194
195         if (chan->out_cbuf.len != 0) {
196             HAL_WRITE_UINT8(port+SER_16550_IER, SIO_IER_ERDAI);
197         } else {
198             HAL_WRITE_UINT8(port+SER_16550_IER, 0);
199         }
200     } else {
201         HAL_WRITE_UINT8(port+SER_16550_IER, _ier);
202     }
203     if (new_config != &chan->config) {
204         chan->config = *new_config;
205     }
206     return true;
207 }
208
209 // Function to initialize the device.  Called at bootstrap time.
210 static bool 
211 atlas_serial_init(struct cyg_devtab_entry *tab)
212 {
213     serial_channel *chan = (serial_channel *)tab->priv;
214     atlas_serial_info *atlas_chan = (atlas_serial_info *)chan->dev_priv;
215 #ifdef CYGDBG_IO_INIT
216     diag_printf("ATLAS SERIAL init - dev: %x.%d\n", atlas_chan->base, atlas_chan->int_num);
217 #endif
218     (chan->callbacks->serial_init)(chan);  // Really only required for interrupt driven devices
219     if (chan->out_cbuf.len != 0) {
220         cyg_drv_interrupt_create(atlas_chan->int_num,
221                                  0,         // can change IRQ0 priority
222                                  (cyg_addrword_t)chan,   //  Data item passed to interrupt handler
223                                  atlas_serial_ISR,
224                                  atlas_serial_DSR,
225                                  &atlas_chan->serial_interrupt_handle,
226                                  &atlas_chan->serial_interrupt);
227         cyg_drv_interrupt_attach(atlas_chan->serial_interrupt_handle);
228         cyg_drv_interrupt_unmask(atlas_chan->int_num);
229     }
230     atlas_serial_config_port(chan, &chan->config, true);
231     return true;
232 }
233
234 // This routine is called when the device is "looked" up (i.e. attached)
235 static Cyg_ErrNo 
236 atlas_serial_lookup(struct cyg_devtab_entry **tab, 
237                   struct cyg_devtab_entry *sub_tab,
238                   const char *name)
239 {
240     serial_channel *chan = (serial_channel *)(*tab)->priv;
241     (chan->callbacks->serial_init)(chan);  // Really only required for interrupt driven devices
242     return ENOERR;
243 }
244
245 // Send a character to the device output buffer.
246 // Return 'true' if character is sent to device
247 static bool
248 atlas_serial_putc(serial_channel *chan, unsigned char c)
249 {
250     atlas_serial_info *atlas_chan = (atlas_serial_info *)chan->dev_priv;
251     cyg_addrword_t port = atlas_chan->base;
252     cyg_uint8 _lsr;
253
254     HAL_READ_UINT8(port+SER_16550_LSR, _lsr);
255     if (_lsr & SIO_LSR_THRE) {
256         // Transmit buffer is empty
257         HAL_WRITE_UINT8(port+SER_16550_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 atlas_serial_getc(serial_channel *chan)
268 {
269     unsigned char c;
270     atlas_serial_info *atlas_chan = (atlas_serial_info *)chan->dev_priv;
271     cyg_addrword_t port = atlas_chan->base;
272     cyg_uint8 _lsr;
273
274     do {
275         HAL_READ_UINT8(port+SER_16550_LSR, _lsr);
276     } while ((_lsr & SIO_LSR_DR) == 0);
277     HAL_READ_UINT8(port+SER_16550_RBR, c);
278     return c;
279 }
280
281 // Set up the device characteristics; baud rate, etc.
282 static Cyg_ErrNo
283 atlas_serial_set_config(serial_channel *chan, cyg_uint32 key,
284                         const void *xbuf, cyg_uint32 *len)
285 {
286     switch (key) {
287     case CYG_IO_SET_CONFIG_SERIAL_INFO:
288       {
289         cyg_serial_info_t *config = (cyg_serial_info_t *)xbuf;
290         if ( *len < sizeof(cyg_serial_info_t) ) {
291             return -EINVAL;
292         }
293         *len = sizeof(cyg_serial_info_t);
294         if ( true != atlas_serial_config_port(chan, config, false) )
295             return -EINVAL;
296       }
297       break;
298     default:
299         return -EINVAL;
300     }
301     return ENOERR;
302 }
303
304 // Enable the transmitter on the device
305 static void
306 atlas_serial_start_xmit(serial_channel *chan)
307 {
308     atlas_serial_info *atlas_chan = (atlas_serial_info *)chan->dev_priv;
309     cyg_addrword_t port = atlas_chan->base;
310     cyg_uint8 _ier;
311
312     HAL_READ_UINT8(port+SER_16550_IER, _ier);
313     _ier |= IER_XMT;                    // Enable xmit interrupt
314     HAL_WRITE_UINT8(port+SER_16550_IER, _ier);
315
316     // We should not need to call this here.  THRE Interrupts are enabled, and the DSR
317     // below calls this function.  However, sometimes we get called with Master Interrupts
318     // disabled, and thus the DSR never runs.  This is unfortunate because it means we
319     // will be doing multiple processing steps for the same thing.
320     (chan->callbacks->xmt_char)(chan);
321 }
322
323 // Disable the transmitter on the device
324 static void 
325 atlas_serial_stop_xmit(serial_channel *chan)
326 {
327     atlas_serial_info *atlas_chan = (atlas_serial_info *)chan->dev_priv;
328     cyg_addrword_t port = atlas_chan->base;
329     cyg_uint8 _ier;
330
331     HAL_READ_UINT8(port+SER_16550_IER, _ier);
332     _ier &= ~IER_XMT;                   // Disable xmit interrupt
333     HAL_WRITE_UINT8(port+SER_16550_IER, _ier);
334 }
335
336 // Serial I/O - low level interrupt handler (ISR)
337 static cyg_uint32 
338 atlas_serial_ISR(cyg_vector_t vector, cyg_addrword_t data)
339 {
340     serial_channel *chan = (serial_channel *)data;
341     atlas_serial_info *atlas_chan = (atlas_serial_info *)chan->dev_priv;
342
343     cyg_drv_interrupt_mask(atlas_chan->int_num);
344     cyg_drv_interrupt_acknowledge(atlas_chan->int_num);
345     return CYG_ISR_CALL_DSR;  // Cause DSR to be run
346 }
347
348 // Serial I/O - high level interrupt handler (DSR)
349 static void       
350 atlas_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
351 {
352     serial_channel *chan = (serial_channel *)data;
353     atlas_serial_info *atlas_chan = (atlas_serial_info *)chan->dev_priv;
354     cyg_addrword_t port = atlas_chan->base;
355     cyg_uint8 _iir;
356
357     HAL_READ_UINT8(port+SER_16550_IIR, _iir);
358     _iir &= SIO_IIR_ID_MASK;
359     if ( ISR_Tx_Empty == _iir ) {
360         (chan->callbacks->xmt_char)(chan);
361     } else if (( ISR_Rx_Avail == _iir ) || ( ISR_Rx_Char_Timeout == _iir )) {
362         cyg_uint8 _c;
363         HAL_READ_UINT8(port+SER_16550_RBR, _c);
364         (chan->callbacks->rcv_char)(chan, _c);
365     }
366
367     cyg_drv_interrupt_unmask(atlas_chan->int_num);
368 }
369
370 #endif
371
372 //-------------------------------------------------------------------------
373 // EOF atlas_serial.c