Subversion Repositories shark

Rev

Rev 732 | Rev 736 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
714 giacomo 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/*
20
 * Copyright (C) 2000 Michele Cirinei
21
 *
22
 * This program is free software; you can redistribute it and/or modify
23
 * it under the terms of the GNU General Public License as published by
24
 * the Free Software Foundation; either version 2 of the License, or
25
 * (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU General Public License
33
 * along with this program; if not, write to the Free Software
34
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
35
 */
36
 
37
#include <kernel/kern.h>
38
#include <drivers/scomirq.h>
39
 
40
/****************************
41
 Variables
42
****************************/
43
static unsigned com_irq[] = {COM1_IRQ, COM2_IRQ, COM3_IRQ, COM4_IRQ};
44
 
45
struct SCom_Access_Type SCom_Access;
46
 
729 giacomo 47
unsigned SCom_Error[4];
714 giacomo 48
 
49
struct TX_data_t{
50
    int ptr;
51
    int len;
52
    BYTE buf[100];
53
};
54
 
729 giacomo 55
static struct TX_data_t SCom_TX_data[4];
714 giacomo 56
 
57
/*****************************
58
* Functions
59
*****************************/
60
 
61
void RBRF_handler(unsigned port);
62
 
63
void THRE_handler(unsigned port);
64
 
65
void LSR_handler(unsigned port);
66
 
67
void com_send_msg(unsigned port, BYTE len, BYTE *m);
68
 
69
void com_irq_sel(int no)
70
{
735 giacomo 71
    BYTE b, v;
714 giacomo 72
    unsigned i;
735 giacomo 73
 
714 giacomo 74
    for(i=0;i<4;i++)
75
      if (com_irq[i] == no) break;
76
 
735 giacomo 77
    while(PENDIRQ(v = com_read(i, IIR))) {
714 giacomo 78
 
735 giacomo 79
      b = DECODE(v);
80
 
81
      switch (b)
82
      {
714 giacomo 83
        case LS_CHANGED:
84
            LSR_handler(i);
85
        break;
86
 
87
        case RX_FULL:
88
            RBRF_handler(i);
89
        break;
90
 
91
        case TX_EMPTY:
92
            THRE_handler(i);
93
        break;
735 giacomo 94
      }
95
 
714 giacomo 96
    }
735 giacomo 97
 
714 giacomo 98
}      
99
 
100
void com_irq_enable(unsigned port, unsigned irq)
101
{
102
    SYS_FLAGS f;
721 giacomo 103
 
714 giacomo 104
    f = kern_fsave();
105
 
106
    com_read(port, LSR);
107
    com_read(port, IIR);
108
    com_write(port, IER, com_read(port, IER) | irq); /* Enable  irq */
109
 
110
    kern_frestore(f);
111
 
112
}
113
 
114
void com_irq_disable(unsigned port, unsigned irq)
115
{
116
 
117
    SYS_FLAGS f;
118
 
119
    f = kern_fsave();
120
 
121
    com_read(port, LSR);
122
    com_read(port, IIR);
123
    com_write(port, IER, com_read(port, IER) & ~irq); /* Disable irq */
124
 
125
    kern_frestore(f);
126
 
127
}
128
 
716 giacomo 129
void com_close_irq(unsigned port)
714 giacomo 130
{
131
    SYS_FLAGS f;
132
 
133
    f = kern_fsave();
134
 
135
    com_write(port, IER, 0);
136
    com_read(port, LSR);
137
    com_read(port, IIR);
138
 
716 giacomo 139
    handler_remove(com_irq[port]);
140
 
714 giacomo 141
    kern_frestore(f);
142
 
143
}
144
 
716 giacomo 145
void com_init_irq(unsigned port)
714 giacomo 146
{
147
    SYS_FLAGS f;
148
 
149
    f = kern_fsave();
150
 
729 giacomo 151
    SCom_Error[port] = 0;
714 giacomo 152
    handler_set(com_irq[port], com_irq_sel, NIL, TRUE);
153
    com_irq_disable(port, ALL_IRQ);     /* Disable interrupts */
154
 
155
    kern_frestore(f);
156
 
157
}
158
 
159
void com_set_functions(void (*confirm)(unsigned port, BYTE msg_status),void (*indication)(unsigned port, BYTE data))
160
{
161
    SYS_FLAGS f;
162
 
163
    f = kern_fsave();
164
 
165
    SCom_Access.confirm    = confirm;
166
    SCom_Access.request    = com_send_msg;
167
    SCom_Access.indication = indication;
168
 
169
    kern_frestore(f);
170
 
171
}
172
 
173
void com_send_msg(unsigned port, BYTE len, BYTE *m)
174
{
175
    SYS_FLAGS f;
176
 
177
    f = kern_fsave();
178
 
179
    /* Transmit first byte of message  */
729 giacomo 180
    SCom_TX_data[port].ptr = 0;
181
    SCom_TX_data[port].len = len;
714 giacomo 182
    if (len < 100) {
729 giacomo 183
      memcpy(SCom_TX_data[port].buf, m, len);
184
      com_write(port, THR, SCom_TX_data[port].buf[SCom_TX_data[port].ptr++]);
714 giacomo 185
    }
186
 
187
    kern_frestore(f);
188
 
189
}
190
 
716 giacomo 191
__inline__ void com_irq_send(unsigned port, BYTE len, BYTE *m) {
192
 
193
  com_send_msg(port, len, m);
194
 
195
}
196
 
714 giacomo 197
void THRE_handler (unsigned port) /* Transmit Data Register Ready for next byte */
730 giacomo 198
{
735 giacomo 199
    if (SCom_TX_data[port].ptr < SCom_TX_data[port].len) {
200
        com_write(port, THR, SCom_TX_data[port].buf[SCom_TX_data[port].ptr++]);
201
    } else {
202
        SCom_Access.confirm(port, COM_OK);
203
    }
204
 
714 giacomo 205
}
206
 
207
void RBRF_handler (unsigned port) /* Receive Data Register Full */
208
{
727 giacomo 209
    SCom_Access.indication(port, com_read(port, RBR));
714 giacomo 210
}
211
 
212
void LSR_handler (unsigned port)    /* break = 8, frame = 4, parity = 2, RBR overrun = 1*/
213
{
729 giacomo 214
    SCom_Error[port] = (com_read(port, LSR) & (bit4|bit3|bit2|bit1) ) >> 1;
735 giacomo 215
    SCom_Access.confirm(port, COM_ERROR);
714 giacomo 216
}
217