1 //==========================================================================
3 // io/serial/v85x/v85x_v850_serial.c
5 // NEC V850 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,jlarmour
46 // Purpose: V850 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>
57 #include <pkgconf/kernel.h>
59 #include CYGBLD_HAL_TARGET_H
61 #include <cyg/io/io.h>
62 #include <cyg/hal/hal_intr.h>
63 #include <cyg/io/devtab.h>
64 #include <cyg/io/serial.h>
65 #include <cyg/infra/diag.h>
67 #ifdef CYGVAR_KERNEL_COUNTERS_CLOCK
68 #include <cyg/kernel/kapi.h>
71 #ifdef CYGPKG_IO_SERIAL_V85X_V850
73 #include "v85x_v850_serial.h"
75 typedef struct v850_serial_info {
78 cyg_interrupt serial_interrupt[3];
79 cyg_handle_t serial_interrupt_handle[3];
81 #ifdef CYGVAR_KERNEL_COUNTERS_CLOCK
83 cyg_handle_t tx_timeout_handle;
87 static bool v850_serial_init(struct cyg_devtab_entry *tab);
88 static bool v850_serial_putc(serial_channel *chan, unsigned char c);
89 static Cyg_ErrNo v850_serial_lookup(struct cyg_devtab_entry **tab,
90 struct cyg_devtab_entry *sub_tab,
92 static unsigned char v850_serial_getc(serial_channel *chan);
93 static Cyg_ErrNo v850_serial_set_config(serial_channel *chan, cyg_uint32 key,
94 const void *xbuf, cyg_uint32 *len);
95 static void v850_serial_start_xmit(serial_channel *chan);
96 static void v850_serial_stop_xmit(serial_channel *chan);
98 static cyg_uint32 v850_serial_ISR(cyg_vector_t vector, cyg_addrword_t data);
99 static void v850_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data);
101 static SERIAL_FUNS(v850_serial_funs,
104 v850_serial_set_config,
105 v850_serial_start_xmit,
106 v850_serial_stop_xmit
109 #ifdef CYGPKG_IO_SERIAL_V85X_V850_SERIAL0
110 static v850_serial_info v850_serial_info0 = {V850_REG_ASIM0,
111 CYGNUM_HAL_VECTOR_INTSER0};
112 #if CYGNUM_IO_SERIAL_V85X_V850_SERIAL0_BUFSIZE > 0
113 static unsigned char v850_serial_out_buf0[CYGNUM_IO_SERIAL_V85X_V850_SERIAL0_BUFSIZE];
114 static unsigned char v850_serial_in_buf0[CYGNUM_IO_SERIAL_V85X_V850_SERIAL0_BUFSIZE];
116 static SERIAL_CHANNEL_USING_INTERRUPTS(v850_serial_channel0,
119 CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_V85X_V850_SERIAL0_BAUD),
120 CYG_SERIAL_STOP_DEFAULT,
121 CYG_SERIAL_PARITY_DEFAULT,
122 CYG_SERIAL_WORD_LENGTH_DEFAULT,
123 CYG_SERIAL_FLAGS_DEFAULT,
124 &v850_serial_out_buf0[0], sizeof(v850_serial_out_buf0),
125 &v850_serial_in_buf0[0], sizeof(v850_serial_in_buf0)
128 static SERIAL_CHANNEL(v850_serial_channel0,
131 CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_V85X_V850_SERIAL0_BAUD),
132 CYG_SERIAL_STOP_DEFAULT,
133 CYG_SERIAL_PARITY_DEFAULT,
134 CYG_SERIAL_WORD_LENGTH_DEFAULT,
135 CYG_SERIAL_FLAGS_DEFAULT
139 DEVTAB_ENTRY(v850_serial_io0,
140 CYGDAT_IO_SERIAL_V85X_V850_SERIAL0_NAME,
141 0, // Does not depend on a lower level interface
142 &cyg_io_serial_devio,
144 v850_serial_lookup, // Serial driver may need initializing
145 &v850_serial_channel0
147 #endif // CYGPKG_IO_SERIAL_V85X_V850_SERIAL0
149 // Internal function to actually configure the hardware to desired baud rate, etc.
151 v850_serial_config_port(serial_channel *chan, cyg_serial_info_t *new_config, bool init)
153 v850_serial_info *v850_chan = (v850_serial_info *)chan->dev_priv;
154 volatile struct serial_port *port = (volatile struct serial_port *)v850_chan->base;
155 unsigned char parity = select_parity[new_config->parity];
156 unsigned char word_length = select_word_length[new_config->word_length-CYGNUM_SERIAL_WORD_LENGTH_5];
157 unsigned char stop_bits = select_stop_bits[new_config->stop];
160 if ((select_baud[new_config->baud].count == 0) ||
161 (word_length == 0xFF) ||
163 (stop_bits == 0xFF)) {
164 return false; // Unsupported configuration
166 port->asim = ASIM_TxRx_Tx | ASIM_TxRx_Rx | parity | word_length | stop_bits;
167 count = select_baud[new_config->baud].count;
168 divisor = select_baud[new_config->baud].divisor;
170 while (count > 0xFF) {
177 port->brgm = divisor & 0x07;
178 #if CYGINT_HAL_V850_VARIANT_SB1
179 port->brgm1 = divisor >> 3;
182 if (new_config != &chan->config) {
183 chan->config = *new_config;
188 #ifdef CYGVAR_KERNEL_COUNTERS_CLOCK
190 // The serial ports on the V850 are incredibly stupid. There is no
191 // interface status register which can tell you if it is possible to
192 // read or write a character! The only way to discern this is by using
193 // interrupts [or at least an interrupt register and polling it]. Thus
194 // the serial transmit code has a problem in that it will be required by
195 // upper layers to "send until full". The only way to decide "not full" is
196 // that an interrupt has happened. If the serial driver is being mixed
197 // with diagnostic I/O, then serial transmit interrupts will possibly be
200 // This code attempts to compenstate by using a kernel alarm to reset the
201 // "device is busy" flag after some timeout. The timeout period will be
202 // sufficiently long so as to not interfere with normal interrupt handling.
205 v850_serial_tx_timeout(cyg_handle_t alarm, cyg_addrword_t p)
207 v850_serial_info *v850_chan = (v850_serial_info *)p;
208 v850_chan->tx_busy = false;
212 // Function to initialize the device. Called at bootstrap time.
214 v850_serial_init(struct cyg_devtab_entry *tab)
217 serial_channel *chan = (serial_channel *)tab->priv;
218 v850_serial_info *v850_chan = (v850_serial_info *)chan->dev_priv;
219 #ifdef CYGVAR_KERNEL_COUNTERS_CLOCK
222 #ifdef CYGDBG_IO_INIT
223 diag_printf("V850 SERIAL init - dev: %x.%d\n", v850_chan->base, v850_chan->int_num);
225 (chan->callbacks->serial_init)(chan); // Really only required for interrupt driven devices
226 if (chan->out_cbuf.len != 0) {
227 for (i = 0; i < 3; i++) {
228 cyg_drv_interrupt_create(v850_chan->int_num+i,
229 99, // Priority - unused
230 (cyg_addrword_t)chan, // Data item passed to interrupt handler
233 &v850_chan->serial_interrupt_handle[i],
234 &v850_chan->serial_interrupt[i]);
235 cyg_drv_interrupt_attach(v850_chan->serial_interrupt_handle[i]);
236 cyg_drv_interrupt_unmask(v850_chan->int_num+i);
237 #ifdef CYGVAR_KERNEL_COUNTERS_CLOCK
238 cyg_clock_to_counter(cyg_real_time_clock(), &h);
239 cyg_alarm_create(h, v850_serial_tx_timeout, (cyg_addrword_t)v850_chan,
240 &v850_chan->tx_timeout_handle, &v850_chan->tx_timeout);
244 v850_chan->tx_busy = false;
245 return v850_serial_config_port(chan, &chan->config, true);
248 // This routine is called when the device is "looked" up (i.e. attached)
250 v850_serial_lookup(struct cyg_devtab_entry **tab,
251 struct cyg_devtab_entry *sub_tab,
254 serial_channel *chan = (serial_channel *)(*tab)->priv;
255 (chan->callbacks->serial_init)(chan); // Really only required for interrupt driven devices
259 // Send a character to the device output buffer.
260 // Return 'true' if character is sent to device
262 v850_serial_putc(serial_channel *chan, unsigned char c)
264 v850_serial_info *v850_chan = (v850_serial_info *)chan->dev_priv;
265 volatile struct serial_port *port = (volatile struct serial_port *)v850_chan->base;
266 if (!v850_chan->tx_busy) {
267 v850_chan->tx_busy = true;
269 #ifdef CYGVAR_KERNEL_COUNTERS_CLOCK
270 cyg_alarm_initialize(v850_chan->tx_timeout_handle, cyg_current_time()+10, 0);
274 return false; // Couldn't send, tx was busy
278 // Fetch a character from the device input buffer, waiting if necessary
280 v850_serial_getc(serial_channel *chan)
282 v850_serial_info *v850_chan = (v850_serial_info *)chan->dev_priv;
283 volatile struct serial_port *port = (volatile struct serial_port *)v850_chan->base;
287 // Set up the device characteristics; baud rate, etc.
289 v850_serial_set_config(serial_channel *chan, cyg_uint32 key,
290 const void *xbuf, cyg_uint32 *len)
293 case CYG_IO_SET_CONFIG_SERIAL_INFO:
295 cyg_serial_info_t *config = (cyg_serial_info_t *)xbuf;
296 if ( *len < sizeof(cyg_serial_info_t) ) {
299 *len = sizeof(cyg_serial_info_t);
300 if ( true != v850_serial_config_port(chan, config, false) )
310 // Enable the transmitter on the device
312 v850_serial_start_xmit(serial_channel *chan)
314 v850_serial_info *v850_chan = (v850_serial_info *)chan->dev_priv;
315 (chan->callbacks->xmt_char)(chan); // Kick transmitter (if necessary)
316 cyg_drv_interrupt_unmask(v850_chan->int_num+INT_Tx); // Enable Tx interrupt
319 // Disable the transmitter on the device
321 v850_serial_stop_xmit(serial_channel *chan)
323 v850_serial_info *v850_chan = (v850_serial_info *)chan->dev_priv;
324 cyg_drv_interrupt_mask(v850_chan->int_num+INT_Tx); // Disable Tx interrupt
327 // Serial I/O - low level interrupt handler (ISR)
329 v850_serial_ISR(cyg_vector_t vector, cyg_addrword_t data)
331 cyg_drv_interrupt_mask(vector);
332 cyg_drv_interrupt_acknowledge(vector);
333 return CYG_ISR_CALL_DSR; // Cause DSR to be run
336 // Serial I/O - high level interrupt handler (DSR)
338 v850_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
340 serial_channel *chan = (serial_channel *)data;
341 v850_serial_info *v850_chan = (v850_serial_info *)chan->dev_priv;
342 volatile struct serial_port *port = (volatile struct serial_port *)v850_chan->base;
343 switch (vector-(v850_chan->int_num)) {
346 (chan->callbacks->rcv_char)(chan, port->rxs);
349 v850_chan->tx_busy = false;
350 #ifdef CYGVAR_KERNEL_COUNTERS_CLOCK
351 cyg_alarm_initialize(v850_chan->tx_timeout_handle, 0, 0);
353 (chan->callbacks->xmt_char)(chan);
356 cyg_drv_interrupt_unmask(vector);
360 // EOF v85x_v850_serial.c