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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [devs/] [serial/] [mips/] [idt79s334a/] [v2_0/] [src/] [mipsidt_serial.c] - Blame information for rev 587

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      mipsidt_serial.c
4
//
5
//      Serial device driver for MIPS IDT79s334a reference platform on-chip serial devices
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):    tmichals based on driver by dmoseley, based on POWERPC driver by jskov
44
// Contributors: gthomas, jskov, dmoseley, tmichals
45
// Date:         2003-02-13
46
// Purpose:      MIPS IDT79s334a reference platform serial device driver
47
// Description:  IDT MIPS serial device driver
48
//
49
// To Do:
50
//   Put in magic to effectively use the FIFOs. Transmitter FIFO fill is a
51
//   problem, and setting receiver FIFO interrupts to happen only after
52
//   n chars may conflict with hal diag.
53
//
54
//####DESCRIPTIONEND####
55
//
56
//==========================================================================
57
 
58
#include <pkgconf/io_serial.h>
59
#include <pkgconf/io.h>
60
 
61
#include <cyg/io/io.h>
62
#include <cyg/hal/hal_intr.h>
63
#include <cyg/io/devtab.h>
64
#include <cyg/infra/diag.h>
65
#include <cyg/io/serial.h>
66
 
67
#ifdef CYGPKG_IO_SERIAL_MIPS_IDT79S334A
68
 
69
#include "mipsidt_serial.h"
70
 
71
typedef struct mipsidt_serial_info {
72
    CYG_ADDRWORD   base;
73
    CYG_WORD       int_num;
74
    cyg_interrupt  serial_interrupt;
75
    cyg_handle_t   serial_interrupt_handle;
76
    cyg_uint8      iir;
77
} mipsidt_serial_info;
78
 
79
static bool mipsidt_serial_init(struct cyg_devtab_entry *tab);
80
static bool mipsidt_serial_putc(serial_channel *chan, unsigned char c);
81
static Cyg_ErrNo mipsidt_serial_lookup(struct cyg_devtab_entry **tab,
82
                                   struct cyg_devtab_entry *sub_tab,
83
                                   const char *name);
84
static unsigned char mipsidt_serial_getc(serial_channel *chan);
85
static Cyg_ErrNo mipsidt_serial_set_config(serial_channel *chan, cyg_uint32 key,
86
                                         const void *xbuf, cyg_uint32 *len);
87
static void mipsidt_serial_start_xmit(serial_channel *chan);
88
static void mipsidt_serial_stop_xmit(serial_channel *chan);
89
 
90
static cyg_uint32 mipsidt_serial_ISR(cyg_vector_t vector, cyg_addrword_t data);
91
static void       mipsidt_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data);
92
 
93
static SERIAL_FUNS(mipsidt_serial_funs,
94
                   mipsidt_serial_putc,
95
                   mipsidt_serial_getc,
96
                   mipsidt_serial_set_config,
97
                   mipsidt_serial_start_xmit,
98
                   mipsidt_serial_stop_xmit
99
    );
100
 
101
static mipsidt_serial_info mipsidt_serial_info0 ={IDTMIPS_SER_16550_BASE_A,
102
                                              CYGNUM_HAL_INTERRUPT_SIO_0};
103
 
104
#if CYGNUM_IO_SERIAL_MIPS_IDT79S334A_SERIAL_A_BUFSIZE > 0
105
static unsigned char mipsidt_serial_out_buf0[CYGNUM_IO_SERIAL_MIPS_IDT79S334A_SERIAL_A_BUFSIZE];
106
static unsigned char mipsidt_serial_in_buf0[CYGNUM_IO_SERIAL_MIPS_IDT79S334A_SERIAL_A_BUFSIZE];
107
 
108
static SERIAL_CHANNEL_USING_INTERRUPTS(mipsidt_serial_channel0,
109
                                       mipsidt_serial_funs,
110
                                       mipsidt_serial_info0,
111
                                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_MIPS_IDT79S334A_SERIAL_A_BAUD),
112
                                       CYG_SERIAL_STOP_DEFAULT,
113
                                       CYG_SERIAL_PARITY_DEFAULT,
114
                                       CYG_SERIAL_WORD_LENGTH_DEFAULT,
115
                                       CYG_SERIAL_FLAGS_DEFAULT,
116
                                       &mipsidt_serial_out_buf0[0],
117
                                       sizeof(mipsidt_serial_out_buf0),
118
                                       &mipsidt_serial_in_buf0[0],
119
                                       sizeof(mipsidt_serial_in_buf0)
120
    );
121
#else
122
static SERIAL_CHANNEL(mipsidt_serial_channel0,
123
                      mipsidt_serial_funs,
124
                      mipsidt_serial_info0,
125
                      CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_MIPS_IDT79S334A_SERIAL_A_BAUD),
126
                      CYG_SERIAL_STOP_DEFAULT,
127
                      CYG_SERIAL_PARITY_DEFAULT,
128
                      CYG_SERIAL_WORD_LENGTH_DEFAULT,
129
                      CYG_SERIAL_FLAGS_DEFAULT
130
    );
131
#endif
132
 
133
DEVTAB_ENTRY(mipsidt_serial_io0,
134
             CYGDAT_IO_SERIAL_MIPS_IDT79S334A_SERIAL_A_NAME,
135
             0,                 // Does not depend on a lower level interface
136
             &cyg_io_serial_devio,
137
             mipsidt_serial_init,
138
             mipsidt_serial_lookup,     // Serial driver may need initializing
139
             &mipsidt_serial_channel0
140
    );
141
 
142
 
143
 
144
// Internal function to actually configure the hardware to desired baud rate, etc.
145
static bool
146
mipsidt_serial_config_port(serial_channel *chan, cyg_serial_info_t *new_config, bool init)
147
{
148
    mipsidt_serial_info *mipsidt_chan = (mipsidt_serial_info *)chan->dev_priv;
149
    cyg_addrword_t port = mipsidt_chan->base;
150
    cyg_uint32 baud_divisor = select_baud[new_config->baud]; ;
151
    cyg_uint8 _lcr, _ier;
152
 
153
    if (baud_divisor == 0)
154
        return false;    // Invalid baud rate selected
155
 
156
        baud_divisor = (CYGHWR_HAL_MIPS_CPU_FREQ_ACTUAL * 10) / (16 * select_baud[new_config->baud]);
157
 
158
        baud_divisor +=5;
159
        baud_divisor =  ((cyg_int32)baud_divisor) / 10;
160
 
161
 
162
    // Disable port interrupts while changing hardware
163
    HAL_READ_UINT8(port+SER_16550_IER, _ier);
164
    HAL_WRITE_UINT8(port+SER_16550_IER, 0);
165
 
166
    // Set databits, stopbits and parity.
167
    _lcr = select_word_length[(new_config->word_length -
168
                               CYGNUM_SERIAL_WORD_LENGTH_5)] |
169
        select_stop_bits[new_config->stop] |
170
        select_parity[new_config->parity];
171
    HAL_WRITE_UINT8(port+SER_16550_LCR, _lcr);
172
 
173
    // Set baud rate.
174
    _lcr |= LCR_DL;
175
    HAL_WRITE_UINT8(port+SER_16550_LCR, _lcr);
176
    HAL_WRITE_UINT8(port+SER_16550_DLM, baud_divisor >> 8);
177
    HAL_WRITE_UINT8(port+SER_16550_DLL, baud_divisor & 0xff);
178
    _lcr &= ~LCR_DL;
179
    HAL_WRITE_UINT8(port+SER_16550_LCR, _lcr);
180
 
181
    if (init) {
182
        // Enable and clear FIFO
183
        HAL_WRITE_UINT8(port+SER_16550_FCR,
184
                        (FCR_ENABLE | FCR_CLEAR_RCVR | FCR_CLEAR_XMIT));
185
 
186
        if (chan->out_cbuf.len != 0) {
187
            HAL_WRITE_UINT8(port+SER_16550_IER, SIO_IER_ERDAI);
188
        } else {
189
            HAL_WRITE_UINT8(port+SER_16550_IER, 0);
190
        }
191
    } else {
192
        HAL_WRITE_UINT8(port+SER_16550_IER, _ier);
193
    }
194
    if (new_config != &chan->config) {
195
        chan->config = *new_config;
196
    }
197
    return true;
198
}
199
 
200
// Function to initialize the device.  Called at bootstrap time.
201
static bool
202
mipsidt_serial_init(struct cyg_devtab_entry *tab)
203
{
204
    serial_channel *chan = (serial_channel *)tab->priv;
205
    mipsidt_serial_info *mipsidt_chan = (mipsidt_serial_info *)chan->dev_priv;
206
#ifdef CYGDBG_IO_INIT
207
    diag_printf("IDT SERIAL init - dev: %x.%d\n", mipsidt_chan->base, mipsidt_chan->int_num);
208
#endif
209
    (chan->callbacks->serial_init)(chan);  // Really only required for interrupt driven devices
210
    if (chan->out_cbuf.len != 0) {
211
        cyg_drv_interrupt_create(mipsidt_chan->int_num,
212
                                 0,         // can change IRQ0 priority
213
                                 (cyg_addrword_t)chan,   //  Data item passed to interrupt handler
214
                                 mipsidt_serial_ISR,
215
                                 mipsidt_serial_DSR,
216
                                 &mipsidt_chan->serial_interrupt_handle,
217
                                 &mipsidt_chan->serial_interrupt);
218
        cyg_drv_interrupt_attach(mipsidt_chan->serial_interrupt_handle);
219
        cyg_drv_interrupt_unmask(mipsidt_chan->int_num);
220
    }
221
    mipsidt_serial_config_port(chan, &chan->config, true);
222
    return true;
223
}
224
 
225
// This routine is called when the device is "looked" up (i.e. attached)
226
static Cyg_ErrNo
227
mipsidt_serial_lookup(struct cyg_devtab_entry **tab,
228
                  struct cyg_devtab_entry *sub_tab,
229
                  const char *name)
230
{
231
    serial_channel *chan = (serial_channel *)(*tab)->priv;
232
    (chan->callbacks->serial_init)(chan);  // Really only required for interrupt driven devices
233
    return ENOERR;
234
}
235
 
236
// Send a character to the device output buffer.
237
// Return 'true' if character is sent to device
238
static bool
239
mipsidt_serial_putc(serial_channel *chan, unsigned char c)
240
{
241
    mipsidt_serial_info *mipsidt_chan = (mipsidt_serial_info *)chan->dev_priv;
242
    cyg_addrword_t port = mipsidt_chan->base;
243
    cyg_uint8 _lsr;
244
 
245
    HAL_READ_UINT8(port+SER_16550_LSR, _lsr);
246
    if (_lsr & SIO_LSR_THRE) {
247
        // Transmit buffer is empty
248
        HAL_WRITE_UINT8(port+SER_16550_THR, c);
249
        return true;
250
    } else {
251
        // No space
252
        return false;
253
    }
254
}
255
 
256
// Fetch a character from the device input buffer, waiting if necessary
257
static unsigned char
258
mipsidt_serial_getc(serial_channel *chan)
259
{
260
    unsigned char c;
261
    mipsidt_serial_info *mipsidt_chan = (mipsidt_serial_info *)chan->dev_priv;
262
    cyg_addrword_t port = mipsidt_chan->base;
263
    cyg_uint8 _lsr;
264
 
265
    do {
266
        HAL_READ_UINT8(port+SER_16550_LSR, _lsr);
267
    } while ((_lsr & SIO_LSR_DR) == 0);
268
    HAL_READ_UINT8(port+SER_16550_RBR, c);
269
    return c;
270
}
271
 
272
// Set up the device characteristics; baud rate, etc.
273
static Cyg_ErrNo
274
mipsidt_serial_set_config(serial_channel *chan, cyg_uint32 key,
275
                        const void *xbuf, cyg_uint32 *len)
276
{
277
    switch (key) {
278
    case CYG_IO_SET_CONFIG_SERIAL_INFO:
279
      {
280
        cyg_serial_info_t *config = (cyg_serial_info_t *)xbuf;
281
        if ( *len < sizeof(cyg_serial_info_t) ) {
282
            return -EINVAL;
283
        }
284
        *len = sizeof(cyg_serial_info_t);
285
        if ( true != mipsidt_serial_config_port(chan, config, false) )
286
            return -EINVAL;
287
      }
288
      break;
289
    default:
290
        return -EINVAL;
291
    }
292
    return ENOERR;
293
}
294
 
295
// Enable the transmitter on the device
296
static void
297
mipsidt_serial_start_xmit(serial_channel *chan)
298
{
299
    mipsidt_serial_info *mipsidt_chan = (mipsidt_serial_info *)chan->dev_priv;
300
    cyg_addrword_t port = mipsidt_chan->base;
301
    cyg_uint8 _ier;
302
 
303
    HAL_READ_UINT8(port+SER_16550_IER, _ier);
304
    _ier |= IER_XMT;                    // Enable xmit interrupt
305
    HAL_WRITE_UINT8(port+SER_16550_IER, _ier);
306
 
307
    // We should not need to call this here.  THRE Interrupts are enabled, and the DSR
308
    // below calls this function.  However, sometimes we get called with Master Interrupts
309
    // disabled, and thus the DSR never runs.  This is unfortunate because it means we
310
    // will be doing multiple processing steps for the same thing.
311
    (chan->callbacks->xmt_char)(chan);
312
}
313
 
314
// Disable the transmitter on the device
315
static void
316
mipsidt_serial_stop_xmit(serial_channel *chan)
317
{
318
    mipsidt_serial_info *mipsidt_chan = (mipsidt_serial_info *)chan->dev_priv;
319
    cyg_addrword_t port = mipsidt_chan->base;
320
    cyg_uint8 _ier;
321
 
322
    HAL_READ_UINT8(port+SER_16550_IER, _ier);
323
    _ier &= ~IER_XMT;                   // Disable xmit interrupt
324
    HAL_WRITE_UINT8(port+SER_16550_IER, _ier);
325
}
326
 
327
// Serial I/O - low level interrupt handler (ISR)
328
static cyg_uint32
329
mipsidt_serial_ISR(cyg_vector_t vector, cyg_addrword_t data)
330
{
331
    serial_channel *chan = (serial_channel *)data;
332
    mipsidt_serial_info *mipsidt_chan = (mipsidt_serial_info *)chan->dev_priv;
333
 
334
    cyg_drv_interrupt_mask(mipsidt_chan->int_num);
335
    cyg_drv_interrupt_acknowledge(mipsidt_chan->int_num);
336
    return CYG_ISR_CALL_DSR;  // Cause DSR to be run
337
}
338
 
339
// Serial I/O - high level interrupt handler (DSR)
340
static void
341
mipsidt_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
342
{
343
    serial_channel *chan = (serial_channel *)data;
344
    mipsidt_serial_info *mipsidt_chan = (mipsidt_serial_info *)chan->dev_priv;
345
    cyg_addrword_t port = mipsidt_chan->base;
346
    cyg_uint8 _iir;
347
 
348
    HAL_READ_UINT8(port+SER_16550_IIR, _iir);
349
    _iir &= SIO_IIR_ID_MASK;
350
    if ( ISR_Tx_Empty == _iir ) {
351
        (chan->callbacks->xmt_char)(chan);
352
    } else if (( ISR_Rx_Avail == _iir ) || ( ISR_Rx_Char_Timeout == _iir )) {
353
        cyg_uint8 _c;
354
        HAL_READ_UINT8(port+SER_16550_RBR, _c);
355
        (chan->callbacks->rcv_char)(chan, _c);
356
    }
357
 
358
    cyg_drv_interrupt_unmask(mipsidt_chan->int_num);
359
}
360
 
361
#endif
362
 
363
//-------------------------------------------------------------------------
364
// EOF mipsidt_serial.c

powered by: WebSVN 2.1.0

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