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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [hal/] [sh/] [sh4/] [v2_0/] [src/] [sh4_scif.c] - Blame information for rev 773

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//=============================================================================
2
//
3
//      sh4_scif.c
4
//
5
//      Simple driver for the SH4 Serial Communication Interface with FIFO
6
//
7
//=============================================================================
8
//####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under
14
// the terms of the GNU General Public License as published by the Free
15
// Software Foundation; either version 2 or (at your option) any later version.
16
//
17
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20
// for more details.
21
//
22
// You should have received a copy of the GNU General Public License along
23
// with eCos; if not, write to the Free Software Foundation, Inc.,
24
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25
//
26
// As a special exception, if other files instantiate templates or use macros
27
// or inline functions from this file, or you compile this file and link it
28
// with other works to produce a work based on this file, this file does not
29
// by itself cause the resulting work to be covered by the GNU General Public
30
// License. However the source code for this file must still be made available
31
// in accordance with section (3) of the GNU General Public License.
32
//
33
// This exception does not invalidate any other reasons why a work based on
34
// this file might be covered by the GNU General Public License.
35
//
36
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37
// at http://sources.redhat.com/ecos/ecos-license/
38
// -------------------------------------------
39
//####ECOSGPLCOPYRIGHTEND####
40
//=============================================================================
41
//#####DESCRIPTIONBEGIN####
42
//
43
// Author(s):   jskov
44
// Contributors:Haruki Kashiwaya
45
// Date:        2000-08-09
46
// Description: Simple driver for the SH Serial Communication Interface
47
//              The driver can be used for the SCIF modules.
48
//              Clients of this file can configure the behavior with:
49
//              CYGNUM_SCIF_PORTS: number of SCI ports
50
//
51
//####DESCRIPTIONEND####
52
//
53
//=============================================================================
54
 
55
#include <pkgconf/hal.h>
56
 
57
#ifdef CYGNUM_HAL_SH_SH4_SCIF_PORTS
58
 
59
#include <cyg/hal/hal_io.h>             // IO macros
60
#include <cyg/hal/drv_api.h>            // CYG_ISR_HANDLED
61
#include <cyg/hal/hal_misc.h>           // Helper functions
62
#include <cyg/hal/hal_intr.h>           // HAL_ENABLE/MASK/UNMASK_INTERRUPTS
63
#include <cyg/hal/hal_arch.h>           // SAVE/RESTORE GP
64
#include <cyg/hal/hal_if.h>             // Calling-if API
65
#include <cyg/hal/sh_regs.h>            // serial register definitions
66
#include <cyg/hal/sh_stub.h>            // target_register_t
67
 
68
#define CYGPRI_HAL_SH_SH4_SCIF_PRIVATE
69
#include <cyg/hal/sh4_scif.h>           // our header
70
 
71
//--------------------------------------------------------------------------
72
 
73
void
74
cyg_hal_plf_scif_init_channel(channel_data_t* chan)
75
{
76
    cyg_uint8* base = chan->base;
77
    cyg_uint16 tmp;
78
    cyg_uint16 sr;
79
 
80
    // Disable everything.
81
    HAL_WRITE_UINT16(base+_REG_SCSCR, 0);
82
 
83
    // Reset FIFO.
84
    HAL_WRITE_UINT16(base+_REG_SCFCR,
85
                    CYGARC_REG_SCIF_SCFCR_TFRST|CYGARC_REG_SCIF_SCFCR_RFRST);
86
 
87
    // 8-1-no parity.
88
    HAL_WRITE_UINT16(base+_REG_SCSMR, 0);
89
 
90
    // Set desired baudrate
91
    HAL_READ_UINT16(base+_REG_SCSMR, tmp);
92
    tmp &= ~CYGARC_REG_SCIF_SCSMR_CKSx_MASK;
93
    tmp |= CYGARC_SCBRR_CKSx(CYGNUM_HAL_SH_SH4_SCIF_BAUD_RATE);
94
    HAL_WRITE_UINT16(base+_REG_SCSMR, tmp);
95
    HAL_WRITE_UINT8(base+_REG_SCBRR,
96
                    CYGARC_SCBRR_N(CYGNUM_HAL_SH_SH4_SCIF_BAUD_RATE));
97
 
98
    // Let things settle: Here we should should wait the equivalent of
99
    // one bit interval, i.e. 1/<baudrate> second, but until we have
100
    // something like the Linux delay loop, it's hard to do reliably. So
101
    // just move on and hope for the best (this is unlikely to cause
102
    // problems since the CPU has just come out of reset anyway).
103
 
104
    // Clear status register (read back first).
105
    HAL_READ_UINT16(base+_REG_SCFSR, sr);
106
    HAL_WRITE_UINT16(base+_REG_SCFSR, 0);
107
 
108
    // Bring FIFO out of reset and set to trigger on every char in
109
    // FIFO (or C-c input would not be processed).
110
    HAL_WRITE_UINT16(base+_REG_SCFCR,
111
                    CYGARC_REG_SCIF_SCFCR_RTRG_1|CYGARC_REG_SCIF_SCFCR_TTRG_1);
112
 
113
    // Leave Tx/Rx interrupts disabled, but enable Tx/Rx
114
    HAL_WRITE_UINT16(base+_REG_SCSCR,
115
                    CYGARC_REG_SCIF_SCSCR_TE|CYGARC_REG_SCIF_SCSCR_RE);
116
}
117
 
118
static cyg_bool
119
cyg_hal_plf_scif_getc_nonblock(void* __ch_data, cyg_uint8* ch)
120
{
121
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
122
    cyg_uint16 fdr, sr;
123
 
124
    HAL_READ_UINT16(base+_REG_SCFDR, fdr);
125
    if ((fdr & CYGARC_REG_SCIF_SCFDR_RCOUNT_MASK) == 0)
126
        return false;
127
 
128
    HAL_READ_UINT8(base+_REG_SCFRDR, *ch);
129
 
130
    // Clear FIFO full flag (read before clearing)
131
    HAL_READ_UINT16(base+_REG_SCFSR, sr);
132
    HAL_WRITE_UINT16(base+_REG_SCFSR,
133
                    CYGARC_REG_SCIF_SCSSR_CLEARMASK & ~CYGARC_REG_SCIF_SCSSR_RDF);
134
 
135
    return true;
136
}
137
 
138
cyg_uint8
139
cyg_hal_plf_scif_getc(void* __ch_data)
140
{
141
    cyg_uint8 ch;
142
    CYGARC_HAL_SAVE_GP();
143
 
144
    while(!cyg_hal_plf_scif_getc_nonblock(__ch_data, &ch));
145
 
146
    CYGARC_HAL_RESTORE_GP();
147
    return ch;
148
}
149
 
150
void
151
cyg_hal_plf_scif_putc(void* __ch_data, cyg_uint8 c)
152
{
153
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
154
    cyg_uint16 fdr, sr;
155
    CYGARC_HAL_SAVE_GP();
156
 
157
    do {
158
        HAL_READ_UINT16(base+_REG_SCFDR, fdr);
159
    } while (((fdr & CYGARC_REG_SCIF_SCFDR_TCOUNT_MASK) >> CYGARC_REG_SCIF_SCFDR_TCOUNT_shift) == 16);
160
 
161
    HAL_WRITE_UINT8(base+_REG_SCFTDR, c);
162
 
163
    // Clear FIFO-empty/transmit end flags (read back SR first)
164
    HAL_READ_UINT16(base+_REG_SCFSR, sr);
165
    HAL_WRITE_UINT16(base+_REG_SCFSR, CYGARC_REG_SCIF_SCSSR_CLEARMASK
166
                     & ~(CYGARC_REG_SCIF_SCSSR_TDFE | CYGARC_REG_SCIF_SCSSR_TEND ));
167
 
168
    // Hang around until the character has been safely sent.
169
    do {
170
        HAL_READ_UINT16(base+_REG_SCFDR, fdr);
171
    } while ((fdr & CYGARC_REG_SCIF_SCFDR_TCOUNT_MASK) != 0);
172
 
173
    CYGARC_HAL_RESTORE_GP();
174
}
175
 
176
 
177
static channel_data_t channels[CYGNUM_HAL_SH_SH4_SCIF_PORTS];
178
 
179
static void
180
cyg_hal_plf_scif_write(void* __ch_data, const cyg_uint8* __buf,
181
                         cyg_uint32 __len)
182
{
183
    CYGARC_HAL_SAVE_GP();
184
 
185
    while(__len-- > 0)
186
        cyg_hal_plf_scif_putc(__ch_data, *__buf++);
187
 
188
    CYGARC_HAL_RESTORE_GP();
189
}
190
 
191
static void
192
cyg_hal_plf_scif_read(void* __ch_data, cyg_uint8* __buf, cyg_uint32 __len)
193
{
194
    CYGARC_HAL_SAVE_GP();
195
 
196
    while(__len-- > 0)
197
        *__buf++ = cyg_hal_plf_scif_getc(__ch_data);
198
 
199
    CYGARC_HAL_RESTORE_GP();
200
}
201
 
202
cyg_bool
203
cyg_hal_plf_scif_getc_timeout(void* __ch_data, cyg_uint8* ch)
204
{
205
    channel_data_t* chan = (channel_data_t*)__ch_data;
206
    int delay_count;
207
    cyg_bool res;
208
    CYGARC_HAL_SAVE_GP();
209
 
210
    delay_count = chan->msec_timeout * 10; // delay in .1 ms steps
211
 
212
    for(;;) {
213
        res = cyg_hal_plf_scif_getc_nonblock(__ch_data, ch);
214
        if (res || 0 == delay_count--)
215
            break;
216
 
217
        CYGACC_CALL_IF_DELAY_US(100);
218
    }
219
 
220
    CYGARC_HAL_RESTORE_GP();
221
    return res;
222
}
223
 
224
static int
225
cyg_hal_plf_scif_control(void *__ch_data, __comm_control_cmd_t __func, ...)
226
{
227
    static int irq_state = 0;
228
    channel_data_t* chan = (channel_data_t*)__ch_data;
229
    cyg_uint8 scr;
230
    int ret = 0;
231
    CYGARC_HAL_SAVE_GP();
232
 
233
    switch (__func) {
234
    case __COMMCTL_IRQ_ENABLE:
235
        irq_state = 1;
236
        HAL_INTERRUPT_UNMASK(chan->isr_vector);
237
        HAL_READ_UINT16(chan->base+_REG_SCSCR, scr);
238
        scr |= CYGARC_REG_SCIF_SCSCR_RIE;
239
        HAL_WRITE_UINT16(chan->base+_REG_SCSCR, scr);
240
        break;
241
    case __COMMCTL_IRQ_DISABLE:
242
        ret = irq_state;
243
        irq_state = 0;
244
        HAL_INTERRUPT_UNMASK(chan->isr_vector);
245
        HAL_READ_UINT16(chan->base+_REG_SCSCR, scr);
246
        scr &= ~CYGARC_REG_SCIF_SCSCR_RIE;
247
        HAL_WRITE_UINT16(chan->base+_REG_SCSCR, scr);
248
        break;
249
    case __COMMCTL_DBG_ISR_VECTOR:
250
        ret = chan->isr_vector;
251
        break;
252
    case __COMMCTL_SET_TIMEOUT:
253
    {
254
        va_list ap;
255
 
256
        va_start(ap, __func);
257
 
258
        ret = chan->msec_timeout;
259
        chan->msec_timeout = va_arg(ap, cyg_uint32);
260
 
261
        va_end(ap);
262
    }
263
    default:
264
        break;
265
    }
266
    CYGARC_HAL_RESTORE_GP();
267
    return ret;
268
}
269
 
270
static int
271
cyg_hal_plf_scif_isr(void *__ch_data, int* __ctrlc,
272
                     CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
273
{
274
    cyg_uint8 c;
275
    cyg_uint16 fdr, sr;
276
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
277
    int res = 0;
278
    CYGARC_HAL_SAVE_GP();
279
 
280
    *__ctrlc = 0;
281
    HAL_READ_UINT16(base+_REG_SCFDR, fdr);
282
    if ((fdr & CYGARC_REG_SCIF_SCFDR_RCOUNT_MASK) != 0) {
283
        HAL_READ_UINT8(base+_REG_SCFRDR, c);
284
 
285
        // Clear buffer full flag (read back first).
286
        HAL_READ_UINT16(base+_REG_SCFSR, sr);
287
        HAL_WRITE_UINT16(base+_REG_SCFSR,
288
                         CYGARC_REG_SCIF_SCSSR_CLEARMASK & ~CYGARC_REG_SCIF_SCSSR_RDF);
289
 
290
        if( cyg_hal_is_break( &c , 1 ) )
291
            *__ctrlc = 1;
292
 
293
        res = CYG_ISR_HANDLED;
294
    }
295
 
296
    CYGARC_HAL_RESTORE_GP();
297
    return res;
298
}
299
 
300
void
301
cyg_hal_plf_scif_init(int scif_index, int comm_index,
302
                      int rcv_vect, cyg_uint8* base)
303
{
304
    channel_data_t* chan = &channels[scif_index];
305
    hal_virtual_comm_table_t* comm;
306
    int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
307
 
308
    // Initialize channel table
309
    chan->base = base;
310
    chan->isr_vector = rcv_vect;
311
    chan->msec_timeout = 1000;
312
 
313
    // Disable interrupts.
314
    HAL_INTERRUPT_MASK(chan->isr_vector);
315
 
316
    // Init channel
317
    cyg_hal_plf_scif_init_channel(chan);
318
 
319
    // Setup procs in the vector table
320
 
321
    // Initialize channel procs
322
    CYGACC_CALL_IF_SET_CONSOLE_COMM(comm_index);
323
    comm = CYGACC_CALL_IF_CONSOLE_PROCS();
324
    CYGACC_COMM_IF_CH_DATA_SET(*comm, chan);
325
    CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_scif_write);
326
    CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_scif_read);
327
    CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_scif_putc);
328
    CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_scif_getc);
329
    CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_scif_control);
330
    CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_scif_isr);
331
    CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_scif_getc_timeout);
332
 
333
    // Restore original console
334
    CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
335
}
336
 
337
#endif // CYGNUM_HAL_SH_SH4_SCIF_PORTS
338
 
339
//-----------------------------------------------------------------------------
340
// end of sh4_scif.c

powered by: WebSVN 2.1.0

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