OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [devs/] [serial/] [arm/] [smdk2410/] [current/] [src/] [smdk2410_serial.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      io/serial/arm/smdk2410_serial.c
4
//
5
//      Samsung SMDK2410 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 Free Software Foundation, 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      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):    michael anburaj <michaelanburaj@hotmail.com>
43
// Contributors: michael anburaj <michaelanburaj@hotmail.com>
44
// Date:         2003-08-01
45
// Purpose:      SMDK2410 Serial I/O module (interrupt driven version)
46
// Description: 
47
//
48
//####DESCRIPTIONEND####
49
//
50
//==========================================================================
51
 
52
#include <pkgconf/system.h>
53
#include <pkgconf/io_serial.h>
54
#include <pkgconf/io.h>
55
#include <cyg/io/io.h>
56
#include <cyg/hal/hal_intr.h>           // interrupt
57
#include <cyg/hal/hal_io.h>             // register base
58
#include <cyg/io/devtab.h>
59
#include <cyg/io/serial.h>
60
#include <cyg/infra/diag.h>
61
 
62
#ifdef CYGPKG_IO_SERIAL_ARM_SMDK2410
63
 
64
#include "smdk2410_serial.h"
65
 
66
typedef struct smdk2410_serial_info {
67
    CYG_ADDRWORD   base;
68
    CYG_WORD       int_num;
69
    cyg_uint32     bit_sub_rxd;
70
    cyg_interrupt  serial_interrupt;
71
    cyg_handle_t   serial_interrupt_handle;
72
} smdk2410_serial_info;
73
 
74
static bool smdk2410_serial_init(struct cyg_devtab_entry *tab);
75
static bool smdk2410_serial_putc(serial_channel *chan, unsigned char c);
76
static Cyg_ErrNo smdk2410_serial_lookup(struct cyg_devtab_entry **tab,
77
                                        struct cyg_devtab_entry *sub_tab,
78
                                        const char *name);
79
static unsigned char smdk2410_serial_getc(serial_channel *chan);
80
static Cyg_ErrNo smdk2410_serial_set_config(serial_channel *chan,
81
                                            cyg_uint32 key,
82
                                            const void *xbuf, cyg_uint32 *len);
83
static void smdk2410_serial_start_xmit(serial_channel *chan);
84
static void smdk2410_serial_stop_xmit(serial_channel *chan);
85
 
86
static cyg_uint32 smdk2410_serial_ISR(cyg_vector_t vector, cyg_addrword_t data);
87
static void       smdk2410_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data);
88
 
89
static SERIAL_FUNS(smdk2410_serial_funs,
90
                   smdk2410_serial_putc,
91
                   smdk2410_serial_getc,
92
                   smdk2410_serial_set_config,
93
                   smdk2410_serial_start_xmit,
94
                   smdk2410_serial_stop_xmit
95
    );
96
 
97
#ifdef CYGPKG_IO_SERIAL_ARM_SMDK2410_SERIAL0
98
static smdk2410_serial_info smdk2410_serial_info0 = {(cyg_uint32)ULCON0,
99
                                                     CYGNUM_HAL_INTERRUPT_UART0,
100
                                                     BIT_SUB_RXD0};
101
#if CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL0_BUFSIZE > 0
102
static unsigned char smdk2410_serial_out_buf0[CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL0_BUFSIZE];
103
static unsigned char smdk2410_serial_in_buf0[CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL0_BUFSIZE];
104
 
105
static SERIAL_CHANNEL_USING_INTERRUPTS(smdk2410_serial_channel0,
106
                                       smdk2410_serial_funs,
107
                                       smdk2410_serial_info0,
108
                                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL0_BAUD),
109
                                       CYG_SERIAL_STOP_DEFAULT,
110
                                       CYG_SERIAL_PARITY_DEFAULT,
111
                                       CYG_SERIAL_WORD_LENGTH_DEFAULT,
112
                                       CYG_SERIAL_FLAGS_DEFAULT,
113
                                       &smdk2410_serial_out_buf0[0], sizeof(smdk2410_serial_out_buf0),
114
                                       &smdk2410_serial_in_buf0[0], sizeof(smdk2410_serial_in_buf0)
115
    );
116
#else
117
static SERIAL_CHANNEL(smdk2410_serial_channel0,
118
                      smdk2410_serial_funs,
119
                      smdk2410_serial_info0,
120
                      CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL0_BAUD),
121
                      CYG_SERIAL_STOP_DEFAULT,
122
                      CYG_SERIAL_PARITY_DEFAULT,
123
                      CYG_SERIAL_WORD_LENGTH_DEFAULT,
124
                      CYG_SERIAL_FLAGS_DEFAULT
125
    );
126
#endif
127
 
128
DEVTAB_ENTRY(smdk2410_serial_io0,
129
             CYGDAT_IO_SERIAL_ARM_SMDK2410_SERIAL0_NAME,
130
             0,                          // Does not depend on a lower level interface
131
             &cyg_io_serial_devio,
132
             smdk2410_serial_init,
133
             smdk2410_serial_lookup,     // Serial driver may need initializing
134
             &smdk2410_serial_channel0
135
    );
136
#endif //  CYGPKG_IO_SERIAL_ARM_SMDK2410_SERIAL0
137
 
138
#ifdef CYGPKG_IO_SERIAL_ARM_SMDK2410_SERIAL1
139
static smdk2410_serial_info smdk2410_serial_info1 = {(cyg_uint32)ULCON1,
140
                                                     CYGNUM_HAL_INTERRUPT_UART1,
141
                                                     BIT_SUB_RXD1};
142
#if CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL1_BUFSIZE > 0
143
static unsigned char smdk2410_serial_out_buf1[CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL1_BUFSIZE];
144
static unsigned char smdk2410_serial_in_buf1[CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL1_BUFSIZE];
145
 
146
static SERIAL_CHANNEL_USING_INTERRUPTS(smdk2410_serial_channel1,
147
                                       smdk2410_serial_funs,
148
                                       smdk2410_serial_info1,
149
                                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL1_BAUD),
150
                                       CYG_SERIAL_STOP_DEFAULT,
151
                                       CYG_SERIAL_PARITY_DEFAULT,
152
                                       CYG_SERIAL_WORD_LENGTH_DEFAULT,
153
                                       CYG_SERIAL_FLAGS_DEFAULT,
154
                                       &smdk2410_serial_out_buf1[0], sizeof(smdk2410_serial_out_buf1),
155
                                       &smdk2410_serial_in_buf1[0], sizeof(smdk2410_serial_in_buf1)
156
    );
157
#else
158
static SERIAL_CHANNEL(smdk2410_serial_channel1,
159
                      smdk2410_serial_funs,
160
                      smdk2410_serial_info1,
161
                      CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_SMDK2410_SERIAL1_BAUD),
162
                      CYG_SERIAL_STOP_DEFAULT,
163
                      CYG_SERIAL_PARITY_DEFAULT,
164
                      CYG_SERIAL_WORD_LENGTH_DEFAULT,
165
                      CYG_SERIAL_FLAGS_DEFAULT
166
    );
167
#endif
168
 
169
DEVTAB_ENTRY(smdk2410_serial_io1,
170
             CYGDAT_IO_SERIAL_ARM_SMDK2410_SERIAL1_NAME,
171
             0,                          // Does not depend on a lower level interface
172
             &cyg_io_serial_devio,
173
             smdk2410_serial_init,
174
             smdk2410_serial_lookup,     // Serial driver may need initializing
175
             &smdk2410_serial_channel1
176
    );
177
#endif //  CYGPKG_IO_SERIAL_ARM_SMDK2410_SERIAL1
178
 
179
// Internal function to actually configure the hardware to desired baud rate, etc.
180
static bool
181
smdk2410_serial_config_port(serial_channel *chan, cyg_serial_info_t *new_config, bool init)
182
{
183
    smdk2410_serial_info *smdk2410_chan = (smdk2410_serial_info *)chan->dev_priv;
184
    CYG_ADDRWORD base = smdk2410_chan->base;
185
    unsigned short baud_divisor = select_baud[new_config->baud];
186
    cyg_uint32 _lcr;
187
 
188
    if (baud_divisor == 0) return false;
189
 
190
    if (init) {
191
        //UART FIFO control register
192
        HAL_WRITE_UINT32(base+OFS_UFCON, (3<<6) | (3<<4) | (1<<2) | (1<<1) | (1<<0));
193
 
194
        //UART modem control register
195
        HAL_WRITE_UINT32(base+OFS_UMCON, 0);
196
    }
197
 
198
    _lcr = select_word_length[new_config->word_length - CYGNUM_SERIAL_WORD_LENGTH_5] |
199
        select_stop_bits[new_config->stop] |
200
        select_parity[new_config->parity];
201
    HAL_WRITE_UINT32(base+OFS_ULCON, _lcr);
202
 
203
    //UART control register, Enable Rx Timeout Int
204
    HAL_WRITE_UINT32(base+OFS_UCON, 0x085);
205
 
206
    if (new_config != &chan->config) {
207
        chan->config = *new_config;
208
    }
209
    return true;
210
}
211
 
212
// Function to initialize the device.  Called at bootstrap time.
213
static bool
214
smdk2410_serial_init(struct cyg_devtab_entry *tab)
215
{
216
    serial_channel *chan = (serial_channel *)tab->priv;
217
    smdk2410_serial_info *smdk2410_chan = (smdk2410_serial_info *)chan->dev_priv;
218
    cyg_uint32 _intsubm;
219
#ifdef CYGDBG_IO_INIT
220
    diag_printf("SMDK2410 SERIAL init - dev: 0x%08x.%d\n",
221
                smdk2410_chan->base, smdk2410_chan->int_num);
222
#endif
223
    (chan->callbacks->serial_init)(chan);  // Really only required for interrupt driven devices
224
    if (chan->out_cbuf.len != 0) {
225
        cyg_drv_interrupt_create(smdk2410_chan->int_num,
226
                                 1,                    // Priority - unused
227
                                 (cyg_addrword_t)chan, //  Data item passed to interrupt handler
228
                                 smdk2410_serial_ISR,
229
                                 smdk2410_serial_DSR,
230
                                 &smdk2410_chan->serial_interrupt_handle,
231
                                 &smdk2410_chan->serial_interrupt);
232
        cyg_drv_interrupt_attach(smdk2410_chan->serial_interrupt_handle);
233
        cyg_drv_interrupt_unmask(smdk2410_chan->int_num);
234
 
235
        HAL_READ_UINT32(INTSUBMSK, _intsubm);
236
        _intsubm &= ~(smdk2410_chan->bit_sub_rxd<<0);         // BIT_SUB_RXD
237
        HAL_WRITE_UINT32(INTSUBMSK, _intsubm);
238
    }
239
    smdk2410_serial_config_port(chan, &chan->config, true);
240
    return true;
241
}
242
 
243
// This routine is called when the device is "looked" up (i.e. attached)
244
static Cyg_ErrNo
245
smdk2410_serial_lookup(struct cyg_devtab_entry **tab,
246
                  struct cyg_devtab_entry *sub_tab,
247
                  const char *name)
248
{
249
    serial_channel *chan = (serial_channel *)(*tab)->priv;
250
    (chan->callbacks->serial_init)(chan);  // Really only required for interrupt driven devices
251
    return ENOERR;
252
}
253
 
254
// Send a character to the device output buffer.
255
// Return 'true' if character is sent to device
256
static bool
257
smdk2410_serial_putc(serial_channel *chan, unsigned char c)
258
{
259
    smdk2410_serial_info *smdk2410_chan = (smdk2410_serial_info *)chan->dev_priv;
260
    CYG_ADDRWORD base = smdk2410_chan->base;
261
    cyg_uint32 _status;
262
 
263
    HAL_READ_UINT32(base+OFS_UFSTAT, _status);
264
    if (_status & 0x200) {
265
        // No space
266
        return false;
267
    } else {
268
        // Transmit buffer is not full
269
        HAL_WRITE_UINT8(base+OFS_UTXH, (cyg_uint32)c);
270
        return true;
271
    }
272
}
273
 
274
// Fetch a character from the device input buffer, waiting if necessary
275
static unsigned char
276
smdk2410_serial_getc(serial_channel *chan)
277
{
278
    smdk2410_serial_info *smdk2410_chan = (smdk2410_serial_info *)chan->dev_priv;
279
    CYG_ADDRWORD base = smdk2410_chan->base;
280
    cyg_uint32 _status;
281
    cyg_uint8 _c;
282
 
283
    do {
284
        HAL_READ_UINT32(base+OFS_UFSTAT, _status);
285
    } while ((_status & 0xf) == 0);
286
 
287
    HAL_READ_UINT8(base+OFS_URXH, _c);
288
    return (unsigned char)_c;
289
}
290
 
291
// Set up the device characteristics; baud rate, etc.
292
static Cyg_ErrNo
293
smdk2410_serial_set_config(serial_channel *chan, cyg_uint32 key,
294
                      const void *xbuf, cyg_uint32 *len)
295
{
296
    switch (key) {
297
    case CYG_IO_SET_CONFIG_SERIAL_INFO:
298
      {
299
        cyg_serial_info_t *config = (cyg_serial_info_t *)xbuf;
300
        if ( *len < sizeof(cyg_serial_info_t) ) {
301
            return -EINVAL;
302
        }
303
        *len = sizeof(cyg_serial_info_t);
304
        if ( true != smdk2410_serial_config_port(chan, config, false) )
305
            return -EINVAL;
306
      }
307
      break;
308
    default:
309
        return -EINVAL;
310
    }
311
    return ENOERR;
312
}
313
 
314
// Enable the transmitter on the device
315
static void
316
smdk2410_serial_start_xmit(serial_channel *chan)
317
{
318
    smdk2410_serial_info *smdk2410_chan = (smdk2410_serial_info *)chan->dev_priv;
319
    cyg_uint32 _intsubm;
320
 
321
    HAL_READ_UINT32(INTSUBMSK, _intsubm);
322
    _intsubm &= ~(smdk2410_chan->bit_sub_rxd<<1);         // BIT_SUB_TXD
323
    HAL_WRITE_UINT32(INTSUBMSK, _intsubm);
324
}
325
 
326
// Disable the transmitter on the device
327
static void
328
smdk2410_serial_stop_xmit(serial_channel *chan)
329
{
330
    smdk2410_serial_info *smdk2410_chan = (smdk2410_serial_info *)chan->dev_priv;
331
    cyg_uint32 _intsubm;
332
 
333
    HAL_READ_UINT32(INTSUBMSK, _intsubm);
334
    _intsubm |= (smdk2410_chan->bit_sub_rxd<<1);          // BIT_SUB_TXD
335
    HAL_WRITE_UINT32(INTSUBMSK, _intsubm);
336
}
337
 
338
// Serial I/O - low level interrupt handler (ISR)
339
static cyg_uint32
340
smdk2410_serial_ISR(cyg_vector_t vector, cyg_addrword_t data)
341
{
342
    serial_channel *chan = (serial_channel *)data;
343
    smdk2410_serial_info *smdk2410_chan = (smdk2410_serial_info *)chan->dev_priv;
344
    cyg_drv_interrupt_mask(smdk2410_chan->int_num);
345
    cyg_drv_interrupt_acknowledge(smdk2410_chan->int_num);
346
    return CYG_ISR_CALL_DSR;  // Cause DSR to be run
347
}
348
 
349
// Serial I/O - high level interrupt handler (DSR)
350
static void
351
smdk2410_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
352
{
353
    serial_channel *chan = (serial_channel *)data;
354
    smdk2410_serial_info *smdk2410_chan = (smdk2410_serial_info *)chan->dev_priv;
355
    CYG_ADDRWORD base = smdk2410_chan->base;
356
    cyg_uint32 _intsubpnd, _status, _c;
357
    cyg_uint32 _rxd_bit = (smdk2410_chan->bit_sub_rxd<<0), _txd_bit=(smdk2410_chan->bit_sub_rxd<<1);
358
 
359
    HAL_READ_UINT32(SUBSRCPND, _intsubpnd);
360
 
361
    // Empty Rx FIFO
362
    if (_intsubpnd & _rxd_bit) {
363
        HAL_READ_UINT32(base+OFS_UFSTAT, _status);
364
        while((_status & 0x0f) != 0) {
365
            HAL_READ_UINT8(base+OFS_URXH, _c);
366
            (chan->callbacks->rcv_char)(chan, (unsigned char)_c);
367
            HAL_READ_UINT32(base+OFS_UFSTAT, _status);
368
        }
369
        HAL_WRITE_UINT32(SUBSRCPND, _rxd_bit);
370
    }
371
 
372
    // Fill into Tx FIFO. xmt_char will mask the interrupt when it
373
    // runs out of chars, so doing this in a loop is OK.
374
    if (_intsubpnd & _txd_bit) {
375
        (chan->callbacks->xmt_char)(chan);
376
        HAL_WRITE_UINT32(SUBSRCPND, _txd_bit);
377
    }
378
 
379
    cyg_drv_interrupt_unmask(smdk2410_chan->int_num);
380
}
381
 
382
#endif // CYGPKG_IO_SERIAL_ARM_SMDK2410
383
 
384
// EOF smdk2410_serial.c

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.