1 //==========================================================================
3 // io/serial/arm/integrator_serial_with_ints.c
5 // ARM INTEGRATOR 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####
43 // Author(s): David A Rusling
44 // Contributors: Philippe Robin
45 // Date: November 7, 2000
46 // Purpose: INTEGRATOR Serial I/O module (interrupt driven)
49 //####DESCRIPTIONEND####
51 //==========================================================================
53 #include <pkgconf/system.h>
54 #include <pkgconf/io.h>
55 #include <pkgconf/io_serial.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/infra/diag.h>
62 #ifdef CYGPKG_IO_SERIAL_ARM_INTEGRATOR
63 #include "integrator_serial.h"
65 typedef struct integrator_serial_info {
68 cyg_interrupt serial_interrupt;
69 cyg_handle_t serial_interrupt_handle;
70 } integrator_serial_info;
72 static bool integrator_serial_init(struct cyg_devtab_entry *tab);
73 static bool integrator_serial_putc(serial_channel *chan, unsigned char c);
74 static Cyg_ErrNo integrator_serial_lookup(struct cyg_devtab_entry **tab,
75 struct cyg_devtab_entry *sub_tab,
77 static unsigned char integrator_serial_getc(serial_channel *chan);
78 static Cyg_ErrNo integrator_serial_set_config(serial_channel *chan, cyg_uint32 key,
79 const void *xbuf, cyg_uint32 *len);
80 static void integrator_serial_start_xmit(serial_channel *chan);
81 static void integrator_serial_stop_xmit(serial_channel *chan);
83 static cyg_uint32 integrator_serial_ISR(cyg_vector_t vector, cyg_addrword_t data);
84 static void integrator_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data);
86 static SERIAL_FUNS(integrator_serial_funs,
87 integrator_serial_putc,
88 integrator_serial_getc,
89 integrator_serial_set_config,
90 integrator_serial_start_xmit,
91 integrator_serial_stop_xmit
94 #ifdef CYGPKG_IO_SERIAL_ARM_INTEGRATOR_SERIAL0
95 #define INTEGRATOR_UART0_BASE 0x16000000 /* UART 0 */
96 static integrator_serial_info integrator_serial_info0 = {INTEGRATOR_UART0_BASE, CYGNUM_HAL_INTERRUPT_UARTINT0};
97 #if CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL0_BUFSIZE > 0
98 static unsigned char integrator_serial_out_buf0[CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL0_BUFSIZE];
99 static unsigned char integrator_serial_in_buf0[CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL0_BUFSIZE];
101 static SERIAL_CHANNEL_USING_INTERRUPTS(integrator_serial_channel0,
102 integrator_serial_funs,
103 integrator_serial_info0,
104 CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL0_BAUD),
105 CYG_SERIAL_STOP_DEFAULT,
106 CYG_SERIAL_PARITY_DEFAULT,
107 CYG_SERIAL_WORD_LENGTH_DEFAULT,
108 CYG_SERIAL_FLAGS_DEFAULT,
109 &integrator_serial_out_buf0[0], sizeof(integrator_serial_out_buf0),
110 &integrator_serial_in_buf0[0], sizeof(integrator_serial_in_buf0)
113 static SERIAL_CHANNEL(integrator_serial_channel0,
114 integrator_serial_funs,
115 integrator_serial_info0,
116 CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL0_BAUD),
117 CYG_SERIAL_STOP_DEFAULT,
118 CYG_SERIAL_PARITY_DEFAULT,
119 CYG_SERIAL_WORD_LENGTH_DEFAULT,
120 CYG_SERIAL_FLAGS_DEFAULT
124 DEVTAB_ENTRY(integrator_serial_io0,
125 CYGDAT_IO_SERIAL_ARM_INTEGRATOR_SERIAL0_NAME,
126 0, // Does not depend on a lower level interface
127 &cyg_io_serial_devio,
128 integrator_serial_init,
129 integrator_serial_lookup, // Serial driver may need initializing
130 &integrator_serial_channel0
132 #endif // CYGPKG_IO_SERIAL_ARM_INTEGRATOR_SERIAL0
134 #ifdef CYGPKG_IO_SERIAL_ARM_INTEGRATOR_SERIAL1
135 #define INTEGRATOR_UART1_BASE 0x17000000 /* UART 1 */
136 static integrator_serial_info integrator_serial_info1 = {INTEGRATOR_UART1_BASE, CYGNUM_HAL_INTERRUPT_UARTINT1};
137 #if CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL1_BUFSIZE > 0
138 static unsigned char integrator_serial_out_buf1[CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL1_BUFSIZE];
139 static unsigned char integrator_serial_in_buf1[CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL1_BUFSIZE];
141 static SERIAL_CHANNEL_USING_INTERRUPTS(integrator_serial_channel1,
142 integrator_serial_funs,
143 integrator_serial_info1,
144 CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL1_BAUD),
145 CYG_SERIAL_STOP_DEFAULT,
146 CYG_SERIAL_PARITY_DEFAULT,
147 CYG_SERIAL_WORD_LENGTH_DEFAULT,
148 CYG_SERIAL_FLAGS_DEFAULT,
149 &integrator_serial_out_buf1[0], sizeof(integrator_serial_out_buf1),
150 &integrator_serial_in_buf1[0], sizeof(integrator_serial_in_buf1)
153 static SERIAL_CHANNEL(integrator_serial_channel1,
154 integrator_serial_funs,
155 integrator_serial_info1,
156 CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_INTEGRATOR_SERIAL1_BAUD),
157 CYG_SERIAL_STOP_DEFAULT,
158 CYG_SERIAL_PARITY_DEFAULT,
159 CYG_SERIAL_WORD_LENGTH_DEFAULT,
160 CYG_SERIAL_FLAGS_DEFAULT
164 DEVTAB_ENTRY(integrator_serial_io1,
165 CYGDAT_IO_SERIAL_ARM_INTEGRATOR_SERIAL1_NAME,
166 0, // Does not depend on a lower level interface
167 &cyg_io_serial_devio,
168 integrator_serial_init,
169 integrator_serial_lookup, // Serial driver may need initializing
170 &integrator_serial_channel1
172 #endif // CYGPKG_IO_SERIAL_ARM_INTEGRATOR_SERIAL1
174 #define GET_INTERRUPT_STATUS(p) IO_READ((p) + AMBA_UARTIIR)
175 #define GET_STATUS(p) (IO_READ((p) + AMBA_UARTFR))
176 #define GET_CHAR(p) (IO_READ((p) + AMBA_UARTDR))
177 #define PUT_CHAR(p, c) (IO_WRITE(((p) + AMBA_UARTDR), (c)))
178 #define IO_READ(p) ((*(volatile unsigned int *)(p)) & 0xFF)
179 #define IO_WRITE(p, c) (*(unsigned int *)(p) = (c))
180 #define RX_DATA(s) (((s) & AMBA_UARTFR_RXFE) == 0)
181 #define TX_READY(s) (((s) & AMBA_UARTFR_TXFF) == 0)
182 #define TX_EMPTY(p) ((GET_STATUS(p) & AMBA_UARTFR_TMSK) == 0)
185 static int chars_rx = 0 ;
186 static int chars_tx = 0 ;
188 // Internal function to actually configure the hardware to desired baud rate, etc.
190 integrator_serial_config_port(serial_channel *chan, cyg_serial_info_t *new_config, bool init)
192 integrator_serial_info *integrator_chan = (integrator_serial_info *)chan->dev_priv;
193 unsigned int port = (unsigned int)integrator_chan->base;
194 unsigned short baud_divisor = select_baud[new_config->baud];
197 // don't do this baud rate...
198 if (baud_divisor == 0) return false; // Invalid configuration
200 // first, disable everything
201 IO_WRITE(port + AMBA_UARTCR, 0x0);
204 IO_WRITE(port + AMBA_UARTLCR_M, ((baud_divisor & 0xf00) >> 8));
205 IO_WRITE(port + AMBA_UARTLCR_L, (baud_divisor & 0xff));
207 // ----------v----------v----------v----------v----------
208 // NOTE: MUST BE WRITTEN LAST (AFTER UARTLCR_M & UARTLCR_L)
209 // ----------^----------^----------^----------^----------
211 select_word_length[new_config->word_length - CYGNUM_SERIAL_WORD_LENGTH_5] |
212 select_stop_bits[new_config->stop] |
213 select_parity[new_config->parity] | AMBA_UARTLCR_H_FEN ;
214 IO_WRITE(port + AMBA_UARTLCR_H, _lcr);
216 // finally, enable the uart
217 IO_WRITE(port + AMBA_UARTCR, (AMBA_UARTCR_RIE | AMBA_UARTCR_RTIE | AMBA_UARTCR_UARTEN));
219 // save the configuration
220 if (new_config != &chan->config) {
221 chan->config = *new_config;
228 // Function to initialize the device. Called at bootstrap time.
230 integrator_serial_init(struct cyg_devtab_entry *tab)
232 serial_channel *chan = (serial_channel *)tab->priv;
233 integrator_serial_info *integrator_chan = (integrator_serial_info *)chan->dev_priv;
234 #ifdef CYGDBG_IO_INIT
235 diag_printf("INTEGRATOR SERIAL init - dev: %x.%d\n", integrator_chan->base, integrator_chan->int_num);
237 (chan->callbacks->serial_init)(chan); // Really only required for interrupt driven devices
238 if (chan->out_cbuf.len != 0) {
239 cyg_drv_interrupt_create(integrator_chan->int_num,
240 99, // Priority - what goes here?
241 (cyg_addrword_t)chan, // Data item passed to interrupt handler
242 integrator_serial_ISR,
243 integrator_serial_DSR,
244 &integrator_chan->serial_interrupt_handle,
245 &integrator_chan->serial_interrupt);
246 cyg_drv_interrupt_attach(integrator_chan->serial_interrupt_handle);
247 cyg_drv_interrupt_unmask(integrator_chan->int_num);
249 integrator_serial_config_port(chan, &chan->config, true);
253 // This routine is called when the device is "looked" up (i.e. attached)
255 integrator_serial_lookup(struct cyg_devtab_entry **tab,
256 struct cyg_devtab_entry *sub_tab,
259 serial_channel *chan = (serial_channel *)(*tab)->priv;
260 (chan->callbacks->serial_init)(chan); // Really only required for interrupt driven devices
264 // Send a character to the device output buffer.
265 // Return 'true' if character is sent to device
267 integrator_serial_putc(serial_channel *chan, unsigned char c)
269 integrator_serial_info *integrator_chan = (integrator_serial_info *)chan->dev_priv;
270 unsigned int status = GET_STATUS(integrator_chan->base) ;
272 if (TX_READY(status)) {
273 // Transmit buffer is empty
274 PUT_CHAR(integrator_chan->base, c) ;
283 // Fetch a character from the device input buffer, waiting if necessary
285 integrator_serial_getc(serial_channel *chan)
288 integrator_serial_info *integrator_chan = (integrator_serial_info *)chan->dev_priv;
289 unsigned int status ;
292 status = GET_STATUS(integrator_chan->base) ;
293 } while (!RX_DATA(status)) ; // Wait for char
298 c = GET_CHAR(integrator_chan->base) ;
302 // Set up the device characteristics; baud rate, etc.
304 integrator_serial_set_config(serial_channel *chan, cyg_uint32 key, const void *xbuf,
307 integrator_serial_info *integrator_chan = (integrator_serial_info *)chan->dev_priv;
310 case CYG_IO_SET_CONFIG_SERIAL_INFO:
312 cyg_serial_info_t *config = (cyg_serial_info_t *)xbuf;
313 if ( *len < sizeof(cyg_serial_info_t) ) {
316 *len = sizeof(cyg_serial_info_t);
317 if ( true != integrator_serial_config_port(chan, config, false) )
321 #ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_HW
323 case CYG_IO_SET_CONFIG_SERIAL_HW_RX_FLOW_THROTTLE:
325 volatile struct serial_port *port = (volatile struct serial_port *)integrator_chan->base;
326 cyg_uint32 *f = (cyg_uint32 *)xbuf;
327 unsigned char mask=0;
331 if ( chan->config.flags & CYGNUM_SERIAL_FLOW_RTSCTS_RX )
333 if ( chan->config.flags & CYGNUM_SERIAL_FLOW_DSRDTR_RX )
335 if (*f) // we should throttle
336 port->REG_mcr &= ~mask;
337 else // we should no longer throttle
338 port->REG_mcr |= mask;
341 case CYG_IO_SET_CONFIG_SERIAL_HW_FLOW_CONFIG:
342 // Nothing to do because we do support both RTSCTS and DSRDTR flow
344 // Other targets would clear any unsupported flags here.
345 // We just return ENOERR.
348 #error "Flow control for Integrator not integrated!"
357 // Enable the transmitter on the device
359 integrator_serial_start_xmit(serial_channel *chan)
361 integrator_serial_info *integrator_chan = (integrator_serial_info *)chan->dev_priv;
363 IO_WRITE(integrator_chan->base + AMBA_UARTCR,
364 IO_READ(integrator_chan->base + AMBA_UARTCR) | AMBA_UARTCR_TIE);
367 // Disable the transmitter on the device
369 integrator_serial_stop_xmit(serial_channel *chan)
371 integrator_serial_info *integrator_chan = (integrator_serial_info *)chan->dev_priv;
373 IO_WRITE(integrator_chan->base + AMBA_UARTCR,
374 IO_READ(integrator_chan->base + AMBA_UARTCR) & ~AMBA_UARTCR_TIE);
377 // Serial I/O - low level interrupt handler (ISR)
379 integrator_serial_ISR(cyg_vector_t vector, cyg_addrword_t data)
381 serial_channel *chan = (serial_channel *)data;
382 integrator_serial_info *integrator_chan = (integrator_serial_info *)chan->dev_priv;
384 cyg_drv_interrupt_mask(integrator_chan->int_num);
385 cyg_drv_interrupt_acknowledge(integrator_chan->int_num);
386 return CYG_ISR_CALL_DSR; // Cause DSR to be run
389 // Serial I/O - high level interrupt handler (DSR)
391 integrator_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
393 serial_channel *chan = (serial_channel *)data;
394 integrator_serial_info *integrator_chan = (integrator_serial_info *)chan->dev_priv;
395 volatile unsigned char isr = GET_INTERRUPT_STATUS(integrator_chan->base) ;
397 while ((isr & (AMBA_UARTIIR_RTIS | AMBA_UARTIIR_TIS | AMBA_UARTIIR_RIS)) != 0) {
398 if (isr & AMBA_UARTIIR_TIS) {
399 (chan->callbacks->xmt_char)(chan);
400 } else if (isr & AMBA_UARTIIR_RTIS) {
402 (chan->callbacks->rcv_char)(chan, GET_CHAR(integrator_chan->base));
403 } else if (isr & AMBA_UARTIIR_RIS) {
405 (chan->callbacks->rcv_char)(chan, GET_CHAR(integrator_chan->base));
407 isr = GET_INTERRUPT_STATUS(integrator_chan->base) ;
409 cyg_drv_interrupt_unmask(integrator_chan->int_num);