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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [hal/] [sh/] [se7751/] [v2_0/] [src/] [ser16c550c.c] - Blame information for rev 27

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

Line No. Rev Author Line
1 27 unneback
//=============================================================================
2
//
3
//      ser16c550c.c
4
//
5
//      Simple driver for the 16c550c serial controllers on the HS7729PCI board
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):   dmoseley
44
// Contributors:dmoseley, jskov
45
// Date:        2001-03-20
46
// Description: Simple driver for the 16c550c serial controller
47
//
48
//####DESCRIPTIONEND####
49
//
50
//=============================================================================
51
 
52
#include <pkgconf/hal.h>
53
#include <pkgconf/system.h>
54
#include CYGBLD_HAL_PLATFORM_H
55
 
56
#include <cyg/hal/hal_arch.h>           // SAVE/RESTORE GP macros
57
#include <cyg/hal/hal_io.h>             // IO macros
58
#include <cyg/hal/hal_if.h>             // interface API
59
#include <cyg/hal/hal_intr.h>           // HAL_ENABLE/MASK/UNMASK_INTERRUPTS
60
#include <cyg/hal/hal_misc.h>           // Helper functions
61
#include <cyg/hal/drv_api.h>            // CYG_ISR_HANDLED
62
 
63
//-----------------------------------------------------------------------------
64
// Define the serial registers. The Malta board is equipped with a 16550C
65
// serial chip.
66
#define SE77X9_SER_CLOCK           1846200
67
#define SE77X9_SER_16550_BASE_A    0x3f8
68
#define SER_16550_RBR 0x00   // receiver buffer register, read, dlab = 0
69
#define SER_16550_THR 0x00   // transmitter holding register, write, dlab = 0
70
#define SER_16550_DLL 0x00   // divisor latch (LS), read/write, dlab = 1
71
#define SER_16550_IER 0x01   // interrupt enable register, read/write, dlab = 0
72
#define SER_16550_DLM 0x01   // divisor latch (MS), read/write, dlab = 1
73
#define SER_16550_IIR 0x02   // interrupt identification reg, read, dlab = 0
74
#define SER_16550_FCR 0x02   // fifo control register, write, dlab = 0
75
#define SER_16550_AFR 0x02   // alternate function reg, read/write, dlab = 1
76
#define SER_16550_LCR 0x03   // line control register, read/write
77
#define SER_16550_MCR 0x04   // modem control register, read/write
78
#define SER_16550_LSR 0x05   // line status register, read
79
#define SER_16550_MSR 0x06   // modem status register, read
80
#define SER_16550_SCR 0x07   // scratch pad register
81
 
82
// The interrupt enable register bits.
83
#define SIO_IER_ERDAI   0x01            // enable received data available irq
84
#define SIO_IER_ETHREI  0x02            // enable THR empty interrupt
85
#define SIO_IER_ELSI    0x04            // enable receiver line status irq
86
#define SIO_IER_EMSI    0x08            // enable modem status interrupt
87
 
88
// The interrupt identification register bits.
89
#define SIO_IIR_IP      0x01            // 0 if interrupt pending
90
#define SIO_IIR_ID_MASK 0x0e            // mask for interrupt ID bits
91
 
92
// The line status register bits.
93
#define SIO_LSR_DR      0x01            // data ready
94
#define SIO_LSR_OE      0x02            // overrun error
95
#define SIO_LSR_PE      0x04            // parity error
96
#define SIO_LSR_FE      0x08            // framing error
97
#define SIO_LSR_BI      0x10            // break interrupt
98
#define SIO_LSR_THRE    0x20            // transmitter holding register empty
99
#define SIO_LSR_TEMT    0x40            // transmitter register empty
100
#define SIO_LSR_ERR     0x80            // any error condition
101
 
102
// The modem status register bits.
103
#define SIO_MSR_DCTS  0x01              // delta clear to send
104
#define SIO_MSR_DDSR  0x02              // delta data set ready
105
#define SIO_MSR_TERI  0x04              // trailing edge ring indicator
106
#define SIO_MSR_DDCD  0x08              // delta data carrier detect
107
#define SIO_MSR_CTS   0x10              // clear to send
108
#define SIO_MSR_DSR   0x20              // data set ready
109
#define SIO_MSR_RI    0x40              // ring indicator
110
#define SIO_MSR_DCD   0x80              // data carrier detect
111
 
112
// The line control register bits.
113
#define SIO_LCR_WLS0   0x01             // word length select bit 0
114
#define SIO_LCR_WLS1   0x02             // word length select bit 1
115
#define SIO_LCR_STB    0x04             // number of stop bits
116
#define SIO_LCR_PEN    0x08             // parity enable
117
#define SIO_LCR_EPS    0x10             // even parity select
118
#define SIO_LCR_SP     0x20             // stick parity
119
#define SIO_LCR_SB     0x40             // set break
120
#define SIO_LCR_DLAB   0x80             // divisor latch access bit
121
 
122
// The FIFO control register
123
#define SIO_FCR_FCR0   0x01             // enable xmit and rcvr fifos
124
#define SIO_FCR_FCR1   0x02             // clear RCVR FIFO
125
#define SIO_FCR_FCR2   0x04             // clear XMIT FIFO
126
 
127
/////////////////////////////////////////
128
// Interrupt Enable Register
129
#define IER_RCV 0x01
130
#define IER_XMT 0x02
131
#define IER_LS  0x04
132
#define IER_MS  0x08
133
 
134
// Line Control Register
135
#define LCR_WL5 0x00    // Word length
136
#define LCR_WL6 0x01
137
#define LCR_WL7 0x02
138
#define LCR_WL8 0x03
139
#define LCR_SB1 0x00    // Number of stop bits
140
#define LCR_SB1_5 0x04  // 1.5 -> only valid with 5 bit words
141
#define LCR_SB2 0x04
142
#define LCR_PN  0x00    // Parity mode - none
143
#define LCR_PE  0x0C    // Parity mode - even
144
#define LCR_PO  0x08    // Parity mode - odd
145
#define LCR_PM  0x28    // Forced "mark" parity
146
#define LCR_PS  0x38    // Forced "space" parity
147
#define LCR_DL  0x80    // Enable baud rate latch
148
 
149
// Line Status Register
150
#define LSR_RSR 0x01
151
#define LSR_THE 0x20
152
 
153
// Modem Control Register
154
#define MCR_DTR 0x01
155
#define MCR_RTS 0x02
156
#define MCR_INT 0x08   // Enable interrupts
157
#define MCR_AFE 0x20
158
 
159
// Interrupt status register
160
#define ISR_None             0x01
161
#define ISR_Rx_Line_Status   0x06
162
#define ISR_Rx_Avail         0x04
163
#define ISR_Rx_Char_Timeout  0x0C
164
#define ISR_Tx_Empty         0x02
165
#define IRS_Modem_Status     0x00
166
 
167
// FIFO control register
168
#define FCR_ENABLE     0x01
169
#define FCR_CLEAR_RCVR 0x02
170
#define FCR_CLEAR_XMIT 0x04
171
 
172
#define CYG_DEV_SERIAL_BAUD_DIVISOR (SE77X9_SER_CLOCK/16/CYGNUM_HAL_VIRTUAL_VECTOR_CHANNELS_DEFAULT_BAUD)
173
 
174
//-----------------------------------------------------------------------------
175
 
176
#define UART_READ_UINT8(_a_, _d_)               \
177
    CYG_MACRO_START                             \
178
    cyg_uint16 t;                               \
179
    HAL_READ_UINT16((_a_), t);                  \
180
    (_d_) = (t >> 8) & 0xff;                    \
181
    CYG_MACRO_END
182
 
183
#define UART_WRITE_UINT8(_a_, _d_)              \
184
    CYG_MACRO_START                             \
185
    HAL_WRITE_UINT16((_a_), (_d_)<<8);          \
186
    CYG_MACRO_END
187
 
188
 
189
//-----------------------------------------------------------------------------
190
typedef struct {
191
    cyg_uint8* base;
192
    cyg_int32 msec_timeout;
193
    int isr_vector;
194
} channel_data_t;
195
 
196
static channel_data_t channels[1] = {
197
    { (cyg_uint8*)SE77X9_SER_16550_BASE_A, 1000, CYGNUM_HAL_INTERRUPT_PC_SIRQ4 },
198
};
199
 
200
//-----------------------------------------------------------------------------
201
// Set the baud rate
202
 
203
static void
204
cyg_hal_plf_serial_set_baud(cyg_uint8* port, cyg_uint16 baud_divisor)
205
{
206
    cyg_uint8 _lcr;
207
 
208
    UART_READ_UINT8(port+SER_16550_LCR, _lcr);
209
    _lcr |= LCR_DL;
210
    UART_WRITE_UINT8(port+SER_16550_LCR, _lcr);
211
 
212
    UART_WRITE_UINT8(port+SER_16550_DLM, baud_divisor >> 8);
213
    UART_WRITE_UINT8(port+SER_16550_DLL, baud_divisor & 0xff);
214
 
215
    _lcr &= ~LCR_DL;
216
    UART_WRITE_UINT8(port+SER_16550_LCR, _lcr);
217
}
218
 
219
//-----------------------------------------------------------------------------
220
// The minimal init, get and put functions. All by polling.
221
 
222
void
223
cyg_hal_plf_serial_init_channel(void* __ch_data)
224
{
225
    cyg_uint8* port;
226
    cyg_uint8 _lcr;
227
 
228
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
229
    // Go ahead and assume it is channels[0].
230
    if (__ch_data == 0)
231
      __ch_data = (void*)&channels[0];
232
 
233
    port = ((channel_data_t*)__ch_data)->base;
234
 
235
    // Disable port interrupts while changing hardware
236
    UART_WRITE_UINT8(port+SER_16550_IER, 0);
237
 
238
    // Set databits, stopbits and parity.
239
    _lcr = LCR_WL8 | LCR_SB1 | LCR_PN;
240
    UART_WRITE_UINT8(port+SER_16550_LCR, _lcr);
241
 
242
    // Set baud rate.
243
    cyg_hal_plf_serial_set_baud(port, CYG_DEV_SERIAL_BAUD_DIVISOR);
244
 
245
    // Enable and clear FIFO
246
    UART_WRITE_UINT8(port+SER_16550_FCR, (FCR_ENABLE | FCR_CLEAR_RCVR | FCR_CLEAR_XMIT));
247
 
248
    // enable RTS to keep host side happy. Also allow interrupts
249
    UART_WRITE_UINT8( port+SER_16550_MCR, MCR_DTR | MCR_RTS | MCR_INT);
250
 
251
    // Don't allow interrupts.
252
    UART_WRITE_UINT8(port+SER_16550_IER, 0);
253
}
254
 
255
void
256
cyg_hal_plf_serial_putc(void* __ch_data, cyg_uint8 __ch)
257
{
258
    cyg_uint8* port;
259
    cyg_uint8 _lsr;
260
 
261
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
262
    // Go ahead and assume it is channels[0].
263
    if (__ch_data == 0)
264
      __ch_data = (void*)&channels[0];
265
 
266
    port = ((channel_data_t*)__ch_data)->base;
267
 
268
    CYGARC_HAL_SAVE_GP();
269
 
270
    do {
271
        UART_READ_UINT8(port+SER_16550_LSR, _lsr);
272
    } while ((_lsr & SIO_LSR_THRE) == 0);
273
 
274
    // Now, the transmit buffer is empty
275
    UART_WRITE_UINT8(port+SER_16550_THR, __ch);
276
 
277
    // Hang around until the character has been safely sent.
278
    do {
279
        UART_READ_UINT8(port+SER_16550_LSR, _lsr);
280
    } while ((_lsr & SIO_LSR_THRE) == 0);
281
 
282
    CYGARC_HAL_RESTORE_GP();
283
}
284
 
285
static cyg_bool
286
cyg_hal_plf_serial_getc_nonblock(void* __ch_data, cyg_uint8* ch)
287
{
288
    cyg_uint8* port;
289
    cyg_uint8 _lsr;
290
 
291
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
292
    // Go ahead and assume it is channels[0].
293
    if (__ch_data == 0)
294
      __ch_data = (void*)&channels[0];
295
 
296
    port = ((channel_data_t*)__ch_data)->base;
297
 
298
    UART_READ_UINT8(port+SER_16550_LSR, _lsr);
299
    if ((_lsr & SIO_LSR_DR) == 0)
300
        return false;
301
 
302
    UART_READ_UINT8(port+SER_16550_RBR, *ch);
303
 
304
    return true;
305
}
306
 
307
cyg_uint8
308
cyg_hal_plf_serial_getc(void* __ch_data)
309
{
310
    cyg_uint8 ch;
311
    CYGARC_HAL_SAVE_GP();
312
 
313
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
314
    // Go ahead and assume it is channels[0].
315
    if (__ch_data == 0)
316
      __ch_data = (void*)&channels[0];
317
 
318
    while(!cyg_hal_plf_serial_getc_nonblock(__ch_data, &ch));
319
 
320
    CYGARC_HAL_RESTORE_GP();
321
    return ch;
322
}
323
 
324
static void
325
cyg_hal_plf_serial_write(void* __ch_data, const cyg_uint8* __buf,
326
                         cyg_uint32 __len)
327
{
328
    CYGARC_HAL_SAVE_GP();
329
 
330
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
331
    // Go ahead and assume it is channels[0].
332
    if (__ch_data == 0)
333
      __ch_data = (void*)&channels[0];
334
 
335
    while(__len-- > 0)
336
        cyg_hal_plf_serial_putc(__ch_data, *__buf++);
337
 
338
    CYGARC_HAL_RESTORE_GP();
339
}
340
 
341
static void
342
cyg_hal_plf_serial_read(void* __ch_data, cyg_uint8* __buf, cyg_uint32 __len)
343
{
344
    CYGARC_HAL_SAVE_GP();
345
 
346
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
347
    // Go ahead and assume it is channels[0].
348
    if (__ch_data == 0)
349
      __ch_data = (void*)&channels[0];
350
 
351
    while(__len-- > 0)
352
        *__buf++ = cyg_hal_plf_serial_getc(__ch_data);
353
 
354
    CYGARC_HAL_RESTORE_GP();
355
}
356
 
357
 
358
cyg_bool
359
cyg_hal_plf_serial_getc_timeout(void* __ch_data, cyg_uint8* ch)
360
{
361
    int delay_count;
362
    channel_data_t* chan;
363
    cyg_bool res;
364
    CYGARC_HAL_SAVE_GP();
365
 
366
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
367
    // Go ahead and assume it is channels[0].
368
    if (__ch_data == 0)
369
      __ch_data = (void*)&channels[0];
370
 
371
    chan = (channel_data_t*)__ch_data;
372
 
373
    delay_count = chan->msec_timeout * 10; // delay in .1 ms steps
374
 
375
    for(;;) {
376
        res = cyg_hal_plf_serial_getc_nonblock(__ch_data, ch);
377
        if (res || 0 == delay_count--)
378
            break;
379
        CYGACC_CALL_IF_DELAY_US(100);
380
    }
381
 
382
    CYGARC_HAL_RESTORE_GP();
383
    return res;
384
}
385
 
386
static int
387
cyg_hal_plf_serial_control(void *__ch_data, __comm_control_cmd_t __func, ...)
388
{
389
    static int irq_state = 0;
390
    channel_data_t* chan;
391
    cyg_uint8 ier;
392
    int ret = 0;
393
    CYGARC_HAL_SAVE_GP();
394
 
395
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
396
    // Go ahead and assume it is channels[0].
397
    if (__ch_data == 0)
398
      __ch_data = (void*)&channels[0];
399
 
400
    chan = (channel_data_t*)__ch_data;
401
 
402
    switch (__func) {
403
    case __COMMCTL_IRQ_ENABLE:
404
        irq_state = 1;
405
 
406
        UART_READ_UINT8(chan->base + SER_16550_IER, ier);
407
        ier |= SIO_IER_ERDAI;
408
        UART_WRITE_UINT8(chan->base + SER_16550_IER, ier);
409
 
410
        HAL_INTERRUPT_SET_LEVEL(chan->isr_vector, 1);
411
        HAL_INTERRUPT_UNMASK(chan->isr_vector);
412
        break;
413
    case __COMMCTL_IRQ_DISABLE:
414
        ret = irq_state;
415
        irq_state = 0;
416
 
417
        UART_READ_UINT8(chan->base + SER_16550_IER, ier);
418
        ier &= ~SIO_IER_ERDAI;
419
        UART_WRITE_UINT8(chan->base + SER_16550_IER, ier);
420
 
421
        HAL_INTERRUPT_MASK(chan->isr_vector);
422
        break;
423
    case __COMMCTL_DBG_ISR_VECTOR:
424
        ret = chan->isr_vector;
425
        break;
426
    case __COMMCTL_SET_TIMEOUT:
427
    {
428
        va_list ap;
429
 
430
        va_start(ap, __func);
431
 
432
        ret = chan->msec_timeout;
433
        chan->msec_timeout = va_arg(ap, cyg_uint32);
434
 
435
        va_end(ap);
436
    }
437
    break;
438
    case __COMMCTL_SETBAUD:
439
    {
440
        cyg_uint32 baud_rate;
441
        cyg_uint16 baud_divisor;
442
        cyg_uint8* port = chan->base;
443
        va_list ap;
444
 
445
        va_start(ap, __func);
446
        baud_rate = va_arg(ap, cyg_uint32);
447
        va_end(ap);
448
 
449
        baud_divisor = (SE77X9_SER_CLOCK / 16 / baud_rate);
450
 
451
        // Disable port interrupts while changing hardware
452
        UART_READ_UINT8(port+SER_16550_IER, ier);
453
        UART_WRITE_UINT8(port+SER_16550_IER, 0);
454
 
455
        // Set baud rate.
456
        cyg_hal_plf_serial_set_baud(port, baud_divisor);
457
 
458
        // Reenable interrupts if necessary
459
        UART_WRITE_UINT8(port+SER_16550_IER, ier);
460
    }
461
    break;
462
 
463
    case __COMMCTL_GETBAUD:
464
        break;
465
    default:
466
        break;
467
    }
468
    CYGARC_HAL_RESTORE_GP();
469
    return ret;
470
}
471
 
472
static int
473
cyg_hal_plf_serial_isr(void *__ch_data, int* __ctrlc,
474
                       CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
475
{
476
    int res = 0;
477
    cyg_uint8 _iir, c;
478
    channel_data_t* chan;
479
    CYGARC_HAL_SAVE_GP();
480
 
481
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
482
    // Go ahead and assume it is channels[0].
483
    if (__ch_data == 0)
484
      __ch_data = (void*)&channels[0];
485
 
486
    chan = (channel_data_t*)__ch_data;
487
 
488
    HAL_INTERRUPT_ACKNOWLEDGE(chan->isr_vector);
489
 
490
    UART_READ_UINT8(chan->base + SER_16550_IIR, _iir);
491
    _iir &= SIO_IIR_ID_MASK;
492
 
493
    *__ctrlc = 0;
494
    if ((_iir == ISR_Rx_Avail) || (_iir == ISR_Rx_Char_Timeout)) {
495
 
496
        UART_READ_UINT8(chan->base + SER_16550_RBR, c);
497
 
498
        if( cyg_hal_is_break( &c , 1 ) )
499
            *__ctrlc = 1;
500
 
501
        res = CYG_ISR_HANDLED;
502
    }
503
 
504
    CYGARC_HAL_RESTORE_GP();
505
    return res;
506
}
507
 
508
void
509
cyg_hal_plf_serial_init(void)
510
{
511
    hal_virtual_comm_table_t* comm;
512
    int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
513
 
514
    // Disable interrupts.
515
    HAL_INTERRUPT_MASK(channels[0].isr_vector);
516
 
517
    // Init channels
518
    cyg_hal_plf_serial_init_channel((void*)&channels[0]);
519
 
520
    // Setup procs in the vector table
521
 
522
    // Set channel 1
523
    CYGACC_CALL_IF_SET_CONSOLE_COMM(1);
524
    comm = CYGACC_CALL_IF_CONSOLE_PROCS();
525
    CYGACC_COMM_IF_CH_DATA_SET(*comm, &channels[0]);
526
    CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_serial_write);
527
    CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_serial_read);
528
    CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_serial_putc);
529
    CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_serial_getc);
530
    CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_serial_control);
531
    CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_serial_isr);
532
    CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_serial_getc_timeout);
533
 
534
    // Restore original console
535
    CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
536
}
537
 
538
//-----------------------------------------------------------------------------
539
// end of ser16c550c.c

powered by: WebSVN 2.1.0

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