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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [hal/] [mips/] [idt79s334a/] [current/] [src/] [ser16c550c.c] - Blame information for rev 786

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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