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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [hal/] [mn10300/] [asb2305/] [current/] [src/] [ser_asb.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//=============================================================================
2
//
3
//      ser_asb.c
4
//
5
//      Simple driver for the serial controllers on the AM33 ASB305 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):   dhowells
43
// Contributors:dmoseley, nickg, gthomas
44
// Date:        2001-05-18
45
// Description: Simple driver for the ASB2305 debug serial port
46
//
47
//####DESCRIPTIONEND####
48
//
49
//=============================================================================
50
 
51
#include <pkgconf/hal.h>
52
 
53
#include <cyg/hal/hal_arch.h>           // SAVE/RESTORE GP macros
54
#include <cyg/hal/hal_io.h>             // IO macros
55
#include <cyg/hal/hal_if.h>             // interface API
56
#include <cyg/hal/hal_intr.h>           // HAL_ENABLE/MASK/UNMASK_INTERRUPTS
57
#include <cyg/hal/hal_misc.h>           // Helper functions
58
#include <cyg/hal/drv_api.h>            // CYG_ISR_HANDLED
59
 
60
#if defined(CYGNUM_HAL_AM33_PLF_SERIAL_CHANNELS) && CYGNUM_HAL_AM33_PLF_SERIAL_CHANNELS > 0
61
 
62
/*---------------------------------------------------------------------------*/
63
/* From serial_16550.h */
64
#if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==9600
65
#define CYG_DEVICE_SERIAL_BAUD_MSB        0x00
66
#define CYG_DEVICE_SERIAL_BAUD_LSB        0x78
67
#endif
68
#if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==19200
69
#define CYG_DEVICE_SERIAL_BAUD_MSB        0x00
70
#define CYG_DEVICE_SERIAL_BAUD_LSB        0x3C
71
#endif
72
#if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==38400
73
#define CYG_DEVICE_SERIAL_BAUD_MSB        0x00
74
#define CYG_DEVICE_SERIAL_BAUD_LSB        0x1E
75
#endif
76
#if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==57600
77
#define CYG_DEVICE_SERIAL_BAUD_MSB        0x00
78
#define CYG_DEVICE_SERIAL_BAUD_LSB        0x14
79
#endif
80
#if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==115200
81
#define CYG_DEVICE_SERIAL_BAUD_MSB        0x00
82
#define CYG_DEVICE_SERIAL_BAUD_LSB        0x0A
83
#endif
84
 
85
#ifndef CYG_DEVICE_SERIAL_BAUD_MSB
86
#error Missing/incorrect serial baud rate defined - CDL error?
87
#endif
88
 
89
/*---------------------------------------------------------------------------*/
90
// Define the serial registers.
91
#define CYG_DEV_RBR 0x00   // receiver buffer register, read, dlab = 0
92
#define CYG_DEV_THR 0x00   // transmitter holding register, write, dlab = 0
93
#define CYG_DEV_DLL 0x00   // divisor latch (LS), read/write, dlab = 1
94
#define CYG_DEV_IER 0x04   // interrupt enable register, read/write, dlab = 0
95
#define CYG_DEV_DLM 0x04   // divisor latch (MS), read/write, dlab = 1
96
#define CYG_DEV_IIR 0x08   // interrupt identification register, read, dlab = 0
97
#define CYG_DEV_FCR 0x08   // fifo control register, write, dlab = 0
98
#define CYG_DEV_LCR 0x0C   // line control register, read/write
99
#define CYG_DEV_MCR 0x10   // modem control register, read/write
100
#define CYG_DEV_LSR 0x14   // line status register, read
101
#define CYG_DEV_MSR 0x18   // modem status register, read
102
 
103
// Interrupt Enable Register
104
#define SIO_IER_RCV 0x01
105
#define SIO_IER_XMT 0x02
106
#define SIO_IER_LS  0x04
107
#define SIO_IER_MS  0x08
108
 
109
// The line status register bits.
110
#define SIO_LSR_DR      0x01            // data ready
111
#define SIO_LSR_OE      0x02            // overrun error
112
#define SIO_LSR_PE      0x04            // parity error
113
#define SIO_LSR_FE      0x08            // framing error
114
#define SIO_LSR_BI      0x10            // break interrupt
115
#define SIO_LSR_THRE    0x20            // transmitter holding register empty
116
#define SIO_LSR_TEMT    0x40            // transmitter register empty
117
#define SIO_LSR_ERR     0x80            // any error condition
118
 
119
// The modem status register bits.
120
#define SIO_MSR_DCTS  0x01              // delta clear to send
121
#define SIO_MSR_DDSR  0x02              // delta data set ready
122
#define SIO_MSR_TERI  0x04              // trailing edge ring indicator
123
#define SIO_MSR_DDCD  0x08              // delta data carrier detect
124
#define SIO_MSR_CTS   0x10              // clear to send
125
#define SIO_MSR_DSR   0x20              // data set ready
126
#define SIO_MSR_RI    0x40              // ring indicator
127
#define SIO_MSR_DCD   0x80              // data carrier detect
128
 
129
// The line control register bits.
130
#define SIO_LCR_WLS0   0x01             // word length select bit 0
131
#define SIO_LCR_WLS1   0x02             // word length select bit 1
132
#define SIO_LCR_STB    0x04             // number of stop bits
133
#define SIO_LCR_PEN    0x08             // parity enable
134
#define SIO_LCR_EPS    0x10             // even parity select
135
#define SIO_LCR_SP     0x20             // stick parity
136
#define SIO_LCR_SB     0x40             // set break
137
#define SIO_LCR_DLAB   0x80             // divisor latch access bit
138
 
139
// Modem Control Register
140
#define SIO_MCR_DTR 0x01
141
#define SIO_MCR_RTS 0x02
142
#define SIO_MCR_INT 0x08   // Enable interrupts
143
 
144
#define LSR_WAIT_FOR(STATE) do { cyg_uint8 lsr; do { HAL_READ_UINT8(base+CYG_DEV_LSR, lsr); } while (!(lsr&SIO_LSR_##STATE)); } while(0)
145
#define LSR_QUERY(STATE) ({ cyg_uint8 lsr; HAL_READ_UINT8(base+CYG_DEV_LSR, lsr); (lsr&SIO_LSR_##STATE); })
146
 
147
#ifdef CYGSEM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_RTSCTS
148
#define FLOWCTL_QUERY(LINE) ({ cyg_uint8 msr; HAL_READ_UINT8(base+CYG_DEV_MSR, msr); (msr&SIO_MSR_##LINE); })
149
#define FLOWCTL_WAIT_FOR(LINE) do { cyg_uint8 msr; do { HAL_READ_UINT8(base+CYG_DEV_MSR, msr); } while (!(msr&SIO_MSR_##LINE)); } while(0)
150
#define FLOWCTL_CLEAR(LINE) do { cyg_uint8 mcr; HAL_READ_UINT8(base+CYG_DEV_MCR,mcr); mcr &= ~SIO_MCR_##LINE; HAL_WRITE_UINT8(base+CYG_DEV_MCR, mcr); } while (0);
151
#define FLOWCTL_SET(LINE) do { cyg_uint8 mcr; HAL_READ_UINT8(base+CYG_DEV_MCR,mcr); mcr |= SIO_MCR_##LINE; HAL_WRITE_UINT8(base+CYG_DEV_MCR, mcr); } while (0);
152
 
153
#else
154
#define FLOWCTL_QUERY(LINE) 1
155
#define FLOWCTL_WAIT_FOR(LINE) do { ; } while(0)
156
#define FLOWCTL_CLEAR(LINE) do { ; } while(0)
157
#define FLOWCTL_SET(LINE) do { ; } while(0)
158
 
159
#endif
160
 
161
//-----------------------------------------------------------------------------
162
typedef struct {
163
    cyg_uint8* base;
164
    cyg_int32 msec_timeout;
165
    int isr_vector;
166
} channel_data_t;
167
 
168
static channel_data_t asb2305_serial_channels[] = {
169
    { (cyg_uint8*)0xA6FB0000, 1000, CYGNUM_HAL_INTERRUPT_SERIAL_0_RX }
170
};
171
 
172
//-----------------------------------------------------------------------------
173
 
174
static void
175
cyg_hal_plf_serial_init_channel(const void* __ch_data)
176
{
177
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
178
    cyg_uint8 lcr;
179
 
180
    // 8-1-no parity.
181
    HAL_WRITE_UINT8(base+CYG_DEV_LCR, SIO_LCR_WLS0 | SIO_LCR_WLS1);
182
 
183
    HAL_READ_UINT8(base+CYG_DEV_LCR, lcr);
184
    lcr |= SIO_LCR_DLAB;
185
    HAL_WRITE_UINT8(base+CYG_DEV_LCR, lcr);
186
    HAL_WRITE_UINT8(base+CYG_DEV_DLL, CYG_DEVICE_SERIAL_BAUD_LSB);
187
    HAL_WRITE_UINT8(base+CYG_DEV_DLM, CYG_DEVICE_SERIAL_BAUD_MSB);
188
    lcr &= ~SIO_LCR_DLAB;
189
    HAL_WRITE_UINT8(base+CYG_DEV_LCR, lcr);
190
    HAL_WRITE_UINT8(base+CYG_DEV_FCR, 0x07);  // Enable & clear FIFO
191
 
192
    FLOWCTL_CLEAR(DTR);
193
    FLOWCTL_CLEAR(RTS);
194
}
195
 
196
static void
197
cyg_hal_plf_serial_putc_aux(cyg_uint8* base, char c)
198
{
199
    LSR_WAIT_FOR(THRE);
200
 
201
    FLOWCTL_WAIT_FOR(CTS);
202
 
203
    HAL_WRITE_UINT8(base+CYG_DEV_THR, c);
204
}
205
 
206
void
207
cyg_hal_plf_serial_putc(void *__ch_data, char c)
208
{
209
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
210
    CYGARC_HAL_SAVE_GP();
211
 
212
    FLOWCTL_SET(DTR);
213
 
214
    cyg_hal_plf_serial_putc_aux(base,c);
215
 
216
    FLOWCTL_CLEAR(DTR);
217
 
218
    CYGARC_HAL_RESTORE_GP();
219
}
220
 
221
static cyg_bool
222
cyg_hal_plf_serial_getc_nonblock(void* __ch_data, cyg_uint8* ch)
223
{
224
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
225
 
226
    if (!LSR_QUERY(DR))
227
        return false;
228
 
229
    HAL_READ_UINT8(base+CYG_DEV_RBR, *ch);
230
 
231
    return true;
232
}
233
 
234
cyg_uint8
235
cyg_hal_plf_serial_getc(void* __ch_data)
236
{
237
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
238
    cyg_uint8 ch;
239
    CYGARC_HAL_SAVE_GP();
240
 
241
    /* see if there's some cached data in the FIFO */
242
    if (!cyg_hal_plf_serial_getc_nonblock(__ch_data, &ch)) {
243
        /* there isn't - open the flood gates */
244
        FLOWCTL_WAIT_FOR(DSR);
245
        FLOWCTL_SET(RTS);
246
 
247
        while (!cyg_hal_plf_serial_getc_nonblock(__ch_data, &ch));
248
 
249
        FLOWCTL_CLEAR(RTS);
250
    }
251
 
252
    CYGARC_HAL_RESTORE_GP();
253
    return ch;
254
}
255
 
256
static void
257
cyg_hal_plf_serial_write(void* __ch_data, const cyg_uint8* __buf,
258
                         cyg_uint32 __len)
259
{
260
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
261
    CYGARC_HAL_SAVE_GP();
262
 
263
    FLOWCTL_SET(DTR);
264
 
265
    while(__len-- > 0)
266
        cyg_hal_plf_serial_putc_aux(__ch_data, *__buf++);
267
 
268
    FLOWCTL_CLEAR(DTR);
269
 
270
    CYGARC_HAL_RESTORE_GP();
271
}
272
 
273
static void
274
cyg_hal_plf_serial_read(void* __ch_data, cyg_uint8* __buf, cyg_uint32 __len)
275
{
276
    CYGARC_HAL_SAVE_GP();
277
 
278
    while(__len-- > 0)
279
        *__buf++ = cyg_hal_plf_serial_getc(__ch_data);
280
 
281
    CYGARC_HAL_RESTORE_GP();
282
}
283
 
284
#define TM0MD 0xD4003000
285
#define TM0BR 0xD4003010
286
#define TM0BC 0xD4003020
287
 
288
cyg_bool
289
cyg_hal_plf_serial_getc_timeout(void* __ch_data, cyg_uint8* ch)
290
{
291
#if 1
292
    int delay_count;
293
    channel_data_t* chan = (channel_data_t*)__ch_data;
294
    cyg_uint8* base = chan->base;
295
    cyg_uint8 last, val;
296
    cyg_bool res;
297
    CYGARC_HAL_SAVE_GP();
298
 
299
    /* see if there's any cached data in the FIFO */
300
    res = cyg_hal_plf_serial_getc_nonblock(__ch_data,ch);
301
    if (!res) {
302
        /* there isn't - open the flood gates */
303
        delay_count = chan->msec_timeout * 125; // want delay in 8uS steps
304
 
305
        HAL_WRITE_UINT8(TM0BR,200); // IOCLK is 25MHz, we want 125KHz
306
        HAL_WRITE_UINT8(TM0MD,0x40); // stop and load
307
        HAL_WRITE_UINT8(TM0MD,0x80); // set source to be IOCLK and go
308
        HAL_READ_UINT8(TM0BC,last);
309
 
310
        while (delay_count>0 && !FLOWCTL_QUERY(DSR)) {
311
                HAL_READ_UINT8(TM0BC,val);
312
                if (val==last) continue;
313
                if (val>last)
314
                        delay_count--; // count the underflows
315
                last = val;
316
        }
317
        if (delay_count==0)
318
            goto timeout;
319
 
320
        FLOWCTL_SET(RTS);
321
 
322
        while (delay_count>0 && !LSR_QUERY(DR)) {
323
                HAL_READ_UINT8(TM0BC,val);
324
                if (val==last) continue;
325
                if (val>last)
326
                        delay_count--; // count the underflows
327
                last = val;
328
        }
329
 
330
        FLOWCTL_CLEAR(RTS);
331
 
332
        if (LSR_QUERY(DR)) {
333
            HAL_READ_UINT8(base+CYG_DEV_RBR, *ch);
334
            res = true;
335
        }
336
 
337
    timeout:
338
        HAL_WRITE_UINT8(TM0MD,0x00); // stop h/w timer
339
    }
340
 
341
    CYGARC_HAL_RESTORE_GP();
342
    return res;
343
 
344
#else
345
    int delay_count;
346
    channel_data_t* chan = (channel_data_t*)__ch_data;
347
    cyg_uint8* base = chan->base;
348
    cyg_bool res;
349
    CYGARC_HAL_SAVE_GP();
350
 
351
    /* see if there's some cached data in the FIFO */
352
    res = cyg_hal_plf_serial_getc_nonblock(__ch_data,ch);
353
    if (!res) {
354
        /* there isn't - open the flood gates */
355
        delay_count = chan->msec_timeout * 1000; // want delay in uS steps
356
 
357
        for (; delay_count>0 && !FLOWCTL_QUERY(DSR); delay_count--)
358
            CYGACC_CALL_IF_DELAY_US(1);
359
        if (delay_count==0)
360
            goto timeout;
361
 
362
        FLOWCTL_SET(RTS);
363
 
364
        for (; delay_count>0 && !LSR_QUERY(DR); delay_count--)
365
            CYGACC_CALL_IF_DELAY_US(1);
366
 
367
        FLOWCTL_CLEAR(RTS);
368
 
369
        if (LSR_QUERY(DR)) {
370
            HAL_READ_UINT8(base+CYG_DEV_RBR, *ch);
371
            res = true;
372
        }
373
 
374
    }
375
 
376
timeout:
377
    CYGARC_HAL_RESTORE_GP();
378
    return res;
379
#endif
380
}
381
 
382
static int
383
cyg_hal_plf_serial_control(void *__ch_data, __comm_control_cmd_t __func, ...)
384
{
385
    static int irq_state = 0;
386
    channel_data_t* chan = (channel_data_t*)__ch_data;
387
    int ret = 0;
388
    CYGARC_HAL_SAVE_GP();
389
 
390
    switch (__func) {
391
    case __COMMCTL_IRQ_ENABLE:
392
        irq_state = 1;
393
 
394
        HAL_WRITE_UINT8(chan->base+CYG_DEV_IER, SIO_IER_RCV);
395
        HAL_WRITE_UINT8(chan->base+CYG_DEV_MCR, SIO_MCR_INT|SIO_MCR_DTR|SIO_MCR_RTS);
396
 
397
        HAL_INTERRUPT_UNMASK(chan->isr_vector);
398
        break;
399
    case __COMMCTL_IRQ_DISABLE:
400
        ret = irq_state;
401
        irq_state = 0;
402
 
403
        HAL_WRITE_UINT8(chan->base+CYG_DEV_IER, 0);
404
 
405
        HAL_INTERRUPT_MASK(chan->isr_vector);
406
        break;
407
    case __COMMCTL_DBG_ISR_VECTOR:
408
        ret = chan->isr_vector;
409
        break;
410
    case __COMMCTL_SET_TIMEOUT:
411
    {
412
        va_list ap;
413
 
414
        va_start(ap, __func);
415
 
416
        ret = chan->msec_timeout;
417
        chan->msec_timeout = va_arg(ap, cyg_uint32);
418
 
419
        va_end(ap);
420
    }
421
    default:
422
        break;
423
    }
424
    CYGARC_HAL_RESTORE_GP();
425
    return ret;
426
}
427
 
428
static int
429
cyg_hal_plf_serial_isr(void *__ch_data, int* __ctrlc,
430
                       CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
431
{
432
    int res = 0;
433
    channel_data_t* chan = (channel_data_t*)__ch_data;
434
    char c;
435
    cyg_uint8 lsr;
436
    CYGARC_HAL_SAVE_GP();
437
 
438
    cyg_drv_interrupt_acknowledge(chan->isr_vector);
439
 
440
    *__ctrlc = 0;
441
    HAL_READ_UINT8(chan->base+CYG_DEV_LSR, lsr);
442
    if ( (lsr & SIO_LSR_DR) != 0 ) {
443
 
444
        HAL_READ_UINT8(chan->base+CYG_DEV_RBR, c);
445
        if( cyg_hal_is_break( &c , 1 ) )
446
            *__ctrlc = 1;
447
 
448
        res = CYG_ISR_HANDLED;
449
    }
450
 
451
    CYGARC_HAL_RESTORE_GP();
452
    return res;
453
}
454
 
455
static void
456
cyg_hal_plf_serial_init(void)
457
{
458
    hal_virtual_comm_table_t* comm;
459
    int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
460
 
461
    // Disable interrupts.
462
    HAL_INTERRUPT_MASK(asb2305_serial_channels[0].isr_vector);
463
 
464
    // Init channels
465
    cyg_hal_plf_serial_init_channel(&asb2305_serial_channels[0]);
466
 
467
    // Setup procs in the vector table
468
 
469
    // Set channel 0
470
    CYGACC_CALL_IF_SET_CONSOLE_COMM(0);
471
    comm = CYGACC_CALL_IF_CONSOLE_PROCS();
472
    CYGACC_COMM_IF_CH_DATA_SET(*comm, &asb2305_serial_channels[0]);
473
    CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_serial_write);
474
    CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_serial_read);
475
    CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_serial_putc);
476
    CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_serial_getc);
477
    CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_serial_control);
478
    CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_serial_isr);
479
    CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_serial_getc_timeout);
480
 
481
    // Restore original console
482
    CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
483
}
484
 
485
void
486
cyg_hal_plf_comms_init(void)
487
{
488
    static int initialized = 0;
489
 
490
    if (initialized)
491
        return;
492
 
493
    initialized = 1;
494
 
495
    cyg_hal_plf_serial_init();
496
 
497
#if defined(CYGNUM_HAL_AM33_SERIAL_CHANNELS) && CYGNUM_HAL_AM33_SERIAL_CHANNELS > 0
498
    cyg_hal_am33_serial_init(1);
499
#endif
500
}
501
 
502
#endif // defined(CYGNUM_HAL_AM33_PLF_SERIAL_CHANNELS) && CYGNUM_HAL_AM33_PLF_SERIAL_CHANNELS > 0
503
 
504
/*---------------------------------------------------------------------------*/
505
/* End of ser_asb.c */

powered by: WebSVN 2.1.0

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