1 //==========================================================================
3 // io/serial/arm/cma230_serial.c
5 // Cogent CMA230 Serial I/O Interface Module (interrupt driven)
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####
44 // Contributors: gthomas
46 // Purpose: CMA230 Serial I/O module (interrupt driven version)
49 //####DESCRIPTIONEND####
51 //==========================================================================
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>
63 #ifdef CYGPKG_IO_SERIAL_ARM_CMA230
65 #include "cma230_serial.h"
67 typedef struct cma230_serial_info {
70 cyg_interrupt serial_interrupt;
71 cyg_handle_t serial_interrupt_handle;
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,
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);
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);
88 static SERIAL_FUNS(cma230_serial_funs,
91 cma230_serial_set_config,
92 cma230_serial_start_xmit,
93 cma230_serial_stop_xmit
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];
103 static SERIAL_CHANNEL_USING_INTERRUPTS(cma230_serial_channel0,
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)
115 static SERIAL_CHANNEL(cma230_serial_channel0,
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
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,
131 cma230_serial_lookup, // Serial driver may need initializing
132 &cma230_serial_channel0
134 #endif // CYGPKG_IO_SERIAL_ARM_CMA230_SERIAL_A
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];
143 static SERIAL_CHANNEL_USING_INTERRUPTS(cma230_serial_channel1,
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)
155 static SERIAL_CHANNEL(cma230_serial_channel1,
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
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,
171 cma230_serial_lookup, // Serial driver may need initializing
172 &cma230_serial_channel1
174 #endif // CYGPKG_IO_SERIAL_ARM_CMA230_SERIAL_B
176 // Internal function to actually configure the hardware to desired baud rate, etc.
178 cma230_serial_config_port(serial_channel *chan, cyg_serial_info_t *new_config, bool init)
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
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];
192 port->mdl = baud_divisor >> 8;
193 port->ldl = baud_divisor & 0xFF;
194 port->lcr &= ~LCR_DL;
196 port->fcr = 0x07; // Enable and clear FIFO
197 if (chan->out_cbuf.len != 0) {
202 port->mcr = MCR_INT|MCR_DTR|MCR_RTS; // Master interrupt enable
206 if (new_config != &chan->config) {
207 chan->config = *new_config;
212 // Function to initialize the device. Called at bootstrap time.
214 cma230_serial_init(struct cyg_devtab_entry *tab)
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);
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
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);
233 cma230_serial_config_port(chan, &chan->config, true);
237 // This routine is called when the device is "looked" up (i.e. attached)
239 cma230_serial_lookup(struct cyg_devtab_entry **tab,
240 struct cyg_devtab_entry *sub_tab,
243 serial_channel *chan = (serial_channel *)(*tab)->priv;
244 (chan->callbacks->serial_init)(chan); // Really only required for interrupt driven devices
248 // Send a character to the device output buffer.
249 // Return 'true' if character is sent to device
251 cma230_serial_putc(serial_channel *chan, unsigned char c)
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
265 // Fetch a character from the device input buffer, waiting if necessary
267 cma230_serial_getc(serial_channel *chan)
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
277 // Set up the device characteristics; baud rate, etc.
279 cma230_serial_set_config(serial_channel *chan, cyg_uint32 key,
280 const void *xbuf, cyg_uint32 *len)
283 case CYG_IO_SET_CONFIG_SERIAL_INFO:
285 cyg_serial_info_t *config = (cyg_serial_info_t *)xbuf;
286 if ( *len < sizeof(cyg_serial_info_t) ) {
289 *len = sizeof(cyg_serial_info_t);
290 if ( true != cma230_serial_config_port(chan, config, false) )
300 // Enable the transmitter on the device
302 cma230_serial_start_xmit(serial_channel *chan)
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
309 // Disable the transmitter on the device
311 cma230_serial_stop_xmit(serial_channel *chan)
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
318 // Serial I/O - low level interrupt handler (ISR)
320 cma230_serial_ISR(cyg_vector_t vector, cyg_addrword_t data)
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
329 // Serial I/O - high level interrupt handler (DSR)
331 cma230_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
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;
337 isr = port->isr & 0x0E;
339 (chan->callbacks->xmt_char)(chan);
340 } else if (isr == ISR_Rx) {
341 (chan->callbacks->rcv_char)(chan, port->rhr);
343 cyg_drv_interrupt_unmask(cma230_chan->int_num);