1 //==========================================================================
3 // io/serial/arm/sa11x0/sa11x0_serial.c
5 // StrongARM SA11x0 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: StrongARM SA11x0 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 <pkgconf/kernel.h>
58 #include <cyg/io/io.h>
59 #include <cyg/hal/hal_intr.h>
60 #include <cyg/io/devtab.h>
61 #include <cyg/io/serial.h>
62 #include <cyg/infra/diag.h>
64 #ifdef CYGPKG_IO_SERIAL_ARM_SA11X0
66 #include "sa11x0_serial.h"
68 typedef struct sa11x0_serial_info {
71 cyg_interrupt serial_interrupt;
72 cyg_handle_t serial_interrupt_handle;
75 static bool sa11x0_serial_init(struct cyg_devtab_entry *tab);
76 static bool sa11x0_serial_putc(serial_channel *chan, unsigned char c);
77 static Cyg_ErrNo sa11x0_serial_lookup(struct cyg_devtab_entry **tab,
78 struct cyg_devtab_entry *sub_tab,
80 static unsigned char sa11x0_serial_getc(serial_channel *chan);
81 static Cyg_ErrNo sa11x0_serial_set_config(serial_channel *chan, cyg_uint32 key,
82 const void *xbuf, cyg_uint32 *len);
83 static void sa11x0_serial_start_xmit(serial_channel *chan);
84 static void sa11x0_serial_stop_xmit(serial_channel *chan);
86 static cyg_uint32 sa11x0_serial_ISR(cyg_vector_t vector, cyg_addrword_t data);
87 static void sa11x0_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data);
89 static SERIAL_FUNS(sa11x0_serial_funs,
92 sa11x0_serial_set_config,
93 sa11x0_serial_start_xmit,
94 sa11x0_serial_stop_xmit
97 #ifdef CYGPKG_IO_SERIAL_ARM_SA11X0_SERIAL0
98 static sa11x0_serial_info sa11x0_serial_info0 = {(CYG_ADDRWORD)SA11X0_UART3_CONTROL0,
99 CYGNUM_HAL_INTERRUPT_UART3};
100 #if CYGNUM_IO_SERIAL_ARM_SA11X0_SERIAL0_BUFSIZE > 0
101 static unsigned char sa11x0_serial_out_buf0[CYGNUM_IO_SERIAL_ARM_SA11X0_SERIAL0_BUFSIZE];
102 static unsigned char sa11x0_serial_in_buf0[CYGNUM_IO_SERIAL_ARM_SA11X0_SERIAL0_BUFSIZE];
104 static SERIAL_CHANNEL_USING_INTERRUPTS(sa11x0_serial_channel0,
107 CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_SA11X0_SERIAL0_BAUD),
108 CYG_SERIAL_STOP_DEFAULT,
109 CYG_SERIAL_PARITY_DEFAULT,
110 CYG_SERIAL_WORD_LENGTH_DEFAULT,
111 CYG_SERIAL_FLAGS_DEFAULT,
112 &sa11x0_serial_out_buf0[0], sizeof(sa11x0_serial_out_buf0),
113 &sa11x0_serial_in_buf0[0], sizeof(sa11x0_serial_in_buf0)
116 static SERIAL_CHANNEL(sa11x0_serial_channel0,
119 CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_SA11X0_SERIAL0_BAUD),
120 CYG_SERIAL_STOP_DEFAULT,
121 CYG_SERIAL_PARITY_DEFAULT,
122 CYG_SERIAL_WORD_LENGTH_DEFAULT,
123 CYG_SERIAL_FLAGS_DEFAULT
127 DEVTAB_ENTRY(sa11x0_serial_io0,
128 CYGDAT_IO_SERIAL_ARM_SA11X0_SERIAL0_NAME,
129 0, // Does not depend on a lower level interface
130 &cyg_io_serial_devio,
132 sa11x0_serial_lookup, // Serial driver may need initializing
133 &sa11x0_serial_channel0
135 #endif // CYGPKG_IO_SERIAL_ARM_SA11X0_SERIAL1
137 #ifdef CYGPKG_IO_SERIAL_ARM_SA11X0_SERIAL1
138 static sa11x0_serial_info sa11x0_serial_info1 = {(CYG_ADDRWORD)SA11X0_UART1_CONTROL0,
139 CYGNUM_HAL_INTERRUPT_UART1};
140 #if CYGNUM_IO_SERIAL_ARM_SA11X0_SERIAL1_BUFSIZE > 0
141 static unsigned char sa11x0_serial_out_buf1[CYGNUM_IO_SERIAL_ARM_SA11X0_SERIAL1_BUFSIZE];
142 static unsigned char sa11x0_serial_in_buf1[CYGNUM_IO_SERIAL_ARM_SA11X0_SERIAL1_BUFSIZE];
144 static SERIAL_CHANNEL_USING_INTERRUPTS(sa11x0_serial_channel1,
147 CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_SA11X0_SERIAL1_BAUD),
148 CYG_SERIAL_STOP_DEFAULT,
149 CYG_SERIAL_PARITY_DEFAULT,
150 CYG_SERIAL_WORD_LENGTH_DEFAULT,
151 CYG_SERIAL_FLAGS_DEFAULT,
152 &sa11x0_serial_out_buf1[0], sizeof(sa11x0_serial_out_buf1),
153 &sa11x0_serial_in_buf1[0], sizeof(sa11x0_serial_in_buf1)
156 static SERIAL_CHANNEL(sa11x0_serial_channel1,
159 CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_SA11X0_SERIAL1_BAUD),
160 CYG_SERIAL_STOP_DEFAULT,
161 CYG_SERIAL_PARITY_DEFAULT,
162 CYG_SERIAL_WORD_LENGTH_DEFAULT,
163 CYG_SERIAL_FLAGS_DEFAULT
167 DEVTAB_ENTRY(sa11x0_serial_io1,
168 CYGDAT_IO_SERIAL_ARM_SA11X0_SERIAL1_NAME,
169 0, // Does not depend on a lower level interface
170 &cyg_io_serial_devio,
172 sa11x0_serial_lookup, // Serial driver may need initializing
173 &sa11x0_serial_channel1
175 #endif // CYGPKG_IO_SERIAL_ARM_SA11X0_SERIAL1
177 // Internal function to actually configure the hardware to desired baud rate, etc.
179 sa11x0_serial_config_port(serial_channel *chan, cyg_serial_info_t *new_config, bool init)
181 sa11x0_serial_info *sa11x0_chan = (sa11x0_serial_info *)chan->dev_priv;
182 volatile struct serial_port *port = (volatile struct serial_port *)sa11x0_chan->base;
183 unsigned char parity = select_parity[new_config->parity];
184 unsigned char word_length = select_word_length[new_config->word_length-CYGNUM_SERIAL_WORD_LENGTH_5];
185 unsigned char stop_bits = select_stop_bits[new_config->stop];
186 int baud = SA11X0_UART_BAUD_RATE_DIVISOR(select_baud[new_config->baud]);
187 if ((word_length == 0xFF) ||
189 (stop_bits == 0xFF)) {
190 return false; // Unsupported configuration
192 // Disable Receiver and Transmitter (clears FIFOs)
193 port->ctl3 = SA11X0_UART_RX_DISABLED |
194 SA11X0_UART_TX_DISABLED;
196 // Clear sticky (writable) status bits.
197 port->stat0 = SA11X0_UART_RX_IDLE |
198 SA11X0_UART_RX_BEGIN_OF_BREAK |
199 SA11X0_UART_RX_END_OF_BREAK;
201 // Set parity, word length, stop bits
202 port->ctl0 = parity |
206 // Set the desired baud rate.
207 port->ctl1 = (baud >> 8) & SA11X0_UART_H_BAUD_RATE_DIVISOR_MASK;
208 port->ctl2 = baud & SA11X0_UART_L_BAUD_RATE_DIVISOR_MASK;
210 // Enable the receiver (with interrupts) and the transmitter.
211 port->ctl3 = SA11X0_UART_RX_ENABLED |
212 SA11X0_UART_TX_ENABLED |
213 SA11X0_UART_RX_FIFO_INT_ENABLED;
217 // Function to initialize the device. Called at bootstrap time.
219 sa11x0_serial_init(struct cyg_devtab_entry *tab)
221 serial_channel *chan = (serial_channel *)tab->priv;
222 sa11x0_serial_info *sa11x0_chan = (sa11x0_serial_info *)chan->dev_priv;
224 #ifdef CYGDBG_IO_INIT
225 diag_printf("SA11X0 SERIAL init - dev: %x.%d\n", sa11x0_chan->base, sa11x0_chan->int_num);
227 (chan->callbacks->serial_init)(chan); // Really only required for interrupt driven devices
228 if (chan->out_cbuf.len != 0) {
229 cyg_drv_interrupt_create(sa11x0_chan->int_num,
230 99, // Priority - unused
231 (cyg_addrword_t)chan, // Data item passed to interrupt handler
234 &sa11x0_chan->serial_interrupt_handle,
235 &sa11x0_chan->serial_interrupt);
236 cyg_drv_interrupt_attach(sa11x0_chan->serial_interrupt_handle);
237 cyg_drv_interrupt_unmask(sa11x0_chan->int_num);
239 res = sa11x0_serial_config_port(chan, &chan->config, true);
243 // This routine is called when the device is "looked" up (i.e. attached)
245 sa11x0_serial_lookup(struct cyg_devtab_entry **tab,
246 struct cyg_devtab_entry *sub_tab,
249 serial_channel *chan = (serial_channel *)(*tab)->priv;
250 (chan->callbacks->serial_init)(chan); // Really only required for interrupt driven devices
254 // Send a character to the device output buffer.
255 // Return 'true' if character is sent to device
257 sa11x0_serial_putc(serial_channel *chan, unsigned char c)
259 sa11x0_serial_info *sa11x0_chan = (sa11x0_serial_info *)chan->dev_priv;
260 volatile struct serial_port *port = (volatile struct serial_port *)sa11x0_chan->base;
261 if (port->stat1 & SA11X0_UART_TX_FIFO_NOT_FULL) {
265 return false; // Couldn't send, tx was busy
269 // Fetch a character from the device input buffer, waiting if necessary
271 sa11x0_serial_getc(serial_channel *chan)
273 sa11x0_serial_info *sa11x0_chan = (sa11x0_serial_info *)chan->dev_priv;
274 volatile struct serial_port *port = (volatile struct serial_port *)sa11x0_chan->base;
278 // Set up the device characteristics; baud rate, etc.
280 sa11x0_serial_set_config(serial_channel *chan, cyg_uint32 key,
281 const void *xbuf, cyg_uint32 *len)
284 case CYG_IO_SET_CONFIG_SERIAL_INFO:
286 cyg_serial_info_t *config = (cyg_serial_info_t *)xbuf;
287 if ( *len < sizeof(cyg_serial_info_t) ) {
290 *len = sizeof(cyg_serial_info_t);
291 if ( true != sa11x0_serial_config_port(chan, config, false) )
301 // Enable the transmitter on the device
303 sa11x0_serial_start_xmit(serial_channel *chan)
305 sa11x0_serial_info *sa11x0_chan = (sa11x0_serial_info *)chan->dev_priv;
306 volatile struct serial_port *port = (volatile struct serial_port *)sa11x0_chan->base;
307 (chan->callbacks->xmt_char)(chan); // Kick transmitter (if necessary)
308 port->ctl3 |= SA11X0_UART_TX_FIFO_INT_ENABLED;
311 // Disable the transmitter on the device
313 sa11x0_serial_stop_xmit(serial_channel *chan)
315 sa11x0_serial_info *sa11x0_chan = (sa11x0_serial_info *)chan->dev_priv;
316 volatile struct serial_port *port = (volatile struct serial_port *)sa11x0_chan->base;
317 port->ctl3 &= ~SA11X0_UART_TX_FIFO_INT_ENABLED;
320 // Serial I/O - low level interrupt handler (ISR)
322 sa11x0_serial_ISR(cyg_vector_t vector, cyg_addrword_t data)
324 cyg_drv_interrupt_mask(vector);
325 cyg_drv_interrupt_acknowledge(vector);
326 return CYG_ISR_CALL_DSR; // Cause DSR to be run
329 // Serial I/O - high level interrupt handler (DSR)
331 sa11x0_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
333 serial_channel *chan = (serial_channel *)data;
334 sa11x0_serial_info *sa11x0_chan = (sa11x0_serial_info *)chan->dev_priv;
335 volatile struct serial_port *port = (volatile struct serial_port *)sa11x0_chan->base;
336 unsigned int stat0 = port->stat0;
337 if (stat0 & SA11X0_UART_TX_SERVICE_REQUEST) {
338 (chan->callbacks->xmt_char)(chan);
340 if (stat0 & SA11X0_UART_RX_INTS) {
341 while (port->stat1 & SA11X0_UART_RX_FIFO_NOT_EMPTY) {
342 (chan->callbacks->rcv_char)(chan, port->data);
344 port->stat0 = SA11X0_UART_RX_IDLE; // Need to clear this manually
346 if (stat0 & (SA11X0_UART_RX_BEGIN_OF_BREAK |
347 SA11X0_UART_RX_END_OF_BREAK ) ) {
348 // Need to clear any of these manually also or noise
349 // from plugging in can cause an interrupt loop!
350 port->stat0 = stat0 & (SA11X0_UART_RX_BEGIN_OF_BREAK |
351 SA11X0_UART_RX_END_OF_BREAK );
353 cyg_drv_interrupt_unmask(vector);