1 //==========================================================================
5 // Serial device driver for ATLAS on-chip serial devices
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.
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.
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
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.
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.
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.
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####
43 // Author(s): dmoseley, based on POWERPC driver by jskov
44 // Contributors: gthomas, jskov, dmoseley
46 // Purpose: ATLAS serial device driver
47 // Description: ATLAS serial device driver
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.
54 //####DESCRIPTIONEND####
56 //==========================================================================
58 #include <pkgconf/io_serial.h>
59 #include <pkgconf/io.h>
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>
67 #ifdef CYGPKG_IO_SERIAL_MIPS_ATLAS
69 #include "atlas_serial.h"
71 typedef struct atlas_serial_info {
74 cyg_interrupt serial_interrupt;
75 cyg_handle_t serial_interrupt_handle;
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,
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);
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);
93 static SERIAL_FUNS(atlas_serial_funs,
96 atlas_serial_set_config,
97 atlas_serial_start_xmit,
98 atlas_serial_stop_xmit
101 static atlas_serial_info atlas_serial_info0 ={ATLAS_SER_16550_BASE_A,
102 CYGNUM_HAL_INTERRUPT_SER};
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];
108 static SERIAL_CHANNEL_USING_INTERRUPTS(atlas_serial_channel0,
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)
122 static SERIAL_CHANNEL(atlas_serial_channel0,
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
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,
138 atlas_serial_lookup, // Serial driver may need initializing
139 &atlas_serial_channel0
144 // Internal function to actually configure the hardware to desired baud rate, etc.
146 atlas_serial_config_port(serial_channel *chan, cyg_serial_info_t *new_config, bool init)
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;
153 if (baud_divisor == 0)
154 return false; // Invalid baud rate selected
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.
162 if (new_config->baud <= CYGNUM_SERIAL_BAUD_2400)
164 // For now, just disable the break reset entirely.
165 HAL_WRITE_UINT32(HAL_ATLAS_BRKRES, 0);
167 // Put the break reset state back to the default
168 HAL_WRITE_UINT32(HAL_ATLAS_BRKRES, HAL_ATLAS_BRKRES_DEFAULT_VALUE);
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);
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);
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);
188 HAL_WRITE_UINT8(port+SER_16550_LCR, _lcr);
191 // Enable and clear FIFO
192 HAL_WRITE_UINT8(port+SER_16550_FCR,
193 (FCR_ENABLE | FCR_CLEAR_RCVR | FCR_CLEAR_XMIT));
195 if (chan->out_cbuf.len != 0) {
196 HAL_WRITE_UINT8(port+SER_16550_IER, SIO_IER_ERDAI);
198 HAL_WRITE_UINT8(port+SER_16550_IER, 0);
201 HAL_WRITE_UINT8(port+SER_16550_IER, _ier);
203 if (new_config != &chan->config) {
204 chan->config = *new_config;
209 // Function to initialize the device. Called at bootstrap time.
211 atlas_serial_init(struct cyg_devtab_entry *tab)
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);
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
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);
230 atlas_serial_config_port(chan, &chan->config, true);
234 // This routine is called when the device is "looked" up (i.e. attached)
236 atlas_serial_lookup(struct cyg_devtab_entry **tab,
237 struct cyg_devtab_entry *sub_tab,
240 serial_channel *chan = (serial_channel *)(*tab)->priv;
241 (chan->callbacks->serial_init)(chan); // Really only required for interrupt driven devices
245 // Send a character to the device output buffer.
246 // Return 'true' if character is sent to device
248 atlas_serial_putc(serial_channel *chan, unsigned char c)
250 atlas_serial_info *atlas_chan = (atlas_serial_info *)chan->dev_priv;
251 cyg_addrword_t port = atlas_chan->base;
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);
265 // Fetch a character from the device input buffer, waiting if necessary
267 atlas_serial_getc(serial_channel *chan)
270 atlas_serial_info *atlas_chan = (atlas_serial_info *)chan->dev_priv;
271 cyg_addrword_t port = atlas_chan->base;
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);
281 // Set up the device characteristics; baud rate, etc.
283 atlas_serial_set_config(serial_channel *chan, cyg_uint32 key,
284 const void *xbuf, cyg_uint32 *len)
287 case CYG_IO_SET_CONFIG_SERIAL_INFO:
289 cyg_serial_info_t *config = (cyg_serial_info_t *)xbuf;
290 if ( *len < sizeof(cyg_serial_info_t) ) {
293 *len = sizeof(cyg_serial_info_t);
294 if ( true != atlas_serial_config_port(chan, config, false) )
304 // Enable the transmitter on the device
306 atlas_serial_start_xmit(serial_channel *chan)
308 atlas_serial_info *atlas_chan = (atlas_serial_info *)chan->dev_priv;
309 cyg_addrword_t port = atlas_chan->base;
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);
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);
323 // Disable the transmitter on the device
325 atlas_serial_stop_xmit(serial_channel *chan)
327 atlas_serial_info *atlas_chan = (atlas_serial_info *)chan->dev_priv;
328 cyg_addrword_t port = atlas_chan->base;
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);
336 // Serial I/O - low level interrupt handler (ISR)
338 atlas_serial_ISR(cyg_vector_t vector, cyg_addrword_t data)
340 serial_channel *chan = (serial_channel *)data;
341 atlas_serial_info *atlas_chan = (atlas_serial_info *)chan->dev_priv;
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
348 // Serial I/O - high level interrupt handler (DSR)
350 atlas_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
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;
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 )) {
363 HAL_READ_UINT8(port+SER_16550_RBR, _c);
364 (chan->callbacks->rcv_char)(chan, _c);
367 cyg_drv_interrupt_unmask(atlas_chan->int_num);
372 //-------------------------------------------------------------------------
373 // EOF atlas_serial.c