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

Subversion Repositories openrisc

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

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

powered by: WebSVN 2.1.0

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