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

Subversion Repositories openrisc

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//=============================================================================
2
//
3
//      am33_serial.c
4
//
5
//      Simple driver for the serial controllers on AM33 (MN103E) CPUs
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):   dmoseley, dhowells
43
// Contributors:msalter
44
// Date:        2002-11-15
45
// Description: Simple driver for the AM33 UARTs
46
//
47
//####DESCRIPTIONEND####
48
//
49
//=============================================================================
50
 
51
#include <pkgconf/hal.h>
52
#include CYGBLD_HAL_TARGET_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
 
62
#if !defined(CYGSEM_HAL_AM33_PLF_USES_SERIAL0) && !defined(CYGSEM_HAL_AM33_PLF_USES_SERIAL1)
63
#define AM33_NUM_UARTS 0
64
#elif defined(CYGSEM_HAL_AM33_PLF_USES_SERIAL0) && defined(CYGSEM_HAL_AM33_PLF_USES_SERIAL1)
65
#define AM33_NUM_UARTS 2
66
#else
67
#define AM33_NUM_UARTS 1
68
#endif
69
 
70
#if AM33_NUM_UARTS > 0
71
 
72
//-----------------------------------------------------------------------------
73
// Base Registers
74
#define AM33_SER0_BASE      0xD4002000
75
#define AM33_SER1_BASE      0xD4002010
76
 
77
/*---------------------------------------------------------------------------*/
78
// AM33 Serial line
79
 
80
#define _SERIAL_CR       0x00
81
#define _SERIAL_ICR      0x04
82
#define _SERIAL_TXR      0x08
83
#define _SERIAL_RXR      0x09
84
#define _SERIAL_SR       0x0c
85
 
86
#define SERIAL0_CR       ((volatile cyg_uint16 *)(AM33_SER0_BASE + _SERIAL_CR))
87
#define SERIAL0_ICR      ((volatile cyg_uint8 *) (AM33_SER0_BASE + _SERIAL_ICR))
88
#define SERIAL0_TXR      ((volatile cyg_uint8 *) (AM33_SER0_BASE + _SERIAL_TXR))
89
#define SERIAL0_RXR      ((volatile cyg_uint8 *) (AM33_SER0_BASE + _SERIAL_RXR))
90
#define SERIAL0_SR       ((volatile cyg_uint16 *)(AM33_SER0_BASE + _SERIAL_SR))
91
 
92
#define SERIAL1_CR       ((volatile cyg_uint16 *)(AM33_SER1_BASE + _SERIAL_CR))
93
#define SERIAL1_ICR      ((volatile cyg_uint8 *) (AM33_SER1_BASE + _SERIAL_ICR))
94
#define SERIAL1_TXR      ((volatile cyg_uint8 *) (AM33_SER1_BASE + _SERIAL_TXR))
95
#define SERIAL1_RXR      ((volatile cyg_uint8 *) (AM33_SER1_BASE + _SERIAL_RXR))
96
#define SERIAL1_SR       ((volatile cyg_uint16 *)(AM33_SER1_BASE + _SERIAL_SR))
97
 
98
// Timer 0 provides a prescaler for lower baud rates
99
#define TIMER0_MD       ((volatile cyg_uint8 *)0xd4003000)
100
#define TIMER0_BR       ((volatile cyg_uint8 *)0xd4003010)
101
 
102
// Timer 2 provides baud rate divisor
103
#define TIMER2_MD       ((volatile cyg_uint8 *)0xd4003002)
104
#define TIMER2_BR       ((volatile cyg_uint8 *)0xd4003012)
105
 
106
// Timer 1 provides a prescaler for lower baud rates
107
#define TIMER1_MD       ((volatile cyg_uint8 *)0xd4003001)
108
#define TIMER1_BR       ((volatile cyg_uint8 *)0xd4003011)
109
 
110
// Timer 3 provides baud rate divisor
111
#define TIMER3_MD       ((volatile cyg_uint8 *)0xd4003003)
112
#define TIMER3_BR       ((volatile cyg_uint8 *)0xd4003013)
113
 
114
#define SIO_LSTAT_TRDY  0x20
115
#define SIO_LSTAT_RRDY  0x10
116
 
117
#define SIO_INT_ENABLE  0x11
118
 
119
#define TMR_ENABLE                   0x80
120
#define TMR_SRC_IOCLOCK              0x00
121
#define TMR_SRC_TMR0_UNDERFLOW       0x04
122
 
123
 
124
//-----------------------------------------------------------------------------
125
 
126
typedef struct {
127
    cyg_uint8* base;
128
    cyg_int32 msec_timeout;
129
    int isr_vector;
130
    cyg_int32 baud_rate;
131
} channel_data_t;
132
 
133
static channel_data_t channels[AM33_NUM_UARTS] = {
134
#if defined(CYGSEM_HAL_AM33_PLF_USES_SERIAL0) && !defined(HAL_PLATFORM_SERIAL1_FIRST)
135
    { (cyg_uint8*)AM33_SER0_BASE, 1000, CYGNUM_HAL_INTERRUPT_SERIAL_0_RX },
136
#endif
137
#ifdef CYGSEM_HAL_AM33_PLF_USES_SERIAL1
138
    { (cyg_uint8*)AM33_SER1_BASE, 1000, CYGNUM_HAL_INTERRUPT_SERIAL_1_RX },
139
#endif
140
#if defined(CYGSEM_HAL_AM33_PLF_USES_SERIAL0) && defined(HAL_PLATFORM_SERIAL1_FIRST)
141
    { (cyg_uint8*)AM33_SER0_BASE, 1000, CYGNUM_HAL_INTERRUPT_SERIAL_0_RX },
142
#endif
143
};
144
 
145
//-----------------------------------------------------------------------------
146
// Set the baud rate
147
 
148
static cyg_uint32
149
baud_divisor(int baud, int prescaler)
150
{
151
    cyg_uint32 divisor;
152
 
153
    // divisor == INT(IOCLK/baud/8 + 0.5)
154
    divisor = CYGHWR_HAL_MN10300_IOCLK_SPEED * 10;
155
    divisor /= (baud / 100);
156
    divisor /= prescaler;
157
    divisor /= 8;
158
    divisor += 500;
159
    divisor /= 1000;
160
    return divisor;
161
}
162
 
163
static int
164
cyg_hal_plf_serial_set_baud(cyg_uint8* port, cyg_uint32 baud_rate)
165
{
166
    volatile cyg_uint8 *timer_base_reg;
167
    volatile cyg_uint8 *timer_mode_reg;
168
    cyg_uint32 divisor, prescaler;
169
 
170
    if (port == (cyg_uint8*)AM33_SER0_BASE)
171
    {
172
        // SER0 uses TMR2
173
        timer_base_reg = TIMER2_BR;
174
        timer_mode_reg = TIMER2_MD;
175
    } else if (port == (cyg_uint8*)AM33_SER1_BASE) {
176
        // SER1 uses TMR3
177
        timer_base_reg = TIMER3_BR;
178
        timer_mode_reg = TIMER3_MD;
179
    } else {
180
        // Unknown port.
181
        return -1;
182
    }
183
 
184
    switch (baud_rate)
185
    {
186
    case 1200:
187
    case 2400:
188
    case 4800:
189
    case 9600:
190
    case 19200:
191
    case 38400:
192
    case 57600:
193
    case 115200:
194
    case 230400:
195
        break;
196
 
197
    default:
198
        // Unknown baud.  Don't change anything
199
        return -1;
200
    }
201
 
202
    for (prescaler = 1; prescaler <= 256; prescaler++) {
203
        divisor = baud_divisor(baud_rate, prescaler);
204
        if (divisor <= 256)
205
            break;
206
    }
207
    --divisor;
208
    --prescaler;
209
 
210
    if (prescaler) {
211
        HAL_WRITE_UINT8(TIMER0_BR, prescaler);
212
        HAL_WRITE_UINT8(TIMER0_MD, TMR_ENABLE | TMR_SRC_IOCLOCK);
213
    } else {
214
        HAL_WRITE_UINT8(TIMER0_BR, 0);
215
        HAL_WRITE_UINT8(TIMER0_MD, 0);
216
    }
217
 
218
    HAL_WRITE_UINT8(timer_base_reg, divisor);
219
    HAL_WRITE_UINT8(timer_mode_reg, TMR_ENABLE |
220
                    (prescaler ? TMR_SRC_TMR0_UNDERFLOW : TMR_SRC_IOCLOCK));
221
 
222
    return 0;
223
}
224
 
225
//-----------------------------------------------------------------------------
226
// The minimal init, get and put functions. All by polling.
227
 
228
static void
229
cyg_hal_plf_serial_init_channel(void* __ch_data)
230
{
231
    cyg_uint8* port;
232
 
233
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
234
    // Go ahead and assume it is channels[0].
235
    if (__ch_data == 0)
236
      __ch_data = (void*)&channels[0];
237
 
238
    port = ((channel_data_t*)__ch_data)->base;
239
 
240
    // No interrupts for now.
241
    HAL_WRITE_UINT8(port + _SERIAL_ICR, 0x00);
242
 
243
    // Source from timer 2 or 3, 8bit chars, enable tx and rx
244
    HAL_WRITE_UINT16(port + _SERIAL_CR, 0xc085);
245
}
246
 
247
static void
248
cyg_hal_plf_serial_putc(void* __ch_data, cyg_uint8 __ch)
249
{
250
    cyg_uint8* port;
251
    cyg_uint16 _status;
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
    do {
261
        HAL_READ_UINT16(port + _SERIAL_SR, _status);
262
    } while ((_status & SIO_LSTAT_TRDY) != 0);
263
 
264
    HAL_WRITE_UINT8(port + _SERIAL_TXR, __ch);
265
}
266
 
267
static cyg_bool
268
cyg_hal_plf_serial_getc_nonblock(void* __ch_data, cyg_uint8* ch)
269
{
270
    cyg_uint8* port;
271
    cyg_uint8 _status;
272
 
273
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
274
    // Go ahead and assume it is channels[0].
275
    if (__ch_data == 0)
276
      __ch_data = (void*)&channels[0];
277
 
278
    port = ((channel_data_t*)__ch_data)->base;
279
 
280
    HAL_READ_UINT8(port + _SERIAL_SR, _status);
281
    if ((_status & SIO_LSTAT_RRDY) == 0)
282
        return false;
283
 
284
    HAL_READ_UINT8(port + _SERIAL_RXR, *ch);
285
 
286
    // We must ack the interrupt caused by that read to avoid
287
    // confusing the GDB stub ROM.
288
    HAL_INTERRUPT_ACKNOWLEDGE( CYGNUM_HAL_INTERRUPT_SERIAL_0_RX );
289
 
290
    return true;
291
}
292
 
293
static cyg_uint8
294
cyg_hal_plf_serial_getc(void* __ch_data)
295
{
296
    cyg_uint8 ch;
297
    CYGARC_HAL_SAVE_GP();
298
 
299
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
300
    // Go ahead and assume it is channels[0].
301
    if (__ch_data == 0)
302
      __ch_data = (void*)&channels[0];
303
 
304
    while(!cyg_hal_plf_serial_getc_nonblock(__ch_data, &ch));
305
 
306
    CYGARC_HAL_RESTORE_GP();
307
    return ch;
308
}
309
 
310
static void
311
cyg_hal_plf_serial_write(void* __ch_data, const cyg_uint8* __buf,
312
                         cyg_uint32 __len)
313
{
314
    CYGARC_HAL_SAVE_GP();
315
 
316
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
317
    // Go ahead and assume it is channels[0].
318
    if (__ch_data == 0)
319
      __ch_data = (void*)&channels[0];
320
 
321
    while(__len-- > 0)
322
        cyg_hal_plf_serial_putc(__ch_data, *__buf++);
323
 
324
    CYGARC_HAL_RESTORE_GP();
325
}
326
 
327
static void
328
cyg_hal_plf_serial_read(void* __ch_data, cyg_uint8* __buf, 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
        *__buf++ = cyg_hal_plf_serial_getc(__ch_data);
339
 
340
    CYGARC_HAL_RESTORE_GP();
341
}
342
 
343
static cyg_bool
344
cyg_hal_plf_serial_getc_timeout(void* __ch_data, cyg_uint8* ch)
345
{
346
    int delay_count;
347
    channel_data_t* chan;
348
    cyg_bool res;
349
    CYGARC_HAL_SAVE_GP();
350
 
351
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
352
    // Go ahead and assume it is channels[0].
353
    if (__ch_data == 0)
354
      __ch_data = (void*)&channels[0];
355
 
356
    chan = (channel_data_t*)__ch_data;
357
 
358
    delay_count = chan->msec_timeout * 10; // delay in .1 ms steps
359
 
360
    for(;;) {
361
        res = cyg_hal_plf_serial_getc_nonblock(__ch_data, ch);
362
        if (res || 0 == delay_count--)
363
            break;
364
        CYGACC_CALL_IF_DELAY_US(100);
365
    }
366
 
367
    CYGARC_HAL_RESTORE_GP();
368
    return res;
369
}
370
 
371
static int
372
cyg_hal_plf_serial_control(void *__ch_data, __comm_control_cmd_t __func, ...)
373
{
374
    static int irq_state = 0;
375
    channel_data_t* chan;
376
    cyg_uint8 icr;
377
    int ret = 0;
378
    CYGARC_HAL_SAVE_GP();
379
 
380
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
381
    // Go ahead and assume it is channels[0].
382
    if (__ch_data == 0)
383
      __ch_data = (void*)&channels[0];
384
 
385
    chan = (channel_data_t*)__ch_data;
386
 
387
    switch (__func) {
388
    case __COMMCTL_IRQ_ENABLE:
389
        irq_state = 1;
390
 
391
        HAL_READ_UINT8(chan->base + _SERIAL_ICR, icr);
392
        icr |= SIO_INT_ENABLE;
393
        HAL_WRITE_UINT8(chan->base + _SERIAL_ICR, icr);
394
 
395
        HAL_INTERRUPT_SET_LEVEL(chan->isr_vector, 1);
396
        HAL_INTERRUPT_UNMASK(chan->isr_vector);
397
        break;
398
 
399
    case __COMMCTL_IRQ_DISABLE:
400
        ret = irq_state;
401
        irq_state = 0;
402
 
403
        HAL_READ_UINT8(chan->base + _SERIAL_ICR, icr);
404
        icr &= ~SIO_INT_ENABLE;
405
        HAL_WRITE_UINT8(chan->base + _SERIAL_ICR, icr);
406
 
407
        HAL_INTERRUPT_MASK(chan->isr_vector);
408
        break;
409
 
410
    case __COMMCTL_DBG_ISR_VECTOR:
411
        ret = chan->isr_vector;
412
        break;
413
 
414
    case __COMMCTL_SET_TIMEOUT:
415
    {
416
        va_list ap;
417
 
418
        va_start(ap, __func);
419
 
420
        ret = chan->msec_timeout;
421
        chan->msec_timeout = va_arg(ap, cyg_uint32);
422
 
423
        va_end(ap);
424
    }
425
    break;
426
 
427
    case __COMMCTL_SETBAUD:
428
    {
429
        cyg_uint32 baud_rate;
430
        cyg_uint8* port = chan->base;
431
        va_list ap;
432
 
433
        va_start(ap, __func);
434
        baud_rate = va_arg(ap, cyg_uint32);
435
        va_end(ap);
436
 
437
        // Disable port interrupts while changing hardware
438
        HAL_READ_UINT8(port + _SERIAL_ICR, icr);
439
        HAL_WRITE_UINT8(port + _SERIAL_ICR, 0);
440
 
441
        // Set baud rate.
442
        ret = cyg_hal_plf_serial_set_baud(port, baud_rate);
443
 
444
        // Reenable interrupts if necessary
445
        HAL_WRITE_UINT8(port + _SERIAL_ICR, icr);
446
    }
447
    break;
448
 
449
    case __COMMCTL_GETBAUD:
450
        break;
451
 
452
    default:
453
        break;
454
    }
455
 
456
    CYGARC_HAL_RESTORE_GP();
457
    return ret;
458
}
459
 
460
static int
461
cyg_hal_plf_serial_isr(void *__ch_data, int* __ctrlc,
462
                       CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
463
{
464
    int res = 0;
465
    channel_data_t* chan;
466
    CYGARC_HAL_SAVE_GP();
467
 
468
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
469
    // Go ahead and assume it is channels[0].
470
    if (__ch_data == 0)
471
      __ch_data = (void*)&channels[0];
472
 
473
    chan = (channel_data_t*)__ch_data;
474
 
475
    HAL_INTERRUPT_ACKNOWLEDGE(chan->isr_vector);
476
 
477
#if 0
478
    HAL_READ_UINT8(chan->base + SER_16550_IIR, _iir);
479
    _iir &= SIO_IIR_ID_MASK;
480
 
481
    *__ctrlc = 0;
482
    if ((_iir == ISR_Rx_Avail) || (_iir == ISR_Rx_Char_Timeout)) {
483
 
484
        HAL_READ_UINT8(chan->base + SER_16550_RBR, c);
485
 
486
        if( cyg_hal_is_break( &c , 1 ) )
487
            *__ctrlc = 1;
488
 
489
        res = CYG_ISR_HANDLED;
490
    }
491
#endif
492
 
493
    CYGARC_HAL_RESTORE_GP();
494
    return res;
495
}
496
 
497
 
498
void
499
cyg_hal_am33_serial_init(int first_chan)
500
{
501
    hal_virtual_comm_table_t* comm;
502
    int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
503
    int i;
504
 
505
    for (i = 0; i < AM33_NUM_UARTS; i++) {
506
 
507
        // Disable interrupts.
508
        HAL_INTERRUPT_MASK(channels[0].isr_vector);
509
 
510
        // Init channel
511
        cyg_hal_plf_serial_init_channel((void*)&channels[i]);
512
        cyg_hal_plf_serial_set_baud(channels[i].base, CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD);
513
 
514
        // Setup procs in the vector table
515
        CYGACC_CALL_IF_SET_CONSOLE_COMM(i + first_chan);
516
        comm = CYGACC_CALL_IF_CONSOLE_PROCS();
517
        CYGACC_COMM_IF_CH_DATA_SET(*comm, &channels[i]);
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
 
527
    // Restore original console
528
    CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
529
}
530
 
531
void
532
cyg_hal_plf_serial_setbaud(void *__ch_data, cyg_uint32 baud_rate)
533
{
534
    cyg_uint8* port;
535
 
536
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
537
    // Go ahead and assume it is channels[0].
538
    if (__ch_data == 0)
539
      __ch_data = (void*)&channels[0];
540
 
541
    port = ((channel_data_t*)__ch_data)->base;
542
 
543
    cyg_hal_plf_serial_set_baud(port, baud_rate);
544
}
545
 
546
 
547
// If the platform provides some channels of its own, then this function will be
548
// provided by that platform.
549
#if !defined(CYGNUM_HAL_AM33_PLF_SERIAL_CHANNELS) || !CYGNUM_HAL_AM33_PLF_SERIAL_CHANNELS 
550
void
551
cyg_hal_plf_comms_init(void)
552
{
553
    static int initialized = 0;
554
 
555
    if (initialized)
556
        return;
557
 
558
    initialized = 1;
559
 
560
    cyg_hal_am33_serial_init(0);
561
}
562
#endif
563
 
564
#endif // AM33_NUM_UARTS > 0
565
 
566
/*---------------------------------------------------------------------------*/
567
/* End of am33_serial.c */

powered by: WebSVN 2.1.0

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