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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [hal/] [mips/] [idt79s334a/] [v2_0/] [src/] [ser16c550c.c] - Blame information for rev 638

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

powered by: WebSVN 2.1.0

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