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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [devs/] [serial/] [mn10300/] [mn10300/] [current/] [src/] [mn10300_serial.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      mn10300_serial.c
4
//
5
//      Serial device driver for mn10300 on-chip serial devices
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):   nickg
43
// Contributors: nickg
44
// Date:        1999-02-25
45
// Purpose:     MN10300 serial device driver
46
// Description: MN10300 serial device driver
47
//
48
//####DESCRIPTIONEND####
49
//
50
//==========================================================================
51
 
52
#include <pkgconf/hal.h>
53
#include <pkgconf/io_serial.h>
54
#include <cyg/hal/hal_io.h>
55
 
56
#include <cyg/io/io.h>
57
#include <cyg/io/devtab.h>
58
#include <cyg/io/serial.h>
59
#include <cyg/hal/hal_intr.h>
60
 
61
#ifdef CYGPKG_IO_SERIAL_MN10300
62
 
63
#define CYG_HAL_MN10300_SERIAL_RX_FIFO
64
 
65
//-------------------------------------------------------------------------
66
 
67
extern void diag_printf(const char *fmt, ...);
68
 
69
//-------------------------------------------------------------------------
70
// Forward definitions
71
 
72
static bool mn10300_serial_init(struct cyg_devtab_entry *tab);
73
static bool mn10300_serial_putc(serial_channel *chan, unsigned char c);
74
static Cyg_ErrNo mn10300_serial_lookup(struct cyg_devtab_entry **tab,
75
                                   struct cyg_devtab_entry *sub_tab,
76
                                   const char *name);
77
static unsigned char mn10300_serial_getc(serial_channel *chan);
78
static Cyg_ErrNo mn10300_serial_set_config(serial_channel *chan, cyg_uint32 key,
79
                                           const void *xbuf, cyg_uint32 *len);
80
static void mn10300_serial_start_xmit(serial_channel *chan);
81
static void mn10300_serial_stop_xmit(serial_channel *chan);
82
 
83
#ifndef CYGPKG_IO_SERIAL_MN10300_POLLED_MODE
84
static cyg_uint32 mn10300_serial_rx_ISR(cyg_vector_t vector, cyg_addrword_t data);
85
static cyg_uint32 mn10300_serial_tx_ISR(cyg_vector_t vector, cyg_addrword_t data);
86
static void       mn10300_serial_rx_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data);
87
static void       mn10300_serial_tx_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data);
88
#endif
89
 
90
//-------------------------------------------------------------------------
91
 
92
#define BUFSIZE 128
93
 
94
//-------------------------------------------------------------------------
95
// MN10300 serial line control register values:
96
 
97
// Offsets to serial control registers from base
98
#define SERIAL_CTR      0x0
99
#define SERIAL_ICR      0x4
100
#define SERIAL_TXB      0x8
101
#define SERIAL_RXB      0x9
102
#define SERIAL_STR      0xc
103
#define SERIAL_TIM      0xd
104
 
105
// Status register bits
106
#define SR_RBF          0x10
107
#define SR_TBF          0x20
108
#define SR_RXF          0x40
109
#define SR_TXF          0x80
110
 
111
// Control register bits
112
#define LCR_SB1         0x00
113
#define LCR_SB1_5       0x00
114
#define LCR_SB2         0x04
115
#define LCR_PN          0x00    // Parity mode - none
116
#define LCR_PS          0x40    // Forced "space" parity
117
#define LCR_PM          0x50    // Forced "mark" parity
118
#define LCR_PE          0x60    // Parity mode - even
119
#define LCR_PO          0x70    // Parity mode - odd
120
#define LCR_WL5         0x00    // not supported - use 7bit
121
#define LCR_WL6         0x00    // not supported - use 7bit
122
#define LCR_WL7         0x00    // 7 bit chars
123
#define LCR_WL8         0x80    // 8 bit chars
124
#define LCR_RXE         0x4000  // receive enable
125
#define LCR_TXE         0x8000  // transmit enable
126
 
127
#if defined(CYGPKG_HAL_MN10300_AM31)
128
#define LCR_TWE         0x0100  // interrupt enable (only on serial2/AM31)
129
#else
130
#define LCR_TWE         0x0000  // Bit does not exist in other variants
131
#endif
132
 
133
//-------------------------------------------------------------------------
134
// MN10300 timer registers:
135
 
136
#undef TIMER_BR
137
#undef TIMER_MD
138
#define TIMER_MD        0x00
139
#define TIMER_BR        0x10
140
 
141
//-------------------------------------------------------------------------
142
// Serial and timer base registers:
143
 
144
#if defined(CYGPKG_HAL_MN10300_AM31)
145
 
146
#define SERIAL0_BASE            0x34000800
147
#define SERIAL1_BASE            0x34000810
148
#define SERIAL2_BASE            0x34000820
149
 
150
#define TIMER0_BASE             0x34001000
151
#define TIMER1_BASE             0x34001001
152
#define TIMER2_BASE             0x34001002
153
 
154
#define SERIAL0_TIMER_SELECT    0x0004          // timer 0
155
#define SERIAL1_TIMER_SELECT    0x0004          // timer 1
156
#define SERIAL2_TIMER_SELECT    0x0001          // timer 2
157
 
158
#ifdef CYGPKG_HAL_MN10300_AM31_STDEVAL1
159
// The use of PORT3 to provide CTS/CTR is specific to
160
// the STDEVAL1 board only.
161
#define PORT3_MD                0x36008025
162
#endif
163
 
164
#define ENABLE_TRANSMIT_INTERRUPT(mn10300_chan) \
165
CYG_MACRO_START                                 \
166
    if( mn10300_chan->is_serial2 )              \
167
        cr |= LCR_TWE;                          \
168
    else                                        \
169
        cr |= LCR_TXE;                          \
170
CYG_MACRO_END
171
 
172
#define DISABLE_TRANSMIT_INTERRUPT(mn10300_chan)        \
173
CYG_MACRO_START                                         \
174
    if( mn10300_chan->is_serial2 )                      \
175
        cr &= ~LCR_TWE;                                 \
176
    else                                                \
177
        cr &= ~LCR_TXE;                                 \
178
CYG_MACRO_END
179
 
180
#elif defined(CYGPKG_HAL_MN10300_AM33)
181
 
182
#define SERIAL0_BASE            0xd4002000
183
#define SERIAL1_BASE            0xd4002010
184
#define SERIAL2_BASE            0xd4002020
185
 
186
#define TIMER0_BASE             0xd4003002
187
#define TIMER1_BASE             0xd4003001
188
#define TIMER2_BASE             0xd4003003
189
 
190
#define SERIAL0_TIMER_SELECT    0x0005          // timer 2
191
#define SERIAL1_TIMER_SELECT    0x0004          // timer 1
192
#define SERIAL2_TIMER_SELECT    0x0003          // timer 3
193
 
194
#define HW_TIMER0               0xd4003000
195
 
196
#define ENABLE_TRANSMIT_INTERRUPT(mn10300_chan)
197
 
198
#define DISABLE_TRANSMIT_INTERRUPT(mn10300_chan)
199
 
200
#else
201
 
202
#error Unsupported MN10300 variant
203
 
204
#endif
205
 
206
//-------------------------------------------------------------------------
207
// Tables to map input values to hardware settings
208
 
209
static unsigned char select_word_length[] = {
210
    LCR_WL5,    // 5 bits / word (char)
211
    LCR_WL6,
212
    LCR_WL7,
213
    LCR_WL8
214
};
215
 
216
static unsigned char select_stop_bits[] = {
217
    0,
218
    LCR_SB1,    // 1 stop bit
219
    LCR_SB1_5,  // 1.5 stop bit
220
    LCR_SB2     // 2 stop bits
221
};
222
 
223
static unsigned char select_parity[] = {
224
    LCR_PN,     // No parity
225
    LCR_PE,     // Even parity
226
    LCR_PO,     // Odd parity
227
    LCR_PM,     // Mark parity
228
    LCR_PS,     // Space parity
229
};
230
 
231
#if defined(CYGPKG_HAL_MN10300_AM31)
232
 
233
static unsigned short select_baud_01[] = {
234
    0,          // Unused
235
    0,          // 50
236
    0,          // 75
237
    0,          // 110
238
    0,          // 134.5
239
    0,          // 150
240
    0,          // 200
241
    0,          // 300
242
    0,          // 600
243
    0,          // 1200
244
    0,          // 1800
245
    0,          // 2400
246
    0,          // 3600
247
    0,          // 4800
248
    0,          // 7200
249
    195,        // 9600
250
    130,        // 14400
251
    98,         // 19200
252
    48,         // 38400
253
    32,         // 57600
254
    16,         // 115200
255
    8,          // 230400
256
};
257
 
258
// Serial 2 has its own timer register in addition to using timer 2 to
259
// supply the baud rate generator. Both of these must be proframmed to
260
// get the right baud rate. The following values come from Matsushita
261
// with some modifications from Cygmon.
262
static struct
263
{
264
    cyg_uint8   serial2_val;
265
    cyg_uint8   timer2_val;
266
} select_baud_2[] = {
267
    {   0,   0 },       // Unused
268
    {   0,   0 },       // 50
269
    {   0,   0 },       // 75
270
    {   0,   0 },       // 110
271
    {   0,   0 },       // 134.5
272
    {   0,   0 },       // 150
273
    {   0,   0 },       // 200
274
    {   0,   0 },       // 300
275
    { 126, 196 },       // 600
276
    { 125,  98 },       // 1200
277
    {   0,   0 },       // 1800
278
    { 124,  49 },       // 2400
279
    { 0,     0 },       // 3600
280
    { 124,  24 },       // 4800
281
    {   0,   0 },       // 7200
282
    {  70,  21 },       // 9600
283
    {   0,   0 },       // 14400
284
    {  70,  10 },       // 19200
285
    {  22,  16 },       // 38400
286
    {  88,   2 },       // 57600
287
    {  64,   1 },       // 115200
288
    {  62,   0 },       // 230400
289
};
290
 
291
#elif defined(CYGPKG_HAL_MN10300_AM33)
292
 
293
// The AM33 runs at a different clock rate and therefore has a
294
// different set of dividers for the baud rate.
295
 
296
static unsigned short select_baud_01[] = {
297
    0,          // Unused
298
    0,          // 50
299
    0,          // 75
300
    0,          // 110
301
    0,          // 134.5
302
    0,          // 150
303
    0,          // 200
304
    0,          // 300
305
    0,          // 600
306
    3168,       // 1200
307
    0,          // 1800
308
    1584,       // 2400
309
    0,          // 3600
310
    792,        // 4800
311
    0,          // 7200
312
    396,        // 9600
313
    0,          // 14400
314
    198,        // 19200
315
    99,         // 38400
316
     0,         // 57600
317
    33,         // 115200
318
    16,         // 230400
319
};
320
 
321
// Serial 2 has its own timer register in addition to using timer 2 to
322
// supply the baud rate generator. Both of these must be proframmed to
323
// get the right baud rate. The following values come from Matsushita
324
// with some modifications from Cygmon.
325
 
326
// The values in the following table differ significantly from those
327
// given in the Matsushita documentation. These have been determined
328
// by (somewhat exhaustive) experiment, the values in the documentation
329
// do not appear to work at all.
330
 
331
static struct
332
{
333
    cyg_uint8   serial2_val;
334
    cyg_uint8   timer2_val;
335
} select_baud_2[] = {
336
    {   0,   0 },       // Unused
337
    {   0,   0 },       // 50
338
    {   0,   0 },       // 75
339
    {   0,   0 },       // 110
340
    {   0,   0 },       // 134.5
341
    {   0,   0 },       // 150
342
    {   0,   0 },       // 200
343
    {   0,   0 },       // 300
344
    {   0,   0 },       // 600
345
    {   0,   0 },       // 1200
346
    {   0,   0 },       // 1800
347
    {   0,   0 },       // 2400
348
    {   0,   0 },       // 3600
349
    { 110,  56 },       // 4800
350
    {   0,   0 },       // 7200
351
    { 110,  28 },       // 9600
352
    {   0,   0 },       // 14400
353
    {  71,  21 },       // 19200
354
    { 102,   7 },       // 38400
355
    {   0,   0 },       // 57600
356
    {   9,  26 },       // 115200
357
    {   0,   0 },       // 230400
358
};
359
 
360
#else
361
 
362
#error Unsupported MN10300 variant
363
 
364
#endif
365
 
366
//-------------------------------------------------------------------------
367
// Info for each serial device controlled
368
 
369
typedef struct mn10300_serial_info {
370
    CYG_ADDRWORD        base;
371
    CYG_ADDRWORD        timer_base;
372
    CYG_WORD            timer_select;
373
    CYG_WORD            rx_int;
374
    CYG_WORD            tx_int;
375
    cyg_bool            is_serial2;
376
    cyg_interrupt       rx_interrupt;
377
    cyg_interrupt       tx_interrupt;
378
    cyg_handle_t        rx_interrupt_handle;
379
    cyg_handle_t        tx_interrupt_handle;
380
#ifdef CYG_HAL_MN10300_SERIAL_RX_FIFO
381
    volatile cyg_int32  fifo_head;
382
    volatile cyg_int32  fifo_tail;
383
    volatile cyg_uint8  fifo[16];
384
#endif    
385
} mn10300_serial_info;
386
 
387
//-------------------------------------------------------------------------
388
// Callback functions exported by this driver
389
 
390
static SERIAL_FUNS(mn10300_serial_funs,
391
                   mn10300_serial_putc,
392
                   mn10300_serial_getc,
393
                   mn10300_serial_set_config,
394
                   mn10300_serial_start_xmit,
395
                   mn10300_serial_stop_xmit
396
    );
397
 
398
//-------------------------------------------------------------------------
399
// Hardware info for each serial line
400
 
401
#ifndef CYGPKG_HAL_MN10300_AM31_STDEVAL1
402
#ifdef CYGPKG_IO_SERIAL_MN10300_SERIAL0
403
static mn10300_serial_info mn10300_serial_info0 = {
404
    SERIAL0_BASE,
405
    TIMER0_BASE,
406
    SERIAL0_TIMER_SELECT,
407
    CYGNUM_HAL_INTERRUPT_SERIAL_0_RX,
408
    CYGNUM_HAL_INTERRUPT_SERIAL_0_TX,
409
    false
410
};
411
#if CYGNUM_IO_SERIAL_MN10300_SERIAL0_BUFSIZE > 0
412
static unsigned char mn10300_serial_out_buf0[CYGNUM_IO_SERIAL_MN10300_SERIAL0_BUFSIZE];
413
static unsigned char mn10300_serial_in_buf0[CYGNUM_IO_SERIAL_MN10300_SERIAL0_BUFSIZE];
414
#endif
415
#endif // CYGPKG_IO_SERIAL_MN10300_SERIAL0
416
#endif
417
 
418
#ifdef CYGPKG_IO_SERIAL_MN10300_SERIAL1
419
static mn10300_serial_info mn10300_serial_info1 = {
420
    SERIAL1_BASE,
421
    TIMER1_BASE,
422
    SERIAL1_TIMER_SELECT,
423
    CYGNUM_HAL_INTERRUPT_SERIAL_1_RX,
424
    CYGNUM_HAL_INTERRUPT_SERIAL_1_TX,
425
    false
426
};
427
#if CYGNUM_IO_SERIAL_MN10300_SERIAL1_BUFSIZE > 0
428
static unsigned char mn10300_serial_out_buf1[CYGNUM_IO_SERIAL_MN10300_SERIAL1_BUFSIZE];
429
static unsigned char mn10300_serial_in_buf1[CYGNUM_IO_SERIAL_MN10300_SERIAL1_BUFSIZE];
430
#endif
431
#endif // CYGPKG_IO_SERIAL_MN10300_SERIAL1
432
 
433
#ifdef CYGPKG_IO_SERIAL_MN10300_SERIAL2
434
static mn10300_serial_info mn10300_serial_info2 = {
435
    SERIAL2_BASE,
436
    TIMER2_BASE,
437
    SERIAL2_TIMER_SELECT,
438
    CYGNUM_HAL_INTERRUPT_SERIAL_2_RX,
439
    CYGNUM_HAL_INTERRUPT_SERIAL_2_TX,
440
    true
441
};
442
#if CYGNUM_IO_SERIAL_MN10300_SERIAL2_BUFSIZE > 0
443
static unsigned char mn10300_serial_out_buf2[CYGNUM_IO_SERIAL_MN10300_SERIAL2_BUFSIZE];
444
static unsigned char mn10300_serial_in_buf2[CYGNUM_IO_SERIAL_MN10300_SERIAL2_BUFSIZE];
445
#endif
446
#endif // CYGPKG_IO_SERIAL_MN10300_SERIAL2
447
 
448
 
449
//-------------------------------------------------------------------------
450
// Channel descriptions:
451
 
452
#ifdef CYGPKG_IO_SERIAL_MN10300_POLLED_MODE
453
#define SIZEOF_BUF(_x_) 0
454
#else
455
#define SIZEOF_BUF(_x_) sizeof(_x_)
456
#endif
457
 
458
#ifndef CYGPKG_HAL_MN10300_AM31_STDEVAL1
459
#ifdef CYGPKG_IO_SERIAL_MN10300_SERIAL0
460
#if CYGNUM_IO_SERIAL_MN10300_SERIAL0_BUFSIZE > 0
461
static SERIAL_CHANNEL_USING_INTERRUPTS(mn10300_serial_channel0,
462
                                       mn10300_serial_funs,
463
                                       mn10300_serial_info0,
464
                                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_MN10300_SERIAL0_BAUD),
465
                                       CYG_SERIAL_STOP_DEFAULT,
466
                                       CYG_SERIAL_PARITY_DEFAULT,
467
                                       CYG_SERIAL_WORD_LENGTH_DEFAULT,
468
                                       CYG_SERIAL_FLAGS_DEFAULT,
469
                                       &mn10300_serial_out_buf0[0],
470
                                       SIZEOF_BUF(mn10300_serial_out_buf0),
471
                                       &mn10300_serial_in_buf0[0],
472
                                       SIZEOF_BUF(mn10300_serial_in_buf0)
473
    );
474
#else
475
static SERIAL_CHANNEL(mn10300_serial_channel0,
476
                      mn10300_serial_funs,
477
                      mn10300_serial_info0,
478
                      CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_MN10300_SERIAL0_BAUD),
479
                      CYG_SERIAL_STOP_DEFAULT,
480
                      CYG_SERIAL_PARITY_DEFAULT,
481
                      CYG_SERIAL_WORD_LENGTH_DEFAULT,
482
                      CYG_SERIAL_FLAGS_DEFAULT
483
    );
484
#endif
485
#endif // CYGPKG_IO_SERIAL_MN10300_SERIAL0
486
#endif
487
 
488
#ifdef CYGPKG_IO_SERIAL_MN10300_SERIAL1
489
#if CYGNUM_IO_SERIAL_MN10300_SERIAL1_BUFSIZE > 0
490
static SERIAL_CHANNEL_USING_INTERRUPTS(mn10300_serial_channel1,
491
                                       mn10300_serial_funs,
492
                                       mn10300_serial_info1,
493
                                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_MN10300_SERIAL1_BAUD),
494
                                       CYG_SERIAL_STOP_DEFAULT,
495
                                       CYG_SERIAL_PARITY_DEFAULT,
496
                                       CYG_SERIAL_WORD_LENGTH_DEFAULT,
497
                                       CYG_SERIAL_FLAGS_DEFAULT,
498
                                       &mn10300_serial_out_buf1[0],
499
                                       SIZEOF_BUF(mn10300_serial_out_buf1),
500
                                       &mn10300_serial_in_buf1[0],
501
                                       SIZEOF_BUF(mn10300_serial_in_buf1)
502
    );
503
#else
504
static SERIAL_CHANNEL(mn10300_serial_channel1,
505
                      mn10300_serial_funs,
506
                      mn10300_serial_info1,
507
                      CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_MN10300_SERIAL1_BAUD),
508
                      CYG_SERIAL_STOP_DEFAULT,
509
                      CYG_SERIAL_PARITY_DEFAULT,
510
                      CYG_SERIAL_WORD_LENGTH_DEFAULT,
511
                      CYG_SERIAL_FLAGS_DEFAULT
512
    );
513
#endif
514
#endif // CYGPKG_IO_SERIAL_MN10300_SERIAL1
515
 
516
#ifdef CYGPKG_IO_SERIAL_MN10300_SERIAL2
517
#if CYGNUM_IO_SERIAL_MN10300_SERIAL2_BUFSIZE > 0
518
static SERIAL_CHANNEL_USING_INTERRUPTS(mn10300_serial_channel2,
519
                                       mn10300_serial_funs,
520
                                       mn10300_serial_info2,
521
                                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_MN10300_SERIAL2_BAUD),
522
                                       CYG_SERIAL_STOP_DEFAULT,
523
                                       CYG_SERIAL_PARITY_DEFAULT,
524
                                       CYG_SERIAL_WORD_LENGTH_DEFAULT,
525
                                       CYG_SERIAL_FLAGS_DEFAULT,
526
                                       &mn10300_serial_out_buf2[0],
527
                                       SIZEOF_BUF(mn10300_serial_out_buf2),
528
                                       &mn10300_serial_in_buf2[0],
529
                                       SIZEOF_BUF(mn10300_serial_in_buf2)
530
    );
531
#else
532
static SERIAL_CHANNEL(mn10300_serial_channel2,
533
                      mn10300_serial_funs,
534
                      mn10300_serial_info2,
535
                      CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_MN10300_SERIAL2_BAUD),
536
                      CYG_SERIAL_STOP_DEFAULT,
537
                      CYG_SERIAL_PARITY_DEFAULT,
538
                      CYG_SERIAL_WORD_LENGTH_DEFAULT,
539
                      CYG_SERIAL_FLAGS_DEFAULT
540
    );
541
#endif
542
#endif // CYGPKG_IO_SERIAL_MN10300_SERIAL2
543
 
544
//-------------------------------------------------------------------------
545
// And finally, the device table entries:
546
 
547
#ifndef CYGPKG_HAL_MN10300_AM31_STDEVAL1
548
#ifdef CYGPKG_IO_SERIAL_MN10300_SERIAL0
549
// On the standard eval board serial0 is not connected. If enabled, it
550
// generates continuous frame error and overrun interrupts. Hence we do
551
// not touch it.
552
DEVTAB_ENTRY(mn10300_serial_io0,
553
             CYGDAT_IO_SERIAL_MN10300_SERIAL0_NAME,
554
             0,                     // Does not depend on a lower level interface
555
             &cyg_io_serial_devio,
556
             mn10300_serial_init,
557
             mn10300_serial_lookup,     // Serial driver may need initializing
558
             &mn10300_serial_channel0
559
    );
560
#endif // CYGPKG_IO_SERIAL_MN10300_SERIAL0
561
#endif
562
 
563
#ifdef CYGPKG_IO_SERIAL_MN10300_SERIAL1
564
DEVTAB_ENTRY(mn10300_serial_io1,
565
             CYGDAT_IO_SERIAL_MN10300_SERIAL1_NAME,
566
             0,                     // Does not depend on a lower level interface
567
             &cyg_io_serial_devio,
568
             mn10300_serial_init,
569
             mn10300_serial_lookup,     // Serial driver may need initializing
570
             &mn10300_serial_channel1
571
    );
572
#endif // CYGPKG_IO_SERIAL_MN10300_SERIAL1
573
 
574
#ifdef CYGPKG_IO_SERIAL_MN10300_SERIAL2
575
DEVTAB_ENTRY(mn10300_serial_io2,
576
             CYGDAT_IO_SERIAL_MN10300_SERIAL2_NAME,
577
             0,                     // Does not depend on a lower level interface
578
             &cyg_io_serial_devio,
579
             mn10300_serial_init,
580
             mn10300_serial_lookup,     // Serial driver may need initializing
581
             &mn10300_serial_channel2
582
    );
583
#endif // CYGPKG_IO_SERIAL_MN10300_SERIAL2
584
 
585
//-------------------------------------------------------------------------
586
// Read the serial line's status register. Serial 2 has an 8 bit status
587
// register while serials 0 and 1 have 16 bit registers. This function
588
// uses the correct size access, but passes back a 16 bit quantity for
589
// both.
590
 
591
static cyg_uint16 mn10300_read_sr( mn10300_serial_info *mn10300_chan )
592
{
593
    cyg_uint16 sr = 0;
594
    if( mn10300_chan->is_serial2 )
595
    {
596
        cyg_uint8 sr8;
597
        HAL_READ_UINT8(mn10300_chan->base+SERIAL_STR, sr8);
598
        sr = sr8;
599
    }
600
    else
601
    {
602
        HAL_READ_UINT16(mn10300_chan->base+SERIAL_STR, sr);
603
    }
604
 
605
    return sr;
606
}
607
 
608
//-------------------------------------------------------------------------
609
 
610
static bool
611
mn10300_serial_config_port(serial_channel *chan, cyg_serial_info_t *new_config, bool init)
612
{
613
    mn10300_serial_info *mn10300_chan = (mn10300_serial_info *)chan->dev_priv;
614
    cyg_uint16 cr = 0;
615
    cyg_uint16 sr;
616
 
617
    // wait for the device to become quiescent. This could take some time
618
    // if the device had been transmitting at a low baud rate.
619
    do {
620
        sr = mn10300_read_sr(mn10300_chan);
621
    } while (sr & (SR_RXF|SR_TXF));
622
 
623
    // Disable device entirely.
624
    HAL_WRITE_UINT16(mn10300_chan->base+SERIAL_CTR, 0);
625
 
626
    // Set up the Interrupt Mode Register
627
    HAL_WRITE_UINT8(mn10300_chan->base+SERIAL_ICR, 0);
628
 
629
    // Set up baud rate
630
    if( mn10300_chan->is_serial2 )
631
    {
632
        // Serial 2 is a bit different from 0 and 1 in the way that the
633
        // baud rate is controlled.
634
 
635
        cyg_uint8 baud_divisor = select_baud_2[new_config->baud].timer2_val;
636
 
637
        if (baud_divisor == 0)
638
            return false; // Invalid baud rate selected
639
 
640
        HAL_WRITE_UINT8(mn10300_chan->timer_base+TIMER_BR, baud_divisor);
641
 
642
        HAL_WRITE_UINT8(mn10300_chan->timer_base+TIMER_MD, 0x80 );
643
 
644
        baud_divisor = select_baud_2[new_config->baud].serial2_val;
645
 
646
        HAL_WRITE_UINT8(mn10300_chan->base+SERIAL_TIM, baud_divisor);
647
 
648
        cr |= mn10300_chan->timer_select;
649
    }
650
    else
651
    {
652
        cyg_uint16 baud_divisor = select_baud_01[new_config->baud];
653
        cyg_uint8 timer_mode = 0x80;
654
 
655
        if (baud_divisor == 0)
656
            return false; // Invalid baud rate selected
657
 
658
#if defined(CYGPKG_HAL_MN10300_AM33)        
659
        if( baud_divisor > 255 )
660
        {
661
            // The AM33 runs at a higher clock rate than the AM31 and
662
            // needs a bigger divisor for low baud rates. We do this by
663
            // using timer 0 as a prescaler. We set it to 198 so we can then
664
            // use it to prescale for both serial0 and serial1 if they need
665
            // it.
666
            static int timer0_initialized = 0;
667
            baud_divisor /= 198;
668
            baud_divisor--;
669
            timer_mode = 0x84;
670
            if( !timer0_initialized )
671
            {
672
                timer0_initialized = 1;
673
                HAL_WRITE_UINT8(HW_TIMER0+TIMER_BR, 198 );
674
                HAL_WRITE_UINT8(HW_TIMER0+TIMER_MD, 0x80 );
675
            }
676
        }
677
#endif
678
 
679
        HAL_WRITE_UINT8(mn10300_chan->timer_base+TIMER_BR, baud_divisor);
680
 
681
        HAL_WRITE_UINT8(mn10300_chan->timer_base+TIMER_MD, timer_mode );
682
 
683
        cr |= mn10300_chan->timer_select;
684
    }
685
 
686
#ifdef PORT3_MD    
687
    HAL_WRITE_UINT8( PORT3_MD, 0x01 );
688
#endif
689
 
690
    // set up other config values:
691
 
692
    cr |= select_word_length[new_config->word_length - CYGNUM_SERIAL_WORD_LENGTH_5];
693
    cr |= select_stop_bits[new_config->stop];
694
    cr |= select_parity[new_config->parity];
695
 
696
    cr |= LCR_RXE | LCR_TXE;        // enable Rx and Tx
697
 
698
#ifdef CYGPKG_HAL_MN10300_AM31
699
    if( mn10300_chan->is_serial2 )
700
    {
701
        // AM31 has an extra TX interrupt enable bit for serial 2.
702
        DISABLE_TRANSMIT_INTERRUPT(mn10300_chan);
703
    }
704
#endif        
705
 
706
    // Write CR into hardware
707
    HAL_WRITE_UINT16(mn10300_chan->base+SERIAL_CTR, cr);
708
 
709
    sr = mn10300_read_sr(mn10300_chan);
710
 
711
    if (new_config != &chan->config) {
712
        chan->config = *new_config;
713
    }
714
    return true;
715
}
716
 
717
//-------------------------------------------------------------------------
718
// Function to initialize the device.  Called at bootstrap time.
719
 
720
bool mn10300_serial_init(struct cyg_devtab_entry *tab)
721
{
722
    serial_channel *chan = (serial_channel *)tab->priv;
723
    mn10300_serial_info *mn10300_chan = (mn10300_serial_info *)chan->dev_priv;
724
 
725
    (chan->callbacks->serial_init)(chan);  // Really only required for interrupt driven devices
726
 
727
#ifndef CYGPKG_IO_SERIAL_MN10300_POLLED_MODE    
728
    if (chan->out_cbuf.len != 0) {
729
        // Install and enable the receive interrupt
730
        cyg_drv_interrupt_create(mn10300_chan->rx_int,
731
                                 4,                      // Priority - what goes here?
732
                                 (cyg_addrword_t)chan,   //  Data item passed to interrupt handler
733
                                 mn10300_serial_rx_ISR,
734
                                 mn10300_serial_rx_DSR,
735
                                 &mn10300_chan->rx_interrupt_handle,
736
                                 &mn10300_chan->rx_interrupt);
737
        cyg_drv_interrupt_attach(mn10300_chan->rx_interrupt_handle);
738
        cyg_drv_interrupt_unmask(mn10300_chan->rx_int);
739
 
740
        // Install and enable the transmit interrupt
741
        cyg_drv_interrupt_create(mn10300_chan->tx_int,
742
                                 4,                      // Priority - what goes here?
743
                                 (cyg_addrword_t)chan,   //  Data item passed to interrupt handler
744
                                 mn10300_serial_tx_ISR,
745
                                 mn10300_serial_tx_DSR,
746
                                 &mn10300_chan->tx_interrupt_handle,
747
                                 &mn10300_chan->tx_interrupt);
748
        cyg_drv_interrupt_attach(mn10300_chan->tx_interrupt_handle);
749
        cyg_drv_interrupt_mask(mn10300_chan->tx_int);
750
    }
751
#endif
752
 
753
    mn10300_serial_config_port(chan, &chan->config, true);
754
 
755
    return true;
756
}
757
 
758
//-------------------------------------------------------------------------
759
// This routine is called when the device is "looked" up (i.e. attached)
760
 
761
static Cyg_ErrNo
762
mn10300_serial_lookup(struct cyg_devtab_entry **tab,
763
                  struct cyg_devtab_entry *sub_tab,
764
                  const char *name)
765
{
766
    serial_channel *chan = (serial_channel *)(*tab)->priv;
767
    (chan->callbacks->serial_init)(chan);  // Really only required for interrupt driven devices
768
    return ENOERR;
769
}
770
 
771
//-------------------------------------------------------------------------
772
// Return 'true' if character is sent to device
773
 
774
bool
775
mn10300_serial_putc(serial_channel *chan, unsigned char c)
776
{
777
    mn10300_serial_info *mn10300_chan = (mn10300_serial_info *)chan->dev_priv;
778
    cyg_uint8 sr = mn10300_read_sr( mn10300_chan);
779
 
780
    if( (sr & SR_TBF) == 0 )
781
    {
782
        HAL_WRITE_UINT8( mn10300_chan->base+SERIAL_TXB, c );
783
 
784
        return true;
785
    }
786
    else return false;
787
}
788
 
789
//-------------------------------------------------------------------------
790
 
791
unsigned char
792
mn10300_serial_getc(serial_channel *chan)
793
{
794
    unsigned char c;
795
    mn10300_serial_info *mn10300_chan = (mn10300_serial_info *)chan->dev_priv;
796
    do
797
    {
798
        cyg_uint8 sr = mn10300_read_sr( mn10300_chan );
799
 
800
        if( (sr & SR_RBF) != 0 )
801
        {
802
            HAL_READ_UINT8( mn10300_chan->base+SERIAL_RXB, c );
803
 
804
            break;
805
        }
806
 
807
    } while(1);
808
 
809
    return c;
810
}
811
 
812
//-------------------------------------------------------------------------
813
 
814
static Cyg_ErrNo
815
mn10300_serial_set_config(serial_channel *chan, cyg_uint32 key,
816
                          const void *xbuf, cyg_uint32 *len)
817
{
818
    switch (key) {
819
    case CYG_IO_SET_CONFIG_SERIAL_INFO:
820
      {
821
        cyg_serial_info_t *config = (cyg_serial_info_t *)xbuf;
822
        if ( *len < sizeof(cyg_serial_info_t) ) {
823
            return -EINVAL;
824
        }
825
        *len = sizeof(cyg_serial_info_t);
826
        if ( true != mn10300_serial_config_port(chan, config, false) )
827
            return -EINVAL;
828
      }
829
      break;
830
    default:
831
        return -EINVAL;
832
    }
833
    return ENOERR;
834
}
835
 
836
//-------------------------------------------------------------------------
837
// Enable the transmitter on the device
838
 
839
static void
840
mn10300_serial_start_xmit(serial_channel *chan)
841
{
842
#ifndef CYGPKG_IO_SERIAL_MN10300_POLLED_MODE    
843
    mn10300_serial_info *mn10300_chan = (mn10300_serial_info *)chan->dev_priv;
844
    cyg_uint16 cr;
845
 
846
    HAL_READ_UINT16( mn10300_chan->base+SERIAL_CTR, cr );
847
 
848
    ENABLE_TRANSMIT_INTERRUPT(mn10300_chan);
849
 
850
    HAL_WRITE_UINT16( mn10300_chan->base+SERIAL_CTR, cr );
851
 
852
    cyg_drv_interrupt_unmask(mn10300_chan->tx_int);
853
 
854
    (chan->callbacks->xmt_char)(chan);
855
#endif    
856
}
857
 
858
//-------------------------------------------------------------------------
859
// Disable the transmitter on the device
860
 
861
static void
862
mn10300_serial_stop_xmit(serial_channel *chan)
863
{
864
#ifndef CYGPKG_IO_SERIAL_MN10300_POLLED_MODE    
865
    mn10300_serial_info *mn10300_chan = (mn10300_serial_info *)chan->dev_priv;
866
    cyg_uint16 cr;
867
    cyg_uint16 sr;
868
 
869
    // Wait until the transmitter has actually stopped before turning it off.
870
 
871
    do
872
    {
873
        sr = mn10300_read_sr( mn10300_chan );
874
 
875
    } while( sr & SR_TXF );
876
 
877
    HAL_READ_UINT16( mn10300_chan->base+SERIAL_CTR, cr );
878
 
879
    DISABLE_TRANSMIT_INTERRUPT(mn10300_chan);
880
 
881
    HAL_WRITE_UINT16( mn10300_chan->base+SERIAL_CTR, cr );
882
 
883
    cyg_drv_interrupt_mask(mn10300_chan->tx_int);
884
 
885
#endif    
886
}
887
 
888
//-------------------------------------------------------------------------
889
// Serial I/O - low level interrupt handlers (ISR)
890
 
891
#ifndef CYGPKG_IO_SERIAL_MN10300_POLLED_MODE
892
 
893
#ifdef CYG_HAL_MN10300_SERIAL_RX_FIFO
894
 
895
// This version of the RX ISR implements a simple receive FIFO. The
896
// MN10300 serial devices do not have hardware FIFOs (as found in
897
// 16550s for example), and it can be difficult at times to keep up
898
// with higher baud rates without overrunning. This ISR implements a
899
// software equivalent of the hardware FIFO, placing recieved
900
// characters into the FIFO as soon as they arrive. Whenever the DSR
901
// is run, it collects all the pending characters from the FIFO for
902
// delivery to the application. Neither the ISR or DSR disable
903
// interrupts, instead we rely on being able to write the head and
904
// tail pointers atomically, to implement lock-free synchronization.
905
 
906
static cyg_uint32
907
mn10300_serial_rx_ISR(cyg_vector_t vector, cyg_addrword_t data)
908
{
909
    serial_channel *chan = (serial_channel *)data;
910
    mn10300_serial_info *mn10300_chan = (mn10300_serial_info *)chan->dev_priv;
911
    cyg_uint8 sr = mn10300_read_sr( mn10300_chan);
912
 
913
    while( (sr & SR_RBF) != 0 )
914
    {
915
        register cyg_int32 head = mn10300_chan->fifo_head;
916
        cyg_uint8 c;
917
        int i;
918
        HAL_READ_UINT8( mn10300_chan->base+SERIAL_RXB, c );
919
 
920
        mn10300_chan->fifo[head++] = c;
921
 
922
        if( head >= sizeof(mn10300_chan->fifo) )
923
            head = 0;
924
 
925
        mn10300_chan->fifo_head = head;
926
 
927
        sr = mn10300_read_sr( mn10300_chan);
928
 
929
    }
930
 
931
    cyg_drv_interrupt_acknowledge(mn10300_chan->rx_int);
932
 
933
    return CYG_ISR_CALL_DSR;  // Cause DSR to be run
934
}
935
 
936
#else
937
 
938
static cyg_uint32
939
mn10300_serial_rx_ISR(cyg_vector_t vector, cyg_addrword_t data)
940
{
941
    serial_channel *chan = (serial_channel *)data;
942
    mn10300_serial_info *mn10300_chan = (mn10300_serial_info *)chan->dev_priv;
943
 
944
    cyg_drv_interrupt_mask(mn10300_chan->rx_int);
945
    cyg_drv_interrupt_acknowledge(mn10300_chan->rx_int);
946
 
947
    return CYG_ISR_CALL_DSR;  // Cause DSR to be run
948
}
949
 
950
#endif
951
 
952
static cyg_uint32
953
mn10300_serial_tx_ISR(cyg_vector_t vector, cyg_addrword_t data)
954
{
955
    serial_channel *chan = (serial_channel *)data;
956
    mn10300_serial_info *mn10300_chan = (mn10300_serial_info *)chan->dev_priv;
957
 
958
    cyg_drv_interrupt_mask(mn10300_chan->tx_int);
959
    cyg_drv_interrupt_acknowledge(mn10300_chan->tx_int);
960
 
961
    return CYG_ISR_CALL_DSR;  // Cause DSR to be run
962
}
963
 
964
#endif
965
 
966
//-------------------------------------------------------------------------
967
// Serial I/O - high level interrupt handler (DSR)
968
 
969
#ifndef CYGPKG_IO_SERIAL_MN10300_POLLED_MODE
970
 
971
#ifdef CYG_HAL_MN10300_SERIAL_RX_FIFO
972
 
973
static void
974
mn10300_serial_rx_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
975
{
976
    serial_channel *chan = (serial_channel *)data;
977
    mn10300_serial_info *mn10300_chan = (mn10300_serial_info *)chan->dev_priv;
978
    register cyg_int32 head = mn10300_chan->fifo_head;
979
    register cyg_int32 tail = mn10300_chan->fifo_tail;
980
 
981
    while( head != tail )
982
    {
983
        cyg_uint8 c = mn10300_chan->fifo[tail++];
984
 
985
        if( tail >= sizeof(mn10300_chan->fifo) ) tail = 0;
986
 
987
        (chan->callbacks->rcv_char)(chan, c);
988
    }
989
 
990
    mn10300_chan->fifo_tail = tail;
991
}
992
 
993
#else
994
 
995
static void
996
mn10300_serial_rx_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
997
{
998
    serial_channel *chan = (serial_channel *)data;
999
    mn10300_serial_info *mn10300_chan = (mn10300_serial_info *)chan->dev_priv;
1000
    cyg_uint8 sr = mn10300_read_sr( mn10300_chan);
1001
 
1002
    if( (sr & SR_RBF) != 0 )
1003
    {
1004
        cyg_uint8 rxb;
1005
        HAL_READ_UINT8( mn10300_chan->base+SERIAL_RXB, rxb );
1006
 
1007
        (chan->callbacks->rcv_char)(chan, rxb);
1008
    }
1009
 
1010
    cyg_drv_interrupt_unmask(mn10300_chan->rx_int);
1011
}
1012
 
1013
#endif
1014
 
1015
static void
1016
mn10300_serial_tx_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
1017
{
1018
    serial_channel *chan = (serial_channel *)data;
1019
    mn10300_serial_info *mn10300_chan = (mn10300_serial_info *)chan->dev_priv;
1020
    cyg_uint8 sr = mn10300_read_sr( mn10300_chan);
1021
 
1022
    if( (sr & SR_TBF) == 0 )
1023
    {
1024
        (chan->callbacks->xmt_char)(chan);
1025
    }
1026
 
1027
    cyg_drv_interrupt_unmask(mn10300_chan->tx_int);
1028
}
1029
 
1030
#endif
1031
 
1032
#endif // CYGPKG_IO_SERIAL_MN10300
1033
 
1034
//-------------------------------------------------------------------------
1035
// EOF mn10300.c

powered by: WebSVN 2.1.0

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