1 //==========================================================================
3 // io/serial/arm/smdk2410_serial.c
5 // Samsung SMDK2410 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): michael anburaj <michaelanburaj@hotmail.com>
44 // Contributors: michael anburaj <michaelanburaj@hotmail.com>
46 // Purpose: SMDK2410 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> // interrupt
58 #include <cyg/hal/hal_io.h> // register base
59 #include <cyg/io/devtab.h>
60 #include <cyg/io/serial.h>
61 #include <cyg/infra/diag.h>
63 #ifdef CYGPKG_IO_SERIAL_ARM_SMDK2410
65 #include "smdk2410_serial.h"
67 typedef struct smdk2410_serial_info {
70 cyg_uint32 bit_sub_rxd;
71 cyg_interrupt serial_interrupt;
72 cyg_handle_t serial_interrupt_handle;
73 } smdk2410_serial_info;
75 static bool smdk2410_serial_init(struct cyg_devtab_entry *tab);
76 static bool smdk2410_serial_putc(serial_channel *chan, unsigned char c);
77 static Cyg_ErrNo smdk2410_serial_lookup(struct cyg_devtab_entry **tab,
78 struct cyg_devtab_entry *sub_tab,
80 static unsigned char smdk2410_serial_getc(serial_channel *chan);
81 static Cyg_ErrNo smdk2410_serial_set_config(serial_channel *chan,
83 const void *xbuf, cyg_uint32 *len);
84 static void smdk2410_serial_start_xmit(serial_channel *chan);
85 static void smdk2410_serial_stop_xmit(serial_channel *chan);
87 static cyg_uint32 smdk2410_serial_ISR(cyg_vector_t vector, cyg_addrword_t data);
88 static void smdk2410_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data);
90 static SERIAL_FUNS(smdk2410_serial_funs,
93 smdk2410_serial_set_config,
94 smdk2410_serial_start_xmit,
95 smdk2410_serial_stop_xmit
98 #ifdef CYGPKG_IO_SERIAL_ARM_SMDK2410_SERIAL0
99 static smdk2410_serial_info smdk2410_serial_info0 = {(cyg_uint32)ULCON0,
100 CYGNUM_HAL_INTERRUPT_UART0,
102 #if CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL0_BUFSIZE > 0
103 static unsigned char smdk2410_serial_out_buf0[CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL0_BUFSIZE];
104 static unsigned char smdk2410_serial_in_buf0[CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL0_BUFSIZE];
106 static SERIAL_CHANNEL_USING_INTERRUPTS(smdk2410_serial_channel0,
107 smdk2410_serial_funs,
108 smdk2410_serial_info0,
109 CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL0_BAUD),
110 CYG_SERIAL_STOP_DEFAULT,
111 CYG_SERIAL_PARITY_DEFAULT,
112 CYG_SERIAL_WORD_LENGTH_DEFAULT,
113 CYG_SERIAL_FLAGS_DEFAULT,
114 &smdk2410_serial_out_buf0[0], sizeof(smdk2410_serial_out_buf0),
115 &smdk2410_serial_in_buf0[0], sizeof(smdk2410_serial_in_buf0)
118 static SERIAL_CHANNEL(smdk2410_serial_channel0,
119 smdk2410_serial_funs,
120 smdk2410_serial_info0,
121 CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL0_BAUD),
122 CYG_SERIAL_STOP_DEFAULT,
123 CYG_SERIAL_PARITY_DEFAULT,
124 CYG_SERIAL_WORD_LENGTH_DEFAULT,
125 CYG_SERIAL_FLAGS_DEFAULT
129 DEVTAB_ENTRY(smdk2410_serial_io0,
130 CYGDAT_IO_SERIAL_ARM_SMDK2410_SERIAL0_NAME,
131 0, // Does not depend on a lower level interface
132 &cyg_io_serial_devio,
133 smdk2410_serial_init,
134 smdk2410_serial_lookup, // Serial driver may need initializing
135 &smdk2410_serial_channel0
137 #endif // CYGPKG_IO_SERIAL_ARM_SMDK2410_SERIAL0
139 #ifdef CYGPKG_IO_SERIAL_ARM_SMDK2410_SERIAL1
140 static smdk2410_serial_info smdk2410_serial_info1 = {(cyg_uint32)ULCON1,
141 CYGNUM_HAL_INTERRUPT_UART1,
143 #if CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL1_BUFSIZE > 0
144 static unsigned char smdk2410_serial_out_buf1[CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL1_BUFSIZE];
145 static unsigned char smdk2410_serial_in_buf1[CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL1_BUFSIZE];
147 static SERIAL_CHANNEL_USING_INTERRUPTS(smdk2410_serial_channel1,
148 smdk2410_serial_funs,
149 smdk2410_serial_info1,
150 CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL1_BAUD),
151 CYG_SERIAL_STOP_DEFAULT,
152 CYG_SERIAL_PARITY_DEFAULT,
153 CYG_SERIAL_WORD_LENGTH_DEFAULT,
154 CYG_SERIAL_FLAGS_DEFAULT,
155 &smdk2410_serial_out_buf1[0], sizeof(smdk2410_serial_out_buf1),
156 &smdk2410_serial_in_buf1[0], sizeof(smdk2410_serial_in_buf1)
159 static SERIAL_CHANNEL(smdk2410_serial_channel1,
160 smdk2410_serial_funs,
161 smdk2410_serial_info1,
162 CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL1_BAUD),
163 CYG_SERIAL_STOP_DEFAULT,
164 CYG_SERIAL_PARITY_DEFAULT,
165 CYG_SERIAL_WORD_LENGTH_DEFAULT,
166 CYG_SERIAL_FLAGS_DEFAULT
170 DEVTAB_ENTRY(smdk2410_serial_io1,
171 CYGDAT_IO_SERIAL_ARM_SMDK2410_SERIAL1_NAME,
172 0, // Does not depend on a lower level interface
173 &cyg_io_serial_devio,
174 smdk2410_serial_init,
175 smdk2410_serial_lookup, // Serial driver may need initializing
176 &smdk2410_serial_channel1
178 #endif // CYGPKG_IO_SERIAL_ARM_SMDK2410_SERIAL1
180 // Internal function to actually configure the hardware to desired baud rate, etc.
182 smdk2410_serial_config_port(serial_channel *chan, cyg_serial_info_t *new_config, bool init)
184 smdk2410_serial_info *smdk2410_chan = (smdk2410_serial_info *)chan->dev_priv;
185 CYG_ADDRWORD base = smdk2410_chan->base;
186 unsigned short baud_divisor = select_baud[new_config->baud];
189 if (baud_divisor == 0) return false;
192 //UART FIFO control register
193 HAL_WRITE_UINT32(base+OFS_UFCON, (3<<6) | (3<<4) | (1<<2) | (1<<1) | (1<<0));
195 //UART modem control register
196 HAL_WRITE_UINT32(base+OFS_UMCON, 0);
199 _lcr = select_word_length[new_config->word_length - CYGNUM_SERIAL_WORD_LENGTH_5] |
200 select_stop_bits[new_config->stop] |
201 select_parity[new_config->parity];
202 HAL_WRITE_UINT32(base+OFS_ULCON, _lcr);
204 //UART control register, Enable Rx Timeout Int
205 HAL_WRITE_UINT32(base+OFS_UCON, 0x085);
207 if (new_config != &chan->config) {
208 chan->config = *new_config;
213 // Function to initialize the device. Called at bootstrap time.
215 smdk2410_serial_init(struct cyg_devtab_entry *tab)
217 serial_channel *chan = (serial_channel *)tab->priv;
218 smdk2410_serial_info *smdk2410_chan = (smdk2410_serial_info *)chan->dev_priv;
220 #ifdef CYGDBG_IO_INIT
221 diag_printf("SMDK2410 SERIAL init - dev: 0x%08x.%d\n",
222 smdk2410_chan->base, smdk2410_chan->int_num);
224 (chan->callbacks->serial_init)(chan); // Really only required for interrupt driven devices
225 if (chan->out_cbuf.len != 0) {
226 cyg_drv_interrupt_create(smdk2410_chan->int_num,
227 1, // Priority - unused
228 (cyg_addrword_t)chan, // Data item passed to interrupt handler
231 &smdk2410_chan->serial_interrupt_handle,
232 &smdk2410_chan->serial_interrupt);
233 cyg_drv_interrupt_attach(smdk2410_chan->serial_interrupt_handle);
234 cyg_drv_interrupt_unmask(smdk2410_chan->int_num);
236 HAL_READ_UINT32(INTSUBMSK, _intsubm);
237 _intsubm &= ~(smdk2410_chan->bit_sub_rxd<<0); // BIT_SUB_RXD
238 HAL_WRITE_UINT32(INTSUBMSK, _intsubm);
240 smdk2410_serial_config_port(chan, &chan->config, true);
244 // This routine is called when the device is "looked" up (i.e. attached)
246 smdk2410_serial_lookup(struct cyg_devtab_entry **tab,
247 struct cyg_devtab_entry *sub_tab,
250 serial_channel *chan = (serial_channel *)(*tab)->priv;
251 (chan->callbacks->serial_init)(chan); // Really only required for interrupt driven devices
255 // Send a character to the device output buffer.
256 // Return 'true' if character is sent to device
258 smdk2410_serial_putc(serial_channel *chan, unsigned char c)
260 smdk2410_serial_info *smdk2410_chan = (smdk2410_serial_info *)chan->dev_priv;
261 CYG_ADDRWORD base = smdk2410_chan->base;
264 HAL_READ_UINT32(base+OFS_UFSTAT, _status);
265 if (_status & 0x200) {
269 // Transmit buffer is not full
270 HAL_WRITE_UINT8(base+OFS_UTXH, (cyg_uint32)c);
275 // Fetch a character from the device input buffer, waiting if necessary
277 smdk2410_serial_getc(serial_channel *chan)
279 smdk2410_serial_info *smdk2410_chan = (smdk2410_serial_info *)chan->dev_priv;
280 CYG_ADDRWORD base = smdk2410_chan->base;
285 HAL_READ_UINT32(base+OFS_UFSTAT, _status);
286 } while ((_status & 0xf) == 0);
288 HAL_READ_UINT8(base+OFS_URXH, _c);
289 return (unsigned char)_c;
292 // Set up the device characteristics; baud rate, etc.
294 smdk2410_serial_set_config(serial_channel *chan, cyg_uint32 key,
295 const void *xbuf, cyg_uint32 *len)
298 case CYG_IO_SET_CONFIG_SERIAL_INFO:
300 cyg_serial_info_t *config = (cyg_serial_info_t *)xbuf;
301 if ( *len < sizeof(cyg_serial_info_t) ) {
304 *len = sizeof(cyg_serial_info_t);
305 if ( true != smdk2410_serial_config_port(chan, config, false) )
315 // Enable the transmitter on the device
317 smdk2410_serial_start_xmit(serial_channel *chan)
319 smdk2410_serial_info *smdk2410_chan = (smdk2410_serial_info *)chan->dev_priv;
322 HAL_READ_UINT32(INTSUBMSK, _intsubm);
323 _intsubm &= ~(smdk2410_chan->bit_sub_rxd<<1); // BIT_SUB_TXD
324 HAL_WRITE_UINT32(INTSUBMSK, _intsubm);
327 // Disable the transmitter on the device
329 smdk2410_serial_stop_xmit(serial_channel *chan)
331 smdk2410_serial_info *smdk2410_chan = (smdk2410_serial_info *)chan->dev_priv;
334 HAL_READ_UINT32(INTSUBMSK, _intsubm);
335 _intsubm |= (smdk2410_chan->bit_sub_rxd<<1); // BIT_SUB_TXD
336 HAL_WRITE_UINT32(INTSUBMSK, _intsubm);
339 // Serial I/O - low level interrupt handler (ISR)
341 smdk2410_serial_ISR(cyg_vector_t vector, cyg_addrword_t data)
343 serial_channel *chan = (serial_channel *)data;
344 smdk2410_serial_info *smdk2410_chan = (smdk2410_serial_info *)chan->dev_priv;
345 cyg_drv_interrupt_mask(smdk2410_chan->int_num);
346 cyg_drv_interrupt_acknowledge(smdk2410_chan->int_num);
347 return CYG_ISR_CALL_DSR; // Cause DSR to be run
350 // Serial I/O - high level interrupt handler (DSR)
352 smdk2410_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
354 serial_channel *chan = (serial_channel *)data;
355 smdk2410_serial_info *smdk2410_chan = (smdk2410_serial_info *)chan->dev_priv;
356 CYG_ADDRWORD base = smdk2410_chan->base;
357 cyg_uint32 _intsubpnd, _status, _c;
358 cyg_uint32 _rxd_bit = (smdk2410_chan->bit_sub_rxd<<0), _txd_bit=(smdk2410_chan->bit_sub_rxd<<1);
360 HAL_READ_UINT32(SUBSRCPND, _intsubpnd);
363 if (_intsubpnd & _rxd_bit) {
364 HAL_READ_UINT32(base+OFS_UFSTAT, _status);
365 while((_status & 0x0f) != 0) {
366 HAL_READ_UINT8(base+OFS_URXH, _c);
367 (chan->callbacks->rcv_char)(chan, (unsigned char)_c);
368 HAL_READ_UINT32(base+OFS_UFSTAT, _status);
370 HAL_WRITE_UINT32(SUBSRCPND, _rxd_bit);
373 // Fill into Tx FIFO. xmt_char will mask the interrupt when it
374 // runs out of chars, so doing this in a loop is OK.
375 if (_intsubpnd & _txd_bit) {
376 (chan->callbacks->xmt_char)(chan);
377 HAL_WRITE_UINT32(SUBSRCPND, _txd_bit);
380 cyg_drv_interrupt_unmask(smdk2410_chan->int_num);
383 #endif // CYGPKG_IO_SERIAL_ARM_SMDK2410
385 // EOF smdk2410_serial.c