]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/serial/v85x/v850/v2_0/src/v85x_v850_serial.c
Initial revision
[karo-tx-redboot.git] / packages / devs / serial / v85x / v850 / v2_0 / src / v85x_v850_serial.c
1 //==========================================================================
2 //
3 //      io/serial/v85x/v85x_v850_serial.c
4 //
5 //      NEC V850 Serial I/O Interface Module (interrupt driven)
6 //
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.
12 //
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.
16 //
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
20 // for more details.
21 //
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.
25 //
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.
32 //
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.
35 //
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####
42 //
43 // Author(s):    gthomas
44 // Contributors: gthomas,jlarmour
45 // Date:         2001-03-21
46 // Purpose:      V850 Serial I/O module (interrupt driven version)
47 // Description: 
48 //
49 //####DESCRIPTIONEND####
50 //
51 //==========================================================================
52
53 #include <pkgconf/system.h>
54 #include <pkgconf/io_serial.h>
55 #include <pkgconf/io.h>
56 #ifdef CYGPKG_KERNEL
57 #include <pkgconf/kernel.h>
58 #endif
59 #include CYGBLD_HAL_TARGET_H
60
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>
66
67 #ifdef CYGVAR_KERNEL_COUNTERS_CLOCK
68 #include <cyg/kernel/kapi.h>
69 #endif
70
71 #ifdef CYGPKG_IO_SERIAL_V85X_V850
72
73 #include "v85x_v850_serial.h"
74
75 typedef struct v850_serial_info {
76     CYG_ADDRWORD   base;
77     CYG_WORD       int_num;
78     cyg_interrupt  serial_interrupt[3];
79     cyg_handle_t   serial_interrupt_handle[3];
80     cyg_bool       tx_busy;
81 #ifdef CYGVAR_KERNEL_COUNTERS_CLOCK
82     cyg_alarm      tx_timeout;
83     cyg_handle_t   tx_timeout_handle;
84 #endif
85 } v850_serial_info;
86
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,
91                                     const char *name);
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);
97
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);
100
101 static SERIAL_FUNS(v850_serial_funs, 
102                    v850_serial_putc, 
103                    v850_serial_getc,
104                    v850_serial_set_config,
105                    v850_serial_start_xmit,
106                    v850_serial_stop_xmit
107     );
108
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];
115
116 static SERIAL_CHANNEL_USING_INTERRUPTS(v850_serial_channel0,
117                                        v850_serial_funs, 
118                                        v850_serial_info0,
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)
126     );
127 #else
128 static SERIAL_CHANNEL(v850_serial_channel0,
129                       v850_serial_funs, 
130                       v850_serial_info0,
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
136     );
137 #endif
138
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, 
143              v850_serial_init, 
144              v850_serial_lookup,     // Serial driver may need initializing
145              &v850_serial_channel0
146     );
147 #endif //  CYGPKG_IO_SERIAL_V85X_V850_SERIAL0
148
149 // Internal function to actually configure the hardware to desired baud rate, etc.
150 static bool
151 v850_serial_config_port(serial_channel *chan, cyg_serial_info_t *new_config, bool init)
152 {
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];
158     int divisor, count;
159
160     if ((select_baud[new_config->baud].count == 0) ||
161         (word_length == 0xFF) ||
162         (parity == 0xFF) ||
163         (stop_bits == 0xFF)) {
164         return false;  // Unsupported configuration
165     }
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;
169
170     while (count > 0xFF) {
171         count >>= 1;
172         divisor++;
173     }
174
175     port->brgc = count;
176     
177     port->brgm = divisor & 0x07; 
178 #if CYGINT_HAL_V850_VARIANT_SB1
179     port->brgm1 = divisor >> 3;
180 #endif
181
182     if (new_config != &chan->config) {
183         chan->config = *new_config;
184     }
185     return true;
186 }
187
188 #ifdef CYGVAR_KERNEL_COUNTERS_CLOCK
189 //
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
198 // lost.  
199 //
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.
203 //
204 static void
205 v850_serial_tx_timeout(cyg_handle_t alarm, cyg_addrword_t p)
206 {
207     v850_serial_info *v850_chan = (v850_serial_info *)p;
208     v850_chan->tx_busy = false;
209 }
210 #endif
211
212 // Function to initialize the device.  Called at bootstrap time.
213 static bool 
214 v850_serial_init(struct cyg_devtab_entry *tab)
215 {
216     int i;
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
220     cyg_handle_t h;
221 #endif
222 #ifdef CYGDBG_IO_INIT
223     diag_printf("V850 SERIAL init - dev: %x.%d\n", v850_chan->base, v850_chan->int_num);
224 #endif
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
231                                      v850_serial_ISR,
232                                      v850_serial_DSR,
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);
241 #endif
242         }
243     }
244     v850_chan->tx_busy = false;
245     return v850_serial_config_port(chan, &chan->config, true);
246 }
247
248 // This routine is called when the device is "looked" up (i.e. attached)
249 static Cyg_ErrNo 
250 v850_serial_lookup(struct cyg_devtab_entry **tab, 
251                   struct cyg_devtab_entry *sub_tab,
252                   const char *name)
253 {
254     serial_channel *chan = (serial_channel *)(*tab)->priv;
255     (chan->callbacks->serial_init)(chan);  // Really only required for interrupt driven devices
256     return ENOERR;
257 }
258
259 // Send a character to the device output buffer.
260 // Return 'true' if character is sent to device
261 static bool
262 v850_serial_putc(serial_channel *chan, unsigned char c)
263 {
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;
268         port->txs = c;
269 #ifdef CYGVAR_KERNEL_COUNTERS_CLOCK
270         cyg_alarm_initialize(v850_chan->tx_timeout_handle, cyg_current_time()+10, 0);
271 #endif
272         return true;
273     } else {
274         return false;  // Couldn't send, tx was busy
275     }
276 }
277
278 // Fetch a character from the device input buffer, waiting if necessary
279 static unsigned char 
280 v850_serial_getc(serial_channel *chan)
281 {
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;
284     return port->rxs;
285 }
286
287 // Set up the device characteristics; baud rate, etc.
288 static Cyg_ErrNo
289 v850_serial_set_config(serial_channel *chan, cyg_uint32 key,
290                        const void *xbuf, cyg_uint32 *len)
291 {
292     switch (key) {
293     case CYG_IO_SET_CONFIG_SERIAL_INFO:
294       {
295         cyg_serial_info_t *config = (cyg_serial_info_t *)xbuf;
296         if ( *len < sizeof(cyg_serial_info_t) ) {
297             return -EINVAL;
298         }
299         *len = sizeof(cyg_serial_info_t);
300         if ( true != v850_serial_config_port(chan, config, false) )
301             return -EINVAL;
302       }
303       break;
304     default:
305         return -EINVAL;
306     }
307     return ENOERR;
308 }
309
310 // Enable the transmitter on the device
311 static void
312 v850_serial_start_xmit(serial_channel *chan)
313 {
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
317 }
318
319 // Disable the transmitter on the device
320 static void 
321 v850_serial_stop_xmit(serial_channel *chan)
322 {
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
325 }
326
327 // Serial I/O - low level interrupt handler (ISR)
328 static cyg_uint32 
329 v850_serial_ISR(cyg_vector_t vector, cyg_addrword_t data)
330 {
331     cyg_drv_interrupt_mask(vector);
332     cyg_drv_interrupt_acknowledge(vector);
333     return CYG_ISR_CALL_DSR;  // Cause DSR to be run
334 }
335
336 // Serial I/O - high level interrupt handler (DSR)
337 static void       
338 v850_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
339 {
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)) {
344     case INT_ERR:
345     case INT_Rx:
346         (chan->callbacks->rcv_char)(chan, port->rxs);
347         break;
348     case INT_Tx:
349         v850_chan->tx_busy = false;
350 #ifdef CYGVAR_KERNEL_COUNTERS_CLOCK
351         cyg_alarm_initialize(v850_chan->tx_timeout_handle, 0, 0);
352 #endif
353         (chan->callbacks->xmt_char)(chan);
354         break;
355     }
356     cyg_drv_interrupt_unmask(vector);
357 }
358 #endif
359
360 // EOF v85x_v850_serial.c