1 //==========================================================================
3 // io/serial/mips/vrc437x_serial.c
5 // Mips VRC437X 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: VRC437X 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>
61 #ifdef CYGPKG_IO_SERIAL_MIPS_VRC437X
63 #include "vrc437x_serial.h"
65 #if defined(CYGPKG_HAL_MIPS_LSBFIRST)
66 #define VRC437X_SCC_BASE 0xC1000000
67 #elif defined(CYGPKG_HAL_MIPS_MSBFIRST)
68 #define VRC437X_SCC_BASE 0xC1000003
70 #error MIPS endianness not defined by configuration
73 #define VRC437X_SCC_INT CYGNUM_HAL_INTERRUPT_DUART
74 #define SCC_CHANNEL_A 4
75 #define SCC_CHANNEL_B 0
77 extern void diag_printf(const char *fmt, ...);
79 typedef struct vrc437x_serial_info {
81 unsigned char regs[16]; // Known register state (since hardware is write-only!)
82 } vrc437x_serial_info;
84 static bool vrc437x_serial_init(struct cyg_devtab_entry *tab);
85 static bool vrc437x_serial_putc(serial_channel *chan, unsigned char c);
86 static Cyg_ErrNo vrc437x_serial_lookup(struct cyg_devtab_entry **tab,
87 struct cyg_devtab_entry *sub_tab,
89 static unsigned char vrc437x_serial_getc(serial_channel *chan);
90 static Cyg_ErrNo vrc437x_serial_set_config(serial_channel *chan, cyg_uint32 key,
91 const void *xbuf, cyg_uint32 *len);
92 static void vrc437x_serial_start_xmit(serial_channel *chan);
93 static void vrc437x_serial_stop_xmit(serial_channel *chan);
95 static cyg_uint32 vrc437x_serial_ISR(cyg_vector_t vector, cyg_addrword_t data);
96 static void vrc437x_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data);
98 static SERIAL_FUNS(vrc437x_serial_funs,
101 vrc437x_serial_set_config,
102 vrc437x_serial_start_xmit,
103 vrc437x_serial_stop_xmit
106 #ifdef CYGPKG_IO_SERIAL_MIPS_VRC437X_SERIAL0
107 static vrc437x_serial_info vrc437x_serial_info0 = {VRC437X_SCC_BASE+SCC_CHANNEL_A};
108 #if CYGNUM_IO_SERIAL_MIPS_VRC437X_SERIAL0_BUFSIZE > 0
109 static unsigned char vrc437x_serial_out_buf0[CYGNUM_IO_SERIAL_MIPS_VRC437X_SERIAL0_BUFSIZE];
110 static unsigned char vrc437x_serial_in_buf0[CYGNUM_IO_SERIAL_MIPS_VRC437X_SERIAL0_BUFSIZE];
112 static SERIAL_CHANNEL_USING_INTERRUPTS(vrc437x_serial_channel0,
114 vrc437x_serial_info0,
115 CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_MIPS_VRC437X_SERIAL0_BAUD),
116 CYG_SERIAL_STOP_DEFAULT,
117 CYG_SERIAL_PARITY_DEFAULT,
118 CYG_SERIAL_WORD_LENGTH_DEFAULT,
119 CYG_SERIAL_FLAGS_DEFAULT,
120 &vrc437x_serial_out_buf0[0], sizeof(vrc437x_serial_out_buf0),
121 &vrc437x_serial_in_buf0[0], sizeof(vrc437x_serial_in_buf0)
124 static SERIAL_CHANNEL(vrc437x_serial_channel0,
126 vrc437x_serial_info0,
127 CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_MIPS_VRC437X_SERIAL0_BAUD),
128 CYG_SERIAL_STOP_DEFAULT,
129 CYG_SERIAL_PARITY_DEFAULT,
130 CYG_SERIAL_WORD_LENGTH_DEFAULT,
131 CYG_SERIAL_FLAGS_DEFAULT
135 DEVTAB_ENTRY(vrc437x_serial_io0,
136 CYGDAT_IO_SERIAL_MIPS_VRC437X_SERIAL0_NAME,
137 0, // Does not depend on a lower level interface
138 &cyg_io_serial_devio,
140 vrc437x_serial_lookup, // Serial driver may need initializing
141 &vrc437x_serial_channel0
143 #endif // CYGPKG_IO_SERIAL_MIPS_VRC437X_SERIAL0
145 #ifdef CYGPKG_IO_SERIAL_MIPS_VRC437X_SERIAL1
146 static vrc437x_serial_info vrc437x_serial_info1 = {VRC437X_SCC_BASE+SCC_CHANNEL_B};
147 #if CYGNUM_IO_SERIAL_MIPS_VRC437X_SERIAL1_BUFSIZE > 0
148 static unsigned char vrc437x_serial_out_buf1[CYGNUM_IO_SERIAL_MIPS_VRC437X_SERIAL1_BUFSIZE];
149 static unsigned char vrc437x_serial_in_buf1[CYGNUM_IO_SERIAL_MIPS_VRC437X_SERIAL1_BUFSIZE];
151 static SERIAL_CHANNEL_USING_INTERRUPTS(vrc437x_serial_channel1,
153 vrc437x_serial_info1,
154 CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_MIPS_VRC437X_SERIAL1_BAUD),
155 CYG_SERIAL_STOP_DEFAULT,
156 CYG_SERIAL_PARITY_DEFAULT,
157 CYG_SERIAL_WORD_LENGTH_DEFAULT,
158 CYG_SERIAL_FLAGS_DEFAULT,
159 &vrc437x_serial_out_buf1[0], sizeof(vrc437x_serial_out_buf1),
160 &vrc437x_serial_in_buf1[0], sizeof(vrc437x_serial_in_buf1)
163 static SERIAL_CHANNEL(vrc437x_serial_channel1,
165 vrc437x_serial_info1,
166 CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_MIPS_VRC437X_SERIAL1_BAUD),
167 CYG_SERIAL_STOP_DEFAULT,
168 CYG_SERIAL_PARITY_DEFAULT,
169 CYG_SERIAL_WORD_LENGTH_DEFAULT,
170 CYG_SERIAL_FLAGS_DEFAULT
174 DEVTAB_ENTRY(vrc437x_serial_io1,
175 CYGDAT_IO_SERIAL_MIPS_VRC437X_SERIAL1_NAME,
176 0, // Does not depend on a lower level interface
177 &cyg_io_serial_devio,
179 vrc437x_serial_lookup, // Serial driver may need initializing
180 &vrc437x_serial_channel1
182 #endif // CYGPKG_IO_SERIAL_MIPS_VRC437X_SERIAL1
184 static cyg_interrupt vrc437x_serial_interrupt;
185 static cyg_handle_t vrc437x_serial_interrupt_handle;
187 // Table which maps hardware channels (A,B) to software ones
188 struct serial_channel *vrc437x_chans[] = {
189 #ifdef CYGPKG_IO_SERIAL_MIPS_VRC437X_SERIAL0 // Hardware channel A
190 &vrc437x_serial_channel0,
194 #ifdef CYGPKG_IO_SERIAL_MIPS_VRC437X_SERIAL1 // Hardware channel B
195 &vrc437x_serial_channel1,
201 // Support functions which access the serial device. Note that this chip requires
202 // a substantial delay after each access.
204 #define SCC_DELAY 100
209 for (i = 0; i < SCC_DELAY; i++) ;
213 scc_write_reg(volatile unsigned char *reg, unsigned char val)
219 inline static unsigned char
220 scc_read_reg(volatile unsigned char *reg)
228 inline static unsigned char
229 scc_read_ctl(volatile struct serial_port *port, int reg)
232 scc_write_reg(&port->scc_ctl, reg);
234 return (scc_read_reg(&port->scc_ctl));
238 scc_write_ctl(volatile struct serial_port *port, int reg, unsigned char val)
241 scc_write_reg(&port->scc_ctl, reg);
243 scc_write_reg(&port->scc_ctl, val);
246 inline static unsigned char
247 scc_read_dat(volatile struct serial_port *port)
249 return (scc_read_reg(&port->scc_dat));
253 scc_write_dat(volatile struct serial_port *port, unsigned char val)
255 scc_write_reg(&port->scc_dat, val);
258 // Internal function to actually configure the hardware to desired baud rate, etc.
260 vrc437x_serial_config_port(serial_channel *chan, cyg_serial_info_t *new_config, bool init)
262 vrc437x_serial_info *vrc437x_chan = (vrc437x_serial_info *)chan->dev_priv;
263 volatile struct serial_port *port = (volatile struct serial_port *)vrc437x_chan->base;
264 cyg_int32 baud_rate = select_baud[new_config->baud];
265 cyg_int32 baud_divisor;
266 unsigned char *regs = &vrc437x_chan->regs[0];
267 if (baud_rate == 0) return false;
268 // Compute state of registers. The register/control state needs to be kept in
269 // the shadow variable 'regs' because the hardware registers can only be written,
270 // not read (in general).
272 // Insert appropriate resets?
273 if (chan->out_cbuf.len != 0) {
274 regs[R1] = WR1_IntAllRx;
275 regs[R9] = WR9_MIE | WR9_NoVector;
280 // Clocks are from the baud rate generator
281 regs[R11] = WR11_TRxCBR | WR11_TRxCOI | WR11_TxCBR | WR11_RxCBR;
282 regs[R14] = WR14_BRenable | WR14_BRSRC;
283 regs[R10] = 0; // Unused in this [async] mode
286 regs[R3] = WR3_RxEnable | select_word_length_WR3[new_config->word_length - CYGNUM_SERIAL_WORD_LENGTH_5];
287 regs[R4] = WR4_X16CLK | select_stop_bits[new_config->stop] | select_parity[new_config->parity];
288 regs[R5] = WR5_TxEnable | select_word_length_WR5[new_config->word_length - CYGNUM_SERIAL_WORD_LENGTH_5];
289 baud_divisor = BRTC(baud_rate);
290 regs[R12] = baud_divisor & 0xFF;
291 regs[R13] = baud_divisor >> 8;
292 // Now load the registers
293 scc_write_ctl(port, R4, regs[R4]);
294 scc_write_ctl(port, R10, regs[R10]);
295 scc_write_ctl(port, R3, regs[R3] & ~WR3_RxEnable);
296 scc_write_ctl(port, R5, regs[R5] & ~WR5_TxEnable);
297 scc_write_ctl(port, R1, regs[R1]);
298 scc_write_ctl(port, R9, regs[R9]);
299 scc_write_ctl(port, R11, regs[R11]);
300 scc_write_ctl(port, R12, regs[R12]);
301 scc_write_ctl(port, R13, regs[R13]);
302 scc_write_ctl(port, R14, regs[R14]);
303 scc_write_ctl(port, R15, regs[R15]);
304 scc_write_ctl(port, R3, regs[R3]);
305 scc_write_ctl(port, R5, regs[R5]);
306 // Update configuration
307 if (new_config != &chan->config) {
308 chan->config = *new_config;
313 // Function to initialize the device. Called at bootstrap time.
315 vrc437x_serial_init(struct cyg_devtab_entry *tab)
317 serial_channel *chan = (serial_channel *)tab->priv;
318 vrc437x_serial_info *vrc437x_chan = (vrc437x_serial_info *)chan->dev_priv;
319 static bool init = false;
320 #ifdef CYGDBG_IO_INIT
321 diag_printf("VRC437X SERIAL init '%s' - dev: %x\n", tab->name, vrc437x_chan->base);
323 (chan->callbacks->serial_init)(chan); // Really only required for interrupt driven devices
324 if (!init && chan->out_cbuf.len != 0) {
326 // Note that the hardware is rather broken. The interrupt status needs to
327 // be read using only channel A
328 cyg_drv_interrupt_create(VRC437X_SCC_INT,
330 (cyg_addrword_t)VRC437X_SCC_BASE+SCC_CHANNEL_A,
333 &vrc437x_serial_interrupt_handle,
334 &vrc437x_serial_interrupt);
335 cyg_drv_interrupt_attach(vrc437x_serial_interrupt_handle);
336 cyg_drv_interrupt_unmask(VRC437X_SCC_INT);
338 vrc437x_serial_config_port(chan, &chan->config, true);
342 // This routine is called when the device is "looked" up (i.e. attached)
344 vrc437x_serial_lookup(struct cyg_devtab_entry **tab,
345 struct cyg_devtab_entry *sub_tab,
348 serial_channel *chan = (serial_channel *)(*tab)->priv;
349 (chan->callbacks->serial_init)(chan); // Really only required for interrupt driven devices
353 // Send a character to the device output buffer.
354 // Return 'true' if character is sent to device
356 vrc437x_serial_putc(serial_channel *chan, unsigned char c)
358 vrc437x_serial_info *vrc437x_chan = (vrc437x_serial_info *)chan->dev_priv;
359 volatile struct serial_port *port = (volatile struct serial_port *)vrc437x_chan->base;
360 if (scc_read_ctl(port, R0) & RR0_TxEmpty) {
361 // Transmit buffer is empty
362 scc_write_dat(port, c);
370 // Fetch a character from the device input buffer, waiting if necessary
372 vrc437x_serial_getc(serial_channel *chan)
375 vrc437x_serial_info *vrc437x_chan = (vrc437x_serial_info *)chan->dev_priv;
376 volatile struct serial_port *port = (volatile struct serial_port *)vrc437x_chan->base;
377 while ((scc_read_ctl(port, R0) & RR0_RxAvail) == 0) ; // Wait for char
378 c = scc_read_dat(port);
382 // Set up the device characteristics; baud rate, etc.
384 vrc437x_serial_set_config(serial_channel *chan, cyg_uint32 key,
385 const void *xbuf, cyg_uint32 *len)
388 case CYG_IO_SET_CONFIG_SERIAL_INFO:
390 cyg_serial_info_t *config = (cyg_serial_info_t *)xbuf;
391 if ( *len < sizeof(cyg_serial_info_t) ) {
394 *len = sizeof(cyg_serial_info_t);
395 if ( true != vrc437x_serial_config_port(chan, config, false) )
405 // Enable the transmitter on the device
407 vrc437x_serial_start_xmit(serial_channel *chan)
409 vrc437x_serial_info *vrc437x_chan = (vrc437x_serial_info *)chan->dev_priv;
410 volatile struct serial_port *port = (volatile struct serial_port *)vrc437x_chan->base;
411 if ((vrc437x_chan->regs[R1] & WR1_TxIntEnab) == 0) {
412 CYG_INTERRUPT_STATE old;
413 HAL_DISABLE_INTERRUPTS(old);
414 vrc437x_chan->regs[R1] |= WR1_TxIntEnab; // Enable Tx interrupt
415 scc_write_ctl(port, R1, vrc437x_chan->regs[R1]);
416 (chan->callbacks->xmt_char)(chan); // Send first character to start xmitter
417 HAL_RESTORE_INTERRUPTS(old);
421 // Disable the transmitter on the device
423 vrc437x_serial_stop_xmit(serial_channel *chan)
425 vrc437x_serial_info *vrc437x_chan = (vrc437x_serial_info *)chan->dev_priv;
426 volatile struct serial_port *port = (volatile struct serial_port *)vrc437x_chan->base;
427 if ((vrc437x_chan->regs[R1] & WR1_TxIntEnab) != 0) {
428 CYG_INTERRUPT_STATE old;
429 HAL_DISABLE_INTERRUPTS(old);
430 vrc437x_chan->regs[R1] &= ~WR1_TxIntEnab; // Disable Tx interrupt
431 scc_write_ctl(port, R1, vrc437x_chan->regs[R1]);
432 HAL_RESTORE_INTERRUPTS(old);
436 // Serial I/O - low level interrupt handler (ISR)
438 vrc437x_serial_ISR(cyg_vector_t vector, cyg_addrword_t data)
440 cyg_drv_interrupt_mask(VRC437X_SCC_INT);
441 cyg_drv_interrupt_acknowledge(VRC437X_SCC_INT);
442 return CYG_ISR_CALL_DSR; // Cause DSR to be run
446 vrc437x_int(serial_channel *chan, unsigned char stat)
448 vrc437x_serial_info *vrc437x_chan = (vrc437x_serial_info *)chan->dev_priv;
449 volatile struct serial_port *port = (volatile struct serial_port *)vrc437x_chan->base;
450 // Note: 'stat' value is interrupt status register, shifted into "B" position
451 if (stat & RR3_BRxIP) {
454 c = scc_read_dat(port);
455 (chan->callbacks->rcv_char)(chan, c);
457 if (stat & RR3_BTxIP) {
458 // Transmit interrupt
459 (chan->callbacks->xmt_char)(chan);
461 if (stat & RR3_BExt) {
462 // Status interrupt (parity error, framing error, etc)
466 // Serial I/O - high level interrupt handler (DSR)
467 // Note: This device presents a single interrupt for both channels. Thus the
468 // interrupt handler has to query the device and decide which channel needs service.
469 // Additionally, more than one interrupt condition may be present so this needs to
470 // be done in a loop until all interrupt requests have been handled.
471 // Also note that the hardware is rather broken. The interrupt status needs to
472 // be read using only channel A (pointed to by 'data')
474 vrc437x_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
476 serial_channel *chan;
477 volatile struct serial_port *port = (volatile struct serial_port *)data;
480 stat = scc_read_ctl(port, R3);
481 if (stat & (RR3_AExt | RR3_ATxIP | RR3_ARxIP)) {
482 chan = vrc437x_chans[0]; // Hardware channel A
483 vrc437x_int(chan, stat>>3); // Handle interrupt
484 } else if (stat & (RR3_BExt | RR3_BTxIP | RR3_BRxIP)) {
485 chan = vrc437x_chans[1]; // Hardware channel B
486 vrc437x_int(chan, stat); // Handle interrupt
488 // No more interrupts, all done
492 cyg_drv_interrupt_unmask(VRC437X_SCC_INT);